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