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