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