blob: d107d8e529eaba75c3cd1f82977f537396aefd0d [file] [log] [blame]
Jesse Hall21558da2013-08-06 15:31:22 -07001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall21558da2013-08-06 15:31:22 -07004 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall21558da2013-08-06 15:31:22 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall21558da2013-08-06 15:31:22 -070010 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jesse Hallb29e5e82012-04-04 16:53:42 -070017#define __STDC_LIMIT_MACROS 1
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hallb29e5e82012-04-04 16:53:42 -070019
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "egl_display.h"
Mathias Agopian4b9511c2011-11-13 23:52:47 -080021
Mathias Agopian39c24a22013-04-04 23:17:56 -070022#include "../egl_impl.h"
23
Cody Northrop1f00e172018-04-02 11:23:31 -060024#include <EGL/eglext_angle.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080025#include <private/EGL/display.h>
26
Tobin Ehlis96a184d2018-07-18 16:14:07 -060027#include <cutils/properties.h>
28#include "Loader.h"
29#include "egl_angle_platform.h"
Jamie Gennisaca51c02011-11-03 17:42:43 -070030#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include "egl_object.h"
32#include "egl_tls.h"
Tobin Ehlis96a184d2018-07-18 16:14:07 -060033
Sundong Ahn204fb1f2020-04-23 21:56:36 +090034#include <SurfaceFlingerProperties.h>
Tobin Ehlis96a184d2018-07-18 16:14:07 -060035#include <android/dlext.h>
36#include <dlfcn.h>
37#include <graphicsenv/GraphicsEnv.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080038
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060039#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
40#include <configstore/Utils.h>
41
42using namespace android::hardware::configstore;
43using namespace android::hardware::configstore::V1_0;
44
Mathias Agopian518ec112011-05-13 16:21:08 -070045// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopian4b9511c2011-11-13 23:52:47 -080049static char const * const sVendorString = "Android";
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -060050static char const* const sVersionString14 = "1.4 Android META-EGL";
51static char const* const sVersionString15 = "1.5 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070052static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080053
Jesse Hall21558da2013-08-06 15:31:22 -070054extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070055extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080056
Mathias Agopian518ec112011-05-13 16:21:08 -070057extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
58
Mathias Agopian518ec112011-05-13 16:21:08 -070059// ----------------------------------------------------------------------------
60
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070061bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070062 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070063 if (!nameLen) {
64 nameLen = strlen(name);
65 }
Kalle Raita7804aa22016-04-18 16:03:37 -070066 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
67 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
68 return true;
69 }
Jesse Hallc2e41222013-08-08 13:40:22 -070070 }
71 }
72 return false;
73}
74
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -070075bool needsAndroidPEglMitigation() {
76 static const int32_t vndk_version = property_get_int32("ro.vndk.version", -1);
77 return vndk_version <= 28;
78}
79
Mathias Agopianb7f9a242017-03-08 22:29:31 -080080int egl_get_init_count(EGLDisplay dpy) {
81 egl_display_t* eglDisplay = egl_display_t::get(dpy);
82 return eglDisplay ? eglDisplay->getRefsCount() : 0;
83}
84
Mathias Agopian518ec112011-05-13 16:21:08 -070085egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
86
87egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080088 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070089}
90
91egl_display_t::~egl_display_t() {
92 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080093 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070094}
95
96egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080097 if (uintptr_t(dpy) == 0) {
98 return nullptr;
99 }
100
Mathias Agopian518ec112011-05-13 16:21:08 -0700101 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -0700102 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
103 return nullptr;
104 }
105 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700106}
107
108void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800109 std::lock_guard<std::mutex> _l(lock);
110 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700111}
112
Mathias Agopian5b287a62011-05-16 18:58:55 -0700113void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800114 std::lock_guard<std::mutex> _l(lock);
115 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700116}
117
Mathias Agopianf0480de2011-11-13 20:50:07 -0800118bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800119 std::lock_guard<std::mutex> _l(lock);
120 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800121 if (object->getDisplay() == this) {
122 object->incRef();
123 return true;
124 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 }
126 return false;
127}
128
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600129EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
130 const EGLAttrib* attrib_list) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700131 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700132 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700133
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600134 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700135}
136
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600137static bool addAnglePlatformAttributes(egl_connection_t* const cnx,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600138 std::vector<EGLAttrib>& attrs) {
139 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
140
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600141 attrs.reserve(4 * 2);
142
143 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
144 attrs.push_back(cnx->angleBackend);
145
146 switch (cnx->angleBackend) {
147 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
148 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600149 char prop[PROPERTY_VALUE_MAX];
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600150 property_get("debug.angle.validation", prop, "0");
151 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
152 attrs.push_back(atoi(prop));
153 break;
154 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
155 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
156 break;
157 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
158 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
159 // NOTE: This is only valid if the backend is OpenGL
160 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
161 attrs.push_back(vendorEGL);
Courtney Goeltzenleuchter559b4332020-03-06 11:12:19 -0700162
163 // Context virtualization is only available on GL back-end.
164 // Needed to support threading with GL back-end
165 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
166 attrs.push_back(EGL_FALSE);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600167 break;
168 default:
Courtney Goeltzenleuchter559b4332020-03-06 11:12:19 -0700169 ALOGE("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600170 break;
171 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600172
173 return true;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600174}
175
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600176static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
177 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600178 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600179 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600180
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600181 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600182 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600183 if (attrib_list) {
184 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
185 attrs.push_back(attr[0]);
186 attrs.push_back(attr[1]);
187 }
188 }
189
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600190 if (!addAnglePlatformAttributes(cnx, attrs)) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600191 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
192 *error = EGL_BAD_PARAMETER;
193 return EGL_NO_DISPLAY;
194 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600195 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600196
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600197 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
198 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
199 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600200 if (dpy == EGL_NO_DISPLAY) {
201 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600202 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700203 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600204 ALOGE("initializeAnglePlatform failed!");
205 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600206 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600207 } else {
208 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
209 display);
210 }
211
212 return dpy;
213}
214
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600215EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
216 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800217 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800218 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700219
220 // get our driver loader
221 Loader& loader(Loader::getInstance());
222
Mathias Agopianada798b2012-02-13 17:09:30 -0800223 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700224 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600225 EGLDisplay dpy = EGL_NO_DISPLAY;
226
Charlie Lao840bd532020-01-16 17:34:37 -0800227 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600228 EGLint error;
229 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
230 if (error != EGL_NONE) {
231 return setError(error, dpy);
232 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600233 }
234 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600235 // NOTE: eglGetPlatformDisplay with a empty attribute list
236 // behaves the same as eglGetDisplay
237 if (cnx->egl.eglGetPlatformDisplay) {
238 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
239 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800240 }
241
242 // It is possible that eglGetPlatformDisplay does not have a
243 // working implementation for Android platform; in that case,
244 // one last fallback to eglGetDisplay
245 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600246 if (attrib_list) {
247 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
248 }
249 dpy = cnx->egl.eglGetDisplay(display);
250 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600251 }
252
Mathias Agopianada798b2012-02-13 17:09:30 -0800253 disp.dpy = dpy;
254 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600255 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700256 }
257 }
258
259 return EGLDisplay(uintptr_t(display) + 1U);
260}
261
262EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
263
Mathias Agopian65421432017-03-08 11:49:05 -0800264 { // scope for refLock
265 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800266 refs++;
267 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600268 // We don't know what to report until we know what the
269 // driver supports. Make sure we are initialized before
270 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800271 while(!eglIsInitialized) {
272 refCond.wait(_l);
273 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600274 egl_connection_t* const cnx = &gEGLImpl;
275
276 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
277 // changing the behavior from the past where we always advertise
278 // version 1.4. May need to check that revision is valid
279 // before using cnx->major & cnx->minor
280 if (major != nullptr) *major = cnx->major;
281 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800282 return EGL_TRUE;
283 }
Mathias Agopian65421432017-03-08 11:49:05 -0800284 while(eglIsInitialized) {
285 refCond.wait(_l);
286 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800287 }
288
Mathias Agopian65421432017-03-08 11:49:05 -0800289 { // scope for lock
290 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800291
Michael Lentine54466bc2015-01-27 09:01:03 -0800292 setGLHooksThreadSpecific(&gHooksNoContext);
293
294 // initialize each EGL and
295 // build our own extension string first, based on the extension we know
296 // and the extension supported by our client implementation
297
298 egl_connection_t* const cnx = &gEGLImpl;
299 cnx->major = -1;
300 cnx->minor = -1;
301 if (cnx->dso) {
302 EGLDisplay idpy = disp.dpy;
303 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
304 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
305 // idpy, cnx->major, cnx->minor, cnx);
306
307 // display is now initialized
308 disp.state = egl_display_t::INITIALIZED;
309
310 // get the query-strings for this display for each implementation
311 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
312 EGL_VENDOR);
313 disp.queryString.version = cnx->egl.eglQueryString(idpy,
314 EGL_VERSION);
315 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
316 EGL_EXTENSIONS);
317 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
318 EGL_CLIENT_APIS);
319
320 } else {
321 ALOGW("eglInitialize(%p) failed (%s)", idpy,
322 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
323 }
324 }
325
Sean Callananad9061c2019-03-12 14:07:23 -0700326 if (cnx->minor == 5) {
327 // full list in egl_entries.in
328 if (!cnx->egl.eglCreateImage ||
329 !cnx->egl.eglDestroyImage ||
330 !cnx->egl.eglGetPlatformDisplay ||
331 !cnx->egl.eglCreatePlatformWindowSurface ||
332 !cnx->egl.eglCreatePlatformPixmapSurface ||
333 !cnx->egl.eglCreateSync ||
334 !cnx->egl.eglDestroySync ||
335 !cnx->egl.eglClientWaitSync ||
336 !cnx->egl.eglGetSyncAttrib ||
337 !cnx->egl.eglWaitSync) {
338 ALOGE("Driver indicates EGL 1.5 support, but does not have "
339 "a critical API");
340 cnx->minor = 4;
341 }
342 }
343
Michael Lentine54466bc2015-01-27 09:01:03 -0800344 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800345 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600346 mVersionString.clear();
347 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600348 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600349 if ((cnx->major == 1) && (cnx->minor == 5)) {
350 mVersionString = sVersionString15;
351 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600352 }
353 if (mVersionString.empty()) {
354 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
355 mVersionString = sVersionString14;
356 }
Mathias Agopian65421432017-03-08 11:49:05 -0800357 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800358
Mathias Agopian65421432017-03-08 11:49:05 -0800359 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600360
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700361 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
362
363 // Note: CDD requires that devices supporting wide color and/or HDR color also support
364 // the EGL_KHR_gl_colorspace extension.
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900365 bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600366
367 // Add wide-color extensions if device can support wide-color
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700368 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600369 mExtensionString.append(
370 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800371 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
372 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600373 }
374
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900375 bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700376
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700377 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700378 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
379 // Typically that means there is an HDR capable display attached, but could be
380 // support for attaching an HDR display. In either case, advertise support for
381 // HDR color spaces.
382 mExtensionString.append(
383 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
384 }
385
Michael Lentine54466bc2015-01-27 09:01:03 -0800386 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800387 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400388 // length of the extension name
389 size_t len = strcspn(start, " ");
390 if (len) {
391 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800392 const std::string ext(start, len);
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -0700393 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
394 // some Android P vendor partitions this extension under the draft KHR name,
395 // but during Khronos review it was decided to demote it to EXT.
396 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
397 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
398 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
399 }
Mathias Agopian65421432017-03-08 11:49:05 -0800400 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400401 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800402 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400403 // advance to the next extension name, skipping the space.
404 start += len;
405 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800406 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400407 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800408
409 egl_cache_t::get()->initialize(this);
410
411 char value[PROPERTY_VALUE_MAX];
412 property_get("debug.egl.finish", value, "0");
413 if (atoi(value)) {
414 finishOnSwap = true;
415 }
416
417 property_get("debug.egl.traceGpuCompletion", value, "0");
418 if (atoi(value)) {
419 traceGpuCompletion = true;
420 }
421
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600422 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
423 // changing the behavior from the past where we always advertise
424 // version 1.4. May need to check that revision is valid
425 // before using cnx->major & cnx->minor
426 if (major != nullptr) *major = cnx->major;
427 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700428 }
429
Mathias Agopian65421432017-03-08 11:49:05 -0800430 { // scope for refLock
431 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800432 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800433 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700434 }
435
Mathias Agopian7773c432012-02-13 20:06:08 -0800436 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700437}
438
439EGLBoolean egl_display_t::terminate() {
440
Mathias Agopian65421432017-03-08 11:49:05 -0800441 { // scope for refLock
442 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800443 if (refs == 0) {
444 /*
445 * From the EGL spec (3.2):
446 * "Termination of a display that has already been terminated,
447 * (...), is allowed, but the only effect of such a call is
448 * to return EGL_TRUE (...)
449 */
450 return EGL_TRUE;
451 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700452
Michael Lentine54466bc2015-01-27 09:01:03 -0800453 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700454 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800455 if (refs > 0) {
456 return EGL_TRUE;
457 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700458 }
459
460 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800461
Mathias Agopian65421432017-03-08 11:49:05 -0800462 { // scope for lock
463 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800464
465 egl_connection_t* const cnx = &gEGLImpl;
466 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600467 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800468 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700469 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600470 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800471 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
472 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
473 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
474 }
475 // REVISIT: it's unclear what to do if eglTerminate() fails
476 disp.state = egl_display_t::TERMINATED;
477 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700478 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800479
Michael Lentine54466bc2015-01-27 09:01:03 -0800480 // Reset the extension string since it will be regenerated if we get
481 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800482 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800483
484 // Mark all objects remaining in the list as terminated, unless
485 // there are no reference to them, it which case, we're free to
486 // delete them.
487 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800488 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800489 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800490 o->destroy();
491 }
492
493 // this marks all object handles are "terminated"
494 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700495 }
496
Mathias Agopian65421432017-03-08 11:49:05 -0800497 { // scope for refLock
498 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800499 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800500 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700501 }
502
Mathias Agopian518ec112011-05-13 16:21:08 -0700503 return res;
504}
505
Mathias Agopianfb87e542012-01-30 18:20:52 -0800506void egl_display_t::loseCurrent(egl_context_t * cur_c)
507{
508 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800509 egl_display_t* display = cur_c->getDisplay();
510 if (display) {
511 display->loseCurrentImpl(cur_c);
512 }
513 }
514}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800515
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800516void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
517{
518 // by construction, these are either 0 or valid (possibly terminated)
519 // it should be impossible for these to be invalid
520 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700521 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
522 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800523
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800524 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800525 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800526 cur_c->onLooseCurrent();
527
Mathias Agopianfb87e542012-01-30 18:20:52 -0800528 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800529
530 // This cannot be called with the lock held because it might end-up
531 // calling back into EGL (in particular when a surface is destroyed
532 // it calls ANativeWindow::disconnect
533 _cur_c.release();
534 _cur_r.release();
535 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800536}
537
538EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700539 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800540 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
541{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800542 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800543
544 // by construction, these are either 0 or valid (possibly terminated)
545 // it should be impossible for these to be invalid
546 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700547 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
548 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800549
550 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800551 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800552 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800553 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800554 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800555 if (result == EGL_TRUE) {
556 c->onMakeCurrent(draw, read);
557 }
558 } else {
559 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800560 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800561 if (result == EGL_TRUE) {
562 cur_c->onLooseCurrent();
563 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800564 }
565 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800566
567 if (result == EGL_TRUE) {
568 // This cannot be called with the lock held because it might end-up
569 // calling back into EGL (in particular when a surface is destroyed
570 // it calls ANativeWindow::disconnect
571 _cur_c.release();
572 _cur_r.release();
573 _cur_d.release();
574 }
575
Mathias Agopianfb87e542012-01-30 18:20:52 -0800576 return result;
577}
Mathias Agopian518ec112011-05-13 16:21:08 -0700578
Jesse Hallc2e41222013-08-08 13:40:22 -0700579bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
580 if (!nameLen) {
581 nameLen = strlen(name);
582 }
Mathias Agopian65421432017-03-08 11:49:05 -0800583 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700584}
585
Jesse Halla0fef1c2012-04-17 12:02:26 -0700586// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700587}; // namespace android
588// ----------------------------------------------------------------------------