Added few more stubs so that control reaches to DestroyDevice().
[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 && disp->Driver->SwapInterval)
1291 ret = disp->Driver->SwapInterval(disp, surf, interval);
1292 else
1293 ret = EGL_TRUE;
1294
1295 if (ret)
1296 surf->SwapInterval = interval;
1297
1298 RETURN_EGL_EVAL(disp, ret);
1299 }
1300
1301
1302 EGLBoolean EGLAPIENTRY
1303 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1304 {
1305 _EGLContext *ctx = _eglGetCurrentContext();
1306 _EGLDisplay *disp = _eglLockDisplay(dpy);
1307 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1308 EGLBoolean ret;
1309
1310 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1311 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
1312
1313 /* surface must be bound to current context in EGL 1.4 */
1314 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1315 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1316 surf != ctx->DrawSurface)
1317 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1318 #endif
1319
1320 if (surf->Type != EGL_WINDOW_BIT)
1321 RETURN_EGL_EVAL(disp, EGL_TRUE);
1322
1323 /* From the EGL 1.5 spec:
1324 *
1325 * If eglSwapBuffers is called and the native window associated with
1326 * surface is no longer valid, an EGL_BAD_NATIVE_WINDOW error is
1327 * generated.
1328 */
1329 if (surf->Lost)
1330 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
1331
1332 ret = disp->Driver->SwapBuffers(disp, surf);
1333
1334 /* EGL_KHR_partial_update
1335 * Frame boundary successfully reached,
1336 * reset damage region and reset BufferAgeRead
1337 */
1338 if (ret) {
1339 surf->SetDamageRegionCalled = EGL_FALSE;
1340 surf->BufferAgeRead = EGL_FALSE;
1341 }
1342
1343 RETURN_EGL_EVAL(disp, ret);
1344 }
1345
1346
1347 static EGLBoolean
1348 _eglSwapBuffersWithDamageCommon(_EGLDisplay *disp, _EGLSurface *surf,
1349 const EGLint *rects, EGLint n_rects)
1350 {
1351 _EGLContext *ctx = _eglGetCurrentContext();
1352 EGLBoolean ret;
1353
1354 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
1355
1356 /* surface must be bound to current context in EGL 1.4 */
1357 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1358 surf != ctx->DrawSurface)
1359 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1360
1361 if (surf->Type != EGL_WINDOW_BIT)
1362 RETURN_EGL_EVAL(disp, EGL_TRUE);
1363
1364 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1365 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1366
1367 ret = disp->Driver->SwapBuffersWithDamageEXT(disp, surf, rects, n_rects);
1368
1369 /* EGL_KHR_partial_update
1370 * Frame boundary successfully reached,
1371 * reset damage region and reset BufferAgeRead
1372 */
1373 if (ret) {
1374 surf->SetDamageRegionCalled = EGL_FALSE;
1375 surf->BufferAgeRead = EGL_FALSE;
1376 }
1377
1378 RETURN_EGL_EVAL(disp, ret);
1379 }
1380
1381 static EGLBoolean EGLAPIENTRY
1382 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1383 const EGLint *rects, EGLint n_rects)
1384 {
1385 _EGLDisplay *disp = _eglLockDisplay(dpy);
1386 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1387 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1388 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1389 }
1390
1391 static EGLBoolean EGLAPIENTRY
1392 eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
1393 const EGLint *rects, EGLint n_rects)
1394 {
1395 _EGLDisplay *disp = _eglLockDisplay(dpy);
1396 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1397 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1398 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1399 }
1400
1401 /**
1402 * Clamp the rectangles so that they lie within the surface.
1403 */
1404
1405 static void
1406 _eglSetDamageRegionKHRClampRects(_EGLSurface* surf,
1407 EGLint *rects, EGLint n_rects)
1408 {
1409 EGLint i;
1410 EGLint surf_height = surf->Height;
1411 EGLint surf_width = surf->Width;
1412
1413 for (i = 0; i < (4 * n_rects); i += 4) {
1414 EGLint x1, y1, x2, y2;
1415 x1 = rects[i];
1416 y1 = rects[i + 1];
1417 x2 = rects[i + 2] + x1;
1418 y2 = rects[i + 3] + y1;
1419
1420 rects[i] = CLAMP(x1, 0, surf_width);
1421 rects[i + 1] = CLAMP(y1, 0, surf_height);
1422 rects[i + 2] = CLAMP(x2, 0, surf_width) - rects[i];
1423 rects[i + 3] = CLAMP(y2, 0, surf_height) - rects[i + 1];
1424 }
1425 }
1426
1427 static EGLBoolean EGLAPIENTRY
1428 eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1429 EGLint *rects, EGLint n_rects)
1430 {
1431 _EGLDisplay *disp = _eglLockDisplay(dpy);
1432 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1433 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1434 _EGLContext *ctx = _eglGetCurrentContext();
1435 EGLBoolean ret;
1436 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
1437
1438 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1439 surf->Type != EGL_WINDOW_BIT ||
1440 ctx->DrawSurface != surf ||
1441 surf->SwapBehavior != EGL_BUFFER_DESTROYED)
1442 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1443
1444 /* If the damage region is already set or
1445 * buffer age is not queried between
1446 * frame boundaries, throw bad access error
1447 */
1448
1449 if (surf->SetDamageRegionCalled || !surf->BufferAgeRead)
1450 RETURN_EGL_ERROR(disp, EGL_BAD_ACCESS, EGL_FALSE);
1451
1452 _eglSetDamageRegionKHRClampRects(surf, rects, n_rects);
1453 ret = disp->Driver->SetDamageRegion(disp, surf, rects, n_rects);
1454
1455 if (ret)
1456 surf->SetDamageRegionCalled = EGL_TRUE;
1457
1458 RETURN_EGL_EVAL(disp, ret);
1459 }
1460
1461 EGLBoolean EGLAPIENTRY
1462 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1463 {
1464 _EGLDisplay *disp = _eglLockDisplay(dpy);
1465 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1466 EGLBoolean ret;
1467 void *native_pixmap_ptr;
1468
1469 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1470 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1471 native_pixmap_ptr = (void*) target;
1472
1473 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
1474 ret = disp->Driver->CopyBuffers(disp, surf, native_pixmap_ptr);
1475
1476 RETURN_EGL_EVAL(disp, ret);
1477 }
1478
1479
1480 static EGLBoolean
1481 _eglWaitClientCommon(void)
1482 {
1483 _EGLContext *ctx = _eglGetCurrentContext();
1484 _EGLDisplay *disp;
1485 EGLBoolean ret;
1486
1487 if (!ctx)
1488 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1489
1490 disp = ctx->Resource.Display;
1491 mtx_lock(&disp->Mutex);
1492
1493 /* let bad current context imply bad current surface */
1494 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1495 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1496 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1497
1498 /* a valid current context implies an initialized current display */
1499 assert(disp->Initialized);
1500 ret = disp->Driver->WaitClient(disp, ctx);
1501
1502 RETURN_EGL_EVAL(disp, ret);
1503 }
1504
1505 EGLBoolean EGLAPIENTRY
1506 eglWaitClient(void)
1507 {
1508 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1509 return _eglWaitClientCommon();
1510 }
1511
1512 EGLBoolean EGLAPIENTRY
1513 eglWaitGL(void)
1514 {
1515 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1516 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1517 return _eglWaitClientCommon();
1518 }
1519
1520
1521 EGLBoolean EGLAPIENTRY
1522 eglWaitNative(EGLint engine)
1523 {
1524 _EGLContext *ctx = _eglGetCurrentContext();
1525 _EGLDisplay *disp;
1526 EGLBoolean ret;
1527
1528 if (!ctx)
1529 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1530
1531 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1532
1533 disp = ctx->Resource.Display;
1534 mtx_lock(&disp->Mutex);
1535
1536 /* let bad current context imply bad current surface */
1537 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1538 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1539 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1540
1541 /* a valid current context implies an initialized current display */
1542 assert(disp->Initialized);
1543 ret = disp->Driver->WaitNative(engine);
1544
1545 RETURN_EGL_EVAL(disp, ret);
1546 }
1547
1548
1549 EGLDisplay EGLAPIENTRY
1550 eglGetCurrentDisplay(void)
1551 {
1552 _EGLContext *ctx = _eglGetCurrentContext();
1553 EGLDisplay ret;
1554
1555 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1556
1557 RETURN_EGL_SUCCESS(NULL, ret);
1558 }
1559
1560
1561 EGLContext EGLAPIENTRY
1562 eglGetCurrentContext(void)
1563 {
1564 _EGLContext *ctx = _eglGetCurrentContext();
1565 EGLContext ret;
1566
1567 ret = _eglGetContextHandle(ctx);
1568
1569 RETURN_EGL_SUCCESS(NULL, ret);
1570 }
1571
1572
1573 EGLSurface EGLAPIENTRY
1574 eglGetCurrentSurface(EGLint readdraw)
1575 {
1576 _EGLContext *ctx = _eglGetCurrentContext();
1577 EGLint err = EGL_SUCCESS;
1578 _EGLSurface *surf;
1579 EGLSurface ret;
1580
1581 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_NO_SURFACE);
1582
1583 if (!ctx)
1584 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1585
1586 switch (readdraw) {
1587 case EGL_DRAW:
1588 surf = ctx->DrawSurface;
1589 break;
1590 case EGL_READ:
1591 surf = ctx->ReadSurface;
1592 break;
1593 default:
1594 surf = NULL;
1595 err = EGL_BAD_PARAMETER;
1596 break;
1597 }
1598
1599 ret = _eglGetSurfaceHandle(surf);
1600
1601 RETURN_EGL_ERROR(NULL, err, ret);
1602 }
1603
1604
1605 EGLint EGLAPIENTRY
1606 eglGetError(void)
1607 {
1608 _EGLThreadInfo *t = _eglGetCurrentThread();
1609 EGLint e = t->LastError;
1610 if (!_eglIsCurrentThreadDummy())
1611 t->LastError = EGL_SUCCESS;
1612 return e;
1613 }
1614
1615
1616 /**
1617 ** EGL 1.2
1618 **/
1619
1620 /**
1621 * Specify the client API to use for subsequent calls including:
1622 * eglCreateContext()
1623 * eglGetCurrentContext()
1624 * eglGetCurrentDisplay()
1625 * eglGetCurrentSurface()
1626 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1627 * eglWaitClient()
1628 * eglWaitNative()
1629 * See section 3.7 "Rendering Context" in the EGL specification for details.
1630 */
1631 EGLBoolean EGLAPIENTRY
1632 eglBindAPI(EGLenum api)
1633 {
1634 _EGLThreadInfo *t;
1635
1636 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1637
1638 t = _eglGetCurrentThread();
1639 if (_eglIsCurrentThreadDummy())
1640 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1641
1642 if (!_eglIsApiValid(api))
1643 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1644
1645 t->CurrentAPI = api;
1646
1647 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1648 }
1649
1650
1651 /**
1652 * Return the last value set with eglBindAPI().
1653 */
1654 EGLenum EGLAPIENTRY
1655 eglQueryAPI(void)
1656 {
1657 _EGLThreadInfo *t = _eglGetCurrentThread();
1658 EGLenum ret;
1659
1660 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1661 ret = t->CurrentAPI;
1662
1663 RETURN_EGL_SUCCESS(NULL, ret);
1664 }
1665
1666
1667 EGLSurface EGLAPIENTRY
1668 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1669 EGLClientBuffer buffer, EGLConfig config,
1670 const EGLint *attrib_list)
1671 {
1672 _EGLDisplay *disp = _eglLockDisplay(dpy);
1673 _EGLConfig *conf = _eglLookupConfig(config, disp);
1674
1675 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1676
1677 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE);
1678
1679 /* OpenVG is not supported */
1680 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1681 }
1682
1683
1684 EGLBoolean EGLAPIENTRY
1685 eglReleaseThread(void)
1686 {
1687 /* unbind current contexts */
1688 if (!_eglIsCurrentThreadDummy()) {
1689 _EGLThreadInfo *t = _eglGetCurrentThread();
1690 _EGLContext *ctx = t->CurrentContext;
1691
1692 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1693
1694 if (ctx) {
1695 _EGLDisplay *disp = ctx->Resource.Display;
1696
1697 mtx_lock(&disp->Mutex);
1698 (void) disp->Driver->MakeCurrent(disp, NULL, NULL, NULL);
1699 mtx_unlock(&disp->Mutex);
1700 }
1701 }
1702
1703 _eglDestroyCurrentThread();
1704
1705 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1706 }
1707
1708
1709 static EGLImage
1710 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1711 EGLClientBuffer buffer, const EGLint *attr_list)
1712 {
1713 _EGLContext *context = _eglLookupContext(ctx, disp);
1714 _EGLImage *img;
1715 EGLImage ret;
1716
1717 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR);
1718 if (!disp->Extensions.KHR_image_base)
1719 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1720 if (!context && ctx != EGL_NO_CONTEXT)
1721 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1722 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1723 * <ctx> must be EGL_NO_CONTEXT..."
1724 */
1725 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1726 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1727
1728 img = disp->Driver->CreateImageKHR(disp, context, target, buffer, attr_list);
1729 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1730
1731 RETURN_EGL_EVAL(disp, ret);
1732 }
1733
1734 static EGLImage EGLAPIENTRY
1735 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1736 EGLClientBuffer buffer, const EGLint *attr_list)
1737 {
1738 _EGLDisplay *disp = _eglLockDisplay(dpy);
1739 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1740 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1741 }
1742
1743
1744 EGLImage EGLAPIENTRY
1745 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1746 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1747 {
1748 _EGLDisplay *disp = _eglLockDisplay(dpy);
1749 EGLImage image;
1750 EGLint *int_attribs;
1751
1752 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1753
1754 int_attribs = _eglConvertAttribsToInt(attr_list);
1755 if (attr_list && !int_attribs)
1756 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1757
1758 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1759 free(int_attribs);
1760 return image;
1761 }
1762
1763
1764 static EGLBoolean
1765 _eglDestroyImageCommon(_EGLDisplay *disp, _EGLImage *img)
1766 {
1767 EGLBoolean ret;
1768
1769 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
1770 if (!disp->Extensions.KHR_image_base)
1771 RETURN_EGL_EVAL(disp, EGL_FALSE);
1772 if (!img)
1773 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1774
1775 _eglUnlinkImage(img);
1776 ret = disp->Driver->DestroyImageKHR(disp, img);
1777
1778 RETURN_EGL_EVAL(disp, ret);
1779 }
1780
1781 EGLBoolean EGLAPIENTRY
1782 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1783 {
1784 _EGLDisplay *disp = _eglLockDisplay(dpy);
1785 _EGLImage *img = _eglLookupImage(image, disp);
1786 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1787 return _eglDestroyImageCommon(disp, img);
1788 }
1789
1790 static EGLBoolean EGLAPIENTRY
1791 eglDestroyImageKHR(EGLDisplay dpy, EGLImage image)
1792 {
1793 _EGLDisplay *disp = _eglLockDisplay(dpy);
1794 _EGLImage *img = _eglLookupImage(image, disp);
1795 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1796 return _eglDestroyImageCommon(disp, img);
1797 }
1798
1799
1800 static EGLSync
1801 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list,
1802 EGLBoolean orig_is_EGLAttrib,
1803 EGLenum invalid_type_error)
1804 {
1805 _EGLContext *ctx = _eglGetCurrentContext();
1806 _EGLSync *sync;
1807 EGLSync ret;
1808
1809 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR);
1810
1811 if (!disp->Extensions.KHR_cl_event2 && orig_is_EGLAttrib) {
1812 /* There exist two EGLAttrib variants of eglCreateSync*:
1813 * eglCreateSync64KHR which requires EGL_KHR_cl_event2, and eglCreateSync
1814 * which requires EGL 1.5. Here we use the presence of EGL_KHR_cl_event2
1815 * support as a proxy for EGL 1.5 support, even though that's not
1816 * entirely correct (though _eglComputeVersion does the same).
1817 *
1818 * The EGL spec provides no guidance on how to handle unsupported
1819 * functions. EGL_BAD_MATCH seems reasonable.
1820 */
1821 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1822 }
1823
1824 /* If type is EGL_SYNC_FENCE and no context is current for the bound API
1825 * (i.e., eglGetCurrentContext returns EGL_NO_CONTEXT ), an EGL_BAD_MATCH
1826 * error is generated.
1827 */
1828 if (!ctx &&
1829 (type == EGL_SYNC_FENCE_KHR || type == EGL_SYNC_NATIVE_FENCE_ANDROID))
1830 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1831
1832 /* return an error if the client API doesn't support GL_[OES|MESA]_EGL_sync. */
1833 if (ctx && (ctx->Resource.Display != disp ||
1834 (ctx->ClientAPI != EGL_OPENGL_ES_API &&
1835 ctx->ClientAPI != EGL_OPENGL_API)))
1836 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1837
1838 switch (type) {
1839 case EGL_SYNC_FENCE_KHR:
1840 if (!disp->Extensions.KHR_fence_sync)
1841 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1842 break;
1843 case EGL_SYNC_REUSABLE_KHR:
1844 if (!disp->Extensions.KHR_reusable_sync)
1845 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1846 break;
1847 case EGL_SYNC_CL_EVENT_KHR:
1848 if (!disp->Extensions.KHR_cl_event2)
1849 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1850 break;
1851 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1852 if (!disp->Extensions.ANDROID_native_fence_sync)
1853 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1854 break;
1855 default:
1856 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1857 }
1858
1859 sync = disp->Driver->CreateSyncKHR(disp, type, attrib_list);
1860 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1861
1862 RETURN_EGL_EVAL(disp, ret);
1863 }
1864
1865
1866 static EGLSync EGLAPIENTRY
1867 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *int_list)
1868 {
1869 _EGLDisplay *disp = _eglLockDisplay(dpy);
1870 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1871
1872 EGLSync sync;
1873 EGLAttrib *attrib_list;
1874 EGLint err;
1875
1876 if (sizeof(int_list[0]) == sizeof(attrib_list[0])) {
1877 attrib_list = (EGLAttrib *) int_list;
1878 } else {
1879 err = _eglConvertIntsToAttribs(int_list, &attrib_list);
1880 if (err != EGL_SUCCESS)
1881 RETURN_EGL_ERROR(disp, err, EGL_NO_SYNC);
1882 }
1883
1884 sync = _eglCreateSync(disp, type, attrib_list, EGL_FALSE,
1885 EGL_BAD_ATTRIBUTE);
1886
1887 if (sizeof(int_list[0]) != sizeof(attrib_list[0]))
1888 free(attrib_list);
1889
1890 /* Don't double-unlock the display. _eglCreateSync already unlocked it. */
1891 return sync;
1892 }
1893
1894
1895 static EGLSync EGLAPIENTRY
1896 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1897 {
1898 _EGLDisplay *disp = _eglLockDisplay(dpy);
1899 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1900 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1901 EGL_BAD_ATTRIBUTE);
1902 }
1903
1904
1905 EGLSync EGLAPIENTRY
1906 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1907 {
1908 _EGLDisplay *disp = _eglLockDisplay(dpy);
1909 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1910 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1911 EGL_BAD_PARAMETER);
1912 }
1913
1914
1915 static EGLBoolean
1916 _eglDestroySync(_EGLDisplay *disp, _EGLSync *s)
1917 {
1918 EGLBoolean ret;
1919
1920 _EGL_CHECK_SYNC(disp, s, EGL_FALSE);
1921 assert(disp->Extensions.KHR_reusable_sync ||
1922 disp->Extensions.KHR_fence_sync ||
1923 disp->Extensions.ANDROID_native_fence_sync);
1924
1925 _eglUnlinkSync(s);
1926 ret = disp->Driver->DestroySyncKHR(disp, s);
1927
1928 RETURN_EGL_EVAL(disp, ret);
1929 }
1930
1931 EGLBoolean EGLAPIENTRY
1932 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1933 {
1934 _EGLDisplay *disp = _eglLockDisplay(dpy);
1935 _EGLSync *s = _eglLookupSync(sync, disp);
1936 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1937 return _eglDestroySync(disp, s);
1938 }
1939
1940 static EGLBoolean EGLAPIENTRY
1941 eglDestroySyncKHR(EGLDisplay dpy, EGLSync sync)
1942 {
1943 _EGLDisplay *disp = _eglLockDisplay(dpy);
1944 _EGLSync *s = _eglLookupSync(sync, disp);
1945 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1946 return _eglDestroySync(disp, s);
1947 }
1948
1949
1950 static EGLint
1951 _eglClientWaitSyncCommon(_EGLDisplay *disp, EGLDisplay dpy,
1952 _EGLSync *s, EGLint flags, EGLTime timeout)
1953 {
1954 EGLint ret;
1955
1956 _EGL_CHECK_SYNC(disp, s, EGL_FALSE);
1957 assert(disp->Extensions.KHR_reusable_sync ||
1958 disp->Extensions.KHR_fence_sync ||
1959 disp->Extensions.ANDROID_native_fence_sync);
1960
1961 if (s->SyncStatus == EGL_SIGNALED_KHR)
1962 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1963
1964 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1965 * unlocked here to allow other threads also to be able to
1966 * go into waiting state.
1967 */
1968
1969 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1970 _eglUnlockDisplay(dpy);
1971
1972 ret = disp->Driver->ClientWaitSyncKHR(disp, s, flags, timeout);
1973
1974 /*
1975 * 'disp' is already unlocked for reusable sync type,
1976 * so passing 'NULL' to bypass unlocking display.
1977 */
1978 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1979 RETURN_EGL_EVAL(NULL, ret);
1980 else
1981 RETURN_EGL_EVAL(disp, ret);
1982 }
1983
1984 EGLint EGLAPIENTRY
1985 eglClientWaitSync(EGLDisplay dpy, EGLSync sync,
1986 EGLint flags, EGLTime timeout)
1987 {
1988 _EGLDisplay *disp = _eglLockDisplay(dpy);
1989 _EGLSync *s = _eglLookupSync(sync, disp);
1990 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1991 return _eglClientWaitSyncCommon(disp, dpy, s, flags, timeout);
1992 }
1993
1994 static EGLint EGLAPIENTRY
1995 eglClientWaitSyncKHR(EGLDisplay dpy, EGLSync sync,
1996 EGLint flags, EGLTime timeout)
1997 {
1998 _EGLDisplay *disp = _eglLockDisplay(dpy);
1999 _EGLSync *s = _eglLookupSync(sync, disp);
2000 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2001 return _eglClientWaitSyncCommon(disp, dpy, s, flags, timeout);
2002 }
2003
2004
2005 static EGLint
2006 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
2007 {
2008 _EGLContext *ctx = _eglGetCurrentContext();
2009 EGLint ret;
2010
2011 _EGL_CHECK_SYNC(disp, s, EGL_FALSE);
2012 assert(disp->Extensions.KHR_wait_sync);
2013
2014 /* return an error if the client API doesn't support GL_[OES|MESA]_EGL_sync. */
2015 if (ctx == EGL_NO_CONTEXT ||
2016 (ctx->ClientAPI != EGL_OPENGL_ES_API &&
2017 ctx->ClientAPI != EGL_OPENGL_API))
2018 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
2019
2020 /* the API doesn't allow any flags yet */
2021 if (flags != 0)
2022 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2023
2024 ret = disp->Driver->WaitSyncKHR(disp, s);
2025
2026 RETURN_EGL_EVAL(disp, ret);
2027 }
2028
2029 static EGLint EGLAPIENTRY
2030 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
2031 {
2032 _EGLDisplay *disp = _eglLockDisplay(dpy);
2033 _EGLSync *s = _eglLookupSync(sync, disp);
2034 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2035 return _eglWaitSyncCommon(disp, s, flags);
2036 }
2037
2038
2039 EGLBoolean EGLAPIENTRY
2040 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
2041 {
2042 /* The KHR version returns EGLint, while the core version returns
2043 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
2044 * EGL_TRUE.
2045 */
2046 _EGLDisplay *disp = _eglLockDisplay(dpy);
2047 _EGLSync *s = _eglLookupSync(sync, disp);
2048 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2049 return _eglWaitSyncCommon(disp, s, flags);
2050 }
2051
2052
2053 static EGLBoolean EGLAPIENTRY
2054 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
2055 {
2056 _EGLDisplay *disp = _eglLockDisplay(dpy);
2057 _EGLSync *s = _eglLookupSync(sync, disp);
2058 EGLBoolean ret;
2059
2060 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2061
2062 _EGL_CHECK_SYNC(disp, s, EGL_FALSE);
2063 assert(disp->Extensions.KHR_reusable_sync);
2064 ret = disp->Driver->SignalSyncKHR(disp, s, mode);
2065
2066 RETURN_EGL_EVAL(disp, ret);
2067 }
2068
2069
2070 static EGLBoolean
2071 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
2072 {
2073 EGLBoolean ret;
2074
2075 _EGL_CHECK_SYNC(disp, s, EGL_FALSE);
2076 assert(disp->Extensions.KHR_reusable_sync ||
2077 disp->Extensions.KHR_fence_sync ||
2078 disp->Extensions.ANDROID_native_fence_sync);
2079
2080 ret = _eglGetSyncAttrib(disp, s, attribute, value);
2081
2082 RETURN_EGL_EVAL(disp, ret);
2083 }
2084
2085 EGLBoolean EGLAPIENTRY
2086 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
2087 {
2088 _EGLDisplay *disp = _eglLockDisplay(dpy);
2089 _EGLSync *s = _eglLookupSync(sync, disp);
2090 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2091
2092 if (!value)
2093 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2094
2095 return _eglGetSyncAttribCommon(disp, s, attribute, value);
2096 }
2097
2098
2099 static EGLBoolean EGLAPIENTRY
2100 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
2101 {
2102 _EGLDisplay *disp = _eglLockDisplay(dpy);
2103 _EGLSync *s = _eglLookupSync(sync, disp);
2104 EGLAttrib attrib;
2105 EGLBoolean result;
2106
2107 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2108
2109 if (!value)
2110 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2111
2112 attrib = *value;
2113 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
2114
2115 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
2116 *
2117 * If any error occurs, <*value> is not modified.
2118 */
2119 if (result == EGL_FALSE)
2120 return result;
2121
2122 *value = attrib;
2123 return result;
2124 }
2125
2126 static EGLint EGLAPIENTRY
2127 eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSync sync)
2128 {
2129 _EGLDisplay *disp = _eglLockDisplay(dpy);
2130 _EGLSync *s = _eglLookupSync(sync, disp);
2131 EGLBoolean ret;
2132
2133 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2134
2135 /* the spec doesn't seem to specify what happens if the fence
2136 * type is not EGL_SYNC_NATIVE_FENCE_ANDROID, but this seems
2137 * sensible:
2138 */
2139 if (!(s && (s->Type == EGL_SYNC_NATIVE_FENCE_ANDROID)))
2140 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_NATIVE_FENCE_FD_ANDROID);
2141
2142 _EGL_CHECK_SYNC(disp, s, EGL_NO_NATIVE_FENCE_FD_ANDROID);
2143 assert(disp->Extensions.ANDROID_native_fence_sync);
2144 ret = disp->Driver->DupNativeFenceFDANDROID(disp, s);
2145
2146 RETURN_EGL_EVAL(disp, ret);
2147 }
2148
2149 static EGLBoolean EGLAPIENTRY
2150 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
2151 EGLint numRects, const EGLint *rects)
2152 {
2153 _EGLContext *ctx = _eglGetCurrentContext();
2154 _EGLDisplay *disp = _eglLockDisplay(dpy);
2155 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2156 EGLBoolean ret;
2157
2158 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2159
2160 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
2161
2162 if (!disp->Extensions.NOK_swap_region)
2163 RETURN_EGL_EVAL(disp, EGL_FALSE);
2164
2165 /* surface must be bound to current context in EGL 1.4 */
2166 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
2167 surf != ctx->DrawSurface)
2168 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
2169
2170 ret = disp->Driver->SwapBuffersRegionNOK(disp, surf, numRects, rects);
2171
2172 RETURN_EGL_EVAL(disp, ret);
2173 }
2174
2175
2176 static EGLImage EGLAPIENTRY
2177 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
2178 {
2179 _EGLDisplay *disp = _eglLockDisplay(dpy);
2180 _EGLImage *img;
2181 EGLImage ret;
2182
2183 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2184
2185 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR);
2186 if (!disp->Extensions.MESA_drm_image)
2187 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
2188
2189 img = disp->Driver->CreateDRMImageMESA(disp, attr_list);
2190 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
2191
2192 RETURN_EGL_EVAL(disp, ret);
2193 }
2194
2195 static EGLBoolean EGLAPIENTRY
2196 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
2197 EGLint *name, EGLint *handle, EGLint *stride)
2198 {
2199 _EGLDisplay *disp = _eglLockDisplay(dpy);
2200 _EGLImage *img = _eglLookupImage(image, disp);
2201 EGLBoolean ret;
2202
2203 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2204
2205 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2206 assert(disp->Extensions.MESA_drm_image);
2207
2208 if (!img)
2209 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2210
2211 ret = disp->Driver->ExportDRMImageMESA(disp, img, name, handle, stride);
2212
2213 RETURN_EGL_EVAL(disp, ret);
2214 }
2215
2216
2217 struct wl_display;
2218
2219 static EGLBoolean EGLAPIENTRY
2220 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2221 {
2222 _EGLDisplay *disp = _eglLockDisplay(dpy);
2223 EGLBoolean ret;
2224
2225 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2226
2227 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2228 assert(disp->Extensions.WL_bind_wayland_display);
2229
2230 if (!display)
2231 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2232
2233 ret = disp->Driver->BindWaylandDisplayWL(disp, display);
2234
2235 RETURN_EGL_EVAL(disp, ret);
2236 }
2237
2238 static EGLBoolean EGLAPIENTRY
2239 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2240 {
2241 _EGLDisplay *disp = _eglLockDisplay(dpy);
2242 EGLBoolean ret;
2243
2244 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2245
2246 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2247 assert(disp->Extensions.WL_bind_wayland_display);
2248
2249 if (!display)
2250 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2251
2252 ret = disp->Driver->UnbindWaylandDisplayWL(disp, display);
2253
2254 RETURN_EGL_EVAL(disp, ret);
2255 }
2256
2257 static EGLBoolean EGLAPIENTRY
2258 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
2259 EGLint attribute, EGLint *value)
2260 {
2261 _EGLDisplay *disp = _eglLockDisplay(dpy);
2262 EGLBoolean ret;
2263
2264 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2265
2266 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2267 assert(disp->Extensions.WL_bind_wayland_display);
2268
2269 if (!buffer)
2270 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2271
2272 ret = disp->Driver->QueryWaylandBufferWL(disp, buffer, attribute, value);
2273
2274 RETURN_EGL_EVAL(disp, ret);
2275 }
2276
2277
2278 static struct wl_buffer * EGLAPIENTRY
2279 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
2280 {
2281 _EGLDisplay *disp = _eglLockDisplay(dpy);
2282 _EGLImage *img;
2283 struct wl_buffer *ret;
2284
2285 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2286
2287 _EGL_CHECK_DISPLAY(disp, NULL);
2288 if (!disp->Extensions.WL_create_wayland_buffer_from_image)
2289 RETURN_EGL_EVAL(disp, NULL);
2290
2291 img = _eglLookupImage(image, disp);
2292
2293 if (!img)
2294 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
2295
2296 ret = disp->Driver->CreateWaylandBufferFromImageWL(disp, img);
2297
2298 RETURN_EGL_EVAL(disp, ret);
2299 }
2300
2301 static EGLBoolean EGLAPIENTRY
2302 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
2303 EGLint x, EGLint y, EGLint width, EGLint height)
2304 {
2305 _EGLDisplay *disp = _eglLockDisplay(dpy);
2306 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2307 EGLBoolean ret;
2308
2309 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2310
2311 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
2312
2313 if (!disp->Extensions.NV_post_sub_buffer)
2314 RETURN_EGL_EVAL(disp, EGL_FALSE);
2315
2316 ret = disp->Driver->PostSubBufferNV(disp, surf, x, y, width, height);
2317
2318 RETURN_EGL_EVAL(disp, ret);
2319 }
2320
2321 static EGLBoolean EGLAPIENTRY
2322 eglGetSyncValuesCHROMIUM(EGLDisplay dpy, EGLSurface surface,
2323 EGLuint64KHR *ust, EGLuint64KHR *msc,
2324 EGLuint64KHR *sbc)
2325 {
2326 _EGLDisplay *disp = _eglLockDisplay(dpy);
2327 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2328 EGLBoolean ret;
2329
2330 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2331
2332 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE);
2333 if (!disp->Extensions.CHROMIUM_sync_control)
2334 RETURN_EGL_EVAL(disp, EGL_FALSE);
2335
2336 if (!ust || !msc || !sbc)
2337 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2338
2339 ret = disp->Driver->GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
2340
2341 RETURN_EGL_EVAL(disp, ret);
2342 }
2343
2344 static EGLBoolean EGLAPIENTRY
2345 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
2346 EGLint *fourcc, EGLint *nplanes,
2347 EGLuint64KHR *modifiers)
2348 {
2349 _EGLDisplay *disp = _eglLockDisplay(dpy);
2350 _EGLImage *img = _eglLookupImage(image, disp);
2351 EGLBoolean ret;
2352
2353 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2354
2355 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2356 assert(disp->Extensions.MESA_image_dma_buf_export);
2357
2358 if (!img)
2359 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2360
2361 ret = disp->Driver->ExportDMABUFImageQueryMESA(disp, img, fourcc, nplanes, modifiers);
2362
2363 RETURN_EGL_EVAL(disp, ret);
2364 }
2365
2366 static EGLBoolean EGLAPIENTRY
2367 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
2368 int *fds, EGLint *strides, EGLint *offsets)
2369 {
2370 _EGLDisplay *disp = _eglLockDisplay(dpy);
2371 _EGLImage *img = _eglLookupImage(image, disp);
2372 EGLBoolean ret;
2373
2374 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2375
2376 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2377 assert(disp->Extensions.MESA_image_dma_buf_export);
2378
2379 if (!img)
2380 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2381
2382 ret = disp->Driver->ExportDMABUFImageMESA(disp, img, fds, strides, offsets);
2383
2384 RETURN_EGL_EVAL(disp, ret);
2385 }
2386
2387 static EGLint EGLAPIENTRY
2388 eglLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object,
2389 EGLLabelKHR label)
2390 {
2391 _EGLDisplay *disp = NULL;
2392 _EGLResourceType type;
2393
2394 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2395
2396 if (objectType == EGL_OBJECT_THREAD_KHR) {
2397 _EGLThreadInfo *t = _eglGetCurrentThread();
2398
2399 if (!_eglIsCurrentThreadDummy()) {
2400 t->Label = label;
2401 return EGL_SUCCESS;
2402 }
2403
2404 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_BAD_ALLOC);
2405 }
2406
2407 disp = _eglLockDisplay(dpy);
2408 if (disp == NULL)
2409 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_BAD_DISPLAY);
2410
2411 if (objectType == EGL_OBJECT_DISPLAY_KHR) {
2412 if (dpy != (EGLDisplay) object)
2413 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2414
2415 disp->Label = label;
2416 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2417 }
2418
2419 switch (objectType) {
2420 case EGL_OBJECT_CONTEXT_KHR:
2421 type = _EGL_RESOURCE_CONTEXT;
2422 break;
2423 case EGL_OBJECT_SURFACE_KHR:
2424 type = _EGL_RESOURCE_SURFACE;
2425 break;
2426 case EGL_OBJECT_IMAGE_KHR:
2427 type = _EGL_RESOURCE_IMAGE;
2428 break;
2429 case EGL_OBJECT_SYNC_KHR:
2430 type = _EGL_RESOURCE_SYNC;
2431 break;
2432 case EGL_OBJECT_STREAM_KHR:
2433 default:
2434 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2435 }
2436
2437 if (_eglCheckResource(object, type, disp)) {
2438 _EGLResource *res = (_EGLResource *) object;
2439
2440 res->Label = label;
2441 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2442 }
2443
2444 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2445 }
2446
2447 static EGLint EGLAPIENTRY
2448 eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
2449 const EGLAttrib *attrib_list)
2450 {
2451 unsigned int newEnabled;
2452
2453 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2454
2455 mtx_lock(_eglGlobal.Mutex);
2456
2457 newEnabled = _eglGlobal.debugTypesEnabled;
2458 if (attrib_list != NULL) {
2459 int i;
2460
2461 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
2462 switch (attrib_list[i]) {
2463 case EGL_DEBUG_MSG_CRITICAL_KHR:
2464 case EGL_DEBUG_MSG_ERROR_KHR:
2465 case EGL_DEBUG_MSG_WARN_KHR:
2466 case EGL_DEBUG_MSG_INFO_KHR:
2467 if (attrib_list[i + 1])
2468 newEnabled |= DebugBitFromType(attrib_list[i]);
2469 else
2470 newEnabled &= ~DebugBitFromType(attrib_list[i]);
2471 break;
2472 default:
2473 // On error, set the last error code, call the current
2474 // debug callback, and return the error code.
2475 mtx_unlock(_eglGlobal.Mutex);
2476 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2477 "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
2478 return EGL_BAD_ATTRIBUTE;
2479 }
2480 }
2481 }
2482
2483 if (callback != NULL) {
2484 _eglGlobal.debugCallback = callback;
2485 _eglGlobal.debugTypesEnabled = newEnabled;
2486 } else {
2487 _eglGlobal.debugCallback = NULL;
2488 _eglGlobal.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR;
2489 }
2490
2491 mtx_unlock(_eglGlobal.Mutex);
2492 return EGL_SUCCESS;
2493 }
2494
2495 static EGLBoolean EGLAPIENTRY
2496 eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
2497 {
2498 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2499
2500 mtx_lock(_eglGlobal.Mutex);
2501
2502 switch (attribute) {
2503 case EGL_DEBUG_MSG_CRITICAL_KHR:
2504 case EGL_DEBUG_MSG_ERROR_KHR:
2505 case EGL_DEBUG_MSG_WARN_KHR:
2506 case EGL_DEBUG_MSG_INFO_KHR:
2507 if (_eglGlobal.debugTypesEnabled & DebugBitFromType(attribute))
2508 *value = EGL_TRUE;
2509 else
2510 *value = EGL_FALSE;
2511 break;
2512 case EGL_DEBUG_CALLBACK_KHR:
2513 *value = (EGLAttrib) _eglGlobal.debugCallback;
2514 break;
2515 default:
2516 mtx_unlock(_eglGlobal.Mutex);
2517 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2518 "Invalid attribute 0x%04lx", (unsigned long) attribute);
2519 return EGL_FALSE;
2520 }
2521
2522 mtx_unlock(_eglGlobal.Mutex);
2523 return EGL_TRUE;
2524 }
2525
2526 static int
2527 _eglFunctionCompare(const void *key, const void *elem)
2528 {
2529 const char *procname = key;
2530 const struct _egl_entrypoint *entrypoint = elem;
2531 return strcmp(procname, entrypoint->name);
2532 }
2533
2534 static EGLBoolean EGLAPIENTRY
2535 eglQueryDmaBufFormatsEXT(EGLDisplay dpy, EGLint max_formats,
2536 EGLint *formats, EGLint *num_formats)
2537 {
2538 _EGLDisplay *disp = _eglLockDisplay(dpy);
2539 EGLBoolean ret;
2540
2541 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2542
2543 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2544
2545 ret = disp->Driver->QueryDmaBufFormatsEXT(disp, max_formats, formats, num_formats);
2546
2547 RETURN_EGL_EVAL(disp, ret);
2548 }
2549
2550 static EGLBoolean EGLAPIENTRY
2551 eglQueryDmaBufModifiersEXT(EGLDisplay dpy, EGLint format, EGLint max_modifiers,
2552 EGLuint64KHR *modifiers, EGLBoolean *external_only,
2553 EGLint *num_modifiers)
2554 {
2555 _EGLDisplay *disp = _eglLockDisplay(dpy);
2556 EGLBoolean ret;
2557
2558 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2559
2560 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2561
2562 ret = disp->Driver->QueryDmaBufModifiersEXT(disp, format, max_modifiers, modifiers,
2563 external_only, num_modifiers);
2564
2565 RETURN_EGL_EVAL(disp, ret);
2566 }
2567
2568 static void EGLAPIENTRY
2569 eglSetBlobCacheFuncsANDROID(EGLDisplay *dpy, EGLSetBlobFuncANDROID set,
2570 EGLGetBlobFuncANDROID get)
2571 {
2572 /* This function does not return anything so we cannot
2573 * utilize the helper macros _EGL_FUNC_START or _EGL_CHECK_DISPLAY.
2574 */
2575 _EGLDisplay *disp = _eglLockDisplay(dpy);
2576 if (!_eglSetFuncName(__func__, disp, EGL_OBJECT_DISPLAY_KHR, NULL)) {
2577 if (disp)
2578 _eglUnlockDisplay(disp);
2579 return;
2580 }
2581
2582 if (!_eglCheckDisplay(disp, __func__)) {
2583 if (disp)
2584 _eglUnlockDisplay(disp);
2585 return;
2586 }
2587
2588 if (!set || !get) {
2589 _eglError(EGL_BAD_PARAMETER,
2590 "eglSetBlobCacheFuncsANDROID: NULL handler given");
2591 _eglUnlockDisplay(disp);
2592 return;
2593 }
2594
2595 if (disp->BlobCacheSet) {
2596 _eglError(EGL_BAD_PARAMETER,
2597 "eglSetBlobCacheFuncsANDROID: functions already set");
2598 _eglUnlockDisplay(disp);
2599 return;
2600 }
2601
2602 disp->BlobCacheSet = set;
2603 disp->BlobCacheGet = get;
2604
2605 disp->Driver->SetBlobCacheFuncsANDROID(disp, set, get);
2606
2607 _eglUnlockDisplay(disp);
2608 }
2609
2610 static EGLBoolean EGLAPIENTRY
2611 eglQueryDeviceAttribEXT(EGLDeviceEXT device,
2612 EGLint attribute,
2613 EGLAttrib *value)
2614 {
2615 _EGLDevice *dev = _eglLookupDevice(device);
2616 EGLBoolean ret;
2617
2618 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2619 if (!dev)
2620 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, EGL_FALSE);
2621
2622 ret = _eglQueryDeviceAttribEXT(dev, attribute, value);
2623 RETURN_EGL_EVAL(NULL, ret);
2624 }
2625
2626 static const char * EGLAPIENTRY
2627 eglQueryDeviceStringEXT(EGLDeviceEXT device,
2628 EGLint name)
2629 {
2630 _EGLDevice *dev = _eglLookupDevice(device);
2631
2632 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2633 if (!dev)
2634 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, NULL);
2635
2636 RETURN_EGL_EVAL(NULL, _eglQueryDeviceStringEXT(dev, name));
2637 }
2638
2639 static EGLBoolean EGLAPIENTRY
2640 eglQueryDevicesEXT(EGLint max_devices,
2641 EGLDeviceEXT *devices,
2642 EGLint *num_devices)
2643 {
2644 EGLBoolean ret;
2645
2646 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2647 ret = _eglQueryDevicesEXT(max_devices, (_EGLDevice **) devices,
2648 num_devices);
2649 RETURN_EGL_EVAL(NULL, ret);
2650 }
2651
2652 static EGLBoolean EGLAPIENTRY
2653 eglQueryDisplayAttribEXT(EGLDisplay dpy,
2654 EGLint attribute,
2655 EGLAttrib *value)
2656 {
2657 _EGLDisplay *disp = _eglLockDisplay(dpy);
2658
2659 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2660 _EGL_CHECK_DISPLAY(disp, EGL_FALSE);
2661
2662 switch (attribute) {
2663 case EGL_DEVICE_EXT:
2664 *value = (EGLAttrib) disp->Device;
2665 break;
2666 default:
2667 RETURN_EGL_ERROR(disp, EGL_BAD_ATTRIBUTE, EGL_FALSE);
2668 }
2669 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
2670 }
2671
2672 static char * EGLAPIENTRY
2673 eglGetDisplayDriverConfig(EGLDisplay dpy)
2674 {
2675 _EGLDisplay *disp = _eglLockDisplay(dpy);
2676 char *ret;
2677
2678 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2679 _EGL_CHECK_DISPLAY(disp, NULL);
2680
2681 assert(disp->Extensions.MESA_query_driver);
2682
2683 ret = disp->Driver->QueryDriverConfig(disp);
2684 RETURN_EGL_EVAL(disp, ret);
2685 }
2686
2687 static const char * EGLAPIENTRY
2688 eglGetDisplayDriverName(EGLDisplay dpy)
2689 {
2690 _EGLDisplay *disp = _eglLockDisplay(dpy);
2691 const char *ret;
2692
2693 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2694 _EGL_CHECK_DISPLAY(disp, NULL);
2695
2696 assert(disp->Extensions.MESA_query_driver);
2697
2698 ret = disp->Driver->QueryDriverName(disp);
2699 RETURN_EGL_EVAL(disp, ret);
2700 }
2701
2702 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
2703 eglGetProcAddress(const char *procname)
2704 {
2705 static const struct _egl_entrypoint egl_functions[] = {
2706 #define EGL_ENTRYPOINT(f) { .name = #f, .function = (_EGLProc) f },
2707 #include "eglentrypoint.h"
2708 #undef EGL_ENTRYPOINT
2709 };
2710 _EGLProc ret = NULL;
2711
2712 if (!procname)
2713 RETURN_EGL_SUCCESS(NULL, NULL);
2714
2715 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2716
2717 if (strncmp(procname, "egl", 3) == 0) {
2718 const struct _egl_entrypoint *entrypoint =
2719 bsearch(procname,
2720 egl_functions, ARRAY_SIZE(egl_functions),
2721 sizeof(egl_functions[0]),
2722 _eglFunctionCompare);
2723 if (entrypoint)
2724 ret = entrypoint->function;
2725 }
2726
2727 if (!ret && _eglDriver.GetProcAddress)
2728 ret = _eglDriver.GetProcAddress(procname);
2729
2730 RETURN_EGL_SUCCESS(NULL, ret);
2731 }
2732
2733 static int
2734 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
2735 _EGLDisplay **disp, _EGLContext **ctx)
2736 {
2737
2738 *disp = _eglLockDisplay(dpy);
2739 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
2740 if (*disp)
2741 _eglUnlockDisplay(*disp);
2742 return MESA_GLINTEROP_INVALID_DISPLAY;
2743 }
2744
2745 *ctx = _eglLookupContext(context, *disp);
2746 if (!*ctx ||
2747 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
2748 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
2749 _eglUnlockDisplay(*disp);
2750 return MESA_GLINTEROP_INVALID_CONTEXT;
2751 }
2752
2753 return MESA_GLINTEROP_SUCCESS;
2754 }
2755
2756 PUBLIC int
2757 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
2758 struct mesa_glinterop_device_info *out)
2759 {
2760 _EGLDisplay *disp;
2761 _EGLContext *ctx;
2762 int ret;
2763
2764 ret = _eglLockDisplayInterop(dpy, context, &disp, &ctx);
2765 if (ret != MESA_GLINTEROP_SUCCESS)
2766 return ret;
2767
2768 if (disp->Driver->GLInteropQueryDeviceInfo)
2769 ret = disp->Driver->GLInteropQueryDeviceInfo(disp, ctx, out);
2770 else
2771 ret = MESA_GLINTEROP_UNSUPPORTED;
2772
2773 _eglUnlockDisplay(disp);
2774 return ret;
2775 }
2776
2777 PUBLIC int
2778 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2779 struct mesa_glinterop_export_in *in,
2780 struct mesa_glinterop_export_out *out)
2781 {
2782 _EGLDisplay *disp;
2783 _EGLContext *ctx;
2784 int ret;
2785
2786 ret = _eglLockDisplayInterop(dpy, context, &disp, &ctx);
2787 if (ret != MESA_GLINTEROP_SUCCESS)
2788 return ret;
2789
2790 if (disp->Driver->GLInteropExportObject)
2791 ret = disp->Driver->GLInteropExportObject(disp, ctx, in, out);
2792 else
2793 ret = MESA_GLINTEROP_UNSUPPORTED;
2794
2795 _eglUnlockDisplay(disp);
2796 return ret;
2797 }