blob: 5c4bae99ba8fdf80ad1ffef66a5ff49eaf7283b5 [file] [log] [blame]
Jason Sams0835d422009-08-04 17:58:23 -07001/*
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
17package android.renderscript;
18
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070019/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070020 * Sampler object that defines how Allocations can be read as textures within a
21 * kernel. Samplers are used in conjunction with the {@code rsSample} runtime
22 * function to return values from normalized coordinates.
23 *
24 * Any Allocation used with a Sampler must have been created with {@link
25 * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE}; using a Sampler on
26 * an {@link android.renderscript.Allocation} that was not created with {@link
27 * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE} is undefined.
Jason Sams0835d422009-08-04 17:58:23 -070028 **/
29public class Sampler extends BaseObj {
30 public enum Value {
31 NEAREST (0),
32 LINEAR (1),
33 LINEAR_MIP_LINEAR (2),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080034 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070035 WRAP (3),
Tim Murray6b9b2ca2013-02-15 13:25:55 -080036 CLAMP (4),
37 MIRRORED_REPEAT (6);
Jason Sams0835d422009-08-04 17:58:23 -070038
39 int mID;
40 Value(int id) {
41 mID = id;
42 }
43 }
44
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070045 Value mMin;
46 Value mMag;
47 Value mWrapS;
48 Value mWrapT;
49 Value mWrapR;
50 float mAniso;
51
Ashok Bhat0e0c0882014-02-04 14:57:58 +000052 Sampler(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070053 super(id, rs);
Yang Ni6484b6b2016-03-24 09:40:32 -070054 guard.open("destroy");
Jason Sams0835d422009-08-04 17:58:23 -070055 }
56
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070057 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070058 * @return minification setting for the sampler
59 */
60 public Value getMinification() {
61 return mMin;
62 }
63
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070064 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070065 * @return magnification setting for the sampler
66 */
67 public Value getMagnification() {
68 return mMag;
69 }
70
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070071 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070072 * @return S wrapping mode for the sampler
73 */
74 public Value getWrapS() {
75 return mWrapS;
76 }
77
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070078 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070079 * @return T wrapping mode for the sampler
80 */
81 public Value getWrapT() {
82 return mWrapT;
83 }
84
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070085 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070086 * @return anisotropy setting for the sampler
87 */
88 public float getAnisotropy() {
89 return mAniso;
90 }
91
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070092 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -080093 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
94 * clamp.
95 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080096 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080097 *
98 * @return Sampler
99 */
Jason Sams4d339932010-05-11 14:03:58 -0700100 public static Sampler CLAMP_NEAREST(RenderScript rs) {
101 if(rs.mSampler_CLAMP_NEAREST == null) {
102 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800103 b.setMinification(Value.NEAREST);
104 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700105 b.setWrapS(Value.CLAMP);
106 b.setWrapT(Value.CLAMP);
107 rs.mSampler_CLAMP_NEAREST = b.create();
108 }
109 return rs.mSampler_CLAMP_NEAREST;
110 }
111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800113 * Retrieve a sampler with min and mag set to linear and wrap modes set to
114 * clamp.
115 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800116 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800117 *
118 * @return Sampler
119 */
Jason Sams4d339932010-05-11 14:03:58 -0700120 public static Sampler CLAMP_LINEAR(RenderScript rs) {
121 if(rs.mSampler_CLAMP_LINEAR == null) {
122 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800123 b.setMinification(Value.LINEAR);
124 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700125 b.setWrapS(Value.CLAMP);
126 b.setWrapT(Value.CLAMP);
127 rs.mSampler_CLAMP_LINEAR = b.create();
128 }
129 return rs.mSampler_CLAMP_LINEAR;
130 }
131
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700132 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800133 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
134 * wrap modes set to clamp.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800135 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800136 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 *
138 * @return Sampler
139 */
Jason Sams4d339932010-05-11 14:03:58 -0700140 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
141 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
142 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800143 b.setMinification(Value.LINEAR_MIP_LINEAR);
144 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700145 b.setWrapS(Value.CLAMP);
146 b.setWrapT(Value.CLAMP);
147 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
148 }
149 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
150 }
151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700152 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800153 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
154 * wrap.
155 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800156 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800157 *
158 * @return Sampler
159 */
Jason Sams4d339932010-05-11 14:03:58 -0700160 public static Sampler WRAP_NEAREST(RenderScript rs) {
161 if(rs.mSampler_WRAP_NEAREST == null) {
162 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800163 b.setMinification(Value.NEAREST);
164 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700165 b.setWrapS(Value.WRAP);
166 b.setWrapT(Value.WRAP);
167 rs.mSampler_WRAP_NEAREST = b.create();
168 }
169 return rs.mSampler_WRAP_NEAREST;
170 }
171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800173 * Retrieve a sampler with min and mag set to linear and wrap modes set to
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800174 * wrap.
175 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800176 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800177 *
178 * @return Sampler
179 */
Jason Sams4d339932010-05-11 14:03:58 -0700180 public static Sampler WRAP_LINEAR(RenderScript rs) {
181 if(rs.mSampler_WRAP_LINEAR == null) {
182 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800183 b.setMinification(Value.LINEAR);
184 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700185 b.setWrapS(Value.WRAP);
186 b.setWrapT(Value.WRAP);
187 rs.mSampler_WRAP_LINEAR = b.create();
188 }
189 return rs.mSampler_WRAP_LINEAR;
190 }
191
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700192 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800193 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
194 * wrap modes set to wrap.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800195 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800196 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800197 *
198 * @return Sampler
199 */
Jason Sams4d339932010-05-11 14:03:58 -0700200 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
201 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
202 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800203 b.setMinification(Value.LINEAR_MIP_LINEAR);
204 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700205 b.setWrapS(Value.WRAP);
206 b.setWrapT(Value.WRAP);
207 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
208 }
209 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
210 }
211
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800212 /**
213 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
214 * mirrored repeat.
215 *
216 * @param rs Context to which the sampler will belong.
217 *
218 * @return Sampler
219 */
220 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
221 if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
222 Builder b = new Builder(rs);
223 b.setMinification(Value.NEAREST);
224 b.setMagnification(Value.NEAREST);
225 b.setWrapS(Value.MIRRORED_REPEAT);
226 b.setWrapT(Value.MIRRORED_REPEAT);
227 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
228 }
229 return rs.mSampler_MIRRORED_REPEAT_NEAREST;
230 }
231
232 /**
233 * Retrieve a sampler with min and mag set to linear and wrap modes set to
234 * mirrored repeat.
235 *
236 * @param rs Context to which the sampler will belong.
237 *
238 * @return Sampler
239 */
240 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
241 if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
242 Builder b = new Builder(rs);
243 b.setMinification(Value.LINEAR);
244 b.setMagnification(Value.LINEAR);
245 b.setWrapS(Value.MIRRORED_REPEAT);
246 b.setWrapT(Value.MIRRORED_REPEAT);
247 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
248 }
249 return rs.mSampler_MIRRORED_REPEAT_LINEAR;
250 }
251
252 /**
253 * Retrieve a sampler with min and mag set to linear and wrap modes set to
254 * mirrored repeat.
255 *
256 * @param rs Context to which the sampler will belong.
257 *
258 * @return Sampler
259 */
260 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
261 if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
262 Builder b = new Builder(rs);
263 b.setMinification(Value.LINEAR_MIP_LINEAR);
264 b.setMagnification(Value.LINEAR);
265 b.setWrapS(Value.MIRRORED_REPEAT);
266 b.setWrapT(Value.MIRRORED_REPEAT);
267 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
268 }
269 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
270 }
Jason Sams4d339932010-05-11 14:03:58 -0700271
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700272 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700273 * Builder for creating non-standard samplers. This is only necessary if
274 * a Sampler with different min and mag modes is desired.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800275 */
Jason Sams0835d422009-08-04 17:58:23 -0700276 public static class Builder {
277 RenderScript mRS;
278 Value mMin;
279 Value mMag;
280 Value mWrapS;
281 Value mWrapT;
282 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700283 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700284
285 public Builder(RenderScript rs) {
286 mRS = rs;
287 mMin = Value.NEAREST;
288 mMag = Value.NEAREST;
289 mWrapS = Value.WRAP;
290 mWrapT = Value.WRAP;
291 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700292 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700293 }
294
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800295 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800296 if (v == Value.NEAREST ||
297 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800298 v == Value.LINEAR_MIP_LINEAR ||
299 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800300 mMin = v;
301 } else {
302 throw new IllegalArgumentException("Invalid value");
303 }
Jason Sams0835d422009-08-04 17:58:23 -0700304 }
305
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800306 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800307 if (v == Value.NEAREST || v == Value.LINEAR) {
308 mMag = v;
309 } else {
310 throw new IllegalArgumentException("Invalid value");
311 }
Jason Sams0835d422009-08-04 17:58:23 -0700312 }
313
314 public void setWrapS(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800315 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800316 mWrapS = v;
317 } else {
318 throw new IllegalArgumentException("Invalid value");
319 }
Jason Sams0835d422009-08-04 17:58:23 -0700320 }
321
322 public void setWrapT(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800323 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800324 mWrapT = v;
325 } else {
326 throw new IllegalArgumentException("Invalid value");
327 }
Jason Sams0835d422009-08-04 17:58:23 -0700328 }
329
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700330 public void setAnisotropy(float v) {
331 if(v >= 0.0f) {
332 mAniso = v;
333 } else {
334 throw new IllegalArgumentException("Invalid value");
335 }
336 }
337
Jason Sams0835d422009-08-04 17:58:23 -0700338 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800339 mRS.validate();
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000340 long id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700341 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
342 Sampler sampler = new Sampler(id, mRS);
343 sampler.mMin = mMin;
344 sampler.mMag = mMag;
345 sampler.mWrapS = mWrapS;
346 sampler.mWrapT = mWrapT;
347 sampler.mWrapR = mWrapR;
348 sampler.mAniso = mAniso;
349 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700350 }
351 }
352
353}
354