Sampler.java revision bf6ef8d78fffbce6c1849a4a28fb3f4401ad039e
1/*
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 *
34 * 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.
37 **/
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) {
53        super(id, rs);
54    }
55
56    /**
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     */
64    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
76    /**
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     */
84    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
96    /**
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     */
104    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);
108            b.setMag(Value.LINEAR);
109            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
116    /**
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     */
124    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
136    /**
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     */
144    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
156    /**
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     */
164    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);
168            b.setMag(Value.LINEAR);
169            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
177    /**
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     */
182    public static class Builder {
183        RenderScript mRS;
184        Value mMin;
185        Value mMag;
186        Value mWrapS;
187        Value mWrapT;
188        Value mWrapR;
189        float mAniso;
190
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;
198            mAniso = 1.0f;
199        }
200
201        public void setMin(Value v) {
202            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            }
209        }
210
211        public void setMag(Value v) {
212            if (v == Value.NEAREST || v == Value.LINEAR) {
213                mMag = v;
214            } else {
215                throw new IllegalArgumentException("Invalid value");
216            }
217        }
218
219        public void setWrapS(Value v) {
220            if (v == Value.WRAP || v == Value.CLAMP) {
221                mWrapS = v;
222            } else {
223                throw new IllegalArgumentException("Invalid value");
224            }
225        }
226
227        public void setWrapT(Value v) {
228            if (v == Value.WRAP || v == Value.CLAMP) {
229                mWrapT = v;
230            } else {
231                throw new IllegalArgumentException("Invalid value");
232            }
233        }
234
235        public void setWrapR(Value v) {
236            if (v == Value.WRAP || v == Value.CLAMP) {
237                mWrapR = v;
238            } else {
239                throw new IllegalArgumentException("Invalid value");
240            }
241        }
242
243        public void setAnisotropy(float v) {
244            if(v >= 0.0f) {
245                mAniso = v;
246            } else {
247                throw new IllegalArgumentException("Invalid value");
248            }
249        }
250
251        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);
258            rs.nSamplerSet2(5, b.mAniso);
259            int id = rs.nSamplerCreate();
260            return new Sampler(id, rs);
261        }
262
263        public Sampler create() {
264            mRS.validate();
265            return internalCreate(mRS, this);
266        }
267    }
268
269}
270
271