egl_display.cpp revision b29e5e8c2682ae145e8c56d9afb061f8da7f854c
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 17#define __STDC_LIMIT_MACROS 1 18 19#include <string.h> 20 21#include "egl_cache.h" 22#include "egl_display.h" 23#include "egl_object.h" 24#include "egl_tls.h" 25#include "egl_impl.h" 26#include "Loader.h" 27#include <cutils/properties.h> 28 29// ---------------------------------------------------------------------------- 30namespace android { 31// ---------------------------------------------------------------------------- 32 33static char const * const sVendorString = "Android"; 34static char const * const sVersionString = "1.4 Android META-EGL"; 35static 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// 47static 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 62 63extern void initEglTraceLevel(); 64extern void initEglDebugLevel(); 65extern void setGLHooksThreadSpecific(gl_hooks_t const *value); 66 67// ---------------------------------------------------------------------------- 68 69egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS]; 70 71egl_display_t::egl_display_t() : 72 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), 73 mWakeCount(0) { 74} 75 76egl_display_t::~egl_display_t() { 77 magic = 0; 78 egl_cache_t::get()->terminate(); 79} 80 81egl_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 86void egl_display_t::addObject(egl_object_t* object) { 87 Mutex::Autolock _l(lock); 88 objects.add(object); 89} 90 91void egl_display_t::removeObject(egl_object_t* object) { 92 Mutex::Autolock _l(lock); 93 objects.remove(object); 94} 95 96bool egl_display_t::getObject(egl_object_t* object) const { 97 Mutex::Autolock _l(lock); 98 if (objects.indexOf(object) >= 0) { 99 if (object->getDisplay() == this) { 100 object->incRef(); 101 return true; 102 } 103 } 104 return false; 105} 106 107EGLDisplay 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 114EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) { 115 116 Mutex::Autolock _l(lock); 117 118 // get our driver loader 119 Loader& loader(Loader::getInstance()); 120 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; 128 } 129 } 130 131 return EGLDisplay(uintptr_t(display) + 1U); 132} 133 134EGLBoolean 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(); 152 initEglDebugLevel(); 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 161 162 egl_connection_t* const cnx = &gEGLImpl; 163 cnx->major = -1; 164 cnx->minor = -1; 165 if (cnx->dso) { 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) { 177 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); 178 } 179#endif 180 181 EGLDisplay idpy = disp.dpy; 182 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { 183 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p", 184 // idpy, cnx->major, cnx->minor, cnx); 185 186 // display is now initialized 187 disp.state = egl_display_t::INITIALIZED; 188 189 // get the query-strings for this display for each implementation 190 disp.queryString.vendor = cnx->egl.eglQueryString(idpy, 191 EGL_VENDOR); 192 disp.queryString.version = cnx->egl.eglQueryString(idpy, 193 EGL_VERSION); 194 disp.queryString.extensions = cnx->egl.eglQueryString(idpy, 195 EGL_EXTENSIONS); 196 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, 197 EGL_CLIENT_APIS); 198 199 } else { 200 ALOGW("eglInitialize(%p) failed (%s)", idpy, 201 egl_tls_t::egl_strerror(cnx->egl.eglGetError())); 202 } 203 } 204 205 // the query strings are per-display 206 mVendorString.setTo(sVendorString); 207 mVersionString.setTo(sVersionString); 208 mClientApiString.setTo(sClientApiString); 209 210 // we only add extensions that exist in the implementation 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; 219 if (len) { 220 // NOTE: we could avoid the copy if we had strnstr. 221 const String8 ext(start, len); 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); 229 } 230 } 231 } 232 // process the next extension string, and skip the space. 233 start = end + 1; 234 } 235 } while (end); 236 237 egl_cache_t::get()->initialize(this); 238 239 char value[PROPERTY_VALUE_MAX]; 240 property_get("debug.egl.finish", value, "0"); 241 if (atoi(value)) { 242 finishOnSwap = true; 243 } 244 245 property_get("debug.egl.traceGpuCompletion", value, "0"); 246 if (atoi(value)) { 247 traceGpuCompletion = true; 248 } 249 250 refs++; 251 if (major != NULL) 252 *major = VERSION_MAJOR; 253 if (minor != NULL) 254 *minor = VERSION_MINOR; 255 return EGL_TRUE; 256} 257 258EGLBoolean egl_display_t::terminate() { 259 260 Mutex::Autolock _l(lock); 261 262 if (refs == 0) { 263 return setError(EGL_NOT_INITIALIZED, EGL_FALSE); 264 } 265 266 // this is specific to Android, display termination is ref-counted. 267 if (refs > 1) { 268 refs--; 269 return EGL_TRUE; 270 } 271 272 EGLBoolean res = EGL_FALSE; 273 egl_connection_t* const cnx = &gEGLImpl; 274 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) { 275 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) { 276 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy, 277 egl_tls_t::egl_strerror(cnx->egl.eglGetError())); 278 } 279 // REVISIT: it's unclear what to do if eglTerminate() fails 280 disp.state = egl_display_t::TERMINATED; 281 res = EGL_TRUE; 282 } 283 284 // Mark all objects remaining in the list as terminated, unless 285 // there are no reference to them, it which case, we're free to 286 // delete them. 287 size_t count = objects.size(); 288 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count); 289 for (size_t i=0 ; i<count ; i++) { 290 egl_object_t* o = objects.itemAt(i); 291 o->destroy(); 292 } 293 294 // this marks all object handles are "terminated" 295 objects.clear(); 296 297 refs--; 298 return res; 299} 300 301void egl_display_t::loseCurrent(egl_context_t * cur_c) 302{ 303 if (cur_c) { 304 egl_display_t* display = cur_c->getDisplay(); 305 if (display) { 306 display->loseCurrentImpl(cur_c); 307 } 308 } 309} 310 311void egl_display_t::loseCurrentImpl(egl_context_t * cur_c) 312{ 313 // by construction, these are either 0 or valid (possibly terminated) 314 // it should be impossible for these to be invalid 315 ContextRef _cur_c(cur_c); 316 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL); 317 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL); 318 319 { // scope for the lock 320 Mutex::Autolock _l(lock); 321 cur_c->onLooseCurrent(); 322 323 } 324 325 // This cannot be called with the lock held because it might end-up 326 // calling back into EGL (in particular when a surface is destroyed 327 // it calls ANativeWindow::disconnect 328 _cur_c.release(); 329 _cur_r.release(); 330 _cur_d.release(); 331} 332 333EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, 334 EGLSurface draw, EGLSurface read, EGLContext ctx, 335 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx) 336{ 337 EGLBoolean result; 338 339 // by construction, these are either 0 or valid (possibly terminated) 340 // it should be impossible for these to be invalid 341 ContextRef _cur_c(cur_c); 342 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL); 343 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL); 344 345 { // scope for the lock 346 Mutex::Autolock _l(lock); 347 if (c) { 348 result = c->cnx->egl.eglMakeCurrent( 349 disp.dpy, impl_draw, impl_read, impl_ctx); 350 if (result == EGL_TRUE) { 351 c->onMakeCurrent(draw, read); 352 } 353 } else { 354 result = cur_c->cnx->egl.eglMakeCurrent( 355 disp.dpy, impl_draw, impl_read, impl_ctx); 356 if (result == EGL_TRUE) { 357 cur_c->onLooseCurrent(); 358 } 359 } 360 } 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 371 return result; 372} 373 374bool egl_display_t::enter() { 375 Mutex::Autolock _l(lock); 376 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX, 377 "Invalid WakeCount (%d) on enter\n", mWakeCount); 378 mWakeCount++; 379 return true; 380} 381 382void egl_display_t::leave() { 383 Mutex::Autolock _l(lock); 384 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount); 385 mWakeCount--; 386} 387 388// ---------------------------------------------------------------------------- 389}; // namespace android 390// ---------------------------------------------------------------------------- 391