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