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