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