• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.renderscript;
18 
19 /**
20  * Sampler object that defines how Allocations can be read as textures within a
21  * kernel. Samplers are used in conjunction with the {@code rsSample} runtime
22  * function to return values from normalized coordinates.
23  *
24  * Any Allocation used with a Sampler must have been created with {@link
25  * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE}; using a Sampler on
26  * an {@link android.renderscript.Allocation} that was not created with {@link
27  * android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE} is undefined.
28  *
29  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
30  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
31  * guide</a> for the proposed alternatives.
32  **/
33 @Deprecated
34 public class Sampler extends BaseObj {
35     public enum Value {
36         NEAREST (0),
37         LINEAR (1),
38         LINEAR_MIP_LINEAR (2),
39         LINEAR_MIP_NEAREST (5),
40         WRAP (3),
41         CLAMP (4),
42         MIRRORED_REPEAT (6);
43 
44         int mID;
Value(int id)45         Value(int id) {
46             mID = id;
47         }
48     }
49 
50     Value mMin;
51     Value mMag;
52     Value mWrapS;
53     Value mWrapT;
54     Value mWrapR;
55     float mAniso;
56 
Sampler(long id, RenderScript rs)57     Sampler(long id, RenderScript rs) {
58         super(id, rs);
59         guard.open("destroy");
60     }
61 
62     /**
63      * @return minification setting for the sampler
64      */
getMinification()65     public Value getMinification() {
66         return mMin;
67     }
68 
69     /**
70      * @return magnification setting for the sampler
71      */
getMagnification()72     public Value getMagnification() {
73         return mMag;
74     }
75 
76     /**
77      * @return S wrapping mode for the sampler
78      */
getWrapS()79     public Value getWrapS() {
80         return mWrapS;
81     }
82 
83     /**
84      * @return T wrapping mode for the sampler
85      */
getWrapT()86     public Value getWrapT() {
87         return mWrapT;
88     }
89 
90     /**
91      * @return anisotropy setting for the sampler
92      */
getAnisotropy()93     public float getAnisotropy() {
94         return mAniso;
95     }
96 
97     /**
98      * Retrieve a sampler with min and mag set to nearest and wrap modes set to
99      * clamp.
100      *
101      * @param rs Context to which the sampler will belong.
102      *
103      * @return Sampler
104      */
CLAMP_NEAREST(RenderScript rs)105     public static Sampler CLAMP_NEAREST(RenderScript rs) {
106         if (rs.mSampler_CLAMP_NEAREST == null) {
107             synchronized (rs) {
108                 if (rs.mSampler_CLAMP_NEAREST == null) {
109                     Builder b = new Builder(rs);
110                     b.setMinification(Value.NEAREST);
111                     b.setMagnification(Value.NEAREST);
112                     b.setWrapS(Value.CLAMP);
113                     b.setWrapT(Value.CLAMP);
114                     rs.mSampler_CLAMP_NEAREST = b.create();
115                 }
116             }
117         }
118         return rs.mSampler_CLAMP_NEAREST;
119     }
120 
121     /**
122      * Retrieve a sampler with min and mag set to linear and wrap modes set to
123      * clamp.
124      *
125      * @param rs Context to which the sampler will belong.
126      *
127      * @return Sampler
128      */
CLAMP_LINEAR(RenderScript rs)129     public static Sampler CLAMP_LINEAR(RenderScript rs) {
130         if (rs.mSampler_CLAMP_LINEAR == null) {
131             synchronized (rs) {
132                 if (rs.mSampler_CLAMP_LINEAR == null) {
133                     Builder b = new Builder(rs);
134                     b.setMinification(Value.LINEAR);
135                     b.setMagnification(Value.LINEAR);
136                     b.setWrapS(Value.CLAMP);
137                     b.setWrapT(Value.CLAMP);
138                     rs.mSampler_CLAMP_LINEAR = b.create();
139                 }
140             }
141         }
142         return rs.mSampler_CLAMP_LINEAR;
143     }
144 
145     /**
146      * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
147      * wrap modes set to clamp.
148      *
149      * @param rs Context to which the sampler will belong.
150      *
151      * @return Sampler
152      */
CLAMP_LINEAR_MIP_LINEAR(RenderScript rs)153     public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
154         if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
155             synchronized (rs) {
156                 if (rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
157                     Builder b = new Builder(rs);
158                     b.setMinification(Value.LINEAR_MIP_LINEAR);
159                     b.setMagnification(Value.LINEAR);
160                     b.setWrapS(Value.CLAMP);
161                     b.setWrapT(Value.CLAMP);
162                     rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
163                 }
164             }
165         }
166         return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
167     }
168 
169     /**
170      * Retrieve a sampler with min and mag set to nearest and wrap modes set to
171      * wrap.
172      *
173      * @param rs Context to which the sampler will belong.
174      *
175      * @return Sampler
176      */
WRAP_NEAREST(RenderScript rs)177     public static Sampler WRAP_NEAREST(RenderScript rs) {
178         if (rs.mSampler_WRAP_NEAREST == null) {
179             synchronized (rs) {
180                 if (rs.mSampler_WRAP_NEAREST == null) {
181                     Builder b = new Builder(rs);
182                     b.setMinification(Value.NEAREST);
183                     b.setMagnification(Value.NEAREST);
184                     b.setWrapS(Value.WRAP);
185                     b.setWrapT(Value.WRAP);
186                     rs.mSampler_WRAP_NEAREST = b.create();
187                 }
188             }
189         }
190         return rs.mSampler_WRAP_NEAREST;
191     }
192 
193     /**
194      * Retrieve a sampler with min and mag set to linear and wrap modes set to
195      * wrap.
196      *
197      * @param rs Context to which the sampler will belong.
198      *
199      * @return Sampler
200      */
WRAP_LINEAR(RenderScript rs)201     public static Sampler WRAP_LINEAR(RenderScript rs) {
202         if (rs.mSampler_WRAP_LINEAR == null) {
203             synchronized (rs) {
204                 if (rs.mSampler_WRAP_LINEAR == null) {
205                     Builder b = new Builder(rs);
206                     b.setMinification(Value.LINEAR);
207                     b.setMagnification(Value.LINEAR);
208                     b.setWrapS(Value.WRAP);
209                     b.setWrapT(Value.WRAP);
210                     rs.mSampler_WRAP_LINEAR = b.create();
211                 }
212             }
213         }
214         return rs.mSampler_WRAP_LINEAR;
215     }
216 
217     /**
218      * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
219      * wrap modes set to wrap.
220      *
221      * @param rs Context to which the sampler will belong.
222      *
223      * @return Sampler
224      */
WRAP_LINEAR_MIP_LINEAR(RenderScript rs)225     public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
226         if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
227             synchronized (rs) {
228                 if (rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
229                     Builder b = new Builder(rs);
230                     b.setMinification(Value.LINEAR_MIP_LINEAR);
231                     b.setMagnification(Value.LINEAR);
232                     b.setWrapS(Value.WRAP);
233                     b.setWrapT(Value.WRAP);
234                     rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
235                 }
236             }
237         }
238         return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
239     }
240 
241     /**
242      * Retrieve a sampler with min and mag set to nearest and wrap modes set to
243      * mirrored repeat.
244      *
245      * @param rs Context to which the sampler will belong.
246      *
247      * @return Sampler
248      */
MIRRORED_REPEAT_NEAREST(RenderScript rs)249     public static Sampler MIRRORED_REPEAT_NEAREST(RenderScript rs) {
250         if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
251             synchronized (rs) {
252                 if (rs.mSampler_MIRRORED_REPEAT_NEAREST == null) {
253                     Builder b = new Builder(rs);
254                     b.setMinification(Value.NEAREST);
255                     b.setMagnification(Value.NEAREST);
256                     b.setWrapS(Value.MIRRORED_REPEAT);
257                     b.setWrapT(Value.MIRRORED_REPEAT);
258                     rs.mSampler_MIRRORED_REPEAT_NEAREST = b.create();
259                 }
260             }
261         }
262         return rs.mSampler_MIRRORED_REPEAT_NEAREST;
263     }
264 
265     /**
266      * Retrieve a sampler with min and mag set to linear and wrap modes set to
267      * mirrored repeat.
268      *
269      * @param rs Context to which the sampler will belong.
270      *
271      * @return Sampler
272      */
MIRRORED_REPEAT_LINEAR(RenderScript rs)273     public static Sampler MIRRORED_REPEAT_LINEAR(RenderScript rs) {
274         if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
275             synchronized (rs) {
276                 if (rs.mSampler_MIRRORED_REPEAT_LINEAR == null) {
277                     Builder b = new Builder(rs);
278                     b.setMinification(Value.LINEAR);
279                     b.setMagnification(Value.LINEAR);
280                     b.setWrapS(Value.MIRRORED_REPEAT);
281                     b.setWrapT(Value.MIRRORED_REPEAT);
282                     rs.mSampler_MIRRORED_REPEAT_LINEAR = b.create();
283                 }
284             }
285         }
286         return rs.mSampler_MIRRORED_REPEAT_LINEAR;
287     }
288 
289     /**
290      * Retrieve a sampler with min and mag set to linear and wrap modes set to
291      * mirrored repeat.
292      *
293      * @param rs Context to which the sampler will belong.
294      *
295      * @return Sampler
296      */
MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs)297     public static Sampler MIRRORED_REPEAT_LINEAR_MIP_LINEAR(RenderScript rs) {
298         if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
299             synchronized (rs) {
300                 if (rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR == null) {
301                     Builder b = new Builder(rs);
302                     b.setMinification(Value.LINEAR_MIP_LINEAR);
303                     b.setMagnification(Value.LINEAR);
304                     b.setWrapS(Value.MIRRORED_REPEAT);
305                     b.setWrapT(Value.MIRRORED_REPEAT);
306                     rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR = b.create();
307                 }
308             }
309         }
310         return rs.mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
311     }
312 
313     /**
314      * Builder for creating non-standard samplers.  This is only necessary if
315      * a Sampler with different min and mag modes is desired.
316      */
317     public static class Builder {
318         RenderScript mRS;
319         Value mMin;
320         Value mMag;
321         Value mWrapS;
322         Value mWrapT;
323         Value mWrapR;
324         float mAniso;
325 
Builder(RenderScript rs)326         public Builder(RenderScript rs) {
327             mRS = rs;
328             mMin = Value.NEAREST;
329             mMag = Value.NEAREST;
330             mWrapS = Value.WRAP;
331             mWrapT = Value.WRAP;
332             mWrapR = Value.WRAP;
333             mAniso = 1.0f;
334         }
335 
setMinification(Value v)336         public void setMinification(Value v) {
337             if (v == Value.NEAREST ||
338                 v == Value.LINEAR ||
339                 v == Value.LINEAR_MIP_LINEAR ||
340                 v == Value.LINEAR_MIP_NEAREST) {
341                 mMin = v;
342             } else {
343                 throw new IllegalArgumentException("Invalid value");
344             }
345         }
346 
setMagnification(Value v)347         public void setMagnification(Value v) {
348             if (v == Value.NEAREST || v == Value.LINEAR) {
349                 mMag = v;
350             } else {
351                 throw new IllegalArgumentException("Invalid value");
352             }
353         }
354 
setWrapS(Value v)355         public void setWrapS(Value v) {
356             if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
357                 mWrapS = v;
358             } else {
359                 throw new IllegalArgumentException("Invalid value");
360             }
361         }
362 
setWrapT(Value v)363         public void setWrapT(Value v) {
364             if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) {
365                 mWrapT = v;
366             } else {
367                 throw new IllegalArgumentException("Invalid value");
368             }
369         }
370 
setAnisotropy(float v)371         public void setAnisotropy(float v) {
372             if(v >= 0.0f) {
373                 mAniso = v;
374             } else {
375                 throw new IllegalArgumentException("Invalid value");
376             }
377         }
378 
create()379         public Sampler create() {
380             mRS.validate();
381             long id = mRS.nSamplerCreate(mMag.mID, mMin.mID,
382                                         mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso);
383             Sampler sampler = new Sampler(id, mRS);
384             sampler.mMin = mMin;
385             sampler.mMag = mMag;
386             sampler.mWrapS = mWrapS;
387             sampler.mWrapT = mWrapT;
388             sampler.mWrapR = mWrapR;
389             sampler.mAniso = mAniso;
390             return sampler;
391         }
392     }
393 
394 }
395 
396