blob: a0659730c4dd9d815d80ae0e2d81c87ddeca0f63 [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);
Ben Line17f69b2018-11-05 16:49:28 -0800268 }
269
270 // It is possible that eglGetPlatformDisplay does not have a
271 // working implementation for Android platform; in that case,
272 // one last fallback to eglGetDisplay
273 if(dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600274 if (attrib_list) {
275 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
276 }
277 dpy = cnx->egl.eglGetDisplay(display);
278 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600279 }
280
Mathias Agopianada798b2012-02-13 17:09:30 -0800281 disp.dpy = dpy;
282 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchteree768c22018-10-25 11:43:52 -0600283 loader.close(cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 }
285 }
286
287 return EGLDisplay(uintptr_t(display) + 1U);
288}
289
290EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
291
Mathias Agopian65421432017-03-08 11:49:05 -0800292 { // scope for refLock
293 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800294 refs++;
295 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600296 // We don't know what to report until we know what the
297 // driver supports. Make sure we are initialized before
298 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800299 while(!eglIsInitialized) {
300 refCond.wait(_l);
301 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600302 egl_connection_t* const cnx = &gEGLImpl;
303
304 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
305 // changing the behavior from the past where we always advertise
306 // version 1.4. May need to check that revision is valid
307 // before using cnx->major & cnx->minor
308 if (major != nullptr) *major = cnx->major;
309 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800310 return EGL_TRUE;
311 }
Mathias Agopian65421432017-03-08 11:49:05 -0800312 while(eglIsInitialized) {
313 refCond.wait(_l);
314 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800315 }
316
Mathias Agopian65421432017-03-08 11:49:05 -0800317 { // scope for lock
318 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800319
Michael Lentine54466bc2015-01-27 09:01:03 -0800320 setGLHooksThreadSpecific(&gHooksNoContext);
321
322 // initialize each EGL and
323 // build our own extension string first, based on the extension we know
324 // and the extension supported by our client implementation
325
326 egl_connection_t* const cnx = &gEGLImpl;
327 cnx->major = -1;
328 cnx->minor = -1;
329 if (cnx->dso) {
330 EGLDisplay idpy = disp.dpy;
331 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
332 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
333 // idpy, cnx->major, cnx->minor, cnx);
334
335 // display is now initialized
336 disp.state = egl_display_t::INITIALIZED;
337
338 // get the query-strings for this display for each implementation
339 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
340 EGL_VENDOR);
341 disp.queryString.version = cnx->egl.eglQueryString(idpy,
342 EGL_VERSION);
343 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
344 EGL_EXTENSIONS);
345 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
346 EGL_CLIENT_APIS);
347
348 } else {
349 ALOGW("eglInitialize(%p) failed (%s)", idpy,
350 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
351 }
352 }
353
354 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800355 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600356 mVersionString.clear();
357 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
358 if ((cnx->major == 1) && (cnx->minor == 5)) {
359 mVersionString = sVersionString15;
360 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
361 } else if ((cnx->major == 1) && (cnx->minor == 4)) {
362 mVersionString = sVersionString14;
363 // Extensions needed for an EGL 1.4 implementation to be
364 // able to support EGL 1.5 functionality
365 std::vector<const char*> egl15extensions = {
366 "EGL_EXT_client_extensions",
367 // "EGL_EXT_platform_base", // implemented by EGL runtime
368 "EGL_KHR_image_base",
369 "EGL_KHR_fence_sync",
370 "EGL_KHR_wait_sync",
371 "EGL_KHR_create_context",
372 "EGL_EXT_create_context_robustness",
373 "EGL_KHR_gl_colorspace",
374 "EGL_ANDROID_native_fence_sync",
375 };
376 bool extensionsFound = true;
377 for (const auto& name : egl15extensions) {
378 extensionsFound &= findExtension(disp.queryString.extensions, name);
379 ALOGV("Extension %s: %s", name,
380 findExtension(disp.queryString.extensions, name) ? "Found" : "Missing");
381 }
382 // NOTE: From the spec:
383 // Creation of fence sync objects requires support from the bound
384 // client API, and will not succeed unless the client API satisfies:
385 // client API is OpenGL ES, and either the OpenGL ES version is 3.0
386 // or greater, or the GL_OES_EGL_sync extension is supported.
387 // We don't have a way to check the GL_EXTENSIONS string at this
388 // point in the code, assume that GL_OES_EGL_sync is supported
389 // because EGL_KHR_fence_sync is supported (as verified above).
390 if (extensionsFound) {
391 // Have everything needed to emulate EGL 1.5 so report EGL 1.5
392 // to the application.
393 mVersionString = sVersionString15;
394 cnx->major = 1;
395 cnx->minor = 5;
396 }
397 }
398 if (mVersionString.empty()) {
399 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
400 mVersionString = sVersionString14;
401 }
Mathias Agopian65421432017-03-08 11:49:05 -0800402 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800403
Mathias Agopian65421432017-03-08 11:49:05 -0800404 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600405
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700406 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
407
408 // Note: CDD requires that devices supporting wide color and/or HDR color also support
409 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600410 bool wideColorBoardConfig =
411 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
412 false);
413
414 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700415 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600416 mExtensionString.append(
417 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
418 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
419 }
420
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700421 bool hasHdrBoardConfig =
422 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
423
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700424 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700425 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
426 // Typically that means there is an HDR capable display attached, but could be
427 // support for attaching an HDR display. In either case, advertise support for
428 // HDR color spaces.
429 mExtensionString.append(
430 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
431 }
432
Michael Lentine54466bc2015-01-27 09:01:03 -0800433 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800434 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400435 // length of the extension name
436 size_t len = strcspn(start, " ");
437 if (len) {
438 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800439 const std::string ext(start, len);
440 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400441 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800442 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400443 // advance to the next extension name, skipping the space.
444 start += len;
445 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800446 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400447 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800448
449 egl_cache_t::get()->initialize(this);
450
451 char value[PROPERTY_VALUE_MAX];
452 property_get("debug.egl.finish", value, "0");
453 if (atoi(value)) {
454 finishOnSwap = true;
455 }
456
457 property_get("debug.egl.traceGpuCompletion", value, "0");
458 if (atoi(value)) {
459 traceGpuCompletion = true;
460 }
461
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600462 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
463 // changing the behavior from the past where we always advertise
464 // version 1.4. May need to check that revision is valid
465 // before using cnx->major & cnx->minor
466 if (major != nullptr) *major = cnx->major;
467 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700468 }
469
Mathias Agopian65421432017-03-08 11:49:05 -0800470 { // scope for refLock
471 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800472 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800473 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700474 }
475
Mathias Agopian7773c432012-02-13 20:06:08 -0800476 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700477}
478
479EGLBoolean egl_display_t::terminate() {
480
Mathias Agopian65421432017-03-08 11:49:05 -0800481 { // scope for refLock
482 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800483 if (refs == 0) {
484 /*
485 * From the EGL spec (3.2):
486 * "Termination of a display that has already been terminated,
487 * (...), is allowed, but the only effect of such a call is
488 * to return EGL_TRUE (...)
489 */
490 return EGL_TRUE;
491 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700492
Michael Lentine54466bc2015-01-27 09:01:03 -0800493 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700494 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800495 if (refs > 0) {
496 return EGL_TRUE;
497 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 }
499
500 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800501
Mathias Agopian65421432017-03-08 11:49:05 -0800502 { // scope for lock
503 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800504
505 egl_connection_t* const cnx = &gEGLImpl;
506 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600507 // If we're using ANGLE reset any custom DisplayPlatform
508 if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) {
509 (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy);
510 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800511 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
512 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
513 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
514 }
515 // REVISIT: it's unclear what to do if eglTerminate() fails
516 disp.state = egl_display_t::TERMINATED;
517 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700518 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800519
Michael Lentine54466bc2015-01-27 09:01:03 -0800520 // Reset the extension string since it will be regenerated if we get
521 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800522 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800523
524 // Mark all objects remaining in the list as terminated, unless
525 // there are no reference to them, it which case, we're free to
526 // delete them.
527 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800528 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800529 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800530 o->destroy();
531 }
532
533 // this marks all object handles are "terminated"
534 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700535 }
536
Mathias Agopian65421432017-03-08 11:49:05 -0800537 { // scope for refLock
538 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800539 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800540 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700541 }
542
Mathias Agopian518ec112011-05-13 16:21:08 -0700543 return res;
544}
545
Mathias Agopianfb87e542012-01-30 18:20:52 -0800546void egl_display_t::loseCurrent(egl_context_t * cur_c)
547{
548 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800549 egl_display_t* display = cur_c->getDisplay();
550 if (display) {
551 display->loseCurrentImpl(cur_c);
552 }
553 }
554}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800555
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800556void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
557{
558 // by construction, these are either 0 or valid (possibly terminated)
559 // it should be impossible for these to be invalid
560 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700561 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
562 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800563
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800564 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800565 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800566 cur_c->onLooseCurrent();
567
Mathias Agopianfb87e542012-01-30 18:20:52 -0800568 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800569
570 // This cannot be called with the lock held because it might end-up
571 // calling back into EGL (in particular when a surface is destroyed
572 // it calls ANativeWindow::disconnect
573 _cur_c.release();
574 _cur_r.release();
575 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800576}
577
578EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700579 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800580 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
581{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800582 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800583
584 // by construction, these are either 0 or valid (possibly terminated)
585 // it should be impossible for these to be invalid
586 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700587 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
588 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800589
590 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800591 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800592 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800593 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800594 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800595 if (result == EGL_TRUE) {
596 c->onMakeCurrent(draw, read);
597 }
598 } else {
599 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800600 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800601 if (result == EGL_TRUE) {
602 cur_c->onLooseCurrent();
603 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800604 }
605 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800606
607 if (result == EGL_TRUE) {
608 // This cannot be called with the lock held because it might end-up
609 // calling back into EGL (in particular when a surface is destroyed
610 // it calls ANativeWindow::disconnect
611 _cur_c.release();
612 _cur_r.release();
613 _cur_d.release();
614 }
615
Mathias Agopianfb87e542012-01-30 18:20:52 -0800616 return result;
617}
Mathias Agopian518ec112011-05-13 16:21:08 -0700618
Jesse Hallc2e41222013-08-08 13:40:22 -0700619bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
620 if (!nameLen) {
621 nameLen = strlen(name);
622 }
Mathias Agopian65421432017-03-08 11:49:05 -0800623 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700624}
625
Jesse Halla0fef1c2012-04-17 12:02:26 -0700626// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700627}; // namespace android
628// ----------------------------------------------------------------------------