eglApi.cpp revision 5b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09
1518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian/*
2518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Copyright 2007, The Android Open Source Project
3518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
4518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
5518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** you may not use this file except in compliance with the License.
6518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** You may obtain a copy of the License at
7518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
8518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
9518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
10518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Unless required by applicable law or agreed to in writing, software
11518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
12518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** See the License for the specific language governing permissions and
14518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** limitations under the License.
15518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian */
16518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
17518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <ctype.h>
18518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <stdlib.h>
19518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <string.h>
20518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
21518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <hardware/gralloc.h>
22518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <system/window.h>
23518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
24518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/egl.h>
25518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/eglext.h>
26518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/gl.h>
27518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/glext.h>
28518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
29518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/log.h>
30518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/atomic.h>
31518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/properties.h>
32518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/memory.h>
33518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
34518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/KeyedVector.h>
35518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/SortedVector.h>
36518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/String8.h>
37518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
38518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_impl.h"
39518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_tls.h"
40518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "glesv2dbg.h"
41518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "hooks.h"
42518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
43518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_display.h"
44518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_impl.h"
45518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_object.h"
46518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_tls.h"
47518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
48518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianusing namespace android;
49518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
50518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
51518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
52518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic char const * const sVendorString     = "Android";
53518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic char const * const sVersionString    = "1.4 Android META-EGL";
54518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic char const * const sClientApiString  = "OpenGL ES";
55518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic char const * const sExtensionString  =
56518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_image "
57518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_image_base "
58518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_image_pixmap "
59518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_gl_texture_2D_image "
60518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_gl_texture_cubemap_image "
61518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_gl_renderbuffer_image "
62518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_KHR_fence_sync "
63518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_ANDROID_image_native_buffer "
64518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        "EGL_ANDROID_swap_rectangle "
65518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        ;
66518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
67518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstruct extention_map_t {
68518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    const char* name;
69518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType address;
70518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
71518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
72518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic const extention_map_t sExtentionMap[] = {
73518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglLockSurfaceKHR",
74518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
75518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglUnlockSurfaceKHR",
76518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
77518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglCreateImageKHR",
78518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
79518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglDestroyImageKHR",
80518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
81518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglSetSwapRectangleANDROID",
82518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
83518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
84518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
85518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// accesses protected by sExtensionMapMutex
86518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
87518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic int sGLExtentionSlot = 0;
88518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
89518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
90518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void(*findProcAddress(const char* name,
91518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const extention_map_t* map, size_t n))() {
92518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (uint32_t i=0 ; i<n ; i++) {
93518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!strcmp(name, map[i].name)) {
94518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return map[i].address;
95518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
96518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
97518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return NULL;
98518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
99518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
100518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
101518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
102518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiantemplate<typename T>
103518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic __attribute__((noinline))
104518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianint binarySearch(T const sortedArray[], int first, int last, T key) {
105518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    while (first <= last) {
106518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        int mid = (first + last) / 2;
107518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (sortedArray[mid] < key) {
108518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            first = mid + 1;
109518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else if (key < sortedArray[mid]) {
110518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            last = mid - 1;
111518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
112518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return mid;
113518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
114518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
115518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return -1;
116518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
117518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
118518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
119518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
120518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiannamespace android {
121518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern void setGLHooksThreadSpecific(gl_hooks_t const *value);
122518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern EGLBoolean egl_init_drivers();
123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
124518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern int gEGLDebugLevel;
125518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern gl_hooks_t gHooksTrace;
126518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern gl_hooks_t gHooksDebug;
127518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian} // namespace android;
128518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
129518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
131518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline void clearError() { egl_tls_t::clearError(); }
132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline EGLContext getContext() { return egl_tls_t::getContext(); }
133518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
134518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
135518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
136518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetDisplay(EGLNativeDisplayType display)
137518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
138518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
139518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
140518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    uint32_t index = uint32_t(display);
141518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (index >= NUM_DISPLAYS) {
142518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
143518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
144518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
145518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
146518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
147518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
148518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
149518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
150518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return dpy;
151518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
152518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
153518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
154518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Initialization
155518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
156518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
157518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
158518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
159518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
160518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
161518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t * const dp = get_display(dpy);
162518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
163518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
164518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->initialize(major, minor);
165518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
166518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
167518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
168518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
169518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglTerminate(EGLDisplay dpy)
170518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
171518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // NOTE: don't unload the drivers b/c some APIs can be called
172518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // after eglTerminate() has been called. eglTerminate() only
173518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // terminates an EGLDisplay, not a EGL itself.
174518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
175518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
176518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
177518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t* const dp = get_display(dpy);
178518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
179518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
180518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->terminate();
181518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
182518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
183518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
184518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
185518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
186518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// configuration
187518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
188518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
189518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigs(   EGLDisplay dpy,
190518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs,
191518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint config_size, EGLint *num_config)
192518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
193518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
194518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
195518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
196518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
197518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
198518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    GLint numConfigs = dp->numTotalConfigs;
199518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!configs) {
200518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        *num_config = numConfigs;
201518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return EGL_TRUE;
202518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
203518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
204518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    GLint n = 0;
205518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
206518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        *configs++ = EGLConfig(i);
207518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        config_size--;
208518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        n++;
209518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
210518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
211518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    *num_config = n;
212518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
213518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
214518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
215518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
216518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs, EGLint config_size,
217518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint *num_config)
218518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
219518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
220518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
221518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
222518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
223518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
224518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (num_config==0) {
225518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
226518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
227518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
228518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLint n;
229518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_FALSE;
230518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    *num_config = 0;
231518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
232518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
233518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
234518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // to do this, we have to go through the attrib_list array once
235518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // to figure out both its size and if it contains an EGL_CONFIG_ID
236518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // key. If so, the full array is copied and patched.
237518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // NOTE: we assume that there can be only one occurrence
238518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // of EGL_CONFIG_ID.
239518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
240518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLint patch_index = -1;
241518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    GLint attr;
242518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    size_t size = 0;
243518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (attrib_list) {
244518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        while ((attr=attrib_list[size]) != EGL_NONE) {
245518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (attr == EGL_CONFIG_ID)
246518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                patch_index = size;
247518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            size += 2;
248518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
249518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
250518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (patch_index >= 0) {
251518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        size += 2; // we need copy the sentinel as well
252518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
253518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (new_list == 0)
254518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_ALLOC, EGL_FALSE);
255518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        memcpy(new_list, attrib_list, size*sizeof(EGLint));
256518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
257518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // patch the requested EGL_CONFIG_ID
258518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        bool found = false;
259518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLConfig ourConfig(0);
260518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint& configId(new_list[patch_index+1]);
261518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
262518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (dp->configs[i].configId == configId) {
263518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                ourConfig = EGLConfig(i);
264518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                configId = dp->configs[i].implConfigId;
265518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                found = true;
266518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                break;
267518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
268518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
269518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
270518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
271518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (found && cnx->dso) {
272518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // and switch to the new list
273518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            attrib_list = const_cast<const EGLint *>(new_list);
274518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
275518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // At this point, the only configuration that can match is
276518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // dp->configs[i][index], however, we don't know if it would be
277518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // rejected because of the other attributes, so we do have to call
278518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // cnx->egl.eglChooseConfig() -- but we don't have to loop
279518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // through all the EGLimpl[].
280518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // We also know we can only get a single config back, and we know
281518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // which one.
282518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
283518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            res = cnx->egl.eglChooseConfig(
284518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
285518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    attrib_list, configs, config_size, &n);
286518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (res && n>0) {
287518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // n has to be 0 or 1, by construction, and we already know
288518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // which config it will return (since there can be only one).
289518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (configs) {
290518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    configs[0] = ourConfig;
291518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
292518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                *num_config = 1;
293518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
294518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
295518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
296518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        free(const_cast<EGLint *>(attrib_list));
297518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return res;
298518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
299518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
300518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
301518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
302518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
303518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso) {
304518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->egl.eglChooseConfig(
305518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
306518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (configs) {
307518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    // now we need to convert these client EGLConfig to our
308518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    // internal EGLConfig format.
309518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    // This is done in O(n Log(n)) time.
310518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    for (int j=0 ; j<n ; j++) {
311518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        egl_config_t key(i, configs[j]);
312518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        intptr_t index = binarySearch<egl_config_t>(
313518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                dp->configs, 0, dp->numTotalConfigs, key);
314518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        if (index >= 0) {
315518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            configs[j] = EGLConfig(index);
316518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        } else {
317518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            return setError(EGL_BAD_CONFIG, EGL_FALSE);
318518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        }
319518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
320518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    configs += n;
321518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    config_size -= n;
322518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
323518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                *num_config += n;
324518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                res = EGL_TRUE;
325518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
326518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
327518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
328518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
329518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
330518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
331518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
332518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint attribute, EGLint *value)
333518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
334518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
335518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
336518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
337518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
338518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!cnx) return EGL_FALSE;
339518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
340518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (attribute == EGL_CONFIG_ID) {
341518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        *value = dp->configs[intptr_t(config)].configId;
342518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return EGL_TRUE;
343518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
344518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return cnx->egl.eglGetConfigAttrib(
345518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
346518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            dp->configs[intptr_t(config)].config, attribute, value);
347518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
348518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
349518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
350518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// surfaces
351518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
352518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
353518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
354518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativeWindowType window,
355518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
356518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
357518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
358518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
359518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
360518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
361518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
362518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
363518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLConfig iConfig = dp->configs[intptr_t(config)].config;
364518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint format;
365518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
366518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // set the native window's buffers format to match this config
367518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->egl.eglGetConfigAttrib(iDpy,
368518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
369518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (format != 0) {
370518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                native_window_set_buffers_geometry(window, 0, 0, format);
371518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
372518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
373518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
374518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreateWindowSurface(
375518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                iDpy, iConfig, window, attrib_list);
376518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
377518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
378518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->configs[intptr_t(config)].impl, cnx);
379518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
380518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
381518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
382518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
383518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
384518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
385518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
386518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativePixmapType pixmap,
387518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
388518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
389518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
390518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
391518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
392518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
393518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
394518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
395518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
396518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->configs[intptr_t(config)].config, pixmap, attrib_list);
397518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
398518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
399518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->configs[intptr_t(config)].impl, cnx);
400518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
401518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
402518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
403518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
404518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
405518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
406518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
407518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
408518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
409518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
410518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
411518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
412518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
413518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
414518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
415518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
416518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->configs[intptr_t(config)].config, attrib_list);
417518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
418518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
419518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->configs[intptr_t(config)].impl, cnx);
420518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
421518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
422518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
423518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
424518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
425518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
426518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
427518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
428518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
429518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
430518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
431518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
432518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
433518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
4345b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
4355b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
436518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
437518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t * const s = get_surface(surface);
438518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result = s->cnx->egl.eglDestroySurface(
439518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            dp->disp[s->impl].dpy, s->surface);
440518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
441518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (s->win != NULL) {
442518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
443518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
444518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _s.terminate();
445518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
446518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
447518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
448518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
449518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
450518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
451518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
452518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
453518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
454518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
455518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
456518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
457518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
4585b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
4595b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
460518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
461518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
462518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result(EGL_TRUE);
463518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (attribute == EGL_CONFIG_ID) {
464518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // We need to remap EGL_CONFIG_IDs
465518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        *value = dp->configs[intptr_t(s->config)].configId;
466518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
467518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = s->cnx->egl.eglQuerySurface(
468518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, attribute, value);
469518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
470518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
471518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
472518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
473518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
474518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
475518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Contexts
476518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
477518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
478518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
479518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLContext share_list, const EGLint *attrib_list)
480518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
481518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
482518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
483518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
484518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
485518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
486518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (share_list != EGL_NO_CONTEXT) {
487518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_context_t* const c = get_context(share_list);
488518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            share_list = c->context;
489518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
490518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLContext context = cnx->egl.eglCreateContext(
491518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
492518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->configs[intptr_t(config)].config,
493518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                share_list, attrib_list);
494518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (context != EGL_NO_CONTEXT) {
495518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // figure out if it's a GLESv1 or GLESv2
496518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            int version = 0;
497518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (attrib_list) {
498518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                while (*attrib_list != EGL_NONE) {
499518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint attr = *attrib_list++;
500518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint value = *attrib_list++;
501518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    if (attr == EGL_CONTEXT_CLIENT_VERSION) {
502518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        if (value == 1) {
503518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            version = GLESv1_INDEX;
504518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        } else if (value == 2) {
505518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            version = GLESv2_INDEX;
506518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        }
507518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
508518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                };
509518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
510518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_context_t* c = new egl_context_t(dpy, context, config,
511518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    dp->configs[intptr_t(config)].impl, cnx, version);
512518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return c;
513518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
514518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
515518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_CONTEXT;
516518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
517518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
518518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
519518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
520518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
521518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
522518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
5235b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!dp)
5245b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return EGL_FALSE;
525518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
526518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
5275b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
5285b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
529518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
530518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
531518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result = c->cnx->egl.eglDestroyContext(
532518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            dp->disp[c->impl].dpy, c->context);
533518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
534518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _c.terminate();
535518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
536518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
537518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
538518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
539518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void loseCurrent(egl_context_t * cur_c)
540518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
541518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cur_c) {
542518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_surface_t * cur_r = get_surface(cur_c->read);
543518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_surface_t * cur_d = get_surface(cur_c->draw);
544518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
545518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // by construction, these are either 0 or valid (possibly terminated)
546518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // it should be impossible for these to be invalid
547518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        ContextRef _cur_c(cur_c);
548518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        SurfaceRef _cur_r(cur_r);
549518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        SurfaceRef _cur_d(cur_d);
550518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
551518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        cur_c->read = NULL;
552518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        cur_c->draw = NULL;
553518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
554518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _cur_c.release();
555518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _cur_r.release();
556518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _cur_d.release();
557518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
558518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
559518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
560518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
561518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLSurface read, EGLContext ctx)
562518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
563518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
564518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
565518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = get_display(dpy);
566518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
567518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
5685b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
5695b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
5705b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // a valid but uninitialized display.
571518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
572518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         (draw != EGL_NO_SURFACE) ) {
573518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
574518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
575518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
576518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // get a reference to the object passed in
577518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
578518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _d(draw);
579518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _r(read);
580518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
581518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // validate the context (if not EGL_NO_CONTEXT)
5825b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
583518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // EGL_NO_CONTEXT is valid
584518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return EGL_FALSE;
585518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
586518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
587518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the underlying implementation's object
588518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext impl_ctx  = EGL_NO_CONTEXT;
589518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_draw = EGL_NO_SURFACE;
590518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_read = EGL_NO_SURFACE;
591518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
592518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are our objects structs passed in
593518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t       * c = NULL;
594518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * d = NULL;
595518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * r = NULL;
596518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
597518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the current objects structs
598518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * cur_c = get_context(getContext());
599518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
600518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx != EGL_NO_CONTEXT) {
601518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        c = get_context(ctx);
602518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_ctx = c->context;
603518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
604518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // no context given, use the implementation of the current context
605518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cur_c == NULL) {
606518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // no current context
607518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
608518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
609518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                return setError(EGL_BAD_MATCH, EGL_FALSE);
610518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
611518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // not an error, there is just no current context.
612518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return EGL_TRUE;
613518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
614518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
615518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
616518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's draw EGLSurface
617518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (draw != EGL_NO_SURFACE) {
618518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        d = get_surface(draw);
619518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // make sure the EGLContext and EGLSurface passed in are for
620518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // the same driver
621518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (c && d->impl != c->impl)
622518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_MATCH, EGL_FALSE);
623518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_draw = d->surface;
624518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
625518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
626518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's read EGLSurface
627518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (read != EGL_NO_SURFACE) {
628518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        r = get_surface(read);
629518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // make sure the EGLContext and EGLSurface passed in are for
630518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // the same driver
631518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (c && r->impl != c->impl)
632518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_MATCH, EGL_FALSE);
633518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_read = r->surface;
634518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
635518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
636518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result;
637518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
638518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (c) {
639518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = c->cnx->egl.eglMakeCurrent(
640518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
641518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
642518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = cur_c->cnx->egl.eglMakeCurrent(
643518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
644518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
645518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
646518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
647518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
648518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        loseCurrent(cur_c);
649518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
650518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (ctx != EGL_NO_CONTEXT) {
651518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
652518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(ctx);
653518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (gEGLDebugLevel > 0) {
654518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                CreateDbgContext(c->version, c->cnx->hooks[c->version]);
655518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
656518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _c.acquire();
657518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _r.acquire();
658518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _d.acquire();
659518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            c->read = read;
660518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            c->draw = draw;
661518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
662518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(&gHooksNoContext);
663518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(EGL_NO_CONTEXT);
664518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
665518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
666518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
667518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
668518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
669518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
670518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
671518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
672518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
673518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
674518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
675518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
676518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
677518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
678518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
679518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
680518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
681518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
682518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
683518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result(EGL_TRUE);
684518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (attribute == EGL_CONFIG_ID) {
685518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        *value = dp->configs[intptr_t(c->config)].configId;
686518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
687518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // We need to remap EGL_CONFIG_IDs
688518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = c->cnx->egl.eglQueryContext(
689518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, c->context, attribute, value);
690518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
691518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
692518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
693518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
694518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
695518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglGetCurrentContext(void)
696518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
697518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
698518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_CONTEXT.
699518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
700518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
701518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
702518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
703518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return ctx;
704518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
705518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
706518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglGetCurrentSurface(EGLint readdraw)
707518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
708518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
709518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_SURFACE.
710518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
711518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
712518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
713518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
714518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
715518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
716518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
717518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        switch (readdraw) {
718518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            case EGL_READ: return c->read;
719518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            case EGL_DRAW: return c->draw;
720518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
721518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
722518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
723518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
724518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
725518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
726518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetCurrentDisplay(void)
727518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
728518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
729518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_DISPLAY.
730518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
731518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
732518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
733518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
734518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
735518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
736518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
737518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return c->dpy;
738518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
739518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_DISPLAY;
740518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
741518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
742518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitGL(void)
743518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
744518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
745518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would return GL_TRUE, which isn't wrong.
746518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
747518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
748518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
749518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
750518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
751518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
752518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
753518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
754518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (uint32_t(c->impl)>=2)
755518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
756518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[c->impl];
757518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!cnx->dso)
758518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
759518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        res = cnx->egl.eglWaitGL();
760518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
761518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
762518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
763518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
764518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitNative(EGLint engine)
765518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
766518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
767518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would return GL_TRUE, which isn't wrong.
768518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
769518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
770518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
771518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
772518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
773518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
774518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
775518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
776518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (uint32_t(c->impl)>=2)
777518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
778518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[c->impl];
779518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!cnx->dso)
780518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
781518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        res = cnx->egl.eglWaitNative(engine);
782518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
783518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
784518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
785518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
786518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLint eglGetError(void)
787518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
788518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLint result = EGL_SUCCESS;
789518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLint err;
790518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
791518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        err = EGL_SUCCESS;
792518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
793518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso)
794518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            err = cnx->egl.eglGetError();
795518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
796518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            result = err;
797518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
798518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    err = egl_tls_t::getError();
799518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_SUCCESS)
800518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = err;
801518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
802518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
803518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
804518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Note: Similar implementations of these functions also exist in
805518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// gl2.cpp and gl.cpp, and are used by applications that call the
806518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// exported entry points directly.
807518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiantypedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
808518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiantypedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
809518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
810518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
811518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
812518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
813518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
814518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
815518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    GLeglImageOES implImage =
816518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
817518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    glEGLImageTargetTexture2DOES_impl(target, implImage);
818518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
819518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
820518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
821518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
822518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    GLeglImageOES implImage =
823518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
824518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
825518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
826518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
827518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
828518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
829518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // eglGetProcAddress() could be the very first function called
830518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // in which case we must make sure we've initialized ourselves, this
831518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // happens the first time egl_get_display() is called.
832518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
833518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
834518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
835518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
836518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        setError(EGL_BAD_PARAMETER, NULL);
837518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return  NULL;
838518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
839518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
840518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType addr;
841518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
842518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (addr) return addr;
843518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
844518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // this protects accesses to sGLExtentionMap and sGLExtentionSlot
845518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_lock(&sExtensionMapMutex);
846518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
847518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        /*
848518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * Since eglGetProcAddress() is not associated to anything, it needs
849518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * to return a function pointer that "works" regardless of what
850518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * the current context is.
851518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
852518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * For this reason, we return a "forwarder", a small stub that takes
853518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * care of calling the function associated with the context
854518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * currently bound.
855518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
856518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * We first look for extensions we've already resolved, if we're seeing
857518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * this extension for the first time, we go through all our
858518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * implementations and call eglGetProcAddress() and record the
859518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * result in the appropriate implementation hooks and return the
860518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * address of the forwarder corresponding to that hook set.
861518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
862518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         */
863518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
864518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const String8 name(procname);
865518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        addr = sGLExtentionMap.valueFor(name);
866518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const int slot = sGLExtentionSlot;
867518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
868518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
869518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                "no more slots for eglGetProcAddress(\"%s\")",
870518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                procname);
871518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
872518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
873518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            bool found = false;
874518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
875518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                egl_connection_t* const cnx = &gEGLImpl[i];
876518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (cnx->dso && cnx->egl.eglGetProcAddress) {
877518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    found = true;
878518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    // Extensions are independent of the bound context
879518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
880518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
881518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#if EGL_TRACE
882518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
883518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#endif
884518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            cnx->egl.eglGetProcAddress(procname);
885518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
886518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
887518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (found) {
888518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                addr = gExtensionForwarders[slot];
889518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
890518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
891518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
892518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
893518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
894518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
895518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
896518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
897518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
898518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
899518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionMap.add(name, addr);
900518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionSlot++;
901518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
902518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
903518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
904518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_unlock(&sExtensionMapMutex);
905518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return addr;
906518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
907518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
908518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
909518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
910518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
911518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (gEGLDebugLevel > 0)
912518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        Debug_eglSwapBuffers(dpy, draw);
913518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
914518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
915518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
916518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
917518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
918518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
919518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(draw);
9205b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
9215b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
922518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
923518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(draw);
924518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
925518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
926518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
927518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
928518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            NativePixmapType target)
929518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
930518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
931518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
932518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
933518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
934518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
935518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
9365b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
9375b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
938518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
939518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
940518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return s->cnx->egl.eglCopyBuffers(
941518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            dp->disp[s->impl].dpy, s->surface, target);
942518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
943518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
944518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianconst char* eglQueryString(EGLDisplay dpy, EGLint name)
945518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
946518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
947518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
948518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
949518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return (const char *) NULL;
950518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
951518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    switch (name) {
952518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VENDOR:
953518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return sVendorString;
954518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VERSION:
955518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return sVersionString;
956518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_EXTENSIONS:
957518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return sExtensionString;
958518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_CLIENT_APIS:
959518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return sClientApiString;
960518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
961518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_PARAMETER, (const char *)0);
962518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
963518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
964518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
965518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
966518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.1
967518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
968518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
969518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSurfaceAttrib(
970518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
971518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
972518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
973518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
974518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
975518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
976518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
977518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
9785b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
9795b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
980518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
981518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
982518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglSurfaceAttrib) {
983518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglSurfaceAttrib(
984518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, attribute, value);
985518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
986518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
987518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
988518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
989518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindTexImage(
990518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
991518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
992518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
993518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
994518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
995518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
996518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
997518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
9985b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
9995b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1000518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1001518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1002518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglBindTexImage) {
1003518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglBindTexImage(
1004518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, buffer);
1005518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1006518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1007518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1008518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1009518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseTexImage(
1010518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1011518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1012518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1013518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1014518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1015518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1016518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1017518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
10185b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
10195b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1020518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1021518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1022518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglReleaseTexImage) {
1023518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglReleaseTexImage(
1024518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, buffer);
1025518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1026518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1027518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1028518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1029518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1030518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1031518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1032518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1033518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1034518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1035518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1036518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
1037518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1038518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
1039518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso) {
1040518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->egl.eglSwapInterval) {
1041518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (cnx->egl.eglSwapInterval(
1042518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        dp->disp[i].dpy, interval) == EGL_FALSE) {
1043518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    res = EGL_FALSE;
1044518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
1045518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1046518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1047518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1048518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1049518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1050518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1051518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1052518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1053518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.2
1054518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1055518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1056518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitClient(void)
1057518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1058518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1059518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1060518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
1061518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would return GL_TRUE, which isn't wrong.
1062518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
1063518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
1064518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
1065518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
1066518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1067518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (uint32_t(c->impl)>=2)
1068518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1069518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[c->impl];
1070518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!cnx->dso)
1071518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1072518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->egl.eglWaitClient) {
1073518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            res = cnx->egl.eglWaitClient();
1074518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
1075518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            res = cnx->egl.eglWaitGL();
1076518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1077518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1078518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1079518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1080518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1081518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindAPI(EGLenum api)
1082518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1083518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1084518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1085518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
1086518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1087518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1088518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1089518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // bind this API on all EGLs
1090518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
1091518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1092518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
1093518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso) {
1094518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->egl.eglBindAPI) {
1095518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
1096518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    res = EGL_FALSE;
1097518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
1098518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1099518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1100518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1101518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1102518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1103518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1104518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLenum eglQueryAPI(void)
1105518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1106518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1107518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1108518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
1109518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1110518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1111518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1112518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1113518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
1114518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso) {
1115518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->egl.eglQueryAPI) {
1116518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // the first one we find is okay, because they all
1117518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // should be the same
1118518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                return cnx->egl.eglQueryAPI();
1119518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1120518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1121518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1122518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // or, it can only be OpenGL ES
1123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_OPENGL_ES_API;
1124518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1125518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1126518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseThread(void)
1127518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1128518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1129518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // If there is context bound to the thread, release it
1131518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    loseCurrent(get_context(getContext()));
1132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1133518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1134518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
1135518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->dso) {
1136518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->egl.eglReleaseThread) {
1137518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                cnx->egl.eglReleaseThread();
1138518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1139518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1140518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1141518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_tls_t::clearTLS();
1142518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    dbgReleaseThread();
1143518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
1144518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1145518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1146518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferFromClientBuffer(
1147518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1148518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLConfig config, const EGLint *attrib_list)
1149518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1150518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1151518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1152518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
1153518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
1154518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!cnx) return EGL_FALSE;
1155518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1156518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return cnx->egl.eglCreatePbufferFromClientBuffer(
1157518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1158518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                buftype, buffer,
1159518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->configs[intptr_t(config)].config, attrib_list);
1160518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1161518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1162518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1163518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1164518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1165518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 3
1166518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1167518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1168518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1169518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const EGLint *attrib_list)
1170518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1171518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1172518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1173518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1174518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1175518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1176518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
11775b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
11785b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1179518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1180518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1181518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglLockSurfaceKHR) {
1182518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglLockSurfaceKHR(
1183518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, attrib_list);
1184518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1185518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1186518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1187518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1188518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1189518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1190518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1191518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1192518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1193518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1194518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1195518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(surface);
11965b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
11975b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1198518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1199518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1200518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglUnlockSurfaceKHR) {
1201518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglUnlockSurfaceKHR(
1202518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface);
1203518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1204518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1205518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1206518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1207518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1208518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLClientBuffer buffer, const EGLint *attrib_list)
1209518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1210518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1211518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1212518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1213518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_IMAGE_KHR;
1214518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1215518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx != EGL_NO_CONTEXT) {
1216518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        ContextRef _c(ctx);
12175b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        if (!_c.get())
12185b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian            return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1219518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t * const c = get_context(ctx);
1220518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // since we have an EGLContext, we know which implementation to use
1221518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
1222518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
1223518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (image == EGL_NO_IMAGE_KHR)
1224518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return image;
1225518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1226518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_image_t* result = new egl_image_t(dpy, ctx);
1227518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result->images[c->impl] = image;
1228518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return (EGLImageKHR)result;
1229518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
1230518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // EGL_NO_CONTEXT is a valid parameter
1231518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1232518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        /* Since we don't have a way to know which implementation to call,
1233518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * we're calling all of them. If at least one of the implementation
1234518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * succeeded, this is a success.
1235518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         */
1236518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1237518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint currentError = eglGetError();
1238518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1239518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
1240518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        bool success = false;
1241518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1242518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_connection_t* const cnx = &gEGLImpl[i];
1243518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            implImages[i] = EGL_NO_IMAGE_KHR;
1244518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->dso) {
1245518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (cnx->egl.eglCreateImageKHR) {
1246518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    implImages[i] = cnx->egl.eglCreateImageKHR(
1247518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            dp->disp[i].dpy, ctx, target, buffer, attrib_list);
1248518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    if (implImages[i] != EGL_NO_IMAGE_KHR) {
1249518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        success = true;
1250518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
1251518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
1252518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1253518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1254518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1255518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!success) {
1256518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // failure, if there was an error when we entered this function,
1257518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // the error flag must not be updated.
1258518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // Otherwise, the error is whatever happened in the implementation
1259518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // that faulted.
1260518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (currentError != EGL_SUCCESS) {
1261518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                setError(currentError, EGL_NO_IMAGE_KHR);
1262518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1263518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return EGL_NO_IMAGE_KHR;
1264518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
1265518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // In case of success, we need to clear all error flags
1266518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // (especially those caused by the implementation that didn't
1267518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // succeed). TODO: we could avoid this if we knew this was
1268518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // a "full" success (all implementation succeeded).
1269518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            eglGetError();
1270518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1271518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1272518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_image_t* result = new egl_image_t(dpy, ctx);
1273518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        memcpy(result->images, implImages, sizeof(implImages));
1274518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return (EGLImageKHR)result;
1275518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1276518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1277518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1278518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1279518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1280518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1281518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1282518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1283518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1284518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1285518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ImageRef _i(img);
1286518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1287518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1288518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_image_t* image = get_image(img);
1289518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    bool success = false;
1290518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1291518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_connection_t* const cnx = &gEGLImpl[i];
1292518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (image->images[i] != EGL_NO_IMAGE_KHR) {
1293518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (cnx->dso) {
1294518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                if (cnx->egl.eglDestroyImageKHR) {
1295518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    if (cnx->egl.eglDestroyImageKHR(
1296518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            dp->disp[i].dpy, image->images[i])) {
1297518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        success = true;
1298518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
1299518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                }
1300518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
1301518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
1302518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1303518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!success)
1304518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return EGL_FALSE;
1305518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1306518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    _i.terminate();
1307518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1308518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
1309518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1310518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1311518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1312518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 5
1313518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1314518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1315518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1316518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1317518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1318518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1319518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1320518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1321518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_SYNC_KHR;
1322518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1323518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = eglGetCurrentContext();
1324518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
13255b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
13265b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
13275b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian
1328518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
1329518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSyncKHR result = EGL_NO_SYNC_KHR;
1330518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (c->cnx->egl.eglCreateSyncKHR) {
1331518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1332518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, type, attrib_list);
1333518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (sync == EGL_NO_SYNC_KHR)
1334518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return sync;
1335518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1336518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1337518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return (EGLSyncKHR)result;
1338518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1339518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1340518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1341518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1342518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1343518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1344518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1345518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1346518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1347518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SyncRef _s(sync);
1348518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1349518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_sync_t* syncObject = get_sync(sync);
1350518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1351518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = syncObject->context;
1352518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
13535b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
13545b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1355518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1356518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result = EGL_FALSE;
1357518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
1358518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (c->cnx->egl.eglDestroySyncKHR) {
1359518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        result = c->cnx->egl.eglDestroySyncKHR(
1360518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, syncObject->sync);
1361518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (result)
1362518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _s.terminate();
1363518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1364518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
1365518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1366518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1367518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1368518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1369518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1370518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1371518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1372518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1373518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1374518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SyncRef _s(sync);
1375518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1376518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_sync_t* syncObject = get_sync(sync);
1377518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1378518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = syncObject->context;
1379518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
13805b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
13815b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1382518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1383518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
1384518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (c->cnx->egl.eglClientWaitSyncKHR) {
1385518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return c->cnx->egl.eglClientWaitSyncKHR(
1386518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
1387518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1388518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1389518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_FALSE;
1390518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1391518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1392518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
1393518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1394518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1395518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1396518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1397518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1398518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1399518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SyncRef _s(sync);
14005b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
14015b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1402518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
14035b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    egl_sync_t* syncObject = get_sync(sync);
1404518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = syncObject->context;
1405518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    ContextRef _c(ctx);
14065b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
14075b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1408518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1409518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
1410518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (c->cnx->egl.eglGetSyncAttribKHR) {
1411518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return c->cnx->egl.eglGetSyncAttribKHR(
1412518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
1413518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1414518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1415518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_FALSE;
1416518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1417518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1418518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1419518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ANDROID extensions
1420518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1421518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1422518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1423518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint left, EGLint top, EGLint width, EGLint height)
1424518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1425518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1426518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1427518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1428518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1429518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1430518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    SurfaceRef _s(draw);
14315b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
14325b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1433518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1434518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(draw);
1435518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1436518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglSetSwapRectangleANDROID(
1437518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                dp->disp[s->impl].dpy, s->surface, left, top, width, height);
1438518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1439518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, NULL);
1440518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1441