blob: 45a3949a2b3410abc69be83dffcab575024358eb [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),
Alex Sakhartchouk08571962010-12-15 09:59:58 -080043 LINEAR_MIP_NEAREST (5),
Jason Sams0835d422009-08-04 17:58:23 -070044 WRAP (3),
45 CLAMP (4);
46
47 int mID;
48 Value(int id) {
49 mID = id;
50 }
51 }
52
53 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070054 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070055 }
56
Jason Samsbf6ef8d2010-12-06 15:59:59 -080057 /**
58 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
59 * clamp.
60 *
61 * @param rs
62 *
63 * @return Sampler
64 */
Jason Sams4d339932010-05-11 14:03:58 -070065 public static Sampler CLAMP_NEAREST(RenderScript rs) {
66 if(rs.mSampler_CLAMP_NEAREST == null) {
67 Builder b = new Builder(rs);
68 b.setMin(Value.NEAREST);
69 b.setMag(Value.NEAREST);
70 b.setWrapS(Value.CLAMP);
71 b.setWrapT(Value.CLAMP);
72 rs.mSampler_CLAMP_NEAREST = b.create();
73 }
74 return rs.mSampler_CLAMP_NEAREST;
75 }
76
Jason Samsbf6ef8d2010-12-06 15:59:59 -080077 /**
78 * Retrieve a sampler with min and mag set to linear and wrap modes set to
79 * clamp.
80 *
81 * @param rs
82 *
83 * @return Sampler
84 */
Jason Sams4d339932010-05-11 14:03:58 -070085 public static Sampler CLAMP_LINEAR(RenderScript rs) {
86 if(rs.mSampler_CLAMP_LINEAR == null) {
87 Builder b = new Builder(rs);
88 b.setMin(Value.LINEAR);
89 b.setMag(Value.LINEAR);
90 b.setWrapS(Value.CLAMP);
91 b.setWrapT(Value.CLAMP);
92 rs.mSampler_CLAMP_LINEAR = b.create();
93 }
94 return rs.mSampler_CLAMP_LINEAR;
95 }
96
Jason Samsbf6ef8d2010-12-06 15:59:59 -080097 /**
98 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
99 * to and wrap modes set to clamp.
100 *
101 * @param rs
102 *
103 * @return Sampler
104 */
Jason Sams4d339932010-05-11 14:03:58 -0700105 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
106 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
107 Builder b = new Builder(rs);
108 b.setMin(Value.LINEAR_MIP_LINEAR);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700109 b.setMag(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700110 b.setWrapS(Value.CLAMP);
111 b.setWrapT(Value.CLAMP);
112 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
113 }
114 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
115 }
116
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800117 /**
118 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
119 * wrap.
120 *
121 * @param rs
122 *
123 * @return Sampler
124 */
Jason Sams4d339932010-05-11 14:03:58 -0700125 public static Sampler WRAP_NEAREST(RenderScript rs) {
126 if(rs.mSampler_WRAP_NEAREST == null) {
127 Builder b = new Builder(rs);
128 b.setMin(Value.NEAREST);
129 b.setMag(Value.NEAREST);
130 b.setWrapS(Value.WRAP);
131 b.setWrapT(Value.WRAP);
132 rs.mSampler_WRAP_NEAREST = b.create();
133 }
134 return rs.mSampler_WRAP_NEAREST;
135 }
136
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 /**
138 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
139 * wrap.
140 *
141 * @param rs
142 *
143 * @return Sampler
144 */
Jason Sams4d339932010-05-11 14:03:58 -0700145 public static Sampler WRAP_LINEAR(RenderScript rs) {
146 if(rs.mSampler_WRAP_LINEAR == null) {
147 Builder b = new Builder(rs);
148 b.setMin(Value.LINEAR);
149 b.setMag(Value.LINEAR);
150 b.setWrapS(Value.WRAP);
151 b.setWrapT(Value.WRAP);
152 rs.mSampler_WRAP_LINEAR = b.create();
153 }
154 return rs.mSampler_WRAP_LINEAR;
155 }
156
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800157 /**
158 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
159 * to and wrap modes set to wrap.
160 *
161 * @param rs
162 *
163 * @return Sampler
164 */
Jason Sams4d339932010-05-11 14:03:58 -0700165 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
166 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
167 Builder b = new Builder(rs);
168 b.setMin(Value.LINEAR_MIP_LINEAR);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700169 b.setMag(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700170 b.setWrapS(Value.WRAP);
171 b.setWrapT(Value.WRAP);
172 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
173 }
174 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
175 }
176
177
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800178 /**
179 * Builder for creating non-standard samplers. Usefull if mix and match of
180 * wrap modes is necesary or if anisotropic filtering is desired.
181 *
182 */
Jason Sams0835d422009-08-04 17:58:23 -0700183 public static class Builder {
184 RenderScript mRS;
185 Value mMin;
186 Value mMag;
187 Value mWrapS;
188 Value mWrapT;
189 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700190 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700191
192 public Builder(RenderScript rs) {
193 mRS = rs;
194 mMin = Value.NEAREST;
195 mMag = Value.NEAREST;
196 mWrapS = Value.WRAP;
197 mWrapT = Value.WRAP;
198 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700199 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700200 }
201
202 public void setMin(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800203 if (v == Value.NEAREST ||
204 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800205 v == Value.LINEAR_MIP_LINEAR ||
206 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800207 mMin = v;
208 } else {
209 throw new IllegalArgumentException("Invalid value");
210 }
Jason Sams0835d422009-08-04 17:58:23 -0700211 }
212
213 public void setMag(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800214 if (v == Value.NEAREST || v == Value.LINEAR) {
215 mMag = v;
216 } else {
217 throw new IllegalArgumentException("Invalid value");
218 }
Jason Sams0835d422009-08-04 17:58:23 -0700219 }
220
221 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800222 if (v == Value.WRAP || v == Value.CLAMP) {
223 mWrapS = v;
224 } else {
225 throw new IllegalArgumentException("Invalid value");
226 }
Jason Sams0835d422009-08-04 17:58:23 -0700227 }
228
229 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800230 if (v == Value.WRAP || v == Value.CLAMP) {
231 mWrapT = v;
232 } else {
233 throw new IllegalArgumentException("Invalid value");
234 }
Jason Sams0835d422009-08-04 17:58:23 -0700235 }
236
237 public void setWrapR(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800238 if (v == Value.WRAP || v == Value.CLAMP) {
239 mWrapR = v;
240 } else {
241 throw new IllegalArgumentException("Invalid value");
242 }
Jason Sams0835d422009-08-04 17:58:23 -0700243 }
244
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700245 public void setAnisotropy(float v) {
246 if(v >= 0.0f) {
247 mAniso = v;
248 } else {
249 throw new IllegalArgumentException("Invalid value");
250 }
251 }
252
Jason Sams0835d422009-08-04 17:58:23 -0700253 static synchronized Sampler internalCreate(RenderScript rs, Builder b) {
254 rs.nSamplerBegin();
255 rs.nSamplerSet(0, b.mMin.mID);
256 rs.nSamplerSet(1, b.mMag.mID);
257 rs.nSamplerSet(2, b.mWrapS.mID);
258 rs.nSamplerSet(3, b.mWrapT.mID);
259 rs.nSamplerSet(4, b.mWrapR.mID);
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700260 rs.nSamplerSet2(5, b.mAniso);
Jason Sams0835d422009-08-04 17:58:23 -0700261 int id = rs.nSamplerCreate();
262 return new Sampler(id, rs);
263 }
264
265 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800266 mRS.validate();
Jason Sams0835d422009-08-04 17:58:23 -0700267 return internalCreate(mRS, this);
268 }
269 }
270
271}
272