blob: 06f036db3aa5341a69200a6a98386d77222d315d [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.
Xusong Wang8b4548c2021-01-05 10:09:52 -080028 *
29 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
30 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
31 * guide</a> for the proposed alternatives.
Jason Sams0835d422009-08-04 17:58:23 -070032 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080033@Deprecated
Jason Sams0835d422009-08-04 17:58:23 -070034public class Sampler extends BaseObj {
35 public enum Value {
36 NEAREST (0),
37 LINEAR (1),
38 LINEAR_MIP_LINEAR (2),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080039 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070040 WRAP (3),
Tim Murray6b9b2ca2013-02-15 13:25:55 -080041 CLAMP (4),
42 MIRRORED_REPEAT (6);
Jason Sams0835d422009-08-04 17:58:23 -070043
44 int mID;
45 Value(int id) {
46 mID = id;
47 }
48 }
49
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070050 Value mMin;
51 Value mMag;
52 Value mWrapS;
53 Value mWrapT;
54 Value mWrapR;
55 float mAniso;
56
Ashok Bhat0e0c0882014-02-04 14:57:58 +000057 Sampler(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070058 super(id, rs);
Yang Nieb4dd082016-03-24 09:40:32 -070059 guard.open("destroy");
Jason Sams0835d422009-08-04 17:58:23 -070060 }
61
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070062 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070063 * @return minification setting for the sampler
64 */
65 public Value getMinification() {
66 return mMin;
67 }
68
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070069 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070070 * @return magnification setting for the sampler
71 */
72 public Value getMagnification() {
73 return mMag;
74 }
75
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070076 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070077 * @return S wrapping mode for the sampler
78 */
79 public Value getWrapS() {
80 return mWrapS;
81 }
82
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070083 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070084 * @return T wrapping mode for the sampler
85 */
86 public Value getWrapT() {
87 return mWrapT;
88 }
89
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070091 * @return anisotropy setting for the sampler
92 */
93 public float getAnisotropy() {
94 return mAniso;
95 }
96
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070097 /**
Jason Samsbf6ef8d72010-12-06 15:59:59 -080098 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
99 * clamp.
100 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800101 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800102 *
103 * @return Sampler
104 */
Jason Sams4d339932010-05-11 14:03:58 -0700105 public static Sampler CLAMP_NEAREST(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700106 if (rs.mSampler_CLAMP_NEAREST == null) {
107 synchronized (rs) {
108 if (rs.mSampler_CLAMP_NEAREST == null) {
109 Builder b = new Builder(rs);
110 b.setMinification(Value.NEAREST);
111 b.setMagnification(Value.NEAREST);
112 b.setWrapS(Value.CLAMP);
113 b.setWrapT(Value.CLAMP);
114 rs.mSampler_CLAMP_NEAREST = b.create();
115 }
116 }
Jason Sams4d339932010-05-11 14:03:58 -0700117 }
118 return rs.mSampler_CLAMP_NEAREST;
119 }
120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700121 /**
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800122 * Retrieve a sampler with min and mag set to linear and wrap modes set to
123 * clamp.
124 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800125 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800126 *
127 * @return Sampler
128 */
Jason Sams4d339932010-05-11 14:03:58 -0700129 public static Sampler CLAMP_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700130 if (rs.mSampler_CLAMP_LINEAR == null) {
131 synchronized (rs) {
132 if (rs.mSampler_CLAMP_LINEAR == null) {
133 Builder b = new Builder(rs);
134 b.setMinification(Value.LINEAR);
135 b.setMagnification(Value.LINEAR);
136 b.setWrapS(Value.CLAMP);
137 b.setWrapT(Value.CLAMP);
138 rs.mSampler_CLAMP_LINEAR = b.create();
139 }
140 }
Jason Sams4d339932010-05-11 14:03:58 -0700141 }
142 return rs.mSampler_CLAMP_LINEAR;
143 }
144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800146 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
147 * wrap modes set to clamp.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800148 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800149 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800150 *
151 * @return Sampler
152 */
Jason Sams4d339932010-05-11 14:03:58 -0700153 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700154 if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
155 synchronized (rs) {
156 if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
157 Builder b = new Builder(rs);
158 b.setMinification(Value.LINEAR_MIP_LINEAR);
159 b.setMagnification(Value.LINEAR);
160 b.setWrapS(Value.CLAMP);
161 b.setWrapT(Value.CLAMP);
162 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
163 }
164 }
Jason Sams4d339932010-05-11 14:03:58 -0700165 }
166 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
167 }
168
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700169 /**
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800170 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
171 * wrap.
172 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800173 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800174 *
175 * @return Sampler
176 */
Jason Sams4d339932010-05-11 14:03:58 -0700177 public static Sampler WRAP_NEAREST(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700178 if (rs.mSampler_WRAP_NEAREST == null) {
179 synchronized (rs) {
180 if (rs.mSampler_WRAP_NEAREST == null) {
181 Builder b = new Builder(rs);
182 b.setMinification(Value.NEAREST);
183 b.setMagnification(Value.NEAREST);
184 b.setWrapS(Value.WRAP);
185 b.setWrapT(Value.WRAP);
186 rs.mSampler_WRAP_NEAREST = b.create();
187 }
188 }
Jason Sams4d339932010-05-11 14:03:58 -0700189 }
190 return rs.mSampler_WRAP_NEAREST;
191 }
192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800194 * Retrieve a sampler with min and mag set to linear and wrap modes set to
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800195 * wrap.
196 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800197 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800198 *
199 * @return Sampler
200 */
Jason Sams4d339932010-05-11 14:03:58 -0700201 public static Sampler WRAP_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700202 if (rs.mSampler_WRAP_LINEAR == null) {
203 synchronized (rs) {
204 if (rs.mSampler_WRAP_LINEAR == null) {
205 Builder b = new Builder(rs);
206 b.setMinification(Value.LINEAR);
207 b.setMagnification(Value.LINEAR);
208 b.setWrapS(Value.WRAP);
209 b.setWrapT(Value.WRAP);
210 rs.mSampler_WRAP_LINEAR = b.create();
211 }
212 }
Jason Sams4d339932010-05-11 14:03:58 -0700213 }
214 return rs.mSampler_WRAP_LINEAR;
215 }
216
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700217 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800218 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
219 * wrap modes set to wrap.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800220 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800221 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800222 *
223 * @return Sampler
224 */
Jason Sams4d339932010-05-11 14:03:58 -0700225 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700226 if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
227 synchronized (rs) {
228 if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
229 Builder b = new Builder(rs);
230 b.setMinification(Value.LINEAR_MIP_LINEAR);
231 b.setMagnification(Value.LINEAR);
232 b.setWrapS(Value.WRAP);
233 b.setWrapT(Value.WRAP);
234 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
235 }
236 }
Jason Sams4d339932010-05-11 14:03:58 -0700237 }
238 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
239 }
240
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800241 /**
242 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
243 * mirrored repeat.
244 *
245 * @param rs Context to which the sampler will belong.
246 *
247 * @return Sampler
248 */
249 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700250 if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
251 synchronized (rs) {
252 if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
253 Builder b = new Builder(rs);
254 b.setMinification(Value.NEAREST);
255 b.setMagnification(Value.NEAREST);
256 b.setWrapS(Value.MIRRORED_REPEAT);
257 b.setWrapT(Value.MIRRORED_REPEAT);
258 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
259 }
260 }
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800261 }
262 return rs.mSampler_MIRRORED_REPEAT_NEAREST;
263 }
264
265 /**
266 * Retrieve a sampler with min and mag set to linear and wrap modes set to
267 * mirrored repeat.
268 *
269 * @param rs Context to which the sampler will belong.
270 *
271 * @return Sampler
272 */
273 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700274 if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
275 synchronized (rs) {
276 if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
277 Builder b = new Builder(rs);
278 b.setMinification(Value.LINEAR);
279 b.setMagnification(Value.LINEAR);
280 b.setWrapS(Value.MIRRORED_REPEAT);
281 b.setWrapT(Value.MIRRORED_REPEAT);
282 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
283 }
284 }
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800285 }
286 return rs.mSampler_MIRRORED_REPEAT_LINEAR;
287 }
288
289 /**
290 * Retrieve a sampler with min and mag set to linear and wrap modes set to
291 * mirrored repeat.
292 *
293 * @param rs Context to which the sampler will belong.
294 *
295 * @return Sampler
296 */
297 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
Yang Ni6bdfe0f2016-04-18 16:56:16 -0700298 if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
299 synchronized (rs) {
300 if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
301 Builder b = new Builder(rs);
302 b.setMinification(Value.LINEAR_MIP_LINEAR);
303 b.setMagnification(Value.LINEAR);
304 b.setWrapS(Value.MIRRORED_REPEAT);
305 b.setWrapT(Value.MIRRORED_REPEAT);
306 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
307 }
308 }
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800309 }
310 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
311 }
Jason Sams4d339932010-05-11 14:03:58 -0700312
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700313 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700314 * Builder for creating non-standard samplers. This is only necessary if
315 * a Sampler with different min and mag modes is desired.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800316 */
Jason Sams0835d422009-08-04 17:58:23 -0700317 public static class Builder {
318 RenderScript mRS;
319 Value mMin;
320 Value mMag;
321 Value mWrapS;
322 Value mWrapT;
323 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700324 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700325
326 public Builder(RenderScript rs) {
327 mRS = rs;
328 mMin = Value.NEAREST;
329 mMag = Value.NEAREST;
330 mWrapS = Value.WRAP;
331 mWrapT = Value.WRAP;
332 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700333 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700334 }
335
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800336 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800337 if (v == Value.NEAREST ||
338 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800339 v == Value.LINEAR_MIP_LINEAR ||
340 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800341 mMin = v;
342 } else {
343 throw new IllegalArgumentException("Invalid value");
344 }
Jason Sams0835d422009-08-04 17:58:23 -0700345 }
346
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800347 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800348 if (v == Value.NEAREST || v == Value.LINEAR) {
349 mMag = v;
350 } else {
351 throw new IllegalArgumentException("Invalid value");
352 }
Jason Sams0835d422009-08-04 17:58:23 -0700353 }
354
355 public void setWrapS(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800356 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800357 mWrapS = v;
358 } else {
359 throw new IllegalArgumentException("Invalid value");
360 }
Jason Sams0835d422009-08-04 17:58:23 -0700361 }
362
363 public void setWrapT(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800364 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800365 mWrapT = v;
366 } else {
367 throw new IllegalArgumentException("Invalid value");
368 }
Jason Sams0835d422009-08-04 17:58:23 -0700369 }
370
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700371 public void setAnisotropy(float v) {
372 if(v >= 0.0f) {
373 mAniso = v;
374 } else {
375 throw new IllegalArgumentException("Invalid value");
376 }
377 }
378
Jason Sams0835d422009-08-04 17:58:23 -0700379 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800380 mRS.validate();
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000381 long id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700382 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
383 Sampler sampler = new Sampler(id, mRS);
384 sampler.mMin = mMin;
385 sampler.mMag = mMag;
386 sampler.mWrapS = mWrapS;
387 sampler.mWrapT = mWrapT;
388 sampler.mWrapR = mWrapR;
389 sampler.mAniso = mAniso;
390 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700391 }
392 }
393
394}
395