blob: eb970d9c118dc2c18ce7889d6b666a1d3ac0deac [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
Mathias Agopianb7f9a242017-03-08 22:29:31 -080074int egl_get_init_count(EGLDisplay dpy) {
75 egl_display_t* eglDisplay = egl_display_t::get(dpy);
76 return eglDisplay ? eglDisplay->getRefsCount() : 0;
77}
78
Mathias Agopian518ec112011-05-13 16:21:08 -070079egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
80
81egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080082 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070083}
84
85egl_display_t::~egl_display_t() {
86 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080087 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070088}
89
90egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080091 if (uintptr_t(dpy) == 0) {
92 return nullptr;
93 }
94
Mathias Agopian518ec112011-05-13 16:21:08 -070095 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070096 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
97 return nullptr;
98 }
99 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -0700100}
101
102void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800103 std::lock_guard<std::mutex> _l(lock);
104 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700105}
106
Mathias Agopian5b287a62011-05-16 18:58:55 -0700107void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800108 std::lock_guard<std::mutex> _l(lock);
109 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700110}
111
Mathias Agopianf0480de2011-11-13 20:50:07 -0800112bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800113 std::lock_guard<std::mutex> _l(lock);
114 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800115 if (object->getDisplay() == this) {
116 object->incRef();
117 return true;
118 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700119 }
120 return false;
121}
122
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600123EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp,
124 const EGLAttrib* attrib_list) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700126 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700127
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600128 return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700129}
130
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600131static bool addAnglePlatformAttributes(egl_connection_t* const cnx,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600132 std::vector<EGLAttrib>& attrs) {
133 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
134
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600135 attrs.reserve(4 * 2);
136
137 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
138 attrs.push_back(cnx->angleBackend);
139
140 switch (cnx->angleBackend) {
141 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
142 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600143 char prop[PROPERTY_VALUE_MAX];
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600144 property_get("debug.angle.validation", prop, "0");
145 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
146 attrs.push_back(atoi(prop));
147 break;
148 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
149 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
150 break;
151 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
152 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
153 // NOTE: This is only valid if the backend is OpenGL
154 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
155 attrs.push_back(vendorEGL);
156 break;
157 default:
158 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
159 break;
160 }
161 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
162 attrs.push_back(EGL_FALSE);
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600163
164 return true;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600165}
166
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600167static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
168 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600169 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600170 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600171
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600172 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600173 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600174 if (attrib_list) {
175 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
176 attrs.push_back(attr[0]);
177 attrs.push_back(attr[1]);
178 }
179 }
180
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600181 if (!addAnglePlatformAttributes(cnx, attrs)) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600182 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
183 *error = EGL_BAD_PARAMETER;
184 return EGL_NO_DISPLAY;
185 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600186 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600187
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600188 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
189 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
190 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600191 if (dpy == EGL_NO_DISPLAY) {
192 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600193 } else {
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700194 if (!angle::initializeAnglePlatform(dpy)) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600195 ALOGE("initializeAnglePlatform failed!");
196 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600197 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600198 } else {
199 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
200 display);
201 }
202
203 return dpy;
204}
205
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600206EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
207 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800208 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800209 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700210
211 // get our driver loader
212 Loader& loader(Loader::getInstance());
213
Mathias Agopianada798b2012-02-13 17:09:30 -0800214 egl_connection_t* const cnx = &gEGLImpl;
215 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600216 EGLDisplay dpy = EGL_NO_DISPLAY;
217
218 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600219 EGLint error;
220 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
221 if (error != EGL_NONE) {
222 return setError(error, dpy);
223 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600224 }
225 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600226 // NOTE: eglGetPlatformDisplay with a empty attribute list
227 // behaves the same as eglGetDisplay
228 if (cnx->egl.eglGetPlatformDisplay) {
229 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
230 attrib_list);
Ben Line17f69b2018-11-05 16:49:28 -0800231 }
232
233 // It is possible that eglGetPlatformDisplay does not have a
234 // working implementation for Android platform; in that case,
235 // one last fallback to eglGetDisplay
236 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600237 if (attrib_list) {
238 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
239 }
240 dpy = cnx->egl.eglGetDisplay(display);
241 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600242 }
243
Mathias Agopianada798b2012-02-13 17:09:30 -0800244 disp.dpy = dpy;
245 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600246 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700247 }
248 }
249
250 return EGLDisplay(uintptr_t(display) + 1U);
251}
252
253EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
254
Mathias Agopian65421432017-03-08 11:49:05 -0800255 { // scope for refLock
256 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800257 refs++;
258 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600259 // We don't know what to report until we know what the
260 // driver supports. Make sure we are initialized before
261 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800262 while(!eglIsInitialized) {
263 refCond.wait(_l);
264 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600265 egl_connection_t* const cnx = &gEGLImpl;
266
267 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
268 // changing the behavior from the past where we always advertise
269 // version 1.4. May need to check that revision is valid
270 // before using cnx->major & cnx->minor
271 if (major != nullptr) *major = cnx->major;
272 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800273 return EGL_TRUE;
274 }
Mathias Agopian65421432017-03-08 11:49:05 -0800275 while(eglIsInitialized) {
276 refCond.wait(_l);
277 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800278 }
279
Mathias Agopian65421432017-03-08 11:49:05 -0800280 { // scope for lock
281 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800282
Michael Lentine54466bc2015-01-27 09:01:03 -0800283 setGLHooksThreadSpecific(&gHooksNoContext);
284
285 // initialize each EGL and
286 // build our own extension string first, based on the extension we know
287 // and the extension supported by our client implementation
288
289 egl_connection_t* const cnx = &gEGLImpl;
290 cnx->major = -1;
291 cnx->minor = -1;
292 if (cnx->dso) {
293 EGLDisplay idpy = disp.dpy;
294 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
295 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
296 // idpy, cnx->major, cnx->minor, cnx);
297
298 // display is now initialized
299 disp.state = egl_display_t::INITIALIZED;
300
301 // get the query-strings for this display for each implementation
302 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
303 EGL_VENDOR);
304 disp.queryString.version = cnx->egl.eglQueryString(idpy,
305 EGL_VERSION);
306 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
307 EGL_EXTENSIONS);
308 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
309 EGL_CLIENT_APIS);
310
311 } else {
312 ALOGW("eglInitialize(%p) failed (%s)", idpy,
313 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
314 }
315 }
316
Sean Callananad9061c2019-03-12 14:07:23 -0700317 if (cnx->minor == 5) {
318 // full list in egl_entries.in
319 if (!cnx->egl.eglCreateImage ||
320 !cnx->egl.eglDestroyImage ||
321 !cnx->egl.eglGetPlatformDisplay ||
322 !cnx->egl.eglCreatePlatformWindowSurface ||
323 !cnx->egl.eglCreatePlatformPixmapSurface ||
324 !cnx->egl.eglCreateSync ||
325 !cnx->egl.eglDestroySync ||
326 !cnx->egl.eglClientWaitSync ||
327 !cnx->egl.eglGetSyncAttrib ||
328 !cnx->egl.eglWaitSync) {
329 ALOGE("Driver indicates EGL 1.5 support, but does not have "
330 "a critical API");
331 cnx->minor = 4;
332 }
333 }
334
Michael Lentine54466bc2015-01-27 09:01:03 -0800335 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800336 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600337 mVersionString.clear();
338 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
Courtney Goeltzenleuchterf5b5c412019-04-10 17:36:19 -0600339 mVersionString = sVersionString14;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600340 if ((cnx->major == 1) && (cnx->minor == 5)) {
341 mVersionString = sVersionString15;
342 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600343 }
344 if (mVersionString.empty()) {
345 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
346 mVersionString = sVersionString14;
347 }
Mathias Agopian65421432017-03-08 11:49:05 -0800348 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800349
Mathias Agopian65421432017-03-08 11:49:05 -0800350 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600351
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700352 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
353
354 // Note: CDD requires that devices supporting wide color and/or HDR color also support
355 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600356 bool wideColorBoardConfig =
357 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
358 false);
359
360 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700361 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600362 mExtensionString.append(
363 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
Peiyong Line0ff3772018-12-08 22:23:20 -0800364 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "
365 "EGL_EXT_gl_colorspace_display_p3_passthrough ");
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600366 }
367
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700368 bool hasHdrBoardConfig =
369 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
370
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700371 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700372 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
373 // Typically that means there is an HDR capable display attached, but could be
374 // support for attaching an HDR display. In either case, advertise support for
375 // HDR color spaces.
376 mExtensionString.append(
377 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
378 }
379
Michael Lentine54466bc2015-01-27 09:01:03 -0800380 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800381 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400382 // length of the extension name
383 size_t len = strcspn(start, " ");
384 if (len) {
385 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800386 const std::string ext(start, len);
387 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400388 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800389 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400390 // advance to the next extension name, skipping the space.
391 start += len;
392 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800393 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400394 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800395
396 egl_cache_t::get()->initialize(this);
397
398 char value[PROPERTY_VALUE_MAX];
399 property_get("debug.egl.finish", value, "0");
400 if (atoi(value)) {
401 finishOnSwap = true;
402 }
403
404 property_get("debug.egl.traceGpuCompletion", value, "0");
405 if (atoi(value)) {
406 traceGpuCompletion = true;
407 }
408
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600409 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
410 // changing the behavior from the past where we always advertise
411 // version 1.4. May need to check that revision is valid
412 // before using cnx->major & cnx->minor
413 if (major != nullptr) *major = cnx->major;
414 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700415 }
416
Mathias Agopian65421432017-03-08 11:49:05 -0800417 { // scope for refLock
418 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800419 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800420 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700421 }
422
Mathias Agopian7773c432012-02-13 20:06:08 -0800423 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700424}
425
426EGLBoolean egl_display_t::terminate() {
427
Mathias Agopian65421432017-03-08 11:49:05 -0800428 { // scope for refLock
429 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800430 if (refs == 0) {
431 /*
432 * From the EGL spec (3.2):
433 * "Termination of a display that has already been terminated,
434 * (...), is allowed, but the only effect of such a call is
435 * to return EGL_TRUE (...)
436 */
437 return EGL_TRUE;
438 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700439
Michael Lentine54466bc2015-01-27 09:01:03 -0800440 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700441 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800442 if (refs > 0) {
443 return EGL_TRUE;
444 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700445 }
446
447 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800448
Mathias Agopian65421432017-03-08 11:49:05 -0800449 { // scope for lock
450 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800451
452 egl_connection_t* const cnx = &gEGLImpl;
453 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600454 // If we're using ANGLE reset any custom DisplayPlatform
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700455 if (cnx->useAngle) {
456 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600457 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800458 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
459 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
460 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
461 }
462 // REVISIT: it's unclear what to do if eglTerminate() fails
463 disp.state = egl_display_t::TERMINATED;
464 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700465 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800466
Michael Lentine54466bc2015-01-27 09:01:03 -0800467 // Reset the extension string since it will be regenerated if we get
468 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800469 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800470
471 // Mark all objects remaining in the list as terminated, unless
472 // there are no reference to them, it which case, we're free to
473 // delete them.
474 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800475 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800476 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800477 o->destroy();
478 }
479
480 // this marks all object handles are "terminated"
481 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700482 }
483
Mathias Agopian65421432017-03-08 11:49:05 -0800484 { // scope for refLock
485 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800486 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800487 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700488 }
489
Mathias Agopian518ec112011-05-13 16:21:08 -0700490 return res;
491}
492
Mathias Agopianfb87e542012-01-30 18:20:52 -0800493void egl_display_t::loseCurrent(egl_context_t * cur_c)
494{
495 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800496 egl_display_t* display = cur_c->getDisplay();
497 if (display) {
498 display->loseCurrentImpl(cur_c);
499 }
500 }
501}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800502
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800503void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
504{
505 // by construction, these are either 0 or valid (possibly terminated)
506 // it should be impossible for these to be invalid
507 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700508 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
509 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800510
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800511 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800512 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800513 cur_c->onLooseCurrent();
514
Mathias Agopianfb87e542012-01-30 18:20:52 -0800515 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800516
517 // This cannot be called with the lock held because it might end-up
518 // calling back into EGL (in particular when a surface is destroyed
519 // it calls ANativeWindow::disconnect
520 _cur_c.release();
521 _cur_r.release();
522 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800523}
524
525EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700526 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800527 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
528{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800529 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800530
531 // by construction, these are either 0 or valid (possibly terminated)
532 // it should be impossible for these to be invalid
533 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700534 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
535 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800536
537 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800538 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800539 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800540 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800541 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800542 if (result == EGL_TRUE) {
543 c->onMakeCurrent(draw, read);
544 }
545 } else {
546 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800547 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800548 if (result == EGL_TRUE) {
549 cur_c->onLooseCurrent();
550 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800551 }
552 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800553
554 if (result == EGL_TRUE) {
555 // This cannot be called with the lock held because it might end-up
556 // calling back into EGL (in particular when a surface is destroyed
557 // it calls ANativeWindow::disconnect
558 _cur_c.release();
559 _cur_r.release();
560 _cur_d.release();
561 }
562
Mathias Agopianfb87e542012-01-30 18:20:52 -0800563 return result;
564}
Mathias Agopian518ec112011-05-13 16:21:08 -0700565
Jesse Hallc2e41222013-08-08 13:40:22 -0700566bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
567 if (!nameLen) {
568 nameLen = strlen(name);
569 }
Mathias Agopian65421432017-03-08 11:49:05 -0800570 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700571}
572
Jesse Halla0fef1c2012-04-17 12:02:26 -0700573// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700574}; // namespace android
575// ----------------------------------------------------------------------------