rsProgramRaster.cpp revision d18c744a37441311c9b65254a35db456835adad3
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#ifndef ANDROID_RS_BUILD_FOR_HOST
18#include "rsContext.h"
19#include <GLES/gl.h>
20#include <GLES/glext.h>
21#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGl/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
27#include "rsProgramRaster.h"
28
29using namespace android;
30using namespace android::renderscript;
31
32
33ProgramRaster::ProgramRaster(Context *rsc,
34                             bool pointSmooth,
35                             bool lineSmooth,
36                             bool pointSprite) :
37    Program(rsc)
38{
39    mAllocFile = __FILE__;
40    mAllocLine = __LINE__;
41    mPointSmooth = pointSmooth;
42    mLineSmooth = lineSmooth;
43    mPointSprite = pointSprite;
44    mLineWidth = 1.0f;
45    mCull = RS_CULL_BACK;
46}
47
48ProgramRaster::~ProgramRaster()
49{
50}
51
52void ProgramRaster::setLineWidth(float s)
53{
54    mLineWidth = s;
55    mDirty = true;
56}
57
58void ProgramRaster::setCullMode(RsCullMode mode)
59{
60    mCull = mode;
61    mDirty = true;
62}
63
64void ProgramRaster::setupGL(const Context *rsc, ProgramRasterState *state)
65{
66    if (state->mLast.get() == this && !mDirty) {
67        return;
68    }
69    state->mLast.set(this);
70    mDirty = false;
71
72    if (mPointSmooth) {
73        glEnable(GL_POINT_SMOOTH);
74    } else {
75        glDisable(GL_POINT_SMOOTH);
76    }
77
78    glLineWidth(mLineWidth);
79    if (mLineSmooth) {
80        glEnable(GL_LINE_SMOOTH);
81    } else {
82        glDisable(GL_LINE_SMOOTH);
83    }
84
85    if (rsc->checkVersion1_1()) {
86#ifndef ANDROID_RS_BUILD_FOR_HOST
87        if (mPointSprite) {
88            glEnable(GL_POINT_SPRITE_OES);
89        } else {
90            glDisable(GL_POINT_SPRITE_OES);
91        }
92#endif //ANDROID_RS_BUILD_FOR_HOST
93    }
94
95    switch(mCull) {
96        case RS_CULL_BACK:
97            glEnable(GL_CULL_FACE);
98            glCullFace(GL_BACK);
99            break;
100        case RS_CULL_FRONT:
101            glEnable(GL_CULL_FACE);
102            glCullFace(GL_FRONT);
103            break;
104        case RS_CULL_NONE:
105            glDisable(GL_CULL_FACE);
106            break;
107    }
108}
109
110void ProgramRaster::setupGL2(const Context *rsc, ProgramRasterState *state)
111{
112    if (state->mLast.get() == this && !mDirty) {
113        return;
114    }
115    state->mLast.set(this);
116    mDirty = false;
117
118    switch(mCull) {
119        case RS_CULL_BACK:
120            glEnable(GL_CULL_FACE);
121            glCullFace(GL_BACK);
122            break;
123        case RS_CULL_FRONT:
124            glEnable(GL_CULL_FACE);
125            glCullFace(GL_FRONT);
126            break;
127        case RS_CULL_NONE:
128            glDisable(GL_CULL_FACE);
129            break;
130    }
131}
132
133void ProgramRaster::serialize(OStream *stream) const
134{
135
136}
137
138ProgramRaster *ProgramRaster::createFromStream(Context *rsc, IStream *stream)
139{
140    return NULL;
141}
142
143ProgramRasterState::ProgramRasterState()
144{
145}
146
147ProgramRasterState::~ProgramRasterState()
148{
149}
150
151void ProgramRasterState::init(Context *rsc)
152{
153    ProgramRaster *pr = new ProgramRaster(rsc, false, false, false);
154    mDefault.set(pr);
155}
156
157void ProgramRasterState::deinit(Context *rsc)
158{
159    mDefault.clear();
160    mLast.clear();
161}
162
163
164namespace android {
165namespace renderscript {
166
167RsProgramRaster rsi_ProgramRasterCreate(Context * rsc,
168                                      bool pointSmooth,
169                                      bool lineSmooth,
170                                      bool pointSprite)
171{
172    ProgramRaster *pr = new ProgramRaster(rsc,
173                                          pointSmooth,
174                                          lineSmooth,
175                                          pointSprite);
176    pr->incUserRef();
177    return pr;
178}
179
180void rsi_ProgramRasterSetLineWidth(Context * rsc, RsProgramRaster vpr, float s)
181{
182    ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
183    pr->setLineWidth(s);
184}
185
186void rsi_ProgramRasterSetCullMode(Context * rsc, RsProgramRaster vpr, RsCullMode mode)
187{
188    ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
189    pr->setCullMode(mode);
190}
191
192
193}
194}
195
196