blob: d1635b727db17c34f9d3867a269394a973aa7ed0 [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
Jamie Gennisaca51c02011-11-03 17:42:43 -070027#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070028#include "egl_object.h"
29#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080030#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080033
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -060034#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
35#include <configstore/Utils.h>
36
37using namespace android::hardware::configstore;
38using namespace android::hardware::configstore::V1_0;
39
Mathias Agopian518ec112011-05-13 16:21:08 -070040// ----------------------------------------------------------------------------
41namespace android {
42// ----------------------------------------------------------------------------
43
Mathias Agopian4b9511c2011-11-13 23:52:47 -080044static char const * const sVendorString = "Android";
45static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070046static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080047
Jesse Hall21558da2013-08-06 15:31:22 -070048extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070049extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080050
Mathias Agopian518ec112011-05-13 16:21:08 -070051extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
52
Mathias Agopian518ec112011-05-13 16:21:08 -070053// ----------------------------------------------------------------------------
54
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070055bool findExtension(const char* exts, const char* name, size_t nameLen) {
Jesse Hallc2e41222013-08-08 13:40:22 -070056 if (exts) {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070057 if (!nameLen) {
58 nameLen = strlen(name);
59 }
Kalle Raita7804aa22016-04-18 16:03:37 -070060 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
61 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
62 return true;
63 }
Jesse Hallc2e41222013-08-08 13:40:22 -070064 }
65 }
66 return false;
67}
68
Mathias Agopianb7f9a242017-03-08 22:29:31 -080069int egl_get_init_count(EGLDisplay dpy) {
70 egl_display_t* eglDisplay = egl_display_t::get(dpy);
71 return eglDisplay ? eglDisplay->getRefsCount() : 0;
72}
73
Mathias Agopian518ec112011-05-13 16:21:08 -070074egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
75
76egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080077 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070078}
79
80egl_display_t::~egl_display_t() {
81 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080082 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070083}
84
85egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano7025b542017-12-06 14:19:44 -080086 if (uintptr_t(dpy) == 0) {
87 return nullptr;
88 }
89
Mathias Agopian518ec112011-05-13 16:21:08 -070090 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070091 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
92 return nullptr;
93 }
94 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070095}
96
97void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080098 std::lock_guard<std::mutex> _l(lock);
99 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -0700100}
101
Mathias Agopian5b287a62011-05-16 18:58:55 -0700102void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -0800103 std::lock_guard<std::mutex> _l(lock);
104 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700105}
106
Mathias Agopianf0480de2011-11-13 20:50:07 -0800107bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800108 std::lock_guard<std::mutex> _l(lock);
109 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800110 if (object->getDisplay() == this) {
111 object->incRef();
112 return true;
113 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700114 }
115 return false;
116}
117
Mathias Agopian518ec112011-05-13 16:21:08 -0700118EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
119 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700120 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700121
122 return sDisplay[uintptr_t(disp)].getDisplay(disp);
123}
124
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600125static void addAnglePlatformAttributes(egl_connection_t* const cnx, const EGLAttrib* attrib_list,
126 std::vector<EGLAttrib>& attrs) {
127 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
128
129 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
130 if (attrib_list) {
131 while (*attrib_list != EGL_NONE) {
132 EGLAttrib attr = *attrib_list++;
133 EGLAttrib value = *attrib_list++;
134 if (attr == EGL_PLATFORM_ANGLE_TYPE_ANGLE) {
135 switch (value) {
136 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
137 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
138 break;
139 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
140 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
141 angleBackendDefault = value;
142 break;
143 default:
144 ALOGW("Invalid EGL_PLATFORM_ANGLE_TYPE_ANGLE attribute: 0x%" PRIxPTR,
145 value);
146 break;
147 }
148 }
149 }
150 }
151
152 cnx->angleBackend = angleBackendDefault;
153
154 // Allow debug property to override application's
155 char prop[PROPERTY_VALUE_MAX];
156 property_get("debug.angle.backend", prop, "0");
157 switch (atoi(prop)) {
158 case 1:
159 ALOGV("addAnglePlatformAttributes: Requesting OpenGLES back-end");
160 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
161 break;
162 case 2:
163 ALOGV("addAnglePlatformAttributes: Requesting Vulkan back-end");
164 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
165 break;
166 default:
167 break;
168 }
169
170 attrs.reserve(4 * 2);
171
172 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
173 attrs.push_back(cnx->angleBackend);
174
175 switch (cnx->angleBackend) {
176 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
177 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
178 property_get("debug.angle.validation", prop, "0");
179 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
180 attrs.push_back(atoi(prop));
181 break;
182 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
183 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
184 break;
185 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
186 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
187 // NOTE: This is only valid if the backend is OpenGL
188 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
189 attrs.push_back(vendorEGL);
190 break;
191 default:
192 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
193 break;
194 }
195 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
196 attrs.push_back(EGL_FALSE);
197}
198
199static EGLDisplay getDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600200 EGLDisplay dpy = EGL_NO_DISPLAY;
201
202 // Locally define this until EGL 1.5 is supported
203 typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYPROC)(EGLenum platform, void* native_display,
204 const EGLAttrib* attrib_list);
205
206 PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay =
207 reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>(
208 cnx->egl.eglGetProcAddress("eglGetPlatformDisplay"));
209
210 if (eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600211 std::vector<EGLAttrib> attrs;
212 addAnglePlatformAttributes(cnx, nullptr, attrs);
213 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600214
Cody Northrop1f00e172018-04-02 11:23:31 -0600215 dpy = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600216 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attrs.data());
217 if (dpy == EGL_NO_DISPLAY) {
218 ALOGE("eglGetPlatformDisplay failed!");
219 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600220 } else {
221 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
222 display);
223 }
224
225 return dpy;
226}
227
Mathias Agopian518ec112011-05-13 16:21:08 -0700228EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
229
Mathias Agopian65421432017-03-08 11:49:05 -0800230 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800231 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700232
233 // get our driver loader
234 Loader& loader(Loader::getInstance());
235
Mathias Agopianada798b2012-02-13 17:09:30 -0800236 egl_connection_t* const cnx = &gEGLImpl;
237 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600238 EGLDisplay dpy = EGL_NO_DISPLAY;
239
240 if (cnx->useAngle) {
241 dpy = getDisplayAngle(display, cnx);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600242 }
243 if (dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600244 dpy = cnx->egl.eglGetDisplay(display);
245 }
246
Mathias Agopianada798b2012-02-13 17:09:30 -0800247 disp.dpy = dpy;
248 if (dpy == EGL_NO_DISPLAY) {
249 loader.close(cnx->dso);
Yi Kong48a6cd22018-07-18 10:07:09 -0700250 cnx->dso = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700251 }
252 }
253
254 return EGLDisplay(uintptr_t(display) + 1U);
255}
256
257EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
258
Mathias Agopian65421432017-03-08 11:49:05 -0800259 { // scope for refLock
260 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800261 refs++;
262 if (refs > 1) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700263 if (major != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800264 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700265 if (minor != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800266 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800267 while(!eglIsInitialized) {
268 refCond.wait(_l);
269 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800270 return EGL_TRUE;
271 }
Mathias Agopian65421432017-03-08 11:49:05 -0800272 while(eglIsInitialized) {
273 refCond.wait(_l);
274 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800275 }
276
Mathias Agopian65421432017-03-08 11:49:05 -0800277 { // scope for lock
278 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800279
Michael Lentine54466bc2015-01-27 09:01:03 -0800280 setGLHooksThreadSpecific(&gHooksNoContext);
281
282 // initialize each EGL and
283 // build our own extension string first, based on the extension we know
284 // and the extension supported by our client implementation
285
286 egl_connection_t* const cnx = &gEGLImpl;
287 cnx->major = -1;
288 cnx->minor = -1;
289 if (cnx->dso) {
290 EGLDisplay idpy = disp.dpy;
291 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
292 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
293 // idpy, cnx->major, cnx->minor, cnx);
294
295 // display is now initialized
296 disp.state = egl_display_t::INITIALIZED;
297
298 // get the query-strings for this display for each implementation
299 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
300 EGL_VENDOR);
301 disp.queryString.version = cnx->egl.eglQueryString(idpy,
302 EGL_VERSION);
303 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
304 EGL_EXTENSIONS);
305 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
306 EGL_CLIENT_APIS);
307
308 } else {
309 ALOGW("eglInitialize(%p) failed (%s)", idpy,
310 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
311 }
312 }
313
314 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800315 mVendorString = sVendorString;
316 mVersionString = sVersionString;
317 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800318
Mathias Agopian65421432017-03-08 11:49:05 -0800319 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600320
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700321 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
322
323 // Note: CDD requires that devices supporting wide color and/or HDR color also support
324 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600325 bool wideColorBoardConfig =
326 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
327 false);
328
329 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700330 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600331 mExtensionString.append(
332 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
333 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
334 }
335
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700336 bool hasHdrBoardConfig =
337 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
338
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700339 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700340 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
341 // Typically that means there is an HDR capable display attached, but could be
342 // support for attaching an HDR display. In either case, advertise support for
343 // HDR color spaces.
344 mExtensionString.append(
345 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
346 }
347
Michael Lentine54466bc2015-01-27 09:01:03 -0800348 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800349 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400350 // length of the extension name
351 size_t len = strcspn(start, " ");
352 if (len) {
353 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800354 const std::string ext(start, len);
355 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400356 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800357 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400358 // advance to the next extension name, skipping the space.
359 start += len;
360 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800361 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400362 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800363
364 egl_cache_t::get()->initialize(this);
365
366 char value[PROPERTY_VALUE_MAX];
367 property_get("debug.egl.finish", value, "0");
368 if (atoi(value)) {
369 finishOnSwap = true;
370 }
371
372 property_get("debug.egl.traceGpuCompletion", value, "0");
373 if (atoi(value)) {
374 traceGpuCompletion = true;
375 }
376
Yi Kong48a6cd22018-07-18 10:07:09 -0700377 if (major != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700378 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700379 if (minor != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700380 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700381 }
382
Mathias Agopian65421432017-03-08 11:49:05 -0800383 { // scope for refLock
384 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800385 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800386 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700387 }
388
Mathias Agopian7773c432012-02-13 20:06:08 -0800389 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700390}
391
392EGLBoolean egl_display_t::terminate() {
393
Mathias Agopian65421432017-03-08 11:49:05 -0800394 { // scope for refLock
395 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800396 if (refs == 0) {
397 /*
398 * From the EGL spec (3.2):
399 * "Termination of a display that has already been terminated,
400 * (...), is allowed, but the only effect of such a call is
401 * to return EGL_TRUE (...)
402 */
403 return EGL_TRUE;
404 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700405
Michael Lentine54466bc2015-01-27 09:01:03 -0800406 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700407 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800408 if (refs > 0) {
409 return EGL_TRUE;
410 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700411 }
412
413 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800414
Mathias Agopian65421432017-03-08 11:49:05 -0800415 { // scope for lock
416 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800417
418 egl_connection_t* const cnx = &gEGLImpl;
419 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
420 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
421 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
422 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
423 }
424 // REVISIT: it's unclear what to do if eglTerminate() fails
425 disp.state = egl_display_t::TERMINATED;
426 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700427 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800428
Michael Lentine54466bc2015-01-27 09:01:03 -0800429 // Reset the extension string since it will be regenerated if we get
430 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800431 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800432
433 // Mark all objects remaining in the list as terminated, unless
434 // there are no reference to them, it which case, we're free to
435 // delete them.
436 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800437 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800438 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800439 o->destroy();
440 }
441
442 // this marks all object handles are "terminated"
443 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700444 }
445
Mathias Agopian65421432017-03-08 11:49:05 -0800446 { // scope for refLock
447 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800448 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800449 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700450 }
451
Mathias Agopian518ec112011-05-13 16:21:08 -0700452 return res;
453}
454
Mathias Agopianfb87e542012-01-30 18:20:52 -0800455void egl_display_t::loseCurrent(egl_context_t * cur_c)
456{
457 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800458 egl_display_t* display = cur_c->getDisplay();
459 if (display) {
460 display->loseCurrentImpl(cur_c);
461 }
462 }
463}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800464
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800465void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
466{
467 // by construction, these are either 0 or valid (possibly terminated)
468 // it should be impossible for these to be invalid
469 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
471 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800472
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800473 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800474 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800475 cur_c->onLooseCurrent();
476
Mathias Agopianfb87e542012-01-30 18:20:52 -0800477 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800478
479 // This cannot be called with the lock held because it might end-up
480 // calling back into EGL (in particular when a surface is destroyed
481 // it calls ANativeWindow::disconnect
482 _cur_c.release();
483 _cur_r.release();
484 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800485}
486
487EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700488 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800489 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
490{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800491 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800492
493 // by construction, these are either 0 or valid (possibly terminated)
494 // it should be impossible for these to be invalid
495 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700496 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
497 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800498
499 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800500 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800501 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800502 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800503 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800504 if (result == EGL_TRUE) {
505 c->onMakeCurrent(draw, read);
506 }
507 } else {
508 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800509 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800510 if (result == EGL_TRUE) {
511 cur_c->onLooseCurrent();
512 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800513 }
514 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800515
516 if (result == EGL_TRUE) {
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();
523 }
524
Mathias Agopianfb87e542012-01-30 18:20:52 -0800525 return result;
526}
Mathias Agopian518ec112011-05-13 16:21:08 -0700527
Jesse Hallc2e41222013-08-08 13:40:22 -0700528bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
529 if (!nameLen) {
530 nameLen = strlen(name);
531 }
Mathias Agopian65421432017-03-08 11:49:05 -0800532 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700533}
534
Jesse Halla0fef1c2012-04-17 12:02:26 -0700535// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700536}; // namespace android
537// ----------------------------------------------------------------------------