blob: d35762d447f27a3884648366685bea90999cad2a [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>
Peiyong Lin60bedb52018-09-05 10:47:31 -070037#include <ui/Region.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070038#include <utils/KeyedVector.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070039#include <utils/String8.h>
40#include <utils/Trace.h>
Peiyong Linf1bada92018-08-29 09:39:31 -070041#include "GLExtensions.h"
Peiyong Line5a9a7f2018-08-30 15:32:13 -070042#include "GLFramebuffer.h"
Peiyong Linf1bada92018-08-29 09:39:31 -070043#include "GLImage.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070044#include "Program.h"
45#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070046
Peiyong Linf11f39b2018-09-05 14:37:41 -070047extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
48
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060049bool checkGlError(const char* op, int lineNumber) {
50 bool errorFound = false;
51 GLint error = glGetError();
52 while (error != GL_NO_ERROR) {
53 errorFound = true;
54 error = glGetError();
55 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
56 }
57 return errorFound;
58}
59
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060060static constexpr bool outputDebugPPMs = false;
61
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060062void writePPM(const char* basename, GLuint width, GLuint height) {
63 ALOGV("writePPM #%s: %d x %d", basename, width, height);
64
65 std::vector<GLubyte> pixels(width * height * 4);
66 std::vector<GLubyte> outBuffer(width * height * 3);
67
68 // TODO(courtneygo): We can now have float formats, need
69 // to remove this code or update to support.
70 // Make returned pixels fit in uint32_t, one byte per component
71 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
72 if (checkGlError(__FUNCTION__, __LINE__)) {
73 return;
74 }
75
76 std::string filename(basename);
77 filename.append(".ppm");
78 std::ofstream file(filename.c_str(), std::ios::binary);
79 if (!file.is_open()) {
80 ALOGE("Unable to open file: %s", filename.c_str());
81 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
82 "surfaceflinger to write debug images");
83 return;
84 }
85
86 file << "P6\n";
87 file << width << "\n";
88 file << height << "\n";
89 file << 255 << "\n";
90
91 auto ptr = reinterpret_cast<char*>(pixels.data());
92 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
93 for (int y = height - 1; y >= 0; y--) {
94 char* data = ptr + y * width * sizeof(uint32_t);
95
96 for (GLuint x = 0; x < width; x++) {
97 // Only copy R, G and B components
98 outPtr[0] = data[0];
99 outPtr[1] = data[1];
100 outPtr[2] = data[2];
101 data += sizeof(uint32_t);
102 outPtr += 3;
103 }
104 }
105 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
106}
107
Mathias Agopian3f844832013-08-07 21:24:32 -0700108namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700109namespace renderengine {
110namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700111
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700112using ui::Dataspace;
113
Peiyong Linf11f39b2018-09-05 14:37:41 -0700114static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
115 EGLint wanted, EGLConfig* outConfig) {
116 EGLint numConfigs = -1, n = 0;
117 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700118 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
119 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
120 configs.resize(n);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700121
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700122 if (!configs.empty()) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700123 if (attribute != EGL_NONE) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700124 for (EGLConfig config : configs) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700125 EGLint value = 0;
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700126 eglGetConfigAttrib(dpy, config, attribute, &value);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700127 if (wanted == value) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700128 *outConfig = config;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700129 return NO_ERROR;
130 }
131 }
132 } else {
133 // just pick the first one
134 *outConfig = configs[0];
Peiyong Linf11f39b2018-09-05 14:37:41 -0700135 return NO_ERROR;
136 }
137 }
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700138
Peiyong Linf11f39b2018-09-05 14:37:41 -0700139 return NAME_NOT_FOUND;
140}
141
142class EGLAttributeVector {
143 struct Attribute;
144 class Adder;
145 friend class Adder;
146 KeyedVector<Attribute, EGLint> mList;
147 struct Attribute {
148 Attribute() : v(0){};
149 explicit Attribute(EGLint v) : v(v) {}
150 EGLint v;
151 bool operator<(const Attribute& other) const {
152 // this places EGL_NONE at the end
153 EGLint lhs(v);
154 EGLint rhs(other.v);
155 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
156 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
157 return lhs < rhs;
158 }
159 };
160 class Adder {
161 friend class EGLAttributeVector;
162 EGLAttributeVector& v;
163 EGLint attribute;
164 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
165
166 public:
167 void operator=(EGLint value) {
168 if (attribute != EGL_NONE) {
169 v.mList.add(Attribute(attribute), value);
170 }
171 }
172 operator EGLint() const { return v.mList[attribute]; }
173 };
174
175public:
176 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
177 void remove(EGLint attribute) {
178 if (attribute != EGL_NONE) {
179 mList.removeItem(Attribute(attribute));
180 }
181 }
182 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
183 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
184 // cast-operator to (EGLint const*)
185 operator EGLint const*() const { return &mList.keyAt(0).v; }
186};
187
188static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
189 EGLConfig* config) {
190 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
191 // it is to be used with WIFI displays
192 status_t err;
193 EGLint wantedAttribute;
194 EGLint wantedAttributeValue;
195
196 EGLAttributeVector attribs;
197 if (renderableType) {
198 attribs[EGL_RENDERABLE_TYPE] = renderableType;
199 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
200 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
201 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
202 attribs[EGL_RED_SIZE] = 8;
203 attribs[EGL_GREEN_SIZE] = 8;
204 attribs[EGL_BLUE_SIZE] = 8;
205 attribs[EGL_ALPHA_SIZE] = 8;
206 wantedAttribute = EGL_NONE;
207 wantedAttributeValue = EGL_NONE;
208 } else {
209 // if no renderable type specified, fallback to a simplified query
210 wantedAttribute = EGL_NATIVE_VISUAL_ID;
211 wantedAttributeValue = format;
212 }
213
214 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
215 if (err == NO_ERROR) {
216 EGLint caveat;
217 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
218 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
219 }
220
221 return err;
222}
223
224std::unique_ptr<GLES20RenderEngine> GLES20RenderEngine::create(int hwcFormat,
225 uint32_t featureFlags) {
226 // initialize EGL for the default display
227 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
228 if (!eglInitialize(display, nullptr, nullptr)) {
229 LOG_ALWAYS_FATAL("failed to initialize EGL");
230 }
231
232 GLExtensions& extensions = GLExtensions::getInstance();
233 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
234 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
235
236 // The code assumes that ES2 or later is available if this extension is
237 // supported.
238 EGLConfig config = EGL_NO_CONFIG;
239 if (!extensions.hasNoConfigContext()) {
240 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
241 }
242
243 EGLint renderableType = 0;
244 if (config == EGL_NO_CONFIG) {
245 renderableType = EGL_OPENGL_ES2_BIT;
246 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
247 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
248 }
249 EGLint contextClientVersion = 0;
250 if (renderableType & EGL_OPENGL_ES2_BIT) {
251 contextClientVersion = 2;
252 } else if (renderableType & EGL_OPENGL_ES_BIT) {
253 contextClientVersion = 1;
254 } else {
255 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
256 }
257
258 std::vector<EGLint> contextAttributes;
259 contextAttributes.reserve(6);
260 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
261 contextAttributes.push_back(contextClientVersion);
262 bool useContextPriority = extensions.hasContextPriority() &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700263 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700264 if (useContextPriority) {
265 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
266 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
267 }
268 contextAttributes.push_back(EGL_NONE);
269
270 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
271
272 // if can't create a GL context, we can only abort.
273 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
274
275 // now figure out what version of GL did we actually get
276 // NOTE: a dummy surface is not needed if KHR_create_context is supported
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800277 // TODO(alecmouri): don't create this surface if EGL_KHR_surfaceless_context
278 // is supported.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700279
280 EGLConfig dummyConfig = config;
281 if (dummyConfig == EGL_NO_CONFIG) {
282 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
283 }
284 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
285 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
286 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
287 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
288 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
289
290 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
291 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
292
293 GlesVersion version = parseGlesVersion(extensions.getVersion());
294
295 // initialize the renderer while GL is current
296
297 std::unique_ptr<GLES20RenderEngine> engine;
298 switch (version) {
299 case GLES_VERSION_1_0:
300 case GLES_VERSION_1_1:
301 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
302 break;
303 case GLES_VERSION_2_0:
304 case GLES_VERSION_3_0:
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800305 engine = std::make_unique<GLES20RenderEngine>(featureFlags, display, config, ctxt,
306 dummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700307 break;
308 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700309
310 ALOGI("OpenGL ES informations:");
311 ALOGI("vendor : %s", extensions.getVendor());
312 ALOGI("renderer : %s", extensions.getRenderer());
313 ALOGI("version : %s", extensions.getVersion());
314 ALOGI("extensions: %s", extensions.getExtensions());
315 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
316 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
317
Peiyong Linf11f39b2018-09-05 14:37:41 -0700318 return engine;
319}
320
321EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
322 status_t err;
323 EGLConfig config;
324
325 // First try to get an ES2 config
326 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
327 if (err != NO_ERROR) {
328 // If ES2 fails, try ES1
329 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
330 if (err != NO_ERROR) {
331 // still didn't work, probably because we're on the emulator...
332 // try a simplified query
333 ALOGW("no suitable EGLConfig found, trying a simpler query");
334 err = selectEGLConfig(display, format, 0, &config);
335 if (err != NO_ERROR) {
336 // this EGL is too lame for android
337 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
338 }
339 }
340 }
341
342 if (logConfig) {
343 // print some debugging info
344 EGLint r, g, b, a;
345 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
346 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
347 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
348 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
349 ALOGI("EGL information:");
350 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
351 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
352 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
353 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
354 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
355 }
356
357 return config;
358}
359
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800360GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
361 EGLContext ctxt, EGLSurface dummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700362 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800363 mEGLDisplay(display),
364 mEGLConfig(config),
365 mEGLContext(ctxt),
366 mDummySurface(dummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700367 mVpWidth(0),
368 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700369 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700370 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
371 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
372
373 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
374 glPixelStorei(GL_PACK_ALIGNMENT, 4);
375
Chia-I Wub027f802017-11-29 14:00:52 -0800376 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700377 glGenTextures(1, &mProtectedTexName);
378 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800383 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 -0700384
Chia-I Wub027f802017-11-29 14:00:52 -0800385 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600386
Peiyong Lin13effd12018-07-24 17:01:47 -0700387 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800388 const ColorSpace srgb(ColorSpace::sRGB());
389 const ColorSpace displayP3(ColorSpace::DisplayP3());
390 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700391
392 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700393 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
394 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
395 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700396 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700397 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
398 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800399
400 // Compute sRGB to Display P3 and BT2020 transform matrix.
401 // NOTE: For now, we are limiting output wide color space support to
402 // Display-P3 and BT2020 only.
403 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
404 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
405
406 // Compute Display P3 to sRGB and BT2020 transform matrix.
407 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
408 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
409
410 // Compute BT2020 to sRGB and Display P3 transform matrix
411 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
412 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600413 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700414}
415
Peiyong Linf11f39b2018-09-05 14:37:41 -0700416GLES20RenderEngine::~GLES20RenderEngine() {
417 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
418 eglTerminate(mEGLDisplay);
419}
Mathias Agopian3f844832013-08-07 21:24:32 -0700420
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700421std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() {
422 return std::make_unique<GLFramebuffer>(*this);
423}
424
Peiyong Linf1bada92018-08-29 09:39:31 -0700425std::unique_ptr<Image> GLES20RenderEngine::createImage() {
426 return std::make_unique<GLImage>(*this);
427}
428
429void GLES20RenderEngine::primeCache() const {
430 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
431}
432
433bool GLES20RenderEngine::isCurrent() const {
434 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
435}
436
Peiyong Lin60bedb52018-09-05 10:47:31 -0700437base::unique_fd GLES20RenderEngine::flush() {
438 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
439 return base::unique_fd();
440 }
441
442 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
443 if (sync == EGL_NO_SYNC_KHR) {
444 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
445 return base::unique_fd();
446 }
447
448 // native fence fd will not be populated until flush() is done.
449 glFlush();
450
451 // get the fence fd
452 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
453 eglDestroySyncKHR(mEGLDisplay, sync);
454 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
455 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
456 }
457
458 return fenceFd;
459}
460
461bool GLES20RenderEngine::finish() {
462 if (!GLExtensions::getInstance().hasFenceSync()) {
463 ALOGW("no synchronization support");
464 return false;
465 }
466
467 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
468 if (sync == EGL_NO_SYNC_KHR) {
469 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
470 return false;
471 }
472
473 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
474 2000000000 /*2 sec*/);
475 EGLint error = eglGetError();
476 eglDestroySyncKHR(mEGLDisplay, sync);
477 if (result != EGL_CONDITION_SATISFIED_KHR) {
478 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
479 ALOGW("fence wait timed out");
480 } else {
481 ALOGW("error waiting on EGL fence: %#x", error);
482 }
483 return false;
484 }
485
486 return true;
487}
488
489bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) {
490 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
491 !GLExtensions::getInstance().hasWaitSync()) {
492 return false;
493 }
494
495 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
496 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
497 if (sync == EGL_NO_SYNC_KHR) {
498 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
499 return false;
500 }
501
502 // fenceFd is now owned by EGLSync
503 (void)fenceFd.release();
504
505 // XXX: The spec draft is inconsistent as to whether this should return an
506 // EGLint or void. Ignore the return value for now, as it's not strictly
507 // needed.
508 eglWaitSyncKHR(mEGLDisplay, sync, 0);
509 EGLint error = eglGetError();
510 eglDestroySyncKHR(mEGLDisplay, sync);
511 if (error != EGL_SUCCESS) {
512 ALOGE("failed to wait for EGL native fence sync: %#x", error);
513 return false;
514 }
515
516 return true;
517}
518
Peiyong Linf11f39b2018-09-05 14:37:41 -0700519void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
520 glClearColor(red, green, blue, alpha);
521 glClear(GL_COLOR_BUFFER_BIT);
522}
523
Chia-I Wu28e3a252018-09-07 12:05:02 -0700524void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green,
525 float blue, float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700526 size_t c;
527 Rect const* r = region.getArray(&c);
528 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
529 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
530 for (size_t i = 0; i < c; i++, r++) {
531 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700532 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700533 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700534 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700535 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700536 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700537 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700538 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700539 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700540 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700541 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700542 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700543 }
544 setupFillWithColor(red, green, blue, alpha);
545 drawMesh(mesh);
546}
547
Alec Mouri05483a02018-09-10 21:03:42 +0000548void GLES20RenderEngine::setScissor(const Rect& region) {
549 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000550 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000551 int32_t glBottom = canvasHeight - region.bottom;
552
553 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700554 glEnable(GL_SCISSOR_TEST);
555}
556
557void GLES20RenderEngine::disableScissor() {
558 glDisable(GL_SCISSOR_TEST);
559}
560
561void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) {
562 glGenTextures(count, names);
563}
564
565void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
566 glDeleteTextures(count, names);
567}
568
Peiyong Lin46080ef2018-10-26 18:43:14 -0700569void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700570 const GLImage& glImage = static_cast<const GLImage&>(image);
571 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
572
573 glBindTexture(target, texName);
574 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700575 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700576 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700577}
578
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700579status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
580 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
581 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
582 uint32_t textureName = glFramebuffer->getTextureName();
583 uint32_t framebufferName = glFramebuffer->getFramebufferName();
584
585 // Bind the texture and turn our EGLImage into a texture
586 glBindTexture(GL_TEXTURE_2D, textureName);
587 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
588
589 // Bind the Framebuffer to render into
590 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700591 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700592
Alec Mouri05483a02018-09-10 21:03:42 +0000593 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700594
595 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
596
Peiyong Lin46080ef2018-10-26 18:43:14 -0700597 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
598 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700599
600 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
601}
602
603void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000604 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700605
606 // back to main framebuffer
607 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700608}
609
Peiyong Lin60bedb52018-09-05 10:47:31 -0700610void GLES20RenderEngine::checkErrors() const {
611 do {
612 // there could be more than one error flag
613 GLenum error = glGetError();
614 if (error == GL_NO_ERROR) break;
615 ALOGE("GL error 0x%04x", int(error));
616 } while (true);
617}
618
Alec Mouri6e57f682018-09-29 20:45:08 -0700619status_t GLES20RenderEngine::drawLayers(const DisplaySettings& /*settings*/,
620 const std::vector<LayerSettings>& /*layers*/,
621 ANativeWindowBuffer* const /*buffer*/,
622 base::unique_fd* /*displayFence*/) const {
623 return NO_ERROR;
624}
625
Chia-I Wub027f802017-11-29 14:00:52 -0800626void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
Peiyong Linefefaac2018-08-17 12:27:51 -0700627 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800628 int32_t l = sourceCrop.left;
629 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700630 int32_t b = sourceCrop.bottom;
631 int32_t t = sourceCrop.top;
Alec Mouri7e593912018-11-17 04:57:33 +0000632 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700633 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700634
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700635 // Apply custom rotation to the projection.
636 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
637 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700638 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700639 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700640 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800641 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700642 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700643 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800644 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700645 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700646 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800647 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700648 break;
649 default:
650 break;
651 }
652
Mathias Agopian3f844832013-08-07 21:24:32 -0700653 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700654 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700655 mVpWidth = vpw;
656 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700657}
658
Chia-I Wub027f802017-11-29 14:00:52 -0800659void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
660 bool disableTexture, const half4& color) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700661 mState.isPremultipliedAlpha = premultipliedAlpha;
662 mState.isOpaque = opaque;
663 mState.color = color;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664
chaviw13fdc492017-06-27 12:40:18 -0700665 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700666 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700667 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000668
chaviw13fdc492017-06-27 12:40:18 -0700669 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700670 glEnable(GL_BLEND);
671 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
672 } else {
673 glDisable(GL_BLEND);
674 }
675}
676
Chia-I Wu131d3762018-01-11 14:35:27 -0800677void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700678 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800679}
680
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700681void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800682 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600683}
684
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700685void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800686 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600687}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600688
Peiyong Linfb069302018-04-25 14:34:31 -0700689void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700690 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700691}
692
Mathias Agopian49457ac2013-08-14 18:20:17 -0700693void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
694 GLuint target = texture.getTextureTarget();
695 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700696 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700697 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700698 filter = GL_LINEAR;
699 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700700 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
701 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
702 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
703 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700704
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700705 mState.texture = texture;
706 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700707}
708
709void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700710 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700711 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
712 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700713 mState.texture = texture;
714 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700715}
716
Peiyong Lind3788632018-09-18 16:01:31 -0700717void GLES20RenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700718 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700719}
720
Mathias Agopian3f844832013-08-07 21:24:32 -0700721void GLES20RenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700722 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700723}
724
725void GLES20RenderEngine::disableBlending() {
726 glDisable(GL_BLEND);
727}
728
Mathias Agopian19733a32013-08-28 18:13:56 -0700729void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700730 mState.isPremultipliedAlpha = true;
731 mState.isOpaque = false;
732 mState.color = half4(r, g, b, a);
733 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700734 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700735}
736
737void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700738 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700739 if (mesh.getTexCoordsSize()) {
740 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800741 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
742 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700743 }
744
Chia-I Wub027f802017-11-29 14:00:52 -0800745 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
746 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700747
Peiyong Lina296b0c2018-04-30 16:55:29 -0700748 // By default, DISPLAY_P3 is the only supported wide color output. However,
749 // when HDR content is present, hardware composer may be able to handle
750 // BT2020 data space, in that case, the output data space is set to be
751 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
752 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700753 if (mUseColorManagement) {
754 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700755 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
756 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700757 Dataspace outputStandard =
758 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
759 Dataspace outputTransfer =
760 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700761 bool needsXYZConversion = needsXYZTransformMatrix();
762
Valerie Haueb8e0762018-11-06 10:10:42 -0800763 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
764 // STANDARD_BT2020, it will be treated as STANDARD_BT709
765 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
766 inputStandard != Dataspace::STANDARD_BT2020) {
767 inputStandard = Dataspace::STANDARD_BT709;
768 }
769
Peiyong Lina296b0c2018-04-30 16:55:29 -0700770 if (needsXYZConversion) {
771 // The supported input color spaces are standard RGB, Display P3 and BT2020.
772 switch (inputStandard) {
773 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700774 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700775 break;
776 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700777 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700778 break;
779 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700780 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700781 break;
782 }
783
Peiyong Lin9b03c732018-05-17 10:14:02 -0700784 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700785 switch (outputStandard) {
786 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700787 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700788 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700789 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700790 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700791 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700792 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700793 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700794 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700795 }
796 } else if (inputStandard != outputStandard) {
797 // At this point, the input data space and output data space could be both
798 // HDR data spaces, but they match each other, we do nothing in this case.
799 // In addition to the case above, the input data space could be
800 // - scRGB linear
801 // - scRGB non-linear
802 // - sRGB
803 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800804 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700805 // The output data spaces could be
806 // - sRGB
807 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800808 // - BT2020
809 switch (outputStandard) {
810 case Dataspace::STANDARD_BT2020:
811 if (inputStandard == Dataspace::STANDARD_BT709) {
812 managedState.outputTransformMatrix = mSrgbToBt2020;
813 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
814 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
815 }
816 break;
817 case Dataspace::STANDARD_DCI_P3:
818 if (inputStandard == Dataspace::STANDARD_BT709) {
819 managedState.outputTransformMatrix = mSrgbToDisplayP3;
820 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
821 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
822 }
823 break;
824 default:
825 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
826 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
827 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
828 managedState.outputTransformMatrix = mBt2020ToSrgb;
829 }
830 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700831 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600832 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700833
834 // we need to convert the RGB value to linear space and convert it back when:
835 // - there is a color matrix that is not an identity matrix, or
836 // - there is an output transform matrix that is not an identity matrix, or
837 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700838 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800839 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700840 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700841 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700842 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700843 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700844 }
845
Peiyong Lin13effd12018-07-24 17:01:47 -0700846 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600847
848 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
849
850 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700851 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600852 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700853 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600854 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
855 }
856 } else {
857 ProgramCache::getInstance().useProgram(mState);
858
859 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
860 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700861
862 if (mesh.getTexCoordsSize()) {
863 glDisableVertexAttribArray(Program::texCoords);
864 }
865}
866
Peiyong Linf1bada92018-08-29 09:39:31 -0700867size_t GLES20RenderEngine::getMaxTextureSize() const {
868 return mMaxTextureSize;
869}
870
871size_t GLES20RenderEngine::getMaxViewportDims() const {
872 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
873}
874
Mathias Agopian3f844832013-08-07 21:24:32 -0700875void GLES20RenderEngine::dump(String8& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700876 const GLExtensions& extensions = GLExtensions::getInstance();
877
878 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
879 result.appendFormat("%s\n", extensions.getEGLExtensions());
880
881 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
882 extensions.getVersion());
883 result.appendFormat("%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700884
885 result.appendFormat("RenderEngine program cache size: %zu\n",
886 ProgramCache::getInstance().getSize());
887
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800888 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700889 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
890 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700891}
892
Peiyong Linf11f39b2018-09-05 14:37:41 -0700893GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) {
894 int major, minor;
895 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
896 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
897 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
898 return GLES_VERSION_1_0;
899 }
900 }
901
902 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
903 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
904 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
905 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
906
907 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
908 return GLES_VERSION_1_0;
909}
910
Peiyong Lina296b0c2018-04-30 16:55:29 -0700911bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
912 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
913 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
914 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700915 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700916}
917
918// For convenience, we want to convert the input color space to XYZ color space first,
919// and then convert from XYZ color space to output color space when
920// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
921// HDR content will be tone-mapped to SDR; Or,
922// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
923// HLG content to PQ content.
924// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
925// input data space or output data space is HDR data space, and the input transfer function
926// doesn't match the output transfer function, we would enable an intermediate transfrom to
927// XYZ color space.
928bool GLES20RenderEngine::needsXYZTransformMatrix() const {
929 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
930 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
931 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700932 const Dataspace outputTransfer =
933 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700934
935 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
936}
937
Peiyong Lin46080ef2018-10-26 18:43:14 -0700938} // namespace gl
939} // namespace renderengine
940} // namespace android