blob: 893cf0be08ae9becc6bf32cc9461e9ce97386802 [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
Mathias Agopian518ec112011-05-13 16:21:08 -0700123EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
124 if (uintptr_t(disp) >= NUM_DISPLAYS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700125 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700126
127 return sDisplay[uintptr_t(disp)].getDisplay(disp);
128}
129
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600130static void addAnglePlatformAttributes(egl_connection_t* const cnx, const EGLAttrib* attrib_list,
131 std::vector<EGLAttrib>& attrs) {
132 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
133
134 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
135 if (attrib_list) {
136 while (*attrib_list != EGL_NONE) {
137 EGLAttrib attr = *attrib_list++;
138 EGLAttrib value = *attrib_list++;
139 if (attr == EGL_PLATFORM_ANGLE_TYPE_ANGLE) {
140 switch (value) {
141 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
142 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
143 break;
144 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
145 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
146 angleBackendDefault = value;
147 break;
148 default:
149 ALOGW("Invalid EGL_PLATFORM_ANGLE_TYPE_ANGLE attribute: 0x%" PRIxPTR,
150 value);
151 break;
152 }
153 }
154 }
155 }
156
157 cnx->angleBackend = angleBackendDefault;
158
159 // Allow debug property to override application's
160 char prop[PROPERTY_VALUE_MAX];
161 property_get("debug.angle.backend", prop, "0");
162 switch (atoi(prop)) {
163 case 1:
164 ALOGV("addAnglePlatformAttributes: Requesting OpenGLES back-end");
165 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
166 break;
167 case 2:
168 ALOGV("addAnglePlatformAttributes: Requesting Vulkan back-end");
169 cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
170 break;
171 default:
172 break;
173 }
174
175 attrs.reserve(4 * 2);
176
177 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
178 attrs.push_back(cnx->angleBackend);
179
180 switch (cnx->angleBackend) {
181 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
182 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
183 property_get("debug.angle.validation", prop, "0");
184 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
185 attrs.push_back(atoi(prop));
186 break;
187 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
188 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
189 break;
190 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
191 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
192 // NOTE: This is only valid if the backend is OpenGL
193 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
194 attrs.push_back(vendorEGL);
195 break;
196 default:
197 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
198 break;
199 }
200 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
201 attrs.push_back(EGL_FALSE);
202}
203
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600204// Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
205bool initializeAnglePlatform(EGLDisplay dpy) {
206 // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
207 android_namespace_t* ns = android_getAngleNamespace();
208 const android_dlextinfo dlextinfo = {
209 .flags = ANDROID_DLEXT_USE_NAMESPACE,
210 .library_namespace = ns,
211 };
212 void* so = android_dlopen_ext("libEGL_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo);
213 angle::AnglePlatformImpl::angleGetDisplayPlatform =
214 reinterpret_cast<angle::GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
215
216 if (!angle::AnglePlatformImpl::angleGetDisplayPlatform) {
217 ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
218 return false;
219 }
220
221 angle::AnglePlatformImpl::angleResetDisplayPlatform =
222 reinterpret_cast<angle::ResetDisplayPlatformFunc>(
223 eglGetProcAddress("ANGLEResetDisplayPlatform"));
224
225 angle::PlatformMethods* platformMethods = nullptr;
226 if (!((angle::AnglePlatformImpl::angleGetDisplayPlatform)(dpy, angle::g_PlatformMethodNames,
227 angle::g_NumPlatformMethods, nullptr,
228 &platformMethods))) {
229 ALOGE("ANGLEGetDisplayPlatform call failed!");
230 return false;
231 }
232 if (platformMethods) {
233 angle::AnglePlatformImpl::assignAnglePlatformMethods(platformMethods);
234 } else {
235 ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
236 "tracing function ptrs!");
237 }
238 return true;
239}
240
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600241static EGLDisplay getDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600242 EGLDisplay dpy = EGL_NO_DISPLAY;
243
244 // Locally define this until EGL 1.5 is supported
245 typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYPROC)(EGLenum platform, void* native_display,
246 const EGLAttrib* attrib_list);
247
248 PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay =
249 reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>(
250 cnx->egl.eglGetProcAddress("eglGetPlatformDisplay"));
251
252 if (eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600253 std::vector<EGLAttrib> attrs;
254 addAnglePlatformAttributes(cnx, nullptr, attrs);
255 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600256
Cody Northrop1f00e172018-04-02 11:23:31 -0600257 dpy = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600258 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attrs.data());
259 if (dpy == EGL_NO_DISPLAY) {
260 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600261 } else {
262 if (!initializeAnglePlatform(dpy)) {
263 ALOGE("initializeAnglePlatform failed!");
264 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600265 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600266 } else {
267 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
268 display);
269 }
270
271 return dpy;
272}
273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
275
Mathias Agopian65421432017-03-08 11:49:05 -0800276 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800277 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700278
279 // get our driver loader
280 Loader& loader(Loader::getInstance());
281
Mathias Agopianada798b2012-02-13 17:09:30 -0800282 egl_connection_t* const cnx = &gEGLImpl;
283 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600284 EGLDisplay dpy = EGL_NO_DISPLAY;
285
286 if (cnx->useAngle) {
287 dpy = getDisplayAngle(display, cnx);
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600288 }
289 if (dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600290 dpy = cnx->egl.eglGetDisplay(display);
291 }
292
Mathias Agopianada798b2012-02-13 17:09:30 -0800293 disp.dpy = dpy;
294 if (dpy == EGL_NO_DISPLAY) {
295 loader.close(cnx->dso);
Yi Kong48a6cd22018-07-18 10:07:09 -0700296 cnx->dso = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700297 }
298 }
299
300 return EGLDisplay(uintptr_t(display) + 1U);
301}
302
303EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
304
Mathias Agopian65421432017-03-08 11:49:05 -0800305 { // scope for refLock
306 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800307 refs++;
308 if (refs > 1) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700309 if (major != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800310 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700311 if (minor != nullptr)
Michael Lentine54466bc2015-01-27 09:01:03 -0800312 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800313 while(!eglIsInitialized) {
314 refCond.wait(_l);
315 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800316 return EGL_TRUE;
317 }
Mathias Agopian65421432017-03-08 11:49:05 -0800318 while(eglIsInitialized) {
319 refCond.wait(_l);
320 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800321 }
322
Mathias Agopian65421432017-03-08 11:49:05 -0800323 { // scope for lock
324 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800325
Michael Lentine54466bc2015-01-27 09:01:03 -0800326 setGLHooksThreadSpecific(&gHooksNoContext);
327
328 // initialize each EGL and
329 // build our own extension string first, based on the extension we know
330 // and the extension supported by our client implementation
331
332 egl_connection_t* const cnx = &gEGLImpl;
333 cnx->major = -1;
334 cnx->minor = -1;
335 if (cnx->dso) {
336 EGLDisplay idpy = disp.dpy;
337 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
338 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
339 // idpy, cnx->major, cnx->minor, cnx);
340
341 // display is now initialized
342 disp.state = egl_display_t::INITIALIZED;
343
344 // get the query-strings for this display for each implementation
345 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
346 EGL_VENDOR);
347 disp.queryString.version = cnx->egl.eglQueryString(idpy,
348 EGL_VERSION);
349 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
350 EGL_EXTENSIONS);
351 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
352 EGL_CLIENT_APIS);
353
354 } else {
355 ALOGW("eglInitialize(%p) failed (%s)", idpy,
356 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
357 }
358 }
359
360 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800361 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600362 mVersionString.clear();
363 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
364 if ((cnx->major == 1) && (cnx->minor == 5)) {
365 mVersionString = sVersionString15;
366 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
367 } else if ((cnx->major == 1) && (cnx->minor == 4)) {
368 mVersionString = sVersionString14;
369 // Extensions needed for an EGL 1.4 implementation to be
370 // able to support EGL 1.5 functionality
371 std::vector<const char*> egl15extensions = {
372 "EGL_EXT_client_extensions",
373 // "EGL_EXT_platform_base", // implemented by EGL runtime
374 "EGL_KHR_image_base",
375 "EGL_KHR_fence_sync",
376 "EGL_KHR_wait_sync",
377 "EGL_KHR_create_context",
378 "EGL_EXT_create_context_robustness",
379 "EGL_KHR_gl_colorspace",
380 "EGL_ANDROID_native_fence_sync",
381 };
382 bool extensionsFound = true;
383 for (const auto& name : egl15extensions) {
384 extensionsFound &= findExtension(disp.queryString.extensions, name);
385 ALOGV("Extension %s: %s", name,
386 findExtension(disp.queryString.extensions, name) ? "Found" : "Missing");
387 }
388 // NOTE: From the spec:
389 // Creation of fence sync objects requires support from the bound
390 // client API, and will not succeed unless the client API satisfies:
391 // client API is OpenGL ES, and either the OpenGL ES version is 3.0
392 // or greater, or the GL_OES_EGL_sync extension is supported.
393 // We don't have a way to check the GL_EXTENSIONS string at this
394 // point in the code, assume that GL_OES_EGL_sync is supported
395 // because EGL_KHR_fence_sync is supported (as verified above).
396 if (extensionsFound) {
397 // Have everything needed to emulate EGL 1.5 so report EGL 1.5
398 // to the application.
399 mVersionString = sVersionString15;
400 cnx->major = 1;
401 cnx->minor = 5;
402 }
403 }
404 if (mVersionString.empty()) {
405 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
406 mVersionString = sVersionString14;
407 }
Mathias Agopian65421432017-03-08 11:49:05 -0800408 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800409
Mathias Agopian65421432017-03-08 11:49:05 -0800410 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600411
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700412 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
413
414 // Note: CDD requires that devices supporting wide color and/or HDR color also support
415 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600416 bool wideColorBoardConfig =
417 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
418 false);
419
420 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700421 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600422 mExtensionString.append(
423 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
424 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
425 }
426
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700427 bool hasHdrBoardConfig =
428 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
429
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700430 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700431 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
432 // Typically that means there is an HDR capable display attached, but could be
433 // support for attaching an HDR display. In either case, advertise support for
434 // HDR color spaces.
435 mExtensionString.append(
436 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
437 }
438
Michael Lentine54466bc2015-01-27 09:01:03 -0800439 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800440 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400441 // length of the extension name
442 size_t len = strcspn(start, " ");
443 if (len) {
444 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800445 const std::string ext(start, len);
446 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400447 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800448 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400449 // advance to the next extension name, skipping the space.
450 start += len;
451 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800452 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400453 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800454
455 egl_cache_t::get()->initialize(this);
456
457 char value[PROPERTY_VALUE_MAX];
458 property_get("debug.egl.finish", value, "0");
459 if (atoi(value)) {
460 finishOnSwap = true;
461 }
462
463 property_get("debug.egl.traceGpuCompletion", value, "0");
464 if (atoi(value)) {
465 traceGpuCompletion = true;
466 }
467
Yi Kong48a6cd22018-07-18 10:07:09 -0700468 if (major != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700469 *major = VERSION_MAJOR;
Yi Kong48a6cd22018-07-18 10:07:09 -0700470 if (minor != nullptr)
Mathias Agopian518ec112011-05-13 16:21:08 -0700471 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700472 }
473
Mathias Agopian65421432017-03-08 11:49:05 -0800474 { // scope for refLock
475 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800476 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800477 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700478 }
479
Mathias Agopian7773c432012-02-13 20:06:08 -0800480 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700481}
482
483EGLBoolean egl_display_t::terminate() {
484
Mathias Agopian65421432017-03-08 11:49:05 -0800485 { // scope for refLock
486 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800487 if (refs == 0) {
488 /*
489 * From the EGL spec (3.2):
490 * "Termination of a display that has already been terminated,
491 * (...), is allowed, but the only effect of such a call is
492 * to return EGL_TRUE (...)
493 */
494 return EGL_TRUE;
495 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700496
Michael Lentine54466bc2015-01-27 09:01:03 -0800497 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800499 if (refs > 0) {
500 return EGL_TRUE;
501 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700502 }
503
504 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800505
Mathias Agopian65421432017-03-08 11:49:05 -0800506 { // scope for lock
507 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800508
509 egl_connection_t* const cnx = &gEGLImpl;
510 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600511 // If we're using ANGLE reset any custom DisplayPlatform
512 if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) {
513 (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy);
514 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800515 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
516 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
517 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
518 }
519 // REVISIT: it's unclear what to do if eglTerminate() fails
520 disp.state = egl_display_t::TERMINATED;
521 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700522 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800523
Michael Lentine54466bc2015-01-27 09:01:03 -0800524 // Reset the extension string since it will be regenerated if we get
525 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800526 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800527
528 // Mark all objects remaining in the list as terminated, unless
529 // there are no reference to them, it which case, we're free to
530 // delete them.
531 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800532 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800533 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800534 o->destroy();
535 }
536
537 // this marks all object handles are "terminated"
538 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700539 }
540
Mathias Agopian65421432017-03-08 11:49:05 -0800541 { // scope for refLock
542 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800543 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800544 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700545 }
546
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 return res;
548}
549
Mathias Agopianfb87e542012-01-30 18:20:52 -0800550void egl_display_t::loseCurrent(egl_context_t * cur_c)
551{
552 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800553 egl_display_t* display = cur_c->getDisplay();
554 if (display) {
555 display->loseCurrentImpl(cur_c);
556 }
557 }
558}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800559
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800560void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
561{
562 // by construction, these are either 0 or valid (possibly terminated)
563 // it should be impossible for these to be invalid
564 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700565 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
566 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800567
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800568 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800569 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800570 cur_c->onLooseCurrent();
571
Mathias Agopianfb87e542012-01-30 18:20:52 -0800572 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800573
574 // This cannot be called with the lock held because it might end-up
575 // calling back into EGL (in particular when a surface is destroyed
576 // it calls ANativeWindow::disconnect
577 _cur_c.release();
578 _cur_r.release();
579 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800580}
581
582EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700583 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800584 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
585{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800586 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800587
588 // by construction, these are either 0 or valid (possibly terminated)
589 // it should be impossible for these to be invalid
590 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700591 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
592 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800593
594 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800595 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800596 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800597 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800598 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800599 if (result == EGL_TRUE) {
600 c->onMakeCurrent(draw, read);
601 }
602 } else {
603 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800604 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800605 if (result == EGL_TRUE) {
606 cur_c->onLooseCurrent();
607 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800608 }
609 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800610
611 if (result == EGL_TRUE) {
612 // This cannot be called with the lock held because it might end-up
613 // calling back into EGL (in particular when a surface is destroyed
614 // it calls ANativeWindow::disconnect
615 _cur_c.release();
616 _cur_r.release();
617 _cur_d.release();
618 }
619
Mathias Agopianfb87e542012-01-30 18:20:52 -0800620 return result;
621}
Mathias Agopian518ec112011-05-13 16:21:08 -0700622
Jesse Hallc2e41222013-08-08 13:40:22 -0700623bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
624 if (!nameLen) {
625 nameLen = strlen(name);
626 }
Mathias Agopian65421432017-03-08 11:49:05 -0800627 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700628}
629
Jesse Halla0fef1c2012-04-17 12:02:26 -0700630// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700631}; // namespace android
632// ----------------------------------------------------------------------------