blob: 07ec327ab3dbbba356d6276df7c6eb547419a493 [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 Hall1508ae62017-01-19 17:43:26 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hallb29e5e82012-04-04 16:53:42 -070018
Mathias Agopian311b4792017-02-28 15:00:49 -080019#include "egl_display.h"
Mathias Agopian4b9511c2011-11-13 23:52:47 -080020
Yiwei Zhang8af003e2020-06-04 14:11:30 -070021#include <SurfaceFlingerProperties.h>
22#include <android-base/properties.h>
23#include <android/dlext.h>
24#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
25#include <configstore/Utils.h>
26#include <dlfcn.h>
27#include <graphicsenv/GraphicsEnv.h>
28
Mathias Agopian39c24a22013-04-04 23:17:56 -070029#include "../egl_impl.h"
Yiwei Zhang8af003e2020-06-04 14:11:30 -070030#include "EGL/eglext_angle.h"
Tobin Ehlis96a184d2018-07-18 16:14:07 -060031#include "Loader.h"
32#include "egl_angle_platform.h"
Jamie Gennisaca51c02011-11-03 17:42:43 -070033#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070034#include "egl_object.h"
35#include "egl_tls.h"
Yiwei Zhang8af003e2020-06-04 14:11:30 -070036#include "private/EGL/display.h"
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060037
38using namespace android::hardware::configstore;
39using namespace android::hardware::configstore::V1_0;
40
Mathias Agopian518ec112011-05-13 16:21:08 -070041namespace android {
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Yiwei Zhang8af03062020-08-12 21:28:15 -070043static const char* const sVendorString = "Android";
44static const char* const sVersionString14 = "1.4 Android META-EGL";
45static const char* const sVersionString15 = "1.5 Android META-EGL";
46static const char* const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080047
Yiwei Zhang8af03062020-08-12 21:28:15 -070048extern const char* const gBuiltinExtensionString;
49extern const char* const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080050
Yiwei Zhang8af03062020-08-12 21:28:15 -070051extern void setGLHooksThreadSpecific(gl_hooks_t const* value);
Mathias Agopian518ec112011-05-13 16:21:08 -070052
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070053bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070054 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070055 if (!nameLen) {
56 nameLen = strlen(name);
57 }
Kalle Raita7804aa22016-04-18 16:03:37 -070058 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
59 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
60 return true;
61 }
Jesse Hallc2e41222013-08-08 13:40:22 -070062 }
63 }
64 return false;
65}
66
Krzysztof Kosińskidbccd222019-05-16 14:10:25 -070067bool needsAndroidPEglMitigation() {
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040068 static const int32_t vndk_version = base::GetIntProperty("ro.vndk.version", -1);
Krzysztof Kosińskidbccd222019-05-16 14:10:25 -070069 return vndk_version <= 28;
70}
71
Mathias Agopianb7f9a242017-03-08 22:29:31 -080072int egl_get_init_count(EGLDisplay dpy) {
73 egl_display_t* eglDisplay = egl_display_t::get(dpy);
74 return eglDisplay ? eglDisplay->getRefsCount() : 0;
75}
76
Mathias Agopian518ec112011-05-13 16:21:08 -070077egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
78
Yiwei Zhang8af03062020-08-12 21:28:15 -070079egl_display_t::egl_display_t()
80 : magic('_dpy'),
81 finishOnSwap(false),
82 traceGpuCompletion(false),
83 refs(0),
84 eglIsInitialized(false) {}
Mathias Agopian518ec112011-05-13 16:21:08 -070085
86egl_display_t::~egl_display_t() {
87 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080088 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070089}
90
91egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080092 if (uintptr_t(dpy) == 0) {
93 return nullptr;
94 }
95
Yiwei Zhang8af03062020-08-12 21:28:15 -070096 uintptr_t index = uintptr_t(dpy) - 1U;
Jesse Halld6e99462016-09-28 11:26:57 -070097 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
98 return nullptr;
99 }
100 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700101}
102
103void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800104 std::lock_guard<std::mutex> _l(lock);
105 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700106}
107
Mathias Agopian5b287a62011-05-16 18:58:55 -0700108void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800109 std::lock_guard<std::mutex> _l(lock);
110 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700111}
112
Mathias Agopianf0480de2011-11-13 20:50:07 -0800113bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800114 std::lock_guard<std::mutex> _l(lock);
115 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800116 if (object->getDisplay() == this) {
117 object->incRef();
118 return true;
119 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 }
121 return false;
122}
123
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600124EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
125 const EGLAttrib* attrib_list) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700126 if (uintptr_t(disp) >= NUM_DISPLAYS) return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700127
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600128 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700129}
130
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600131static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
132 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600133 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600134 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600135
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600136 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600137 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600138 if (attrib_list) {
139 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
140 attrs.push_back(attr[0]);
141 attrs.push_back(attr[1]);
142 }
143 }
144
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600145 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
146 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
147
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600148 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400149 attrs.push_back(base::GetBoolProperty("debug.angle.validation", false));
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600150
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600151 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600152
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600153 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
154 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
155 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600156 if (dpy == EGL_NO_DISPLAY) {
157 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600158 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700159 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600160 ALOGE("initializeAnglePlatform failed!");
161 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600162 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600163 } else {
164 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
165 display);
166 }
167
168 return dpy;
169}
170
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600171EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
172 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800173 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800174 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700175
176 // get our driver loader
177 Loader& loader(Loader::getInstance());
178
Mathias Agopianada798b2012-02-13 17:09:30 -0800179 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700180 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600181 EGLDisplay dpy = EGL_NO_DISPLAY;
182
Charlie Lao840bd532020-01-16 17:34:37 -0800183 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600184 EGLint error;
185 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
186 if (error != EGL_NONE) {
187 return setError(error, dpy);
188 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600189 }
190 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600191 // NOTE: eglGetPlatformDisplay with a empty attribute list
192 // behaves the same as eglGetDisplay
193 if (cnx->egl.eglGetPlatformDisplay) {
194 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
195 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800196 }
197
198 // It is possible that eglGetPlatformDisplay does not have a
199 // working implementation for Android platform; in that case,
200 // one last fallback to eglGetDisplay
Yiwei Zhang8af03062020-08-12 21:28:15 -0700201 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600202 if (attrib_list) {
203 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
204 }
205 dpy = cnx->egl.eglGetDisplay(display);
206 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600207 }
208
Mathias Agopianada798b2012-02-13 17:09:30 -0800209 disp.dpy = dpy;
210 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600211 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700212 }
213 }
214
215 return EGLDisplay(uintptr_t(display) + 1U);
216}
217
Yiwei Zhang8af03062020-08-12 21:28:15 -0700218EGLBoolean egl_display_t::initialize(EGLint* major, EGLint* minor) {
Mathias Agopian65421432017-03-08 11:49:05 -0800219 { // scope for refLock
220 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800221 refs++;
222 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600223 // We don't know what to report until we know what the
224 // driver supports. Make sure we are initialized before
225 // returning the version info.
Yiwei Zhang8af03062020-08-12 21:28:15 -0700226 while (!eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800227 refCond.wait(_l);
228 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600229 egl_connection_t* const cnx = &gEGLImpl;
230
231 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
232 // changing the behavior from the past where we always advertise
233 // version 1.4. May need to check that revision is valid
234 // before using cnx->major & cnx->minor
235 if (major != nullptr) *major = cnx->major;
236 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800237 return EGL_TRUE;
238 }
Yiwei Zhang8af03062020-08-12 21:28:15 -0700239 while (eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800240 refCond.wait(_l);
241 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800242 }
243
Mathias Agopian65421432017-03-08 11:49:05 -0800244 { // scope for lock
245 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800246
Michael Lentine54466bc2015-01-27 09:01:03 -0800247 setGLHooksThreadSpecific(&gHooksNoContext);
248
249 // initialize each EGL and
250 // build our own extension string first, based on the extension we know
251 // and the extension supported by our client implementation
252
253 egl_connection_t* const cnx = &gEGLImpl;
254 cnx->major = -1;
255 cnx->minor = -1;
256 if (cnx->dso) {
257 EGLDisplay idpy = disp.dpy;
258 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700259 // ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
Michael Lentine54466bc2015-01-27 09:01:03 -0800260 // idpy, cnx->major, cnx->minor, cnx);
261
262 // display is now initialized
263 disp.state = egl_display_t::INITIALIZED;
264
265 // get the query-strings for this display for each implementation
Yiwei Zhang8af03062020-08-12 21:28:15 -0700266 disp.queryString.vendor = cnx->egl.eglQueryString(idpy, EGL_VENDOR);
267 disp.queryString.version = cnx->egl.eglQueryString(idpy, EGL_VERSION);
268 disp.queryString.extensions = cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
269 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
Michael Lentine54466bc2015-01-27 09:01:03 -0800270
271 } else {
272 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700273 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800274 }
275 }
276
Sean Callananad9061c2019-03-12 14:07:23 -0700277 if (cnx->minor == 5) {
278 // full list in egl_entries.in
Yiwei Zhang8af03062020-08-12 21:28:15 -0700279 if (!cnx->egl.eglCreateImage || !cnx->egl.eglDestroyImage ||
280 !cnx->egl.eglGetPlatformDisplay || !cnx->egl.eglCreatePlatformWindowSurface ||
281 !cnx->egl.eglCreatePlatformPixmapSurface || !cnx->egl.eglCreateSync ||
282 !cnx->egl.eglDestroySync || !cnx->egl.eglClientWaitSync ||
283 !cnx->egl.eglGetSyncAttrib || !cnx->egl.eglWaitSync) {
Sean Callananad9061c2019-03-12 14:07:23 -0700284 ALOGE("Driver indicates EGL 1.5 support, but does not have "
285 "a critical API");
286 cnx->minor = 4;
287 }
288 }
289
Michael Lentine54466bc2015-01-27 09:01:03 -0800290 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800291 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600292 mVersionString.clear();
293 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600294 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600295 if ((cnx->major == 1) && (cnx->minor == 5)) {
296 mVersionString = sVersionString15;
297 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600298 }
299 if (mVersionString.empty()) {
300 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
301 mVersionString = sVersionString14;
302 }
Mathias Agopian65421432017-03-08 11:49:05 -0800303 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800304
Mathias Agopian65421432017-03-08 11:49:05 -0800305 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600306
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700307 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
308
309 // Note: CDD requires that devices supporting wide color and/or HDR color also support
310 // the EGL_KHR_gl_colorspace extension.
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900311 bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600312
313 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700314 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600315 mExtensionString.append(
316 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800317 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
318 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600319 }
320
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900321 bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700322
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700323 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700324 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
325 // Typically that means there is an HDR capable display attached, but could be
326 // support for attaching an HDR display. In either case, advertise support for
327 // HDR color spaces.
328 mExtensionString.append(
329 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
330 }
331
Michael Lentine54466bc2015-01-27 09:01:03 -0800332 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800333 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400334 // length of the extension name
335 size_t len = strcspn(start, " ");
336 if (len) {
337 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800338 const std::string ext(start, len);
Krzysztof Kosińskidbccd222019-05-16 14:10:25 -0700339 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
340 // some Android P vendor partitions this extension under the draft KHR name,
341 // but during Khronos review it was decided to demote it to EXT.
342 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
343 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
344 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
345 }
Mathias Agopian65421432017-03-08 11:49:05 -0800346 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400347 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800348 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400349 // advance to the next extension name, skipping the space.
350 start += len;
351 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800352 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400353 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800354
355 egl_cache_t::get()->initialize(this);
356
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400357 finishOnSwap = base::GetBoolProperty("debug.egl.finish", false);
358 traceGpuCompletion = base::GetBoolProperty("debug.egl.traceGpuCompletion", false);
Michael Lentine54466bc2015-01-27 09:01:03 -0800359
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600360 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
361 // changing the behavior from the past where we always advertise
362 // version 1.4. May need to check that revision is valid
363 // before using cnx->major & cnx->minor
364 if (major != nullptr) *major = cnx->major;
365 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 }
367
Mathias Agopian65421432017-03-08 11:49:05 -0800368 { // scope for refLock
369 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800370 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800371 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 }
373
Mathias Agopian7773c432012-02-13 20:06:08 -0800374 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700375}
376
377EGLBoolean egl_display_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800378 { // scope for refLock
379 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800380 if (refs == 0) {
381 /*
382 * From the EGL spec (3.2):
383 * "Termination of a display that has already been terminated,
384 * (...), is allowed, but the only effect of such a call is
385 * to return EGL_TRUE (...)
386 */
387 return EGL_TRUE;
388 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700389
Michael Lentine54466bc2015-01-27 09:01:03 -0800390 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700391 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800392 if (refs > 0) {
393 return EGL_TRUE;
394 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700395 }
396
397 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800398
Mathias Agopian65421432017-03-08 11:49:05 -0800399 { // scope for lock
400 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800401
402 egl_connection_t* const cnx = &gEGLImpl;
403 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600404 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800405 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700406 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600407 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800408 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
409 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700410 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800411 }
412 // REVISIT: it's unclear what to do if eglTerminate() fails
413 disp.state = egl_display_t::TERMINATED;
414 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700415 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800416
Michael Lentine54466bc2015-01-27 09:01:03 -0800417 // Reset the extension string since it will be regenerated if we get
418 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800419 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800420
421 // Mark all objects remaining in the list as terminated, unless
422 // there are no reference to them, it which case, we're free to
423 // delete them.
424 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800425 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800426 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800427 o->destroy();
428 }
429
430 // this marks all object handles are "terminated"
431 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
433
Mathias Agopian65421432017-03-08 11:49:05 -0800434 { // scope for refLock
435 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800436 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800437 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700438 }
439
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 return res;
441}
442
Yiwei Zhang8af03062020-08-12 21:28:15 -0700443void egl_display_t::loseCurrent(egl_context_t* cur_c) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800444 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800445 egl_display_t* display = cur_c->getDisplay();
446 if (display) {
447 display->loseCurrentImpl(cur_c);
448 }
449 }
450}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800451
Yiwei Zhang8af03062020-08-12 21:28:15 -0700452void egl_display_t::loseCurrentImpl(egl_context_t* cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800453 // by construction, these are either 0 or valid (possibly terminated)
454 // it should be impossible for these to be invalid
455 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700456 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
457 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800458
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800459 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800460 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800461 cur_c->onLooseCurrent();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800462 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800463
464 // This cannot be called with the lock held because it might end-up
465 // calling back into EGL (in particular when a surface is destroyed
466 // it calls ANativeWindow::disconnect
467 _cur_c.release();
468 _cur_r.release();
469 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800470}
471
Yiwei Zhang8af03062020-08-12 21:28:15 -0700472EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw,
473 EGLSurface read, EGLContext /*ctx*/, EGLSurface impl_draw,
474 EGLSurface impl_read, EGLContext impl_ctx) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800475 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800476
477 // by construction, these are either 0 or valid (possibly terminated)
478 // it should be impossible for these to be invalid
479 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700480 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
481 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800482
483 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800484 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800485 if (c) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700486 result = c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800487 if (result == EGL_TRUE) {
488 c->onMakeCurrent(draw, read);
489 }
490 } else {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700491 result = cur_c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800492 if (result == EGL_TRUE) {
493 cur_c->onLooseCurrent();
494 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800495 }
496 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800497
498 if (result == EGL_TRUE) {
499 // This cannot be called with the lock held because it might end-up
500 // calling back into EGL (in particular when a surface is destroyed
501 // it calls ANativeWindow::disconnect
502 _cur_c.release();
503 _cur_r.release();
504 _cur_d.release();
505 }
506
Mathias Agopianfb87e542012-01-30 18:20:52 -0800507 return result;
508}
Mathias Agopian518ec112011-05-13 16:21:08 -0700509
Jesse Hallc2e41222013-08-08 13:40:22 -0700510bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
511 if (!nameLen) {
512 nameLen = strlen(name);
513 }
Mathias Agopian65421432017-03-08 11:49:05 -0800514 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700515}
516
Yiwei Zhang8af03062020-08-12 21:28:15 -0700517egl_display_t* validate_display(EGLDisplay dpy) {
518 egl_display_t* const dp = get_display(dpy);
519 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)nullptr);
520 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)nullptr);
521
522 return dp;
523}
524
525egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx) {
526 *outCnx = nullptr;
527 egl_display_t* dp = validate_display(dpy);
528 if (!dp) return dp;
529 *outCnx = &gEGLImpl;
530 if ((*outCnx)->dso == nullptr) {
531 return setError(EGL_BAD_CONFIG, (egl_display_t*)nullptr);
532 }
533 return dp;
534}
535
Mathias Agopian518ec112011-05-13 16:21:08 -0700536}; // namespace android