blob: 2bc9851f0ca0532f0de1f188e0510f90d783f82d [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
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <hardware/gralloc.h>
24#include <system/window.h>
25
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
29#include <cutils/log.h>
30#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032#include <cutils/properties.h>
33#include <cutils/memory.h>
34
35#include <utils/KeyedVector.h>
36#include <utils/SortedVector.h>
37#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080038#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070039
Mathias Agopian39c24a22013-04-04 23:17:56 -070040#include "../egl_impl.h"
41#include "../glestrace.h"
42#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070043
44#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070045#include "egl_object.h"
46#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080047#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070048
49using namespace android;
50
51// ----------------------------------------------------------------------------
52
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070053namespace android {
54
Mathias Agopian518ec112011-05-13 16:21:08 -070055struct extention_map_t {
56 const char* name;
57 __eglMustCastToProperFunctionPointerType address;
58};
59
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070060/*
61 * This is the list of EGL extensions exposed to applications,
62 * some of them are mandatory because used by the ANDROID system.
63 *
64 * Mandatory extensions are required per the CDD and not explicitly
65 * checked during EGL initialization. the system *assumes* these extensions
66 * are present. the system may not function properly if some mandatory
67 * extensions are missing.
68 *
69 * NOTE: gExtensionString MUST have a single space as the last character.
70 */
71extern char const * const gExtensionString =
72 "EGL_KHR_image " // mandatory
73 "EGL_KHR_image_base " // mandatory
74 "EGL_KHR_image_pixmap "
75 "EGL_KHR_lock_surface "
76 "EGL_KHR_gl_texture_2D_image "
77 "EGL_KHR_gl_texture_cubemap_image "
78 "EGL_KHR_gl_renderbuffer_image "
79 "EGL_KHR_reusable_sync "
80 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -070081 "EGL_KHR_create_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070082 "EGL_EXT_create_context_robustness "
83 "EGL_NV_system_time "
84 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -070085 "EGL_KHR_wait_sync " // strongly recommended
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070086 "EGL_ANDROID_presentation_time "
87 ;
88
89// extensions not exposed to applications but used by the ANDROID system
90// "EGL_ANDROID_blob_cache " // strongly recommended
91// "EGL_IMG_hibernate_process " // optional
92// "EGL_ANDROID_native_fence_sync " // strongly recommended
93// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
94// "EGL_ANDROID_recordable " // mandatory
95
96
97/*
98 * EGL Extensions entry-points exposed to 3rd party applications
99 * (keep in sync with gExtensionString above)
100 *
101 */
102static const extention_map_t sExtensionMap[] = {
103 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 { "eglLockSurfaceKHR",
105 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
106 { "eglUnlockSurfaceKHR",
107 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700108
109 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700110 { "eglCreateImageKHR",
111 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
112 { "eglDestroyImageKHR",
113 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700114
115 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
116 { "eglCreateSyncKHR",
117 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
118 { "eglDestroySyncKHR",
119 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
120 { "eglClientWaitSyncKHR",
121 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
122 { "eglSignalSyncKHR",
123 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
124 { "eglGetSyncAttribKHR",
125 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
126
127 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800128 { "eglGetSystemTimeFrequencyNV",
129 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
130 { "eglGetSystemTimeNV",
131 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700132
Mathias Agopian2bb71682013-03-27 17:32:41 -0700133 // EGL_KHR_wait_sync
134 { "eglWaitSyncKHR",
135 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700136
137 // EGL_ANDROID_presentation_time
138 { "eglPresentationTimeANDROID",
139 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700140};
141
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700142/*
143 * These extensions entry-points should not be exposed to applications.
144 * They're used internally by the Android EGL layer.
145 */
146#define FILTER_EXTENSIONS(procname) \
147 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
148 !strcmp((procname), "eglHibernateProcessIMG") || \
149 !strcmp((procname), "eglAwakenProcessIMG") || \
150 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
151
152
153
Mathias Agopian518ec112011-05-13 16:21:08 -0700154// accesses protected by sExtensionMapMutex
155static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
156static int sGLExtentionSlot = 0;
157static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
158
159static void(*findProcAddress(const char* name,
160 const extention_map_t* map, size_t n))() {
161 for (uint32_t i=0 ; i<n ; i++) {
162 if (!strcmp(name, map[i].name)) {
163 return map[i].address;
164 }
165 }
166 return NULL;
167}
168
169// ----------------------------------------------------------------------------
170
Mathias Agopian518ec112011-05-13 16:21:08 -0700171extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
172extern EGLBoolean egl_init_drivers();
173extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Siva Velusamya73a9772012-12-18 14:56:55 -0800174extern int getEGLDebugLevel();
175extern void setEGLDebugLevel(int level);
Mathias Agopian518ec112011-05-13 16:21:08 -0700176extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700177
Mathias Agopian518ec112011-05-13 16:21:08 -0700178} // namespace android;
179
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700180
Mathias Agopian518ec112011-05-13 16:21:08 -0700181// ----------------------------------------------------------------------------
182
183static inline void clearError() { egl_tls_t::clearError(); }
184static inline EGLContext getContext() { return egl_tls_t::getContext(); }
185
186// ----------------------------------------------------------------------------
187
188EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
189{
190 clearError();
191
192 uint32_t index = uint32_t(display);
193 if (index >= NUM_DISPLAYS) {
194 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
195 }
196
197 if (egl_init_drivers() == EGL_FALSE) {
198 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
199 }
200
201 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
202 return dpy;
203}
204
205// ----------------------------------------------------------------------------
206// Initialization
207// ----------------------------------------------------------------------------
208
209EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
210{
211 clearError();
212
Jesse Hallb29e5e82012-04-04 16:53:42 -0700213 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700214 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
215
216 EGLBoolean res = dp->initialize(major, minor);
217
218 return res;
219}
220
221EGLBoolean eglTerminate(EGLDisplay dpy)
222{
223 // NOTE: don't unload the drivers b/c some APIs can be called
224 // after eglTerminate() has been called. eglTerminate() only
225 // terminates an EGLDisplay, not a EGL itself.
226
227 clearError();
228
Jesse Hallb29e5e82012-04-04 16:53:42 -0700229 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700230 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
231
232 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800233
Mathias Agopian518ec112011-05-13 16:21:08 -0700234 return res;
235}
236
237// ----------------------------------------------------------------------------
238// configuration
239// ----------------------------------------------------------------------------
240
241EGLBoolean eglGetConfigs( EGLDisplay dpy,
242 EGLConfig *configs,
243 EGLint config_size, EGLint *num_config)
244{
245 clearError();
246
Jesse Hallb29e5e82012-04-04 16:53:42 -0700247 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700248 if (!dp) return EGL_FALSE;
249
Mathias Agopian7773c432012-02-13 20:06:08 -0800250 if (num_config==0) {
251 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700252 }
253
Mathias Agopian7773c432012-02-13 20:06:08 -0800254 EGLBoolean res = EGL_FALSE;
255 *num_config = 0;
256
257 egl_connection_t* const cnx = &gEGLImpl;
258 if (cnx->dso) {
259 res = cnx->egl.eglGetConfigs(
260 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700261 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800262
263 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700264}
265
266EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
267 EGLConfig *configs, EGLint config_size,
268 EGLint *num_config)
269{
270 clearError();
271
Jesse Hallb29e5e82012-04-04 16:53:42 -0700272 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 if (!dp) return EGL_FALSE;
274
275 if (num_config==0) {
276 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
277 }
278
Mathias Agopian518ec112011-05-13 16:21:08 -0700279 EGLBoolean res = EGL_FALSE;
280 *num_config = 0;
281
Mathias Agopianada798b2012-02-13 17:09:30 -0800282 egl_connection_t* const cnx = &gEGLImpl;
283 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700284 if (attrib_list) {
285 char value[PROPERTY_VALUE_MAX];
286 property_get("debug.egl.force_msaa", value, "false");
287
288 if (!strcmp(value, "true")) {
289 size_t attribCount = 0;
290 EGLint attrib = attrib_list[0];
291
292 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700293 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700294 const EGLint *attribRendererable = NULL;
295 const EGLint *attribCaveat = NULL;
296
297 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700298 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700299 while (attrib != EGL_NONE) {
300 attrib = attrib_list[attribCount];
301 switch (attrib) {
302 case EGL_RENDERABLE_TYPE:
303 attribRendererable = &attrib_list[attribCount];
304 break;
305 case EGL_CONFIG_CAVEAT:
306 attribCaveat = &attrib_list[attribCount];
307 break;
308 }
309 attribCount++;
310 }
311
312 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
313 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800314
Romain Guy1cffc802012-10-15 18:13:05 -0700315 // Insert 2 extra attributes to force-enable MSAA 4x
316 EGLint aaAttribs[attribCount + 4];
317 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
318 aaAttribs[1] = 1;
319 aaAttribs[2] = EGL_SAMPLES;
320 aaAttribs[3] = 4;
321
322 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
323
324 EGLint numConfigAA;
325 EGLBoolean resAA = cnx->egl.eglChooseConfig(
326 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
327
328 if (resAA == EGL_TRUE && numConfigAA > 0) {
329 ALOGD("Enabling MSAA 4x");
330 *num_config = numConfigAA;
331 return resAA;
332 }
333 }
334 }
335 }
336
Mathias Agopian7773c432012-02-13 20:06:08 -0800337 res = cnx->egl.eglChooseConfig(
338 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700339 }
340 return res;
341}
342
343EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
344 EGLint attribute, EGLint *value)
345{
346 clearError();
347
Jesse Hallb29e5e82012-04-04 16:53:42 -0700348 egl_connection_t* cnx = NULL;
349 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
350 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800351
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800353 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354}
355
356// ----------------------------------------------------------------------------
357// surfaces
358// ----------------------------------------------------------------------------
359
360EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
361 NativeWindowType window,
362 const EGLint *attrib_list)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 egl_connection_t* cnx = NULL;
367 egl_display_ptr dp = validate_display_connection(dpy, cnx);
368 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800369 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700370 EGLint format;
371
Mathias Agopian81a63352011-07-29 17:55:48 -0700372 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000373 ALOGE("EGLNativeWindowType %p already connected to another API",
Mathias Agopian81a63352011-07-29 17:55:48 -0700374 window);
375 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
376 }
377
Mathias Agopian518ec112011-05-13 16:21:08 -0700378 // set the native window's buffers format to match this config
379 if (cnx->egl.eglGetConfigAttrib(iDpy,
Mathias Agopian7773c432012-02-13 20:06:08 -0800380 config, EGL_NATIVE_VISUAL_ID, &format)) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700381 if (format != 0) {
Jamie Gennisbee205f2011-07-01 13:12:07 -0700382 int err = native_window_set_buffers_format(window, format);
383 if (err != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000384 ALOGE("error setting native window pixel format: %s (%d)",
Jamie Gennisbee205f2011-07-01 13:12:07 -0700385 strerror(-err), err);
Mathias Agopian81a63352011-07-29 17:55:48 -0700386 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700387 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
388 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700389 }
390 }
391
Jamie Gennis59769462011-11-19 18:04:43 -0800392 // the EGL spec requires that a new EGLSurface default to swap interval
393 // 1, so explicitly set that on the window here.
394 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
395 anw->setSwapInterval(anw, 1);
396
Mathias Agopian518ec112011-05-13 16:21:08 -0700397 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800398 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700399 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700400 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
401 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700402 return s;
403 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700404
405 // EGLSurface creation failed
406 native_window_set_buffers_format(window, 0);
407 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700408 }
409 return EGL_NO_SURFACE;
410}
411
412EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
413 NativePixmapType pixmap,
414 const EGLint *attrib_list)
415{
416 clearError();
417
Jesse Hallb29e5e82012-04-04 16:53:42 -0700418 egl_connection_t* cnx = NULL;
419 egl_display_ptr dp = validate_display_connection(dpy, cnx);
420 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700421 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800422 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700423 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700424 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
425 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700426 return s;
427 }
428 }
429 return EGL_NO_SURFACE;
430}
431
432EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
433 const EGLint *attrib_list)
434{
435 clearError();
436
Jesse Hallb29e5e82012-04-04 16:53:42 -0700437 egl_connection_t* cnx = NULL;
438 egl_display_ptr dp = validate_display_connection(dpy, cnx);
439 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800441 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700442 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700443 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
444 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700445 return s;
446 }
447 }
448 return EGL_NO_SURFACE;
449}
Jesse Hall47743382013-02-08 11:13:46 -0800450
Mathias Agopian518ec112011-05-13 16:21:08 -0700451EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
452{
453 clearError();
454
Jesse Hallb29e5e82012-04-04 16:53:42 -0700455 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700456 if (!dp) return EGL_FALSE;
457
Jesse Hallb29e5e82012-04-04 16:53:42 -0700458 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700459 if (!_s.get())
460 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700461
462 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800463 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700464 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700465 _s.terminate();
466 }
467 return result;
468}
469
470EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
471 EGLint attribute, EGLint *value)
472{
473 clearError();
474
Jesse Hallb29e5e82012-04-04 16:53:42 -0700475 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700476 if (!dp) return EGL_FALSE;
477
Jesse Hallb29e5e82012-04-04 16:53:42 -0700478 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700479 if (!_s.get())
480 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700481
Mathias Agopian518ec112011-05-13 16:21:08 -0700482 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800483 return s->cnx->egl.eglQuerySurface(
484 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700485}
486
Jamie Gennise8696a42012-01-15 18:54:57 -0800487void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800488 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800489 clearError();
490
Jesse Hallb29e5e82012-04-04 16:53:42 -0700491 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800492 if (!dp) {
493 return;
494 }
495
Jesse Hallb29e5e82012-04-04 16:53:42 -0700496 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800497 if (!_s.get()) {
498 setError(EGL_BAD_SURFACE, EGL_FALSE);
499 return;
500 }
501
502 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
503
504 egl_surface_t const * const s = get_surface(surface);
505 native_window_set_buffers_timestamp(s->win.get(), timestamp);
506}
507
Mathias Agopian518ec112011-05-13 16:21:08 -0700508// ----------------------------------------------------------------------------
509// Contexts
510// ----------------------------------------------------------------------------
511
512EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
513 EGLContext share_list, const EGLint *attrib_list)
514{
515 clearError();
516
Jesse Hallb29e5e82012-04-04 16:53:42 -0700517 egl_connection_t* cnx = NULL;
518 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700519 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700520 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700521 if (!ContextRef(dp.get(), share_list).get()) {
522 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
523 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700524 egl_context_t* const c = get_context(share_list);
525 share_list = c->context;
526 }
527 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800528 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700529 if (context != EGL_NO_CONTEXT) {
530 // figure out if it's a GLESv1 or GLESv2
531 int version = 0;
532 if (attrib_list) {
533 while (*attrib_list != EGL_NONE) {
534 GLint attr = *attrib_list++;
535 GLint value = *attrib_list++;
536 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
537 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800538 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800539 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800540 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700541 }
542 }
543 };
544 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700545 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
546 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800547#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800548 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -0800549 GLTrace_eglCreateContext(version, c);
550#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700551 return c;
Mathias Agopian500407a2012-09-24 17:57:48 -0700552 } else {
553 EGLint error = eglGetError();
554 ALOGE_IF(error == EGL_SUCCESS,
555 "eglCreateContext(%p, %p, %p, %p) returned EGL_NO_CONTEXT "
556 "but no EGL error!",
557 dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 }
559 }
560 return EGL_NO_CONTEXT;
561}
562
563EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
564{
565 clearError();
566
Jesse Hallb29e5e82012-04-04 16:53:42 -0700567 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700568 if (!dp)
569 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700570
Jesse Hallb29e5e82012-04-04 16:53:42 -0700571 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700572 if (!_c.get())
573 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800574
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800576 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700577 if (result == EGL_TRUE) {
578 _c.terminate();
579 }
580 return result;
581}
582
Mathias Agopian518ec112011-05-13 16:21:08 -0700583EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
584 EGLSurface read, EGLContext ctx)
585{
586 clearError();
587
Jesse Hallb29e5e82012-04-04 16:53:42 -0700588 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700589 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
590
Mathias Agopian5b287a62011-05-16 18:58:55 -0700591 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
592 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
593 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700594 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
595 (draw != EGL_NO_SURFACE) ) {
596 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
597 }
598
599 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700600 ContextRef _c(dp.get(), ctx);
601 SurfaceRef _d(dp.get(), draw);
602 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700603
604 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700605 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700607 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700608 }
609
610 // these are the underlying implementation's object
611 EGLContext impl_ctx = EGL_NO_CONTEXT;
612 EGLSurface impl_draw = EGL_NO_SURFACE;
613 EGLSurface impl_read = EGL_NO_SURFACE;
614
615 // these are our objects structs passed in
616 egl_context_t * c = NULL;
617 egl_surface_t const * d = NULL;
618 egl_surface_t const * r = NULL;
619
620 // these are the current objects structs
621 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 if (ctx != EGL_NO_CONTEXT) {
624 c = get_context(ctx);
625 impl_ctx = c->context;
626 } else {
627 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700628 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
629 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
630 return setError(EGL_BAD_MATCH, EGL_FALSE);
631 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700632 if (cur_c == NULL) {
633 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700634 // not an error, there is just no current context.
635 return EGL_TRUE;
636 }
637 }
638
639 // retrieve the underlying implementation's draw EGLSurface
640 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700641 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700642 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700643 impl_draw = d->surface;
644 }
645
646 // retrieve the underlying implementation's read EGLSurface
647 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700648 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700649 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700650 impl_read = r->surface;
651 }
652
Mathias Agopian518ec112011-05-13 16:21:08 -0700653
Jesse Hallb29e5e82012-04-04 16:53:42 -0700654 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800655 draw, read, ctx,
656 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700657
658 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800659 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700660 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
661 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800662#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800663 if (getEGLDebugLevel() > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800664 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800665#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700666 _c.acquire();
667 _r.acquire();
668 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700669 } else {
670 setGLHooksThreadSpecific(&gHooksNoContext);
671 egl_tls_t::setContext(EGL_NO_CONTEXT);
672 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700673 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000674 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700675 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700676 }
677 return result;
678}
679
680
681EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
682 EGLint attribute, EGLint *value)
683{
684 clearError();
685
Jesse Hallb29e5e82012-04-04 16:53:42 -0700686 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700687 if (!dp) return EGL_FALSE;
688
Jesse Hallb29e5e82012-04-04 16:53:42 -0700689 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
691
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800693 return c->cnx->egl.eglQueryContext(
694 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700695
Mathias Agopian518ec112011-05-13 16:21:08 -0700696}
697
698EGLContext eglGetCurrentContext(void)
699{
700 // could be called before eglInitialize(), but we wouldn't have a context
701 // then, and this function would correctly return EGL_NO_CONTEXT.
702
703 clearError();
704
705 EGLContext ctx = getContext();
706 return ctx;
707}
708
709EGLSurface eglGetCurrentSurface(EGLint readdraw)
710{
711 // could be called before eglInitialize(), but we wouldn't have a context
712 // then, and this function would correctly return EGL_NO_SURFACE.
713
714 clearError();
715
716 EGLContext ctx = getContext();
717 if (ctx) {
718 egl_context_t const * const c = get_context(ctx);
719 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
720 switch (readdraw) {
721 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800722 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700723 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
724 }
725 }
726 return EGL_NO_SURFACE;
727}
728
729EGLDisplay eglGetCurrentDisplay(void)
730{
731 // could be called before eglInitialize(), but we wouldn't have a context
732 // then, and this function would correctly return EGL_NO_DISPLAY.
733
734 clearError();
735
736 EGLContext ctx = getContext();
737 if (ctx) {
738 egl_context_t const * const c = get_context(ctx);
739 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
740 return c->dpy;
741 }
742 return EGL_NO_DISPLAY;
743}
744
745EGLBoolean eglWaitGL(void)
746{
Mathias Agopian518ec112011-05-13 16:21:08 -0700747 clearError();
748
Mathias Agopianada798b2012-02-13 17:09:30 -0800749 egl_connection_t* const cnx = &gEGLImpl;
750 if (!cnx->dso)
751 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
752
753 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700754}
755
756EGLBoolean eglWaitNative(EGLint engine)
757{
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 clearError();
759
Mathias Agopianada798b2012-02-13 17:09:30 -0800760 egl_connection_t* const cnx = &gEGLImpl;
761 if (!cnx->dso)
762 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
763
764 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765}
766
767EGLint eglGetError(void)
768{
Mathias Agopianada798b2012-02-13 17:09:30 -0800769 EGLint err = EGL_SUCCESS;
770 egl_connection_t* const cnx = &gEGLImpl;
771 if (cnx->dso) {
772 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800774 if (err == EGL_SUCCESS) {
775 err = egl_tls_t::getError();
776 }
777 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700778}
779
Mathias Agopian518ec112011-05-13 16:21:08 -0700780__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
781{
782 // eglGetProcAddress() could be the very first function called
783 // in which case we must make sure we've initialized ourselves, this
784 // happens the first time egl_get_display() is called.
785
786 clearError();
787
788 if (egl_init_drivers() == EGL_FALSE) {
789 setError(EGL_BAD_PARAMETER, NULL);
790 return NULL;
791 }
792
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700793 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700794 return NULL;
795 }
796
Mathias Agopian518ec112011-05-13 16:21:08 -0700797 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700798 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 if (addr) return addr;
800
Jamie Gennisaca51c02011-11-03 17:42:43 -0700801
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
803 pthread_mutex_lock(&sExtensionMapMutex);
804
805 /*
806 * Since eglGetProcAddress() is not associated to anything, it needs
807 * to return a function pointer that "works" regardless of what
808 * the current context is.
809 *
810 * For this reason, we return a "forwarder", a small stub that takes
811 * care of calling the function associated with the context
812 * currently bound.
813 *
814 * We first look for extensions we've already resolved, if we're seeing
815 * this extension for the first time, we go through all our
816 * implementations and call eglGetProcAddress() and record the
817 * result in the appropriate implementation hooks and return the
818 * address of the forwarder corresponding to that hook set.
819 *
820 */
821
822 const String8 name(procname);
823 addr = sGLExtentionMap.valueFor(name);
824 const int slot = sGLExtentionSlot;
825
Steve Blocke6f43dd2012-01-06 19:20:56 +0000826 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 "no more slots for eglGetProcAddress(\"%s\")",
828 procname);
829
Siva Velusamy0469dd62011-11-30 15:05:37 -0800830#if EGL_TRACE
831 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
832#endif
833
Mathias Agopian518ec112011-05-13 16:21:08 -0700834 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
835 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800836
837 egl_connection_t* const cnx = &gEGLImpl;
838 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800839 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800840 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800841 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
842 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700843#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800844 debugHooks->ext.extensions[slot] =
845 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700846#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800847 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800848 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700849 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800850
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 if (found) {
luliuhui69d10072012-08-30 11:15:36 +0800852#if USE_FAST_TLS_KEY
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 addr = gExtensionForwarders[slot];
luliuhui69d10072012-08-30 11:15:36 +0800854#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 sGLExtentionMap.add(name, addr);
856 sGLExtentionSlot++;
857 }
858 }
859
860 pthread_mutex_unlock(&sExtensionMapMutex);
861 return addr;
862}
863
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700864class FrameCompletionThread : public Thread {
865public:
866
867 static void queueSync(EGLSyncKHR sync) {
868 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
869 static bool running = false;
870 if (!running) {
871 thread->run("GPUFrameCompletion");
872 running = true;
873 }
874 {
875 Mutex::Autolock lock(thread->mMutex);
876 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
877 thread->mFramesQueued).string());
878 thread->mQueue.push_back(sync);
879 thread->mCondition.signal();
880 thread->mFramesQueued++;
881 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
882 }
883 }
884
885private:
886 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
887
888 virtual bool threadLoop() {
889 EGLSyncKHR sync;
890 uint32_t frameNum;
891 {
892 Mutex::Autolock lock(mMutex);
893 while (mQueue.isEmpty()) {
894 mCondition.wait(mMutex);
895 }
896 sync = mQueue[0];
897 frameNum = mFramesCompleted;
898 }
899 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
900 {
901 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
902 frameNum).string());
903 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
904 if (result == EGL_FALSE) {
905 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
906 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
907 ALOGE("FrameCompletion: timeout waiting for fence");
908 }
909 eglDestroySyncKHR(dpy, sync);
910 }
911 {
912 Mutex::Autolock lock(mMutex);
913 mQueue.removeAt(0);
914 mFramesCompleted++;
915 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
916 }
917 return true;
918 }
919
920 uint32_t mFramesQueued;
921 uint32_t mFramesCompleted;
922 Vector<EGLSyncKHR> mQueue;
923 Condition mCondition;
924 Mutex mMutex;
925};
926
Mathias Agopian518ec112011-05-13 16:21:08 -0700927EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
928{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800929 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700930 clearError();
931
Jesse Hallb29e5e82012-04-04 16:53:42 -0700932 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700933 if (!dp) return EGL_FALSE;
934
Jesse Hallb29e5e82012-04-04 16:53:42 -0700935 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700936 if (!_s.get())
937 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700938
Siva Velusamy0469dd62011-11-30 15:05:37 -0800939#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800940 gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
941 if (getEGLDebugLevel() > 0) {
942 if (trace_hooks == NULL) {
943 if (GLTrace_start() < 0) {
944 ALOGE("Disabling Tracer for OpenGL ES");
945 setEGLDebugLevel(0);
946 } else {
947 // switch over to the trace version of hooks
948 EGLContext ctx = egl_tls_t::getContext();
949 egl_context_t * const c = get_context(ctx);
950 if (c) {
951 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
952 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
953 }
954 }
955 }
956
Siva Velusamy0469dd62011-11-30 15:05:37 -0800957 GLTrace_eglSwapBuffers(dpy, draw);
Siva Velusamya73a9772012-12-18 14:56:55 -0800958 } else if (trace_hooks != NULL) {
959 // tracing is now disabled, so switch back to the non trace version
960 EGLContext ctx = egl_tls_t::getContext();
961 egl_context_t * const c = get_context(ctx);
962 if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
963 GLTrace_stop();
964 }
Siva Velusamy0469dd62011-11-30 15:05:37 -0800965#endif
966
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -0700968
Mathias Agopianed6d08b2013-04-16 16:39:46 -0700969 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
970 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
971 if (sync != EGL_NO_SYNC_KHR) {
972 FrameCompletionThread::queueSync(sync);
973 }
974 }
975
Mathias Agopian7db993a2012-03-25 00:49:46 -0700976 if (CC_UNLIKELY(dp->finishOnSwap)) {
977 uint32_t pixel;
978 egl_context_t * const c = get_context( egl_tls_t::getContext() );
979 if (c) {
980 // glReadPixels() ensures that the frame is complete
981 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
982 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
983 }
984 }
985
Mathias Agopianed6d08b2013-04-16 16:39:46 -0700986 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700987}
988
989EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
990 NativePixmapType target)
991{
992 clearError();
993
Jesse Hallb29e5e82012-04-04 16:53:42 -0700994 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700995 if (!dp) return EGL_FALSE;
996
Jesse Hallb29e5e82012-04-04 16:53:42 -0700997 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700998 if (!_s.get())
999 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001000
Mathias Agopian518ec112011-05-13 16:21:08 -07001001 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001002 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001003}
1004
1005const char* eglQueryString(EGLDisplay dpy, EGLint name)
1006{
1007 clearError();
1008
Jesse Hallb29e5e82012-04-04 16:53:42 -07001009 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 if (!dp) return (const char *) NULL;
1011
1012 switch (name) {
1013 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001014 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001015 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001016 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001018 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001020 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 }
1022 return setError(EGL_BAD_PARAMETER, (const char *)0);
1023}
1024
Mathias Agopianca088332013-03-28 17:44:13 -07001025EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1026{
1027 clearError();
1028
1029 const egl_display_ptr dp = validate_display(dpy);
1030 if (!dp) return (const char *) NULL;
1031
1032 switch (name) {
1033 case EGL_VENDOR:
1034 return dp->disp.queryString.vendor;
1035 case EGL_VERSION:
1036 return dp->disp.queryString.version;
1037 case EGL_EXTENSIONS:
1038 return dp->disp.queryString.extensions;
1039 case EGL_CLIENT_APIS:
1040 return dp->disp.queryString.clientApi;
1041 }
1042 return setError(EGL_BAD_PARAMETER, (const char *)0);
1043}
Mathias Agopian518ec112011-05-13 16:21:08 -07001044
1045// ----------------------------------------------------------------------------
1046// EGL 1.1
1047// ----------------------------------------------------------------------------
1048
1049EGLBoolean eglSurfaceAttrib(
1050 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1051{
1052 clearError();
1053
Jesse Hallb29e5e82012-04-04 16:53:42 -07001054 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001055 if (!dp) return EGL_FALSE;
1056
Jesse Hallb29e5e82012-04-04 16:53:42 -07001057 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001058 if (!_s.get())
1059 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001060
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 egl_surface_t const * const s = get_surface(surface);
1062 if (s->cnx->egl.eglSurfaceAttrib) {
1063 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001064 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001065 }
1066 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1067}
1068
1069EGLBoolean eglBindTexImage(
1070 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1071{
1072 clearError();
1073
Jesse Hallb29e5e82012-04-04 16:53:42 -07001074 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001075 if (!dp) return EGL_FALSE;
1076
Jesse Hallb29e5e82012-04-04 16:53:42 -07001077 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001078 if (!_s.get())
1079 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001080
Mathias Agopian518ec112011-05-13 16:21:08 -07001081 egl_surface_t const * const s = get_surface(surface);
1082 if (s->cnx->egl.eglBindTexImage) {
1083 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001084 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 }
1086 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1087}
1088
1089EGLBoolean eglReleaseTexImage(
1090 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1091{
1092 clearError();
1093
Jesse Hallb29e5e82012-04-04 16:53:42 -07001094 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 if (!dp) return EGL_FALSE;
1096
Jesse Hallb29e5e82012-04-04 16:53:42 -07001097 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001098 if (!_s.get())
1099 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001100
Mathias Agopian518ec112011-05-13 16:21:08 -07001101 egl_surface_t const * const s = get_surface(surface);
1102 if (s->cnx->egl.eglReleaseTexImage) {
1103 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001104 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001105 }
1106 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1107}
1108
1109EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1110{
1111 clearError();
1112
Jesse Hallb29e5e82012-04-04 16:53:42 -07001113 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001114 if (!dp) return EGL_FALSE;
1115
1116 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001117 egl_connection_t* const cnx = &gEGLImpl;
1118 if (cnx->dso && cnx->egl.eglSwapInterval) {
1119 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001120 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001121
Mathias Agopian518ec112011-05-13 16:21:08 -07001122 return res;
1123}
1124
1125
1126// ----------------------------------------------------------------------------
1127// EGL 1.2
1128// ----------------------------------------------------------------------------
1129
1130EGLBoolean eglWaitClient(void)
1131{
1132 clearError();
1133
Mathias Agopianada798b2012-02-13 17:09:30 -08001134 egl_connection_t* const cnx = &gEGLImpl;
1135 if (!cnx->dso)
1136 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1137
1138 EGLBoolean res;
1139 if (cnx->egl.eglWaitClient) {
1140 res = cnx->egl.eglWaitClient();
1141 } else {
1142 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001143 }
1144 return res;
1145}
1146
1147EGLBoolean eglBindAPI(EGLenum api)
1148{
1149 clearError();
1150
1151 if (egl_init_drivers() == EGL_FALSE) {
1152 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1153 }
1154
1155 // bind this API on all EGLs
1156 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001157 egl_connection_t* const cnx = &gEGLImpl;
1158 if (cnx->dso && cnx->egl.eglBindAPI) {
1159 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001160 }
1161 return res;
1162}
1163
1164EGLenum eglQueryAPI(void)
1165{
1166 clearError();
1167
1168 if (egl_init_drivers() == EGL_FALSE) {
1169 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1170 }
1171
Mathias Agopianada798b2012-02-13 17:09:30 -08001172 egl_connection_t* const cnx = &gEGLImpl;
1173 if (cnx->dso && cnx->egl.eglQueryAPI) {
1174 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001175 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001176
Mathias Agopian518ec112011-05-13 16:21:08 -07001177 // or, it can only be OpenGL ES
1178 return EGL_OPENGL_ES_API;
1179}
1180
1181EGLBoolean eglReleaseThread(void)
1182{
1183 clearError();
1184
Mathias Agopian4e620dd2013-05-30 16:07:36 -07001185#if EGL_TRACE
1186 if (getEGLDebugLevel() > 0)
1187 GLTrace_eglReleaseThread();
1188#endif
1189
Mathias Agopian518ec112011-05-13 16:21:08 -07001190 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001191 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001192
Mathias Agopianada798b2012-02-13 17:09:30 -08001193 egl_connection_t* const cnx = &gEGLImpl;
1194 if (cnx->dso && cnx->egl.eglReleaseThread) {
1195 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001196 }
1197 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 return EGL_TRUE;
1199}
1200
1201EGLSurface eglCreatePbufferFromClientBuffer(
1202 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1203 EGLConfig config, const EGLint *attrib_list)
1204{
1205 clearError();
1206
Jesse Hallb29e5e82012-04-04 16:53:42 -07001207 egl_connection_t* cnx = NULL;
1208 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1209 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001210 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1211 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001212 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001213 }
1214 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1215}
1216
1217// ----------------------------------------------------------------------------
1218// EGL_EGLEXT_VERSION 3
1219// ----------------------------------------------------------------------------
1220
1221EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1222 const EGLint *attrib_list)
1223{
1224 clearError();
1225
Jesse Hallb29e5e82012-04-04 16:53:42 -07001226 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001227 if (!dp) return EGL_FALSE;
1228
Jesse Hallb29e5e82012-04-04 16:53:42 -07001229 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001230 if (!_s.get())
1231 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001232
1233 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001234 if (s->cnx->egl.eglLockSurfaceKHR) {
1235 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001236 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001237 }
1238 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1239}
1240
1241EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1242{
1243 clearError();
1244
Jesse Hallb29e5e82012-04-04 16:53:42 -07001245 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 if (!dp) return EGL_FALSE;
1247
Jesse Hallb29e5e82012-04-04 16:53:42 -07001248 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001249 if (!_s.get())
1250 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001251
1252 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001254 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 }
1256 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1257}
1258
1259EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1260 EGLClientBuffer buffer, const EGLint *attrib_list)
1261{
1262 clearError();
1263
Jesse Hallb29e5e82012-04-04 16:53:42 -07001264 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001265 if (!dp) return EGL_NO_IMAGE_KHR;
1266
Jesse Hallb29e5e82012-04-04 16:53:42 -07001267 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001268 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001269
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001270 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1271 egl_connection_t* const cnx = &gEGLImpl;
1272 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1273 result = cnx->egl.eglCreateImageKHR(
1274 dp->disp.dpy,
1275 c ? c->context : EGL_NO_CONTEXT,
1276 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001277 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001278 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001279}
1280
1281EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1282{
1283 clearError();
1284
Jesse Hallb29e5e82012-04-04 16:53:42 -07001285 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001286 if (!dp) return EGL_FALSE;
1287
Steven Holte646a5c52012-06-04 20:02:11 -07001288 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001289 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001290 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001291 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001292 }
Steven Holte646a5c52012-06-04 20:02:11 -07001293 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001294}
1295
1296// ----------------------------------------------------------------------------
1297// EGL_EGLEXT_VERSION 5
1298// ----------------------------------------------------------------------------
1299
1300
1301EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1302{
1303 clearError();
1304
Jesse Hallb29e5e82012-04-04 16:53:42 -07001305 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001306 if (!dp) return EGL_NO_SYNC_KHR;
1307
Mathias Agopian518ec112011-05-13 16:21:08 -07001308 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001309 egl_connection_t* const cnx = &gEGLImpl;
1310 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1311 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001312 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001313 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001314}
1315
1316EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1317{
1318 clearError();
1319
Jesse Hallb29e5e82012-04-04 16:53:42 -07001320 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 if (!dp) return EGL_FALSE;
1322
Mathias Agopian518ec112011-05-13 16:21:08 -07001323 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001324 egl_connection_t* const cnx = &gEGLImpl;
1325 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1326 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 }
1328 return result;
1329}
1330
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001331EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1332 clearError();
1333
1334 const egl_display_ptr dp = validate_display(dpy);
1335 if (!dp) return EGL_FALSE;
1336
1337 EGLBoolean result = EGL_FALSE;
1338 egl_connection_t* const cnx = &gEGLImpl;
1339 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1340 result = cnx->egl.eglSignalSyncKHR(
1341 dp->disp.dpy, sync, mode);
1342 }
1343 return result;
1344}
1345
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001346EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1347 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001348{
1349 clearError();
1350
Jesse Hallb29e5e82012-04-04 16:53:42 -07001351 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001352 if (!dp) return EGL_FALSE;
1353
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001354 EGLBoolean result = EGL_FALSE;
1355 egl_connection_t* const cnx = &gEGLImpl;
1356 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1357 result = cnx->egl.eglClientWaitSyncKHR(
1358 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001359 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001360 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001361}
1362
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001363EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1364 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001365{
1366 clearError();
1367
Jesse Hallb29e5e82012-04-04 16:53:42 -07001368 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001369 if (!dp) return EGL_FALSE;
1370
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001371 EGLBoolean result = EGL_FALSE;
1372 egl_connection_t* const cnx = &gEGLImpl;
1373 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1374 result = cnx->egl.eglGetSyncAttribKHR(
1375 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001377 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001378}
1379
1380// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001381// EGL_EGLEXT_VERSION 15
1382// ----------------------------------------------------------------------------
1383
1384EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1385 clearError();
1386 const egl_display_ptr dp = validate_display(dpy);
1387 if (!dp) return EGL_FALSE;
1388 EGLint result = EGL_FALSE;
1389 egl_connection_t* const cnx = &gEGLImpl;
1390 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1391 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1392 }
1393 return result;
1394}
1395
1396// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001397// ANDROID extensions
1398// ----------------------------------------------------------------------------
1399
Jamie Gennis331841b2012-09-06 14:52:00 -07001400EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1401{
1402 clearError();
1403
1404 const egl_display_ptr dp = validate_display(dpy);
1405 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1406
1407 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1408 egl_connection_t* const cnx = &gEGLImpl;
1409 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1410 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1411 }
1412 return result;
1413}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001414
Andy McFadden72841452013-03-01 16:25:32 -08001415EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1416 EGLnsecsANDROID time)
1417{
1418 clearError();
1419
1420 const egl_display_ptr dp = validate_display(dpy);
1421 if (!dp) {
1422 return EGL_FALSE;
1423 }
1424
1425 SurfaceRef _s(dp.get(), surface);
1426 if (!_s.get()) {
1427 setError(EGL_BAD_SURFACE, EGL_FALSE);
1428 return EGL_FALSE;
1429 }
1430
1431 egl_surface_t const * const s = get_surface(surface);
1432 native_window_set_buffers_timestamp(s->win.get(), time);
1433
1434 return EGL_TRUE;
1435}
1436
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001437// ----------------------------------------------------------------------------
1438// NVIDIA extensions
1439// ----------------------------------------------------------------------------
1440EGLuint64NV eglGetSystemTimeFrequencyNV()
1441{
1442 clearError();
1443
1444 if (egl_init_drivers() == EGL_FALSE) {
1445 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1446 }
1447
1448 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001449 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001450
Mathias Agopianada798b2012-02-13 17:09:30 -08001451 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1452 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001453 }
1454
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001455 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001456}
1457
1458EGLuint64NV eglGetSystemTimeNV()
1459{
1460 clearError();
1461
1462 if (egl_init_drivers() == EGL_FALSE) {
1463 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1464 }
1465
1466 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001467 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001468
Mathias Agopianada798b2012-02-13 17:09:30 -08001469 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1470 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001471 }
1472
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001473 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001474}