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