blob: 90404fae6ba31dad93f744fb2245e2655b430476 [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
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700112using ui::Dataspace;
113
Chia-I Wub027f802017-11-29 14:00:52 -0800114GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
115 : mVpWidth(0), mVpHeight(0), mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700116 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
117 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
118
119 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
120 glPixelStorei(GL_PACK_ALIGNMENT, 4);
121
Chia-I Wub027f802017-11-29 14:00:52 -0800122 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700123 glGenTextures(1, &mProtectedTexName);
124 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800129 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 -0700130
Chia-I Wub027f802017-11-29 14:00:52 -0800131 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600132
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600133 if (mPlatformHasWideColor) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700134 ColorSpace srgb(ColorSpace::sRGB());
135 ColorSpace displayP3(ColorSpace::DisplayP3());
136 ColorSpace bt2020(ColorSpace::BT2020());
Chia-I Wu131d3762018-01-11 14:35:27 -0800137
Peiyong Lina296b0c2018-04-30 16:55:29 -0700138 // Compute sRGB to Display P3 transform matrix.
139 // NOTE: For now, we are limiting output wide color space support to
140 // Display-P3 only.
141 mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform());
142
143 // Compute Display P3 to sRGB transform matrix.
144 mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform());
145
146 // no chromatic adaptation needed since all color spaces use D65 for their white points.
147 mSrgbToXyz = srgb.getRGBtoXYZ();
148 mDisplayP3ToXyz = displayP3.getRGBtoXYZ();
149 mBt2020ToXyz = bt2020.getRGBtoXYZ();
Peiyong Lin9b03c732018-05-17 10:14:02 -0700150 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700151 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
152 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600153 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700154}
155
Chia-I Wub027f802017-11-29 14:00:52 -0800156GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700157
158size_t GLES20RenderEngine::getMaxTextureSize() const {
159 return mMaxTextureSize;
160}
161
162size_t GLES20RenderEngine::getMaxViewportDims() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800163 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
Mathias Agopian3f844832013-08-07 21:24:32 -0700164}
165
Chia-I Wub027f802017-11-29 14:00:52 -0800166void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
167 size_t hwh, bool yswap,
168 Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800169 int32_t l = sourceCrop.left;
170 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700171
172 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800173 int32_t t = hwh - sourceCrop.top;
174 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700175
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:
Chia-I Wub027f802017-11-29 14:00:52 -0800189 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700190 break;
191 case Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800192 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700193 break;
194 case Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800195 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700196 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
Chia-I Wub027f802017-11-29 14:00:52 -0800207void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
208 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
Chia-I Wu131d3762018-01-11 14:35:27 -0800225void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
226 mState.setY410BT2020(enable);
227}
228
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700229void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800230 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600231}
232
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700233void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800234 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600235}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600236
Peiyong Linfb069302018-04-25 14:34:31 -0700237void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
238 mState.setDisplayMaxLuminance(maxLuminance);
239}
240
Mathias Agopian49457ac2013-08-14 18:20:17 -0700241void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
242 GLuint target = texture.getTextureTarget();
243 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700244 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700245 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700246 filter = GL_LINEAR;
247 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700248 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
249 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
250 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
251 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700252
Mathias Agopian49457ac2013-08-14 18:20:17 -0700253 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700254}
255
256void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700257 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700258 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
259 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
260 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700261}
262
Chia-I Wu8e50e692018-05-04 10:12:37 -0700263void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700264 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700265}
266
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700267void GLES20RenderEngine::setSaturationMatrix(const mat4& saturationMatrix) {
268 mState.setSaturationMatrix(saturationMatrix);
269}
270
Mathias Agopian3f844832013-08-07 21:24:32 -0700271void GLES20RenderEngine::disableTexturing() {
272 mState.disableTexture();
273}
274
275void GLES20RenderEngine::disableBlending() {
276 glDisable(GL_BLEND);
277}
278
Chia-I Wub027f802017-11-29 14:00:52 -0800279void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
280 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700281 GLuint tname, name;
282 // turn our EGLImage into a texture
283 glGenTextures(1, &tname);
284 glBindTexture(GL_TEXTURE_2D, tname);
285 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
286
287 // create a Framebuffer Object to render into
288 glGenFramebuffers(1, &name);
289 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700290 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700291
292 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
293 *texName = tname;
294 *fbName = name;
295}
296
297void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
298 glBindFramebuffer(GL_FRAMEBUFFER, 0);
299 glDeleteFramebuffers(1, &fbName);
300 glDeleteTextures(1, &texName);
301}
302
Mathias Agopian19733a32013-08-28 18:13:56 -0700303void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700304 mState.setPremultipliedAlpha(true);
305 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700306 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700307 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700309}
310
311void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700312 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 if (mesh.getTexCoordsSize()) {
314 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800315 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
316 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700317 }
318
Chia-I Wub027f802017-11-29 14:00:52 -0800319 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
320 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700321
Peiyong Lina296b0c2018-04-30 16:55:29 -0700322 // By default, DISPLAY_P3 is the only supported wide color output. However,
323 // when HDR content is present, hardware composer may be able to handle
324 // BT2020 data space, in that case, the output data space is set to be
325 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
326 // to respect this and convert non-HDR content to HDR format.
327 if (mPlatformHasWideColor) {
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600328 Description wideColorState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700329 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
330 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
331 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
332 Dataspace::STANDARD_MASK);
333 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
334 Dataspace::TRANSFER_MASK);
335 bool needsXYZConversion = needsXYZTransformMatrix();
336
337 if (needsXYZConversion) {
338 // The supported input color spaces are standard RGB, Display P3 and BT2020.
339 switch (inputStandard) {
340 case Dataspace::STANDARD_DCI_P3:
341 wideColorState.setInputTransformMatrix(mDisplayP3ToXyz);
342 break;
343 case Dataspace::STANDARD_BT2020:
344 wideColorState.setInputTransformMatrix(mBt2020ToXyz);
345 break;
346 default:
347 wideColorState.setInputTransformMatrix(mSrgbToXyz);
348 break;
349 }
350
Peiyong Lin9b03c732018-05-17 10:14:02 -0700351 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700352 switch (outputStandard) {
353 case Dataspace::STANDARD_BT2020:
354 wideColorState.setOutputTransformMatrix(mXyzToBt2020);
355 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700356 case Dataspace::STANDARD_DCI_P3:
Peiyong Lina296b0c2018-04-30 16:55:29 -0700357 wideColorState.setOutputTransformMatrix(mXyzToDisplayP3);
358 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700359 default:
360 wideColorState.setOutputTransformMatrix(mXyzToSrgb);
361 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700362 }
363 } else if (inputStandard != outputStandard) {
364 // At this point, the input data space and output data space could be both
365 // HDR data spaces, but they match each other, we do nothing in this case.
366 // In addition to the case above, the input data space could be
367 // - scRGB linear
368 // - scRGB non-linear
369 // - sRGB
370 // - Display P3
371 // The output data spaces could be
372 // - sRGB
373 // - Display P3
374 if (outputStandard == Dataspace::STANDARD_BT709) {
375 wideColorState.setOutputTransformMatrix(mDisplayP3ToSrgb);
376 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
377 wideColorState.setOutputTransformMatrix(mSrgbToDisplayP3);
378 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600379 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700380
381 // we need to convert the RGB value to linear space and convert it back when:
382 // - there is a color matrix that is not an identity matrix, or
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700383 // - there is a saturation matrix that is not an identity matrix, or
Peiyong Lina296b0c2018-04-30 16:55:29 -0700384 // - there is an output transform matrix that is not an identity matrix, or
385 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700386 if (wideColorState.hasColorMatrix() || wideColorState.hasSaturationMatrix() ||
387 wideColorState.hasOutputTransformMatrix() || inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700388 switch (inputTransfer) {
389 case Dataspace::TRANSFER_ST2084:
390 wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084);
391 break;
392 case Dataspace::TRANSFER_HLG:
393 wideColorState.setInputTransferFunction(Description::TransferFunction::HLG);
394 break;
395 case Dataspace::TRANSFER_LINEAR:
396 wideColorState.setInputTransferFunction(Description::TransferFunction::LINEAR);
397 break;
398 default:
399 wideColorState.setInputTransferFunction(Description::TransferFunction::SRGB);
400 break;
401 }
402
403 switch (outputTransfer) {
404 case Dataspace::TRANSFER_ST2084:
405 wideColorState.setOutputTransferFunction(Description::TransferFunction::ST2084);
406 break;
407 case Dataspace::TRANSFER_HLG:
408 wideColorState.setOutputTransferFunction(Description::TransferFunction::HLG);
409 break;
410 default:
411 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
412 break;
413 }
414 }
415
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600416 ProgramCache::getInstance().useProgram(wideColorState);
417
418 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
419
420 if (outputDebugPPMs) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800421 static uint64_t wideColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600422 std::ostringstream out;
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800423 out << "/data/texture_out" << wideColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600424 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
425 }
426 } else {
427 ProgramCache::getInstance().useProgram(mState);
428
429 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
430 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700431
432 if (mesh.getTexCoordsSize()) {
433 glDisableVertexAttribArray(Program::texCoords);
434 }
435}
436
437void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700438 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800439 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700440 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
441 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700442}
443
Peiyong Lina296b0c2018-04-30 16:55:29 -0700444bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
445 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
446 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
447 return standard == Dataspace::STANDARD_BT2020 &&
448 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
449}
450
451// For convenience, we want to convert the input color space to XYZ color space first,
452// and then convert from XYZ color space to output color space when
453// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
454// HDR content will be tone-mapped to SDR; Or,
455// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
456// HLG content to PQ content.
457// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
458// input data space or output data space is HDR data space, and the input transfer function
459// doesn't match the output transfer function, we would enable an intermediate transfrom to
460// XYZ color space.
461bool GLES20RenderEngine::needsXYZTransformMatrix() const {
462 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
463 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
464 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
465 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
466 Dataspace::TRANSFER_MASK);
467
468 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
469}
470
Mathias Agopian3f844832013-08-07 21:24:32 -0700471// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800472} // namespace impl
473} // namespace RE
474} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700475// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700476
477#if defined(__gl_h_)
478#error "don't include gl/gl.h in this file"
479#endif