blob: 94af2e7bb05323163214ee1b74fc1d865645a454 [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
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600167// Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
168bool initializeAnglePlatform(EGLDisplay dpy) {
169 // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600170 android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600171 const android_dlextinfo dlextinfo = {
172 .flags = ANDROID_DLEXT_USE_NAMESPACE,
173 .library_namespace = ns,
174 };
175 void* so = android_dlopen_ext("libEGL_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo);
176 angle::AnglePlatformImpl::angleGetDisplayPlatform =
177 reinterpret_cast<angle::GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
178
179 if (!angle::AnglePlatformImpl::angleGetDisplayPlatform) {
180 ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
181 return false;
182 }
183
184 angle::AnglePlatformImpl::angleResetDisplayPlatform =
185 reinterpret_cast<angle::ResetDisplayPlatformFunc>(
186 eglGetProcAddress("ANGLEResetDisplayPlatform"));
187
188 angle::PlatformMethods* platformMethods = nullptr;
189 if (!((angle::AnglePlatformImpl::angleGetDisplayPlatform)(dpy, angle::g_PlatformMethodNames,
190 angle::g_NumPlatformMethods, nullptr,
191 &platformMethods))) {
192 ALOGE("ANGLEGetDisplayPlatform call failed!");
193 return false;
194 }
195 if (platformMethods) {
196 angle::AnglePlatformImpl::assignAnglePlatformMethods(platformMethods);
197 } else {
198 ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
199 "tracing function ptrs!");
200 }
201 return true;
202}
203
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600204static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
205 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600206 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600207 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600208
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600209 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600210 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600211 if (attrib_list) {
212 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
213 attrs.push_back(attr[0]);
214 attrs.push_back(attr[1]);
215 }
216 }
217
Courtney Goeltzenleuchterb91b9892018-10-23 16:28:00 -0600218 if (!addAnglePlatformAttributes(cnx, attrs)) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600219 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
220 *error = EGL_BAD_PARAMETER;
221 return EGL_NO_DISPLAY;
222 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600223 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600224
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600225 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
226 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
227 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600228 if (dpy == EGL_NO_DISPLAY) {
229 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600230 } else {
231 if (!initializeAnglePlatform(dpy)) {
232 ALOGE("initializeAnglePlatform failed!");
233 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600234 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600235 } else {
236 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
237 display);
238 }
239
240 return dpy;
241}
242
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600243EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
244 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800245 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800246 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700247
248 // get our driver loader
249 Loader& loader(Loader::getInstance());
250
Mathias Agopianada798b2012-02-13 17:09:30 -0800251 egl_connection_t* const cnx = &gEGLImpl;
252 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600253 EGLDisplay dpy = EGL_NO_DISPLAY;
254
255 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600256 EGLint error;
257 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
258 if (error != EGL_NONE) {
259 return setError(error, dpy);
260 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600261 }
262 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600263 // NOTE: eglGetPlatformDisplay with a empty attribute list
264 // behaves the same as eglGetDisplay
265 if (cnx->egl.eglGetPlatformDisplay) {
266 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
267 attrib_list);
268 } else {
269 if (attrib_list) {
270 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
271 }
272 dpy = cnx->egl.eglGetDisplay(display);
273 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600274 }
275
Mathias Agopianada798b2012-02-13 17:09:30 -0800276 disp.dpy = dpy;
277 if (dpy == EGL_NO_DISPLAY) {
278 loader.close(cnx->dso);
Yi Kong48a6cd22018-07-18 10:07:09 -0700279 cnx->dso = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 }
281 }
282
283 return EGLDisplay(uintptr_t(display) + 1U);
284}
285
286EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
287
Mathias Agopian65421432017-03-08 11:49:05 -0800288 { // scope for refLock
289 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800290 refs++;
291 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600292 // We don't know what to report until we know what the
293 // driver supports. Make sure we are initialized before
294 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800295 while(!eglIsInitialized) {
296 refCond.wait(_l);
297 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600298 egl_connection_t* const cnx = &gEGLImpl;
299
300 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
301 // changing the behavior from the past where we always advertise
302 // version 1.4. May need to check that revision is valid
303 // before using cnx->major & cnx->minor
304 if (major != nullptr) *major = cnx->major;
305 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800306 return EGL_TRUE;
307 }
Mathias Agopian65421432017-03-08 11:49:05 -0800308 while(eglIsInitialized) {
309 refCond.wait(_l);
310 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800311 }
312
Mathias Agopian65421432017-03-08 11:49:05 -0800313 { // scope for lock
314 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800315
Michael Lentine54466bc2015-01-27 09:01:03 -0800316 setGLHooksThreadSpecific(&gHooksNoContext);
317
318 // initialize each EGL and
319 // build our own extension string first, based on the extension we know
320 // and the extension supported by our client implementation
321
322 egl_connection_t* const cnx = &gEGLImpl;
323 cnx->major = -1;
324 cnx->minor = -1;
325 if (cnx->dso) {
326 EGLDisplay idpy = disp.dpy;
327 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
328 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
329 // idpy, cnx->major, cnx->minor, cnx);
330
331 // display is now initialized
332 disp.state = egl_display_t::INITIALIZED;
333
334 // get the query-strings for this display for each implementation
335 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
336 EGL_VENDOR);
337 disp.queryString.version = cnx->egl.eglQueryString(idpy,
338 EGL_VERSION);
339 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
340 EGL_EXTENSIONS);
341 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
342 EGL_CLIENT_APIS);
343
344 } else {
345 ALOGW("eglInitialize(%p) failed (%s)", idpy,
346 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
347 }
348 }
349
350 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800351 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600352 mVersionString.clear();
353 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
354 if ((cnx->major == 1) && (cnx->minor == 5)) {
355 mVersionString = sVersionString15;
356 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
357 } else if ((cnx->major == 1) && (cnx->minor == 4)) {
358 mVersionString = sVersionString14;
359 // Extensions needed for an EGL 1.4 implementation to be
360 // able to support EGL 1.5 functionality
361 std::vector<const char*> egl15extensions = {
362 "EGL_EXT_client_extensions",
363 // "EGL_EXT_platform_base", // implemented by EGL runtime
364 "EGL_KHR_image_base",
365 "EGL_KHR_fence_sync",
366 "EGL_KHR_wait_sync",
367 "EGL_KHR_create_context",
368 "EGL_EXT_create_context_robustness",
369 "EGL_KHR_gl_colorspace",
370 "EGL_ANDROID_native_fence_sync",
371 };
372 bool extensionsFound = true;
373 for (const auto& name : egl15extensions) {
374 extensionsFound &= findExtension(disp.queryString.extensions, name);
375 ALOGV("Extension %s: %s", name,
376 findExtension(disp.queryString.extensions, name) ? "Found" : "Missing");
377 }
378 // NOTE: From the spec:
379 // Creation of fence sync objects requires support from the bound
380 // client API, and will not succeed unless the client API satisfies:
381 // client API is OpenGL ES, and either the OpenGL ES version is 3.0
382 // or greater, or the GL_OES_EGL_sync extension is supported.
383 // We don't have a way to check the GL_EXTENSIONS string at this
384 // point in the code, assume that GL_OES_EGL_sync is supported
385 // because EGL_KHR_fence_sync is supported (as verified above).
386 if (extensionsFound) {
387 // Have everything needed to emulate EGL 1.5 so report EGL 1.5
388 // to the application.
389 mVersionString = sVersionString15;
390 cnx->major = 1;
391 cnx->minor = 5;
392 }
393 }
394 if (mVersionString.empty()) {
395 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
396 mVersionString = sVersionString14;
397 }
Mathias Agopian65421432017-03-08 11:49:05 -0800398 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800399
Mathias Agopian65421432017-03-08 11:49:05 -0800400 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600401
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700402 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
403
404 // Note: CDD requires that devices supporting wide color and/or HDR color also support
405 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600406 bool wideColorBoardConfig =
407 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
408 false);
409
410 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700411 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600412 mExtensionString.append(
413 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
414 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
415 }
416
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700417 bool hasHdrBoardConfig =
418 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
419
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700420 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700421 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
422 // Typically that means there is an HDR capable display attached, but could be
423 // support for attaching an HDR display. In either case, advertise support for
424 // HDR color spaces.
425 mExtensionString.append(
426 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
427 }
428
Michael Lentine54466bc2015-01-27 09:01:03 -0800429 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800430 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400431 // length of the extension name
432 size_t len = strcspn(start, " ");
433 if (len) {
434 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800435 const std::string ext(start, len);
436 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400437 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800438 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400439 // advance to the next extension name, skipping the space.
440 start += len;
441 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800442 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400443 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800444
445 egl_cache_t::get()->initialize(this);
446
447 char value[PROPERTY_VALUE_MAX];
448 property_get("debug.egl.finish", value, "0");
449 if (atoi(value)) {
450 finishOnSwap = true;
451 }
452
453 property_get("debug.egl.traceGpuCompletion", value, "0");
454 if (atoi(value)) {
455 traceGpuCompletion = true;
456 }
457
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600458 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
459 // changing the behavior from the past where we always advertise
460 // version 1.4. May need to check that revision is valid
461 // before using cnx->major & cnx->minor
462 if (major != nullptr) *major = cnx->major;
463 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700464 }
465
Mathias Agopian65421432017-03-08 11:49:05 -0800466 { // scope for refLock
467 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800468 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800469 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700470 }
471
Mathias Agopian7773c432012-02-13 20:06:08 -0800472 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700473}
474
475EGLBoolean egl_display_t::terminate() {
476
Mathias Agopian65421432017-03-08 11:49:05 -0800477 { // scope for refLock
478 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800479 if (refs == 0) {
480 /*
481 * From the EGL spec (3.2):
482 * "Termination of a display that has already been terminated,
483 * (...), is allowed, but the only effect of such a call is
484 * to return EGL_TRUE (...)
485 */
486 return EGL_TRUE;
487 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700488
Michael Lentine54466bc2015-01-27 09:01:03 -0800489 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700490 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800491 if (refs > 0) {
492 return EGL_TRUE;
493 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700494 }
495
496 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800497
Mathias Agopian65421432017-03-08 11:49:05 -0800498 { // scope for lock
499 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800500
501 egl_connection_t* const cnx = &gEGLImpl;
502 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600503 // If we're using ANGLE reset any custom DisplayPlatform
504 if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) {
505 (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy);
506 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800507 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
508 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
509 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
510 }
511 // REVISIT: it's unclear what to do if eglTerminate() fails
512 disp.state = egl_display_t::TERMINATED;
513 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700514 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800515
Michael Lentine54466bc2015-01-27 09:01:03 -0800516 // Reset the extension string since it will be regenerated if we get
517 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800518 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800519
520 // Mark all objects remaining in the list as terminated, unless
521 // there are no reference to them, it which case, we're free to
522 // delete them.
523 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800524 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800525 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800526 o->destroy();
527 }
528
529 // this marks all object handles are "terminated"
530 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700531 }
532
Mathias Agopian65421432017-03-08 11:49:05 -0800533 { // scope for refLock
534 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800535 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800536 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700537 }
538
Mathias Agopian518ec112011-05-13 16:21:08 -0700539 return res;
540}
541
Mathias Agopianfb87e542012-01-30 18:20:52 -0800542void egl_display_t::loseCurrent(egl_context_t * cur_c)
543{
544 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800545 egl_display_t* display = cur_c->getDisplay();
546 if (display) {
547 display->loseCurrentImpl(cur_c);
548 }
549 }
550}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800551
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800552void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
553{
554 // by construction, these are either 0 or valid (possibly terminated)
555 // it should be impossible for these to be invalid
556 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700557 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
558 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800559
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800560 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800561 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800562 cur_c->onLooseCurrent();
563
Mathias Agopianfb87e542012-01-30 18:20:52 -0800564 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800565
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();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800572}
573
574EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700575 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800576 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
577{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800578 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800579
580 // by construction, these are either 0 or valid (possibly terminated)
581 // it should be impossible for these to be invalid
582 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700583 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
584 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800585
586 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800587 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800588 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800589 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800590 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800591 if (result == EGL_TRUE) {
592 c->onMakeCurrent(draw, read);
593 }
594 } else {
595 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800596 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800597 if (result == EGL_TRUE) {
598 cur_c->onLooseCurrent();
599 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800600 }
601 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800602
603 if (result == EGL_TRUE) {
604 // This cannot be called with the lock held because it might end-up
605 // calling back into EGL (in particular when a surface is destroyed
606 // it calls ANativeWindow::disconnect
607 _cur_c.release();
608 _cur_r.release();
609 _cur_d.release();
610 }
611
Mathias Agopianfb87e542012-01-30 18:20:52 -0800612 return result;
613}
Mathias Agopian518ec112011-05-13 16:21:08 -0700614
Jesse Hallc2e41222013-08-08 13:40:22 -0700615bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
616 if (!nameLen) {
617 nameLen = strlen(name);
618 }
Mathias Agopian65421432017-03-08 11:49:05 -0800619 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700620}
621
Jesse Halla0fef1c2012-04-17 12:02:26 -0700622// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700623}; // namespace android
624// ----------------------------------------------------------------------------