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.
21 #include <hardware/gralloc.h>
22 #include <system/window.h>
25 #include <EGL/eglext.h>
27 #include <GLES/glext.h>
29 #include <cutils/log.h>
30 #include <cutils/atomic.h>
31 #include <cutils/properties.h>
32 #include <cutils/memory.h>
34 #include <utils/KeyedVector.h>
35 #include <utils/SortedVector.h>
36 #include <utils/String8.h>
40 #include "glesv2dbg.h"
43 #include "egl_display.h"
45 #include "egl_object.h"
48 using namespace android;
50 // ----------------------------------------------------------------------------
52 static char const * const sVendorString = "Android";
53 static char const * const sVersionString = "1.4 Android META-EGL";
54 static char const * const sClientApiString = "OpenGL ES";
55 static char const * const sExtensionString =
58 "EGL_KHR_image_pixmap "
59 "EGL_KHR_gl_texture_2D_image "
60 "EGL_KHR_gl_texture_cubemap_image "
61 "EGL_KHR_gl_renderbuffer_image "
63 "EGL_ANDROID_image_native_buffer "
64 "EGL_ANDROID_swap_rectangle "
68 struct extention_map_t {
70 __eglMustCastToProperFunctionPointerType address;
73 static const extention_map_t sExtentionMap[] = {
74 { "eglLockSurfaceKHR",
75 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
76 { "eglUnlockSurfaceKHR",
77 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
78 { "eglCreateImageKHR",
79 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
80 { "eglDestroyImageKHR",
81 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
82 { "eglSetSwapRectangleANDROID",
83 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
84 { "eglGetSystemTimeFrequencyNV",
85 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
86 { "eglGetSystemTimeNV",
87 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
90 // accesses protected by sExtensionMapMutex
91 static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
92 static int sGLExtentionSlot = 0;
93 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
95 static void(*findProcAddress(const char* name,
96 const extention_map_t* map, size_t n))() {
97 for (uint32_t i=0 ; i<n ; i++) {
98 if (!strcmp(name, map[i].name)) {
99 return map[i].address;
105 // ----------------------------------------------------------------------------
108 static __attribute__((noinline))
109 int binarySearch(T const sortedArray[], int first, int last, T key) {
110 while (first <= last) {
111 int mid = (first + last) / 2;
112 if (sortedArray[mid] < key) {
114 } else if (key < sortedArray[mid]) {
123 // ----------------------------------------------------------------------------
126 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
127 extern EGLBoolean egl_init_drivers();
128 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
129 extern int gEGLDebugLevel;
130 extern gl_hooks_t gHooksTrace;
131 extern gl_hooks_t gHooksDebug;
132 } // namespace android;
134 // ----------------------------------------------------------------------------
136 static inline void clearError() { egl_tls_t::clearError(); }
137 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
139 // ----------------------------------------------------------------------------
141 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
145 uint32_t index = uint32_t(display);
146 if (index >= NUM_DISPLAYS) {
147 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
150 if (egl_init_drivers() == EGL_FALSE) {
151 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
154 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
166 egl_display_t * const dp = get_display(dpy);
167 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
169 EGLBoolean res = dp->initialize(major, minor);
174 EGLBoolean eglTerminate(EGLDisplay dpy)
176 // NOTE: don't unload the drivers b/c some APIs can be called
177 // after eglTerminate() has been called. eglTerminate() only
178 // terminates an EGLDisplay, not a EGL itself.
182 egl_display_t* const dp = get_display(dpy);
183 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
185 EGLBoolean res = dp->terminate();
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 EGLBoolean eglGetConfigs( EGLDisplay dpy,
196 EGLint config_size, EGLint *num_config)
200 egl_display_t const * const dp = validate_display(dpy);
201 if (!dp) return EGL_FALSE;
203 GLint numConfigs = dp->numTotalConfigs;
205 *num_config = numConfigs;
210 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
211 *configs++ = EGLConfig(i);
220 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
221 EGLConfig *configs, EGLint config_size,
226 egl_display_t const * const dp = validate_display(dpy);
227 if (!dp) return EGL_FALSE;
230 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
234 EGLBoolean res = EGL_FALSE;
238 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
239 // to do this, we have to go through the attrib_list array once
240 // to figure out both its size and if it contains an EGL_CONFIG_ID
241 // key. If so, the full array is copied and patched.
242 // NOTE: we assume that there can be only one occurrence
245 EGLint patch_index = -1;
249 while ((attr=attrib_list[size]) != EGL_NONE) {
250 if (attr == EGL_CONFIG_ID)
255 if (patch_index >= 0) {
256 size += 2; // we need copy the sentinel as well
257 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
259 return setError(EGL_BAD_ALLOC, EGL_FALSE);
260 memcpy(new_list, attrib_list, size*sizeof(EGLint));
262 // patch the requested EGL_CONFIG_ID
264 EGLConfig ourConfig(0);
265 EGLint& configId(new_list[patch_index+1]);
266 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
267 if (dp->configs[i].configId == configId) {
268 ourConfig = EGLConfig(i);
269 configId = dp->configs[i].implConfigId;
275 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
276 if (found && cnx->dso) {
277 // and switch to the new list
278 attrib_list = const_cast<const EGLint *>(new_list);
280 // At this point, the only configuration that can match is
281 // dp->configs[i][index], however, we don't know if it would be
282 // rejected because of the other attributes, so we do have to call
283 // cnx->egl.eglChooseConfig() -- but we don't have to loop
284 // through all the EGLimpl[].
285 // We also know we can only get a single config back, and we know
288 res = cnx->egl.eglChooseConfig(
289 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
290 attrib_list, configs, config_size, &n);
292 // n has to be 0 or 1, by construction, and we already know
293 // which config it will return (since there can be only one).
295 configs[0] = ourConfig;
301 free(const_cast<EGLint *>(attrib_list));
306 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
307 egl_connection_t* const cnx = &gEGLImpl[i];
309 if (cnx->egl.eglChooseConfig(
310 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
312 // now we need to convert these client EGLConfig to our
313 // internal EGLConfig format.
314 // This is done in O(n Log(n)) time.
315 for (int j=0 ; j<n ; j++) {
316 egl_config_t key(i, configs[j]);
317 intptr_t index = binarySearch<egl_config_t>(
318 dp->configs, 0, dp->numTotalConfigs, key);
320 configs[j] = EGLConfig(index);
322 return setError(EGL_BAD_CONFIG, EGL_FALSE);
336 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
337 EGLint attribute, EGLint *value)
341 egl_display_t const* dp = 0;
342 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
343 if (!cnx) return EGL_FALSE;
345 if (attribute == EGL_CONFIG_ID) {
346 *value = dp->configs[intptr_t(config)].configId;
349 return cnx->egl.eglGetConfigAttrib(
350 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
351 dp->configs[intptr_t(config)].config, attribute, value);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
359 NativeWindowType window,
360 const EGLint *attrib_list)
364 egl_display_t const* dp = 0;
365 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
367 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
368 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
371 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
372 LOGE("EGLNativeWindowType %p already connected to another API",
374 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
377 // set the native window's buffers format to match this config
378 if (cnx->egl.eglGetConfigAttrib(iDpy,
379 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
381 int err = native_window_set_buffers_format(window, format);
383 LOGE("error setting native window pixel format: %s (%d)",
384 strerror(-err), err);
385 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
386 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
391 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
392 iDpy, iConfig, window, attrib_list);
393 if (surface != EGL_NO_SURFACE) {
394 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
395 dp->configs[intptr_t(config)].impl, cnx);
399 // EGLSurface creation failed
400 native_window_set_buffers_format(window, 0);
401 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
403 return EGL_NO_SURFACE;
406 EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
407 NativePixmapType pixmap,
408 const EGLint *attrib_list)
412 egl_display_t const* dp = 0;
413 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
415 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
416 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
417 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
418 if (surface != EGL_NO_SURFACE) {
419 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
420 dp->configs[intptr_t(config)].impl, cnx);
424 return EGL_NO_SURFACE;
427 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
428 const EGLint *attrib_list)
432 egl_display_t const* dp = 0;
433 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
435 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
436 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
437 dp->configs[intptr_t(config)].config, attrib_list);
438 if (surface != EGL_NO_SURFACE) {
439 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
440 dp->configs[intptr_t(config)].impl, cnx);
444 return EGL_NO_SURFACE;
447 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
451 egl_display_t const * const dp = validate_display(dpy);
452 if (!dp) return EGL_FALSE;
454 SurfaceRef _s(surface);
456 return setError(EGL_BAD_SURFACE, EGL_FALSE);
458 egl_surface_t * const s = get_surface(surface);
459 EGLBoolean result = s->cnx->egl.eglDestroySurface(
460 dp->disp[s->impl].dpy, s->surface);
461 if (result == EGL_TRUE) {
467 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
468 EGLint attribute, EGLint *value)
472 egl_display_t const * const dp = validate_display(dpy);
473 if (!dp) return EGL_FALSE;
475 SurfaceRef _s(surface);
477 return setError(EGL_BAD_SURFACE, EGL_FALSE);
479 egl_surface_t const * const s = get_surface(surface);
480 EGLBoolean result(EGL_TRUE);
481 if (attribute == EGL_CONFIG_ID) {
482 // We need to remap EGL_CONFIG_IDs
483 *value = dp->configs[intptr_t(s->config)].configId;
485 result = s->cnx->egl.eglQuerySurface(
486 dp->disp[s->impl].dpy, s->surface, attribute, value);
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
497 EGLContext share_list, const EGLint *attrib_list)
501 egl_display_t const* dp = 0;
502 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
504 if (share_list != EGL_NO_CONTEXT) {
505 egl_context_t* const c = get_context(share_list);
506 share_list = c->context;
508 EGLContext context = cnx->egl.eglCreateContext(
509 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
510 dp->configs[intptr_t(config)].config,
511 share_list, attrib_list);
512 if (context != EGL_NO_CONTEXT) {
513 // figure out if it's a GLESv1 or GLESv2
516 while (*attrib_list != EGL_NONE) {
517 GLint attr = *attrib_list++;
518 GLint value = *attrib_list++;
519 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
521 version = GLESv1_INDEX;
522 } else if (value == 2) {
523 version = GLESv2_INDEX;
528 egl_context_t* c = new egl_context_t(dpy, context, config,
529 dp->configs[intptr_t(config)].impl, cnx, version);
533 return EGL_NO_CONTEXT;
536 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
540 egl_display_t const * const dp = validate_display(dpy);
546 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
548 egl_context_t * const c = get_context(ctx);
549 EGLBoolean result = c->cnx->egl.eglDestroyContext(
550 dp->disp[c->impl].dpy, c->context);
551 if (result == EGL_TRUE) {
557 static void loseCurrent(egl_context_t * cur_c)
560 egl_surface_t * cur_r = get_surface(cur_c->read);
561 egl_surface_t * cur_d = get_surface(cur_c->draw);
563 // by construction, these are either 0 or valid (possibly terminated)
564 // it should be impossible for these to be invalid
565 ContextRef _cur_c(cur_c);
566 SurfaceRef _cur_r(cur_r);
567 SurfaceRef _cur_d(cur_d);
578 EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
579 EGLSurface read, EGLContext ctx)
583 egl_display_t const * const dp = get_display(dpy);
584 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
586 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
587 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
588 // a valid but uninitialized display.
589 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
590 (draw != EGL_NO_SURFACE) ) {
591 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
594 // get a reference to the object passed in
599 // validate the context (if not EGL_NO_CONTEXT)
600 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
601 // EGL_NO_CONTEXT is valid
605 // these are the underlying implementation's object
606 EGLContext impl_ctx = EGL_NO_CONTEXT;
607 EGLSurface impl_draw = EGL_NO_SURFACE;
608 EGLSurface impl_read = EGL_NO_SURFACE;
610 // these are our objects structs passed in
611 egl_context_t * c = NULL;
612 egl_surface_t const * d = NULL;
613 egl_surface_t const * r = NULL;
615 // these are the current objects structs
616 egl_context_t * cur_c = get_context(getContext());
618 if (ctx != EGL_NO_CONTEXT) {
619 c = get_context(ctx);
620 impl_ctx = c->context;
622 // no context given, use the implementation of the current context
624 // no current context
625 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
626 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
627 return setError(EGL_BAD_MATCH, EGL_FALSE);
629 // not an error, there is just no current context.
634 // retrieve the underlying implementation's draw EGLSurface
635 if (draw != EGL_NO_SURFACE) {
636 d = get_surface(draw);
637 // make sure the EGLContext and EGLSurface passed in are for
639 if (c && d->impl != c->impl)
640 return setError(EGL_BAD_MATCH, EGL_FALSE);
641 impl_draw = d->surface;
644 // retrieve the underlying implementation's read EGLSurface
645 if (read != EGL_NO_SURFACE) {
646 r = get_surface(read);
647 // make sure the EGLContext and EGLSurface passed in are for
649 if (c && r->impl != c->impl)
650 return setError(EGL_BAD_MATCH, EGL_FALSE);
651 impl_read = r->surface;
657 result = c->cnx->egl.eglMakeCurrent(
658 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
660 result = cur_c->cnx->egl.eglMakeCurrent(
661 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
664 if (result == EGL_TRUE) {
668 if (ctx != EGL_NO_CONTEXT) {
669 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
670 egl_tls_t::setContext(ctx);
671 if (gEGLDebugLevel > 0) {
672 CreateDbgContext(c->version, c->cnx->hooks[c->version]);
680 setGLHooksThreadSpecific(&gHooksNoContext);
681 egl_tls_t::setContext(EGL_NO_CONTEXT);
684 // this will LOGE the error
685 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
691 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
692 EGLint attribute, EGLint *value)
696 egl_display_t const * const dp = validate_display(dpy);
697 if (!dp) return EGL_FALSE;
700 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
702 egl_context_t * const c = get_context(ctx);
704 EGLBoolean result(EGL_TRUE);
705 if (attribute == EGL_CONFIG_ID) {
706 *value = dp->configs[intptr_t(c->config)].configId;
708 // We need to remap EGL_CONFIG_IDs
709 result = c->cnx->egl.eglQueryContext(
710 dp->disp[c->impl].dpy, c->context, attribute, value);
716 EGLContext eglGetCurrentContext(void)
718 // could be called before eglInitialize(), but we wouldn't have a context
719 // then, and this function would correctly return EGL_NO_CONTEXT.
723 EGLContext ctx = getContext();
727 EGLSurface eglGetCurrentSurface(EGLint readdraw)
729 // could be called before eglInitialize(), but we wouldn't have a context
730 // then, and this function would correctly return EGL_NO_SURFACE.
734 EGLContext ctx = getContext();
736 egl_context_t const * const c = get_context(ctx);
737 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
739 case EGL_READ: return c->read;
740 case EGL_DRAW: return c->draw;
741 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
744 return EGL_NO_SURFACE;
747 EGLDisplay eglGetCurrentDisplay(void)
749 // could be called before eglInitialize(), but we wouldn't have a context
750 // then, and this function would correctly return EGL_NO_DISPLAY.
754 EGLContext ctx = getContext();
756 egl_context_t const * const c = get_context(ctx);
757 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
760 return EGL_NO_DISPLAY;
763 EGLBoolean eglWaitGL(void)
765 // could be called before eglInitialize(), but we wouldn't have a context
766 // then, and this function would return GL_TRUE, which isn't wrong.
770 EGLBoolean res = EGL_TRUE;
771 EGLContext ctx = getContext();
773 egl_context_t const * const c = get_context(ctx);
774 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
775 if (uint32_t(c->impl)>=2)
776 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
777 egl_connection_t* const cnx = &gEGLImpl[c->impl];
779 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
780 res = cnx->egl.eglWaitGL();
785 EGLBoolean eglWaitNative(EGLint engine)
787 // could be called before eglInitialize(), but we wouldn't have a context
788 // then, and this function would return GL_TRUE, which isn't wrong.
792 EGLBoolean res = EGL_TRUE;
793 EGLContext ctx = getContext();
795 egl_context_t const * const c = get_context(ctx);
796 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
797 if (uint32_t(c->impl)>=2)
798 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
799 egl_connection_t* const cnx = &gEGLImpl[c->impl];
801 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
802 res = cnx->egl.eglWaitNative(engine);
807 EGLint eglGetError(void)
809 EGLint result = EGL_SUCCESS;
811 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
813 egl_connection_t* const cnx = &gEGLImpl[i];
815 err = cnx->egl.eglGetError();
816 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
819 err = egl_tls_t::getError();
820 if (result == EGL_SUCCESS)
825 // Note: Similar implementations of these functions also exist in
826 // gl2.cpp and gl.cpp, and are used by applications that call the
827 // exported entry points directly.
828 typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
829 typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
831 static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
832 static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
834 static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
836 GLeglImageOES implImage =
837 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
838 glEGLImageTargetTexture2DOES_impl(target, implImage);
841 static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
843 GLeglImageOES implImage =
844 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
845 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
848 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
850 // eglGetProcAddress() could be the very first function called
851 // in which case we must make sure we've initialized ourselves, this
852 // happens the first time egl_get_display() is called.
856 if (egl_init_drivers() == EGL_FALSE) {
857 setError(EGL_BAD_PARAMETER, NULL);
861 __eglMustCastToProperFunctionPointerType addr;
862 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
863 if (addr) return addr;
865 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
866 pthread_mutex_lock(&sExtensionMapMutex);
869 * Since eglGetProcAddress() is not associated to anything, it needs
870 * to return a function pointer that "works" regardless of what
871 * the current context is.
873 * For this reason, we return a "forwarder", a small stub that takes
874 * care of calling the function associated with the context
877 * We first look for extensions we've already resolved, if we're seeing
878 * this extension for the first time, we go through all our
879 * implementations and call eglGetProcAddress() and record the
880 * result in the appropriate implementation hooks and return the
881 * address of the forwarder corresponding to that hook set.
885 const String8 name(procname);
886 addr = sGLExtentionMap.valueFor(name);
887 const int slot = sGLExtentionSlot;
889 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
890 "no more slots for eglGetProcAddress(\"%s\")",
893 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
895 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
896 egl_connection_t* const cnx = &gEGLImpl[i];
897 if (cnx->dso && cnx->egl.eglGetProcAddress) {
899 // Extensions are independent of the bound context
900 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
901 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
903 gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
905 cnx->egl.eglGetProcAddress(procname);
909 addr = gExtensionForwarders[slot];
911 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
912 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
913 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
915 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
916 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
917 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
920 sGLExtentionMap.add(name, addr);
925 pthread_mutex_unlock(&sExtensionMapMutex);
929 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
931 EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
932 if (gEGLDebugLevel > 0)
933 Debug_eglSwapBuffers(dpy, draw);
937 egl_display_t const * const dp = validate_display(dpy);
938 if (!dp) return EGL_FALSE;
942 return setError(EGL_BAD_SURFACE, EGL_FALSE);
944 egl_surface_t const * const s = get_surface(draw);
945 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
948 EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
949 NativePixmapType target)
953 egl_display_t const * const dp = validate_display(dpy);
954 if (!dp) return EGL_FALSE;
956 SurfaceRef _s(surface);
958 return setError(EGL_BAD_SURFACE, EGL_FALSE);
960 egl_surface_t const * const s = get_surface(surface);
961 return s->cnx->egl.eglCopyBuffers(
962 dp->disp[s->impl].dpy, s->surface, target);
965 const char* eglQueryString(EGLDisplay dpy, EGLint name)
969 egl_display_t const * const dp = validate_display(dpy);
970 if (!dp) return (const char *) NULL;
974 return sVendorString;
976 return sVersionString;
978 return sExtensionString;
979 case EGL_CLIENT_APIS:
980 return sClientApiString;
982 return setError(EGL_BAD_PARAMETER, (const char *)0);
986 // ----------------------------------------------------------------------------
988 // ----------------------------------------------------------------------------
990 EGLBoolean eglSurfaceAttrib(
991 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
995 egl_display_t const * const dp = validate_display(dpy);
996 if (!dp) return EGL_FALSE;
998 SurfaceRef _s(surface);
1000 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1002 egl_surface_t const * const s = get_surface(surface);
1003 if (s->cnx->egl.eglSurfaceAttrib) {
1004 return s->cnx->egl.eglSurfaceAttrib(
1005 dp->disp[s->impl].dpy, s->surface, attribute, value);
1007 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1010 EGLBoolean eglBindTexImage(
1011 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1015 egl_display_t const * const dp = validate_display(dpy);
1016 if (!dp) return EGL_FALSE;
1018 SurfaceRef _s(surface);
1020 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1022 egl_surface_t const * const s = get_surface(surface);
1023 if (s->cnx->egl.eglBindTexImage) {
1024 return s->cnx->egl.eglBindTexImage(
1025 dp->disp[s->impl].dpy, s->surface, buffer);
1027 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1030 EGLBoolean eglReleaseTexImage(
1031 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1035 egl_display_t const * const dp = validate_display(dpy);
1036 if (!dp) return EGL_FALSE;
1038 SurfaceRef _s(surface);
1040 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1042 egl_surface_t const * const s = get_surface(surface);
1043 if (s->cnx->egl.eglReleaseTexImage) {
1044 return s->cnx->egl.eglReleaseTexImage(
1045 dp->disp[s->impl].dpy, s->surface, buffer);
1047 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1050 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1054 egl_display_t const * const dp = validate_display(dpy);
1055 if (!dp) return EGL_FALSE;
1057 EGLBoolean res = EGL_TRUE;
1058 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1059 egl_connection_t* const cnx = &gEGLImpl[i];
1061 if (cnx->egl.eglSwapInterval) {
1062 if (cnx->egl.eglSwapInterval(
1063 dp->disp[i].dpy, interval) == EGL_FALSE) {
1073 // ----------------------------------------------------------------------------
1075 // ----------------------------------------------------------------------------
1077 EGLBoolean eglWaitClient(void)
1081 // could be called before eglInitialize(), but we wouldn't have a context
1082 // then, and this function would return GL_TRUE, which isn't wrong.
1083 EGLBoolean res = EGL_TRUE;
1084 EGLContext ctx = getContext();
1086 egl_context_t const * const c = get_context(ctx);
1087 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1088 if (uint32_t(c->impl)>=2)
1089 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1090 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1092 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1093 if (cnx->egl.eglWaitClient) {
1094 res = cnx->egl.eglWaitClient();
1096 res = cnx->egl.eglWaitGL();
1102 EGLBoolean eglBindAPI(EGLenum api)
1106 if (egl_init_drivers() == EGL_FALSE) {
1107 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1110 // bind this API on all EGLs
1111 EGLBoolean res = EGL_TRUE;
1112 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1113 egl_connection_t* const cnx = &gEGLImpl[i];
1115 if (cnx->egl.eglBindAPI) {
1116 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
1125 EGLenum eglQueryAPI(void)
1129 if (egl_init_drivers() == EGL_FALSE) {
1130 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1133 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1134 egl_connection_t* const cnx = &gEGLImpl[i];
1136 if (cnx->egl.eglQueryAPI) {
1137 // the first one we find is okay, because they all
1138 // should be the same
1139 return cnx->egl.eglQueryAPI();
1143 // or, it can only be OpenGL ES
1144 return EGL_OPENGL_ES_API;
1147 EGLBoolean eglReleaseThread(void)
1151 // If there is context bound to the thread, release it
1152 loseCurrent(get_context(getContext()));
1154 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1155 egl_connection_t* const cnx = &gEGLImpl[i];
1157 if (cnx->egl.eglReleaseThread) {
1158 cnx->egl.eglReleaseThread();
1162 egl_tls_t::clearTLS();
1167 EGLSurface eglCreatePbufferFromClientBuffer(
1168 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1169 EGLConfig config, const EGLint *attrib_list)
1173 egl_display_t const* dp = 0;
1174 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
1175 if (!cnx) return EGL_FALSE;
1176 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1177 return cnx->egl.eglCreatePbufferFromClientBuffer(
1178 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1180 dp->configs[intptr_t(config)].config, attrib_list);
1182 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1185 // ----------------------------------------------------------------------------
1186 // EGL_EGLEXT_VERSION 3
1187 // ----------------------------------------------------------------------------
1189 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1190 const EGLint *attrib_list)
1194 egl_display_t const * const dp = validate_display(dpy);
1195 if (!dp) return EGL_FALSE;
1197 SurfaceRef _s(surface);
1199 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1201 egl_surface_t const * const s = get_surface(surface);
1202 if (s->cnx->egl.eglLockSurfaceKHR) {
1203 return s->cnx->egl.eglLockSurfaceKHR(
1204 dp->disp[s->impl].dpy, s->surface, attrib_list);
1206 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1209 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1213 egl_display_t const * const dp = validate_display(dpy);
1214 if (!dp) return EGL_FALSE;
1216 SurfaceRef _s(surface);
1218 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1220 egl_surface_t const * const s = get_surface(surface);
1221 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1222 return s->cnx->egl.eglUnlockSurfaceKHR(
1223 dp->disp[s->impl].dpy, s->surface);
1225 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1228 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1229 EGLClientBuffer buffer, const EGLint *attrib_list)
1233 egl_display_t const * const dp = validate_display(dpy);
1234 if (!dp) return EGL_NO_IMAGE_KHR;
1236 if (ctx != EGL_NO_CONTEXT) {
1239 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1240 egl_context_t * const c = get_context(ctx);
1241 // since we have an EGLContext, we know which implementation to use
1242 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
1243 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
1244 if (image == EGL_NO_IMAGE_KHR)
1247 egl_image_t* result = new egl_image_t(dpy, ctx);
1248 result->images[c->impl] = image;
1249 return (EGLImageKHR)result;
1251 // EGL_NO_CONTEXT is a valid parameter
1253 /* Since we don't have a way to know which implementation to call,
1254 * we're calling all of them. If at least one of the implementation
1255 * succeeded, this is a success.
1258 EGLint currentError = eglGetError();
1260 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
1261 bool success = false;
1262 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1263 egl_connection_t* const cnx = &gEGLImpl[i];
1264 implImages[i] = EGL_NO_IMAGE_KHR;
1266 if (cnx->egl.eglCreateImageKHR) {
1267 implImages[i] = cnx->egl.eglCreateImageKHR(
1268 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
1269 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1277 // failure, if there was an error when we entered this function,
1278 // the error flag must not be updated.
1279 // Otherwise, the error is whatever happened in the implementation
1281 if (currentError != EGL_SUCCESS) {
1282 setError(currentError, EGL_NO_IMAGE_KHR);
1284 return EGL_NO_IMAGE_KHR;
1286 // In case of success, we need to clear all error flags
1287 // (especially those caused by the implementation that didn't
1288 // succeed). TODO: we could avoid this if we knew this was
1289 // a "full" success (all implementation succeeded).
1293 egl_image_t* result = new egl_image_t(dpy, ctx);
1294 memcpy(result->images, implImages, sizeof(implImages));
1295 return (EGLImageKHR)result;
1299 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1303 egl_display_t const * const dp = validate_display(dpy);
1304 if (!dp) return EGL_FALSE;
1307 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1309 egl_image_t* image = get_image(img);
1310 bool success = false;
1311 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1312 egl_connection_t* const cnx = &gEGLImpl[i];
1313 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1315 if (cnx->egl.eglDestroyImageKHR) {
1316 if (cnx->egl.eglDestroyImageKHR(
1317 dp->disp[i].dpy, image->images[i])) {
1332 // ----------------------------------------------------------------------------
1333 // EGL_EGLEXT_VERSION 5
1334 // ----------------------------------------------------------------------------
1337 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1341 egl_display_t const * const dp = validate_display(dpy);
1342 if (!dp) return EGL_NO_SYNC_KHR;
1344 EGLContext ctx = eglGetCurrentContext();
1347 return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
1349 egl_context_t * const c = get_context(ctx);
1350 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1351 if (c->cnx->egl.eglCreateSyncKHR) {
1352 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1353 dp->disp[c->impl].dpy, type, attrib_list);
1354 if (sync == EGL_NO_SYNC_KHR)
1356 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1358 return (EGLSyncKHR)result;
1361 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1365 egl_display_t const * const dp = validate_display(dpy);
1366 if (!dp) return EGL_FALSE;
1369 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1370 egl_sync_t* syncObject = get_sync(sync);
1372 EGLContext ctx = syncObject->context;
1375 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1377 EGLBoolean result = EGL_FALSE;
1378 egl_context_t * const c = get_context(ctx);
1379 if (c->cnx->egl.eglDestroySyncKHR) {
1380 result = c->cnx->egl.eglDestroySyncKHR(
1381 dp->disp[c->impl].dpy, syncObject->sync);
1388 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1392 egl_display_t const * const dp = validate_display(dpy);
1393 if (!dp) return EGL_FALSE;
1396 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1397 egl_sync_t* syncObject = get_sync(sync);
1399 EGLContext ctx = syncObject->context;
1402 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1404 egl_context_t * const c = get_context(ctx);
1405 if (c->cnx->egl.eglClientWaitSyncKHR) {
1406 return c->cnx->egl.eglClientWaitSyncKHR(
1407 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
1413 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
1417 egl_display_t const * const dp = validate_display(dpy);
1418 if (!dp) return EGL_FALSE;
1422 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1424 egl_sync_t* syncObject = get_sync(sync);
1425 EGLContext ctx = syncObject->context;
1428 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1430 egl_context_t * const c = get_context(ctx);
1431 if (c->cnx->egl.eglGetSyncAttribKHR) {
1432 return c->cnx->egl.eglGetSyncAttribKHR(
1433 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
1439 // ----------------------------------------------------------------------------
1440 // ANDROID extensions
1441 // ----------------------------------------------------------------------------
1443 EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1444 EGLint left, EGLint top, EGLint width, EGLint height)
1448 egl_display_t const * const dp = validate_display(dpy);
1449 if (!dp) return EGL_FALSE;
1451 SurfaceRef _s(draw);
1453 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1455 egl_surface_t const * const s = get_surface(draw);
1456 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1457 return s->cnx->egl.eglSetSwapRectangleANDROID(
1458 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
1460 return setError(EGL_BAD_DISPLAY, NULL);
1463 // ----------------------------------------------------------------------------
1464 // NVIDIA extensions
1465 // ----------------------------------------------------------------------------
1466 EGLuint64NV eglGetSystemTimeFrequencyNV()
1470 if (egl_init_drivers() == EGL_FALSE) {
1471 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1474 EGLuint64NV ret = 0;
1475 egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE];
1478 if (cnx->egl.eglGetSystemTimeFrequencyNV) {
1479 return cnx->egl.eglGetSystemTimeFrequencyNV();
1483 return setError(EGL_BAD_DISPLAY, 0);;
1486 EGLuint64NV eglGetSystemTimeNV()
1490 if (egl_init_drivers() == EGL_FALSE) {
1491 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1494 EGLuint64NV ret = 0;
1495 egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE];
1498 if (cnx->egl.eglGetSystemTimeNV) {
1499 return cnx->egl.eglGetSystemTimeNV();
1503 return setError(EGL_BAD_DISPLAY, 0);;