blob: 8ebafbced359cd4e1598830c0d45b84175c4fb82 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
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>
Mathias Agopian458197d2013-08-15 14:56:51 -070020#include <GLES2/gl2ext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070021
Dan Stozac1879002014-05-22 15:59:05 -070022#include <ui/Rect.h>
23
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include <utils/String8.h>
25#include <utils/Trace.h>
26
27#include <cutils/compiler.h>
Riley Andrewsc3ebe662014-09-04 16:20:31 -070028#include <gui/ISurfaceComposer.h>
29#include <math.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070030
31#include "GLES20RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070032#include "Program.h"
33#include "ProgramCache.h"
34#include "Description.h"
35#include "Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070036#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070037
38// ---------------------------------------------------------------------------
39namespace android {
40// ---------------------------------------------------------------------------
41
Mathias Agopianff2ed702013-09-01 21:36:12 -070042GLES20RenderEngine::GLES20RenderEngine() :
43 mVpWidth(0), mVpHeight(0) {
Mathias Agopian3f844832013-08-07 21:24:32 -070044
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 struct pack565 {
52 inline uint16_t operator() (int r, int g, int b) const {
53 return (r<<11)|(g<<5)|b;
54 }
55 } pack565;
56
Andy McFaddenc6f21692013-10-11 11:16:03 -070057 const uint16_t protTexData[] = { 0 };
Mathias Agopian3f844832013-08-07 21:24:32 -070058 glGenTextures(1, &mProtectedTexName);
59 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
64 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
65 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -070066
67 //mColorBlindnessCorrection = M;
Mathias Agopian3f844832013-08-07 21:24:32 -070068}
69
70GLES20RenderEngine::~GLES20RenderEngine() {
71}
72
73
74size_t GLES20RenderEngine::getMaxTextureSize() const {
75 return mMaxTextureSize;
76}
77
78size_t GLES20RenderEngine::getMaxViewportDims() const {
79 return
80 mMaxViewportDims[0] < mMaxViewportDims[1] ?
81 mMaxViewportDims[0] : mMaxViewportDims[1];
82}
83
84void GLES20RenderEngine::setViewportAndProjection(
Riley Andrewsc3ebe662014-09-04 16:20:31 -070085 size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
86 Transform::orientation_flags rotation) {
Dan Stozac1879002014-05-22 15:59:05 -070087
88 size_t l = sourceCrop.left;
89 size_t r = sourceCrop.right;
90
91 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
92 size_t t = hwh - sourceCrop.top;
93 size_t b = hwh - sourceCrop.bottom;
94
Mathias Agopiana8c386f2013-08-26 20:42:07 -070095 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -070096 if (yswap) {
97 m = mat4::ortho(l, r, t, b, 0, 1);
98 } else {
99 m = mat4::ortho(l, r, b, t, 0, 1);
100 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700101
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700102 // Apply custom rotation to the projection.
103 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
104 switch (rotation) {
105 case Transform::ROT_0:
106 break;
107 case Transform::ROT_90:
108 m = mat4::rotate(rot90InRadians, vec3(0,0,1)) * m;
109 break;
110 case Transform::ROT_180:
111 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0,0,1)) * m;
112 break;
113 case Transform::ROT_270:
114 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0,0,1)) * m;
115 break;
116 default:
117 break;
118 }
119
Mathias Agopian3f844832013-08-07 21:24:32 -0700120 glViewport(0, 0, vpw, vph);
121 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700122 mVpWidth = vpw;
123 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700124}
125
126void GLES20RenderEngine::setupLayerBlending(
127 bool premultipliedAlpha, bool opaque, int alpha) {
128
129 mState.setPremultipliedAlpha(premultipliedAlpha);
130 mState.setOpaque(opaque);
131 mState.setPlaneAlpha(alpha / 255.0f);
132
133 if (alpha < 0xFF || !opaque) {
134 glEnable(GL_BLEND);
135 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
136 } else {
137 glDisable(GL_BLEND);
138 }
139}
140
141void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700142 mState.setPlaneAlpha(1.0f);
143 mState.setPremultipliedAlpha(true);
144 mState.setOpaque(false);
145 mState.setColor(0, 0, 0, alpha/255.0f);
146 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700147
148 if (alpha == 0xFF) {
149 glDisable(GL_BLEND);
150 } else {
151 glEnable(GL_BLEND);
152 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
153 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700154}
155
Mathias Agopian49457ac2013-08-14 18:20:17 -0700156void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
157 GLuint target = texture.getTextureTarget();
158 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700159 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700160 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700161 filter = GL_LINEAR;
162 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700163 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
164 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
165 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
166 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700167
Mathias Agopian49457ac2013-08-14 18:20:17 -0700168 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700169}
170
171void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700172 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700173 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
174 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
175 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700176}
177
178void GLES20RenderEngine::disableTexturing() {
179 mState.disableTexture();
180}
181
182void GLES20RenderEngine::disableBlending() {
183 glDisable(GL_BLEND);
184}
185
Mathias Agopian458197d2013-08-15 14:56:51 -0700186
187void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
188 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
189 GLuint tname, name;
190 // turn our EGLImage into a texture
191 glGenTextures(1, &tname);
192 glBindTexture(GL_TEXTURE_2D, tname);
193 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
194
195 // create a Framebuffer Object to render into
196 glGenFramebuffers(1, &name);
197 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700198 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700199
200 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
201 *texName = tname;
202 *fbName = name;
203}
204
205void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
206 glBindFramebuffer(GL_FRAMEBUFFER, 0);
207 glDeleteFramebuffers(1, &fbName);
208 glDeleteTextures(1, &texName);
209}
210
Mathias Agopian19733a32013-08-28 18:13:56 -0700211void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
212 mState.setPlaneAlpha(1.0f);
213 mState.setPremultipliedAlpha(true);
214 mState.setOpaque(false);
Mathias Agopian3f844832013-08-07 21:24:32 -0700215 mState.setColor(r, g, b, a);
Mathias Agopian19733a32013-08-28 18:13:56 -0700216 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700217 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700218}
219
220void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
221
222 ProgramCache::getInstance().useProgram(mState);
223
224 if (mesh.getTexCoordsSize()) {
225 glEnableVertexAttribArray(Program::texCoords);
226 glVertexAttribPointer(Program::texCoords,
227 mesh.getTexCoordsSize(),
228 GL_FLOAT, GL_FALSE,
229 mesh.getByteStride(),
230 mesh.getTexCoords());
231 }
232
233 glVertexAttribPointer(Program::position,
234 mesh.getVertexSize(),
235 GL_FLOAT, GL_FALSE,
236 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700237 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700238
239 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
240
241 if (mesh.getTexCoordsSize()) {
242 glDisableVertexAttribArray(Program::texCoords);
243 }
244}
245
Mathias Agopianff2ed702013-09-01 21:36:12 -0700246void GLES20RenderEngine::beginGroup(const mat4& colorTransform) {
247
248 GLuint tname, name;
249 // create the texture
250 glGenTextures(1, &tname);
251 glBindTexture(GL_TEXTURE_2D, tname);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
257
258 // create a Framebuffer Object to render into
259 glGenFramebuffers(1, &name);
260 glBindFramebuffer(GL_FRAMEBUFFER, name);
261 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
262
263 Group group;
264 group.texture = tname;
265 group.fbo = name;
266 group.width = mVpWidth;
267 group.height = mVpHeight;
268 group.colorTransform = colorTransform;
269
270 mGroupStack.push(group);
271}
272
273void GLES20RenderEngine::endGroup() {
274
275 const Group group(mGroupStack.top());
276 mGroupStack.pop();
277
278 // activate the previous render target
279 GLuint fbo = 0;
280 if (!mGroupStack.isEmpty()) {
281 fbo = mGroupStack.top().fbo;
282 }
283 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
284
285 // set our state
286 Texture texture(Texture::TEXTURE_2D, group.texture);
287 texture.setDimensions(group.width, group.height);
288 glBindTexture(GL_TEXTURE_2D, group.texture);
289
290 mState.setPlaneAlpha(1.0f);
291 mState.setPremultipliedAlpha(true);
292 mState.setOpaque(false);
293 mState.setTexture(texture);
294 mState.setColorMatrix(group.colorTransform);
295 glDisable(GL_BLEND);
296
297 Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2);
298 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
299 Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>());
300 position[0] = vec2(0, 0);
301 position[1] = vec2(group.width, 0);
302 position[2] = vec2(group.width, group.height);
303 position[3] = vec2(0, group.height);
304 texCoord[0] = vec2(0, 0);
305 texCoord[1] = vec2(1, 0);
306 texCoord[2] = vec2(1, 1);
307 texCoord[3] = vec2(0, 1);
308 drawMesh(mesh);
309
310 // reset color matrix
311 mState.setColorMatrix(mat4());
312
313 // free our fbo and texture
314 glDeleteFramebuffers(1, &group.fbo);
315 glDeleteTextures(1, &group.texture);
316}
317
Mathias Agopian3f844832013-08-07 21:24:32 -0700318void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700319 RenderEngine::dump(result);
Mathias Agopian3f844832013-08-07 21:24:32 -0700320}
321
322// ---------------------------------------------------------------------------
323}; // namespace android
324// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700325
326#if defined(__gl_h_)
327#error "don't include gl/gl.h in this file"
328#endif