blob: def3abbfef4ed1becf26257611e0224893619425 [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,
Peiyong Linefefaac2018-08-17 12:27:51 -0700205 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800206 int32_t l = sourceCrop.left;
207 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700208 int32_t b = sourceCrop.bottom;
209 int32_t t = sourceCrop.top;
210 if (mRenderToFbo) {
211 std::swap(t, b);
Dan Stozac1879002014-05-22 15:59:05 -0700212 }
Chia-I Wu1be50b52018-08-29 10:44:48 -0700213 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700214
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700215 // Apply custom rotation to the projection.
216 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
217 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700218 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700219 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700220 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800221 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700222 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700223 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800224 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700225 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700226 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800227 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700228 break;
229 default:
230 break;
231 }
232
Mathias Agopian3f844832013-08-07 21:24:32 -0700233 glViewport(0, 0, vpw, vph);
234 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700235 mVpWidth = vpw;
236 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700237}
238
Chia-I Wub027f802017-11-29 14:00:52 -0800239void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
240 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700241 mState.setPremultipliedAlpha(premultipliedAlpha);
242 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700243 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800244
chaviw13fdc492017-06-27 12:40:18 -0700245 if (disableTexture) {
246 mState.disableTexture();
247 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000248
chaviw13fdc492017-06-27 12:40:18 -0700249 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700250 glEnable(GL_BLEND);
251 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
252 } else {
253 glDisable(GL_BLEND);
254 }
255}
256
Chia-I Wu131d3762018-01-11 14:35:27 -0800257void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
258 mState.setY410BT2020(enable);
259}
260
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700261void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800262 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600263}
264
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700265void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800266 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600267}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600268
Peiyong Linfb069302018-04-25 14:34:31 -0700269void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
270 mState.setDisplayMaxLuminance(maxLuminance);
271}
272
Mathias Agopian49457ac2013-08-14 18:20:17 -0700273void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
274 GLuint target = texture.getTextureTarget();
275 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700276 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700277 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700278 filter = GL_LINEAR;
279 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700280 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
281 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
282 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
283 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700284
Mathias Agopian49457ac2013-08-14 18:20:17 -0700285 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700286}
287
288void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700289 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700290 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
291 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
292 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700293}
294
Chia-I Wu8e50e692018-05-04 10:12:37 -0700295void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700296 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700297}
298
Mathias Agopian3f844832013-08-07 21:24:32 -0700299void GLES20RenderEngine::disableTexturing() {
300 mState.disableTexture();
301}
302
303void GLES20RenderEngine::disableBlending() {
304 glDisable(GL_BLEND);
305}
306
Chia-I Wub027f802017-11-29 14:00:52 -0800307void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
308 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700309 GLuint tname, name;
310 // turn our EGLImage into a texture
311 glGenTextures(1, &tname);
312 glBindTexture(GL_TEXTURE_2D, tname);
313 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
314
315 // create a Framebuffer Object to render into
316 glGenFramebuffers(1, &name);
317 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700318 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700319
320 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
321 *texName = tname;
322 *fbName = name;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700323
324 mRenderToFbo = true;
Mathias Agopian458197d2013-08-15 14:56:51 -0700325}
326
327void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
Chia-I Wu1be50b52018-08-29 10:44:48 -0700328 mRenderToFbo = false;
329
Mathias Agopian458197d2013-08-15 14:56:51 -0700330 glBindFramebuffer(GL_FRAMEBUFFER, 0);
331 glDeleteFramebuffers(1, &fbName);
332 glDeleteTextures(1, &texName);
333}
334
Mathias Agopian19733a32013-08-28 18:13:56 -0700335void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700336 mState.setPremultipliedAlpha(true);
337 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700338 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700339 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700340 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700341}
342
343void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700344 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700345 if (mesh.getTexCoordsSize()) {
346 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800347 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
348 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700349 }
350
Chia-I Wub027f802017-11-29 14:00:52 -0800351 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
352 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700353
Peiyong Lina296b0c2018-04-30 16:55:29 -0700354 // By default, DISPLAY_P3 is the only supported wide color output. However,
355 // when HDR content is present, hardware composer may be able to handle
356 // BT2020 data space, in that case, the output data space is set to be
357 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
358 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700359 if (mUseColorManagement) {
360 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700361 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
362 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
363 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
364 Dataspace::STANDARD_MASK);
365 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
366 Dataspace::TRANSFER_MASK);
367 bool needsXYZConversion = needsXYZTransformMatrix();
368
369 if (needsXYZConversion) {
370 // The supported input color spaces are standard RGB, Display P3 and BT2020.
371 switch (inputStandard) {
372 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700373 managedState.setInputTransformMatrix(mDisplayP3ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700374 break;
375 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700376 managedState.setInputTransformMatrix(mBt2020ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700377 break;
378 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700379 managedState.setInputTransformMatrix(mSrgbToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700380 break;
381 }
382
Peiyong Lin9b03c732018-05-17 10:14:02 -0700383 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700384 switch (outputStandard) {
385 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700386 managedState.setOutputTransformMatrix(mXyzToBt2020);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700387 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700388 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700389 managedState.setOutputTransformMatrix(mXyzToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700390 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700391 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700392 managedState.setOutputTransformMatrix(mXyzToSrgb);
Peiyong Lin9b03c732018-05-17 10:14:02 -0700393 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700394 }
395 } else if (inputStandard != outputStandard) {
396 // At this point, the input data space and output data space could be both
397 // HDR data spaces, but they match each other, we do nothing in this case.
398 // In addition to the case above, the input data space could be
399 // - scRGB linear
400 // - scRGB non-linear
401 // - sRGB
402 // - Display P3
403 // The output data spaces could be
404 // - sRGB
405 // - Display P3
406 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700407 managedState.setOutputTransformMatrix(mDisplayP3ToSrgb);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700408 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700409 managedState.setOutputTransformMatrix(mSrgbToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700410 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600411 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700412
413 // we need to convert the RGB value to linear space and convert it back when:
414 // - there is a color matrix that is not an identity matrix, or
415 // - there is an output transform matrix that is not an identity matrix, or
416 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700417 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800418 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700419 switch (inputTransfer) {
420 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700421 managedState.setInputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700422 break;
423 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700424 managedState.setInputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700425 break;
426 case Dataspace::TRANSFER_LINEAR:
Peiyong Lin13effd12018-07-24 17:01:47 -0700427 managedState.setInputTransferFunction(Description::TransferFunction::LINEAR);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700428 break;
429 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700430 managedState.setInputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700431 break;
432 }
433
434 switch (outputTransfer) {
435 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700436 managedState.setOutputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700437 break;
438 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700439 managedState.setOutputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700440 break;
441 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700442 managedState.setOutputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700443 break;
444 }
445 }
446
Peiyong Lin13effd12018-07-24 17:01:47 -0700447 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600448
449 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
450
451 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700452 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600453 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700454 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600455 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
456 }
457 } else {
458 ProgramCache::getInstance().useProgram(mState);
459
460 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
461 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700462
463 if (mesh.getTexCoordsSize()) {
464 glDisableVertexAttribArray(Program::texCoords);
465 }
466}
467
Peiyong Linf1bada92018-08-29 09:39:31 -0700468size_t GLES20RenderEngine::getMaxTextureSize() const {
469 return mMaxTextureSize;
470}
471
472size_t GLES20RenderEngine::getMaxViewportDims() const {
473 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
474}
475
Mathias Agopian3f844832013-08-07 21:24:32 -0700476void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700477 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800478 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700479 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
480 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700481}
482
Peiyong Lina296b0c2018-04-30 16:55:29 -0700483bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
484 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
485 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
486 return standard == Dataspace::STANDARD_BT2020 &&
487 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
488}
489
490// For convenience, we want to convert the input color space to XYZ color space first,
491// and then convert from XYZ color space to output color space when
492// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
493// HDR content will be tone-mapped to SDR; Or,
494// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
495// HLG content to PQ content.
496// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
497// input data space or output data space is HDR data space, and the input transfer function
498// doesn't match the output transfer function, we would enable an intermediate transfrom to
499// XYZ color space.
500bool GLES20RenderEngine::needsXYZTransformMatrix() const {
501 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
502 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
503 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
504 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
505 Dataspace::TRANSFER_MASK);
506
507 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
508}
509
Peiyong Lin833074a2018-08-28 11:53:54 -0700510} // namespace gl
511} // namespace renderengine
512} // namespace android