blob: 8c1f04e560ad86cc3b663eeab85f1205a721f9ca [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>
28
29#include "GLES20RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070030#include "Program.h"
31#include "ProgramCache.h"
32#include "Description.h"
33#include "Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070034#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070035
36// ---------------------------------------------------------------------------
37namespace android {
38// ---------------------------------------------------------------------------
39
Mathias Agopianff2ed702013-09-01 21:36:12 -070040GLES20RenderEngine::GLES20RenderEngine() :
41 mVpWidth(0), mVpHeight(0) {
Mathias Agopian3f844832013-08-07 21:24:32 -070042
43 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
44 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
45
46 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
47 glPixelStorei(GL_PACK_ALIGNMENT, 4);
48
49 struct pack565 {
50 inline uint16_t operator() (int r, int g, int b) const {
51 return (r<<11)|(g<<5)|b;
52 }
53 } pack565;
54
Andy McFaddenc6f21692013-10-11 11:16:03 -070055 const uint16_t protTexData[] = { 0 };
Mathias Agopian3f844832013-08-07 21:24:32 -070056 glGenTextures(1, &mProtectedTexName);
57 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
62 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
63 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -070064
65 //mColorBlindnessCorrection = M;
Mathias Agopian3f844832013-08-07 21:24:32 -070066}
67
68GLES20RenderEngine::~GLES20RenderEngine() {
69}
70
71
72size_t GLES20RenderEngine::getMaxTextureSize() const {
73 return mMaxTextureSize;
74}
75
76size_t GLES20RenderEngine::getMaxViewportDims() const {
77 return
78 mMaxViewportDims[0] < mMaxViewportDims[1] ?
79 mMaxViewportDims[0] : mMaxViewportDims[1];
80}
81
82void GLES20RenderEngine::setViewportAndProjection(
Dan Stozac1879002014-05-22 15:59:05 -070083 size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap) {
84
85 size_t l = sourceCrop.left;
86 size_t r = sourceCrop.right;
87
88 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
89 size_t t = hwh - sourceCrop.top;
90 size_t b = hwh - sourceCrop.bottom;
91
Mathias Agopiana8c386f2013-08-26 20:42:07 -070092 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -070093 if (yswap) {
94 m = mat4::ortho(l, r, t, b, 0, 1);
95 } else {
96 m = mat4::ortho(l, r, b, t, 0, 1);
97 }
Mathias Agopian3f844832013-08-07 21:24:32 -070098
99 glViewport(0, 0, vpw, vph);
100 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700101 mVpWidth = vpw;
102 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700103}
104
105void GLES20RenderEngine::setupLayerBlending(
106 bool premultipliedAlpha, bool opaque, int alpha) {
107
108 mState.setPremultipliedAlpha(premultipliedAlpha);
109 mState.setOpaque(opaque);
110 mState.setPlaneAlpha(alpha / 255.0f);
111
112 if (alpha < 0xFF || !opaque) {
113 glEnable(GL_BLEND);
114 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
115 } else {
116 glDisable(GL_BLEND);
117 }
118}
119
120void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700121 mState.setPlaneAlpha(1.0f);
122 mState.setPremultipliedAlpha(true);
123 mState.setOpaque(false);
124 mState.setColor(0, 0, 0, alpha/255.0f);
125 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700126
127 if (alpha == 0xFF) {
128 glDisable(GL_BLEND);
129 } else {
130 glEnable(GL_BLEND);
131 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
132 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700133}
134
Mathias Agopian49457ac2013-08-14 18:20:17 -0700135void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
136 GLuint target = texture.getTextureTarget();
137 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700138 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700139 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700140 filter = GL_LINEAR;
141 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700142 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
143 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
144 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
145 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700146
Mathias Agopian49457ac2013-08-14 18:20:17 -0700147 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700148}
149
150void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700151 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700152 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
153 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
154 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700155}
156
157void GLES20RenderEngine::disableTexturing() {
158 mState.disableTexture();
159}
160
161void GLES20RenderEngine::disableBlending() {
162 glDisable(GL_BLEND);
163}
164
Mathias Agopian458197d2013-08-15 14:56:51 -0700165
166void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
167 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
168 GLuint tname, name;
169 // turn our EGLImage into a texture
170 glGenTextures(1, &tname);
171 glBindTexture(GL_TEXTURE_2D, tname);
172 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
173
174 // create a Framebuffer Object to render into
175 glGenFramebuffers(1, &name);
176 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700177 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700178
179 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
180 *texName = tname;
181 *fbName = name;
182}
183
184void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
185 glBindFramebuffer(GL_FRAMEBUFFER, 0);
186 glDeleteFramebuffers(1, &fbName);
187 glDeleteTextures(1, &texName);
188}
189
Mathias Agopian19733a32013-08-28 18:13:56 -0700190void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
191 mState.setPlaneAlpha(1.0f);
192 mState.setPremultipliedAlpha(true);
193 mState.setOpaque(false);
Mathias Agopian3f844832013-08-07 21:24:32 -0700194 mState.setColor(r, g, b, a);
Mathias Agopian19733a32013-08-28 18:13:56 -0700195 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700196 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700197}
198
199void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
200
201 ProgramCache::getInstance().useProgram(mState);
202
203 if (mesh.getTexCoordsSize()) {
204 glEnableVertexAttribArray(Program::texCoords);
205 glVertexAttribPointer(Program::texCoords,
206 mesh.getTexCoordsSize(),
207 GL_FLOAT, GL_FALSE,
208 mesh.getByteStride(),
209 mesh.getTexCoords());
210 }
211
212 glVertexAttribPointer(Program::position,
213 mesh.getVertexSize(),
214 GL_FLOAT, GL_FALSE,
215 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700216 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700217
218 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
219
220 if (mesh.getTexCoordsSize()) {
221 glDisableVertexAttribArray(Program::texCoords);
222 }
223}
224
Mathias Agopianff2ed702013-09-01 21:36:12 -0700225void GLES20RenderEngine::beginGroup(const mat4& colorTransform) {
226
227 GLuint tname, name;
228 // create the texture
229 glGenTextures(1, &tname);
230 glBindTexture(GL_TEXTURE_2D, tname);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
236
237 // create a Framebuffer Object to render into
238 glGenFramebuffers(1, &name);
239 glBindFramebuffer(GL_FRAMEBUFFER, name);
240 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
241
242 Group group;
243 group.texture = tname;
244 group.fbo = name;
245 group.width = mVpWidth;
246 group.height = mVpHeight;
247 group.colorTransform = colorTransform;
248
249 mGroupStack.push(group);
250}
251
252void GLES20RenderEngine::endGroup() {
253
254 const Group group(mGroupStack.top());
255 mGroupStack.pop();
256
257 // activate the previous render target
258 GLuint fbo = 0;
259 if (!mGroupStack.isEmpty()) {
260 fbo = mGroupStack.top().fbo;
261 }
262 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
263
264 // set our state
265 Texture texture(Texture::TEXTURE_2D, group.texture);
266 texture.setDimensions(group.width, group.height);
267 glBindTexture(GL_TEXTURE_2D, group.texture);
268
269 mState.setPlaneAlpha(1.0f);
270 mState.setPremultipliedAlpha(true);
271 mState.setOpaque(false);
272 mState.setTexture(texture);
273 mState.setColorMatrix(group.colorTransform);
274 glDisable(GL_BLEND);
275
276 Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2);
277 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
278 Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>());
279 position[0] = vec2(0, 0);
280 position[1] = vec2(group.width, 0);
281 position[2] = vec2(group.width, group.height);
282 position[3] = vec2(0, group.height);
283 texCoord[0] = vec2(0, 0);
284 texCoord[1] = vec2(1, 0);
285 texCoord[2] = vec2(1, 1);
286 texCoord[3] = vec2(0, 1);
287 drawMesh(mesh);
288
289 // reset color matrix
290 mState.setColorMatrix(mat4());
291
292 // free our fbo and texture
293 glDeleteFramebuffers(1, &group.fbo);
294 glDeleteTextures(1, &group.texture);
295}
296
Mathias Agopian3f844832013-08-07 21:24:32 -0700297void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700298 RenderEngine::dump(result);
Mathias Agopian3f844832013-08-07 21:24:32 -0700299}
300
301// ---------------------------------------------------------------------------
302}; // namespace android
303// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700304
305#if defined(__gl_h_)
306#error "don't include gl/gl.h in this file"
307#endif