1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Copyright (C) 2009 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsContext.h"
18fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#include "rsProgramStore.h"
191aa5a4eb81b8b88aeb5d2b6f4c47356fd0a62923Jason Sams
20326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android;
21326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android::renderscript;
22326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
23326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
24721acc495b859c6d884725a4f9b5523583dd11c7Jason SamsProgramStore::ProgramStore(Context *rsc,
25721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                           bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
26721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                           bool depthMask, bool ditherEnable,
27721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                           RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
287f126c78a107257090c6675ea40ffac41516a9dcAlex Sakhartchouk                           RsDepthFunc depthFunc) : ProgramBase(rsc) {
298feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    memset(&mHal, 0, sizeof(mHal));
308feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
31721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.ditherEnable = ditherEnable;
328feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
33721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.colorRWriteEnable = colorMaskR;
34721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.colorGWriteEnable = colorMaskG;
35721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.colorBWriteEnable = colorMaskB;
36721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.colorAWriteEnable = colorMaskA;
37721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.blendSrc = srcFunc;
38721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.blendDst = destFunc;
398feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
40721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.depthWriteEnable = depthMask;
41721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams    mHal.state.depthFunc = depthFunc;
42326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
43326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
44c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchoukvoid ProgramStore::preDestroy() const {
45c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    for (uint32_t ct = 0; ct < mRSC->mStateFragmentStore.mStorePrograms.size(); ct++) {
46c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (mRSC->mStateFragmentStore.mStorePrograms[ct] == this) {
47c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk            mRSC->mStateFragmentStore.mStorePrograms.removeAt(ct);
48c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk            break;
49c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        }
50c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    }
51c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk}
52c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
53afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramStore::~ProgramStore() {
548feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    mRSC->mHal.funcs.store.destroy(mRSC, this);
55326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
56326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
57721acc495b859c6d884725a4f9b5523583dd11c7Jason Samsvoid ProgramStore::setup(const Context *rsc, ProgramStoreState *state) {
58c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    if (state->mLast.get() == this) {
59c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams        return;
60c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    }
61c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    state->mLast.set(this);
62c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
638feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    rsc->mHal.funcs.store.setActive(rsc, this);
64326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
65326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
66e3150cfb3edb028407669e4a65e087eae77e718cJason Samsvoid ProgramStore::serialize(Context *rsc, OStream *stream) const {
67fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
68fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
69afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) {
70fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    return NULL;
71fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
72fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
738feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsvoid ProgramStore::init() {
748feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    mRSC->mHal.funcs.store.init(mRSC, this);
75326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
76326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
77afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramStoreState::ProgramStoreState() {
78326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
79326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
80afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramStoreState::~ProgramStoreState() {
81326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
82326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
83c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex SakhartchoukObjectBaseRef<ProgramStore> ProgramStore::getProgramStore(Context *rsc,
84c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          bool colorMaskR,
85c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          bool colorMaskG,
86c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          bool colorMaskB,
87c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          bool colorMaskA,
88c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          bool depthMask, bool ditherEnable,
89c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          RsBlendSrcFunc srcFunc,
90c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          RsBlendDstFunc destFunc,
91c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                          RsDepthFunc depthFunc) {
92c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<ProgramStore> returnRef;
93c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBase::asyncLock();
94c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    for (uint32_t ct = 0; ct < rsc->mStateFragmentStore.mStorePrograms.size(); ct++) {
95c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        ProgramStore *existing = rsc->mStateFragmentStore.mStorePrograms[ct];
96c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.ditherEnable != ditherEnable) continue;
97c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.colorRWriteEnable != colorMaskR) continue;
98c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.colorGWriteEnable != colorMaskG) continue;
99c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.colorBWriteEnable != colorMaskB) continue;
100c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.colorAWriteEnable != colorMaskA) continue;
101c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.blendSrc != srcFunc) continue;
102c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.blendDst != destFunc) continue;
103c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.depthWriteEnable != depthMask) continue;
104c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        if (existing->mHal.state.depthFunc != depthFunc) continue;
105c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
106c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        returnRef.set(existing);
107c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        ObjectBase::asyncUnlock();
108c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk        return returnRef;
109c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    }
110c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBase::asyncUnlock();
111c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
112c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ProgramStore *pfs = new ProgramStore(rsc,
113c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                         colorMaskR, colorMaskG, colorMaskB, colorMaskA,
114c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                         depthMask, ditherEnable,
115c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                         srcFunc, destFunc, depthFunc);
116c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    returnRef.set(pfs);
117c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
118c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    pfs->init();
119c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
120c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBase::asyncLock();
121c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    rsc->mStateFragmentStore.mStorePrograms.push(pfs);
122c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBase::asyncUnlock();
123c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
124c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    return returnRef;
125c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk}
126c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
127c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
128c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
129afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramStoreState::init(Context *rsc) {
130c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    mDefault.set(ProgramStore::getProgramStore(rsc,
131c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                               true, true, true, true,
132c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                               true, true,
133c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                               RS_BLEND_SRC_ONE, RS_BLEND_DST_ZERO,
134c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                               RS_DEPTH_FUNC_LESS).get());
1358ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams}
1368ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams
137afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramStoreState::deinit(Context *rsc) {
138f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mDefault.clear();
139f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mLast.clear();
140f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams}
141f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams
1428feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
143326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
144326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
145326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
146721acc495b859c6d884725a4f9b5523583dd11c7Jason SamsRsProgramStore rsi_ProgramStoreCreate(Context *rsc,
147721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                                      bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
148721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                                      bool depthMask, bool ditherEnable,
149721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                                      RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
150721acc495b859c6d884725a4f9b5523583dd11c7Jason Sams                                      RsDepthFunc depthFunc) {
151326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
152c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
153c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<ProgramStore> ps = ProgramStore::getProgramStore(rsc,
154c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                   colorMaskR, colorMaskG,
155c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                   colorMaskB, colorMaskA,
156c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                   depthMask, ditherEnable,
157c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                   srcFunc, destFunc, depthFunc);
158c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ps->incUserRef();
159c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    return ps.get();
160326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
161326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
162326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
163326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
164