blob: e244a83c098de59a837350cc676f6b51353fa244 [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"
Alec Mourif1d19c72018-11-15 00:00:50 +000044#include "GLSurface.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070045#include "Program.h"
46#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070047
Peiyong Linf11f39b2018-09-05 14:37:41 -070048extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
49
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060050bool checkGlError(const char* op, int lineNumber) {
51 bool errorFound = false;
52 GLint error = glGetError();
53 while (error != GL_NO_ERROR) {
54 errorFound = true;
55 error = glGetError();
56 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
57 }
58 return errorFound;
59}
60
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060061static constexpr bool outputDebugPPMs = false;
62
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060063void writePPM(const char* basename, GLuint width, GLuint height) {
64 ALOGV("writePPM #%s: %d x %d", basename, width, height);
65
66 std::vector<GLubyte> pixels(width * height * 4);
67 std::vector<GLubyte> outBuffer(width * height * 3);
68
69 // TODO(courtneygo): We can now have float formats, need
70 // to remove this code or update to support.
71 // Make returned pixels fit in uint32_t, one byte per component
72 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
73 if (checkGlError(__FUNCTION__, __LINE__)) {
74 return;
75 }
76
77 std::string filename(basename);
78 filename.append(".ppm");
79 std::ofstream file(filename.c_str(), std::ios::binary);
80 if (!file.is_open()) {
81 ALOGE("Unable to open file: %s", filename.c_str());
82 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
83 "surfaceflinger to write debug images");
84 return;
85 }
86
87 file << "P6\n";
88 file << width << "\n";
89 file << height << "\n";
90 file << 255 << "\n";
91
92 auto ptr = reinterpret_cast<char*>(pixels.data());
93 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
94 for (int y = height - 1; y >= 0; y--) {
95 char* data = ptr + y * width * sizeof(uint32_t);
96
97 for (GLuint x = 0; x < width; x++) {
98 // Only copy R, G and B components
99 outPtr[0] = data[0];
100 outPtr[1] = data[1];
101 outPtr[2] = data[2];
102 data += sizeof(uint32_t);
103 outPtr += 3;
104 }
105 }
106 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
107}
108
Mathias Agopian3f844832013-08-07 21:24:32 -0700109namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700110namespace renderengine {
111namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700112
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700113using ui::Dataspace;
114
Peiyong Linf11f39b2018-09-05 14:37:41 -0700115static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
116 EGLint wanted, EGLConfig* outConfig) {
117 EGLint numConfigs = -1, n = 0;
118 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700119 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
120 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
121 configs.resize(n);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700122
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700123 if (!configs.empty()) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700124 if (attribute != EGL_NONE) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700125 for (EGLConfig config : configs) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700126 EGLint value = 0;
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700127 eglGetConfigAttrib(dpy, config, attribute, &value);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700128 if (wanted == value) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700129 *outConfig = config;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700130 return NO_ERROR;
131 }
132 }
133 } else {
134 // just pick the first one
135 *outConfig = configs[0];
Peiyong Linf11f39b2018-09-05 14:37:41 -0700136 return NO_ERROR;
137 }
138 }
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700139
Peiyong Linf11f39b2018-09-05 14:37:41 -0700140 return NAME_NOT_FOUND;
141}
142
143class EGLAttributeVector {
144 struct Attribute;
145 class Adder;
146 friend class Adder;
147 KeyedVector<Attribute, EGLint> mList;
148 struct Attribute {
149 Attribute() : v(0){};
150 explicit Attribute(EGLint v) : v(v) {}
151 EGLint v;
152 bool operator<(const Attribute& other) const {
153 // this places EGL_NONE at the end
154 EGLint lhs(v);
155 EGLint rhs(other.v);
156 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
157 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
158 return lhs < rhs;
159 }
160 };
161 class Adder {
162 friend class EGLAttributeVector;
163 EGLAttributeVector& v;
164 EGLint attribute;
165 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
166
167 public:
168 void operator=(EGLint value) {
169 if (attribute != EGL_NONE) {
170 v.mList.add(Attribute(attribute), value);
171 }
172 }
173 operator EGLint() const { return v.mList[attribute]; }
174 };
175
176public:
177 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
178 void remove(EGLint attribute) {
179 if (attribute != EGL_NONE) {
180 mList.removeItem(Attribute(attribute));
181 }
182 }
183 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
184 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
185 // cast-operator to (EGLint const*)
186 operator EGLint const*() const { return &mList.keyAt(0).v; }
187};
188
189static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
190 EGLConfig* config) {
191 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
192 // it is to be used with WIFI displays
193 status_t err;
194 EGLint wantedAttribute;
195 EGLint wantedAttributeValue;
196
197 EGLAttributeVector attribs;
198 if (renderableType) {
199 attribs[EGL_RENDERABLE_TYPE] = renderableType;
200 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
201 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
202 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
203 attribs[EGL_RED_SIZE] = 8;
204 attribs[EGL_GREEN_SIZE] = 8;
205 attribs[EGL_BLUE_SIZE] = 8;
206 attribs[EGL_ALPHA_SIZE] = 8;
207 wantedAttribute = EGL_NONE;
208 wantedAttributeValue = EGL_NONE;
209 } else {
210 // if no renderable type specified, fallback to a simplified query
211 wantedAttribute = EGL_NATIVE_VISUAL_ID;
212 wantedAttributeValue = format;
213 }
214
215 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
216 if (err == NO_ERROR) {
217 EGLint caveat;
218 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
219 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
220 }
221
222 return err;
223}
224
225std::unique_ptr<GLES20RenderEngine> GLES20RenderEngine::create(int hwcFormat,
226 uint32_t featureFlags) {
227 // initialize EGL for the default display
228 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
229 if (!eglInitialize(display, nullptr, nullptr)) {
230 LOG_ALWAYS_FATAL("failed to initialize EGL");
231 }
232
233 GLExtensions& extensions = GLExtensions::getInstance();
234 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
235 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
236
237 // The code assumes that ES2 or later is available if this extension is
238 // supported.
239 EGLConfig config = EGL_NO_CONFIG;
240 if (!extensions.hasNoConfigContext()) {
241 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
242 }
243
244 EGLint renderableType = 0;
245 if (config == EGL_NO_CONFIG) {
246 renderableType = EGL_OPENGL_ES2_BIT;
247 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
248 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
249 }
250 EGLint contextClientVersion = 0;
251 if (renderableType & EGL_OPENGL_ES2_BIT) {
252 contextClientVersion = 2;
253 } else if (renderableType & EGL_OPENGL_ES_BIT) {
254 contextClientVersion = 1;
255 } else {
256 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
257 }
258
259 std::vector<EGLint> contextAttributes;
260 contextAttributes.reserve(6);
261 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
262 contextAttributes.push_back(contextClientVersion);
263 bool useContextPriority = extensions.hasContextPriority() &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700264 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700265 if (useContextPriority) {
266 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
267 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
268 }
269 contextAttributes.push_back(EGL_NONE);
270
271 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
272
273 // if can't create a GL context, we can only abort.
274 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
275
276 // now figure out what version of GL did we actually get
277 // NOTE: a dummy surface is not needed if KHR_create_context is supported
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800278 // TODO(alecmouri): don't create this surface if EGL_KHR_surfaceless_context
279 // is supported.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700280
281 EGLConfig dummyConfig = config;
282 if (dummyConfig == EGL_NO_CONFIG) {
283 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
284 }
285 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
286 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
287 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
288 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
289 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
290
291 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
292 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
293
294 GlesVersion version = parseGlesVersion(extensions.getVersion());
295
296 // initialize the renderer while GL is current
297
298 std::unique_ptr<GLES20RenderEngine> engine;
299 switch (version) {
300 case GLES_VERSION_1_0:
301 case GLES_VERSION_1_1:
302 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
303 break;
304 case GLES_VERSION_2_0:
305 case GLES_VERSION_3_0:
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800306 engine = std::make_unique<GLES20RenderEngine>(featureFlags, display, config, ctxt,
307 dummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700308 break;
309 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700310
311 ALOGI("OpenGL ES informations:");
312 ALOGI("vendor : %s", extensions.getVendor());
313 ALOGI("renderer : %s", extensions.getRenderer());
314 ALOGI("version : %s", extensions.getVersion());
315 ALOGI("extensions: %s", extensions.getExtensions());
316 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
317 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
318
Peiyong Linf11f39b2018-09-05 14:37:41 -0700319 return engine;
320}
321
322EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
323 status_t err;
324 EGLConfig config;
325
326 // First try to get an ES2 config
327 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
328 if (err != NO_ERROR) {
329 // If ES2 fails, try ES1
330 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
331 if (err != NO_ERROR) {
332 // still didn't work, probably because we're on the emulator...
333 // try a simplified query
334 ALOGW("no suitable EGLConfig found, trying a simpler query");
335 err = selectEGLConfig(display, format, 0, &config);
336 if (err != NO_ERROR) {
337 // this EGL is too lame for android
338 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
339 }
340 }
341 }
342
343 if (logConfig) {
344 // print some debugging info
345 EGLint r, g, b, a;
346 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
347 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
348 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
349 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
350 ALOGI("EGL information:");
351 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
352 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
353 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
354 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
355 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
356 }
357
358 return config;
359}
360
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800361GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
362 EGLContext ctxt, EGLSurface dummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700363 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800364 mEGLDisplay(display),
365 mEGLConfig(config),
366 mEGLContext(ctxt),
367 mDummySurface(dummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700368 mVpWidth(0),
369 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700370 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700371 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
372 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
373
374 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
375 glPixelStorei(GL_PACK_ALIGNMENT, 4);
376
Chia-I Wub027f802017-11-29 14:00:52 -0800377 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700378 glGenTextures(1, &mProtectedTexName);
379 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800384 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 -0700385
Chia-I Wub027f802017-11-29 14:00:52 -0800386 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600387
Peiyong Lin13effd12018-07-24 17:01:47 -0700388 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800389 const ColorSpace srgb(ColorSpace::sRGB());
390 const ColorSpace displayP3(ColorSpace::DisplayP3());
391 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700392
393 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700394 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
395 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
396 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700397 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700398 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
399 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800400
401 // Compute sRGB to Display P3 and BT2020 transform matrix.
402 // NOTE: For now, we are limiting output wide color space support to
403 // Display-P3 and BT2020 only.
404 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
405 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
406
407 // Compute Display P3 to sRGB and BT2020 transform matrix.
408 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
409 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
410
411 // Compute BT2020 to sRGB and Display P3 transform matrix
412 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
413 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600414 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700415}
416
Peiyong Linf11f39b2018-09-05 14:37:41 -0700417GLES20RenderEngine::~GLES20RenderEngine() {
418 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
419 eglTerminate(mEGLDisplay);
420}
Mathias Agopian3f844832013-08-07 21:24:32 -0700421
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700422std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() {
423 return std::make_unique<GLFramebuffer>(*this);
424}
425
Alec Mourif1d19c72018-11-15 00:00:50 +0000426std::unique_ptr<Surface> GLES20RenderEngine::createSurface() {
427 return std::make_unique<GLSurface>(*this);
428}
429
Peiyong Linf1bada92018-08-29 09:39:31 -0700430std::unique_ptr<Image> GLES20RenderEngine::createImage() {
431 return std::make_unique<GLImage>(*this);
432}
433
434void GLES20RenderEngine::primeCache() const {
435 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
436}
437
438bool GLES20RenderEngine::isCurrent() const {
439 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
440}
441
Alec Mourif1d19c72018-11-15 00:00:50 +0000442bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) {
443 // Surface is an abstract interface. GLES20RenderEngine only ever
444 // creates GLSurface's, so it is safe to just cast to the actual
445 // type.
446 bool success = true;
447 const GLSurface& glSurface = static_cast<const GLSurface&>(surface);
448 EGLSurface eglSurface = glSurface.getEGLSurface();
449 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
450 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
451 if (success && glSurface.getAsync()) {
452 eglSwapInterval(mEGLDisplay, 0);
453 }
454 if (success) {
455 mSurfaceHeight = glSurface.getHeight();
456 }
457 }
458
459 return success;
460}
461
462void GLES20RenderEngine::resetCurrentSurface() {
463 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
464 mSurfaceHeight = 0;
465}
466
Peiyong Lin60bedb52018-09-05 10:47:31 -0700467base::unique_fd GLES20RenderEngine::flush() {
468 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
469 return base::unique_fd();
470 }
471
472 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
473 if (sync == EGL_NO_SYNC_KHR) {
474 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
475 return base::unique_fd();
476 }
477
478 // native fence fd will not be populated until flush() is done.
479 glFlush();
480
481 // get the fence fd
482 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
483 eglDestroySyncKHR(mEGLDisplay, sync);
484 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
485 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
486 }
487
488 return fenceFd;
489}
490
491bool GLES20RenderEngine::finish() {
492 if (!GLExtensions::getInstance().hasFenceSync()) {
493 ALOGW("no synchronization support");
494 return false;
495 }
496
497 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
498 if (sync == EGL_NO_SYNC_KHR) {
499 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
500 return false;
501 }
502
503 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
504 2000000000 /*2 sec*/);
505 EGLint error = eglGetError();
506 eglDestroySyncKHR(mEGLDisplay, sync);
507 if (result != EGL_CONDITION_SATISFIED_KHR) {
508 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
509 ALOGW("fence wait timed out");
510 } else {
511 ALOGW("error waiting on EGL fence: %#x", error);
512 }
513 return false;
514 }
515
516 return true;
517}
518
519bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) {
520 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
521 !GLExtensions::getInstance().hasWaitSync()) {
522 return false;
523 }
524
525 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
526 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
527 if (sync == EGL_NO_SYNC_KHR) {
528 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
529 return false;
530 }
531
532 // fenceFd is now owned by EGLSync
533 (void)fenceFd.release();
534
535 // XXX: The spec draft is inconsistent as to whether this should return an
536 // EGLint or void. Ignore the return value for now, as it's not strictly
537 // needed.
538 eglWaitSyncKHR(mEGLDisplay, sync, 0);
539 EGLint error = eglGetError();
540 eglDestroySyncKHR(mEGLDisplay, sync);
541 if (error != EGL_SUCCESS) {
542 ALOGE("failed to wait for EGL native fence sync: %#x", error);
543 return false;
544 }
545
546 return true;
547}
548
Peiyong Linf11f39b2018-09-05 14:37:41 -0700549void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
550 glClearColor(red, green, blue, alpha);
551 glClear(GL_COLOR_BUFFER_BIT);
552}
553
Chia-I Wu28e3a252018-09-07 12:05:02 -0700554void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green,
555 float blue, float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700556 size_t c;
557 Rect const* r = region.getArray(&c);
558 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
559 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
560 for (size_t i = 0; i < c; i++, r++) {
561 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700562 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700563 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700564 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700565 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700566 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700567 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700568 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700569 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700570 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700571 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700572 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700573 }
574 setupFillWithColor(red, green, blue, alpha);
575 drawMesh(mesh);
576}
577
Alec Mouri05483a02018-09-10 21:03:42 +0000578void GLES20RenderEngine::setScissor(const Rect& region) {
579 // Invert y-coordinate to map to GL-space.
Alec Mourif1d19c72018-11-15 00:00:50 +0000580 int32_t canvasHeight = mRenderToFbo ? mFboHeight : mSurfaceHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000581 int32_t glBottom = canvasHeight - region.bottom;
582
583 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700584 glEnable(GL_SCISSOR_TEST);
585}
586
587void GLES20RenderEngine::disableScissor() {
588 glDisable(GL_SCISSOR_TEST);
589}
590
591void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) {
592 glGenTextures(count, names);
593}
594
595void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
596 glDeleteTextures(count, names);
597}
598
Peiyong Lin46080ef2018-10-26 18:43:14 -0700599void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700600 const GLImage& glImage = static_cast<const GLImage&>(image);
601 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
602
603 glBindTexture(target, texName);
604 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700605 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700606 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700607}
608
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700609status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
610 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
611 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
612 uint32_t textureName = glFramebuffer->getTextureName();
613 uint32_t framebufferName = glFramebuffer->getFramebufferName();
614
615 // Bind the texture and turn our EGLImage into a texture
616 glBindTexture(GL_TEXTURE_2D, textureName);
617 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
618
619 // Bind the Framebuffer to render into
620 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700621 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700622
Alec Mourif1d19c72018-11-15 00:00:50 +0000623 mRenderToFbo = true;
Alec Mouri05483a02018-09-10 21:03:42 +0000624 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700625
626 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
627
Peiyong Lin46080ef2018-10-26 18:43:14 -0700628 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
629 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700630
631 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
632}
633
634void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mourif1d19c72018-11-15 00:00:50 +0000635 mRenderToFbo = false;
Alec Mouri05483a02018-09-10 21:03:42 +0000636 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700637
638 // back to main framebuffer
639 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700640}
641
Peiyong Lin60bedb52018-09-05 10:47:31 -0700642void GLES20RenderEngine::checkErrors() const {
643 do {
644 // there could be more than one error flag
645 GLenum error = glGetError();
646 if (error == GL_NO_ERROR) break;
647 ALOGE("GL error 0x%04x", int(error));
648 } while (true);
649}
650
Alec Mouri6e57f682018-09-29 20:45:08 -0700651status_t GLES20RenderEngine::drawLayers(const DisplaySettings& /*settings*/,
652 const std::vector<LayerSettings>& /*layers*/,
653 ANativeWindowBuffer* const /*buffer*/,
654 base::unique_fd* /*displayFence*/) const {
655 return NO_ERROR;
656}
657
Chia-I Wub027f802017-11-29 14:00:52 -0800658void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
Peiyong Linefefaac2018-08-17 12:27:51 -0700659 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800660 int32_t l = sourceCrop.left;
661 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700662 int32_t b = sourceCrop.bottom;
663 int32_t t = sourceCrop.top;
Alec Mourif1d19c72018-11-15 00:00:50 +0000664 if (mRenderToFbo) {
665 std::swap(t, b);
666 }
Chia-I Wu1be50b52018-08-29 10:44:48 -0700667 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700668
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700669 // Apply custom rotation to the projection.
670 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
671 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700672 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700673 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700674 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800675 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700676 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700677 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800678 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700679 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700680 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800681 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700682 break;
683 default:
684 break;
685 }
686
Mathias Agopian3f844832013-08-07 21:24:32 -0700687 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700688 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700689 mVpWidth = vpw;
690 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700691}
692
Chia-I Wub027f802017-11-29 14:00:52 -0800693void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
694 bool disableTexture, const half4& color) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700695 mState.isPremultipliedAlpha = premultipliedAlpha;
696 mState.isOpaque = opaque;
697 mState.color = color;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698
chaviw13fdc492017-06-27 12:40:18 -0700699 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700700 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700701 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000702
chaviw13fdc492017-06-27 12:40:18 -0700703 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700704 glEnable(GL_BLEND);
705 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
706 } else {
707 glDisable(GL_BLEND);
708 }
709}
710
Chia-I Wu131d3762018-01-11 14:35:27 -0800711void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700712 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800713}
714
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700715void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800716 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600717}
718
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700719void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800720 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600721}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600722
Peiyong Linfb069302018-04-25 14:34:31 -0700723void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700724 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700725}
726
Mathias Agopian49457ac2013-08-14 18:20:17 -0700727void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
728 GLuint target = texture.getTextureTarget();
729 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700730 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700731 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700732 filter = GL_LINEAR;
733 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700734 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
735 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
736 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
737 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700738
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700739 mState.texture = texture;
740 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700741}
742
743void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700744 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700745 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
746 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700747 mState.texture = texture;
748 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700749}
750
Peiyong Lind3788632018-09-18 16:01:31 -0700751void GLES20RenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700752 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700753}
754
Mathias Agopian3f844832013-08-07 21:24:32 -0700755void GLES20RenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700756 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700757}
758
759void GLES20RenderEngine::disableBlending() {
760 glDisable(GL_BLEND);
761}
762
Mathias Agopian19733a32013-08-28 18:13:56 -0700763void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700764 mState.isPremultipliedAlpha = true;
765 mState.isOpaque = false;
766 mState.color = half4(r, g, b, a);
767 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700768 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700769}
770
771void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700772 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700773 if (mesh.getTexCoordsSize()) {
774 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800775 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
776 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700777 }
778
Chia-I Wub027f802017-11-29 14:00:52 -0800779 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
780 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700781
Peiyong Lina296b0c2018-04-30 16:55:29 -0700782 // By default, DISPLAY_P3 is the only supported wide color output. However,
783 // when HDR content is present, hardware composer may be able to handle
784 // BT2020 data space, in that case, the output data space is set to be
785 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
786 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700787 if (mUseColorManagement) {
788 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700789 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
790 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700791 Dataspace outputStandard =
792 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
793 Dataspace outputTransfer =
794 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700795 bool needsXYZConversion = needsXYZTransformMatrix();
796
Valerie Haueb8e0762018-11-06 10:10:42 -0800797 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
798 // STANDARD_BT2020, it will be treated as STANDARD_BT709
799 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
800 inputStandard != Dataspace::STANDARD_BT2020) {
801 inputStandard = Dataspace::STANDARD_BT709;
802 }
803
Peiyong Lina296b0c2018-04-30 16:55:29 -0700804 if (needsXYZConversion) {
805 // The supported input color spaces are standard RGB, Display P3 and BT2020.
806 switch (inputStandard) {
807 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700808 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700809 break;
810 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700811 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700812 break;
813 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700814 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700815 break;
816 }
817
Peiyong Lin9b03c732018-05-17 10:14:02 -0700818 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700819 switch (outputStandard) {
820 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700821 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700822 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700823 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700824 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700825 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700826 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700827 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700828 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700829 }
830 } else if (inputStandard != outputStandard) {
831 // At this point, the input data space and output data space could be both
832 // HDR data spaces, but they match each other, we do nothing in this case.
833 // In addition to the case above, the input data space could be
834 // - scRGB linear
835 // - scRGB non-linear
836 // - sRGB
837 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800838 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700839 // The output data spaces could be
840 // - sRGB
841 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800842 // - BT2020
843 switch (outputStandard) {
844 case Dataspace::STANDARD_BT2020:
845 if (inputStandard == Dataspace::STANDARD_BT709) {
846 managedState.outputTransformMatrix = mSrgbToBt2020;
847 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
848 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
849 }
850 break;
851 case Dataspace::STANDARD_DCI_P3:
852 if (inputStandard == Dataspace::STANDARD_BT709) {
853 managedState.outputTransformMatrix = mSrgbToDisplayP3;
854 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
855 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
856 }
857 break;
858 default:
859 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
860 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
861 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
862 managedState.outputTransformMatrix = mBt2020ToSrgb;
863 }
864 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700865 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600866 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700867
868 // we need to convert the RGB value to linear space and convert it back when:
869 // - there is a color matrix that is not an identity matrix, or
870 // - there is an output transform matrix that is not an identity matrix, or
871 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700872 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800873 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700874 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700875 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700876 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700877 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700878 }
879
Peiyong Lin13effd12018-07-24 17:01:47 -0700880 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600881
882 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
883
884 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700885 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600886 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700887 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600888 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
889 }
890 } else {
891 ProgramCache::getInstance().useProgram(mState);
892
893 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
894 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700895
896 if (mesh.getTexCoordsSize()) {
897 glDisableVertexAttribArray(Program::texCoords);
898 }
899}
900
Peiyong Linf1bada92018-08-29 09:39:31 -0700901size_t GLES20RenderEngine::getMaxTextureSize() const {
902 return mMaxTextureSize;
903}
904
905size_t GLES20RenderEngine::getMaxViewportDims() const {
906 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
907}
908
Mathias Agopian3f844832013-08-07 21:24:32 -0700909void GLES20RenderEngine::dump(String8& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700910 const GLExtensions& extensions = GLExtensions::getInstance();
911
912 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
913 result.appendFormat("%s\n", extensions.getEGLExtensions());
914
915 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
916 extensions.getVersion());
917 result.appendFormat("%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700918
919 result.appendFormat("RenderEngine program cache size: %zu\n",
920 ProgramCache::getInstance().getSize());
921
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800922 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700923 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
924 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700925}
926
Peiyong Linf11f39b2018-09-05 14:37:41 -0700927GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) {
928 int major, minor;
929 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
930 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
931 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
932 return GLES_VERSION_1_0;
933 }
934 }
935
936 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
937 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
938 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
939 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
940
941 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
942 return GLES_VERSION_1_0;
943}
944
Peiyong Lina296b0c2018-04-30 16:55:29 -0700945bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
946 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
947 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
948 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700949 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700950}
951
952// For convenience, we want to convert the input color space to XYZ color space first,
953// and then convert from XYZ color space to output color space when
954// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
955// HDR content will be tone-mapped to SDR; Or,
956// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
957// HLG content to PQ content.
958// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
959// input data space or output data space is HDR data space, and the input transfer function
960// doesn't match the output transfer function, we would enable an intermediate transfrom to
961// XYZ color space.
962bool GLES20RenderEngine::needsXYZTransformMatrix() const {
963 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
964 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
965 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700966 const Dataspace outputTransfer =
967 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700968
969 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
970}
971
Peiyong Lin46080ef2018-10-26 18:43:14 -0700972} // namespace gl
973} // namespace renderengine
974} // namespace android