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