eglApi.cpp revision 81b316aa5395b6318ad4a10e1ed27d0b9b0df07f
1/* 2 ** Copyright 2007, The Android Open Source Project 3 ** 4 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** you may not use this file except in compliance with the License. 6 ** You may obtain a copy of the License at 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 10 ** Unless required by applicable law or agreed to in writing, software 11 ** distributed under the License is distributed on an "AS IS" BASIS, 12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ** See the License for the specific language governing permissions and 14 ** limitations under the License. 15 */ 16 17#define ATRACE_TAG ATRACE_TAG_GRAPHICS 18 19#include <ctype.h> 20#include <stdlib.h> 21#include <string.h> 22 23#include <hardware/gralloc.h> 24#include <system/window.h> 25 26#include <EGL/egl.h> 27#include <EGL/eglext.h> 28#include <GLES/gl.h> 29#include <GLES/glext.h> 30 31#include <cutils/log.h> 32#include <cutils/atomic.h> 33#include <cutils/compiler.h> 34#include <cutils/properties.h> 35#include <cutils/memory.h> 36 37#include <utils/KeyedVector.h> 38#include <utils/SortedVector.h> 39#include <utils/String8.h> 40#include <utils/Trace.h> 41 42#include "egl_impl.h" 43#include "egl_tls.h" 44#include "glestrace.h" 45#include "hooks.h" 46 47#include "egl_display.h" 48#include "egl_impl.h" 49#include "egl_object.h" 50#include "egl_tls.h" 51#include "egldefs.h" 52 53using namespace android; 54 55// ---------------------------------------------------------------------------- 56 57#define EGL_VERSION_HW_ANDROID 0x3143 58 59struct extention_map_t { 60 const char* name; 61 __eglMustCastToProperFunctionPointerType address; 62}; 63 64static const extention_map_t sExtentionMap[] = { 65 { "eglLockSurfaceKHR", 66 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR }, 67 { "eglUnlockSurfaceKHR", 68 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR }, 69 { "eglCreateImageKHR", 70 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, 71 { "eglDestroyImageKHR", 72 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, 73 { "eglGetSystemTimeFrequencyNV", 74 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV }, 75 { "eglGetSystemTimeNV", 76 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV }, 77}; 78 79// accesses protected by sExtensionMapMutex 80static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap; 81static int sGLExtentionSlot = 0; 82static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER; 83 84static void(*findProcAddress(const char* name, 85 const extention_map_t* map, size_t n))() { 86 for (uint32_t i=0 ; i<n ; i++) { 87 if (!strcmp(name, map[i].name)) { 88 return map[i].address; 89 } 90 } 91 return NULL; 92} 93 94// ---------------------------------------------------------------------------- 95 96namespace android { 97extern void setGLHooksThreadSpecific(gl_hooks_t const *value); 98extern EGLBoolean egl_init_drivers(); 99extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS]; 100extern int gEGLDebugLevel; 101extern gl_hooks_t gHooksTrace; 102} // namespace android; 103 104// ---------------------------------------------------------------------------- 105 106static inline void clearError() { egl_tls_t::clearError(); } 107static inline EGLContext getContext() { return egl_tls_t::getContext(); } 108 109// ---------------------------------------------------------------------------- 110 111EGLDisplay eglGetDisplay(EGLNativeDisplayType display) 112{ 113 clearError(); 114 115 uint32_t index = uint32_t(display); 116 if (index >= NUM_DISPLAYS) { 117 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); 118 } 119 120 if (egl_init_drivers() == EGL_FALSE) { 121 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); 122 } 123 124 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display); 125 return dpy; 126} 127 128// ---------------------------------------------------------------------------- 129// Initialization 130// ---------------------------------------------------------------------------- 131 132EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) 133{ 134 clearError(); 135 136 egl_display_ptr dp = get_display(dpy); 137 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 138 139 EGLBoolean res = dp->initialize(major, minor); 140 141 return res; 142} 143 144EGLBoolean eglTerminate(EGLDisplay dpy) 145{ 146 // NOTE: don't unload the drivers b/c some APIs can be called 147 // after eglTerminate() has been called. eglTerminate() only 148 // terminates an EGLDisplay, not a EGL itself. 149 150 clearError(); 151 152 egl_display_ptr dp = get_display(dpy); 153 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 154 155 EGLBoolean res = dp->terminate(); 156 157 return res; 158} 159 160// ---------------------------------------------------------------------------- 161// configuration 162// ---------------------------------------------------------------------------- 163 164EGLBoolean eglGetConfigs( EGLDisplay dpy, 165 EGLConfig *configs, 166 EGLint config_size, EGLint *num_config) 167{ 168 clearError(); 169 170 const egl_display_ptr dp = validate_display(dpy); 171 if (!dp) return EGL_FALSE; 172 173 if (num_config==0) { 174 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 175 } 176 177 EGLBoolean res = EGL_FALSE; 178 *num_config = 0; 179 180 egl_connection_t* const cnx = &gEGLImpl; 181 if (cnx->dso) { 182 res = cnx->egl.eglGetConfigs( 183 dp->disp.dpy, configs, config_size, num_config); 184 } 185 186 return res; 187} 188 189EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, 190 EGLConfig *configs, EGLint config_size, 191 EGLint *num_config) 192{ 193 clearError(); 194 195 const egl_display_ptr dp = validate_display(dpy); 196 if (!dp) return EGL_FALSE; 197 198 if (num_config==0) { 199 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 200 } 201 202 EGLBoolean res = EGL_FALSE; 203 *num_config = 0; 204 205 egl_connection_t* const cnx = &gEGLImpl; 206 if (cnx->dso) { 207 res = cnx->egl.eglChooseConfig( 208 dp->disp.dpy, attrib_list, configs, config_size, num_config); 209 } 210 return res; 211} 212 213EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, 214 EGLint attribute, EGLint *value) 215{ 216 clearError(); 217 218 egl_connection_t* cnx = NULL; 219 const egl_display_ptr dp = validate_display_connection(dpy, cnx); 220 if (!dp) return EGL_FALSE; 221 222 return cnx->egl.eglGetConfigAttrib( 223 dp->disp.dpy, config, attribute, value); 224} 225 226// ---------------------------------------------------------------------------- 227// surfaces 228// ---------------------------------------------------------------------------- 229 230EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, 231 NativeWindowType window, 232 const EGLint *attrib_list) 233{ 234 clearError(); 235 236 egl_connection_t* cnx = NULL; 237 egl_display_ptr dp = validate_display_connection(dpy, cnx); 238 if (dp) { 239 EGLDisplay iDpy = dp->disp.dpy; 240 EGLint format; 241 242 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) { 243 ALOGE("EGLNativeWindowType %p already connected to another API", 244 window); 245 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); 246 } 247 248 // set the native window's buffers format to match this config 249 if (cnx->egl.eglGetConfigAttrib(iDpy, 250 config, EGL_NATIVE_VISUAL_ID, &format)) { 251 if (format != 0) { 252 int err = native_window_set_buffers_format(window, format); 253 if (err != 0) { 254 ALOGE("error setting native window pixel format: %s (%d)", 255 strerror(-err), err); 256 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); 257 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); 258 } 259 } 260 } 261 262 // the EGL spec requires that a new EGLSurface default to swap interval 263 // 1, so explicitly set that on the window here. 264 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window); 265 anw->setSwapInterval(anw, 1); 266 267 EGLSurface surface = cnx->egl.eglCreateWindowSurface( 268 iDpy, config, window, attrib_list); 269 if (surface != EGL_NO_SURFACE) { 270 egl_surface_t* s = new egl_surface_t(dp.get(), config, window, 271 surface, cnx); 272 return s; 273 } 274 275 // EGLSurface creation failed 276 native_window_set_buffers_format(window, 0); 277 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); 278 } 279 return EGL_NO_SURFACE; 280} 281 282EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, 283 NativePixmapType pixmap, 284 const EGLint *attrib_list) 285{ 286 clearError(); 287 288 egl_connection_t* cnx = NULL; 289 egl_display_ptr dp = validate_display_connection(dpy, cnx); 290 if (dp) { 291 EGLSurface surface = cnx->egl.eglCreatePixmapSurface( 292 dp->disp.dpy, config, pixmap, attrib_list); 293 if (surface != EGL_NO_SURFACE) { 294 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, 295 surface, cnx); 296 return s; 297 } 298 } 299 return EGL_NO_SURFACE; 300} 301 302EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, 303 const EGLint *attrib_list) 304{ 305 clearError(); 306 307 egl_connection_t* cnx = NULL; 308 egl_display_ptr dp = validate_display_connection(dpy, cnx); 309 if (dp) { 310 EGLSurface surface = cnx->egl.eglCreatePbufferSurface( 311 dp->disp.dpy, config, attrib_list); 312 if (surface != EGL_NO_SURFACE) { 313 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, 314 surface, cnx); 315 return s; 316 } 317 } 318 return EGL_NO_SURFACE; 319} 320 321EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) 322{ 323 clearError(); 324 325 const egl_display_ptr dp = validate_display(dpy); 326 if (!dp) return EGL_FALSE; 327 328 SurfaceRef _s(dp.get(), surface); 329 if (!_s.get()) 330 return setError(EGL_BAD_SURFACE, EGL_FALSE); 331 332 egl_surface_t * const s = get_surface(surface); 333 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface); 334 if (result == EGL_TRUE) { 335 _s.terminate(); 336 } 337 return result; 338} 339 340EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface, 341 EGLint attribute, EGLint *value) 342{ 343 clearError(); 344 345 const egl_display_ptr dp = validate_display(dpy); 346 if (!dp) return EGL_FALSE; 347 348 SurfaceRef _s(dp.get(), surface); 349 if (!_s.get()) 350 return setError(EGL_BAD_SURFACE, EGL_FALSE); 351 352 egl_surface_t const * const s = get_surface(surface); 353 return s->cnx->egl.eglQuerySurface( 354 dp->disp.dpy, s->surface, attribute, value); 355} 356 357void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) { 358 ATRACE_CALL(); 359 clearError(); 360 361 const egl_display_ptr dp = validate_display(dpy); 362 if (!dp) { 363 return; 364 } 365 366 SurfaceRef _s(dp.get(), surface); 367 if (!_s.get()) { 368 setError(EGL_BAD_SURFACE, EGL_FALSE); 369 return; 370 } 371 372 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC); 373 374 egl_surface_t const * const s = get_surface(surface); 375 native_window_set_buffers_timestamp(s->win.get(), timestamp); 376} 377 378// ---------------------------------------------------------------------------- 379// Contexts 380// ---------------------------------------------------------------------------- 381 382EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, 383 EGLContext share_list, const EGLint *attrib_list) 384{ 385 clearError(); 386 387 egl_connection_t* cnx = NULL; 388 const egl_display_ptr dp = validate_display_connection(dpy, cnx); 389 if (dpy) { 390 if (share_list != EGL_NO_CONTEXT) { 391 egl_context_t* const c = get_context(share_list); 392 share_list = c->context; 393 } 394 EGLContext context = cnx->egl.eglCreateContext( 395 dp->disp.dpy, config, share_list, attrib_list); 396 if (context != EGL_NO_CONTEXT) { 397 // figure out if it's a GLESv1 or GLESv2 398 int version = 0; 399 if (attrib_list) { 400 while (*attrib_list != EGL_NONE) { 401 GLint attr = *attrib_list++; 402 GLint value = *attrib_list++; 403 if (attr == EGL_CONTEXT_CLIENT_VERSION) { 404 if (value == 1) { 405 version = egl_connection_t::GLESv1_INDEX; 406 } else if (value == 2) { 407 version = egl_connection_t::GLESv2_INDEX; 408 } 409 } 410 }; 411 } 412 egl_context_t* c = new egl_context_t(dpy, context, config, cnx, 413 version); 414#if EGL_TRACE 415 if (gEGLDebugLevel > 0) 416 GLTrace_eglCreateContext(version, c); 417#endif 418 return c; 419 } 420 } 421 return EGL_NO_CONTEXT; 422} 423 424EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) 425{ 426 clearError(); 427 428 const egl_display_ptr dp = validate_display(dpy); 429 if (!dp) 430 return EGL_FALSE; 431 432 ContextRef _c(dp.get(), ctx); 433 if (!_c.get()) 434 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 435 436 egl_context_t * const c = get_context(ctx); 437 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context); 438 if (result == EGL_TRUE) { 439 _c.terminate(); 440 } 441 return result; 442} 443 444EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, 445 EGLSurface read, EGLContext ctx) 446{ 447 clearError(); 448 449 egl_display_ptr dp = validate_display(dpy); 450 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); 451 452 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not 453 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is 454 // a valid but uninitialized display. 455 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) || 456 (draw != EGL_NO_SURFACE) ) { 457 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE); 458 } 459 460 // get a reference to the object passed in 461 ContextRef _c(dp.get(), ctx); 462 SurfaceRef _d(dp.get(), draw); 463 SurfaceRef _r(dp.get(), read); 464 465 // validate the context (if not EGL_NO_CONTEXT) 466 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) { 467 // EGL_NO_CONTEXT is valid 468 return EGL_FALSE; 469 } 470 471 // these are the underlying implementation's object 472 EGLContext impl_ctx = EGL_NO_CONTEXT; 473 EGLSurface impl_draw = EGL_NO_SURFACE; 474 EGLSurface impl_read = EGL_NO_SURFACE; 475 476 // these are our objects structs passed in 477 egl_context_t * c = NULL; 478 egl_surface_t const * d = NULL; 479 egl_surface_t const * r = NULL; 480 481 // these are the current objects structs 482 egl_context_t * cur_c = get_context(getContext()); 483 484 if (ctx != EGL_NO_CONTEXT) { 485 c = get_context(ctx); 486 impl_ctx = c->context; 487 } else { 488 // no context given, use the implementation of the current context 489 if (cur_c == NULL) { 490 // no current context 491 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) { 492 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT); 493 return setError(EGL_BAD_MATCH, EGL_FALSE); 494 } 495 // not an error, there is just no current context. 496 return EGL_TRUE; 497 } 498 } 499 500 // retrieve the underlying implementation's draw EGLSurface 501 if (draw != EGL_NO_SURFACE) { 502 d = get_surface(draw); 503 impl_draw = d->surface; 504 } 505 506 // retrieve the underlying implementation's read EGLSurface 507 if (read != EGL_NO_SURFACE) { 508 r = get_surface(read); 509 impl_read = r->surface; 510 } 511 512 513 EGLBoolean result = dp->makeCurrent(c, cur_c, 514 draw, read, ctx, 515 impl_draw, impl_read, impl_ctx); 516 517 if (result == EGL_TRUE) { 518 if (c) { 519 setGLHooksThreadSpecific(c->cnx->hooks[c->version]); 520 egl_tls_t::setContext(ctx); 521#if EGL_TRACE 522 if (gEGLDebugLevel > 0) 523 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx); 524#endif 525 _c.acquire(); 526 _r.acquire(); 527 _d.acquire(); 528 } else { 529 setGLHooksThreadSpecific(&gHooksNoContext); 530 egl_tls_t::setContext(EGL_NO_CONTEXT); 531 } 532 } else { 533 // this will ALOGE the error 534 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE); 535 } 536 return result; 537} 538 539 540EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, 541 EGLint attribute, EGLint *value) 542{ 543 clearError(); 544 545 const egl_display_ptr dp = validate_display(dpy); 546 if (!dp) return EGL_FALSE; 547 548 ContextRef _c(dp.get(), ctx); 549 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); 550 551 egl_context_t * const c = get_context(ctx); 552 return c->cnx->egl.eglQueryContext( 553 dp->disp.dpy, c->context, attribute, value); 554 555} 556 557EGLContext eglGetCurrentContext(void) 558{ 559 // could be called before eglInitialize(), but we wouldn't have a context 560 // then, and this function would correctly return EGL_NO_CONTEXT. 561 562 clearError(); 563 564 EGLContext ctx = getContext(); 565 return ctx; 566} 567 568EGLSurface eglGetCurrentSurface(EGLint readdraw) 569{ 570 // could be called before eglInitialize(), but we wouldn't have a context 571 // then, and this function would correctly return EGL_NO_SURFACE. 572 573 clearError(); 574 575 EGLContext ctx = getContext(); 576 if (ctx) { 577 egl_context_t const * const c = get_context(ctx); 578 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); 579 switch (readdraw) { 580 case EGL_READ: return c->read; 581 case EGL_DRAW: return c->draw; 582 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); 583 } 584 } 585 return EGL_NO_SURFACE; 586} 587 588EGLDisplay eglGetCurrentDisplay(void) 589{ 590 // could be called before eglInitialize(), but we wouldn't have a context 591 // then, and this function would correctly return EGL_NO_DISPLAY. 592 593 clearError(); 594 595 EGLContext ctx = getContext(); 596 if (ctx) { 597 egl_context_t const * const c = get_context(ctx); 598 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); 599 return c->dpy; 600 } 601 return EGL_NO_DISPLAY; 602} 603 604EGLBoolean eglWaitGL(void) 605{ 606 clearError(); 607 608 egl_connection_t* const cnx = &gEGLImpl; 609 if (!cnx->dso) 610 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 611 612 return cnx->egl.eglWaitGL(); 613} 614 615EGLBoolean eglWaitNative(EGLint engine) 616{ 617 clearError(); 618 619 egl_connection_t* const cnx = &gEGLImpl; 620 if (!cnx->dso) 621 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 622 623 return cnx->egl.eglWaitNative(engine); 624} 625 626EGLint eglGetError(void) 627{ 628 EGLint err = EGL_SUCCESS; 629 egl_connection_t* const cnx = &gEGLImpl; 630 if (cnx->dso) { 631 err = cnx->egl.eglGetError(); 632 } 633 if (err == EGL_SUCCESS) { 634 err = egl_tls_t::getError(); 635 } 636 return err; 637} 638 639__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname) 640{ 641 // eglGetProcAddress() could be the very first function called 642 // in which case we must make sure we've initialized ourselves, this 643 // happens the first time egl_get_display() is called. 644 645 clearError(); 646 647 if (egl_init_drivers() == EGL_FALSE) { 648 setError(EGL_BAD_PARAMETER, NULL); 649 return NULL; 650 } 651 652 // These extensions should not be exposed to applications. They're used 653 // internally by the Android EGL layer. 654 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") || 655 !strcmp(procname, "eglDupNativeFenceFDANDROID") || 656 !strcmp(procname, "eglWaitSyncANDROID") || 657 !strcmp(procname, "eglHibernateProcessIMG") || 658 !strcmp(procname, "eglAwakenProcessIMG")) { 659 return NULL; 660 } 661 662 __eglMustCastToProperFunctionPointerType addr; 663 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap)); 664 if (addr) return addr; 665 666 667 // this protects accesses to sGLExtentionMap and sGLExtentionSlot 668 pthread_mutex_lock(&sExtensionMapMutex); 669 670 /* 671 * Since eglGetProcAddress() is not associated to anything, it needs 672 * to return a function pointer that "works" regardless of what 673 * the current context is. 674 * 675 * For this reason, we return a "forwarder", a small stub that takes 676 * care of calling the function associated with the context 677 * currently bound. 678 * 679 * We first look for extensions we've already resolved, if we're seeing 680 * this extension for the first time, we go through all our 681 * implementations and call eglGetProcAddress() and record the 682 * result in the appropriate implementation hooks and return the 683 * address of the forwarder corresponding to that hook set. 684 * 685 */ 686 687 const String8 name(procname); 688 addr = sGLExtentionMap.valueFor(name); 689 const int slot = sGLExtentionSlot; 690 691 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS, 692 "no more slots for eglGetProcAddress(\"%s\")", 693 procname); 694 695#if EGL_TRACE 696 gl_hooks_t *debugHooks = GLTrace_getGLHooks(); 697#endif 698 699 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) { 700 bool found = false; 701 702 egl_connection_t* const cnx = &gEGLImpl; 703 if (cnx->dso && cnx->egl.eglGetProcAddress) { 704 found = true; 705 // Extensions are independent of the bound context 706 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] = 707 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] = 708#if EGL_TRACE 709 debugHooks->ext.extensions[slot] = 710 gHooksTrace.ext.extensions[slot] = 711#endif 712 cnx->egl.eglGetProcAddress(procname); 713 } 714 715 if (found) { 716 addr = gExtensionForwarders[slot]; 717 sGLExtentionMap.add(name, addr); 718 sGLExtentionSlot++; 719 } 720 } 721 722 pthread_mutex_unlock(&sExtensionMapMutex); 723 return addr; 724} 725 726class FrameCompletionThread : public Thread { 727public: 728 729 static void queueSync(EGLSyncKHR sync) { 730 static sp<FrameCompletionThread> thread(new FrameCompletionThread); 731 static bool running = false; 732 if (!running) { 733 thread->run("GPUFrameCompletion"); 734 running = true; 735 } 736 { 737 Mutex::Autolock lock(thread->mMutex); 738 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d", 739 thread->mFramesQueued).string()); 740 thread->mQueue.push_back(sync); 741 thread->mCondition.signal(); 742 thread->mFramesQueued++; 743 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size()); 744 } 745 } 746 747private: 748 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {} 749 750 virtual bool threadLoop() { 751 EGLSyncKHR sync; 752 uint32_t frameNum; 753 { 754 Mutex::Autolock lock(mMutex); 755 while (mQueue.isEmpty()) { 756 mCondition.wait(mMutex); 757 } 758 sync = mQueue[0]; 759 frameNum = mFramesCompleted; 760 } 761 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 762 { 763 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d", 764 frameNum).string()); 765 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR); 766 if (result == EGL_FALSE) { 767 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError()); 768 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { 769 ALOGE("FrameCompletion: timeout waiting for fence"); 770 } 771 eglDestroySyncKHR(dpy, sync); 772 } 773 { 774 Mutex::Autolock lock(mMutex); 775 mQueue.removeAt(0); 776 mFramesCompleted++; 777 ATRACE_INT("GPU Frames Outstanding", mQueue.size()); 778 } 779 return true; 780 } 781 782 uint32_t mFramesQueued; 783 uint32_t mFramesCompleted; 784 Vector<EGLSyncKHR> mQueue; 785 Condition mCondition; 786 Mutex mMutex; 787}; 788 789EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) 790{ 791 ATRACE_CALL(); 792 clearError(); 793 794 const egl_display_ptr dp = validate_display(dpy); 795 if (!dp) return EGL_FALSE; 796 797 SurfaceRef _s(dp.get(), draw); 798 if (!_s.get()) 799 return setError(EGL_BAD_SURFACE, EGL_FALSE); 800 801#if EGL_TRACE 802 if (gEGLDebugLevel > 0) 803 GLTrace_eglSwapBuffers(dpy, draw); 804#endif 805 806 egl_surface_t const * const s = get_surface(draw); 807 808 if (CC_UNLIKELY(dp->finishOnSwap)) { 809 uint32_t pixel; 810 egl_context_t * const c = get_context( egl_tls_t::getContext() ); 811 if (c) { 812 // glReadPixels() ensures that the frame is complete 813 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1, 814 GL_RGBA,GL_UNSIGNED_BYTE,&pixel); 815 } 816 } 817 818 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface); 819 820 if (CC_UNLIKELY(dp->traceGpuCompletion)) { 821 EGLSyncKHR sync = EGL_NO_SYNC_KHR; 822 { 823 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); 824 } 825 if (sync != EGL_NO_SYNC_KHR) { 826 FrameCompletionThread::queueSync(sync); 827 } 828 } 829 830 return result; 831} 832 833EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, 834 NativePixmapType target) 835{ 836 clearError(); 837 838 const egl_display_ptr dp = validate_display(dpy); 839 if (!dp) return EGL_FALSE; 840 841 SurfaceRef _s(dp.get(), surface); 842 if (!_s.get()) 843 return setError(EGL_BAD_SURFACE, EGL_FALSE); 844 845 egl_surface_t const * const s = get_surface(surface); 846 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target); 847} 848 849const char* eglQueryString(EGLDisplay dpy, EGLint name) 850{ 851 clearError(); 852 853 const egl_display_ptr dp = validate_display(dpy); 854 if (!dp) return (const char *) NULL; 855 856 switch (name) { 857 case EGL_VENDOR: 858 return dp->getVendorString(); 859 case EGL_VERSION: 860 return dp->getVersionString(); 861 case EGL_EXTENSIONS: 862 return dp->getExtensionString(); 863 case EGL_CLIENT_APIS: 864 return dp->getClientApiString(); 865 case EGL_VERSION_HW_ANDROID: 866 return dp->disp.queryString.version; 867 } 868 return setError(EGL_BAD_PARAMETER, (const char *)0); 869} 870 871 872// ---------------------------------------------------------------------------- 873// EGL 1.1 874// ---------------------------------------------------------------------------- 875 876EGLBoolean eglSurfaceAttrib( 877 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) 878{ 879 clearError(); 880 881 const egl_display_ptr dp = validate_display(dpy); 882 if (!dp) return EGL_FALSE; 883 884 SurfaceRef _s(dp.get(), surface); 885 if (!_s.get()) 886 return setError(EGL_BAD_SURFACE, EGL_FALSE); 887 888 egl_surface_t const * const s = get_surface(surface); 889 if (s->cnx->egl.eglSurfaceAttrib) { 890 return s->cnx->egl.eglSurfaceAttrib( 891 dp->disp.dpy, s->surface, attribute, value); 892 } 893 return setError(EGL_BAD_SURFACE, EGL_FALSE); 894} 895 896EGLBoolean eglBindTexImage( 897 EGLDisplay dpy, EGLSurface surface, EGLint buffer) 898{ 899 clearError(); 900 901 const egl_display_ptr dp = validate_display(dpy); 902 if (!dp) return EGL_FALSE; 903 904 SurfaceRef _s(dp.get(), surface); 905 if (!_s.get()) 906 return setError(EGL_BAD_SURFACE, EGL_FALSE); 907 908 egl_surface_t const * const s = get_surface(surface); 909 if (s->cnx->egl.eglBindTexImage) { 910 return s->cnx->egl.eglBindTexImage( 911 dp->disp.dpy, s->surface, buffer); 912 } 913 return setError(EGL_BAD_SURFACE, EGL_FALSE); 914} 915 916EGLBoolean eglReleaseTexImage( 917 EGLDisplay dpy, EGLSurface surface, EGLint buffer) 918{ 919 clearError(); 920 921 const egl_display_ptr dp = validate_display(dpy); 922 if (!dp) return EGL_FALSE; 923 924 SurfaceRef _s(dp.get(), surface); 925 if (!_s.get()) 926 return setError(EGL_BAD_SURFACE, EGL_FALSE); 927 928 egl_surface_t const * const s = get_surface(surface); 929 if (s->cnx->egl.eglReleaseTexImage) { 930 return s->cnx->egl.eglReleaseTexImage( 931 dp->disp.dpy, s->surface, buffer); 932 } 933 return setError(EGL_BAD_SURFACE, EGL_FALSE); 934} 935 936EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) 937{ 938 clearError(); 939 940 const egl_display_ptr dp = validate_display(dpy); 941 if (!dp) return EGL_FALSE; 942 943 EGLBoolean res = EGL_TRUE; 944 egl_connection_t* const cnx = &gEGLImpl; 945 if (cnx->dso && cnx->egl.eglSwapInterval) { 946 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval); 947 } 948 949 return res; 950} 951 952 953// ---------------------------------------------------------------------------- 954// EGL 1.2 955// ---------------------------------------------------------------------------- 956 957EGLBoolean eglWaitClient(void) 958{ 959 clearError(); 960 961 egl_connection_t* const cnx = &gEGLImpl; 962 if (!cnx->dso) 963 return setError(EGL_BAD_CONTEXT, EGL_FALSE); 964 965 EGLBoolean res; 966 if (cnx->egl.eglWaitClient) { 967 res = cnx->egl.eglWaitClient(); 968 } else { 969 res = cnx->egl.eglWaitGL(); 970 } 971 return res; 972} 973 974EGLBoolean eglBindAPI(EGLenum api) 975{ 976 clearError(); 977 978 if (egl_init_drivers() == EGL_FALSE) { 979 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 980 } 981 982 // bind this API on all EGLs 983 EGLBoolean res = EGL_TRUE; 984 egl_connection_t* const cnx = &gEGLImpl; 985 if (cnx->dso && cnx->egl.eglBindAPI) { 986 res = cnx->egl.eglBindAPI(api); 987 } 988 return res; 989} 990 991EGLenum eglQueryAPI(void) 992{ 993 clearError(); 994 995 if (egl_init_drivers() == EGL_FALSE) { 996 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 997 } 998 999 egl_connection_t* const cnx = &gEGLImpl; 1000 if (cnx->dso && cnx->egl.eglQueryAPI) { 1001 return cnx->egl.eglQueryAPI(); 1002 } 1003 1004 // or, it can only be OpenGL ES 1005 return EGL_OPENGL_ES_API; 1006} 1007 1008EGLBoolean eglReleaseThread(void) 1009{ 1010 clearError(); 1011 1012 // If there is context bound to the thread, release it 1013 egl_display_t::loseCurrent(get_context(getContext())); 1014 1015 egl_connection_t* const cnx = &gEGLImpl; 1016 if (cnx->dso && cnx->egl.eglReleaseThread) { 1017 cnx->egl.eglReleaseThread(); 1018 } 1019 1020 egl_tls_t::clearTLS(); 1021#if EGL_TRACE 1022 if (gEGLDebugLevel > 0) 1023 GLTrace_eglReleaseThread(); 1024#endif 1025 return EGL_TRUE; 1026} 1027 1028EGLSurface eglCreatePbufferFromClientBuffer( 1029 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, 1030 EGLConfig config, const EGLint *attrib_list) 1031{ 1032 clearError(); 1033 1034 egl_connection_t* cnx = NULL; 1035 const egl_display_ptr dp = validate_display_connection(dpy, cnx); 1036 if (!dp) return EGL_FALSE; 1037 if (cnx->egl.eglCreatePbufferFromClientBuffer) { 1038 return cnx->egl.eglCreatePbufferFromClientBuffer( 1039 dp->disp.dpy, buftype, buffer, config, attrib_list); 1040 } 1041 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE); 1042} 1043 1044// ---------------------------------------------------------------------------- 1045// EGL_EGLEXT_VERSION 3 1046// ---------------------------------------------------------------------------- 1047 1048EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, 1049 const EGLint *attrib_list) 1050{ 1051 clearError(); 1052 1053 const egl_display_ptr dp = validate_display(dpy); 1054 if (!dp) return EGL_FALSE; 1055 1056 SurfaceRef _s(dp.get(), surface); 1057 if (!_s.get()) 1058 return setError(EGL_BAD_SURFACE, EGL_FALSE); 1059 1060 egl_surface_t const * const s = get_surface(surface); 1061 if (s->cnx->egl.eglLockSurfaceKHR) { 1062 return s->cnx->egl.eglLockSurfaceKHR( 1063 dp->disp.dpy, s->surface, attrib_list); 1064 } 1065 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1066} 1067 1068EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) 1069{ 1070 clearError(); 1071 1072 const egl_display_ptr dp = validate_display(dpy); 1073 if (!dp) return EGL_FALSE; 1074 1075 SurfaceRef _s(dp.get(), surface); 1076 if (!_s.get()) 1077 return setError(EGL_BAD_SURFACE, EGL_FALSE); 1078 1079 egl_surface_t const * const s = get_surface(surface); 1080 if (s->cnx->egl.eglUnlockSurfaceKHR) { 1081 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface); 1082 } 1083 return setError(EGL_BAD_DISPLAY, EGL_FALSE); 1084} 1085 1086EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, 1087 EGLClientBuffer buffer, const EGLint *attrib_list) 1088{ 1089 clearError(); 1090 1091 const egl_display_ptr dp = validate_display(dpy); 1092 if (!dp) return EGL_NO_IMAGE_KHR; 1093 1094 ContextRef _c(dp.get(), ctx); 1095 egl_context_t * const c = _c.get(); 1096 1097 EGLImageKHR result = EGL_NO_IMAGE_KHR; 1098 egl_connection_t* const cnx = &gEGLImpl; 1099 if (cnx->dso && cnx->egl.eglCreateImageKHR) { 1100 result = cnx->egl.eglCreateImageKHR( 1101 dp->disp.dpy, 1102 c ? c->context : EGL_NO_CONTEXT, 1103 target, buffer, attrib_list); 1104 } 1105 return result; 1106} 1107 1108EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) 1109{ 1110 clearError(); 1111 1112 const egl_display_ptr dp = validate_display(dpy); 1113 if (!dp) return EGL_FALSE; 1114 1115 EGLBoolean result = EGL_FALSE; 1116 egl_connection_t* const cnx = &gEGLImpl; 1117 if (cnx->dso && cnx->egl.eglDestroyImageKHR) { 1118 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img); 1119 } 1120 return result; 1121} 1122 1123// ---------------------------------------------------------------------------- 1124// EGL_EGLEXT_VERSION 5 1125// ---------------------------------------------------------------------------- 1126 1127 1128EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list) 1129{ 1130 clearError(); 1131 1132 const egl_display_ptr dp = validate_display(dpy); 1133 if (!dp) return EGL_NO_SYNC_KHR; 1134 1135 EGLSyncKHR result = EGL_NO_SYNC_KHR; 1136 egl_connection_t* const cnx = &gEGLImpl; 1137 if (cnx->dso && cnx->egl.eglCreateSyncKHR) { 1138 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list); 1139 } 1140 return result; 1141} 1142 1143EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync) 1144{ 1145 clearError(); 1146 1147 const egl_display_ptr dp = validate_display(dpy); 1148 if (!dp) return EGL_FALSE; 1149 1150 EGLBoolean result = EGL_FALSE; 1151 egl_connection_t* const cnx = &gEGLImpl; 1152 if (cnx->dso && cnx->egl.eglDestroySyncKHR) { 1153 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync); 1154 } 1155 return result; 1156} 1157 1158EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, 1159 EGLint flags, EGLTimeKHR timeout) 1160{ 1161 clearError(); 1162 1163 const egl_display_ptr dp = validate_display(dpy); 1164 if (!dp) return EGL_FALSE; 1165 1166 EGLBoolean result = EGL_FALSE; 1167 egl_connection_t* const cnx = &gEGLImpl; 1168 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) { 1169 result = cnx->egl.eglClientWaitSyncKHR( 1170 dp->disp.dpy, sync, flags, timeout); 1171 } 1172 return result; 1173} 1174 1175EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, 1176 EGLint attribute, EGLint *value) 1177{ 1178 clearError(); 1179 1180 const egl_display_ptr dp = validate_display(dpy); 1181 if (!dp) return EGL_FALSE; 1182 1183 EGLBoolean result = EGL_FALSE; 1184 egl_connection_t* const cnx = &gEGLImpl; 1185 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) { 1186 result = cnx->egl.eglGetSyncAttribKHR( 1187 dp->disp.dpy, sync, attribute, value); 1188 } 1189 return result; 1190} 1191 1192// ---------------------------------------------------------------------------- 1193// ANDROID extensions 1194// ---------------------------------------------------------------------------- 1195 1196EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync) 1197{ 1198 clearError(); 1199 1200 const egl_display_ptr dp = validate_display(dpy); 1201 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID; 1202 1203 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID; 1204 egl_connection_t* const cnx = &gEGLImpl; 1205 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) { 1206 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync); 1207 } 1208 return result; 1209} 1210 1211EGLint eglWaitSyncANDROID(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) 1212{ 1213 clearError(); 1214 1215 const egl_display_ptr dp = validate_display(dpy); 1216 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID; 1217 1218 EGLint result = EGL_FALSE; 1219 egl_connection_t* const cnx = &gEGLImpl; 1220 if (cnx->dso && cnx->egl.eglWaitSyncANDROID) { 1221 result = cnx->egl.eglWaitSyncANDROID(dp->disp.dpy, sync, flags); 1222 } 1223 return result; 1224} 1225 1226// ---------------------------------------------------------------------------- 1227// NVIDIA extensions 1228// ---------------------------------------------------------------------------- 1229EGLuint64NV eglGetSystemTimeFrequencyNV() 1230{ 1231 clearError(); 1232 1233 if (egl_init_drivers() == EGL_FALSE) { 1234 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 1235 } 1236 1237 EGLuint64NV ret = 0; 1238 egl_connection_t* const cnx = &gEGLImpl; 1239 1240 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) { 1241 return cnx->egl.eglGetSystemTimeFrequencyNV(); 1242 } 1243 1244 return setErrorQuiet(EGL_BAD_DISPLAY, 0); 1245} 1246 1247EGLuint64NV eglGetSystemTimeNV() 1248{ 1249 clearError(); 1250 1251 if (egl_init_drivers() == EGL_FALSE) { 1252 return setError(EGL_BAD_PARAMETER, EGL_FALSE); 1253 } 1254 1255 EGLuint64NV ret = 0; 1256 egl_connection_t* const cnx = &gEGLImpl; 1257 1258 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) { 1259 return cnx->egl.eglGetSystemTimeNV(); 1260 } 1261 1262 return setErrorQuiet(EGL_BAD_DISPLAY, 0); 1263} 1264