GLES20RenderEngine.cpp revision 89fd4f7fa6bd17ce5400979c3b9e5ba0bf7e919e
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#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19#include <GLES2/gl2.h>
20#include <GLES2/gl2ext.h>
21
22#include <ui/Rect.h>
23
24#include <utils/String8.h>
25#include <utils/Trace.h>
26
27#include <cutils/compiler.h>
28#include <gui/ISurfaceComposer.h>
29#include <math.h>
30
31#include "GLES20RenderEngine.h"
32#include "Program.h"
33#include "ProgramCache.h"
34#include "Description.h"
35#include "Mesh.h"
36#include "Texture.h"
37
38// ---------------------------------------------------------------------------
39namespace android {
40// ---------------------------------------------------------------------------
41
42GLES20RenderEngine::GLES20RenderEngine() :
43        mVpWidth(0), mVpHeight(0) {
44
45    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
46    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
47
48    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
49    glPixelStorei(GL_PACK_ALIGNMENT, 4);
50
51    const uint16_t protTexData[] = { 0 };
52    glGenTextures(1, &mProtectedTexName);
53    glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
54    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
55    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
57    glTexParameteri(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);
60
61    //mColorBlindnessCorrection = M;
62}
63
64GLES20RenderEngine::~GLES20RenderEngine() {
65}
66
67
68size_t GLES20RenderEngine::getMaxTextureSize() const {
69    return mMaxTextureSize;
70}
71
72size_t GLES20RenderEngine::getMaxViewportDims() const {
73    return
74        mMaxViewportDims[0] < mMaxViewportDims[1] ?
75            mMaxViewportDims[0] : mMaxViewportDims[1];
76}
77
78void GLES20RenderEngine::setViewportAndProjection(
79        size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
80        Transform::orientation_flags rotation) {
81
82    size_t l = sourceCrop.left;
83    size_t r = sourceCrop.right;
84
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;
88
89    mat4 m;
90    if (yswap) {
91        m = mat4::ortho(l, r, t, b, 0, 1);
92    } else {
93        m = mat4::ortho(l, r, b, t, 0, 1);
94    }
95
96    // Apply custom rotation to the projection.
97    float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
98    switch (rotation) {
99        case Transform::ROT_0:
100            break;
101        case Transform::ROT_90:
102            m = mat4::rotate(rot90InRadians, vec3(0,0,1)) * m;
103            break;
104        case Transform::ROT_180:
105            m = mat4::rotate(rot90InRadians * 2.0f, vec3(0,0,1)) * m;
106            break;
107        case Transform::ROT_270:
108            m = mat4::rotate(rot90InRadians * 3.0f, vec3(0,0,1)) * m;
109            break;
110        default:
111            break;
112    }
113
114    glViewport(0, 0, vpw, vph);
115    mState.setProjectionMatrix(m);
116    mVpWidth = vpw;
117    mVpHeight = vph;
118}
119
120void GLES20RenderEngine::setupLayerBlending(
121    bool premultipliedAlpha, bool opaque, int alpha) {
122
123    mState.setPremultipliedAlpha(premultipliedAlpha);
124    mState.setOpaque(opaque);
125    mState.setPlaneAlpha(alpha / 255.0f);
126
127    if (alpha < 0xFF || !opaque) {
128        glEnable(GL_BLEND);
129        glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
130    } else {
131        glDisable(GL_BLEND);
132    }
133}
134
135void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
136    mState.setPlaneAlpha(1.0f);
137    mState.setPremultipliedAlpha(true);
138    mState.setOpaque(false);
139    mState.setColor(0, 0, 0, alpha/255.0f);
140    mState.disableTexture();
141
142    if (alpha == 0xFF) {
143        glDisable(GL_BLEND);
144    } else {
145        glEnable(GL_BLEND);
146        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
147    }
148}
149
150void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
151    GLuint target = texture.getTextureTarget();
152    glBindTexture(target, texture.getTextureName());
153    GLenum filter = GL_NEAREST;
154    if (texture.getFiltering()) {
155        filter = GL_LINEAR;
156    }
157    glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
158    glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
159    glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
160    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
161
162    mState.setTexture(texture);
163}
164
165void GLES20RenderEngine::setupLayerBlackedOut() {
166    glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
167    Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
168    texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
169    mState.setTexture(texture);
170}
171
172void GLES20RenderEngine::disableTexturing() {
173    mState.disableTexture();
174}
175
176void GLES20RenderEngine::disableBlending() {
177    glDisable(GL_BLEND);
178}
179
180
181void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
182        uint32_t* texName, uint32_t* fbName, uint32_t* status) {
183    GLuint tname, name;
184    // turn our EGLImage into a texture
185    glGenTextures(1, &tname);
186    glBindTexture(GL_TEXTURE_2D, tname);
187    glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
188
189    // create a Framebuffer Object to render into
190    glGenFramebuffers(1, &name);
191    glBindFramebuffer(GL_FRAMEBUFFER, name);
192    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
193
194    *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
195    *texName = tname;
196    *fbName = name;
197}
198
199void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
200    glBindFramebuffer(GL_FRAMEBUFFER, 0);
201    glDeleteFramebuffers(1, &fbName);
202    glDeleteTextures(1, &texName);
203}
204
205void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
206    mState.setPlaneAlpha(1.0f);
207    mState.setPremultipliedAlpha(true);
208    mState.setOpaque(false);
209    mState.setColor(r, g, b, a);
210    mState.disableTexture();
211    glDisable(GL_BLEND);
212}
213
214void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
215
216    ProgramCache::getInstance().useProgram(mState);
217
218    if (mesh.getTexCoordsSize()) {
219        glEnableVertexAttribArray(Program::texCoords);
220        glVertexAttribPointer(Program::texCoords,
221                mesh.getTexCoordsSize(),
222                GL_FLOAT, GL_FALSE,
223                mesh.getByteStride(),
224                mesh.getTexCoords());
225    }
226
227    glVertexAttribPointer(Program::position,
228            mesh.getVertexSize(),
229            GL_FLOAT, GL_FALSE,
230            mesh.getByteStride(),
231            mesh.getPositions());
232
233    glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
234
235    if (mesh.getTexCoordsSize()) {
236        glDisableVertexAttribArray(Program::texCoords);
237    }
238}
239
240void GLES20RenderEngine::beginGroup(const mat4& colorTransform) {
241
242    GLuint tname, name;
243    // create the texture
244    glGenTextures(1, &tname);
245    glBindTexture(GL_TEXTURE_2D, tname);
246    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
247    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
248    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
249    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
250    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
251
252    // create a Framebuffer Object to render into
253    glGenFramebuffers(1, &name);
254    glBindFramebuffer(GL_FRAMEBUFFER, name);
255    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
256
257    Group group;
258    group.texture = tname;
259    group.fbo = name;
260    group.width = mVpWidth;
261    group.height = mVpHeight;
262    group.colorTransform = colorTransform;
263
264    mGroupStack.push(group);
265}
266
267void GLES20RenderEngine::endGroup() {
268
269    const Group group(mGroupStack.top());
270    mGroupStack.pop();
271
272    // activate the previous render target
273    GLuint fbo = 0;
274    if (!mGroupStack.isEmpty()) {
275        fbo = mGroupStack.top().fbo;
276    }
277    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
278
279    // set our state
280    Texture texture(Texture::TEXTURE_2D, group.texture);
281    texture.setDimensions(group.width, group.height);
282    glBindTexture(GL_TEXTURE_2D, group.texture);
283
284    mState.setPlaneAlpha(1.0f);
285    mState.setPremultipliedAlpha(true);
286    mState.setOpaque(false);
287    mState.setTexture(texture);
288    mState.setColorMatrix(group.colorTransform);
289    glDisable(GL_BLEND);
290
291    Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2);
292    Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
293    Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>());
294    position[0] = vec2(0, 0);
295    position[1] = vec2(group.width, 0);
296    position[2] = vec2(group.width, group.height);
297    position[3] = vec2(0, group.height);
298    texCoord[0] = vec2(0, 0);
299    texCoord[1] = vec2(1, 0);
300    texCoord[2] = vec2(1, 1);
301    texCoord[3] = vec2(0, 1);
302    drawMesh(mesh);
303
304    // reset color matrix
305    mState.setColorMatrix(mat4());
306
307    // free our fbo and texture
308    glDeleteFramebuffers(1, &group.fbo);
309    glDeleteTextures(1, &group.texture);
310}
311
312void GLES20RenderEngine::dump(String8& result) {
313    RenderEngine::dump(result);
314}
315
316// ---------------------------------------------------------------------------
317}; // namespace android
318// ---------------------------------------------------------------------------
319
320#if defined(__gl_h_)
321#error "don't include gl/gl.h in this file"
322#endif
323