blob: 9ecf8ce2e7b8badd5f5f570a33d1e479013851d8 [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
Chia-I Wub027f802017-11-29 14:00:52 -080036#include "Description.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070037#include "GLES20RenderEngine.h"
Chia-I Wub027f802017-11-29 14:00:52 -080038#include "Mesh.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070039#include "Program.h"
40#include "ProgramCache.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070041#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070042
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060043#include <fstream>
Chia-I Wub027f802017-11-29 14:00:52 -080044#include <sstream>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060045
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060046// ---------------------------------------------------------------------------
47bool checkGlError(const char* op, int lineNumber) {
48 bool errorFound = false;
49 GLint error = glGetError();
50 while (error != GL_NO_ERROR) {
51 errorFound = true;
52 error = glGetError();
53 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
54 }
55 return errorFound;
56}
57
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060058static constexpr bool outputDebugPPMs = false;
59
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060060void writePPM(const char* basename, GLuint width, GLuint height) {
61 ALOGV("writePPM #%s: %d x %d", basename, width, height);
62
63 std::vector<GLubyte> pixels(width * height * 4);
64 std::vector<GLubyte> outBuffer(width * height * 3);
65
66 // TODO(courtneygo): We can now have float formats, need
67 // to remove this code or update to support.
68 // Make returned pixels fit in uint32_t, one byte per component
69 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
70 if (checkGlError(__FUNCTION__, __LINE__)) {
71 return;
72 }
73
74 std::string filename(basename);
75 filename.append(".ppm");
76 std::ofstream file(filename.c_str(), std::ios::binary);
77 if (!file.is_open()) {
78 ALOGE("Unable to open file: %s", filename.c_str());
79 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
80 "surfaceflinger to write debug images");
81 return;
82 }
83
84 file << "P6\n";
85 file << width << "\n";
86 file << height << "\n";
87 file << 255 << "\n";
88
89 auto ptr = reinterpret_cast<char*>(pixels.data());
90 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
91 for (int y = height - 1; y >= 0; y--) {
92 char* data = ptr + y * width * sizeof(uint32_t);
93
94 for (GLuint x = 0; x < width; x++) {
95 // Only copy R, G and B components
96 outPtr[0] = data[0];
97 outPtr[1] = data[1];
98 outPtr[2] = data[2];
99 data += sizeof(uint32_t);
100 outPtr += 3;
101 }
102 }
103 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
104}
105
Mathias Agopian3f844832013-08-07 21:24:32 -0700106// ---------------------------------------------------------------------------
107namespace android {
Lloyd Pique144e1162017-12-20 16:44:52 -0800108namespace RE {
109namespace impl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700110// ---------------------------------------------------------------------------
111
Chia-I Wub027f802017-11-29 14:00:52 -0800112GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
113 : mVpWidth(0), mVpHeight(0), mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700114 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
115 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
116
117 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
118 glPixelStorei(GL_PACK_ALIGNMENT, 4);
119
Chia-I Wub027f802017-11-29 14:00:52 -0800120 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700121 glGenTextures(1, &mProtectedTexName);
122 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700128
Chia-I Wub027f802017-11-29 14:00:52 -0800129 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600130
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600131 if (mPlatformHasWideColor) {
132 // Compute sRGB to DisplayP3 color transform
133 // NOTE: For now, we are limiting wide-color support to
134 // Display-P3 only.
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800135 mSrgbToDisplayP3 = mat4(
136 ColorSpaceConnector(ColorSpace::sRGB(), ColorSpace::DisplayP3()).getTransform());
Chia-I Wu131d3762018-01-11 14:35:27 -0800137
138 // Compute BT2020 to DisplayP3 color transform
139 mBt2020ToDisplayP3 = mat4(
140 ColorSpaceConnector(ColorSpace::BT2020(), ColorSpace::DisplayP3()).getTransform());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600141 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700142}
143
Chia-I Wub027f802017-11-29 14:00:52 -0800144GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700145
146size_t GLES20RenderEngine::getMaxTextureSize() const {
147 return mMaxTextureSize;
148}
149
150size_t GLES20RenderEngine::getMaxViewportDims() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800151 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
Mathias Agopian3f844832013-08-07 21:24:32 -0700152}
153
Chia-I Wub027f802017-11-29 14:00:52 -0800154void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
155 size_t hwh, bool yswap,
156 Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800157 int32_t l = sourceCrop.left;
158 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700159
160 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800161 int32_t t = hwh - sourceCrop.top;
162 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700163
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700164 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700165 if (yswap) {
166 m = mat4::ortho(l, r, t, b, 0, 1);
167 } else {
168 m = mat4::ortho(l, r, b, t, 0, 1);
169 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700170
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700171 // Apply custom rotation to the projection.
172 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
173 switch (rotation) {
174 case Transform::ROT_0:
175 break;
176 case Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800177 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700178 break;
179 case Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800180 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700181 break;
182 case Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800183 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700184 break;
185 default:
186 break;
187 }
188
Mathias Agopian3f844832013-08-07 21:24:32 -0700189 glViewport(0, 0, vpw, vph);
190 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700191 mVpWidth = vpw;
192 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700193}
194
Chia-I Wub027f802017-11-29 14:00:52 -0800195void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
196 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700197 mState.setPremultipliedAlpha(premultipliedAlpha);
198 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700199 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800200
chaviw13fdc492017-06-27 12:40:18 -0700201 if (disableTexture) {
202 mState.disableTexture();
203 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000204
chaviw13fdc492017-06-27 12:40:18 -0700205 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700206 glEnable(GL_BLEND);
207 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
208 } else {
209 glDisable(GL_BLEND);
210 }
211}
212
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600213void GLES20RenderEngine::setColorMode(android_color_mode mode) {
214 ALOGV("setColorMode: %s (0x%x)", decodeColorMode(mode).c_str(), mode);
215
216 if (mColorMode == mode) return;
217
218 if (!mPlatformHasWideColor || !mDisplayHasWideColor || mode == HAL_COLOR_MODE_SRGB ||
219 mode == HAL_COLOR_MODE_NATIVE) {
220 // We are returning back to our default color_mode
221 mUseWideColor = false;
222 mWideColorFrameCount = 0;
223 } else {
224 mUseWideColor = true;
225 }
226
227 mColorMode = mode;
228}
229
230void GLES20RenderEngine::setSourceDataSpace(android_dataspace source) {
231 if (source == HAL_DATASPACE_UNKNOWN) {
232 // Treat UNKNOWN as SRGB
233 source = HAL_DATASPACE_V0_SRGB;
234 }
235 mDataSpace = source;
236}
237
Chia-I Wu131d3762018-01-11 14:35:27 -0800238void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
239 mState.setY410BT2020(enable);
240}
241
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600242void GLES20RenderEngine::setWideColor(bool hasWideColor) {
243 ALOGV("setWideColor: %s", hasWideColor ? "true" : "false");
244 mDisplayHasWideColor = hasWideColor;
245}
246
247bool GLES20RenderEngine::usesWideColor() {
248 return mUseWideColor;
249}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600250
Mathias Agopian49457ac2013-08-14 18:20:17 -0700251void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
252 GLuint target = texture.getTextureTarget();
253 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700254 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700255 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700256 filter = GL_LINEAR;
257 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700258 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
259 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
260 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
261 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700262
Mathias Agopian49457ac2013-08-14 18:20:17 -0700263 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700264}
265
266void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700267 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700268 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
269 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
270 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700271}
272
Dan Stozaf0087992014-10-20 15:46:09 -0700273mat4 GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
274 mat4 oldTransform = mState.getColorMatrix();
275 mState.setColorMatrix(colorTransform);
276 return oldTransform;
277}
278
Mathias Agopian3f844832013-08-07 21:24:32 -0700279void GLES20RenderEngine::disableTexturing() {
280 mState.disableTexture();
281}
282
283void GLES20RenderEngine::disableBlending() {
284 glDisable(GL_BLEND);
285}
286
Chia-I Wub027f802017-11-29 14:00:52 -0800287void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
288 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700289 GLuint tname, name;
290 // turn our EGLImage into a texture
291 glGenTextures(1, &tname);
292 glBindTexture(GL_TEXTURE_2D, tname);
293 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
294
295 // create a Framebuffer Object to render into
296 glGenFramebuffers(1, &name);
297 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700298 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700299
300 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
301 *texName = tname;
302 *fbName = name;
303}
304
305void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
306 glBindFramebuffer(GL_FRAMEBUFFER, 0);
307 glDeleteFramebuffers(1, &fbName);
308 glDeleteTextures(1, &texName);
309}
310
Mathias Agopian19733a32013-08-28 18:13:56 -0700311void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700312 mState.setPremultipliedAlpha(true);
313 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700314 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700315 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700316 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700317}
318
319void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 if (mesh.getTexCoordsSize()) {
321 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800322 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
323 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700324 }
325
Chia-I Wub027f802017-11-29 14:00:52 -0800326 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
327 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700328
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600329 if (usesWideColor()) {
330 Description wideColorState = mState;
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800331 switch (int(mDataSpace)) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800332 case HAL_DATASPACE_DISPLAY_P3:
333 // input matches output
334 break;
Peiyong Linb0094482018-02-02 11:34:16 -0800335 case HAL_DATASPACE_V0_SCRGB_LINEAR:
336 wideColorState.setColorMatrix(mState.getColorMatrix() * mSrgbToDisplayP3);
337 wideColorState.setInputTransferFunction(Description::TransferFunction::LINEAR);
338 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
339 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800340 case HAL_DATASPACE_BT2020_PQ:
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800341 case HAL_DATASPACE_BT2020_ITU_PQ:
Chia-I Wu131d3762018-01-11 14:35:27 -0800342 wideColorState.setColorMatrix(mState.getColorMatrix() * mBt2020ToDisplayP3);
343 wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084);
344 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
345 wideColorState.enableToneMapping(true);
346 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800347 default:
348 wideColorState.setColorMatrix(mState.getColorMatrix() * mSrgbToDisplayP3);
349 wideColorState.setInputTransferFunction(Description::TransferFunction::SRGB);
350 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
351 ALOGV("drawMesh: gamut transform applied");
352 break;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600353 }
354 ProgramCache::getInstance().useProgram(wideColorState);
355
356 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
357
358 if (outputDebugPPMs) {
359 std::ostringstream out;
360 out << "/data/texture_out" << mWideColorFrameCount++;
361 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
362 }
363 } else {
364 ProgramCache::getInstance().useProgram(mState);
365
366 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
367 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700368
369 if (mesh.getTexCoordsSize()) {
370 glDisableVertexAttribArray(Program::texCoords);
371 }
372}
373
374void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700375 RenderEngine::dump(result);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600376 if (usesWideColor()) {
Courtney Goeltzenleuchterf3b2de12017-03-27 12:18:12 -0600377 result.append("Wide-color: On\n");
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600378 } else {
Courtney Goeltzenleuchterf3b2de12017-03-27 12:18:12 -0600379 result.append("Wide-color: Off\n");
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600380 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700381}
382
383// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800384} // namespace impl
385} // namespace RE
386} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700387// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700388
389#if defined(__gl_h_)
390#error "don't include gl/gl.h in this file"
391#endif