egl: add and enable EGL_KHR_config_attribs
[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 static 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_config_attribs);
489 _EGL_CHECK_EXTENSION(KHR_create_context);
490 _EGL_CHECK_EXTENSION(KHR_fence_sync);
491 _EGL_CHECK_EXTENSION(KHR_get_all_proc_addresses);
492 _EGL_CHECK_EXTENSION(KHR_gl_colorspace);
493 _EGL_CHECK_EXTENSION(KHR_gl_renderbuffer_image);
494 _EGL_CHECK_EXTENSION(KHR_gl_texture_2D_image);
495 _EGL_CHECK_EXTENSION(KHR_gl_texture_3D_image);
496 _EGL_CHECK_EXTENSION(KHR_gl_texture_cubemap_image);
497 if (dpy->Extensions.KHR_image_base && dpy->Extensions.KHR_image_pixmap)
498 _eglAppendExtension(&exts, "EGL_KHR_image");
499 _EGL_CHECK_EXTENSION(KHR_image_base);
500 _EGL_CHECK_EXTENSION(KHR_image_pixmap);
501 _EGL_CHECK_EXTENSION(KHR_no_config_context);
502 _EGL_CHECK_EXTENSION(KHR_reusable_sync);
503 _EGL_CHECK_EXTENSION(KHR_surfaceless_context);
504 if (dpy->Extensions.EXT_swap_buffers_with_damage)
505 _eglAppendExtension(&exts, "EGL_KHR_swap_buffers_with_damage");
506 _EGL_CHECK_EXTENSION(KHR_wait_sync);
507
508 if (dpy->Extensions.KHR_no_config_context)
509 _eglAppendExtension(&exts, "EGL_MESA_configless_context");
510 _EGL_CHECK_EXTENSION(MESA_drm_image);
511 _EGL_CHECK_EXTENSION(MESA_image_dma_buf_export);
512
513 _EGL_CHECK_EXTENSION(NOK_swap_region);
514 _EGL_CHECK_EXTENSION(NOK_texture_from_pixmap);
515
516 _EGL_CHECK_EXTENSION(NV_post_sub_buffer);
517
518 _EGL_CHECK_EXTENSION(WL_bind_wayland_display);
519 _EGL_CHECK_EXTENSION(WL_create_wayland_buffer_from_image);
520
521 #undef _EGL_CHECK_EXTENSION
522 }
523
524 static void
525 _eglCreateAPIsString(_EGLDisplay *dpy)
526 {
527 if (dpy->ClientAPIs & EGL_OPENGL_BIT)
528 strcat(dpy->ClientAPIsString, "OpenGL ");
529
530 if (dpy->ClientAPIs & EGL_OPENGL_ES_BIT ||
531 dpy->ClientAPIs & EGL_OPENGL_ES2_BIT ||
532 dpy->ClientAPIs & EGL_OPENGL_ES3_BIT_KHR) {
533 strcat(dpy->ClientAPIsString, "OpenGL_ES ");
534 }
535
536 if (dpy->ClientAPIs & EGL_OPENVG_BIT)
537 strcat(dpy->ClientAPIsString, "OpenVG ");
538
539 assert(strlen(dpy->ClientAPIsString) < sizeof(dpy->ClientAPIsString));
540 }
541
542 static void
543 _eglComputeVersion(_EGLDisplay *disp)
544 {
545 disp->Version = 14;
546
547 if (disp->Extensions.KHR_fence_sync &&
548 disp->Extensions.KHR_cl_event2 &&
549 disp->Extensions.KHR_wait_sync &&
550 disp->Extensions.KHR_image_base &&
551 disp->Extensions.KHR_gl_texture_2D_image &&
552 disp->Extensions.KHR_gl_texture_3D_image &&
553 disp->Extensions.KHR_gl_texture_cubemap_image &&
554 disp->Extensions.KHR_gl_renderbuffer_image &&
555 disp->Extensions.KHR_create_context &&
556 disp->Extensions.EXT_create_context_robustness &&
557 disp->Extensions.KHR_get_all_proc_addresses &&
558 disp->Extensions.KHR_gl_colorspace &&
559 disp->Extensions.KHR_surfaceless_context)
560 disp->Version = 15;
561 }
562
563 /**
564 * This is typically the second EGL function that an application calls.
565 * Here we load/initialize the actual hardware driver.
566 */
567 EGLBoolean EGLAPIENTRY
568 eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
569 {
570 _EGLDisplay *disp = _eglLockDisplay(dpy);
571
572 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
573
574 if (!disp)
575 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
576
577 if (!disp->Initialized) {
578 if (!_eglMatchDriver(disp, EGL_FALSE))
579 RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
580
581 /* limit to APIs supported by core */
582 disp->ClientAPIs &= _EGL_API_ALL_BITS;
583
584 /* EGL_KHR_get_all_proc_addresses is a corner-case extension. The spec
585 * classifies it as an EGL display extension, though conceptually it's an
586 * EGL client extension.
587 *
588 * From the EGL_KHR_get_all_proc_addresses spec:
589 *
590 * The EGL implementation must expose the name
591 * EGL_KHR_client_get_all_proc_addresses if and only if it exposes
592 * EGL_KHR_get_all_proc_addresses and supports
593 * EGL_EXT_client_extensions.
594 *
595 * Mesa unconditionally exposes both client extensions mentioned above,
596 * so the spec requires that each EGLDisplay unconditionally expose
597 * EGL_KHR_get_all_proc_addresses also.
598 */
599 disp->Extensions.KHR_get_all_proc_addresses = EGL_TRUE;
600
601 /* Extensions is used to provide EGL 1.3 functionality for 1.2 aware
602 * programs. It is driver agnostic and handled in the main EGL code.
603 */
604 disp->Extensions.KHR_config_attribs = EGL_TRUE;
605
606 _eglComputeVersion(disp);
607 _eglCreateExtensionsString(disp);
608 _eglCreateAPIsString(disp);
609 snprintf(disp->VersionString, sizeof(disp->VersionString),
610 "%d.%d (%s)", disp->Version / 10, disp->Version % 10,
611 disp->Driver->Name);
612 }
613
614 /* Update applications version of major and minor if not NULL */
615 if ((major != NULL) && (minor != NULL)) {
616 *major = disp->Version / 10;
617 *minor = disp->Version % 10;
618 }
619
620 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
621 }
622
623
624 EGLBoolean EGLAPIENTRY
625 eglTerminate(EGLDisplay dpy)
626 {
627 _EGLDisplay *disp = _eglLockDisplay(dpy);
628
629 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
630
631 if (!disp)
632 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
633
634 if (disp->Initialized) {
635 _EGLDriver *drv = disp->Driver;
636
637 drv->API.Terminate(drv, disp);
638 /* do not reset disp->Driver */
639 disp->ClientAPIsString[0] = 0;
640 disp->Initialized = EGL_FALSE;
641 }
642
643 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
644 }
645
646
647 const char * EGLAPIENTRY
648 eglQueryString(EGLDisplay dpy, EGLint name)
649 {
650 _EGLDisplay *disp;
651 _EGLDriver *drv;
652
653 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS) {
654 RETURN_EGL_SUCCESS(NULL, _eglGlobal.ClientExtensionString);
655 }
656
657 disp = _eglLockDisplay(dpy);
658 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, NULL);
659 _EGL_CHECK_DISPLAY(disp, NULL, drv);
660
661 switch (name) {
662 case EGL_VENDOR:
663 RETURN_EGL_SUCCESS(disp, _EGL_VENDOR_STRING);
664 case EGL_VERSION:
665 RETURN_EGL_SUCCESS(disp, disp->VersionString);
666 case EGL_EXTENSIONS:
667 RETURN_EGL_SUCCESS(disp, disp->ExtensionsString);
668 case EGL_CLIENT_APIS:
669 RETURN_EGL_SUCCESS(disp, disp->ClientAPIsString);
670 default:
671 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
672 }
673 }
674
675
676 EGLBoolean EGLAPIENTRY
677 eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
678 EGLint config_size, EGLint *num_config)
679 {
680 _EGLDisplay *disp = _eglLockDisplay(dpy);
681 _EGLDriver *drv;
682 EGLBoolean ret;
683
684 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
685
686 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
687 ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
688
689 RETURN_EGL_EVAL(disp, ret);
690 }
691
692
693 EGLBoolean EGLAPIENTRY
694 eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
695 EGLint config_size, EGLint *num_config)
696 {
697 _EGLDisplay *disp = _eglLockDisplay(dpy);
698 _EGLDriver *drv;
699 EGLBoolean ret;
700
701 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
702
703 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
704 ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
705 config_size, num_config);
706
707 RETURN_EGL_EVAL(disp, ret);
708 }
709
710
711 EGLBoolean EGLAPIENTRY
712 eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
713 EGLint attribute, EGLint *value)
714 {
715 _EGLDisplay *disp = _eglLockDisplay(dpy);
716 _EGLConfig *conf = _eglLookupConfig(config, disp);
717 _EGLDriver *drv;
718 EGLBoolean ret;
719
720 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
721
722 _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
723 ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
724
725 RETURN_EGL_EVAL(disp, ret);
726 }
727
728
729 EGLContext EGLAPIENTRY
730 eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
731 const EGLint *attrib_list)
732 {
733 _EGLDisplay *disp = _eglLockDisplay(dpy);
734 _EGLConfig *conf = _eglLookupConfig(config, disp);
735 _EGLContext *share = _eglLookupContext(share_list, disp);
736 _EGLDriver *drv;
737 _EGLContext *context;
738 EGLContext ret;
739
740 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_CONTEXT);
741
742 _EGL_CHECK_DISPLAY(disp, EGL_NO_CONTEXT, drv);
743
744 if (config != EGL_NO_CONFIG_KHR)
745 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_CONTEXT, drv);
746 else if (!disp->Extensions.KHR_no_config_context)
747 RETURN_EGL_ERROR(disp, EGL_BAD_CONFIG, EGL_NO_CONTEXT);
748
749 if (!share && share_list != EGL_NO_CONTEXT)
750 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
751
752 context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
753 ret = (context) ? _eglLinkContext(context) : EGL_NO_CONTEXT;
754
755 RETURN_EGL_EVAL(disp, ret);
756 }
757
758
759 EGLBoolean EGLAPIENTRY
760 eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
761 {
762 _EGLDisplay *disp = _eglLockDisplay(dpy);
763 _EGLContext *context = _eglLookupContext(ctx, disp);
764 _EGLDriver *drv;
765 EGLBoolean ret;
766
767 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
768
769 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
770 _eglUnlinkContext(context);
771 ret = drv->API.DestroyContext(drv, disp, context);
772
773 RETURN_EGL_EVAL(disp, ret);
774 }
775
776
777 EGLBoolean EGLAPIENTRY
778 eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
779 EGLContext ctx)
780 {
781 _EGLDisplay *disp = _eglLockDisplay(dpy);
782 _EGLContext *context = _eglLookupContext(ctx, disp);
783 _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
784 _EGLSurface *read_surf = _eglLookupSurface(read, disp);
785 _EGLDriver *drv;
786 EGLBoolean ret;
787
788 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
789
790 if (!disp)
791 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
792 drv = disp->Driver;
793
794 /* display is allowed to be uninitialized under certain condition */
795 if (!disp->Initialized) {
796 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
797 ctx != EGL_NO_CONTEXT)
798 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
799 }
800 if (!drv)
801 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
802
803 if (!context && ctx != EGL_NO_CONTEXT)
804 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
805 if (!draw_surf || !read_surf) {
806 /* From the EGL 1.4 (20130211) spec:
807 *
808 * To release the current context without assigning a new one, set ctx
809 * to EGL_NO_CONTEXT and set draw and read to EGL_NO_SURFACE.
810 */
811 if (!disp->Extensions.KHR_surfaceless_context && ctx != EGL_NO_CONTEXT)
812 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
813
814 if ((!draw_surf && draw != EGL_NO_SURFACE) ||
815 (!read_surf && read != EGL_NO_SURFACE))
816 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
817 if (draw_surf || read_surf)
818 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
819 }
820
821 ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
822
823 RETURN_EGL_EVAL(disp, ret);
824 }
825
826
827 EGLBoolean EGLAPIENTRY
828 eglQueryContext(EGLDisplay dpy, EGLContext ctx,
829 EGLint attribute, EGLint *value)
830 {
831 _EGLDisplay *disp = _eglLockDisplay(dpy);
832 _EGLContext *context = _eglLookupContext(ctx, disp);
833 _EGLDriver *drv;
834 EGLBoolean ret;
835
836 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
837
838 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
839 ret = drv->API.QueryContext(drv, disp, context, attribute, value);
840
841 RETURN_EGL_EVAL(disp, ret);
842 }
843
844
845 static EGLSurface
846 _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
847 void *native_window, const EGLint *attrib_list)
848 {
849 _EGLConfig *conf = _eglLookupConfig(config, disp);
850 _EGLDriver *drv;
851 _EGLSurface *surf;
852 EGLSurface ret;
853
854
855 if (native_window == NULL)
856 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
857
858 #ifdef HAVE_SURFACELESS_PLATFORM
859 if (disp->Platform == _EGL_PLATFORM_SURFACELESS) {
860 /* From the EGL_MESA_platform_surfaceless spec (v1):
861 *
862 * eglCreatePlatformWindowSurface fails when called with a <display>
863 * that belongs to the surfaceless platform. It returns
864 * EGL_NO_SURFACE and generates EGL_BAD_NATIVE_WINDOW. The
865 * justification for this unconditional failure is that the
866 * surfaceless platform has no native windows, and therefore the
867 * <native_window> parameter is always invalid.
868 *
869 * This check must occur before checking the EGLConfig, which emits
870 * EGL_BAD_CONFIG.
871 */
872 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
873 }
874 #endif
875
876 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
877
878 surf = drv->API.CreateWindowSurface(drv, disp, conf, native_window,
879 attrib_list);
880 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
881
882 RETURN_EGL_EVAL(disp, ret);
883 }
884
885
886 EGLSurface EGLAPIENTRY
887 eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
888 EGLNativeWindowType window, const EGLint *attrib_list)
889 {
890 _EGLDisplay *disp = _eglLockDisplay(dpy);
891
892 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
893 STATIC_ASSERT(sizeof(void*) == sizeof(window));
894 return _eglCreateWindowSurfaceCommon(disp, config, (void*) window,
895 attrib_list);
896 }
897
898 static void *
899 _fixupNativeWindow(_EGLDisplay *disp, void *native_window)
900 {
901 #ifdef HAVE_X11_PLATFORM
902 if (disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
903 /* The `native_window` parameter for the X11 platform differs between
904 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
905 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
906 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
907 * `Window*`. Convert `Window*` to `Window` because that's what
908 * dri2_x11_create_window_surface() expects.
909 */
910 return (void *)(* (Window*) native_window);
911 }
912 #endif
913 return native_window;
914 }
915
916 static EGLSurface EGLAPIENTRY
917 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
918 void *native_window,
919 const EGLint *attrib_list)
920 {
921 _EGLDisplay *disp = _eglLockDisplay(dpy);
922
923 native_window = _fixupNativeWindow(disp, native_window);
924
925 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
926 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
927 attrib_list);
928 }
929
930
931 EGLSurface EGLAPIENTRY
932 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
933 void *native_window,
934 const EGLAttrib *attrib_list)
935 {
936 _EGLDisplay *disp = _eglLockDisplay(dpy);
937 EGLSurface surface;
938 EGLint *int_attribs;
939
940 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
941
942 int_attribs = _eglConvertAttribsToInt(attrib_list);
943 if (attrib_list && !int_attribs)
944 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
945
946 native_window = _fixupNativeWindow(disp, native_window);
947 surface = _eglCreateWindowSurfaceCommon(disp, config, native_window,
948 int_attribs);
949 free(int_attribs);
950 return surface;
951 }
952
953 static void *
954 _fixupNativePixmap(_EGLDisplay *disp, void *native_pixmap)
955 {
956 #ifdef HAVE_X11_PLATFORM
957 /* The `native_pixmap` parameter for the X11 platform differs between
958 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
959 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
960 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
961 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
962 * dri2_x11_create_pixmap_surface() expects.
963 */
964 if (disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL)
965 return (void *)(* (Pixmap*) native_pixmap);
966 #endif
967 return native_pixmap;
968 }
969
970 static EGLSurface
971 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
972 void *native_pixmap, const EGLint *attrib_list)
973 {
974 _EGLConfig *conf = _eglLookupConfig(config, disp);
975 _EGLDriver *drv;
976 _EGLSurface *surf;
977 EGLSurface ret;
978
979 #if HAVE_SURFACELESS_PLATFORM
980 if (disp->Platform == _EGL_PLATFORM_SURFACELESS) {
981 /* From the EGL_MESA_platform_surfaceless spec (v1):
982 *
983 * [Like eglCreatePlatformWindowSurface,] eglCreatePlatformPixmapSurface
984 * also fails when called with a <display> that belongs to the
985 * surfaceless platform. It returns EGL_NO_SURFACE and generates
986 * EGL_BAD_NATIVE_PIXMAP.
987 *
988 * This check must occur before checking the EGLConfig, which emits
989 * EGL_BAD_CONFIG.
990 */
991 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
992 }
993 #endif
994
995 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
996 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
997 attrib_list);
998 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
999
1000 RETURN_EGL_EVAL(disp, ret);
1001 }
1002
1003
1004 EGLSurface EGLAPIENTRY
1005 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
1006 EGLNativePixmapType pixmap, const EGLint *attrib_list)
1007 {
1008 _EGLDisplay *disp = _eglLockDisplay(dpy);
1009
1010 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1011 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
1012 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
1013 attrib_list);
1014 }
1015
1016 static EGLSurface EGLAPIENTRY
1017 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
1018 void *native_pixmap,
1019 const EGLint *attrib_list)
1020 {
1021 _EGLDisplay *disp = _eglLockDisplay(dpy);
1022
1023 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1024 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1025 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1026 attrib_list);
1027 }
1028
1029
1030 EGLSurface EGLAPIENTRY
1031 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
1032 void *native_pixmap,
1033 const EGLAttrib *attrib_list)
1034 {
1035 _EGLDisplay *disp = _eglLockDisplay(dpy);
1036 EGLSurface surface;
1037 EGLint *int_attribs;
1038
1039 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1040
1041 int_attribs = _eglConvertAttribsToInt(attrib_list);
1042 if (attrib_list && !int_attribs)
1043 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1044
1045 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1046 surface = _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1047 int_attribs);
1048 free(int_attribs);
1049 return surface;
1050 }
1051
1052
1053 EGLSurface EGLAPIENTRY
1054 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
1055 const EGLint *attrib_list)
1056 {
1057 _EGLDisplay *disp = _eglLockDisplay(dpy);
1058 _EGLConfig *conf = _eglLookupConfig(config, disp);
1059 _EGLDriver *drv;
1060 _EGLSurface *surf;
1061 EGLSurface ret;
1062
1063 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1064 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1065
1066 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
1067 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1068
1069 RETURN_EGL_EVAL(disp, ret);
1070 }
1071
1072
1073 EGLBoolean EGLAPIENTRY
1074 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1075 {
1076 _EGLDisplay *disp = _eglLockDisplay(dpy);
1077 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1078 _EGLDriver *drv;
1079 EGLBoolean ret;
1080
1081 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1082 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1083 _eglUnlinkSurface(surf);
1084 ret = drv->API.DestroySurface(drv, disp, surf);
1085
1086 RETURN_EGL_EVAL(disp, ret);
1087 }
1088
1089 EGLBoolean EGLAPIENTRY
1090 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
1091 EGLint attribute, EGLint *value)
1092 {
1093 _EGLDisplay *disp = _eglLockDisplay(dpy);
1094 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1095 _EGLDriver *drv;
1096 EGLBoolean ret;
1097
1098 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1099 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1100 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
1101
1102 RETURN_EGL_EVAL(disp, ret);
1103 }
1104
1105 EGLBoolean EGLAPIENTRY
1106 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
1107 EGLint attribute, EGLint value)
1108 {
1109 _EGLDisplay *disp = _eglLockDisplay(dpy);
1110 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1111 _EGLDriver *drv;
1112 EGLBoolean ret;
1113
1114 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1115 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1116 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
1117
1118 RETURN_EGL_EVAL(disp, ret);
1119 }
1120
1121
1122 EGLBoolean EGLAPIENTRY
1123 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1124 {
1125 _EGLDisplay *disp = _eglLockDisplay(dpy);
1126 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1127 _EGLDriver *drv;
1128 EGLBoolean ret;
1129
1130 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1131 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1132 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
1133
1134 RETURN_EGL_EVAL(disp, ret);
1135 }
1136
1137
1138 EGLBoolean EGLAPIENTRY
1139 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1140 {
1141 _EGLDisplay *disp = _eglLockDisplay(dpy);
1142 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1143 _EGLDriver *drv;
1144 EGLBoolean ret;
1145
1146 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1147 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1148 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
1149
1150 RETURN_EGL_EVAL(disp, ret);
1151 }
1152
1153
1154 EGLBoolean EGLAPIENTRY
1155 eglSwapInterval(EGLDisplay dpy, EGLint interval)
1156 {
1157 _EGLDisplay *disp = _eglLockDisplay(dpy);
1158 _EGLContext *ctx = _eglGetCurrentContext();
1159 _EGLSurface *surf = ctx ? ctx->DrawSurface : NULL;
1160 _EGLDriver *drv;
1161 EGLBoolean ret;
1162
1163 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1164 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1165
1166 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1167 ctx->Resource.Display != disp)
1168 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
1169
1170 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
1171 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1172
1173 ret = drv->API.SwapInterval(drv, disp, surf, interval);
1174
1175 RETURN_EGL_EVAL(disp, ret);
1176 }
1177
1178
1179 EGLBoolean EGLAPIENTRY
1180 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1181 {
1182 _EGLContext *ctx = _eglGetCurrentContext();
1183 _EGLDisplay *disp = _eglLockDisplay(dpy);
1184 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1185 _EGLDriver *drv;
1186 EGLBoolean ret;
1187
1188 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1189 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1190
1191 /* surface must be bound to current context in EGL 1.4 */
1192 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1193 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1194 surf != ctx->DrawSurface)
1195 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1196 #endif
1197
1198 ret = drv->API.SwapBuffers(drv, disp, surf);
1199
1200 RETURN_EGL_EVAL(disp, ret);
1201 }
1202
1203
1204 static EGLBoolean
1205 _eglSwapBuffersWithDamageCommon(_EGLDisplay *disp, _EGLSurface *surf,
1206 EGLint *rects, EGLint n_rects)
1207 {
1208 _EGLContext *ctx = _eglGetCurrentContext();
1209 _EGLDriver *drv;
1210 EGLBoolean ret;
1211
1212 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1213
1214 /* surface must be bound to current context in EGL 1.4 */
1215 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1216 surf != ctx->DrawSurface)
1217 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1218
1219 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1220 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1221
1222 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1223
1224 RETURN_EGL_EVAL(disp, ret);
1225 }
1226
1227 static EGLBoolean EGLAPIENTRY
1228 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1229 EGLint *rects, EGLint n_rects)
1230 {
1231 _EGLDisplay *disp = _eglLockDisplay(dpy);
1232 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1233 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1234 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1235 }
1236
1237 static EGLBoolean EGLAPIENTRY
1238 eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
1239 EGLint *rects, EGLint n_rects)
1240 {
1241 _EGLDisplay *disp = _eglLockDisplay(dpy);
1242 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1243 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1244 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1245 }
1246
1247 EGLBoolean EGLAPIENTRY
1248 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1249 {
1250 _EGLDisplay *disp = _eglLockDisplay(dpy);
1251 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1252 _EGLDriver *drv;
1253 EGLBoolean ret;
1254 void *native_pixmap_ptr;
1255
1256 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1257 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1258 native_pixmap_ptr = (void*) target;
1259
1260 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1261 if (disp->Platform != _eglGetNativePlatform(disp->PlatformDisplay))
1262 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_FALSE);
1263 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1264
1265 RETURN_EGL_EVAL(disp, ret);
1266 }
1267
1268
1269 static EGLBoolean
1270 _eglWaitClientCommon(void)
1271 {
1272 _EGLContext *ctx = _eglGetCurrentContext();
1273 _EGLDisplay *disp;
1274 _EGLDriver *drv;
1275 EGLBoolean ret;
1276
1277 if (!ctx)
1278 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1279
1280 disp = ctx->Resource.Display;
1281 mtx_lock(&disp->Mutex);
1282
1283 /* let bad current context imply bad current surface */
1284 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1285 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1286 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1287
1288 /* a valid current context implies an initialized current display */
1289 assert(disp->Initialized);
1290 drv = disp->Driver;
1291 ret = drv->API.WaitClient(drv, disp, ctx);
1292
1293 RETURN_EGL_EVAL(disp, ret);
1294 }
1295
1296 EGLBoolean EGLAPIENTRY
1297 eglWaitClient(void)
1298 {
1299 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1300 return _eglWaitClientCommon();
1301 }
1302
1303 EGLBoolean EGLAPIENTRY
1304 eglWaitGL(void)
1305 {
1306 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1307 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1308 return _eglWaitClientCommon();
1309 }
1310
1311
1312 EGLBoolean EGLAPIENTRY
1313 eglWaitNative(EGLint engine)
1314 {
1315 _EGLContext *ctx = _eglGetCurrentContext();
1316 _EGLDisplay *disp;
1317 _EGLDriver *drv;
1318 EGLBoolean ret;
1319
1320 if (!ctx)
1321 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1322
1323 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1324
1325 disp = ctx->Resource.Display;
1326 mtx_lock(&disp->Mutex);
1327
1328 /* let bad current context imply bad current surface */
1329 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1330 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1331 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1332
1333 /* a valid current context implies an initialized current display */
1334 assert(disp->Initialized);
1335 drv = disp->Driver;
1336 ret = drv->API.WaitNative(drv, disp, engine);
1337
1338 RETURN_EGL_EVAL(disp, ret);
1339 }
1340
1341
1342 EGLDisplay EGLAPIENTRY
1343 eglGetCurrentDisplay(void)
1344 {
1345 _EGLContext *ctx = _eglGetCurrentContext();
1346 EGLDisplay ret;
1347
1348 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1349
1350 RETURN_EGL_SUCCESS(NULL, ret);
1351 }
1352
1353
1354 EGLContext EGLAPIENTRY
1355 eglGetCurrentContext(void)
1356 {
1357 _EGLContext *ctx = _eglGetCurrentContext();
1358 EGLContext ret;
1359
1360 ret = _eglGetContextHandle(ctx);
1361
1362 RETURN_EGL_SUCCESS(NULL, ret);
1363 }
1364
1365
1366 EGLSurface EGLAPIENTRY
1367 eglGetCurrentSurface(EGLint readdraw)
1368 {
1369 _EGLContext *ctx = _eglGetCurrentContext();
1370 EGLint err = EGL_SUCCESS;
1371 _EGLSurface *surf;
1372 EGLSurface ret;
1373
1374 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_NO_SURFACE);
1375
1376 if (!ctx)
1377 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1378
1379 switch (readdraw) {
1380 case EGL_DRAW:
1381 surf = ctx->DrawSurface;
1382 break;
1383 case EGL_READ:
1384 surf = ctx->ReadSurface;
1385 break;
1386 default:
1387 surf = NULL;
1388 err = EGL_BAD_PARAMETER;
1389 break;
1390 }
1391
1392 ret = _eglGetSurfaceHandle(surf);
1393
1394 RETURN_EGL_ERROR(NULL, err, ret);
1395 }
1396
1397
1398 EGLint EGLAPIENTRY
1399 eglGetError(void)
1400 {
1401 _EGLThreadInfo *t = _eglGetCurrentThread();
1402 EGLint e = t->LastError;
1403 if (!_eglIsCurrentThreadDummy())
1404 t->LastError = EGL_SUCCESS;
1405 return e;
1406 }
1407
1408
1409 /**
1410 ** EGL 1.2
1411 **/
1412
1413 /**
1414 * Specify the client API to use for subsequent calls including:
1415 * eglCreateContext()
1416 * eglGetCurrentContext()
1417 * eglGetCurrentDisplay()
1418 * eglGetCurrentSurface()
1419 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1420 * eglWaitClient()
1421 * eglWaitNative()
1422 * See section 3.7 "Rendering Context" in the EGL specification for details.
1423 */
1424 EGLBoolean EGLAPIENTRY
1425 eglBindAPI(EGLenum api)
1426 {
1427 _EGLThreadInfo *t;
1428
1429 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1430
1431 t = _eglGetCurrentThread();
1432 if (_eglIsCurrentThreadDummy())
1433 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1434
1435 if (!_eglIsApiValid(api))
1436 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1437
1438 t->CurrentAPI = api;
1439
1440 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1441 }
1442
1443
1444 /**
1445 * Return the last value set with eglBindAPI().
1446 */
1447 EGLenum EGLAPIENTRY
1448 eglQueryAPI(void)
1449 {
1450 _EGLThreadInfo *t = _eglGetCurrentThread();
1451 EGLenum ret;
1452
1453 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1454 ret = t->CurrentAPI;
1455
1456 RETURN_EGL_SUCCESS(NULL, ret);
1457 }
1458
1459
1460 EGLSurface EGLAPIENTRY
1461 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1462 EGLClientBuffer buffer, EGLConfig config,
1463 const EGLint *attrib_list)
1464 {
1465 _EGLDisplay *disp = _eglLockDisplay(dpy);
1466 _EGLConfig *conf = _eglLookupConfig(config, disp);
1467 _EGLDriver *drv;
1468 _EGLSurface *surf;
1469 EGLSurface ret;
1470
1471 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1472
1473 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1474
1475 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1476 conf, attrib_list);
1477 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1478
1479 RETURN_EGL_EVAL(disp, ret);
1480 }
1481
1482
1483 EGLBoolean EGLAPIENTRY
1484 eglReleaseThread(void)
1485 {
1486 /* unbind current contexts */
1487 if (!_eglIsCurrentThreadDummy()) {
1488 _EGLThreadInfo *t = _eglGetCurrentThread();
1489 _EGLContext *ctx = t->CurrentContext;
1490
1491 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1492
1493 if (ctx) {
1494 _EGLDisplay *disp = ctx->Resource.Display;
1495 _EGLDriver *drv;
1496
1497 mtx_lock(&disp->Mutex);
1498 drv = disp->Driver;
1499 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1500 mtx_unlock(&disp->Mutex);
1501 }
1502 }
1503
1504 _eglDestroyCurrentThread();
1505
1506 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1507 }
1508
1509
1510 static EGLImage
1511 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1512 EGLClientBuffer buffer, const EGLint *attr_list)
1513 {
1514 _EGLContext *context = _eglLookupContext(ctx, disp);
1515 _EGLDriver *drv;
1516 _EGLImage *img;
1517 EGLImage ret;
1518
1519 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1520 if (!disp->Extensions.KHR_image_base)
1521 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1522 if (!context && ctx != EGL_NO_CONTEXT)
1523 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1524 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1525 * <ctx> must be EGL_NO_CONTEXT..."
1526 */
1527 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1528 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1529
1530 img = drv->API.CreateImageKHR(drv,
1531 disp, context, target, buffer, attr_list);
1532 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1533
1534 RETURN_EGL_EVAL(disp, ret);
1535 }
1536
1537 static EGLImage EGLAPIENTRY
1538 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1539 EGLClientBuffer buffer, const EGLint *attr_list)
1540 {
1541 _EGLDisplay *disp = _eglLockDisplay(dpy);
1542 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1543 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1544 }
1545
1546
1547 EGLImage EGLAPIENTRY
1548 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1549 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1550 {
1551 _EGLDisplay *disp = _eglLockDisplay(dpy);
1552 EGLImage image;
1553 EGLint *int_attribs;
1554
1555 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1556
1557 int_attribs = _eglConvertAttribsToInt(attr_list);
1558 if (attr_list && !int_attribs)
1559 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1560
1561 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1562 free(int_attribs);
1563 return image;
1564 }
1565
1566
1567 EGLBoolean EGLAPIENTRY
1568 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1569 {
1570 _EGLDisplay *disp = _eglLockDisplay(dpy);
1571 _EGLImage *img = _eglLookupImage(image, disp);
1572 _EGLDriver *drv;
1573 EGLBoolean ret;
1574
1575 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1576
1577 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1578 if (!disp->Extensions.KHR_image_base)
1579 RETURN_EGL_EVAL(disp, EGL_FALSE);
1580 if (!img)
1581 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1582
1583 _eglUnlinkImage(img);
1584 ret = drv->API.DestroyImageKHR(drv, disp, img);
1585
1586 RETURN_EGL_EVAL(disp, ret);
1587 }
1588
1589
1590 static EGLSync
1591 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list,
1592 EGLBoolean orig_is_EGLAttrib,
1593 EGLenum invalid_type_error)
1594 {
1595 _EGLContext *ctx = _eglGetCurrentContext();
1596 _EGLDriver *drv;
1597 _EGLSync *sync;
1598 EGLSync ret;
1599
1600 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1601
1602 if (!disp->Extensions.KHR_cl_event2 && orig_is_EGLAttrib) {
1603 /* There exist two EGLAttrib variants of eglCreateSync*:
1604 * eglCreateSync64KHR which requires EGL_KHR_cl_event2, and eglCreateSync
1605 * which requires EGL 1.5. Here we use the presence of EGL_KHR_cl_event2
1606 * support as a proxy for EGL 1.5 support, even though that's not
1607 * entirely correct (though _eglComputeVersion does the same).
1608 *
1609 * The EGL spec provides no guidance on how to handle unsupported
1610 * functions. EGL_BAD_MATCH seems reasonable.
1611 */
1612 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1613 }
1614
1615 /* If type is EGL_SYNC_FENCE and no context is current for the bound API
1616 * (i.e., eglGetCurrentContext returns EGL_NO_CONTEXT ), an EGL_BAD_MATCH
1617 * error is generated.
1618 */
1619 if (!ctx &&
1620 (type == EGL_SYNC_FENCE_KHR || type == EGL_SYNC_NATIVE_FENCE_ANDROID))
1621 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1622
1623 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1624 if (ctx && (ctx->Resource.Display != disp ||
1625 ctx->ClientAPI != EGL_OPENGL_ES_API))
1626 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1627
1628 switch (type) {
1629 case EGL_SYNC_FENCE_KHR:
1630 if (!disp->Extensions.KHR_fence_sync)
1631 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1632 break;
1633 case EGL_SYNC_REUSABLE_KHR:
1634 if (!disp->Extensions.KHR_reusable_sync)
1635 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1636 break;
1637 case EGL_SYNC_CL_EVENT_KHR:
1638 if (!disp->Extensions.KHR_cl_event2)
1639 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1640 break;
1641 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1642 if (!disp->Extensions.ANDROID_native_fence_sync)
1643 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1644 break;
1645 default:
1646 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1647 }
1648
1649 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list);
1650 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1651
1652 RETURN_EGL_EVAL(disp, ret);
1653 }
1654
1655
1656 static EGLSync EGLAPIENTRY
1657 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *int_list)
1658 {
1659 _EGLDisplay *disp = _eglLockDisplay(dpy);
1660 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1661
1662 EGLSync sync;
1663 EGLAttrib *attrib_list;
1664 EGLint err;
1665
1666 if (sizeof(int_list[0]) == sizeof(attrib_list[0])) {
1667 attrib_list = (EGLAttrib *) int_list;
1668 } else {
1669 err = _eglConvertIntsToAttribs(int_list, &attrib_list);
1670 if (err != EGL_SUCCESS)
1671 RETURN_EGL_ERROR(disp, err, EGL_NO_SYNC);
1672 }
1673
1674 sync = _eglCreateSync(disp, type, attrib_list, EGL_FALSE,
1675 EGL_BAD_ATTRIBUTE);
1676
1677 if (sizeof(int_list[0]) != sizeof(attrib_list[0]))
1678 free(attrib_list);
1679
1680 /* Don't double-unlock the display. _eglCreateSync already unlocked it. */
1681 return sync;
1682 }
1683
1684
1685 static EGLSync EGLAPIENTRY
1686 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1687 {
1688 _EGLDisplay *disp = _eglLockDisplay(dpy);
1689 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1690 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1691 EGL_BAD_ATTRIBUTE);
1692 }
1693
1694
1695 EGLSync EGLAPIENTRY
1696 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1697 {
1698 _EGLDisplay *disp = _eglLockDisplay(dpy);
1699 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1700 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1701 EGL_BAD_PARAMETER);
1702 }
1703
1704
1705 EGLBoolean EGLAPIENTRY
1706 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1707 {
1708 _EGLDisplay *disp = _eglLockDisplay(dpy);
1709 _EGLSync *s = _eglLookupSync(sync, disp);
1710 _EGLDriver *drv;
1711 EGLBoolean ret;
1712
1713 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1714
1715 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1716 assert(disp->Extensions.KHR_reusable_sync ||
1717 disp->Extensions.KHR_fence_sync ||
1718 disp->Extensions.ANDROID_native_fence_sync);
1719
1720 _eglUnlinkSync(s);
1721 ret = drv->API.DestroySyncKHR(drv, disp, s);
1722
1723 RETURN_EGL_EVAL(disp, ret);
1724 }
1725
1726
1727 EGLint EGLAPIENTRY
1728 eglClientWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags, EGLTime timeout)
1729 {
1730 _EGLDisplay *disp = _eglLockDisplay(dpy);
1731 _EGLSync *s = _eglLookupSync(sync, disp);
1732 _EGLDriver *drv;
1733 EGLint ret;
1734
1735 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1736
1737 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1738 assert(disp->Extensions.KHR_reusable_sync ||
1739 disp->Extensions.KHR_fence_sync ||
1740 disp->Extensions.ANDROID_native_fence_sync);
1741
1742 if (s->SyncStatus == EGL_SIGNALED_KHR)
1743 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1744
1745 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1746 * unlocked here to allow other threads also to be able to
1747 * go into waiting state.
1748 */
1749
1750 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1751 _eglUnlockDisplay(dpy);
1752
1753 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1754
1755 /*
1756 * 'disp' is already unlocked for reusable sync type,
1757 * so passing 'NULL' to bypass unlocking display.
1758 */
1759 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1760 RETURN_EGL_EVAL(NULL, ret);
1761 else
1762 RETURN_EGL_EVAL(disp, ret);
1763 }
1764
1765
1766 static EGLint
1767 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
1768 {
1769 _EGLContext *ctx = _eglGetCurrentContext();
1770 _EGLDriver *drv;
1771 EGLint ret;
1772
1773 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1774 assert(disp->Extensions.KHR_wait_sync);
1775
1776 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1777 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
1778 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1779
1780 /* the API doesn't allow any flags yet */
1781 if (flags != 0)
1782 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1783
1784 ret = drv->API.WaitSyncKHR(drv, disp, s);
1785
1786 RETURN_EGL_EVAL(disp, ret);
1787 }
1788
1789 static EGLint EGLAPIENTRY
1790 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
1791 {
1792 _EGLDisplay *disp = _eglLockDisplay(dpy);
1793 _EGLSync *s = _eglLookupSync(sync, disp);
1794 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1795 return _eglWaitSyncCommon(disp, s, flags);
1796 }
1797
1798
1799 EGLBoolean EGLAPIENTRY
1800 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
1801 {
1802 /* The KHR version returns EGLint, while the core version returns
1803 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
1804 * EGL_TRUE.
1805 */
1806 _EGLDisplay *disp = _eglLockDisplay(dpy);
1807 _EGLSync *s = _eglLookupSync(sync, disp);
1808 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1809 return _eglWaitSyncCommon(disp, s, flags);
1810 }
1811
1812
1813 static EGLBoolean EGLAPIENTRY
1814 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
1815 {
1816 _EGLDisplay *disp = _eglLockDisplay(dpy);
1817 _EGLSync *s = _eglLookupSync(sync, disp);
1818 _EGLDriver *drv;
1819 EGLBoolean ret;
1820
1821 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1822
1823 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1824 assert(disp->Extensions.KHR_reusable_sync);
1825 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
1826
1827 RETURN_EGL_EVAL(disp, ret);
1828 }
1829
1830
1831 static EGLBoolean
1832 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
1833 {
1834 _EGLDriver *drv;
1835 EGLBoolean ret;
1836
1837 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1838 assert(disp->Extensions.KHR_reusable_sync ||
1839 disp->Extensions.KHR_fence_sync ||
1840 disp->Extensions.ANDROID_native_fence_sync);
1841 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
1842
1843 RETURN_EGL_EVAL(disp, ret);
1844 }
1845
1846 EGLBoolean EGLAPIENTRY
1847 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
1848 {
1849 _EGLDisplay *disp = _eglLockDisplay(dpy);
1850 _EGLSync *s = _eglLookupSync(sync, disp);
1851 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1852 return _eglGetSyncAttribCommon(disp, s, attribute, value);
1853 }
1854
1855
1856 static EGLBoolean EGLAPIENTRY
1857 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
1858 {
1859 _EGLDisplay *disp = _eglLockDisplay(dpy);
1860 _EGLSync *s = _eglLookupSync(sync, disp);
1861 EGLAttrib attrib;
1862 EGLBoolean result;
1863
1864 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1865
1866 if (!value)
1867 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1868
1869 attrib = *value;
1870 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
1871
1872 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
1873 *
1874 * If any error occurs, <*value> is not modified.
1875 */
1876 if (result == EGL_FALSE)
1877 return result;
1878
1879 *value = attrib;
1880 return result;
1881 }
1882
1883 static EGLint EGLAPIENTRY
1884 eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSync sync)
1885 {
1886 _EGLDisplay *disp = _eglLockDisplay(dpy);
1887 _EGLSync *s = _eglLookupSync(sync, disp);
1888 _EGLDriver *drv;
1889 EGLBoolean ret;
1890
1891 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1892
1893 /* the spec doesn't seem to specify what happens if the fence
1894 * type is not EGL_SYNC_NATIVE_FENCE_ANDROID, but this seems
1895 * sensible:
1896 */
1897 if (!(s && (s->Type == EGL_SYNC_NATIVE_FENCE_ANDROID)))
1898 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_NATIVE_FENCE_FD_ANDROID);
1899
1900 _EGL_CHECK_SYNC(disp, s, EGL_NO_NATIVE_FENCE_FD_ANDROID, drv);
1901 assert(disp->Extensions.ANDROID_native_fence_sync);
1902 ret = drv->API.DupNativeFenceFDANDROID(drv, disp, s);
1903
1904 RETURN_EGL_EVAL(disp, ret);
1905 }
1906
1907 static EGLBoolean EGLAPIENTRY
1908 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
1909 EGLint numRects, const EGLint *rects)
1910 {
1911 _EGLContext *ctx = _eglGetCurrentContext();
1912 _EGLDisplay *disp = _eglLockDisplay(dpy);
1913 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1914 _EGLDriver *drv;
1915 EGLBoolean ret;
1916
1917 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1918
1919 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1920
1921 if (!disp->Extensions.NOK_swap_region)
1922 RETURN_EGL_EVAL(disp, EGL_FALSE);
1923
1924 /* surface must be bound to current context in EGL 1.4 */
1925 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1926 surf != ctx->DrawSurface)
1927 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1928
1929 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
1930
1931 RETURN_EGL_EVAL(disp, ret);
1932 }
1933
1934
1935 static EGLImage EGLAPIENTRY
1936 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
1937 {
1938 _EGLDisplay *disp = _eglLockDisplay(dpy);
1939 _EGLDriver *drv;
1940 _EGLImage *img;
1941 EGLImage ret;
1942
1943 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1944
1945 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1946 if (!disp->Extensions.MESA_drm_image)
1947 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1948
1949 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
1950 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1951
1952 RETURN_EGL_EVAL(disp, ret);
1953 }
1954
1955 static EGLBoolean EGLAPIENTRY
1956 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
1957 EGLint *name, EGLint *handle, EGLint *stride)
1958 {
1959 _EGLDisplay *disp = _eglLockDisplay(dpy);
1960 _EGLImage *img = _eglLookupImage(image, disp);
1961 _EGLDriver *drv;
1962 EGLBoolean ret;
1963
1964 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1965
1966 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1967 assert(disp->Extensions.MESA_drm_image);
1968
1969 if (!img)
1970 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1971
1972 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
1973
1974 RETURN_EGL_EVAL(disp, ret);
1975 }
1976
1977
1978 struct wl_display;
1979
1980 static EGLBoolean EGLAPIENTRY
1981 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1982 {
1983 _EGLDisplay *disp = _eglLockDisplay(dpy);
1984 _EGLDriver *drv;
1985 EGLBoolean ret;
1986
1987 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1988
1989 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1990 assert(disp->Extensions.WL_bind_wayland_display);
1991
1992 if (!display)
1993 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1994
1995 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
1996
1997 RETURN_EGL_EVAL(disp, ret);
1998 }
1999
2000 static EGLBoolean EGLAPIENTRY
2001 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2002 {
2003 _EGLDisplay *disp = _eglLockDisplay(dpy);
2004 _EGLDriver *drv;
2005 EGLBoolean ret;
2006
2007 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2008
2009 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2010 assert(disp->Extensions.WL_bind_wayland_display);
2011
2012 if (!display)
2013 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2014
2015 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
2016
2017 RETURN_EGL_EVAL(disp, ret);
2018 }
2019
2020 static EGLBoolean EGLAPIENTRY
2021 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
2022 EGLint attribute, EGLint *value)
2023 {
2024 _EGLDisplay *disp = _eglLockDisplay(dpy);
2025 _EGLDriver *drv;
2026 EGLBoolean ret;
2027
2028 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2029
2030 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2031 assert(disp->Extensions.WL_bind_wayland_display);
2032
2033 if (!buffer)
2034 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2035
2036 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
2037
2038 RETURN_EGL_EVAL(disp, ret);
2039 }
2040
2041
2042 static struct wl_buffer * EGLAPIENTRY
2043 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
2044 {
2045 _EGLDisplay *disp = _eglLockDisplay(dpy);
2046 _EGLImage *img;
2047 _EGLDriver *drv;
2048 struct wl_buffer *ret;
2049
2050 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2051
2052 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2053 assert(disp->Extensions.WL_create_wayland_buffer_from_image);
2054
2055 img = _eglLookupImage(image, disp);
2056
2057 if (!img)
2058 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
2059
2060 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
2061
2062 RETURN_EGL_EVAL(disp, ret);
2063 }
2064
2065 static EGLBoolean EGLAPIENTRY
2066 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
2067 EGLint x, EGLint y, EGLint width, EGLint height)
2068 {
2069 _EGLDisplay *disp = _eglLockDisplay(dpy);
2070 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2071 _EGLDriver *drv;
2072 EGLBoolean ret;
2073
2074 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2075
2076 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2077
2078 if (!disp->Extensions.NV_post_sub_buffer)
2079 RETURN_EGL_EVAL(disp, EGL_FALSE);
2080
2081 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
2082
2083 RETURN_EGL_EVAL(disp, ret);
2084 }
2085
2086 static EGLBoolean EGLAPIENTRY
2087 eglGetSyncValuesCHROMIUM(EGLDisplay display, EGLSurface surface,
2088 EGLuint64KHR *ust, EGLuint64KHR *msc,
2089 EGLuint64KHR *sbc)
2090 {
2091 _EGLDisplay *disp = _eglLockDisplay(display);
2092 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2093 _EGLDriver *drv;
2094 EGLBoolean ret;
2095
2096 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2097
2098 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2099 if (!disp->Extensions.CHROMIUM_sync_control)
2100 RETURN_EGL_EVAL(disp, EGL_FALSE);
2101
2102 if (!ust || !msc || !sbc)
2103 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2104
2105 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
2106
2107 RETURN_EGL_EVAL(disp, ret);
2108 }
2109
2110 static EGLBoolean EGLAPIENTRY
2111 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
2112 EGLint *fourcc, EGLint *nplanes,
2113 EGLuint64KHR *modifiers)
2114 {
2115 _EGLDisplay *disp = _eglLockDisplay(dpy);
2116 _EGLImage *img = _eglLookupImage(image, disp);
2117 _EGLDriver *drv;
2118 EGLBoolean ret;
2119
2120 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2121
2122 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2123 assert(disp->Extensions.MESA_image_dma_buf_export);
2124
2125 if (!img)
2126 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2127
2128 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
2129 modifiers);
2130
2131 RETURN_EGL_EVAL(disp, ret);
2132 }
2133
2134 static EGLBoolean EGLAPIENTRY
2135 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
2136 int *fds, EGLint *strides, EGLint *offsets)
2137 {
2138 _EGLDisplay *disp = _eglLockDisplay(dpy);
2139 _EGLImage *img = _eglLookupImage(image, disp);
2140 _EGLDriver *drv;
2141 EGLBoolean ret;
2142
2143 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2144
2145 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2146 assert(disp->Extensions.MESA_image_dma_buf_export);
2147
2148 if (!img)
2149 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2150
2151 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
2152
2153 RETURN_EGL_EVAL(disp, ret);
2154 }
2155
2156 static EGLint EGLAPIENTRY
2157 eglLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object,
2158 EGLLabelKHR label)
2159 {
2160 _EGLDisplay *disp = NULL;
2161 _EGLResourceType type;
2162
2163 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2164
2165 if (objectType == EGL_OBJECT_THREAD_KHR) {
2166 _EGLThreadInfo *t = _eglGetCurrentThread();
2167
2168 if (!_eglIsCurrentThreadDummy()) {
2169 t->Label = label;
2170 return EGL_SUCCESS;
2171 }
2172
2173 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_BAD_ALLOC);
2174 }
2175
2176 disp = _eglLockDisplay(dpy);
2177 if (disp == NULL)
2178 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_BAD_DISPLAY);
2179
2180 if (objectType == EGL_OBJECT_DISPLAY_KHR) {
2181 if (dpy != (EGLDisplay) object)
2182 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2183
2184 disp->Label = label;
2185 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2186 }
2187
2188 switch (objectType) {
2189 case EGL_OBJECT_CONTEXT_KHR:
2190 type = _EGL_RESOURCE_CONTEXT;
2191 break;
2192 case EGL_OBJECT_SURFACE_KHR:
2193 type = _EGL_RESOURCE_SURFACE;
2194 break;
2195 case EGL_OBJECT_IMAGE_KHR:
2196 type = _EGL_RESOURCE_IMAGE;
2197 break;
2198 case EGL_OBJECT_SYNC_KHR:
2199 type = _EGL_RESOURCE_SYNC;
2200 break;
2201 case EGL_OBJECT_STREAM_KHR:
2202 default:
2203 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2204 }
2205
2206 if (_eglCheckResource(object, type, disp)) {
2207 _EGLResource *res = (_EGLResource *) object;
2208
2209 res->Label = label;
2210 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2211 }
2212
2213 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2214 }
2215
2216 static EGLBoolean
2217 _validDebugMessageLevel(EGLAttrib level)
2218 {
2219 return (level >= EGL_DEBUG_MSG_CRITICAL_KHR &&
2220 level <= EGL_DEBUG_MSG_INFO_KHR);
2221 }
2222
2223 static EGLint EGLAPIENTRY
2224 eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
2225 const EGLAttrib *attrib_list)
2226 {
2227 unsigned int newEnabled;
2228
2229 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2230
2231 mtx_lock(_eglGlobal.Mutex);
2232
2233 newEnabled = _eglGlobal.debugTypesEnabled;
2234 if (attrib_list != NULL) {
2235 int i;
2236
2237 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
2238 if (_validDebugMessageLevel(attrib_list[i])) {
2239 if (attrib_list[i + 1])
2240 newEnabled |= DebugBitFromType(attrib_list[i]);
2241 else
2242 newEnabled &= ~DebugBitFromType(attrib_list[i]);
2243 continue;
2244 }
2245
2246 // On error, set the last error code, call the current
2247 // debug callback, and return the error code.
2248 mtx_unlock(_eglGlobal.Mutex);
2249 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2250 "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
2251 return EGL_BAD_ATTRIBUTE;
2252 }
2253 }
2254
2255 if (callback != NULL) {
2256 _eglGlobal.debugCallback = callback;
2257 _eglGlobal.debugTypesEnabled = newEnabled;
2258 } else {
2259 _eglGlobal.debugCallback = NULL;
2260 _eglGlobal.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR;
2261 }
2262
2263 mtx_unlock(_eglGlobal.Mutex);
2264 return EGL_SUCCESS;
2265 }
2266
2267 static EGLBoolean EGLAPIENTRY
2268 eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
2269 {
2270 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2271
2272 mtx_lock(_eglGlobal.Mutex);
2273
2274 do {
2275 if (_validDebugMessageLevel(attribute)) {
2276 if (_eglGlobal.debugTypesEnabled & DebugBitFromType(attribute))
2277 *value = EGL_TRUE;
2278 else
2279 *value = EGL_FALSE;
2280 break;
2281 }
2282
2283 if (attribute == EGL_DEBUG_CALLBACK_KHR) {
2284 *value = (EGLAttrib) _eglGlobal.debugCallback;
2285 break;
2286 }
2287
2288 mtx_unlock(_eglGlobal.Mutex);
2289 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2290 "Invalid attribute 0x%04lx", (unsigned long) attribute);
2291 return EGL_FALSE;
2292 } while (0);
2293
2294 mtx_unlock(_eglGlobal.Mutex);
2295 return EGL_TRUE;
2296 }
2297
2298 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
2299 eglGetProcAddress(const char *procname)
2300 {
2301 static const struct {
2302 const char *name;
2303 _EGLProc function;
2304 } egl_functions[] = {
2305 /* core functions queryable in the presence of
2306 * EGL_KHR_get_all_proc_addresses or EGL 1.5
2307 */
2308 /* alphabetical order */
2309 { "eglBindAPI", (_EGLProc) eglBindAPI },
2310 { "eglBindTexImage", (_EGLProc) eglBindTexImage },
2311 { "eglChooseConfig", (_EGLProc) eglChooseConfig },
2312 { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
2313 { "eglCreateContext", (_EGLProc) eglCreateContext },
2314 { "eglCreatePbufferFromClientBuffer", (_EGLProc) eglCreatePbufferFromClientBuffer },
2315 { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
2316 { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
2317 { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
2318 { "eglDestroyContext", (_EGLProc) eglDestroyContext },
2319 { "eglDestroySurface", (_EGLProc) eglDestroySurface },
2320 { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
2321 { "eglGetConfigs", (_EGLProc) eglGetConfigs },
2322 { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
2323 { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
2324 { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
2325 { "eglGetDisplay", (_EGLProc) eglGetDisplay },
2326 { "eglGetError", (_EGLProc) eglGetError },
2327 { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
2328 { "eglInitialize", (_EGLProc) eglInitialize },
2329 { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
2330 { "eglQueryAPI", (_EGLProc) eglQueryAPI },
2331 { "eglQueryContext", (_EGLProc) eglQueryContext },
2332 { "eglQueryString", (_EGLProc) eglQueryString },
2333 { "eglQuerySurface", (_EGLProc) eglQuerySurface },
2334 { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
2335 { "eglReleaseThread", (_EGLProc) eglReleaseThread },
2336 { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
2337 { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
2338 { "eglSwapInterval", (_EGLProc) eglSwapInterval },
2339 { "eglTerminate", (_EGLProc) eglTerminate },
2340 { "eglWaitClient", (_EGLProc) eglWaitClient },
2341 { "eglWaitGL", (_EGLProc) eglWaitGL },
2342 { "eglWaitNative", (_EGLProc) eglWaitNative },
2343 { "eglCreateSync", (_EGLProc) eglCreateSync },
2344 { "eglDestroySync", (_EGLProc) eglDestroySync },
2345 { "eglClientWaitSync", (_EGLProc) eglClientWaitSync },
2346 { "eglGetSyncAttrib", (_EGLProc) eglGetSyncAttrib },
2347 { "eglWaitSync", (_EGLProc) eglWaitSync },
2348 { "eglCreateImage", (_EGLProc) eglCreateImage },
2349 { "eglDestroyImage", (_EGLProc) eglDestroyImage },
2350 { "eglGetPlatformDisplay", (_EGLProc) eglGetPlatformDisplay },
2351 { "eglCreatePlatformWindowSurface", (_EGLProc) eglCreatePlatformWindowSurface },
2352 { "eglCreatePlatformPixmapSurface", (_EGLProc) eglCreatePlatformPixmapSurface },
2353 { "eglCreateImageKHR", (_EGLProc) eglCreateImageKHR },
2354 { "eglDestroyImageKHR", (_EGLProc) eglDestroyImage },
2355 { "eglCreateSyncKHR", (_EGLProc) eglCreateSyncKHR },
2356 { "eglCreateSync64KHR", (_EGLProc) eglCreateSync64KHR },
2357 { "eglDestroySyncKHR", (_EGLProc) eglDestroySync },
2358 { "eglClientWaitSyncKHR", (_EGLProc) eglClientWaitSync },
2359 { "eglWaitSyncKHR", (_EGLProc) eglWaitSyncKHR },
2360 { "eglSignalSyncKHR", (_EGLProc) eglSignalSyncKHR },
2361 { "eglGetSyncAttribKHR", (_EGLProc) eglGetSyncAttribKHR },
2362 { "eglSwapBuffersRegionNOK", (_EGLProc) eglSwapBuffersRegionNOK },
2363 { "eglCreateDRMImageMESA", (_EGLProc) eglCreateDRMImageMESA },
2364 { "eglExportDRMImageMESA", (_EGLProc) eglExportDRMImageMESA },
2365 { "eglBindWaylandDisplayWL", (_EGLProc) eglBindWaylandDisplayWL },
2366 { "eglUnbindWaylandDisplayWL", (_EGLProc) eglUnbindWaylandDisplayWL },
2367 { "eglQueryWaylandBufferWL", (_EGLProc) eglQueryWaylandBufferWL },
2368 { "eglCreateWaylandBufferFromImageWL", (_EGLProc) eglCreateWaylandBufferFromImageWL },
2369 { "eglPostSubBufferNV", (_EGLProc) eglPostSubBufferNV },
2370 { "eglSwapBuffersWithDamageEXT", (_EGLProc) eglSwapBuffersWithDamageEXT },
2371 { "eglSwapBuffersWithDamageKHR", (_EGLProc) eglSwapBuffersWithDamageKHR },
2372 { "eglGetPlatformDisplayEXT", (_EGLProc) eglGetPlatformDisplayEXT },
2373 { "eglCreatePlatformWindowSurfaceEXT", (_EGLProc) eglCreatePlatformWindowSurfaceEXT },
2374 { "eglCreatePlatformPixmapSurfaceEXT", (_EGLProc) eglCreatePlatformPixmapSurfaceEXT },
2375 { "eglGetSyncValuesCHROMIUM", (_EGLProc) eglGetSyncValuesCHROMIUM },
2376 { "eglExportDMABUFImageQueryMESA", (_EGLProc) eglExportDMABUFImageQueryMESA },
2377 { "eglExportDMABUFImageMESA", (_EGLProc) eglExportDMABUFImageMESA },
2378 { "eglLabelObjectKHR", (_EGLProc) eglLabelObjectKHR },
2379 { "eglDebugMessageControlKHR", (_EGLProc) eglDebugMessageControlKHR },
2380 { "eglQueryDebugKHR", (_EGLProc) eglQueryDebugKHR },
2381 { "eglDupNativeFenceFDANDROID", (_EGLProc) eglDupNativeFenceFDANDROID },
2382 { NULL, NULL }
2383 };
2384 EGLint i;
2385 _EGLProc ret;
2386
2387 if (!procname)
2388 RETURN_EGL_SUCCESS(NULL, NULL);
2389
2390 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2391
2392 ret = NULL;
2393 if (strncmp(procname, "egl", 3) == 0) {
2394 for (i = 0; egl_functions[i].name; i++) {
2395 if (strcmp(egl_functions[i].name, procname) == 0) {
2396 ret = egl_functions[i].function;
2397 break;
2398 }
2399 }
2400 }
2401 if (!ret)
2402 ret = _eglGetDriverProc(procname);
2403
2404 RETURN_EGL_SUCCESS(NULL, ret);
2405 }
2406
2407 static int
2408 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
2409 _EGLDisplay **disp, _EGLDriver **drv,
2410 _EGLContext **ctx)
2411 {
2412
2413 *disp = _eglLockDisplay(dpy);
2414 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
2415 if (*disp)
2416 _eglUnlockDisplay(*disp);
2417 return MESA_GLINTEROP_INVALID_DISPLAY;
2418 }
2419
2420 *drv = (*disp)->Driver;
2421
2422 *ctx = _eglLookupContext(context, *disp);
2423 if (!*ctx ||
2424 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
2425 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
2426 _eglUnlockDisplay(*disp);
2427 return MESA_GLINTEROP_INVALID_CONTEXT;
2428 }
2429
2430 return MESA_GLINTEROP_SUCCESS;
2431 }
2432
2433 PUBLIC int
2434 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
2435 struct mesa_glinterop_device_info *out)
2436 {
2437 _EGLDisplay *disp;
2438 _EGLDriver *drv;
2439 _EGLContext *ctx;
2440 int ret;
2441
2442 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2443 if (ret != MESA_GLINTEROP_SUCCESS)
2444 return ret;
2445
2446 if (drv->API.GLInteropQueryDeviceInfo)
2447 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
2448 else
2449 ret = MESA_GLINTEROP_UNSUPPORTED;
2450
2451 _eglUnlockDisplay(disp);
2452 return ret;
2453 }
2454
2455 PUBLIC int
2456 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2457 struct mesa_glinterop_export_in *in,
2458 struct mesa_glinterop_export_out *out)
2459 {
2460 _EGLDisplay *disp;
2461 _EGLDriver *drv;
2462 _EGLContext *ctx;
2463 int ret;
2464
2465 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2466 if (ret != MESA_GLINTEROP_SUCCESS)
2467 return ret;
2468
2469 if (drv->API.GLInteropExportObject)
2470 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
2471 else
2472 ret = MESA_GLINTEROP_UNSUPPORTED;
2473
2474 _eglUnlockDisplay(disp);
2475 return ret;
2476 }