blob: 11a13c311ae7ecc84929c9519d47ceb1d612a848 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hallc07b5202013-07-04 12:08:16 -070019#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include <ctype.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <hardware/gralloc.h>
25#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30#include <cutils/log.h>
31#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/properties.h>
34#include <cutils/memory.h>
35
36#include <utils/KeyedVector.h>
37#include <utils/SortedVector.h>
38#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080039#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070040
Mathias Agopian39c24a22013-04-04 23:17:56 -070041#include "../egl_impl.h"
42#include "../glestrace.h"
43#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080048#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
Jesse Halla2ba4282013-09-14 21:00:14 -070052// This extension has not been ratified yet, so can't be shipped.
53// Implementation is incomplete and untested.
54#define ENABLE_EGL_KHR_GL_COLORSPACE 0
55
Mathias Agopian518ec112011-05-13 16:21:08 -070056// ----------------------------------------------------------------------------
57
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070058namespace android {
59
Mathias Agopian518ec112011-05-13 16:21:08 -070060struct extention_map_t {
61 const char* name;
62 __eglMustCastToProperFunctionPointerType address;
63};
64
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065/*
Jesse Hall21558da2013-08-06 15:31:22 -070066 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070067 *
Jesse Hall21558da2013-08-06 15:31:22 -070068 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
69 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070 *
Jesse Hall21558da2013-08-06 15:31:22 -070071 * The rest (gExtensionString) depend on support in the EGL driver, and are
72 * only available if the driver supports them. However, some of these must be
73 * supported because they are used by the Android system itself; these are
74 * listd as mandatory below and are required by the CDD. The system *assumes*
75 * the mandatory extensions are present and may not function properly if some
76 * are missing.
77 *
78 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070079 */
Jesse Hall21558da2013-08-06 15:31:22 -070080extern char const * const gBuiltinExtensionString =
81 "EGL_KHR_get_all_proc_addresses "
82 "EGL_ANDROID_presentation_time "
83 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070084extern char const * const gExtensionString =
85 "EGL_KHR_image " // mandatory
86 "EGL_KHR_image_base " // mandatory
87 "EGL_KHR_image_pixmap "
88 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070089#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070090 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070091#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070092 "EGL_KHR_gl_texture_2D_image "
93 "EGL_KHR_gl_texture_cubemap_image "
94 "EGL_KHR_gl_renderbuffer_image "
95 "EGL_KHR_reusable_sync "
96 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -070097 "EGL_KHR_create_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_EXT_create_context_robustness "
99 "EGL_NV_system_time "
100 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700101 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700102 "EGL_ANDROID_recordable " // mandatory
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 ;
104
105// extensions not exposed to applications but used by the ANDROID system
106// "EGL_ANDROID_blob_cache " // strongly recommended
107// "EGL_IMG_hibernate_process " // optional
108// "EGL_ANDROID_native_fence_sync " // strongly recommended
109// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700110// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700111
112/*
113 * EGL Extensions entry-points exposed to 3rd party applications
114 * (keep in sync with gExtensionString above)
115 *
116 */
117static const extention_map_t sExtensionMap[] = {
118 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700119 { "eglLockSurfaceKHR",
120 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
121 { "eglUnlockSurfaceKHR",
122 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700123
124 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 { "eglCreateImageKHR",
126 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
127 { "eglDestroyImageKHR",
128 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700129
130 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
131 { "eglCreateSyncKHR",
132 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
133 { "eglDestroySyncKHR",
134 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
135 { "eglClientWaitSyncKHR",
136 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
137 { "eglSignalSyncKHR",
138 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
139 { "eglGetSyncAttribKHR",
140 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
141
142 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800143 { "eglGetSystemTimeFrequencyNV",
144 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
145 { "eglGetSystemTimeNV",
146 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700147
Mathias Agopian2bb71682013-03-27 17:32:41 -0700148 // EGL_KHR_wait_sync
149 { "eglWaitSyncKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700151
152 // EGL_ANDROID_presentation_time
153 { "eglPresentationTimeANDROID",
154 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700155};
156
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700157/*
158 * These extensions entry-points should not be exposed to applications.
159 * They're used internally by the Android EGL layer.
160 */
161#define FILTER_EXTENSIONS(procname) \
162 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
163 !strcmp((procname), "eglHibernateProcessIMG") || \
164 !strcmp((procname), "eglAwakenProcessIMG") || \
165 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
166
167
168
Mathias Agopian518ec112011-05-13 16:21:08 -0700169// accesses protected by sExtensionMapMutex
170static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
171static int sGLExtentionSlot = 0;
172static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
173
174static void(*findProcAddress(const char* name,
175 const extention_map_t* map, size_t n))() {
176 for (uint32_t i=0 ; i<n ; i++) {
177 if (!strcmp(name, map[i].name)) {
178 return map[i].address;
179 }
180 }
181 return NULL;
182}
183
184// ----------------------------------------------------------------------------
185
Mathias Agopian518ec112011-05-13 16:21:08 -0700186extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
187extern EGLBoolean egl_init_drivers();
188extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Siva Velusamya73a9772012-12-18 14:56:55 -0800189extern int getEGLDebugLevel();
190extern void setEGLDebugLevel(int level);
Mathias Agopian518ec112011-05-13 16:21:08 -0700191extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700192
Mathias Agopian518ec112011-05-13 16:21:08 -0700193} // namespace android;
194
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700195
Mathias Agopian518ec112011-05-13 16:21:08 -0700196// ----------------------------------------------------------------------------
197
198static inline void clearError() { egl_tls_t::clearError(); }
199static inline EGLContext getContext() { return egl_tls_t::getContext(); }
200
201// ----------------------------------------------------------------------------
202
203EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
204{
205 clearError();
206
Dan Stozac3289c42014-01-17 11:38:34 -0800207 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700208 if (index >= NUM_DISPLAYS) {
209 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
210 }
211
212 if (egl_init_drivers() == EGL_FALSE) {
213 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
214 }
215
216 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
217 return dpy;
218}
219
220// ----------------------------------------------------------------------------
221// Initialization
222// ----------------------------------------------------------------------------
223
224EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
225{
226 clearError();
227
Jesse Hallb29e5e82012-04-04 16:53:42 -0700228 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700229 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
230
231 EGLBoolean res = dp->initialize(major, minor);
232
233 return res;
234}
235
236EGLBoolean eglTerminate(EGLDisplay dpy)
237{
238 // NOTE: don't unload the drivers b/c some APIs can be called
239 // after eglTerminate() has been called. eglTerminate() only
240 // terminates an EGLDisplay, not a EGL itself.
241
242 clearError();
243
Jesse Hallb29e5e82012-04-04 16:53:42 -0700244 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700245 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
246
247 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800248
Mathias Agopian518ec112011-05-13 16:21:08 -0700249 return res;
250}
251
252// ----------------------------------------------------------------------------
253// configuration
254// ----------------------------------------------------------------------------
255
256EGLBoolean eglGetConfigs( EGLDisplay dpy,
257 EGLConfig *configs,
258 EGLint config_size, EGLint *num_config)
259{
260 clearError();
261
Jesse Hallb29e5e82012-04-04 16:53:42 -0700262 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700263 if (!dp) return EGL_FALSE;
264
Mathias Agopian7773c432012-02-13 20:06:08 -0800265 if (num_config==0) {
266 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 }
268
Mathias Agopian7773c432012-02-13 20:06:08 -0800269 EGLBoolean res = EGL_FALSE;
270 *num_config = 0;
271
272 egl_connection_t* const cnx = &gEGLImpl;
273 if (cnx->dso) {
274 res = cnx->egl.eglGetConfigs(
275 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800277
278 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700279}
280
281EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
282 EGLConfig *configs, EGLint config_size,
283 EGLint *num_config)
284{
285 clearError();
286
Jesse Hallb29e5e82012-04-04 16:53:42 -0700287 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 if (!dp) return EGL_FALSE;
289
290 if (num_config==0) {
291 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
292 }
293
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 EGLBoolean res = EGL_FALSE;
295 *num_config = 0;
296
Mathias Agopianada798b2012-02-13 17:09:30 -0800297 egl_connection_t* const cnx = &gEGLImpl;
298 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700299 if (attrib_list) {
300 char value[PROPERTY_VALUE_MAX];
301 property_get("debug.egl.force_msaa", value, "false");
302
303 if (!strcmp(value, "true")) {
304 size_t attribCount = 0;
305 EGLint attrib = attrib_list[0];
306
307 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700308 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700309 const EGLint *attribRendererable = NULL;
310 const EGLint *attribCaveat = NULL;
311
312 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700313 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700314 while (attrib != EGL_NONE) {
315 attrib = attrib_list[attribCount];
316 switch (attrib) {
317 case EGL_RENDERABLE_TYPE:
318 attribRendererable = &attrib_list[attribCount];
319 break;
320 case EGL_CONFIG_CAVEAT:
321 attribCaveat = &attrib_list[attribCount];
322 break;
323 }
324 attribCount++;
325 }
326
327 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
328 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800329
Romain Guy1cffc802012-10-15 18:13:05 -0700330 // Insert 2 extra attributes to force-enable MSAA 4x
331 EGLint aaAttribs[attribCount + 4];
332 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
333 aaAttribs[1] = 1;
334 aaAttribs[2] = EGL_SAMPLES;
335 aaAttribs[3] = 4;
336
337 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
338
339 EGLint numConfigAA;
340 EGLBoolean resAA = cnx->egl.eglChooseConfig(
341 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
342
343 if (resAA == EGL_TRUE && numConfigAA > 0) {
344 ALOGD("Enabling MSAA 4x");
345 *num_config = numConfigAA;
346 return resAA;
347 }
348 }
349 }
350 }
351
Mathias Agopian7773c432012-02-13 20:06:08 -0800352 res = cnx->egl.eglChooseConfig(
353 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 }
355 return res;
356}
357
358EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
359 EGLint attribute, EGLint *value)
360{
361 clearError();
362
Jesse Hallb29e5e82012-04-04 16:53:42 -0700363 egl_connection_t* cnx = NULL;
364 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
365 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800366
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800368 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700369}
370
371// ----------------------------------------------------------------------------
372// surfaces
373// ----------------------------------------------------------------------------
374
Jesse Halla2ba4282013-09-14 21:00:14 -0700375// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700376// been added to the Khronos egl.h.
377#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
378#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
379#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
380
381// Turn linear formats into corresponding sRGB formats when colorspace is
382// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
383// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800384// the modification isn't possible, the original dataSpace is returned.
385static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
386 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700387 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800388 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700389 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800390 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700391 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800392 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700393}
394
Mathias Agopian518ec112011-05-13 16:21:08 -0700395EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
396 NativeWindowType window,
397 const EGLint *attrib_list)
398{
399 clearError();
400
Jesse Hallb29e5e82012-04-04 16:53:42 -0700401 egl_connection_t* cnx = NULL;
402 egl_display_ptr dp = validate_display_connection(dpy, cnx);
403 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800404 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700405
Andy McFaddend566ce32014-01-07 15:54:17 -0800406 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
407 if (result != OK) {
408 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
409 "failed (%#x) (already connected to another API?)",
410 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700411 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700412 }
413
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700414 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700415 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
416 // of our native format. So if sRGB gamma is requested, we have to
417 // modify the EGLconfig's format before setting the native window's
418 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800419
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700420 // by default, just pick RGBA_8888
421 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800422 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700423
424 EGLint a = 0;
425 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
426 if (a > 0) {
427 // alpha-channel requested, there's really only one suitable format
428 format = HAL_PIXEL_FORMAT_RGBA_8888;
429 } else {
430 EGLint r, g, b;
431 r = g = b = 0;
432 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
433 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
434 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
435 EGLint colorDepth = r + g + b;
436 if (colorDepth <= 16) {
437 format = HAL_PIXEL_FORMAT_RGB_565;
438 } else {
439 format = HAL_PIXEL_FORMAT_RGBX_8888;
440 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700441 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700442
443 // now select a corresponding sRGB format if needed
444 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
445 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
446 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700447 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800448 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700449 } else {
450 // Normally we'd pass through unhandled attributes to
451 // the driver. But in case the driver implements this
452 // extension but we're disabling it, we want to prevent
453 // it getting through -- support will be broken without
454 // our help.
455 ALOGE("sRGB window surfaces not supported");
456 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
457 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700458 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700459 }
460 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800461
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 if (format != 0) {
463 int err = native_window_set_buffers_format(window, format);
464 if (err != 0) {
465 ALOGE("error setting native window pixel format: %s (%d)",
466 strerror(-err), err);
467 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
468 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
469 }
470 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700471
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800472 if (dataSpace != 0) {
473 int err = native_window_set_buffers_data_space(window, dataSpace);
474 if (err != 0) {
475 ALOGE("error setting native window pixel dataSpace: %s (%d)",
476 strerror(-err), err);
477 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
478 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
479 }
480 }
481
Jamie Gennis59769462011-11-19 18:04:43 -0800482 // the EGL spec requires that a new EGLSurface default to swap interval
483 // 1, so explicitly set that on the window here.
484 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
485 anw->setSwapInterval(anw, 1);
486
Mathias Agopian518ec112011-05-13 16:21:08 -0700487 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800488 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700489 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700490 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
491 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700492 return s;
493 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700494
495 // EGLSurface creation failed
496 native_window_set_buffers_format(window, 0);
497 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 }
499 return EGL_NO_SURFACE;
500}
501
502EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
503 NativePixmapType pixmap,
504 const EGLint *attrib_list)
505{
506 clearError();
507
Jesse Hallb29e5e82012-04-04 16:53:42 -0700508 egl_connection_t* cnx = NULL;
509 egl_display_ptr dp = validate_display_connection(dpy, cnx);
510 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700511 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800512 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700513 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700514 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
515 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700516 return s;
517 }
518 }
519 return EGL_NO_SURFACE;
520}
521
522EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
523 const EGLint *attrib_list)
524{
525 clearError();
526
Jesse Hallb29e5e82012-04-04 16:53:42 -0700527 egl_connection_t* cnx = NULL;
528 egl_display_ptr dp = validate_display_connection(dpy, cnx);
529 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700530 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800531 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700532 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700533 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
534 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700535 return s;
536 }
537 }
538 return EGL_NO_SURFACE;
539}
Jesse Hall47743382013-02-08 11:13:46 -0800540
Mathias Agopian518ec112011-05-13 16:21:08 -0700541EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
542{
543 clearError();
544
Jesse Hallb29e5e82012-04-04 16:53:42 -0700545 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700546 if (!dp) return EGL_FALSE;
547
Jesse Hallb29e5e82012-04-04 16:53:42 -0700548 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700549 if (!_s.get())
550 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700551
552 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800553 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700554 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700555 _s.terminate();
556 }
557 return result;
558}
559
560EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
561 EGLint attribute, EGLint *value)
562{
563 clearError();
564
Jesse Hallb29e5e82012-04-04 16:53:42 -0700565 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700566 if (!dp) return EGL_FALSE;
567
Jesse Hallb29e5e82012-04-04 16:53:42 -0700568 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700569 if (!_s.get())
570 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700571
Mathias Agopian518ec112011-05-13 16:21:08 -0700572 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800573 return s->cnx->egl.eglQuerySurface(
574 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575}
576
Jamie Gennise8696a42012-01-15 18:54:57 -0800577void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800578 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800579 clearError();
580
Jesse Hallb29e5e82012-04-04 16:53:42 -0700581 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800582 if (!dp) {
583 return;
584 }
585
Jesse Hallb29e5e82012-04-04 16:53:42 -0700586 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800587 if (!_s.get()) {
588 setError(EGL_BAD_SURFACE, EGL_FALSE);
589 return;
590 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800591}
592
Mathias Agopian518ec112011-05-13 16:21:08 -0700593// ----------------------------------------------------------------------------
594// Contexts
595// ----------------------------------------------------------------------------
596
597EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
598 EGLContext share_list, const EGLint *attrib_list)
599{
600 clearError();
601
Jesse Hallb29e5e82012-04-04 16:53:42 -0700602 egl_connection_t* cnx = NULL;
603 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700604 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700605 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700606 if (!ContextRef(dp.get(), share_list).get()) {
607 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
608 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700609 egl_context_t* const c = get_context(share_list);
610 share_list = c->context;
611 }
612 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800613 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 if (context != EGL_NO_CONTEXT) {
615 // figure out if it's a GLESv1 or GLESv2
616 int version = 0;
617 if (attrib_list) {
618 while (*attrib_list != EGL_NONE) {
619 GLint attr = *attrib_list++;
620 GLint value = *attrib_list++;
621 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
622 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800623 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800624 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800625 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700626 }
627 }
628 };
629 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700630 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
631 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800632#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800633 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -0800634 GLTrace_eglCreateContext(version, c);
635#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700636 return c;
637 }
638 }
639 return EGL_NO_CONTEXT;
640}
641
642EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
643{
644 clearError();
645
Jesse Hallb29e5e82012-04-04 16:53:42 -0700646 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700647 if (!dp)
648 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700649
Jesse Hallb29e5e82012-04-04 16:53:42 -0700650 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700651 if (!_c.get())
652 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800653
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800655 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700656 if (result == EGL_TRUE) {
657 _c.terminate();
658 }
659 return result;
660}
661
Mathias Agopian518ec112011-05-13 16:21:08 -0700662EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
663 EGLSurface read, EGLContext ctx)
664{
665 clearError();
666
Jesse Hallb29e5e82012-04-04 16:53:42 -0700667 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700668 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
669
Mathias Agopian5b287a62011-05-16 18:58:55 -0700670 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
671 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
672 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700673 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
674 (draw != EGL_NO_SURFACE) ) {
675 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
676 }
677
678 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700679 ContextRef _c(dp.get(), ctx);
680 SurfaceRef _d(dp.get(), draw);
681 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700682
683 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700684 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700685 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700686 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700687 }
688
689 // these are the underlying implementation's object
690 EGLContext impl_ctx = EGL_NO_CONTEXT;
691 EGLSurface impl_draw = EGL_NO_SURFACE;
692 EGLSurface impl_read = EGL_NO_SURFACE;
693
694 // these are our objects structs passed in
695 egl_context_t * c = NULL;
696 egl_surface_t const * d = NULL;
697 egl_surface_t const * r = NULL;
698
699 // these are the current objects structs
700 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800701
Mathias Agopian518ec112011-05-13 16:21:08 -0700702 if (ctx != EGL_NO_CONTEXT) {
703 c = get_context(ctx);
704 impl_ctx = c->context;
705 } else {
706 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700707 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
708 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
709 return setError(EGL_BAD_MATCH, EGL_FALSE);
710 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700711 if (cur_c == NULL) {
712 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 // not an error, there is just no current context.
714 return EGL_TRUE;
715 }
716 }
717
718 // retrieve the underlying implementation's draw EGLSurface
719 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700720 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700721 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700722 impl_draw = d->surface;
723 }
724
725 // retrieve the underlying implementation's read EGLSurface
726 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700727 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700728 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700729 impl_read = r->surface;
730 }
731
Mathias Agopian518ec112011-05-13 16:21:08 -0700732
Jesse Hallb29e5e82012-04-04 16:53:42 -0700733 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800734 draw, read, ctx,
735 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700736
737 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800738 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
740 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800741#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800742 if (getEGLDebugLevel() > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800743 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800744#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700745 _c.acquire();
746 _r.acquire();
747 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700748 } else {
749 setGLHooksThreadSpecific(&gHooksNoContext);
750 egl_tls_t::setContext(EGL_NO_CONTEXT);
751 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700752 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000753 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700754 egl_connection_t* const cnx = &gEGLImpl;
755 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 }
757 return result;
758}
759
760
761EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
762 EGLint attribute, EGLint *value)
763{
764 clearError();
765
Jesse Hallb29e5e82012-04-04 16:53:42 -0700766 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 if (!dp) return EGL_FALSE;
768
Jesse Hallb29e5e82012-04-04 16:53:42 -0700769 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700770 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
771
Mathias Agopian518ec112011-05-13 16:21:08 -0700772 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800773 return c->cnx->egl.eglQueryContext(
774 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775
Mathias Agopian518ec112011-05-13 16:21:08 -0700776}
777
778EGLContext eglGetCurrentContext(void)
779{
780 // could be called before eglInitialize(), but we wouldn't have a context
781 // then, and this function would correctly return EGL_NO_CONTEXT.
782
783 clearError();
784
785 EGLContext ctx = getContext();
786 return ctx;
787}
788
789EGLSurface eglGetCurrentSurface(EGLint readdraw)
790{
791 // could be called before eglInitialize(), but we wouldn't have a context
792 // then, and this function would correctly return EGL_NO_SURFACE.
793
794 clearError();
795
796 EGLContext ctx = getContext();
797 if (ctx) {
798 egl_context_t const * const c = get_context(ctx);
799 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
800 switch (readdraw) {
801 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800802 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700803 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
804 }
805 }
806 return EGL_NO_SURFACE;
807}
808
809EGLDisplay eglGetCurrentDisplay(void)
810{
811 // could be called before eglInitialize(), but we wouldn't have a context
812 // then, and this function would correctly return EGL_NO_DISPLAY.
813
814 clearError();
815
816 EGLContext ctx = getContext();
817 if (ctx) {
818 egl_context_t const * const c = get_context(ctx);
819 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
820 return c->dpy;
821 }
822 return EGL_NO_DISPLAY;
823}
824
825EGLBoolean eglWaitGL(void)
826{
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 clearError();
828
Mathias Agopianada798b2012-02-13 17:09:30 -0800829 egl_connection_t* const cnx = &gEGLImpl;
830 if (!cnx->dso)
831 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
832
833 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700834}
835
836EGLBoolean eglWaitNative(EGLint engine)
837{
Mathias Agopian518ec112011-05-13 16:21:08 -0700838 clearError();
839
Mathias Agopianada798b2012-02-13 17:09:30 -0800840 egl_connection_t* const cnx = &gEGLImpl;
841 if (!cnx->dso)
842 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
843
844 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700845}
846
847EGLint eglGetError(void)
848{
Mathias Agopianada798b2012-02-13 17:09:30 -0800849 EGLint err = EGL_SUCCESS;
850 egl_connection_t* const cnx = &gEGLImpl;
851 if (cnx->dso) {
852 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800854 if (err == EGL_SUCCESS) {
855 err = egl_tls_t::getError();
856 }
857 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700858}
859
Michael Chockc0ec5e22014-01-27 08:14:33 -0800860static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700861 const char* procname) {
862 const egl_connection_t* cnx = &gEGLImpl;
863 void* proc = NULL;
864
Michael Chockc0ec5e22014-01-27 08:14:33 -0800865 proc = dlsym(cnx->libEgl, procname);
866 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
867
Jesse Hallc07b5202013-07-04 12:08:16 -0700868 proc = dlsym(cnx->libGles2, procname);
869 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
870
871 proc = dlsym(cnx->libGles1, procname);
872 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
873
874 return NULL;
875}
876
Mathias Agopian518ec112011-05-13 16:21:08 -0700877__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
878{
879 // eglGetProcAddress() could be the very first function called
880 // in which case we must make sure we've initialized ourselves, this
881 // happens the first time egl_get_display() is called.
882
883 clearError();
884
885 if (egl_init_drivers() == EGL_FALSE) {
886 setError(EGL_BAD_PARAMETER, NULL);
887 return NULL;
888 }
889
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700890 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700891 return NULL;
892 }
893
Mathias Agopian518ec112011-05-13 16:21:08 -0700894 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700895 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700896 if (addr) return addr;
897
Michael Chockc0ec5e22014-01-27 08:14:33 -0800898 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700899 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700900
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
902 pthread_mutex_lock(&sExtensionMapMutex);
903
904 /*
905 * Since eglGetProcAddress() is not associated to anything, it needs
906 * to return a function pointer that "works" regardless of what
907 * the current context is.
908 *
909 * For this reason, we return a "forwarder", a small stub that takes
910 * care of calling the function associated with the context
911 * currently bound.
912 *
913 * We first look for extensions we've already resolved, if we're seeing
914 * this extension for the first time, we go through all our
915 * implementations and call eglGetProcAddress() and record the
916 * result in the appropriate implementation hooks and return the
917 * address of the forwarder corresponding to that hook set.
918 *
919 */
920
921 const String8 name(procname);
922 addr = sGLExtentionMap.valueFor(name);
923 const int slot = sGLExtentionSlot;
924
Steve Blocke6f43dd2012-01-06 19:20:56 +0000925 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 "no more slots for eglGetProcAddress(\"%s\")",
927 procname);
928
Siva Velusamy0469dd62011-11-30 15:05:37 -0800929#if EGL_TRACE
930 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
931#endif
932
Mathias Agopian518ec112011-05-13 16:21:08 -0700933 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
934 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800935
936 egl_connection_t* const cnx = &gEGLImpl;
937 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800938 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800939 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800940 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
941 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700942#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800943 debugHooks->ext.extensions[slot] =
944 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700945#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800946 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800947 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700948 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 if (found) {
951 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 sGLExtentionMap.add(name, addr);
953 sGLExtentionSlot++;
954 }
955 }
956
957 pthread_mutex_unlock(&sExtensionMapMutex);
958 return addr;
959}
960
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700961class FrameCompletionThread : public Thread {
962public:
963
964 static void queueSync(EGLSyncKHR sync) {
965 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
966 static bool running = false;
967 if (!running) {
968 thread->run("GPUFrameCompletion");
969 running = true;
970 }
971 {
972 Mutex::Autolock lock(thread->mMutex);
973 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
974 thread->mFramesQueued).string());
975 thread->mQueue.push_back(sync);
976 thread->mCondition.signal();
977 thread->mFramesQueued++;
978 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
979 }
980 }
981
982private:
983 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
984
985 virtual bool threadLoop() {
986 EGLSyncKHR sync;
987 uint32_t frameNum;
988 {
989 Mutex::Autolock lock(mMutex);
990 while (mQueue.isEmpty()) {
991 mCondition.wait(mMutex);
992 }
993 sync = mQueue[0];
994 frameNum = mFramesCompleted;
995 }
996 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
997 {
998 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
999 frameNum).string());
1000 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1001 if (result == EGL_FALSE) {
1002 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1003 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1004 ALOGE("FrameCompletion: timeout waiting for fence");
1005 }
1006 eglDestroySyncKHR(dpy, sync);
1007 }
1008 {
1009 Mutex::Autolock lock(mMutex);
1010 mQueue.removeAt(0);
1011 mFramesCompleted++;
1012 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1013 }
1014 return true;
1015 }
1016
1017 uint32_t mFramesQueued;
1018 uint32_t mFramesCompleted;
1019 Vector<EGLSyncKHR> mQueue;
1020 Condition mCondition;
1021 Mutex mMutex;
1022};
1023
Mathias Agopian518ec112011-05-13 16:21:08 -07001024EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1025{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001026 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 clearError();
1028
Jesse Hallb29e5e82012-04-04 16:53:42 -07001029 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001030 if (!dp) return EGL_FALSE;
1031
Jesse Hallb29e5e82012-04-04 16:53:42 -07001032 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001033 if (!_s.get())
1034 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001035
Siva Velusamy0469dd62011-11-30 15:05:37 -08001036#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -08001037 gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
1038 if (getEGLDebugLevel() > 0) {
1039 if (trace_hooks == NULL) {
1040 if (GLTrace_start() < 0) {
1041 ALOGE("Disabling Tracer for OpenGL ES");
1042 setEGLDebugLevel(0);
1043 } else {
1044 // switch over to the trace version of hooks
1045 EGLContext ctx = egl_tls_t::getContext();
1046 egl_context_t * const c = get_context(ctx);
1047 if (c) {
1048 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1049 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
1050 }
1051 }
1052 }
1053
Siva Velusamy0469dd62011-11-30 15:05:37 -08001054 GLTrace_eglSwapBuffers(dpy, draw);
Siva Velusamya73a9772012-12-18 14:56:55 -08001055 } else if (trace_hooks != NULL) {
1056 // tracing is now disabled, so switch back to the non trace version
1057 EGLContext ctx = egl_tls_t::getContext();
1058 egl_context_t * const c = get_context(ctx);
1059 if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1060 GLTrace_stop();
1061 }
Siva Velusamy0469dd62011-11-30 15:05:37 -08001062#endif
1063
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001065
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001066 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1067 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1068 if (sync != EGL_NO_SYNC_KHR) {
1069 FrameCompletionThread::queueSync(sync);
1070 }
1071 }
1072
Mathias Agopian7db993a2012-03-25 00:49:46 -07001073 if (CC_UNLIKELY(dp->finishOnSwap)) {
1074 uint32_t pixel;
1075 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1076 if (c) {
1077 // glReadPixels() ensures that the frame is complete
1078 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1079 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1080 }
1081 }
1082
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001083 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084}
1085
1086EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1087 NativePixmapType target)
1088{
1089 clearError();
1090
Jesse Hallb29e5e82012-04-04 16:53:42 -07001091 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 if (!dp) return EGL_FALSE;
1093
Jesse Hallb29e5e82012-04-04 16:53:42 -07001094 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001095 if (!_s.get())
1096 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001097
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001099 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001100}
1101
1102const char* eglQueryString(EGLDisplay dpy, EGLint name)
1103{
1104 clearError();
1105
Jesse Hallb29e5e82012-04-04 16:53:42 -07001106 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 if (!dp) return (const char *) NULL;
1108
1109 switch (name) {
1110 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001111 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001112 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001113 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001114 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001115 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001116 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001117 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001118 }
1119 return setError(EGL_BAD_PARAMETER, (const char *)0);
1120}
1121
Mathias Agopianca088332013-03-28 17:44:13 -07001122EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1123{
1124 clearError();
1125
1126 const egl_display_ptr dp = validate_display(dpy);
1127 if (!dp) return (const char *) NULL;
1128
1129 switch (name) {
1130 case EGL_VENDOR:
1131 return dp->disp.queryString.vendor;
1132 case EGL_VERSION:
1133 return dp->disp.queryString.version;
1134 case EGL_EXTENSIONS:
1135 return dp->disp.queryString.extensions;
1136 case EGL_CLIENT_APIS:
1137 return dp->disp.queryString.clientApi;
1138 }
1139 return setError(EGL_BAD_PARAMETER, (const char *)0);
1140}
Mathias Agopian518ec112011-05-13 16:21:08 -07001141
1142// ----------------------------------------------------------------------------
1143// EGL 1.1
1144// ----------------------------------------------------------------------------
1145
1146EGLBoolean eglSurfaceAttrib(
1147 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1148{
1149 clearError();
1150
Jesse Hallb29e5e82012-04-04 16:53:42 -07001151 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001152 if (!dp) return EGL_FALSE;
1153
Jesse Hallb29e5e82012-04-04 16:53:42 -07001154 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001155 if (!_s.get())
1156 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001157
Mathias Agopian518ec112011-05-13 16:21:08 -07001158 egl_surface_t const * const s = get_surface(surface);
1159 if (s->cnx->egl.eglSurfaceAttrib) {
1160 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001161 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 }
1163 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1164}
1165
1166EGLBoolean eglBindTexImage(
1167 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1168{
1169 clearError();
1170
Jesse Hallb29e5e82012-04-04 16:53:42 -07001171 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001172 if (!dp) return EGL_FALSE;
1173
Jesse Hallb29e5e82012-04-04 16:53:42 -07001174 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001175 if (!_s.get())
1176 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001177
Mathias Agopian518ec112011-05-13 16:21:08 -07001178 egl_surface_t const * const s = get_surface(surface);
1179 if (s->cnx->egl.eglBindTexImage) {
1180 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001181 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001182 }
1183 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1184}
1185
1186EGLBoolean eglReleaseTexImage(
1187 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1188{
1189 clearError();
1190
Jesse Hallb29e5e82012-04-04 16:53:42 -07001191 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001192 if (!dp) return EGL_FALSE;
1193
Jesse Hallb29e5e82012-04-04 16:53:42 -07001194 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001195 if (!_s.get())
1196 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001197
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 egl_surface_t const * const s = get_surface(surface);
1199 if (s->cnx->egl.eglReleaseTexImage) {
1200 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001201 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001202 }
1203 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1204}
1205
1206EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1207{
1208 clearError();
1209
Jesse Hallb29e5e82012-04-04 16:53:42 -07001210 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 if (!dp) return EGL_FALSE;
1212
1213 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001214 egl_connection_t* const cnx = &gEGLImpl;
1215 if (cnx->dso && cnx->egl.eglSwapInterval) {
1216 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001217 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001218
Mathias Agopian518ec112011-05-13 16:21:08 -07001219 return res;
1220}
1221
1222
1223// ----------------------------------------------------------------------------
1224// EGL 1.2
1225// ----------------------------------------------------------------------------
1226
1227EGLBoolean eglWaitClient(void)
1228{
1229 clearError();
1230
Mathias Agopianada798b2012-02-13 17:09:30 -08001231 egl_connection_t* const cnx = &gEGLImpl;
1232 if (!cnx->dso)
1233 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1234
1235 EGLBoolean res;
1236 if (cnx->egl.eglWaitClient) {
1237 res = cnx->egl.eglWaitClient();
1238 } else {
1239 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001240 }
1241 return res;
1242}
1243
1244EGLBoolean eglBindAPI(EGLenum api)
1245{
1246 clearError();
1247
1248 if (egl_init_drivers() == EGL_FALSE) {
1249 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1250 }
1251
1252 // bind this API on all EGLs
1253 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001254 egl_connection_t* const cnx = &gEGLImpl;
1255 if (cnx->dso && cnx->egl.eglBindAPI) {
1256 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001257 }
1258 return res;
1259}
1260
1261EGLenum eglQueryAPI(void)
1262{
1263 clearError();
1264
1265 if (egl_init_drivers() == EGL_FALSE) {
1266 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1267 }
1268
Mathias Agopianada798b2012-02-13 17:09:30 -08001269 egl_connection_t* const cnx = &gEGLImpl;
1270 if (cnx->dso && cnx->egl.eglQueryAPI) {
1271 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001273
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 // or, it can only be OpenGL ES
1275 return EGL_OPENGL_ES_API;
1276}
1277
1278EGLBoolean eglReleaseThread(void)
1279{
1280 clearError();
1281
Mathias Agopian4e620dd2013-05-30 16:07:36 -07001282#if EGL_TRACE
1283 if (getEGLDebugLevel() > 0)
1284 GLTrace_eglReleaseThread();
1285#endif
1286
Mathias Agopian518ec112011-05-13 16:21:08 -07001287 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001288 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001289
Mathias Agopianada798b2012-02-13 17:09:30 -08001290 egl_connection_t* const cnx = &gEGLImpl;
1291 if (cnx->dso && cnx->egl.eglReleaseThread) {
1292 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001293 }
1294 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001295 return EGL_TRUE;
1296}
1297
1298EGLSurface eglCreatePbufferFromClientBuffer(
1299 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1300 EGLConfig config, const EGLint *attrib_list)
1301{
1302 clearError();
1303
Jesse Hallb29e5e82012-04-04 16:53:42 -07001304 egl_connection_t* cnx = NULL;
1305 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1306 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001307 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1308 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001309 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001310 }
1311 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1312}
1313
1314// ----------------------------------------------------------------------------
1315// EGL_EGLEXT_VERSION 3
1316// ----------------------------------------------------------------------------
1317
1318EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1319 const EGLint *attrib_list)
1320{
1321 clearError();
1322
Jesse Hallb29e5e82012-04-04 16:53:42 -07001323 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001324 if (!dp) return EGL_FALSE;
1325
Jesse Hallb29e5e82012-04-04 16:53:42 -07001326 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001327 if (!_s.get())
1328 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001329
1330 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001331 if (s->cnx->egl.eglLockSurfaceKHR) {
1332 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001333 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001334 }
1335 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1336}
1337
1338EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1339{
1340 clearError();
1341
Jesse Hallb29e5e82012-04-04 16:53:42 -07001342 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001343 if (!dp) return EGL_FALSE;
1344
Jesse Hallb29e5e82012-04-04 16:53:42 -07001345 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001346 if (!_s.get())
1347 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001348
1349 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001351 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001352 }
1353 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1354}
1355
1356EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1357 EGLClientBuffer buffer, const EGLint *attrib_list)
1358{
1359 clearError();
1360
Jesse Hallb29e5e82012-04-04 16:53:42 -07001361 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001362 if (!dp) return EGL_NO_IMAGE_KHR;
1363
Jesse Hallb29e5e82012-04-04 16:53:42 -07001364 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001365 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001366
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001367 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1368 egl_connection_t* const cnx = &gEGLImpl;
1369 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1370 result = cnx->egl.eglCreateImageKHR(
1371 dp->disp.dpy,
1372 c ? c->context : EGL_NO_CONTEXT,
1373 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001375 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001376}
1377
1378EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1379{
1380 clearError();
1381
Jesse Hallb29e5e82012-04-04 16:53:42 -07001382 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001383 if (!dp) return EGL_FALSE;
1384
Steven Holte646a5c52012-06-04 20:02:11 -07001385 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001386 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001387 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001388 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001389 }
Steven Holte646a5c52012-06-04 20:02:11 -07001390 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001391}
1392
1393// ----------------------------------------------------------------------------
1394// EGL_EGLEXT_VERSION 5
1395// ----------------------------------------------------------------------------
1396
1397
1398EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1399{
1400 clearError();
1401
Jesse Hallb29e5e82012-04-04 16:53:42 -07001402 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (!dp) return EGL_NO_SYNC_KHR;
1404
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001406 egl_connection_t* const cnx = &gEGLImpl;
1407 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1408 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001409 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001410 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001411}
1412
1413EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1414{
1415 clearError();
1416
Jesse Hallb29e5e82012-04-04 16:53:42 -07001417 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418 if (!dp) return EGL_FALSE;
1419
Mathias Agopian518ec112011-05-13 16:21:08 -07001420 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001421 egl_connection_t* const cnx = &gEGLImpl;
1422 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1423 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001424 }
1425 return result;
1426}
1427
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001428EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1429 clearError();
1430
1431 const egl_display_ptr dp = validate_display(dpy);
1432 if (!dp) return EGL_FALSE;
1433
1434 EGLBoolean result = EGL_FALSE;
1435 egl_connection_t* const cnx = &gEGLImpl;
1436 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1437 result = cnx->egl.eglSignalSyncKHR(
1438 dp->disp.dpy, sync, mode);
1439 }
1440 return result;
1441}
1442
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001443EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1444 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001445{
1446 clearError();
1447
Jesse Hallb29e5e82012-04-04 16:53:42 -07001448 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001449 if (!dp) return EGL_FALSE;
1450
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001451 EGLBoolean result = EGL_FALSE;
1452 egl_connection_t* const cnx = &gEGLImpl;
1453 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1454 result = cnx->egl.eglClientWaitSyncKHR(
1455 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001456 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001457 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001458}
1459
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001460EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1461 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001462{
1463 clearError();
1464
Jesse Hallb29e5e82012-04-04 16:53:42 -07001465 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001466 if (!dp) return EGL_FALSE;
1467
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001468 EGLBoolean result = EGL_FALSE;
1469 egl_connection_t* const cnx = &gEGLImpl;
1470 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1471 result = cnx->egl.eglGetSyncAttribKHR(
1472 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001473 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001474 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001475}
1476
1477// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001478// EGL_EGLEXT_VERSION 15
1479// ----------------------------------------------------------------------------
1480
1481EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1482 clearError();
1483 const egl_display_ptr dp = validate_display(dpy);
1484 if (!dp) return EGL_FALSE;
1485 EGLint result = EGL_FALSE;
1486 egl_connection_t* const cnx = &gEGLImpl;
1487 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1488 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1489 }
1490 return result;
1491}
1492
1493// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001494// ANDROID extensions
1495// ----------------------------------------------------------------------------
1496
Jamie Gennis331841b2012-09-06 14:52:00 -07001497EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1498{
1499 clearError();
1500
1501 const egl_display_ptr dp = validate_display(dpy);
1502 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1503
1504 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1505 egl_connection_t* const cnx = &gEGLImpl;
1506 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1507 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1508 }
1509 return result;
1510}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001511
Andy McFadden72841452013-03-01 16:25:32 -08001512EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1513 EGLnsecsANDROID time)
1514{
1515 clearError();
1516
1517 const egl_display_ptr dp = validate_display(dpy);
1518 if (!dp) {
1519 return EGL_FALSE;
1520 }
1521
1522 SurfaceRef _s(dp.get(), surface);
1523 if (!_s.get()) {
1524 setError(EGL_BAD_SURFACE, EGL_FALSE);
1525 return EGL_FALSE;
1526 }
1527
1528 egl_surface_t const * const s = get_surface(surface);
1529 native_window_set_buffers_timestamp(s->win.get(), time);
1530
1531 return EGL_TRUE;
1532}
1533
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001534// ----------------------------------------------------------------------------
1535// NVIDIA extensions
1536// ----------------------------------------------------------------------------
1537EGLuint64NV eglGetSystemTimeFrequencyNV()
1538{
1539 clearError();
1540
1541 if (egl_init_drivers() == EGL_FALSE) {
1542 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1543 }
1544
1545 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001546 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001547
Mathias Agopianada798b2012-02-13 17:09:30 -08001548 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1549 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001550 }
1551
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001552 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001553}
1554
1555EGLuint64NV eglGetSystemTimeNV()
1556{
1557 clearError();
1558
1559 if (egl_init_drivers() == EGL_FALSE) {
1560 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1561 }
1562
1563 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001564 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001565
Mathias Agopianada798b2012-02-13 17:09:30 -08001566 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1567 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001568 }
1569
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001570 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001571}