blob: 53b0e4cf20e59c38d9fcc9f9f58f34283ccadbc3 [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 Lin7e219eb2018-12-03 05:40:42 -080022#include "GLESRenderEngine.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070023
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>
Yiwei Zhang5434a782018-12-05 18:06:32 -080030#include <android-base/stringprintf.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070031#include <cutils/compiler.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <renderengine/Mesh.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070033#include <renderengine/Texture.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070034#include <renderengine/private/Description.h>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060035#include <ui/ColorSpace.h>
36#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070037#include <ui/Rect.h>
Peiyong Lin60bedb52018-09-05 10:47:31 -070038#include <ui/Region.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070039#include <utils/KeyedVector.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070040#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
Yiwei Zhang5434a782018-12-05 18:06:32 -0800112using base::StringAppendF;
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
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800225std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32_t featureFlags) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700226 // 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 Linfb530cf2018-12-15 05:07:38 +0000245 EGLContext protectedContext = EGL_NO_CONTEXT;
246 if (extensions.hasProtectedContent()) {
247 protectedContext = createEglContext(display, config, nullptr, useContextPriority,
248 Protection::PROTECTED);
249 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
250 }
251
252 EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority,
253 Protection::UNPROTECTED);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700254
255 // if can't create a GL context, we can only abort.
256 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
257
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800258 EGLSurface dummy = EGL_NO_SURFACE;
259 if (!extensions.hasSurfacelessContext()) {
Peiyong Linfb530cf2018-12-15 05:07:38 +0000260 dummy = createDummyEglPbufferSurface(display, config, hwcFormat, Protection::UNPROTECTED);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800261 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700262 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700263 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
264 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700265 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
266 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
267
Peiyong Linfb530cf2018-12-15 05:07:38 +0000268 // In order to have protected contents in GPU composition, the OpenGL ES extension
269 // GL_EXT_protected_textures must be supported. If it's not supported, reset
270 // protected context to EGL_NO_CONTEXT to indicate that protected contents is not supported.
271 if (!extensions.hasProtectedTexture()) {
272 protectedContext = EGL_NO_CONTEXT;
273 }
274
275 EGLSurface protectedDummy = EGL_NO_SURFACE;
276 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
277 protectedDummy =
278 createDummyEglPbufferSurface(display, config, hwcFormat, Protection::PROTECTED);
279 ALOGE_IF(protectedDummy == EGL_NO_SURFACE, "can't create protected dummy pbuffer");
280 }
281
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800282 // now figure out what version of GL did we actually get
Peiyong Linf11f39b2018-09-05 14:37:41 -0700283 GlesVersion version = parseGlesVersion(extensions.getVersion());
284
285 // initialize the renderer while GL is current
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800286 std::unique_ptr<GLESRenderEngine> engine;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700287 switch (version) {
288 case GLES_VERSION_1_0:
289 case GLES_VERSION_1_1:
290 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
291 break;
292 case GLES_VERSION_2_0:
293 case GLES_VERSION_3_0:
Peiyong Linfb530cf2018-12-15 05:07:38 +0000294 engine = std::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy,
295 protectedContext, protectedDummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700296 break;
297 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700298
299 ALOGI("OpenGL ES informations:");
300 ALOGI("vendor : %s", extensions.getVendor());
301 ALOGI("renderer : %s", extensions.getRenderer());
302 ALOGI("version : %s", extensions.getVersion());
303 ALOGI("extensions: %s", extensions.getExtensions());
304 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
305 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
306
Peiyong Linf11f39b2018-09-05 14:37:41 -0700307 return engine;
308}
309
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800310EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700311 status_t err;
312 EGLConfig config;
313
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800314 // First try to get an ES3 config
315 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700316 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800317 // If ES3 fails, try to get an ES2 config
318 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700319 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800320 // If ES2 still doesn't work, probably because we're on the emulator.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700321 // try a simplified query
322 ALOGW("no suitable EGLConfig found, trying a simpler query");
323 err = selectEGLConfig(display, format, 0, &config);
324 if (err != NO_ERROR) {
325 // this EGL is too lame for android
326 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
327 }
328 }
329 }
330
331 if (logConfig) {
332 // print some debugging info
333 EGLint r, g, b, a;
334 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
335 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
336 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
337 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
338 ALOGI("EGL information:");
339 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
340 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
341 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
342 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
343 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
344 }
345
346 return config;
347}
348
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800349GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000350 EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
351 EGLSurface protectedDummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700352 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800353 mEGLDisplay(display),
354 mEGLConfig(config),
355 mEGLContext(ctxt),
356 mDummySurface(dummy),
Peiyong Linfb530cf2018-12-15 05:07:38 +0000357 mProtectedEGLContext(protectedContext),
358 mProtectedDummySurface(protectedDummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700359 mVpWidth(0),
360 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700361 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700362 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
363 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
364
365 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
366 glPixelStorei(GL_PACK_ALIGNMENT, 4);
367
Peiyong Linfb530cf2018-12-15 05:07:38 +0000368 // Initialize protected EGL Context.
369 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
370 EGLBoolean success = eglMakeCurrent(display, mProtectedDummySurface, mProtectedDummySurface,
371 mProtectedEGLContext);
372 ALOGE_IF(!success, "can't make protected context current");
373 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
374 glPixelStorei(GL_PACK_ALIGNMENT, 4);
375 success = eglMakeCurrent(display, mDummySurface, mDummySurface, mEGLContext);
376 LOG_ALWAYS_FATAL_IF(!success, "can't make default context current");
377 }
378
Chia-I Wub027f802017-11-29 14:00:52 -0800379 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700380 glGenTextures(1, &mProtectedTexName);
381 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800386 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 -0700387
Chia-I Wub027f802017-11-29 14:00:52 -0800388 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600389
Peiyong Lin13effd12018-07-24 17:01:47 -0700390 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800391 const ColorSpace srgb(ColorSpace::sRGB());
392 const ColorSpace displayP3(ColorSpace::DisplayP3());
393 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700394
395 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700396 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
397 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
398 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700399 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700400 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
401 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800402
403 // Compute sRGB to Display P3 and BT2020 transform matrix.
404 // NOTE: For now, we are limiting output wide color space support to
405 // Display-P3 and BT2020 only.
406 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
407 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
408
409 // Compute Display P3 to sRGB and BT2020 transform matrix.
410 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
411 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
412
413 // Compute BT2020 to sRGB and Display P3 transform matrix
414 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
415 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600416 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700417}
418
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800419GLESRenderEngine::~GLESRenderEngine() {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700420 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
421 eglTerminate(mEGLDisplay);
422}
Mathias Agopian3f844832013-08-07 21:24:32 -0700423
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800424std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700425 return std::make_unique<GLFramebuffer>(*this);
426}
427
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800428std::unique_ptr<Image> GLESRenderEngine::createImage() {
Peiyong Linf1bada92018-08-29 09:39:31 -0700429 return std::make_unique<GLImage>(*this);
430}
431
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800432void GLESRenderEngine::primeCache() const {
Peiyong Linfb530cf2018-12-15 05:07:38 +0000433 ProgramCache::getInstance().primeCache(mInProtectedContext ? mProtectedEGLContext : mEGLContext,
434 mFeatureFlags & USE_COLOR_MANAGEMENT);
Peiyong Linf1bada92018-08-29 09:39:31 -0700435}
436
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800437bool GLESRenderEngine::isCurrent() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700438 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
439}
440
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800441base::unique_fd GLESRenderEngine::flush() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700442 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
443 return base::unique_fd();
444 }
445
446 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
447 if (sync == EGL_NO_SYNC_KHR) {
448 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
449 return base::unique_fd();
450 }
451
452 // native fence fd will not be populated until flush() is done.
453 glFlush();
454
455 // get the fence fd
456 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
457 eglDestroySyncKHR(mEGLDisplay, sync);
458 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
459 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
460 }
461
462 return fenceFd;
463}
464
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800465bool GLESRenderEngine::finish() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700466 if (!GLExtensions::getInstance().hasFenceSync()) {
467 ALOGW("no synchronization support");
468 return false;
469 }
470
471 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
472 if (sync == EGL_NO_SYNC_KHR) {
473 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
474 return false;
475 }
476
477 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
478 2000000000 /*2 sec*/);
479 EGLint error = eglGetError();
480 eglDestroySyncKHR(mEGLDisplay, sync);
481 if (result != EGL_CONDITION_SATISFIED_KHR) {
482 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
483 ALOGW("fence wait timed out");
484 } else {
485 ALOGW("error waiting on EGL fence: %#x", error);
486 }
487 return false;
488 }
489
490 return true;
491}
492
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800493bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700494 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
495 !GLExtensions::getInstance().hasWaitSync()) {
496 return false;
497 }
498
499 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
500 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
501 if (sync == EGL_NO_SYNC_KHR) {
502 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
503 return false;
504 }
505
506 // fenceFd is now owned by EGLSync
507 (void)fenceFd.release();
508
509 // XXX: The spec draft is inconsistent as to whether this should return an
510 // EGLint or void. Ignore the return value for now, as it's not strictly
511 // needed.
512 eglWaitSyncKHR(mEGLDisplay, sync, 0);
513 EGLint error = eglGetError();
514 eglDestroySyncKHR(mEGLDisplay, sync);
515 if (error != EGL_SUCCESS) {
516 ALOGE("failed to wait for EGL native fence sync: %#x", error);
517 return false;
518 }
519
520 return true;
521}
522
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800523void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700524 glClearColor(red, green, blue, alpha);
525 glClear(GL_COLOR_BUFFER_BIT);
526}
527
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800528void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue,
529 float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700530 size_t c;
531 Rect const* r = region.getArray(&c);
532 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
533 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
534 for (size_t i = 0; i < c; i++, r++) {
535 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700536 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700537 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700538 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700539 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700540 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700541 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700542 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700543 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700544 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700545 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700546 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700547 }
548 setupFillWithColor(red, green, blue, alpha);
549 drawMesh(mesh);
550}
551
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800552void GLESRenderEngine::setScissor(const Rect& region) {
Alec Mouri05483a02018-09-10 21:03:42 +0000553 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000554 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000555 int32_t glBottom = canvasHeight - region.bottom;
556
557 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700558 glEnable(GL_SCISSOR_TEST);
559}
560
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800561void GLESRenderEngine::disableScissor() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700562 glDisable(GL_SCISSOR_TEST);
563}
564
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800565void GLESRenderEngine::genTextures(size_t count, uint32_t* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700566 glGenTextures(count, names);
567}
568
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800569void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700570 glDeleteTextures(count, names);
571}
572
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800573void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700574 const GLImage& glImage = static_cast<const GLImage&>(image);
575 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
576
577 glBindTexture(target, texName);
Peiyong Linfb530cf2018-12-15 05:07:38 +0000578 if (supportsProtectedContent()) {
579 glTexParameteri(target, GL_TEXTURE_PROTECTED_EXT,
580 glImage.isProtected() ? GL_TRUE : GL_FALSE);
581 }
Peiyong Linf1bada92018-08-29 09:39:31 -0700582 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700583 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700584 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700585}
586
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800587status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700588 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
589 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
590 uint32_t textureName = glFramebuffer->getTextureName();
591 uint32_t framebufferName = glFramebuffer->getFramebufferName();
592
593 // Bind the texture and turn our EGLImage into a texture
594 glBindTexture(GL_TEXTURE_2D, textureName);
Peiyong Linfb530cf2018-12-15 05:07:38 +0000595 if (supportsProtectedContent()) {
596 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_PROTECTED_EXT,
597 mInProtectedContext ? GL_TRUE : GL_FALSE);
598 }
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700599 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
600
601 // Bind the Framebuffer to render into
602 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700603 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700604
Alec Mouri05483a02018-09-10 21:03:42 +0000605 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700606
607 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
608
Peiyong Lin46080ef2018-10-26 18:43:14 -0700609 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
610 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700611
612 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
613}
614
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800615void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000616 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700617
618 // back to main framebuffer
619 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700620}
621
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800622void GLESRenderEngine::checkErrors() const {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700623 do {
624 // there could be more than one error flag
625 GLenum error = glGetError();
626 if (error == GL_NO_ERROR) break;
627 ALOGE("GL error 0x%04x", int(error));
628 } while (true);
629}
630
Peiyong Linfb530cf2018-12-15 05:07:38 +0000631bool GLESRenderEngine::supportsProtectedContent() const {
632 return mProtectedEGLContext != EGL_NO_CONTEXT;
633}
634
635bool GLESRenderEngine::useProtectedContext(bool useProtectedContext) {
636 if (useProtectedContext == mInProtectedContext) {
637 return true;
638 }
639 if (useProtectedContext && mProtectedEGLContext == EGL_NO_CONTEXT) {
640 return false;
641 }
642 const EGLSurface surface = useProtectedContext ? mProtectedDummySurface : mDummySurface;
643 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
644 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
645 if (success) {
646 mInProtectedContext = useProtectedContext;
647 }
648 return success;
649}
650
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800651status_t GLESRenderEngine::drawLayers(const DisplaySettings& /*settings*/,
652 const std::vector<LayerSettings>& /*layers*/,
653 ANativeWindowBuffer* const /*buffer*/,
654 base::unique_fd* /*displayFence*/) const {
Alec Mouri6e57f682018-09-29 20:45:08 -0700655 return NO_ERROR;
656}
657
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800658void GLESRenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
659 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 Mouri7e593912018-11-17 04:57:33 +0000664 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700665 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700666
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700667 // Apply custom rotation to the projection.
668 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
669 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700670 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700671 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700672 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800673 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700674 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700675 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800676 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700677 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700678 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800679 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700680 break;
681 default:
682 break;
683 }
684
Mathias Agopian3f844832013-08-07 21:24:32 -0700685 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700686 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700687 mVpWidth = vpw;
688 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700689}
690
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800691void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
692 const half4& color, float cornerRadius) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700693 mState.isPremultipliedAlpha = premultipliedAlpha;
694 mState.isOpaque = opaque;
695 mState.color = color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700696 mState.cornerRadius = cornerRadius;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697
chaviw13fdc492017-06-27 12:40:18 -0700698 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700699 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700700 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000701
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700702 if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700703 glEnable(GL_BLEND);
704 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
705 } else {
706 glDisable(GL_BLEND);
707 }
708}
709
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800710void GLESRenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700711 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800712}
713
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800714void GLESRenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800715 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600716}
717
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800718void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800719 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600720}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600721
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800722void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700723 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700724}
725
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800726void GLESRenderEngine::setupLayerTexturing(const Texture& texture) {
Mathias Agopian49457ac2013-08-14 18:20:17 -0700727 GLuint target = texture.getTextureTarget();
728 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700729 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700730 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700731 filter = GL_LINEAR;
732 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700733 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
734 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
735 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
736 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700737
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700738 mState.texture = texture;
739 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700740}
741
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800742void GLESRenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700743 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700744 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
745 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700746 mState.texture = texture;
747 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700748}
749
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800750void GLESRenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700751 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700752}
753
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800754void GLESRenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700755 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700756}
757
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800758void GLESRenderEngine::disableBlending() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700759 glDisable(GL_BLEND);
760}
761
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800762void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700763 mState.isPremultipliedAlpha = true;
764 mState.isOpaque = false;
765 mState.color = half4(r, g, b, a);
766 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700767 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700768}
769
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800770void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700771 mState.cropSize = half2(width, height);
772}
773
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800774void GLESRenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700775 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700776 if (mesh.getTexCoordsSize()) {
777 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800778 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
779 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700780 }
781
Chia-I Wub027f802017-11-29 14:00:52 -0800782 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
783 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700784
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700785 if (mState.cornerRadius > 0.0f) {
786 glEnableVertexAttribArray(Program::cropCoords);
787 glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
788 mesh.getByteStride(), mesh.getCropCoords());
789 }
790
Peiyong Lina296b0c2018-04-30 16:55:29 -0700791 // By default, DISPLAY_P3 is the only supported wide color output. However,
792 // when HDR content is present, hardware composer may be able to handle
793 // BT2020 data space, in that case, the output data space is set to be
794 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
795 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700796 if (mUseColorManagement) {
797 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700798 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
799 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700800 Dataspace outputStandard =
801 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
802 Dataspace outputTransfer =
803 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700804 bool needsXYZConversion = needsXYZTransformMatrix();
805
Valerie Haueb8e0762018-11-06 10:10:42 -0800806 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
807 // STANDARD_BT2020, it will be treated as STANDARD_BT709
808 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
809 inputStandard != Dataspace::STANDARD_BT2020) {
810 inputStandard = Dataspace::STANDARD_BT709;
811 }
812
Peiyong Lina296b0c2018-04-30 16:55:29 -0700813 if (needsXYZConversion) {
814 // The supported input color spaces are standard RGB, Display P3 and BT2020.
815 switch (inputStandard) {
816 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700817 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700818 break;
819 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700820 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700821 break;
822 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700823 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700824 break;
825 }
826
Peiyong Lin9b03c732018-05-17 10:14:02 -0700827 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700828 switch (outputStandard) {
829 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700830 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700831 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700832 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700833 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700834 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700835 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700836 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700837 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700838 }
839 } else if (inputStandard != outputStandard) {
840 // At this point, the input data space and output data space could be both
841 // HDR data spaces, but they match each other, we do nothing in this case.
842 // In addition to the case above, the input data space could be
843 // - scRGB linear
844 // - scRGB non-linear
845 // - sRGB
846 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800847 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700848 // The output data spaces could be
849 // - sRGB
850 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800851 // - BT2020
852 switch (outputStandard) {
853 case Dataspace::STANDARD_BT2020:
854 if (inputStandard == Dataspace::STANDARD_BT709) {
855 managedState.outputTransformMatrix = mSrgbToBt2020;
856 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
857 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
858 }
859 break;
860 case Dataspace::STANDARD_DCI_P3:
861 if (inputStandard == Dataspace::STANDARD_BT709) {
862 managedState.outputTransformMatrix = mSrgbToDisplayP3;
863 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
864 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
865 }
866 break;
867 default:
868 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
869 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
870 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
871 managedState.outputTransformMatrix = mBt2020ToSrgb;
872 }
873 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700874 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600875 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700876
877 // we need to convert the RGB value to linear space and convert it back when:
878 // - there is a color matrix that is not an identity matrix, or
879 // - there is an output transform matrix that is not an identity matrix, or
880 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700881 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800882 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700883 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700884 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700885 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700886 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700887 }
888
Peiyong Linfb530cf2018-12-15 05:07:38 +0000889 ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext
890 : mEGLContext,
891 managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600892
893 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
894
895 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700896 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600897 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700898 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600899 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
900 }
901 } else {
Peiyong Linfb530cf2018-12-15 05:07:38 +0000902 ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext
903 : mEGLContext,
904 mState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600905
906 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
907 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700908
909 if (mesh.getTexCoordsSize()) {
910 glDisableVertexAttribArray(Program::texCoords);
911 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700912
913 if (mState.cornerRadius > 0.0f) {
914 glDisableVertexAttribArray(Program::cropCoords);
915 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700916}
917
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800918size_t GLESRenderEngine::getMaxTextureSize() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700919 return mMaxTextureSize;
920}
921
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800922size_t GLESRenderEngine::getMaxViewportDims() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700923 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
924}
925
Yiwei Zhang5434a782018-12-05 18:06:32 -0800926void GLESRenderEngine::dump(std::string& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700927 const GLExtensions& extensions = GLExtensions::getInstance();
Peiyong Linfb530cf2018-12-15 05:07:38 +0000928 ProgramCache& cache = ProgramCache::getInstance();
Peiyong Linf11f39b2018-09-05 14:37:41 -0700929
Yiwei Zhang5434a782018-12-05 18:06:32 -0800930 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
931 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
Yiwei Zhang5434a782018-12-05 18:06:32 -0800932 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
933 extensions.getVersion());
934 StringAppendF(&result, "%s\n", extensions.getExtensions());
Peiyong Linfb530cf2018-12-15 05:07:38 +0000935 StringAppendF(&result, "RenderEngine is in protected context : %d\n", mInProtectedContext);
936 StringAppendF(&result, "RenderEngine program cache size for unprotected context: %zu\n",
937 cache.getSize(mEGLContext));
938 StringAppendF(&result, "RenderEngine program cache size for protected context: %zu\n",
939 cache.getSize(mProtectedEGLContext));
Yiwei Zhang5434a782018-12-05 18:06:32 -0800940 StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n",
941 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
942 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700943}
944
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800945GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700946 int major, minor;
947 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
948 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
949 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
950 return GLES_VERSION_1_0;
951 }
952 }
953
954 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
955 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
956 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
957 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
958
959 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
960 return GLES_VERSION_1_0;
961}
962
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800963EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000964 EGLContext shareContext, bool useContextPriority,
965 Protection protection) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800966 EGLint renderableType = 0;
967 if (config == EGL_NO_CONFIG) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800968 renderableType = EGL_OPENGL_ES3_BIT;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800969 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
970 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
971 }
972 EGLint contextClientVersion = 0;
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800973 if (renderableType & EGL_OPENGL_ES3_BIT) {
974 contextClientVersion = 3;
975 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800976 contextClientVersion = 2;
977 } else if (renderableType & EGL_OPENGL_ES_BIT) {
978 contextClientVersion = 1;
979 } else {
980 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
981 }
982
983 std::vector<EGLint> contextAttributes;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000984 contextAttributes.reserve(7);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800985 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
986 contextAttributes.push_back(contextClientVersion);
987 if (useContextPriority) {
988 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
989 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
990 }
Peiyong Linfb530cf2018-12-15 05:07:38 +0000991 if (protection == Protection::PROTECTED) {
992 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
993 contextAttributes.push_back(EGL_TRUE);
994 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800995 contextAttributes.push_back(EGL_NONE);
996
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800997 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
998
999 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
1000 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
1001 // EGL_NO_CONTEXT so that we can abort.
1002 if (config != EGL_NO_CONFIG) {
1003 return context;
1004 }
1005 // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should
1006 // try to fall back to GLES 2.
1007 contextAttributes[1] = 2;
1008 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
1009 }
1010
1011 return context;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001012}
1013
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001014EGLSurface GLESRenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +00001015 int hwcFormat, Protection protection) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001016 EGLConfig dummyConfig = config;
1017 if (dummyConfig == EGL_NO_CONFIG) {
1018 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
1019 }
1020 std::vector<EGLint> attributes;
Peiyong Linfb530cf2018-12-15 05:07:38 +00001021 attributes.reserve(7);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001022 attributes.push_back(EGL_WIDTH);
1023 attributes.push_back(1);
1024 attributes.push_back(EGL_HEIGHT);
1025 attributes.push_back(1);
Peiyong Linfb530cf2018-12-15 05:07:38 +00001026 if (protection == Protection::PROTECTED) {
1027 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1028 attributes.push_back(EGL_TRUE);
1029 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001030 attributes.push_back(EGL_NONE);
1031
1032 return eglCreatePbufferSurface(display, dummyConfig, attributes.data());
1033}
1034
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001035bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
Peiyong Lina296b0c2018-04-30 16:55:29 -07001036 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
1037 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
1038 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -07001039 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -07001040}
1041
1042// For convenience, we want to convert the input color space to XYZ color space first,
1043// and then convert from XYZ color space to output color space when
1044// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
1045// HDR content will be tone-mapped to SDR; Or,
1046// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
1047// HLG content to PQ content.
1048// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
1049// input data space or output data space is HDR data space, and the input transfer function
1050// doesn't match the output transfer function, we would enable an intermediate transfrom to
1051// XYZ color space.
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001052bool GLESRenderEngine::needsXYZTransformMatrix() const {
Peiyong Lina296b0c2018-04-30 16:55:29 -07001053 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
1054 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
1055 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -07001056 const Dataspace outputTransfer =
1057 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -07001058
1059 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
1060}
1061
Peiyong Lin46080ef2018-10-26 18:43:14 -07001062} // namespace gl
1063} // namespace renderengine
1064} // namespace android