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