blob: 21865941ade8d76ce062f98d1af571edcedccabc [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 <math.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070034
Chia-I Wub027f802017-11-29 14:00:52 -080035#include "Description.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070036#include "GLES20RenderEngine.h"
Chia-I Wub027f802017-11-29 14:00:52 -080037#include "Mesh.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070038#include "Program.h"
39#include "ProgramCache.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070040#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070041
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060042#include <fstream>
Chia-I Wub027f802017-11-29 14:00:52 -080043#include <sstream>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060044
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060045// ---------------------------------------------------------------------------
46bool checkGlError(const char* op, int lineNumber) {
47 bool errorFound = false;
48 GLint error = glGetError();
49 while (error != GL_NO_ERROR) {
50 errorFound = true;
51 error = glGetError();
52 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
53 }
54 return errorFound;
55}
56
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060057static constexpr bool outputDebugPPMs = false;
58
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060059void writePPM(const char* basename, GLuint width, GLuint height) {
60 ALOGV("writePPM #%s: %d x %d", basename, width, height);
61
62 std::vector<GLubyte> pixels(width * height * 4);
63 std::vector<GLubyte> outBuffer(width * height * 3);
64
65 // TODO(courtneygo): We can now have float formats, need
66 // to remove this code or update to support.
67 // Make returned pixels fit in uint32_t, one byte per component
68 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
69 if (checkGlError(__FUNCTION__, __LINE__)) {
70 return;
71 }
72
73 std::string filename(basename);
74 filename.append(".ppm");
75 std::ofstream file(filename.c_str(), std::ios::binary);
76 if (!file.is_open()) {
77 ALOGE("Unable to open file: %s", filename.c_str());
78 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
79 "surfaceflinger to write debug images");
80 return;
81 }
82
83 file << "P6\n";
84 file << width << "\n";
85 file << height << "\n";
86 file << 255 << "\n";
87
88 auto ptr = reinterpret_cast<char*>(pixels.data());
89 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
90 for (int y = height - 1; y >= 0; y--) {
91 char* data = ptr + y * width * sizeof(uint32_t);
92
93 for (GLuint x = 0; x < width; x++) {
94 // Only copy R, G and B components
95 outPtr[0] = data[0];
96 outPtr[1] = data[1];
97 outPtr[2] = data[2];
98 data += sizeof(uint32_t);
99 outPtr += 3;
100 }
101 }
102 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
103}
104
Mathias Agopian3f844832013-08-07 21:24:32 -0700105// ---------------------------------------------------------------------------
106namespace android {
Lloyd Pique144e1162017-12-20 16:44:52 -0800107namespace RE {
108namespace impl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700109// ---------------------------------------------------------------------------
110
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700111using ui::Dataspace;
112
Chia-I Wub027f802017-11-29 14:00:52 -0800113GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
Chia-I Wu93e14df2018-06-04 10:10:17 -0700114 : RenderEngine(featureFlags),
115 mVpWidth(0),
116 mVpHeight(0),
117 mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700118 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
119 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
120
121 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
122 glPixelStorei(GL_PACK_ALIGNMENT, 4);
123
Chia-I Wub027f802017-11-29 14:00:52 -0800124 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700125 glGenTextures(1, &mProtectedTexName);
126 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800131 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 -0700132
Chia-I Wub027f802017-11-29 14:00:52 -0800133 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600134
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600135 if (mPlatformHasWideColor) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700136 ColorSpace srgb(ColorSpace::sRGB());
137 ColorSpace displayP3(ColorSpace::DisplayP3());
138 ColorSpace bt2020(ColorSpace::BT2020());
Chia-I Wu131d3762018-01-11 14:35:27 -0800139
Peiyong Lina296b0c2018-04-30 16:55:29 -0700140 // Compute sRGB to Display P3 transform matrix.
141 // NOTE: For now, we are limiting output wide color space support to
142 // Display-P3 only.
143 mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform());
144
145 // Compute Display P3 to sRGB transform matrix.
146 mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform());
147
148 // no chromatic adaptation needed since all color spaces use D65 for their white points.
149 mSrgbToXyz = srgb.getRGBtoXYZ();
150 mDisplayP3ToXyz = displayP3.getRGBtoXYZ();
151 mBt2020ToXyz = bt2020.getRGBtoXYZ();
Peiyong Lin9b03c732018-05-17 10:14:02 -0700152 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700153 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
154 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600155 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700156}
157
Chia-I Wub027f802017-11-29 14:00:52 -0800158GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700159
160size_t GLES20RenderEngine::getMaxTextureSize() const {
161 return mMaxTextureSize;
162}
163
164size_t GLES20RenderEngine::getMaxViewportDims() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800165 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
Mathias Agopian3f844832013-08-07 21:24:32 -0700166}
167
Chia-I Wub027f802017-11-29 14:00:52 -0800168void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
169 size_t hwh, bool yswap,
Peiyong Linefefaac2018-08-17 12:27:51 -0700170 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800171 int32_t l = sourceCrop.left;
172 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700173
174 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800175 int32_t t = hwh - sourceCrop.top;
176 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700177
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700178 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700179 if (yswap) {
180 m = mat4::ortho(l, r, t, b, 0, 1);
181 } else {
182 m = mat4::ortho(l, r, b, t, 0, 1);
183 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700184
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700185 // Apply custom rotation to the projection.
186 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
187 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700188 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700189 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700190 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800191 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700192 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700193 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800194 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700195 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700196 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800197 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700198 break;
199 default:
200 break;
201 }
202
Mathias Agopian3f844832013-08-07 21:24:32 -0700203 glViewport(0, 0, vpw, vph);
204 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700205 mVpWidth = vpw;
206 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700207}
208
Chia-I Wub027f802017-11-29 14:00:52 -0800209void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
210 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700211 mState.setPremultipliedAlpha(premultipliedAlpha);
212 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700213 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214
chaviw13fdc492017-06-27 12:40:18 -0700215 if (disableTexture) {
216 mState.disableTexture();
217 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000218
chaviw13fdc492017-06-27 12:40:18 -0700219 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700220 glEnable(GL_BLEND);
221 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
222 } else {
223 glDisable(GL_BLEND);
224 }
225}
226
Chia-I Wu131d3762018-01-11 14:35:27 -0800227void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
228 mState.setY410BT2020(enable);
229}
230
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700231void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800232 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600233}
234
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700235void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800236 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600237}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600238
Peiyong Linfb069302018-04-25 14:34:31 -0700239void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
240 mState.setDisplayMaxLuminance(maxLuminance);
241}
242
Mathias Agopian49457ac2013-08-14 18:20:17 -0700243void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
244 GLuint target = texture.getTextureTarget();
245 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700246 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700247 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700248 filter = GL_LINEAR;
249 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700250 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
251 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
252 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
253 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700254
Mathias Agopian49457ac2013-08-14 18:20:17 -0700255 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700256}
257
258void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700259 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700260 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
261 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
262 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700263}
264
Chia-I Wu8e50e692018-05-04 10:12:37 -0700265void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700266 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700267}
268
Mathias Agopian3f844832013-08-07 21:24:32 -0700269void GLES20RenderEngine::disableTexturing() {
270 mState.disableTexture();
271}
272
273void GLES20RenderEngine::disableBlending() {
274 glDisable(GL_BLEND);
275}
276
Chia-I Wub027f802017-11-29 14:00:52 -0800277void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
278 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700279 GLuint tname, name;
280 // turn our EGLImage into a texture
281 glGenTextures(1, &tname);
282 glBindTexture(GL_TEXTURE_2D, tname);
283 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
284
285 // create a Framebuffer Object to render into
286 glGenFramebuffers(1, &name);
287 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700288 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700289
290 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
291 *texName = tname;
292 *fbName = name;
293}
294
295void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
296 glBindFramebuffer(GL_FRAMEBUFFER, 0);
297 glDeleteFramebuffers(1, &fbName);
298 glDeleteTextures(1, &texName);
299}
300
Mathias Agopian19733a32013-08-28 18:13:56 -0700301void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700302 mState.setPremultipliedAlpha(true);
303 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700304 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700305 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700307}
308
309void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700310 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 if (mesh.getTexCoordsSize()) {
312 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800313 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
314 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 }
316
Chia-I Wub027f802017-11-29 14:00:52 -0800317 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
318 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700319
Peiyong Lina296b0c2018-04-30 16:55:29 -0700320 // By default, DISPLAY_P3 is the only supported wide color output. However,
321 // when HDR content is present, hardware composer may be able to handle
322 // BT2020 data space, in that case, the output data space is set to be
323 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
324 // to respect this and convert non-HDR content to HDR format.
325 if (mPlatformHasWideColor) {
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600326 Description wideColorState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700327 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
328 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
329 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
330 Dataspace::STANDARD_MASK);
331 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
332 Dataspace::TRANSFER_MASK);
333 bool needsXYZConversion = needsXYZTransformMatrix();
334
335 if (needsXYZConversion) {
336 // The supported input color spaces are standard RGB, Display P3 and BT2020.
337 switch (inputStandard) {
338 case Dataspace::STANDARD_DCI_P3:
339 wideColorState.setInputTransformMatrix(mDisplayP3ToXyz);
340 break;
341 case Dataspace::STANDARD_BT2020:
342 wideColorState.setInputTransformMatrix(mBt2020ToXyz);
343 break;
344 default:
345 wideColorState.setInputTransformMatrix(mSrgbToXyz);
346 break;
347 }
348
Peiyong Lin9b03c732018-05-17 10:14:02 -0700349 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700350 switch (outputStandard) {
351 case Dataspace::STANDARD_BT2020:
352 wideColorState.setOutputTransformMatrix(mXyzToBt2020);
353 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700354 case Dataspace::STANDARD_DCI_P3:
Peiyong Lina296b0c2018-04-30 16:55:29 -0700355 wideColorState.setOutputTransformMatrix(mXyzToDisplayP3);
356 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700357 default:
358 wideColorState.setOutputTransformMatrix(mXyzToSrgb);
359 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700360 }
361 } else if (inputStandard != outputStandard) {
362 // At this point, the input data space and output data space could be both
363 // HDR data spaces, but they match each other, we do nothing in this case.
364 // In addition to the case above, the input data space could be
365 // - scRGB linear
366 // - scRGB non-linear
367 // - sRGB
368 // - Display P3
369 // The output data spaces could be
370 // - sRGB
371 // - Display P3
372 if (outputStandard == Dataspace::STANDARD_BT709) {
373 wideColorState.setOutputTransformMatrix(mDisplayP3ToSrgb);
374 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
375 wideColorState.setOutputTransformMatrix(mSrgbToDisplayP3);
376 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600377 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700378
379 // we need to convert the RGB value to linear space and convert it back when:
380 // - there is a color matrix that is not an identity matrix, or
381 // - there is an output transform matrix that is not an identity matrix, or
382 // - the input transfer function doesn't match the output transfer function.
Chia-I Wud49d6692018-06-27 07:17:41 +0800383 if (wideColorState.hasColorMatrix() || wideColorState.hasOutputTransformMatrix() ||
384 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700385 switch (inputTransfer) {
386 case Dataspace::TRANSFER_ST2084:
387 wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084);
388 break;
389 case Dataspace::TRANSFER_HLG:
390 wideColorState.setInputTransferFunction(Description::TransferFunction::HLG);
391 break;
392 case Dataspace::TRANSFER_LINEAR:
393 wideColorState.setInputTransferFunction(Description::TransferFunction::LINEAR);
394 break;
395 default:
396 wideColorState.setInputTransferFunction(Description::TransferFunction::SRGB);
397 break;
398 }
399
400 switch (outputTransfer) {
401 case Dataspace::TRANSFER_ST2084:
402 wideColorState.setOutputTransferFunction(Description::TransferFunction::ST2084);
403 break;
404 case Dataspace::TRANSFER_HLG:
405 wideColorState.setOutputTransferFunction(Description::TransferFunction::HLG);
406 break;
407 default:
408 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
409 break;
410 }
411 }
412
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600413 ProgramCache::getInstance().useProgram(wideColorState);
414
415 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
416
417 if (outputDebugPPMs) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800418 static uint64_t wideColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600419 std::ostringstream out;
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800420 out << "/data/texture_out" << wideColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600421 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
422 }
423 } else {
424 ProgramCache::getInstance().useProgram(mState);
425
426 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
427 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700428
429 if (mesh.getTexCoordsSize()) {
430 glDisableVertexAttribArray(Program::texCoords);
431 }
432}
433
434void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700435 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800436 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700437 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
438 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700439}
440
Peiyong Lina296b0c2018-04-30 16:55:29 -0700441bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
442 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
443 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
444 return standard == Dataspace::STANDARD_BT2020 &&
445 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
446}
447
448// For convenience, we want to convert the input color space to XYZ color space first,
449// and then convert from XYZ color space to output color space when
450// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
451// HDR content will be tone-mapped to SDR; Or,
452// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
453// HLG content to PQ content.
454// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
455// input data space or output data space is HDR data space, and the input transfer function
456// doesn't match the output transfer function, we would enable an intermediate transfrom to
457// XYZ color space.
458bool GLES20RenderEngine::needsXYZTransformMatrix() const {
459 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
460 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
461 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
462 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
463 Dataspace::TRANSFER_MASK);
464
465 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
466}
467
Mathias Agopian3f844832013-08-07 21:24:32 -0700468// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800469} // namespace impl
470} // namespace RE
471} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700472// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700473
474#if defined(__gl_h_)
475#error "don't include gl/gl.h in this file"
476#endif