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