blob: b94cdca24237e8187ee47ae97b7895a678c806c5 [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"
44#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);
119 EGLConfig* const configs = new EGLConfig[numConfigs];
120 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
121
122 if (n) {
123 if (attribute != EGL_NONE) {
124 for (int i = 0; i < n; i++) {
125 EGLint value = 0;
126 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
127 if (wanted == value) {
128 *outConfig = configs[i];
129 delete[] configs;
130 return NO_ERROR;
131 }
132 }
133 } else {
134 // just pick the first one
135 *outConfig = configs[0];
136 delete[] configs;
137 return NO_ERROR;
138 }
139 }
140 delete[] configs;
141 return NAME_NOT_FOUND;
142}
143
144class EGLAttributeVector {
145 struct Attribute;
146 class Adder;
147 friend class Adder;
148 KeyedVector<Attribute, EGLint> mList;
149 struct Attribute {
150 Attribute() : v(0){};
151 explicit Attribute(EGLint v) : v(v) {}
152 EGLint v;
153 bool operator<(const Attribute& other) const {
154 // this places EGL_NONE at the end
155 EGLint lhs(v);
156 EGLint rhs(other.v);
157 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
158 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
159 return lhs < rhs;
160 }
161 };
162 class Adder {
163 friend class EGLAttributeVector;
164 EGLAttributeVector& v;
165 EGLint attribute;
166 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
167
168 public:
169 void operator=(EGLint value) {
170 if (attribute != EGL_NONE) {
171 v.mList.add(Attribute(attribute), value);
172 }
173 }
174 operator EGLint() const { return v.mList[attribute]; }
175 };
176
177public:
178 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
179 void remove(EGLint attribute) {
180 if (attribute != EGL_NONE) {
181 mList.removeItem(Attribute(attribute));
182 }
183 }
184 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
185 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
186 // cast-operator to (EGLint const*)
187 operator EGLint const*() const { return &mList.keyAt(0).v; }
188};
189
190static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
191 EGLConfig* config) {
192 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
193 // it is to be used with WIFI displays
194 status_t err;
195 EGLint wantedAttribute;
196 EGLint wantedAttributeValue;
197
198 EGLAttributeVector attribs;
199 if (renderableType) {
200 attribs[EGL_RENDERABLE_TYPE] = renderableType;
201 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
202 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
203 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
204 attribs[EGL_RED_SIZE] = 8;
205 attribs[EGL_GREEN_SIZE] = 8;
206 attribs[EGL_BLUE_SIZE] = 8;
207 attribs[EGL_ALPHA_SIZE] = 8;
208 wantedAttribute = EGL_NONE;
209 wantedAttributeValue = EGL_NONE;
210 } else {
211 // if no renderable type specified, fallback to a simplified query
212 wantedAttribute = EGL_NATIVE_VISUAL_ID;
213 wantedAttributeValue = format;
214 }
215
216 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
217 if (err == NO_ERROR) {
218 EGLint caveat;
219 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
220 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
221 }
222
223 return err;
224}
225
226std::unique_ptr<GLES20RenderEngine> GLES20RenderEngine::create(int hwcFormat,
227 uint32_t featureFlags) {
228 // initialize EGL for the default display
229 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
230 if (!eglInitialize(display, nullptr, nullptr)) {
231 LOG_ALWAYS_FATAL("failed to initialize EGL");
232 }
233
234 GLExtensions& extensions = GLExtensions::getInstance();
235 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
236 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
237
238 // The code assumes that ES2 or later is available if this extension is
239 // supported.
240 EGLConfig config = EGL_NO_CONFIG;
241 if (!extensions.hasNoConfigContext()) {
242 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
243 }
244
245 EGLint renderableType = 0;
246 if (config == EGL_NO_CONFIG) {
247 renderableType = EGL_OPENGL_ES2_BIT;
248 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
249 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
250 }
251 EGLint contextClientVersion = 0;
252 if (renderableType & EGL_OPENGL_ES2_BIT) {
253 contextClientVersion = 2;
254 } else if (renderableType & EGL_OPENGL_ES_BIT) {
255 contextClientVersion = 1;
256 } else {
257 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
258 }
259
260 std::vector<EGLint> contextAttributes;
261 contextAttributes.reserve(6);
262 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
263 contextAttributes.push_back(contextClientVersion);
264 bool useContextPriority = extensions.hasContextPriority() &&
265 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
266 if (useContextPriority) {
267 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
268 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
269 }
270 contextAttributes.push_back(EGL_NONE);
271
272 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
273
274 // if can't create a GL context, we can only abort.
275 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
276
277 // now figure out what version of GL did we actually get
278 // NOTE: a dummy surface is not needed if KHR_create_context is supported
279
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:
305 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
306 break;
307 }
308 engine->setEGLHandles(display, config, ctxt);
309
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
318 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
319 eglDestroySurface(display, dummy);
320
321 return engine;
322}
323
324EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
325 status_t err;
326 EGLConfig config;
327
328 // First try to get an ES2 config
329 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
330 if (err != NO_ERROR) {
331 // If ES2 fails, try ES1
332 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
333 if (err != NO_ERROR) {
334 // still didn't work, probably because we're on the emulator...
335 // try a simplified query
336 ALOGW("no suitable EGLConfig found, trying a simpler query");
337 err = selectEGLConfig(display, format, 0, &config);
338 if (err != NO_ERROR) {
339 // this EGL is too lame for android
340 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
341 }
342 }
343 }
344
345 if (logConfig) {
346 // print some debugging info
347 EGLint r, g, b, a;
348 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
349 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
350 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
351 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
352 ALOGI("EGL information:");
353 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
354 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
355 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
356 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
357 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
358 }
359
360 return config;
361}
362
Chia-I Wub027f802017-11-29 14:00:52 -0800363GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700364 : renderengine::impl::RenderEngine(featureFlags),
365 mEGLDisplay(EGL_NO_DISPLAY),
366 mEGLConfig(nullptr),
367 mEGLContext(EGL_NO_CONTEXT),
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) {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700389 ColorSpace srgb(ColorSpace::sRGB());
390 ColorSpace displayP3(ColorSpace::DisplayP3());
391 ColorSpace bt2020(ColorSpace::BT2020());
Chia-I Wu131d3762018-01-11 14:35:27 -0800392
Peiyong Lina296b0c2018-04-30 16:55:29 -0700393 // Compute sRGB to Display P3 transform matrix.
394 // NOTE: For now, we are limiting output wide color space support to
395 // Display-P3 only.
396 mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform());
397
398 // Compute Display P3 to sRGB transform matrix.
399 mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform());
400
401 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700402 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
403 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
404 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700405 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700406 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
407 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600408 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700409}
410
Peiyong Linf11f39b2018-09-05 14:37:41 -0700411GLES20RenderEngine::~GLES20RenderEngine() {
412 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
413 eglTerminate(mEGLDisplay);
414}
Mathias Agopian3f844832013-08-07 21:24:32 -0700415
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700416std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() {
417 return std::make_unique<GLFramebuffer>(*this);
418}
419
Peiyong Linf1bada92018-08-29 09:39:31 -0700420std::unique_ptr<Surface> GLES20RenderEngine::createSurface() {
421 return std::make_unique<GLSurface>(*this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700422}
423
Peiyong Linf1bada92018-08-29 09:39:31 -0700424std::unique_ptr<Image> GLES20RenderEngine::createImage() {
425 return std::make_unique<GLImage>(*this);
426}
427
428void GLES20RenderEngine::primeCache() const {
429 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
430}
431
432bool GLES20RenderEngine::isCurrent() const {
433 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
434}
435
436bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) {
437 // Surface is an abstract interface. GLES20RenderEngine only ever
438 // creates GLSurface's, so it is safe to just cast to the actual
439 // type.
440 bool success = true;
441 const GLSurface& glSurface = static_cast<const GLSurface&>(surface);
442 EGLSurface eglSurface = glSurface.getEGLSurface();
443 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
444 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
445 if (success && glSurface.getAsync()) {
446 eglSwapInterval(mEGLDisplay, 0);
447 }
Alec Mouri05483a02018-09-10 21:03:42 +0000448 if (success) {
449 mSurfaceHeight = glSurface.getHeight();
450 }
Peiyong Linf1bada92018-08-29 09:39:31 -0700451 }
Alec Mouri05483a02018-09-10 21:03:42 +0000452
Peiyong Linf1bada92018-08-29 09:39:31 -0700453 return success;
454}
455
456void GLES20RenderEngine::resetCurrentSurface() {
457 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Alec Mouri05483a02018-09-10 21:03:42 +0000458 mSurfaceHeight = 0;
Peiyong Linf1bada92018-08-29 09:39:31 -0700459}
460
Peiyong Lin60bedb52018-09-05 10:47:31 -0700461base::unique_fd GLES20RenderEngine::flush() {
462 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
463 return base::unique_fd();
464 }
465
466 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
467 if (sync == EGL_NO_SYNC_KHR) {
468 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
469 return base::unique_fd();
470 }
471
472 // native fence fd will not be populated until flush() is done.
473 glFlush();
474
475 // get the fence fd
476 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
477 eglDestroySyncKHR(mEGLDisplay, sync);
478 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
479 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
480 }
481
482 return fenceFd;
483}
484
485bool GLES20RenderEngine::finish() {
486 if (!GLExtensions::getInstance().hasFenceSync()) {
487 ALOGW("no synchronization support");
488 return false;
489 }
490
491 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
492 if (sync == EGL_NO_SYNC_KHR) {
493 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
494 return false;
495 }
496
497 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
498 2000000000 /*2 sec*/);
499 EGLint error = eglGetError();
500 eglDestroySyncKHR(mEGLDisplay, sync);
501 if (result != EGL_CONDITION_SATISFIED_KHR) {
502 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
503 ALOGW("fence wait timed out");
504 } else {
505 ALOGW("error waiting on EGL fence: %#x", error);
506 }
507 return false;
508 }
509
510 return true;
511}
512
513bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) {
514 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
515 !GLExtensions::getInstance().hasWaitSync()) {
516 return false;
517 }
518
519 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
520 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
521 if (sync == EGL_NO_SYNC_KHR) {
522 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
523 return false;
524 }
525
526 // fenceFd is now owned by EGLSync
527 (void)fenceFd.release();
528
529 // XXX: The spec draft is inconsistent as to whether this should return an
530 // EGLint or void. Ignore the return value for now, as it's not strictly
531 // needed.
532 eglWaitSyncKHR(mEGLDisplay, sync, 0);
533 EGLint error = eglGetError();
534 eglDestroySyncKHR(mEGLDisplay, sync);
535 if (error != EGL_SUCCESS) {
536 ALOGE("failed to wait for EGL native fence sync: %#x", error);
537 return false;
538 }
539
540 return true;
541}
542
Peiyong Linf11f39b2018-09-05 14:37:41 -0700543void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
544 glClearColor(red, green, blue, alpha);
545 glClear(GL_COLOR_BUFFER_BIT);
546}
547
Chia-I Wu28e3a252018-09-07 12:05:02 -0700548void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green,
549 float blue, float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700550 size_t c;
551 Rect const* r = region.getArray(&c);
552 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
553 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
554 for (size_t i = 0; i < c; i++, r++) {
555 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700556 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700557 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700558 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700559 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700560 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700561 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700562 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700563 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700564 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700565 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700566 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700567 }
568 setupFillWithColor(red, green, blue, alpha);
569 drawMesh(mesh);
570}
571
Alec Mouri05483a02018-09-10 21:03:42 +0000572void GLES20RenderEngine::setScissor(const Rect& region) {
573 // Invert y-coordinate to map to GL-space.
574 int32_t canvasHeight = mRenderToFbo ? mFboHeight : mSurfaceHeight;
575 int32_t glBottom = canvasHeight - region.bottom;
576
577 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700578 glEnable(GL_SCISSOR_TEST);
579}
580
581void GLES20RenderEngine::disableScissor() {
582 glDisable(GL_SCISSOR_TEST);
583}
584
585void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) {
586 glGenTextures(count, names);
587}
588
589void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
590 glDeleteTextures(count, names);
591}
592
Peiyong Linf1bada92018-08-29 09:39:31 -0700593void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName,
594 const Image& image) {
595 const GLImage& glImage = static_cast<const GLImage&>(image);
596 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
597
598 glBindTexture(target, texName);
599 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700600 glEGLImageTargetTexture2DOES(target,
601 static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700602 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700603}
604
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700605status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
606 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
607 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
608 uint32_t textureName = glFramebuffer->getTextureName();
609 uint32_t framebufferName = glFramebuffer->getFramebufferName();
610
611 // Bind the texture and turn our EGLImage into a texture
612 glBindTexture(GL_TEXTURE_2D, textureName);
613 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
614
615 // Bind the Framebuffer to render into
616 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
617 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
618 GL_TEXTURE_2D, textureName, 0);
619
620 mRenderToFbo = true;
Alec Mouri05483a02018-09-10 21:03:42 +0000621 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700622
623 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
624
625 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES,
626 "glCheckFramebufferStatusOES error %d", glStatus);
627
628 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
629}
630
631void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
632 mRenderToFbo = false;
Alec Mouri05483a02018-09-10 21:03:42 +0000633 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700634
635 // back to main framebuffer
636 glBindFramebuffer(GL_FRAMEBUFFER, 0);
637
638 // Workaround for b/77935566 to force the EGL driver to release the
639 // screenshot buffer
Alec Mouri05483a02018-09-10 21:03:42 +0000640 setScissor(Rect::EMPTY_RECT);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700641 clearWithColor(0.0, 0.0, 0.0, 0.0);
642 disableScissor();
643}
644
Peiyong Lin60bedb52018-09-05 10:47:31 -0700645void GLES20RenderEngine::checkErrors() const {
646 do {
647 // there could be more than one error flag
648 GLenum error = glGetError();
649 if (error == GL_NO_ERROR) break;
650 ALOGE("GL error 0x%04x", int(error));
651 } while (true);
652}
653
Chia-I Wub027f802017-11-29 14:00:52 -0800654void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
Peiyong Linefefaac2018-08-17 12:27:51 -0700655 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800656 int32_t l = sourceCrop.left;
657 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700658 int32_t b = sourceCrop.bottom;
659 int32_t t = sourceCrop.top;
660 if (mRenderToFbo) {
661 std::swap(t, b);
Dan Stozac1879002014-05-22 15:59:05 -0700662 }
Chia-I Wu1be50b52018-08-29 10:44:48 -0700663 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700664
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700665 // Apply custom rotation to the projection.
666 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
667 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700668 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700669 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700670 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800671 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700672 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700673 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800674 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700675 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700676 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800677 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700678 break;
679 default:
680 break;
681 }
682
Mathias Agopian3f844832013-08-07 21:24:32 -0700683 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700684 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700685 mVpWidth = vpw;
686 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700687}
688
Chia-I Wub027f802017-11-29 14:00:52 -0800689void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
690 bool disableTexture, const half4& color) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700691 mState.isPremultipliedAlpha = premultipliedAlpha;
692 mState.isOpaque = opaque;
693 mState.color = color;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694
chaviw13fdc492017-06-27 12:40:18 -0700695 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700696 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700697 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000698
chaviw13fdc492017-06-27 12:40:18 -0700699 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700700 glEnable(GL_BLEND);
701 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
702 } else {
703 glDisable(GL_BLEND);
704 }
705}
706
Chia-I Wu131d3762018-01-11 14:35:27 -0800707void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700708 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800709}
710
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700711void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800712 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600713}
714
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700715void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800716 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600717}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600718
Peiyong Linfb069302018-04-25 14:34:31 -0700719void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700720 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700721}
722
Mathias Agopian49457ac2013-08-14 18:20:17 -0700723void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
724 GLuint target = texture.getTextureTarget();
725 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700726 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700727 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700728 filter = GL_LINEAR;
729 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700730 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
731 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
732 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
733 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700734
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700735 mState.texture = texture;
736 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700737}
738
739void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700740 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700741 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
742 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700743 mState.texture = texture;
744 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700745}
746
Peiyong Lind3788632018-09-18 16:01:31 -0700747void GLES20RenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700748 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700749}
750
Mathias Agopian3f844832013-08-07 21:24:32 -0700751void GLES20RenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700752 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700753}
754
755void GLES20RenderEngine::disableBlending() {
756 glDisable(GL_BLEND);
757}
758
Mathias Agopian19733a32013-08-28 18:13:56 -0700759void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700760 mState.isPremultipliedAlpha = true;
761 mState.isOpaque = false;
762 mState.color = half4(r, g, b, a);
763 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700764 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700765}
766
767void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700768 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700769 if (mesh.getTexCoordsSize()) {
770 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800771 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
772 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700773 }
774
Chia-I Wub027f802017-11-29 14:00:52 -0800775 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
776 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700777
Peiyong Lina296b0c2018-04-30 16:55:29 -0700778 // By default, DISPLAY_P3 is the only supported wide color output. However,
779 // when HDR content is present, hardware composer may be able to handle
780 // BT2020 data space, in that case, the output data space is set to be
781 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
782 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700783 if (mUseColorManagement) {
784 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700785 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
786 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
787 Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace &
788 Dataspace::STANDARD_MASK);
789 Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
790 Dataspace::TRANSFER_MASK);
791 bool needsXYZConversion = needsXYZTransformMatrix();
792
793 if (needsXYZConversion) {
794 // The supported input color spaces are standard RGB, Display P3 and BT2020.
795 switch (inputStandard) {
796 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700797 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700798 break;
799 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700800 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700801 break;
802 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700803 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700804 break;
805 }
806
Peiyong Lin9b03c732018-05-17 10:14:02 -0700807 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700808 switch (outputStandard) {
809 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700810 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700811 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700812 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700813 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700814 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700815 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700816 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700817 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700818 }
819 } else if (inputStandard != outputStandard) {
820 // At this point, the input data space and output data space could be both
821 // HDR data spaces, but they match each other, we do nothing in this case.
822 // In addition to the case above, the input data space could be
823 // - scRGB linear
824 // - scRGB non-linear
825 // - sRGB
826 // - Display P3
827 // The output data spaces could be
828 // - sRGB
829 // - Display P3
830 if (outputStandard == Dataspace::STANDARD_BT709) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700831 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700832 } else if (outputStandard == Dataspace::STANDARD_DCI_P3) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700833 managedState.outputTransformMatrix = mSrgbToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700834 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600835 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700836
837 // we need to convert the RGB value to linear space and convert it back when:
838 // - there is a color matrix that is not an identity matrix, or
839 // - there is an output transform matrix that is not an identity matrix, or
840 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700841 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800842 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700843 managedState.inputTransferFunction =
844 Description::dataSpaceToTransferFunction(inputTransfer);
845 managedState.outputTransferFunction =
846 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700847 }
848
Peiyong Lin13effd12018-07-24 17:01:47 -0700849 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600850
851 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
852
853 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700854 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600855 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700856 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600857 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
858 }
859 } else {
860 ProgramCache::getInstance().useProgram(mState);
861
862 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
863 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700864
865 if (mesh.getTexCoordsSize()) {
866 glDisableVertexAttribArray(Program::texCoords);
867 }
868}
869
Peiyong Linf1bada92018-08-29 09:39:31 -0700870size_t GLES20RenderEngine::getMaxTextureSize() const {
871 return mMaxTextureSize;
872}
873
874size_t GLES20RenderEngine::getMaxViewportDims() const {
875 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
876}
877
Mathias Agopian3f844832013-08-07 21:24:32 -0700878void GLES20RenderEngine::dump(String8& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700879 const GLExtensions& extensions = GLExtensions::getInstance();
880
881 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
882 result.appendFormat("%s\n", extensions.getEGLExtensions());
883
884 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
885 extensions.getVersion());
886 result.appendFormat("%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700887
888 result.appendFormat("RenderEngine program cache size: %zu\n",
889 ProgramCache::getInstance().getSize());
890
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800891 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700892 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
893 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700894}
895
Peiyong Linf11f39b2018-09-05 14:37:41 -0700896GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) {
897 int major, minor;
898 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
899 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
900 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
901 return GLES_VERSION_1_0;
902 }
903 }
904
905 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
906 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
907 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
908 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
909
910 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
911 return GLES_VERSION_1_0;
912}
913
Peiyong Lina296b0c2018-04-30 16:55:29 -0700914bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
915 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
916 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
917 return standard == Dataspace::STANDARD_BT2020 &&
918 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
919}
920
921// For convenience, we want to convert the input color space to XYZ color space first,
922// and then convert from XYZ color space to output color space when
923// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
924// HDR content will be tone-mapped to SDR; Or,
925// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
926// HLG content to PQ content.
927// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
928// input data space or output data space is HDR data space, and the input transfer function
929// doesn't match the output transfer function, we would enable an intermediate transfrom to
930// XYZ color space.
931bool GLES20RenderEngine::needsXYZTransformMatrix() const {
932 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
933 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
934 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
935 const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace &
936 Dataspace::TRANSFER_MASK);
937
938 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
939}
940
Peiyong Linf11f39b2018-09-05 14:37:41 -0700941void GLES20RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
942 mEGLDisplay = display;
943 mEGLConfig = config;
944 mEGLContext = ctxt;
945}
946
Peiyong Lin833074a2018-08-28 11:53:54 -0700947} // namespace gl
948} // namespace renderengine
949} // namespace android