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