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