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 "rsProgramRaster.h"
19
20using namespace android;
21using namespace android::renderscript;
22
23
24ProgramRaster::ProgramRaster(Context *rsc, bool pointSprite, RsCullMode cull)
25    : ProgramBase(rsc) {
26
27    memset(&mHal, 0, sizeof(mHal));
28    mHal.state.pointSprite = pointSprite;
29    mHal.state.cull = cull;
30    rsc->mHal.funcs.raster.init(rsc, this);
31}
32
33void ProgramRaster::preDestroy() const {
34    for (uint32_t ct = 0; ct < mRSC->mStateRaster.mRasterPrograms.size(); ct++) {
35        if (mRSC->mStateRaster.mRasterPrograms[ct] == this) {
36            mRSC->mStateRaster.mRasterPrograms.removeAt(ct);
37            break;
38        }
39    }
40}
41
42ProgramRaster::~ProgramRaster() {
43    mRSC->mHal.funcs.raster.destroy(mRSC, this);
44}
45
46void ProgramRaster::setup(const Context *rsc, ProgramRasterState *state) {
47    if (state->mLast.get() == this && !mDirty) {
48        return;
49    }
50    state->mLast.set(this);
51    mDirty = false;
52
53    rsc->mHal.funcs.raster.setActive(rsc, this);
54}
55
56void ProgramRaster::serialize(Context *rsc, OStream *stream) const {
57}
58
59ProgramRaster *ProgramRaster::createFromStream(Context *rsc, IStream *stream) {
60    return NULL;
61}
62
63ProgramRasterState::ProgramRasterState() {
64}
65
66ProgramRasterState::~ProgramRasterState() {
67}
68
69void ProgramRasterState::init(Context *rsc) {
70    mDefault.set(ProgramRaster::getProgramRaster(rsc, false, RS_CULL_BACK).get());
71}
72
73void ProgramRasterState::deinit(Context *rsc) {
74    mDefault.clear();
75    mLast.clear();
76}
77
78ObjectBaseRef<ProgramRaster> ProgramRaster::getProgramRaster(Context *rsc,
79                                                             bool pointSprite,
80                                                             RsCullMode cull) {
81    ObjectBaseRef<ProgramRaster> returnRef;
82    ObjectBase::asyncLock();
83    for (uint32_t ct = 0; ct < rsc->mStateRaster.mRasterPrograms.size(); ct++) {
84        ProgramRaster *existing = rsc->mStateRaster.mRasterPrograms[ct];
85        if (existing->mHal.state.pointSprite != pointSprite) continue;
86        if (existing->mHal.state.cull != cull) continue;
87        returnRef.set(existing);
88        ObjectBase::asyncUnlock();
89        return returnRef;
90    }
91    ObjectBase::asyncUnlock();
92
93    ProgramRaster *pr = new ProgramRaster(rsc, pointSprite, cull);
94    returnRef.set(pr);
95
96    ObjectBase::asyncLock();
97    rsc->mStateRaster.mRasterPrograms.push(pr);
98    ObjectBase::asyncUnlock();
99
100    return returnRef;
101}
102
103namespace android {
104namespace renderscript {
105
106RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, bool pointSprite, RsCullMode cull) {
107    ObjectBaseRef<ProgramRaster> pr = ProgramRaster::getProgramRaster(rsc, pointSprite, cull);
108    pr->incUserRef();
109    return pr.get();
110}
111
112}
113}
114
115