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 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame^] | 125 | EGLDisplay getDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx) { |
| 126 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 127 | |
| 128 | // Locally define this until EGL 1.5 is supported |
| 129 | typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYPROC)(EGLenum platform, void* native_display, |
| 130 | const EGLAttrib* attrib_list); |
| 131 | |
| 132 | PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay = |
| 133 | reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>( |
| 134 | cnx->egl.eglGetProcAddress("eglGetPlatformDisplay")); |
| 135 | |
| 136 | if (eglGetPlatformDisplay) { |
| 137 | intptr_t vendorEGL = (intptr_t)cnx->vendorEGL; |
| 138 | |
| 139 | EGLAttrib attrs[] = { |
| 140 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE, |
| 141 | EGL_PLATFORM_ANGLE_EGL_HANDLE_ANGLE, vendorEGL, |
| 142 | EGL_NONE // list terminator |
| 143 | }; |
| 144 | |
| 145 | // Initially, request the default display type |
| 146 | dpy = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, |
| 147 | reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attrs); |
| 148 | |
| 149 | } else { |
| 150 | ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE", |
| 151 | display); |
| 152 | } |
| 153 | |
| 154 | return dpy; |
| 155 | } |
| 156 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 157 | EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) { |
| 158 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 159 | std::lock_guard<std::mutex> _l(lock); |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 160 | ATRACE_CALL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 161 | |
| 162 | // get our driver loader |
| 163 | Loader& loader(Loader::getInstance()); |
| 164 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 165 | egl_connection_t* const cnx = &gEGLImpl; |
| 166 | if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame^] | 167 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 168 | |
| 169 | if (cnx->useAngle) { |
| 170 | dpy = getDisplayAngle(display, cnx); |
| 171 | } else { |
| 172 | dpy = cnx->egl.eglGetDisplay(display); |
| 173 | } |
| 174 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 175 | disp.dpy = dpy; |
| 176 | if (dpy == EGL_NO_DISPLAY) { |
| 177 | loader.close(cnx->dso); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 178 | cnx->dso = nullptr; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | return EGLDisplay(uintptr_t(display) + 1U); |
| 183 | } |
| 184 | |
| 185 | EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) { |
| 186 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 187 | { // scope for refLock |
| 188 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 189 | refs++; |
| 190 | if (refs > 1) { |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 191 | if (major != nullptr) |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 192 | *major = VERSION_MAJOR; |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 193 | if (minor != nullptr) |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 194 | *minor = VERSION_MINOR; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 195 | while(!eglIsInitialized) { |
| 196 | refCond.wait(_l); |
| 197 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 198 | return EGL_TRUE; |
| 199 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 200 | while(eglIsInitialized) { |
| 201 | refCond.wait(_l); |
| 202 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 205 | { // scope for lock |
| 206 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 207 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 208 | setGLHooksThreadSpecific(&gHooksNoContext); |
| 209 | |
| 210 | // initialize each EGL and |
| 211 | // build our own extension string first, based on the extension we know |
| 212 | // and the extension supported by our client implementation |
| 213 | |
| 214 | egl_connection_t* const cnx = &gEGLImpl; |
| 215 | cnx->major = -1; |
| 216 | cnx->minor = -1; |
| 217 | if (cnx->dso) { |
| 218 | EGLDisplay idpy = disp.dpy; |
| 219 | if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { |
| 220 | //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p", |
| 221 | // idpy, cnx->major, cnx->minor, cnx); |
| 222 | |
| 223 | // display is now initialized |
| 224 | disp.state = egl_display_t::INITIALIZED; |
| 225 | |
| 226 | // get the query-strings for this display for each implementation |
| 227 | disp.queryString.vendor = cnx->egl.eglQueryString(idpy, |
| 228 | EGL_VENDOR); |
| 229 | disp.queryString.version = cnx->egl.eglQueryString(idpy, |
| 230 | EGL_VERSION); |
| 231 | disp.queryString.extensions = cnx->egl.eglQueryString(idpy, |
| 232 | EGL_EXTENSIONS); |
| 233 | disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, |
| 234 | EGL_CLIENT_APIS); |
| 235 | |
| 236 | } else { |
| 237 | ALOGW("eglInitialize(%p) failed (%s)", idpy, |
| 238 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // the query strings are per-display |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 243 | mVendorString = sVendorString; |
| 244 | mVersionString = sVersionString; |
| 245 | mClientApiString = sClientApiString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 246 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 247 | mExtensionString = gBuiltinExtensionString; |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 248 | |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 249 | hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace"); |
| 250 | |
| 251 | // Note: CDD requires that devices supporting wide color and/or HDR color also support |
| 252 | // the EGL_KHR_gl_colorspace extension. |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 253 | bool wideColorBoardConfig = |
| 254 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>( |
| 255 | false); |
| 256 | |
| 257 | // Add wide-color extensions if device can support wide-color |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 258 | if (wideColorBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 259 | mExtensionString.append( |
| 260 | "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear " |
| 261 | "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 "); |
| 262 | } |
| 263 | |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 264 | bool hasHdrBoardConfig = |
| 265 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false); |
| 266 | |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 267 | if (hasHdrBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 268 | // hasHDRBoardConfig indicates the system is capable of supporting HDR content. |
| 269 | // Typically that means there is an HDR capable display attached, but could be |
| 270 | // support for attaching an HDR display. In either case, advertise support for |
| 271 | // HDR color spaces. |
| 272 | mExtensionString.append( |
| 273 | "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq "); |
| 274 | } |
| 275 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 276 | char const* start = gExtensionString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 277 | do { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 278 | // length of the extension name |
| 279 | size_t len = strcspn(start, " "); |
| 280 | if (len) { |
| 281 | // NOTE: we could avoid the copy if we had strnstr. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 282 | const std::string ext(start, len); |
| 283 | if (findExtension(disp.queryString.extensions, ext.c_str(), len)) { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 284 | mExtensionString.append(ext + " "); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 285 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 286 | // advance to the next extension name, skipping the space. |
| 287 | start += len; |
| 288 | start += (*start == ' ') ? 1 : 0; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 289 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 290 | } while (*start != '\0'); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 291 | |
| 292 | egl_cache_t::get()->initialize(this); |
| 293 | |
| 294 | char value[PROPERTY_VALUE_MAX]; |
| 295 | property_get("debug.egl.finish", value, "0"); |
| 296 | if (atoi(value)) { |
| 297 | finishOnSwap = true; |
| 298 | } |
| 299 | |
| 300 | property_get("debug.egl.traceGpuCompletion", value, "0"); |
| 301 | if (atoi(value)) { |
| 302 | traceGpuCompletion = true; |
| 303 | } |
| 304 | |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 305 | if (major != nullptr) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 306 | *major = VERSION_MAJOR; |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 307 | if (minor != nullptr) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 308 | *minor = VERSION_MINOR; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 311 | { // scope for refLock |
| 312 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 313 | eglIsInitialized = true; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 314 | refCond.notify_all(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 317 | return EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | EGLBoolean egl_display_t::terminate() { |
| 321 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 322 | { // scope for refLock |
| 323 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 324 | if (refs == 0) { |
| 325 | /* |
| 326 | * From the EGL spec (3.2): |
| 327 | * "Termination of a display that has already been terminated, |
| 328 | * (...), is allowed, but the only effect of such a call is |
| 329 | * to return EGL_TRUE (...) |
| 330 | */ |
| 331 | return EGL_TRUE; |
| 332 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 333 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 334 | // this is specific to Android, display termination is ref-counted. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 335 | refs--; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 336 | if (refs > 0) { |
| 337 | return EGL_TRUE; |
| 338 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | EGLBoolean res = EGL_FALSE; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 342 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 343 | { // scope for lock |
| 344 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 345 | |
| 346 | egl_connection_t* const cnx = &gEGLImpl; |
| 347 | if (cnx->dso && disp.state == egl_display_t::INITIALIZED) { |
| 348 | if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) { |
| 349 | ALOGW("eglTerminate(%p) failed (%s)", disp.dpy, |
| 350 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 351 | } |
| 352 | // REVISIT: it's unclear what to do if eglTerminate() fails |
| 353 | disp.state = egl_display_t::TERMINATED; |
| 354 | res = EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 355 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 356 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 357 | // Reset the extension string since it will be regenerated if we get |
| 358 | // reinitialized. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 359 | mExtensionString.clear(); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 360 | |
| 361 | // Mark all objects remaining in the list as terminated, unless |
| 362 | // there are no reference to them, it which case, we're free to |
| 363 | // delete them. |
| 364 | size_t count = objects.size(); |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 365 | ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 366 | for (auto o : objects) { |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 367 | o->destroy(); |
| 368 | } |
| 369 | |
| 370 | // this marks all object handles are "terminated" |
| 371 | objects.clear(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 374 | { // scope for refLock |
| 375 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 376 | eglIsInitialized = false; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 377 | refCond.notify_all(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 380 | return res; |
| 381 | } |
| 382 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 383 | void egl_display_t::loseCurrent(egl_context_t * cur_c) |
| 384 | { |
| 385 | if (cur_c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 386 | egl_display_t* display = cur_c->getDisplay(); |
| 387 | if (display) { |
| 388 | display->loseCurrentImpl(cur_c); |
| 389 | } |
| 390 | } |
| 391 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 392 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 393 | void egl_display_t::loseCurrentImpl(egl_context_t * cur_c) |
| 394 | { |
| 395 | // by construction, these are either 0 or valid (possibly terminated) |
| 396 | // it should be impossible for these to be invalid |
| 397 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 398 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 399 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 400 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 401 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 402 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 403 | cur_c->onLooseCurrent(); |
| 404 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 405 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 406 | |
| 407 | // This cannot be called with the lock held because it might end-up |
| 408 | // calling back into EGL (in particular when a surface is destroyed |
| 409 | // it calls ANativeWindow::disconnect |
| 410 | _cur_c.release(); |
| 411 | _cur_r.release(); |
| 412 | _cur_d.release(); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | 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] | 416 | EGLSurface draw, EGLSurface read, EGLContext /*ctx*/, |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 417 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx) |
| 418 | { |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 419 | EGLBoolean result; |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 420 | |
| 421 | // by construction, these are either 0 or valid (possibly terminated) |
| 422 | // it should be impossible for these to be invalid |
| 423 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 424 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 425 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 426 | |
| 427 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 428 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 429 | if (c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 430 | result = c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 431 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 432 | if (result == EGL_TRUE) { |
| 433 | c->onMakeCurrent(draw, read); |
| 434 | } |
| 435 | } else { |
| 436 | result = cur_c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 437 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 438 | if (result == EGL_TRUE) { |
| 439 | cur_c->onLooseCurrent(); |
| 440 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 443 | |
| 444 | if (result == EGL_TRUE) { |
| 445 | // This cannot be called with the lock held because it might end-up |
| 446 | // calling back into EGL (in particular when a surface is destroyed |
| 447 | // it calls ANativeWindow::disconnect |
| 448 | _cur_c.release(); |
| 449 | _cur_r.release(); |
| 450 | _cur_d.release(); |
| 451 | } |
| 452 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 453 | return result; |
| 454 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 455 | |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 456 | bool egl_display_t::haveExtension(const char* name, size_t nameLen) const { |
| 457 | if (!nameLen) { |
| 458 | nameLen = strlen(name); |
| 459 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 460 | return findExtension(mExtensionString.c_str(), name, nameLen); |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 463 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 464 | }; // namespace android |
| 465 | // ---------------------------------------------------------------------------- |