blob: 9fbc09ac287c6dde38c97370b06314b5b490ea13 [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;
25import android.util.Config;
26import android.util.Log;
27
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30
31/**
32 * @hide
33 *
Jason Samsbf6ef8d2010-12-06 15:59:59 -080034 * Sampler object which defines how data is extracted from textures. Samplers
35 * are attached to Program objects (currently only fragment) when those objects
36 * need to access texture data.
Jason Sams0835d422009-08-04 17:58:23 -070037 **/
38public class Sampler extends BaseObj {
39 public enum Value {
40 NEAREST (0),
41 LINEAR (1),
42 LINEAR_MIP_LINEAR (2),
43 WRAP (3),
44 CLAMP (4);
45
46 int mID;
47 Value(int id) {
48 mID = id;
49 }
50 }
51
52 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070053 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070054 }
55
Jason Samsbf6ef8d2010-12-06 15:59:59 -080056 /**
57 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
58 * clamp.
59 *
60 * @param rs
61 *
62 * @return Sampler
63 */
Jason Sams4d339932010-05-11 14:03:58 -070064 public static Sampler CLAMP_NEAREST(RenderScript rs) {
65 if(rs.mSampler_CLAMP_NEAREST == null) {
66 Builder b = new Builder(rs);
67 b.setMin(Value.NEAREST);
68 b.setMag(Value.NEAREST);
69 b.setWrapS(Value.CLAMP);
70 b.setWrapT(Value.CLAMP);
71 rs.mSampler_CLAMP_NEAREST = b.create();
72 }
73 return rs.mSampler_CLAMP_NEAREST;
74 }
75
Jason Samsbf6ef8d2010-12-06 15:59:59 -080076 /**
77 * Retrieve a sampler with min and mag set to linear and wrap modes set to
78 * clamp.
79 *
80 * @param rs
81 *
82 * @return Sampler
83 */
Jason Sams4d339932010-05-11 14:03:58 -070084 public static Sampler CLAMP_LINEAR(RenderScript rs) {
85 if(rs.mSampler_CLAMP_LINEAR == null) {
86 Builder b = new Builder(rs);
87 b.setMin(Value.LINEAR);
88 b.setMag(Value.LINEAR);
89 b.setWrapS(Value.CLAMP);
90 b.setWrapT(Value.CLAMP);
91 rs.mSampler_CLAMP_LINEAR = b.create();
92 }
93 return rs.mSampler_CLAMP_LINEAR;
94 }
95
Jason Samsbf6ef8d2010-12-06 15:59:59 -080096 /**
97 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
98 * to and wrap modes set to clamp.
99 *
100 * @param rs
101 *
102 * @return Sampler
103 */
Jason Sams4d339932010-05-11 14:03:58 -0700104 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
105 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
106 Builder b = new Builder(rs);
107 b.setMin(Value.LINEAR_MIP_LINEAR);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700108 b.setMag(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700109 b.setWrapS(Value.CLAMP);
110 b.setWrapT(Value.CLAMP);
111 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
112 }
113 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
114 }
115
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800116 /**
117 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
118 * wrap.
119 *
120 * @param rs
121 *
122 * @return Sampler
123 */
Jason Sams4d339932010-05-11 14:03:58 -0700124 public static Sampler WRAP_NEAREST(RenderScript rs) {
125 if(rs.mSampler_WRAP_NEAREST == null) {
126 Builder b = new Builder(rs);
127 b.setMin(Value.NEAREST);
128 b.setMag(Value.NEAREST);
129 b.setWrapS(Value.WRAP);
130 b.setWrapT(Value.WRAP);
131 rs.mSampler_WRAP_NEAREST = b.create();
132 }
133 return rs.mSampler_WRAP_NEAREST;
134 }
135
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800136 /**
137 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
138 * wrap.
139 *
140 * @param rs
141 *
142 * @return Sampler
143 */
Jason Sams4d339932010-05-11 14:03:58 -0700144 public static Sampler WRAP_LINEAR(RenderScript rs) {
145 if(rs.mSampler_WRAP_LINEAR == null) {
146 Builder b = new Builder(rs);
147 b.setMin(Value.LINEAR);
148 b.setMag(Value.LINEAR);
149 b.setWrapS(Value.WRAP);
150 b.setWrapT(Value.WRAP);
151 rs.mSampler_WRAP_LINEAR = b.create();
152 }
153 return rs.mSampler_WRAP_LINEAR;
154 }
155
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800156 /**
157 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
158 * to and wrap modes set to wrap.
159 *
160 * @param rs
161 *
162 * @return Sampler
163 */
Jason Sams4d339932010-05-11 14:03:58 -0700164 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
165 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
166 Builder b = new Builder(rs);
167 b.setMin(Value.LINEAR_MIP_LINEAR);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700168 b.setMag(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700169 b.setWrapS(Value.WRAP);
170 b.setWrapT(Value.WRAP);
171 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
172 }
173 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
174 }
175
176
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800177 /**
178 * Builder for creating non-standard samplers. Usefull if mix and match of
179 * wrap modes is necesary or if anisotropic filtering is desired.
180 *
181 */
Jason Sams0835d422009-08-04 17:58:23 -0700182 public static class Builder {
183 RenderScript mRS;
184 Value mMin;
185 Value mMag;
186 Value mWrapS;
187 Value mWrapT;
188 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700189 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700190
191 public Builder(RenderScript rs) {
192 mRS = rs;
193 mMin = Value.NEAREST;
194 mMag = Value.NEAREST;
195 mWrapS = Value.WRAP;
196 mWrapT = Value.WRAP;
197 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700198 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700199 }
200
201 public void setMin(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800202 if (v == Value.NEAREST ||
203 v == Value.LINEAR ||
204 v == Value.LINEAR_MIP_LINEAR) {
205 mMin = v;
206 } else {
207 throw new IllegalArgumentException("Invalid value");
208 }
Jason Sams0835d422009-08-04 17:58:23 -0700209 }
210
211 public void setMag(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800212 if (v == Value.NEAREST || v == Value.LINEAR) {
213 mMag = v;
214 } else {
215 throw new IllegalArgumentException("Invalid value");
216 }
Jason Sams0835d422009-08-04 17:58:23 -0700217 }
218
219 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800220 if (v == Value.WRAP || v == Value.CLAMP) {
221 mWrapS = v;
222 } else {
223 throw new IllegalArgumentException("Invalid value");
224 }
Jason Sams0835d422009-08-04 17:58:23 -0700225 }
226
227 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800228 if (v == Value.WRAP || v == Value.CLAMP) {
229 mWrapT = v;
230 } else {
231 throw new IllegalArgumentException("Invalid value");
232 }
Jason Sams0835d422009-08-04 17:58:23 -0700233 }
234
235 public void setWrapR(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800236 if (v == Value.WRAP || v == Value.CLAMP) {
237 mWrapR = v;
238 } else {
239 throw new IllegalArgumentException("Invalid value");
240 }
Jason Sams0835d422009-08-04 17:58:23 -0700241 }
242
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700243 public void setAnisotropy(float v) {
244 if(v >= 0.0f) {
245 mAniso = v;
246 } else {
247 throw new IllegalArgumentException("Invalid value");
248 }
249 }
250
Jason Sams0835d422009-08-04 17:58:23 -0700251 static synchronized Sampler internalCreate(RenderScript rs, Builder b) {
252 rs.nSamplerBegin();
253 rs.nSamplerSet(0, b.mMin.mID);
254 rs.nSamplerSet(1, b.mMag.mID);
255 rs.nSamplerSet(2, b.mWrapS.mID);
256 rs.nSamplerSet(3, b.mWrapT.mID);
257 rs.nSamplerSet(4, b.mWrapR.mID);
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700258 rs.nSamplerSet2(5, b.mAniso);
Jason Sams0835d422009-08-04 17:58:23 -0700259 int id = rs.nSamplerCreate();
260 return new Sampler(id, rs);
261 }
262
263 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800264 mRS.validate();
Jason Sams0835d422009-08-04 17:58:23 -0700265 return internalCreate(mRS, this);
266 }
267 }
268
269}
270