blob: daaa11e1d3c8166004a830a1720bb9bd18548702 [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
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
Mathias Agopian3f844832013-08-07 21:24:32 -070020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070023#include <GLES2/gl2ext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070024
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060025#include <ui/ColorSpace.h>
26#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070027#include <ui/Rect.h>
28
Mathias Agopian3f844832013-08-07 21:24:32 -070029#include <utils/String8.h>
30#include <utils/Trace.h>
31
32#include <cutils/compiler.h>
Riley Andrewsc3ebe662014-09-04 16:20:31 -070033#include <gui/ISurfaceComposer.h>
34#include <math.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070035
36#include "GLES20RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070037#include "Program.h"
38#include "ProgramCache.h"
39#include "Description.h"
40#include "Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070041#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070042
Kalle Raitabbdcf1f2017-05-22 15:47:46 -070043#include <sstream>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060044#include <fstream>
45
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060046// ---------------------------------------------------------------------------
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060047#ifdef USE_HWC2
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060048bool checkGlError(const char* op, int lineNumber) {
49 bool errorFound = false;
50 GLint error = glGetError();
51 while (error != GL_NO_ERROR) {
52 errorFound = true;
53 error = glGetError();
54 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
55 }
56 return errorFound;
57}
58
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060059static constexpr bool outputDebugPPMs = false;
60
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060061void writePPM(const char* basename, GLuint width, GLuint height) {
62 ALOGV("writePPM #%s: %d x %d", basename, width, height);
63
64 std::vector<GLubyte> pixels(width * height * 4);
65 std::vector<GLubyte> outBuffer(width * height * 3);
66
67 // TODO(courtneygo): We can now have float formats, need
68 // to remove this code or update to support.
69 // Make returned pixels fit in uint32_t, one byte per component
70 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
71 if (checkGlError(__FUNCTION__, __LINE__)) {
72 return;
73 }
74
75 std::string filename(basename);
76 filename.append(".ppm");
77 std::ofstream file(filename.c_str(), std::ios::binary);
78 if (!file.is_open()) {
79 ALOGE("Unable to open file: %s", filename.c_str());
80 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
81 "surfaceflinger to write debug images");
82 return;
83 }
84
85 file << "P6\n";
86 file << width << "\n";
87 file << height << "\n";
88 file << 255 << "\n";
89
90 auto ptr = reinterpret_cast<char*>(pixels.data());
91 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
92 for (int y = height - 1; y >= 0; y--) {
93 char* data = ptr + y * width * sizeof(uint32_t);
94
95 for (GLuint x = 0; x < width; x++) {
96 // Only copy R, G and B components
97 outPtr[0] = data[0];
98 outPtr[1] = data[1];
99 outPtr[2] = data[2];
100 data += sizeof(uint32_t);
101 outPtr += 3;
102 }
103 }
104 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
105}
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600106#endif
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600107
Mathias Agopian3f844832013-08-07 21:24:32 -0700108// ---------------------------------------------------------------------------
109namespace android {
110// ---------------------------------------------------------------------------
111
Kalle Raitabbdcf1f2017-05-22 15:47:46 -0700112GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags) :
113 mVpWidth(0),
114 mVpHeight(0),
115 mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700116
117 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
118 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
119
120 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
121 glPixelStorei(GL_PACK_ALIGNMENT, 4);
122
Andy McFaddenc6f21692013-10-11 11:16:03 -0700123 const uint16_t protTexData[] = { 0 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700124 glGenTextures(1, &mProtectedTexName);
125 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
131 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700132
133 //mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600134
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600135#ifdef USE_HWC2
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600136 if (mPlatformHasWideColor) {
137 // Compute sRGB to DisplayP3 color transform
138 // NOTE: For now, we are limiting wide-color support to
139 // Display-P3 only.
Courtney Goeltzenleuchter396f3bb2017-05-05 15:09:14 -0600140 mat3 srgbToP3 = ColorSpaceConnector(ColorSpace::sRGB(), ColorSpace::DisplayP3()).getTransform();
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600141
Romain Guy88d37dd2017-05-26 17:57:05 -0700142 // color transform needs to be expanded to 4x4 to be what the shader wants
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600143 // mat has an initializer that expands mat3 to mat4, but
144 // not an assignment operator
Romain Guy88d37dd2017-05-26 17:57:05 -0700145 mat4 gamutTransform(srgbToP3);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600146 mSrgbToDisplayP3 = gamutTransform;
147 }
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600148#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700149}
150
151GLES20RenderEngine::~GLES20RenderEngine() {
152}
153
154
155size_t GLES20RenderEngine::getMaxTextureSize() const {
156 return mMaxTextureSize;
157}
158
159size_t GLES20RenderEngine::getMaxViewportDims() const {
160 return
161 mMaxViewportDims[0] < mMaxViewportDims[1] ?
162 mMaxViewportDims[0] : mMaxViewportDims[1];
163}
164
165void GLES20RenderEngine::setViewportAndProjection(
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700166 size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
167 Transform::orientation_flags rotation) {
Dan Stozac1879002014-05-22 15:59:05 -0700168
169 size_t l = sourceCrop.left;
170 size_t r = sourceCrop.right;
171
172 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
173 size_t t = hwh - sourceCrop.top;
174 size_t b = hwh - sourceCrop.bottom;
175
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700176 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700177 if (yswap) {
178 m = mat4::ortho(l, r, t, b, 0, 1);
179 } else {
180 m = mat4::ortho(l, r, b, t, 0, 1);
181 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700182
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700183 // Apply custom rotation to the projection.
184 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
185 switch (rotation) {
186 case Transform::ROT_0:
187 break;
188 case Transform::ROT_90:
189 m = mat4::rotate(rot90InRadians, vec3(0,0,1)) * m;
190 break;
191 case Transform::ROT_180:
192 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0,0,1)) * m;
193 break;
194 case Transform::ROT_270:
195 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0,0,1)) * m;
196 break;
197 default:
198 break;
199 }
200
Mathias Agopian3f844832013-08-07 21:24:32 -0700201 glViewport(0, 0, vpw, vph);
202 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700203 mVpWidth = vpw;
204 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700205}
206
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha,
chaviw13fdc492017-06-27 12:40:18 -0700208 bool opaque, bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700209 mState.setPremultipliedAlpha(premultipliedAlpha);
210 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700211 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800212
chaviw13fdc492017-06-27 12:40:18 -0700213 if (disableTexture) {
214 mState.disableTexture();
215 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000216
chaviw13fdc492017-06-27 12:40:18 -0700217 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700218 glEnable(GL_BLEND);
219 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
220 } else {
221 glDisable(GL_BLEND);
222 }
223}
224
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000225#ifdef USE_HWC2
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600226void GLES20RenderEngine::setColorMode(android_color_mode mode) {
227 ALOGV("setColorMode: %s (0x%x)", decodeColorMode(mode).c_str(), mode);
228
229 if (mColorMode == mode) return;
230
231 if (!mPlatformHasWideColor || !mDisplayHasWideColor || mode == HAL_COLOR_MODE_SRGB ||
232 mode == HAL_COLOR_MODE_NATIVE) {
233 // We are returning back to our default color_mode
234 mUseWideColor = false;
235 mWideColorFrameCount = 0;
236 } else {
237 mUseWideColor = true;
238 }
239
240 mColorMode = mode;
241}
242
243void GLES20RenderEngine::setSourceDataSpace(android_dataspace source) {
244 if (source == HAL_DATASPACE_UNKNOWN) {
245 // Treat UNKNOWN as SRGB
246 source = HAL_DATASPACE_V0_SRGB;
247 }
248 mDataSpace = source;
249}
250
251void GLES20RenderEngine::setWideColor(bool hasWideColor) {
252 ALOGV("setWideColor: %s", hasWideColor ? "true" : "false");
253 mDisplayHasWideColor = hasWideColor;
254}
255
256bool GLES20RenderEngine::usesWideColor() {
257 return mUseWideColor;
258}
259#endif
260
Mathias Agopian49457ac2013-08-14 18:20:17 -0700261void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
262 GLuint target = texture.getTextureTarget();
263 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700264 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700265 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700266 filter = GL_LINEAR;
267 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700268 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
269 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
270 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
271 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700272
Mathias Agopian49457ac2013-08-14 18:20:17 -0700273 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700274}
275
276void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700277 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700278 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
279 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
280 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700281}
282
Dan Stozaf0087992014-10-20 15:46:09 -0700283mat4 GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
284 mat4 oldTransform = mState.getColorMatrix();
285 mState.setColorMatrix(colorTransform);
286 return oldTransform;
287}
288
Mathias Agopian3f844832013-08-07 21:24:32 -0700289void GLES20RenderEngine::disableTexturing() {
290 mState.disableTexture();
291}
292
293void GLES20RenderEngine::disableBlending() {
294 glDisable(GL_BLEND);
295}
296
Mathias Agopian458197d2013-08-15 14:56:51 -0700297
298void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
299 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
300 GLuint tname, name;
301 // turn our EGLImage into a texture
302 glGenTextures(1, &tname);
303 glBindTexture(GL_TEXTURE_2D, tname);
304 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
305
306 // create a Framebuffer Object to render into
307 glGenFramebuffers(1, &name);
308 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700309 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700310
311 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
312 *texName = tname;
313 *fbName = name;
314}
315
316void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
317 glBindFramebuffer(GL_FRAMEBUFFER, 0);
318 glDeleteFramebuffers(1, &fbName);
319 glDeleteTextures(1, &texName);
320}
321
Mathias Agopian19733a32013-08-28 18:13:56 -0700322void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700323 mState.setPremultipliedAlpha(true);
324 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700325 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700326 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700327 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700328}
329
330void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
331
Mathias Agopian3f844832013-08-07 21:24:32 -0700332 if (mesh.getTexCoordsSize()) {
333 glEnableVertexAttribArray(Program::texCoords);
334 glVertexAttribPointer(Program::texCoords,
335 mesh.getTexCoordsSize(),
336 GL_FLOAT, GL_FALSE,
337 mesh.getByteStride(),
338 mesh.getTexCoords());
339 }
340
341 glVertexAttribPointer(Program::position,
342 mesh.getVertexSize(),
343 GL_FLOAT, GL_FALSE,
344 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700345 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700346
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600347#ifdef USE_HWC2
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600348 if (usesWideColor()) {
349 Description wideColorState = mState;
350 if (mDataSpace != HAL_DATASPACE_DISPLAY_P3) {
351 wideColorState.setColorMatrix(mState.getColorMatrix() * mSrgbToDisplayP3);
Romain Guy88d37dd2017-05-26 17:57:05 -0700352 wideColorState.setWideGamut(true);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600353 ALOGV("drawMesh: gamut transform applied");
354 }
355 ProgramCache::getInstance().useProgram(wideColorState);
356
357 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
358
359 if (outputDebugPPMs) {
360 std::ostringstream out;
361 out << "/data/texture_out" << mWideColorFrameCount++;
362 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
363 }
364 } else {
365 ProgramCache::getInstance().useProgram(mState);
366
367 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
368 }
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600369#else
370 ProgramCache::getInstance().useProgram(mState);
371
372 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
373#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700374
375 if (mesh.getTexCoordsSize()) {
376 glDisableVertexAttribArray(Program::texCoords);
377 }
378}
379
380void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700381 RenderEngine::dump(result);
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600382#ifdef USE_HWC2
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600383 if (usesWideColor()) {
Courtney Goeltzenleuchterf3b2de12017-03-27 12:18:12 -0600384 result.append("Wide-color: On\n");
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600385 } else {
Courtney Goeltzenleuchterf3b2de12017-03-27 12:18:12 -0600386 result.append("Wide-color: Off\n");
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600387 }
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600388#endif
Mathias Agopian3f844832013-08-07 21:24:32 -0700389}
390
391// ---------------------------------------------------------------------------
392}; // namespace android
393// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700394
395#if defined(__gl_h_)
396#error "don't include gl/gl.h in this file"
397#endif