Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 1 | /* |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 4 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 10 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 17 | #define __STDC_LIMIT_MACROS 1 |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 19 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 20 | #include "egl_display.h" |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 21 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 22 | #include "../egl_impl.h" |
| 23 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 24 | #include <EGL/eglext_angle.h> |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 25 | #include <private/EGL/display.h> |
| 26 | |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame^] | 27 | #include <cutils/properties.h> |
| 28 | #include "Loader.h" |
| 29 | #include "egl_angle_platform.h" |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 30 | #include "egl_cache.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | #include "egl_object.h" |
| 32 | #include "egl_tls.h" |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame^] | 33 | |
| 34 | #include <android/dlext.h> |
| 35 | #include <dlfcn.h> |
| 36 | #include <graphicsenv/GraphicsEnv.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 37 | |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 38 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 39 | #include <configstore/Utils.h> |
| 40 | |
| 41 | using namespace android::hardware::configstore; |
| 42 | using namespace android::hardware::configstore::V1_0; |
| 43 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | // ---------------------------------------------------------------------------- |
| 45 | namespace android { |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 48 | static char const * const sVendorString = "Android"; |
| 49 | static char const * const sVersionString = "1.4 Android META-EGL"; |
Mathias Agopian | cc2b156 | 2012-05-21 14:01:37 -0700 | [diff] [blame] | 50 | static char const * const sClientApiString = "OpenGL_ES"; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 51 | |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 52 | extern char const * const gBuiltinExtensionString; |
Mathias Agopian | e9b3dfb | 2013-03-27 14:30:19 -0700 | [diff] [blame] | 53 | extern char const * const gExtensionString; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 54 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 55 | extern void setGLHooksThreadSpecific(gl_hooks_t const *value); |
| 56 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | |
Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 59 | bool findExtension(const char* exts, const char* name, size_t nameLen) { |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 60 | if (exts) { |
Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 61 | if (!nameLen) { |
| 62 | nameLen = strlen(name); |
| 63 | } |
Kalle Raita | 7804aa2 | 2016-04-18 16:03:37 -0700 | [diff] [blame] | 64 | for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) { |
| 65 | if (match[nameLen] == '\0' || match[nameLen] == ' ') { |
| 66 | return true; |
| 67 | } |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 73 | int egl_get_init_count(EGLDisplay dpy) { |
| 74 | egl_display_t* eglDisplay = egl_display_t::get(dpy); |
| 75 | return eglDisplay ? eglDisplay->getRefsCount() : 0; |
| 76 | } |
| 77 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 78 | egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS]; |
| 79 | |
| 80 | egl_display_t::egl_display_t() : |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 81 | magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | egl_display_t::~egl_display_t() { |
| 85 | magic = 0; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 86 | egl_cache_t::get()->terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | egl_display_t* egl_display_t::get(EGLDisplay dpy) { |
Ivan Lozano | 7025b54 | 2017-12-06 14:19:44 -0800 | [diff] [blame] | 90 | if (uintptr_t(dpy) == 0) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 94 | uintptr_t index = uintptr_t(dpy)-1U; |
Jesse Hall | d6e9946 | 2016-09-28 11:26:57 -0700 | [diff] [blame] | 95 | if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) { |
| 96 | return nullptr; |
| 97 | } |
| 98 | return &sDisplay[index]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void egl_display_t::addObject(egl_object_t* object) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 102 | std::lock_guard<std::mutex> _l(lock); |
| 103 | objects.insert(object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 106 | void egl_display_t::removeObject(egl_object_t* object) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 107 | std::lock_guard<std::mutex> _l(lock); |
| 108 | objects.erase(object); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 111 | bool egl_display_t::getObject(egl_object_t* object) const { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 112 | std::lock_guard<std::mutex> _l(lock); |
| 113 | if (objects.find(object) != objects.end()) { |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 114 | if (object->getDisplay() == this) { |
| 115 | object->incRef(); |
| 116 | return true; |
| 117 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 122 | EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) { |
| 123 | if (uintptr_t(disp) >= NUM_DISPLAYS) |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 124 | return nullptr; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 125 | |
| 126 | return sDisplay[uintptr_t(disp)].getDisplay(disp); |
| 127 | } |
| 128 | |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 129 | static void addAnglePlatformAttributes(egl_connection_t* const cnx, const EGLAttrib* attrib_list, |
| 130 | std::vector<EGLAttrib>& attrs) { |
| 131 | intptr_t vendorEGL = (intptr_t)cnx->vendorEGL; |
| 132 | |
| 133 | EGLint angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE; |
| 134 | if (attrib_list) { |
| 135 | while (*attrib_list != EGL_NONE) { |
| 136 | EGLAttrib attr = *attrib_list++; |
| 137 | EGLAttrib value = *attrib_list++; |
| 138 | if (attr == EGL_PLATFORM_ANGLE_TYPE_ANGLE) { |
| 139 | switch (value) { |
| 140 | case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE: |
| 141 | angleBackendDefault = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE; |
| 142 | break; |
| 143 | case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE: |
| 144 | case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE: |
| 145 | angleBackendDefault = value; |
| 146 | break; |
| 147 | default: |
| 148 | ALOGW("Invalid EGL_PLATFORM_ANGLE_TYPE_ANGLE attribute: 0x%" PRIxPTR, |
| 149 | value); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | cnx->angleBackend = angleBackendDefault; |
| 157 | |
| 158 | // 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"); |
| 164 | cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE; |
| 165 | break; |
| 166 | case 2: |
| 167 | ALOGV("addAnglePlatformAttributes: Requesting Vulkan back-end"); |
| 168 | cnx->angleBackend = EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE; |
| 169 | break; |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | attrs.reserve(4 * 2); |
| 175 | |
| 176 | attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE); |
| 177 | attrs.push_back(cnx->angleBackend); |
| 178 | |
| 179 | switch (cnx->angleBackend) { |
| 180 | case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE: |
| 181 | ALOGV("%s: Requesting Vulkan ANGLE back-end", __FUNCTION__); |
| 182 | property_get("debug.angle.validation", prop, "0"); |
| 183 | attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE); |
| 184 | attrs.push_back(atoi(prop)); |
| 185 | break; |
| 186 | case EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE: |
| 187 | ALOGV("%s: Requesting Default ANGLE back-end", __FUNCTION__); |
| 188 | break; |
| 189 | case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE: |
| 190 | ALOGV("%s: Requesting OpenGL ES ANGLE back-end", __FUNCTION__); |
| 191 | // NOTE: This is only valid if the backend is OpenGL |
| 192 | attrs.push_back(EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE); |
| 193 | attrs.push_back(vendorEGL); |
| 194 | break; |
| 195 | default: |
| 196 | ALOGV("%s: Requesting Unknown (%d) ANGLE back-end", __FUNCTION__, cnx->angleBackend); |
| 197 | break; |
| 198 | } |
| 199 | attrs.push_back(EGL_PLATFORM_ANGLE_CONTEXT_VIRTUALIZATION_ANGLE); |
| 200 | attrs.push_back(EGL_FALSE); |
| 201 | } |
| 202 | |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame^] | 203 | // Initialize function ptrs for ANGLE PlatformMethods struct, used for systrace |
| 204 | bool initializeAnglePlatform(EGLDisplay dpy) { |
| 205 | // Since we're inside libEGL, use dlsym to lookup fptr for ANGLEGetDisplayPlatform |
| 206 | android_namespace_t* ns = android_getAngleNamespace(); |
| 207 | const android_dlextinfo dlextinfo = { |
| 208 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 209 | .library_namespace = ns, |
| 210 | }; |
| 211 | void* so = android_dlopen_ext("libEGL_angle.so", RTLD_LOCAL | RTLD_NOW, &dlextinfo); |
| 212 | angle::AnglePlatformImpl::angleGetDisplayPlatform = |
| 213 | reinterpret_cast<angle::GetDisplayPlatformFunc>(dlsym(so, "ANGLEGetDisplayPlatform")); |
| 214 | |
| 215 | if (!angle::AnglePlatformImpl::angleGetDisplayPlatform) { |
| 216 | ALOGE("dlsym lookup of ANGLEGetDisplayPlatform in libEGL_angle failed!"); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | angle::AnglePlatformImpl::angleResetDisplayPlatform = |
| 221 | reinterpret_cast<angle::ResetDisplayPlatformFunc>( |
| 222 | eglGetProcAddress("ANGLEResetDisplayPlatform")); |
| 223 | |
| 224 | angle::PlatformMethods* platformMethods = nullptr; |
| 225 | if (!((angle::AnglePlatformImpl::angleGetDisplayPlatform)(dpy, angle::g_PlatformMethodNames, |
| 226 | angle::g_NumPlatformMethods, nullptr, |
| 227 | &platformMethods))) { |
| 228 | ALOGE("ANGLEGetDisplayPlatform call failed!"); |
| 229 | return false; |
| 230 | } |
| 231 | if (platformMethods) { |
| 232 | angle::AnglePlatformImpl::assignAnglePlatformMethods(platformMethods); |
| 233 | } else { |
| 234 | ALOGE("In initializeAnglePlatform() platformMethods struct ptr is NULL. Not assigning " |
| 235 | "tracing function ptrs!"); |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 240 | static EGLDisplay getDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 241 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 242 | |
| 243 | // Locally define this until EGL 1.5 is supported |
| 244 | typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYPROC)(EGLenum platform, void* native_display, |
| 245 | const EGLAttrib* attrib_list); |
| 246 | |
| 247 | PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay = |
| 248 | reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>( |
| 249 | cnx->egl.eglGetProcAddress("eglGetPlatformDisplay")); |
| 250 | |
| 251 | if (eglGetPlatformDisplay) { |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 252 | std::vector<EGLAttrib> attrs; |
| 253 | addAnglePlatformAttributes(cnx, nullptr, attrs); |
| 254 | attrs.push_back(EGL_NONE); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 255 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 256 | dpy = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 257 | reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attrs.data()); |
| 258 | if (dpy == EGL_NO_DISPLAY) { |
| 259 | ALOGE("eglGetPlatformDisplay failed!"); |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame^] | 260 | } else { |
| 261 | if (!initializeAnglePlatform(dpy)) { |
| 262 | ALOGE("initializeAnglePlatform failed!"); |
| 263 | } |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 264 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 265 | } else { |
| 266 | ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE", |
| 267 | display); |
| 268 | } |
| 269 | |
| 270 | return dpy; |
| 271 | } |
| 272 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 273 | EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) { |
| 274 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 275 | std::lock_guard<std::mutex> _l(lock); |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 276 | ATRACE_CALL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 277 | |
| 278 | // get our driver loader |
| 279 | Loader& loader(Loader::getInstance()); |
| 280 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 281 | egl_connection_t* const cnx = &gEGLImpl; |
| 282 | if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 283 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 284 | |
| 285 | if (cnx->useAngle) { |
| 286 | dpy = getDisplayAngle(display, cnx); |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 287 | } |
| 288 | if (dpy == EGL_NO_DISPLAY) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 289 | dpy = cnx->egl.eglGetDisplay(display); |
| 290 | } |
| 291 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 292 | disp.dpy = dpy; |
| 293 | if (dpy == EGL_NO_DISPLAY) { |
| 294 | loader.close(cnx->dso); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 295 | cnx->dso = nullptr; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | return EGLDisplay(uintptr_t(display) + 1U); |
| 300 | } |
| 301 | |
| 302 | EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) { |
| 303 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 304 | { // scope for refLock |
| 305 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 306 | refs++; |
| 307 | if (refs > 1) { |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 308 | if (major != nullptr) |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 309 | *major = VERSION_MAJOR; |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 310 | if (minor != nullptr) |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 311 | *minor = VERSION_MINOR; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 312 | while(!eglIsInitialized) { |
| 313 | refCond.wait(_l); |
| 314 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 315 | return EGL_TRUE; |
| 316 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 317 | while(eglIsInitialized) { |
| 318 | refCond.wait(_l); |
| 319 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 322 | { // scope for lock |
| 323 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 324 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 325 | setGLHooksThreadSpecific(&gHooksNoContext); |
| 326 | |
| 327 | // initialize each EGL and |
| 328 | // build our own extension string first, based on the extension we know |
| 329 | // and the extension supported by our client implementation |
| 330 | |
| 331 | egl_connection_t* const cnx = &gEGLImpl; |
| 332 | cnx->major = -1; |
| 333 | cnx->minor = -1; |
| 334 | if (cnx->dso) { |
| 335 | EGLDisplay idpy = disp.dpy; |
| 336 | if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { |
| 337 | //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p", |
| 338 | // idpy, cnx->major, cnx->minor, cnx); |
| 339 | |
| 340 | // display is now initialized |
| 341 | disp.state = egl_display_t::INITIALIZED; |
| 342 | |
| 343 | // get the query-strings for this display for each implementation |
| 344 | disp.queryString.vendor = cnx->egl.eglQueryString(idpy, |
| 345 | EGL_VENDOR); |
| 346 | disp.queryString.version = cnx->egl.eglQueryString(idpy, |
| 347 | EGL_VERSION); |
| 348 | disp.queryString.extensions = cnx->egl.eglQueryString(idpy, |
| 349 | EGL_EXTENSIONS); |
| 350 | disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, |
| 351 | EGL_CLIENT_APIS); |
| 352 | |
| 353 | } else { |
| 354 | ALOGW("eglInitialize(%p) failed (%s)", idpy, |
| 355 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // the query strings are per-display |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 360 | mVendorString = sVendorString; |
| 361 | mVersionString = sVersionString; |
| 362 | mClientApiString = sClientApiString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 363 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 364 | mExtensionString = gBuiltinExtensionString; |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 365 | |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 366 | hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace"); |
| 367 | |
| 368 | // Note: CDD requires that devices supporting wide color and/or HDR color also support |
| 369 | // the EGL_KHR_gl_colorspace extension. |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 370 | bool wideColorBoardConfig = |
| 371 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>( |
| 372 | false); |
| 373 | |
| 374 | // Add wide-color extensions if device can support wide-color |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 375 | if (wideColorBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 376 | mExtensionString.append( |
| 377 | "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear " |
| 378 | "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "); |
| 379 | } |
| 380 | |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 381 | bool hasHdrBoardConfig = |
| 382 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false); |
| 383 | |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 384 | if (hasHdrBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 385 | // hasHDRBoardConfig indicates the system is capable of supporting HDR content. |
| 386 | // Typically that means there is an HDR capable display attached, but could be |
| 387 | // support for attaching an HDR display. In either case, advertise support for |
| 388 | // HDR color spaces. |
| 389 | mExtensionString.append( |
| 390 | "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq "); |
| 391 | } |
| 392 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 393 | char const* start = gExtensionString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 394 | do { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 395 | // length of the extension name |
| 396 | size_t len = strcspn(start, " "); |
| 397 | if (len) { |
| 398 | // NOTE: we could avoid the copy if we had strnstr. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 399 | const std::string ext(start, len); |
| 400 | if (findExtension(disp.queryString.extensions, ext.c_str(), len)) { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 401 | mExtensionString.append(ext + " "); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 402 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 403 | // advance to the next extension name, skipping the space. |
| 404 | start += len; |
| 405 | start += (*start == ' ') ? 1 : 0; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 406 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 407 | } while (*start != '\0'); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 408 | |
| 409 | egl_cache_t::get()->initialize(this); |
| 410 | |
| 411 | char value[PROPERTY_VALUE_MAX]; |
| 412 | property_get("debug.egl.finish", value, "0"); |
| 413 | if (atoi(value)) { |
| 414 | finishOnSwap = true; |
| 415 | } |
| 416 | |
| 417 | property_get("debug.egl.traceGpuCompletion", value, "0"); |
| 418 | if (atoi(value)) { |
| 419 | traceGpuCompletion = true; |
| 420 | } |
| 421 | |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 422 | if (major != nullptr) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 423 | *major = VERSION_MAJOR; |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 424 | if (minor != nullptr) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 425 | *minor = VERSION_MINOR; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 428 | { // scope for refLock |
| 429 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 430 | eglIsInitialized = true; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 431 | refCond.notify_all(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 434 | return EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | EGLBoolean egl_display_t::terminate() { |
| 438 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 439 | { // scope for refLock |
| 440 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 441 | if (refs == 0) { |
| 442 | /* |
| 443 | * From the EGL spec (3.2): |
| 444 | * "Termination of a display that has already been terminated, |
| 445 | * (...), is allowed, but the only effect of such a call is |
| 446 | * to return EGL_TRUE (...) |
| 447 | */ |
| 448 | return EGL_TRUE; |
| 449 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 450 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 451 | // this is specific to Android, display termination is ref-counted. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 452 | refs--; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 453 | if (refs > 0) { |
| 454 | return EGL_TRUE; |
| 455 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | EGLBoolean res = EGL_FALSE; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 459 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 460 | { // scope for lock |
| 461 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 462 | |
| 463 | egl_connection_t* const cnx = &gEGLImpl; |
| 464 | if (cnx->dso && disp.state == egl_display_t::INITIALIZED) { |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame^] | 465 | // If we're using ANGLE reset any custom DisplayPlatform |
| 466 | if (cnx->useAngle && angle::AnglePlatformImpl::angleResetDisplayPlatform) { |
| 467 | (angle::AnglePlatformImpl::angleResetDisplayPlatform)(disp.dpy); |
| 468 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 469 | if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) { |
| 470 | ALOGW("eglTerminate(%p) failed (%s)", disp.dpy, |
| 471 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 472 | } |
| 473 | // REVISIT: it's unclear what to do if eglTerminate() fails |
| 474 | disp.state = egl_display_t::TERMINATED; |
| 475 | res = EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 476 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 477 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 478 | // Reset the extension string since it will be regenerated if we get |
| 479 | // reinitialized. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 480 | mExtensionString.clear(); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 481 | |
| 482 | // Mark all objects remaining in the list as terminated, unless |
| 483 | // there are no reference to them, it which case, we're free to |
| 484 | // delete them. |
| 485 | size_t count = objects.size(); |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 486 | ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 487 | for (auto o : objects) { |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 488 | o->destroy(); |
| 489 | } |
| 490 | |
| 491 | // this marks all object handles are "terminated" |
| 492 | objects.clear(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 495 | { // scope for refLock |
| 496 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 497 | eglIsInitialized = false; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 498 | refCond.notify_all(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 501 | return res; |
| 502 | } |
| 503 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 504 | void egl_display_t::loseCurrent(egl_context_t * cur_c) |
| 505 | { |
| 506 | if (cur_c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 507 | egl_display_t* display = cur_c->getDisplay(); |
| 508 | if (display) { |
| 509 | display->loseCurrentImpl(cur_c); |
| 510 | } |
| 511 | } |
| 512 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 513 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 514 | void egl_display_t::loseCurrentImpl(egl_context_t * cur_c) |
| 515 | { |
| 516 | // by construction, these are either 0 or valid (possibly terminated) |
| 517 | // it should be impossible for these to be invalid |
| 518 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 519 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 520 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 521 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 522 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 523 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 524 | cur_c->onLooseCurrent(); |
| 525 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 526 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 527 | |
| 528 | // This cannot be called with the lock held because it might end-up |
| 529 | // calling back into EGL (in particular when a surface is destroyed |
| 530 | // it calls ANativeWindow::disconnect |
| 531 | _cur_c.release(); |
| 532 | _cur_r.release(); |
| 533 | _cur_d.release(); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 537 | EGLSurface draw, EGLSurface read, EGLContext /*ctx*/, |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 538 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx) |
| 539 | { |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 540 | EGLBoolean result; |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 541 | |
| 542 | // by construction, these are either 0 or valid (possibly terminated) |
| 543 | // it should be impossible for these to be invalid |
| 544 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 545 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 546 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 547 | |
| 548 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 549 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 550 | if (c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 551 | result = c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 552 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 553 | if (result == EGL_TRUE) { |
| 554 | c->onMakeCurrent(draw, read); |
| 555 | } |
| 556 | } else { |
| 557 | result = cur_c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 558 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 559 | if (result == EGL_TRUE) { |
| 560 | cur_c->onLooseCurrent(); |
| 561 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 564 | |
| 565 | if (result == EGL_TRUE) { |
| 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(); |
| 572 | } |
| 573 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 574 | return result; |
| 575 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 576 | |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 577 | bool egl_display_t::haveExtension(const char* name, size_t nameLen) const { |
| 578 | if (!nameLen) { |
| 579 | nameLen = strlen(name); |
| 580 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 581 | return findExtension(mExtensionString.c_str(), name, nameLen); |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 584 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 585 | }; // namespace android |
| 586 | // ---------------------------------------------------------------------------- |