blob: 0df1012d753393bad13d3815a9f02f1df2d2fe9c [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),
42 CLAMP (4);
43
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
Jason Sams0835d422009-08-04 17:58:23 -070057 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070058 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070059 }
60
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070061 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070062 * @return minification setting for the sampler
63 */
64 public Value getMinification() {
65 return mMin;
66 }
67
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070069 * @return magnification setting for the sampler
70 */
71 public Value getMagnification() {
72 return mMag;
73 }
74
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070075 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070076 * @return S wrapping mode for the sampler
77 */
78 public Value getWrapS() {
79 return mWrapS;
80 }
81
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070082 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070083 * @return T wrapping mode for the sampler
84 */
85 public Value getWrapT() {
86 return mWrapT;
87 }
88
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070089 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070090 * @return anisotropy setting for the sampler
91 */
92 public float getAnisotropy() {
93 return mAniso;
94 }
95
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -080097 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
98 * clamp.
99 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800100 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800101 *
102 * @return Sampler
103 */
Jason Sams4d339932010-05-11 14:03:58 -0700104 public static Sampler CLAMP_NEAREST(RenderScript rs) {
105 if(rs.mSampler_CLAMP_NEAREST == null) {
106 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800107 b.setMinification(Value.NEAREST);
108 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700109 b.setWrapS(Value.CLAMP);
110 b.setWrapT(Value.CLAMP);
111 rs.mSampler_CLAMP_NEAREST = b.create();
112 }
113 return rs.mSampler_CLAMP_NEAREST;
114 }
115
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700116 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800117 * Retrieve a sampler with min and mag set to linear and wrap modes set to
118 * clamp.
119 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800120 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800121 *
122 * @return Sampler
123 */
Jason Sams4d339932010-05-11 14:03:58 -0700124 public static Sampler CLAMP_LINEAR(RenderScript rs) {
125 if(rs.mSampler_CLAMP_LINEAR == null) {
126 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800127 b.setMinification(Value.LINEAR);
128 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700129 b.setWrapS(Value.CLAMP);
130 b.setWrapT(Value.CLAMP);
131 rs.mSampler_CLAMP_LINEAR = b.create();
132 }
133 return rs.mSampler_CLAMP_LINEAR;
134 }
135
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700136 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
138 * to and wrap modes set to clamp.
139 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800140 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800141 *
142 * @return Sampler
143 */
Jason Sams4d339932010-05-11 14:03:58 -0700144 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
145 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
146 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800147 b.setMinification(Value.LINEAR_MIP_LINEAR);
148 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700149 b.setWrapS(Value.CLAMP);
150 b.setWrapT(Value.CLAMP);
151 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
152 }
153 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
154 }
155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700156 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800157 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
158 * wrap.
159 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800160 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800161 *
162 * @return Sampler
163 */
Jason Sams4d339932010-05-11 14:03:58 -0700164 public static Sampler WRAP_NEAREST(RenderScript rs) {
165 if(rs.mSampler_WRAP_NEAREST == null) {
166 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800167 b.setMinification(Value.NEAREST);
168 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700169 b.setWrapS(Value.WRAP);
170 b.setWrapT(Value.WRAP);
171 rs.mSampler_WRAP_NEAREST = b.create();
172 }
173 return rs.mSampler_WRAP_NEAREST;
174 }
175
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700176 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800177 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
178 * wrap.
179 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800180 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800181 *
182 * @return Sampler
183 */
Jason Sams4d339932010-05-11 14:03:58 -0700184 public static Sampler WRAP_LINEAR(RenderScript rs) {
185 if(rs.mSampler_WRAP_LINEAR == null) {
186 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800187 b.setMinification(Value.LINEAR);
188 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700189 b.setWrapS(Value.WRAP);
190 b.setWrapT(Value.WRAP);
191 rs.mSampler_WRAP_LINEAR = b.create();
192 }
193 return rs.mSampler_WRAP_LINEAR;
194 }
195
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700196 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800197 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
198 * to and wrap modes set to wrap.
199 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800200 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800201 *
202 * @return Sampler
203 */
Jason Sams4d339932010-05-11 14:03:58 -0700204 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
205 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
206 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800207 b.setMinification(Value.LINEAR_MIP_LINEAR);
208 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700209 b.setWrapS(Value.WRAP);
210 b.setWrapT(Value.WRAP);
211 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
212 }
213 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
214 }
215
216
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700217 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800218 * Builder for creating non-standard samplers. Usefull if mix and match of
219 * wrap modes is necesary or if anisotropic filtering is desired.
220 *
221 */
Jason Sams0835d422009-08-04 17:58:23 -0700222 public static class Builder {
223 RenderScript mRS;
224 Value mMin;
225 Value mMag;
226 Value mWrapS;
227 Value mWrapT;
228 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700229 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700230
231 public Builder(RenderScript rs) {
232 mRS = rs;
233 mMin = Value.NEAREST;
234 mMag = Value.NEAREST;
235 mWrapS = Value.WRAP;
236 mWrapT = Value.WRAP;
237 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700238 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700239 }
240
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800241 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800242 if (v == Value.NEAREST ||
243 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800244 v == Value.LINEAR_MIP_LINEAR ||
245 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800246 mMin = v;
247 } else {
248 throw new IllegalArgumentException("Invalid value");
249 }
Jason Sams0835d422009-08-04 17:58:23 -0700250 }
251
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800252 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800253 if (v == Value.NEAREST || v == Value.LINEAR) {
254 mMag = v;
255 } else {
256 throw new IllegalArgumentException("Invalid value");
257 }
Jason Sams0835d422009-08-04 17:58:23 -0700258 }
259
260 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800261 if (v == Value.WRAP || v == Value.CLAMP) {
262 mWrapS = v;
263 } else {
264 throw new IllegalArgumentException("Invalid value");
265 }
Jason Sams0835d422009-08-04 17:58:23 -0700266 }
267
268 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800269 if (v == Value.WRAP || v == Value.CLAMP) {
270 mWrapT = v;
271 } else {
272 throw new IllegalArgumentException("Invalid value");
273 }
Jason Sams0835d422009-08-04 17:58:23 -0700274 }
275
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700276 public void setAnisotropy(float v) {
277 if(v >= 0.0f) {
278 mAniso = v;
279 } else {
280 throw new IllegalArgumentException("Invalid value");
281 }
282 }
283
Jason Sams0835d422009-08-04 17:58:23 -0700284 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800285 mRS.validate();
Alex Sakhartchouk918e8402012-04-11 14:04:23 -0700286 int id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700287 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
288 Sampler sampler = new Sampler(id, mRS);
289 sampler.mMin = mMin;
290 sampler.mMag = mMag;
291 sampler.mWrapS = mWrapS;
292 sampler.mWrapT = mWrapT;
293 sampler.mWrapR = mWrapR;
294 sampler.mAniso = mAniso;
295 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700296 }
297 }
298
299}
300