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; 25 import android.util.Log; 26 27 import android.graphics.Bitmap; 28 import android.graphics.BitmapFactory; 29 30 /** 31 * Sampler object which defines how data is extracted from textures. Samplers 32 * are attached to Program objects (currently only ProgramFragment) when those objects 33 * need to access texture data. 34 **/ 35 public class Sampler extends BaseObj { 36 public enum Value { 37 NEAREST (0), 38 LINEAR (1), 39 LINEAR_MIP_LINEAR (2), 40 LINEAR_MIP_NEAREST (5), 41 WRAP (3), 42 CLAMP (4), 43 MIRRORED_REPEAT (6); 44 45 int mID; Value(int id)46 Value(int id) { 47 mID = id; 48 } 49 } 50 51 Value mMin; 52 Value mMag; 53 Value mWrapS; 54 Value mWrapT; 55 Value mWrapR; 56 float mAniso; 57 Sampler(int id, RenderScript rs)58 Sampler(int id, RenderScript rs) { 59 super(id, rs); 60 } 61 62 /** 63 * @return minification setting for the sampler 64 */ getMinification()65 public Value getMinification() { 66 return mMin; 67 } 68 69 /** 70 * @return magnification setting for the sampler 71 */ getMagnification()72 public Value getMagnification() { 73 return mMag; 74 } 75 76 /** 77 * @return S wrapping mode for the sampler 78 */ getWrapS()79 public Value getWrapS() { 80 return mWrapS; 81 } 82 83 /** 84 * @return T wrapping mode for the sampler 85 */ getWrapT()86 public Value getWrapT() { 87 return mWrapT; 88 } 89 90 /** 91 * @return anisotropy setting for the sampler 92 */ getAnisotropy()93 public float getAnisotropy() { 94 return mAniso; 95 } 96 97 /** 98 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 99 * clamp. 100 * 101 * @param rs Context to which the sampler will belong. 102 * 103 * @return Sampler 104 */ CLAMP_NEAREST(RenderScript rs)105 public static Sampler CLAMP_NEAREST(RenderScript rs) { 106 if(rs.mSampler_CLAMP_NEAREST == null) { 107 Builder b = new Builder(rs); 108 b.setMinification(Value.NEAREST); 109 b.setMagnification(Value.NEAREST); 110 b.setWrapS(Value.CLAMP); 111 b.setWrapT(Value.CLAMP); 112 rs.mSampler_CLAMP_NEAREST = b.create(); 113 } 114 return rs.mSampler_CLAMP_NEAREST; 115 } 116 117 /** 118 * Retrieve a sampler with min and mag set to linear and wrap modes set to 119 * clamp. 120 * 121 * @param rs Context to which the sampler will belong. 122 * 123 * @return Sampler 124 */ CLAMP_LINEAR(RenderScript rs)125 public static Sampler CLAMP_LINEAR(RenderScript rs) { 126 if(rs.mSampler_CLAMP_LINEAR == null) { 127 Builder b = new Builder(rs); 128 b.setMinification(Value.LINEAR); 129 b.setMagnification(Value.LINEAR); 130 b.setWrapS(Value.CLAMP); 131 b.setWrapT(Value.CLAMP); 132 rs.mSampler_CLAMP_LINEAR = b.create(); 133 } 134 return rs.mSampler_CLAMP_LINEAR; 135 } 136 137 /** 138 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and 139 * wrap modes set to clamp. 140 * 141 * @param rs Context to which the sampler will belong. 142 * 143 * @return Sampler 144 */ CLAMP_LINEAR_MIP_LINEAR(RenderScript rs)145 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { 146 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { 147 Builder b = new Builder(rs); 148 b.setMinification(Value.LINEAR_MIP_LINEAR); 149 b.setMagnification(Value.LINEAR); 150 b.setWrapS(Value.CLAMP); 151 b.setWrapT(Value.CLAMP); 152 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); 153 } 154 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; 155 } 156 157 /** 158 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 159 * wrap. 160 * 161 * @param rs Context to which the sampler will belong. 162 * 163 * @return Sampler 164 */ WRAP_NEAREST(RenderScript rs)165 public static Sampler WRAP_NEAREST(RenderScript rs) { 166 if(rs.mSampler_WRAP_NEAREST == null) { 167 Builder b = new Builder(rs); 168 b.setMinification(Value.NEAREST); 169 b.setMagnification(Value.NEAREST); 170 b.setWrapS(Value.WRAP); 171 b.setWrapT(Value.WRAP); 172 rs.mSampler_WRAP_NEAREST = b.create(); 173 } 174 return rs.mSampler_WRAP_NEAREST; 175 } 176 177 /** 178 * Retrieve a sampler with min and mag set to linear and wrap modes set to 179 * wrap. 180 * 181 * @param rs Context to which the sampler will belong. 182 * 183 * @return Sampler 184 */ WRAP_LINEAR(RenderScript rs)185 public static Sampler WRAP_LINEAR(RenderScript rs) { 186 if(rs.mSampler_WRAP_LINEAR == null) { 187 Builder b = new Builder(rs); 188 b.setMinification(Value.LINEAR); 189 b.setMagnification(Value.LINEAR); 190 b.setWrapS(Value.WRAP); 191 b.setWrapT(Value.WRAP); 192 rs.mSampler_WRAP_LINEAR = b.create(); 193 } 194 return rs.mSampler_WRAP_LINEAR; 195 } 196 197 /** 198 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and 199 * wrap modes set to wrap. 200 * 201 * @param rs Context to which the sampler will belong. 202 * 203 * @return Sampler 204 */ WRAP_LINEAR_MIP_LINEAR(RenderScript rs)205 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { 206 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { 207 Builder b = new Builder(rs); 208 b.setMinification(Value.LINEAR_MIP_LINEAR); 209 b.setMagnification(Value.LINEAR); 210 b.setWrapS(Value.WRAP); 211 b.setWrapT(Value.WRAP); 212 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); 213 } 214 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; 215 } 216 217 /** 218 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 219 * mirrored repeat. 220 * 221 * @param rs Context to which the sampler will belong. 222 * 223 * @return Sampler 224 */ MIRRORED_REPEAT_NEAREST(RenderScript rs)225 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) { 226 if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) { 227 Builder b = new Builder(rs); 228 b.setMinification(Value.NEAREST); 229 b.setMagnification(Value.NEAREST); 230 b.setWrapS(Value.MIRRORED_REPEAT); 231 b.setWrapT(Value.MIRRORED_REPEAT); 232 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create(); 233 } 234 return rs.mSampler_MIRRORED_REPEAT_NEAREST; 235 } 236 237 /** 238 * Retrieve a sampler with min and mag set to linear and wrap modes set to 239 * mirrored repeat. 240 * 241 * @param rs Context to which the sampler will belong. 242 * 243 * @return Sampler 244 */ MIRRORED_REPEAT_LINEAR(RenderScript rs)245 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) { 246 if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) { 247 Builder b = new Builder(rs); 248 b.setMinification(Value.LINEAR); 249 b.setMagnification(Value.LINEAR); 250 b.setWrapS(Value.MIRRORED_REPEAT); 251 b.setWrapT(Value.MIRRORED_REPEAT); 252 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create(); 253 } 254 return rs.mSampler_MIRRORED_REPEAT_LINEAR; 255 } 256 257 /** 258 * Retrieve a sampler with min and mag set to linear and wrap modes set to 259 * mirrored repeat. 260 * 261 * @param rs Context to which the sampler will belong. 262 * 263 * @return Sampler 264 */ MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs)265 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) { 266 if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) { 267 Builder b = new Builder(rs); 268 b.setMinification(Value.LINEAR_MIP_LINEAR); 269 b.setMagnification(Value.LINEAR); 270 b.setWrapS(Value.MIRRORED_REPEAT); 271 b.setWrapT(Value.MIRRORED_REPEAT); 272 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create(); 273 } 274 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR; 275 } 276 277 /** 278 * Builder for creating non-standard samplers. Usefull if mix and match of 279 * wrap modes is necesary or if anisotropic filtering is desired. 280 * 281 */ 282 public static class Builder { 283 RenderScript mRS; 284 Value mMin; 285 Value mMag; 286 Value mWrapS; 287 Value mWrapT; 288 Value mWrapR; 289 float mAniso; 290 Builder(RenderScript rs)291 public Builder(RenderScript rs) { 292 mRS = rs; 293 mMin = Value.NEAREST; 294 mMag = Value.NEAREST; 295 mWrapS = Value.WRAP; 296 mWrapT = Value.WRAP; 297 mWrapR = Value.WRAP; 298 mAniso = 1.0f; 299 } 300 setMinification(Value v)301 public void setMinification(Value v) { 302 if (v == Value.NEAREST || 303 v == Value.LINEAR || 304 v == Value.LINEAR_MIP_LINEAR || 305 v == Value.LINEAR_MIP_NEAREST) { 306 mMin = v; 307 } else { 308 throw new IllegalArgumentException("Invalid value"); 309 } 310 } 311 setMagnification(Value v)312 public void setMagnification(Value v) { 313 if (v == Value.NEAREST || v == Value.LINEAR) { 314 mMag = v; 315 } else { 316 throw new IllegalArgumentException("Invalid value"); 317 } 318 } 319 setWrapS(Value v)320 public void setWrapS(Value v) { 321 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) { 322 mWrapS = v; 323 } else { 324 throw new IllegalArgumentException("Invalid value"); 325 } 326 } 327 setWrapT(Value v)328 public void setWrapT(Value v) { 329 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) { 330 mWrapT = v; 331 } else { 332 throw new IllegalArgumentException("Invalid value"); 333 } 334 } 335 setAnisotropy(float v)336 public void setAnisotropy(float v) { 337 if(v >= 0.0f) { 338 mAniso = v; 339 } else { 340 throw new IllegalArgumentException("Invalid value"); 341 } 342 } 343 create()344 public Sampler create() { 345 mRS.validate(); 346 int id = mRS.nSamplerCreate(mMag.mID, mMin.mID, 347 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso); 348 Sampler sampler = new Sampler(id, mRS); 349 sampler.mMin = mMin; 350 sampler.mMag = mMag; 351 sampler.mWrapS = mWrapS; 352 sampler.mWrapT = mWrapT; 353 sampler.mWrapR = mWrapR; 354 sampler.mAniso = mAniso; 355 return sampler; 356 } 357 } 358 359 } 360 361