egl: add EGL_ANDROID_native_fence_sync
[mesa.git] / src / egl / main / eglapi.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30
31 /**
32 * Public EGL API entrypoints
33 *
34 * Generally, we use the EGLDisplay parameter as a key to lookup the
35 * appropriate device driver handle, then jump though the driver's
36 * dispatch table to handle the function.
37 *
38 * That allows us the option of supporting multiple, simultaneous,
39 * heterogeneous hardware devices in the future.
40 *
41 * The EGLDisplay, EGLConfig, EGLContext and EGLSurface types are
42 * opaque handles. Internal objects are linked to a display to
43 * create the handles.
44 *
45 * For each public API entry point, the opaque handles are looked up
46 * before being dispatched to the drivers. When it fails to look up
47 * a handle, one of
48 *
49 * EGL_BAD_DISPLAY
50 * EGL_BAD_CONFIG
51 * EGL_BAD_CONTEXT
52 * EGL_BAD_SURFACE
53 * EGL_BAD_SCREEN_MESA
54 * EGL_BAD_MODE_MESA
55 *
56 * is generated and the driver function is not called. An
57 * uninitialized EGLDisplay has no driver associated with it. When
58 * such display is detected,
59 *
60 * EGL_NOT_INITIALIZED
61 *
62 * is generated.
63 *
64 * Some of the entry points use current display, context, or surface
65 * implicitly. For such entry points, the implicit objects are also
66 * checked before calling the driver function. Other than the
67 * errors listed above,
68 *
69 * EGL_BAD_CURRENT_SURFACE
70 *
71 * may also be generated.
72 *
73 * Notes on naming conventions:
74 *
75 * eglFooBar - public EGL function
76 * EGL_FOO_BAR - public EGL token
77 * EGLDatatype - public EGL datatype
78 *
79 * _eglFooBar - private EGL function
80 * _EGLDatatype - private EGL datatype, typedef'd struct
81 * _egl_struct - private EGL struct, non-typedef'd
82 *
83 */
84
85
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include "c99_compat.h"
90 #include "c11/threads.h"
91 #include "GL/mesa_glinterop.h"
92 #include "eglcompiler.h"
93
94 #include "eglglobals.h"
95 #include "eglcontext.h"
96 #include "egldisplay.h"
97 #include "egltypedefs.h"
98 #include "eglcurrent.h"
99 #include "egldriver.h"
100 #include "eglsurface.h"
101 #include "eglconfig.h"
102 #include "eglimage.h"
103 #include "eglsync.h"
104
105
106 /**
107 * Macros to help return an API entrypoint.
108 *
109 * These macros will unlock the display and record the error code.
110 */
111 #define RETURN_EGL_ERROR(disp, err, ret) \
112 do { \
113 if (disp) \
114 _eglUnlockDisplay(disp); \
115 /* EGL error codes are non-zero */ \
116 if (err) \
117 _eglError(err, __func__); \
118 return ret; \
119 } while (0)
120
121 #define RETURN_EGL_SUCCESS(disp, ret) \
122 RETURN_EGL_ERROR(disp, EGL_SUCCESS, ret)
123
124 /* record EGL_SUCCESS only when ret evaluates to true */
125 #define RETURN_EGL_EVAL(disp, ret) \
126 RETURN_EGL_ERROR(disp, (ret) ? EGL_SUCCESS : 0, ret)
127
128
129 /*
130 * A bunch of macros and checks to simplify error checking.
131 */
132
133 #define _EGL_CHECK_DISPLAY(disp, ret, drv) \
134 do { \
135 drv = _eglCheckDisplay(disp, __func__); \
136 if (!drv) \
137 RETURN_EGL_ERROR(disp, 0, ret); \
138 } while (0)
139
140 #define _EGL_CHECK_OBJECT(disp, type, obj, ret, drv) \
141 do { \
142 drv = _eglCheck ## type(disp, obj, __func__); \
143 if (!drv) \
144 RETURN_EGL_ERROR(disp, 0, ret); \
145 } while (0)
146
147 #define _EGL_CHECK_SURFACE(disp, surf, ret, drv) \
148 _EGL_CHECK_OBJECT(disp, Surface, surf, ret, drv)
149
150 #define _EGL_CHECK_CONTEXT(disp, context, ret, drv) \
151 _EGL_CHECK_OBJECT(disp, Context, context, ret, drv)
152
153 #define _EGL_CHECK_CONFIG(disp, conf, ret, drv) \
154 _EGL_CHECK_OBJECT(disp, Config, conf, ret, drv)
155
156 #define _EGL_CHECK_SYNC(disp, s, ret, drv) \
157 _EGL_CHECK_OBJECT(disp, Sync, s, ret, drv)
158
159
160 static inline _EGLDriver *
161 _eglCheckDisplay(_EGLDisplay *disp, const char *msg)
162 {
163 if (!disp) {
164 _eglError(EGL_BAD_DISPLAY, msg);
165 return NULL;
166 }
167 if (!disp->Initialized) {
168 _eglError(EGL_NOT_INITIALIZED, msg);
169 return NULL;
170 }
171 return disp->Driver;
172 }
173
174
175 static inline _EGLDriver *
176 _eglCheckSurface(_EGLDisplay *disp, _EGLSurface *surf, const char *msg)
177 {
178 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
179 if (!drv)
180 return NULL;
181 if (!surf) {
182 _eglError(EGL_BAD_SURFACE, msg);
183 return NULL;
184 }
185 return drv;
186 }
187
188
189 static inline _EGLDriver *
190 _eglCheckContext(_EGLDisplay *disp, _EGLContext *context, const char *msg)
191 {
192 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
193 if (!drv)
194 return NULL;
195 if (!context) {
196 _eglError(EGL_BAD_CONTEXT, msg);
197 return NULL;
198 }
199 return drv;
200 }
201
202
203 static inline _EGLDriver *
204 _eglCheckConfig(_EGLDisplay *disp, _EGLConfig *conf, const char *msg)
205 {
206 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
207 if (!drv)
208 return NULL;
209 if (!conf) {
210 _eglError(EGL_BAD_CONFIG, msg);
211 return NULL;
212 }
213 return drv;
214 }
215
216
217 static inline _EGLDriver *
218 _eglCheckSync(_EGLDisplay *disp, _EGLSync *s, const char *msg)
219 {
220 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
221 if (!drv)
222 return NULL;
223 if (!s) {
224 _eglError(EGL_BAD_PARAMETER, msg);
225 return NULL;
226 }
227 return drv;
228 }
229
230
231 /**
232 * Lookup and lock a display.
233 */
234 static inline _EGLDisplay *
235 _eglLockDisplay(EGLDisplay display)
236 {
237 _EGLDisplay *dpy = _eglLookupDisplay(display);
238 if (dpy)
239 mtx_lock(&dpy->Mutex);
240 return dpy;
241 }
242
243
244 /**
245 * Unlock a display.
246 */
247 static inline void
248 _eglUnlockDisplay(_EGLDisplay *dpy)
249 {
250 mtx_unlock(&dpy->Mutex);
251 }
252
253 static EGLBoolean
254 _eglSetFuncName(const char *funcName, _EGLDisplay *disp, EGLenum objectType, _EGLResource *object)
255 {
256 _EGLThreadInfo *thr = _eglGetCurrentThread();
257 if (!_eglIsCurrentThreadDummy()) {
258 thr->CurrentFuncName = funcName;
259 thr->CurrentObjectLabel = NULL;
260
261 if (objectType == EGL_OBJECT_THREAD_KHR)
262 thr->CurrentObjectLabel = thr->Label;
263 else if (objectType == EGL_OBJECT_DISPLAY_KHR && disp)
264 thr->CurrentObjectLabel = disp->Label;
265 else if (object)
266 thr->CurrentObjectLabel = object->Label;
267
268 return EGL_TRUE;
269 }
270
271 _eglDebugReportFull(EGL_BAD_ALLOC, funcName, funcName,
272 EGL_DEBUG_MSG_CRITICAL_KHR, NULL, NULL);
273 return EGL_FALSE;
274 }
275
276 #define _EGL_FUNC_START(disp, objectType, object, ret) \
277 do { \
278 if (!_eglSetFuncName(__func__, disp, objectType, (_EGLResource *) object)) { \
279 if (disp) \
280 _eglUnlockDisplay(disp); \
281 return ret; \
282 } \
283 } while(0)
284
285 /**
286 * Convert an attribute list from EGLint[] to EGLAttrib[].
287 *
288 * Return an EGL error code. The output parameter out_attrib_list is modified
289 * only on success.
290 */
291 EGLint
292 _eglConvertIntsToAttribs(const EGLint *int_list, EGLAttrib **out_attrib_list)
293 {
294 size_t len = 0;
295 EGLAttrib *attrib_list;
296
297 if (int_list) {
298 while (int_list[2*len] != EGL_NONE)
299 ++len;
300 }
301
302 if (len == 0) {
303 *out_attrib_list = NULL;
304 return EGL_SUCCESS;
305 }
306
307 if (2*len + 1 > SIZE_MAX / sizeof(EGLAttrib))
308 return EGL_BAD_ALLOC;
309
310 attrib_list = malloc((2*len + 1) * sizeof(EGLAttrib));
311 if (!attrib_list)
312 return EGL_BAD_ALLOC;
313
314 for (size_t i = 0; i < len; ++i) {
315 attrib_list[2*i + 0] = int_list[2*i + 0];
316 attrib_list[2*i + 1] = int_list[2*i + 1];
317 }
318
319 attrib_list[2*len] = EGL_NONE;
320
321 *out_attrib_list = attrib_list;
322 return EGL_SUCCESS;
323 }
324
325
326 static EGLint *
327 _eglConvertAttribsToInt(const EGLAttrib *attr_list)
328 {
329 EGLint *int_attribs = NULL;
330
331 /* Convert attributes from EGLAttrib[] to EGLint[] */
332 if (attr_list) {
333 int i, size = 0;
334
335 while (attr_list[size] != EGL_NONE)
336 size += 2;
337
338 size += 1; /* add space for EGL_NONE */
339
340 int_attribs = calloc(size, sizeof(int_attribs[0]));
341 if (!int_attribs)
342 return NULL;
343
344 for (i = 0; i < size; i++)
345 int_attribs[i] = attr_list[i];
346 }
347 return int_attribs;
348 }
349
350
351 /**
352 * This is typically the first EGL function that an application calls.
353 * It associates a private _EGLDisplay object to the native display.
354 */
355 EGLDisplay EGLAPIENTRY
356 eglGetDisplay(EGLNativeDisplayType nativeDisplay)
357 {
358 _EGLPlatformType plat;
359 _EGLDisplay *dpy;
360 void *native_display_ptr;
361
362 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
363
364 STATIC_ASSERT(sizeof(void*) == sizeof(nativeDisplay));
365 native_display_ptr = (void*) nativeDisplay;
366
367 plat = _eglGetNativePlatform(native_display_ptr);
368 dpy = _eglFindDisplay(plat, native_display_ptr);
369 return _eglGetDisplayHandle(dpy);
370 }
371
372 static EGLDisplay
373 _eglGetPlatformDisplayCommon(EGLenum platform, void *native_display,
374 const EGLint *attrib_list)
375 {
376 _EGLDisplay *dpy;
377
378 switch (platform) {
379 #ifdef HAVE_X11_PLATFORM
380 case EGL_PLATFORM_X11_EXT:
381 dpy = _eglGetX11Display((Display*) native_display, attrib_list);
382 break;
383 #endif
384 #ifdef HAVE_DRM_PLATFORM
385 case EGL_PLATFORM_GBM_MESA:
386 dpy = _eglGetGbmDisplay((struct gbm_device*) native_display,
387 attrib_list);
388 break;
389 #endif
390 #ifdef HAVE_WAYLAND_PLATFORM
391 case EGL_PLATFORM_WAYLAND_EXT:
392 dpy = _eglGetWaylandDisplay((struct wl_display*) native_display,
393 attrib_list);
394 break;
395 #endif
396 #ifdef HAVE_SURFACELESS_PLATFORM
397 case EGL_PLATFORM_SURFACELESS_MESA:
398 dpy = _eglGetSurfacelessDisplay(native_display, attrib_list);
399 break;
400 #endif
401 default:
402 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, NULL);
403 }
404
405 return _eglGetDisplayHandle(dpy);
406 }
407
408 static EGLDisplay EGLAPIENTRY
409 eglGetPlatformDisplayEXT(EGLenum platform, void *native_display,
410 const EGLint *attrib_list)
411 {
412 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
413 return _eglGetPlatformDisplayCommon(platform, native_display, attrib_list);
414 }
415
416 EGLDisplay EGLAPIENTRY
417 eglGetPlatformDisplay(EGLenum platform, void *native_display,
418 const EGLAttrib *attrib_list)
419 {
420 EGLDisplay display;
421 EGLint *int_attribs;
422
423 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
424
425 int_attribs = _eglConvertAttribsToInt(attrib_list);
426 if (attrib_list && !int_attribs)
427 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, NULL);
428
429 display = _eglGetPlatformDisplayCommon(platform, native_display, int_attribs);
430 free(int_attribs);
431 return display;
432 }
433
434 /**
435 * Copy the extension into the string and update the string pointer.
436 */
437 static EGLint
438 _eglAppendExtension(char **str, const char *ext)
439 {
440 char *s = *str;
441 size_t len = strlen(ext);
442
443 if (s) {
444 memcpy(s, ext, len);
445 s[len++] = ' ';
446 s[len] = '\0';
447
448 *str += len;
449 }
450 else {
451 len++;
452 }
453
454 return (EGLint) len;
455 }
456
457 /**
458 * Examine the individual extension enable/disable flags and recompute
459 * the driver's Extensions string.
460 */
461 static void
462 _eglCreateExtensionsString(_EGLDisplay *dpy)
463 {
464 #define _EGL_CHECK_EXTENSION(ext) \
465 do { \
466 if (dpy->Extensions.ext) { \
467 _eglAppendExtension(&exts, "EGL_" #ext); \
468 assert(exts <= dpy->ExtensionsString + _EGL_MAX_EXTENSIONS_LEN); \
469 } \
470 } while (0)
471
472 char *exts = dpy->ExtensionsString;
473
474 /* Please keep these sorted alphabetically. */
475 _EGL_CHECK_EXTENSION(ANDROID_framebuffer_target);
476 _EGL_CHECK_EXTENSION(ANDROID_image_native_buffer);
477 _EGL_CHECK_EXTENSION(ANDROID_native_fence_sync);
478 _EGL_CHECK_EXTENSION(ANDROID_recordable);
479
480 _EGL_CHECK_EXTENSION(CHROMIUM_sync_control);
481
482 _EGL_CHECK_EXTENSION(EXT_buffer_age);
483 _EGL_CHECK_EXTENSION(EXT_create_context_robustness);
484 _EGL_CHECK_EXTENSION(EXT_image_dma_buf_import);
485 _EGL_CHECK_EXTENSION(EXT_swap_buffers_with_damage);
486
487 _EGL_CHECK_EXTENSION(KHR_cl_event2);
488 _EGL_CHECK_EXTENSION(KHR_create_context);
489 _EGL_CHECK_EXTENSION(KHR_fence_sync);
490 _EGL_CHECK_EXTENSION(KHR_get_all_proc_addresses);
491 _EGL_CHECK_EXTENSION(KHR_gl_colorspace);
492 _EGL_CHECK_EXTENSION(KHR_gl_renderbuffer_image);
493 _EGL_CHECK_EXTENSION(KHR_gl_texture_2D_image);
494 _EGL_CHECK_EXTENSION(KHR_gl_texture_3D_image);
495 _EGL_CHECK_EXTENSION(KHR_gl_texture_cubemap_image);
496 if (dpy->Extensions.KHR_image_base && dpy->Extensions.KHR_image_pixmap)
497 _eglAppendExtension(&exts, "EGL_KHR_image");
498 _EGL_CHECK_EXTENSION(KHR_image_base);
499 _EGL_CHECK_EXTENSION(KHR_image_pixmap);
500 _EGL_CHECK_EXTENSION(KHR_no_config_context);
501 _EGL_CHECK_EXTENSION(KHR_reusable_sync);
502 _EGL_CHECK_EXTENSION(KHR_surfaceless_context);
503 if (dpy->Extensions.EXT_swap_buffers_with_damage)
504 _eglAppendExtension(&exts, "EGL_KHR_swap_buffers_with_damage");
505 _EGL_CHECK_EXTENSION(KHR_wait_sync);
506
507 if (dpy->Extensions.KHR_no_config_context)
508 _eglAppendExtension(&exts, "EGL_MESA_configless_context");
509 _EGL_CHECK_EXTENSION(MESA_drm_image);
510 _EGL_CHECK_EXTENSION(MESA_image_dma_buf_export);
511
512 _EGL_CHECK_EXTENSION(NOK_swap_region);
513 _EGL_CHECK_EXTENSION(NOK_texture_from_pixmap);
514
515 _EGL_CHECK_EXTENSION(NV_post_sub_buffer);
516
517 _EGL_CHECK_EXTENSION(WL_bind_wayland_display);
518 _EGL_CHECK_EXTENSION(WL_create_wayland_buffer_from_image);
519
520 #undef _EGL_CHECK_EXTENSION
521 }
522
523 static void
524 _eglCreateAPIsString(_EGLDisplay *dpy)
525 {
526 if (dpy->ClientAPIs & EGL_OPENGL_BIT)
527 strcat(dpy->ClientAPIsString, "OpenGL ");
528
529 if (dpy->ClientAPIs & EGL_OPENGL_ES_BIT ||
530 dpy->ClientAPIs & EGL_OPENGL_ES2_BIT ||
531 dpy->ClientAPIs & EGL_OPENGL_ES3_BIT_KHR) {
532 strcat(dpy->ClientAPIsString, "OpenGL_ES ");
533 }
534
535 if (dpy->ClientAPIs & EGL_OPENVG_BIT)
536 strcat(dpy->ClientAPIsString, "OpenVG ");
537
538 assert(strlen(dpy->ClientAPIsString) < sizeof(dpy->ClientAPIsString));
539 }
540
541 static void
542 _eglComputeVersion(_EGLDisplay *disp)
543 {
544 disp->Version = 14;
545
546 if (disp->Extensions.KHR_fence_sync &&
547 disp->Extensions.KHR_cl_event2 &&
548 disp->Extensions.KHR_wait_sync &&
549 disp->Extensions.KHR_image_base &&
550 disp->Extensions.KHR_gl_texture_2D_image &&
551 disp->Extensions.KHR_gl_texture_3D_image &&
552 disp->Extensions.KHR_gl_texture_cubemap_image &&
553 disp->Extensions.KHR_gl_renderbuffer_image &&
554 disp->Extensions.KHR_create_context &&
555 disp->Extensions.EXT_create_context_robustness &&
556 disp->Extensions.KHR_get_all_proc_addresses &&
557 disp->Extensions.KHR_gl_colorspace &&
558 disp->Extensions.KHR_surfaceless_context)
559 disp->Version = 15;
560 }
561
562 /**
563 * This is typically the second EGL function that an application calls.
564 * Here we load/initialize the actual hardware driver.
565 */
566 EGLBoolean EGLAPIENTRY
567 eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
568 {
569 _EGLDisplay *disp = _eglLockDisplay(dpy);
570
571 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
572
573 if (!disp)
574 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
575
576 if (!disp->Initialized) {
577 if (!_eglMatchDriver(disp, EGL_FALSE))
578 RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
579
580 /* limit to APIs supported by core */
581 disp->ClientAPIs &= _EGL_API_ALL_BITS;
582
583 /* EGL_KHR_get_all_proc_addresses is a corner-case extension. The spec
584 * classifies it as an EGL display extension, though conceptually it's an
585 * EGL client extension.
586 *
587 * From the EGL_KHR_get_all_proc_addresses spec:
588 *
589 * The EGL implementation must expose the name
590 * EGL_KHR_client_get_all_proc_addresses if and only if it exposes
591 * EGL_KHR_get_all_proc_addresses and supports
592 * EGL_EXT_client_extensions.
593 *
594 * Mesa unconditionally exposes both client extensions mentioned above,
595 * so the spec requires that each EGLDisplay unconditionally expose
596 * EGL_KHR_get_all_proc_addresses also.
597 */
598 disp->Extensions.KHR_get_all_proc_addresses = EGL_TRUE;
599
600 _eglComputeVersion(disp);
601 _eglCreateExtensionsString(disp);
602 _eglCreateAPIsString(disp);
603 snprintf(disp->VersionString, sizeof(disp->VersionString),
604 "%d.%d (%s)", disp->Version / 10, disp->Version % 10,
605 disp->Driver->Name);
606 }
607
608 /* Update applications version of major and minor if not NULL */
609 if ((major != NULL) && (minor != NULL)) {
610 *major = disp->Version / 10;
611 *minor = disp->Version % 10;
612 }
613
614 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
615 }
616
617
618 EGLBoolean EGLAPIENTRY
619 eglTerminate(EGLDisplay dpy)
620 {
621 _EGLDisplay *disp = _eglLockDisplay(dpy);
622
623 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
624
625 if (!disp)
626 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
627
628 if (disp->Initialized) {
629 _EGLDriver *drv = disp->Driver;
630
631 drv->API.Terminate(drv, disp);
632 /* do not reset disp->Driver */
633 disp->ClientAPIsString[0] = 0;
634 disp->Initialized = EGL_FALSE;
635 }
636
637 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
638 }
639
640
641 const char * EGLAPIENTRY
642 eglQueryString(EGLDisplay dpy, EGLint name)
643 {
644 _EGLDisplay *disp;
645 _EGLDriver *drv;
646
647 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS) {
648 RETURN_EGL_SUCCESS(NULL, _eglGlobal.ClientExtensionString);
649 }
650
651 disp = _eglLockDisplay(dpy);
652 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, NULL);
653 _EGL_CHECK_DISPLAY(disp, NULL, drv);
654
655 switch (name) {
656 case EGL_VENDOR:
657 RETURN_EGL_SUCCESS(disp, _EGL_VENDOR_STRING);
658 case EGL_VERSION:
659 RETURN_EGL_SUCCESS(disp, disp->VersionString);
660 case EGL_EXTENSIONS:
661 RETURN_EGL_SUCCESS(disp, disp->ExtensionsString);
662 case EGL_CLIENT_APIS:
663 RETURN_EGL_SUCCESS(disp, disp->ClientAPIsString);
664 default:
665 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
666 }
667 }
668
669
670 EGLBoolean EGLAPIENTRY
671 eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
672 EGLint config_size, EGLint *num_config)
673 {
674 _EGLDisplay *disp = _eglLockDisplay(dpy);
675 _EGLDriver *drv;
676 EGLBoolean ret;
677
678 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
679
680 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
681 ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
682
683 RETURN_EGL_EVAL(disp, ret);
684 }
685
686
687 EGLBoolean EGLAPIENTRY
688 eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
689 EGLint config_size, EGLint *num_config)
690 {
691 _EGLDisplay *disp = _eglLockDisplay(dpy);
692 _EGLDriver *drv;
693 EGLBoolean ret;
694
695 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
696
697 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
698 ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
699 config_size, num_config);
700
701 RETURN_EGL_EVAL(disp, ret);
702 }
703
704
705 EGLBoolean EGLAPIENTRY
706 eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
707 EGLint attribute, EGLint *value)
708 {
709 _EGLDisplay *disp = _eglLockDisplay(dpy);
710 _EGLConfig *conf = _eglLookupConfig(config, disp);
711 _EGLDriver *drv;
712 EGLBoolean ret;
713
714 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
715
716 _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
717 ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
718
719 RETURN_EGL_EVAL(disp, ret);
720 }
721
722
723 EGLContext EGLAPIENTRY
724 eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
725 const EGLint *attrib_list)
726 {
727 _EGLDisplay *disp = _eglLockDisplay(dpy);
728 _EGLConfig *conf = _eglLookupConfig(config, disp);
729 _EGLContext *share = _eglLookupContext(share_list, disp);
730 _EGLDriver *drv;
731 _EGLContext *context;
732 EGLContext ret;
733
734 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_CONTEXT);
735
736 _EGL_CHECK_DISPLAY(disp, EGL_NO_CONTEXT, drv);
737
738 if (config != EGL_NO_CONFIG_KHR)
739 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_CONTEXT, drv);
740 else if (!disp->Extensions.KHR_no_config_context)
741 RETURN_EGL_ERROR(disp, EGL_BAD_CONFIG, EGL_NO_CONTEXT);
742
743 if (!share && share_list != EGL_NO_CONTEXT)
744 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
745
746 context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
747 ret = (context) ? _eglLinkContext(context) : EGL_NO_CONTEXT;
748
749 RETURN_EGL_EVAL(disp, ret);
750 }
751
752
753 EGLBoolean EGLAPIENTRY
754 eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
755 {
756 _EGLDisplay *disp = _eglLockDisplay(dpy);
757 _EGLContext *context = _eglLookupContext(ctx, disp);
758 _EGLDriver *drv;
759 EGLBoolean ret;
760
761 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
762
763 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
764 _eglUnlinkContext(context);
765 ret = drv->API.DestroyContext(drv, disp, context);
766
767 RETURN_EGL_EVAL(disp, ret);
768 }
769
770
771 EGLBoolean EGLAPIENTRY
772 eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
773 EGLContext ctx)
774 {
775 _EGLDisplay *disp = _eglLockDisplay(dpy);
776 _EGLContext *context = _eglLookupContext(ctx, disp);
777 _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
778 _EGLSurface *read_surf = _eglLookupSurface(read, disp);
779 _EGLDriver *drv;
780 EGLBoolean ret;
781
782 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
783
784 if (!disp)
785 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
786 drv = disp->Driver;
787
788 /* display is allowed to be uninitialized under certain condition */
789 if (!disp->Initialized) {
790 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
791 ctx != EGL_NO_CONTEXT)
792 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
793 }
794 if (!drv)
795 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
796
797 if (!context && ctx != EGL_NO_CONTEXT)
798 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
799 if (!draw_surf || !read_surf) {
800 /* From the EGL 1.4 (20130211) spec:
801 *
802 * To release the current context without assigning a new one, set ctx
803 * to EGL_NO_CONTEXT and set draw and read to EGL_NO_SURFACE.
804 */
805 if (!disp->Extensions.KHR_surfaceless_context && ctx != EGL_NO_CONTEXT)
806 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
807
808 if ((!draw_surf && draw != EGL_NO_SURFACE) ||
809 (!read_surf && read != EGL_NO_SURFACE))
810 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
811 if (draw_surf || read_surf)
812 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
813 }
814
815 ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
816
817 RETURN_EGL_EVAL(disp, ret);
818 }
819
820
821 EGLBoolean EGLAPIENTRY
822 eglQueryContext(EGLDisplay dpy, EGLContext ctx,
823 EGLint attribute, EGLint *value)
824 {
825 _EGLDisplay *disp = _eglLockDisplay(dpy);
826 _EGLContext *context = _eglLookupContext(ctx, disp);
827 _EGLDriver *drv;
828 EGLBoolean ret;
829
830 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
831
832 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
833 ret = drv->API.QueryContext(drv, disp, context, attribute, value);
834
835 RETURN_EGL_EVAL(disp, ret);
836 }
837
838
839 static EGLSurface
840 _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
841 void *native_window, const EGLint *attrib_list)
842 {
843 _EGLConfig *conf = _eglLookupConfig(config, disp);
844 _EGLDriver *drv;
845 _EGLSurface *surf;
846 EGLSurface ret;
847
848
849 if (native_window == NULL)
850 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
851
852 #ifdef HAVE_SURFACELESS_PLATFORM
853 if (disp->Platform == _EGL_PLATFORM_SURFACELESS) {
854 /* From the EGL_MESA_platform_surfaceless spec (v1):
855 *
856 * eglCreatePlatformWindowSurface fails when called with a <display>
857 * that belongs to the surfaceless platform. It returns
858 * EGL_NO_SURFACE and generates EGL_BAD_NATIVE_WINDOW. The
859 * justification for this unconditional failure is that the
860 * surfaceless platform has no native windows, and therefore the
861 * <native_window> parameter is always invalid.
862 *
863 * This check must occur before checking the EGLConfig, which emits
864 * EGL_BAD_CONFIG.
865 */
866 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
867 }
868 #endif
869
870 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
871
872 surf = drv->API.CreateWindowSurface(drv, disp, conf, native_window,
873 attrib_list);
874 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
875
876 RETURN_EGL_EVAL(disp, ret);
877 }
878
879
880 EGLSurface EGLAPIENTRY
881 eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
882 EGLNativeWindowType window, const EGLint *attrib_list)
883 {
884 _EGLDisplay *disp = _eglLockDisplay(dpy);
885
886 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
887 STATIC_ASSERT(sizeof(void*) == sizeof(window));
888 return _eglCreateWindowSurfaceCommon(disp, config, (void*) window,
889 attrib_list);
890 }
891
892 static void *
893 fixupNativeWindow(_EGLDisplay *disp, void *native_window)
894 {
895 #ifdef HAVE_X11_PLATFORM
896 if (disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
897 /* The `native_window` parameter for the X11 platform differs between
898 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
899 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
900 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
901 * `Window*`. Convert `Window*` to `Window` because that's what
902 * dri2_x11_create_window_surface() expects.
903 */
904 return (void *)(* (Window*) native_window);
905 }
906 #endif
907 return native_window;
908 }
909
910 static EGLSurface EGLAPIENTRY
911 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
912 void *native_window,
913 const EGLint *attrib_list)
914 {
915 _EGLDisplay *disp = _eglLockDisplay(dpy);
916
917 native_window = fixupNativeWindow(disp, native_window);
918
919 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
920 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
921 attrib_list);
922 }
923
924
925 EGLSurface EGLAPIENTRY
926 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
927 void *native_window,
928 const EGLAttrib *attrib_list)
929 {
930 _EGLDisplay *disp = _eglLockDisplay(dpy);
931 EGLSurface surface;
932 EGLint *int_attribs;
933
934 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
935
936 int_attribs = _eglConvertAttribsToInt(attrib_list);
937 if (attrib_list && !int_attribs)
938 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
939
940 native_window = fixupNativeWindow(disp, native_window);
941 surface = _eglCreateWindowSurfaceCommon(disp, config, native_window,
942 int_attribs);
943 free(int_attribs);
944 return surface;
945 }
946
947 static void *
948 fixupNativePixmap(_EGLDisplay *disp, void *native_pixmap)
949 {
950 #ifdef HAVE_X11_PLATFORM
951 /* The `native_pixmap` parameter for the X11 platform differs between
952 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
953 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
954 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
955 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
956 * dri2_x11_create_pixmap_surface() expects.
957 */
958 if (disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL)
959 return (void *)(* (Pixmap*) native_pixmap);
960 #endif
961 return native_pixmap;
962 }
963
964 static EGLSurface
965 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
966 void *native_pixmap, const EGLint *attrib_list)
967 {
968 _EGLConfig *conf = _eglLookupConfig(config, disp);
969 _EGLDriver *drv;
970 _EGLSurface *surf;
971 EGLSurface ret;
972
973 #if HAVE_SURFACELESS_PLATFORM
974 if (disp->Platform == _EGL_PLATFORM_SURFACELESS) {
975 /* From the EGL_MESA_platform_surfaceless spec (v1):
976 *
977 * [Like eglCreatePlatformWindowSurface,] eglCreatePlatformPixmapSurface
978 * also fails when called with a <display> that belongs to the
979 * surfaceless platform. It returns EGL_NO_SURFACE and generates
980 * EGL_BAD_NATIVE_PIXMAP.
981 *
982 * This check must occur before checking the EGLConfig, which emits
983 * EGL_BAD_CONFIG.
984 */
985 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
986 }
987 #endif
988
989 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
990 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
991 attrib_list);
992 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
993
994 RETURN_EGL_EVAL(disp, ret);
995 }
996
997
998 EGLSurface EGLAPIENTRY
999 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
1000 EGLNativePixmapType pixmap, const EGLint *attrib_list)
1001 {
1002 _EGLDisplay *disp = _eglLockDisplay(dpy);
1003
1004 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1005 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
1006 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
1007 attrib_list);
1008 }
1009
1010 static EGLSurface EGLAPIENTRY
1011 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
1012 void *native_pixmap,
1013 const EGLint *attrib_list)
1014 {
1015 _EGLDisplay *disp = _eglLockDisplay(dpy);
1016
1017 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1018 native_pixmap = fixupNativePixmap(disp, native_pixmap);
1019 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1020 attrib_list);
1021 }
1022
1023
1024 EGLSurface EGLAPIENTRY
1025 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
1026 void *native_pixmap,
1027 const EGLAttrib *attrib_list)
1028 {
1029 _EGLDisplay *disp = _eglLockDisplay(dpy);
1030 EGLSurface surface;
1031 EGLint *int_attribs;
1032
1033 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1034
1035 int_attribs = _eglConvertAttribsToInt(attrib_list);
1036 if (attrib_list && !int_attribs)
1037 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1038
1039 native_pixmap = fixupNativePixmap(disp, native_pixmap);
1040 surface = _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1041 int_attribs);
1042 free(int_attribs);
1043 return surface;
1044 }
1045
1046
1047 EGLSurface EGLAPIENTRY
1048 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
1049 const EGLint *attrib_list)
1050 {
1051 _EGLDisplay *disp = _eglLockDisplay(dpy);
1052 _EGLConfig *conf = _eglLookupConfig(config, disp);
1053 _EGLDriver *drv;
1054 _EGLSurface *surf;
1055 EGLSurface ret;
1056
1057 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1058 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1059
1060 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
1061 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1062
1063 RETURN_EGL_EVAL(disp, ret);
1064 }
1065
1066
1067 EGLBoolean EGLAPIENTRY
1068 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1069 {
1070 _EGLDisplay *disp = _eglLockDisplay(dpy);
1071 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1072 _EGLDriver *drv;
1073 EGLBoolean ret;
1074
1075 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1076 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1077 _eglUnlinkSurface(surf);
1078 ret = drv->API.DestroySurface(drv, disp, surf);
1079
1080 RETURN_EGL_EVAL(disp, ret);
1081 }
1082
1083 EGLBoolean EGLAPIENTRY
1084 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
1085 EGLint attribute, EGLint *value)
1086 {
1087 _EGLDisplay *disp = _eglLockDisplay(dpy);
1088 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1089 _EGLDriver *drv;
1090 EGLBoolean ret;
1091
1092 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1093 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1094 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
1095
1096 RETURN_EGL_EVAL(disp, ret);
1097 }
1098
1099 EGLBoolean EGLAPIENTRY
1100 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
1101 EGLint attribute, EGLint value)
1102 {
1103 _EGLDisplay *disp = _eglLockDisplay(dpy);
1104 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1105 _EGLDriver *drv;
1106 EGLBoolean ret;
1107
1108 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1109 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1110 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
1111
1112 RETURN_EGL_EVAL(disp, ret);
1113 }
1114
1115
1116 EGLBoolean EGLAPIENTRY
1117 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1118 {
1119 _EGLDisplay *disp = _eglLockDisplay(dpy);
1120 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1121 _EGLDriver *drv;
1122 EGLBoolean ret;
1123
1124 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1125 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1126 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
1127
1128 RETURN_EGL_EVAL(disp, ret);
1129 }
1130
1131
1132 EGLBoolean EGLAPIENTRY
1133 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1134 {
1135 _EGLDisplay *disp = _eglLockDisplay(dpy);
1136 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1137 _EGLDriver *drv;
1138 EGLBoolean ret;
1139
1140 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1141 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1142 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
1143
1144 RETURN_EGL_EVAL(disp, ret);
1145 }
1146
1147
1148 EGLBoolean EGLAPIENTRY
1149 eglSwapInterval(EGLDisplay dpy, EGLint interval)
1150 {
1151 _EGLDisplay *disp = _eglLockDisplay(dpy);
1152 _EGLContext *ctx = _eglGetCurrentContext();
1153 _EGLSurface *surf = ctx ? ctx->DrawSurface : NULL;
1154 _EGLDriver *drv;
1155 EGLBoolean ret;
1156
1157 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1158 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1159
1160 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1161 ctx->Resource.Display != disp)
1162 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
1163
1164 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
1165 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1166
1167 ret = drv->API.SwapInterval(drv, disp, surf, interval);
1168
1169 RETURN_EGL_EVAL(disp, ret);
1170 }
1171
1172
1173 EGLBoolean EGLAPIENTRY
1174 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1175 {
1176 _EGLContext *ctx = _eglGetCurrentContext();
1177 _EGLDisplay *disp = _eglLockDisplay(dpy);
1178 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1179 _EGLDriver *drv;
1180 EGLBoolean ret;
1181
1182 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1183 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1184
1185 /* surface must be bound to current context in EGL 1.4 */
1186 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1187 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1188 surf != ctx->DrawSurface)
1189 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1190 #endif
1191
1192 ret = drv->API.SwapBuffers(drv, disp, surf);
1193
1194 RETURN_EGL_EVAL(disp, ret);
1195 }
1196
1197
1198 static EGLBoolean
1199 _eglSwapBuffersWithDamageCommon(_EGLDisplay *disp, _EGLSurface *surf,
1200 EGLint *rects, EGLint n_rects)
1201 {
1202 _EGLContext *ctx = _eglGetCurrentContext();
1203 _EGLDriver *drv;
1204 EGLBoolean ret;
1205
1206 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1207
1208 /* surface must be bound to current context in EGL 1.4 */
1209 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1210 surf != ctx->DrawSurface)
1211 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1212
1213 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1214 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1215
1216 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1217
1218 RETURN_EGL_EVAL(disp, ret);
1219 }
1220
1221 static EGLBoolean EGLAPIENTRY
1222 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1223 EGLint *rects, EGLint n_rects)
1224 {
1225 _EGLDisplay *disp = _eglLockDisplay(dpy);
1226 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1227 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1228 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1229 }
1230
1231 static EGLBoolean EGLAPIENTRY
1232 eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
1233 EGLint *rects, EGLint n_rects)
1234 {
1235 _EGLDisplay *disp = _eglLockDisplay(dpy);
1236 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1237 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1238 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1239 }
1240
1241 EGLBoolean EGLAPIENTRY
1242 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1243 {
1244 _EGLDisplay *disp = _eglLockDisplay(dpy);
1245 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1246 _EGLDriver *drv;
1247 EGLBoolean ret;
1248 void *native_pixmap_ptr;
1249
1250 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1251 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1252 native_pixmap_ptr = (void*) target;
1253
1254 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1255 if (disp->Platform != _eglGetNativePlatform(disp->PlatformDisplay))
1256 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_FALSE);
1257 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1258
1259 RETURN_EGL_EVAL(disp, ret);
1260 }
1261
1262
1263 static EGLBoolean
1264 _eglWaitClientCommon(void)
1265 {
1266 _EGLContext *ctx = _eglGetCurrentContext();
1267 _EGLDisplay *disp;
1268 _EGLDriver *drv;
1269 EGLBoolean ret;
1270
1271 if (!ctx)
1272 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1273
1274 disp = ctx->Resource.Display;
1275 mtx_lock(&disp->Mutex);
1276
1277 /* let bad current context imply bad current surface */
1278 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1279 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1280 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1281
1282 /* a valid current context implies an initialized current display */
1283 assert(disp->Initialized);
1284 drv = disp->Driver;
1285 ret = drv->API.WaitClient(drv, disp, ctx);
1286
1287 RETURN_EGL_EVAL(disp, ret);
1288 }
1289
1290 EGLBoolean EGLAPIENTRY
1291 eglWaitClient(void)
1292 {
1293 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1294 return _eglWaitClientCommon();
1295 }
1296
1297 EGLBoolean EGLAPIENTRY
1298 eglWaitGL(void)
1299 {
1300 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1301 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1302 return _eglWaitClientCommon();
1303 }
1304
1305
1306 EGLBoolean EGLAPIENTRY
1307 eglWaitNative(EGLint engine)
1308 {
1309 _EGLContext *ctx = _eglGetCurrentContext();
1310 _EGLDisplay *disp;
1311 _EGLDriver *drv;
1312 EGLBoolean ret;
1313
1314 if (!ctx)
1315 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1316
1317 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1318
1319 disp = ctx->Resource.Display;
1320 mtx_lock(&disp->Mutex);
1321
1322 /* let bad current context imply bad current surface */
1323 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1324 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1325 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1326
1327 /* a valid current context implies an initialized current display */
1328 assert(disp->Initialized);
1329 drv = disp->Driver;
1330 ret = drv->API.WaitNative(drv, disp, engine);
1331
1332 RETURN_EGL_EVAL(disp, ret);
1333 }
1334
1335
1336 EGLDisplay EGLAPIENTRY
1337 eglGetCurrentDisplay(void)
1338 {
1339 _EGLContext *ctx = _eglGetCurrentContext();
1340 EGLDisplay ret;
1341
1342 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1343
1344 RETURN_EGL_SUCCESS(NULL, ret);
1345 }
1346
1347
1348 EGLContext EGLAPIENTRY
1349 eglGetCurrentContext(void)
1350 {
1351 _EGLContext *ctx = _eglGetCurrentContext();
1352 EGLContext ret;
1353
1354 ret = _eglGetContextHandle(ctx);
1355
1356 RETURN_EGL_SUCCESS(NULL, ret);
1357 }
1358
1359
1360 EGLSurface EGLAPIENTRY
1361 eglGetCurrentSurface(EGLint readdraw)
1362 {
1363 _EGLContext *ctx = _eglGetCurrentContext();
1364 EGLint err = EGL_SUCCESS;
1365 _EGLSurface *surf;
1366 EGLSurface ret;
1367
1368 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_NO_SURFACE);
1369
1370 if (!ctx)
1371 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1372
1373 switch (readdraw) {
1374 case EGL_DRAW:
1375 surf = ctx->DrawSurface;
1376 break;
1377 case EGL_READ:
1378 surf = ctx->ReadSurface;
1379 break;
1380 default:
1381 surf = NULL;
1382 err = EGL_BAD_PARAMETER;
1383 break;
1384 }
1385
1386 ret = _eglGetSurfaceHandle(surf);
1387
1388 RETURN_EGL_ERROR(NULL, err, ret);
1389 }
1390
1391
1392 EGLint EGLAPIENTRY
1393 eglGetError(void)
1394 {
1395 _EGLThreadInfo *t = _eglGetCurrentThread();
1396 EGLint e = t->LastError;
1397 if (!_eglIsCurrentThreadDummy())
1398 t->LastError = EGL_SUCCESS;
1399 return e;
1400 }
1401
1402
1403 /**
1404 ** EGL 1.2
1405 **/
1406
1407 /**
1408 * Specify the client API to use for subsequent calls including:
1409 * eglCreateContext()
1410 * eglGetCurrentContext()
1411 * eglGetCurrentDisplay()
1412 * eglGetCurrentSurface()
1413 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1414 * eglWaitClient()
1415 * eglWaitNative()
1416 * See section 3.7 "Rendering Context" in the EGL specification for details.
1417 */
1418 EGLBoolean EGLAPIENTRY
1419 eglBindAPI(EGLenum api)
1420 {
1421 _EGLThreadInfo *t;
1422
1423 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1424
1425 t = _eglGetCurrentThread();
1426 if (_eglIsCurrentThreadDummy())
1427 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1428
1429 if (!_eglIsApiValid(api))
1430 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1431
1432 t->CurrentAPI = api;
1433
1434 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1435 }
1436
1437
1438 /**
1439 * Return the last value set with eglBindAPI().
1440 */
1441 EGLenum EGLAPIENTRY
1442 eglQueryAPI(void)
1443 {
1444 _EGLThreadInfo *t = _eglGetCurrentThread();
1445 EGLenum ret;
1446
1447 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1448 ret = t->CurrentAPI;
1449
1450 RETURN_EGL_SUCCESS(NULL, ret);
1451 }
1452
1453
1454 EGLSurface EGLAPIENTRY
1455 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1456 EGLClientBuffer buffer, EGLConfig config,
1457 const EGLint *attrib_list)
1458 {
1459 _EGLDisplay *disp = _eglLockDisplay(dpy);
1460 _EGLConfig *conf = _eglLookupConfig(config, disp);
1461 _EGLDriver *drv;
1462 _EGLSurface *surf;
1463 EGLSurface ret;
1464
1465 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1466
1467 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1468
1469 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1470 conf, attrib_list);
1471 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1472
1473 RETURN_EGL_EVAL(disp, ret);
1474 }
1475
1476
1477 EGLBoolean EGLAPIENTRY
1478 eglReleaseThread(void)
1479 {
1480 /* unbind current contexts */
1481 if (!_eglIsCurrentThreadDummy()) {
1482 _EGLThreadInfo *t = _eglGetCurrentThread();
1483 _EGLContext *ctx = t->CurrentContext;
1484
1485 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1486
1487 if (ctx) {
1488 _EGLDisplay *disp = ctx->Resource.Display;
1489 _EGLDriver *drv;
1490
1491 mtx_lock(&disp->Mutex);
1492 drv = disp->Driver;
1493 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1494 mtx_unlock(&disp->Mutex);
1495 }
1496 }
1497
1498 _eglDestroyCurrentThread();
1499
1500 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1501 }
1502
1503
1504 static EGLImage
1505 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1506 EGLClientBuffer buffer, const EGLint *attr_list)
1507 {
1508 _EGLContext *context = _eglLookupContext(ctx, disp);
1509 _EGLDriver *drv;
1510 _EGLImage *img;
1511 EGLImage ret;
1512
1513 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1514 if (!disp->Extensions.KHR_image_base)
1515 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1516 if (!context && ctx != EGL_NO_CONTEXT)
1517 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1518 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1519 * <ctx> must be EGL_NO_CONTEXT..."
1520 */
1521 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1522 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1523
1524 img = drv->API.CreateImageKHR(drv,
1525 disp, context, target, buffer, attr_list);
1526 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1527
1528 RETURN_EGL_EVAL(disp, ret);
1529 }
1530
1531 static EGLImage EGLAPIENTRY
1532 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1533 EGLClientBuffer buffer, const EGLint *attr_list)
1534 {
1535 _EGLDisplay *disp = _eglLockDisplay(dpy);
1536 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1537 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1538 }
1539
1540
1541 EGLImage EGLAPIENTRY
1542 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1543 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1544 {
1545 _EGLDisplay *disp = _eglLockDisplay(dpy);
1546 EGLImage image;
1547 EGLint *int_attribs;
1548
1549 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1550
1551 int_attribs = _eglConvertAttribsToInt(attr_list);
1552 if (attr_list && !int_attribs)
1553 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1554
1555 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1556 free(int_attribs);
1557 return image;
1558 }
1559
1560
1561 EGLBoolean EGLAPIENTRY
1562 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1563 {
1564 _EGLDisplay *disp = _eglLockDisplay(dpy);
1565 _EGLImage *img = _eglLookupImage(image, disp);
1566 _EGLDriver *drv;
1567 EGLBoolean ret;
1568
1569 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1570
1571 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1572 if (!disp->Extensions.KHR_image_base)
1573 RETURN_EGL_EVAL(disp, EGL_FALSE);
1574 if (!img)
1575 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1576
1577 _eglUnlinkImage(img);
1578 ret = drv->API.DestroyImageKHR(drv, disp, img);
1579
1580 RETURN_EGL_EVAL(disp, ret);
1581 }
1582
1583
1584 static EGLSync
1585 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list,
1586 EGLBoolean orig_is_EGLAttrib,
1587 EGLenum invalid_type_error)
1588 {
1589 _EGLContext *ctx = _eglGetCurrentContext();
1590 _EGLDriver *drv;
1591 _EGLSync *sync;
1592 EGLSync ret;
1593
1594 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1595
1596 if (!disp->Extensions.KHR_cl_event2 && orig_is_EGLAttrib) {
1597 /* There exist two EGLAttrib variants of eglCreateSync*:
1598 * eglCreateSync64KHR which requires EGL_KHR_cl_event2, and eglCreateSync
1599 * which requires EGL 1.5. Here we use the presence of EGL_KHR_cl_event2
1600 * support as a proxy for EGL 1.5 support, even though that's not
1601 * entirely correct (though _eglComputeVersion does the same).
1602 *
1603 * The EGL spec provides no guidance on how to handle unsupported
1604 * functions. EGL_BAD_MATCH seems reasonable.
1605 */
1606 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1607 }
1608
1609 /* If type is EGL_SYNC_FENCE and no context is current for the bound API
1610 * (i.e., eglGetCurrentContext returns EGL_NO_CONTEXT ), an EGL_BAD_MATCH
1611 * error is generated.
1612 */
1613 if (!ctx &&
1614 (type == EGL_SYNC_FENCE_KHR || type == EGL_SYNC_NATIVE_FENCE_ANDROID))
1615 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1616
1617 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1618 if (ctx && (ctx->Resource.Display != disp ||
1619 ctx->ClientAPI != EGL_OPENGL_ES_API))
1620 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1621
1622 switch (type) {
1623 case EGL_SYNC_FENCE_KHR:
1624 if (!disp->Extensions.KHR_fence_sync)
1625 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1626 break;
1627 case EGL_SYNC_REUSABLE_KHR:
1628 if (!disp->Extensions.KHR_reusable_sync)
1629 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1630 break;
1631 case EGL_SYNC_CL_EVENT_KHR:
1632 if (!disp->Extensions.KHR_cl_event2)
1633 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1634 break;
1635 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1636 if (!disp->Extensions.ANDROID_native_fence_sync)
1637 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1638 break;
1639 default:
1640 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1641 }
1642
1643 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list);
1644 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1645
1646 RETURN_EGL_EVAL(disp, ret);
1647 }
1648
1649
1650 static EGLSync EGLAPIENTRY
1651 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *int_list)
1652 {
1653 _EGLDisplay *disp = _eglLockDisplay(dpy);
1654 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1655
1656 EGLSync sync;
1657 EGLAttrib *attrib_list;
1658 EGLint err;
1659
1660 if (sizeof(int_list[0]) == sizeof(attrib_list[0])) {
1661 attrib_list = (EGLAttrib *) int_list;
1662 } else {
1663 err = _eglConvertIntsToAttribs(int_list, &attrib_list);
1664 if (err != EGL_SUCCESS)
1665 RETURN_EGL_ERROR(disp, err, EGL_NO_SYNC);
1666 }
1667
1668 sync = _eglCreateSync(disp, type, attrib_list, EGL_FALSE,
1669 EGL_BAD_ATTRIBUTE);
1670
1671 if (sizeof(int_list[0]) != sizeof(attrib_list[0]))
1672 free(attrib_list);
1673
1674 /* Don't double-unlock the display. _eglCreateSync already unlocked it. */
1675 return sync;
1676 }
1677
1678
1679 static EGLSync EGLAPIENTRY
1680 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1681 {
1682 _EGLDisplay *disp = _eglLockDisplay(dpy);
1683 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1684 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1685 EGL_BAD_ATTRIBUTE);
1686 }
1687
1688
1689 EGLSync EGLAPIENTRY
1690 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1691 {
1692 _EGLDisplay *disp = _eglLockDisplay(dpy);
1693 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1694 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1695 EGL_BAD_PARAMETER);
1696 }
1697
1698
1699 EGLBoolean EGLAPIENTRY
1700 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1701 {
1702 _EGLDisplay *disp = _eglLockDisplay(dpy);
1703 _EGLSync *s = _eglLookupSync(sync, disp);
1704 _EGLDriver *drv;
1705 EGLBoolean ret;
1706
1707 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1708
1709 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1710 assert(disp->Extensions.KHR_reusable_sync ||
1711 disp->Extensions.KHR_fence_sync ||
1712 disp->Extensions.ANDROID_native_fence_sync);
1713
1714 _eglUnlinkSync(s);
1715 ret = drv->API.DestroySyncKHR(drv, disp, s);
1716
1717 RETURN_EGL_EVAL(disp, ret);
1718 }
1719
1720
1721 EGLint EGLAPIENTRY
1722 eglClientWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags, EGLTime timeout)
1723 {
1724 _EGLDisplay *disp = _eglLockDisplay(dpy);
1725 _EGLSync *s = _eglLookupSync(sync, disp);
1726 _EGLDriver *drv;
1727 EGLint ret;
1728
1729 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1730
1731 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1732 assert(disp->Extensions.KHR_reusable_sync ||
1733 disp->Extensions.KHR_fence_sync ||
1734 disp->Extensions.ANDROID_native_fence_sync);
1735
1736 if (s->SyncStatus == EGL_SIGNALED_KHR)
1737 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1738
1739 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1740 * unlocked here to allow other threads also to be able to
1741 * go into waiting state.
1742 */
1743
1744 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1745 _eglUnlockDisplay(dpy);
1746
1747 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1748
1749 /*
1750 * 'disp' is already unlocked for reusable sync type,
1751 * so passing 'NULL' to bypass unlocking display.
1752 */
1753 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1754 RETURN_EGL_EVAL(NULL, ret);
1755 else
1756 RETURN_EGL_EVAL(disp, ret);
1757 }
1758
1759
1760 static EGLint
1761 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
1762 {
1763 _EGLContext *ctx = _eglGetCurrentContext();
1764 _EGLDriver *drv;
1765 EGLint ret;
1766
1767 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1768 assert(disp->Extensions.KHR_wait_sync);
1769
1770 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1771 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
1772 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1773
1774 /* the API doesn't allow any flags yet */
1775 if (flags != 0)
1776 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1777
1778 ret = drv->API.WaitSyncKHR(drv, disp, s);
1779
1780 RETURN_EGL_EVAL(disp, ret);
1781 }
1782
1783 static EGLint EGLAPIENTRY
1784 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
1785 {
1786 _EGLDisplay *disp = _eglLockDisplay(dpy);
1787 _EGLSync *s = _eglLookupSync(sync, disp);
1788 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1789 return _eglWaitSyncCommon(disp, s, flags);
1790 }
1791
1792
1793 EGLBoolean EGLAPIENTRY
1794 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
1795 {
1796 /* The KHR version returns EGLint, while the core version returns
1797 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
1798 * EGL_TRUE.
1799 */
1800 _EGLDisplay *disp = _eglLockDisplay(dpy);
1801 _EGLSync *s = _eglLookupSync(sync, disp);
1802 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1803 return _eglWaitSyncCommon(disp, s, flags);
1804 }
1805
1806
1807 static EGLBoolean EGLAPIENTRY
1808 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
1809 {
1810 _EGLDisplay *disp = _eglLockDisplay(dpy);
1811 _EGLSync *s = _eglLookupSync(sync, disp);
1812 _EGLDriver *drv;
1813 EGLBoolean ret;
1814
1815 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1816
1817 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1818 assert(disp->Extensions.KHR_reusable_sync);
1819 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
1820
1821 RETURN_EGL_EVAL(disp, ret);
1822 }
1823
1824
1825 static EGLBoolean
1826 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
1827 {
1828 _EGLDriver *drv;
1829 EGLBoolean ret;
1830
1831 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1832 assert(disp->Extensions.KHR_reusable_sync ||
1833 disp->Extensions.KHR_fence_sync ||
1834 disp->Extensions.ANDROID_native_fence_sync);
1835 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
1836
1837 RETURN_EGL_EVAL(disp, ret);
1838 }
1839
1840 EGLBoolean EGLAPIENTRY
1841 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
1842 {
1843 _EGLDisplay *disp = _eglLockDisplay(dpy);
1844 _EGLSync *s = _eglLookupSync(sync, disp);
1845 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1846 return _eglGetSyncAttribCommon(disp, s, attribute, value);
1847 }
1848
1849
1850 static EGLBoolean EGLAPIENTRY
1851 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
1852 {
1853 _EGLDisplay *disp = _eglLockDisplay(dpy);
1854 _EGLSync *s = _eglLookupSync(sync, disp);
1855 EGLAttrib attrib;
1856 EGLBoolean result;
1857
1858 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1859
1860 if (!value)
1861 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1862
1863 attrib = *value;
1864 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
1865
1866 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
1867 *
1868 * If any error occurs, <*value> is not modified.
1869 */
1870 if (result == EGL_FALSE)
1871 return result;
1872
1873 *value = attrib;
1874 return result;
1875 }
1876
1877 static EGLint EGLAPIENTRY
1878 eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSync sync)
1879 {
1880 _EGLDisplay *disp = _eglLockDisplay(dpy);
1881 _EGLSync *s = _eglLookupSync(sync, disp);
1882 _EGLDriver *drv;
1883 EGLBoolean ret;
1884
1885 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1886
1887 /* the spec doesn't seem to specify what happens if the fence
1888 * type is not EGL_SYNC_NATIVE_FENCE_ANDROID, but this seems
1889 * sensible:
1890 */
1891 if (!(s && (s->Type == EGL_SYNC_NATIVE_FENCE_ANDROID)))
1892 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_NATIVE_FENCE_FD_ANDROID);
1893
1894 _EGL_CHECK_SYNC(disp, s, EGL_NO_NATIVE_FENCE_FD_ANDROID, drv);
1895 assert(disp->Extensions.ANDROID_native_fence_sync);
1896 ret = drv->API.DupNativeFenceFDANDROID(drv, disp, s);
1897
1898 RETURN_EGL_EVAL(disp, ret);
1899 }
1900
1901 static EGLBoolean EGLAPIENTRY
1902 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
1903 EGLint numRects, const EGLint *rects)
1904 {
1905 _EGLContext *ctx = _eglGetCurrentContext();
1906 _EGLDisplay *disp = _eglLockDisplay(dpy);
1907 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1908 _EGLDriver *drv;
1909 EGLBoolean ret;
1910
1911 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1912
1913 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1914
1915 if (!disp->Extensions.NOK_swap_region)
1916 RETURN_EGL_EVAL(disp, EGL_FALSE);
1917
1918 /* surface must be bound to current context in EGL 1.4 */
1919 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1920 surf != ctx->DrawSurface)
1921 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1922
1923 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
1924
1925 RETURN_EGL_EVAL(disp, ret);
1926 }
1927
1928
1929 static EGLImage EGLAPIENTRY
1930 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
1931 {
1932 _EGLDisplay *disp = _eglLockDisplay(dpy);
1933 _EGLDriver *drv;
1934 _EGLImage *img;
1935 EGLImage ret;
1936
1937 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1938
1939 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1940 if (!disp->Extensions.MESA_drm_image)
1941 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1942
1943 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
1944 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1945
1946 RETURN_EGL_EVAL(disp, ret);
1947 }
1948
1949 static EGLBoolean EGLAPIENTRY
1950 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
1951 EGLint *name, EGLint *handle, EGLint *stride)
1952 {
1953 _EGLDisplay *disp = _eglLockDisplay(dpy);
1954 _EGLImage *img = _eglLookupImage(image, disp);
1955 _EGLDriver *drv;
1956 EGLBoolean ret;
1957
1958 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1959
1960 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1961 assert(disp->Extensions.MESA_drm_image);
1962
1963 if (!img)
1964 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1965
1966 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
1967
1968 RETURN_EGL_EVAL(disp, ret);
1969 }
1970
1971
1972 struct wl_display;
1973
1974 static EGLBoolean EGLAPIENTRY
1975 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1976 {
1977 _EGLDisplay *disp = _eglLockDisplay(dpy);
1978 _EGLDriver *drv;
1979 EGLBoolean ret;
1980
1981 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1982
1983 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1984 assert(disp->Extensions.WL_bind_wayland_display);
1985
1986 if (!display)
1987 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1988
1989 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
1990
1991 RETURN_EGL_EVAL(disp, ret);
1992 }
1993
1994 static EGLBoolean EGLAPIENTRY
1995 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1996 {
1997 _EGLDisplay *disp = _eglLockDisplay(dpy);
1998 _EGLDriver *drv;
1999 EGLBoolean ret;
2000
2001 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2002
2003 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2004 assert(disp->Extensions.WL_bind_wayland_display);
2005
2006 if (!display)
2007 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2008
2009 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
2010
2011 RETURN_EGL_EVAL(disp, ret);
2012 }
2013
2014 static EGLBoolean EGLAPIENTRY
2015 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
2016 EGLint attribute, EGLint *value)
2017 {
2018 _EGLDisplay *disp = _eglLockDisplay(dpy);
2019 _EGLDriver *drv;
2020 EGLBoolean ret;
2021
2022 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2023
2024 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2025 assert(disp->Extensions.WL_bind_wayland_display);
2026
2027 if (!buffer)
2028 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2029
2030 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
2031
2032 RETURN_EGL_EVAL(disp, ret);
2033 }
2034
2035
2036 static struct wl_buffer * EGLAPIENTRY
2037 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
2038 {
2039 _EGLDisplay *disp = _eglLockDisplay(dpy);
2040 _EGLImage *img;
2041 _EGLDriver *drv;
2042 struct wl_buffer *ret;
2043
2044 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2045
2046 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2047 assert(disp->Extensions.WL_create_wayland_buffer_from_image);
2048
2049 img = _eglLookupImage(image, disp);
2050
2051 if (!img)
2052 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
2053
2054 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
2055
2056 RETURN_EGL_EVAL(disp, ret);
2057 }
2058
2059 static EGLBoolean EGLAPIENTRY
2060 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
2061 EGLint x, EGLint y, EGLint width, EGLint height)
2062 {
2063 _EGLDisplay *disp = _eglLockDisplay(dpy);
2064 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2065 _EGLDriver *drv;
2066 EGLBoolean ret;
2067
2068 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2069
2070 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2071
2072 if (!disp->Extensions.NV_post_sub_buffer)
2073 RETURN_EGL_EVAL(disp, EGL_FALSE);
2074
2075 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
2076
2077 RETURN_EGL_EVAL(disp, ret);
2078 }
2079
2080 static EGLBoolean EGLAPIENTRY
2081 eglGetSyncValuesCHROMIUM(EGLDisplay display, EGLSurface surface,
2082 EGLuint64KHR *ust, EGLuint64KHR *msc,
2083 EGLuint64KHR *sbc)
2084 {
2085 _EGLDisplay *disp = _eglLockDisplay(display);
2086 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2087 _EGLDriver *drv;
2088 EGLBoolean ret;
2089
2090 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2091
2092 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2093 if (!disp->Extensions.CHROMIUM_sync_control)
2094 RETURN_EGL_EVAL(disp, EGL_FALSE);
2095
2096 if (!ust || !msc || !sbc)
2097 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2098
2099 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
2100
2101 RETURN_EGL_EVAL(disp, ret);
2102 }
2103
2104 static EGLBoolean EGLAPIENTRY
2105 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
2106 EGLint *fourcc, EGLint *nplanes,
2107 EGLuint64KHR *modifiers)
2108 {
2109 _EGLDisplay *disp = _eglLockDisplay(dpy);
2110 _EGLImage *img = _eglLookupImage(image, disp);
2111 _EGLDriver *drv;
2112 EGLBoolean ret;
2113
2114 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2115
2116 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2117 assert(disp->Extensions.MESA_image_dma_buf_export);
2118
2119 if (!img)
2120 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2121
2122 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
2123 modifiers);
2124
2125 RETURN_EGL_EVAL(disp, ret);
2126 }
2127
2128 static EGLBoolean EGLAPIENTRY
2129 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
2130 int *fds, EGLint *strides, EGLint *offsets)
2131 {
2132 _EGLDisplay *disp = _eglLockDisplay(dpy);
2133 _EGLImage *img = _eglLookupImage(image, disp);
2134 _EGLDriver *drv;
2135 EGLBoolean ret;
2136
2137 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2138
2139 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2140 assert(disp->Extensions.MESA_image_dma_buf_export);
2141
2142 if (!img)
2143 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2144
2145 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
2146
2147 RETURN_EGL_EVAL(disp, ret);
2148 }
2149
2150 static EGLint EGLAPIENTRY
2151 eglLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object,
2152 EGLLabelKHR label)
2153 {
2154 _EGLDisplay *disp = NULL;
2155 _EGLResourceType type;
2156
2157 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2158
2159 if (objectType == EGL_OBJECT_THREAD_KHR) {
2160 _EGLThreadInfo *t = _eglGetCurrentThread();
2161
2162 if (!_eglIsCurrentThreadDummy()) {
2163 t->Label = label;
2164 return EGL_SUCCESS;
2165 }
2166
2167 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_BAD_ALLOC);
2168 }
2169
2170 disp = _eglLockDisplay(dpy);
2171 if (disp == NULL)
2172 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_BAD_DISPLAY);
2173
2174 if (objectType == EGL_OBJECT_DISPLAY_KHR) {
2175 if (dpy != (EGLDisplay) object)
2176 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2177
2178 disp->Label = label;
2179 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2180 }
2181
2182 switch (objectType) {
2183 case EGL_OBJECT_CONTEXT_KHR:
2184 type = _EGL_RESOURCE_CONTEXT;
2185 break;
2186 case EGL_OBJECT_SURFACE_KHR:
2187 type = _EGL_RESOURCE_SURFACE;
2188 break;
2189 case EGL_OBJECT_IMAGE_KHR:
2190 type = _EGL_RESOURCE_IMAGE;
2191 break;
2192 case EGL_OBJECT_SYNC_KHR:
2193 type = _EGL_RESOURCE_SYNC;
2194 break;
2195 case EGL_OBJECT_STREAM_KHR:
2196 default:
2197 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2198 }
2199
2200 if (_eglCheckResource(object, type, disp)) {
2201 _EGLResource *res = (_EGLResource *) object;
2202
2203 res->Label = label;
2204 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2205 }
2206
2207 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2208 }
2209
2210 static EGLBoolean
2211 validDebugMessageLevel(EGLAttrib level)
2212 {
2213 return (level >= EGL_DEBUG_MSG_CRITICAL_KHR &&
2214 level <= EGL_DEBUG_MSG_INFO_KHR);
2215 }
2216
2217 static EGLint EGLAPIENTRY
2218 eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
2219 const EGLAttrib *attrib_list)
2220 {
2221 unsigned int newEnabled;
2222
2223 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2224
2225 mtx_lock(_eglGlobal.Mutex);
2226
2227 newEnabled = _eglGlobal.debugTypesEnabled;
2228 if (attrib_list != NULL) {
2229 int i;
2230
2231 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
2232 if (validDebugMessageLevel(attrib_list[i])) {
2233 if (attrib_list[i + 1])
2234 newEnabled |= DebugBitFromType(attrib_list[i]);
2235 else
2236 newEnabled &= ~DebugBitFromType(attrib_list[i]);
2237 continue;
2238 }
2239
2240 // On error, set the last error code, call the current
2241 // debug callback, and return the error code.
2242 mtx_unlock(_eglGlobal.Mutex);
2243 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2244 "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
2245 return EGL_BAD_ATTRIBUTE;
2246 }
2247 }
2248
2249 if (callback != NULL) {
2250 _eglGlobal.debugCallback = callback;
2251 _eglGlobal.debugTypesEnabled = newEnabled;
2252 } else {
2253 _eglGlobal.debugCallback = NULL;
2254 _eglGlobal.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR;
2255 }
2256
2257 mtx_unlock(_eglGlobal.Mutex);
2258 return EGL_SUCCESS;
2259 }
2260
2261 static EGLBoolean EGLAPIENTRY
2262 eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
2263 {
2264 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2265
2266 mtx_lock(_eglGlobal.Mutex);
2267
2268 do {
2269 if (validDebugMessageLevel(attribute)) {
2270 if (_eglGlobal.debugTypesEnabled & DebugBitFromType(attribute))
2271 *value = EGL_TRUE;
2272 else
2273 *value = EGL_FALSE;
2274 break;
2275 }
2276
2277 if (attribute == EGL_DEBUG_CALLBACK_KHR) {
2278 *value = (EGLAttrib) _eglGlobal.debugCallback;
2279 break;
2280 }
2281
2282 mtx_unlock(_eglGlobal.Mutex);
2283 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2284 "Invalid attribute 0x%04lx", (unsigned long) attribute);
2285 return EGL_FALSE;
2286 } while (0);
2287
2288 mtx_unlock(_eglGlobal.Mutex);
2289 return EGL_TRUE;
2290 }
2291
2292 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
2293 eglGetProcAddress(const char *procname)
2294 {
2295 static const struct {
2296 const char *name;
2297 _EGLProc function;
2298 } egl_functions[] = {
2299 /* core functions queryable in the presence of
2300 * EGL_KHR_get_all_proc_addresses or EGL 1.5
2301 */
2302 /* alphabetical order */
2303 { "eglBindAPI", (_EGLProc) eglBindAPI },
2304 { "eglBindTexImage", (_EGLProc) eglBindTexImage },
2305 { "eglChooseConfig", (_EGLProc) eglChooseConfig },
2306 { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
2307 { "eglCreateContext", (_EGLProc) eglCreateContext },
2308 { "eglCreatePbufferFromClientBuffer", (_EGLProc) eglCreatePbufferFromClientBuffer },
2309 { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
2310 { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
2311 { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
2312 { "eglDestroyContext", (_EGLProc) eglDestroyContext },
2313 { "eglDestroySurface", (_EGLProc) eglDestroySurface },
2314 { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
2315 { "eglGetConfigs", (_EGLProc) eglGetConfigs },
2316 { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
2317 { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
2318 { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
2319 { "eglGetDisplay", (_EGLProc) eglGetDisplay },
2320 { "eglGetError", (_EGLProc) eglGetError },
2321 { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
2322 { "eglInitialize", (_EGLProc) eglInitialize },
2323 { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
2324 { "eglQueryAPI", (_EGLProc) eglQueryAPI },
2325 { "eglQueryContext", (_EGLProc) eglQueryContext },
2326 { "eglQueryString", (_EGLProc) eglQueryString },
2327 { "eglQuerySurface", (_EGLProc) eglQuerySurface },
2328 { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
2329 { "eglReleaseThread", (_EGLProc) eglReleaseThread },
2330 { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
2331 { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
2332 { "eglSwapInterval", (_EGLProc) eglSwapInterval },
2333 { "eglTerminate", (_EGLProc) eglTerminate },
2334 { "eglWaitClient", (_EGLProc) eglWaitClient },
2335 { "eglWaitGL", (_EGLProc) eglWaitGL },
2336 { "eglWaitNative", (_EGLProc) eglWaitNative },
2337 { "eglCreateSync", (_EGLProc) eglCreateSync },
2338 { "eglDestroySync", (_EGLProc) eglDestroySync },
2339 { "eglClientWaitSync", (_EGLProc) eglClientWaitSync },
2340 { "eglGetSyncAttrib", (_EGLProc) eglGetSyncAttrib },
2341 { "eglWaitSync", (_EGLProc) eglWaitSync },
2342 { "eglCreateImage", (_EGLProc) eglCreateImage },
2343 { "eglDestroyImage", (_EGLProc) eglDestroyImage },
2344 { "eglGetPlatformDisplay", (_EGLProc) eglGetPlatformDisplay },
2345 { "eglCreatePlatformWindowSurface", (_EGLProc) eglCreatePlatformWindowSurface },
2346 { "eglCreatePlatformPixmapSurface", (_EGLProc) eglCreatePlatformPixmapSurface },
2347 { "eglCreateImageKHR", (_EGLProc) eglCreateImageKHR },
2348 { "eglDestroyImageKHR", (_EGLProc) eglDestroyImage },
2349 { "eglCreateSyncKHR", (_EGLProc) eglCreateSyncKHR },
2350 { "eglCreateSync64KHR", (_EGLProc) eglCreateSync64KHR },
2351 { "eglDestroySyncKHR", (_EGLProc) eglDestroySync },
2352 { "eglClientWaitSyncKHR", (_EGLProc) eglClientWaitSync },
2353 { "eglWaitSyncKHR", (_EGLProc) eglWaitSyncKHR },
2354 { "eglSignalSyncKHR", (_EGLProc) eglSignalSyncKHR },
2355 { "eglGetSyncAttribKHR", (_EGLProc) eglGetSyncAttribKHR },
2356 { "eglSwapBuffersRegionNOK", (_EGLProc) eglSwapBuffersRegionNOK },
2357 { "eglCreateDRMImageMESA", (_EGLProc) eglCreateDRMImageMESA },
2358 { "eglExportDRMImageMESA", (_EGLProc) eglExportDRMImageMESA },
2359 { "eglBindWaylandDisplayWL", (_EGLProc) eglBindWaylandDisplayWL },
2360 { "eglUnbindWaylandDisplayWL", (_EGLProc) eglUnbindWaylandDisplayWL },
2361 { "eglQueryWaylandBufferWL", (_EGLProc) eglQueryWaylandBufferWL },
2362 { "eglCreateWaylandBufferFromImageWL", (_EGLProc) eglCreateWaylandBufferFromImageWL },
2363 { "eglPostSubBufferNV", (_EGLProc) eglPostSubBufferNV },
2364 { "eglSwapBuffersWithDamageEXT", (_EGLProc) eglSwapBuffersWithDamageEXT },
2365 { "eglSwapBuffersWithDamageKHR", (_EGLProc) eglSwapBuffersWithDamageKHR },
2366 { "eglGetPlatformDisplayEXT", (_EGLProc) eglGetPlatformDisplayEXT },
2367 { "eglCreatePlatformWindowSurfaceEXT", (_EGLProc) eglCreatePlatformWindowSurfaceEXT },
2368 { "eglCreatePlatformPixmapSurfaceEXT", (_EGLProc) eglCreatePlatformPixmapSurfaceEXT },
2369 { "eglGetSyncValuesCHROMIUM", (_EGLProc) eglGetSyncValuesCHROMIUM },
2370 { "eglExportDMABUFImageQueryMESA", (_EGLProc) eglExportDMABUFImageQueryMESA },
2371 { "eglExportDMABUFImageMESA", (_EGLProc) eglExportDMABUFImageMESA },
2372 { "eglLabelObjectKHR", (_EGLProc) eglLabelObjectKHR },
2373 { "eglDebugMessageControlKHR", (_EGLProc) eglDebugMessageControlKHR },
2374 { "eglQueryDebugKHR", (_EGLProc) eglQueryDebugKHR },
2375 { "eglDupNativeFenceFDANDROID", (_EGLProc) eglDupNativeFenceFDANDROID },
2376 { NULL, NULL }
2377 };
2378 EGLint i;
2379 _EGLProc ret;
2380
2381 if (!procname)
2382 RETURN_EGL_SUCCESS(NULL, NULL);
2383
2384 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2385
2386 ret = NULL;
2387 if (strncmp(procname, "egl", 3) == 0) {
2388 for (i = 0; egl_functions[i].name; i++) {
2389 if (strcmp(egl_functions[i].name, procname) == 0) {
2390 ret = egl_functions[i].function;
2391 break;
2392 }
2393 }
2394 }
2395 if (!ret)
2396 ret = _eglGetDriverProc(procname);
2397
2398 RETURN_EGL_SUCCESS(NULL, ret);
2399 }
2400
2401 static int
2402 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
2403 _EGLDisplay **disp, _EGLDriver **drv,
2404 _EGLContext **ctx)
2405 {
2406
2407 *disp = _eglLockDisplay(dpy);
2408 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
2409 if (*disp)
2410 _eglUnlockDisplay(*disp);
2411 return MESA_GLINTEROP_INVALID_DISPLAY;
2412 }
2413
2414 *drv = (*disp)->Driver;
2415
2416 *ctx = _eglLookupContext(context, *disp);
2417 if (!*ctx ||
2418 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
2419 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
2420 _eglUnlockDisplay(*disp);
2421 return MESA_GLINTEROP_INVALID_CONTEXT;
2422 }
2423
2424 return MESA_GLINTEROP_SUCCESS;
2425 }
2426
2427 PUBLIC int
2428 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
2429 struct mesa_glinterop_device_info *out)
2430 {
2431 _EGLDisplay *disp;
2432 _EGLDriver *drv;
2433 _EGLContext *ctx;
2434 int ret;
2435
2436 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2437 if (ret != MESA_GLINTEROP_SUCCESS)
2438 return ret;
2439
2440 if (drv->API.GLInteropQueryDeviceInfo)
2441 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
2442 else
2443 ret = MESA_GLINTEROP_UNSUPPORTED;
2444
2445 _eglUnlockDisplay(disp);
2446 return ret;
2447 }
2448
2449 PUBLIC int
2450 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2451 struct mesa_glinterop_export_in *in,
2452 struct mesa_glinterop_export_out *out)
2453 {
2454 _EGLDisplay *disp;
2455 _EGLDriver *drv;
2456 _EGLContext *ctx;
2457 int ret;
2458
2459 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2460 if (ret != MESA_GLINTEROP_SUCCESS)
2461 return ret;
2462
2463 if (drv->API.GLInteropExportObject)
2464 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
2465 else
2466 ret = MESA_GLINTEROP_UNSUPPORTED;
2467
2468 _eglUnlockDisplay(disp);
2469 return ret;
2470 }