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