blob: c656d75013f1ec8915e524c4d47aebc28c45715d [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/**
Jason Samsbf6ef8d2010-12-06 15:59:59 -080032 * Sampler object which defines how data is extracted from textures. Samplers
33 * are attached to Program objects (currently only fragment) when those objects
34 * need to access texture data.
Jason Sams0835d422009-08-04 17:58:23 -070035 **/
36public class Sampler extends BaseObj {
37 public enum Value {
38 NEAREST (0),
39 LINEAR (1),
40 LINEAR_MIP_LINEAR (2),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080041 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070042 WRAP (3),
43 CLAMP (4);
44
45 int mID;
46 Value(int id) {
47 mID = id;
48 }
49 }
50
51 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070052 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070053 }
54
Jason Samsbf6ef8d2010-12-06 15:59:59 -080055 /**
56 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
57 * clamp.
58 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080059 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080060 *
61 * @return Sampler
62 */
Jason Sams4d339932010-05-11 14:03:58 -070063 public static Sampler CLAMP_NEAREST(RenderScript rs) {
64 if(rs.mSampler_CLAMP_NEAREST == null) {
65 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080066 b.setMinification(Value.NEAREST);
67 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -070068 b.setWrapS(Value.CLAMP);
69 b.setWrapT(Value.CLAMP);
70 rs.mSampler_CLAMP_NEAREST = b.create();
71 }
72 return rs.mSampler_CLAMP_NEAREST;
73 }
74
Jason Samsbf6ef8d2010-12-06 15:59:59 -080075 /**
76 * Retrieve a sampler with min and mag set to linear and wrap modes set to
77 * clamp.
78 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080079 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080080 *
81 * @return Sampler
82 */
Jason Sams4d339932010-05-11 14:03:58 -070083 public static Sampler CLAMP_LINEAR(RenderScript rs) {
84 if(rs.mSampler_CLAMP_LINEAR == null) {
85 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080086 b.setMinification(Value.LINEAR);
87 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -070088 b.setWrapS(Value.CLAMP);
89 b.setWrapT(Value.CLAMP);
90 rs.mSampler_CLAMP_LINEAR = b.create();
91 }
92 return rs.mSampler_CLAMP_LINEAR;
93 }
94
Jason Samsbf6ef8d2010-12-06 15:59:59 -080095 /**
96 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
97 * to and wrap modes set to clamp.
98 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080099 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800100 *
101 * @return Sampler
102 */
Jason Sams4d339932010-05-11 14:03:58 -0700103 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
104 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
105 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800106 b.setMinification(Value.LINEAR_MIP_LINEAR);
107 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700108 b.setWrapS(Value.CLAMP);
109 b.setWrapT(Value.CLAMP);
110 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
111 }
112 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
113 }
114
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800115 /**
116 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
117 * wrap.
118 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800119 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800120 *
121 * @return Sampler
122 */
Jason Sams4d339932010-05-11 14:03:58 -0700123 public static Sampler WRAP_NEAREST(RenderScript rs) {
124 if(rs.mSampler_WRAP_NEAREST == null) {
125 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800126 b.setMinification(Value.NEAREST);
127 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700128 b.setWrapS(Value.WRAP);
129 b.setWrapT(Value.WRAP);
130 rs.mSampler_WRAP_NEAREST = b.create();
131 }
132 return rs.mSampler_WRAP_NEAREST;
133 }
134
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800135 /**
136 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
137 * wrap.
138 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800139 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800140 *
141 * @return Sampler
142 */
Jason Sams4d339932010-05-11 14:03:58 -0700143 public static Sampler WRAP_LINEAR(RenderScript rs) {
144 if(rs.mSampler_WRAP_LINEAR == null) {
145 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800146 b.setMinification(Value.LINEAR);
147 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700148 b.setWrapS(Value.WRAP);
149 b.setWrapT(Value.WRAP);
150 rs.mSampler_WRAP_LINEAR = b.create();
151 }
152 return rs.mSampler_WRAP_LINEAR;
153 }
154
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800155 /**
156 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
157 * to and wrap modes set to wrap.
158 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800159 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800160 *
161 * @return Sampler
162 */
Jason Sams4d339932010-05-11 14:03:58 -0700163 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
164 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
165 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800166 b.setMinification(Value.LINEAR_MIP_LINEAR);
167 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700168 b.setWrapS(Value.WRAP);
169 b.setWrapT(Value.WRAP);
170 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
171 }
172 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
173 }
174
175
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800176 /**
177 * Builder for creating non-standard samplers. Usefull if mix and match of
178 * wrap modes is necesary or if anisotropic filtering is desired.
179 *
180 */
Jason Sams0835d422009-08-04 17:58:23 -0700181 public static class Builder {
182 RenderScript mRS;
183 Value mMin;
184 Value mMag;
185 Value mWrapS;
186 Value mWrapT;
187 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700188 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700189
190 public Builder(RenderScript rs) {
191 mRS = rs;
192 mMin = Value.NEAREST;
193 mMag = Value.NEAREST;
194 mWrapS = Value.WRAP;
195 mWrapT = Value.WRAP;
196 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700197 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700198 }
199
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800200 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800201 if (v == Value.NEAREST ||
202 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800203 v == Value.LINEAR_MIP_LINEAR ||
204 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800205 mMin = v;
206 } else {
207 throw new IllegalArgumentException("Invalid value");
208 }
Jason Sams0835d422009-08-04 17:58:23 -0700209 }
210
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800211 public void setMagnification(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
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700235 public void setAnisotropy(float v) {
236 if(v >= 0.0f) {
237 mAniso = v;
238 } else {
239 throw new IllegalArgumentException("Invalid value");
240 }
241 }
242
Jason Sams0835d422009-08-04 17:58:23 -0700243 static synchronized Sampler internalCreate(RenderScript rs, Builder b) {
244 rs.nSamplerBegin();
245 rs.nSamplerSet(0, b.mMin.mID);
246 rs.nSamplerSet(1, b.mMag.mID);
247 rs.nSamplerSet(2, b.mWrapS.mID);
248 rs.nSamplerSet(3, b.mWrapT.mID);
249 rs.nSamplerSet(4, b.mWrapR.mID);
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700250 rs.nSamplerSet2(5, b.mAniso);
Jason Sams0835d422009-08-04 17:58:23 -0700251 int id = rs.nSamplerCreate();
252 return new Sampler(id, rs);
253 }
254
255 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800256 mRS.validate();
Jason Sams0835d422009-08-04 17:58:23 -0700257 return internalCreate(mRS, this);
258 }
259 }
260
261}
262