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