blob: 7112ca8d21ed1b47227d34904d9c68b4d909d06e [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
22#include <utils/String8.h>
23#include <utils/Trace.h>
24
25#include <cutils/compiler.h>
26
27#include "GLES20RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070028#include "Program.h"
29#include "ProgramCache.h"
30#include "Description.h"
31#include "Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070032#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070033
34// ---------------------------------------------------------------------------
35namespace android {
36// ---------------------------------------------------------------------------
37
Mathias Agopianff2ed702013-09-01 21:36:12 -070038GLES20RenderEngine::GLES20RenderEngine() :
39 mVpWidth(0), mVpHeight(0) {
Mathias Agopian3f844832013-08-07 21:24:32 -070040
41 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
42 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
43
44 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
45 glPixelStorei(GL_PACK_ALIGNMENT, 4);
46
47 struct pack565 {
48 inline uint16_t operator() (int r, int g, int b) const {
49 return (r<<11)|(g<<5)|b;
50 }
51 } pack565;
52
53 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
54 glGenTextures(1, &mProtectedTexName);
55 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
60 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
61 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -070062
63 //mColorBlindnessCorrection = M;
Mathias Agopian3f844832013-08-07 21:24:32 -070064}
65
66GLES20RenderEngine::~GLES20RenderEngine() {
67}
68
69
70size_t GLES20RenderEngine::getMaxTextureSize() const {
71 return mMaxTextureSize;
72}
73
74size_t GLES20RenderEngine::getMaxViewportDims() const {
75 return
76 mMaxViewportDims[0] < mMaxViewportDims[1] ?
77 mMaxViewportDims[0] : mMaxViewportDims[1];
78}
79
80void GLES20RenderEngine::setViewportAndProjection(
81 size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
Mathias Agopiana8c386f2013-08-26 20:42:07 -070082 mat4 m;
83 if (yswap) m = mat4::ortho(0, w, h, 0, 0, 1);
84 else m = mat4::ortho(0, w, 0, h, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -070085
86 glViewport(0, 0, vpw, vph);
87 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -070088 mVpWidth = vpw;
89 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -070090}
91
92void GLES20RenderEngine::setupLayerBlending(
93 bool premultipliedAlpha, bool opaque, int alpha) {
94
95 mState.setPremultipliedAlpha(premultipliedAlpha);
96 mState.setOpaque(opaque);
97 mState.setPlaneAlpha(alpha / 255.0f);
98
99 if (alpha < 0xFF || !opaque) {
100 glEnable(GL_BLEND);
101 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102 } else {
103 glDisable(GL_BLEND);
104 }
105}
106
107void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700108 mState.setPlaneAlpha(1.0f);
109 mState.setPremultipliedAlpha(true);
110 mState.setOpaque(false);
111 mState.setColor(0, 0, 0, alpha/255.0f);
112 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700113
114 if (alpha == 0xFF) {
115 glDisable(GL_BLEND);
116 } else {
117 glEnable(GL_BLEND);
118 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
119 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700120}
121
Mathias Agopian49457ac2013-08-14 18:20:17 -0700122void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
123 GLuint target = texture.getTextureTarget();
124 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700125 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700126 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700127 filter = GL_LINEAR;
128 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700129 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
130 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
131 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
132 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700133
Mathias Agopian49457ac2013-08-14 18:20:17 -0700134 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700135}
136
137void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700138 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700139 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
140 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
141 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700142}
143
144void GLES20RenderEngine::disableTexturing() {
145 mState.disableTexture();
146}
147
148void GLES20RenderEngine::disableBlending() {
149 glDisable(GL_BLEND);
150}
151
Mathias Agopian458197d2013-08-15 14:56:51 -0700152
153void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
154 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
155 GLuint tname, name;
156 // turn our EGLImage into a texture
157 glGenTextures(1, &tname);
158 glBindTexture(GL_TEXTURE_2D, tname);
159 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
160
161 // create a Framebuffer Object to render into
162 glGenFramebuffers(1, &name);
163 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700164 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700165
166 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
167 *texName = tname;
168 *fbName = name;
169}
170
171void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
172 glBindFramebuffer(GL_FRAMEBUFFER, 0);
173 glDeleteFramebuffers(1, &fbName);
174 glDeleteTextures(1, &texName);
175}
176
Mathias Agopian19733a32013-08-28 18:13:56 -0700177void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
178 mState.setPlaneAlpha(1.0f);
179 mState.setPremultipliedAlpha(true);
180 mState.setOpaque(false);
Mathias Agopian3f844832013-08-07 21:24:32 -0700181 mState.setColor(r, g, b, a);
Mathias Agopian19733a32013-08-28 18:13:56 -0700182 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700183 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700184}
185
186void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
187
188 ProgramCache::getInstance().useProgram(mState);
189
190 if (mesh.getTexCoordsSize()) {
191 glEnableVertexAttribArray(Program::texCoords);
192 glVertexAttribPointer(Program::texCoords,
193 mesh.getTexCoordsSize(),
194 GL_FLOAT, GL_FALSE,
195 mesh.getByteStride(),
196 mesh.getTexCoords());
197 }
198
199 glVertexAttribPointer(Program::position,
200 mesh.getVertexSize(),
201 GL_FLOAT, GL_FALSE,
202 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700203 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700204
205 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
206
207 if (mesh.getTexCoordsSize()) {
208 glDisableVertexAttribArray(Program::texCoords);
209 }
210}
211
Mathias Agopianff2ed702013-09-01 21:36:12 -0700212void GLES20RenderEngine::beginGroup(const mat4& colorTransform) {
213
214 GLuint tname, name;
215 // create the texture
216 glGenTextures(1, &tname);
217 glBindTexture(GL_TEXTURE_2D, tname);
218 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
219 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
222 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
223
224 // create a Framebuffer Object to render into
225 glGenFramebuffers(1, &name);
226 glBindFramebuffer(GL_FRAMEBUFFER, name);
227 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
228
229 Group group;
230 group.texture = tname;
231 group.fbo = name;
232 group.width = mVpWidth;
233 group.height = mVpHeight;
234 group.colorTransform = colorTransform;
235
236 mGroupStack.push(group);
237}
238
239void GLES20RenderEngine::endGroup() {
240
241 const Group group(mGroupStack.top());
242 mGroupStack.pop();
243
244 // activate the previous render target
245 GLuint fbo = 0;
246 if (!mGroupStack.isEmpty()) {
247 fbo = mGroupStack.top().fbo;
248 }
249 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
250
251 // set our state
252 Texture texture(Texture::TEXTURE_2D, group.texture);
253 texture.setDimensions(group.width, group.height);
254 glBindTexture(GL_TEXTURE_2D, group.texture);
255
256 mState.setPlaneAlpha(1.0f);
257 mState.setPremultipliedAlpha(true);
258 mState.setOpaque(false);
259 mState.setTexture(texture);
260 mState.setColorMatrix(group.colorTransform);
261 glDisable(GL_BLEND);
262
263 Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2);
264 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
265 Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>());
266 position[0] = vec2(0, 0);
267 position[1] = vec2(group.width, 0);
268 position[2] = vec2(group.width, group.height);
269 position[3] = vec2(0, group.height);
270 texCoord[0] = vec2(0, 0);
271 texCoord[1] = vec2(1, 0);
272 texCoord[2] = vec2(1, 1);
273 texCoord[3] = vec2(0, 1);
274 drawMesh(mesh);
275
276 // reset color matrix
277 mState.setColorMatrix(mat4());
278
279 // free our fbo and texture
280 glDeleteFramebuffers(1, &group.fbo);
281 glDeleteTextures(1, &group.texture);
282}
283
Mathias Agopian3f844832013-08-07 21:24:32 -0700284void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700285 RenderEngine::dump(result);
Mathias Agopian3f844832013-08-07 21:24:32 -0700286}
287
288// ---------------------------------------------------------------------------
289}; // namespace android
290// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700291
292#if defined(__gl_h_)
293#error "don't include gl/gl.h in this file"
294#endif