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 if (!ContextRef(dp.get(), share_list).get()) {
445 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
447 egl_context_t* const c = get_context(share_list);
448 share_list = c->context;
450 EGLContext context = cnx->egl.eglCreateContext(
451 dp->disp.dpy, config, share_list, attrib_list);
452 if (context != EGL_NO_CONTEXT) {
453 // figure out if it's a GLESv1 or GLESv2
456 while (*attrib_list != EGL_NONE) {
457 GLint attr = *attrib_list++;
458 GLint value = *attrib_list++;
459 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
461 version = egl_connection_t::GLESv1_INDEX;
462 } else if (value == 2) {
463 version = egl_connection_t::GLESv2_INDEX;
468 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
471 if (gEGLDebugLevel > 0)
472 GLTrace_eglCreateContext(version, c);
476 EGLint error = eglGetError();
477 ALOGE_IF(error == EGL_SUCCESS,
478 "eglCreateContext(%p, %p, %p, %p) returned EGL_NO_CONTEXT "
480 dpy, config, share_list, attrib_list);
483 return EGL_NO_CONTEXT;
486 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
490 const egl_display_ptr dp = validate_display(dpy);
494 ContextRef _c(dp.get(), ctx);
496 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
498 egl_context_t * const c = get_context(ctx);
499 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
500 if (result == EGL_TRUE) {
506 EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
507 EGLSurface read, EGLContext ctx)
511 egl_display_ptr dp = validate_display(dpy);
512 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
514 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
515 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
516 // a valid but uninitialized display.
517 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
518 (draw != EGL_NO_SURFACE) ) {
519 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
522 // get a reference to the object passed in
523 ContextRef _c(dp.get(), ctx);
524 SurfaceRef _d(dp.get(), draw);
525 SurfaceRef _r(dp.get(), read);
527 // validate the context (if not EGL_NO_CONTEXT)
528 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
529 // EGL_NO_CONTEXT is valid
530 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
533 // these are the underlying implementation's object
534 EGLContext impl_ctx = EGL_NO_CONTEXT;
535 EGLSurface impl_draw = EGL_NO_SURFACE;
536 EGLSurface impl_read = EGL_NO_SURFACE;
538 // these are our objects structs passed in
539 egl_context_t * c = NULL;
540 egl_surface_t const * d = NULL;
541 egl_surface_t const * r = NULL;
543 // these are the current objects structs
544 egl_context_t * cur_c = get_context(getContext());
546 if (ctx != EGL_NO_CONTEXT) {
547 c = get_context(ctx);
548 impl_ctx = c->context;
550 // no context given, use the implementation of the current context
551 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
552 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
553 return setError(EGL_BAD_MATCH, EGL_FALSE);
556 // no current context
557 // not an error, there is just no current context.
562 // retrieve the underlying implementation's draw EGLSurface
563 if (draw != EGL_NO_SURFACE) {
564 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
565 d = get_surface(draw);
566 impl_draw = d->surface;
569 // retrieve the underlying implementation's read EGLSurface
570 if (read != EGL_NO_SURFACE) {
571 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
572 r = get_surface(read);
573 impl_read = r->surface;
577 EGLBoolean result = dp->makeCurrent(c, cur_c,
579 impl_draw, impl_read, impl_ctx);
581 if (result == EGL_TRUE) {
583 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
584 egl_tls_t::setContext(ctx);
586 if (gEGLDebugLevel > 0)
587 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
593 setGLHooksThreadSpecific(&gHooksNoContext);
594 egl_tls_t::setContext(EGL_NO_CONTEXT);
597 // this will ALOGE the error
598 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
604 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
605 EGLint attribute, EGLint *value)
609 const egl_display_ptr dp = validate_display(dpy);
610 if (!dp) return EGL_FALSE;
612 ContextRef _c(dp.get(), ctx);
613 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
615 egl_context_t * const c = get_context(ctx);
616 return c->cnx->egl.eglQueryContext(
617 dp->disp.dpy, c->context, attribute, value);
621 EGLContext eglGetCurrentContext(void)
623 // could be called before eglInitialize(), but we wouldn't have a context
624 // then, and this function would correctly return EGL_NO_CONTEXT.
628 EGLContext ctx = getContext();
632 EGLSurface eglGetCurrentSurface(EGLint readdraw)
634 // could be called before eglInitialize(), but we wouldn't have a context
635 // then, and this function would correctly return EGL_NO_SURFACE.
639 EGLContext ctx = getContext();
641 egl_context_t const * const c = get_context(ctx);
642 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
644 case EGL_READ: return c->read;
645 case EGL_DRAW: return c->draw;
646 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
649 return EGL_NO_SURFACE;
652 EGLDisplay eglGetCurrentDisplay(void)
654 // could be called before eglInitialize(), but we wouldn't have a context
655 // then, and this function would correctly return EGL_NO_DISPLAY.
659 EGLContext ctx = getContext();
661 egl_context_t const * const c = get_context(ctx);
662 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
665 return EGL_NO_DISPLAY;
668 EGLBoolean eglWaitGL(void)
672 egl_connection_t* const cnx = &gEGLImpl;
674 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
676 return cnx->egl.eglWaitGL();
679 EGLBoolean eglWaitNative(EGLint engine)
683 egl_connection_t* const cnx = &gEGLImpl;
685 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
687 return cnx->egl.eglWaitNative(engine);
690 EGLint eglGetError(void)
692 EGLint err = EGL_SUCCESS;
693 egl_connection_t* const cnx = &gEGLImpl;
695 err = cnx->egl.eglGetError();
697 if (err == EGL_SUCCESS) {
698 err = egl_tls_t::getError();
703 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
705 // eglGetProcAddress() could be the very first function called
706 // in which case we must make sure we've initialized ourselves, this
707 // happens the first time egl_get_display() is called.
711 if (egl_init_drivers() == EGL_FALSE) {
712 setError(EGL_BAD_PARAMETER, NULL);
716 // These extensions should not be exposed to applications. They're used
717 // internally by the Android EGL layer.
718 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") ||
719 !strcmp(procname, "eglDupNativeFenceFDANDROID") ||
720 !strcmp(procname, "eglWaitSyncANDROID") ||
721 !strcmp(procname, "eglHibernateProcessIMG") ||
722 !strcmp(procname, "eglAwakenProcessIMG")) {
726 __eglMustCastToProperFunctionPointerType addr;
727 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
728 if (addr) return addr;
731 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
732 pthread_mutex_lock(&sExtensionMapMutex);
735 * Since eglGetProcAddress() is not associated to anything, it needs
736 * to return a function pointer that "works" regardless of what
737 * the current context is.
739 * For this reason, we return a "forwarder", a small stub that takes
740 * care of calling the function associated with the context
743 * We first look for extensions we've already resolved, if we're seeing
744 * this extension for the first time, we go through all our
745 * implementations and call eglGetProcAddress() and record the
746 * result in the appropriate implementation hooks and return the
747 * address of the forwarder corresponding to that hook set.
751 const String8 name(procname);
752 addr = sGLExtentionMap.valueFor(name);
753 const int slot = sGLExtentionSlot;
755 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
756 "no more slots for eglGetProcAddress(\"%s\")",
760 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
763 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
766 egl_connection_t* const cnx = &gEGLImpl;
767 if (cnx->dso && cnx->egl.eglGetProcAddress) {
768 // Extensions are independent of the bound context
770 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
771 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
773 debugHooks->ext.extensions[slot] =
774 gHooksTrace.ext.extensions[slot] =
776 cnx->egl.eglGetProcAddress(procname);
777 if (addr) found = true;
782 addr = gExtensionForwarders[slot];
784 sGLExtentionMap.add(name, addr);
789 pthread_mutex_unlock(&sExtensionMapMutex);
793 class FrameCompletionThread : public Thread {
796 static void queueSync(EGLSyncKHR sync) {
797 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
798 static bool running = false;
800 thread->run("GPUFrameCompletion");
804 Mutex::Autolock lock(thread->mMutex);
805 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
806 thread->mFramesQueued).string());
807 thread->mQueue.push_back(sync);
808 thread->mCondition.signal();
809 thread->mFramesQueued++;
810 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
815 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
817 virtual bool threadLoop() {
821 Mutex::Autolock lock(mMutex);
822 while (mQueue.isEmpty()) {
823 mCondition.wait(mMutex);
826 frameNum = mFramesCompleted;
828 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
830 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
832 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
833 if (result == EGL_FALSE) {
834 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
835 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
836 ALOGE("FrameCompletion: timeout waiting for fence");
838 eglDestroySyncKHR(dpy, sync);
841 Mutex::Autolock lock(mMutex);
844 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
849 uint32_t mFramesQueued;
850 uint32_t mFramesCompleted;
851 Vector<EGLSyncKHR> mQueue;
852 Condition mCondition;
856 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
861 const egl_display_ptr dp = validate_display(dpy);
862 if (!dp) return EGL_FALSE;
864 SurfaceRef _s(dp.get(), draw);
866 return setError(EGL_BAD_SURFACE, EGL_FALSE);
869 if (gEGLDebugLevel > 0)
870 GLTrace_eglSwapBuffers(dpy, draw);
873 egl_surface_t const * const s = get_surface(draw);
875 if (CC_UNLIKELY(dp->finishOnSwap)) {
877 egl_context_t * const c = get_context( egl_tls_t::getContext() );
879 // glReadPixels() ensures that the frame is complete
880 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
881 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
885 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
887 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
888 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
890 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
892 if (sync != EGL_NO_SYNC_KHR) {
893 FrameCompletionThread::queueSync(sync);
900 EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
901 NativePixmapType target)
905 const egl_display_ptr dp = validate_display(dpy);
906 if (!dp) return EGL_FALSE;
908 SurfaceRef _s(dp.get(), surface);
910 return setError(EGL_BAD_SURFACE, EGL_FALSE);
912 egl_surface_t const * const s = get_surface(surface);
913 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
916 const char* eglQueryString(EGLDisplay dpy, EGLint name)
920 const egl_display_ptr dp = validate_display(dpy);
921 if (!dp) return (const char *) NULL;
925 return dp->getVendorString();
927 return dp->getVersionString();
929 return dp->getExtensionString();
930 case EGL_CLIENT_APIS:
931 return dp->getClientApiString();
932 case EGL_VERSION_HW_ANDROID:
933 return dp->disp.queryString.version;
935 return setError(EGL_BAD_PARAMETER, (const char *)0);
939 // ----------------------------------------------------------------------------
941 // ----------------------------------------------------------------------------
943 EGLBoolean eglSurfaceAttrib(
944 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
948 const egl_display_ptr dp = validate_display(dpy);
949 if (!dp) return EGL_FALSE;
951 SurfaceRef _s(dp.get(), surface);
953 return setError(EGL_BAD_SURFACE, EGL_FALSE);
955 egl_surface_t const * const s = get_surface(surface);
956 if (s->cnx->egl.eglSurfaceAttrib) {
957 return s->cnx->egl.eglSurfaceAttrib(
958 dp->disp.dpy, s->surface, attribute, value);
960 return setError(EGL_BAD_SURFACE, EGL_FALSE);
963 EGLBoolean eglBindTexImage(
964 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
968 const egl_display_ptr dp = validate_display(dpy);
969 if (!dp) return EGL_FALSE;
971 SurfaceRef _s(dp.get(), surface);
973 return setError(EGL_BAD_SURFACE, EGL_FALSE);
975 egl_surface_t const * const s = get_surface(surface);
976 if (s->cnx->egl.eglBindTexImage) {
977 return s->cnx->egl.eglBindTexImage(
978 dp->disp.dpy, s->surface, buffer);
980 return setError(EGL_BAD_SURFACE, EGL_FALSE);
983 EGLBoolean eglReleaseTexImage(
984 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
988 const egl_display_ptr dp = validate_display(dpy);
989 if (!dp) return EGL_FALSE;
991 SurfaceRef _s(dp.get(), surface);
993 return setError(EGL_BAD_SURFACE, EGL_FALSE);
995 egl_surface_t const * const s = get_surface(surface);
996 if (s->cnx->egl.eglReleaseTexImage) {
997 return s->cnx->egl.eglReleaseTexImage(
998 dp->disp.dpy, s->surface, buffer);
1000 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1003 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1007 const egl_display_ptr dp = validate_display(dpy);
1008 if (!dp) return EGL_FALSE;
1010 EGLBoolean res = EGL_TRUE;
1011 egl_connection_t* const cnx = &gEGLImpl;
1012 if (cnx->dso && cnx->egl.eglSwapInterval) {
1013 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1020 // ----------------------------------------------------------------------------
1022 // ----------------------------------------------------------------------------
1024 EGLBoolean eglWaitClient(void)
1028 egl_connection_t* const cnx = &gEGLImpl;
1030 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1033 if (cnx->egl.eglWaitClient) {
1034 res = cnx->egl.eglWaitClient();
1036 res = cnx->egl.eglWaitGL();
1041 EGLBoolean eglBindAPI(EGLenum api)
1045 if (egl_init_drivers() == EGL_FALSE) {
1046 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1049 // bind this API on all EGLs
1050 EGLBoolean res = EGL_TRUE;
1051 egl_connection_t* const cnx = &gEGLImpl;
1052 if (cnx->dso && cnx->egl.eglBindAPI) {
1053 res = cnx->egl.eglBindAPI(api);
1058 EGLenum eglQueryAPI(void)
1062 if (egl_init_drivers() == EGL_FALSE) {
1063 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1066 egl_connection_t* const cnx = &gEGLImpl;
1067 if (cnx->dso && cnx->egl.eglQueryAPI) {
1068 return cnx->egl.eglQueryAPI();
1071 // or, it can only be OpenGL ES
1072 return EGL_OPENGL_ES_API;
1075 EGLBoolean eglReleaseThread(void)
1079 // If there is context bound to the thread, release it
1080 egl_display_t::loseCurrent(get_context(getContext()));
1082 egl_connection_t* const cnx = &gEGLImpl;
1083 if (cnx->dso && cnx->egl.eglReleaseThread) {
1084 cnx->egl.eglReleaseThread();
1087 egl_tls_t::clearTLS();
1089 if (gEGLDebugLevel > 0)
1090 GLTrace_eglReleaseThread();
1095 EGLSurface eglCreatePbufferFromClientBuffer(
1096 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1097 EGLConfig config, const EGLint *attrib_list)
1101 egl_connection_t* cnx = NULL;
1102 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1103 if (!dp) return EGL_FALSE;
1104 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1105 return cnx->egl.eglCreatePbufferFromClientBuffer(
1106 dp->disp.dpy, buftype, buffer, config, attrib_list);
1108 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1111 // ----------------------------------------------------------------------------
1112 // EGL_EGLEXT_VERSION 3
1113 // ----------------------------------------------------------------------------
1115 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1116 const EGLint *attrib_list)
1120 const egl_display_ptr dp = validate_display(dpy);
1121 if (!dp) return EGL_FALSE;
1123 SurfaceRef _s(dp.get(), surface);
1125 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1127 egl_surface_t const * const s = get_surface(surface);
1128 if (s->cnx->egl.eglLockSurfaceKHR) {
1129 return s->cnx->egl.eglLockSurfaceKHR(
1130 dp->disp.dpy, s->surface, attrib_list);
1132 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1135 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1139 const egl_display_ptr dp = validate_display(dpy);
1140 if (!dp) return EGL_FALSE;
1142 SurfaceRef _s(dp.get(), surface);
1144 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1146 egl_surface_t const * const s = get_surface(surface);
1147 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1148 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1150 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1153 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1154 EGLClientBuffer buffer, const EGLint *attrib_list)
1158 const egl_display_ptr dp = validate_display(dpy);
1159 if (!dp) return EGL_NO_IMAGE_KHR;
1161 ContextRef _c(dp.get(), ctx);
1162 egl_context_t * const c = _c.get();
1164 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1165 egl_connection_t* const cnx = &gEGLImpl;
1166 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1167 result = cnx->egl.eglCreateImageKHR(
1169 c ? c->context : EGL_NO_CONTEXT,
1170 target, buffer, attrib_list);
1175 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1179 const egl_display_ptr dp = validate_display(dpy);
1180 if (!dp) return EGL_FALSE;
1182 EGLBoolean result = EGL_FALSE;
1183 egl_connection_t* const cnx = &gEGLImpl;
1184 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1185 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1190 // ----------------------------------------------------------------------------
1191 // EGL_EGLEXT_VERSION 5
1192 // ----------------------------------------------------------------------------
1195 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1199 const egl_display_ptr dp = validate_display(dpy);
1200 if (!dp) return EGL_NO_SYNC_KHR;
1202 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1203 egl_connection_t* const cnx = &gEGLImpl;
1204 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1205 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1210 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1214 const egl_display_ptr dp = validate_display(dpy);
1215 if (!dp) return EGL_FALSE;
1217 EGLBoolean result = EGL_FALSE;
1218 egl_connection_t* const cnx = &gEGLImpl;
1219 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1220 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1225 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1226 EGLint flags, EGLTimeKHR timeout)
1230 const egl_display_ptr dp = validate_display(dpy);
1231 if (!dp) return EGL_FALSE;
1233 EGLBoolean result = EGL_FALSE;
1234 egl_connection_t* const cnx = &gEGLImpl;
1235 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1236 result = cnx->egl.eglClientWaitSyncKHR(
1237 dp->disp.dpy, sync, flags, timeout);
1242 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1243 EGLint attribute, EGLint *value)
1247 const egl_display_ptr dp = validate_display(dpy);
1248 if (!dp) return EGL_FALSE;
1250 EGLBoolean result = EGL_FALSE;
1251 egl_connection_t* const cnx = &gEGLImpl;
1252 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1253 result = cnx->egl.eglGetSyncAttribKHR(
1254 dp->disp.dpy, sync, attribute, value);
1259 // ----------------------------------------------------------------------------
1260 // ANDROID extensions
1261 // ----------------------------------------------------------------------------
1263 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1267 const egl_display_ptr dp = validate_display(dpy);
1268 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1270 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1271 egl_connection_t* const cnx = &gEGLImpl;
1272 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1273 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1278 EGLint eglWaitSyncANDROID(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags)
1282 const egl_display_ptr dp = validate_display(dpy);
1283 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1285 EGLint result = EGL_FALSE;
1286 egl_connection_t* const cnx = &gEGLImpl;
1287 if (cnx->dso && cnx->egl.eglWaitSyncANDROID) {
1288 result = cnx->egl.eglWaitSyncANDROID(dp->disp.dpy, sync, flags);
1293 // ----------------------------------------------------------------------------
1294 // NVIDIA extensions
1295 // ----------------------------------------------------------------------------
1296 EGLuint64NV eglGetSystemTimeFrequencyNV()
1300 if (egl_init_drivers() == EGL_FALSE) {
1301 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1304 EGLuint64NV ret = 0;
1305 egl_connection_t* const cnx = &gEGLImpl;
1307 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1308 return cnx->egl.eglGetSystemTimeFrequencyNV();
1311 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
1314 EGLuint64NV eglGetSystemTimeNV()
1318 if (egl_init_drivers() == EGL_FALSE) {
1319 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1322 EGLuint64NV ret = 0;
1323 egl_connection_t* const cnx = &gEGLImpl;
1325 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1326 return cnx->egl.eglGetSystemTimeNV();
1329 return setErrorQuiet(EGL_BAD_DISPLAY, 0);