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 #include "rsContext.h"
18 #include "rsProgramStore.h"
19 
20 using namespace android;
21 using namespace android::renderscript;
22 
23 
ProgramStore(Context * rsc,bool colorMaskR,bool colorMaskG,bool colorMaskB,bool colorMaskA,bool depthMask,bool ditherEnable,RsBlendSrcFunc srcFunc,RsBlendDstFunc destFunc,RsDepthFunc depthFunc)24 ProgramStore::ProgramStore(Context *rsc,
25                            bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
26                            bool depthMask, bool ditherEnable,
27                            RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
28                            RsDepthFunc depthFunc) : ProgramBase(rsc) {
29     memset(&mHal, 0, sizeof(mHal));
30 
31     mHal.state.ditherEnable = ditherEnable;
32 
33     mHal.state.colorRWriteEnable = colorMaskR;
34     mHal.state.colorGWriteEnable = colorMaskG;
35     mHal.state.colorBWriteEnable = colorMaskB;
36     mHal.state.colorAWriteEnable = colorMaskA;
37     mHal.state.blendSrc = srcFunc;
38     mHal.state.blendDst = destFunc;
39 
40     mHal.state.depthWriteEnable = depthMask;
41     mHal.state.depthFunc = depthFunc;
42 }
43 
preDestroy() const44 void ProgramStore::preDestroy() const {
45     for (uint32_t ct = 0; ct < mRSC->mStateFragmentStore.mStorePrograms.size(); ct++) {
46         if (mRSC->mStateFragmentStore.mStorePrograms[ct] == this) {
47             mRSC->mStateFragmentStore.mStorePrograms.removeAt(ct);
48             break;
49         }
50     }
51 }
52 
~ProgramStore()53 ProgramStore::~ProgramStore() {
54     mRSC->mHal.funcs.store.destroy(mRSC, this);
55 }
56 
setup(const Context * rsc,ProgramStoreState * state)57 void ProgramStore::setup(const Context *rsc, ProgramStoreState *state) {
58     if (state->mLast.get() == this) {
59         return;
60     }
61     state->mLast.set(this);
62 
63     rsc->mHal.funcs.store.setActive(rsc, this);
64 }
65 
serialize(Context * rsc,OStream * stream) const66 void ProgramStore::serialize(Context *rsc, OStream *stream) const {
67 }
68 
createFromStream(Context * rsc,IStream * stream)69 ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) {
70     return nullptr;
71 }
72 
init()73 void ProgramStore::init() {
74     mRSC->mHal.funcs.store.init(mRSC, this);
75 }
76 
ProgramStoreState()77 ProgramStoreState::ProgramStoreState() {
78 }
79 
~ProgramStoreState()80 ProgramStoreState::~ProgramStoreState() {
81 }
82 
getProgramStore(Context * rsc,bool colorMaskR,bool colorMaskG,bool colorMaskB,bool colorMaskA,bool depthMask,bool ditherEnable,RsBlendSrcFunc srcFunc,RsBlendDstFunc destFunc,RsDepthFunc depthFunc)83 ObjectBaseRef<ProgramStore> ProgramStore::getProgramStore(Context *rsc,
84                                                           bool colorMaskR,
85                                                           bool colorMaskG,
86                                                           bool colorMaskB,
87                                                           bool colorMaskA,
88                                                           bool depthMask, bool ditherEnable,
89                                                           RsBlendSrcFunc srcFunc,
90                                                           RsBlendDstFunc destFunc,
91                                                           RsDepthFunc depthFunc) {
92     ObjectBaseRef<ProgramStore> returnRef;
93     ObjectBase::asyncLock();
94     for (uint32_t ct = 0; ct < rsc->mStateFragmentStore.mStorePrograms.size(); ct++) {
95         ProgramStore *existing = rsc->mStateFragmentStore.mStorePrograms[ct];
96         if (existing->mHal.state.ditherEnable != ditherEnable) continue;
97         if (existing->mHal.state.colorRWriteEnable != colorMaskR) continue;
98         if (existing->mHal.state.colorGWriteEnable != colorMaskG) continue;
99         if (existing->mHal.state.colorBWriteEnable != colorMaskB) continue;
100         if (existing->mHal.state.colorAWriteEnable != colorMaskA) continue;
101         if (existing->mHal.state.blendSrc != srcFunc) continue;
102         if (existing->mHal.state.blendDst != destFunc) continue;
103         if (existing->mHal.state.depthWriteEnable != depthMask) continue;
104         if (existing->mHal.state.depthFunc != depthFunc) continue;
105 
106         returnRef.set(existing);
107         ObjectBase::asyncUnlock();
108         return returnRef;
109     }
110     ObjectBase::asyncUnlock();
111 
112     ProgramStore *pfs = new ProgramStore(rsc,
113                                          colorMaskR, colorMaskG, colorMaskB, colorMaskA,
114                                          depthMask, ditherEnable,
115                                          srcFunc, destFunc, depthFunc);
116     returnRef.set(pfs);
117 
118     pfs->init();
119 
120     ObjectBase::asyncLock();
121     rsc->mStateFragmentStore.mStorePrograms.push(pfs);
122     ObjectBase::asyncUnlock();
123 
124     return returnRef;
125 }
126 
127 
128 
init(Context * rsc)129 void ProgramStoreState::init(Context *rsc) {
130     mDefault.set(ProgramStore::getProgramStore(rsc,
131                                                true, true, true, true,
132                                                true, true,
133                                                RS_BLEND_SRC_ONE, RS_BLEND_DST_ZERO,
134                                                RS_DEPTH_FUNC_LESS).get());
135 }
136 
deinit(Context * rsc)137 void ProgramStoreState::deinit(Context *rsc) {
138     mDefault.clear();
139     mLast.clear();
140 }
141 
142 
143 namespace android {
144 namespace renderscript {
145 
rsi_ProgramStoreCreate(Context * rsc,bool colorMaskR,bool colorMaskG,bool colorMaskB,bool colorMaskA,bool depthMask,bool ditherEnable,RsBlendSrcFunc srcFunc,RsBlendDstFunc destFunc,RsDepthFunc depthFunc)146 RsProgramStore rsi_ProgramStoreCreate(Context *rsc,
147                                       bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
148                                       bool depthMask, bool ditherEnable,
149                                       RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
150                                       RsDepthFunc depthFunc) {
151 
152 
153     ObjectBaseRef<ProgramStore> ps = ProgramStore::getProgramStore(rsc,
154                                                                    colorMaskR, colorMaskG,
155                                                                    colorMaskB, colorMaskA,
156                                                                    depthMask, ditherEnable,
157                                                                    srcFunc, destFunc, depthFunc);
158     ps->incUserRef();
159     return ps.get();
160 }
161 
162 }
163 }
164