blob: 476b304bc823b44b5839851eee48aff79c76b88e [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 Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600131static bool addAnglePlatformAttributes(egl_connection_t* const cnx, const EGLAttrib* attrib_list,
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600132 std::vector<EGLAttrib>& attrs) {
133 intptr_t vendorEGL = (intptr_t)cnx->vendorEGL;
134
135 EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
136 if (attrib_list) {
137 while (*attrib_list != EGL_NONE) {
138 EGLAttrib attr = *attrib_list++;
139 EGLAttrib value = *attrib_list++;
140 if (attr == EGL_PLATFORM_ANGLE_TYPE_ANGLE) {
141 switch (value) {
142 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
143 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
144 break;
145 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
146 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
147 angleBackendDefault = value;
148 break;
149 default:
150 ALOGW("Invalid EGL_PLATFORM_ANGLE_TYPE_ANGLE attribute: 0x%" PRIxPTR,
151 value);
152 break;
153 }
154 }
155 }
156 }
157
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600158 // Allow debug property to override application's
159 char prop[PROPERTY_VALUE_MAX];
160 property_get("debug.angle.backend", prop, "0");
161 switch (atoi(prop)) {
162 case 1:
163 ALOGV("addAnglePlatformAttributes: Requesting OpenGLES back-end");
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600164 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600165 break;
166 case 2:
167 ALOGV("addAnglePlatformAttributes: Requesting Vulkan back-end");
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600168 angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600169 break;
170 default:
171 break;
172 }
173
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600174 if (cnx->angleBackend == 0) {
175 // Haven't been initialized yet, so set it.
176 cnx->angleBackend = angleBackendDefault;
177 } else if (cnx->angleBackend != angleBackendDefault) {
178 return false;
179 }
180
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600181 attrs.reserve(4 * 2);
182
183 attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
184 attrs.push_back(cnx->angleBackend);
185
186 switch (cnx->angleBackend) {
187 case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
188 ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__);
189 property_get("debug.angle.validation", prop, "0");
190 attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE);
191 attrs.push_back(atoi(prop));
192 break;
193 case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE:
194 ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__);
195 break;
196 case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
197 ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__);
198 // NOTE: This is only valid if the backend is OpenGL
199 attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE);
200 attrs.push_back(vendorEGL);
201 break;
202 default:
203 ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend);
204 break;
205 }
206 attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE);
207 attrs.push_back(EGL_FALSE);
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600208
209 return true;
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600210}
211
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600212// Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace
213bool initializeAnglePlatform(EGLDisplay dpy) {
214 // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform
215 android_namespace_t* ns = android_getAngleNamespace();
216 const android_dlextinfo dlextinfo = {
217 .flags = ANDROID_DLEXT_USE_NAMESPACE,
218 .library_namespace = ns,
219 };
220 void* so = android_dlopen_ext("libEGL_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo);
221 angle::AnglePlatformImpl::angleGetDisplayPlatform =
222 reinterpret_cast<angle::GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform"));
223
224 if (!angle::AnglePlatformImpl::angleGetDisplayPlatform) {
225 ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!");
226 return false;
227 }
228
229 angle::AnglePlatformImpl::angleResetDisplayPlatform =
230 reinterpret_cast<angle::ResetDisplayPlatformFunc>(
231 eglGetProcAddress("ANGLEResetDisplayPlatform"));
232
233 angle::PlatformMethods* platformMethods = nullptr;
234 if (!((angle::AnglePlatformImpl::angleGetDisplayPlatform)(dpy, angle::g_PlatformMethodNames,
235 angle::g_NumPlatformMethods, nullptr,
236 &platformMethods))) {
237 ALOGE("ANGLEGetDisplayPlatform call failed!");
238 return false;
239 }
240 if (platformMethods) {
241 angle::AnglePlatformImpl::assignAnglePlatformMethods(platformMethods);
242 } else {
243 ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning "
244 "tracing function ptrs!");
245 }
246 return true;
247}
248
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600249static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx,
250 const EGLAttrib* attrib_list, EGLint* error) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600251 EGLDisplay dpy = EGL_NO_DISPLAY;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600252 *error = EGL_NONE;
Cody Northrop1f00e172018-04-02 11:23:31 -0600253
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600254 if (cnx->egl.eglGetPlatformDisplay) {
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600255 std::vector<EGLAttrib> attrs;
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600256 if (attrib_list) {
257 for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
258 attrs.push_back(attr[0]);
259 attrs.push_back(attr[1]);
260 }
261 }
262
263 if (!addAnglePlatformAttributes(cnx, attrib_list, attrs)) {
264 ALOGE("eglGetDisplay(%p) failed: Mismatch display request", display);
265 *error = EGL_BAD_PARAMETER;
266 return EGL_NO_DISPLAY;
267 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600268 attrs.push_back(EGL_NONE);
Cody Northrop1f00e172018-04-02 11:23:31 -0600269
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600270 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE,
271 reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY),
272 attrs.data());
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600273 if (dpy == EGL_NO_DISPLAY) {
274 ALOGE("eglGetPlatformDisplay failed!");
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600275 } else {
276 if (!initializeAnglePlatform(dpy)) {
277 ALOGE("initializeAnglePlatform failed!");
278 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600279 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600280 } else {
281 ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE",
282 display);
283 }
284
285 return dpy;
286}
287
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600288EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display,
289 const EGLAttrib* attrib_list) {
Mathias Agopian65421432017-03-08 11:49:05 -0800290 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800291 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700292
293 // get our driver loader
294 Loader& loader(Loader::getInstance());
295
Mathias Agopianada798b2012-02-13 17:09:30 -0800296 egl_connection_t* const cnx = &gEGLImpl;
297 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600298 EGLDisplay dpy = EGL_NO_DISPLAY;
299
300 if (cnx->useAngle) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600301 EGLint error;
302 dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error);
303 if (error != EGL_NONE) {
304 return setError(error, dpy);
305 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600306 }
307 if (dpy == EGL_NO_DISPLAY) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600308 // NOTE: eglGetPlatformDisplay with a empty attribute list
309 // behaves the same as eglGetDisplay
310 if (cnx->egl.eglGetPlatformDisplay) {
311 dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display,
312 attrib_list);
313 } else {
314 if (attrib_list) {
315 ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored");
316 }
317 dpy = cnx->egl.eglGetDisplay(display);
318 }
Cody Northrop1f00e172018-04-02 11:23:31 -0600319 }
320
Mathias Agopianada798b2012-02-13 17:09:30 -0800321 disp.dpy = dpy;
322 if (dpy == EGL_NO_DISPLAY) {
323 loader.close(cnx->dso);
Yi Kong48a6cd22018-07-18 10:07:09 -0700324 cnx->dso = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 }
326 }
327
328 return EGLDisplay(uintptr_t(display) + 1U);
329}
330
331EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
332
Mathias Agopian65421432017-03-08 11:49:05 -0800333 { // scope for refLock
334 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800335 refs++;
336 if (refs > 1) {
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600337 // We don't know what to report until we know what the
338 // driver supports. Make sure we are initialized before
339 // returning the version info.
Mathias Agopian65421432017-03-08 11:49:05 -0800340 while(!eglIsInitialized) {
341 refCond.wait(_l);
342 }
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600343 egl_connection_t* const cnx = &gEGLImpl;
344
345 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
346 // changing the behavior from the past where we always advertise
347 // version 1.4. May need to check that revision is valid
348 // before using cnx->major & cnx->minor
349 if (major != nullptr) *major = cnx->major;
350 if (minor != nullptr) *minor = cnx->minor;
Michael Lentine54466bc2015-01-27 09:01:03 -0800351 return EGL_TRUE;
352 }
Mathias Agopian65421432017-03-08 11:49:05 -0800353 while(eglIsInitialized) {
354 refCond.wait(_l);
355 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800356 }
357
Mathias Agopian65421432017-03-08 11:49:05 -0800358 { // scope for lock
359 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800360
Michael Lentine54466bc2015-01-27 09:01:03 -0800361 setGLHooksThreadSpecific(&gHooksNoContext);
362
363 // initialize each EGL and
364 // build our own extension string first, based on the extension we know
365 // and the extension supported by our client implementation
366
367 egl_connection_t* const cnx = &gEGLImpl;
368 cnx->major = -1;
369 cnx->minor = -1;
370 if (cnx->dso) {
371 EGLDisplay idpy = disp.dpy;
372 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
373 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
374 // idpy, cnx->major, cnx->minor, cnx);
375
376 // display is now initialized
377 disp.state = egl_display_t::INITIALIZED;
378
379 // get the query-strings for this display for each implementation
380 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
381 EGL_VENDOR);
382 disp.queryString.version = cnx->egl.eglQueryString(idpy,
383 EGL_VERSION);
384 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
385 EGL_EXTENSIONS);
386 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
387 EGL_CLIENT_APIS);
388
389 } else {
390 ALOGW("eglInitialize(%p) failed (%s)", idpy,
391 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
392 }
393 }
394
395 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800396 mVendorString = sVendorString;
Courtney Goeltzenleuchter015ed672018-07-27 13:43:23 -0600397 mVersionString.clear();
398 cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0);
399 if ((cnx->major == 1) && (cnx->minor == 5)) {
400 mVersionString = sVersionString15;
401 cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0);
402 } else if ((cnx->major == 1) && (cnx->minor == 4)) {
403 mVersionString = sVersionString14;
404 // Extensions needed for an EGL 1.4 implementation to be
405 // able to support EGL 1.5 functionality
406 std::vector<const char*> egl15extensions = {
407 "EGL_EXT_client_extensions",
408 // "EGL_EXT_platform_base", // implemented by EGL runtime
409 "EGL_KHR_image_base",
410 "EGL_KHR_fence_sync",
411 "EGL_KHR_wait_sync",
412 "EGL_KHR_create_context",
413 "EGL_EXT_create_context_robustness",
414 "EGL_KHR_gl_colorspace",
415 "EGL_ANDROID_native_fence_sync",
416 };
417 bool extensionsFound = true;
418 for (const auto& name : egl15extensions) {
419 extensionsFound &= findExtension(disp.queryString.extensions, name);
420 ALOGV("Extension %s: %s", name,
421 findExtension(disp.queryString.extensions, name) ? "Found" : "Missing");
422 }
423 // NOTE: From the spec:
424 // Creation of fence sync objects requires support from the bound
425 // client API, and will not succeed unless the client API satisfies:
426 // client API is OpenGL ES, and either the OpenGL ES version is 3.0
427 // or greater, or the GL_OES_EGL_sync extension is supported.
428 // We don't have a way to check the GL_EXTENSIONS string at this
429 // point in the code, assume that GL_OES_EGL_sync is supported
430 // because EGL_KHR_fence_sync is supported (as verified above).
431 if (extensionsFound) {
432 // Have everything needed to emulate EGL 1.5 so report EGL 1.5
433 // to the application.
434 mVersionString = sVersionString15;
435 cnx->major = 1;
436 cnx->minor = 5;
437 }
438 }
439 if (mVersionString.empty()) {
440 ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor);
441 mVersionString = sVersionString14;
442 }
Mathias Agopian65421432017-03-08 11:49:05 -0800443 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800444
Mathias Agopian65421432017-03-08 11:49:05 -0800445 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600446
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700447 hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace");
448
449 // Note: CDD requires that devices supporting wide color and/or HDR color also support
450 // the EGL_KHR_gl_colorspace extension.
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600451 bool wideColorBoardConfig =
452 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
453 false);
454
455 // Add wide-color extensions if device can support wide-color
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700456 if (wideColorBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600457 mExtensionString.append(
458 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
459 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
460 }
461
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700462 bool hasHdrBoardConfig =
463 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
464
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700465 if (hasHdrBoardConfig && hasColorSpaceSupport) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700466 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
467 // Typically that means there is an HDR capable display attached, but could be
468 // support for attaching an HDR display. In either case, advertise support for
469 // HDR color spaces.
470 mExtensionString.append(
471 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
472 }
473
Michael Lentine54466bc2015-01-27 09:01:03 -0800474 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800475 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400476 // length of the extension name
477 size_t len = strcspn(start, " ");
478 if (len) {
479 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800480 const std::string ext(start, len);
481 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400482 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800483 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400484 // advance to the next extension name, skipping the space.
485 start += len;
486 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800487 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400488 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800489
490 egl_cache_t::get()->initialize(this);
491
492 char value[PROPERTY_VALUE_MAX];
493 property_get("debug.egl.finish", value, "0");
494 if (atoi(value)) {
495 finishOnSwap = true;
496 }
497
498 property_get("debug.egl.traceGpuCompletion", value, "0");
499 if (atoi(value)) {
500 traceGpuCompletion = true;
501 }
502
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -0600503 // TODO: If device doesn't provide 1.4 or 1.5 then we'll be
504 // changing the behavior from the past where we always advertise
505 // version 1.4. May need to check that revision is valid
506 // before using cnx->major & cnx->minor
507 if (major != nullptr) *major = cnx->major;
508 if (minor != nullptr) *minor = cnx->minor;
Mathias Agopian518ec112011-05-13 16:21:08 -0700509 }
510
Mathias Agopian65421432017-03-08 11:49:05 -0800511 { // scope for refLock
512 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800513 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800514 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700515 }
516
Mathias Agopian7773c432012-02-13 20:06:08 -0800517 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700518}
519
520EGLBoolean egl_display_t::terminate() {
521
Mathias Agopian65421432017-03-08 11:49:05 -0800522 { // scope for refLock
523 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800524 if (refs == 0) {
525 /*
526 * From the EGL spec (3.2):
527 * "Termination of a display that has already been terminated,
528 * (...), is allowed, but the only effect of such a call is
529 * to return EGL_TRUE (...)
530 */
531 return EGL_TRUE;
532 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700533
Michael Lentine54466bc2015-01-27 09:01:03 -0800534 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700535 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800536 if (refs > 0) {
537 return EGL_TRUE;
538 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700539 }
540
541 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800542
Mathias Agopian65421432017-03-08 11:49:05 -0800543 { // scope for lock
544 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800545
546 egl_connection_t* const cnx = &gEGLImpl;
547 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
Tobin Ehlis96a184d2018-07-18 16:14:07 -0600548 // If we're using ANGLE reset any custom DisplayPlatform
549 if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) {
550 (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy);
551 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800552 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
553 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
554 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
555 }
556 // REVISIT: it's unclear what to do if eglTerminate() fails
557 disp.state = egl_display_t::TERMINATED;
558 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700559 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800560
Michael Lentine54466bc2015-01-27 09:01:03 -0800561 // Reset the extension string since it will be regenerated if we get
562 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800563 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800564
565 // Mark all objects remaining in the list as terminated, unless
566 // there are no reference to them, it which case, we're free to
567 // delete them.
568 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800569 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800570 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800571 o->destroy();
572 }
573
574 // this marks all object handles are "terminated"
575 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700576 }
577
Mathias Agopian65421432017-03-08 11:49:05 -0800578 { // scope for refLock
579 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800580 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800581 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700582 }
583
Mathias Agopian518ec112011-05-13 16:21:08 -0700584 return res;
585}
586
Mathias Agopianfb87e542012-01-30 18:20:52 -0800587void egl_display_t::loseCurrent(egl_context_t * cur_c)
588{
589 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800590 egl_display_t* display = cur_c->getDisplay();
591 if (display) {
592 display->loseCurrentImpl(cur_c);
593 }
594 }
595}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800596
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800597void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
598{
599 // by construction, these are either 0 or valid (possibly terminated)
600 // it should be impossible for these to be invalid
601 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700602 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
603 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800604
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800605 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800606 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800607 cur_c->onLooseCurrent();
608
Mathias Agopianfb87e542012-01-30 18:20:52 -0800609 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800610
611 // This cannot be called with the lock held because it might end-up
612 // calling back into EGL (in particular when a surface is destroyed
613 // it calls ANativeWindow::disconnect
614 _cur_c.release();
615 _cur_r.release();
616 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800617}
618
619EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700620 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800621 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
622{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800623 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800624
625 // by construction, these are either 0 or valid (possibly terminated)
626 // it should be impossible for these to be invalid
627 ContextRef _cur_c(cur_c);
Yi Kong48a6cd22018-07-18 10:07:09 -0700628 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr);
629 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800630
631 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800632 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800633 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800634 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800635 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800636 if (result == EGL_TRUE) {
637 c->onMakeCurrent(draw, read);
638 }
639 } else {
640 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800641 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800642 if (result == EGL_TRUE) {
643 cur_c->onLooseCurrent();
644 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800645 }
646 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800647
648 if (result == EGL_TRUE) {
649 // This cannot be called with the lock held because it might end-up
650 // calling back into EGL (in particular when a surface is destroyed
651 // it calls ANativeWindow::disconnect
652 _cur_c.release();
653 _cur_r.release();
654 _cur_d.release();
655 }
656
Mathias Agopianfb87e542012-01-30 18:20:52 -0800657 return result;
658}
Mathias Agopian518ec112011-05-13 16:21:08 -0700659
Jesse Hallc2e41222013-08-08 13:40:22 -0700660bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
661 if (!nameLen) {
662 nameLen = strlen(name);
663 }
Mathias Agopian65421432017-03-08 11:49:05 -0800664 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700665}
666
Jesse Halla0fef1c2012-04-17 12:02:26 -0700667// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700668}; // namespace android
669// ----------------------------------------------------------------------------