blob: a288c21f314c9779bf177360e60775a6494f0770 [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
Woody Chowa9550f32020-08-31 14:34:12 +090077std::map<EGLDisplay, std::unique_ptr<egl_display_t>> egl_display_t::displayMap;
78std::mutex egl_display_t::displayMapLock;
Mathias Agopian518ec112011-05-13 16:21:08 -070079
Yiwei Zhang8af03062020-08-12 21:28:15 -070080egl_display_t::egl_display_t()
81 : magic('_dpy'),
82 finishOnSwap(false),
83 traceGpuCompletion(false),
84 refs(0),
85 eglIsInitialized(false) {}
Mathias Agopian518ec112011-05-13 16:21:08 -070086
87egl_display_t::~egl_display_t() {
88 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080089 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070090}
91
92egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080093 if (uintptr_t(dpy) == 0) {
94 return nullptr;
95 }
96
Woody Chowa9550f32020-08-31 14:34:12 +090097 const std::lock_guard<std::mutex> lock(displayMapLock);
98 auto search = displayMap.find(dpy);
99 if (search == displayMap.end() || !search->second->isValid()) {
Jesse Halld6e99462016-09-28 11:26:57 -0700100 return nullptr;
101 }
Woody Chowa9550f32020-08-31 14:34:12 +0900102 return search->second.get();
Mathias Agopian518ec112011-05-13 16:21:08 -0700103}
104
105void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800106 std::lock_guard<std::mutex> _l(lock);
107 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700108}
109
Mathias Agopian5b287a62011-05-16 18:58:55 -0700110void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800111 std::lock_guard<std::mutex> _l(lock);
112 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700113}
114
Mathias Agopianf0480de2011-11-13 20:50:07 -0800115bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800116 std::lock_guard<std::mutex> _l(lock);
117 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800118 if (object->getDisplay() == this) {
119 object->incRef();
120 return true;
121 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700122 }
123 return false;
124}
125
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600126EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
127 const EGLAttrib* attrib_list) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700128 if (uintptr_t(disp) >= NUM_DISPLAYS) return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700129
Woody Chowa9550f32020-08-31 14:34:12 +0900130 return getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700131}
132
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600133static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
134 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600135 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600136 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600137
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600138 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600139 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600140 if (attrib_list) {
141 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
142 attrs.push_back(attr[0]);
143 attrs.push_back(attr[1]);
144 }
145 }
146
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600147 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
148 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
149
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600150 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400151 attrs.push_back(base::GetBoolProperty("debug.angle.validation", false));
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600152
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600153 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600154
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600155 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
156 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
157 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600158 if (dpy == EGL_NO_DISPLAY) {
159 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600160 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700161 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600162 ALOGE("initializeAnglePlatform failed!");
163 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600164 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600165 } else {
166 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
167 display);
168 }
169
170 return dpy;
171}
172
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600173EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
174 const EGLAttrib* attrib_list) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800175 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700176
177 // get our driver loader
178 Loader& loader(Loader::getInstance());
179
Mathias Agopianada798b2012-02-13 17:09:30 -0800180 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700181 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600182 EGLDisplay dpy = EGL_NO_DISPLAY;
183
Charlie Lao840bd532020-01-16 17:34:37 -0800184 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600185 EGLint error;
186 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
187 if (error != EGL_NONE) {
188 return setError(error, dpy);
189 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600190 }
191 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600192 // NOTE: eglGetPlatformDisplay with a empty attribute list
193 // behaves the same as eglGetDisplay
194 if (cnx->egl.eglGetPlatformDisplay) {
195 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
196 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800197 }
198
199 // It is possible that eglGetPlatformDisplay does not have a
200 // working implementation for Android platform; in that case,
201 // one last fallback to eglGetDisplay
Yiwei Zhang8af03062020-08-12 21:28:15 -0700202 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600203 if (attrib_list) {
204 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
205 }
206 dpy = cnx->egl.eglGetDisplay(display);
207 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600208 }
209
Mathias Agopianada798b2012-02-13 17:09:30 -0800210 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600211 loader.close(cnx);
Woody Chowa9550f32020-08-31 14:34:12 +0900212 } else {
213 const std::lock_guard<std::mutex> lock(displayMapLock);
214 if (displayMap.find(dpy) == displayMap.end()) {
215 auto d = std::make_unique<egl_display_t>();
216 d->disp.dpy = dpy;
217 displayMap[dpy] = std::move(d);
218 }
219 return dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700220 }
221 }
222
Woody Chowa9550f32020-08-31 14:34:12 +0900223 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700224}
225
Yiwei Zhang8af03062020-08-12 21:28:15 -0700226EGLBoolean egl_display_t::initialize(EGLint* major, EGLint* minor) {
Mathias Agopian65421432017-03-08 11:49:05 -0800227 { // scope for refLock
228 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800229 refs++;
230 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600231 // We don't know what to report until we know what the
232 // driver supports. Make sure we are initialized before
233 // returning the version info.
Yiwei Zhang8af03062020-08-12 21:28:15 -0700234 while (!eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800235 refCond.wait(_l);
236 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600237 egl_connection_t* const cnx = &gEGLImpl;
238
239 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
240 // changing the behavior from the past where we always advertise
241 // version 1.4. May need to check that revision is valid
242 // before using cnx->major & cnx->minor
243 if (major != nullptr) *major = cnx->major;
244 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800245 return EGL_TRUE;
246 }
Yiwei Zhang8af03062020-08-12 21:28:15 -0700247 while (eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800248 refCond.wait(_l);
249 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800250 }
251
Mathias Agopian65421432017-03-08 11:49:05 -0800252 { // scope for lock
253 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800254
Michael Lentine54466bc2015-01-27 09:01:03 -0800255 setGLHooksThreadSpecific(&gHooksNoContext);
256
257 // initialize each EGL and
258 // build our own extension string first, based on the extension we know
259 // and the extension supported by our client implementation
260
261 egl_connection_t* const cnx = &gEGLImpl;
262 cnx->major = -1;
263 cnx->minor = -1;
264 if (cnx->dso) {
265 EGLDisplay idpy = disp.dpy;
266 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700267 // ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
Michael Lentine54466bc2015-01-27 09:01:03 -0800268 // idpy, cnx->major, cnx->minor, cnx);
269
270 // display is now initialized
271 disp.state = egl_display_t::INITIALIZED;
272
273 // get the query-strings for this display for each implementation
Yiwei Zhang8af03062020-08-12 21:28:15 -0700274 disp.queryString.vendor = cnx->egl.eglQueryString(idpy, EGL_VENDOR);
275 disp.queryString.version = cnx->egl.eglQueryString(idpy, EGL_VERSION);
276 disp.queryString.extensions = cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
277 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
Michael Lentine54466bc2015-01-27 09:01:03 -0800278
279 } else {
280 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700281 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800282 }
283 }
284
Sean Callananad9061c2019-03-12 14:07:23 -0700285 if (cnx->minor == 5) {
286 // full list in egl_entries.in
Yiwei Zhang8af03062020-08-12 21:28:15 -0700287 if (!cnx->egl.eglCreateImage || !cnx->egl.eglDestroyImage ||
288 !cnx->egl.eglGetPlatformDisplay || !cnx->egl.eglCreatePlatformWindowSurface ||
289 !cnx->egl.eglCreatePlatformPixmapSurface || !cnx->egl.eglCreateSync ||
290 !cnx->egl.eglDestroySync || !cnx->egl.eglClientWaitSync ||
291 !cnx->egl.eglGetSyncAttrib || !cnx->egl.eglWaitSync) {
Sean Callananad9061c2019-03-12 14:07:23 -0700292 ALOGE("Driver indicates EGL 1.5 support, but does not have "
293 "a critical API");
294 cnx->minor = 4;
295 }
296 }
297
Michael Lentine54466bc2015-01-27 09:01:03 -0800298 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800299 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600300 mVersionString.clear();
301 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600302 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600303 if ((cnx->major == 1) && (cnx->minor == 5)) {
304 mVersionString = sVersionString15;
305 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600306 }
307 if (mVersionString.empty()) {
308 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
309 mVersionString = sVersionString14;
310 }
Mathias Agopian65421432017-03-08 11:49:05 -0800311 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800312
Mathias Agopian65421432017-03-08 11:49:05 -0800313 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600314
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700315 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
316
317 // Note: CDD requires that devices supporting wide color and/or HDR color also support
318 // the EGL_KHR_gl_colorspace extension.
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900319 bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600320
321 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700322 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600323 mExtensionString.append(
324 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800325 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
326 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600327 }
328
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900329 bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700330
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700331 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700332 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
333 // Typically that means there is an HDR capable display attached, but could be
334 // support for attaching an HDR display. In either case, advertise support for
335 // HDR color spaces.
336 mExtensionString.append(
337 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
338 }
339
Michael Lentine54466bc2015-01-27 09:01:03 -0800340 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800341 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400342 // length of the extension name
343 size_t len = strcspn(start, " ");
344 if (len) {
345 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800346 const std::string ext(start, len);
Krzysztof Kosińskidbccd222019-05-16 14:10:25 -0700347 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
348 // some Android P vendor partitions this extension under the draft KHR name,
349 // but during Khronos review it was decided to demote it to EXT.
350 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
351 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
352 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
353 }
Mathias Agopian65421432017-03-08 11:49:05 -0800354 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400355 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800356 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400357 // advance to the next extension name, skipping the space.
358 start += len;
359 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800360 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400361 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800362
363 egl_cache_t::get()->initialize(this);
364
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400365 finishOnSwap = base::GetBoolProperty("debug.egl.finish", false);
366 traceGpuCompletion = base::GetBoolProperty("debug.egl.traceGpuCompletion", false);
Michael Lentine54466bc2015-01-27 09:01:03 -0800367
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600368 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
369 // changing the behavior from the past where we always advertise
370 // version 1.4. May need to check that revision is valid
371 // before using cnx->major & cnx->minor
372 if (major != nullptr) *major = cnx->major;
373 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700374 }
375
Mathias Agopian65421432017-03-08 11:49:05 -0800376 { // scope for refLock
377 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800378 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800379 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700380 }
381
Mathias Agopian7773c432012-02-13 20:06:08 -0800382 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700383}
384
385EGLBoolean egl_display_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800386 { // scope for refLock
387 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800388 if (refs == 0) {
389 /*
390 * From the EGL spec (3.2):
391 * "Termination of a display that has already been terminated,
392 * (...), is allowed, but the only effect of such a call is
393 * to return EGL_TRUE (...)
394 */
395 return EGL_TRUE;
396 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700397
Michael Lentine54466bc2015-01-27 09:01:03 -0800398 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700399 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800400 if (refs > 0) {
401 return EGL_TRUE;
402 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700403 }
404
405 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800406
Mathias Agopian65421432017-03-08 11:49:05 -0800407 { // scope for lock
408 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800409
410 egl_connection_t* const cnx = &gEGLImpl;
411 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600412 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800413 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700414 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600415 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800416 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
417 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700418 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800419 }
420 // REVISIT: it's unclear what to do if eglTerminate() fails
421 disp.state = egl_display_t::TERMINATED;
422 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700423 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800424
Michael Lentine54466bc2015-01-27 09:01:03 -0800425 // Reset the extension string since it will be regenerated if we get
426 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800427 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800428
429 // Mark all objects remaining in the list as terminated, unless
430 // there are no reference to them, it which case, we're free to
431 // delete them.
432 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800433 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800434 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800435 o->destroy();
436 }
437
438 // this marks all object handles are "terminated"
439 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 }
441
Mathias Agopian65421432017-03-08 11:49:05 -0800442 { // scope for refLock
443 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800444 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800445 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700446 }
447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return res;
449}
450
Yiwei Zhang8af03062020-08-12 21:28:15 -0700451void egl_display_t::loseCurrent(egl_context_t* cur_c) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800452 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800453 egl_display_t* display = cur_c->getDisplay();
454 if (display) {
455 display->loseCurrentImpl(cur_c);
456 }
457 }
458}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800459
Yiwei Zhang8af03062020-08-12 21:28:15 -0700460void egl_display_t::loseCurrentImpl(egl_context_t* cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800461 // by construction, these are either 0 or valid (possibly terminated)
462 // it should be impossible for these to be invalid
463 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700464 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
465 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800466
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800467 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800468 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800469 cur_c->onLooseCurrent();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800470 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800471
472 // This cannot be called with the lock held because it might end-up
473 // calling back into EGL (in particular when a surface is destroyed
474 // it calls ANativeWindow::disconnect
475 _cur_c.release();
476 _cur_r.release();
477 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800478}
479
Yiwei Zhang8af03062020-08-12 21:28:15 -0700480EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw,
481 EGLSurface read, EGLContext /*ctx*/, EGLSurface impl_draw,
482 EGLSurface impl_read, EGLContext impl_ctx) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800483 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800484
485 // by construction, these are either 0 or valid (possibly terminated)
486 // it should be impossible for these to be invalid
487 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700488 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
489 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800490
491 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800492 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800493 if (c) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700494 result = c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800495 if (result == EGL_TRUE) {
496 c->onMakeCurrent(draw, read);
497 }
498 } else {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700499 result = cur_c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800500 if (result == EGL_TRUE) {
501 cur_c->onLooseCurrent();
502 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800503 }
504 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800505
506 if (result == EGL_TRUE) {
507 // This cannot be called with the lock held because it might end-up
508 // calling back into EGL (in particular when a surface is destroyed
509 // it calls ANativeWindow::disconnect
510 _cur_c.release();
511 _cur_r.release();
512 _cur_d.release();
513 }
514
Mathias Agopianfb87e542012-01-30 18:20:52 -0800515 return result;
516}
Mathias Agopian518ec112011-05-13 16:21:08 -0700517
Jesse Hallc2e41222013-08-08 13:40:22 -0700518bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
519 if (!nameLen) {
520 nameLen = strlen(name);
521 }
Mathias Agopian65421432017-03-08 11:49:05 -0800522 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700523}
524
Yiwei Zhang8af03062020-08-12 21:28:15 -0700525egl_display_t* validate_display(EGLDisplay dpy) {
526 egl_display_t* const dp = get_display(dpy);
527 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)nullptr);
528 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)nullptr);
529
530 return dp;
531}
532
533egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx) {
534 *outCnx = nullptr;
535 egl_display_t* dp = validate_display(dpy);
536 if (!dp) return dp;
537 *outCnx = &gEGLImpl;
538 if ((*outCnx)->dso == nullptr) {
539 return setError(EGL_BAD_CONFIG, (egl_display_t*)nullptr);
540 }
541 return dp;
542}
543
Mathias Agopian518ec112011-05-13 16:21:08 -0700544}; // namespace android