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