blob: a58e87ca3e724bd119fbc47ed79de52435eb8649 [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);
Courtney Goeltzenleuchter559b4332020-03-06 11:12:19 -0700161
162 // Context virtualization is only available on GL back-end.
163 // Needed to support threading with GL back-end
164 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
165 attrs.push_back(EGL_FALSE);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600166 break;
167 default:
Courtney Goeltzenleuchter559b4332020-03-06 11:12:19 -0700168 ALOGE("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600169 break;
170 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600171
172 return true;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600173}
174
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600175static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
176 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600177 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600178 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600179
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600180 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600181 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600182 if (attrib_list) {
183 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
184 attrs.push_back(attr[0]);
185 attrs.push_back(attr[1]);
186 }
187 }
188
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600189 if (!addAnglePlatformAttributes(cnx, attrs)) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600190 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
191 *error = EGL_BAD_PARAMETER;
192 return EGL_NO_DISPLAY;
193 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600194 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600195
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600196 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
197 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
198 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600199 if (dpy == EGL_NO_DISPLAY) {
200 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600201 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700202 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600203 ALOGE("initializeAnglePlatform failed!");
204 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600205 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600206 } else {
207 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
208 display);
209 }
210
211 return dpy;
212}
213
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600214EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
215 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800216 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800217 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700218
219 // get our driver loader
220 Loader& loader(Loader::getInstance());
221
Mathias Agopianada798b2012-02-13 17:09:30 -0800222 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700223 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600224 EGLDisplay dpy = EGL_NO_DISPLAY;
225
Charlie Lao840bd532020-01-16 17:34:37 -0800226 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600227 EGLint error;
228 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
229 if (error != EGL_NONE) {
230 return setError(error, dpy);
231 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600232 }
233 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600234 // NOTE: eglGetPlatformDisplay with a empty attribute list
235 // behaves the same as eglGetDisplay
236 if (cnx->egl.eglGetPlatformDisplay) {
237 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
238 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800239 }
240
241 // It is possible that eglGetPlatformDisplay does not have a
242 // working implementation for Android platform; in that case,
243 // one last fallback to eglGetDisplay
244 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600245 if (attrib_list) {
246 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
247 }
248 dpy = cnx->egl.eglGetDisplay(display);
249 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600250 }
251
Mathias Agopianada798b2012-02-13 17:09:30 -0800252 disp.dpy = dpy;
253 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600254 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700255 }
256 }
257
258 return EGLDisplay(uintptr_t(display) + 1U);
259}
260
261EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
262
Mathias Agopian65421432017-03-08 11:49:05 -0800263 { // scope for refLock
264 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800265 refs++;
266 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600267 // We don't know what to report until we know what the
268 // driver supports. Make sure we are initialized before
269 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800270 while(!eglIsInitialized) {
271 refCond.wait(_l);
272 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600273 egl_connection_t* const cnx = &gEGLImpl;
274
275 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
276 // changing the behavior from the past where we always advertise
277 // version 1.4. May need to check that revision is valid
278 // before using cnx->major & cnx->minor
279 if (major != nullptr) *major = cnx->major;
280 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800281 return EGL_TRUE;
282 }
Mathias Agopian65421432017-03-08 11:49:05 -0800283 while(eglIsInitialized) {
284 refCond.wait(_l);
285 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800286 }
287
Mathias Agopian65421432017-03-08 11:49:05 -0800288 { // scope for lock
289 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800290
Michael Lentine54466bc2015-01-27 09:01:03 -0800291 setGLHooksThreadSpecific(&gHooksNoContext);
292
293 // initialize each EGL and
294 // build our own extension string first, based on the extension we know
295 // and the extension supported by our client implementation
296
297 egl_connection_t* const cnx = &gEGLImpl;
298 cnx->major = -1;
299 cnx->minor = -1;
300 if (cnx->dso) {
301 EGLDisplay idpy = disp.dpy;
302 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
303 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
304 // idpy, cnx->major, cnx->minor, cnx);
305
306 // display is now initialized
307 disp.state = egl_display_t::INITIALIZED;
308
309 // get the query-strings for this display for each implementation
310 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
311 EGL_VENDOR);
312 disp.queryString.version = cnx->egl.eglQueryString(idpy,
313 EGL_VERSION);
314 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
315 EGL_EXTENSIONS);
316 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
317 EGL_CLIENT_APIS);
318
319 } else {
320 ALOGW("eglInitialize(%p) failed (%s)", idpy,
321 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
322 }
323 }
324
Sean Callananad9061c2019-03-12 14:07:23 -0700325 if (cnx->minor == 5) {
326 // full list in egl_entries.in
327 if (!cnx->egl.eglCreateImage ||
328 !cnx->egl.eglDestroyImage ||
329 !cnx->egl.eglGetPlatformDisplay ||
330 !cnx->egl.eglCreatePlatformWindowSurface ||
331 !cnx->egl.eglCreatePlatformPixmapSurface ||
332 !cnx->egl.eglCreateSync ||
333 !cnx->egl.eglDestroySync ||
334 !cnx->egl.eglClientWaitSync ||
335 !cnx->egl.eglGetSyncAttrib ||
336 !cnx->egl.eglWaitSync) {
337 ALOGE("Driver indicates EGL 1.5 support, but does not have "
338 "a critical API");
339 cnx->minor = 4;
340 }
341 }
342
Michael Lentine54466bc2015-01-27 09:01:03 -0800343 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800344 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600345 mVersionString.clear();
346 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600347 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600348 if ((cnx->major == 1) && (cnx->minor == 5)) {
349 mVersionString = sVersionString15;
350 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600351 }
352 if (mVersionString.empty()) {
353 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
354 mVersionString = sVersionString14;
355 }
Mathias Agopian65421432017-03-08 11:49:05 -0800356 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800357
Mathias Agopian65421432017-03-08 11:49:05 -0800358 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600359
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700360 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
361
362 // Note: CDD requires that devices supporting wide color and/or HDR color also support
363 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600364 bool wideColorBoardConfig =
365 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
366 false);
367
368 // Add wide-color extensions if device can support wide-color
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700369 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600370 mExtensionString.append(
371 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800372 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
373 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600374 }
375
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700376 bool hasHdrBoardConfig =
377 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
378
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700379 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700380 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
381 // Typically that means there is an HDR capable display attached, but could be
382 // support for attaching an HDR display. In either case, advertise support for
383 // HDR color spaces.
384 mExtensionString.append(
385 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
386 }
387
Michael Lentine54466bc2015-01-27 09:01:03 -0800388 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800389 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400390 // length of the extension name
391 size_t len = strcspn(start, " ");
392 if (len) {
393 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800394 const std::string ext(start, len);
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -0700395 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
396 // some Android P vendor partitions this extension under the draft KHR name,
397 // but during Khronos review it was decided to demote it to EXT.
398 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
399 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
400 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
401 }
Mathias Agopian65421432017-03-08 11:49:05 -0800402 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400403 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800404 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400405 // advance to the next extension name, skipping the space.
406 start += len;
407 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800408 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400409 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800410
411 egl_cache_t::get()->initialize(this);
412
413 char value[PROPERTY_VALUE_MAX];
414 property_get("debug.egl.finish", value, "0");
415 if (atoi(value)) {
416 finishOnSwap = true;
417 }
418
419 property_get("debug.egl.traceGpuCompletion", value, "0");
420 if (atoi(value)) {
421 traceGpuCompletion = true;
422 }
423
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600424 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
425 // changing the behavior from the past where we always advertise
426 // version 1.4. May need to check that revision is valid
427 // before using cnx->major & cnx->minor
428 if (major != nullptr) *major = cnx->major;
429 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700430 }
431
Mathias Agopian65421432017-03-08 11:49:05 -0800432 { // scope for refLock
433 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800434 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800435 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700436 }
437
Mathias Agopian7773c432012-02-13 20:06:08 -0800438 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700439}
440
441EGLBoolean egl_display_t::terminate() {
442
Mathias Agopian65421432017-03-08 11:49:05 -0800443 { // scope for refLock
444 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800445 if (refs == 0) {
446 /*
447 * From the EGL spec (3.2):
448 * "Termination of a display that has already been terminated,
449 * (...), is allowed, but the only effect of such a call is
450 * to return EGL_TRUE (...)
451 */
452 return EGL_TRUE;
453 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700454
Michael Lentine54466bc2015-01-27 09:01:03 -0800455 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700456 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800457 if (refs > 0) {
458 return EGL_TRUE;
459 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700460 }
461
462 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800463
Mathias Agopian65421432017-03-08 11:49:05 -0800464 { // scope for lock
465 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800466
467 egl_connection_t* const cnx = &gEGLImpl;
468 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600469 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800470 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700471 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600472 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800473 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
474 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
475 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
476 }
477 // REVISIT: it's unclear what to do if eglTerminate() fails
478 disp.state = egl_display_t::TERMINATED;
479 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700480 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800481
Michael Lentine54466bc2015-01-27 09:01:03 -0800482 // Reset the extension string since it will be regenerated if we get
483 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800484 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800485
486 // Mark all objects remaining in the list as terminated, unless
487 // there are no reference to them, it which case, we're free to
488 // delete them.
489 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800490 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800491 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800492 o->destroy();
493 }
494
495 // this marks all object handles are "terminated"
496 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700497 }
498
Mathias Agopian65421432017-03-08 11:49:05 -0800499 { // scope for refLock
500 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800501 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800502 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700503 }
504
Mathias Agopian518ec112011-05-13 16:21:08 -0700505 return res;
506}
507
Mathias Agopianfb87e542012-01-30 18:20:52 -0800508void egl_display_t::loseCurrent(egl_context_t * cur_c)
509{
510 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800511 egl_display_t* display = cur_c->getDisplay();
512 if (display) {
513 display->loseCurrentImpl(cur_c);
514 }
515 }
516}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800517
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800518void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
519{
520 // by construction, these are either 0 or valid (possibly terminated)
521 // it should be impossible for these to be invalid
522 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700523 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
524 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800525
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800526 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800527 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800528 cur_c->onLooseCurrent();
529
Mathias Agopianfb87e542012-01-30 18:20:52 -0800530 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800531
532 // This cannot be called with the lock held because it might end-up
533 // calling back into EGL (in particular when a surface is destroyed
534 // it calls ANativeWindow::disconnect
535 _cur_c.release();
536 _cur_r.release();
537 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800538}
539
540EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700541 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800542 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
543{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800544 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800545
546 // by construction, these are either 0 or valid (possibly terminated)
547 // it should be impossible for these to be invalid
548 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700549 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
550 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800551
552 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800553 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800554 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800555 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800556 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800557 if (result == EGL_TRUE) {
558 c->onMakeCurrent(draw, read);
559 }
560 } else {
561 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800562 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800563 if (result == EGL_TRUE) {
564 cur_c->onLooseCurrent();
565 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800566 }
567 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800568
569 if (result == EGL_TRUE) {
570 // This cannot be called with the lock held because it might end-up
571 // calling back into EGL (in particular when a surface is destroyed
572 // it calls ANativeWindow::disconnect
573 _cur_c.release();
574 _cur_r.release();
575 _cur_d.release();
576 }
577
Mathias Agopianfb87e542012-01-30 18:20:52 -0800578 return result;
579}
Mathias Agopian518ec112011-05-13 16:21:08 -0700580
Jesse Hallc2e41222013-08-08 13:40:22 -0700581bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
582 if (!nameLen) {
583 nameLen = strlen(name);
584 }
Mathias Agopian65421432017-03-08 11:49:05 -0800585 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700586}
587
Jesse Halla0fef1c2012-04-17 12:02:26 -0700588// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700589}; // namespace android
590// ----------------------------------------------------------------------------