| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | |
| 23 | import android.content.res.Resources; |
| 24 | import android.os.Bundle; |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
| 29 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 30 | /** @deprecated renderscript is deprecated in J |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 31 | * Sampler object which defines how data is extracted from textures. Samplers |
| 32 | * are attached to Program objects (currently only ProgramFragment) when those objects |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 33 | * need to access texture data. |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 34 | **/ |
| 35 | public class Sampler extends BaseObj { |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 36 | /** @deprecated renderscript is deprecated in J |
| 37 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 38 | public enum Value { |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 39 | /** @deprecated renderscript is deprecated in J |
| 40 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 41 | NEAREST (0), |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 42 | /** @deprecated renderscript is deprecated in J |
| 43 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 44 | LINEAR (1), |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 45 | /** @deprecated renderscript is deprecated in J |
| 46 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 47 | LINEAR_MIP_LINEAR (2), |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 48 | /** @deprecated renderscript is deprecated in J |
| 49 | */ |
| Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 50 | LINEAR_MIP_NEAREST (5), |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 51 | /** @deprecated renderscript is deprecated in J |
| 52 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 53 | WRAP (3), |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 54 | /** @deprecated renderscript is deprecated in J |
| 55 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 56 | CLAMP (4); |
| 57 | |
| 58 | int mID; |
| 59 | Value(int id) { |
| 60 | mID = id; |
| 61 | } |
| 62 | } |
| 63 | |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 64 | Value mMin; |
| 65 | Value mMag; |
| 66 | Value mWrapS; |
| 67 | Value mWrapT; |
| 68 | Value mWrapR; |
| 69 | float mAniso; |
| 70 | |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 71 | Sampler(int id, RenderScript rs) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 72 | super(id, rs); |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 75 | /** @hide renderscript is deprecated in J |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 76 | * @return minification setting for the sampler |
| 77 | */ |
| 78 | public Value getMinification() { |
| 79 | return mMin; |
| 80 | } |
| 81 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 82 | /** @hide renderscript is deprecated in J |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 83 | * @return magnification setting for the sampler |
| 84 | */ |
| 85 | public Value getMagnification() { |
| 86 | return mMag; |
| 87 | } |
| 88 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 89 | /** @hide renderscript is deprecated in J |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 90 | * @return S wrapping mode for the sampler |
| 91 | */ |
| 92 | public Value getWrapS() { |
| 93 | return mWrapS; |
| 94 | } |
| 95 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 96 | /** @hide renderscript is deprecated in J |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 97 | * @return T wrapping mode for the sampler |
| 98 | */ |
| 99 | public Value getWrapT() { |
| 100 | return mWrapT; |
| 101 | } |
| 102 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 103 | /** @hide renderscript is deprecated in J |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 104 | * @return anisotropy setting for the sampler |
| 105 | */ |
| 106 | public float getAnisotropy() { |
| 107 | return mAniso; |
| 108 | } |
| 109 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 110 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 111 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 112 | * clamp. |
| 113 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 114 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 115 | * |
| 116 | * @return Sampler |
| 117 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 118 | public static Sampler CLAMP_NEAREST(RenderScript rs) { |
| 119 | if(rs.mSampler_CLAMP_NEAREST == null) { |
| 120 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 121 | b.setMinification(Value.NEAREST); |
| 122 | b.setMagnification(Value.NEAREST); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 123 | b.setWrapS(Value.CLAMP); |
| 124 | b.setWrapT(Value.CLAMP); |
| 125 | rs.mSampler_CLAMP_NEAREST = b.create(); |
| 126 | } |
| 127 | return rs.mSampler_CLAMP_NEAREST; |
| 128 | } |
| 129 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 130 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 131 | * Retrieve a sampler with min and mag set to linear and wrap modes set to |
| 132 | * clamp. |
| 133 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 134 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 135 | * |
| 136 | * @return Sampler |
| 137 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 138 | public static Sampler CLAMP_LINEAR(RenderScript rs) { |
| 139 | if(rs.mSampler_CLAMP_LINEAR == null) { |
| 140 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 141 | b.setMinification(Value.LINEAR); |
| 142 | b.setMagnification(Value.LINEAR); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 143 | b.setWrapS(Value.CLAMP); |
| 144 | b.setWrapT(Value.CLAMP); |
| 145 | rs.mSampler_CLAMP_LINEAR = b.create(); |
| 146 | } |
| 147 | return rs.mSampler_CLAMP_LINEAR; |
| 148 | } |
| 149 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 150 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 151 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 152 | * to and wrap modes set to clamp. |
| 153 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 154 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 155 | * |
| 156 | * @return Sampler |
| 157 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 158 | public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { |
| 159 | if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { |
| 160 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 161 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 162 | b.setMagnification(Value.LINEAR); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 163 | b.setWrapS(Value.CLAMP); |
| 164 | b.setWrapT(Value.CLAMP); |
| 165 | rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); |
| 166 | } |
| 167 | return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; |
| 168 | } |
| 169 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 170 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 171 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 172 | * wrap. |
| 173 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 174 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 175 | * |
| 176 | * @return Sampler |
| 177 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 178 | public static Sampler WRAP_NEAREST(RenderScript rs) { |
| 179 | if(rs.mSampler_WRAP_NEAREST == null) { |
| 180 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 181 | b.setMinification(Value.NEAREST); |
| 182 | b.setMagnification(Value.NEAREST); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 183 | b.setWrapS(Value.WRAP); |
| 184 | b.setWrapT(Value.WRAP); |
| 185 | rs.mSampler_WRAP_NEAREST = b.create(); |
| 186 | } |
| 187 | return rs.mSampler_WRAP_NEAREST; |
| 188 | } |
| 189 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 190 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 191 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 192 | * wrap. |
| 193 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 194 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 195 | * |
| 196 | * @return Sampler |
| 197 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 198 | public static Sampler WRAP_LINEAR(RenderScript rs) { |
| 199 | if(rs.mSampler_WRAP_LINEAR == null) { |
| 200 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 201 | b.setMinification(Value.LINEAR); |
| 202 | b.setMagnification(Value.LINEAR); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 203 | b.setWrapS(Value.WRAP); |
| 204 | b.setWrapT(Value.WRAP); |
| 205 | rs.mSampler_WRAP_LINEAR = b.create(); |
| 206 | } |
| 207 | return rs.mSampler_WRAP_LINEAR; |
| 208 | } |
| 209 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 210 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 211 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 212 | * to and wrap modes set to wrap. |
| 213 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 214 | * @param rs Context to which the sampler will belong. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 215 | * |
| 216 | * @return Sampler |
| 217 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 218 | public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { |
| 219 | if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { |
| 220 | Builder b = new Builder(rs); |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 221 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 222 | b.setMagnification(Value.LINEAR); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 223 | b.setWrapS(Value.WRAP); |
| 224 | b.setWrapT(Value.WRAP); |
| 225 | rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); |
| 226 | } |
| 227 | return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; |
| 228 | } |
| 229 | |
| 230 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 231 | /** @deprecated renderscript is deprecated in J |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 232 | * Builder for creating non-standard samplers. Usefull if mix and match of |
| 233 | * wrap modes is necesary or if anisotropic filtering is desired. |
| 234 | * |
| 235 | */ |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 236 | public static class Builder { |
| 237 | RenderScript mRS; |
| 238 | Value mMin; |
| 239 | Value mMag; |
| 240 | Value mWrapS; |
| 241 | Value mWrapT; |
| 242 | Value mWrapR; |
| Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 243 | float mAniso; |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 244 | |
| 245 | public Builder(RenderScript rs) { |
| 246 | mRS = rs; |
| 247 | mMin = Value.NEAREST; |
| 248 | mMag = Value.NEAREST; |
| 249 | mWrapS = Value.WRAP; |
| 250 | mWrapT = Value.WRAP; |
| 251 | mWrapR = Value.WRAP; |
| Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 252 | mAniso = 1.0f; |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 255 | public void setMinification(Value v) { |
| Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 256 | if (v == Value.NEAREST || |
| 257 | v == Value.LINEAR || |
| Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 258 | v == Value.LINEAR_MIP_LINEAR || |
| 259 | v == Value.LINEAR_MIP_NEAREST) { |
| Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 260 | mMin = v; |
| 261 | } else { |
| 262 | throw new IllegalArgumentException("Invalid value"); |
| 263 | } |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 266 | public void setMagnification(Value v) { |
| Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 267 | if (v == Value.NEAREST || v == Value.LINEAR) { |
| 268 | mMag = v; |
| 269 | } else { |
| 270 | throw new IllegalArgumentException("Invalid value"); |
| 271 | } |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | public void setWrapS(Value v) { |
| Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 275 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 276 | mWrapS = v; |
| 277 | } else { |
| 278 | throw new IllegalArgumentException("Invalid value"); |
| 279 | } |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | public void setWrapT(Value v) { |
| Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 283 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 284 | mWrapT = v; |
| 285 | } else { |
| 286 | throw new IllegalArgumentException("Invalid value"); |
| 287 | } |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 290 | public void setAnisotropy(float v) { |
| 291 | if(v >= 0.0f) { |
| 292 | mAniso = v; |
| 293 | } else { |
| 294 | throw new IllegalArgumentException("Invalid value"); |
| 295 | } |
| 296 | } |
| 297 | |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 298 | public Sampler create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 299 | mRS.validate(); |
| Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 300 | int id = mRS.nSamplerCreate(mMag.mID, mMin.mID, |
| Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 301 | mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso); |
| 302 | Sampler sampler = new Sampler(id, mRS); |
| 303 | sampler.mMin = mMin; |
| 304 | sampler.mMag = mMag; |
| 305 | sampler.mWrapS = mWrapS; |
| 306 | sampler.mWrapT = mWrapT; |
| 307 | sampler.mWrapR = mWrapR; |
| 308 | sampler.mAniso = mAniso; |
| 309 | return sampler; |
| Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | } |
| 314 | |