blob: 6af7cd260b609180839bcc8f9c1fd6de40f4fc98 [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
Sundong Ahn204fb1f2020-04-23 21:56:36 +090034#include <SurfaceFlingerProperties.h>
Tobin Ehlis96a184d2018-07-18 16:14:07 -060035#include <android/dlext.h>
36#include <dlfcn.h>
37#include <graphicsenv/GraphicsEnv.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080038
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060039#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
40#include <configstore/Utils.h>
41
42using namespace android::hardware::configstore;
43using namespace android::hardware::configstore::V1_0;
44
Mathias Agopian518ec112011-05-13 16:21:08 -070045// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopian4b9511c2011-11-13 23:52:47 -080049static char const * const sVendorString = "Android";
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -060050static char const* const sVersionString14 = "1.4 Android META-EGL";
51static char const* const sVersionString15 = "1.5 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070052static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080053
Jesse Hall21558da2013-08-06 15:31:22 -070054extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070055extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080056
Mathias Agopian518ec112011-05-13 16:21:08 -070057extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
58
Mathias Agopian518ec112011-05-13 16:21:08 -070059// ----------------------------------------------------------------------------
60
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070061bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070062 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070063 if (!nameLen) {
64 nameLen = strlen(name);
65 }
Kalle Raita7804aa22016-04-18 16:03:37 -070066 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
67 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
68 return true;
69 }
Jesse Hallc2e41222013-08-08 13:40:22 -070070 }
71 }
72 return false;
73}
74
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -070075bool needsAndroidPEglMitigation() {
76 static const int32_t vndk_version = property_get_int32("ro.vndk.version", -1);
77 return vndk_version <= 28;
78}
79
Mathias Agopianb7f9a242017-03-08 22:29:31 -080080int egl_get_init_count(EGLDisplay dpy) {
81 egl_display_t* eglDisplay = egl_display_t::get(dpy);
82 return eglDisplay ? eglDisplay->getRefsCount() : 0;
83}
84
Mathias Agopian518ec112011-05-13 16:21:08 -070085egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
86
87egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080088 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070089}
90
91egl_display_t::~egl_display_t() {
92 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080093 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070094}
95
96egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080097 if (uintptr_t(dpy) == 0) {
98 return nullptr;
99 }
100
Mathias Agopian518ec112011-05-13 16:21:08 -0700101 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -0700102 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
103 return nullptr;
104 }
105 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700106}
107
108void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800109 std::lock_guard<std::mutex> _l(lock);
110 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700111}
112
Mathias Agopian5b287a62011-05-16 18:58:55 -0700113void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800114 std::lock_guard<std::mutex> _l(lock);
115 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700116}
117
Mathias Agopianf0480de2011-11-13 20:50:07 -0800118bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800119 std::lock_guard<std::mutex> _l(lock);
120 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800121 if (object->getDisplay() == this) {
122 object->incRef();
123 return true;
124 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 }
126 return false;
127}
128
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600129EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
130 const EGLAttrib* attrib_list) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700131 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700132 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700133
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600134 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700135}
136
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600137static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
138 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600139 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600140 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600141
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600142 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600143 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600144 if (attrib_list) {
145 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
146 attrs.push_back(attr[0]);
147 attrs.push_back(attr[1]);
148 }
149 }
150
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600151 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
152 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
153
154 char prop[PROPERTY_VALUE_MAX];
155 property_get("debug.angle.validation", prop, "0");
156 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
157 attrs.push_back(atoi(prop));
158
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600159 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600160
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600161 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
162 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
163 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600164 if (dpy == EGL_NO_DISPLAY) {
165 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600166 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700167 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600168 ALOGE("initializeAnglePlatform failed!");
169 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600170 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600171 } else {
172 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
173 display);
174 }
175
176 return dpy;
177}
178
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600179EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
180 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800181 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800182 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700183
184 // get our driver loader
185 Loader& loader(Loader::getInstance());
186
Mathias Agopianada798b2012-02-13 17:09:30 -0800187 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700188 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600189 EGLDisplay dpy = EGL_NO_DISPLAY;
190
Charlie Lao840bd532020-01-16 17:34:37 -0800191 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600192 EGLint error;
193 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
194 if (error != EGL_NONE) {
195 return setError(error, dpy);
196 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600197 }
198 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600199 // NOTE: eglGetPlatformDisplay with a empty attribute list
200 // behaves the same as eglGetDisplay
201 if (cnx->egl.eglGetPlatformDisplay) {
202 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
203 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800204 }
205
206 // It is possible that eglGetPlatformDisplay does not have a
207 // working implementation for Android platform; in that case,
208 // one last fallback to eglGetDisplay
209 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600210 if (attrib_list) {
211 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
212 }
213 dpy = cnx->egl.eglGetDisplay(display);
214 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600215 }
216
Mathias Agopianada798b2012-02-13 17:09:30 -0800217 disp.dpy = dpy;
218 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600219 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700220 }
221 }
222
223 return EGLDisplay(uintptr_t(display) + 1U);
224}
225
226EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
227
Mathias Agopian65421432017-03-08 11:49:05 -0800228 { // scope for refLock
229 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800230 refs++;
231 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600232 // We don't know what to report until we know what the
233 // driver supports. Make sure we are initialized before
234 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800235 while(!eglIsInitialized) {
236 refCond.wait(_l);
237 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600238 egl_connection_t* const cnx = &gEGLImpl;
239
240 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
241 // changing the behavior from the past where we always advertise
242 // version 1.4. May need to check that revision is valid
243 // before using cnx->major & cnx->minor
244 if (major != nullptr) *major = cnx->major;
245 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800246 return EGL_TRUE;
247 }
Mathias Agopian65421432017-03-08 11:49:05 -0800248 while(eglIsInitialized) {
249 refCond.wait(_l);
250 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800251 }
252
Mathias Agopian65421432017-03-08 11:49:05 -0800253 { // scope for lock
254 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800255
Michael Lentine54466bc2015-01-27 09:01:03 -0800256 setGLHooksThreadSpecific(&gHooksNoContext);
257
258 // initialize each EGL and
259 // build our own extension string first, based on the extension we know
260 // and the extension supported by our client implementation
261
262 egl_connection_t* const cnx = &gEGLImpl;
263 cnx->major = -1;
264 cnx->minor = -1;
265 if (cnx->dso) {
266 EGLDisplay idpy = disp.dpy;
267 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
268 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
269 // idpy, cnx->major, cnx->minor, cnx);
270
271 // display is now initialized
272 disp.state = egl_display_t::INITIALIZED;
273
274 // get the query-strings for this display for each implementation
275 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
276 EGL_VENDOR);
277 disp.queryString.version = cnx->egl.eglQueryString(idpy,
278 EGL_VERSION);
279 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
280 EGL_EXTENSIONS);
281 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
282 EGL_CLIENT_APIS);
283
284 } else {
285 ALOGW("eglInitialize(%p) failed (%s)", idpy,
286 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
287 }
288 }
289
Sean Callananad9061c2019-03-12 14:07:23 -0700290 if (cnx->minor == 5) {
291 // full list in egl_entries.in
292 if (!cnx->egl.eglCreateImage ||
293 !cnx->egl.eglDestroyImage ||
294 !cnx->egl.eglGetPlatformDisplay ||
295 !cnx->egl.eglCreatePlatformWindowSurface ||
296 !cnx->egl.eglCreatePlatformPixmapSurface ||
297 !cnx->egl.eglCreateSync ||
298 !cnx->egl.eglDestroySync ||
299 !cnx->egl.eglClientWaitSync ||
300 !cnx->egl.eglGetSyncAttrib ||
301 !cnx->egl.eglWaitSync) {
302 ALOGE("Driver indicates EGL 1.5 support, but does not have "
303 "a critical API");
304 cnx->minor = 4;
305 }
306 }
307
Michael Lentine54466bc2015-01-27 09:01:03 -0800308 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800309 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600310 mVersionString.clear();
311 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600312 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600313 if ((cnx->major == 1) && (cnx->minor == 5)) {
314 mVersionString = sVersionString15;
315 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600316 }
317 if (mVersionString.empty()) {
318 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
319 mVersionString = sVersionString14;
320 }
Mathias Agopian65421432017-03-08 11:49:05 -0800321 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800322
Mathias Agopian65421432017-03-08 11:49:05 -0800323 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600324
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700325 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
326
327 // Note: CDD requires that devices supporting wide color and/or HDR color also support
328 // the EGL_KHR_gl_colorspace extension.
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900329 bool wideColorBoardConfig = android::sysprop::has_wide_color_display(false);
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600330
331 // Add wide-color extensions if device can support wide-color
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700332 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600333 mExtensionString.append(
334 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800335 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
336 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600337 }
338
Sundong Ahn204fb1f2020-04-23 21:56:36 +0900339 bool hasHdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700340
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700341 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700342 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
343 // Typically that means there is an HDR capable display attached, but could be
344 // support for attaching an HDR display. In either case, advertise support for
345 // HDR color spaces.
346 mExtensionString.append(
347 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
348 }
349
Michael Lentine54466bc2015-01-27 09:01:03 -0800350 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800351 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400352 // length of the extension name
353 size_t len = strcspn(start, " ");
354 if (len) {
355 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800356 const std::string ext(start, len);
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -0700357 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
358 // some Android P vendor partitions this extension under the draft KHR name,
359 // but during Khronos review it was decided to demote it to EXT.
360 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
361 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
362 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
363 }
Mathias Agopian65421432017-03-08 11:49:05 -0800364 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400365 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800366 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400367 // advance to the next extension name, skipping the space.
368 start += len;
369 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800370 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400371 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800372
373 egl_cache_t::get()->initialize(this);
374
375 char value[PROPERTY_VALUE_MAX];
376 property_get("debug.egl.finish", value, "0");
377 if (atoi(value)) {
378 finishOnSwap = true;
379 }
380
381 property_get("debug.egl.traceGpuCompletion", value, "0");
382 if (atoi(value)) {
383 traceGpuCompletion = true;
384 }
385
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600386 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
387 // changing the behavior from the past where we always advertise
388 // version 1.4. May need to check that revision is valid
389 // before using cnx->major & cnx->minor
390 if (major != nullptr) *major = cnx->major;
391 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700392 }
393
Mathias Agopian65421432017-03-08 11:49:05 -0800394 { // scope for refLock
395 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800396 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800397 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700398 }
399
Mathias Agopian7773c432012-02-13 20:06:08 -0800400 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700401}
402
403EGLBoolean egl_display_t::terminate() {
404
Mathias Agopian65421432017-03-08 11:49:05 -0800405 { // scope for refLock
406 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800407 if (refs == 0) {
408 /*
409 * From the EGL spec (3.2):
410 * "Termination of a display that has already been terminated,
411 * (...), is allowed, but the only effect of such a call is
412 * to return EGL_TRUE (...)
413 */
414 return EGL_TRUE;
415 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700416
Michael Lentine54466bc2015-01-27 09:01:03 -0800417 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700418 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800419 if (refs > 0) {
420 return EGL_TRUE;
421 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700422 }
423
424 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800425
Mathias Agopian65421432017-03-08 11:49:05 -0800426 { // scope for lock
427 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800428
429 egl_connection_t* const cnx = &gEGLImpl;
430 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600431 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800432 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700433 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600434 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800435 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
436 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
437 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
438 }
439 // REVISIT: it's unclear what to do if eglTerminate() fails
440 disp.state = egl_display_t::TERMINATED;
441 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700442 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800443
Michael Lentine54466bc2015-01-27 09:01:03 -0800444 // Reset the extension string since it will be regenerated if we get
445 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800446 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800447
448 // Mark all objects remaining in the list as terminated, unless
449 // there are no reference to them, it which case, we're free to
450 // delete them.
451 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800452 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800453 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800454 o->destroy();
455 }
456
457 // this marks all object handles are "terminated"
458 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700459 }
460
Mathias Agopian65421432017-03-08 11:49:05 -0800461 { // scope for refLock
462 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800463 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800464 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700465 }
466
Mathias Agopian518ec112011-05-13 16:21:08 -0700467 return res;
468}
469
Mathias Agopianfb87e542012-01-30 18:20:52 -0800470void egl_display_t::loseCurrent(egl_context_t * cur_c)
471{
472 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800473 egl_display_t* display = cur_c->getDisplay();
474 if (display) {
475 display->loseCurrentImpl(cur_c);
476 }
477 }
478}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800479
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800480void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
481{
482 // by construction, these are either 0 or valid (possibly terminated)
483 // it should be impossible for these to be invalid
484 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700485 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
486 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800487
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800488 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800489 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800490 cur_c->onLooseCurrent();
491
Mathias Agopianfb87e542012-01-30 18:20:52 -0800492 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800493
494 // This cannot be called with the lock held because it might end-up
495 // calling back into EGL (in particular when a surface is destroyed
496 // it calls ANativeWindow::disconnect
497 _cur_c.release();
498 _cur_r.release();
499 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800500}
501
502EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700503 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800504 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
505{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800506 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800507
508 // by construction, these are either 0 or valid (possibly terminated)
509 // it should be impossible for these to be invalid
510 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700511 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
512 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800513
514 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800515 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800516 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800517 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800518 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800519 if (result == EGL_TRUE) {
520 c->onMakeCurrent(draw, read);
521 }
522 } else {
523 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800524 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800525 if (result == EGL_TRUE) {
526 cur_c->onLooseCurrent();
527 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800528 }
529 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800530
531 if (result == EGL_TRUE) {
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();
538 }
539
Mathias Agopianfb87e542012-01-30 18:20:52 -0800540 return result;
541}
Mathias Agopian518ec112011-05-13 16:21:08 -0700542
Jesse Hallc2e41222013-08-08 13:40:22 -0700543bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
544 if (!nameLen) {
545 nameLen = strlen(name);
546 }
Mathias Agopian65421432017-03-08 11:49:05 -0800547 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700548}
549
Jesse Halla0fef1c2012-04-17 12:02:26 -0700550// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700551}; // namespace android
552// ----------------------------------------------------------------------------