2 * Copyright 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <GLES/glext.h>
22 #include <utils/String8.h>
23 #include <cutils/compiler.h>
25 #include "GLES11RenderEngine.h"
29 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 GLES11RenderEngine::GLES11RenderEngine() {
35 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
36 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
38 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
39 glPixelStorei(GL_PACK_ALIGNMENT, 4);
40 glEnableClientState(GL_VERTEX_ARRAY);
41 glShadeModel(GL_FLAT);
43 glDisable(GL_CULL_FACE);
46 inline uint16_t operator() (int r, int g, int b) const {
47 return (r<<11)|(g<<5)|b;
51 const uint16_t protTexData[] = { 0 };
52 glGenTextures(1, &mProtectedTexName);
53 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
54 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
55 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
57 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
59 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
62 GLES11RenderEngine::~GLES11RenderEngine() {
66 size_t GLES11RenderEngine::getMaxTextureSize() const {
67 return mMaxTextureSize;
70 size_t GLES11RenderEngine::getMaxViewportDims() const {
72 mMaxViewportDims[0] < mMaxViewportDims[1] ?
73 mMaxViewportDims[0] : mMaxViewportDims[1];
76 void GLES11RenderEngine::setViewportAndProjection(
77 size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap) {
78 glViewport(0, 0, vpw, vph);
79 glMatrixMode(GL_PROJECTION);
82 size_t l = sourceCrop.left;
83 size_t r = sourceCrop.right;
85 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
86 size_t t = hwh - sourceCrop.top;
87 size_t b = hwh - sourceCrop.bottom;
90 glOrthof(l, r, t, b, 0, 1);
92 glOrthof(l, r, b, t, 0, 1);
94 glMatrixMode(GL_MODELVIEW);
97 void GLES11RenderEngine::setupLayerBlending(
98 bool premultipliedAlpha, bool opaque, int alpha) {
104 if (CC_UNLIKELY(alpha < 0xFF)) {
105 // Cv = premultiplied ? Cs*alpha : Cs
106 // Av = !opaque ? As*alpha : As
107 combineRGB = premultipliedAlpha ? GL_MODULATE : GL_REPLACE;
108 combineAlpha = !opaque ? GL_MODULATE : GL_REPLACE;
109 src0Alpha = GL_CONSTANT;
110 envColor[0] = alpha * (1.0f / 255.0f);
113 // Av = opaque ? 1.0 : As
114 combineRGB = GL_REPLACE;
115 combineAlpha = GL_REPLACE;
116 src0Alpha = opaque ? GL_CONSTANT : GL_TEXTURE;
120 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
121 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB);
122 glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
123 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
124 if (combineRGB == GL_MODULATE) {
125 glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
126 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
128 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha);
129 glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha);
130 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
131 if (combineAlpha == GL_MODULATE) {
132 glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
133 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
135 if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) {
136 envColor[1] = envColor[0];
137 envColor[2] = envColor[0];
138 envColor[3] = envColor[0];
139 glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor);
142 if (alpha < 0xFF || !opaque) {
144 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA,
145 GL_ONE_MINUS_SRC_ALPHA);
151 void GLES11RenderEngine::setupDimLayerBlending(int alpha) {
152 glDisable(GL_TEXTURE_EXTERNAL_OES);
153 glDisable(GL_TEXTURE_2D);
158 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
160 glColor4f(0, 0, 0, alpha/255.0f);
163 void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) {
164 GLuint target = texture.getTextureTarget();
165 glBindTexture(target, texture.getTextureName());
166 GLenum filter = GL_NEAREST;
167 if (texture.getFiltering()) {
170 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
171 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
172 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, filter);
173 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, filter);
174 glMatrixMode(GL_TEXTURE);
175 glLoadMatrixf(texture.getMatrix().asArray());
176 glMatrixMode(GL_MODELVIEW);
177 glDisable(GL_TEXTURE_2D);
178 glEnable(GL_TEXTURE_EXTERNAL_OES);
181 void GLES11RenderEngine::setupLayerBlackedOut() {
182 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
183 glMatrixMode(GL_TEXTURE);
185 glMatrixMode(GL_MODELVIEW);
186 glDisable(GL_TEXTURE_EXTERNAL_OES);
187 glEnable(GL_TEXTURE_2D);
190 void GLES11RenderEngine::disableTexturing() {
191 glDisable(GL_TEXTURE_EXTERNAL_OES);
192 glDisable(GL_TEXTURE_2D);
195 void GLES11RenderEngine::disableBlending() {
199 void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
200 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
202 // turn our EGLImage into a texture
203 glGenTextures(1, &tname);
204 glBindTexture(GL_TEXTURE_2D, tname);
205 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
207 // create a Framebuffer Object to render into
208 glGenFramebuffersOES(1, &name);
209 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
210 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
211 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
213 *status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
218 void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
219 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
220 glDeleteFramebuffersOES(1, &fbName);
221 glDeleteTextures(1, &texName);
224 void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
225 glColor4f(r, g, b, a);
226 glDisable(GL_TEXTURE_EXTERNAL_OES);
227 glDisable(GL_TEXTURE_2D);
231 void GLES11RenderEngine::drawMesh(const Mesh& mesh) {
232 if (mesh.getTexCoordsSize()) {
233 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
234 glTexCoordPointer(mesh.getTexCoordsSize(),
236 mesh.getByteStride(),
237 mesh.getTexCoords());
240 glVertexPointer(mesh.getVertexSize(),
242 mesh.getByteStride(),
243 mesh.getPositions());
245 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
247 if (mesh.getTexCoordsSize()) {
248 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
252 void GLES11RenderEngine::beginGroup(const mat4& /*colorTransform*/) {
253 // doesn't do anything in GLES 1.1
256 void GLES11RenderEngine::endGroup() {
257 // doesn't do anything in GLES 1.1
260 void GLES11RenderEngine::dump(String8& result) {
261 RenderEngine::dump(result);
264 // ---------------------------------------------------------------------------
265 }; // namespace android
266 // ---------------------------------------------------------------------------
268 #if defined(__gl2_h_)
269 #error "don't include gl2/gl2.h in this file"