blob: 3b19b32a3e3c98535296bf0053fca2129f4edac7 [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
34#include <android/dlext.h>
35#include <dlfcn.h>
36#include <graphicsenv/GraphicsEnv.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080037
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060038#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
39#include <configstore/Utils.h>
40
41using namespace android::hardware::configstore;
42using namespace android::hardware::configstore::V1_0;
43
Mathias Agopian518ec112011-05-13 16:21:08 -070044// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
Mathias Agopian4b9511c2011-11-13 23:52:47 -080048static char const * const sVendorString = "Android";
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -060049static char const* const sVersionString14 = "1.4 Android META-EGL";
50static char const* const sVersionString15 = "1.5 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070051static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080052
Jesse Hall21558da2013-08-06 15:31:22 -070053extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080055
Mathias Agopian518ec112011-05-13 16:21:08 -070056extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058// ----------------------------------------------------------------------------
59
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070060bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070061 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070062 if (!nameLen) {
63 nameLen = strlen(name);
64 }
Kalle Raita7804aa22016-04-18 16:03:37 -070065 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
66 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
67 return true;
68 }
Jesse Hallc2e41222013-08-08 13:40:22 -070069 }
70 }
71 return false;
72}
73
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -070074bool needsAndroidPEglMitigation() {
75 static const int32_t vndk_version = property_get_int32("ro.vndk.version", -1);
76 return vndk_version <= 28;
77}
78
Mathias Agopianb7f9a242017-03-08 22:29:31 -080079int egl_get_init_count(EGLDisplay dpy) {
80 egl_display_t* eglDisplay = egl_display_t::get(dpy);
81 return eglDisplay ? eglDisplay->getRefsCount() : 0;
82}
83
Mathias Agopian518ec112011-05-13 16:21:08 -070084egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
85
86egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080087 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070088}
89
90egl_display_t::~egl_display_t() {
91 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080092 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070093}
94
95egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080096 if (uintptr_t(dpy) == 0) {
97 return nullptr;
98 }
99
Mathias Agopian518ec112011-05-13 16:21:08 -0700100 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -0700101 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
102 return nullptr;
103 }
104 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700105}
106
107void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800108 std::lock_guard<std::mutex> _l(lock);
109 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700110}
111
Mathias Agopian5b287a62011-05-16 18:58:55 -0700112void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800113 std::lock_guard<std::mutex> _l(lock);
114 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700115}
116
Mathias Agopianf0480de2011-11-13 20:50:07 -0800117bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800118 std::lock_guard<std::mutex> _l(lock);
119 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800120 if (object->getDisplay() == this) {
121 object->incRef();
122 return true;
123 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700124 }
125 return false;
126}
127
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600128EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
129 const EGLAttrib* attrib_list) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700130 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700131 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700132
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600133 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700134}
135
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600136static bool addAnglePlatformAttributes(egl_connection_t* const cnx,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600137 std::vector<EGLAttrib>& attrs) {
138 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
139
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600140 attrs.reserve(4 * 2);
141
142 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
143 attrs.push_back(cnx->angleBackend);
144
145 switch (cnx->angleBackend) {
146 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
147 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600148 char prop[PROPERTY_VALUE_MAX];
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600149 property_get("debug.angle.validation", prop, "0");
150 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
151 attrs.push_back(atoi(prop));
152 break;
153 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
154 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
155 break;
156 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
157 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
158 // NOTE: This is only valid if the backend is OpenGL
159 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
160 attrs.push_back(vendorEGL);
161 break;
162 default:
163 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
164 break;
165 }
166 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
167 attrs.push_back(EGL_FALSE);
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600168
169 return true;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600170}
171
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600172static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
173 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600174 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600175 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600176
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600177 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600178 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600179 if (attrib_list) {
180 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
181 attrs.push_back(attr[0]);
182 attrs.push_back(attr[1]);
183 }
184 }
185
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600186 if (!addAnglePlatformAttributes(cnx, attrs)) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600187 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
188 *error = EGL_BAD_PARAMETER;
189 return EGL_NO_DISPLAY;
190 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600191 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600192
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600193 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
194 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
195 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600196 if (dpy == EGL_NO_DISPLAY) {
197 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600198 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700199 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600200 ALOGE("initializeAnglePlatform failed!");
201 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600202 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600203 } else {
204 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
205 display);
206 }
207
208 return dpy;
209}
210
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600211EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
212 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800213 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800214 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700215
216 // get our driver loader
217 Loader& loader(Loader::getInstance());
218
Mathias Agopianada798b2012-02-13 17:09:30 -0800219 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700220 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600221 EGLDisplay dpy = EGL_NO_DISPLAY;
222
Charlie Lao95c7aea2020-01-10 11:12:03 -0800223 if (cnx->loadedDriverType == egl_connection_t::GLES_ANGLE_STANDALONE_DRIVER) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600224 EGLint error;
225 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
226 if (error != EGL_NONE) {
227 return setError(error, dpy);
228 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600229 }
230 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600231 // NOTE: eglGetPlatformDisplay with a empty attribute list
232 // behaves the same as eglGetDisplay
233 if (cnx->egl.eglGetPlatformDisplay) {
234 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
235 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800236 }
237
238 // It is possible that eglGetPlatformDisplay does not have a
239 // working implementation for Android platform; in that case,
240 // one last fallback to eglGetDisplay
241 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600242 if (attrib_list) {
243 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
244 }
245 dpy = cnx->egl.eglGetDisplay(display);
246 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600247 }
248
Mathias Agopianada798b2012-02-13 17:09:30 -0800249 disp.dpy = dpy;
250 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600251 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700252 }
253 }
254
255 return EGLDisplay(uintptr_t(display) + 1U);
256}
257
258EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
259
Mathias Agopian65421432017-03-08 11:49:05 -0800260 { // scope for refLock
261 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800262 refs++;
263 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600264 // We don't know what to report until we know what the
265 // driver supports. Make sure we are initialized before
266 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800267 while(!eglIsInitialized) {
268 refCond.wait(_l);
269 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600270 egl_connection_t* const cnx = &gEGLImpl;
271
272 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
273 // changing the behavior from the past where we always advertise
274 // version 1.4. May need to check that revision is valid
275 // before using cnx->major & cnx->minor
276 if (major != nullptr) *major = cnx->major;
277 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800278 return EGL_TRUE;
279 }
Mathias Agopian65421432017-03-08 11:49:05 -0800280 while(eglIsInitialized) {
281 refCond.wait(_l);
282 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800283 }
284
Mathias Agopian65421432017-03-08 11:49:05 -0800285 { // scope for lock
286 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800287
Michael Lentine54466bc2015-01-27 09:01:03 -0800288 setGLHooksThreadSpecific(&gHooksNoContext);
289
290 // initialize each EGL and
291 // build our own extension string first, based on the extension we know
292 // and the extension supported by our client implementation
293
294 egl_connection_t* const cnx = &gEGLImpl;
295 cnx->major = -1;
296 cnx->minor = -1;
297 if (cnx->dso) {
298 EGLDisplay idpy = disp.dpy;
299 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
300 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
301 // idpy, cnx->major, cnx->minor, cnx);
302
303 // display is now initialized
304 disp.state = egl_display_t::INITIALIZED;
305
306 // get the query-strings for this display for each implementation
307 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
308 EGL_VENDOR);
309 disp.queryString.version = cnx->egl.eglQueryString(idpy,
310 EGL_VERSION);
311 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
312 EGL_EXTENSIONS);
313 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
314 EGL_CLIENT_APIS);
315
316 } else {
317 ALOGW("eglInitialize(%p) failed (%s)", idpy,
318 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
319 }
320 }
321
Sean Callananad9061c2019-03-12 14:07:23 -0700322 if (cnx->minor == 5) {
323 // full list in egl_entries.in
324 if (!cnx->egl.eglCreateImage ||
325 !cnx->egl.eglDestroyImage ||
326 !cnx->egl.eglGetPlatformDisplay ||
327 !cnx->egl.eglCreatePlatformWindowSurface ||
328 !cnx->egl.eglCreatePlatformPixmapSurface ||
329 !cnx->egl.eglCreateSync ||
330 !cnx->egl.eglDestroySync ||
331 !cnx->egl.eglClientWaitSync ||
332 !cnx->egl.eglGetSyncAttrib ||
333 !cnx->egl.eglWaitSync) {
334 ALOGE("Driver indicates EGL 1.5 support, but does not have "
335 "a critical API");
336 cnx->minor = 4;
337 }
338 }
339
Michael Lentine54466bc2015-01-27 09:01:03 -0800340 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800341 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600342 mVersionString.clear();
343 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600344 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600345 if ((cnx->major == 1) && (cnx->minor == 5)) {
346 mVersionString = sVersionString15;
347 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600348 }
349 if (mVersionString.empty()) {
350 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
351 mVersionString = sVersionString14;
352 }
Mathias Agopian65421432017-03-08 11:49:05 -0800353 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800354
Mathias Agopian65421432017-03-08 11:49:05 -0800355 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600356
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700357 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
358
359 // Note: CDD requires that devices supporting wide color and/or HDR color also support
360 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600361 bool wideColorBoardConfig =
362 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
363 false);
364
365 // Add wide-color extensions if device can support wide-color
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700366 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600367 mExtensionString.append(
368 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800369 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
370 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600371 }
372
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700373 bool hasHdrBoardConfig =
374 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
375
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700376 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700377 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
378 // Typically that means there is an HDR capable display attached, but could be
379 // support for attaching an HDR display. In either case, advertise support for
380 // HDR color spaces.
381 mExtensionString.append(
382 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
383 }
384
Michael Lentine54466bc2015-01-27 09:01:03 -0800385 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800386 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400387 // length of the extension name
388 size_t len = strcspn(start, " ");
389 if (len) {
390 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800391 const std::string ext(start, len);
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -0700392 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
393 // some Android P vendor partitions this extension under the draft KHR name,
394 // but during Khronos review it was decided to demote it to EXT.
395 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
396 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
397 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
398 }
Mathias Agopian65421432017-03-08 11:49:05 -0800399 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400400 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800401 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400402 // advance to the next extension name, skipping the space.
403 start += len;
404 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800405 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400406 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800407
408 egl_cache_t::get()->initialize(this);
409
410 char value[PROPERTY_VALUE_MAX];
411 property_get("debug.egl.finish", value, "0");
412 if (atoi(value)) {
413 finishOnSwap = true;
414 }
415
416 property_get("debug.egl.traceGpuCompletion", value, "0");
417 if (atoi(value)) {
418 traceGpuCompletion = true;
419 }
420
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600421 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
422 // changing the behavior from the past where we always advertise
423 // version 1.4. May need to check that revision is valid
424 // before using cnx->major & cnx->minor
425 if (major != nullptr) *major = cnx->major;
426 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700427 }
428
Mathias Agopian65421432017-03-08 11:49:05 -0800429 { // scope for refLock
430 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800431 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800432 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700433 }
434
Mathias Agopian7773c432012-02-13 20:06:08 -0800435 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700436}
437
438EGLBoolean egl_display_t::terminate() {
439
Mathias Agopian65421432017-03-08 11:49:05 -0800440 { // scope for refLock
441 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800442 if (refs == 0) {
443 /*
444 * From the EGL spec (3.2):
445 * "Termination of a display that has already been terminated,
446 * (...), is allowed, but the only effect of such a call is
447 * to return EGL_TRUE (...)
448 */
449 return EGL_TRUE;
450 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700451
Michael Lentine54466bc2015-01-27 09:01:03 -0800452 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700453 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800454 if (refs > 0) {
455 return EGL_TRUE;
456 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700457 }
458
459 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800460
Mathias Agopian65421432017-03-08 11:49:05 -0800461 { // scope for lock
462 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800463
464 egl_connection_t* const cnx = &gEGLImpl;
465 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600466 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao95c7aea2020-01-10 11:12:03 -0800467 if (cnx->loadedDriverType == egl_connection_t::GLES_ANGLE_STANDALONE_DRIVER) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700468 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600469 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800470 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
471 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
472 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
473 }
474 // REVISIT: it's unclear what to do if eglTerminate() fails
475 disp.state = egl_display_t::TERMINATED;
476 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700477 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800478
Michael Lentine54466bc2015-01-27 09:01:03 -0800479 // Reset the extension string since it will be regenerated if we get
480 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800481 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800482
483 // Mark all objects remaining in the list as terminated, unless
484 // there are no reference to them, it which case, we're free to
485 // delete them.
486 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800487 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800488 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800489 o->destroy();
490 }
491
492 // this marks all object handles are "terminated"
493 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700494 }
495
Mathias Agopian65421432017-03-08 11:49:05 -0800496 { // scope for refLock
497 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800498 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800499 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700500 }
501
Mathias Agopian518ec112011-05-13 16:21:08 -0700502 return res;
503}
504
Mathias Agopianfb87e542012-01-30 18:20:52 -0800505void egl_display_t::loseCurrent(egl_context_t * cur_c)
506{
507 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800508 egl_display_t* display = cur_c->getDisplay();
509 if (display) {
510 display->loseCurrentImpl(cur_c);
511 }
512 }
513}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800514
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800515void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
516{
517 // by construction, these are either 0 or valid (possibly terminated)
518 // it should be impossible for these to be invalid
519 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700520 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
521 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800522
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800523 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800524 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800525 cur_c->onLooseCurrent();
526
Mathias Agopianfb87e542012-01-30 18:20:52 -0800527 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800528
529 // This cannot be called with the lock held because it might end-up
530 // calling back into EGL (in particular when a surface is destroyed
531 // it calls ANativeWindow::disconnect
532 _cur_c.release();
533 _cur_r.release();
534 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800535}
536
537EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700538 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800539 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
540{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800541 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800542
543 // by construction, these are either 0 or valid (possibly terminated)
544 // it should be impossible for these to be invalid
545 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700546 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
547 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800548
549 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800550 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800551 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800552 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800553 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800554 if (result == EGL_TRUE) {
555 c->onMakeCurrent(draw, read);
556 }
557 } else {
558 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800559 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800560 if (result == EGL_TRUE) {
561 cur_c->onLooseCurrent();
562 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800563 }
564 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800565
566 if (result == EGL_TRUE) {
567 // This cannot be called with the lock held because it might end-up
568 // calling back into EGL (in particular when a surface is destroyed
569 // it calls ANativeWindow::disconnect
570 _cur_c.release();
571 _cur_r.release();
572 _cur_d.release();
573 }
574
Mathias Agopianfb87e542012-01-30 18:20:52 -0800575 return result;
576}
Mathias Agopian518ec112011-05-13 16:21:08 -0700577
Jesse Hallc2e41222013-08-08 13:40:22 -0700578bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
579 if (!nameLen) {
580 nameLen = strlen(name);
581 }
Mathias Agopian65421432017-03-08 11:49:05 -0800582 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700583}
584
Jesse Halla0fef1c2012-04-17 12:02:26 -0700585// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700586}; // namespace android
587// ----------------------------------------------------------------------------