blob: e0f1850faa3a289372081801d62966fc442d0ec2 [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
Peiyong Lin833074a2018-08-28 11:53:54 -070022#include "GLES20RenderEngine.h"
23
24#include <math.h>
25#include <fstream>
26#include <sstream>
Peiyong Lincbc184f2018-08-22 13:24:10 -070027
Mathias Agopian3f844832013-08-07 21:24:32 -070028#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070029#include <GLES2/gl2ext.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070030#include <cutils/compiler.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Mesh.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <renderengine/Texture.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070033#include <renderengine/private/Description.h>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060034#include <ui/ColorSpace.h>
35#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070036#include <ui/Rect.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070037#include <utils/String8.h>
38#include <utils/Trace.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070039#include "Program.h"
40#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070041
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060042bool checkGlError(const char* op, int lineNumber) {
43 bool errorFound = false;
44 GLint error = glGetError();
45 while (error != GL_NO_ERROR) {
46 errorFound = true;
47 error = glGetError();
48 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
49 }
50 return errorFound;
51}
52
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060053static constexpr bool outputDebugPPMs = false;
54
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060055void writePPM(const char* basename, GLuint width, GLuint height) {
56 ALOGV("writePPM #%s: %d x %d", basename, width, height);
57
58 std::vector<GLubyte> pixels(width * height * 4);
59 std::vector<GLubyte> outBuffer(width * height * 3);
60
61 // TODO(courtneygo): We can now have float formats, need
62 // to remove this code or update to support.
63 // Make returned pixels fit in uint32_t, one byte per component
64 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
65 if (checkGlError(__FUNCTION__, __LINE__)) {
66 return;
67 }
68
69 std::string filename(basename);
70 filename.append(".ppm");
71 std::ofstream file(filename.c_str(), std::ios::binary);
72 if (!file.is_open()) {
73 ALOGE("Unable to open file: %s", filename.c_str());
74 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
75 "surfaceflinger to write debug images");
76 return;
77 }
78
79 file << "P6\n";
80 file << width << "\n";
81 file << height << "\n";
82 file << 255 << "\n";
83
84 auto ptr = reinterpret_cast<char*>(pixels.data());
85 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
86 for (int y = height - 1; y >= 0; y--) {
87 char* data = ptr + y * width * sizeof(uint32_t);
88
89 for (GLuint x = 0; x < width; x++) {
90 // Only copy R, G and B components
91 outPtr[0] = data[0];
92 outPtr[1] = data[1];
93 outPtr[2] = data[2];
94 data += sizeof(uint32_t);
95 outPtr += 3;
96 }
97 }
98 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
99}
100
Mathias Agopian3f844832013-08-07 21:24:32 -0700101namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700102namespace renderengine {
103namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700104
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700105using ui::Dataspace;
106
Chia-I Wub027f802017-11-29 14:00:52 -0800107GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
Chia-I Wu93e14df2018-06-04 10:10:17 -0700108 : RenderEngine(featureFlags),
109 mVpWidth(0),
110 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700111 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700112 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
113 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
114
115 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
116 glPixelStorei(GL_PACK_ALIGNMENT, 4);
117
Chia-I Wub027f802017-11-29 14:00:52 -0800118 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700119 glGenTextures(1, &mProtectedTexName);
120 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800125 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 -0700126
Chia-I Wub027f802017-11-29 14:00:52 -0800127 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600128
Peiyong Lin13effd12018-07-24 17:01:47 -0700129 if (mUseColorManagement) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700130 ColorSpace srgb(ColorSpace::sRGB());
131 ColorSpace displayP3(ColorSpace::DisplayP3());
132 ColorSpace bt2020(ColorSpace::BT2020());
Chia-I Wu131d3762018-01-11 14:35:27 -0800133
Peiyong Lina296b0c2018-04-30 16:55:29 -0700134 // Compute sRGB to Display P3 transform matrix.
135 // NOTE: For now, we are limiting output wide color space support to
136 // Display-P3 only.
137 mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform());
138
139 // Compute Display P3 to sRGB transform matrix.
140 mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform());
141
142 // no chromatic adaptation needed since all color spaces use D65 for their white points.
143 mSrgbToXyz = srgb.getRGBtoXYZ();
144 mDisplayP3ToXyz = displayP3.getRGBtoXYZ();
145 mBt2020ToXyz = bt2020.getRGBtoXYZ();
Peiyong Lin9b03c732018-05-17 10:14:02 -0700146 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700147 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
148 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600149 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700150}
151
Chia-I Wub027f802017-11-29 14:00:52 -0800152GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700153
154size_t GLES20RenderEngine::getMaxTextureSize() const {
155 return mMaxTextureSize;
156}
157
158size_t GLES20RenderEngine::getMaxViewportDims() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800159 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
Mathias Agopian3f844832013-08-07 21:24:32 -0700160}
161
Chia-I Wub027f802017-11-29 14:00:52 -0800162void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
163 size_t hwh, bool yswap,
Peiyong Linefefaac2018-08-17 12:27:51 -0700164 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800165 int32_t l = sourceCrop.left;
166 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700167
168 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800169 int32_t t = hwh - sourceCrop.top;
170 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700171
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700172 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700173 if (yswap) {
174 m = mat4::ortho(l, r, t, b, 0, 1);
175 } else {
176 m = mat4::ortho(l, r, b, t, 0, 1);
177 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700178
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700179 // Apply custom rotation to the projection.
180 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
181 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700182 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700183 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700184 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800185 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700186 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700187 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800188 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700189 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700190 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800191 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700192 break;
193 default:
194 break;
195 }
196
Mathias Agopian3f844832013-08-07 21:24:32 -0700197 glViewport(0, 0, vpw, vph);
198 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700199 mVpWidth = vpw;
200 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700201}
202
Chia-I Wub027f802017-11-29 14:00:52 -0800203void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
204 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700205 mState.setPremultipliedAlpha(premultipliedAlpha);
206 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700207 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208
chaviw13fdc492017-06-27 12:40:18 -0700209 if (disableTexture) {
210 mState.disableTexture();
211 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000212
chaviw13fdc492017-06-27 12:40:18 -0700213 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700214 glEnable(GL_BLEND);
215 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
216 } else {
217 glDisable(GL_BLEND);
218 }
219}
220
Chia-I Wu131d3762018-01-11 14:35:27 -0800221void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
222 mState.setY410BT2020(enable);
223}
224
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700225void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800226 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600227}
228
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700229void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800230 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600231}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600232
Peiyong Linfb069302018-04-25 14:34:31 -0700233void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
234 mState.setDisplayMaxLuminance(maxLuminance);
235}
236
Mathias Agopian49457ac2013-08-14 18:20:17 -0700237void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
238 GLuint target = texture.getTextureTarget();
239 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700240 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700241 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700242 filter = GL_LINEAR;
243 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700244 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
245 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
246 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
247 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700248
Mathias Agopian49457ac2013-08-14 18:20:17 -0700249 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700250}
251
252void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700253 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700254 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
255 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
256 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700257}
258
Chia-I Wu8e50e692018-05-04 10:12:37 -0700259void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700260 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700261}
262
Mathias Agopian3f844832013-08-07 21:24:32 -0700263void GLES20RenderEngine::disableTexturing() {
264 mState.disableTexture();
265}
266
267void GLES20RenderEngine::disableBlending() {
268 glDisable(GL_BLEND);
269}
270
Chia-I Wub027f802017-11-29 14:00:52 -0800271void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
272 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700273 GLuint tname, name;
274 // turn our EGLImage into a texture
275 glGenTextures(1, &tname);
276 glBindTexture(GL_TEXTURE_2D, tname);
277 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
278
279 // create a Framebuffer Object to render into
280 glGenFramebuffers(1, &name);
281 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700282 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700283
284 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
285 *texName = tname;
286 *fbName = name;
287}
288
289void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
290 glBindFramebuffer(GL_FRAMEBUFFER, 0);
291 glDeleteFramebuffers(1, &fbName);
292 glDeleteTextures(1, &texName);
293}
294
Mathias Agopian19733a32013-08-28 18:13:56 -0700295void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700296 mState.setPremultipliedAlpha(true);
297 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700298 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700299 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700300 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700301}
302
303void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700304 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700305 if (mesh.getTexCoordsSize()) {
306 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800307 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
308 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 }
310
Chia-I Wub027f802017-11-29 14:00:52 -0800311 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
312 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700313
Peiyong Lina296b0c2018-04-30 16:55:29 -0700314 // By default, DISPLAY_P3 is the only supported wide color output. However,
315 // when HDR content is present, hardware composer may be able to handle
316 // BT2020 data space, in that case, the output data space is set to be
317 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
318 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700319 if (mUseColorManagement) {
320 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700321 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
322 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
323 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
324 Dataspace::STANDARD_MASK);
325 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
326 Dataspace::TRANSFER_MASK);
327 bool needsXYZConversion = needsXYZTransformMatrix();
328
329 if (needsXYZConversion) {
330 // The supported input color spaces are standard RGB, Display P3 and BT2020.
331 switch (inputStandard) {
332 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700333 managedState.setInputTransformMatrix(mDisplayP3ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700334 break;
335 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700336 managedState.setInputTransformMatrix(mBt2020ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700337 break;
338 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700339 managedState.setInputTransformMatrix(mSrgbToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700340 break;
341 }
342
Peiyong Lin9b03c732018-05-17 10:14:02 -0700343 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700344 switch (outputStandard) {
345 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700346 managedState.setOutputTransformMatrix(mXyzToBt2020);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700347 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700348 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700349 managedState.setOutputTransformMatrix(mXyzToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700350 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700351 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700352 managedState.setOutputTransformMatrix(mXyzToSrgb);
Peiyong Lin9b03c732018-05-17 10:14:02 -0700353 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700354 }
355 } else if (inputStandard != outputStandard) {
356 // At this point, the input data space and output data space could be both
357 // HDR data spaces, but they match each other, we do nothing in this case.
358 // In addition to the case above, the input data space could be
359 // - scRGB linear
360 // - scRGB non-linear
361 // - sRGB
362 // - Display P3
363 // The output data spaces could be
364 // - sRGB
365 // - Display P3
366 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700367 managedState.setOutputTransformMatrix(mDisplayP3ToSrgb);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700368 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700369 managedState.setOutputTransformMatrix(mSrgbToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700370 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600371 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700372
373 // we need to convert the RGB value to linear space and convert it back when:
374 // - there is a color matrix that is not an identity matrix, or
375 // - there is an output transform matrix that is not an identity matrix, or
376 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700377 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800378 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700379 switch (inputTransfer) {
380 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700381 managedState.setInputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700382 break;
383 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700384 managedState.setInputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700385 break;
386 case Dataspace::TRANSFER_LINEAR:
Peiyong Lin13effd12018-07-24 17:01:47 -0700387 managedState.setInputTransferFunction(Description::TransferFunction::LINEAR);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700388 break;
389 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700390 managedState.setInputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700391 break;
392 }
393
394 switch (outputTransfer) {
395 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700396 managedState.setOutputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700397 break;
398 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700399 managedState.setOutputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700400 break;
401 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700402 managedState.setOutputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700403 break;
404 }
405 }
406
Peiyong Lin13effd12018-07-24 17:01:47 -0700407 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600408
409 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
410
411 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700412 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600413 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700414 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600415 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
416 }
417 } else {
418 ProgramCache::getInstance().useProgram(mState);
419
420 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
421 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700422
423 if (mesh.getTexCoordsSize()) {
424 glDisableVertexAttribArray(Program::texCoords);
425 }
426}
427
428void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700429 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800430 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700431 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
432 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700433}
434
Peiyong Lina296b0c2018-04-30 16:55:29 -0700435bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
436 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
437 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
438 return standard == Dataspace::STANDARD_BT2020 &&
439 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
440}
441
442// For convenience, we want to convert the input color space to XYZ color space first,
443// and then convert from XYZ color space to output color space when
444// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
445// HDR content will be tone-mapped to SDR; Or,
446// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
447// HLG content to PQ content.
448// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
449// input data space or output data space is HDR data space, and the input transfer function
450// doesn't match the output transfer function, we would enable an intermediate transfrom to
451// XYZ color space.
452bool GLES20RenderEngine::needsXYZTransformMatrix() const {
453 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
454 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
455 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
456 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
457 Dataspace::TRANSFER_MASK);
458
459 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
460}
461
Peiyong Lin833074a2018-08-28 11:53:54 -0700462} // namespace gl
463} // namespace renderengine
464} // namespace android
Mathias Agopian458197d2013-08-15 14:56:51 -0700465
466#if defined(__gl_h_)
467#error "don't include gl/gl.h in this file"
468#endif