blob: 4433af0826524e1617c8fffd8a50ee02e0cae640 [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
317 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800318 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600319 mVersionString.clear();
320 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
321 if ((cnx->major == 1) && (cnx->minor == 5)) {
322 mVersionString = sVersionString15;
323 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
324 } else if ((cnx->major == 1) && (cnx->minor == 4)) {
325 mVersionString = sVersionString14;
326 // Extensions needed for an EGL 1.4 implementation to be
327 // able to support EGL 1.5 functionality
328 std::vector<const char*> egl15extensions = {
329 "EGL_EXT_client_extensions",
330 // "EGL_EXT_platform_base", // implemented by EGL runtime
331 "EGL_KHR_image_base",
332 "EGL_KHR_fence_sync",
333 "EGL_KHR_wait_sync",
334 "EGL_KHR_create_context",
335 "EGL_EXT_create_context_robustness",
336 "EGL_KHR_gl_colorspace",
337 "EGL_ANDROID_native_fence_sync",
338 };
339 bool extensionsFound = true;
340 for (const auto& name : egl15extensions) {
341 extensionsFound &= findExtension(disp.queryString.extensions, name);
342 ALOGV("Extension %s: %s", name,
343 findExtension(disp.queryString.extensions, name) ? "Found" : "Missing");
344 }
345 // NOTE: From the spec:
346 // Creation of fence sync objects requires support from the bound
347 // client API, and will not succeed unless the client API satisfies:
348 // client API is OpenGL ES, and either the OpenGL ES version is 3.0
349 // or greater, or the GL_OES_EGL_sync extension is supported.
350 // We don't have a way to check the GL_EXTENSIONS string at this
351 // point in the code, assume that GL_OES_EGL_sync is supported
352 // because EGL_KHR_fence_sync is supported (as verified above).
353 if (extensionsFound) {
354 // Have everything needed to emulate EGL 1.5 so report EGL 1.5
355 // to the application.
356 mVersionString = sVersionString15;
357 cnx->major = 1;
358 cnx->minor = 5;
359 }
360 }
361 if (mVersionString.empty()) {
362 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
363 mVersionString = sVersionString14;
364 }
Mathias Agopian65421432017-03-08 11:49:05 -0800365 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800366
Mathias Agopian65421432017-03-08 11:49:05 -0800367 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600368
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700369 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
370
371 // Note: CDD requires that devices supporting wide color and/or HDR color also support
372 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600373 bool wideColorBoardConfig =
374 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
375 false);
376
377 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700378 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600379 mExtensionString.append(
380 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
381 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
382 }
383
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700384 bool hasHdrBoardConfig =
385 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
386
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700387 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700388 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
389 // Typically that means there is an HDR capable display attached, but could be
390 // support for attaching an HDR display. In either case, advertise support for
391 // HDR color spaces.
392 mExtensionString.append(
393 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
394 }
395
Michael Lentine54466bc2015-01-27 09:01:03 -0800396 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800397 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400398 // length of the extension name
399 size_t len = strcspn(start, " ");
400 if (len) {
401 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800402 const std::string ext(start, len);
403 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400404 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800405 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400406 // advance to the next extension name, skipping the space.
407 start += len;
408 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800409 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400410 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800411
412 egl_cache_t::get()->initialize(this);
413
414 char value[PROPERTY_VALUE_MAX];
415 property_get("debug.egl.finish", value, "0");
416 if (atoi(value)) {
417 finishOnSwap = true;
418 }
419
420 property_get("debug.egl.traceGpuCompletion", value, "0");
421 if (atoi(value)) {
422 traceGpuCompletion = true;
423 }
424
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600425 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
426 // changing the behavior from the past where we always advertise
427 // version 1.4. May need to check that revision is valid
428 // before using cnx->major & cnx->minor
429 if (major != nullptr) *major = cnx->major;
430 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700431 }
432
Mathias Agopian65421432017-03-08 11:49:05 -0800433 { // scope for refLock
434 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800435 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800436 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700437 }
438
Mathias Agopian7773c432012-02-13 20:06:08 -0800439 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700440}
441
442EGLBoolean egl_display_t::terminate() {
443
Mathias Agopian65421432017-03-08 11:49:05 -0800444 { // scope for refLock
445 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800446 if (refs == 0) {
447 /*
448 * From the EGL spec (3.2):
449 * "Termination of a display that has already been terminated,
450 * (...), is allowed, but the only effect of such a call is
451 * to return EGL_TRUE (...)
452 */
453 return EGL_TRUE;
454 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700455
Michael Lentine54466bc2015-01-27 09:01:03 -0800456 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700457 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800458 if (refs > 0) {
459 return EGL_TRUE;
460 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700461 }
462
463 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800464
Mathias Agopian65421432017-03-08 11:49:05 -0800465 { // scope for lock
466 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800467
468 egl_connection_t* const cnx = &gEGLImpl;
469 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600470 // If we're using ANGLE reset any custom DisplayPlatform
Tobin Ehlis75ce4772018-11-20 09:48:47 -0700471 if (cnx->useAngle) {
472 angle::resetAnglePlatform(disp.dpy);
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600473 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800474 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
475 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
476 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
477 }
478 // REVISIT: it's unclear what to do if eglTerminate() fails
479 disp.state = egl_display_t::TERMINATED;
480 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700481 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800482
Michael Lentine54466bc2015-01-27 09:01:03 -0800483 // Reset the extension string since it will be regenerated if we get
484 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800485 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800486
487 // Mark all objects remaining in the list as terminated, unless
488 // there are no reference to them, it which case, we're free to
489 // delete them.
490 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800491 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800492 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800493 o->destroy();
494 }
495
496 // this marks all object handles are "terminated"
497 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 }
499
Mathias Agopian65421432017-03-08 11:49:05 -0800500 { // scope for refLock
501 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800502 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800503 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700504 }
505
Mathias Agopian518ec112011-05-13 16:21:08 -0700506 return res;
507}
508
Mathias Agopianfb87e542012-01-30 18:20:52 -0800509void egl_display_t::loseCurrent(egl_context_t * cur_c)
510{
511 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800512 egl_display_t* display = cur_c->getDisplay();
513 if (display) {
514 display->loseCurrentImpl(cur_c);
515 }
516 }
517}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800518
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800519void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
520{
521 // by construction, these are either 0 or valid (possibly terminated)
522 // it should be impossible for these to be invalid
523 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700524 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
525 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800526
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800527 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800528 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800529 cur_c->onLooseCurrent();
530
Mathias Agopianfb87e542012-01-30 18:20:52 -0800531 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800532
533 // This cannot be called with the lock held because it might end-up
534 // calling back into EGL (in particular when a surface is destroyed
535 // it calls ANativeWindow::disconnect
536 _cur_c.release();
537 _cur_r.release();
538 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800539}
540
541EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700542 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800543 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
544{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800545 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800546
547 // by construction, these are either 0 or valid (possibly terminated)
548 // it should be impossible for these to be invalid
549 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700550 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
551 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800552
553 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800554 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800555 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800556 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800557 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800558 if (result == EGL_TRUE) {
559 c->onMakeCurrent(draw, read);
560 }
561 } else {
562 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800563 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800564 if (result == EGL_TRUE) {
565 cur_c->onLooseCurrent();
566 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800567 }
568 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800569
570 if (result == EGL_TRUE) {
571 // This cannot be called with the lock held because it might end-up
572 // calling back into EGL (in particular when a surface is destroyed
573 // it calls ANativeWindow::disconnect
574 _cur_c.release();
575 _cur_r.release();
576 _cur_d.release();
577 }
578
Mathias Agopianfb87e542012-01-30 18:20:52 -0800579 return result;
580}
Mathias Agopian518ec112011-05-13 16:21:08 -0700581
Jesse Hallc2e41222013-08-08 13:40:22 -0700582bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
583 if (!nameLen) {
584 nameLen = strlen(name);
585 }
Mathias Agopian65421432017-03-08 11:49:05 -0800586 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700587}
588
Jesse Halla0fef1c2012-04-17 12:02:26 -0700589// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700590}; // namespace android
591// ----------------------------------------------------------------------------