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 namespace android {
21 namespace renderscript {
22 
ProgramStore(Context * rsc,bool colorMaskR,bool colorMaskG,bool colorMaskB,bool colorMaskA,bool depthMask,bool ditherEnable,RsBlendSrcFunc srcFunc,RsBlendDstFunc destFunc,RsDepthFunc depthFunc)23 ProgramStore::ProgramStore(Context *rsc,
24                            bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
25                            bool depthMask, bool ditherEnable,
26                            RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
27                            RsDepthFunc depthFunc) : ProgramBase(rsc) {
28     memset(&mHal, 0, sizeof(mHal));
29 
30     mHal.state.ditherEnable = ditherEnable;
31 
32     mHal.state.colorRWriteEnable = colorMaskR;
33     mHal.state.colorGWriteEnable = colorMaskG;
34     mHal.state.colorBWriteEnable = colorMaskB;
35     mHal.state.colorAWriteEnable = colorMaskA;
36     mHal.state.blendSrc = srcFunc;
37     mHal.state.blendDst = destFunc;
38 
39     mHal.state.depthWriteEnable = depthMask;
40     mHal.state.depthFunc = depthFunc;
41 }
42 
preDestroy() const43 void ProgramStore::preDestroy() const {
44     auto& storePrograms = mRSC->mStateFragmentStore.mStorePrograms;
45     for (uint32_t ct = 0; ct < storePrograms.size(); ct++) {
46         if (storePrograms[ct] == this) {
47             storePrograms.erase(storePrograms.begin() + 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_back(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 
rsi_ProgramStoreCreate(Context * rsc,bool colorMaskR,bool colorMaskG,bool colorMaskB,bool colorMaskA,bool depthMask,bool ditherEnable,RsBlendSrcFunc srcFunc,RsBlendDstFunc destFunc,RsDepthFunc depthFunc)143 RsProgramStore rsi_ProgramStoreCreate(Context *rsc,
144                                       bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
145                                       bool depthMask, bool ditherEnable,
146                                       RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
147                                       RsDepthFunc depthFunc) {
148 
149 
150     ObjectBaseRef<ProgramStore> ps = ProgramStore::getProgramStore(rsc,
151                                                                    colorMaskR, colorMaskG,
152                                                                    colorMaskB, colorMaskA,
153                                                                    depthMask, ditherEnable,
154                                                                    srcFunc, destFunc, depthFunc);
155     ps->incUserRef();
156     return ps.get();
157 }
158 
159 } // namespace renderscript
160 } // namespace android
161