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 import android.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 
22 
23 /**
24  * @hide
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  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
38  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
39  * guide</a> for the proposed alternatives.
40  **/
41 @Deprecated
42 public class ProgramStore extends BaseObj {
43     /**
44     * Specifies the function used to determine whether a fragment
45     * will be drawn during the depth testing stage in the rendering
46     * pipeline by comparing its value with that already in the depth
47     * buffer. DepthFunc is only valid when depth buffer is present
48     * and depth testing is enabled
49     */
50     public enum DepthFunc {
51 
52         /**
53         * Always drawn
54         */
55         @UnsupportedAppUsage
56         ALWAYS (0),
57         /**
58         * Drawn if the incoming depth value is less than that in the
59         * depth buffer
60         */
61         @UnsupportedAppUsage
62         LESS (1),
63         /**
64         * Drawn if the incoming depth value is less or equal to that in
65         * the depth buffer
66         */
67         LESS_OR_EQUAL (2),
68         /**
69         * Drawn if the incoming depth value is greater than that in the
70         * depth buffer
71         */
72         GREATER (3),
73         /**
74         * Drawn if the incoming depth value is greater or equal to that
75         * in the depth buffer
76         */
77         GREATER_OR_EQUAL (4),
78         /**
79         * Drawn if the incoming depth value is equal to that in the
80         * depth buffer
81         */
82         EQUAL (5),
83         /**
84         * Drawn if the incoming depth value is not equal to that in the
85         * depth buffer
86         */
87         NOT_EQUAL (6);
88 
89         int mID;
DepthFunc(int id)90         DepthFunc(int id) {
91             mID = id;
92         }
93     }
94 
95     /**
96     * Specifies the functions used to combine incoming pixels with
97     * those already in the frame buffer.
98     *
99     * BlendSrcFunc describes how the coefficient used to scale the
100     * source pixels during the blending operation is computed
101     *
102     */
103     public enum BlendSrcFunc {
104         ZERO (0),
105         @UnsupportedAppUsage
106         ONE (1),
107         DST_COLOR (2),
108         ONE_MINUS_DST_COLOR (3),
109         @UnsupportedAppUsage
110         SRC_ALPHA (4),
111         ONE_MINUS_SRC_ALPHA (5),
112         DST_ALPHA (6),
113         ONE_MINUS_DST_ALPHA (7),
114         SRC_ALPHA_SATURATE (8);
115 
116         int mID;
BlendSrcFunc(int id)117         BlendSrcFunc(int id) {
118             mID = id;
119         }
120     }
121 
122     /**
123     * Specifies the functions used to combine incoming pixels with
124     * those already in the frame buffer.
125     *
126     * BlendDstFunc describes how the coefficient used to scale the
127     * pixels already in the framebuffer is computed during the
128     * blending operation
129     *
130     */
131     public enum BlendDstFunc {
132         @UnsupportedAppUsage
133         ZERO (0),
134         @UnsupportedAppUsage
135         ONE (1),
136         SRC_COLOR (2),
137         ONE_MINUS_SRC_COLOR (3),
138         SRC_ALPHA (4),
139         @UnsupportedAppUsage
140         ONE_MINUS_SRC_ALPHA (5),
141         DST_ALPHA (6),
142         ONE_MINUS_DST_ALPHA (7);
143 
144         int mID;
BlendDstFunc(int id)145         BlendDstFunc(int id) {
146             mID = id;
147         }
148     }
149 
150     DepthFunc mDepthFunc;
151     boolean mDepthMask;
152     boolean mColorMaskR;
153     boolean mColorMaskG;
154     boolean mColorMaskB;
155     boolean mColorMaskA;
156     BlendSrcFunc mBlendSrc;
157     BlendDstFunc mBlendDst;
158     boolean mDither;
159 
ProgramStore(long id, RenderScript rs)160     ProgramStore(long id, RenderScript rs) {
161         super(id, rs);
162     }
163 
164     /**
165     * Returns the function used to test writing into the depth
166     * buffer
167     * @return depth function
168     */
getDepthFunc()169     public DepthFunc getDepthFunc() {
170         return mDepthFunc;
171     }
172 
173     /**
174     * Queries whether writes are enabled into the depth buffer
175     * @return depth mask
176     */
isDepthMaskEnabled()177     public boolean isDepthMaskEnabled() {
178         return mDepthMask;
179     }
180 
181     /**
182     * Queries whether red channel is written
183     * @return red color channel mask
184     */
isColorMaskRedEnabled()185     public boolean isColorMaskRedEnabled() {
186         return mColorMaskR;
187     }
188 
189     /**
190     * Queries whether green channel is written
191     * @return green color channel mask
192     */
isColorMaskGreenEnabled()193     public boolean isColorMaskGreenEnabled() {
194         return mColorMaskG;
195     }
196 
197     /**
198     * Queries whether blue channel is written
199     * @return blue color channel mask
200     */
isColorMaskBlueEnabled()201     public boolean isColorMaskBlueEnabled() {
202         return mColorMaskB;
203     }
204 
205     /**
206     * Queries whether alpha channel is written
207     * @return alpha channel mask
208     */
isColorMaskAlphaEnabled()209     public boolean isColorMaskAlphaEnabled() {
210         return mColorMaskA;
211     }
212 
213     /**
214     * Specifies how the source blending factor is computed
215     * @return source blend function
216     */
getBlendSrcFunc()217     public BlendSrcFunc getBlendSrcFunc() {
218         return mBlendSrc;
219     }
220 
221     /**
222     * Specifies how the destination blending factor is computed
223     * @return destination blend function
224     */
getBlendDstFunc()225     public BlendDstFunc getBlendDstFunc() {
226         return mBlendDst;
227     }
228 
229     /**
230     * Specifies whether colors are dithered before writing into the
231     * framebuffer
232     * @return whether dither is enabled
233     */
isDitherEnabled()234     public boolean isDitherEnabled() {
235         return mDither;
236     }
237 
238     /**
239     * Returns a pre-defined program store object with the following
240     * characteristics:
241     *  - incoming pixels are drawn if their depth value is less than
242     *    the stored value in the depth buffer. If the pixel is
243     *    drawn, its value is also stored in the depth buffer
244     *  - incoming pixels override the value stored in the color
245     *    buffer if it passes the depth test
246     *
247     *  @param rs Context to which the program will belong.
248     **/
BLEND_NONE_DEPTH_TEST(RenderScript rs)249     public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) {
250         if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) {
251             ProgramStore.Builder builder = new ProgramStore.Builder(rs);
252             builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
253             builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
254             builder.setDitherEnabled(false);
255             builder.setDepthMaskEnabled(true);
256             rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create();
257         }
258         return rs.mProgramStore_BLEND_NONE_DEPTH_TEST;
259     }
260     /**
261     * Returns a pre-defined program store object with the following
262     * characteristics:
263     *  - incoming pixels always pass the depth test and their value
264     *    is not stored in the depth buffer
265     *  - incoming pixels override the value stored in the color
266     *    buffer
267     *
268     *  @param rs Context to which the program will belong.
269     **/
BLEND_NONE_DEPTH_NONE(RenderScript rs)270     public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) {
271         if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) {
272             ProgramStore.Builder builder = new ProgramStore.Builder(rs);
273             builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
274             builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
275             builder.setDitherEnabled(false);
276             builder.setDepthMaskEnabled(false);
277             rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create();
278         }
279         return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
280     }
281     /**
282     * Returns a pre-defined program store object with the following
283     * characteristics:
284     *  - incoming pixels are drawn if their depth value is less than
285     *    the stored value in the depth buffer. If the pixel is
286     *    drawn, its value is also stored in the depth buffer
287     *  - if the incoming (Source) pixel passes depth test, its value
288     *    is combined with the stored color (Dest) using the
289     *    following formula
290     *  Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
291     *
292     *  @param rs Context to which the program will belong.
293     **/
BLEND_ALPHA_DEPTH_TEST(RenderScript rs)294     public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) {
295         if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) {
296             ProgramStore.Builder builder = new ProgramStore.Builder(rs);
297             builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
298             builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
299             builder.setDitherEnabled(false);
300             builder.setDepthMaskEnabled(true);
301             rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create();
302         }
303         return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST;
304     }
305     /**
306     * Returns a pre-defined program store object with the following
307     * characteristics:
308     *  - incoming pixels always pass the depth test and their value
309     *    is not stored in the depth buffer
310     *  - incoming pixel's value is combined with the stored color
311     *    (Dest) using the following formula
312     *  Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
313     *
314     *  @param rs Context to which the program will belong.
315     **/
316     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
BLEND_ALPHA_DEPTH_NONE(RenderScript rs)317     public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) {
318         if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) {
319             ProgramStore.Builder builder = new ProgramStore.Builder(rs);
320             builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
321             builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
322             builder.setDitherEnabled(false);
323             builder.setDepthMaskEnabled(false);
324             rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create();
325         }
326         return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
327     }
328 
329     /**
330     * Builder class for ProgramStore object. If the builder is left
331     * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be
332     * returned
333     */
334     public static class Builder {
335         RenderScript mRS;
336         DepthFunc mDepthFunc;
337         boolean mDepthMask;
338         boolean mColorMaskR;
339         boolean mColorMaskG;
340         boolean mColorMaskB;
341         boolean mColorMaskA;
342         BlendSrcFunc mBlendSrc;
343         BlendDstFunc mBlendDst;
344         boolean mDither;
345 
346         @UnsupportedAppUsage
Builder(RenderScript rs)347         public Builder(RenderScript rs) {
348             mRS = rs;
349             mDepthFunc = DepthFunc.ALWAYS;
350             mDepthMask = false;
351             mColorMaskR = true;
352             mColorMaskG = true;
353             mColorMaskB = true;
354             mColorMaskA = true;
355             mBlendSrc = BlendSrcFunc.ONE;
356             mBlendDst = BlendDstFunc.ZERO;
357         }
358 
359         /**
360         * Specifies the depth testing behavior
361         *
362         * @param func function used for depth testing
363         *
364         * @return this
365         */
366         @UnsupportedAppUsage
setDepthFunc(DepthFunc func)367         public Builder setDepthFunc(DepthFunc func) {
368             mDepthFunc = func;
369             return this;
370         }
371 
372         /**
373         * Enables writes into the depth buffer
374         *
375         * @param enable specifies whether depth writes are
376         *         enabled or disabled
377         *
378         * @return this
379         */
380         @UnsupportedAppUsage
setDepthMaskEnabled(boolean enable)381         public Builder setDepthMaskEnabled(boolean enable) {
382             mDepthMask = enable;
383             return this;
384         }
385 
386         /**
387         * Enables writes into the color buffer
388         *
389         * @param r specifies whether red channel is written
390         * @param g specifies whether green channel is written
391         * @param b specifies whether blue channel is written
392         * @param a specifies whether alpha channel is written
393         *
394         * @return this
395         */
setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a)396         public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) {
397             mColorMaskR = r;
398             mColorMaskG = g;
399             mColorMaskB = b;
400             mColorMaskA = a;
401             return this;
402         }
403 
404         /**
405         * Specifies how incoming pixels are combined with the pixels
406         * stored in the framebuffer
407         *
408         * @param src specifies how the source blending factor is
409         *            computed
410         * @param dst specifies how the destination blending factor is
411         *            computed
412         *
413         * @return this
414         */
415         @UnsupportedAppUsage
setBlendFunc(BlendSrcFunc src, BlendDstFunc dst)416         public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
417             mBlendSrc = src;
418             mBlendDst = dst;
419             return this;
420         }
421 
422         /**
423         * Enables dithering
424         *
425         * @param enable specifies whether dithering is enabled or
426         *               disabled
427         *
428         * @return this
429         */
430         @UnsupportedAppUsage
setDitherEnabled(boolean enable)431         public Builder setDitherEnabled(boolean enable) {
432             mDither = enable;
433             return this;
434         }
435 
436         /**
437         * Creates a program store from the current state of the builder
438         */
439         @UnsupportedAppUsage
create()440         public ProgramStore create() {
441             mRS.validate();
442             long id = mRS.nProgramStoreCreate(mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA,
443                                              mDepthMask, mDither,
444                                              mBlendSrc.mID, mBlendDst.mID, mDepthFunc.mID);
445             ProgramStore programStore = new ProgramStore(id, mRS);
446             programStore.mDepthFunc = mDepthFunc;
447             programStore.mDepthMask = mDepthMask;
448             programStore.mColorMaskR = mColorMaskR;
449             programStore.mColorMaskG = mColorMaskG;
450             programStore.mColorMaskB = mColorMaskB;
451             programStore.mColorMaskA = mColorMaskA;
452             programStore.mBlendSrc = mBlendSrc;
453             programStore.mBlendDst = mBlendDst;
454             programStore.mDither = mDither;
455             return programStore;
456         }
457     }
458 
459 }
460 
461 
462 
463 
464