blob: 98943a159cc3c13d6fad03b7ffb7ef5e3f684c0c [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
50 Sampler(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070051 super(id, rs);
Jason Sams0835d422009-08-04 17:58:23 -070052 }
53
Jason Samsbf6ef8d2010-12-06 15:59:59 -080054 /**
55 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
56 * clamp.
57 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080058 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080059 *
60 * @return Sampler
61 */
Jason Sams4d339932010-05-11 14:03:58 -070062 public static Sampler CLAMP_NEAREST(RenderScript rs) {
63 if(rs.mSampler_CLAMP_NEAREST == null) {
64 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080065 b.setMinification(Value.NEAREST);
66 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -070067 b.setWrapS(Value.CLAMP);
68 b.setWrapT(Value.CLAMP);
69 rs.mSampler_CLAMP_NEAREST = b.create();
70 }
71 return rs.mSampler_CLAMP_NEAREST;
72 }
73
Jason Samsbf6ef8d2010-12-06 15:59:59 -080074 /**
75 * Retrieve a sampler with min and mag set to linear and wrap modes set to
76 * clamp.
77 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080078 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080079 *
80 * @return Sampler
81 */
Jason Sams4d339932010-05-11 14:03:58 -070082 public static Sampler CLAMP_LINEAR(RenderScript rs) {
83 if(rs.mSampler_CLAMP_LINEAR == null) {
84 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080085 b.setMinification(Value.LINEAR);
86 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -070087 b.setWrapS(Value.CLAMP);
88 b.setWrapT(Value.CLAMP);
89 rs.mSampler_CLAMP_LINEAR = b.create();
90 }
91 return rs.mSampler_CLAMP_LINEAR;
92 }
93
Jason Samsbf6ef8d2010-12-06 15:59:59 -080094 /**
95 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
96 * to and wrap modes set to clamp.
97 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080098 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -080099 *
100 * @return Sampler
101 */
Jason Sams4d339932010-05-11 14:03:58 -0700102 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
103 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
104 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800105 b.setMinification(Value.LINEAR_MIP_LINEAR);
106 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700107 b.setWrapS(Value.CLAMP);
108 b.setWrapT(Value.CLAMP);
109 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
110 }
111 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
112 }
113
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800114 /**
115 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
116 * wrap.
117 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800118 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800119 *
120 * @return Sampler
121 */
Jason Sams4d339932010-05-11 14:03:58 -0700122 public static Sampler WRAP_NEAREST(RenderScript rs) {
123 if(rs.mSampler_WRAP_NEAREST == null) {
124 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800125 b.setMinification(Value.NEAREST);
126 b.setMagnification(Value.NEAREST);
Jason Sams4d339932010-05-11 14:03:58 -0700127 b.setWrapS(Value.WRAP);
128 b.setWrapT(Value.WRAP);
129 rs.mSampler_WRAP_NEAREST = b.create();
130 }
131 return rs.mSampler_WRAP_NEAREST;
132 }
133
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800134 /**
135 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
136 * wrap.
137 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800138 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800139 *
140 * @return Sampler
141 */
Jason Sams4d339932010-05-11 14:03:58 -0700142 public static Sampler WRAP_LINEAR(RenderScript rs) {
143 if(rs.mSampler_WRAP_LINEAR == null) {
144 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800145 b.setMinification(Value.LINEAR);
146 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700147 b.setWrapS(Value.WRAP);
148 b.setWrapT(Value.WRAP);
149 rs.mSampler_WRAP_LINEAR = b.create();
150 }
151 return rs.mSampler_WRAP_LINEAR;
152 }
153
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800154 /**
155 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and
156 * to and wrap modes set to wrap.
157 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800158 * @param rs Context to which the sampler will belong.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800159 *
160 * @return Sampler
161 */
Jason Sams4d339932010-05-11 14:03:58 -0700162 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
163 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
164 Builder b = new Builder(rs);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800165 b.setMinification(Value.LINEAR_MIP_LINEAR);
166 b.setMagnification(Value.LINEAR);
Jason Sams4d339932010-05-11 14:03:58 -0700167 b.setWrapS(Value.WRAP);
168 b.setWrapT(Value.WRAP);
169 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
170 }
171 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
172 }
173
174
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800175 /**
176 * Builder for creating non-standard samplers. Usefull if mix and match of
177 * wrap modes is necesary or if anisotropic filtering is desired.
178 *
179 */
Jason Sams0835d422009-08-04 17:58:23 -0700180 public static class Builder {
181 RenderScript mRS;
182 Value mMin;
183 Value mMag;
184 Value mWrapS;
185 Value mWrapT;
186 Value mWrapR;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700187 float mAniso;
Jason Sams0835d422009-08-04 17:58:23 -0700188
189 public Builder(RenderScript rs) {
190 mRS = rs;
191 mMin = Value.NEAREST;
192 mMag = Value.NEAREST;
193 mWrapS = Value.WRAP;
194 mWrapT = Value.WRAP;
195 mWrapR = Value.WRAP;
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700196 mAniso = 1.0f;
Jason Sams0835d422009-08-04 17:58:23 -0700197 }
198
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800199 public void setMinification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800200 if (v == Value.NEAREST ||
201 v == Value.LINEAR ||
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800202 v == Value.LINEAR_MIP_LINEAR ||
203 v == Value.LINEAR_MIP_NEAREST) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800204 mMin = v;
205 } else {
206 throw new IllegalArgumentException("Invalid value");
207 }
Jason Sams0835d422009-08-04 17:58:23 -0700208 }
209
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800210 public void setMagnification(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800211 if (v == Value.NEAREST || v == Value.LINEAR) {
212 mMag = v;
213 } else {
214 throw new IllegalArgumentException("Invalid value");
215 }
Jason Sams0835d422009-08-04 17:58:23 -0700216 }
217
218 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800219 if (v == Value.WRAP || v == Value.CLAMP) {
220 mWrapS = v;
221 } else {
222 throw new IllegalArgumentException("Invalid value");
223 }
Jason Sams0835d422009-08-04 17:58:23 -0700224 }
225
226 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800227 if (v == Value.WRAP || v == Value.CLAMP) {
228 mWrapT = v;
229 } else {
230 throw new IllegalArgumentException("Invalid value");
231 }
Jason Sams0835d422009-08-04 17:58:23 -0700232 }
233
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700234 public void setAnisotropy(float v) {
235 if(v >= 0.0f) {
236 mAniso = v;
237 } else {
238 throw new IllegalArgumentException("Invalid value");
239 }
240 }
241
Jason Sams0835d422009-08-04 17:58:23 -0700242 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800243 mRS.validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700244 int id = mRS.nSamplerCreate(mMag.mID, mMin.mID, mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
245 return new Sampler(id, mRS);
Jason Sams0835d422009-08-04 17:58:23 -0700246 }
247 }
248
249}
250