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