blob: f577b33358cd23b57ed303144fe85a9e81bfa265 [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 Linf1bada92018-08-29 09:39:31 -070039#include "GLExtensions.h"
40#include "GLImage.h"
41#include "GLSurface.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070042#include "Program.h"
43#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070044
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060045bool checkGlError(const char* op, int lineNumber) {
46 bool errorFound = false;
47 GLint error = glGetError();
48 while (error != GL_NO_ERROR) {
49 errorFound = true;
50 error = glGetError();
51 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
52 }
53 return errorFound;
54}
55
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060056static constexpr bool outputDebugPPMs = false;
57
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060058void writePPM(const char* basename, GLuint width, GLuint height) {
59 ALOGV("writePPM #%s: %d x %d", basename, width, height);
60
61 std::vector<GLubyte> pixels(width * height * 4);
62 std::vector<GLubyte> outBuffer(width * height * 3);
63
64 // TODO(courtneygo): We can now have float formats, need
65 // to remove this code or update to support.
66 // Make returned pixels fit in uint32_t, one byte per component
67 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
68 if (checkGlError(__FUNCTION__, __LINE__)) {
69 return;
70 }
71
72 std::string filename(basename);
73 filename.append(".ppm");
74 std::ofstream file(filename.c_str(), std::ios::binary);
75 if (!file.is_open()) {
76 ALOGE("Unable to open file: %s", filename.c_str());
77 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
78 "surfaceflinger to write debug images");
79 return;
80 }
81
82 file << "P6\n";
83 file << width << "\n";
84 file << height << "\n";
85 file << 255 << "\n";
86
87 auto ptr = reinterpret_cast<char*>(pixels.data());
88 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
89 for (int y = height - 1; y >= 0; y--) {
90 char* data = ptr + y * width * sizeof(uint32_t);
91
92 for (GLuint x = 0; x < width; x++) {
93 // Only copy R, G and B components
94 outPtr[0] = data[0];
95 outPtr[1] = data[1];
96 outPtr[2] = data[2];
97 data += sizeof(uint32_t);
98 outPtr += 3;
99 }
100 }
101 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
102}
103
Mathias Agopian3f844832013-08-07 21:24:32 -0700104namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700105namespace renderengine {
106namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700107
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700108using ui::Dataspace;
109
Chia-I Wub027f802017-11-29 14:00:52 -0800110GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
Chia-I Wu93e14df2018-06-04 10:10:17 -0700111 : RenderEngine(featureFlags),
112 mVpWidth(0),
113 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700114 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700115 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
116 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
117
118 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
119 glPixelStorei(GL_PACK_ALIGNMENT, 4);
120
Chia-I Wub027f802017-11-29 14:00:52 -0800121 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700122 glGenTextures(1, &mProtectedTexName);
123 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800128 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 -0700129
Chia-I Wub027f802017-11-29 14:00:52 -0800130 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600131
Peiyong Lin13effd12018-07-24 17:01:47 -0700132 if (mUseColorManagement) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700133 ColorSpace srgb(ColorSpace::sRGB());
134 ColorSpace displayP3(ColorSpace::DisplayP3());
135 ColorSpace bt2020(ColorSpace::BT2020());
Chia-I Wu131d3762018-01-11 14:35:27 -0800136
Peiyong Lina296b0c2018-04-30 16:55:29 -0700137 // Compute sRGB to Display P3 transform matrix.
138 // NOTE: For now, we are limiting output wide color space support to
139 // Display-P3 only.
140 mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform());
141
142 // Compute Display P3 to sRGB transform matrix.
143 mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform());
144
145 // no chromatic adaptation needed since all color spaces use D65 for their white points.
146 mSrgbToXyz = srgb.getRGBtoXYZ();
147 mDisplayP3ToXyz = displayP3.getRGBtoXYZ();
148 mBt2020ToXyz = bt2020.getRGBtoXYZ();
Peiyong Lin9b03c732018-05-17 10:14:02 -0700149 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700150 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
151 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600152 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700153}
154
Chia-I Wub027f802017-11-29 14:00:52 -0800155GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700156
Peiyong Linf1bada92018-08-29 09:39:31 -0700157std::unique_ptr<Surface> GLES20RenderEngine::createSurface() {
158 return std::make_unique<GLSurface>(*this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700159}
160
Peiyong Linf1bada92018-08-29 09:39:31 -0700161std::unique_ptr<Image> GLES20RenderEngine::createImage() {
162 return std::make_unique<GLImage>(*this);
163}
164
165void GLES20RenderEngine::primeCache() const {
166 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
167}
168
169bool GLES20RenderEngine::isCurrent() const {
170 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
171}
172
173bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) {
174 // Surface is an abstract interface. GLES20RenderEngine only ever
175 // creates GLSurface's, so it is safe to just cast to the actual
176 // type.
177 bool success = true;
178 const GLSurface& glSurface = static_cast<const GLSurface&>(surface);
179 EGLSurface eglSurface = glSurface.getEGLSurface();
180 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
181 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
182 if (success && glSurface.getAsync()) {
183 eglSwapInterval(mEGLDisplay, 0);
184 }
185 }
186 return success;
187}
188
189void GLES20RenderEngine::resetCurrentSurface() {
190 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
191}
192
193void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName,
194 const Image& image) {
195 const GLImage& glImage = static_cast<const GLImage&>(image);
196 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
197
198 glBindTexture(target, texName);
199 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
200 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
201 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700202}
203
Chia-I Wub027f802017-11-29 14:00:52 -0800204void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
205 size_t hwh, bool yswap,
Peiyong Linefefaac2018-08-17 12:27:51 -0700206 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800207 int32_t l = sourceCrop.left;
208 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700209
210 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800211 int32_t t = hwh - sourceCrop.top;
212 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700213
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700214 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700215 if (yswap) {
216 m = mat4::ortho(l, r, t, b, 0, 1);
217 } else {
218 m = mat4::ortho(l, r, b, t, 0, 1);
219 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700220
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700221 // Apply custom rotation to the projection.
222 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
223 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700224 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700225 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700226 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800227 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700228 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700229 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800230 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700231 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700232 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800233 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700234 break;
235 default:
236 break;
237 }
238
Mathias Agopian3f844832013-08-07 21:24:32 -0700239 glViewport(0, 0, vpw, vph);
240 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700241 mVpWidth = vpw;
242 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700243}
244
Chia-I Wub027f802017-11-29 14:00:52 -0800245void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
246 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700247 mState.setPremultipliedAlpha(premultipliedAlpha);
248 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700249 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800250
chaviw13fdc492017-06-27 12:40:18 -0700251 if (disableTexture) {
252 mState.disableTexture();
253 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000254
chaviw13fdc492017-06-27 12:40:18 -0700255 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700256 glEnable(GL_BLEND);
257 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
258 } else {
259 glDisable(GL_BLEND);
260 }
261}
262
Chia-I Wu131d3762018-01-11 14:35:27 -0800263void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
264 mState.setY410BT2020(enable);
265}
266
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700267void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800268 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600269}
270
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700271void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800272 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600273}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600274
Peiyong Linfb069302018-04-25 14:34:31 -0700275void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
276 mState.setDisplayMaxLuminance(maxLuminance);
277}
278
Mathias Agopian49457ac2013-08-14 18:20:17 -0700279void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
280 GLuint target = texture.getTextureTarget();
281 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700282 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700283 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700284 filter = GL_LINEAR;
285 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700286 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
287 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
288 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
289 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700290
Mathias Agopian49457ac2013-08-14 18:20:17 -0700291 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700292}
293
294void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700295 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700296 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
297 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
298 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700299}
300
Chia-I Wu8e50e692018-05-04 10:12:37 -0700301void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700302 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700303}
304
Mathias Agopian3f844832013-08-07 21:24:32 -0700305void GLES20RenderEngine::disableTexturing() {
306 mState.disableTexture();
307}
308
309void GLES20RenderEngine::disableBlending() {
310 glDisable(GL_BLEND);
311}
312
Chia-I Wub027f802017-11-29 14:00:52 -0800313void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
314 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700315 GLuint tname, name;
316 // turn our EGLImage into a texture
317 glGenTextures(1, &tname);
318 glBindTexture(GL_TEXTURE_2D, tname);
319 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
320
321 // create a Framebuffer Object to render into
322 glGenFramebuffers(1, &name);
323 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700324 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700325
326 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
327 *texName = tname;
328 *fbName = name;
329}
330
331void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
332 glBindFramebuffer(GL_FRAMEBUFFER, 0);
333 glDeleteFramebuffers(1, &fbName);
334 glDeleteTextures(1, &texName);
335}
336
Mathias Agopian19733a32013-08-28 18:13:56 -0700337void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700338 mState.setPremultipliedAlpha(true);
339 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700340 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700341 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700342 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700343}
344
345void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700346 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700347 if (mesh.getTexCoordsSize()) {
348 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800349 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
350 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700351 }
352
Chia-I Wub027f802017-11-29 14:00:52 -0800353 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
354 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700355
Peiyong Lina296b0c2018-04-30 16:55:29 -0700356 // By default, DISPLAY_P3 is the only supported wide color output. However,
357 // when HDR content is present, hardware composer may be able to handle
358 // BT2020 data space, in that case, the output data space is set to be
359 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
360 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700361 if (mUseColorManagement) {
362 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700363 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
364 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
365 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
366 Dataspace::STANDARD_MASK);
367 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
368 Dataspace::TRANSFER_MASK);
369 bool needsXYZConversion = needsXYZTransformMatrix();
370
371 if (needsXYZConversion) {
372 // The supported input color spaces are standard RGB, Display P3 and BT2020.
373 switch (inputStandard) {
374 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700375 managedState.setInputTransformMatrix(mDisplayP3ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700376 break;
377 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700378 managedState.setInputTransformMatrix(mBt2020ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700379 break;
380 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700381 managedState.setInputTransformMatrix(mSrgbToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700382 break;
383 }
384
Peiyong Lin9b03c732018-05-17 10:14:02 -0700385 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700386 switch (outputStandard) {
387 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700388 managedState.setOutputTransformMatrix(mXyzToBt2020);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700389 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700390 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700391 managedState.setOutputTransformMatrix(mXyzToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700392 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700393 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700394 managedState.setOutputTransformMatrix(mXyzToSrgb);
Peiyong Lin9b03c732018-05-17 10:14:02 -0700395 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700396 }
397 } else if (inputStandard != outputStandard) {
398 // At this point, the input data space and output data space could be both
399 // HDR data spaces, but they match each other, we do nothing in this case.
400 // In addition to the case above, the input data space could be
401 // - scRGB linear
402 // - scRGB non-linear
403 // - sRGB
404 // - Display P3
405 // The output data spaces could be
406 // - sRGB
407 // - Display P3
408 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700409 managedState.setOutputTransformMatrix(mDisplayP3ToSrgb);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700410 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700411 managedState.setOutputTransformMatrix(mSrgbToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700412 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600413 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700414
415 // we need to convert the RGB value to linear space and convert it back when:
416 // - there is a color matrix that is not an identity matrix, or
417 // - there is an output transform matrix that is not an identity matrix, or
418 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700419 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800420 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700421 switch (inputTransfer) {
422 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700423 managedState.setInputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700424 break;
425 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700426 managedState.setInputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700427 break;
428 case Dataspace::TRANSFER_LINEAR:
Peiyong Lin13effd12018-07-24 17:01:47 -0700429 managedState.setInputTransferFunction(Description::TransferFunction::LINEAR);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700430 break;
431 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700432 managedState.setInputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700433 break;
434 }
435
436 switch (outputTransfer) {
437 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700438 managedState.setOutputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700439 break;
440 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700441 managedState.setOutputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700442 break;
443 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700444 managedState.setOutputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700445 break;
446 }
447 }
448
Peiyong Lin13effd12018-07-24 17:01:47 -0700449 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600450
451 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
452
453 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700454 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600455 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700456 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600457 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
458 }
459 } else {
460 ProgramCache::getInstance().useProgram(mState);
461
462 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
463 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700464
465 if (mesh.getTexCoordsSize()) {
466 glDisableVertexAttribArray(Program::texCoords);
467 }
468}
469
Peiyong Linf1bada92018-08-29 09:39:31 -0700470size_t GLES20RenderEngine::getMaxTextureSize() const {
471 return mMaxTextureSize;
472}
473
474size_t GLES20RenderEngine::getMaxViewportDims() const {
475 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
476}
477
Mathias Agopian3f844832013-08-07 21:24:32 -0700478void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700479 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800480 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700481 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
482 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700483}
484
Peiyong Lina296b0c2018-04-30 16:55:29 -0700485bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
486 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
487 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
488 return standard == Dataspace::STANDARD_BT2020 &&
489 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
490}
491
492// For convenience, we want to convert the input color space to XYZ color space first,
493// and then convert from XYZ color space to output color space when
494// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
495// HDR content will be tone-mapped to SDR; Or,
496// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
497// HLG content to PQ content.
498// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
499// input data space or output data space is HDR data space, and the input transfer function
500// doesn't match the output transfer function, we would enable an intermediate transfrom to
501// XYZ color space.
502bool GLES20RenderEngine::needsXYZTransformMatrix() const {
503 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
504 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
505 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
506 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
507 Dataspace::TRANSFER_MASK);
508
509 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
510}
511
Peiyong Lin833074a2018-08-28 11:53:54 -0700512} // namespace gl
513} // namespace renderengine
514} // namespace android