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