blob: 8d0e29e5eeff13a7041c006b4fccbbba9025730c [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
19
20import java.io.IOException;
21import java.io.InputStream;
22
23import android.content.res.Resources;
24import android.os.Bundle;
Jason Sams0835d422009-08-04 17:58:23 -070025import android.util.Log;
26
27import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
29
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070031 * 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 * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE}; using a Sampler on
37 * an {@link android.renderscript.Allocation} that was not created with {@link
38 * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE} is undefined.
Jason Sams0835d422009-08-04 17:58:23 -070039 **/
40public class Sampler extends BaseObj {
41 public enum Value {
42 NEAREST (0),
43 LINEAR (1),
44 LINEAR_MIP_LINEAR (2),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080045 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070046 WRAP (3),
Tim Murray6b9b2ca2013-02-15 13:25:55 -080047 CLAMP (4),
48 MIRRORED_REPEAT (6);
Jason Sams0835d422009-08-04 17:58:23 -070049
50 int mID;
51 Value(int id) {
52 mID = id;
53 }
54 }
55
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070056 Value mMin;
57 Value mMag;
58 Value mWrapS;
59 Value mWrapT;
60 Value mWrapR;
61 float mAniso;
62
Ashok Bhat0e0c0882014-02-04 14:57:58 +000063 Sampler(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070064 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070065 }
66
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070067 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070068 * @return minification setting for the sampler
69 */
70 public Value getMinification() {
71 return mMin;
72 }
73
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070074 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070075 * @return magnification setting for the sampler
76 */
77 public Value getMagnification() {
78 return mMag;
79 }
80
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070081 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070082 * @return S wrapping mode for the sampler
83 */
84 public Value getWrapS() {
85 return mWrapS;
86 }
87
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070088 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070089 * @return T wrapping mode for the sampler
90 */
91 public Value getWrapT() {
92 return mWrapT;
93 }
94
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070095 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070096 * @return anisotropy setting for the sampler
97 */
98 public float getAnisotropy() {
99 return mAniso;
100 }
101
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700102 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800103 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
104 * clamp.
105 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800106 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800107 *
108 * @return Sampler
109 */
Jason Sams4d339932010-05-11 14:03:58 -0700110 public static Sampler CLAMP_NEAREST(RenderScript rs) {
111 if(rs.mSampler_CLAMP_NEAREST == null) {
112 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800113 b.setMinification(Value.NEAREST);
114 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700115 b.setWrapS(Value.CLAMP);
116 b.setWrapT(Value.CLAMP);
117 rs.mSampler_CLAMP_NEAREST = b.create();
118 }
119 return rs.mSampler_CLAMP_NEAREST;
120 }
121
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700122 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800123 * Retrieve a sampler with min and mag set to linear and wrap modes set to
124 * clamp.
125 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800126 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800127 *
128 * @return Sampler
129 */
Jason Sams4d339932010-05-11 14:03:58 -0700130 public static Sampler CLAMP_LINEAR(RenderScript rs) {
131 if(rs.mSampler_CLAMP_LINEAR == null) {
132 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800133 b.setMinification(Value.LINEAR);
134 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700135 b.setWrapS(Value.CLAMP);
136 b.setWrapT(Value.CLAMP);
137 rs.mSampler_CLAMP_LINEAR = b.create();
138 }
139 return rs.mSampler_CLAMP_LINEAR;
140 }
141
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700142 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800143 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
144 * wrap modes set to clamp.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800145 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800146 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800147 *
148 * @return Sampler
149 */
Jason Sams4d339932010-05-11 14:03:58 -0700150 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
151 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
152 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800153 b.setMinification(Value.LINEAR_MIP_LINEAR);
154 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700155 b.setWrapS(Value.CLAMP);
156 b.setWrapT(Value.CLAMP);
157 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
158 }
159 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
160 }
161
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700162 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800163 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
164 * wrap.
165 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800166 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800167 *
168 * @return Sampler
169 */
Jason Sams4d339932010-05-11 14:03:58 -0700170 public static Sampler WRAP_NEAREST(RenderScript rs) {
171 if(rs.mSampler_WRAP_NEAREST == null) {
172 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800173 b.setMinification(Value.NEAREST);
174 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700175 b.setWrapS(Value.WRAP);
176 b.setWrapT(Value.WRAP);
177 rs.mSampler_WRAP_NEAREST = b.create();
178 }
179 return rs.mSampler_WRAP_NEAREST;
180 }
181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700182 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800183 * Retrieve a sampler with min and mag set to linear and wrap modes set to
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800184 * wrap.
185 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800186 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800187 *
188 * @return Sampler
189 */
Jason Sams4d339932010-05-11 14:03:58 -0700190 public static Sampler WRAP_LINEAR(RenderScript rs) {
191 if(rs.mSampler_WRAP_LINEAR == null) {
192 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800193 b.setMinification(Value.LINEAR);
194 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700195 b.setWrapS(Value.WRAP);
196 b.setWrapT(Value.WRAP);
197 rs.mSampler_WRAP_LINEAR = b.create();
198 }
199 return rs.mSampler_WRAP_LINEAR;
200 }
201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800203 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
204 * wrap modes set to wrap.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800205 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800206 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800207 *
208 * @return Sampler
209 */
Jason Sams4d339932010-05-11 14:03:58 -0700210 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
211 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
212 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800213 b.setMinification(Value.LINEAR_MIP_LINEAR);
214 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700215 b.setWrapS(Value.WRAP);
216 b.setWrapT(Value.WRAP);
217 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
218 }
219 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
220 }
221
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800222 /**
223 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
224 * mirrored repeat.
225 *
226 * @param rs Context to which the sampler will belong.
227 *
228 * @return Sampler
229 */
230 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
231 if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
232 Builder b = new Builder(rs);
233 b.setMinification(Value.NEAREST);
234 b.setMagnification(Value.NEAREST);
235 b.setWrapS(Value.MIRRORED_REPEAT);
236 b.setWrapT(Value.MIRRORED_REPEAT);
237 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
238 }
239 return rs.mSampler_MIRRORED_REPEAT_NEAREST;
240 }
241
242 /**
243 * Retrieve a sampler with min and mag set to linear and wrap modes set to
244 * mirrored repeat.
245 *
246 * @param rs Context to which the sampler will belong.
247 *
248 * @return Sampler
249 */
250 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
251 if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
252 Builder b = new Builder(rs);
253 b.setMinification(Value.LINEAR);
254 b.setMagnification(Value.LINEAR);
255 b.setWrapS(Value.MIRRORED_REPEAT);
256 b.setWrapT(Value.MIRRORED_REPEAT);
257 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
258 }
259 return rs.mSampler_MIRRORED_REPEAT_LINEAR;
260 }
261
262 /**
263 * Retrieve a sampler with min and mag set to linear and wrap modes set to
264 * mirrored repeat.
265 *
266 * @param rs Context to which the sampler will belong.
267 *
268 * @return Sampler
269 */
270 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
271 if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
272 Builder b = new Builder(rs);
273 b.setMinification(Value.LINEAR_MIP_LINEAR);
274 b.setMagnification(Value.LINEAR);
275 b.setWrapS(Value.MIRRORED_REPEAT);
276 b.setWrapT(Value.MIRRORED_REPEAT);
277 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
278 }
279 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
280 }
Jason Sams4d339932010-05-11 14:03:58 -0700281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700282 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700283 * Builder for creating non-standard samplers. This is only necessary if
284 * a Sampler with different min and mag modes is desired.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800285 */
Jason Sams0835d422009-08-04 17:58:23 -0700286 public static class Builder {
287 RenderScript mRS;
288 Value mMin;
289 Value mMag;
290 Value mWrapS;
291 Value mWrapT;
292 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700293 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700294
295 public Builder(RenderScript rs) {
296 mRS = rs;
297 mMin = Value.NEAREST;
298 mMag = Value.NEAREST;
299 mWrapS = Value.WRAP;
300 mWrapT = Value.WRAP;
301 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700302 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700303 }
304
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800305 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800306 if (v == Value.NEAREST ||
307 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800308 v == Value.LINEAR_MIP_LINEAR ||
309 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800310 mMin = v;
311 } else {
312 throw new IllegalArgumentException("Invalid value");
313 }
Jason Sams0835d422009-08-04 17:58:23 -0700314 }
315
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800316 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800317 if (v == Value.NEAREST || v == Value.LINEAR) {
318 mMag = v;
319 } else {
320 throw new IllegalArgumentException("Invalid value");
321 }
Jason Sams0835d422009-08-04 17:58:23 -0700322 }
323
324 public void setWrapS(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800325 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800326 mWrapS = v;
327 } else {
328 throw new IllegalArgumentException("Invalid value");
329 }
Jason Sams0835d422009-08-04 17:58:23 -0700330 }
331
332 public void setWrapT(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800333 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800334 mWrapT = v;
335 } else {
336 throw new IllegalArgumentException("Invalid value");
337 }
Jason Sams0835d422009-08-04 17:58:23 -0700338 }
339
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700340 public void setAnisotropy(float v) {
341 if(v >= 0.0f) {
342 mAniso = v;
343 } else {
344 throw new IllegalArgumentException("Invalid value");
345 }
346 }
347
Jason Sams0835d422009-08-04 17:58:23 -0700348 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800349 mRS.validate();
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000350 long id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700351 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
352 Sampler sampler = new Sampler(id, mRS);
353 sampler.mMin = mMin;
354 sampler.mMag = mMag;
355 sampler.mWrapS = mWrapS;
356 sampler.mWrapT = mWrapT;
357 sampler.mWrapR = mWrapR;
358 sampler.mAniso = mAniso;
359 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700360 }
361 }
362
363}
364