egl.cpp revision c3ce8809728cad1724458006a38892d2fa0d0e4d
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#include <ctype.h> 18#include <stdlib.h> 19#include <string.h> 20#include <errno.h> 21#include <dlfcn.h> 22 23#include <sys/ioctl.h> 24 25#if HAVE_ANDROID_OS 26#include <linux/android_pmem.h> 27#endif 28 29#include <EGL/egl.h> 30#include <EGL/eglext.h> 31#include <GLES/gl.h> 32#include <GLES/glext.h> 33 34#include <cutils/log.h> 35#include <cutils/atomic.h> 36#include <cutils/properties.h> 37#include <cutils/memory.h> 38 39#include <utils/SortedVector.h> 40#include <utils/KeyedVector.h> 41#include <utils/String8.h> 42 43#include <ui/egl/android_natives.h> 44 45#include "hooks.h" 46#include "egl_impl.h" 47#include "Loader.h" 48 49#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r) 50 51// ---------------------------------------------------------------------------- 52namespace android { 53// ---------------------------------------------------------------------------- 54 55#define VERSION_MAJOR 1 56#define VERSION_MINOR 4 57static char const * const gVendorString = "Android"; 58static char const * const gVersionString = "1.4 Android META-EGL"; 59static char const * const gClientApiString = "OpenGL ES"; 60static char const * const gExtensionString = 61 "EGL_KHR_image " 62 "EGL_KHR_image_base " 63 "EGL_KHR_image_pixmap " 64 "EGL_ANDROID_image_native_buffer " 65 "EGL_ANDROID_swap_rectangle " 66 ; 67 68// ---------------------------------------------------------------------------- 69 70class egl_object_t { 71 static SortedVector<egl_object_t*> sObjects; 72 static Mutex sLock; 73 74 volatile int32_t terminated; 75 mutable volatile int32_t count; 76 77public: 78 egl_object_t() : terminated(0), count(1) { 79 Mutex::Autolock _l(sLock); 80 sObjects.add(this); 81 } 82 83 inline bool isAlive() const { return !terminated; } 84 85private: 86 bool get() { 87 Mutex::Autolock _l(sLock); 88 if (egl_object_t::sObjects.indexOf(this) >= 0) { 89 android_atomic_inc(&count); 90 return true; 91 } 92 return false; 93 } 94 95 bool put() { 96 Mutex::Autolock _l(sLock); 97 if (android_atomic_dec(&count) == 1) { 98 sObjects.remove(this); 99 return true; 100 } 101 return false; 102 } 103 104public: 105 template <typename N, typename T> 106 struct LocalRef { 107 N* ref; 108 LocalRef(T o) : ref(0) { 109 N* native = reinterpret_cast<N*>(o); 110 if (o && native->get()) { 111 ref = native; 112 } 113 } 114 ~LocalRef() { 115 if (ref && ref->put()) { 116 delete ref; 117 } 118 } 119 inline N* get() { 120 return ref; 121 } 122 void acquire() const { 123 if (ref) { 124 android_atomic_inc(&ref->count); 125 } 126 } 127 void release() const { 128 if (ref) { 129 int32_t c = android_atomic_dec(&ref->count); 130 // ref->count cannot be 1 prior atomic_dec because we have 131 // a reference, and if we have one, it means there was 132 // already one before us. 133 LOGE_IF(c==1, "refcount is now 0 in release()"); 134 } 135 } 136 void terminate() { 137 if (ref) { 138 ref->terminated = 1; 139 release(); 140 } 141 } 142 }; 143}; 144 145SortedVector<egl_object_t*> egl_object_t::sObjects; 146Mutex egl_object_t::sLock; 147 148 149struct egl_config_t { 150 egl_config_t() {} 151 egl_config_t(int impl, EGLConfig config) 152 : impl(impl), config(config), configId(0), implConfigId(0) { } 153 int impl; // the implementation this config is for 154 EGLConfig config; // the implementation's EGLConfig 155 EGLint configId; // our CONFIG_ID 156 EGLint implConfigId; // the implementation's CONFIG_ID 157 inline bool operator < (const egl_config_t& rhs) const { 158 if (impl < rhs.impl) return true; 159 if (impl > rhs.impl) return false; 160 return config < rhs.config; 161 } 162}; 163 164struct egl_display_t { 165 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED }; 166 167 struct strings_t { 168 char const * vendor; 169 char const * version; 170 char const * clientApi; 171 char const * extensions; 172 }; 173 174 struct DisplayImpl { 175 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0), 176 state(NOT_INITIALIZED), numConfigs(0) { } 177 EGLDisplay dpy; 178 EGLConfig* config; 179 EGLint state; 180 EGLint numConfigs; 181 strings_t queryString; 182 }; 183 184 uint32_t magic; 185 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS]; 186 EGLint numTotalConfigs; 187 egl_config_t* configs; 188 uint32_t refs; 189 Mutex lock; 190 191 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { } 192 ~egl_display_t() { magic = 0; } 193 inline bool isValid() const { return magic == '_dpy'; } 194 inline bool isAlive() const { return isValid(); } 195}; 196 197struct egl_surface_t : public egl_object_t 198{ 199 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref; 200 201 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, 202 EGLSurface surface, int impl, egl_connection_t const* cnx) 203 : dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) { 204 } 205 ~egl_surface_t() { 206 } 207 EGLDisplay dpy; 208 EGLSurface surface; 209 EGLConfig config; 210 sp<ANativeWindow> win; 211 int impl; 212 egl_connection_t const* cnx; 213}; 214 215struct egl_context_t : public egl_object_t 216{ 217 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; 218 219 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, 220 int impl, egl_connection_t const* cnx, int version) 221 : dpy(dpy), context(context), config(config), read(0), draw(0), impl(impl), 222 cnx(cnx), version(version) 223 { 224 } 225 EGLDisplay dpy; 226 EGLContext context; 227 EGLConfig config; 228 EGLSurface read; 229 EGLSurface draw; 230 int impl; 231 egl_connection_t const* cnx; 232 int version; 233}; 234 235struct egl_image_t : public egl_object_t 236{ 237 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref; 238 239 egl_image_t(EGLDisplay dpy, EGLContext context) 240 : dpy(dpy), context(context) 241 { 242 memset(images, 0, sizeof(images)); 243 } 244 EGLDisplay dpy; 245 EGLContext context; 246 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS]; 247}; 248 249typedef egl_surface_t::Ref SurfaceRef; 250typedef egl_context_t::Ref ContextRef; 251typedef egl_image_t::Ref ImageRef; 252 253struct tls_t 254{ 255 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { } 256 EGLint error; 257 EGLContext ctx; 258 EGLBoolean logCallWithNoContext; 259}; 260 261 262// ---------------------------------------------------------------------------- 263 264static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS]; 265static egl_display_t gDisplay[NUM_DISPLAYS]; 266static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER; 267static pthread_key_t gEGLThreadLocalStorageKey = -1; 268 269// ---------------------------------------------------------------------------- 270 271EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS]; 272EGLAPI gl_hooks_t gHooksNoContext; 273EGLAPI pthread_key_t gGLWrapperKey = -1; 274 275// ---------------------------------------------------------------------------- 276 277static __attribute__((noinline)) 278const char *egl_strerror(EGLint err) 279{ 280 switch (err){ 281 case EGL_SUCCESS: return "EGL_SUCCESS"; 282 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; 283 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; 284 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; 285 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; 286 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; 287 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; 288 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; 289 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; 290 case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; 291 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; 292 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; 293 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; 294 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; 295 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; 296 default: return "UNKNOWN"; 297 } 298} 299 300static __attribute__((noinline)) 301void clearTLS() { 302 if (gEGLThreadLocalStorageKey != -1) { 303 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); 304 if (tls) { 305 delete tls; 306 pthread_setspecific(gEGLThreadLocalStorageKey, 0); 307 } 308 } 309} 310 311static tls_t* getTLS() 312{ 313 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); 314 if (tls == 0) { 315 tls = new tls_t; 316 pthread_setspecific(gEGLThreadLocalStorageKey, tls); 317 } 318 return tls; 319} 320 321template<typename T> 322static __attribute__((noinline)) 323T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) { 324 if (gEGLThreadLocalStorageKey == -1) { 325 pthread_mutex_lock(&gThreadLocalStorageKeyMutex); 326 if (gEGLThreadLocalStorageKey == -1) 327 pthread_key_create(&gEGLThreadLocalStorageKey, NULL); 328 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex); 329 } 330 tls_t* tls = getTLS(); 331 if (tls->error != error) { 332 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error)); 333 tls->error = error; 334 } 335 return returnValue; 336} 337 338static __attribute__((noinline)) 339GLint getError() { 340 if (gEGLThreadLocalStorageKey == -1) 341 return EGL_SUCCESS; 342 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); 343 if (!tls) return EGL_SUCCESS; 344 GLint error = tls->error; 345 tls->error = EGL_SUCCESS; 346 return error; 347} 348 349static __attribute__((noinline)) 350void setContext(EGLContext ctx) { 351 if (gEGLThreadLocalStorageKey == -1) { 352 pthread_mutex_lock(&gThreadLocalStorageKeyMutex); 353 if (gEGLThreadLocalStorageKey == -1) 354 pthread_key_create(&gEGLThreadLocalStorageKey, NULL); 355 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex); 356 } 357 tls_t* tls = getTLS(); 358 tls->ctx = ctx; 359} 360 361static __attribute__((noinline)) 362EGLContext getContext() { 363 if (gEGLThreadLocalStorageKey == -1) 364 return EGL_NO_CONTEXT; 365 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); 366 if (!tls) return EGL_NO_CONTEXT; 367 return tls->ctx; 368} 369 370/*****************************************************************************/ 371 372template<typename T> 373static __attribute__((noinline)) 374int binarySearch( 375 T const sortedArray[], int first, int last, T key) 376{ 377 while (first <= last) { 378 int mid = (first + last) / 2; 379 if (sortedArray[mid] < key) { 380 first = mid + 1; 381 } else if (key < sortedArray[mid]) { 382 last = mid - 1; 383 } else { 384 return mid; 385 } 386 } 387 return -1; 388} 389 390static int cmp_configs(const void* a, const void *b) 391{ 392 const egl_config_t& c0 = *(egl_config_t const *)a; 393 const egl_config_t& c1 = *(egl_config_t const *)b; 394 return c0<c1 ? -1 : (c1<c0 ? 1 : 0); 395} 396 397struct extention_map_t { 398 const char* name; 399 __eglMustCastToProperFunctionPointerType address; 400}; 401 402static const extention_map_t gExtentionMap[] = { 403 { "eglLockSurfaceKHR", 404 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR }, 405 { "eglUnlockSurfaceKHR", 406 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR }, 407 { "eglCreateImageKHR", 408 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, 409 { "eglDestroyImageKHR", 410 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, 411 { "eglSetSwapRectangleANDROID", 412 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID }, 413 { "glEGLImageTargetTexture2DOES", 414 (__eglMustCastToProperFunctionPointerType)NULL }, 415 { "glEGLImageTargetRenderbufferStorageOES", 416 (__eglMustCastToProperFunctionPointerType)NULL }, 417}; 418 419extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS]; 420 421// accesses protected by gInitDriverMutex 422static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap; 423static int gGLExtentionSlot = 0; 424 425static void(*findProcAddress(const char* name, 426 const extention_map_t* map, size_t n))() 427{ 428 for (uint32_t i=0 ; i<n ; i++) { 429 if (!strcmp(name, map[i].name)) { 430 return map[i].address; 431 } 432 } 433 return NULL; 434} 435 436// ---------------------------------------------------------------------------- 437 438static int gl_no_context() { 439 tls_t* tls = getTLS(); 440 if (tls->logCallWithNoContext == EGL_TRUE) { 441 tls->logCallWithNoContext = EGL_FALSE; 442 LOGE("call to OpenGL ES API with no current context " 443 "(logged once per thread)"); 444 } 445 return 0; 446} 447 448static void early_egl_init(void) 449{ 450#if !USE_FAST_TLS_KEY 451 pthread_key_create(&gGLWrapperKey, NULL); 452#endif 453 uint32_t addr = (uint32_t)((void*)gl_no_context); 454 android_memset32( 455 (uint32_t*)(void*)&gHooksNoContext, 456 addr, 457 sizeof(gHooksNoContext)); 458 459 setGlThreadSpecific(&gHooksNoContext); 460} 461 462static pthread_once_t once_control = PTHREAD_ONCE_INIT; 463static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); 464 465 466static inline 467egl_display_t* get_display(EGLDisplay dpy) 468{ 469 uintptr_t index = uintptr_t(dpy)-1U; 470 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index]; 471} 472 473template<typename NATIVE, typename EGL> 474static inline NATIVE* egl_to_native_cast(EGL arg) { 475 return reinterpret_cast<NATIVE*>(arg); 476} 477 478static inline 479egl_surface_t* get_surface(EGLSurface surface) { 480 return egl_to_native_cast<egl_surface_t>(surface); 481} 482 483static inline 484egl_context_t* get_context(EGLContext context) { 485 return egl_to_native_cast<egl_context_t>(context); 486} 487 488static inline 489egl_image_t* get_image(EGLImageKHR image) { 490 return egl_to_native_cast<egl_image_t>(image); 491} 492 493static egl_connection_t* validate_display_config( 494 EGLDisplay dpy, EGLConfig config, 495 egl_display_t const*& dp) 496{ 497 dp = get_display(dpy); 498 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL); 499 500 if (intptr_t(config) >= dp->numTotalConfigs) { 501 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL); 502 } 503 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl]; 504 if (cnx->dso == 0) { 505 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL); 506 } 507 return cnx; 508} 509 510static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx) 511{ 512 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) 513 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 514 if (!get_display(dpy)->isAlive()) 515 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 516 if (!get_context(ctx)->isAlive()) 517 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 518 return EGL_TRUE; 519} 520 521static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface) 522{ 523 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) 524 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 525 if (!get_display(dpy)->isAlive()) 526 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 527 if (!get_surface(surface)->isAlive()) 528 return setError(EGL_BAD_SURFACE, EGL_FALSE); 529 return EGL_TRUE; 530} 531 532EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image) 533{ 534 ImageRef _i(image); 535 if (!_i.get()) return EGL_NO_IMAGE_KHR; 536 537 EGLContext context = getContext(); 538 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR) 539 return EGL_NO_IMAGE_KHR; 540 541 egl_context_t const * const c = get_context(context); 542 if (!c->isAlive()) 543 return EGL_NO_IMAGE_KHR; 544 545 egl_image_t const * const i = get_image(image); 546 return i->images[c->impl]; 547} 548 549// ---------------------------------------------------------------------------- 550 551// this mutex protects: 552// d->disp[] 553// egl_init_drivers_locked() 554// 555static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; 556 557EGLBoolean egl_init_drivers_locked() 558{ 559 if (sEarlyInitState) { 560 // initialized by static ctor. should be set here. 561 return EGL_FALSE; 562 } 563 564 // get our driver loader 565 Loader& loader(Loader::getInstance()); 566 567 // dynamically load all our EGL implementations for all displays 568 // and retrieve the corresponding EGLDisplay 569 // if that fails, don't use this driver. 570 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY 571 egl_connection_t* cnx; 572 egl_display_t* d = &gDisplay[0]; 573 574 cnx = &gEGLImpl[IMPL_SOFTWARE]; 575 if (cnx->dso == 0) { 576 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE]; 577 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE]; 578 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx); 579 if (cnx->dso) { 580 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); 581 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!"); 582 d->disp[IMPL_SOFTWARE].dpy = dpy; 583 if (dpy == EGL_NO_DISPLAY) { 584 loader.close(cnx->dso); 585 cnx->dso = NULL; 586 } 587 } 588 } 589 590 cnx = &gEGLImpl[IMPL_HARDWARE]; 591 if (cnx->dso == 0) { 592 char value[PROPERTY_VALUE_MAX]; 593 property_get("debug.egl.hw", value, "1"); 594 if (atoi(value) != 0) { 595 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE]; 596 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE]; 597 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx); 598 if (cnx->dso) { 599 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); 600 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!"); 601 d->disp[IMPL_HARDWARE].dpy = dpy; 602 if (dpy == EGL_NO_DISPLAY) { 603 loader.close(cnx->dso); 604 cnx->dso = NULL; 605 } 606 } 607 } else { 608 LOGD("3D hardware acceleration is disabled"); 609 } 610 } 611 612 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) { 613 return EGL_FALSE; 614 } 615 616 return EGL_TRUE; 617} 618 619EGLBoolean egl_init_drivers() 620{ 621 EGLBoolean res; 622 pthread_mutex_lock(&gInitDriverMutex); 623 res = egl_init_drivers_locked(); 624 pthread_mutex_unlock(&gInitDriverMutex); 625 return res; 626} 627 628// ---------------------------------------------------------------------------- 629}; // namespace android 630// ---------------------------------------------------------------------------- 631 632using namespace android; 633 634EGLDisplay eglGetDisplay(NativeDisplayType display) 635{ 636 uint32_t index = uint32_t(display); 637 if (index >= NUM_DISPLAYS) { 638 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); 639 } 640 641 if (egl_init_drivers() == EGL_FALSE) { 642 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); 643 } 644 645 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU); 646 return dpy; 647} 648 649// ---------------------------------------------------------------------------- 650// Initialization 651// ---------------------------------------------------------------------------- 652 653EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) 654{ 655 egl_display_t * const dp = get_display(dpy); 656 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 657 658 Mutex::Autolock _l(dp->lock); 659 660 if (dp->refs > 0) { 661 if (major != NULL) *major = VERSION_MAJOR; 662 if (minor != NULL) *minor = VERSION_MINOR; 663 dp->refs++; 664 return EGL_TRUE; 665 } 666 667 setGlThreadSpecific(&gHooksNoContext); 668 669 // initialize each EGL and 670 // build our own extension string first, based on the extension we know 671 // and the extension supported by our client implementation 672 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 673 egl_connection_t* const cnx = &gEGLImpl[i]; 674 cnx->major = -1; 675 cnx->minor = -1; 676 if (!cnx->dso) 677 continue; 678 679#if defined(ADRENO130) 680#warning "Adreno-130 eglInitialize() workaround" 681 /* 682 * The ADRENO 130 driver returns a different EGLDisplay each time 683 * eglGetDisplay() is called, but also makes the EGLDisplay invalid 684 * after eglTerminate() has been called, so that eglInitialize() 685 * cannot be called again. Therefore, we need to make sure to call 686 * eglGetDisplay() before calling eglInitialize(); 687 */ 688 if (i == IMPL_HARDWARE) { 689 dp->disp[i].dpy = 690 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); 691 } 692#endif 693 694 695 EGLDisplay idpy = dp->disp[i].dpy; 696 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { 697 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p", 698 // i, idpy, cnx->major, cnx->minor, cnx); 699 700 // display is now initialized 701 dp->disp[i].state = egl_display_t::INITIALIZED; 702 703 // get the query-strings for this display for each implementation 704 dp->disp[i].queryString.vendor = 705 cnx->egl.eglQueryString(idpy, EGL_VENDOR); 706 dp->disp[i].queryString.version = 707 cnx->egl.eglQueryString(idpy, EGL_VERSION); 708 dp->disp[i].queryString.extensions = 709 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS); 710 dp->disp[i].queryString.clientApi = 711 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS); 712 713 } else { 714 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy, 715 egl_strerror(cnx->egl.eglGetError())); 716 } 717 } 718 719 EGLBoolean res = EGL_FALSE; 720 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 721 egl_connection_t* const cnx = &gEGLImpl[i]; 722 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) { 723 EGLint n; 724 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) { 725 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n); 726 if (dp->disp[i].config) { 727 if (cnx->egl.eglGetConfigs( 728 dp->disp[i].dpy, dp->disp[i].config, n, 729 &dp->disp[i].numConfigs)) 730 { 731 dp->numTotalConfigs += n; 732 res = EGL_TRUE; 733 } 734 } 735 } 736 } 737 } 738 739 if (res == EGL_TRUE) { 740 dp->configs = new egl_config_t[ dp->numTotalConfigs ]; 741 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 742 egl_connection_t* const cnx = &gEGLImpl[i]; 743 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) { 744 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) { 745 dp->configs[k].impl = i; 746 dp->configs[k].config = dp->disp[i].config[j]; 747 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1 748 // store the implementation's CONFIG_ID 749 cnx->egl.eglGetConfigAttrib( 750 dp->disp[i].dpy, 751 dp->disp[i].config[j], 752 EGL_CONFIG_ID, 753 &dp->configs[k].implConfigId); 754 k++; 755 } 756 } 757 } 758 759 // sort our configurations so we can do binary-searches 760 qsort( dp->configs, 761 dp->numTotalConfigs, 762 sizeof(egl_config_t), cmp_configs); 763 764 dp->refs++; 765 if (major != NULL) *major = VERSION_MAJOR; 766 if (minor != NULL) *minor = VERSION_MINOR; 767 return EGL_TRUE; 768 } 769 return setError(EGL_NOT_INITIALIZED, EGL_FALSE); 770} 771 772EGLBoolean eglTerminate(EGLDisplay dpy) 773{ 774 // NOTE: don't unload the drivers b/c some APIs can be called 775 // after eglTerminate() has been called. eglTerminate() only 776 // terminates an EGLDisplay, not a EGL itself. 777 778 egl_display_t* const dp = get_display(dpy); 779 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 780 781 Mutex::Autolock _l(dp->lock); 782 783 if (dp->refs == 0) { 784 return setError(EGL_NOT_INITIALIZED, EGL_FALSE); 785 } 786 787 // this is specific to Android, display termination is ref-counted. 788 if (dp->refs > 1) { 789 dp->refs--; 790 return EGL_TRUE; 791 } 792 793 EGLBoolean res = EGL_FALSE; 794 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 795 egl_connection_t* const cnx = &gEGLImpl[i]; 796 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) { 797 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) { 798 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy, 799 egl_strerror(cnx->egl.eglGetError())); 800 } 801 // REVISIT: it's unclear what to do if eglTerminate() fails 802 free(dp->disp[i].config); 803 804 dp->disp[i].numConfigs = 0; 805 dp->disp[i].config = 0; 806 dp->disp[i].state = egl_display_t::TERMINATED; 807 808 res = EGL_TRUE; 809 } 810 } 811 812 // TODO: all egl_object_t should be marked for termination 813 814 dp->refs--; 815 dp->numTotalConfigs = 0; 816 delete [] dp->configs; 817 clearTLS(); 818 return res; 819} 820 821// ---------------------------------------------------------------------------- 822// configuration 823// ---------------------------------------------------------------------------- 824 825EGLBoolean eglGetConfigs( EGLDisplay dpy, 826 EGLConfig *configs, 827 EGLint config_size, EGLint *num_config) 828{ 829 egl_display_t const * const dp = get_display(dpy); 830 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 831 832 GLint numConfigs = dp->numTotalConfigs; 833 if (!configs) { 834 *num_config = numConfigs; 835 return EGL_TRUE; 836 } 837 838 GLint n = 0; 839 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) { 840 *configs++ = EGLConfig(i); 841 config_size--; 842 n++; 843 } 844 845 *num_config = n; 846 return EGL_TRUE; 847} 848 849EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, 850 EGLConfig *configs, EGLint config_size, 851 EGLint *num_config) 852{ 853 egl_display_t const * const dp = get_display(dpy); 854 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 855 856 if (num_config==0) { 857 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 858 } 859 860 EGLint n; 861 EGLBoolean res = EGL_FALSE; 862 *num_config = 0; 863 864 865 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs, 866 // to do this, we have to go through the attrib_list array once 867 // to figure out both its size and if it contains an EGL_CONFIG_ID 868 // key. If so, the full array is copied and patched. 869 // NOTE: we assume that there can be only one occurrence 870 // of EGL_CONFIG_ID. 871 872 EGLint patch_index = -1; 873 GLint attr; 874 size_t size = 0; 875 if (attrib_list) { 876 while ((attr=attrib_list[size]) != EGL_NONE) { 877 if (attr == EGL_CONFIG_ID) 878 patch_index = size; 879 size += 2; 880 } 881 } 882 if (patch_index >= 0) { 883 size += 2; // we need copy the sentinel as well 884 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint)); 885 if (new_list == 0) 886 return setError(EGL_BAD_ALLOC, EGL_FALSE); 887 memcpy(new_list, attrib_list, size*sizeof(EGLint)); 888 889 // patch the requested EGL_CONFIG_ID 890 bool found = false; 891 EGLConfig ourConfig(0); 892 EGLint& configId(new_list[patch_index+1]); 893 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) { 894 if (dp->configs[i].configId == configId) { 895 ourConfig = EGLConfig(i); 896 configId = dp->configs[i].implConfigId; 897 found = true; 898 break; 899 } 900 } 901 902 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl]; 903 if (found && cnx->dso) { 904 // and switch to the new list 905 attrib_list = const_cast<const EGLint *>(new_list); 906 907 // At this point, the only configuration that can match is 908 // dp->configs[i][index], however, we don't know if it would be 909 // rejected because of the other attributes, so we do have to call 910 // cnx->egl.eglChooseConfig() -- but we don't have to loop 911 // through all the EGLimpl[]. 912 // We also know we can only get a single config back, and we know 913 // which one. 914 915 res = cnx->egl.eglChooseConfig( 916 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy, 917 attrib_list, configs, config_size, &n); 918 if (res && n>0) { 919 // n has to be 0 or 1, by construction, and we already know 920 // which config it will return (since there can be only one). 921 if (configs) { 922 configs[0] = ourConfig; 923 } 924 *num_config = 1; 925 } 926 } 927 928 free(const_cast<EGLint *>(attrib_list)); 929 return res; 930 } 931 932 933 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 934 egl_connection_t* const cnx = &gEGLImpl[i]; 935 if (cnx->dso) { 936 if (cnx->egl.eglChooseConfig( 937 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) { 938 if (configs) { 939 // now we need to convert these client EGLConfig to our 940 // internal EGLConfig format. 941 // This is done in O(n Log(n)) time. 942 for (int j=0 ; j<n ; j++) { 943 egl_config_t key(i, configs[j]); 944 intptr_t index = binarySearch<egl_config_t>( 945 dp->configs, 0, dp->numTotalConfigs, key); 946 if (index >= 0) { 947 configs[j] = EGLConfig(index); 948 } else { 949 return setError(EGL_BAD_CONFIG, EGL_FALSE); 950 } 951 } 952 configs += n; 953 config_size -= n; 954 } 955 *num_config += n; 956 res = EGL_TRUE; 957 } 958 } 959 } 960 return res; 961} 962 963EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, 964 EGLint attribute, EGLint *value) 965{ 966 egl_display_t const* dp = 0; 967 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 968 if (!cnx) return EGL_FALSE; 969 970 if (attribute == EGL_CONFIG_ID) { 971 *value = dp->configs[intptr_t(config)].configId; 972 return EGL_TRUE; 973 } 974 return cnx->egl.eglGetConfigAttrib( 975 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, 976 dp->configs[intptr_t(config)].config, attribute, value); 977} 978 979// ---------------------------------------------------------------------------- 980// surfaces 981// ---------------------------------------------------------------------------- 982 983EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, 984 NativeWindowType window, 985 const EGLint *attrib_list) 986{ 987 egl_display_t const* dp = 0; 988 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 989 if (cnx) { 990 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy; 991 EGLConfig iConfig = dp->configs[intptr_t(config)].config; 992 EGLint format; 993 994 // set the native window's buffers format to match this config 995 if (cnx->egl.eglGetConfigAttrib(iDpy, 996 iConfig, EGL_NATIVE_VISUAL_ID, &format)) { 997 if (format != 0) { 998 native_window_set_buffers_geometry(window, 0, 0, format); 999 } 1000 } 1001 1002 EGLSurface surface = cnx->egl.eglCreateWindowSurface( 1003 iDpy, iConfig, window, attrib_list); 1004 if (surface != EGL_NO_SURFACE) { 1005 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface, 1006 dp->configs[intptr_t(config)].impl, cnx); 1007 return s; 1008 } 1009 } 1010 return EGL_NO_SURFACE; 1011} 1012 1013EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, 1014 NativePixmapType pixmap, 1015 const EGLint *attrib_list) 1016{ 1017 egl_display_t const* dp = 0; 1018 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 1019 if (cnx) { 1020 EGLSurface surface = cnx->egl.eglCreatePixmapSurface( 1021 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, 1022 dp->configs[intptr_t(config)].config, pixmap, attrib_list); 1023 if (surface != EGL_NO_SURFACE) { 1024 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, 1025 dp->configs[intptr_t(config)].impl, cnx); 1026 return s; 1027 } 1028 } 1029 return EGL_NO_SURFACE; 1030} 1031 1032EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, 1033 const EGLint *attrib_list) 1034{ 1035 egl_display_t const* dp = 0; 1036 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 1037 if (cnx) { 1038 EGLSurface surface = cnx->egl.eglCreatePbufferSurface( 1039 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, 1040 dp->configs[intptr_t(config)].config, attrib_list); 1041 if (surface != EGL_NO_SURFACE) { 1042 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, 1043 dp->configs[intptr_t(config)].impl, cnx); 1044 return s; 1045 } 1046 } 1047 return EGL_NO_SURFACE; 1048} 1049 1050EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) 1051{ 1052 SurfaceRef _s(surface); 1053 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1054 1055 if (!validate_display_surface(dpy, surface)) 1056 return EGL_FALSE; 1057 egl_display_t const * const dp = get_display(dpy); 1058 1059 egl_surface_t * const s = get_surface(surface); 1060 EGLBoolean result = s->cnx->egl.eglDestroySurface( 1061 dp->disp[s->impl].dpy, s->surface); 1062 if (result == EGL_TRUE) { 1063 if (s->win != NULL) { 1064 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0); 1065 } 1066 _s.terminate(); 1067 } 1068 return result; 1069} 1070 1071EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface, 1072 EGLint attribute, EGLint *value) 1073{ 1074 SurfaceRef _s(surface); 1075 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1076 1077 if (!validate_display_surface(dpy, surface)) 1078 return EGL_FALSE; 1079 egl_display_t const * const dp = get_display(dpy); 1080 egl_surface_t const * const s = get_surface(surface); 1081 1082 EGLBoolean result(EGL_TRUE); 1083 if (attribute == EGL_CONFIG_ID) { 1084 // We need to remap EGL_CONFIG_IDs 1085 *value = dp->configs[intptr_t(s->config)].configId; 1086 } else { 1087 result = s->cnx->egl.eglQuerySurface( 1088 dp->disp[s->impl].dpy, s->surface, attribute, value); 1089 } 1090 1091 return result; 1092} 1093 1094// ---------------------------------------------------------------------------- 1095// Contexts 1096// ---------------------------------------------------------------------------- 1097 1098EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, 1099 EGLContext share_list, const EGLint *attrib_list) 1100{ 1101 egl_display_t const* dp = 0; 1102 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 1103 if (cnx) { 1104 if (share_list != EGL_NO_CONTEXT) { 1105 egl_context_t* const c = get_context(share_list); 1106 share_list = c->context; 1107 } 1108 EGLContext context = cnx->egl.eglCreateContext( 1109 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, 1110 dp->configs[intptr_t(config)].config, 1111 share_list, attrib_list); 1112 if (context != EGL_NO_CONTEXT) { 1113 // figure out if it's a GLESv1 or GLESv2 1114 int version = 0; 1115 if (attrib_list) { 1116 while (*attrib_list != EGL_NONE) { 1117 GLint attr = *attrib_list++; 1118 GLint value = *attrib_list++; 1119 if (attr == EGL_CONTEXT_CLIENT_VERSION) { 1120 if (value == 1) { 1121 version = GLESv1_INDEX; 1122 } else if (value == 2) { 1123 version = GLESv2_INDEX; 1124 } 1125 } 1126 }; 1127 } 1128 egl_context_t* c = new egl_context_t(dpy, context, config, 1129 dp->configs[intptr_t(config)].impl, cnx, version); 1130 return c; 1131 } 1132 } 1133 return EGL_NO_CONTEXT; 1134} 1135 1136EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) 1137{ 1138 ContextRef _c(ctx); 1139 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1140 1141 if (!validate_display_context(dpy, ctx)) 1142 return EGL_FALSE; 1143 egl_display_t const * const dp = get_display(dpy); 1144 egl_context_t * const c = get_context(ctx); 1145 EGLBoolean result = c->cnx->egl.eglDestroyContext( 1146 dp->disp[c->impl].dpy, c->context); 1147 if (result == EGL_TRUE) { 1148 _c.terminate(); 1149 } 1150 return result; 1151} 1152 1153EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, 1154 EGLSurface read, EGLContext ctx) 1155{ 1156 // get a reference to the object passed in 1157 ContextRef _c(ctx); 1158 SurfaceRef _d(draw); 1159 SurfaceRef _r(read); 1160 1161 // validate the display and the context (if not EGL_NO_CONTEXT) 1162 egl_display_t const * const dp = get_display(dpy); 1163 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1164 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) { 1165 // EGL_NO_CONTEXT is valid 1166 return EGL_FALSE; 1167 } 1168 1169 // these are the underlying implementation's object 1170 EGLContext impl_ctx = EGL_NO_CONTEXT; 1171 EGLSurface impl_draw = EGL_NO_SURFACE; 1172 EGLSurface impl_read = EGL_NO_SURFACE; 1173 1174 // these are our objects structs passed in 1175 egl_context_t * c = NULL; 1176 egl_surface_t const * d = NULL; 1177 egl_surface_t const * r = NULL; 1178 1179 // these are the current objects structs 1180 egl_context_t * cur_c = get_context(getContext()); 1181 egl_surface_t * cur_r = NULL; 1182 egl_surface_t * cur_d = NULL; 1183 1184 if (ctx != EGL_NO_CONTEXT) { 1185 c = get_context(ctx); 1186 cur_r = get_surface(c->read); 1187 cur_d = get_surface(c->draw); 1188 impl_ctx = c->context; 1189 } else { 1190 // no context given, use the implementation of the current context 1191 if (cur_c == NULL) { 1192 // no current context 1193 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) { 1194 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT); 1195 return setError(EGL_BAD_MATCH, EGL_FALSE); 1196 } 1197 // not an error, there is just no current context. 1198 return EGL_TRUE; 1199 } 1200 } 1201 1202 // retrieve the underlying implementation's draw EGLSurface 1203 if (draw != EGL_NO_SURFACE) { 1204 d = get_surface(draw); 1205 // make sure the EGLContext and EGLSurface passed in are for 1206 // the same driver 1207 if (c && d->impl != c->impl) 1208 return setError(EGL_BAD_MATCH, EGL_FALSE); 1209 impl_draw = d->surface; 1210 } 1211 1212 // retrieve the underlying implementation's read EGLSurface 1213 if (read != EGL_NO_SURFACE) { 1214 r = get_surface(read); 1215 // make sure the EGLContext and EGLSurface passed in are for 1216 // the same driver 1217 if (c && r->impl != c->impl) 1218 return setError(EGL_BAD_MATCH, EGL_FALSE); 1219 impl_read = r->surface; 1220 } 1221 1222 EGLBoolean result; 1223 1224 if (c) { 1225 result = c->cnx->egl.eglMakeCurrent( 1226 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx); 1227 } else { 1228 result = cur_c->cnx->egl.eglMakeCurrent( 1229 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx); 1230 } 1231 1232 if (result == EGL_TRUE) { 1233 // by construction, these are either 0 or valid (possibly terminated) 1234 // it should be impossible for these to be invalid 1235 ContextRef _cur_c(cur_c); 1236 SurfaceRef _cur_r(cur_r); 1237 SurfaceRef _cur_d(cur_d); 1238 1239 // cur_c has to be valid here (but could be terminated) 1240 if (ctx != EGL_NO_CONTEXT) { 1241 setGlThreadSpecific(c->cnx->hooks[c->version]); 1242 setContext(ctx); 1243 _c.acquire(); 1244 } else { 1245 setGlThreadSpecific(&gHooksNoContext); 1246 setContext(EGL_NO_CONTEXT); 1247 } 1248 _cur_c.release(); 1249 1250 _r.acquire(); 1251 _cur_r.release(); 1252 if (c) c->read = read; 1253 1254 _d.acquire(); 1255 _cur_d.release(); 1256 if (c) c->draw = draw; 1257 } 1258 return result; 1259} 1260 1261 1262EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, 1263 EGLint attribute, EGLint *value) 1264{ 1265 ContextRef _c(ctx); 1266 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1267 1268 if (!validate_display_context(dpy, ctx)) 1269 return EGL_FALSE; 1270 1271 egl_display_t const * const dp = get_display(dpy); 1272 egl_context_t * const c = get_context(ctx); 1273 1274 EGLBoolean result(EGL_TRUE); 1275 if (attribute == EGL_CONFIG_ID) { 1276 *value = dp->configs[intptr_t(c->config)].configId; 1277 } else { 1278 // We need to remap EGL_CONFIG_IDs 1279 result = c->cnx->egl.eglQueryContext( 1280 dp->disp[c->impl].dpy, c->context, attribute, value); 1281 } 1282 1283 return result; 1284} 1285 1286EGLContext eglGetCurrentContext(void) 1287{ 1288 // could be called before eglInitialize(), but we wouldn't have a context 1289 // then, and this function would correctly return EGL_NO_CONTEXT. 1290 1291 EGLContext ctx = getContext(); 1292 return ctx; 1293} 1294 1295EGLSurface eglGetCurrentSurface(EGLint readdraw) 1296{ 1297 // could be called before eglInitialize(), but we wouldn't have a context 1298 // then, and this function would correctly return EGL_NO_SURFACE. 1299 1300 EGLContext ctx = getContext(); 1301 if (ctx) { 1302 egl_context_t const * const c = get_context(ctx); 1303 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); 1304 switch (readdraw) { 1305 case EGL_READ: return c->read; 1306 case EGL_DRAW: return c->draw; 1307 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); 1308 } 1309 } 1310 return EGL_NO_SURFACE; 1311} 1312 1313EGLDisplay eglGetCurrentDisplay(void) 1314{ 1315 // could be called before eglInitialize(), but we wouldn't have a context 1316 // then, and this function would correctly return EGL_NO_DISPLAY. 1317 1318 EGLContext ctx = getContext(); 1319 if (ctx) { 1320 egl_context_t const * const c = get_context(ctx); 1321 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); 1322 return c->dpy; 1323 } 1324 return EGL_NO_DISPLAY; 1325} 1326 1327EGLBoolean eglWaitGL(void) 1328{ 1329 // could be called before eglInitialize(), but we wouldn't have a context 1330 // then, and this function would return GL_TRUE, which isn't wrong. 1331 1332 EGLBoolean res = EGL_TRUE; 1333 EGLContext ctx = getContext(); 1334 if (ctx) { 1335 egl_context_t const * const c = get_context(ctx); 1336 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1337 if (uint32_t(c->impl)>=2) 1338 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1339 egl_connection_t* const cnx = &gEGLImpl[c->impl]; 1340 if (!cnx->dso) 1341 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1342 res = cnx->egl.eglWaitGL(); 1343 } 1344 return res; 1345} 1346 1347EGLBoolean eglWaitNative(EGLint engine) 1348{ 1349 // could be called before eglInitialize(), but we wouldn't have a context 1350 // then, and this function would return GL_TRUE, which isn't wrong. 1351 1352 EGLBoolean res = EGL_TRUE; 1353 EGLContext ctx = getContext(); 1354 if (ctx) { 1355 egl_context_t const * const c = get_context(ctx); 1356 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1357 if (uint32_t(c->impl)>=2) 1358 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1359 egl_connection_t* const cnx = &gEGLImpl[c->impl]; 1360 if (!cnx->dso) 1361 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1362 res = cnx->egl.eglWaitNative(engine); 1363 } 1364 return res; 1365} 1366 1367EGLint eglGetError(void) 1368{ 1369 EGLint result = EGL_SUCCESS; 1370 EGLint err; 1371 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1372 err = EGL_SUCCESS; 1373 egl_connection_t* const cnx = &gEGLImpl[i]; 1374 if (cnx->dso) 1375 err = cnx->egl.eglGetError(); 1376 if (err!=EGL_SUCCESS && result==EGL_SUCCESS) 1377 result = err; 1378 } 1379 err = getError(); 1380 if (result == EGL_SUCCESS) 1381 result = err; 1382 return result; 1383} 1384 1385__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname) 1386{ 1387 // eglGetProcAddress() could be the very first function called 1388 // in which case we must make sure we've initialized ourselves, this 1389 // happens the first time egl_get_display() is called. 1390 1391 if (egl_init_drivers() == EGL_FALSE) { 1392 setError(EGL_BAD_PARAMETER, NULL); 1393 return NULL; 1394 } 1395 1396 __eglMustCastToProperFunctionPointerType addr; 1397 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap)); 1398 if (addr) return addr; 1399 1400 // this protects accesses to gGLExtentionMap and gGLExtentionSlot 1401 pthread_mutex_lock(&gInitDriverMutex); 1402 1403 /* 1404 * Since eglGetProcAddress() is not associated to anything, it needs 1405 * to return a function pointer that "works" regardless of what 1406 * the current context is. 1407 * 1408 * For this reason, we return a "forwarder", a small stub that takes 1409 * care of calling the function associated with the context 1410 * currently bound. 1411 * 1412 * We first look for extensions we've already resolved, if we're seeing 1413 * this extension for the first time, we go through all our 1414 * implementations and call eglGetProcAddress() and record the 1415 * result in the appropriate implementation hooks and return the 1416 * address of the forwarder corresponding to that hook set. 1417 * 1418 */ 1419 1420 const String8 name(procname); 1421 addr = gGLExtentionMap.valueFor(name); 1422 const int slot = gGLExtentionSlot; 1423 1424 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS, 1425 "no more slots for eglGetProcAddress(\"%s\")", 1426 procname); 1427 1428 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) { 1429 bool found = false; 1430 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1431 egl_connection_t* const cnx = &gEGLImpl[i]; 1432 if (cnx->dso && cnx->egl.eglGetProcAddress) { 1433 found = true; 1434 // Extensions are independent of the bound context 1435 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] = 1436 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] = 1437 cnx->egl.eglGetProcAddress(procname); 1438 } 1439 } 1440 if (found) { 1441 addr = gExtensionForwarders[slot]; 1442 gGLExtentionMap.add(name, addr); 1443 gGLExtentionSlot++; 1444 } 1445 } 1446 1447 pthread_mutex_unlock(&gInitDriverMutex); 1448 return addr; 1449} 1450 1451EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) 1452{ 1453 SurfaceRef _s(draw); 1454 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1455 1456 if (!validate_display_surface(dpy, draw)) 1457 return EGL_FALSE; 1458 egl_display_t const * const dp = get_display(dpy); 1459 egl_surface_t const * const s = get_surface(draw); 1460 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface); 1461} 1462 1463EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, 1464 NativePixmapType target) 1465{ 1466 SurfaceRef _s(surface); 1467 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1468 1469 if (!validate_display_surface(dpy, surface)) 1470 return EGL_FALSE; 1471 egl_display_t const * const dp = get_display(dpy); 1472 egl_surface_t const * const s = get_surface(surface); 1473 return s->cnx->egl.eglCopyBuffers( 1474 dp->disp[s->impl].dpy, s->surface, target); 1475} 1476 1477const char* eglQueryString(EGLDisplay dpy, EGLint name) 1478{ 1479 egl_display_t const * const dp = get_display(dpy); 1480 switch (name) { 1481 case EGL_VENDOR: 1482 return gVendorString; 1483 case EGL_VERSION: 1484 return gVersionString; 1485 case EGL_EXTENSIONS: 1486 return gExtensionString; 1487 case EGL_CLIENT_APIS: 1488 return gClientApiString; 1489 } 1490 return setError(EGL_BAD_PARAMETER, (const char *)0); 1491} 1492 1493 1494// ---------------------------------------------------------------------------- 1495// EGL 1.1 1496// ---------------------------------------------------------------------------- 1497 1498EGLBoolean eglSurfaceAttrib( 1499 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) 1500{ 1501 SurfaceRef _s(surface); 1502 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1503 1504 if (!validate_display_surface(dpy, surface)) 1505 return EGL_FALSE; 1506 egl_display_t const * const dp = get_display(dpy); 1507 egl_surface_t const * const s = get_surface(surface); 1508 if (s->cnx->egl.eglSurfaceAttrib) { 1509 return s->cnx->egl.eglSurfaceAttrib( 1510 dp->disp[s->impl].dpy, s->surface, attribute, value); 1511 } 1512 return setError(EGL_BAD_SURFACE, EGL_FALSE); 1513} 1514 1515EGLBoolean eglBindTexImage( 1516 EGLDisplay dpy, EGLSurface surface, EGLint buffer) 1517{ 1518 SurfaceRef _s(surface); 1519 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1520 1521 if (!validate_display_surface(dpy, surface)) 1522 return EGL_FALSE; 1523 egl_display_t const * const dp = get_display(dpy); 1524 egl_surface_t const * const s = get_surface(surface); 1525 if (s->cnx->egl.eglBindTexImage) { 1526 return s->cnx->egl.eglBindTexImage( 1527 dp->disp[s->impl].dpy, s->surface, buffer); 1528 } 1529 return setError(EGL_BAD_SURFACE, EGL_FALSE); 1530} 1531 1532EGLBoolean eglReleaseTexImage( 1533 EGLDisplay dpy, EGLSurface surface, EGLint buffer) 1534{ 1535 SurfaceRef _s(surface); 1536 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1537 1538 if (!validate_display_surface(dpy, surface)) 1539 return EGL_FALSE; 1540 egl_display_t const * const dp = get_display(dpy); 1541 egl_surface_t const * const s = get_surface(surface); 1542 if (s->cnx->egl.eglReleaseTexImage) { 1543 return s->cnx->egl.eglReleaseTexImage( 1544 dp->disp[s->impl].dpy, s->surface, buffer); 1545 } 1546 return setError(EGL_BAD_SURFACE, EGL_FALSE); 1547} 1548 1549EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) 1550{ 1551 egl_display_t * const dp = get_display(dpy); 1552 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1553 1554 EGLBoolean res = EGL_TRUE; 1555 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1556 egl_connection_t* const cnx = &gEGLImpl[i]; 1557 if (cnx->dso) { 1558 if (cnx->egl.eglSwapInterval) { 1559 if (cnx->egl.eglSwapInterval( 1560 dp->disp[i].dpy, interval) == EGL_FALSE) { 1561 res = EGL_FALSE; 1562 } 1563 } 1564 } 1565 } 1566 return res; 1567} 1568 1569 1570// ---------------------------------------------------------------------------- 1571// EGL 1.2 1572// ---------------------------------------------------------------------------- 1573 1574EGLBoolean eglWaitClient(void) 1575{ 1576 // could be called before eglInitialize(), but we wouldn't have a context 1577 // then, and this function would return GL_TRUE, which isn't wrong. 1578 EGLBoolean res = EGL_TRUE; 1579 EGLContext ctx = getContext(); 1580 if (ctx) { 1581 egl_context_t const * const c = get_context(ctx); 1582 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1583 if (uint32_t(c->impl)>=2) 1584 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1585 egl_connection_t* const cnx = &gEGLImpl[c->impl]; 1586 if (!cnx->dso) 1587 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 1588 if (cnx->egl.eglWaitClient) { 1589 res = cnx->egl.eglWaitClient(); 1590 } else { 1591 res = cnx->egl.eglWaitGL(); 1592 } 1593 } 1594 return res; 1595} 1596 1597EGLBoolean eglBindAPI(EGLenum api) 1598{ 1599 if (egl_init_drivers() == EGL_FALSE) { 1600 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 1601 } 1602 1603 // bind this API on all EGLs 1604 EGLBoolean res = EGL_TRUE; 1605 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1606 egl_connection_t* const cnx = &gEGLImpl[i]; 1607 if (cnx->dso) { 1608 if (cnx->egl.eglBindAPI) { 1609 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) { 1610 res = EGL_FALSE; 1611 } 1612 } 1613 } 1614 } 1615 return res; 1616} 1617 1618EGLenum eglQueryAPI(void) 1619{ 1620 if (egl_init_drivers() == EGL_FALSE) { 1621 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 1622 } 1623 1624 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1625 egl_connection_t* const cnx = &gEGLImpl[i]; 1626 if (cnx->dso) { 1627 if (cnx->egl.eglQueryAPI) { 1628 // the first one we find is okay, because they all 1629 // should be the same 1630 return cnx->egl.eglQueryAPI(); 1631 } 1632 } 1633 } 1634 // or, it can only be OpenGL ES 1635 return EGL_OPENGL_ES_API; 1636} 1637 1638EGLBoolean eglReleaseThread(void) 1639{ 1640 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1641 egl_connection_t* const cnx = &gEGLImpl[i]; 1642 if (cnx->dso) { 1643 if (cnx->egl.eglReleaseThread) { 1644 cnx->egl.eglReleaseThread(); 1645 } 1646 } 1647 } 1648 clearTLS(); 1649 return EGL_TRUE; 1650} 1651 1652EGLSurface eglCreatePbufferFromClientBuffer( 1653 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, 1654 EGLConfig config, const EGLint *attrib_list) 1655{ 1656 egl_display_t const* dp = 0; 1657 egl_connection_t* cnx = validate_display_config(dpy, config, dp); 1658 if (!cnx) return EGL_FALSE; 1659 if (cnx->egl.eglCreatePbufferFromClientBuffer) { 1660 return cnx->egl.eglCreatePbufferFromClientBuffer( 1661 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, 1662 buftype, buffer, 1663 dp->configs[intptr_t(config)].config, attrib_list); 1664 } 1665 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE); 1666} 1667 1668// ---------------------------------------------------------------------------- 1669// EGL_EGLEXT_VERSION 3 1670// ---------------------------------------------------------------------------- 1671 1672EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, 1673 const EGLint *attrib_list) 1674{ 1675 SurfaceRef _s(surface); 1676 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1677 1678 if (!validate_display_surface(dpy, surface)) 1679 return EGL_FALSE; 1680 1681 egl_display_t const * const dp = get_display(dpy); 1682 egl_surface_t const * const s = get_surface(surface); 1683 1684 if (s->cnx->egl.eglLockSurfaceKHR) { 1685 return s->cnx->egl.eglLockSurfaceKHR( 1686 dp->disp[s->impl].dpy, s->surface, attrib_list); 1687 } 1688 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1689} 1690 1691EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) 1692{ 1693 SurfaceRef _s(surface); 1694 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1695 1696 if (!validate_display_surface(dpy, surface)) 1697 return EGL_FALSE; 1698 1699 egl_display_t const * const dp = get_display(dpy); 1700 egl_surface_t const * const s = get_surface(surface); 1701 1702 if (s->cnx->egl.eglUnlockSurfaceKHR) { 1703 return s->cnx->egl.eglUnlockSurfaceKHR( 1704 dp->disp[s->impl].dpy, s->surface); 1705 } 1706 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1707} 1708 1709EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, 1710 EGLClientBuffer buffer, const EGLint *attrib_list) 1711{ 1712 if (ctx != EGL_NO_CONTEXT) { 1713 ContextRef _c(ctx); 1714 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR); 1715 if (!validate_display_context(dpy, ctx)) 1716 return EGL_NO_IMAGE_KHR; 1717 egl_display_t const * const dp = get_display(dpy); 1718 egl_context_t * const c = get_context(ctx); 1719 // since we have an EGLContext, we know which implementation to use 1720 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR( 1721 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list); 1722 if (image == EGL_NO_IMAGE_KHR) 1723 return image; 1724 1725 egl_image_t* result = new egl_image_t(dpy, ctx); 1726 result->images[c->impl] = image; 1727 return (EGLImageKHR)result; 1728 } else { 1729 // EGL_NO_CONTEXT is a valid parameter 1730 egl_display_t const * const dp = get_display(dpy); 1731 if (dp == 0) { 1732 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR); 1733 } 1734 1735 /* Since we don't have a way to know which implementation to call, 1736 * we're calling all of them. If at least one of the implementation 1737 * succeeded, this is a success. 1738 */ 1739 1740 EGLint currentError = eglGetError(); 1741 1742 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS]; 1743 bool success = false; 1744 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1745 egl_connection_t* const cnx = &gEGLImpl[i]; 1746 implImages[i] = EGL_NO_IMAGE_KHR; 1747 if (cnx->dso) { 1748 if (cnx->egl.eglCreateImageKHR) { 1749 implImages[i] = cnx->egl.eglCreateImageKHR( 1750 dp->disp[i].dpy, ctx, target, buffer, attrib_list); 1751 if (implImages[i] != EGL_NO_IMAGE_KHR) { 1752 success = true; 1753 } 1754 } 1755 } 1756 } 1757 1758 if (!success) { 1759 // failure, if there was an error when we entered this function, 1760 // the error flag must not be updated. 1761 // Otherwise, the error is whatever happened in the implementation 1762 // that faulted. 1763 if (currentError != EGL_SUCCESS) { 1764 setError(currentError, EGL_NO_IMAGE_KHR); 1765 } 1766 return EGL_NO_IMAGE_KHR; 1767 } else { 1768 // In case of success, we need to clear all error flags 1769 // (especially those caused by the implementation that didn't 1770 // succeed). TODO: we could avoid this if we knew this was 1771 // a "full" success (all implementation succeeded). 1772 eglGetError(); 1773 } 1774 1775 egl_image_t* result = new egl_image_t(dpy, ctx); 1776 memcpy(result->images, implImages, sizeof(implImages)); 1777 return (EGLImageKHR)result; 1778 } 1779} 1780 1781EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) 1782{ 1783 egl_display_t const * const dp = get_display(dpy); 1784 if (dp == 0) { 1785 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1786 } 1787 1788 ImageRef _i(img); 1789 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE); 1790 1791 egl_image_t* image = get_image(img); 1792 bool success = false; 1793 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { 1794 egl_connection_t* const cnx = &gEGLImpl[i]; 1795 if (image->images[i] != EGL_NO_IMAGE_KHR) { 1796 if (cnx->dso) { 1797 if (cnx->egl.eglDestroyImageKHR) { 1798 if (cnx->egl.eglDestroyImageKHR( 1799 dp->disp[i].dpy, image->images[i])) { 1800 success = true; 1801 } 1802 } 1803 } 1804 } 1805 } 1806 if (!success) 1807 return EGL_FALSE; 1808 1809 _i.terminate(); 1810 1811 return EGL_TRUE; 1812} 1813 1814 1815// ---------------------------------------------------------------------------- 1816// ANDROID extensions 1817// ---------------------------------------------------------------------------- 1818 1819EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw, 1820 EGLint left, EGLint top, EGLint width, EGLint height) 1821{ 1822 SurfaceRef _s(draw); 1823 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); 1824 1825 if (!validate_display_surface(dpy, draw)) 1826 return EGL_FALSE; 1827 egl_display_t const * const dp = get_display(dpy); 1828 egl_surface_t const * const s = get_surface(draw); 1829 if (s->cnx->egl.eglSetSwapRectangleANDROID) { 1830 return s->cnx->egl.eglSetSwapRectangleANDROID( 1831 dp->disp[s->impl].dpy, s->surface, left, top, width, height); 1832 } 1833 return setError(EGL_BAD_DISPLAY, NULL); 1834} 1835