blob: 7adda83926c6852c0bffda946b3e1eb0b12f7629 [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
Peiyong Linf11f39b2018-09-05 14:37:41 -0700243 bool useContextPriority = extensions.hasContextPriority() &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700244 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800245 EGLContext ctxt = createEglContext(display, config, EGL_NO_CONTEXT, useContextPriority);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700246
247 // if can't create a GL context, we can only abort.
248 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
249
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800250 EGLSurface dummy = EGL_NO_SURFACE;
251 if (!extensions.hasSurfacelessContext()) {
252 dummy = createDummyEglPbufferSurface(display, config, hwcFormat);
253 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700254 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800255
Peiyong Linf11f39b2018-09-05 14:37:41 -0700256 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
257 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
258
259 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
260 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
261
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800262 // now figure out what version of GL did we actually get
Peiyong Linf11f39b2018-09-05 14:37:41 -0700263 GlesVersion version = parseGlesVersion(extensions.getVersion());
264
265 // initialize the renderer while GL is current
Peiyong Linf11f39b2018-09-05 14:37:41 -0700266 std::unique_ptr<GLES20RenderEngine> engine;
267 switch (version) {
268 case GLES_VERSION_1_0:
269 case GLES_VERSION_1_1:
270 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
271 break;
272 case GLES_VERSION_2_0:
273 case GLES_VERSION_3_0:
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800274 engine = std::make_unique<GLES20RenderEngine>(featureFlags, display, config, ctxt,
275 dummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700276 break;
277 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700278
279 ALOGI("OpenGL ES informations:");
280 ALOGI("vendor : %s", extensions.getVendor());
281 ALOGI("renderer : %s", extensions.getRenderer());
282 ALOGI("version : %s", extensions.getVersion());
283 ALOGI("extensions: %s", extensions.getExtensions());
284 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
285 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
286
Peiyong Linf11f39b2018-09-05 14:37:41 -0700287 return engine;
288}
289
290EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
291 status_t err;
292 EGLConfig config;
293
294 // First try to get an ES2 config
295 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
296 if (err != NO_ERROR) {
297 // If ES2 fails, try ES1
298 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
299 if (err != NO_ERROR) {
300 // still didn't work, probably because we're on the emulator...
301 // try a simplified query
302 ALOGW("no suitable EGLConfig found, trying a simpler query");
303 err = selectEGLConfig(display, format, 0, &config);
304 if (err != NO_ERROR) {
305 // this EGL is too lame for android
306 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
307 }
308 }
309 }
310
311 if (logConfig) {
312 // print some debugging info
313 EGLint r, g, b, a;
314 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
315 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
316 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
317 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
318 ALOGI("EGL information:");
319 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
320 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
321 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
322 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
323 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
324 }
325
326 return config;
327}
328
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800329GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
330 EGLContext ctxt, EGLSurface dummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700331 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800332 mEGLDisplay(display),
333 mEGLConfig(config),
334 mEGLContext(ctxt),
335 mDummySurface(dummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700336 mVpWidth(0),
337 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700338 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700339 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
340 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
341
342 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
343 glPixelStorei(GL_PACK_ALIGNMENT, 4);
344
Chia-I Wub027f802017-11-29 14:00:52 -0800345 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700346 glGenTextures(1, &mProtectedTexName);
347 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
351 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800352 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 -0700353
Chia-I Wub027f802017-11-29 14:00:52 -0800354 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600355
Peiyong Lin13effd12018-07-24 17:01:47 -0700356 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800357 const ColorSpace srgb(ColorSpace::sRGB());
358 const ColorSpace displayP3(ColorSpace::DisplayP3());
359 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700360
361 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700362 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
363 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
364 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700365 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700366 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
367 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800368
369 // Compute sRGB to Display P3 and BT2020 transform matrix.
370 // NOTE: For now, we are limiting output wide color space support to
371 // Display-P3 and BT2020 only.
372 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
373 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
374
375 // Compute Display P3 to sRGB and BT2020 transform matrix.
376 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
377 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
378
379 // Compute BT2020 to sRGB and Display P3 transform matrix
380 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
381 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600382 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700383}
384
Peiyong Linf11f39b2018-09-05 14:37:41 -0700385GLES20RenderEngine::~GLES20RenderEngine() {
386 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
387 eglTerminate(mEGLDisplay);
388}
Mathias Agopian3f844832013-08-07 21:24:32 -0700389
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700390std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() {
391 return std::make_unique<GLFramebuffer>(*this);
392}
393
Peiyong Linf1bada92018-08-29 09:39:31 -0700394std::unique_ptr<Image> GLES20RenderEngine::createImage() {
395 return std::make_unique<GLImage>(*this);
396}
397
398void GLES20RenderEngine::primeCache() const {
399 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
400}
401
402bool GLES20RenderEngine::isCurrent() const {
403 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
404}
405
Peiyong Lin60bedb52018-09-05 10:47:31 -0700406base::unique_fd GLES20RenderEngine::flush() {
407 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
408 return base::unique_fd();
409 }
410
411 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
412 if (sync == EGL_NO_SYNC_KHR) {
413 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
414 return base::unique_fd();
415 }
416
417 // native fence fd will not be populated until flush() is done.
418 glFlush();
419
420 // get the fence fd
421 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
422 eglDestroySyncKHR(mEGLDisplay, sync);
423 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
424 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
425 }
426
427 return fenceFd;
428}
429
430bool GLES20RenderEngine::finish() {
431 if (!GLExtensions::getInstance().hasFenceSync()) {
432 ALOGW("no synchronization support");
433 return false;
434 }
435
436 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
437 if (sync == EGL_NO_SYNC_KHR) {
438 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
439 return false;
440 }
441
442 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
443 2000000000 /*2 sec*/);
444 EGLint error = eglGetError();
445 eglDestroySyncKHR(mEGLDisplay, sync);
446 if (result != EGL_CONDITION_SATISFIED_KHR) {
447 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
448 ALOGW("fence wait timed out");
449 } else {
450 ALOGW("error waiting on EGL fence: %#x", error);
451 }
452 return false;
453 }
454
455 return true;
456}
457
458bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) {
459 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
460 !GLExtensions::getInstance().hasWaitSync()) {
461 return false;
462 }
463
464 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
465 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
466 if (sync == EGL_NO_SYNC_KHR) {
467 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
468 return false;
469 }
470
471 // fenceFd is now owned by EGLSync
472 (void)fenceFd.release();
473
474 // XXX: The spec draft is inconsistent as to whether this should return an
475 // EGLint or void. Ignore the return value for now, as it's not strictly
476 // needed.
477 eglWaitSyncKHR(mEGLDisplay, sync, 0);
478 EGLint error = eglGetError();
479 eglDestroySyncKHR(mEGLDisplay, sync);
480 if (error != EGL_SUCCESS) {
481 ALOGE("failed to wait for EGL native fence sync: %#x", error);
482 return false;
483 }
484
485 return true;
486}
487
Peiyong Linf11f39b2018-09-05 14:37:41 -0700488void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
489 glClearColor(red, green, blue, alpha);
490 glClear(GL_COLOR_BUFFER_BIT);
491}
492
Chia-I Wu28e3a252018-09-07 12:05:02 -0700493void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green,
494 float blue, float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700495 size_t c;
496 Rect const* r = region.getArray(&c);
497 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
498 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
499 for (size_t i = 0; i < c; i++, r++) {
500 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700501 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700502 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700503 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700504 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700505 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700506 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700507 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700508 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700509 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700510 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700511 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700512 }
513 setupFillWithColor(red, green, blue, alpha);
514 drawMesh(mesh);
515}
516
Alec Mouri05483a02018-09-10 21:03:42 +0000517void GLES20RenderEngine::setScissor(const Rect& region) {
518 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000519 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000520 int32_t glBottom = canvasHeight - region.bottom;
521
522 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700523 glEnable(GL_SCISSOR_TEST);
524}
525
526void GLES20RenderEngine::disableScissor() {
527 glDisable(GL_SCISSOR_TEST);
528}
529
530void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) {
531 glGenTextures(count, names);
532}
533
534void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
535 glDeleteTextures(count, names);
536}
537
Peiyong Lin46080ef2018-10-26 18:43:14 -0700538void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700539 const GLImage& glImage = static_cast<const GLImage&>(image);
540 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
541
542 glBindTexture(target, texName);
543 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700544 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700545 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700546}
547
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700548status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
549 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
550 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
551 uint32_t textureName = glFramebuffer->getTextureName();
552 uint32_t framebufferName = glFramebuffer->getFramebufferName();
553
554 // Bind the texture and turn our EGLImage into a texture
555 glBindTexture(GL_TEXTURE_2D, textureName);
556 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
557
558 // Bind the Framebuffer to render into
559 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700560 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700561
Alec Mouri05483a02018-09-10 21:03:42 +0000562 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700563
564 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
565
Peiyong Lin46080ef2018-10-26 18:43:14 -0700566 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
567 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700568
569 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
570}
571
572void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000573 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700574
575 // back to main framebuffer
576 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700577}
578
Peiyong Lin60bedb52018-09-05 10:47:31 -0700579void GLES20RenderEngine::checkErrors() const {
580 do {
581 // there could be more than one error flag
582 GLenum error = glGetError();
583 if (error == GL_NO_ERROR) break;
584 ALOGE("GL error 0x%04x", int(error));
585 } while (true);
586}
587
Alec Mouri6e57f682018-09-29 20:45:08 -0700588status_t GLES20RenderEngine::drawLayers(const DisplaySettings& /*settings*/,
589 const std::vector<LayerSettings>& /*layers*/,
590 ANativeWindowBuffer* const /*buffer*/,
591 base::unique_fd* /*displayFence*/) const {
592 return NO_ERROR;
593}
594
Chia-I Wub027f802017-11-29 14:00:52 -0800595void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
Peiyong Linefefaac2018-08-17 12:27:51 -0700596 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800597 int32_t l = sourceCrop.left;
598 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700599 int32_t b = sourceCrop.bottom;
600 int32_t t = sourceCrop.top;
Alec Mouri7e593912018-11-17 04:57:33 +0000601 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700602 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700603
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700604 // Apply custom rotation to the projection.
605 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
606 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700607 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700608 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700609 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800610 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700611 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700612 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800613 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700614 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800616 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700617 break;
618 default:
619 break;
620 }
621
Mathias Agopian3f844832013-08-07 21:24:32 -0700622 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700623 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700624 mVpWidth = vpw;
625 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700626}
627
Chia-I Wub027f802017-11-29 14:00:52 -0800628void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700629 bool disableTexture, const half4& color,
630 float cornerRadius) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700631 mState.isPremultipliedAlpha = premultipliedAlpha;
632 mState.isOpaque = opaque;
633 mState.color = color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700634 mState.cornerRadius = cornerRadius;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635
chaviw13fdc492017-06-27 12:40:18 -0700636 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700637 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700638 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000639
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700640 if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700641 glEnable(GL_BLEND);
642 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
643 } else {
644 glDisable(GL_BLEND);
645 }
646}
647
Chia-I Wu131d3762018-01-11 14:35:27 -0800648void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700649 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800650}
651
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700652void GLES20RenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800653 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600654}
655
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700656void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800657 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600658}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600659
Peiyong Linfb069302018-04-25 14:34:31 -0700660void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700661 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700662}
663
Mathias Agopian49457ac2013-08-14 18:20:17 -0700664void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
665 GLuint target = texture.getTextureTarget();
666 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700667 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700668 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700669 filter = GL_LINEAR;
670 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700671 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
672 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
673 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
674 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700675
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700676 mState.texture = texture;
677 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700678}
679
680void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700681 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700682 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
683 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700684 mState.texture = texture;
685 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700686}
687
Peiyong Lind3788632018-09-18 16:01:31 -0700688void GLES20RenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700689 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700690}
691
Mathias Agopian3f844832013-08-07 21:24:32 -0700692void GLES20RenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700693 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700694}
695
696void GLES20RenderEngine::disableBlending() {
697 glDisable(GL_BLEND);
698}
699
Mathias Agopian19733a32013-08-28 18:13:56 -0700700void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700701 mState.isPremultipliedAlpha = true;
702 mState.isOpaque = false;
703 mState.color = half4(r, g, b, a);
704 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700705 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700706}
707
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700708void GLES20RenderEngine::setupCornerRadiusCropSize(float width, float height) {
709 mState.cropSize = half2(width, height);
710}
711
Mathias Agopian3f844832013-08-07 21:24:32 -0700712void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700713 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700714 if (mesh.getTexCoordsSize()) {
715 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800716 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
717 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700718 }
719
Chia-I Wub027f802017-11-29 14:00:52 -0800720 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
721 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700722
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700723 if (mState.cornerRadius > 0.0f) {
724 glEnableVertexAttribArray(Program::cropCoords);
725 glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
726 mesh.getByteStride(), mesh.getCropCoords());
727 }
728
Peiyong Lina296b0c2018-04-30 16:55:29 -0700729 // By default, DISPLAY_P3 is the only supported wide color output. However,
730 // when HDR content is present, hardware composer may be able to handle
731 // BT2020 data space, in that case, the output data space is set to be
732 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
733 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700734 if (mUseColorManagement) {
735 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700736 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
737 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700738 Dataspace outputStandard =
739 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
740 Dataspace outputTransfer =
741 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700742 bool needsXYZConversion = needsXYZTransformMatrix();
743
Valerie Haueb8e0762018-11-06 10:10:42 -0800744 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
745 // STANDARD_BT2020, it will be treated as STANDARD_BT709
746 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
747 inputStandard != Dataspace::STANDARD_BT2020) {
748 inputStandard = Dataspace::STANDARD_BT709;
749 }
750
Peiyong Lina296b0c2018-04-30 16:55:29 -0700751 if (needsXYZConversion) {
752 // The supported input color spaces are standard RGB, Display P3 and BT2020.
753 switch (inputStandard) {
754 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700755 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700756 break;
757 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700758 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700759 break;
760 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700761 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700762 break;
763 }
764
Peiyong Lin9b03c732018-05-17 10:14:02 -0700765 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700766 switch (outputStandard) {
767 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700768 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700769 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700770 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700771 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700772 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700773 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700774 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700775 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700776 }
777 } else if (inputStandard != outputStandard) {
778 // At this point, the input data space and output data space could be both
779 // HDR data spaces, but they match each other, we do nothing in this case.
780 // In addition to the case above, the input data space could be
781 // - scRGB linear
782 // - scRGB non-linear
783 // - sRGB
784 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800785 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700786 // The output data spaces could be
787 // - sRGB
788 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800789 // - BT2020
790 switch (outputStandard) {
791 case Dataspace::STANDARD_BT2020:
792 if (inputStandard == Dataspace::STANDARD_BT709) {
793 managedState.outputTransformMatrix = mSrgbToBt2020;
794 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
795 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
796 }
797 break;
798 case Dataspace::STANDARD_DCI_P3:
799 if (inputStandard == Dataspace::STANDARD_BT709) {
800 managedState.outputTransformMatrix = mSrgbToDisplayP3;
801 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
802 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
803 }
804 break;
805 default:
806 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
807 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
808 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
809 managedState.outputTransformMatrix = mBt2020ToSrgb;
810 }
811 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700812 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600813 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700814
815 // we need to convert the RGB value to linear space and convert it back when:
816 // - there is a color matrix that is not an identity matrix, or
817 // - there is an output transform matrix that is not an identity matrix, or
818 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700819 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800820 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700821 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700822 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700823 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700824 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700825 }
826
Peiyong Lin13effd12018-07-24 17:01:47 -0700827 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600828
829 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
830
831 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700832 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600833 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700834 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600835 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
836 }
837 } else {
838 ProgramCache::getInstance().useProgram(mState);
839
840 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
841 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700842
843 if (mesh.getTexCoordsSize()) {
844 glDisableVertexAttribArray(Program::texCoords);
845 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700846
847 if (mState.cornerRadius > 0.0f) {
848 glDisableVertexAttribArray(Program::cropCoords);
849 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700850}
851
Peiyong Linf1bada92018-08-29 09:39:31 -0700852size_t GLES20RenderEngine::getMaxTextureSize() const {
853 return mMaxTextureSize;
854}
855
856size_t GLES20RenderEngine::getMaxViewportDims() const {
857 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
858}
859
Mathias Agopian3f844832013-08-07 21:24:32 -0700860void GLES20RenderEngine::dump(String8& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700861 const GLExtensions& extensions = GLExtensions::getInstance();
862
863 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
864 result.appendFormat("%s\n", extensions.getEGLExtensions());
865
866 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
867 extensions.getVersion());
868 result.appendFormat("%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700869
870 result.appendFormat("RenderEngine program cache size: %zu\n",
871 ProgramCache::getInstance().getSize());
872
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800873 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700874 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
875 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700876}
877
Peiyong Linf11f39b2018-09-05 14:37:41 -0700878GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) {
879 int major, minor;
880 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
881 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
882 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
883 return GLES_VERSION_1_0;
884 }
885 }
886
887 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
888 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
889 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
890 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
891
892 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
893 return GLES_VERSION_1_0;
894}
895
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800896EGLContext GLES20RenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
897 EGLContext shareContext, bool useContextPriority) {
898 EGLint renderableType = 0;
899 if (config == EGL_NO_CONFIG) {
900 renderableType = EGL_OPENGL_ES2_BIT;
901 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
902 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
903 }
904 EGLint contextClientVersion = 0;
905 if (renderableType & EGL_OPENGL_ES2_BIT) {
906 contextClientVersion = 2;
907 } else if (renderableType & EGL_OPENGL_ES_BIT) {
908 contextClientVersion = 1;
909 } else {
910 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
911 }
912
913 std::vector<EGLint> contextAttributes;
914 contextAttributes.reserve(5);
915 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
916 contextAttributes.push_back(contextClientVersion);
917 if (useContextPriority) {
918 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
919 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
920 }
921 contextAttributes.push_back(EGL_NONE);
922
923 return eglCreateContext(display, config, shareContext, contextAttributes.data());
924}
925
926EGLSurface GLES20RenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
927 int hwcFormat) {
928 EGLConfig dummyConfig = config;
929 if (dummyConfig == EGL_NO_CONFIG) {
930 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
931 }
932 std::vector<EGLint> attributes;
933 attributes.reserve(5);
934 attributes.push_back(EGL_WIDTH);
935 attributes.push_back(1);
936 attributes.push_back(EGL_HEIGHT);
937 attributes.push_back(1);
938 attributes.push_back(EGL_NONE);
939
940 return eglCreatePbufferSurface(display, dummyConfig, attributes.data());
941}
942
Peiyong Lina296b0c2018-04-30 16:55:29 -0700943bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
944 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
945 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
946 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700947 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700948}
949
950// For convenience, we want to convert the input color space to XYZ color space first,
951// and then convert from XYZ color space to output color space when
952// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
953// HDR content will be tone-mapped to SDR; Or,
954// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
955// HLG content to PQ content.
956// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
957// input data space or output data space is HDR data space, and the input transfer function
958// doesn't match the output transfer function, we would enable an intermediate transfrom to
959// XYZ color space.
960bool GLES20RenderEngine::needsXYZTransformMatrix() const {
961 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
962 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
963 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700964 const Dataspace outputTransfer =
965 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700966
967 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
968}
969
Peiyong Lin46080ef2018-10-26 18:43:14 -0700970} // namespace gl
971} // namespace renderengine
972} // namespace android