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