rsProgramStore.cpp revision afb743aca56c18beb7ab924e75cb6e070ef3e55a
1/*
2 * Copyright (C) 2009 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#ifndef ANDROID_RS_BUILD_FOR_HOST
18#include "rsContext.h"
19#include <GLES/gl.h>
20#include <GLES/glext.h>
21#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGl/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
27#include "rsProgramStore.h"
28
29using namespace android;
30using namespace android::renderscript;
31
32
33ProgramStore::ProgramStore(Context *rsc) : Program(rsc) {
34    mDitherEnable = true;
35    mBlendEnable = false;
36    mColorRWriteEnable = true;
37    mColorGWriteEnable = true;
38    mColorBWriteEnable = true;
39    mColorAWriteEnable = true;
40    mBlendSrc = GL_ONE;
41    mBlendDst = GL_ZERO;
42
43    mDepthTestEnable = false;
44    mDepthWriteEnable = true;
45    mDepthFunc = GL_LESS;
46}
47
48ProgramStore::~ProgramStore() {
49}
50
51void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state) {
52    if (state->mLast.get() == this) {
53        return;
54    }
55    state->mLast.set(this);
56
57    glColorMask(mColorRWriteEnable,
58                mColorGWriteEnable,
59                mColorBWriteEnable,
60                mColorAWriteEnable);
61    if (mBlendEnable) {
62        glEnable(GL_BLEND);
63        glBlendFunc(mBlendSrc, mBlendDst);
64    } else {
65        glDisable(GL_BLEND);
66    }
67
68    //LOGE("pfs  %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
69
70    if (rsc->mUserSurfaceConfig.depthMin > 0) {
71        glDepthMask(mDepthWriteEnable);
72        if (mDepthTestEnable || mDepthWriteEnable) {
73            glEnable(GL_DEPTH_TEST);
74            glDepthFunc(mDepthFunc);
75        } else {
76            glDisable(GL_DEPTH_TEST);
77        }
78    } else {
79        glDepthMask(false);
80        glDisable(GL_DEPTH_TEST);
81    }
82
83    if (rsc->mUserSurfaceConfig.stencilMin > 0) {
84    } else {
85        glStencilMask(0);
86        glDisable(GL_STENCIL_TEST);
87    }
88
89    if (mDitherEnable) {
90        glEnable(GL_DITHER);
91    } else {
92        glDisable(GL_DITHER);
93    }
94}
95
96void ProgramStore::setDitherEnable(bool enable) {
97    mDitherEnable = enable;
98}
99
100void ProgramStore::serialize(OStream *stream) const {
101}
102
103ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) {
104    return NULL;
105}
106
107void ProgramStore::setDepthFunc(RsDepthFunc func) {
108    mDepthTestEnable = true;
109
110    switch (func) {
111    case RS_DEPTH_FUNC_ALWAYS:
112        mDepthTestEnable = false;
113        mDepthFunc = GL_ALWAYS;
114        break;
115    case RS_DEPTH_FUNC_LESS:
116        mDepthFunc = GL_LESS;
117        break;
118    case RS_DEPTH_FUNC_LEQUAL:
119        mDepthFunc = GL_LEQUAL;
120        break;
121    case RS_DEPTH_FUNC_GREATER:
122        mDepthFunc = GL_GREATER;
123        break;
124    case RS_DEPTH_FUNC_GEQUAL:
125        mDepthFunc = GL_GEQUAL;
126        break;
127    case RS_DEPTH_FUNC_EQUAL:
128        mDepthFunc = GL_EQUAL;
129        break;
130    case RS_DEPTH_FUNC_NOTEQUAL:
131        mDepthFunc = GL_NOTEQUAL;
132        break;
133    }
134}
135
136void ProgramStore::setDepthMask(bool mask) {
137    mDepthWriteEnable = mask;
138}
139
140void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) {
141    mBlendEnable = true;
142    if ((src == RS_BLEND_SRC_ONE) &&
143        (dst == RS_BLEND_DST_ZERO)) {
144        mBlendEnable = false;
145    }
146
147    switch (src) {
148    case RS_BLEND_SRC_ZERO:
149        mBlendSrc = GL_ZERO;
150        break;
151    case RS_BLEND_SRC_ONE:
152        mBlendSrc = GL_ONE;
153        break;
154    case RS_BLEND_SRC_DST_COLOR:
155        mBlendSrc = GL_DST_COLOR;
156        break;
157    case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
158        mBlendSrc = GL_ONE_MINUS_DST_COLOR;
159        break;
160    case RS_BLEND_SRC_SRC_ALPHA:
161        mBlendSrc = GL_SRC_ALPHA;
162        break;
163    case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
164        mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
165        break;
166    case RS_BLEND_SRC_DST_ALPHA:
167        mBlendSrc = GL_DST_ALPHA;
168        break;
169    case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
170        mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
171        break;
172    case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
173        mBlendSrc = GL_SRC_ALPHA_SATURATE;
174        break;
175    }
176
177    switch (dst) {
178    case RS_BLEND_DST_ZERO:
179        mBlendDst = GL_ZERO;
180        break;
181    case RS_BLEND_DST_ONE:
182        mBlendDst = GL_ONE;
183        break;
184    case RS_BLEND_DST_SRC_COLOR:
185        mBlendDst = GL_SRC_COLOR;
186        break;
187    case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
188        mBlendDst = GL_ONE_MINUS_SRC_COLOR;
189        break;
190    case RS_BLEND_DST_SRC_ALPHA:
191        mBlendDst = GL_SRC_ALPHA;
192        break;
193    case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
194        mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
195        break;
196    case RS_BLEND_DST_DST_ALPHA:
197        mBlendDst = GL_DST_ALPHA;
198        break;
199    case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
200        mBlendDst = GL_ONE_MINUS_DST_ALPHA;
201        break;
202    }
203}
204
205void ProgramStore::setColorMask(bool r, bool g, bool b, bool a) {
206    mColorRWriteEnable = r;
207    mColorGWriteEnable = g;
208    mColorBWriteEnable = b;
209    mColorAWriteEnable = a;
210}
211
212ProgramStoreState::ProgramStoreState() {
213    mPFS = NULL;
214}
215
216ProgramStoreState::~ProgramStoreState() {
217    ObjectBase::checkDelete(mPFS);
218    mPFS = NULL;
219}
220
221void ProgramStoreState::init(Context *rsc) {
222    ProgramStore *pfs = new ProgramStore(rsc);
223    mDefault.set(pfs);
224}
225
226void ProgramStoreState::deinit(Context *rsc) {
227    mDefault.clear();
228    mLast.clear();
229}
230
231namespace android {
232namespace renderscript {
233
234void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out) {
235    ObjectBase::checkDelete(rsc->mStateFragmentStore.mPFS);
236    rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
237}
238
239void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func) {
240    rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
241}
242
243void rsi_ProgramStoreDepthMask(Context *rsc, bool mask) {
244    rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
245}
246
247void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) {
248    rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
249}
250
251void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) {
252    rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
253}
254
255RsProgramStore rsi_ProgramStoreCreate(Context *rsc) {
256    ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
257    pfs->incUserRef();
258    rsc->mStateFragmentStore.mPFS = 0;
259    return pfs;
260}
261
262void rsi_ProgramStoreDither(Context *rsc, bool enable) {
263    rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
264}
265
266}
267}
268