blob: 1ada33ed6e2a7e4d7e1026958c8ee5607156fe09 [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
Mathias Agopianb7f9a242017-03-08 22:29:31 -080067int egl_get_init_count(EGLDisplay dpy) {
68 egl_display_t* eglDisplay = egl_display_t::get(dpy);
69 return eglDisplay ? eglDisplay->getRefsCount() : 0;
70}
71
Woody Chowa9550f32020-08-31 14:34:12 +090072std::map<EGLDisplay, std::unique_ptr<egl_display_t>> egl_display_t::displayMap;
73std::mutex egl_display_t::displayMapLock;
Mathias Agopian518ec112011-05-13 16:21:08 -070074
Yiwei Zhang8af03062020-08-12 21:28:15 -070075egl_display_t::egl_display_t()
76 : magic('_dpy'),
77 finishOnSwap(false),
78 traceGpuCompletion(false),
79 refs(0),
80 eglIsInitialized(false) {}
Mathias Agopian518ec112011-05-13 16:21:08 -070081
82egl_display_t::~egl_display_t() {
83 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080084 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070085}
86
87egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080088 if (uintptr_t(dpy) == 0) {
89 return nullptr;
90 }
91
Woody Chowa9550f32020-08-31 14:34:12 +090092 const std::lock_guard<std::mutex> lock(displayMapLock);
93 auto search = displayMap.find(dpy);
94 if (search == displayMap.end() || !search->second->isValid()) {
Jesse Halld6e99462016-09-28 11:26:57 -070095 return nullptr;
96 }
Woody Chowa9550f32020-08-31 14:34:12 +090097 return search->second.get();
Mathias Agopian518ec112011-05-13 16:21:08 -070098}
99
100void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800101 std::lock_guard<std::mutex> _l(lock);
102 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700103}
104
Mathias Agopian5b287a62011-05-16 18:58:55 -0700105void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800106 std::lock_guard<std::mutex> _l(lock);
107 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700108}
109
Mathias Agopianf0480de2011-11-13 20:50:07 -0800110bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800111 std::lock_guard<std::mutex> _l(lock);
112 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800113 if (object->getDisplay() == this) {
114 object->incRef();
115 return true;
116 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700117 }
118 return false;
119}
120
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600121EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
122 const EGLAttrib* attrib_list) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700123 if (uintptr_t(disp) >= NUM_DISPLAYS) return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700124
Woody Chowa9550f32020-08-31 14:34:12 +0900125 return getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700126}
127
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600128static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
129 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600130 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600131 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600132
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600133 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600134 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600135 if (attrib_list) {
136 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
137 attrs.push_back(attr[0]);
138 attrs.push_back(attr[1]);
139 }
140 }
Peiyong Lin2f707e62020-09-26 13:52:10 -0700141 const auto& eglFeatures = GraphicsEnv::getInstance().getAngleEglFeatures();
142 std::vector<const char*> features;
143 if (eglFeatures.size() > 0) {
144 for (const std::string& eglFeature : eglFeatures) {
145 features.push_back(eglFeature.c_str());
146 }
147 features.push_back(0);
148 attrs.push_back(EGL_FEATURE_OVERRIDES_ENABLED_ANGLE);
149 attrs.push_back(reinterpret_cast<EGLAttrib>(features.data()));
150 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600151
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600152 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
153 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
154
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600155 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400156 attrs.push_back(base::GetBoolProperty("debug.angle.validation", false));
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600157
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600158 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600159
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600160 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
161 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
162 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600163 if (dpy == EGL_NO_DISPLAY) {
164 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600165 } else {
Peiyong Lin9fc4cf32023-11-03 05:18:53 +0000166 if (!angle::initializeAnglePlatform(dpy, cnx)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600167 ALOGE("initializeAnglePlatform failed!");
168 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600169 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600170 } else {
171 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
172 display);
173 }
174
175 return dpy;
176}
177
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600178EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
179 const EGLAttrib* attrib_list) {
Jesse Hall1508ae62017-01-19 17:43:26 -0800180 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700181
182 // get our driver loader
183 Loader& loader(Loader::getInstance());
184
Mathias Agopianada798b2012-02-13 17:09:30 -0800185 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700186 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600187 EGLDisplay dpy = EGL_NO_DISPLAY;
188
Peiyong Lin2f51e752023-06-15 22:35:14 +0000189 if (cnx->angleLoaded) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600190 EGLint error;
191 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
192 if (error != EGL_NONE) {
193 return setError(error, dpy);
194 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600195 }
196 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600197 // NOTE: eglGetPlatformDisplay with a empty attribute list
198 // behaves the same as eglGetDisplay
199 if (cnx->egl.eglGetPlatformDisplay) {
200 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
201 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800202 }
203
204 // It is possible that eglGetPlatformDisplay does not have a
205 // working implementation for Android platform; in that case,
206 // one last fallback to eglGetDisplay
Yiwei Zhang8af03062020-08-12 21:28:15 -0700207 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600208 if (attrib_list) {
209 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
210 }
211 dpy = cnx->egl.eglGetDisplay(display);
212 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600213 }
214
Mathias Agopianada798b2012-02-13 17:09:30 -0800215 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600216 loader.close(cnx);
Woody Chowa9550f32020-08-31 14:34:12 +0900217 } else {
218 const std::lock_guard<std::mutex> lock(displayMapLock);
219 if (displayMap.find(dpy) == displayMap.end()) {
220 auto d = std::make_unique<egl_display_t>();
221 d->disp.dpy = dpy;
222 displayMap[dpy] = std::move(d);
223 }
224 return dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700225 }
226 }
227
Woody Chowa9550f32020-08-31 14:34:12 +0900228 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700229}
230
Yiwei Zhang8af03062020-08-12 21:28:15 -0700231EGLBoolean egl_display_t::initialize(EGLint* major, EGLint* minor) {
Mathias Agopian65421432017-03-08 11:49:05 -0800232 { // scope for refLock
233 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800234 refs++;
235 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600236 // We don't know what to report until we know what the
237 // driver supports. Make sure we are initialized before
238 // returning the version info.
Yiwei Zhang8af03062020-08-12 21:28:15 -0700239 while (!eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800240 refCond.wait(_l);
241 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600242 egl_connection_t* const cnx = &gEGLImpl;
243
244 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
245 // changing the behavior from the past where we always advertise
246 // version 1.4. May need to check that revision is valid
247 // before using cnx->major & cnx->minor
248 if (major != nullptr) *major = cnx->major;
249 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800250 return EGL_TRUE;
251 }
Yiwei Zhang8af03062020-08-12 21:28:15 -0700252 while (eglIsInitialized) {
Mathias Agopian65421432017-03-08 11:49:05 -0800253 refCond.wait(_l);
254 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800255 }
256
Mathias Agopian65421432017-03-08 11:49:05 -0800257 { // scope for lock
258 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800259
Michael Lentine54466bc2015-01-27 09:01:03 -0800260 setGLHooksThreadSpecific(&gHooksNoContext);
261
262 // initialize each EGL and
263 // build our own extension string first, based on the extension we know
264 // and the extension supported by our client implementation
265
266 egl_connection_t* const cnx = &gEGLImpl;
267 cnx->major = -1;
268 cnx->minor = -1;
269 if (cnx->dso) {
270 EGLDisplay idpy = disp.dpy;
271 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700272 // ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
Michael Lentine54466bc2015-01-27 09:01:03 -0800273 // idpy, cnx->major, cnx->minor, cnx);
274
275 // display is now initialized
276 disp.state = egl_display_t::INITIALIZED;
277
278 // get the query-strings for this display for each implementation
Yiwei Zhang8af03062020-08-12 21:28:15 -0700279 disp.queryString.vendor = cnx->egl.eglQueryString(idpy, EGL_VENDOR);
280 disp.queryString.version = cnx->egl.eglQueryString(idpy, EGL_VERSION);
281 disp.queryString.extensions = cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
282 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
Michael Lentine54466bc2015-01-27 09:01:03 -0800283
284 } else {
285 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700286 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800287 }
288 }
289
Sean Callananad9061c2019-03-12 14:07:23 -0700290 if (cnx->minor == 5) {
291 // full list in egl_entries.in
Yiwei Zhang8af03062020-08-12 21:28:15 -0700292 if (!cnx->egl.eglCreateImage || !cnx->egl.eglDestroyImage ||
293 !cnx->egl.eglGetPlatformDisplay || !cnx->egl.eglCreatePlatformWindowSurface ||
294 !cnx->egl.eglCreatePlatformPixmapSurface || !cnx->egl.eglCreateSync ||
295 !cnx->egl.eglDestroySync || !cnx->egl.eglClientWaitSync ||
296 !cnx->egl.eglGetSyncAttrib || !cnx->egl.eglWaitSync) {
Sean Callananad9061c2019-03-12 14:07:23 -0700297 ALOGE("Driver indicates EGL 1.5 support, but does not have "
298 "a critical API");
299 cnx->minor = 4;
300 }
301 }
302
Michael Lentine54466bc2015-01-27 09:01:03 -0800303 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800304 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600305 mVersionString.clear();
306 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600307 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600308 if ((cnx->major == 1) && (cnx->minor == 5)) {
309 mVersionString = sVersionString15;
310 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600311 }
312 if (mVersionString.empty()) {
313 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
314 mVersionString = sVersionString14;
315 }
Mathias Agopian65421432017-03-08 11:49:05 -0800316 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800317
Mathias Agopian65421432017-03-08 11:49:05 -0800318 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600319
Yuxin Hu7fee4412023-03-14 01:43:05 +0000320 // b/269060366 Conditionally enabled EGL_ANDROID_get_frame_timestamps extension if the
321 // device's present timestamps are reliable (which may not be the case on emulators).
Peiyong Lin2f51e752023-06-15 22:35:14 +0000322 if (cnx->angleLoaded) {
Yuxin Hu7fee4412023-03-14 01:43:05 +0000323 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Yuxin Hu0bc64a82023-05-08 23:48:42 +0000324 mExtensionString.append("EGL_ANDROID_get_frame_timestamps ");
Yuxin Hu7fee4412023-03-14 01:43:05 +0000325 }
326 } else {
Yuxin Hu0bc64a82023-05-08 23:48:42 +0000327 mExtensionString.append("EGL_ANDROID_get_frame_timestamps ");
Yuxin Hu7fee4412023-03-14 01:43:05 +0000328 }
329
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700330 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
331
332 // Note: CDD requires that devices supporting wide color and/or HDR color also support
333 // the EGL_KHR_gl_colorspace extension.
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900334 bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600335
336 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700337 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600338 mExtensionString.append(
339 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800340 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
341 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600342 }
343
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900344 bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700345
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700346 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700347 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
348 // Typically that means there is an HDR capable display attached, but could be
349 // support for attaching an HDR display. In either case, advertise support for
350 // HDR color spaces.
Chris Glover9f746f62023-04-20 10:23:22 +0100351 mExtensionString.append("EGL_EXT_gl_colorspace_bt2020_hlg "
352 "EGL_EXT_gl_colorspace_bt2020_linear "
353 "EGL_EXT_gl_colorspace_bt2020_pq ");
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700354 }
355
Michael Lentine54466bc2015-01-27 09:01:03 -0800356 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800357 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400358 // length of the extension name
359 size_t len = strcspn(start, " ");
360 if (len) {
361 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800362 const std::string ext(start, len);
363 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400364 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800365 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400366 // advance to the next extension name, skipping the space.
367 start += len;
368 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800369 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400370 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800371
372 egl_cache_t::get()->initialize(this);
373
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400374 finishOnSwap = base::GetBoolProperty("debug.egl.finish", false);
375 traceGpuCompletion = base::GetBoolProperty("debug.egl.traceGpuCompletion", false);
Michael Lentine54466bc2015-01-27 09:01:03 -0800376
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600377 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
378 // changing the behavior from the past where we always advertise
379 // version 1.4. May need to check that revision is valid
380 // before using cnx->major & cnx->minor
381 if (major != nullptr) *major = cnx->major;
382 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700383 }
384
Mathias Agopian65421432017-03-08 11:49:05 -0800385 { // scope for refLock
386 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800387 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800388 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700389 }
390
Mathias Agopian7773c432012-02-13 20:06:08 -0800391 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700392}
393
394EGLBoolean egl_display_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800395 { // scope for refLock
396 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800397 if (refs == 0) {
398 /*
399 * From the EGL spec (3.2):
400 * "Termination of a display that has already been terminated,
401 * (...), is allowed, but the only effect of such a call is
402 * to return EGL_TRUE (...)
403 */
404 return EGL_TRUE;
405 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700406
Michael Lentine54466bc2015-01-27 09:01:03 -0800407 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700408 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800409 if (refs > 0) {
410 return EGL_TRUE;
411 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700412 }
413
414 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800415
Mathias Agopian65421432017-03-08 11:49:05 -0800416 { // scope for lock
417 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800418
419 egl_connection_t* const cnx = &gEGLImpl;
420 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600421 // If we're using ANGLE reset any custom DisplayPlatform
Peiyong Lin2f51e752023-06-15 22:35:14 +0000422 if (cnx->angleLoaded) {
Peiyong Lin9fc4cf32023-11-03 05:18:53 +0000423 angle::resetAnglePlatform(disp.dpy, cnx);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600424 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800425 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
426 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
Yiwei Zhang8af03062020-08-12 21:28:15 -0700427 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Michael Lentine54466bc2015-01-27 09:01:03 -0800428 }
429 // REVISIT: it's unclear what to do if eglTerminate() fails
430 disp.state = egl_display_t::TERMINATED;
431 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800433
Michael Lentine54466bc2015-01-27 09:01:03 -0800434 // Reset the extension string since it will be regenerated if we get
435 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800436 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800437
438 // Mark all objects remaining in the list as terminated, unless
439 // there are no reference to them, it which case, we're free to
440 // delete them.
441 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800442 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800443 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800444 o->destroy();
445 }
446
447 // this marks all object handles are "terminated"
448 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700449 }
450
Mathias Agopian65421432017-03-08 11:49:05 -0800451 { // scope for refLock
452 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800453 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800454 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700455 }
456
Mathias Agopian518ec112011-05-13 16:21:08 -0700457 return res;
458}
459
Yiwei Zhang8af03062020-08-12 21:28:15 -0700460void egl_display_t::loseCurrent(egl_context_t* cur_c) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800461 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800462 egl_display_t* display = cur_c->getDisplay();
463 if (display) {
464 display->loseCurrentImpl(cur_c);
465 }
466 }
467}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800468
Yiwei Zhang8af03062020-08-12 21:28:15 -0700469void egl_display_t::loseCurrentImpl(egl_context_t* cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800470 // by construction, these are either 0 or valid (possibly terminated)
471 // it should be impossible for these to be invalid
472 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700473 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
474 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800475
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800476 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800477 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800478 cur_c->onLooseCurrent();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800479 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800480
481 // This cannot be called with the lock held because it might end-up
482 // calling back into EGL (in particular when a surface is destroyed
483 // it calls ANativeWindow::disconnect
484 _cur_c.release();
485 _cur_r.release();
486 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800487}
488
Yiwei Zhang8af03062020-08-12 21:28:15 -0700489EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw,
490 EGLSurface read, EGLContext /*ctx*/, EGLSurface impl_draw,
491 EGLSurface impl_read, EGLContext impl_ctx) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800492 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800493
494 // by construction, these are either 0 or valid (possibly terminated)
495 // it should be impossible for these to be invalid
496 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700497 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
498 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800499
500 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800501 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800502 if (c) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700503 result = c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800504 if (result == EGL_TRUE) {
505 c->onMakeCurrent(draw, read);
506 }
507 } else {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700508 result = cur_c->cnx->egl.eglMakeCurrent(disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800509 if (result == EGL_TRUE) {
510 cur_c->onLooseCurrent();
511 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800512 }
513 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800514
515 if (result == EGL_TRUE) {
516 // This cannot be called with the lock held because it might end-up
517 // calling back into EGL (in particular when a surface is destroyed
518 // it calls ANativeWindow::disconnect
519 _cur_c.release();
520 _cur_r.release();
521 _cur_d.release();
522 }
523
Mathias Agopianfb87e542012-01-30 18:20:52 -0800524 return result;
525}
Mathias Agopian518ec112011-05-13 16:21:08 -0700526
Jesse Hallc2e41222013-08-08 13:40:22 -0700527bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
528 if (!nameLen) {
529 nameLen = strlen(name);
530 }
Mathias Agopian65421432017-03-08 11:49:05 -0800531 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700532}
533
Yiwei Zhang8af03062020-08-12 21:28:15 -0700534egl_display_t* validate_display(EGLDisplay dpy) {
535 egl_display_t* const dp = get_display(dpy);
536 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)nullptr);
537 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)nullptr);
538
539 return dp;
540}
541
542egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx) {
543 *outCnx = nullptr;
544 egl_display_t* dp = validate_display(dpy);
545 if (!dp) return dp;
546 *outCnx = &gEGLImpl;
547 if ((*outCnx)->dso == nullptr) {
548 return setError(EGL_BAD_CONFIG, (egl_display_t*)nullptr);
549 }
550 return dp;
551}
552
Mathias Agopian518ec112011-05-13 16:21:08 -0700553}; // namespace android