• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 <GLES/gl.h>
18 #include <GLES/glext.h>
19 
20 #include <ui/Rect.h>
21 
22 #include <utils/String8.h>
23 #include <cutils/compiler.h>
24 #include <gui/ISurfaceComposer.h>
25 
26 #include "GLES11RenderEngine.h"
27 #include "Mesh.h"
28 #include "Texture.h"
29 
30 // ---------------------------------------------------------------------------
31 namespace android {
32 // ---------------------------------------------------------------------------
33 
GLES11RenderEngine()34 GLES11RenderEngine::GLES11RenderEngine() {
35 
36     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
37     glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
38 
39     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
40     glPixelStorei(GL_PACK_ALIGNMENT, 4);
41     glEnableClientState(GL_VERTEX_ARRAY);
42     glShadeModel(GL_FLAT);
43     glDisable(GL_DITHER);
44     glDisable(GL_CULL_FACE);
45 
46     struct pack565 {
47         inline uint16_t operator() (int r, int g, int b) const {
48             return (r<<11)|(g<<5)|b;
49         }
50     } pack565;
51 
52     const uint16_t protTexData[] = { 0 };
53     glGenTextures(1, &mProtectedTexName);
54     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
55     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
56     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
57     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
58     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
59     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
60             GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
61 }
62 
~GLES11RenderEngine()63 GLES11RenderEngine::~GLES11RenderEngine() {
64 }
65 
66 
getMaxTextureSize() const67 size_t GLES11RenderEngine::getMaxTextureSize() const {
68     return mMaxTextureSize;
69 }
70 
getMaxViewportDims() const71 size_t GLES11RenderEngine::getMaxViewportDims() const {
72     return
73         mMaxViewportDims[0] < mMaxViewportDims[1] ?
74             mMaxViewportDims[0] : mMaxViewportDims[1];
75 }
76 
setViewportAndProjection(size_t vpw,size_t vph,Rect sourceCrop,size_t hwh,bool yswap,Transform::orientation_flags rotation)77 void GLES11RenderEngine::setViewportAndProjection(
78         size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
79         Transform::orientation_flags rotation) {
80     glViewport(0, 0, vpw, vph);
81     glMatrixMode(GL_PROJECTION);
82     glLoadIdentity();
83 
84     size_t l = sourceCrop.left;
85     size_t r = sourceCrop.right;
86 
87     // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
88     size_t t = hwh - sourceCrop.top;
89     size_t b = hwh - sourceCrop.bottom;
90 
91     if (yswap) {
92         glOrthof(l, r, t, b, 0, 1);
93     } else {
94         glOrthof(l, r, b, t, 0, 1);
95     }
96 
97     switch (rotation) {
98         case Transform::ROT_0:
99             break;
100         case Transform::ROT_90:
101             glRotatef(90, 0, 0, 1);
102             break;
103         case Transform::ROT_180:
104             glRotatef(180, 0, 0, 1);
105             break;
106         case Transform::ROT_270:
107             glRotatef(270, 0, 0, 1);
108             break;
109         default:
110             break;
111     }
112 
113     glMatrixMode(GL_MODELVIEW);
114 }
115 
setupLayerBlending(bool premultipliedAlpha,bool opaque,int alpha)116 void GLES11RenderEngine::setupLayerBlending(
117     bool premultipliedAlpha, bool opaque, int alpha) {
118     GLenum combineRGB;
119     GLenum combineAlpha;
120     GLenum src0Alpha;
121     GLfloat envColor[4];
122 
123     if (CC_UNLIKELY(alpha < 0xFF)) {
124         // Cv = premultiplied ? Cs*alpha : Cs
125         // Av = !opaque       ? As*alpha : As
126         combineRGB   = premultipliedAlpha ? GL_MODULATE : GL_REPLACE;
127         combineAlpha = !opaque            ? GL_MODULATE : GL_REPLACE;
128         src0Alpha    = GL_CONSTANT;
129         envColor[0]  = alpha * (1.0f / 255.0f);
130     } else {
131         // Cv = Cs
132         // Av = opaque ? 1.0 : As
133         combineRGB   = GL_REPLACE;
134         combineAlpha = GL_REPLACE;
135         src0Alpha    = opaque ? GL_CONSTANT : GL_TEXTURE;
136         envColor[0]  = 1.0f;
137     }
138 
139     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
140     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB);
141     glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
142     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
143     if (combineRGB == GL_MODULATE) {
144         glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
145         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
146     }
147     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha);
148     glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha);
149     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
150     if (combineAlpha == GL_MODULATE) {
151         glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
152         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
153     }
154     if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) {
155         envColor[1] = envColor[0];
156         envColor[2] = envColor[0];
157         envColor[3] = envColor[0];
158         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor);
159     }
160 
161     if (alpha < 0xFF || !opaque) {
162         glEnable(GL_BLEND);
163         glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA,
164                     GL_ONE_MINUS_SRC_ALPHA);
165     } else {
166         glDisable(GL_BLEND);
167     }
168 }
169 
setupDimLayerBlending(int alpha)170 void GLES11RenderEngine::setupDimLayerBlending(int alpha) {
171     glDisable(GL_TEXTURE_EXTERNAL_OES);
172     glDisable(GL_TEXTURE_2D);
173     if (alpha == 0xFF) {
174         glDisable(GL_BLEND);
175     } else {
176         glEnable(GL_BLEND);
177         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
178     }
179     glColor4f(0, 0, 0, alpha/255.0f);
180 }
181 
setupLayerTexturing(const Texture & texture)182 void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) {
183     GLuint target = texture.getTextureTarget();
184     glBindTexture(target, texture.getTextureName());
185     GLenum filter = GL_NEAREST;
186     if (texture.getFiltering()) {
187         filter = GL_LINEAR;
188     }
189     glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
190     glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
191     glTexParameterx(target, GL_TEXTURE_MAG_FILTER, filter);
192     glTexParameterx(target, GL_TEXTURE_MIN_FILTER, filter);
193     glMatrixMode(GL_TEXTURE);
194     glLoadMatrixf(texture.getMatrix().asArray());
195     glMatrixMode(GL_MODELVIEW);
196     glDisable(GL_TEXTURE_2D);
197     glEnable(GL_TEXTURE_EXTERNAL_OES);
198 }
199 
setupLayerBlackedOut()200 void GLES11RenderEngine::setupLayerBlackedOut() {
201     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
202     glMatrixMode(GL_TEXTURE);
203     glLoadIdentity();
204     glMatrixMode(GL_MODELVIEW);
205     glDisable(GL_TEXTURE_EXTERNAL_OES);
206     glEnable(GL_TEXTURE_2D);
207 }
208 
disableTexturing()209 void GLES11RenderEngine::disableTexturing() {
210     glDisable(GL_TEXTURE_EXTERNAL_OES);
211     glDisable(GL_TEXTURE_2D);
212 }
213 
disableBlending()214 void GLES11RenderEngine::disableBlending() {
215     glDisable(GL_BLEND);
216 }
217 
bindImageAsFramebuffer(EGLImageKHR image,uint32_t * texName,uint32_t * fbName,uint32_t * status)218 void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
219         uint32_t* texName, uint32_t* fbName, uint32_t* status) {
220     GLuint tname, name;
221     // turn our EGLImage into a texture
222     glGenTextures(1, &tname);
223     glBindTexture(GL_TEXTURE_2D, tname);
224     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
225 
226     // create a Framebuffer Object to render into
227     glGenFramebuffersOES(1, &name);
228     glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
229     glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
230             GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
231 
232     *status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
233     *texName = tname;
234     *fbName = name;
235 }
236 
unbindFramebuffer(uint32_t texName,uint32_t fbName)237 void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
238     glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
239     glDeleteFramebuffersOES(1, &fbName);
240     glDeleteTextures(1, &texName);
241 }
242 
setupFillWithColor(float r,float g,float b,float a)243 void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
244     glColor4f(r, g, b, a);
245     glDisable(GL_TEXTURE_EXTERNAL_OES);
246     glDisable(GL_TEXTURE_2D);
247     glDisable(GL_BLEND);
248 }
249 
drawMesh(const Mesh & mesh)250 void GLES11RenderEngine::drawMesh(const Mesh& mesh) {
251     if (mesh.getTexCoordsSize()) {
252         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
253         glTexCoordPointer(mesh.getTexCoordsSize(),
254                 GL_FLOAT,
255                 mesh.getByteStride(),
256                 mesh.getTexCoords());
257     }
258 
259     glVertexPointer(mesh.getVertexSize(),
260             GL_FLOAT,
261             mesh.getByteStride(),
262             mesh.getPositions());
263 
264     glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
265 
266     if (mesh.getTexCoordsSize()) {
267         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
268     }
269 }
270 
beginGroup(const mat4 &)271 void GLES11RenderEngine::beginGroup(const mat4& /*colorTransform*/) {
272     // doesn't do anything in GLES 1.1
273 }
274 
endGroup()275 void GLES11RenderEngine::endGroup() {
276     // doesn't do anything in GLES 1.1
277 }
278 
dump(String8 & result)279 void GLES11RenderEngine::dump(String8& result) {
280     RenderEngine::dump(result);
281 }
282 
283 // ---------------------------------------------------------------------------
284 }; // namespace android
285 // ---------------------------------------------------------------------------
286 
287 #if defined(__gl2_h_)
288 #error "don't include gl2/gl2.h in this file"
289 #endif
290