ProgramStore.java revision f5c876e82d7cc647ba94d29eb914e64b7977c303
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 android.util.Config;
21import android.util.Log;
22
23
24/**
25 * ProgarmStore contains a set of parameters that control how
26 * the graphics hardware handles writes to the framebuffer.
27 *
28 * It could be used to:
29 *   - enable/diable depth testing
30 *   - specify wheather depth writes are performed
31 *   - setup various blending modes for use in effects like
32 *     transparency
33 *   - define write masks for color components written into the
34 *     framebuffer
35 *
36 **/
37public class ProgramStore extends BaseObj {
38    /**
39    * Specifies the function used to determine whether a fragment
40    * will be drawn during the depth testing stage in the rendering
41    * pipeline by comparing its value with that already in the depth
42    * buffer. DepthFunc is only valid when depth buffer is present
43    * and depth testing is enabled
44    */
45    public enum DepthFunc {
46
47        /**
48        * Always drawn
49        */
50        ALWAYS (0),
51        /**
52        * Drawn if the incoming depth value is less than that in the
53        * depth buffer
54        */
55        LESS (1),
56        /**
57        * Drawn if the incoming depth value is less or equal to that in
58        * the depth buffer
59        */
60        LESS_OR_EQUAL (2),
61        /**
62        * Drawn if the incoming depth value is greater than that in the
63        * depth buffer
64        */
65        GREATER (3),
66        /**
67        * Drawn if the incoming depth value is greater or equal to that
68        * in the depth buffer
69        */
70        GREATER_OR_EQUAL (4),
71        /**
72        * Drawn if the incoming depth value is equal to that in the
73        * depth buffer
74        */
75        EQUAL (5),
76        /**
77        * Drawn if the incoming depth value is not equal to that in the
78        * depth buffer
79        */
80        NOT_EQUAL (6);
81
82        int mID;
83        DepthFunc(int id) {
84            mID = id;
85        }
86    }
87
88    /**
89    * Specifies the functions used to combine incoming pixels with
90    * those already in the frame buffer.
91    *
92    * BlendSrcFunc describes how the coefficient used to scale the
93    * source pixels during the blending operation is computed
94    *
95    */
96    public enum BlendSrcFunc {
97        ZERO (0),
98        ONE (1),
99        DST_COLOR (2),
100        ONE_MINUS_DST_COLOR (3),
101        SRC_ALPHA (4),
102        ONE_MINUS_SRC_ALPHA (5),
103        DST_ALPHA (6),
104        ONE_MINUS_DST_ALPHA (7),
105        SRC_ALPHA_SATURATE (8);
106
107        int mID;
108        BlendSrcFunc(int id) {
109            mID = id;
110        }
111    }
112
113    /**
114    * Specifies the functions used to combine incoming pixels with
115    * those already in the frame buffer.
116    *
117    * BlendDstFunc describes how the coefficient used to scale the
118    * pixels already in the framebuffer is computed during the
119    * blending operation
120    *
121    */
122    public enum BlendDstFunc {
123        ZERO (0),
124        ONE (1),
125        SRC_COLOR (2),
126        ONE_MINUS_SRC_COLOR (3),
127        SRC_ALPHA (4),
128        ONE_MINUS_SRC_ALPHA (5),
129        DST_ALPHA (6),
130        ONE_MINUS_DST_ALPHA (7);
131
132        int mID;
133        BlendDstFunc(int id) {
134            mID = id;
135        }
136    }
137
138
139    ProgramStore(int id, RenderScript rs) {
140        super(id, rs);
141    }
142
143    /**
144    * Returns a pre-defined program store object with the following
145    * characteristics:
146    *  - incoming pixels are drawn if their depth value is less than
147    *    the stored value in the depth buffer. If the pixel is
148    *    drawn, its value is also stored in the depth buffer
149    *  - incoming pixels override the value stored in the color
150    *    buffer if it passes the depth test
151    *
152    *  @param rs Context to which the program will belong.
153    **/
154    public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) {
155        if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) {
156            ProgramStore.Builder builder = new ProgramStore.Builder(rs);
157            builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
158            builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
159            builder.setDitherEnabled(false);
160            builder.setDepthMaskEnabled(true);
161            rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create();
162        }
163        return rs.mProgramStore_BLEND_NONE_DEPTH_TEST;
164    }
165    /**
166    * Returns a pre-defined program store object with the following
167    * characteristics:
168    *  - incoming pixels always pass the depth test and their value
169    *    is not stored in the depth buffer
170    *  - incoming pixels override the value stored in the color
171    *    buffer
172    *
173    *  @param rs Context to which the program will belong.
174    **/
175    public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) {
176        if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) {
177            ProgramStore.Builder builder = new ProgramStore.Builder(rs);
178            builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
179            builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
180            builder.setDitherEnabled(false);
181            builder.setDepthMaskEnabled(false);
182            rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create();
183        }
184        return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
185    }
186    /**
187    * Returns a pre-defined program store object with the following
188    * characteristics:
189    *  - incoming pixels are drawn if their depth value is less than
190    *    the stored value in the depth buffer. If the pixel is
191    *    drawn, its value is also stored in the depth buffer
192    *  - if the incoming (Source) pixel passes depth test, its value
193    *    is combined with the stored color (Dest) using the
194    *    following formula
195    *  Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
196    *
197    *  @param rs Context to which the program will belong.
198    **/
199    public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) {
200        if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) {
201            ProgramStore.Builder builder = new ProgramStore.Builder(rs);
202            builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
203            builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
204            builder.setDitherEnabled(false);
205            builder.setDepthMaskEnabled(true);
206            rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create();
207        }
208        return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST;
209    }
210    /**
211    * Returns a pre-defined program store object with the following
212    * characteristics:
213    *  - incoming pixels always pass the depth test and their value
214    *    is not stored in the depth buffer
215    *  - incoming pixel's value is combined with the stored color
216    *    (Dest) using the following formula
217    *  Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
218    *
219    *  @param rs Context to which the program will belong.
220    **/
221    public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) {
222        if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) {
223            ProgramStore.Builder builder = new ProgramStore.Builder(rs);
224            builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
225            builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
226            builder.setDitherEnabled(false);
227            builder.setDepthMaskEnabled(false);
228            rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create();
229        }
230        return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
231    }
232
233    /**
234    * Builder class for ProgramStore object. If the builder is left
235    * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be
236    * returned
237    */
238    public static class Builder {
239        RenderScript mRS;
240        DepthFunc mDepthFunc;
241        boolean mDepthMask;
242        boolean mColorMaskR;
243        boolean mColorMaskG;
244        boolean mColorMaskB;
245        boolean mColorMaskA;
246        BlendSrcFunc mBlendSrc;
247        BlendDstFunc mBlendDst;
248        boolean mDither;
249
250        public Builder(RenderScript rs) {
251            mRS = rs;
252            mDepthFunc = DepthFunc.ALWAYS;
253            mDepthMask = false;
254            mColorMaskR = true;
255            mColorMaskG = true;
256            mColorMaskB = true;
257            mColorMaskA = true;
258            mBlendSrc = BlendSrcFunc.ONE;
259            mBlendDst = BlendDstFunc.ZERO;
260        }
261
262        /**
263        * Specifies the depth testing behavior
264        *
265        * @param func function used for depth testing
266        *
267        * @return this
268        */
269        public Builder setDepthFunc(DepthFunc func) {
270            mDepthFunc = func;
271            return this;
272        }
273
274        /**
275        * Enables writes into the depth buffer
276        *
277        * @param enable specifies whether depth writes are
278        *         enabled or disabled
279        *
280        * @return this
281        */
282        public Builder setDepthMaskEnabled(boolean enable) {
283            mDepthMask = enable;
284            return this;
285        }
286
287        /**
288        * Enables writes into the color buffer
289        *
290        * @param r specifies whether red channel is written
291        * @param g specifies whether green channel is written
292        * @param b specifies whether blue channel is written
293        * @param a specifies whether alpha channel is written
294        *
295        * @return this
296        */
297        public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) {
298            mColorMaskR = r;
299            mColorMaskG = g;
300            mColorMaskB = b;
301            mColorMaskA = a;
302            return this;
303        }
304
305        /**
306        * Specifies how incoming pixels are combined with the pixels
307        * stored in the framebuffer
308        *
309        * @param src specifies how the source blending factor is
310        *            computed
311        * @param dst specifies how the destination blending factor is
312        *            computed
313        *
314        * @return this
315        */
316        public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
317            mBlendSrc = src;
318            mBlendDst = dst;
319            return this;
320        }
321
322        /**
323        * Enables dithering
324        *
325        * @param enable specifies whether dithering is enabled or
326        *               disabled
327        *
328        * @return this
329        */
330        public Builder setDitherEnabled(boolean enable) {
331            mDither = enable;
332            return this;
333        }
334
335        static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) {
336            rs.nProgramStoreBegin(0, 0);
337            rs.nProgramStoreDepthFunc(b.mDepthFunc.mID);
338            rs.nProgramStoreDepthMask(b.mDepthMask);
339            rs.nProgramStoreColorMask(b.mColorMaskR,
340                                              b.mColorMaskG,
341                                              b.mColorMaskB,
342                                              b.mColorMaskA);
343            rs.nProgramStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID);
344            rs.nProgramStoreDither(b.mDither);
345
346            int id = rs.nProgramStoreCreate();
347            return new ProgramStore(id, rs);
348        }
349
350        /**
351        * Creates a program store from the current state of the builder
352        */
353        public ProgramStore create() {
354            mRS.validate();
355            return internalCreate(mRS, this);
356        }
357    }
358
359}
360
361
362
363
364