eglApi.cpp revision 28ef8d7911dbfd1bf8256fb43acba894d87fc07a
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
171c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
181c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
19518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <ctype.h>
20518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <stdlib.h>
21518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <string.h>
22518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
23518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <hardware/gralloc.h>
24518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <system/window.h>
25518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
26518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/egl.h>
27518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/eglext.h>
28518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/gl.h>
29518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/glext.h>
30518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
31518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/log.h>
32518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/atomic.h>
337db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian#include <cutils/compiler.h>
34518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/properties.h>
35518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/memory.h>
36518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
37518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/KeyedVector.h>
38518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/SortedVector.h>
39518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/String8.h>
401c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
41518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
42518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_impl.h"
43518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_tls.h"
440469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include "glestrace.h"
45518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "hooks.h"
46518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
47518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_display.h"
48518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_impl.h"
49518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_object.h"
50518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_tls.h"
51ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian#include "egldefs.h"
52518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
53518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianusing namespace android;
54518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
55518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
56518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
57bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian#define EGL_VERSION_HW_ANDROID  0x3143
58bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
59518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstruct extention_map_t {
60518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    const char* name;
61518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType address;
62518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
63518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
64518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic const extention_map_t sExtentionMap[] = {
65518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglLockSurfaceKHR",
66518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
67518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglUnlockSurfaceKHR",
68518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
69518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglCreateImageKHR",
70518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
71518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglDestroyImageKHR",
72518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
731c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    { "eglGetSystemTimeFrequencyNV",
741c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang            (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
751c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    { "eglGetSystemTimeNV",
761c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang            (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
77518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
78518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
79518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// accesses protected by sExtensionMapMutex
80518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
81518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic int sGLExtentionSlot = 0;
82518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
83518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
84518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void(*findProcAddress(const char* name,
85518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const extention_map_t* map, size_t n))() {
86518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (uint32_t i=0 ; i<n ; i++) {
87518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!strcmp(name, map[i].name)) {
88518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return map[i].address;
89518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
90518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
91518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return NULL;
92518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
93518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
94518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
95518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
96518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiannamespace android {
97518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern void setGLHooksThreadSpecific(gl_hooks_t const *value);
98518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern EGLBoolean egl_init_drivers();
99518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
100518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern int gEGLDebugLevel;
101518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern gl_hooks_t gHooksTrace;
102518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian} // namespace android;
103518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
104518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
105518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
106518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline void clearError() { egl_tls_t::clearError(); }
107518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline EGLContext getContext() { return egl_tls_t::getContext(); }
108518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
109518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
110518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
111518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetDisplay(EGLNativeDisplayType display)
112518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
113518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
114518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
115518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    uint32_t index = uint32_t(display);
116518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (index >= NUM_DISPLAYS) {
117518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
118518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
119518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
120518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
121518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
122518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
124518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
125518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return dpy;
126518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
127518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
128518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
129518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Initialization
130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
131518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
133518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
134518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
135518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
136518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t * const dp = get_display(dpy);
137518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
138518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
139518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->initialize(major, minor);
140518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
141518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
142518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
143518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
144518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglTerminate(EGLDisplay dpy)
145518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
146518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // NOTE: don't unload the drivers b/c some APIs can be called
147518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // after eglTerminate() has been called. eglTerminate() only
148518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // terminates an EGLDisplay, not a EGL itself.
149518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
150518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
151518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
152518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t* const dp = get_display(dpy);
153518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
154518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
155518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->terminate();
156518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
157518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
158518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
159518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
160518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
161518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// configuration
162518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
163518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
164518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigs(   EGLDisplay dpy,
165518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs,
166518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint config_size, EGLint *num_config)
167518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
168518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
169518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
170518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
171518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
172518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1737773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    if (num_config==0) {
1747773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
175518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
176518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1777773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    EGLBoolean res = EGL_FALSE;
1787773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    *num_config = 0;
1797773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian
1807773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1817773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    if (cnx->dso) {
1827773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        res = cnx->egl.eglGetConfigs(
1837773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, configs, config_size, num_config);
184518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1857773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian
1867773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return res;
187518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
188518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
189518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
190518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs, EGLint config_size,
191518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            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    if (num_config==0) {
199518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
200518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
201518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
202518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_FALSE;
203518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    *num_config = 0;
204518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
205ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
206ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso) {
2077773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        res = cnx->egl.eglChooseConfig(
2087773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, attrib_list, configs, config_size, num_config);
209518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
210518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
211518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
212518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
213518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
214518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint attribute, EGLint *value)
215518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
216518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
217518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
218518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
219518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
220518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!cnx) return EGL_FALSE;
221518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
222518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return cnx->egl.eglGetConfigAttrib(
2237773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, config, attribute, value);
224518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
225518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
226518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
227518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// surfaces
228518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
229518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
230518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
231518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativeWindowType window,
232518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
233518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
234518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
235518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
236518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
237518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
238518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
239ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        EGLDisplay iDpy = dp->disp.dpy;
240518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint format;
241518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
24281a63350527cafce6929309533c58586878f10b5Mathias Agopian        if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
243e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE("EGLNativeWindowType %p already connected to another API",
24481a63350527cafce6929309533c58586878f10b5Mathias Agopian                    window);
24581a63350527cafce6929309533c58586878f10b5Mathias Agopian            return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
24681a63350527cafce6929309533c58586878f10b5Mathias Agopian        }
24781a63350527cafce6929309533c58586878f10b5Mathias Agopian
248518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // set the native window's buffers format to match this config
249518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cnx->egl.eglGetConfigAttrib(iDpy,
2507773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                config, EGL_NATIVE_VISUAL_ID, &format)) {
251518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (format != 0) {
252bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                int err = native_window_set_buffers_format(window, format);
253bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                if (err != 0) {
254e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                    ALOGE("error setting native window pixel format: %s (%d)",
255bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                            strerror(-err), err);
25681a63350527cafce6929309533c58586878f10b5Mathias Agopian                    native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
257bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                    return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
258bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                }
259518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
260518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
261518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
26259769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        // the EGL spec requires that a new EGLSurface default to swap interval
26359769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        // 1, so explicitly set that on the window here.
26459769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
26559769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        anw->setSwapInterval(anw, 1);
26659769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis
267518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreateWindowSurface(
2687773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                iDpy, config, window, attrib_list);
269518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
270ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, window, surface, cnx);
271518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
272518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
27381a63350527cafce6929309533c58586878f10b5Mathias Agopian
27481a63350527cafce6929309533c58586878f10b5Mathias Agopian        // EGLSurface creation failed
27581a63350527cafce6929309533c58586878f10b5Mathias Agopian        native_window_set_buffers_format(window, 0);
27681a63350527cafce6929309533c58586878f10b5Mathias Agopian        native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
277518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
278518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
279518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
280518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
281518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
282518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativePixmapType pixmap,
283518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
284518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
285518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
286518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
287518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
288518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
289518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
290518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
2917773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, pixmap, attrib_list);
292518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
293ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, cnx);
294518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
295518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
296518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
297518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
298518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
299518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
300518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
301518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
302518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
303518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
304518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
305518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
306518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
307518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
308518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
3097773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, attrib_list);
310518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
311ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, cnx);
312518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
313518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
314518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
315518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
316518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
317518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
318518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
319518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
320518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
321518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
322518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
323518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
324518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
325f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
3265b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
3275b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
328518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
329518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t * const s = get_surface(surface);
330ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
331518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
332518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _s.terminate();
333518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
334518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
335518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
336518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
337518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
338518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
339518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
340518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
341518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
342518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
343518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
344518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
345f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
3465b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
3475b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
348518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
349518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
3507773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return s->cnx->egl.eglQuerySurface(
3517773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, s->surface, attribute, value);
352518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
353518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
354e8696a40e09b24b634214684d18526187b316a2fJamie Gennisvoid EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
3551c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
356e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    clearError();
357e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
358e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    egl_display_t const * const dp = validate_display(dpy);
359e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    if (!dp) {
360e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        return;
361e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    }
362e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
363e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    SurfaceRef _s(dp, surface);
364e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    if (!_s.get()) {
365e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        setError(EGL_BAD_SURFACE, EGL_FALSE);
366e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        return;
367e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    }
368e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
369e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
370e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
371e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    egl_surface_t const * const s = get_surface(surface);
372e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    native_window_set_buffers_timestamp(s->win.get(), timestamp);
373e8696a40e09b24b634214684d18526187b316a2fJamie Gennis}
374e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
375518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
376518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Contexts
377518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
378518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
379518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
380518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLContext share_list, const EGLint *attrib_list)
381518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
382518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
383518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
384518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
385518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
386518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx) {
387518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (share_list != EGL_NO_CONTEXT) {
388518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_context_t* const c = get_context(share_list);
389518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            share_list = c->context;
390518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
391518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLContext context = cnx->egl.eglCreateContext(
3927773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, share_list, attrib_list);
393518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (context != EGL_NO_CONTEXT) {
394518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // figure out if it's a GLESv1 or GLESv2
395518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            int version = 0;
396518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (attrib_list) {
397518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                while (*attrib_list != EGL_NONE) {
398518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint attr = *attrib_list++;
399518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint value = *attrib_list++;
400518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    if (attr == EGL_CONTEXT_CLIENT_VERSION) {
401518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        if (value == 1) {
4027773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                            version = egl_connection_t::GLESv1_INDEX;
403518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        } else if (value == 2) {
4047773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                            version = egl_connection_t::GLESv2_INDEX;
405518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        }
406518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
407518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                };
408518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
409ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_context_t* c = new egl_context_t(dpy, context, config, cnx, version);
4100469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
4110469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy            if (gEGLDebugLevel > 0)
4120469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy                GLTrace_eglCreateContext(version, c);
4130469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
414518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return c;
415518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
416518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
417518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_CONTEXT;
418518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
419518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
420518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
421518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
422518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
423518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
424518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
4255b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!dp)
4265b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return EGL_FALSE;
427518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
428f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    ContextRef _c(dp, ctx);
4295b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
4305b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
431518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
432518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
433ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
434518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
435518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _c.terminate();
436518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
437518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
438518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
439518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
440518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
441518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLSurface read, EGLContext ctx)
442518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
443518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
444518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
445518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = get_display(dpy);
446518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
447518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
4485b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
4495b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
4505b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // a valid but uninitialized display.
451518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
452518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         (draw != EGL_NO_SURFACE) ) {
453518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
454518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
455518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
456518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // get a reference to the object passed in
457f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    ContextRef _c(dp, ctx);
458f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _d(dp, draw);
459f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _r(dp, read);
460518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
461518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // validate the context (if not EGL_NO_CONTEXT)
4625b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
463518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // EGL_NO_CONTEXT is valid
464518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return EGL_FALSE;
465518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
466518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
467518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the underlying implementation's object
468518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext impl_ctx  = EGL_NO_CONTEXT;
469518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_draw = EGL_NO_SURFACE;
470518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_read = EGL_NO_SURFACE;
471518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
472518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are our objects structs passed in
473518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t       * c = NULL;
474518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * d = NULL;
475518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * r = NULL;
476518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
477518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the current objects structs
478518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * cur_c = get_context(getContext());
479518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
480518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx != EGL_NO_CONTEXT) {
481518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        c = get_context(ctx);
482518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_ctx = c->context;
483518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
484518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // no context given, use the implementation of the current context
485518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cur_c == NULL) {
486518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // no current context
487518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
488518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
489518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                return setError(EGL_BAD_MATCH, EGL_FALSE);
490518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
491518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // not an error, there is just no current context.
492518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return EGL_TRUE;
493518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
494518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
495518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
496518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's draw EGLSurface
497518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (draw != EGL_NO_SURFACE) {
498518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        d = get_surface(draw);
499518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_draw = d->surface;
500518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
501518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
502518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's read EGLSurface
503518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (read != EGL_NO_SURFACE) {
504518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        r = get_surface(read);
505518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_read = r->surface;
506518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
507518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
508518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
509fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian    EGLBoolean result = const_cast<egl_display_t*>(dp)->makeCurrent(c, cur_c,
510fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian            draw, read, ctx,
511fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian            impl_draw, impl_read, impl_ctx);
512518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
513518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
514fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian        if (c) {
515518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
516518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(ctx);
5170469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
5180469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy            if (gEGLDebugLevel > 0)
51993a826f78f6313db791e6fc880439189897651b3Siva Velusamy                GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
5200469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
521518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _c.acquire();
522518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _r.acquire();
523518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _d.acquire();
524518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
525518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(&gHooksNoContext);
526518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(EGL_NO_CONTEXT);
527518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
5285fecea776a5f093c21ac1a0ad3552b847d4be23eMathias Agopian    } else {
529e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        // this will ALOGE the error
5305fecea776a5f093c21ac1a0ad3552b847d4be23eMathias Agopian        result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
531518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
532518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
533518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
534518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
535518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
536518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
537518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
538518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
539518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
540518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
541518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
542518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
543518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
544f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    ContextRef _c(dp, ctx);
545518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
546518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
547518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
5487773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return c->cnx->egl.eglQueryContext(
5497773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, c->context, attribute, value);
550518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
551518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
552518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
553518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglGetCurrentContext(void)
554518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
555518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
556518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_CONTEXT.
557518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
558518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
559518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
560518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
561518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return ctx;
562518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
563518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
564518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglGetCurrentSurface(EGLint readdraw)
565518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
566518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
567518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_SURFACE.
568518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
569518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
570518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
571518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
572518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
573518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
574518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
575518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        switch (readdraw) {
576518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            case EGL_READ: return c->read;
577518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            case EGL_DRAW: return c->draw;
578518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
579518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
580518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
581518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
582518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
583518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
584518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetCurrentDisplay(void)
585518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
586518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
587518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_DISPLAY.
588518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
589518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
590518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
591518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
592518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
593518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
594518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
595518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return c->dpy;
596518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
597518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_DISPLAY;
598518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
599518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
600518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitGL(void)
601518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
602518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
603518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
604ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
605ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
606ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
607ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
608ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return cnx->egl.eglWaitGL();
609518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
610518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
611518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitNative(EGLint engine)
612518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
613518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
614518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
615ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
616ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
617ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
618ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
619ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return cnx->egl.eglWaitNative(engine);
620518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
621518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
622518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLint eglGetError(void)
623518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
624ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLint err = EGL_SUCCESS;
625ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
626ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso) {
627ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        err = cnx->egl.eglGetError();
628518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
629ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (err == EGL_SUCCESS) {
630ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        err = egl_tls_t::getError();
631ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    }
632ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return err;
633518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
634518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
635518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
636518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
637518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // eglGetProcAddress() could be the very first function called
638518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // in which case we must make sure we've initialized ourselves, this
639518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // happens the first time egl_get_display() is called.
640518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
641518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
642518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
643518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
644518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        setError(EGL_BAD_PARAMETER, NULL);
645518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return  NULL;
646518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
647518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
648aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis    // The EGL_ANDROID_blob_cache extension should not be exposed to
649aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis    // applications.  It is used internally by the Android EGL layer.
650c42fcf05ce253d5342993b28c412be16e61efffbJamie Gennis    if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID")) {
651aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis        return NULL;
652aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis    }
653aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis
654518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType addr;
655518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
656518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (addr) return addr;
657518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
658aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis
659518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // this protects accesses to sGLExtentionMap and sGLExtentionSlot
660518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_lock(&sExtensionMapMutex);
661518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
662518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        /*
663518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * Since eglGetProcAddress() is not associated to anything, it needs
664518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * to return a function pointer that "works" regardless of what
665518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * the current context is.
666518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
667518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * For this reason, we return a "forwarder", a small stub that takes
668518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * care of calling the function associated with the context
669518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * currently bound.
670518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
671518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * We first look for extensions we've already resolved, if we're seeing
672518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * this extension for the first time, we go through all our
673518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * implementations and call eglGetProcAddress() and record the
674518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * result in the appropriate implementation hooks and return the
675518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * address of the forwarder corresponding to that hook set.
676518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
677518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         */
678518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
679518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const String8 name(procname);
680518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        addr = sGLExtentionMap.valueFor(name);
681518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const int slot = sGLExtentionSlot;
682518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
683e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
684518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                "no more slots for eglGetProcAddress(\"%s\")",
685518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                procname);
686518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
6870469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
6880469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy        gl_hooks_t *debugHooks = GLTrace_getGLHooks();
6890469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
6900469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
691518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
692518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            bool found = false;
693ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
694ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_connection_t* const cnx = &gEGLImpl;
695ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            if (cnx->dso && cnx->egl.eglGetProcAddress) {
696ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                found = true;
697ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                // Extensions are independent of the bound context
6987773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
6997773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
700518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#if EGL_TRACE
701ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                debugHooks->ext.extensions[slot] =
702ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                gHooksTrace.ext.extensions[slot] =
703518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#endif
704ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                        cnx->egl.eglGetProcAddress(procname);
705518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
706ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
707518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (found) {
708518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                addr = gExtensionForwarders[slot];
709518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionMap.add(name, addr);
710518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionSlot++;
711518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
712518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
713518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
714518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_unlock(&sExtensionMapMutex);
715518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return addr;
716518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
717518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
71828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennisclass FrameCompletionThread : public Thread {
71928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennispublic:
72028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
72128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    static void queueSync(EGLSyncKHR sync) {
72228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        static sp<FrameCompletionThread> thread(new FrameCompletionThread);
72328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        static bool running = false;
72428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        if (!running) {
72528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->run("GPUFrameCompletion");
72628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            running = true;
72728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
72828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
72928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(thread->mMutex);
73028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
73128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                    thread->mFramesQueued).string());
73228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mQueue.push_back(sync);
73328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mCondition.signal();
73428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mFramesQueued++;
73528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
73628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
73728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    }
73828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
73928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennisprivate:
74028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
74128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
74228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    virtual bool threadLoop() {
74328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        EGLSyncKHR sync;
74428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        uint32_t frameNum;
74528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
74628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(mMutex);
74728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            while (mQueue.isEmpty()) {
74828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                mCondition.wait(mMutex);
74928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            }
75028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            sync = mQueue[0];
75128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            frameNum = mFramesCompleted;
75228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
75328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
75428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
75528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
75628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                    frameNum).string());
75728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
75828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            if (result == EGL_FALSE) {
75928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
76028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
76128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                ALOGE("FrameCompletion: timeout waiting for fence");
76228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            }
76328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            eglDestroySyncKHR(dpy, sync);
76428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
76528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
76628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(mMutex);
76728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            mQueue.removeAt(0);
76828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            mFramesCompleted++;
76928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ATRACE_INT("GPU Frames Outstanding", mQueue.size());
77028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
77128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        return true;
77228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    }
77328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
77428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    uint32_t mFramesQueued;
77528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    uint32_t mFramesCompleted;
77628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Vector<EGLSyncKHR> mQueue;
77728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Condition mCondition;
77828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Mutex mMutex;
77928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis};
78028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
781518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
782518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
7831c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
784518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
785518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
786518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
787518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
788518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
789f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, draw);
7905b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
7915b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
792518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
7930469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
7940469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    if (gEGLDebugLevel > 0)
7950469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy        GLTrace_eglSwapBuffers(dpy, draw);
7960469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
7970469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
798518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(draw);
7997db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian
8007db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian    if (CC_UNLIKELY(dp->finishOnSwap)) {
8017db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        uint32_t pixel;
8027db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        egl_context_t * const c = get_context( egl_tls_t::getContext() );
8037db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        if (c) {
8047db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian            // glReadPixels() ensures that the frame is complete
8057db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian            s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
8067db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian                    GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
8077db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        }
8087db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian    }
8097db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian
81028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
81128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
81228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    if (CC_UNLIKELY(dp->traceGpuCompletion)) {
81328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        EGLSyncKHR sync = EGL_NO_SYNC_KHR;
81428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
81528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
81628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
81728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        if (sync != EGL_NO_SYNC_KHR) {
81828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            FrameCompletionThread::queueSync(sync);
81928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
82028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    }
82128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
82228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    return result;
823518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
824518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
825518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
826518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            NativePixmapType target)
827518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
828518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
829518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
830518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
831518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
832518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
833f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
8345b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
8355b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
836518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
837518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
838ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
839518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
840518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
841518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianconst char* eglQueryString(EGLDisplay dpy, EGLint name)
842518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
843518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
844518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
845518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
846518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return (const char *) NULL;
847518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
848518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    switch (name) {
849518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VENDOR:
8504b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getVendorString();
851518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VERSION:
8524b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getVersionString();
853518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_EXTENSIONS:
8544b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getExtensionString();
855518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_CLIENT_APIS:
8564b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getClientApiString();
857ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        case EGL_VERSION_HW_ANDROID:
858ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            return dp->disp.queryString.version;
859518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
860518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_PARAMETER, (const char *)0);
861518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
862518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
863518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
864518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
865518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.1
866518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
867518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
868518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSurfaceAttrib(
869518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
870518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
871518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
872518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
873518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
874518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
875518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
876f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
8775b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
8785b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
879518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
880518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
881518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglSurfaceAttrib) {
882518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglSurfaceAttrib(
883ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, attribute, value);
884518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
885518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
886518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
887518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
888518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindTexImage(
889518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
890518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
891518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
892518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
893518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
894518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
895518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
896f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
8975b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
8985b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
899518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
900518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
901518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglBindTexImage) {
902518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglBindTexImage(
903ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, buffer);
904518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
905518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
906518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
907518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
908518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseTexImage(
909518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
910518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
911518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
912518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
913518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
914518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
915518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
916f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
9175b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
9185b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
919518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
920518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
921518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglReleaseTexImage) {
922518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglReleaseTexImage(
923ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, buffer);
924518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
925518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
926518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
927518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
928518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
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    EGLBoolean res = EGL_TRUE;
936ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
937ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglSwapInterval) {
938ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
939518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
940ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
941518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
942518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
943518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
944518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
945518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
946518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.2
947518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
948518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
949518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitClient(void)
950518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
951518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
952518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
953ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
954ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
955ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
956ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
957ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean res;
958ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->egl.eglWaitClient) {
959ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglWaitClient();
960ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    } else {
961ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglWaitGL();
962518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
963518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
964518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
965518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
966518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindAPI(EGLenum api)
967518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
968518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
969518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
970518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
971518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
972518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
973518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
974518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // bind this API on all EGLs
975518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
976ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
977ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglBindAPI) {
978ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglBindAPI(api);
979518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
980518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
981518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
982518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
983518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLenum eglQueryAPI(void)
984518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
985518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
986518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
987518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
988518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
989518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
990518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
991ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
992ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglQueryAPI) {
993ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglQueryAPI();
994518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
995ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
996518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // or, it can only be OpenGL ES
997518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_OPENGL_ES_API;
998518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
999518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1000518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseThread(void)
1001518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1002518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1003518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1004518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // If there is context bound to the thread, release it
1005fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian    egl_display_t::loseCurrent(get_context(getContext()));
1006518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1007ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1008ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglReleaseThread) {
1009ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        cnx->egl.eglReleaseThread();
1010518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1011ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
1012518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_tls_t::clearTLS();
10130469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
10140469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    if (gEGLDebugLevel > 0)
10150469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy        GLTrace_eglReleaseThread();
10160469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
1017518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
1018518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1019518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1020518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferFromClientBuffer(
1021518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1022518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLConfig config, const EGLint *attrib_list)
1023518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1024518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1025518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1026518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const* dp = 0;
1027518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_connection_t* cnx = validate_display_config(dpy, config, dp);
1028518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!cnx) return EGL_FALSE;
1029518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1030518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return cnx->egl.eglCreatePbufferFromClientBuffer(
10317773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, buftype, buffer, config, attrib_list);
1032518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1033518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1034518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1035518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1036518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1037518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 3
1038518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1039518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1040518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1041518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const EGLint *attrib_list)
1042518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1043518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1044518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1045518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1046518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1047518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1048f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
10495b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
10505b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1051518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1052518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1053518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglLockSurfaceKHR) {
1054518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglLockSurfaceKHR(
1055ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, attrib_list);
1056518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1057518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1058518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1059518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1060518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1061518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1062518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1063518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1064518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1065518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1066518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1067f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    SurfaceRef _s(dp, surface);
10685b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
10695b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1070518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1071518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1072518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglUnlockSurfaceKHR) {
1073ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1074518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1075518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1076518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1077518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1078518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1079518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLClientBuffer buffer, const EGLint *attrib_list)
1080518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1081518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1082518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1083518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1084518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_IMAGE_KHR;
1085518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
10867c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    ContextRef _c(dp, ctx);
10877c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_context_t * const c = _c.get();
1088518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
10897c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLImageKHR result = EGL_NO_IMAGE_KHR;
10907c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
10917c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglCreateImageKHR) {
10927c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglCreateImageKHR(
10937c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy,
10947c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                c ? c->context : EGL_NO_CONTEXT,
10957c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                target, buffer, attrib_list);
1096518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
10977c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1098518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1099518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1100518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1101518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1102518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1103518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1104518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1105518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1106518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1107ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
11087c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
11097c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1110518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1111518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
1112518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1113518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1114518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1115518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 5
1116518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1117518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1118518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1119518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1120518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1121518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1122518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1124518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_SYNC_KHR;
1125518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1126518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSyncKHR result = EGL_NO_SYNC_KHR;
11277c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
11287c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
11297c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
11317c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1133518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1134518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1135518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1136518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1137518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1138518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1139518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1140518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1141518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result = EGL_FALSE;
11427c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
11437c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
11447c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1145518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1146518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
1147518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1148518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
11497c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias AgopianEGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
11507c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        EGLint flags, EGLTimeKHR timeout)
1151518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1152518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1153518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1154518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1155518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1156518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
11577c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLBoolean result = EGL_FALSE;
11587c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
11597c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
11607c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglClientWaitSyncKHR(
11617c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy, sync, flags, timeout);
1162518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
11637c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1164518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1165518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
11667c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias AgopianEGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
11677c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        EGLint attribute, EGLint *value)
1168518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1169518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1170518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1171518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_display_t const * const dp = validate_display(dpy);
1172518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1173518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
11747c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLBoolean result = EGL_FALSE;
11757c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
11767c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
11777c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglGetSyncAttribKHR(
11787c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy, sync, attribute, value);
1179518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
11807c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1181518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1182518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1183518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1184518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ANDROID extensions
1185518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1186518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
11874b9511c16195a646242eff833b0af212933b6ecaMathias Agopian/* ANDROID extensions entry-point go here */
11881c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
11891c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// ----------------------------------------------------------------------------
11901c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// NVIDIA extensions
11911c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// ----------------------------------------------------------------------------
11921c3d72a2291827fb15e2ef311a571c860e0dba41Jonas YangEGLuint64NV eglGetSystemTimeFrequencyNV()
11931c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang{
11941c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    clearError();
11951c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
11961c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    if (egl_init_drivers() == EGL_FALSE) {
11971c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
11981c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
11991c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12001c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    EGLuint64NV ret = 0;
1201ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
12021c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
1203ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1204ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglGetSystemTimeFrequencyNV();
12051c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
12061c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12070e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    return setErrorQuiet(EGL_BAD_DISPLAY, 0);
12081c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang}
12091c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12101c3d72a2291827fb15e2ef311a571c860e0dba41Jonas YangEGLuint64NV eglGetSystemTimeNV()
12111c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang{
12121c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    clearError();
12131c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12141c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    if (egl_init_drivers() == EGL_FALSE) {
12151c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
12161c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
12171c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12181c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    EGLuint64NV ret = 0;
1219ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
12201c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
1221ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1222ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglGetSystemTimeNV();
12231c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
12241c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
12250e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    return setErrorQuiet(EGL_BAD_DISPLAY, 0);
12261c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang}
1227