blob: 406e611eef4d3d5b0e895f452d763fdf91bc4f11 [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
Andy McFaddenc6f21692013-10-11 11:16:03 -070051 const uint16_t protTexData[] = { 0 };
Mathias Agopian3f844832013-08-07 21:24:32 -070052 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);
Mathias Agopianff2ed702013-09-01 21:36:12 -070060
61 //mColorBlindnessCorrection = M;
Mathias Agopian3f844832013-08-07 21:24:32 -070062}
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(
Riley Andrewsc3ebe662014-09-04 16:20:31 -070079 size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
80 Transform::orientation_flags rotation) {
Dan Stozac1879002014-05-22 15:59:05 -070081
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
Mathias Agopiana8c386f2013-08-26 20:42:07 -070089 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -070090 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 }
Mathias Agopian3f844832013-08-07 21:24:32 -070095
Riley Andrewsc3ebe662014-09-04 16:20:31 -070096 // 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
Mathias Agopian3f844832013-08-07 21:24:32 -0700114 glViewport(0, 0, vpw, vph);
115 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700116 mVpWidth = vpw;
117 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700118}
119
Dan Stoza9e56aa02015-11-02 13:00:03 -0800120#ifdef USE_HWC2
121void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha,
122 bool opaque, float alpha) {
123#else
Mathias Agopian3f844832013-08-07 21:24:32 -0700124void GLES20RenderEngine::setupLayerBlending(
125 bool premultipliedAlpha, bool opaque, int alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800126#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700127
128 mState.setPremultipliedAlpha(premultipliedAlpha);
129 mState.setOpaque(opaque);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130#ifdef USE_HWC2
131 mState.setPlaneAlpha(alpha);
132
133 if (alpha < 1.0f || !opaque) {
134#else
Mathias Agopian3f844832013-08-07 21:24:32 -0700135 mState.setPlaneAlpha(alpha / 255.0f);
136
137 if (alpha < 0xFF || !opaque) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700139 glEnable(GL_BLEND);
140 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 } else {
142 glDisable(GL_BLEND);
143 }
144}
145
Dan Stoza9e56aa02015-11-02 13:00:03 -0800146#ifdef USE_HWC2
147void GLES20RenderEngine::setupDimLayerBlending(float alpha) {
148#else
Mathias Agopian3f844832013-08-07 21:24:32 -0700149void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150#endif
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700151 mState.setPlaneAlpha(1.0f);
152 mState.setPremultipliedAlpha(true);
153 mState.setOpaque(false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800154#ifdef USE_HWC2
155 mState.setColor(0, 0, 0, alpha);
156#else
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700157 mState.setColor(0, 0, 0, alpha/255.0f);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800158#endif
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700159 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700160
Dan Stoza9e56aa02015-11-02 13:00:03 -0800161#ifdef USE_HWC2
162 if (alpha == 1.0f) {
163#else
Mathias Agopian3f844832013-08-07 21:24:32 -0700164 if (alpha == 0xFF) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800165#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700166 glDisable(GL_BLEND);
167 } else {
168 glEnable(GL_BLEND);
169 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
170 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700171}
172
Mathias Agopian49457ac2013-08-14 18:20:17 -0700173void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
174 GLuint target = texture.getTextureTarget();
175 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700176 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700177 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700178 filter = GL_LINEAR;
179 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700180 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
181 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
182 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
183 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700184
Mathias Agopian49457ac2013-08-14 18:20:17 -0700185 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700186}
187
188void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700189 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700190 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
191 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
192 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700193}
194
Dan Stozaf0087992014-10-20 15:46:09 -0700195mat4 GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
196 mat4 oldTransform = mState.getColorMatrix();
197 mState.setColorMatrix(colorTransform);
198 return oldTransform;
199}
200
Mathias Agopian3f844832013-08-07 21:24:32 -0700201void GLES20RenderEngine::disableTexturing() {
202 mState.disableTexture();
203}
204
205void GLES20RenderEngine::disableBlending() {
206 glDisable(GL_BLEND);
207}
208
Mathias Agopian458197d2013-08-15 14:56:51 -0700209
210void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
211 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
212 GLuint tname, name;
213 // turn our EGLImage into a texture
214 glGenTextures(1, &tname);
215 glBindTexture(GL_TEXTURE_2D, tname);
216 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
217
218 // create a Framebuffer Object to render into
219 glGenFramebuffers(1, &name);
220 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700221 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700222
223 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
224 *texName = tname;
225 *fbName = name;
226}
227
228void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
229 glBindFramebuffer(GL_FRAMEBUFFER, 0);
230 glDeleteFramebuffers(1, &fbName);
231 glDeleteTextures(1, &texName);
232}
233
Mathias Agopian19733a32013-08-28 18:13:56 -0700234void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
235 mState.setPlaneAlpha(1.0f);
236 mState.setPremultipliedAlpha(true);
237 mState.setOpaque(false);
Mathias Agopian3f844832013-08-07 21:24:32 -0700238 mState.setColor(r, g, b, a);
Mathias Agopian19733a32013-08-28 18:13:56 -0700239 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700240 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700241}
242
243void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
244
245 ProgramCache::getInstance().useProgram(mState);
246
247 if (mesh.getTexCoordsSize()) {
248 glEnableVertexAttribArray(Program::texCoords);
249 glVertexAttribPointer(Program::texCoords,
250 mesh.getTexCoordsSize(),
251 GL_FLOAT, GL_FALSE,
252 mesh.getByteStride(),
253 mesh.getTexCoords());
254 }
255
256 glVertexAttribPointer(Program::position,
257 mesh.getVertexSize(),
258 GL_FLOAT, GL_FALSE,
259 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700260 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700261
262 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
263
264 if (mesh.getTexCoordsSize()) {
265 glDisableVertexAttribArray(Program::texCoords);
266 }
267}
268
269void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700270 RenderEngine::dump(result);
Mathias Agopian3f844832013-08-07 21:24:32 -0700271}
272
273// ---------------------------------------------------------------------------
274}; // namespace android
275// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700276
277#if defined(__gl_h_)
278#error "don't include gl/gl.h in this file"
279#endif