blob: a4edbb50ac1096d15fc24d05c44af6b5bbeb03b8 [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);
Jason Sams0835d422009-08-04 17:58:23 -070054 }
55
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070056 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070057 * @return minification setting for the sampler
58 */
59 public Value getMinification() {
60 return mMin;
61 }
62
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070063 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070064 * @return magnification setting for the sampler
65 */
66 public Value getMagnification() {
67 return mMag;
68 }
69
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070070 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070071 * @return S wrapping mode for the sampler
72 */
73 public Value getWrapS() {
74 return mWrapS;
75 }
76
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070077 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070078 * @return T wrapping mode for the sampler
79 */
80 public Value getWrapT() {
81 return mWrapT;
82 }
83
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070084 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070085 * @return anisotropy setting for the sampler
86 */
87 public float getAnisotropy() {
88 return mAniso;
89 }
90
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070091 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -080092 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
93 * clamp.
94 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080095 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080096 *
97 * @return Sampler
98 */
Jason Sams4d339932010-05-11 14:03:58 -070099 public static Sampler CLAMP_NEAREST(RenderScript rs) {
100 if(rs.mSampler_CLAMP_NEAREST == null) {
101 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800102 b.setMinification(Value.NEAREST);
103 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700104 b.setWrapS(Value.CLAMP);
105 b.setWrapT(Value.CLAMP);
106 rs.mSampler_CLAMP_NEAREST = b.create();
107 }
108 return rs.mSampler_CLAMP_NEAREST;
109 }
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800112 * Retrieve a sampler with min and mag set to linear and wrap modes set to
113 * clamp.
114 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800115 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800116 *
117 * @return Sampler
118 */
Jason Sams4d339932010-05-11 14:03:58 -0700119 public static Sampler CLAMP_LINEAR(RenderScript rs) {
120 if(rs.mSampler_CLAMP_LINEAR == null) {
121 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800122 b.setMinification(Value.LINEAR);
123 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700124 b.setWrapS(Value.CLAMP);
125 b.setWrapT(Value.CLAMP);
126 rs.mSampler_CLAMP_LINEAR = b.create();
127 }
128 return rs.mSampler_CLAMP_LINEAR;
129 }
130
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700131 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800132 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
133 * wrap modes set to clamp.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800134 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800135 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800136 *
137 * @return Sampler
138 */
Jason Sams4d339932010-05-11 14:03:58 -0700139 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
140 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
141 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800142 b.setMinification(Value.LINEAR_MIP_LINEAR);
143 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700144 b.setWrapS(Value.CLAMP);
145 b.setWrapT(Value.CLAMP);
146 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
147 }
148 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
149 }
150
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700151 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800152 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
153 * wrap.
154 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800155 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800156 *
157 * @return Sampler
158 */
Jason Sams4d339932010-05-11 14:03:58 -0700159 public static Sampler WRAP_NEAREST(RenderScript rs) {
160 if(rs.mSampler_WRAP_NEAREST == null) {
161 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800162 b.setMinification(Value.NEAREST);
163 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700164 b.setWrapS(Value.WRAP);
165 b.setWrapT(Value.WRAP);
166 rs.mSampler_WRAP_NEAREST = b.create();
167 }
168 return rs.mSampler_WRAP_NEAREST;
169 }
170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800172 * Retrieve a sampler with min and mag set to linear and wrap modes set to
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800173 * wrap.
174 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800175 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800176 *
177 * @return Sampler
178 */
Jason Sams4d339932010-05-11 14:03:58 -0700179 public static Sampler WRAP_LINEAR(RenderScript rs) {
180 if(rs.mSampler_WRAP_LINEAR == null) {
181 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800182 b.setMinification(Value.LINEAR);
183 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700184 b.setWrapS(Value.WRAP);
185 b.setWrapT(Value.WRAP);
186 rs.mSampler_WRAP_LINEAR = b.create();
187 }
188 return rs.mSampler_WRAP_LINEAR;
189 }
190
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700191 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800192 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
193 * wrap modes set to wrap.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800194 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800195 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800196 *
197 * @return Sampler
198 */
Jason Sams4d339932010-05-11 14:03:58 -0700199 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
200 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
201 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800202 b.setMinification(Value.LINEAR_MIP_LINEAR);
203 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700204 b.setWrapS(Value.WRAP);
205 b.setWrapT(Value.WRAP);
206 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
207 }
208 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
209 }
210
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800211 /**
212 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
213 * mirrored repeat.
214 *
215 * @param rs Context to which the sampler will belong.
216 *
217 * @return Sampler
218 */
219 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
220 if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
221 Builder b = new Builder(rs);
222 b.setMinification(Value.NEAREST);
223 b.setMagnification(Value.NEAREST);
224 b.setWrapS(Value.MIRRORED_REPEAT);
225 b.setWrapT(Value.MIRRORED_REPEAT);
226 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
227 }
228 return rs.mSampler_MIRRORED_REPEAT_NEAREST;
229 }
230
231 /**
232 * Retrieve a sampler with min and mag set to linear and wrap modes set to
233 * mirrored repeat.
234 *
235 * @param rs Context to which the sampler will belong.
236 *
237 * @return Sampler
238 */
239 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
240 if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
241 Builder b = new Builder(rs);
242 b.setMinification(Value.LINEAR);
243 b.setMagnification(Value.LINEAR);
244 b.setWrapS(Value.MIRRORED_REPEAT);
245 b.setWrapT(Value.MIRRORED_REPEAT);
246 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
247 }
248 return rs.mSampler_MIRRORED_REPEAT_LINEAR;
249 }
250
251 /**
252 * Retrieve a sampler with min and mag set to linear and wrap modes set to
253 * mirrored repeat.
254 *
255 * @param rs Context to which the sampler will belong.
256 *
257 * @return Sampler
258 */
259 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
260 if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
261 Builder b = new Builder(rs);
262 b.setMinification(Value.LINEAR_MIP_LINEAR);
263 b.setMagnification(Value.LINEAR);
264 b.setWrapS(Value.MIRRORED_REPEAT);
265 b.setWrapT(Value.MIRRORED_REPEAT);
266 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
267 }
268 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
269 }
Jason Sams4d339932010-05-11 14:03:58 -0700270
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700271 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700272 * Builder for creating non-standard samplers. This is only necessary if
273 * a Sampler with different min and mag modes is desired.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800274 */
Jason Sams0835d422009-08-04 17:58:23 -0700275 public static class Builder {
276 RenderScript mRS;
277 Value mMin;
278 Value mMag;
279 Value mWrapS;
280 Value mWrapT;
281 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700282 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700283
284 public Builder(RenderScript rs) {
285 mRS = rs;
286 mMin = Value.NEAREST;
287 mMag = Value.NEAREST;
288 mWrapS = Value.WRAP;
289 mWrapT = Value.WRAP;
290 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700291 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700292 }
293
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800294 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800295 if (v == Value.NEAREST ||
296 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800297 v == Value.LINEAR_MIP_LINEAR ||
298 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800299 mMin = v;
300 } else {
301 throw new IllegalArgumentException("Invalid value");
302 }
Jason Sams0835d422009-08-04 17:58:23 -0700303 }
304
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800305 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800306 if (v == Value.NEAREST || v == Value.LINEAR) {
307 mMag = v;
308 } else {
309 throw new IllegalArgumentException("Invalid value");
310 }
Jason Sams0835d422009-08-04 17:58:23 -0700311 }
312
313 public void setWrapS(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800314 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800315 mWrapS = v;
316 } else {
317 throw new IllegalArgumentException("Invalid value");
318 }
Jason Sams0835d422009-08-04 17:58:23 -0700319 }
320
321 public void setWrapT(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800322 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800323 mWrapT = v;
324 } else {
325 throw new IllegalArgumentException("Invalid value");
326 }
Jason Sams0835d422009-08-04 17:58:23 -0700327 }
328
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700329 public void setAnisotropy(float v) {
330 if(v >= 0.0f) {
331 mAniso = v;
332 } else {
333 throw new IllegalArgumentException("Invalid value");
334 }
335 }
336
Jason Sams0835d422009-08-04 17:58:23 -0700337 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800338 mRS.validate();
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000339 long id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700340 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
341 Sampler sampler = new Sampler(id, mRS);
342 sampler.mMin = mMin;
343 sampler.mMag = mMag;
344 sampler.mWrapS = mWrapS;
345 sampler.mWrapT = mWrapT;
346 sampler.mWrapR = mWrapR;
347 sampler.mAniso = mAniso;
348 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700349 }
350 }
351
352}
353