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