2 ** Copyright 2007, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
23 #include <hardware/gralloc.h>
24 #include <system/window.h>
27 #include <EGL/eglext.h>
29 #include <GLES/glext.h>
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>
37 #include <utils/KeyedVector.h>
38 #include <utils/SortedVector.h>
39 #include <utils/String8.h>
40 #include <utils/Trace.h>
44 #include "glestrace.h"
47 #include "egl_display.h"
49 #include "egl_object.h"
53 using namespace android;
55 // ----------------------------------------------------------------------------
57 #define EGL_VERSION_HW_ANDROID 0x3143
59 struct extention_map_t {
61 __eglMustCastToProperFunctionPointerType address;
64 static 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 },
79 // accesses protected by sExtensionMapMutex
80 static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
81 static int sGLExtentionSlot = 0;
82 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
84 static 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;
94 // ----------------------------------------------------------------------------
97 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
98 extern EGLBoolean egl_init_drivers();
99 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
100 extern int gEGLDebugLevel;
101 extern gl_hooks_t gHooksTrace;
102 } // namespace android;
104 // ----------------------------------------------------------------------------
106 static inline void clearError() { egl_tls_t::clearError(); }
107 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
109 // ----------------------------------------------------------------------------
111 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
115 uint32_t index = uint32_t(display);
116 if (index >= NUM_DISPLAYS) {
117 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
120 if (egl_init_drivers() == EGL_FALSE) {
121 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
124 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
136 egl_display_ptr dp = get_display(dpy);
137 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
139 EGLBoolean res = dp->initialize(major, minor);
144 EGLBoolean eglTerminate(EGLDisplay dpy)
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.
152 egl_display_ptr dp = get_display(dpy);
153 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
155 EGLBoolean res = dp->terminate();
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 EGLBoolean eglGetConfigs( EGLDisplay dpy,
166 EGLint config_size, EGLint *num_config)
170 const egl_display_ptr dp = validate_display(dpy);
171 if (!dp) return EGL_FALSE;
174 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
177 EGLBoolean res = EGL_FALSE;
180 egl_connection_t* const cnx = &gEGLImpl;
182 res = cnx->egl.eglGetConfigs(
183 dp->disp.dpy, configs, config_size, num_config);
189 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
190 EGLConfig *configs, EGLint config_size,
195 const egl_display_ptr dp = validate_display(dpy);
196 if (!dp) return EGL_FALSE;
199 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
202 EGLBoolean res = EGL_FALSE;
205 egl_connection_t* const cnx = &gEGLImpl;
208 char value[PROPERTY_VALUE_MAX];
209 property_get("debug.egl.force_msaa", value, "false");
211 if (!strcmp(value, "true")) {
212 size_t attribCount = 0;
213 EGLint attrib = attrib_list[0];
215 // Only enable MSAA if the context is OpenGL ES 2.0 and
216 // if no caveat is requested
217 const EGLint *attribRendererable = NULL;
218 const EGLint *attribCaveat = NULL;
220 // Count the number of attributes and look for
221 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
222 while (attrib != EGL_NONE) {
223 attrib = attrib_list[attribCount];
225 case EGL_RENDERABLE_TYPE:
226 attribRendererable = &attrib_list[attribCount];
228 case EGL_CONFIG_CAVEAT:
229 attribCaveat = &attrib_list[attribCount];
235 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
236 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
238 // Insert 2 extra attributes to force-enable MSAA 4x
239 EGLint aaAttribs[attribCount + 4];
240 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
242 aaAttribs[2] = EGL_SAMPLES;
245 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
248 EGLBoolean resAA = cnx->egl.eglChooseConfig(
249 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
251 if (resAA == EGL_TRUE && numConfigAA > 0) {
252 ALOGD("Enabling MSAA 4x");
253 *num_config = numConfigAA;
260 res = cnx->egl.eglChooseConfig(
261 dp->disp.dpy, attrib_list, configs, config_size, num_config);
266 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
267 EGLint attribute, EGLint *value)
271 egl_connection_t* cnx = NULL;
272 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
273 if (!dp) return EGL_FALSE;
275 return cnx->egl.eglGetConfigAttrib(
276 dp->disp.dpy, config, attribute, value);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
284 NativeWindowType window,
285 const EGLint *attrib_list)
289 egl_connection_t* cnx = NULL;
290 egl_display_ptr dp = validate_display_connection(dpy, cnx);
292 EGLDisplay iDpy = dp->disp.dpy;
295 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
296 ALOGE("EGLNativeWindowType %p already connected to another API",
298 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
301 // set the native window's buffers format to match this config
302 if (cnx->egl.eglGetConfigAttrib(iDpy,
303 config, EGL_NATIVE_VISUAL_ID, &format)) {
305 int err = native_window_set_buffers_format(window, format);
307 ALOGE("error setting native window pixel format: %s (%d)",
308 strerror(-err), err);
309 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
310 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
315 // the EGL spec requires that a new EGLSurface default to swap interval
316 // 1, so explicitly set that on the window here.
317 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
318 anw->setSwapInterval(anw, 1);
320 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
321 iDpy, config, window, attrib_list);
322 if (surface != EGL_NO_SURFACE) {
323 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
328 // EGLSurface creation failed
329 native_window_set_buffers_format(window, 0);
330 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
332 return EGL_NO_SURFACE;
335 EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
336 NativePixmapType pixmap,
337 const EGLint *attrib_list)
341 egl_connection_t* cnx = NULL;
342 egl_display_ptr dp = validate_display_connection(dpy, cnx);
344 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
345 dp->disp.dpy, config, pixmap, attrib_list);
346 if (surface != EGL_NO_SURFACE) {
347 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
352 return EGL_NO_SURFACE;
355 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
356 const EGLint *attrib_list)
360 egl_connection_t* cnx = NULL;
361 egl_display_ptr dp = validate_display_connection(dpy, cnx);
363 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
364 dp->disp.dpy, config, attrib_list);
365 if (surface != EGL_NO_SURFACE) {
366 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
371 return EGL_NO_SURFACE;
374 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
378 const egl_display_ptr dp = validate_display(dpy);
379 if (!dp) return EGL_FALSE;
381 SurfaceRef _s(dp.get(), surface);
383 return setError(EGL_BAD_SURFACE, EGL_FALSE);
385 egl_surface_t * const s = get_surface(surface);
386 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
387 if (result == EGL_TRUE) {
393 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
394 EGLint attribute, EGLint *value)
398 const egl_display_ptr dp = validate_display(dpy);
399 if (!dp) return EGL_FALSE;
401 SurfaceRef _s(dp.get(), surface);
403 return setError(EGL_BAD_SURFACE, EGL_FALSE);
405 egl_surface_t const * const s = get_surface(surface);
406 return s->cnx->egl.eglQuerySurface(
407 dp->disp.dpy, s->surface, attribute, value);
410 void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
414 const egl_display_ptr dp = validate_display(dpy);
419 SurfaceRef _s(dp.get(), surface);
421 setError(EGL_BAD_SURFACE, EGL_FALSE);
425 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
427 egl_surface_t const * const s = get_surface(surface);
428 native_window_set_buffers_timestamp(s->win.get(), timestamp);
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
436 EGLContext share_list, const EGLint *attrib_list)
440 egl_connection_t* cnx = NULL;
441 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (share_list != EGL_NO_CONTEXT) {
444 egl_context_t* const c = get_context(share_list);
445 share_list = c->context;
447 EGLContext context = cnx->egl.eglCreateContext(
448 dp->disp.dpy, config, share_list, attrib_list);
449 if (context != EGL_NO_CONTEXT) {
450 // figure out if it's a GLESv1 or GLESv2
453 while (*attrib_list != EGL_NONE) {
454 GLint attr = *attrib_list++;
455 GLint value = *attrib_list++;
456 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
458 version = egl_connection_t::GLESv1_INDEX;
459 } else if (value == 2) {
460 version = egl_connection_t::GLESv2_INDEX;
465 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
468 if (gEGLDebugLevel > 0)
469 GLTrace_eglCreateContext(version, c);
473 EGLint error = eglGetError();
474 ALOGE_IF(error == EGL_SUCCESS,
475 "eglCreateContext(%p, %p, %p, %p) returned EGL_NO_CONTEXT "
477 dpy, config, share_list, attrib_list);
480 return EGL_NO_CONTEXT;
483 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
487 const egl_display_ptr dp = validate_display(dpy);
491 ContextRef _c(dp.get(), ctx);
493 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
495 egl_context_t * const c = get_context(ctx);
496 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
497 if (result == EGL_TRUE) {
503 EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
504 EGLSurface read, EGLContext ctx)
508 egl_display_ptr dp = validate_display(dpy);
509 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
511 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
512 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
513 // a valid but uninitialized display.
514 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
515 (draw != EGL_NO_SURFACE) ) {
516 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
519 // get a reference to the object passed in
520 ContextRef _c(dp.get(), ctx);
521 SurfaceRef _d(dp.get(), draw);
522 SurfaceRef _r(dp.get(), read);
524 // validate the context (if not EGL_NO_CONTEXT)
525 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
526 // EGL_NO_CONTEXT is valid
530 // these are the underlying implementation's object
531 EGLContext impl_ctx = EGL_NO_CONTEXT;
532 EGLSurface impl_draw = EGL_NO_SURFACE;
533 EGLSurface impl_read = EGL_NO_SURFACE;
535 // these are our objects structs passed in
536 egl_context_t * c = NULL;
537 egl_surface_t const * d = NULL;
538 egl_surface_t const * r = NULL;
540 // these are the current objects structs
541 egl_context_t * cur_c = get_context(getContext());
543 if (ctx != EGL_NO_CONTEXT) {
544 c = get_context(ctx);
545 impl_ctx = c->context;
547 // no context given, use the implementation of the current context
549 // no current context
550 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
551 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
552 return setError(EGL_BAD_MATCH, EGL_FALSE);
554 // not an error, there is just no current context.
559 // retrieve the underlying implementation's draw EGLSurface
560 if (draw != EGL_NO_SURFACE) {
561 d = get_surface(draw);
562 impl_draw = d->surface;
565 // retrieve the underlying implementation's read EGLSurface
566 if (read != EGL_NO_SURFACE) {
567 r = get_surface(read);
568 impl_read = r->surface;
572 EGLBoolean result = dp->makeCurrent(c, cur_c,
574 impl_draw, impl_read, impl_ctx);
576 if (result == EGL_TRUE) {
578 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
579 egl_tls_t::setContext(ctx);
581 if (gEGLDebugLevel > 0)
582 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
588 setGLHooksThreadSpecific(&gHooksNoContext);
589 egl_tls_t::setContext(EGL_NO_CONTEXT);
592 // this will ALOGE the error
593 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
599 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
600 EGLint attribute, EGLint *value)
604 const egl_display_ptr dp = validate_display(dpy);
605 if (!dp) return EGL_FALSE;
607 ContextRef _c(dp.get(), ctx);
608 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
610 egl_context_t * const c = get_context(ctx);
611 return c->cnx->egl.eglQueryContext(
612 dp->disp.dpy, c->context, attribute, value);
616 EGLContext eglGetCurrentContext(void)
618 // could be called before eglInitialize(), but we wouldn't have a context
619 // then, and this function would correctly return EGL_NO_CONTEXT.
623 EGLContext ctx = getContext();
627 EGLSurface eglGetCurrentSurface(EGLint readdraw)
629 // could be called before eglInitialize(), but we wouldn't have a context
630 // then, and this function would correctly return EGL_NO_SURFACE.
634 EGLContext ctx = getContext();
636 egl_context_t const * const c = get_context(ctx);
637 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
639 case EGL_READ: return c->read;
640 case EGL_DRAW: return c->draw;
641 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
644 return EGL_NO_SURFACE;
647 EGLDisplay eglGetCurrentDisplay(void)
649 // could be called before eglInitialize(), but we wouldn't have a context
650 // then, and this function would correctly return EGL_NO_DISPLAY.
654 EGLContext ctx = getContext();
656 egl_context_t const * const c = get_context(ctx);
657 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
660 return EGL_NO_DISPLAY;
663 EGLBoolean eglWaitGL(void)
667 egl_connection_t* const cnx = &gEGLImpl;
669 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
671 return cnx->egl.eglWaitGL();
674 EGLBoolean eglWaitNative(EGLint engine)
678 egl_connection_t* const cnx = &gEGLImpl;
680 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
682 return cnx->egl.eglWaitNative(engine);
685 EGLint eglGetError(void)
687 EGLint err = EGL_SUCCESS;
688 egl_connection_t* const cnx = &gEGLImpl;
690 err = cnx->egl.eglGetError();
692 if (err == EGL_SUCCESS) {
693 err = egl_tls_t::getError();
698 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
700 // eglGetProcAddress() could be the very first function called
701 // in which case we must make sure we've initialized ourselves, this
702 // happens the first time egl_get_display() is called.
706 if (egl_init_drivers() == EGL_FALSE) {
707 setError(EGL_BAD_PARAMETER, NULL);
711 // These extensions should not be exposed to applications. They're used
712 // internally by the Android EGL layer.
713 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") ||
714 !strcmp(procname, "eglDupNativeFenceFDANDROID") ||
715 !strcmp(procname, "eglWaitSyncANDROID") ||
716 !strcmp(procname, "eglHibernateProcessIMG") ||
717 !strcmp(procname, "eglAwakenProcessIMG")) {
721 __eglMustCastToProperFunctionPointerType addr;
722 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
723 if (addr) return addr;
726 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
727 pthread_mutex_lock(&sExtensionMapMutex);
730 * Since eglGetProcAddress() is not associated to anything, it needs
731 * to return a function pointer that "works" regardless of what
732 * the current context is.
734 * For this reason, we return a "forwarder", a small stub that takes
735 * care of calling the function associated with the context
738 * We first look for extensions we've already resolved, if we're seeing
739 * this extension for the first time, we go through all our
740 * implementations and call eglGetProcAddress() and record the
741 * result in the appropriate implementation hooks and return the
742 * address of the forwarder corresponding to that hook set.
746 const String8 name(procname);
747 addr = sGLExtentionMap.valueFor(name);
748 const int slot = sGLExtentionSlot;
750 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
751 "no more slots for eglGetProcAddress(\"%s\")",
755 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
758 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
761 egl_connection_t* const cnx = &gEGLImpl;
762 if (cnx->dso && cnx->egl.eglGetProcAddress) {
763 // Extensions are independent of the bound context
765 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
766 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
768 debugHooks->ext.extensions[slot] =
769 gHooksTrace.ext.extensions[slot] =
771 cnx->egl.eglGetProcAddress(procname);
772 if (addr) found = true;
777 addr = gExtensionForwarders[slot];
779 sGLExtentionMap.add(name, addr);
784 pthread_mutex_unlock(&sExtensionMapMutex);
788 class FrameCompletionThread : public Thread {
791 static void queueSync(EGLSyncKHR sync) {
792 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
793 static bool running = false;
795 thread->run("GPUFrameCompletion");
799 Mutex::Autolock lock(thread->mMutex);
800 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
801 thread->mFramesQueued).string());
802 thread->mQueue.push_back(sync);
803 thread->mCondition.signal();
804 thread->mFramesQueued++;
805 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
810 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
812 virtual bool threadLoop() {
816 Mutex::Autolock lock(mMutex);
817 while (mQueue.isEmpty()) {
818 mCondition.wait(mMutex);
821 frameNum = mFramesCompleted;
823 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
825 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
827 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
828 if (result == EGL_FALSE) {
829 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
830 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
831 ALOGE("FrameCompletion: timeout waiting for fence");
833 eglDestroySyncKHR(dpy, sync);
836 Mutex::Autolock lock(mMutex);
839 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
844 uint32_t mFramesQueued;
845 uint32_t mFramesCompleted;
846 Vector<EGLSyncKHR> mQueue;
847 Condition mCondition;
851 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
856 const egl_display_ptr dp = validate_display(dpy);
857 if (!dp) return EGL_FALSE;
859 SurfaceRef _s(dp.get(), draw);
861 return setError(EGL_BAD_SURFACE, EGL_FALSE);
864 if (gEGLDebugLevel > 0)
865 GLTrace_eglSwapBuffers(dpy, draw);
868 egl_surface_t const * const s = get_surface(draw);
870 if (CC_UNLIKELY(dp->finishOnSwap)) {
872 egl_context_t * const c = get_context( egl_tls_t::getContext() );
874 // glReadPixels() ensures that the frame is complete
875 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
876 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
880 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
882 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
883 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
885 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
887 if (sync != EGL_NO_SYNC_KHR) {
888 FrameCompletionThread::queueSync(sync);
895 EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
896 NativePixmapType target)
900 const egl_display_ptr dp = validate_display(dpy);
901 if (!dp) return EGL_FALSE;
903 SurfaceRef _s(dp.get(), surface);
905 return setError(EGL_BAD_SURFACE, EGL_FALSE);
907 egl_surface_t const * const s = get_surface(surface);
908 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
911 const char* eglQueryString(EGLDisplay dpy, EGLint name)
915 const egl_display_ptr dp = validate_display(dpy);
916 if (!dp) return (const char *) NULL;
920 return dp->getVendorString();
922 return dp->getVersionString();
924 return dp->getExtensionString();
925 case EGL_CLIENT_APIS:
926 return dp->getClientApiString();
927 case EGL_VERSION_HW_ANDROID:
928 return dp->disp.queryString.version;
930 return setError(EGL_BAD_PARAMETER, (const char *)0);
934 // ----------------------------------------------------------------------------
936 // ----------------------------------------------------------------------------
938 EGLBoolean eglSurfaceAttrib(
939 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
943 const egl_display_ptr dp = validate_display(dpy);
944 if (!dp) return EGL_FALSE;
946 SurfaceRef _s(dp.get(), surface);
948 return setError(EGL_BAD_SURFACE, EGL_FALSE);
950 egl_surface_t const * const s = get_surface(surface);
951 if (s->cnx->egl.eglSurfaceAttrib) {
952 return s->cnx->egl.eglSurfaceAttrib(
953 dp->disp.dpy, s->surface, attribute, value);
955 return setError(EGL_BAD_SURFACE, EGL_FALSE);
958 EGLBoolean eglBindTexImage(
959 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
963 const egl_display_ptr dp = validate_display(dpy);
964 if (!dp) return EGL_FALSE;
966 SurfaceRef _s(dp.get(), surface);
968 return setError(EGL_BAD_SURFACE, EGL_FALSE);
970 egl_surface_t const * const s = get_surface(surface);
971 if (s->cnx->egl.eglBindTexImage) {
972 return s->cnx->egl.eglBindTexImage(
973 dp->disp.dpy, s->surface, buffer);
975 return setError(EGL_BAD_SURFACE, EGL_FALSE);
978 EGLBoolean eglReleaseTexImage(
979 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
983 const egl_display_ptr dp = validate_display(dpy);
984 if (!dp) return EGL_FALSE;
986 SurfaceRef _s(dp.get(), surface);
988 return setError(EGL_BAD_SURFACE, EGL_FALSE);
990 egl_surface_t const * const s = get_surface(surface);
991 if (s->cnx->egl.eglReleaseTexImage) {
992 return s->cnx->egl.eglReleaseTexImage(
993 dp->disp.dpy, s->surface, buffer);
995 return setError(EGL_BAD_SURFACE, EGL_FALSE);
998 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1002 const egl_display_ptr dp = validate_display(dpy);
1003 if (!dp) return EGL_FALSE;
1005 EGLBoolean res = EGL_TRUE;
1006 egl_connection_t* const cnx = &gEGLImpl;
1007 if (cnx->dso && cnx->egl.eglSwapInterval) {
1008 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1015 // ----------------------------------------------------------------------------
1017 // ----------------------------------------------------------------------------
1019 EGLBoolean eglWaitClient(void)
1023 egl_connection_t* const cnx = &gEGLImpl;
1025 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1028 if (cnx->egl.eglWaitClient) {
1029 res = cnx->egl.eglWaitClient();
1031 res = cnx->egl.eglWaitGL();
1036 EGLBoolean eglBindAPI(EGLenum api)
1040 if (egl_init_drivers() == EGL_FALSE) {
1041 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1044 // bind this API on all EGLs
1045 EGLBoolean res = EGL_TRUE;
1046 egl_connection_t* const cnx = &gEGLImpl;
1047 if (cnx->dso && cnx->egl.eglBindAPI) {
1048 res = cnx->egl.eglBindAPI(api);
1053 EGLenum eglQueryAPI(void)
1057 if (egl_init_drivers() == EGL_FALSE) {
1058 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1061 egl_connection_t* const cnx = &gEGLImpl;
1062 if (cnx->dso && cnx->egl.eglQueryAPI) {
1063 return cnx->egl.eglQueryAPI();
1066 // or, it can only be OpenGL ES
1067 return EGL_OPENGL_ES_API;
1070 EGLBoolean eglReleaseThread(void)
1074 // If there is context bound to the thread, release it
1075 egl_display_t::loseCurrent(get_context(getContext()));
1077 egl_connection_t* const cnx = &gEGLImpl;
1078 if (cnx->dso && cnx->egl.eglReleaseThread) {
1079 cnx->egl.eglReleaseThread();
1082 egl_tls_t::clearTLS();
1084 if (gEGLDebugLevel > 0)
1085 GLTrace_eglReleaseThread();
1090 EGLSurface eglCreatePbufferFromClientBuffer(
1091 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1092 EGLConfig config, const EGLint *attrib_list)
1096 egl_connection_t* cnx = NULL;
1097 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1098 if (!dp) return EGL_FALSE;
1099 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1100 return cnx->egl.eglCreatePbufferFromClientBuffer(
1101 dp->disp.dpy, buftype, buffer, config, attrib_list);
1103 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1106 // ----------------------------------------------------------------------------
1107 // EGL_EGLEXT_VERSION 3
1108 // ----------------------------------------------------------------------------
1110 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1111 const EGLint *attrib_list)
1115 const egl_display_ptr dp = validate_display(dpy);
1116 if (!dp) return EGL_FALSE;
1118 SurfaceRef _s(dp.get(), surface);
1120 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1122 egl_surface_t const * const s = get_surface(surface);
1123 if (s->cnx->egl.eglLockSurfaceKHR) {
1124 return s->cnx->egl.eglLockSurfaceKHR(
1125 dp->disp.dpy, s->surface, attrib_list);
1127 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1130 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1134 const egl_display_ptr dp = validate_display(dpy);
1135 if (!dp) return EGL_FALSE;
1137 SurfaceRef _s(dp.get(), surface);
1139 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1141 egl_surface_t const * const s = get_surface(surface);
1142 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1143 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1145 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1148 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1149 EGLClientBuffer buffer, const EGLint *attrib_list)
1153 const egl_display_ptr dp = validate_display(dpy);
1154 if (!dp) return EGL_NO_IMAGE_KHR;
1156 ContextRef _c(dp.get(), ctx);
1157 egl_context_t * const c = _c.get();
1159 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1160 egl_connection_t* const cnx = &gEGLImpl;
1161 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1162 result = cnx->egl.eglCreateImageKHR(
1164 c ? c->context : EGL_NO_CONTEXT,
1165 target, buffer, attrib_list);
1170 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1174 const egl_display_ptr dp = validate_display(dpy);
1175 if (!dp) return EGL_FALSE;
1177 EGLBoolean result = EGL_FALSE;
1178 egl_connection_t* const cnx = &gEGLImpl;
1179 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1180 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1185 // ----------------------------------------------------------------------------
1186 // EGL_EGLEXT_VERSION 5
1187 // ----------------------------------------------------------------------------
1190 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1194 const egl_display_ptr dp = validate_display(dpy);
1195 if (!dp) return EGL_NO_SYNC_KHR;
1197 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1198 egl_connection_t* const cnx = &gEGLImpl;
1199 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1200 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1205 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1209 const egl_display_ptr dp = validate_display(dpy);
1210 if (!dp) return EGL_FALSE;
1212 EGLBoolean result = EGL_FALSE;
1213 egl_connection_t* const cnx = &gEGLImpl;
1214 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1215 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1220 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1221 EGLint flags, EGLTimeKHR timeout)
1225 const egl_display_ptr dp = validate_display(dpy);
1226 if (!dp) return EGL_FALSE;
1228 EGLBoolean result = EGL_FALSE;
1229 egl_connection_t* const cnx = &gEGLImpl;
1230 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1231 result = cnx->egl.eglClientWaitSyncKHR(
1232 dp->disp.dpy, sync, flags, timeout);
1237 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1238 EGLint attribute, EGLint *value)
1242 const egl_display_ptr dp = validate_display(dpy);
1243 if (!dp) return EGL_FALSE;
1245 EGLBoolean result = EGL_FALSE;
1246 egl_connection_t* const cnx = &gEGLImpl;
1247 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1248 result = cnx->egl.eglGetSyncAttribKHR(
1249 dp->disp.dpy, sync, attribute, value);
1254 // ----------------------------------------------------------------------------
1255 // ANDROID extensions
1256 // ----------------------------------------------------------------------------
1258 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1262 const egl_display_ptr dp = validate_display(dpy);
1263 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1265 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1266 egl_connection_t* const cnx = &gEGLImpl;
1267 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1268 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1273 EGLint eglWaitSyncANDROID(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags)
1277 const egl_display_ptr dp = validate_display(dpy);
1278 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1280 EGLint result = EGL_FALSE;
1281 egl_connection_t* const cnx = &gEGLImpl;
1282 if (cnx->dso && cnx->egl.eglWaitSyncANDROID) {
1283 result = cnx->egl.eglWaitSyncANDROID(dp->disp.dpy, sync, flags);
1288 // ----------------------------------------------------------------------------
1289 // NVIDIA extensions
1290 // ----------------------------------------------------------------------------
1291 EGLuint64NV eglGetSystemTimeFrequencyNV()
1295 if (egl_init_drivers() == EGL_FALSE) {
1296 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1299 EGLuint64NV ret = 0;
1300 egl_connection_t* const cnx = &gEGLImpl;
1302 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1303 return cnx->egl.eglGetSystemTimeFrequencyNV();
1306 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
1309 EGLuint64NV eglGetSystemTimeNV()
1313 if (egl_init_drivers() == EGL_FALSE) {
1314 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1317 EGLuint64NV ret = 0;
1318 egl_connection_t* const cnx = &gEGLImpl;
1320 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1321 return cnx->egl.eglGetSystemTimeNV();
1324 return setErrorQuiet(EGL_BAD_DISPLAY, 0);