egl.cpp revision b519abb606c819c446a408f422530268b21a100b
1/*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17#define LOG_TAG "libEGL"
18
19#include <ctype.h>
20#include <string.h>
21#include <errno.h>
22#include <dlfcn.h>
23
24#include <sys/ioctl.h>
25
26#if HAVE_ANDROID_OS
27#include <linux/android_pmem.h>
28#endif
29
30#include <EGL/egl.h>
31#include <EGL/eglext.h>
32#include <GLES/gl.h>
33#include <GLES/glext.h>
34
35#include <cutils/log.h>
36#include <cutils/atomic.h>
37#include <cutils/properties.h>
38#include <cutils/memory.h>
39
40#include <utils/RefBase.h>
41
42#include "hooks.h"
43#include "egl_impl.h"
44
45
46#define MAKE_CONFIG(_impl, _index)  ((EGLConfig)(((_impl)<<24) | (_index)))
47#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
48
49// ----------------------------------------------------------------------------
50namespace android {
51// ----------------------------------------------------------------------------
52
53#define VERSION_MAJOR 1
54#define VERSION_MINOR 4
55static char const * const gVendorString     = "Android";
56static char const * const gVersionString    = "1.31 Android META-EGL";
57static char const * const gClientApiString  = "OpenGL ES";
58static char const * const gExtensionString  = "";
59
60template <int MAGIC>
61struct egl_object_t
62{
63    egl_object_t() : magic(MAGIC) { }
64    ~egl_object_t() { magic = 0; }
65    bool isValid() const { return magic == MAGIC; }
66private:
67    uint32_t    magic;
68};
69
70struct egl_display_t : public egl_object_t<'_dpy'>
71{
72    EGLDisplay  dpys[IMPL_NUM_DRIVERS_IMPLEMENTATIONS];
73    EGLConfig*  configs[IMPL_NUM_DRIVERS_IMPLEMENTATIONS];
74    EGLint      numConfigs[IMPL_NUM_DRIVERS_IMPLEMENTATIONS];
75    EGLint      numTotalConfigs;
76    char const* extensionsString;
77    volatile int32_t refs;
78    struct strings_t {
79        char const * vendor;
80        char const * version;
81        char const * clientApi;
82        char const * extensions;
83    };
84    strings_t   queryString[IMPL_NUM_DRIVERS_IMPLEMENTATIONS];
85};
86
87struct egl_surface_t : public egl_object_t<'_srf'>
88{
89    egl_surface_t(EGLDisplay dpy, EGLSurface surface,
90            NativeWindowType window, int impl, egl_connection_t const* cnx)
91    : dpy(dpy), surface(surface), window(window), impl(impl), cnx(cnx)
92    {
93        // NOTE: window must be incRef'ed and connected already
94    }
95    ~egl_surface_t() {
96        if (window) {
97            if (window->disconnect)
98                window->disconnect(window);
99            window->decRef(window);
100        }
101    }
102    EGLDisplay                  dpy;
103    EGLSurface                  surface;
104    NativeWindowType            window;
105    int                         impl;
106    egl_connection_t const*     cnx;
107};
108
109struct egl_context_t : public egl_object_t<'_ctx'>
110{
111    egl_context_t(EGLDisplay dpy, EGLContext context,
112            int impl, egl_connection_t const* cnx)
113    : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx)
114    {
115    }
116    EGLDisplay                  dpy;
117    EGLContext                  context;
118    EGLSurface                  read;
119    EGLSurface                  draw;
120    int                         impl;
121    egl_connection_t const*     cnx;
122};
123
124struct tls_t
125{
126    tls_t() : error(EGL_SUCCESS), ctx(0) { }
127    EGLint      error;
128    EGLContext  ctx;
129};
130
131static void gl_unimplemented() {
132    LOGE("called unimplemented OpenGL ES API");
133}
134
135// ----------------------------------------------------------------------------
136// GL / EGL hooks
137// ----------------------------------------------------------------------------
138
139#undef GL_ENTRY
140#undef EGL_ENTRY
141#define GL_ENTRY(_r, _api, ...) #_api,
142#define EGL_ENTRY(_r, _api, ...) #_api,
143
144static char const * const gl_names[] = {
145    #include "gl_entries.in"
146    #include "glext_entries.in"
147    NULL
148};
149
150static char const * const egl_names[] = {
151    #include "egl_entries.in"
152    NULL
153};
154
155#undef GL_ENTRY
156#undef EGL_ENTRY
157
158// ----------------------------------------------------------------------------
159
160egl_connection_t gEGLImpl[IMPL_NUM_DRIVERS_IMPLEMENTATIONS];
161static egl_display_t gDisplay[NUM_DISPLAYS];
162static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
163static pthread_key_t gEGLThreadLocalStorageKey = -1;
164
165// ----------------------------------------------------------------------------
166
167gl_hooks_t gHooks[IMPL_NUM_IMPLEMENTATIONS];
168pthread_key_t gGLWrapperKey = -1;
169
170// ----------------------------------------------------------------------------
171
172static __attribute__((noinline))
173const char *egl_strerror(EGLint err)
174{
175    switch (err){
176        case EGL_SUCCESS:               return "EGL_SUCCESS";
177        case EGL_NOT_INITIALIZED:       return "EGL_NOT_INITIALIZED";
178        case EGL_BAD_ACCESS:            return "EGL_BAD_ACCESS";
179        case EGL_BAD_ALLOC:             return "EGL_BAD_ALLOC";
180        case EGL_BAD_ATTRIBUTE:         return "EGL_BAD_ATTRIBUTE";
181        case EGL_BAD_CONFIG:            return "EGL_BAD_CONFIG";
182        case EGL_BAD_CONTEXT:           return "EGL_BAD_CONTEXT";
183        case EGL_BAD_CURRENT_SURFACE:   return "EGL_BAD_CURRENT_SURFACE";
184        case EGL_BAD_DISPLAY:           return "EGL_BAD_DISPLAY";
185        case EGL_BAD_MATCH:             return "EGL_BAD_MATCH";
186        case EGL_BAD_NATIVE_PIXMAP:     return "EGL_BAD_NATIVE_PIXMAP";
187        case EGL_BAD_NATIVE_WINDOW:     return "EGL_BAD_NATIVE_WINDOW";
188        case EGL_BAD_PARAMETER:         return "EGL_BAD_PARAMETER";
189        case EGL_BAD_SURFACE:           return "EGL_BAD_SURFACE";
190        case EGL_CONTEXT_LOST:          return "EGL_CONTEXT_LOST";
191        default: return "UNKNOWN";
192    }
193}
194
195static __attribute__((noinline))
196void clearTLS() {
197    if (gEGLThreadLocalStorageKey != -1) {
198        tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
199        if (tls) {
200            delete tls;
201            pthread_setspecific(gEGLThreadLocalStorageKey, 0);
202        }
203    }
204}
205
206static tls_t* getTLS()
207{
208    tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
209    if (tls == 0) {
210        tls = new tls_t;
211        pthread_setspecific(gEGLThreadLocalStorageKey, tls);
212    }
213    return tls;
214}
215
216template<typename T>
217static __attribute__((noinline))
218T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
219    if (gEGLThreadLocalStorageKey == -1) {
220        pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
221        if (gEGLThreadLocalStorageKey == -1)
222            pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
223        pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
224    }
225    tls_t* tls = getTLS();
226    if (tls->error != error) {
227        LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
228        tls->error = error;
229    }
230    return returnValue;
231}
232
233static __attribute__((noinline))
234GLint getError() {
235    if (gEGLThreadLocalStorageKey == -1)
236        return EGL_SUCCESS;
237    tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
238    if (!tls) return EGL_SUCCESS;
239    GLint error = tls->error;
240    tls->error = EGL_SUCCESS;
241    return error;
242}
243
244static __attribute__((noinline))
245void setContext(EGLContext ctx) {
246    if (gEGLThreadLocalStorageKey == -1) {
247        pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
248        if (gEGLThreadLocalStorageKey == -1)
249            pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
250        pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
251    }
252    tls_t* tls = getTLS();
253    tls->ctx = ctx;
254}
255
256static __attribute__((noinline))
257EGLContext getContext() {
258    if (gEGLThreadLocalStorageKey == -1)
259        return EGL_NO_CONTEXT;
260    tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
261    if (!tls) return EGL_NO_CONTEXT;
262    return tls->ctx;
263}
264
265
266/*****************************************************************************/
267
268class ISurfaceComposer;
269const sp<ISurfaceComposer>& getSurfaceFlinger();
270request_gpu_t* gpu_acquire(void* user);
271int gpu_release(void*, request_gpu_t* gpu);
272
273static __attribute__((noinline))
274void *load_driver(const char* driver, gl_hooks_t* hooks)
275{
276    void* dso = dlopen(driver, RTLD_NOW | RTLD_LOCAL);
277    LOGE_IF(!dso,
278            "couldn't load <%s> library (%s)",
279            driver, dlerror());
280
281    if (dso) {
282        // first find the symbol for eglGetProcAddress
283
284        typedef __eglMustCastToProperFunctionPointerType (*getProcAddressType)(
285                const char*);
286
287        getProcAddressType getProcAddress =
288            (getProcAddressType)dlsym(dso, "eglGetProcAddress");
289
290        LOGE_IF(!getProcAddress,
291                "can't find eglGetProcAddress() in %s", driver);
292
293        __eglMustCastToProperFunctionPointerType* curr;
294        char const * const * api;
295
296        gl_hooks_t::egl_t* egl = &hooks->egl;
297        curr = (__eglMustCastToProperFunctionPointerType*)egl;
298        api = egl_names;
299        while (*api) {
300            char const * name = *api;
301            __eglMustCastToProperFunctionPointerType f =
302                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
303            if (f == NULL) {
304                // couldn't find the entry-point, use eglGetProcAddress()
305                f = getProcAddress(name);
306                if (f == NULL) {
307                    f = (__eglMustCastToProperFunctionPointerType)0;
308                }
309            }
310            *curr++ = f;
311            api++;
312        }
313
314        gl_hooks_t::gl_t* gl = &hooks->gl;
315        curr = (__eglMustCastToProperFunctionPointerType*)gl;
316        api = gl_names;
317        while (*api) {
318            char const * name = *api;
319            __eglMustCastToProperFunctionPointerType f =
320                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
321            if (f == NULL) {
322                // couldn't find the entry-point, use eglGetProcAddress()
323                f = getProcAddress(name);
324                if (f == NULL) {
325                    f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
326                }
327            }
328            *curr++ = f;
329            api++;
330        }
331
332        // hook this driver up with surfaceflinger if needed
333        register_gpu_t register_gpu =
334            (register_gpu_t)dlsym(dso, "oem_register_gpu");
335
336        if (register_gpu != NULL) {
337            if (getSurfaceFlinger() != 0) {
338                register_gpu(dso, gpu_acquire, gpu_release);
339            }
340        }
341    }
342    return dso;
343}
344
345template<typename T>
346static __attribute__((noinline))
347int binarySearch(
348        T const sortedArray[], int first, int last, T key)
349{
350    while (first <= last) {
351        int mid = (first + last) / 2;
352        if (key > sortedArray[mid]) {
353            first = mid + 1;
354        } else if (key < sortedArray[mid]) {
355            last = mid - 1;
356        } else {
357            return mid;
358        }
359    }
360    return -1;
361}
362
363static EGLint configToUniqueId(egl_display_t const* dp, int i, int index)
364{
365    // NOTE: this mapping works only if we have no more than two EGLimpl
366    return (i>0 ? dp->numConfigs[0] : 0) + index;
367}
368
369static void uniqueIdToConfig(egl_display_t const* dp, EGLint configId,
370        int& i, int& index)
371{
372    // NOTE: this mapping works only if we have no more than two EGLimpl
373    size_t numConfigs = dp->numConfigs[0];
374    i = configId / numConfigs;
375    index = configId % numConfigs;
376}
377
378static int cmp_configs(const void* a, const void *b)
379{
380    EGLConfig c0 = *(EGLConfig const *)a;
381    EGLConfig c1 = *(EGLConfig const *)b;
382    return c0<c1 ? -1 : (c0>c1 ? 1 : 0);
383}
384
385struct extention_map_t {
386    const char* name;
387    __eglMustCastToProperFunctionPointerType address;
388};
389
390static const extention_map_t gExtentionMap[] = {
391};
392
393static extention_map_t gGLExtentionMap[MAX_NUMBER_OF_GL_EXTENSIONS];
394
395static void(*findProcAddress(const char* name,
396        const extention_map_t* map, size_t n))()
397{
398    for (uint32_t i=0 ; i<n ; i++) {
399        if (!strcmp(name, map[i].name)) {
400            return map[i].address;
401        }
402    }
403    return NULL;
404}
405
406// ----------------------------------------------------------------------------
407
408static int gl_context_lost() {
409    setGlThreadSpecific(&gHooks[IMPL_CONTEXT_LOST]);
410    return 0;
411}
412static int egl_context_lost() {
413    setGlThreadSpecific(&gHooks[IMPL_CONTEXT_LOST]);
414    return EGL_FALSE;
415}
416static EGLBoolean egl_context_lost_swap_buffers(void*, void*) {
417    usleep(100000); // don't use all the CPU
418    setGlThreadSpecific(&gHooks[IMPL_CONTEXT_LOST]);
419    return EGL_FALSE;
420}
421static GLint egl_context_lost_get_error() {
422    return EGL_CONTEXT_LOST;
423}
424static int ext_context_lost() {
425    return 0;
426}
427
428static void gl_no_context() {
429    LOGE("call to OpenGL ES API with no current context");
430}
431static void early_egl_init(void)
432{
433#if !USE_FAST_TLS_KEY
434    pthread_key_create(&gGLWrapperKey, NULL);
435#endif
436    uint32_t addr = (uint32_t)((void*)gl_no_context);
437    android_memset32(
438            (uint32_t*)(void*)&gHooks[IMPL_NO_CONTEXT],
439            addr,
440            sizeof(gHooks[IMPL_NO_CONTEXT]));
441    setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
442}
443
444static pthread_once_t once_control = PTHREAD_ONCE_INIT;
445static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
446
447
448static inline
449egl_display_t* get_display(EGLDisplay dpy)
450{
451    uintptr_t index = uintptr_t(dpy)-1U;
452    return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
453}
454
455template<typename NATIVE, typename EGL>
456static inline NATIVE* egl_to_native_cast(EGL arg) {
457    return reinterpret_cast<NATIVE*>(arg);
458}
459
460static inline
461egl_surface_t* get_surface(EGLSurface surface) {
462    return egl_to_native_cast<egl_surface_t>(surface);
463}
464
465static inline
466egl_context_t* get_context(EGLContext context) {
467    return egl_to_native_cast<egl_context_t>(context);
468}
469
470static egl_connection_t* validate_display_config(
471        EGLDisplay dpy, EGLConfig config,
472        egl_display_t const*& dp, int& impl, int& index)
473{
474    dp = get_display(dpy);
475    if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
476
477    impl = uintptr_t(config)>>24;
478    if (uint32_t(impl) >= IMPL_NUM_DRIVERS_IMPLEMENTATIONS) {
479        return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
480    }
481    index = uintptr_t(config) & 0xFFFFFF;
482    if (index >= dp->numConfigs[impl]) {
483        return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
484    }
485    egl_connection_t* const cnx = &gEGLImpl[impl];
486    if (cnx->dso == 0) {
487        return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
488    }
489    return cnx;
490}
491
492static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
493{
494    if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
495        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
496    if (!get_display(dpy)->isValid())
497        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
498    if (!ctx) // TODO: make sure context is a valid object
499        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
500    if (!get_context(ctx)->isValid())
501        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
502    return EGL_TRUE;
503}
504
505static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
506{
507    if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
508        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
509    if (!get_display(dpy)->isValid())
510        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
511    if (!surface) // TODO: make sure surface is a valid object
512        return setError(EGL_BAD_SURFACE, EGL_FALSE);
513    if (!get_surface(surface)->isValid())
514        return setError(EGL_BAD_SURFACE, EGL_FALSE);
515    return EGL_TRUE;
516}
517
518
519EGLDisplay egl_init_displays(NativeDisplayType display)
520{
521    if (sEarlyInitState) {
522        return EGL_NO_DISPLAY;
523    }
524
525    uint32_t index = uint32_t(display);
526    if (index >= NUM_DISPLAYS) {
527        return EGL_NO_DISPLAY;
528    }
529
530    EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
531    egl_display_t* d = &gDisplay[index];
532
533    // dynamically load all our EGL implementations for that display
534    // and call into the real eglGetGisplay()
535    egl_connection_t* cnx = &gEGLImpl[IMPL_SOFTWARE];
536    if (cnx->dso == 0) {
537        cnx->hooks = &gHooks[IMPL_SOFTWARE];
538        cnx->dso = load_driver("libagl.so", cnx->hooks);
539    }
540    if (cnx->dso && d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY) {
541        d->dpys[IMPL_SOFTWARE] = cnx->hooks->egl.eglGetDisplay(display);
542        LOGE_IF(d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY,
543                "No EGLDisplay for software EGL!");
544    }
545
546    cnx = &gEGLImpl[IMPL_HARDWARE];
547    if (cnx->dso == 0 && cnx->unavailable == 0) {
548        char value[PROPERTY_VALUE_MAX];
549        property_get("debug.egl.hw", value, "1");
550        if (atoi(value) != 0) {
551            cnx->hooks = &gHooks[IMPL_HARDWARE];
552            cnx->dso = load_driver("libhgl.so", cnx->hooks);
553        } else {
554            LOGD("3D hardware acceleration is disabled");
555        }
556    }
557    if (cnx->dso && d->dpys[IMPL_HARDWARE]==EGL_NO_DISPLAY) {
558        android_memset32(
559                (uint32_t*)(void*)&gHooks[IMPL_CONTEXT_LOST].gl,
560                (uint32_t)((void*)gl_context_lost),
561                sizeof(gHooks[IMPL_CONTEXT_LOST].gl));
562        android_memset32(
563                (uint32_t*)(void*)&gHooks[IMPL_CONTEXT_LOST].egl,
564                (uint32_t)((void*)egl_context_lost),
565                sizeof(gHooks[IMPL_CONTEXT_LOST].egl));
566        android_memset32(
567                (uint32_t*)(void*)&gHooks[IMPL_CONTEXT_LOST].ext,
568                (uint32_t)((void*)ext_context_lost),
569                sizeof(gHooks[IMPL_CONTEXT_LOST].ext));
570
571        gHooks[IMPL_CONTEXT_LOST].egl.eglSwapBuffers =
572                egl_context_lost_swap_buffers;
573
574        gHooks[IMPL_CONTEXT_LOST].egl.eglGetError =
575                egl_context_lost_get_error;
576
577        gHooks[IMPL_CONTEXT_LOST].egl.eglTerminate =
578                gHooks[IMPL_HARDWARE].egl.eglTerminate;
579
580        d->dpys[IMPL_HARDWARE] = cnx->hooks->egl.eglGetDisplay(display);
581        if (d->dpys[IMPL_HARDWARE] == EGL_NO_DISPLAY) {
582            LOGE("h/w accelerated eglGetDisplay() failed (%s)",
583                    egl_strerror(cnx->hooks->egl.eglGetError()));
584            dlclose((void*)cnx->dso);
585            cnx->dso = 0;
586            // in case of failure, we want to make sure we don't try again
587            // as it's expensive.
588            cnx->unavailable = 1;
589        }
590    }
591
592    return dpy;
593}
594
595
596// ----------------------------------------------------------------------------
597}; // namespace android
598// ----------------------------------------------------------------------------
599
600using namespace android;
601
602EGLDisplay eglGetDisplay(NativeDisplayType display)
603{
604    return egl_init_displays(display);
605}
606
607// ----------------------------------------------------------------------------
608// Initialization
609// ----------------------------------------------------------------------------
610
611EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
612{
613    egl_display_t * const dp = get_display(dpy);
614    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
615
616    if (android_atomic_inc(&dp->refs) > 0) {
617        if (major != NULL) *major = VERSION_MAJOR;
618        if (minor != NULL) *minor = VERSION_MINOR;
619        return EGL_TRUE;
620    }
621
622    setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
623
624    // initialize each EGL and
625    // build our own extension string first, based on the extension we know
626    // and the extension supported by our client implementation
627    dp->extensionsString = strdup(gExtensionString);
628    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
629        egl_connection_t* const cnx = &gEGLImpl[i];
630        cnx->major = -1;
631        cnx->minor = -1;
632        if (!cnx->dso)
633            continue;
634
635        if (cnx->hooks->egl.eglInitialize(
636                dp->dpys[i], &cnx->major, &cnx->minor)) {
637
638            //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
639            //        i, dp->dpys[i], cnx->major, cnx->minor, cnx);
640
641            // get the query-strings for this display for each implementation
642            dp->queryString[i].vendor =
643                cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VENDOR);
644            dp->queryString[i].version =
645                cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VERSION);
646            dp->queryString[i].extensions = strdup(
647                    cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_EXTENSIONS));
648            dp->queryString[i].clientApi =
649                cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_CLIENT_APIS);
650
651        } else {
652            LOGD("%d: eglInitialize() failed (%s)",
653                    i, egl_strerror(cnx->hooks->egl.eglGetError()));
654        }
655    }
656
657    EGLBoolean res = EGL_FALSE;
658    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
659        egl_connection_t* const cnx = &gEGLImpl[i];
660        if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
661            EGLint n;
662            if (cnx->hooks->egl.eglGetConfigs(dp->dpys[i], 0, 0, &n)) {
663                dp->configs[i] = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
664                if (dp->configs[i]) {
665                    if (cnx->hooks->egl.eglGetConfigs(
666                            dp->dpys[i], dp->configs[i], n, &dp->numConfigs[i]))
667                    {
668                        // sort the configurations so we can do binary searches
669                        qsort(  dp->configs[i],
670                                dp->numConfigs[i],
671                                sizeof(EGLConfig), cmp_configs);
672
673                        dp->numTotalConfigs += n;
674                        res = EGL_TRUE;
675                    }
676                }
677            }
678        }
679    }
680
681    if (res == EGL_TRUE) {
682        if (major != NULL) *major = VERSION_MAJOR;
683        if (minor != NULL) *minor = VERSION_MINOR;
684        return EGL_TRUE;
685    }
686    return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
687}
688
689EGLBoolean eglTerminate(EGLDisplay dpy)
690{
691    egl_display_t* const dp = get_display(dpy);
692    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
693    if (android_atomic_dec(&dp->refs) != 1)
694        return EGL_TRUE;
695
696    EGLBoolean res = EGL_FALSE;
697    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
698        egl_connection_t* const cnx = &gEGLImpl[i];
699        if (cnx->dso) {
700            cnx->hooks->egl.eglTerminate(dp->dpys[i]);
701
702            /* REVISIT: it's unclear what to do if eglTerminate() fails,
703             * on one end we shouldn't care, on the other end if it fails
704             * it might not be safe to call dlclose() (there could be some
705             * threads around). */
706
707            free(dp->configs[i]);
708            free((void*)dp->queryString[i].extensions);
709            dp->numConfigs[i] = 0;
710            dp->dpys[i] = EGL_NO_DISPLAY;
711            dlclose((void*)cnx->dso);
712            cnx->dso = 0;
713            res = EGL_TRUE;
714        }
715    }
716    free((void*)dp->extensionsString);
717    dp->extensionsString = 0;
718    dp->numTotalConfigs = 0;
719    clearTLS();
720    return res;
721}
722
723// ----------------------------------------------------------------------------
724// configuration
725// ----------------------------------------------------------------------------
726
727EGLBoolean eglGetConfigs(   EGLDisplay dpy,
728                            EGLConfig *configs,
729                            EGLint config_size, EGLint *num_config)
730{
731    egl_display_t const * const dp = get_display(dpy);
732    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
733
734    GLint numConfigs = dp->numTotalConfigs;
735    if (!configs) {
736        *num_config = numConfigs;
737        return EGL_TRUE;
738    }
739    GLint n = 0;
740    for (int j=0 ; j<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; j++) {
741        for (int i=0 ; i<dp->numConfigs[j] && config_size ; i++) {
742            *configs++ = MAKE_CONFIG(j, i);
743            config_size--;
744            n++;
745        }
746    }
747
748    *num_config = n;
749    return EGL_TRUE;
750}
751
752EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
753                            EGLConfig *configs, EGLint config_size,
754                            EGLint *num_config)
755{
756    egl_display_t const * const dp = get_display(dpy);
757    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
758
759    if (num_config==0) {
760        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
761    }
762
763    EGLint n;
764    EGLBoolean res = EGL_FALSE;
765    *num_config = 0;
766
767
768    // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
769    // to do  this, we have to go through the attrib_list array once
770    // to figure out both its size and if it contains an EGL_CONFIG_ID
771    // key. If so, the full array is copied and patched.
772    // NOTE: we assume that there can be only one occurrence
773    // of EGL_CONFIG_ID.
774
775    EGLint patch_index = -1;
776    GLint attr;
777    size_t size = 0;
778    while ((attr=attrib_list[size])) {
779        if (attr == EGL_CONFIG_ID)
780            patch_index = size;
781        size += 2;
782    }
783    if (patch_index >= 0) {
784        size += 2; // we need copy the sentinel as well
785        EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
786        if (new_list == 0)
787            return setError(EGL_BAD_ALLOC, EGL_FALSE);
788        memcpy(new_list, attrib_list, size*sizeof(EGLint));
789
790        // patch the requested EGL_CONFIG_ID
791        int i, index;
792        EGLint& configId(new_list[patch_index+1]);
793        uniqueIdToConfig(dp, configId, i, index);
794
795        egl_connection_t* const cnx = &gEGLImpl[i];
796        if (cnx->dso) {
797            cnx->hooks->egl.eglGetConfigAttrib(
798                    dp->dpys[i], dp->configs[i][index],
799                    EGL_CONFIG_ID, &configId);
800
801            // and switch to the new list
802            attrib_list = const_cast<const EGLint *>(new_list);
803
804            // At this point, the only configuration that can match is
805            // dp->configs[i][index], however, we don't know if it would be
806            // rejected because of the other attributes, so we do have to call
807            // cnx->hooks->egl.eglChooseConfig() -- but we don't have to loop
808            // through all the EGLimpl[].
809            // We also know we can only get a single config back, and we know
810            // which one.
811
812            res = cnx->hooks->egl.eglChooseConfig(
813                    dp->dpys[i], attrib_list, configs, config_size, &n);
814            if (res && n>0) {
815                // n has to be 0 or 1, by construction, and we already know
816                // which config it will return (since there can be only one).
817                if (configs) {
818                    configs[0] = MAKE_CONFIG(i, index);
819                }
820                *num_config = 1;
821            }
822        }
823
824        free(const_cast<EGLint *>(attrib_list));
825        return res;
826    }
827
828    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
829        egl_connection_t* const cnx = &gEGLImpl[i];
830        if (cnx->dso) {
831            if (cnx->hooks->egl.eglChooseConfig(
832                    dp->dpys[i], attrib_list, configs, config_size, &n)) {
833                if (configs) {
834                    // now we need to convert these client EGLConfig to our
835                    // internal EGLConfig format. This is done in O(n log n).
836                    for (int j=0 ; j<n ; j++) {
837                        int index = binarySearch<EGLConfig>(
838                                dp->configs[i], 0, dp->numConfigs[i]-1, configs[j]);
839                        if (index >= 0) {
840                            if (configs) {
841                                configs[j] = MAKE_CONFIG(i, index);
842                            }
843                        } else {
844                            return setError(EGL_BAD_CONFIG, EGL_FALSE);
845                        }
846                    }
847                    configs += n;
848                    config_size -= n;
849                }
850                *num_config += n;
851                res = EGL_TRUE;
852            }
853        }
854    }
855    return res;
856}
857
858EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
859        EGLint attribute, EGLint *value)
860{
861    egl_display_t const* dp = 0;
862    int i=0, index=0;
863    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
864    if (!cnx) return EGL_FALSE;
865
866    if (attribute == EGL_CONFIG_ID) {
867        // EGL_CONFIG_IDs must be unique, just use the order of the selected
868        // EGLConfig.
869        *value = configToUniqueId(dp, i, index);
870        return EGL_TRUE;
871    }
872    return cnx->hooks->egl.eglGetConfigAttrib(
873            dp->dpys[i], dp->configs[i][index], attribute, value);
874}
875
876// ----------------------------------------------------------------------------
877// surfaces
878// ----------------------------------------------------------------------------
879
880EGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
881                                    NativeWindowType window,
882                                    const EGLint *attrib_list)
883{
884    egl_display_t const* dp = 0;
885    int i=0, index=0;
886    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
887    if (cnx) {
888        // window must be connected upon calling underlying
889        // eglCreateWindowSurface
890        if (window) {
891            window->incRef(window);
892            if (window->connect)
893                window->connect(window);
894        }
895
896        EGLSurface surface = cnx->hooks->egl.eglCreateWindowSurface(
897                dp->dpys[i], dp->configs[i][index], window, attrib_list);
898        if (surface != EGL_NO_SURFACE) {
899            egl_surface_t* s = new egl_surface_t(dpy, surface, window, i, cnx);
900            return s;
901        }
902
903        // something went wrong, disconnect and free window
904        // (will disconnect() automatically)
905        if (window) {
906            window->decRef(window);
907        }
908    }
909    return EGL_NO_SURFACE;
910}
911
912EGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
913                                    NativePixmapType pixmap,
914                                    const EGLint *attrib_list)
915{
916    egl_display_t const* dp = 0;
917    int i=0, index=0;
918    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
919    if (cnx) {
920        EGLSurface surface = cnx->hooks->egl.eglCreatePixmapSurface(
921                dp->dpys[i], dp->configs[i][index], pixmap, attrib_list);
922        if (surface != EGL_NO_SURFACE) {
923            egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
924            return s;
925        }
926    }
927    return EGL_NO_SURFACE;
928}
929
930EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
931                                    const EGLint *attrib_list)
932{
933    egl_display_t const* dp = 0;
934    int i=0, index=0;
935    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
936    if (cnx) {
937        EGLSurface surface = cnx->hooks->egl.eglCreatePbufferSurface(
938                dp->dpys[i], dp->configs[i][index], attrib_list);
939        if (surface != EGL_NO_SURFACE) {
940            egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
941            return s;
942        }
943    }
944    return EGL_NO_SURFACE;
945}
946
947EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
948{
949    if (!validate_display_surface(dpy, surface))
950        return EGL_FALSE;
951    egl_display_t const * const dp = get_display(dpy);
952    egl_surface_t const * const s = get_surface(surface);
953
954    EGLBoolean result = s->cnx->hooks->egl.eglDestroySurface(
955            dp->dpys[s->impl], s->surface);
956
957    delete s;
958    return result;
959}
960
961EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
962                            EGLint attribute, EGLint *value)
963{
964    if (!validate_display_surface(dpy, surface))
965        return EGL_FALSE;
966    egl_display_t const * const dp = get_display(dpy);
967    egl_surface_t const * const s = get_surface(surface);
968
969    return s->cnx->hooks->egl.eglQuerySurface(
970            dp->dpys[s->impl], s->surface, attribute, value);
971}
972
973// ----------------------------------------------------------------------------
974// contextes
975// ----------------------------------------------------------------------------
976
977EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
978                            EGLContext share_list, const EGLint *attrib_list)
979{
980    egl_display_t const* dp = 0;
981    int i=0, index=0;
982    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
983    if (cnx) {
984        EGLContext context = cnx->hooks->egl.eglCreateContext(
985                dp->dpys[i], dp->configs[i][index], share_list, attrib_list);
986        if (context != EGL_NO_CONTEXT) {
987            egl_context_t* c = new egl_context_t(dpy, context, i, cnx);
988            return c;
989        }
990    }
991    return EGL_NO_CONTEXT;
992}
993
994EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
995{
996    if (!validate_display_context(dpy, ctx))
997        return EGL_FALSE;
998    egl_display_t const * const dp = get_display(dpy);
999    egl_context_t * const c = get_context(ctx);
1000    EGLBoolean result = c->cnx->hooks->egl.eglDestroyContext(
1001            dp->dpys[c->impl], c->context);
1002    delete c;
1003    return result;
1004}
1005
1006EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
1007                            EGLSurface read, EGLContext ctx)
1008{
1009    egl_display_t const * const dp = get_display(dpy);
1010    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1011
1012    if (read == EGL_NO_SURFACE && draw  == EGL_NO_SURFACE &&
1013            ctx == EGL_NO_CONTEXT)
1014    {
1015        EGLBoolean result = EGL_TRUE;
1016        ctx = getContext();
1017        if (ctx) {
1018            egl_context_t * const c = get_context(ctx);
1019            result = c->cnx->hooks->egl.eglMakeCurrent(dp->dpys[c->impl], 0, 0, 0);
1020            if (result == EGL_TRUE) {
1021                setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
1022                setContext(EGL_NO_CONTEXT);
1023            }
1024        }
1025        return result;
1026    }
1027
1028    if (!validate_display_context(dpy, ctx))
1029        return EGL_FALSE;
1030
1031    egl_context_t * const c = get_context(ctx);
1032    if (draw != EGL_NO_SURFACE) {
1033        egl_surface_t const * d = get_surface(draw);
1034        if (!d) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1035        if (d->impl != c->impl)
1036            return setError(EGL_BAD_MATCH, EGL_FALSE);
1037        draw = d->surface;
1038    }
1039    if (read != EGL_NO_SURFACE) {
1040        egl_surface_t const * r = get_surface(read);
1041        if (!r) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1042        if (r->impl != c->impl)
1043            return setError(EGL_BAD_MATCH, EGL_FALSE);
1044        read = r->surface;
1045    }
1046    EGLBoolean result = c->cnx->hooks->egl.eglMakeCurrent(
1047            dp->dpys[c->impl], draw, read, c->context);
1048
1049    if (result == EGL_TRUE) {
1050        setGlThreadSpecific(c->cnx->hooks);
1051        setContext(ctx);
1052        c->read = read;
1053        c->draw = draw;
1054    }
1055    return result;
1056}
1057
1058
1059EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1060                            EGLint attribute, EGLint *value)
1061{
1062    if (!validate_display_context(dpy, ctx))
1063        return EGL_FALSE;
1064
1065    egl_display_t const * const dp = get_display(dpy);
1066    egl_context_t * const c = get_context(ctx);
1067
1068    return c->cnx->hooks->egl.eglQueryContext(
1069            dp->dpys[c->impl], c->context, attribute, value);
1070}
1071
1072EGLContext eglGetCurrentContext(void)
1073{
1074    EGLContext ctx = getContext();
1075    return ctx;
1076}
1077
1078EGLSurface eglGetCurrentSurface(EGLint readdraw)
1079{
1080    EGLContext ctx = getContext();
1081    if (ctx) {
1082        egl_context_t const * const c = get_context(ctx);
1083        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1084        switch (readdraw) {
1085            case EGL_READ: return c->read;
1086            case EGL_DRAW: return c->draw;
1087            default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1088        }
1089    }
1090    return EGL_NO_SURFACE;
1091}
1092
1093EGLDisplay eglGetCurrentDisplay(void)
1094{
1095    EGLContext ctx = getContext();
1096    if (ctx) {
1097        egl_context_t const * const c = get_context(ctx);
1098        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1099        return c->dpy;
1100    }
1101    return EGL_NO_DISPLAY;
1102}
1103
1104EGLBoolean eglWaitGL(void)
1105{
1106    EGLBoolean res = EGL_TRUE;
1107    EGLContext ctx = getContext();
1108    if (ctx) {
1109        egl_context_t const * const c = get_context(ctx);
1110        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1111        if (uint32_t(c->impl)>=2)
1112            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1113        egl_connection_t* const cnx = &gEGLImpl[c->impl];
1114        if (!cnx->dso)
1115            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1116        res = cnx->hooks->egl.eglWaitGL();
1117    }
1118    return res;
1119}
1120
1121EGLBoolean eglWaitNative(EGLint engine)
1122{
1123    EGLBoolean res = EGL_TRUE;
1124    EGLContext ctx = getContext();
1125    if (ctx) {
1126        egl_context_t const * const c = get_context(ctx);
1127        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1128        if (uint32_t(c->impl)>=2)
1129            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1130        egl_connection_t* const cnx = &gEGLImpl[c->impl];
1131        if (!cnx->dso)
1132            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1133        res = cnx->hooks->egl.eglWaitNative(engine);
1134    }
1135    return res;
1136}
1137
1138EGLint eglGetError(void)
1139{
1140    EGLint result = EGL_SUCCESS;
1141    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1142        EGLint err = EGL_SUCCESS;
1143        egl_connection_t* const cnx = &gEGLImpl[i];
1144        if (cnx->dso)
1145            err = cnx->hooks->egl.eglGetError();
1146        if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1147            result = err;
1148    }
1149    if (result == EGL_SUCCESS)
1150        result = getError();
1151    return result;
1152}
1153
1154__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1155{
1156    // eglGetProcAddress() could be the very first function called
1157    // in which case we must make sure we've initialized ourselves, this
1158    // happens the first time egl_get_display() is called.
1159
1160    if (egl_init_displays(EGL_DEFAULT_DISPLAY) == EGL_NO_DISPLAY)
1161        return NULL;
1162
1163    __eglMustCastToProperFunctionPointerType addr;
1164    addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1165    if (addr) return addr;
1166
1167    return NULL; // TODO: finish implementation below
1168
1169    addr = findProcAddress(procname, gGLExtentionMap, NELEM(gGLExtentionMap));
1170    if (addr) return addr;
1171
1172    addr = 0;
1173    int slot = -1;
1174    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1175        egl_connection_t* const cnx = &gEGLImpl[i];
1176        if (cnx->dso) {
1177            if (cnx->hooks->egl.eglGetProcAddress) {
1178                addr = cnx->hooks->egl.eglGetProcAddress(procname);
1179                if (addr) {
1180                    if (slot == -1) {
1181                        slot = 0; // XXX: find free slot
1182                        if (slot == -1) {
1183                            addr = 0;
1184                            break;
1185                        }
1186                    }
1187                    cnx->hooks->ext.extensions[slot] = addr;
1188                }
1189            }
1190        }
1191    }
1192
1193    if (slot >= 0) {
1194        addr = 0; // XXX: address of stub 'slot'
1195        gGLExtentionMap[slot].name = strdup(procname);
1196        gGLExtentionMap[slot].address = addr;
1197    }
1198
1199    return addr;
1200
1201
1202    /*
1203     *  TODO: For OpenGL ES extensions, we must generate a stub
1204     *  that looks like
1205     *      mov     r12, #0xFFFF0FFF
1206     *      ldr     r12, [r12, #-15]
1207     *      ldr     r12, [r12, #TLS_SLOT_OPENGL_API*4]
1208     *      mov     r12, [r12, #api_offset]
1209     *      ldrne   pc, r12
1210     *      mov     pc, #unsupported_extension
1211     *
1212     *  and write the address of the extension in *all*
1213     *  gl_hooks_t::gl_ext_t at offset "api_offset" from gl_hooks_t
1214     *
1215     */
1216}
1217
1218EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1219{
1220    if (!validate_display_surface(dpy, draw))
1221        return EGL_FALSE;
1222    egl_display_t const * const dp = get_display(dpy);
1223    egl_surface_t const * const s = get_surface(draw);
1224    return s->cnx->hooks->egl.eglSwapBuffers(dp->dpys[s->impl], s->surface);
1225}
1226
1227EGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
1228                            NativePixmapType target)
1229{
1230    if (!validate_display_surface(dpy, surface))
1231        return EGL_FALSE;
1232    egl_display_t const * const dp = get_display(dpy);
1233    egl_surface_t const * const s = get_surface(surface);
1234    return s->cnx->hooks->egl.eglCopyBuffers(
1235            dp->dpys[s->impl], s->surface, target);
1236}
1237
1238const char* eglQueryString(EGLDisplay dpy, EGLint name)
1239{
1240    egl_display_t const * const dp = get_display(dpy);
1241    switch (name) {
1242        case EGL_VENDOR:
1243            return gVendorString;
1244        case EGL_VERSION:
1245            return gVersionString;
1246        case EGL_EXTENSIONS:
1247            return gExtensionString;
1248        case EGL_CLIENT_APIS:
1249            return gClientApiString;
1250    }
1251    return setError(EGL_BAD_PARAMETER, (const char *)0);
1252}
1253
1254
1255// ----------------------------------------------------------------------------
1256// EGL 1.1
1257// ----------------------------------------------------------------------------
1258
1259EGLBoolean eglSurfaceAttrib(
1260        EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1261{
1262    if (!validate_display_surface(dpy, surface))
1263        return EGL_FALSE;
1264    egl_display_t const * const dp = get_display(dpy);
1265    egl_surface_t const * const s = get_surface(surface);
1266    if (s->cnx->hooks->egl.eglSurfaceAttrib) {
1267        return s->cnx->hooks->egl.eglSurfaceAttrib(
1268                dp->dpys[s->impl], s->surface, attribute, value);
1269    }
1270    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1271}
1272
1273EGLBoolean eglBindTexImage(
1274        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1275{
1276    if (!validate_display_surface(dpy, surface))
1277        return EGL_FALSE;
1278    egl_display_t const * const dp = get_display(dpy);
1279    egl_surface_t const * const s = get_surface(surface);
1280    if (s->cnx->hooks->egl.eglBindTexImage) {
1281        return s->cnx->hooks->egl.eglBindTexImage(
1282                dp->dpys[s->impl], s->surface, buffer);
1283    }
1284    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1285}
1286
1287EGLBoolean eglReleaseTexImage(
1288        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1289{
1290    if (!validate_display_surface(dpy, surface))
1291        return EGL_FALSE;
1292    egl_display_t const * const dp = get_display(dpy);
1293    egl_surface_t const * const s = get_surface(surface);
1294    if (s->cnx->hooks->egl.eglReleaseTexImage) {
1295        return s->cnx->hooks->egl.eglReleaseTexImage(
1296                dp->dpys[s->impl], s->surface, buffer);
1297    }
1298    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1299}
1300
1301EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1302{
1303    egl_display_t * const dp = get_display(dpy);
1304    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1305
1306    EGLBoolean res = EGL_TRUE;
1307    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1308        egl_connection_t* const cnx = &gEGLImpl[i];
1309        if (cnx->dso) {
1310            if (cnx->hooks->egl.eglSwapInterval) {
1311                if (cnx->hooks->egl.eglSwapInterval(dp->dpys[i], interval) == EGL_FALSE) {
1312                    res = EGL_FALSE;
1313                }
1314            }
1315        }
1316    }
1317    return res;
1318}
1319
1320
1321// ----------------------------------------------------------------------------
1322// EGL 1.2
1323// ----------------------------------------------------------------------------
1324
1325EGLBoolean eglWaitClient(void)
1326{
1327    EGLBoolean res = EGL_TRUE;
1328    EGLContext ctx = getContext();
1329    if (ctx) {
1330        egl_context_t const * const c = get_context(ctx);
1331        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1332        if (uint32_t(c->impl)>=2)
1333            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1334        egl_connection_t* const cnx = &gEGLImpl[c->impl];
1335        if (!cnx->dso)
1336            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1337        if (cnx->hooks->egl.eglWaitClient) {
1338            res = cnx->hooks->egl.eglWaitClient();
1339        } else {
1340            res = cnx->hooks->egl.eglWaitGL();
1341        }
1342    }
1343    return res;
1344}
1345
1346EGLBoolean eglBindAPI(EGLenum api)
1347{
1348    // bind this API on all EGLs
1349    EGLBoolean res = EGL_TRUE;
1350    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1351        egl_connection_t* const cnx = &gEGLImpl[i];
1352        if (cnx->dso) {
1353            if (cnx->hooks->egl.eglBindAPI) {
1354                if (cnx->hooks->egl.eglBindAPI(api) == EGL_FALSE) {
1355                    res = EGL_FALSE;
1356                }
1357            }
1358        }
1359    }
1360    return res;
1361}
1362
1363EGLenum eglQueryAPI(void)
1364{
1365    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1366        egl_connection_t* const cnx = &gEGLImpl[i];
1367        if (cnx->dso) {
1368            if (cnx->hooks->egl.eglQueryAPI) {
1369                // the first one we find is okay, because they all
1370                // should be the same
1371                return cnx->hooks->egl.eglQueryAPI();
1372            }
1373        }
1374    }
1375    // or, it can only be OpenGL ES
1376    return EGL_OPENGL_ES_API;
1377}
1378
1379EGLBoolean eglReleaseThread(void)
1380{
1381    for (int i=0 ; i<IMPL_NUM_DRIVERS_IMPLEMENTATIONS ; i++) {
1382        egl_connection_t* const cnx = &gEGLImpl[i];
1383        if (cnx->dso) {
1384            if (cnx->hooks->egl.eglReleaseThread) {
1385                cnx->hooks->egl.eglReleaseThread();
1386            }
1387        }
1388    }
1389    clearTLS();
1390    return EGL_TRUE;
1391}
1392
1393EGLSurface eglCreatePbufferFromClientBuffer(
1394          EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1395          EGLConfig config, const EGLint *attrib_list)
1396{
1397    egl_display_t const* dp = 0;
1398    int i=0, index=0;
1399    egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1400    if (!cnx) return EGL_FALSE;
1401    if (cnx->hooks->egl.eglCreatePbufferFromClientBuffer) {
1402        return cnx->hooks->egl.eglCreatePbufferFromClientBuffer(
1403                dp->dpys[i], buftype, buffer, dp->configs[i][index], attrib_list);
1404    }
1405    return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1406}
1407