eglApi.cpp revision 408e59f63aaaba30fb6db1477e985025dda085cd
14774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall/*
2518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Copyright 2007, The Android Open Source Project
3518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
44774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** Licensed under the Apache License, Version 2.0 (the "License");
54774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** you may not use this file except in compliance with the License.
64774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** You may obtain a copy of the License at
7518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
84774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall **     http://www.apache.org/licenses/LICENSE-2.0
9518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
104774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** Unless required by applicable law or agreed to in writing, software
114774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** distributed under the License is distributed on an "AS IS" BASIS,
124774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall ** 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
19c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall#include <dlfcn.h>
20518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <ctype.h>
21518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <stdlib.h>
22518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <string.h>
23518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
24518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <hardware/gralloc.h>
25518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <system/window.h>
26518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
27518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/egl.h>
28518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/eglext.h>
29518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
30518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/log.h>
31518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/atomic.h>
327db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian#include <cutils/compiler.h>
33518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/properties.h>
34518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <cutils/memory.h>
35518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
36518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/KeyedVector.h>
37518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/SortedVector.h>
38518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/String8.h>
391c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
40518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
4139c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../egl_impl.h"
4239c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../glestrace.h"
4339c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../hooks.h"
44518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
45518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_display.h"
46518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_object.h"
47518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_tls.h"
48ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian#include "egldefs.h"
49518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
50518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianusing namespace android;
51518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
52a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall// This extension has not been ratified yet, so can't be shipped.
53a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall// Implementation is incomplete and untested.
54a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall#define ENABLE_EGL_KHR_GL_COLORSPACE 0
55a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall
56518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
57518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
58e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopiannamespace android {
59e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
60518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstruct extention_map_t {
61518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    const char* name;
62518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType address;
63518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
64518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
65e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian/*
6621558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * This is the list of EGL extensions exposed to applications.
67e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian *
6821558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
6921558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * wrapper and are always available.
70e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian *
7121558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * The rest (gExtensionString) depend on support in the EGL driver, and are
7221558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * only available if the driver supports them. However, some of these must be
7321558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * supported because they are used by the Android system itself; these are
7421558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * listd as mandatory below and are required by the CDD. The system *assumes*
7521558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * the mandatory extensions are present and may not function properly if some
7621558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * are missing.
7721558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall *
7821558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall * NOTE: Both strings MUST have a single space as the last character.
79e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian */
8021558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hallextern char const * const gBuiltinExtensionString =
8121558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall        "EGL_KHR_get_all_proc_addresses "
8221558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall        "EGL_ANDROID_presentation_time "
83a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        "EGL_KHR_swap_buffers_with_damage "
8421558daf691dbcdff4a41e659fd013273db4d0b7Jesse Hall        ;
85e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopianextern char const * const gExtensionString  =
86e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_image "                        // mandatory
87e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_image_base "                   // mandatory
88e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_image_pixmap "
89e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_lock_surface "
90a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
91c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        "EGL_KHR_gl_colorspace "
92a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall#endif
93e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_gl_texture_2D_image "
94e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_gl_texture_cubemap_image "
95e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_gl_renderbuffer_image "
96e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_reusable_sync "
97e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_KHR_fence_sync "
98f6d1c3930eeba6b089ba12fb82ecad1c6622e550Jamie Gennis        "EGL_KHR_create_context "
99e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_EXT_create_context_robustness "
100e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_NV_system_time "
101e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        "EGL_ANDROID_image_native_buffer "      // mandatory
1022bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian        "EGL_KHR_wait_sync "                    // strongly recommended
103dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis        "EGL_ANDROID_recordable "               // mandatory
104a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        "EGL_KHR_partial_update "               // strongly recommended
105a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        "EGL_EXT_buffer_age "                   // strongly recommended with partial_update
106408e59f63aaaba30fb6db1477e985025dda085cdJesse Hall        "EGL_KHR_create_context_no_error "
107e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        ;
108e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
109e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian// extensions not exposed to applications but used by the ANDROID system
110e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian//      "EGL_ANDROID_blob_cache "               // strongly recommended
111e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian//      "EGL_IMG_hibernate_process "            // optional
112e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian//      "EGL_ANDROID_native_fence_sync "        // strongly recommended
113e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian//      "EGL_ANDROID_framebuffer_target "       // mandatory for HWC 1.1
114dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis//      "EGL_ANDROID_image_crop "               // optional
115e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
116e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian/*
117e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian * EGL Extensions entry-points exposed to 3rd party applications
118e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian * (keep in sync with gExtensionString above)
119e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian *
120e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian */
121e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopianstatic const extention_map_t sExtensionMap[] = {
122e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    // EGL_KHR_lock_surface
123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglLockSurfaceKHR",
124518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
125518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglUnlockSurfaceKHR",
126518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
127e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
128e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    // EGL_KHR_image, EGL_KHR_image_base
129518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglCreateImageKHR",
130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
131518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    { "eglDestroyImageKHR",
132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
133e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
134e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
135e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglCreateSyncKHR",
136e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
137e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglDestroySyncKHR",
138e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
139e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglClientWaitSyncKHR",
140e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
141e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglSignalSyncKHR",
142e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
143e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglGetSyncAttribKHR",
144e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
145e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
146e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    // EGL_NV_system_time
1471c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    { "eglGetSystemTimeFrequencyNV",
1481c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang            (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
1491c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    { "eglGetSystemTimeNV",
1501c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang            (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
151e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
1522bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    // EGL_KHR_wait_sync
1532bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    { "eglWaitSyncKHR",
1542bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
155e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
156e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    // EGL_ANDROID_presentation_time
157e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    { "eglPresentationTimeANDROID",
158e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian            (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
159a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
160a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    // EGL_KHR_swap_buffers_with_damage
161a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    { "eglSwapBuffersWithDamageKHR",
162a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza            (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
163a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
164a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    // EGL_KHR_partial_update
165a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    { "eglSetDamageRegionKHR",
166a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza            (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
167518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
168518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
169e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian/*
170e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian * These extensions entry-points should not be exposed to applications.
171e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian * They're used internally by the Android EGL layer.
172e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian */
173e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian#define FILTER_EXTENSIONS(procname) \
174e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") ||    \
175e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian         !strcmp((procname), "eglHibernateProcessIMG")      ||    \
176e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian         !strcmp((procname), "eglAwakenProcessIMG")         ||    \
177e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian         !strcmp((procname), "eglDupNativeFenceFDANDROID"))
178e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
179e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
180e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
181518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// accesses protected by sExtensionMapMutex
182518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
183518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic int sGLExtentionSlot = 0;
184518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
185518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
186518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic void(*findProcAddress(const char* name,
187518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const extention_map_t* map, size_t n))() {
188518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    for (uint32_t i=0 ; i<n ; i++) {
189518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!strcmp(name, map[i].name)) {
190518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return map[i].address;
191518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
192518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
193518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return NULL;
194518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
195518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
196518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
197518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
198518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern void setGLHooksThreadSpecific(gl_hooks_t const *value);
199518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern EGLBoolean egl_init_drivers();
200518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
201a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamyextern int getEGLDebugLevel();
202a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamyextern void setEGLDebugLevel(int level);
203518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianextern gl_hooks_t gHooksTrace;
204e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
205518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian} // namespace android;
206518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
207e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
208518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
209518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
210518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline void clearError() { egl_tls_t::clearError(); }
211518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianstatic inline EGLContext getContext() { return egl_tls_t::getContext(); }
212518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
213518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
214518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
215518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetDisplay(EGLNativeDisplayType display)
216518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
217518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
218518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
219c3289c41e794117817895653300bd2cf7daa0a01Dan Stoza    uintptr_t index = reinterpret_cast<uintptr_t>(display);
220518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (index >= NUM_DISPLAYS) {
221518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
222518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
223518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
224518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
225518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
226518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
227518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
228518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
229518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return dpy;
230518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
231518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
232518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
233518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Initialization
234518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
235518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
236518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
237518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
238518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
239518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
240b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = get_display(dpy);
241518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
242518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
243518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->initialize(major, minor);
244518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
245518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
246518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
247518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
248518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglTerminate(EGLDisplay dpy)
249518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
250518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // NOTE: don't unload the drivers b/c some APIs can be called
251518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // after eglTerminate() has been called. eglTerminate() only
252518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // terminates an EGLDisplay, not a EGL itself.
253518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
254518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
255518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
256b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = get_display(dpy);
257518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
258518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
259518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = dp->terminate();
2604774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
261518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
262518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
263518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
264518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
265518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// configuration
266518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
267518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
268518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigs(   EGLDisplay dpy,
269518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs,
270518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint config_size, EGLint *num_config)
271518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
272518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
273518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
274b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
275518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
276518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
2777773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    if (num_config==0) {
2787773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
279518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
280518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
2817773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    EGLBoolean res = EGL_FALSE;
2827773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    *num_config = 0;
2837773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian
2847773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
2857773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    if (cnx->dso) {
2867773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        res = cnx->egl.eglGetConfigs(
2877773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, configs, config_size, num_config);
288518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
2897773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian
2907773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return res;
291518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
292518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
293518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
294518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLConfig *configs, EGLint config_size,
295518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint *num_config)
296518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
297518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
298518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
299b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
300518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
301518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
302518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (num_config==0) {
303518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
304518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
305518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
306518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_FALSE;
307518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    *num_config = 0;
308518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
309ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
310ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso) {
3111cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy        if (attrib_list) {
3121cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy            char value[PROPERTY_VALUE_MAX];
3131cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy            property_get("debug.egl.force_msaa", value, "false");
3141cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3151cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy            if (!strcmp(value, "true")) {
3161cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                size_t attribCount = 0;
3171cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                EGLint attrib = attrib_list[0];
3181cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3191cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                // Only enable MSAA if the context is OpenGL ES 2.0 and
320be3c3e4ecad501eecfe1f7a424a792f0f7f3f307Romain Guy                // if no caveat is requested
3211cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                const EGLint *attribRendererable = NULL;
3221cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                const EGLint *attribCaveat = NULL;
3231cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3241cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                // Count the number of attributes and look for
325be3c3e4ecad501eecfe1f7a424a792f0f7f3f307Romain Guy                // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
3261cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                while (attrib != EGL_NONE) {
3271cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    attrib = attrib_list[attribCount];
3281cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    switch (attrib) {
3291cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        case EGL_RENDERABLE_TYPE:
3301cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                            attribRendererable = &attrib_list[attribCount];
3311cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                            break;
3321cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        case EGL_CONFIG_CAVEAT:
3331cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                            attribCaveat = &attrib_list[attribCount];
3341cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                            break;
3351cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    }
3361cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    attribCount++;
3371cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                }
3381cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3391cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
3401cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
3414774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
3421cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    // Insert 2 extra attributes to force-enable MSAA 4x
3431cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    EGLint aaAttribs[attribCount + 4];
3441cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    aaAttribs[0] = EGL_SAMPLE_BUFFERS;
3451cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    aaAttribs[1] = 1;
3461cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    aaAttribs[2] = EGL_SAMPLES;
3471cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    aaAttribs[3] = 4;
3481cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3491cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
3501cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3511cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    EGLint numConfigAA;
3521cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    EGLBoolean resAA = cnx->egl.eglChooseConfig(
3531cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                            dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
3541cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3551cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    if (resAA == EGL_TRUE && numConfigAA > 0) {
3561cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        ALOGD("Enabling MSAA 4x");
3571cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        *num_config = numConfigAA;
3581cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                        return resAA;
3591cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                    }
3601cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy                }
3611cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy            }
3621cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy        }
3631cffc80f978c55f09203d9d9a905775b951ba59aRomain Guy
3647773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian        res = cnx->egl.eglChooseConfig(
3657773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, attrib_list, configs, config_size, num_config);
366518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
367518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
368518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
369518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
370518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
371518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLint attribute, EGLint *value)
372518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
373518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
374518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
375b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
376b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display_connection(dpy, cnx);
377b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    if (!dp) return EGL_FALSE;
3784774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
379518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return cnx->egl.eglGetConfigAttrib(
3807773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, config, attribute, value);
381518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
382518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
383518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
384518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// surfaces
385518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
386518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
387a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
388c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall// been added to the Khronos egl.h.
389c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall#define EGL_GL_COLORSPACE_KHR           EGL_VG_COLORSPACE
390c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall#define EGL_GL_COLORSPACE_SRGB_KHR      EGL_VG_COLORSPACE_sRGB
391c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall#define EGL_GL_COLORSPACE_LINEAR_KHR    EGL_VG_COLORSPACE_LINEAR
392c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall
393c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall// Turn linear formats into corresponding sRGB formats when colorspace is
394c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
395c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
39682c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala// the modification isn't possible, the original dataSpace is returned.
39782c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvalastatic android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
39882c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                                                EGLint colorspace) {
399c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall    if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
40082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        return HAL_DATASPACE_SRGB_LINEAR;
401c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall    } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
40282c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        return HAL_DATASPACE_SRGB;
403c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall    }
40482c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala    return dataSpace;
405c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall}
406c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall
407518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
408518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativeWindowType window,
409518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
410518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
411518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
412518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
413b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
414b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = validate_display_connection(dpy, cnx);
415b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    if (dp) {
416ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        EGLDisplay iDpy = dp->disp.dpy;
417518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
418d566ce3a26ce781ecdbc479aaba0e172b7c807a9Andy McFadden        int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
419d566ce3a26ce781ecdbc479aaba0e172b7c807a9Andy McFadden        if (result != OK) {
420d566ce3a26ce781ecdbc479aaba0e172b7c807a9Andy McFadden            ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
421d566ce3a26ce781ecdbc479aaba0e172b7c807a9Andy McFadden                    "failed (%#x) (already connected to another API?)",
422d566ce3a26ce781ecdbc479aaba0e172b7c807a9Andy McFadden                    window, result);
42377a9b4a6bd21188e2744ae9dcd8092c6837bff7cJonathan Hamilton            return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
42481a63350527cafce6929309533c58586878f10b5Mathias Agopian        }
42581a63350527cafce6929309533c58586878f10b5Mathias Agopian
4260f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        // Set the native window's buffers format to match what this config requests.
427c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        // Whether to use sRGB gamma is not part of the EGLconfig, but is part
428c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        // of our native format. So if sRGB gamma is requested, we have to
429c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        // modify the EGLconfig's format before setting the native window's
430c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        // format.
431733a80754786d39cdc0fee09509b194472c320bcAlistair Strachan
4320f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        // by default, just pick RGBA_8888
4330f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
43482c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
4350f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian
4360f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        EGLint a = 0;
4370f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
4380f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        if (a > 0) {
4390f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            // alpha-channel requested, there's really only one suitable format
4400f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            format = HAL_PIXEL_FORMAT_RGBA_8888;
4410f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        } else {
4420f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            EGLint r, g, b;
4430f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            r = g = b = 0;
4440f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE,   &r);
4450f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
4460f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE,  &b);
4470f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            EGLint colorDepth = r + g + b;
4480f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            if (colorDepth <= 16) {
4490f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian                format = HAL_PIXEL_FORMAT_RGB_565;
4500f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            } else {
4510f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian                format = HAL_PIXEL_FORMAT_RGBX_8888;
4520f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            }
453c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        }
4540f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian
4550f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        // now select a corresponding sRGB format if needed
4560f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian        if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
4570f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian            for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
4580f288fcc9c1b2d23b1cff2f5c59689aef76b77aeMathias Agopian                if (*attr == EGL_GL_COLORSPACE_KHR) {
459a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                    if (ENABLE_EGL_KHR_GL_COLORSPACE) {
46082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                        dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
461a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                    } else {
462a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        // Normally we'd pass through unhandled attributes to
463a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        // the driver. But in case the driver implements this
464a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        // extension but we're disabling it, we want to prevent
465a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        // it getting through -- support will be broken without
466a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        // our help.
467a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        ALOGE("sRGB window surfaces not supported");
468a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                        return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
469a2ba428a22125558a129c6ba4001dfc2169de2feJesse Hall                    }
470bee205fd58a27c10a0895de5339e76025d429d2bJamie Gennis                }
471518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
472518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
473733a80754786d39cdc0fee09509b194472c320bcAlistair Strachan
474c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        if (format != 0) {
475c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall            int err = native_window_set_buffers_format(window, format);
476c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall            if (err != 0) {
477c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall                ALOGE("error setting native window pixel format: %s (%d)",
478c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall                        strerror(-err), err);
47982c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
48082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
48182c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala            }
48282c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        }
48382c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala
48482c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        if (dataSpace != 0) {
48582c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala            int err = native_window_set_buffers_data_space(window, dataSpace);
48682c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala            if (err != 0) {
48782c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                ALOGE("error setting native window pixel dataSpace: %s (%d)",
48882c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                        strerror(-err), err);
489c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall                native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
490c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall                return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
491c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall            }
492c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        }
493518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
49459769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        // the EGL spec requires that a new EGLSurface default to swap interval
49559769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        // 1, so explicitly set that on the window here.
49659769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
49759769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis        anw->setSwapInterval(anw, 1);
49859769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis
499518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreateWindowSurface(
5007773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                iDpy, config, window, attrib_list);
501518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
502b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall            egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
503b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall                    surface, cnx);
504518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
505518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
50681a63350527cafce6929309533c58586878f10b5Mathias Agopian
50781a63350527cafce6929309533c58586878f10b5Mathias Agopian        // EGLSurface creation failed
50881a63350527cafce6929309533c58586878f10b5Mathias Agopian        native_window_set_buffers_format(window, 0);
50981a63350527cafce6929309533c58586878f10b5Mathias Agopian        native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
510518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
511518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
512518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
513518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
514518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
515518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    NativePixmapType pixmap,
516518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
517518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
518518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
519518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
520b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
521b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = validate_display_connection(dpy, cnx);
522b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    if (dp) {
523518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
5247773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, pixmap, attrib_list);
525518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
526b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall            egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
527b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall                    surface, cnx);
528518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
529518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
530518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
531518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
532518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
533518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
534518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
535518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                                    const EGLint *attrib_list)
536518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
537518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
538518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
539b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
540b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = validate_display_connection(dpy, cnx);
541b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    if (dp) {
542518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
5437773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, attrib_list);
544518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (surface != EGL_NO_SURFACE) {
545b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall            egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
546b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall                    surface, cnx);
547518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return s;
548518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
549518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
550518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
551518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
5524774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
553518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
554518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
555518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
556518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
557b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
558518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
559518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
560b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
5615b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
5625b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
563518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
564518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t * const s = get_surface(surface);
565ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
566518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
567518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _s.terminate();
568518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
569518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
570518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
571518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
572518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
573518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
574518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
575518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
576518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
577b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
578518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
579518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
580b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
5815b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
5825b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
583518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
584518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
5857773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return s->cnx->egl.eglQuerySurface(
5867773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, s->surface, attribute, value);
587518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
588518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
589e8696a40e09b24b634214684d18526187b316a2fJamie Gennisvoid EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
5901c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
591e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    clearError();
592e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
593b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
594e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    if (!dp) {
595e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        return;
596e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    }
597e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
598b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
599e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    if (!_s.get()) {
600e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        setError(EGL_BAD_SURFACE, EGL_FALSE);
601e8696a40e09b24b634214684d18526187b316a2fJamie Gennis        return;
602e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    }
603e8696a40e09b24b634214684d18526187b316a2fJamie Gennis}
604e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
605518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
606518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// Contexts
607518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
608518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
609518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
610518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLContext share_list, const EGLint *attrib_list)
611518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
612518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
613518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
614b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
615b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display_connection(dpy, cnx);
6160673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock    if (dp) {
617518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (share_list != EGL_NO_CONTEXT) {
6180673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock            if (!ContextRef(dp.get(), share_list).get()) {
6190673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock                return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
6200673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock            }
621518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_context_t* const c = get_context(share_list);
622518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            share_list = c->context;
623518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
624518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLContext context = cnx->egl.eglCreateContext(
6257773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, config, share_list, attrib_list);
626518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (context != EGL_NO_CONTEXT) {
627518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // figure out if it's a GLESv1 or GLESv2
628518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            int version = 0;
629518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (attrib_list) {
630518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                while (*attrib_list != EGL_NONE) {
631518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint attr = *attrib_list++;
632518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    GLint value = *attrib_list++;
633518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    if (attr == EGL_CONTEXT_CLIENT_VERSION) {
634518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        if (value == 1) {
6357773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                            version = egl_connection_t::GLESv1_INDEX;
6364774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall                        } else if (value == 2 || value == 3) {
6377773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                            version = egl_connection_t::GLESv2_INDEX;
638518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                        }
639518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                    }
640518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                };
641518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
642b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall            egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
643b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall                    version);
6440469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
645a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy            if (getEGLDebugLevel() > 0)
6460469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy                GLTrace_eglCreateContext(version, c);
6470469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
648518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return c;
649518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
650518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
651518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_CONTEXT;
652518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
653518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
654518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
655518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
656518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
657518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
658b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
6595b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!dp)
6605b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return EGL_FALSE;
661518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
662b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    ContextRef _c(dp.get(), ctx);
6635b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_c.get())
6645b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
6654774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
666518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
667ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
668518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
669518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        _c.terminate();
670518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
671518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
672518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
673518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
674518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
675518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLSurface read, EGLContext ctx)
676518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
677518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
678518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
679b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_display_ptr dp = validate_display(dpy);
680518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
681518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
6825b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
6835b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
6845b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // a valid but uninitialized display.
685518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
686518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         (draw != EGL_NO_SURFACE) ) {
687518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
688518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
689518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
690518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // get a reference to the object passed in
691b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    ContextRef _c(dp.get(), ctx);
692b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _d(dp.get(), draw);
693b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _r(dp.get(), read);
694518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
695518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // validate the context (if not EGL_NO_CONTEXT)
6965b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
697518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // EGL_NO_CONTEXT is valid
6980673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
699518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
700518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
701518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the underlying implementation's object
702518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext impl_ctx  = EGL_NO_CONTEXT;
703518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_draw = EGL_NO_SURFACE;
704518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSurface impl_read = EGL_NO_SURFACE;
705518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
706518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are our objects structs passed in
707518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t       * c = NULL;
708518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * d = NULL;
709518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * r = NULL;
710518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
711518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // these are the current objects structs
712518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * cur_c = get_context(getContext());
7134774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall
714518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx != EGL_NO_CONTEXT) {
715518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        c = get_context(ctx);
716518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_ctx = c->context;
717518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    } else {
718518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        // no context given, use the implementation of the current context
7190673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock        if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
7200673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock            // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
7210673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock            return setError(EGL_BAD_MATCH, EGL_FALSE);
7220673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock        }
723518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (cur_c == NULL) {
724518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // no current context
725518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            // not an error, there is just no current context.
726518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            return EGL_TRUE;
727518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
728518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
729518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
730518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's draw EGLSurface
731518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (draw != EGL_NO_SURFACE) {
7320673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock        if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
733518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        d = get_surface(draw);
734518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_draw = d->surface;
735518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
736518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
737518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // retrieve the underlying implementation's read EGLSurface
738518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (read != EGL_NO_SURFACE) {
7390673e1e2d77c673c2e9bc57616a02c3188b55ad1Michael Chock        if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
740518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        r = get_surface(read);
741518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        impl_read = r->surface;
742518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
743518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
744518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
745b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    EGLBoolean result = dp->makeCurrent(c, cur_c,
746fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian            draw, read, ctx,
747fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian            impl_draw, impl_read, impl_ctx);
748518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
749518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (result == EGL_TRUE) {
750fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian        if (c) {
751518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
752518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(ctx);
7530469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
754a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy            if (getEGLDebugLevel() > 0)
75593a826f78f6313db791e6fc880439189897651b3Siva Velusamy                GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
7560469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
757518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _c.acquire();
758518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _r.acquire();
759518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            _d.acquire();
760518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        } else {
761518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            setGLHooksThreadSpecific(&gHooksNoContext);
762518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            egl_tls_t::setContext(EGL_NO_CONTEXT);
763518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
7645fecea776a5f093c21ac1a0ad3552b847d4be23eMathias Agopian    } else {
765e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        // this will ALOGE the error
76663108c34ec181e923b68ee840bb7960f205466a7Mathias Agopian        egl_connection_t* const cnx = &gEGLImpl;
76763108c34ec181e923b68ee840bb7960f205466a7Mathias Agopian        result = setError(cnx->egl.eglGetError(), EGL_FALSE);
768518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
769518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
770518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
771518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
772518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
773518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
774518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            EGLint attribute, EGLint *value)
775518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
776518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
777518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
778b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
779518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
780518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
781b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    ContextRef _c(dp.get(), ctx);
782518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
783518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
784518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_context_t * const c = get_context(ctx);
7857773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    return c->cnx->egl.eglQueryContext(
7867773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian            dp->disp.dpy, c->context, attribute, value);
787518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
788518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
789518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
790518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLContext eglGetCurrentContext(void)
791518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
792518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
793518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_CONTEXT.
794518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
795518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
796518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
797518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
798518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return ctx;
799518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
800518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
801518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglGetCurrentSurface(EGLint readdraw)
802518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
803518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
804518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_SURFACE.
805518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
806518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
807518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
808518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
809518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
810518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
811518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
812518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        switch (readdraw) {
813518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            case EGL_READ: return c->read;
8144774338bd0ad1ebe42c311fd0c72f13786b5c800Jesse Hall            case EGL_DRAW: return c->draw;
815518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
816518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
817518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
818518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_SURFACE;
819518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
820518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
821518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLDisplay eglGetCurrentDisplay(void)
822518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
823518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // could be called before eglInitialize(), but we wouldn't have a context
824518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // then, and this function would correctly return EGL_NO_DISPLAY.
825518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
826518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
827518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
828518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext ctx = getContext();
829518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (ctx) {
830518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        egl_context_t const * const c = get_context(ctx);
831518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
832518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return c->dpy;
833518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
834518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_NO_DISPLAY;
835518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
836518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
837518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitGL(void)
838518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
839518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
840518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
841ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
842ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
843ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
844ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
845ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return cnx->egl.eglWaitGL();
846518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
847518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
848518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitNative(EGLint engine)
849518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
850518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
851518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
852ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
853ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
854ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
855ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
856ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return cnx->egl.eglWaitNative(engine);
857518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
858518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
859518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLint eglGetError(void)
860518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
861ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLint err = EGL_SUCCESS;
862ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
863ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso) {
864ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        err = cnx->egl.eglGetError();
865518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
866ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (err == EGL_SUCCESS) {
867ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        err = egl_tls_t::getError();
868ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    }
869ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return err;
870518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
871518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
872c0ec5e2333b6350480851b8b48f000c78ea3f88aMichael Chockstatic __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
873c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall        const char* procname) {
874c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    const egl_connection_t* cnx = &gEGLImpl;
875c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    void* proc = NULL;
876c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall
877c0ec5e2333b6350480851b8b48f000c78ea3f88aMichael Chock    proc = dlsym(cnx->libEgl, procname);
878c0ec5e2333b6350480851b8b48f000c78ea3f88aMichael Chock    if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
879c0ec5e2333b6350480851b8b48f000c78ea3f88aMichael Chock
880c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    proc = dlsym(cnx->libGles2, procname);
881c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
882c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall
883c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    proc = dlsym(cnx->libGles1, procname);
884c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
885c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall
886c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    return NULL;
887c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall}
888c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall
889518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
890518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
891518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // eglGetProcAddress() could be the very first function called
892518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // in which case we must make sure we've initialized ourselves, this
893518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // happens the first time egl_get_display() is called.
894518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
895518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
896518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
897518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
898518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        setError(EGL_BAD_PARAMETER, NULL);
899518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return  NULL;
900518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
901518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
902e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    if (FILTER_EXTENSIONS(procname)) {
903aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis        return NULL;
904aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis    }
905aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis
906518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    __eglMustCastToProperFunctionPointerType addr;
907e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
908518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (addr) return addr;
909518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
910c0ec5e2333b6350480851b8b48f000c78ea3f88aMichael Chock    addr = findBuiltinWrapper(procname);
911c07b52060acd627c8510c1a9151e0753fce76330Jesse Hall    if (addr) return addr;
912aca51c06f38155f1435fbc6944d7fc0a9bf1e4e9Jamie Gennis
913518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // this protects accesses to sGLExtentionMap and sGLExtentionSlot
914518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_lock(&sExtensionMapMutex);
915518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
916518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        /*
917518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * Since eglGetProcAddress() is not associated to anything, it needs
918518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * to return a function pointer that "works" regardless of what
919518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * the current context is.
920518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
921518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * For this reason, we return a "forwarder", a small stub that takes
922518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * care of calling the function associated with the context
923518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * currently bound.
924518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
925518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * We first look for extensions we've already resolved, if we're seeing
926518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * this extension for the first time, we go through all our
927518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * implementations and call eglGetProcAddress() and record the
928518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * result in the appropriate implementation hooks and return the
929518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         * address of the forwarder corresponding to that hook set.
930518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         *
931518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian         */
932518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
933518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const String8 name(procname);
934518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        addr = sGLExtentionMap.valueFor(name);
935518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const int slot = sGLExtentionSlot;
936518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
937e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
938518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                "no more slots for eglGetProcAddress(\"%s\")",
939518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                procname);
940518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
9410469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
9420469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy        gl_hooks_t *debugHooks = GLTrace_getGLHooks();
9430469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
9440469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
945518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
946518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            bool found = false;
947ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
948ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            egl_connection_t* const cnx = &gEGLImpl;
949ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            if (cnx->dso && cnx->egl.eglGetProcAddress) {
950ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                // Extensions are independent of the bound context
95169d100762c7c26d8328f4bb61cfef026d3a69bbfluliuhui                addr =
9527773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
9537773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
954518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#if EGL_TRACE
955ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                debugHooks->ext.extensions[slot] =
956ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                gHooksTrace.ext.extensions[slot] =
957518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#endif
958ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                        cnx->egl.eglGetProcAddress(procname);
95969d100762c7c26d8328f4bb61cfef026d3a69bbfluliuhui                if (addr) found = true;
960518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
961ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
962518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            if (found) {
963518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                addr = gExtensionForwarders[slot];
964518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionMap.add(name, addr);
965518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                sGLExtentionSlot++;
966518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian            }
967518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        }
968518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
969518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    pthread_mutex_unlock(&sExtensionMapMutex);
970518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return addr;
971518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
972518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
97328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennisclass FrameCompletionThread : public Thread {
97428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennispublic:
97528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
97628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    static void queueSync(EGLSyncKHR sync) {
97728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        static sp<FrameCompletionThread> thread(new FrameCompletionThread);
97828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        static bool running = false;
97928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        if (!running) {
98028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->run("GPUFrameCompletion");
98128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            running = true;
98228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
98328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
98428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(thread->mMutex);
98528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
98628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                    thread->mFramesQueued).string());
98728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mQueue.push_back(sync);
98828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mCondition.signal();
98928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            thread->mFramesQueued++;
99028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
99128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
99228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    }
99328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
99428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennisprivate:
99528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
99628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
99728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    virtual bool threadLoop() {
99828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        EGLSyncKHR sync;
99928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        uint32_t frameNum;
100028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
100128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(mMutex);
100228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            while (mQueue.isEmpty()) {
100328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                mCondition.wait(mMutex);
100428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            }
100528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            sync = mQueue[0];
100628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            frameNum = mFramesCompleted;
100728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
100828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
100928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
101028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
101128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                    frameNum).string());
101228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
101328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            if (result == EGL_FALSE) {
101428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
101528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
101628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis                ALOGE("FrameCompletion: timeout waiting for fence");
101728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            }
101828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            eglDestroySyncKHR(dpy, sync);
101928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
102028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        {
102128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            Mutex::Autolock lock(mMutex);
102228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            mQueue.removeAt(0);
102328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            mFramesCompleted++;
102428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis            ATRACE_INT("GPU Frames Outstanding", mQueue.size());
102528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        }
102628ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis        return true;
102728ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    }
102828ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
102928ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    uint32_t mFramesQueued;
103028ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    uint32_t mFramesCompleted;
103128ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Vector<EGLSyncKHR> mQueue;
103228ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Condition mCondition;
103328ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis    Mutex mMutex;
103428ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis};
103528ef8d7911dbfd1bf8256fb43acba894d87fc07aJamie Gennis
1036a894d082cfee8d12ee5913163a34ec5dc521d005Dan StozaEGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1037a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        EGLint *rects, EGLint n_rects)
1038518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
10391c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
1040518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1041518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1042b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1043518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1044518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1045b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), draw);
10465b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
10475b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1048518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
10490469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#if EGL_TRACE
1050a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy    gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
1051a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy    if (getEGLDebugLevel() > 0) {
1052a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        if (trace_hooks == NULL) {
1053a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy            if (GLTrace_start() < 0) {
1054a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                ALOGE("Disabling Tracer for OpenGL ES");
1055a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                setEGLDebugLevel(0);
1056a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy            } else {
1057a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                // switch over to the trace version of hooks
1058a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                EGLContext ctx = egl_tls_t::getContext();
1059a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                egl_context_t * const c = get_context(ctx);
1060a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                if (c) {
1061a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                    setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1062a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                    GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
1063a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy                }
1064a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy            }
1065a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        }
1066a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy
10670469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy        GLTrace_eglSwapBuffers(dpy, draw);
1068a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy    } else if (trace_hooks != NULL) {
1069a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        // tracing is now disabled, so switch back to the non trace version
1070a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        EGLContext ctx = egl_tls_t::getContext();
1071a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        egl_context_t * const c = get_context(ctx);
1072a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1073a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy        GLTrace_stop();
1074a73a97728befb5ba5ad647ab3b60058c4d536ba4Siva Velusamy    }
10750469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
10760469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
1077518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(draw);
10787db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian
1079ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian    if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1080ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian        EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1081ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian        if (sync != EGL_NO_SYNC_KHR) {
1082ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian            FrameCompletionThread::queueSync(sync);
1083ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian        }
1084ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian    }
1085ed6d08b70d775852f4827471814c83eba3606aaaMathias Agopian
10867db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian    if (CC_UNLIKELY(dp->finishOnSwap)) {
10877db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        uint32_t pixel;
10887db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        egl_context_t * const c = get_context( egl_tls_t::getContext() );
10897db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        if (c) {
10907db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian            // glReadPixels() ensures that the frame is complete
10917db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian            s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
10927db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian                    GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
10937db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian        }
10947db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian    }
10957db993a98b9239bd4e384cc4aa128262fe3cf52cMathias Agopian
1096a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    if (n_rects == 0) {
1097a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1098a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1099a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1100a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    Vector<android_native_rect_t> androidRects;
1101a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    for (int r = 0; r < n_rects; ++r) {
1102a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        int offset = r * 4;
1103a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        int x = rects[offset];
1104a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        int y = rects[offset + 1];
1105a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        int width = rects[offset + 2];
1106a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        int height = rects[offset + 3];
1107a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        android_native_rect_t androidRect;
1108a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        androidRect.left = x;
1109a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        androidRect.top = y + height;
1110a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        androidRect.right = x + width;
1111a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        androidRect.bottom = y;
1112a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        androidRects.push_back(androidRect);
1113a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1114a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    native_window_set_surface_damage(s->win.get(), androidRects.array(),
1115a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza            androidRects.size());
1116a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1117a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1118a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1119a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza                rects, n_rects);
1120a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    } else {
1121a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1122a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1123a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza}
1124a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1125a894d082cfee8d12ee5913163a34ec5dc521d005Dan StozaEGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1126a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza{
1127a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
1128518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1129518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1130518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
1131518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian                            NativePixmapType target)
1132518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1133518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1134518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1135b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1136518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1137518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1138b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
11395b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
11405b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1141518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1142518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1143ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
1144518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1145518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1146518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianconst char* eglQueryString(EGLDisplay dpy, EGLint name)
1147518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1148518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1149518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1150b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1151518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return (const char *) NULL;
1152518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1153518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    switch (name) {
1154518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VENDOR:
11554b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getVendorString();
1156518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_VERSION:
11574b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getVersionString();
1158518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_EXTENSIONS:
11594b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getExtensionString();
1160518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        case EGL_CLIENT_APIS:
11614b9511c16195a646242eff833b0af212933b6ecaMathias Agopian            return dp->getClientApiString();
1162518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1163518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_PARAMETER, (const char *)0);
1164518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1165518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1166ca08833d5ea99130797e10ad68a651b50e99da74Mathias AgopianEGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1167ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian{
1168ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    clearError();
1169ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
1170ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    const egl_display_ptr dp = validate_display(dpy);
1171ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (!dp) return (const char *) NULL;
1172ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
1173ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    switch (name) {
1174ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        case EGL_VENDOR:
1175ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian            return dp->disp.queryString.vendor;
1176ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        case EGL_VERSION:
1177ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian            return dp->disp.queryString.version;
1178ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        case EGL_EXTENSIONS:
1179ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian            return dp->disp.queryString.extensions;
1180ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        case EGL_CLIENT_APIS:
1181ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian            return dp->disp.queryString.clientApi;
1182ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
1183ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return setError(EGL_BAD_PARAMETER, (const char *)0);
1184ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
1185518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1186518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1187518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.1
1188518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1189518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1190518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSurfaceAttrib(
1191518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1192518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1193518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1194518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1195b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1196518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1197518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1198b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
11995b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
12005b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1201518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1202518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1203518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglSurfaceAttrib) {
1204518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglSurfaceAttrib(
1205ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, attribute, value);
1206518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1207518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1208518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1209518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1210518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindTexImage(
1211518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1212518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1213518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1214518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1215b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1216518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1217518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1218b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
12195b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
12205b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1221518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1222518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1223518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglBindTexImage) {
1224518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglBindTexImage(
1225ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, buffer);
1226518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1227518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1228518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1229518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1230518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseTexImage(
1231518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1232518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1233518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1234518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1235b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1236518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1237518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1238b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
12395b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
12405b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1241518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1242518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1243518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglReleaseTexImage) {
1244518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglReleaseTexImage(
1245ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, buffer);
1246518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1247518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_SURFACE, EGL_FALSE);
1248518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1249518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1250518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1251518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1252518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1253518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1254b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1255518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1256518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1257518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
1258ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1259ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglSwapInterval) {
1260ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1261518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1262ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
1263518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1264518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1265518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1266518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1267518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1268518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL 1.2
1269518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1270518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1271518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglWaitClient(void)
1272518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1273518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1274518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1275ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1276ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (!cnx->dso)
1277ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1278ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
1279ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    EGLBoolean res;
1280ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->egl.eglWaitClient) {
1281ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglWaitClient();
1282ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    } else {
1283ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglWaitGL();
1284518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1285518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1286518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1287518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1288518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglBindAPI(EGLenum api)
1289518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1290518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1291518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1292518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
1293518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1294518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1295518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1296518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // bind this API on all EGLs
1297518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean res = EGL_TRUE;
1298ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1299ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglBindAPI) {
1300ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        res = cnx->egl.eglBindAPI(api);
1301518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1302518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return res;
1303518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1304518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1305518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLenum eglQueryAPI(void)
1306518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1307518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1308518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1309518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (egl_init_drivers() == EGL_FALSE) {
1310518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1311518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1312518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1313ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1314ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglQueryAPI) {
1315ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglQueryAPI();
1316518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1317ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian
1318518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // or, it can only be OpenGL ES
1319518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_OPENGL_ES_API;
1320518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1321518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1322518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglReleaseThread(void)
1323518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1324518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1325518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
13264e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian#if EGL_TRACE
13274e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian    if (getEGLDebugLevel() > 0)
13284e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian        GLTrace_eglReleaseThread();
13294e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian#endif
13304e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian
1331518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    // If there is context bound to the thread, release it
1332fb87e54a9af8bc5063ca4deebe81d90126992480Mathias Agopian    egl_display_t::loseCurrent(get_context(getContext()));
1333518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1334ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1335ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglReleaseThread) {
1336ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        cnx->egl.eglReleaseThread();
1337518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1338518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_tls_t::clearTLS();
1339518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return EGL_TRUE;
1340518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1341518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1342518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSurface eglCreatePbufferFromClientBuffer(
1343518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1344518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian          EGLConfig config, const EGLint *attrib_list)
1345518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1346518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1347518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1348b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_connection_t* cnx = NULL;
1349b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1350b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    if (!dp) return EGL_FALSE;
1351518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1352518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return cnx->egl.eglCreatePbufferFromClientBuffer(
13537773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                dp->disp.dpy, buftype, buffer, config, attrib_list);
1354518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1355518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1356518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1357518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1358518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1359518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 3
1360518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1361518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1362518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1363518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        const EGLint *attrib_list)
1364518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1365518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1366518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1367b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1368518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1369518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1370b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
13715b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
13725b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1373518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1374518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1375518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglLockSurfaceKHR) {
1376518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return s->cnx->egl.eglLockSurfaceKHR(
1377ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                dp->disp.dpy, s->surface, attrib_list);
1378518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1379518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1380518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1381518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1382518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1383518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1384518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1385518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1386b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1387518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1388518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1389b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    SurfaceRef _s(dp.get(), surface);
13905b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (!_s.get())
13915b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        return setError(EGL_BAD_SURFACE, EGL_FALSE);
1392518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1393518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_surface_t const * const s = get_surface(surface);
1394518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (s->cnx->egl.eglUnlockSurfaceKHR) {
1395ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1396518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1397518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1398518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1399518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1400518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1401518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        EGLClientBuffer buffer, const EGLint *attrib_list)
1402518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1403518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1404518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1405b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1406518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_IMAGE_KHR;
1407518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1408b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    ContextRef _c(dp.get(), ctx);
14097c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_context_t * const c = _c.get();
1410518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
14117c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLImageKHR result = EGL_NO_IMAGE_KHR;
14127c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
14137c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglCreateImageKHR) {
14147c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglCreateImageKHR(
14157c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy,
14167c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                c ? c->context : EGL_NO_CONTEXT,
14177c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                target, buffer, attrib_list);
1418518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
14197c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1420518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1421518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1422518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1423518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1424518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1425518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1426b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1427518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1428518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1429646a5c593f9819dc5da6a1ec859bc70cb7ba096fSteven Holte    EGLBoolean result = EGL_FALSE;
1430ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
14317c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1432646a5c593f9819dc5da6a1ec859bc70cb7ba096fSteven Holte        result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1433518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1434646a5c593f9819dc5da6a1ec859bc70cb7ba096fSteven Holte    return result;
1435518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1436518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1437518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1438518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// EGL_EGLEXT_VERSION 5
1439518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1440518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1441518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1442518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1443518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1444518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1445518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1446b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1447518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_NO_SYNC_KHR;
1448518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1449518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLSyncKHR result = EGL_NO_SYNC_KHR;
14507c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
14517c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
14527c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1453518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
14547c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1455518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1456518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1457518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias AgopianEGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1458518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1459518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1460518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1461b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1462518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1463518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1464518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLBoolean result = EGL_FALSE;
14657c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
14667c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
14677c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1468518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
1469518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    return result;
1470518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1471518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1472e9b3dfb7d5cc233747407381a51a081c335dc076Mathias AgopianEGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1473e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    clearError();
1474e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
1475e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    const egl_display_ptr dp = validate_display(dpy);
1476e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    if (!dp) return EGL_FALSE;
1477e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
1478e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    EGLBoolean result = EGL_FALSE;
1479e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
1480e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1481e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian        result = cnx->egl.eglSignalSyncKHR(
1482e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian                dp->disp.dpy, sync, mode);
1483e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    }
1484e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian    return result;
1485e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian}
1486e9b3dfb7d5cc233747407381a51a081c335dc076Mathias Agopian
14877c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias AgopianEGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
14887c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        EGLint flags, EGLTimeKHR timeout)
1489518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1490518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1491518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1492b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1493518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1494518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
14957c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLBoolean result = EGL_FALSE;
14967c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
14977c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
14987c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglClientWaitSyncKHR(
14997c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy, sync, flags, timeout);
1500518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
15017c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1502518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1503518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
15047c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias AgopianEGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
15057c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        EGLint attribute, EGLint *value)
1506518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian{
1507518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    clearError();
1508518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1509b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    const egl_display_ptr dp = validate_display(dpy);
1510518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    if (!dp) return EGL_FALSE;
1511518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
15127c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    EGLBoolean result = EGL_FALSE;
15137c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
15147c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
15157c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian        result = cnx->egl.eglGetSyncAttribKHR(
15167c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian                dp->disp.dpy, sync, attribute, value);
1517518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
15187c0441ac271f4e00a2d63eb3048c037ebffa90b9Mathias Agopian    return result;
1519518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
1520518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
1521518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
15222bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian// EGL_EGLEXT_VERSION 15
1523518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
1524518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
15252bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias AgopianEGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1526331841b96b92646c93c87627c03f77b892f711cdJamie Gennis    clearError();
1527331841b96b92646c93c87627c03f77b892f711cdJamie Gennis    const egl_display_ptr dp = validate_display(dpy);
15282bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    if (!dp) return EGL_FALSE;
15292bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    EGLint result = EGL_FALSE;
1530331841b96b92646c93c87627c03f77b892f711cdJamie Gennis    egl_connection_t* const cnx = &gEGLImpl;
15312bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
15322bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian        result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1533331841b96b92646c93c87627c03f77b892f711cdJamie Gennis    }
1534331841b96b92646c93c87627c03f77b892f711cdJamie Gennis    return result;
1535331841b96b92646c93c87627c03f77b892f711cdJamie Gennis}
15361c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
15372bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian// ----------------------------------------------------------------------------
15382bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian// ANDROID extensions
15392bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian// ----------------------------------------------------------------------------
15402bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian
15412bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias AgopianEGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1542010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis{
1543010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    clearError();
1544010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis
1545010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    const egl_display_ptr dp = validate_display(dpy);
1546010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1547010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis
15482bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1549010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    egl_connection_t* const cnx = &gEGLImpl;
15502bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian    if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
15512bb716871cf8bfadfff1193ed798da3bffc1f8ecMathias Agopian        result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1552010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    }
1553010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis    return result;
1554010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis}
1555010dd4fb892aecf71e4631c22148fe57ef5b3958Jamie Gennis
15567284145d564fa8a422a8e564a38c730fb4a2962bAndy McFaddenEGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
15577284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden        EGLnsecsANDROID time)
15587284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden{
15597284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    clearError();
15607284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden
15617284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    const egl_display_ptr dp = validate_display(dpy);
15627284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    if (!dp) {
15637284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden        return EGL_FALSE;
15647284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    }
15657284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden
15667284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    SurfaceRef _s(dp.get(), surface);
15677284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    if (!_s.get()) {
15687284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden        setError(EGL_BAD_SURFACE, EGL_FALSE);
15697284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden        return EGL_FALSE;
15707284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    }
15717284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden
15727284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    egl_surface_t const * const s = get_surface(surface);
15737284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    native_window_set_buffers_timestamp(s->win.get(), time);
15747284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden
15757284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden    return EGL_TRUE;
15767284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden}
15777284145d564fa8a422a8e564a38c730fb4a2962bAndy McFadden
15781c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// ----------------------------------------------------------------------------
15791c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// NVIDIA extensions
15801c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang// ----------------------------------------------------------------------------
15811c3d72a2291827fb15e2ef311a571c860e0dba41Jonas YangEGLuint64NV eglGetSystemTimeFrequencyNV()
15821c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang{
15831c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    clearError();
15841c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
15851c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    if (egl_init_drivers() == EGL_FALSE) {
15861c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
15871c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
15881c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
15891c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    EGLuint64NV ret = 0;
1590ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
15911c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
1592ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1593ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglGetSystemTimeFrequencyNV();
15941c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
15951c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
15960e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    return setErrorQuiet(EGL_BAD_DISPLAY, 0);
15971c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang}
15981c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
15991c3d72a2291827fb15e2ef311a571c860e0dba41Jonas YangEGLuint64NV eglGetSystemTimeNV()
16001c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang{
16011c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    clearError();
16021c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
16031c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    if (egl_init_drivers() == EGL_FALSE) {
16041c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
16051c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
16061c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
16071c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    EGLuint64NV ret = 0;
1608ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    egl_connection_t* const cnx = &gEGLImpl;
16091c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
1610ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1611ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        return cnx->egl.eglGetSystemTimeNV();
16121c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang    }
16131c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang
16140e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    return setErrorQuiet(EGL_BAD_DISPLAY, 0);
16151c3d72a2291827fb15e2ef311a571c860e0dba41Jonas Yang}
1616a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1617a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza// ----------------------------------------------------------------------------
1618a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza// Partial update extension
1619a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza// ----------------------------------------------------------------------------
1620a894d082cfee8d12ee5913163a34ec5dc521d005Dan StozaEGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1621a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        EGLint *rects, EGLint n_rects)
1622a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza{
1623a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    clearError();
1624a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1625a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    const egl_display_ptr dp = validate_display(dpy);
1626a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    if (!dp) {
1627a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        setError(EGL_BAD_DISPLAY, EGL_FALSE);
1628a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return EGL_FALSE;
1629a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1630a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1631a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    SurfaceRef _s(dp.get(), surface);
1632a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    if (!_s.get()) {
1633a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        setError(EGL_BAD_SURFACE, EGL_FALSE);
1634a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return EGL_FALSE;
1635a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1636a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1637a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    egl_surface_t const * const s = get_surface(surface);
1638a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    if (s->cnx->egl.eglSetDamageRegionKHR) {
1639a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza        return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1640a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza                rects, n_rects);
1641a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    }
1642a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza
1643a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza    return EGL_FALSE;
1644a894d082cfee8d12ee5913163a34ec5dc521d005Dan Stoza}
1645