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