blob: bfabf4d873eb9dfd23fd0b82a4bbb7c44bb1129d [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"
Peiyong Line5a9a7f2018-08-30 15:32:13 -070040#include "GLFramebuffer.h"
Peiyong Linf1bada92018-08-29 09:39:31 -070041#include "GLImage.h"
42#include "GLSurface.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070043#include "Program.h"
44#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070045
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060046bool checkGlError(const char* op, int lineNumber) {
47 bool errorFound = false;
48 GLint error = glGetError();
49 while (error != GL_NO_ERROR) {
50 errorFound = true;
51 error = glGetError();
52 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
53 }
54 return errorFound;
55}
56
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060057static constexpr bool outputDebugPPMs = false;
58
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060059void writePPM(const char* basename, GLuint width, GLuint height) {
60 ALOGV("writePPM #%s: %d x %d", basename, width, height);
61
62 std::vector<GLubyte> pixels(width * height * 4);
63 std::vector<GLubyte> outBuffer(width * height * 3);
64
65 // TODO(courtneygo): We can now have float formats, need
66 // to remove this code or update to support.
67 // Make returned pixels fit in uint32_t, one byte per component
68 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
69 if (checkGlError(__FUNCTION__, __LINE__)) {
70 return;
71 }
72
73 std::string filename(basename);
74 filename.append(".ppm");
75 std::ofstream file(filename.c_str(), std::ios::binary);
76 if (!file.is_open()) {
77 ALOGE("Unable to open file: %s", filename.c_str());
78 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
79 "surfaceflinger to write debug images");
80 return;
81 }
82
83 file << "P6\n";
84 file << width << "\n";
85 file << height << "\n";
86 file << 255 << "\n";
87
88 auto ptr = reinterpret_cast<char*>(pixels.data());
89 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
90 for (int y = height - 1; y >= 0; y--) {
91 char* data = ptr + y * width * sizeof(uint32_t);
92
93 for (GLuint x = 0; x < width; x++) {
94 // Only copy R, G and B components
95 outPtr[0] = data[0];
96 outPtr[1] = data[1];
97 outPtr[2] = data[2];
98 data += sizeof(uint32_t);
99 outPtr += 3;
100 }
101 }
102 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
103}
104
Mathias Agopian3f844832013-08-07 21:24:32 -0700105namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700106namespace renderengine {
107namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700108
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
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700158std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() {
159 return std::make_unique<GLFramebuffer>(*this);
160}
161
Peiyong Linf1bada92018-08-29 09:39:31 -0700162std::unique_ptr<Surface> GLES20RenderEngine::createSurface() {
163 return std::make_unique<GLSurface>(*this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700164}
165
Peiyong Linf1bada92018-08-29 09:39:31 -0700166std::unique_ptr<Image> GLES20RenderEngine::createImage() {
167 return std::make_unique<GLImage>(*this);
168}
169
170void GLES20RenderEngine::primeCache() const {
171 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
172}
173
174bool GLES20RenderEngine::isCurrent() const {
175 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
176}
177
178bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) {
179 // Surface is an abstract interface. GLES20RenderEngine only ever
180 // creates GLSurface's, so it is safe to just cast to the actual
181 // type.
182 bool success = true;
183 const GLSurface& glSurface = static_cast<const GLSurface&>(surface);
184 EGLSurface eglSurface = glSurface.getEGLSurface();
185 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
186 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
187 if (success && glSurface.getAsync()) {
188 eglSwapInterval(mEGLDisplay, 0);
189 }
190 }
191 return success;
192}
193
194void GLES20RenderEngine::resetCurrentSurface() {
195 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
196}
197
198void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName,
199 const Image& image) {
200 const GLImage& glImage = static_cast<const GLImage&>(image);
201 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
202
203 glBindTexture(target, texName);
204 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700205 glEGLImageTargetTexture2DOES(target,
206 static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700207 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700208}
209
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700210status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
211 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
212 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
213 uint32_t textureName = glFramebuffer->getTextureName();
214 uint32_t framebufferName = glFramebuffer->getFramebufferName();
215
216 // Bind the texture and turn our EGLImage into a texture
217 glBindTexture(GL_TEXTURE_2D, textureName);
218 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
219
220 // Bind the Framebuffer to render into
221 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
222 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
223 GL_TEXTURE_2D, textureName, 0);
224
225 mRenderToFbo = true;
226
227 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
228
229 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES,
230 "glCheckFramebufferStatusOES error %d", glStatus);
231
232 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
233}
234
235void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
236 mRenderToFbo = false;
237
238 // back to main framebuffer
239 glBindFramebuffer(GL_FRAMEBUFFER, 0);
240
241 // Workaround for b/77935566 to force the EGL driver to release the
242 // screenshot buffer
243 setScissor(0, 0, 0, 0);
244 clearWithColor(0.0, 0.0, 0.0, 0.0);
245 disableScissor();
246}
247
Chia-I Wub027f802017-11-29 14:00:52 -0800248void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
Peiyong Linefefaac2018-08-17 12:27:51 -0700249 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800250 int32_t l = sourceCrop.left;
251 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700252 int32_t b = sourceCrop.bottom;
253 int32_t t = sourceCrop.top;
254 if (mRenderToFbo) {
255 std::swap(t, b);
Dan Stozac1879002014-05-22 15:59:05 -0700256 }
Chia-I Wu1be50b52018-08-29 10:44:48 -0700257 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700258
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700259 // Apply custom rotation to the projection.
260 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
261 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700262 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700263 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700264 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800265 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700266 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700267 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800268 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700269 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700270 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800271 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700272 break;
273 default:
274 break;
275 }
276
Mathias Agopian3f844832013-08-07 21:24:32 -0700277 glViewport(0, 0, vpw, vph);
278 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700279 mVpWidth = vpw;
280 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700281}
282
Chia-I Wub027f802017-11-29 14:00:52 -0800283void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
284 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700285 mState.setPremultipliedAlpha(premultipliedAlpha);
286 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700287 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288
chaviw13fdc492017-06-27 12:40:18 -0700289 if (disableTexture) {
290 mState.disableTexture();
291 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000292
chaviw13fdc492017-06-27 12:40:18 -0700293 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700294 glEnable(GL_BLEND);
295 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
296 } else {
297 glDisable(GL_BLEND);
298 }
299}
300
Chia-I Wu131d3762018-01-11 14:35:27 -0800301void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
302 mState.setY410BT2020(enable);
303}
304
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700305void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800306 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600307}
308
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700309void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800310 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600311}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600312
Peiyong Linfb069302018-04-25 14:34:31 -0700313void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
314 mState.setDisplayMaxLuminance(maxLuminance);
315}
316
Mathias Agopian49457ac2013-08-14 18:20:17 -0700317void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
318 GLuint target = texture.getTextureTarget();
319 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700321 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700322 filter = GL_LINEAR;
323 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700324 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
325 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
326 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
327 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700328
Mathias Agopian49457ac2013-08-14 18:20:17 -0700329 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700330}
331
332void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700333 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700334 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
335 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
336 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700337}
338
Chia-I Wu8e50e692018-05-04 10:12:37 -0700339void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
Dan Stozaf0087992014-10-20 15:46:09 -0700340 mState.setColorMatrix(colorTransform);
Dan Stozaf0087992014-10-20 15:46:09 -0700341}
342
Mathias Agopian3f844832013-08-07 21:24:32 -0700343void GLES20RenderEngine::disableTexturing() {
344 mState.disableTexture();
345}
346
347void GLES20RenderEngine::disableBlending() {
348 glDisable(GL_BLEND);
349}
350
Mathias Agopian19733a32013-08-28 18:13:56 -0700351void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700352 mState.setPremultipliedAlpha(true);
353 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700354 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700355 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700356 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700357}
358
359void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700360 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700361 if (mesh.getTexCoordsSize()) {
362 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800363 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
364 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700365 }
366
Chia-I Wub027f802017-11-29 14:00:52 -0800367 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
368 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700369
Peiyong Lina296b0c2018-04-30 16:55:29 -0700370 // By default, DISPLAY_P3 is the only supported wide color output. However,
371 // when HDR content is present, hardware composer may be able to handle
372 // BT2020 data space, in that case, the output data space is set to be
373 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
374 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700375 if (mUseColorManagement) {
376 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700377 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
378 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
379 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
380 Dataspace::STANDARD_MASK);
381 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
382 Dataspace::TRANSFER_MASK);
383 bool needsXYZConversion = needsXYZTransformMatrix();
384
385 if (needsXYZConversion) {
386 // The supported input color spaces are standard RGB, Display P3 and BT2020.
387 switch (inputStandard) {
388 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700389 managedState.setInputTransformMatrix(mDisplayP3ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700390 break;
391 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700392 managedState.setInputTransformMatrix(mBt2020ToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700393 break;
394 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700395 managedState.setInputTransformMatrix(mSrgbToXyz);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700396 break;
397 }
398
Peiyong Lin9b03c732018-05-17 10:14:02 -0700399 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700400 switch (outputStandard) {
401 case Dataspace::STANDARD_BT2020:
Peiyong Lin13effd12018-07-24 17:01:47 -0700402 managedState.setOutputTransformMatrix(mXyzToBt2020);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700403 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700404 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin13effd12018-07-24 17:01:47 -0700405 managedState.setOutputTransformMatrix(mXyzToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700406 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700407 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700408 managedState.setOutputTransformMatrix(mXyzToSrgb);
Peiyong Lin9b03c732018-05-17 10:14:02 -0700409 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700410 }
411 } else if (inputStandard != outputStandard) {
412 // At this point, the input data space and output data space could be both
413 // HDR data spaces, but they match each other, we do nothing in this case.
414 // In addition to the case above, the input data space could be
415 // - scRGB linear
416 // - scRGB non-linear
417 // - sRGB
418 // - Display P3
419 // The output data spaces could be
420 // - sRGB
421 // - Display P3
422 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700423 managedState.setOutputTransformMatrix(mDisplayP3ToSrgb);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700424 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700425 managedState.setOutputTransformMatrix(mSrgbToDisplayP3);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700426 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600427 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700428
429 // we need to convert the RGB value to linear space and convert it back when:
430 // - there is a color matrix that is not an identity matrix, or
431 // - there is an output transform matrix that is not an identity matrix, or
432 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700433 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800434 inputTransfer != outputTransfer) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700435 switch (inputTransfer) {
436 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700437 managedState.setInputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700438 break;
439 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700440 managedState.setInputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700441 break;
442 case Dataspace::TRANSFER_LINEAR:
Peiyong Lin13effd12018-07-24 17:01:47 -0700443 managedState.setInputTransferFunction(Description::TransferFunction::LINEAR);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700444 break;
445 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700446 managedState.setInputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700447 break;
448 }
449
450 switch (outputTransfer) {
451 case Dataspace::TRANSFER_ST2084:
Peiyong Lin13effd12018-07-24 17:01:47 -0700452 managedState.setOutputTransferFunction(Description::TransferFunction::ST2084);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700453 break;
454 case Dataspace::TRANSFER_HLG:
Peiyong Lin13effd12018-07-24 17:01:47 -0700455 managedState.setOutputTransferFunction(Description::TransferFunction::HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700456 break;
457 default:
Peiyong Lin13effd12018-07-24 17:01:47 -0700458 managedState.setOutputTransferFunction(Description::TransferFunction::SRGB);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700459 break;
460 }
461 }
462
Peiyong Lin13effd12018-07-24 17:01:47 -0700463 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600464
465 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
466
467 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700468 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600469 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700470 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600471 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
472 }
473 } else {
474 ProgramCache::getInstance().useProgram(mState);
475
476 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
477 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700478
479 if (mesh.getTexCoordsSize()) {
480 glDisableVertexAttribArray(Program::texCoords);
481 }
482}
483
Peiyong Linf1bada92018-08-29 09:39:31 -0700484size_t GLES20RenderEngine::getMaxTextureSize() const {
485 return mMaxTextureSize;
486}
487
488size_t GLES20RenderEngine::getMaxViewportDims() const {
489 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
490}
491
Mathias Agopian3f844832013-08-07 21:24:32 -0700492void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700493 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800494 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700495 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
496 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700497}
498
Peiyong Lina296b0c2018-04-30 16:55:29 -0700499bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
500 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
501 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
502 return standard == Dataspace::STANDARD_BT2020 &&
503 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
504}
505
506// For convenience, we want to convert the input color space to XYZ color space first,
507// and then convert from XYZ color space to output color space when
508// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
509// HDR content will be tone-mapped to SDR; Or,
510// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
511// HLG content to PQ content.
512// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
513// input data space or output data space is HDR data space, and the input transfer function
514// doesn't match the output transfer function, we would enable an intermediate transfrom to
515// XYZ color space.
516bool GLES20RenderEngine::needsXYZTransformMatrix() const {
517 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
518 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
519 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
520 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
521 Dataspace::TRANSFER_MASK);
522
523 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
524}
525
Peiyong Lin833074a2018-08-28 11:53:54 -0700526} // namespace gl
527} // namespace renderengine
528} // namespace android