blob: 0a3c91daa18ae9e7e800a819efc8bdcdfeff3d7e [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
30/**
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
Jason Samsbf6ef8d2010-12-06 15:59:59 -080061 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -070062 * @hide
63 * @return minification setting for the sampler
64 */
65 public Value getMinification() {
66 return mMin;
67 }
68
69 /**
70 * @hide
71 * @return magnification setting for the sampler
72 */
73 public Value getMagnification() {
74 return mMag;
75 }
76
77 /**
78 * @hide
79 * @return S wrapping mode for the sampler
80 */
81 public Value getWrapS() {
82 return mWrapS;
83 }
84
85 /**
86 * @hide
87 * @return T wrapping mode for the sampler
88 */
89 public Value getWrapT() {
90 return mWrapT;
91 }
92
93 /**
94 * @hide
95 * @return anisotropy setting for the sampler
96 */
97 public float getAnisotropy() {
98 return mAniso;
99 }
100
101 /**
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800102 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
103 * clamp.
104 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800105 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800106 *
107 * @return Sampler
108 */
Jason Sams4d339932010-05-11 14:03:58 -0700109 public static Sampler CLAMP_NEAREST(RenderScript rs) {
110 if(rs.mSampler_CLAMP_NEAREST == null) {
111 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800112 b.setMinification(Value.NEAREST);
113 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700114 b.setWrapS(Value.CLAMP);
115 b.setWrapT(Value.CLAMP);
116 rs.mSampler_CLAMP_NEAREST = b.create();
117 }
118 return rs.mSampler_CLAMP_NEAREST;
119 }
120
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800121 /**
122 * 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 Samsbf6ef8d2010-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) {
130 if(rs.mSampler_CLAMP_LINEAR == null) {
131 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800132 b.setMinification(Value.LINEAR);
133 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700134 b.setWrapS(Value.CLAMP);
135 b.setWrapT(Value.CLAMP);
136 rs.mSampler_CLAMP_LINEAR = b.create();
137 }
138 return rs.mSampler_CLAMP_LINEAR;
139 }
140
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800141 /**
142 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
143 * to and wrap modes set to clamp.
144 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800145 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800146 *
147 * @return Sampler
148 */
Jason Sams4d339932010-05-11 14:03:58 -0700149 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
150 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
151 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800152 b.setMinification(Value.LINEAR_MIP_LINEAR);
153 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700154 b.setWrapS(Value.CLAMP);
155 b.setWrapT(Value.CLAMP);
156 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
157 }
158 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
159 }
160
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800161 /**
162 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
163 * wrap.
164 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800165 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800166 *
167 * @return Sampler
168 */
Jason Sams4d339932010-05-11 14:03:58 -0700169 public static Sampler WRAP_NEAREST(RenderScript rs) {
170 if(rs.mSampler_WRAP_NEAREST == null) {
171 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800172 b.setMinification(Value.NEAREST);
173 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700174 b.setWrapS(Value.WRAP);
175 b.setWrapT(Value.WRAP);
176 rs.mSampler_WRAP_NEAREST = b.create();
177 }
178 return rs.mSampler_WRAP_NEAREST;
179 }
180
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800181 /**
182 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
183 * wrap.
184 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800185 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800186 *
187 * @return Sampler
188 */
Jason Sams4d339932010-05-11 14:03:58 -0700189 public static Sampler WRAP_LINEAR(RenderScript rs) {
190 if(rs.mSampler_WRAP_LINEAR == null) {
191 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800192 b.setMinification(Value.LINEAR);
193 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700194 b.setWrapS(Value.WRAP);
195 b.setWrapT(Value.WRAP);
196 rs.mSampler_WRAP_LINEAR = b.create();
197 }
198 return rs.mSampler_WRAP_LINEAR;
199 }
200
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800201 /**
202 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
203 * to and wrap modes set to wrap.
204 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800205 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800206 *
207 * @return Sampler
208 */
Jason Sams4d339932010-05-11 14:03:58 -0700209 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
210 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
211 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800212 b.setMinification(Value.LINEAR_MIP_LINEAR);
213 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700214 b.setWrapS(Value.WRAP);
215 b.setWrapT(Value.WRAP);
216 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
217 }
218 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
219 }
220
221
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800222 /**
223 * Builder for creating non-standard samplers. Usefull if mix and match of
224 * wrap modes is necesary or if anisotropic filtering is desired.
225 *
226 */
Jason Sams0835d422009-08-04 17:58:23 -0700227 public static class Builder {
228 RenderScript mRS;
229 Value mMin;
230 Value mMag;
231 Value mWrapS;
232 Value mWrapT;
233 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700234 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700235
236 public Builder(RenderScript rs) {
237 mRS = rs;
238 mMin = Value.NEAREST;
239 mMag = Value.NEAREST;
240 mWrapS = Value.WRAP;
241 mWrapT = Value.WRAP;
242 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700243 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700244 }
245
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800246 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800247 if (v == Value.NEAREST ||
248 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800249 v == Value.LINEAR_MIP_LINEAR ||
250 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800251 mMin = v;
252 } else {
253 throw new IllegalArgumentException("Invalid value");
254 }
Jason Sams0835d422009-08-04 17:58:23 -0700255 }
256
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800257 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800258 if (v == Value.NEAREST || v == Value.LINEAR) {
259 mMag = v;
260 } else {
261 throw new IllegalArgumentException("Invalid value");
262 }
Jason Sams0835d422009-08-04 17:58:23 -0700263 }
264
265 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800266 if (v == Value.WRAP || v == Value.CLAMP) {
267 mWrapS = v;
268 } else {
269 throw new IllegalArgumentException("Invalid value");
270 }
Jason Sams0835d422009-08-04 17:58:23 -0700271 }
272
273 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800274 if (v == Value.WRAP || v == Value.CLAMP) {
275 mWrapT = v;
276 } else {
277 throw new IllegalArgumentException("Invalid value");
278 }
Jason Sams0835d422009-08-04 17:58:23 -0700279 }
280
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700281 public void setAnisotropy(float v) {
282 if(v >= 0.0f) {
283 mAniso = v;
284 } else {
285 throw new IllegalArgumentException("Invalid value");
286 }
287 }
288
Jason Sams0835d422009-08-04 17:58:23 -0700289 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800290 mRS.validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700291 int id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
292 mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
293 Sampler sampler = new Sampler(id, mRS);
294 sampler.mMin = mMin;
295 sampler.mMag = mMag;
296 sampler.mWrapS = mWrapS;
297 sampler.mWrapT = mWrapT;
298 sampler.mWrapR = mWrapR;
299 sampler.mAniso = mAniso;
300 return sampler;
Jason Sams0835d422009-08-04 17:58:23 -0700301 }
302 }
303
304}
305