blob: 617ba76197f43f66638791d77591a94fe00be824 [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 Lincbc184f2018-08-22 13:24:10 -070022#include <renderengine/GLES20RenderEngine.h>
23
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070025#include <GLES2/gl2ext.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070026#include <renderengine/Description.h>
27#include <renderengine/Mesh.h>
28#include <renderengine/Program.h>
29#include <renderengine/ProgramCache.h>
30#include <renderengine/Texture.h>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060031#include <ui/ColorSpace.h>
32#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070033#include <ui/Rect.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070034#include <utils/String8.h>
35#include <utils/Trace.h>
36
37#include <cutils/compiler.h>
Riley Andrewsc3ebe662014-09-04 16:20:31 -070038#include <math.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070039
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060040#include <fstream>
Chia-I Wub027f802017-11-29 14:00:52 -080041#include <sstream>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060042
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060043// ---------------------------------------------------------------------------
44bool checkGlError(const char* op, int lineNumber) {
45 bool errorFound = false;
46 GLint error = glGetError();
47 while (error != GL_NO_ERROR) {
48 errorFound = true;
49 error = glGetError();
50 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
51 }
52 return errorFound;
53}
54
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060055static constexpr bool outputDebugPPMs = false;
56
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060057void writePPM(const char* basename, GLuint width, GLuint height) {
58 ALOGV("writePPM #%s: %d x %d", basename, width, height);
59
60 std::vector<GLubyte> pixels(width * height * 4);
61 std::vector<GLubyte> outBuffer(width * height * 3);
62
63 // TODO(courtneygo): We can now have float formats, need
64 // to remove this code or update to support.
65 // Make returned pixels fit in uint32_t, one byte per component
66 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
67 if (checkGlError(__FUNCTION__, __LINE__)) {
68 return;
69 }
70
71 std::string filename(basename);
72 filename.append(".ppm");
73 std::ofstream file(filename.c_str(), std::ios::binary);
74 if (!file.is_open()) {
75 ALOGE("Unable to open file: %s", filename.c_str());
76 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
77 "surfaceflinger to write debug images");
78 return;
79 }
80
81 file << "P6\n";
82 file << width << "\n";
83 file << height << "\n";
84 file << 255 << "\n";
85
86 auto ptr = reinterpret_cast<char*>(pixels.data());
87 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
88 for (int y = height - 1; y >= 0; y--) {
89 char* data = ptr + y * width * sizeof(uint32_t);
90
91 for (GLuint x = 0; x < width; x++) {
92 // Only copy R, G and B components
93 outPtr[0] = data[0];
94 outPtr[1] = data[1];
95 outPtr[2] = data[2];
96 data += sizeof(uint32_t);
97 outPtr += 3;
98 }
99 }
100 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
101}
102
Mathias Agopian3f844832013-08-07 21:24:32 -0700103// ---------------------------------------------------------------------------
104namespace android {
Lloyd Pique144e1162017-12-20 16:44:52 -0800105namespace RE {
106namespace impl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700107// ---------------------------------------------------------------------------
108
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700109using ui::Dataspace;
110
Chia-I Wub027f802017-11-29 14:00:52 -0800111GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
Chia-I Wu93e14df2018-06-04 10:10:17 -0700112 : RenderEngine(featureFlags),
113 mVpWidth(0),
114 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700115 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
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
Peiyong Lin13effd12018-07-24 17:01:47 -0700133 if (mUseColorManagement) {
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,
Peiyong Linefefaac2018-08-17 12:27:51 -0700168 ui::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) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700186 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700187 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700188 case ui::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;
Peiyong Linefefaac2018-08-17 12:27:51 -0700191 case ui::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;
Peiyong Linefefaac2018-08-17 12:27:51 -0700194 case ui::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
Mathias Agopian3f844832013-08-07 21:24:32 -0700267void GLES20RenderEngine::disableTexturing() {
268 mState.disableTexture();
269}
270
271void GLES20RenderEngine::disableBlending() {
272 glDisable(GL_BLEND);
273}
274
Chia-I Wub027f802017-11-29 14:00:52 -0800275void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
276 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700277 GLuint tname, name;
278 // turn our EGLImage into a texture
279 glGenTextures(1, &tname);
280 glBindTexture(GL_TEXTURE_2D, tname);
281 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
282
283 // create a Framebuffer Object to render into
284 glGenFramebuffers(1, &name);
285 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700286 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700287
288 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
289 *texName = tname;
290 *fbName = name;
291}
292
293void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
294 glBindFramebuffer(GL_FRAMEBUFFER, 0);
295 glDeleteFramebuffers(1, &fbName);
296 glDeleteTextures(1, &texName);
297}
298
Mathias Agopian19733a32013-08-28 18:13:56 -0700299void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700300 mState.setPremultipliedAlpha(true);
301 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700302 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700303 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700304 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700305}
306
307void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700308 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 if (mesh.getTexCoordsSize()) {
310 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800311 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
312 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 }
314
Chia-I Wub027f802017-11-29 14:00:52 -0800315 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
316 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700317
Peiyong Lina296b0c2018-04-30 16:55:29 -0700318 // By default, DISPLAY_P3 is the only supported wide color output. However,
319 // when HDR content is present, hardware composer may be able to handle
320 // BT2020 data space, in that case, the output data space is set to be
321 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
322 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700323 if (mUseColorManagement) {
324 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700325 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
326 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
327 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
328 Dataspace::STANDARD_MASK);
329 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
330 Dataspace::TRANSFER_MASK);
331 bool needsXYZConversion = needsXYZTransformMatrix();
332
333 if (needsXYZConversion) {
334 // The supported input color spaces are standard RGB, Display P3 and BT2020.
335 switch (inputStandard) {
336 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700337 managedState.setInputTransformMatrix(mDisplayP3ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700338 break;
339 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700340 managedState.setInputTransformMatrix(mBt2020ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700341 break;
342 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700343 managedState.setInputTransformMatrix(mSrgbToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700344 break;
345 }
346
Peiyong Lin9b03c732018-05-17 10:14:02 -0700347 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700348 switch (outputStandard) {
349 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700350 managedState.setOutputTransformMatrix(mXyzToBt2020);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700351 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700352 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700353 managedState.setOutputTransformMatrix(mXyzToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700354 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700355 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700356 managedState.setOutputTransformMatrix(mXyzToSrgb);
Peiyong Lin9b03c732018-05-17 10:14:02 -0700357 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700358 }
359 } else if (inputStandard != outputStandard) {
360 // At this point, the input data space and output data space could be both
361 // HDR data spaces, but they match each other, we do nothing in this case.
362 // In addition to the case above, the input data space could be
363 // - scRGB linear
364 // - scRGB non-linear
365 // - sRGB
366 // - Display P3
367 // The output data spaces could be
368 // - sRGB
369 // - Display P3
370 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700371 managedState.setOutputTransformMatrix(mDisplayP3ToSrgb);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700372 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700373 managedState.setOutputTransformMatrix(mSrgbToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700374 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600375 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700376
377 // we need to convert the RGB value to linear space and convert it back when:
378 // - there is a color matrix that is not an identity matrix, or
379 // - there is an output transform matrix that is not an identity matrix, or
380 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700381 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800382 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700383 switch (inputTransfer) {
384 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700385 managedState.setInputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700386 break;
387 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700388 managedState.setInputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700389 break;
390 case Dataspace::TRANSFER_LINEAR:
Peiyong Lin13effd12018-07-24 17:01:47 -0700391 managedState.setInputTransferFunction(Description::TransferFunction::LINEAR);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700392 break;
393 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700394 managedState.setInputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700395 break;
396 }
397
398 switch (outputTransfer) {
399 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700400 managedState.setOutputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700401 break;
402 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700403 managedState.setOutputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700404 break;
405 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700406 managedState.setOutputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700407 break;
408 }
409 }
410
Peiyong Lin13effd12018-07-24 17:01:47 -0700411 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600412
413 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
414
415 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700416 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600417 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700418 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600419 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
420 }
421 } else {
422 ProgramCache::getInstance().useProgram(mState);
423
424 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
425 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700426
427 if (mesh.getTexCoordsSize()) {
428 glDisableVertexAttribArray(Program::texCoords);
429 }
430}
431
432void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700433 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800434 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700435 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
436 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700437}
438
Peiyong Lina296b0c2018-04-30 16:55:29 -0700439bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
440 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
441 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
442 return standard == Dataspace::STANDARD_BT2020 &&
443 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
444}
445
446// For convenience, we want to convert the input color space to XYZ color space first,
447// and then convert from XYZ color space to output color space when
448// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
449// HDR content will be tone-mapped to SDR; Or,
450// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
451// HLG content to PQ content.
452// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
453// input data space or output data space is HDR data space, and the input transfer function
454// doesn't match the output transfer function, we would enable an intermediate transfrom to
455// XYZ color space.
456bool GLES20RenderEngine::needsXYZTransformMatrix() const {
457 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
458 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
459 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
460 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
461 Dataspace::TRANSFER_MASK);
462
463 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
464}
465
Mathias Agopian3f844832013-08-07 21:24:32 -0700466// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800467} // namespace impl
468} // namespace RE
469} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700470// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700471
472#if defined(__gl_h_)
473#error "don't include gl/gl.h in this file"
474#endif