blob: 057e9b5452f81b26185adfa02799435899d8b2d3 [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/**
Robert Ly11518ac2011-02-09 13:57:06 -080031 * Sampler object which defines how data is extracted from textures. Samplers
32 * are attached to Program objects (currently only ProgramFragment) when those objects
Jason Samsbf6ef8d2010-12-06 15:59:59 -080033 * need to access texture data.
Jason Sams0835d422009-08-04 17:58:23 -070034 **/
35public class Sampler extends BaseObj {
36 public enum Value {
37 NEAREST (0),
38 LINEAR (1),
39 LINEAR_MIP_LINEAR (2),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080040 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070041 WRAP (3),
Tim Murray6b9b2ca2013-02-15 13:25:55 -080042 CLAMP (4),
43 MIRRORED_REPEAT (6);
Jason Sams0835d422009-08-04 17:58:23 -070044
45 int mID;
46 Value(int id) {
47 mID = id;
48 }
49 }
50
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070051 Value mMin;
52 Value mMag;
53 Value mWrapS;
54 Value mWrapT;
55 Value mWrapR;
56 float mAniso;
57
Jason Sams0835d422009-08-04 17:58:23 -070058 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070059 super(id, rs);
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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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) {
106 if(rs.mSampler_CLAMP_NEAREST == null) {
107 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800108 b.setMinification(Value.NEAREST);
109 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700110 b.setWrapS(Value.CLAMP);
111 b.setWrapT(Value.CLAMP);
112 rs.mSampler_CLAMP_NEAREST = b.create();
113 }
114 return rs.mSampler_CLAMP_NEAREST;
115 }
116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800118 * Retrieve a sampler with min and mag set to linear and wrap modes set to
119 * clamp.
120 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800121 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800122 *
123 * @return Sampler
124 */
Jason Sams4d339932010-05-11 14:03:58 -0700125 public static Sampler CLAMP_LINEAR(RenderScript rs) {
126 if(rs.mSampler_CLAMP_LINEAR == null) {
127 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800128 b.setMinification(Value.LINEAR);
129 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700130 b.setWrapS(Value.CLAMP);
131 b.setWrapT(Value.CLAMP);
132 rs.mSampler_CLAMP_LINEAR = b.create();
133 }
134 return rs.mSampler_CLAMP_LINEAR;
135 }
136
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700137 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800138 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
139 * wrap modes set to clamp.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800140 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800141 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800142 *
143 * @return Sampler
144 */
Jason Sams4d339932010-05-11 14:03:58 -0700145 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
146 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
147 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800148 b.setMinification(Value.LINEAR_MIP_LINEAR);
149 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700150 b.setWrapS(Value.CLAMP);
151 b.setWrapT(Value.CLAMP);
152 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
153 }
154 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
155 }
156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800158 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
159 * wrap.
160 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800161 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800162 *
163 * @return Sampler
164 */
Jason Sams4d339932010-05-11 14:03:58 -0700165 public static Sampler WRAP_NEAREST(RenderScript rs) {
166 if(rs.mSampler_WRAP_NEAREST == null) {
167 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800168 b.setMinification(Value.NEAREST);
169 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700170 b.setWrapS(Value.WRAP);
171 b.setWrapT(Value.WRAP);
172 rs.mSampler_WRAP_NEAREST = b.create();
173 }
174 return rs.mSampler_WRAP_NEAREST;
175 }
176
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700177 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800178 * Retrieve a sampler with min and mag set to linear and wrap modes set to
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800179 * wrap.
180 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800181 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800182 *
183 * @return Sampler
184 */
Jason Sams4d339932010-05-11 14:03:58 -0700185 public static Sampler WRAP_LINEAR(RenderScript rs) {
186 if(rs.mSampler_WRAP_LINEAR == null) {
187 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800188 b.setMinification(Value.LINEAR);
189 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700190 b.setWrapS(Value.WRAP);
191 b.setWrapT(Value.WRAP);
192 rs.mSampler_WRAP_LINEAR = b.create();
193 }
194 return rs.mSampler_WRAP_LINEAR;
195 }
196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800198 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
199 * wrap modes set to wrap.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800200 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800201 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800202 *
203 * @return Sampler
204 */
Jason Sams4d339932010-05-11 14:03:58 -0700205 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
206 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
207 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800208 b.setMinification(Value.LINEAR_MIP_LINEAR);
209 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700210 b.setWrapS(Value.WRAP);
211 b.setWrapT(Value.WRAP);
212 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
213 }
214 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
215 }
216
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800217 /**
218 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
219 * mirrored repeat.
220 *
221 * @param rs Context to which the sampler will belong.
222 *
223 * @return Sampler
224 */
225 public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
226 if(rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
227 Builder b = new Builder(rs);
228 b.setMinification(Value.NEAREST);
229 b.setMagnification(Value.NEAREST);
230 b.setWrapS(Value.MIRRORED_REPEAT);
231 b.setWrapT(Value.MIRRORED_REPEAT);
232 rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
233 }
234 return rs.mSampler_MIRRORED_REPEAT_NEAREST;
235 }
236
237 /**
238 * Retrieve a sampler with min and mag set to linear and wrap modes set to
239 * mirrored repeat.
240 *
241 * @param rs Context to which the sampler will belong.
242 *
243 * @return Sampler
244 */
245 public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
246 if(rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
247 Builder b = new Builder(rs);
248 b.setMinification(Value.LINEAR);
249 b.setMagnification(Value.LINEAR);
250 b.setWrapS(Value.MIRRORED_REPEAT);
251 b.setWrapT(Value.MIRRORED_REPEAT);
252 rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
253 }
254 return rs.mSampler_MIRRORED_REPEAT_LINEAR;
255 }
256
257 /**
258 * Retrieve a sampler with min and mag set to linear and wrap modes set to
259 * mirrored repeat.
260 *
261 * @param rs Context to which the sampler will belong.
262 *
263 * @return Sampler
264 */
265 public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
266 if(rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
267 Builder b = new Builder(rs);
268 b.setMinification(Value.LINEAR_MIP_LINEAR);
269 b.setMagnification(Value.LINEAR);
270 b.setWrapS(Value.MIRRORED_REPEAT);
271 b.setWrapT(Value.MIRRORED_REPEAT);
272 rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
273 }
274 return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
275 }
Jason Sams4d339932010-05-11 14:03:58 -0700276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800278 * Builder for creating non-standard samplers. Usefull if mix and match of
279 * wrap modes is necesary or if anisotropic filtering is desired.
280 *
281 */
Jason Sams0835d422009-08-04 17:58:23 -0700282 public static class Builder {
283 RenderScript mRS;
284 Value mMin;
285 Value mMag;
286 Value mWrapS;
287 Value mWrapT;
288 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700289 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700290
291 public Builder(RenderScript rs) {
292 mRS = rs;
293 mMin = Value.NEAREST;
294 mMag = Value.NEAREST;
295 mWrapS = Value.WRAP;
296 mWrapT = Value.WRAP;
297 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700298 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700299 }
300
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800301 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800302 if (v == Value.NEAREST ||
303 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800304 v == Value.LINEAR_MIP_LINEAR ||
305 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800306 mMin = v;
307 } else {
308 throw new IllegalArgumentException("Invalid value");
309 }
Jason Sams0835d422009-08-04 17:58:23 -0700310 }
311
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800312 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800313 if (v == Value.NEAREST || v == Value.LINEAR) {
314 mMag = v;
315 } else {
316 throw new IllegalArgumentException("Invalid value");
317 }
Jason Sams0835d422009-08-04 17:58:23 -0700318 }
319
320 public void setWrapS(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800321 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800322 mWrapS = v;
323 } else {
324 throw new IllegalArgumentException("Invalid value");
325 }
Jason Sams0835d422009-08-04 17:58:23 -0700326 }
327
328 public void setWrapT(Value v) {
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800329 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800330 mWrapT = v;
331 } else {
332 throw new IllegalArgumentException("Invalid value");
333 }
Jason Sams0835d422009-08-04 17:58:23 -0700334 }
335
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700336 public void setAnisotropy(float v) {
337 if(v >= 0.0f) {
338 mAniso = v;
339 } else {
340 throw new IllegalArgumentException("Invalid value");
341 }
342 }
343
Jason Sams0835d422009-08-04 17:58:23 -0700344 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800345 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700346 int id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700347 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
348 Sampler sampler = new Sampler(id, mRS);
349 sampler.mMin = mMin;
350 sampler.mMag = mMag;
351 sampler.mWrapS = mWrapS;
352 sampler.mWrapT = mWrapT;
353 sampler.mWrapR = mWrapR;
354 sampler.mAniso = mAniso;
355 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700356 }
357 }
358
359}
360