blob: 95c3068fc8eca4484c985a6e3b1a0cba76717ada [file] [log] [blame]
Jesse Hall21558da2013-08-06 15:31:22 -07001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall21558da2013-08-06 15:31:22 -07004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall21558da2013-08-06 15:31:22 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall21558da2013-08-06 15:31:22 -070010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jesse Hallb29e5e82012-04-04 16:53:42 -070017#define __STDC_LIMIT_MACROS 1
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hallb29e5e82012-04-04 16:53:42 -070019
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "egl_display.h"
Mathias Agopian4b9511c2011-11-13 23:52:47 -080021
Mathias Agopian39c24a22013-04-04 23:17:56 -070022#include "../egl_impl.h"
23
Cody Northrop1f00e172018-04-02 11:23:31 -060024#include <EGL/eglext_angle.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080025#include <private/EGL/display.h>
26
Tobin Ehlis96a184d2018-07-18 16:14:07 -060027#include <cutils/properties.h>
28#include "Loader.h"
29#include "egl_angle_platform.h"
Jamie Gennisaca51c02011-11-03 17:42:43 -070030#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include "egl_object.h"
32#include "egl_tls.h"
Tobin Ehlis96a184d2018-07-18 16:14:07 -060033
34#include <android/dlext.h>
35#include <dlfcn.h>
36#include <graphicsenv/GraphicsEnv.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080037
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060038#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
39#include <configstore/Utils.h>
40
41using namespace android::hardware::configstore;
42using namespace android::hardware::configstore::V1_0;
43
Mathias Agopian518ec112011-05-13 16:21:08 -070044// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
Mathias Agopian4b9511c2011-11-13 23:52:47 -080048static char const * const sVendorString = "Android";
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -060049static char const* const sVersionString14 = "1.4 Android META-EGL";
50static char const* const sVersionString15 = "1.5 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070051static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080052
Jesse Hall21558da2013-08-06 15:31:22 -070053extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080055
Mathias Agopian518ec112011-05-13 16:21:08 -070056extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058// ----------------------------------------------------------------------------
59
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070060bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070061 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070062 if (!nameLen) {
63 nameLen = strlen(name);
64 }
Kalle Raita7804aa22016-04-18 16:03:37 -070065 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
66 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
67 return true;
68 }
Jesse Hallc2e41222013-08-08 13:40:22 -070069 }
70 }
71 return false;
72}
73
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -070074bool needsAndroidPEglMitigation() {
75 static const int32_t vndk_version = property_get_int32("ro.vndk.version", -1);
76 return vndk_version <= 28;
77}
78
Mathias Agopianb7f9a242017-03-08 22:29:31 -080079int egl_get_init_count(EGLDisplay dpy) {
80 egl_display_t* eglDisplay = egl_display_t::get(dpy);
81 return eglDisplay ? eglDisplay->getRefsCount() : 0;
82}
83
Mathias Agopian518ec112011-05-13 16:21:08 -070084egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
85
86egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080087 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070088}
89
90egl_display_t::~egl_display_t() {
91 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080092 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070093}
94
95egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080096 if (uintptr_t(dpy) == 0) {
97 return nullptr;
98 }
99
Mathias Agopian518ec112011-05-13 16:21:08 -0700100 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -0700101 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
102 return nullptr;
103 }
104 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700105}
106
107void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800108 std::lock_guard<std::mutex> _l(lock);
109 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700110}
111
Mathias Agopian5b287a62011-05-16 18:58:55 -0700112void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800113 std::lock_guard<std::mutex> _l(lock);
114 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700115}
116
Mathias Agopianf0480de2011-11-13 20:50:07 -0800117bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800118 std::lock_guard<std::mutex> _l(lock);
119 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800120 if (object->getDisplay() == this) {
121 object->incRef();
122 return true;
123 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700124 }
125 return false;
126}
127
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600128EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
129 const EGLAttrib* attrib_list) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700130 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700131 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700132
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600133 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700134}
135
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600136static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
137 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600138 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600139 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600140
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600141 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600142 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600143 if (attrib_list) {
144 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
145 attrs.push_back(attr[0]);
146 attrs.push_back(attr[1]);
147 }
148 }
149
Courtney Goeltzenleuchterc1a1a862020-04-23 08:19:11 -0600150 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
151 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE);
152
153 char prop[PROPERTY_VALUE_MAX];
154 property_get("debug.angle.validation", prop, "0");
155 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
156 attrs.push_back(atoi(prop));
157
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 {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700166 if (!angle::initializeAnglePlatform(dpy)) {
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) {
Mathias Agopian65421432017-03-08 11:49:05 -0800180 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800181 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700182
183 // get our driver loader
184 Loader& loader(Loader::getInstance());
185
Mathias Agopianada798b2012-02-13 17:09:30 -0800186 egl_connection_t* const cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700187 if (cnx->dso) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600188 EGLDisplay dpy = EGL_NO_DISPLAY;
189
Charlie Lao840bd532020-01-16 17:34:37 -0800190 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600191 EGLint error;
192 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
193 if (error != EGL_NONE) {
194 return setError(error, dpy);
195 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600196 }
197 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600198 // NOTE: eglGetPlatformDisplay with a empty attribute list
199 // behaves the same as eglGetDisplay
200 if (cnx->egl.eglGetPlatformDisplay) {
201 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
202 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800203 }
204
205 // It is possible that eglGetPlatformDisplay does not have a
206 // working implementation for Android platform; in that case,
207 // one last fallback to eglGetDisplay
208 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600209 if (attrib_list) {
210 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
211 }
212 dpy = cnx->egl.eglGetDisplay(display);
213 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600214 }
215
Mathias Agopianada798b2012-02-13 17:09:30 -0800216 disp.dpy = dpy;
217 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600218 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700219 }
220 }
221
222 return EGLDisplay(uintptr_t(display) + 1U);
223}
224
225EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
226
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.
Mathias Agopian65421432017-03-08 11:49:05 -0800234 while(!eglIsInitialized) {
235 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 }
Mathias Agopian65421432017-03-08 11:49:05 -0800247 while(eglIsInitialized) {
248 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)) {
267 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
268 // 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
274 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
275 EGL_VENDOR);
276 disp.queryString.version = cnx->egl.eglQueryString(idpy,
277 EGL_VERSION);
278 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
279 EGL_EXTENSIONS);
280 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
281 EGL_CLIENT_APIS);
282
283 } else {
284 ALOGW("eglInitialize(%p) failed (%s)", idpy,
285 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
286 }
287 }
288
Sean Callananad9061c2019-03-12 14:07:23 -0700289 if (cnx->minor == 5) {
290 // full list in egl_entries.in
291 if (!cnx->egl.eglCreateImage ||
292 !cnx->egl.eglDestroyImage ||
293 !cnx->egl.eglGetPlatformDisplay ||
294 !cnx->egl.eglCreatePlatformWindowSurface ||
295 !cnx->egl.eglCreatePlatformPixmapSurface ||
296 !cnx->egl.eglCreateSync ||
297 !cnx->egl.eglDestroySync ||
298 !cnx->egl.eglClientWaitSync ||
299 !cnx->egl.eglGetSyncAttrib ||
300 !cnx->egl.eglWaitSync) {
301 ALOGE("Driver indicates EGL 1.5 support, but does not have "
302 "a critical API");
303 cnx->minor = 4;
304 }
305 }
306
Michael Lentine54466bc2015-01-27 09:01:03 -0800307 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800308 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600309 mVersionString.clear();
310 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600311 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600312 if ((cnx->major == 1) && (cnx->minor == 5)) {
313 mVersionString = sVersionString15;
314 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600315 }
316 if (mVersionString.empty()) {
317 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
318 mVersionString = sVersionString14;
319 }
Mathias Agopian65421432017-03-08 11:49:05 -0800320 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800321
Mathias Agopian65421432017-03-08 11:49:05 -0800322 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600323
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700324 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
325
326 // Note: CDD requires that devices supporting wide color and/or HDR color also support
327 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600328 bool wideColorBoardConfig =
329 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
330 false);
331
332 // Add wide-color extensions if device can support wide-color
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700333 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600334 mExtensionString.append(
335 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800336 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
337 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600338 }
339
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700340 bool hasHdrBoardConfig =
341 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
342
Krzysztof KosiƄskif2fc4e92018-04-18 16:29:49 -0700343 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700344 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
345 // Typically that means there is an HDR capable display attached, but could be
346 // support for attaching an HDR display. In either case, advertise support for
347 // HDR color spaces.
348 mExtensionString.append(
349 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
350 }
351
Michael Lentine54466bc2015-01-27 09:01:03 -0800352 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800353 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400354 // length of the extension name
355 size_t len = strcspn(start, " ");
356 if (len) {
357 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800358 const std::string ext(start, len);
Krzysztof KosiƄskidbccd222019-05-16 14:10:25 -0700359 // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on
360 // some Android P vendor partitions this extension under the draft KHR name,
361 // but during Khronos review it was decided to demote it to EXT.
362 if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" &&
363 findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) {
364 mExtensionString.append("EGL_EXT_image_gl_colorspace ");
365 }
Mathias Agopian65421432017-03-08 11:49:05 -0800366 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400367 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800368 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400369 // advance to the next extension name, skipping the space.
370 start += len;
371 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800372 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400373 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800374
375 egl_cache_t::get()->initialize(this);
376
377 char value[PROPERTY_VALUE_MAX];
378 property_get("debug.egl.finish", value, "0");
379 if (atoi(value)) {
380 finishOnSwap = true;
381 }
382
383 property_get("debug.egl.traceGpuCompletion", value, "0");
384 if (atoi(value)) {
385 traceGpuCompletion = true;
386 }
387
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600388 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
389 // changing the behavior from the past where we always advertise
390 // version 1.4. May need to check that revision is valid
391 // before using cnx->major & cnx->minor
392 if (major != nullptr) *major = cnx->major;
393 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700394 }
395
Mathias Agopian65421432017-03-08 11:49:05 -0800396 { // scope for refLock
397 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800398 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800399 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700400 }
401
Mathias Agopian7773c432012-02-13 20:06:08 -0800402 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700403}
404
405EGLBoolean egl_display_t::terminate() {
406
Mathias Agopian65421432017-03-08 11:49:05 -0800407 { // scope for refLock
408 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800409 if (refs == 0) {
410 /*
411 * From the EGL spec (3.2):
412 * "Termination of a display that has already been terminated,
413 * (...), is allowed, but the only effect of such a call is
414 * to return EGL_TRUE (...)
415 */
416 return EGL_TRUE;
417 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700418
Michael Lentine54466bc2015-01-27 09:01:03 -0800419 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700420 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800421 if (refs > 0) {
422 return EGL_TRUE;
423 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700424 }
425
426 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800427
Mathias Agopian65421432017-03-08 11:49:05 -0800428 { // scope for lock
429 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800430
431 egl_connection_t* const cnx = &gEGLImpl;
432 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600433 // If we're using ANGLE reset any custom DisplayPlatform
Charlie Lao840bd532020-01-16 17:34:37 -0800434 if (cnx->useAngle) {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700435 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600436 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800437 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
438 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
439 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
440 }
441 // REVISIT: it's unclear what to do if eglTerminate() fails
442 disp.state = egl_display_t::TERMINATED;
443 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700444 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800445
Michael Lentine54466bc2015-01-27 09:01:03 -0800446 // Reset the extension string since it will be regenerated if we get
447 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800448 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800449
450 // Mark all objects remaining in the list as terminated, unless
451 // there are no reference to them, it which case, we're free to
452 // delete them.
453 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800454 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800455 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800456 o->destroy();
457 }
458
459 // this marks all object handles are "terminated"
460 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700461 }
462
Mathias Agopian65421432017-03-08 11:49:05 -0800463 { // scope for refLock
464 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800465 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800466 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700467 }
468
Mathias Agopian518ec112011-05-13 16:21:08 -0700469 return res;
470}
471
Mathias Agopianfb87e542012-01-30 18:20:52 -0800472void egl_display_t::loseCurrent(egl_context_t * cur_c)
473{
474 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800475 egl_display_t* display = cur_c->getDisplay();
476 if (display) {
477 display->loseCurrentImpl(cur_c);
478 }
479 }
480}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800481
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800482void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
483{
484 // by construction, these are either 0 or valid (possibly terminated)
485 // it should be impossible for these to be invalid
486 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700487 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
488 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800489
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800490 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800491 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800492 cur_c->onLooseCurrent();
493
Mathias Agopianfb87e542012-01-30 18:20:52 -0800494 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800495
496 // This cannot be called with the lock held because it might end-up
497 // calling back into EGL (in particular when a surface is destroyed
498 // it calls ANativeWindow::disconnect
499 _cur_c.release();
500 _cur_r.release();
501 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800502}
503
504EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700505 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800506 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
507{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800508 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800509
510 // by construction, these are either 0 or valid (possibly terminated)
511 // it should be impossible for these to be invalid
512 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700513 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
514 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800515
516 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800517 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800518 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800519 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800520 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800521 if (result == EGL_TRUE) {
522 c->onMakeCurrent(draw, read);
523 }
524 } else {
525 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800526 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800527 if (result == EGL_TRUE) {
528 cur_c->onLooseCurrent();
529 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800530 }
531 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800532
533 if (result == EGL_TRUE) {
534 // This cannot be called with the lock held because it might end-up
535 // calling back into EGL (in particular when a surface is destroyed
536 // it calls ANativeWindow::disconnect
537 _cur_c.release();
538 _cur_r.release();
539 _cur_d.release();
540 }
541
Mathias Agopianfb87e542012-01-30 18:20:52 -0800542 return result;
543}
Mathias Agopian518ec112011-05-13 16:21:08 -0700544
Jesse Hallc2e41222013-08-08 13:40:22 -0700545bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
546 if (!nameLen) {
547 nameLen = strlen(name);
548 }
Mathias Agopian65421432017-03-08 11:49:05 -0800549 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700550}
551
Jesse Halla0fef1c2012-04-17 12:02:26 -0700552// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700553}; // namespace android
554// ----------------------------------------------------------------------------