blob: d452a6cba9648bbc8aa0a7f44b7cd0cb2c19ef8d [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";
49static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070050static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080051
Jesse Hall21558da2013-08-06 15:31:22 -070052extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070053extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080054
Mathias Agopian518ec112011-05-13 16:21:08 -070055extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
56
Mathias Agopian518ec112011-05-13 16:21:08 -070057// ----------------------------------------------------------------------------
58
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070059bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070060 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070061 if (!nameLen) {
62 nameLen = strlen(name);
63 }
Kalle Raita7804aa22016-04-18 16:03:37 -070064 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
65 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
66 return true;
67 }
Jesse Hallc2e41222013-08-08 13:40:22 -070068 }
69 }
70 return false;
71}
72
Mathias Agopianb7f9a242017-03-08 22:29:31 -080073int egl_get_init_count(EGLDisplay dpy) {
74 egl_display_t* eglDisplay = egl_display_t::get(dpy);
75 return eglDisplay ? eglDisplay->getRefsCount() : 0;
76}
77
Mathias Agopian518ec112011-05-13 16:21:08 -070078egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
79
80egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080081 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070082}
83
84egl_display_t::~egl_display_t() {
85 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080086 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070087}
88
89egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080090 if (uintptr_t(dpy) == 0) {
91 return nullptr;
92 }
93
Mathias Agopian518ec112011-05-13 16:21:08 -070094 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070095 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
96 return nullptr;
97 }
98 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070099}
100
101void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800102 std::lock_guard<std::mutex> _l(lock);
103 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700104}
105
Mathias Agopian5b287a62011-05-16 18:58:55 -0700106void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800107 std::lock_guard<std::mutex> _l(lock);
108 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700109}
110
Mathias Agopianf0480de2011-11-13 20:50:07 -0800111bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800112 std::lock_guard<std::mutex> _l(lock);
113 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800114 if (object->getDisplay() == this) {
115 object->incRef();
116 return true;
117 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700118 }
119 return false;
120}
121
Mathias Agopian518ec112011-05-13 16:21:08 -0700122EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
123 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700124 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700125
126 return sDisplay[uintptr_t(disp)].getDisplay(disp);
127}
128
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600129static void addAnglePlatformAttributes(egl_connection_t* const cnx, const EGLAttrib* attrib_list,
130 std::vector<EGLAttrib>& attrs) {
131 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
132
133 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
134 if (attrib_list) {
135 while (*attrib_list != EGL_NONE) {
136 EGLAttrib attr = *attrib_list++;
137 EGLAttrib value = *attrib_list++;
138 if (attr == EGL_PLATFORM_ANGLE_TYPE_ANGLE) {
139 switch (value) {
140 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
141 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
142 break;
143 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
144 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
145 angleBackendDefault = value;
146 break;
147 default:
148 ALOGW("Invalid EGL_PLATFORM_ANGLE_TYPE_ANGLE attribute: 0x%" PRIxPTR,
149 value);
150 break;
151 }
152 }
153 }
154 }
155
156 cnx->angleBackend = angleBackendDefault;
157
158 // Allow debug property to override application's
159 char prop[PROPERTY_VALUE_MAX];
160 property_get("debug.angle.backend", prop, "0");
161 switch (atoi(prop)) {
162 case 1:
163 ALOGV("addAnglePlatformAttributes: Requesting OpenGLES back-end");
164 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
165 break;
166 case 2:
167 ALOGV("addAnglePlatformAttributes: Requesting Vulkan back-end");
168 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
169 break;
170 default:
171 break;
172 }
173
174 attrs.reserve(4 * 2);
175
176 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
177 attrs.push_back(cnx->angleBackend);
178
179 switch (cnx->angleBackend) {
180 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
181 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
182 property_get("debug.angle.validation", prop, "0");
183 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
184 attrs.push_back(atoi(prop));
185 break;
186 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
187 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
188 break;
189 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
190 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
191 // NOTE: This is only valid if the backend is OpenGL
192 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
193 attrs.push_back(vendorEGL);
194 break;
195 default:
196 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
197 break;
198 }
199 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
200 attrs.push_back(EGL_FALSE);
201}
202
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600203// Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
204bool initializeAnglePlatform(EGLDisplay dpy) {
205 // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
206 android_namespace_t* ns = android_getAngleNamespace();
207 const android_dlextinfo dlextinfo = {
208 .flags = ANDROID_DLEXT_USE_NAMESPACE,
209 .library_namespace = ns,
210 };
211 void* so = android_dlopen_ext("libEGL_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo);
212 angle::AnglePlatformImpl::angleGetDisplayPlatform =
213 reinterpret_cast<angle::GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
214
215 if (!angle::AnglePlatformImpl::angleGetDisplayPlatform) {
216 ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
217 return false;
218 }
219
220 angle::AnglePlatformImpl::angleResetDisplayPlatform =
221 reinterpret_cast<angle::ResetDisplayPlatformFunc>(
222 eglGetProcAddress("ANGLEResetDisplayPlatform"));
223
224 angle::PlatformMethods* platformMethods = nullptr;
225 if (!((angle::AnglePlatformImpl::angleGetDisplayPlatform)(dpy, angle::g_PlatformMethodNames,
226 angle::g_NumPlatformMethods, nullptr,
227 &platformMethods))) {
228 ALOGE("ANGLEGetDisplayPlatform call failed!");
229 return false;
230 }
231 if (platformMethods) {
232 angle::AnglePlatformImpl::assignAnglePlatformMethods(platformMethods);
233 } else {
234 ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
235 "tracing function ptrs!");
236 }
237 return true;
238}
239
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600240static EGLDisplay getDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600241 EGLDisplay dpy = EGL_NO_DISPLAY;
242
243 // Locally define this until EGL 1.5 is supported
244 typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYPROC)(EGLenum platform, void* native_display,
245 const EGLAttrib* attrib_list);
246
247 PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay =
248 reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>(
249 cnx->egl.eglGetProcAddress("eglGetPlatformDisplay"));
250
251 if (eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600252 std::vector<EGLAttrib> attrs;
253 addAnglePlatformAttributes(cnx, nullptr, attrs);
254 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600255
Cody Northrop1f00e172018-04-02 11:23:31 -0600256 dpy = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600257 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attrs.data());
258 if (dpy == EGL_NO_DISPLAY) {
259 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600260 } else {
261 if (!initializeAnglePlatform(dpy)) {
262 ALOGE("initializeAnglePlatform failed!");
263 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600264 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600265 } else {
266 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
267 display);
268 }
269
270 return dpy;
271}
272
Mathias Agopian518ec112011-05-13 16:21:08 -0700273EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
274
Mathias Agopian65421432017-03-08 11:49:05 -0800275 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800276 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700277
278 // get our driver loader
279 Loader& loader(Loader::getInstance());
280
Mathias Agopianada798b2012-02-13 17:09:30 -0800281 egl_connection_t* const cnx = &gEGLImpl;
282 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600283 EGLDisplay dpy = EGL_NO_DISPLAY;
284
285 if (cnx->useAngle) {
286 dpy = getDisplayAngle(display, cnx);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600287 }
288 if (dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600289 dpy = cnx->egl.eglGetDisplay(display);
290 }
291
Mathias Agopianada798b2012-02-13 17:09:30 -0800292 disp.dpy = dpy;
293 if (dpy == EGL_NO_DISPLAY) {
294 loader.close(cnx->dso);
Yi Kong48a6cd22018-07-18 10:07:09 -0700295 cnx->dso = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700296 }
297 }
298
299 return EGLDisplay(uintptr_t(display) + 1U);
300}
301
302EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
303
Mathias Agopian65421432017-03-08 11:49:05 -0800304 { // scope for refLock
305 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800306 refs++;
307 if (refs > 1) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700308 if (major != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800309 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700310 if (minor != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800311 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800312 while(!eglIsInitialized) {
313 refCond.wait(_l);
314 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800315 return EGL_TRUE;
316 }
Mathias Agopian65421432017-03-08 11:49:05 -0800317 while(eglIsInitialized) {
318 refCond.wait(_l);
319 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800320 }
321
Mathias Agopian65421432017-03-08 11:49:05 -0800322 { // scope for lock
323 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800324
Michael Lentine54466bc2015-01-27 09:01:03 -0800325 setGLHooksThreadSpecific(&gHooksNoContext);
326
327 // initialize each EGL and
328 // build our own extension string first, based on the extension we know
329 // and the extension supported by our client implementation
330
331 egl_connection_t* const cnx = &gEGLImpl;
332 cnx->major = -1;
333 cnx->minor = -1;
334 if (cnx->dso) {
335 EGLDisplay idpy = disp.dpy;
336 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
337 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
338 // idpy, cnx->major, cnx->minor, cnx);
339
340 // display is now initialized
341 disp.state = egl_display_t::INITIALIZED;
342
343 // get the query-strings for this display for each implementation
344 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
345 EGL_VENDOR);
346 disp.queryString.version = cnx->egl.eglQueryString(idpy,
347 EGL_VERSION);
348 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
349 EGL_EXTENSIONS);
350 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
351 EGL_CLIENT_APIS);
352
353 } else {
354 ALOGW("eglInitialize(%p) failed (%s)", idpy,
355 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
356 }
357 }
358
359 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800360 mVendorString = sVendorString;
361 mVersionString = sVersionString;
362 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800363
Mathias Agopian65421432017-03-08 11:49:05 -0800364 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600365
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700366 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
367
368 // Note: CDD requires that devices supporting wide color and/or HDR color also support
369 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600370 bool wideColorBoardConfig =
371 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
372 false);
373
374 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700375 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600376 mExtensionString.append(
377 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
378 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
379 }
380
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700381 bool hasHdrBoardConfig =
382 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
383
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700384 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700385 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
386 // Typically that means there is an HDR capable display attached, but could be
387 // support for attaching an HDR display. In either case, advertise support for
388 // HDR color spaces.
389 mExtensionString.append(
390 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
391 }
392
Michael Lentine54466bc2015-01-27 09:01:03 -0800393 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800394 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400395 // length of the extension name
396 size_t len = strcspn(start, " ");
397 if (len) {
398 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800399 const std::string ext(start, len);
400 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400401 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800402 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400403 // advance to the next extension name, skipping the space.
404 start += len;
405 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800406 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400407 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800408
409 egl_cache_t::get()->initialize(this);
410
411 char value[PROPERTY_VALUE_MAX];
412 property_get("debug.egl.finish", value, "0");
413 if (atoi(value)) {
414 finishOnSwap = true;
415 }
416
417 property_get("debug.egl.traceGpuCompletion", value, "0");
418 if (atoi(value)) {
419 traceGpuCompletion = true;
420 }
421
Yi Kong48a6cd22018-07-18 10:07:09 -0700422 if (major != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700423 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700424 if (minor != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700425 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700426 }
427
Mathias Agopian65421432017-03-08 11:49:05 -0800428 { // scope for refLock
429 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800430 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800431 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
433
Mathias Agopian7773c432012-02-13 20:06:08 -0800434 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700435}
436
437EGLBoolean egl_display_t::terminate() {
438
Mathias Agopian65421432017-03-08 11:49:05 -0800439 { // scope for refLock
440 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800441 if (refs == 0) {
442 /*
443 * From the EGL spec (3.2):
444 * "Termination of a display that has already been terminated,
445 * (...), is allowed, but the only effect of such a call is
446 * to return EGL_TRUE (...)
447 */
448 return EGL_TRUE;
449 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700450
Michael Lentine54466bc2015-01-27 09:01:03 -0800451 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700452 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800453 if (refs > 0) {
454 return EGL_TRUE;
455 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700456 }
457
458 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800459
Mathias Agopian65421432017-03-08 11:49:05 -0800460 { // scope for lock
461 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800462
463 egl_connection_t* const cnx = &gEGLImpl;
464 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600465 // If we're using ANGLE reset any custom DisplayPlatform
466 if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) {
467 (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy);
468 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800469 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
470 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
471 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
472 }
473 // REVISIT: it's unclear what to do if eglTerminate() fails
474 disp.state = egl_display_t::TERMINATED;
475 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700476 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800477
Michael Lentine54466bc2015-01-27 09:01:03 -0800478 // Reset the extension string since it will be regenerated if we get
479 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800480 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800481
482 // Mark all objects remaining in the list as terminated, unless
483 // there are no reference to them, it which case, we're free to
484 // delete them.
485 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800486 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800487 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800488 o->destroy();
489 }
490
491 // this marks all object handles are "terminated"
492 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700493 }
494
Mathias Agopian65421432017-03-08 11:49:05 -0800495 { // scope for refLock
496 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800497 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800498 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700499 }
500
Mathias Agopian518ec112011-05-13 16:21:08 -0700501 return res;
502}
503
Mathias Agopianfb87e542012-01-30 18:20:52 -0800504void egl_display_t::loseCurrent(egl_context_t * cur_c)
505{
506 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800507 egl_display_t* display = cur_c->getDisplay();
508 if (display) {
509 display->loseCurrentImpl(cur_c);
510 }
511 }
512}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800513
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800514void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
515{
516 // by construction, these are either 0 or valid (possibly terminated)
517 // it should be impossible for these to be invalid
518 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700519 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
520 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800521
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800522 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800523 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800524 cur_c->onLooseCurrent();
525
Mathias Agopianfb87e542012-01-30 18:20:52 -0800526 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800527
528 // This cannot be called with the lock held because it might end-up
529 // calling back into EGL (in particular when a surface is destroyed
530 // it calls ANativeWindow::disconnect
531 _cur_c.release();
532 _cur_r.release();
533 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800534}
535
536EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700537 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800538 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
539{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800540 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800541
542 // by construction, these are either 0 or valid (possibly terminated)
543 // it should be impossible for these to be invalid
544 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700545 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
546 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800547
548 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800549 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800550 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800551 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800552 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800553 if (result == EGL_TRUE) {
554 c->onMakeCurrent(draw, read);
555 }
556 } else {
557 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800558 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800559 if (result == EGL_TRUE) {
560 cur_c->onLooseCurrent();
561 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800562 }
563 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800564
565 if (result == EGL_TRUE) {
566 // This cannot be called with the lock held because it might end-up
567 // calling back into EGL (in particular when a surface is destroyed
568 // it calls ANativeWindow::disconnect
569 _cur_c.release();
570 _cur_r.release();
571 _cur_d.release();
572 }
573
Mathias Agopianfb87e542012-01-30 18:20:52 -0800574 return result;
575}
Mathias Agopian518ec112011-05-13 16:21:08 -0700576
Jesse Hallc2e41222013-08-08 13:40:22 -0700577bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
578 if (!nameLen) {
579 nameLen = strlen(name);
580 }
Mathias Agopian65421432017-03-08 11:49:05 -0800581 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700582}
583
Jesse Halla0fef1c2012-04-17 12:02:26 -0700584// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700585}; // namespace android
586// ----------------------------------------------------------------------------