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
20using namespace android;
21using namespace android::renderscript;
22
23
24ProgramStore::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
44void 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
53ProgramStore::~ProgramStore() {
54    mRSC->mHal.funcs.store.destroy(mRSC, this);
55}
56
57void 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
66void ProgramStore::serialize(Context *rsc, OStream *stream) const {
67}
68
69ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) {
70    return NULL;
71}
72
73void ProgramStore::init() {
74    mRSC->mHal.funcs.store.init(mRSC, this);
75}
76
77ProgramStoreState::ProgramStoreState() {
78}
79
80ProgramStoreState::~ProgramStoreState() {
81}
82
83ObjectBaseRef<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
129void 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
137void ProgramStoreState::deinit(Context *rsc) {
138    mDefault.clear();
139    mLast.clear();
140}
141
142
143namespace android {
144namespace renderscript {
145
146RsProgramStore 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