| 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 | 
|  | 18 |  | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 19 | #include <string.h> | 
|  | 20 |  | 
| Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 21 | #include "../egl_impl.h" | 
|  | 22 |  | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 23 | #include "egl_cache.h" | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 24 | #include "egl_display.h" | 
|  | 25 | #include "egl_object.h" | 
|  | 26 | #include "egl_tls.h" | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 27 | #include "Loader.h" | 
| Mathias Agopian | 7db993a | 2012-03-25 00:49:46 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | // ---------------------------------------------------------------------------- | 
|  | 31 | namespace android { | 
|  | 32 | // ---------------------------------------------------------------------------- | 
|  | 33 |  | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 34 | static char const * const sVendorString     = "Android"; | 
|  | 35 | static char const * const sVersionString    = "1.4 Android META-EGL"; | 
| Mathias Agopian | cc2b156 | 2012-05-21 14:01:37 -0700 | [diff] [blame] | 36 | static char const * const sClientApiString  = "OpenGL_ES"; | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 37 |  | 
| Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 38 | extern char const * const gBuiltinExtensionString; | 
| Mathias Agopian | e9b3dfb | 2013-03-27 14:30:19 -0700 | [diff] [blame] | 39 | extern char const * const gExtensionString; | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 40 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 41 | extern void initEglTraceLevel(); | 
| Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 42 | extern void initEglDebugLevel(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 43 | extern void setGLHooksThreadSpecific(gl_hooks_t const *value); | 
|  | 44 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 45 | // ---------------------------------------------------------------------------- | 
|  | 46 |  | 
| Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 47 | static bool findExtension(const char* exts, const char* name, size_t nameLen) { | 
|  | 48 | if (exts) { | 
|  | 49 | const char* match = strstr(exts, name); | 
|  | 50 | if (match && (match[nameLen] == '\0' || match[nameLen] == ' ')) { | 
|  | 51 | return true; | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 | return false; | 
|  | 55 | } | 
|  | 56 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 57 | egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS]; | 
|  | 58 |  | 
|  | 59 | egl_display_t::egl_display_t() : | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 60 | magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | egl_display_t::~egl_display_t() { | 
|  | 64 | magic = 0; | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 65 | egl_cache_t::get()->terminate(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | egl_display_t* egl_display_t::get(EGLDisplay dpy) { | 
|  | 69 | uintptr_t index = uintptr_t(dpy)-1U; | 
|  | 70 | return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index]; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | void egl_display_t::addObject(egl_object_t* object) { | 
|  | 74 | Mutex::Autolock _l(lock); | 
|  | 75 | objects.add(object); | 
|  | 76 | } | 
|  | 77 |  | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 78 | void egl_display_t::removeObject(egl_object_t* object) { | 
|  | 79 | Mutex::Autolock _l(lock); | 
|  | 80 | objects.remove(object); | 
|  | 81 | } | 
|  | 82 |  | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 83 | bool egl_display_t::getObject(egl_object_t* object) const { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 84 | Mutex::Autolock _l(lock); | 
|  | 85 | if (objects.indexOf(object) >= 0) { | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 86 | if (object->getDisplay() == this) { | 
|  | 87 | object->incRef(); | 
|  | 88 | return true; | 
|  | 89 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 90 | } | 
|  | 91 | return false; | 
|  | 92 | } | 
|  | 93 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 94 | EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) { | 
|  | 95 | if (uintptr_t(disp) >= NUM_DISPLAYS) | 
|  | 96 | return NULL; | 
|  | 97 |  | 
|  | 98 | return sDisplay[uintptr_t(disp)].getDisplay(disp); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) { | 
|  | 102 |  | 
|  | 103 | Mutex::Autolock _l(lock); | 
|  | 104 |  | 
|  | 105 | // get our driver loader | 
|  | 106 | Loader& loader(Loader::getInstance()); | 
|  | 107 |  | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 108 | egl_connection_t* const cnx = &gEGLImpl; | 
|  | 109 | if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) { | 
|  | 110 | EGLDisplay dpy = cnx->egl.eglGetDisplay(display); | 
|  | 111 | disp.dpy = dpy; | 
|  | 112 | if (dpy == EGL_NO_DISPLAY) { | 
|  | 113 | loader.close(cnx->dso); | 
|  | 114 | cnx->dso = NULL; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | return EGLDisplay(uintptr_t(display) + 1U); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) { | 
|  | 122 |  | 
|  | 123 | Mutex::Autolock _l(lock); | 
|  | 124 |  | 
|  | 125 | if (refs > 0) { | 
|  | 126 | if (major != NULL) | 
|  | 127 | *major = VERSION_MAJOR; | 
|  | 128 | if (minor != NULL) | 
|  | 129 | *minor = VERSION_MINOR; | 
|  | 130 | refs++; | 
|  | 131 | return EGL_TRUE; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | #if EGL_TRACE | 
|  | 135 |  | 
|  | 136 | // Called both at early_init time and at this time. (Early_init is pre-zygote, so | 
|  | 137 | // the information from that call may be stale.) | 
|  | 138 | initEglTraceLevel(); | 
| Siva Velusamy | b13c78f | 2012-03-09 14:51:28 -0800 | [diff] [blame] | 139 | initEglDebugLevel(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | #endif | 
|  | 142 |  | 
|  | 143 | setGLHooksThreadSpecific(&gHooksNoContext); | 
|  | 144 |  | 
|  | 145 | // initialize each EGL and | 
|  | 146 | // build our own extension string first, based on the extension we know | 
|  | 147 | // and the extension supported by our client implementation | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | egl_connection_t* const cnx = &gEGLImpl; | 
|  | 150 | cnx->major = -1; | 
|  | 151 | cnx->minor = -1; | 
|  | 152 | if (cnx->dso) { | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 153 | EGLDisplay idpy = disp.dpy; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 154 | if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 155 | //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p", | 
|  | 156 | //        idpy, cnx->major, cnx->minor, cnx); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 157 |  | 
|  | 158 | // display is now initialized | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 159 | disp.state = egl_display_t::INITIALIZED; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | // get the query-strings for this display for each implementation | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 162 | disp.queryString.vendor = cnx->egl.eglQueryString(idpy, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 163 | EGL_VENDOR); | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 164 | disp.queryString.version = cnx->egl.eglQueryString(idpy, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 165 | EGL_VERSION); | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 166 | disp.queryString.extensions = cnx->egl.eglQueryString(idpy, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 167 | EGL_EXTENSIONS); | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 168 | disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 169 | EGL_CLIENT_APIS); | 
|  | 170 |  | 
|  | 171 | } else { | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 172 | ALOGW("eglInitialize(%p) failed (%s)", idpy, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 173 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 177 | // the query strings are per-display | 
|  | 178 | mVendorString.setTo(sVendorString); | 
|  | 179 | mVersionString.setTo(sVersionString); | 
|  | 180 | mClientApiString.setTo(sClientApiString); | 
|  | 181 |  | 
| Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 182 | mExtensionString.setTo(gBuiltinExtensionString); | 
| Mathias Agopian | e9b3dfb | 2013-03-27 14:30:19 -0700 | [diff] [blame] | 183 | char const* start = gExtensionString; | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 184 | char const* end; | 
|  | 185 | do { | 
|  | 186 | // find the space separating this extension for the next one | 
|  | 187 | end = strchr(start, ' '); | 
|  | 188 | if (end) { | 
|  | 189 | // length of the extension string | 
|  | 190 | const size_t len = end - start; | 
| Mathias Agopian | f3ae82d | 2011-11-16 16:49:25 -0800 | [diff] [blame] | 191 | if (len) { | 
|  | 192 | // NOTE: we could avoid the copy if we had strnstr. | 
|  | 193 | const String8 ext(start, len); | 
| Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 194 | if (findExtension(disp.queryString.extensions, ext.string(), | 
|  | 195 | len)) { | 
|  | 196 | mExtensionString.append(start, len+1); | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 197 | } | 
|  | 198 | } | 
|  | 199 | // process the next extension string, and skip the space. | 
|  | 200 | start = end + 1; | 
|  | 201 | } | 
|  | 202 | } while (end); | 
|  | 203 |  | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 204 | egl_cache_t::get()->initialize(this); | 
|  | 205 |  | 
| Mathias Agopian | 7db993a | 2012-03-25 00:49:46 -0700 | [diff] [blame] | 206 | char value[PROPERTY_VALUE_MAX]; | 
|  | 207 | property_get("debug.egl.finish", value, "0"); | 
|  | 208 | if (atoi(value)) { | 
|  | 209 | finishOnSwap = true; | 
|  | 210 | } | 
|  | 211 |  | 
| Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 212 | property_get("debug.egl.traceGpuCompletion", value, "0"); | 
|  | 213 | if (atoi(value)) { | 
|  | 214 | traceGpuCompletion = true; | 
|  | 215 | } | 
|  | 216 |  | 
| Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 217 | refs++; | 
|  | 218 | if (major != NULL) | 
|  | 219 | *major = VERSION_MAJOR; | 
|  | 220 | if (minor != NULL) | 
|  | 221 | *minor = VERSION_MINOR; | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 222 |  | 
|  | 223 | mHibernation.setDisplayValid(true); | 
|  | 224 |  | 
| Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 225 | return EGL_TRUE; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
|  | 228 | EGLBoolean egl_display_t::terminate() { | 
|  | 229 |  | 
|  | 230 | Mutex::Autolock _l(lock); | 
|  | 231 |  | 
|  | 232 | if (refs == 0) { | 
| Mathias Agopian | fe98127 | 2012-06-13 15:21:21 -0700 | [diff] [blame] | 233 | /* | 
|  | 234 | * From the EGL spec (3.2): | 
|  | 235 | * "Termination of a display that has already been terminated, | 
|  | 236 | *  (...), is allowed, but the only effect of such a call is | 
|  | 237 | *  to return EGL_TRUE (...) | 
|  | 238 | */ | 
|  | 239 | return EGL_TRUE; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
|  | 242 | // this is specific to Android, display termination is ref-counted. | 
|  | 243 | if (refs > 1) { | 
|  | 244 | refs--; | 
|  | 245 | return EGL_TRUE; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | EGLBoolean res = EGL_FALSE; | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 249 | egl_connection_t* const cnx = &gEGLImpl; | 
|  | 250 | if (cnx->dso && disp.state == egl_display_t::INITIALIZED) { | 
|  | 251 | if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) { | 
|  | 252 | ALOGW("eglTerminate(%p) failed (%s)", disp.dpy, | 
|  | 253 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 254 | } | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 255 | // REVISIT: it's unclear what to do if eglTerminate() fails | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 256 | disp.state = egl_display_t::TERMINATED; | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 257 | res = EGL_TRUE; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 260 | mHibernation.setDisplayValid(false); | 
|  | 261 |  | 
| Jamie Gennis | a08cf6e | 2012-09-16 14:02:20 -0700 | [diff] [blame] | 262 | // Reset the extension string since it will be regenerated if we get | 
|  | 263 | // reinitialized. | 
|  | 264 | mExtensionString.setTo(""); | 
|  | 265 |  | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 266 | // Mark all objects remaining in the list as terminated, unless | 
|  | 267 | // there are no reference to them, it which case, we're free to | 
|  | 268 | // delete them. | 
|  | 269 | size_t count = objects.size(); | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 270 | ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count); | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 271 | for (size_t i=0 ; i<count ; i++) { | 
|  | 272 | egl_object_t* o = objects.itemAt(i); | 
|  | 273 | o->destroy(); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | // this marks all object handles are "terminated" | 
|  | 277 | objects.clear(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 278 |  | 
|  | 279 | refs--; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 280 | return res; | 
|  | 281 | } | 
|  | 282 |  | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 283 | void egl_display_t::loseCurrent(egl_context_t * cur_c) | 
|  | 284 | { | 
|  | 285 | if (cur_c) { | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 286 | egl_display_t* display = cur_c->getDisplay(); | 
|  | 287 | if (display) { | 
|  | 288 | display->loseCurrentImpl(cur_c); | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 | } | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 292 |  | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 293 | void egl_display_t::loseCurrentImpl(egl_context_t * cur_c) | 
|  | 294 | { | 
|  | 295 | // by construction, these are either 0 or valid (possibly terminated) | 
|  | 296 | // it should be impossible for these to be invalid | 
|  | 297 | ContextRef _cur_c(cur_c); | 
|  | 298 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL); | 
|  | 299 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL); | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 300 |  | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 301 | { // scope for the lock | 
|  | 302 | Mutex::Autolock _l(lock); | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 303 | cur_c->onLooseCurrent(); | 
|  | 304 |  | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 305 | } | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 306 |  | 
|  | 307 | // This cannot be called with the lock held because it might end-up | 
|  | 308 | // calling back into EGL (in particular when a surface is destroyed | 
|  | 309 | // it calls ANativeWindow::disconnect | 
|  | 310 | _cur_c.release(); | 
|  | 311 | _cur_r.release(); | 
|  | 312 | _cur_d.release(); | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 313 | } | 
|  | 314 |  | 
|  | 315 | 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] | 316 | EGLSurface draw, EGLSurface read, EGLContext /*ctx*/, | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 317 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx) | 
|  | 318 | { | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 319 | EGLBoolean result; | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 320 |  | 
|  | 321 | // by construction, these are either 0 or valid (possibly terminated) | 
|  | 322 | // it should be impossible for these to be invalid | 
|  | 323 | ContextRef _cur_c(cur_c); | 
|  | 324 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL); | 
|  | 325 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL); | 
|  | 326 |  | 
|  | 327 | { // scope for the lock | 
|  | 328 | Mutex::Autolock _l(lock); | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 329 | if (c) { | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 330 | result = c->cnx->egl.eglMakeCurrent( | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 331 | disp.dpy, impl_draw, impl_read, impl_ctx); | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 332 | if (result == EGL_TRUE) { | 
|  | 333 | c->onMakeCurrent(draw, read); | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 334 | if (!cur_c) { | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 335 | mHibernation.incWakeCount(HibernationMachine::STRONG); | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 336 | } | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 337 | } | 
|  | 338 | } else { | 
|  | 339 | result = cur_c->cnx->egl.eglMakeCurrent( | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 340 | disp.dpy, impl_draw, impl_read, impl_ctx); | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 341 | if (result == EGL_TRUE) { | 
|  | 342 | cur_c->onLooseCurrent(); | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 343 | mHibernation.decWakeCount(HibernationMachine::STRONG); | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 344 | } | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 345 | } | 
|  | 346 | } | 
| Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 347 |  | 
|  | 348 | if (result == EGL_TRUE) { | 
|  | 349 | // This cannot be called with the lock held because it might end-up | 
|  | 350 | // calling back into EGL (in particular when a surface is destroyed | 
|  | 351 | // it calls ANativeWindow::disconnect | 
|  | 352 | _cur_c.release(); | 
|  | 353 | _cur_r.release(); | 
|  | 354 | _cur_d.release(); | 
|  | 355 | } | 
|  | 356 |  | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 357 | return result; | 
|  | 358 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 359 |  | 
| Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 360 | bool egl_display_t::haveExtension(const char* name, size_t nameLen) const { | 
|  | 361 | if (!nameLen) { | 
|  | 362 | nameLen = strlen(name); | 
|  | 363 | } | 
|  | 364 | return findExtension(mExtensionString.string(), name, nameLen); | 
|  | 365 | } | 
|  | 366 |  | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 367 | // ---------------------------------------------------------------------------- | 
|  | 368 |  | 
|  | 369 | bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) { | 
|  | 370 | Mutex::Autolock _l(mLock); | 
| Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 371 | ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX, | 
|  | 372 | "Invalid WakeCount (%d) on enter\n", mWakeCount); | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 373 |  | 
| Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 374 | mWakeCount++; | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 375 | if (strength == STRONG) | 
|  | 376 | mAttemptHibernation = false; | 
|  | 377 |  | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 378 | if (CC_UNLIKELY(mHibernating)) { | 
|  | 379 | ALOGV("Awakening\n"); | 
|  | 380 | egl_connection_t* const cnx = &gEGLImpl; | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 381 |  | 
|  | 382 | // These conditions should be guaranteed before entering hibernation; | 
|  | 383 | // we don't want to get into a state where we can't wake up. | 
|  | 384 | ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG, | 
|  | 385 | "Invalid hibernation state, unable to awaken\n"); | 
|  | 386 |  | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 387 | if (!cnx->egl.eglAwakenProcessIMG()) { | 
|  | 388 | ALOGE("Failed to awaken EGL implementation\n"); | 
|  | 389 | return false; | 
|  | 390 | } | 
|  | 391 | mHibernating = false; | 
|  | 392 | } | 
| Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 393 | return true; | 
|  | 394 | } | 
|  | 395 |  | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 396 | void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) { | 
|  | 397 | Mutex::Autolock _l(mLock); | 
| Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 398 | ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount); | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 399 |  | 
|  | 400 | mWakeCount--; | 
|  | 401 | if (strength == STRONG) | 
|  | 402 | mAttemptHibernation = true; | 
|  | 403 |  | 
|  | 404 | if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) { | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 405 | egl_connection_t* const cnx = &gEGLImpl; | 
|  | 406 | mAttemptHibernation = false; | 
| Jesse Hall | 201f3b2 | 2012-05-04 14:52:40 -0700 | [diff] [blame] | 407 | if (mAllowHibernation && mDpyValid && | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 408 | cnx->egl.eglHibernateProcessIMG && | 
|  | 409 | cnx->egl.eglAwakenProcessIMG) { | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 410 | ALOGV("Hibernating\n"); | 
|  | 411 | if (!cnx->egl.eglHibernateProcessIMG()) { | 
|  | 412 | ALOGE("Failed to hibernate EGL implementation\n"); | 
|  | 413 | return; | 
|  | 414 | } | 
|  | 415 | mHibernating = true; | 
|  | 416 | } | 
|  | 417 | } | 
|  | 418 | } | 
|  | 419 |  | 
| Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 420 | void egl_display_t::HibernationMachine::setDisplayValid(bool valid) { | 
|  | 421 | Mutex::Autolock _l(mLock); | 
|  | 422 | mDpyValid = valid; | 
| Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 425 | // ---------------------------------------------------------------------------- | 
|  | 426 | }; // namespace android | 
|  | 427 | // ---------------------------------------------------------------------------- |