egl: move eglGetConfigs() fallback to eglapi.c
[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 if (drv->API.GetConfigs)
729 ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
730 else
731 ret = _eglGetConfigs(drv, disp, configs, config_size, num_config);
732
733 RETURN_EGL_EVAL(disp, ret);
734 }
735
736
737 EGLBoolean EGLAPIENTRY
738 eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
739 EGLint config_size, EGLint *num_config)
740 {
741 _EGLDisplay *disp = _eglLockDisplay(dpy);
742 _EGLDriver *drv;
743 EGLBoolean ret;
744
745 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
746
747 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
748
749 if (!num_config)
750 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
751
752 ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
753 config_size, num_config);
754
755 RETURN_EGL_EVAL(disp, ret);
756 }
757
758
759 EGLBoolean EGLAPIENTRY
760 eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
761 EGLint attribute, EGLint *value)
762 {
763 _EGLDisplay *disp = _eglLockDisplay(dpy);
764 _EGLConfig *conf = _eglLookupConfig(config, disp);
765 _EGLDriver *drv;
766 EGLBoolean ret;
767
768 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
769
770 _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
771 ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
772
773 RETURN_EGL_EVAL(disp, ret);
774 }
775
776
777 EGLContext EGLAPIENTRY
778 eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
779 const EGLint *attrib_list)
780 {
781 _EGLDisplay *disp = _eglLockDisplay(dpy);
782 _EGLConfig *conf = _eglLookupConfig(config, disp);
783 _EGLContext *share = _eglLookupContext(share_list, disp);
784 _EGLDriver *drv;
785 _EGLContext *context;
786 EGLContext ret;
787
788 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_CONTEXT);
789
790 _EGL_CHECK_DISPLAY(disp, EGL_NO_CONTEXT, drv);
791
792 if (config != EGL_NO_CONFIG_KHR)
793 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_CONTEXT, drv);
794 else if (!disp->Extensions.KHR_no_config_context)
795 RETURN_EGL_ERROR(disp, EGL_BAD_CONFIG, EGL_NO_CONTEXT);
796
797 if (!share && share_list != EGL_NO_CONTEXT)
798 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
799
800 context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
801 ret = (context) ? _eglLinkContext(context) : EGL_NO_CONTEXT;
802
803 RETURN_EGL_EVAL(disp, ret);
804 }
805
806
807 EGLBoolean EGLAPIENTRY
808 eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
809 {
810 _EGLDisplay *disp = _eglLockDisplay(dpy);
811 _EGLContext *context = _eglLookupContext(ctx, disp);
812 _EGLDriver *drv;
813 EGLBoolean ret;
814
815 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
816
817 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
818 _eglUnlinkContext(context);
819 ret = drv->API.DestroyContext(drv, disp, context);
820
821 RETURN_EGL_EVAL(disp, ret);
822 }
823
824
825 EGLBoolean EGLAPIENTRY
826 eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
827 EGLContext ctx)
828 {
829 _EGLDisplay *disp = _eglLockDisplay(dpy);
830 _EGLContext *context = _eglLookupContext(ctx, disp);
831 _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
832 _EGLSurface *read_surf = _eglLookupSurface(read, disp);
833 _EGLDriver *drv;
834 EGLBoolean ret;
835
836 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
837
838 if (!disp)
839 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
840 drv = disp->Driver;
841
842 /* display is allowed to be uninitialized under certain condition */
843 if (!disp->Initialized) {
844 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
845 ctx != EGL_NO_CONTEXT)
846 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
847 }
848 if (!drv)
849 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
850
851 if (!context && ctx != EGL_NO_CONTEXT)
852 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
853 if (!draw_surf || !read_surf) {
854 /* From the EGL 1.4 (20130211) spec:
855 *
856 * To release the current context without assigning a new one, set ctx
857 * to EGL_NO_CONTEXT and set draw and read to EGL_NO_SURFACE.
858 */
859 if (!disp->Extensions.KHR_surfaceless_context && ctx != EGL_NO_CONTEXT)
860 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
861
862 if ((!draw_surf && draw != EGL_NO_SURFACE) ||
863 (!read_surf && read != EGL_NO_SURFACE))
864 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
865 if (draw_surf || read_surf)
866 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
867 }
868
869 /* If a native window underlying either draw or read is no longer valid,
870 * an EGL_BAD_NATIVE_WINDOW error is generated.
871 */
872 if (draw_surf && draw_surf->Lost)
873 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
874 if (read_surf && read_surf->Lost)
875 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
876
877 ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
878
879 RETURN_EGL_EVAL(disp, ret);
880 }
881
882
883 EGLBoolean EGLAPIENTRY
884 eglQueryContext(EGLDisplay dpy, EGLContext ctx,
885 EGLint attribute, EGLint *value)
886 {
887 _EGLDisplay *disp = _eglLockDisplay(dpy);
888 _EGLContext *context = _eglLookupContext(ctx, disp);
889 _EGLDriver *drv;
890 EGLBoolean ret;
891
892 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
893
894 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
895 ret = drv->API.QueryContext(drv, disp, context, attribute, value);
896
897 RETURN_EGL_EVAL(disp, ret);
898 }
899
900
901 /* In EGL specs 1.4 and 1.5, at the end of sections 3.5.1 and 3.5.4, it says
902 * that if native_surface was already used to create a window or pixmap, we
903 * can't create a new one. This is what this function checks for.
904 */
905 static bool
906 _eglNativeSurfaceAlreadyUsed(_EGLDisplay *disp, void *native_surface)
907 {
908 _EGLResource *list;
909
910 list = disp->ResourceLists[_EGL_RESOURCE_SURFACE];
911 while (list) {
912 _EGLSurface *surf = (_EGLSurface *) list;
913
914 list = list->Next;
915
916 if (surf->Type == EGL_PBUFFER_BIT)
917 continue;
918
919 if (surf->NativeSurface == native_surface)
920 return true;
921 }
922
923 return false;
924 }
925
926
927 static EGLSurface
928 _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
929 void *native_window, const EGLint *attrib_list)
930 {
931 _EGLConfig *conf = _eglLookupConfig(config, disp);
932 _EGLDriver *drv;
933 _EGLSurface *surf;
934 EGLSurface ret;
935
936
937 if (native_window == NULL)
938 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
939
940 if (disp && (disp->Platform == _EGL_PLATFORM_SURFACELESS ||
941 disp->Platform == _EGL_PLATFORM_DEVICE)) {
942 /* From the EGL_MESA_platform_surfaceless spec (v1):
943 *
944 * eglCreatePlatformWindowSurface fails when called with a <display>
945 * that belongs to the surfaceless platform. It returns
946 * EGL_NO_SURFACE and generates EGL_BAD_NATIVE_WINDOW. The
947 * justification for this unconditional failure is that the
948 * surfaceless platform has no native windows, and therefore the
949 * <native_window> parameter is always invalid.
950 *
951 * This check must occur before checking the EGLConfig, which emits
952 * EGL_BAD_CONFIG.
953 */
954 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
955 }
956
957 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
958
959 if ((conf->SurfaceType & EGL_WINDOW_BIT) == 0)
960 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
961
962 if (_eglNativeSurfaceAlreadyUsed(disp, native_window))
963 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
964
965 surf = drv->API.CreateWindowSurface(drv, disp, conf, native_window,
966 attrib_list);
967 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
968
969 RETURN_EGL_EVAL(disp, ret);
970 }
971
972
973 EGLSurface EGLAPIENTRY
974 eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
975 EGLNativeWindowType window, const EGLint *attrib_list)
976 {
977 _EGLDisplay *disp = _eglLockDisplay(dpy);
978
979 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
980 STATIC_ASSERT(sizeof(void*) == sizeof(window));
981 return _eglCreateWindowSurfaceCommon(disp, config, (void*) window,
982 attrib_list);
983 }
984
985 static void *
986 _fixupNativeWindow(_EGLDisplay *disp, void *native_window)
987 {
988 #ifdef HAVE_X11_PLATFORM
989 if (disp && disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
990 /* The `native_window` parameter for the X11 platform differs between
991 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
992 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
993 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
994 * `Window*`. Convert `Window*` to `Window` because that's what
995 * dri2_x11_create_window_surface() expects.
996 */
997 return (void *)(* (Window*) native_window);
998 }
999 #endif
1000 return native_window;
1001 }
1002
1003 static EGLSurface EGLAPIENTRY
1004 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
1005 void *native_window,
1006 const EGLint *attrib_list)
1007 {
1008 _EGLDisplay *disp = _eglLockDisplay(dpy);
1009
1010 native_window = _fixupNativeWindow(disp, native_window);
1011
1012 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1013 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
1014 attrib_list);
1015 }
1016
1017
1018 EGLSurface EGLAPIENTRY
1019 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
1020 void *native_window,
1021 const EGLAttrib *attrib_list)
1022 {
1023 _EGLDisplay *disp = _eglLockDisplay(dpy);
1024 EGLSurface surface;
1025 EGLint *int_attribs;
1026
1027 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1028
1029 int_attribs = _eglConvertAttribsToInt(attrib_list);
1030 if (attrib_list && !int_attribs)
1031 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1032
1033 native_window = _fixupNativeWindow(disp, native_window);
1034 surface = _eglCreateWindowSurfaceCommon(disp, config, native_window,
1035 int_attribs);
1036 free(int_attribs);
1037 return surface;
1038 }
1039
1040 static void *
1041 _fixupNativePixmap(_EGLDisplay *disp, void *native_pixmap)
1042 {
1043 #ifdef HAVE_X11_PLATFORM
1044 /* The `native_pixmap` parameter for the X11 platform differs between
1045 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
1046 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
1047 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
1048 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
1049 * dri2_x11_create_pixmap_surface() expects.
1050 */
1051 if (disp && disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL)
1052 return (void *)(* (Pixmap*) native_pixmap);
1053 #endif
1054 return native_pixmap;
1055 }
1056
1057 static EGLSurface
1058 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
1059 void *native_pixmap, const EGLint *attrib_list)
1060 {
1061 _EGLConfig *conf = _eglLookupConfig(config, disp);
1062 _EGLDriver *drv;
1063 _EGLSurface *surf;
1064 EGLSurface ret;
1065
1066 if (disp && (disp->Platform == _EGL_PLATFORM_SURFACELESS ||
1067 disp->Platform == _EGL_PLATFORM_DEVICE)) {
1068 /* From the EGL_MESA_platform_surfaceless spec (v1):
1069 *
1070 * [Like eglCreatePlatformWindowSurface,] eglCreatePlatformPixmapSurface
1071 * also fails when called with a <display> that belongs to the
1072 * surfaceless platform. It returns EGL_NO_SURFACE and generates
1073 * EGL_BAD_NATIVE_PIXMAP.
1074 *
1075 * This check must occur before checking the EGLConfig, which emits
1076 * EGL_BAD_CONFIG.
1077 */
1078 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1079 }
1080
1081 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1082
1083 if ((conf->SurfaceType & EGL_PIXMAP_BIT) == 0)
1084 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
1085
1086 if (native_pixmap == NULL)
1087 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1088
1089 if (_eglNativeSurfaceAlreadyUsed(disp, native_pixmap))
1090 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1091
1092 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
1093 attrib_list);
1094 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1095
1096 RETURN_EGL_EVAL(disp, ret);
1097 }
1098
1099
1100 EGLSurface EGLAPIENTRY
1101 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
1102 EGLNativePixmapType pixmap, const EGLint *attrib_list)
1103 {
1104 _EGLDisplay *disp = _eglLockDisplay(dpy);
1105
1106 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1107 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
1108 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
1109 attrib_list);
1110 }
1111
1112 static EGLSurface EGLAPIENTRY
1113 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
1114 void *native_pixmap,
1115 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 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1121 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1122 attrib_list);
1123 }
1124
1125
1126 EGLSurface EGLAPIENTRY
1127 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
1128 void *native_pixmap,
1129 const EGLAttrib *attrib_list)
1130 {
1131 _EGLDisplay *disp = _eglLockDisplay(dpy);
1132 EGLSurface surface;
1133 EGLint *int_attribs;
1134
1135 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1136
1137 int_attribs = _eglConvertAttribsToInt(attrib_list);
1138 if (attrib_list && !int_attribs)
1139 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1140
1141 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1142 surface = _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1143 int_attribs);
1144 free(int_attribs);
1145 return surface;
1146 }
1147
1148
1149 EGLSurface EGLAPIENTRY
1150 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
1151 const EGLint *attrib_list)
1152 {
1153 _EGLDisplay *disp = _eglLockDisplay(dpy);
1154 _EGLConfig *conf = _eglLookupConfig(config, disp);
1155 _EGLDriver *drv;
1156 _EGLSurface *surf;
1157 EGLSurface ret;
1158
1159 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1160 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1161
1162 if ((conf->SurfaceType & EGL_PBUFFER_BIT) == 0)
1163 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
1164
1165 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
1166 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1167
1168 RETURN_EGL_EVAL(disp, ret);
1169 }
1170
1171
1172 EGLBoolean EGLAPIENTRY
1173 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1174 {
1175 _EGLDisplay *disp = _eglLockDisplay(dpy);
1176 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1177 _EGLDriver *drv;
1178 EGLBoolean ret;
1179
1180 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1181 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1182 _eglUnlinkSurface(surf);
1183 ret = drv->API.DestroySurface(drv, disp, surf);
1184
1185 RETURN_EGL_EVAL(disp, ret);
1186 }
1187
1188 EGLBoolean EGLAPIENTRY
1189 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
1190 EGLint attribute, EGLint *value)
1191 {
1192 _EGLDisplay *disp = _eglLockDisplay(dpy);
1193 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1194 _EGLDriver *drv;
1195 EGLBoolean ret;
1196
1197 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1198 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1199 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
1200
1201 RETURN_EGL_EVAL(disp, ret);
1202 }
1203
1204 EGLBoolean EGLAPIENTRY
1205 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
1206 EGLint attribute, EGLint value)
1207 {
1208 _EGLDisplay *disp = _eglLockDisplay(dpy);
1209 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1210 _EGLDriver *drv;
1211 EGLBoolean ret;
1212
1213 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1214 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1215 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
1216
1217 RETURN_EGL_EVAL(disp, ret);
1218 }
1219
1220
1221 EGLBoolean EGLAPIENTRY
1222 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1223 {
1224 _EGLDisplay *disp = _eglLockDisplay(dpy);
1225 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1226 _EGLDriver *drv;
1227 EGLBoolean ret;
1228
1229 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1230 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1231 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
1232
1233 RETURN_EGL_EVAL(disp, ret);
1234 }
1235
1236
1237 EGLBoolean EGLAPIENTRY
1238 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1239 {
1240 _EGLDisplay *disp = _eglLockDisplay(dpy);
1241 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1242 _EGLDriver *drv;
1243 EGLBoolean ret;
1244
1245 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1246 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1247 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
1248
1249 RETURN_EGL_EVAL(disp, ret);
1250 }
1251
1252
1253 EGLBoolean EGLAPIENTRY
1254 eglSwapInterval(EGLDisplay dpy, EGLint interval)
1255 {
1256 _EGLDisplay *disp = _eglLockDisplay(dpy);
1257 _EGLContext *ctx = _eglGetCurrentContext();
1258 _EGLSurface *surf = ctx ? ctx->DrawSurface : NULL;
1259 _EGLDriver *drv;
1260 EGLBoolean ret;
1261
1262 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1263 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1264
1265 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1266 ctx->Resource.Display != disp)
1267 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
1268
1269 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
1270 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1271
1272 if (surf->Type != EGL_WINDOW_BIT)
1273 RETURN_EGL_EVAL(disp, EGL_TRUE);
1274
1275 interval = CLAMP(interval,
1276 surf->Config->MinSwapInterval,
1277 surf->Config->MaxSwapInterval);
1278
1279 if (surf->SwapInterval != interval)
1280 ret = drv->API.SwapInterval(drv, disp, surf, interval);
1281 else
1282 ret = EGL_TRUE;
1283
1284 if (ret)
1285 surf->SwapInterval = interval;
1286
1287 RETURN_EGL_EVAL(disp, ret);
1288 }
1289
1290
1291 EGLBoolean EGLAPIENTRY
1292 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1293 {
1294 _EGLContext *ctx = _eglGetCurrentContext();
1295 _EGLDisplay *disp = _eglLockDisplay(dpy);
1296 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1297 _EGLDriver *drv;
1298 EGLBoolean ret;
1299
1300 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1301 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1302
1303 /* surface must be bound to current context in EGL 1.4 */
1304 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1305 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1306 surf != ctx->DrawSurface)
1307 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1308 #endif
1309
1310 if (surf->Type != EGL_WINDOW_BIT)
1311 RETURN_EGL_EVAL(disp, EGL_TRUE);
1312
1313 /* From the EGL 1.5 spec:
1314 *
1315 * If eglSwapBuffers is called and the native window associated with
1316 * surface is no longer valid, an EGL_BAD_NATIVE_WINDOW error is
1317 * generated.
1318 */
1319 if (surf->Lost)
1320 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
1321
1322 ret = drv->API.SwapBuffers(drv, disp, surf);
1323
1324 /* EGL_KHR_partial_update
1325 * Frame boundary successfully reached,
1326 * reset damage region and reset BufferAgeRead
1327 */
1328 if (ret) {
1329 surf->SetDamageRegionCalled = EGL_FALSE;
1330 surf->BufferAgeRead = EGL_FALSE;
1331 }
1332
1333 RETURN_EGL_EVAL(disp, ret);
1334 }
1335
1336
1337 static EGLBoolean
1338 _eglSwapBuffersWithDamageCommon(_EGLDisplay *disp, _EGLSurface *surf,
1339 EGLint *rects, EGLint n_rects)
1340 {
1341 _EGLContext *ctx = _eglGetCurrentContext();
1342 _EGLDriver *drv;
1343 EGLBoolean ret;
1344
1345 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1346
1347 /* surface must be bound to current context in EGL 1.4 */
1348 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1349 surf != ctx->DrawSurface)
1350 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1351
1352 if (surf->Type != EGL_WINDOW_BIT)
1353 RETURN_EGL_EVAL(disp, EGL_TRUE);
1354
1355 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1356 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1357
1358 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1359
1360 /* EGL_KHR_partial_update
1361 * Frame boundary successfully reached,
1362 * reset damage region and reset BufferAgeRead
1363 */
1364 if (ret) {
1365 surf->SetDamageRegionCalled = EGL_FALSE;
1366 surf->BufferAgeRead = EGL_FALSE;
1367 }
1368
1369 RETURN_EGL_EVAL(disp, ret);
1370 }
1371
1372 static EGLBoolean EGLAPIENTRY
1373 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1374 EGLint *rects, EGLint n_rects)
1375 {
1376 _EGLDisplay *disp = _eglLockDisplay(dpy);
1377 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1378 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1379 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1380 }
1381
1382 static EGLBoolean EGLAPIENTRY
1383 eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
1384 EGLint *rects, EGLint n_rects)
1385 {
1386 _EGLDisplay *disp = _eglLockDisplay(dpy);
1387 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1388 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1389 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1390 }
1391
1392 /**
1393 * Clamp the rectangles so that they lie within the surface.
1394 */
1395
1396 static void
1397 _eglSetDamageRegionKHRClampRects(_EGLDisplay* disp, _EGLSurface* surf,
1398 EGLint *rects, EGLint n_rects)
1399 {
1400 EGLint i;
1401 EGLint surf_height = surf->Height;
1402 EGLint surf_width = surf->Width;
1403
1404 for (i = 0; i < (4 * n_rects); i += 4) {
1405 EGLint x1, y1, x2, y2;
1406 x1 = rects[i];
1407 y1 = rects[i + 1];
1408 x2 = rects[i + 2] + x1;
1409 y2 = rects[i + 3] + y1;
1410
1411 rects[i] = CLAMP(x1, 0, surf_width);
1412 rects[i + 1] = CLAMP(y1, 0, surf_height);
1413 rects[i + 2] = CLAMP(x2, 0, surf_width) - rects[i];
1414 rects[i + 3] = CLAMP(y2, 0, surf_height) - rects[i + 1];
1415 }
1416 }
1417
1418 static EGLBoolean EGLAPIENTRY
1419 eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1420 EGLint *rects, EGLint n_rects)
1421 {
1422 _EGLDisplay *disp = _eglLockDisplay(dpy);
1423 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1424 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1425 _EGLContext *ctx = _eglGetCurrentContext();
1426 _EGLDriver *drv;
1427 EGLBoolean ret;
1428 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1429
1430 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1431 surf->Type != EGL_WINDOW_BIT ||
1432 ctx->DrawSurface != surf ||
1433 surf->SwapBehavior != EGL_BUFFER_DESTROYED)
1434 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1435
1436 /* If the damage region is already set or
1437 * buffer age is not queried between
1438 * frame boundaries, throw bad access error
1439 */
1440
1441 if (surf->SetDamageRegionCalled || !surf->BufferAgeRead)
1442 RETURN_EGL_ERROR(disp, EGL_BAD_ACCESS, EGL_FALSE);
1443
1444 _eglSetDamageRegionKHRClampRects(disp, surf, rects, n_rects);
1445 ret = drv->API.SetDamageRegion(drv, disp, surf, rects, n_rects);
1446
1447 if (ret)
1448 surf->SetDamageRegionCalled = EGL_TRUE;
1449
1450 RETURN_EGL_EVAL(disp, ret);
1451 }
1452
1453 EGLBoolean EGLAPIENTRY
1454 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1455 {
1456 _EGLDisplay *disp = _eglLockDisplay(dpy);
1457 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1458 _EGLDriver *drv;
1459 EGLBoolean ret;
1460 void *native_pixmap_ptr;
1461
1462 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1463 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1464 native_pixmap_ptr = (void*) target;
1465
1466 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1467 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1468
1469 RETURN_EGL_EVAL(disp, ret);
1470 }
1471
1472
1473 static EGLBoolean
1474 _eglWaitClientCommon(void)
1475 {
1476 _EGLContext *ctx = _eglGetCurrentContext();
1477 _EGLDisplay *disp;
1478 _EGLDriver *drv;
1479 EGLBoolean ret;
1480
1481 if (!ctx)
1482 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1483
1484 disp = ctx->Resource.Display;
1485 mtx_lock(&disp->Mutex);
1486
1487 /* let bad current context imply bad current surface */
1488 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1489 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1490 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1491
1492 /* a valid current context implies an initialized current display */
1493 assert(disp->Initialized);
1494 drv = disp->Driver;
1495 ret = drv->API.WaitClient(drv, disp, ctx);
1496
1497 RETURN_EGL_EVAL(disp, ret);
1498 }
1499
1500 EGLBoolean EGLAPIENTRY
1501 eglWaitClient(void)
1502 {
1503 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1504 return _eglWaitClientCommon();
1505 }
1506
1507 EGLBoolean EGLAPIENTRY
1508 eglWaitGL(void)
1509 {
1510 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1511 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1512 return _eglWaitClientCommon();
1513 }
1514
1515
1516 EGLBoolean EGLAPIENTRY
1517 eglWaitNative(EGLint engine)
1518 {
1519 _EGLContext *ctx = _eglGetCurrentContext();
1520 _EGLDisplay *disp;
1521 _EGLDriver *drv;
1522 EGLBoolean ret;
1523
1524 if (!ctx)
1525 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1526
1527 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1528
1529 disp = ctx->Resource.Display;
1530 mtx_lock(&disp->Mutex);
1531
1532 /* let bad current context imply bad current surface */
1533 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1534 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1535 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1536
1537 /* a valid current context implies an initialized current display */
1538 assert(disp->Initialized);
1539 drv = disp->Driver;
1540 ret = drv->API.WaitNative(drv, disp, engine);
1541
1542 RETURN_EGL_EVAL(disp, ret);
1543 }
1544
1545
1546 EGLDisplay EGLAPIENTRY
1547 eglGetCurrentDisplay(void)
1548 {
1549 _EGLContext *ctx = _eglGetCurrentContext();
1550 EGLDisplay ret;
1551
1552 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1553
1554 RETURN_EGL_SUCCESS(NULL, ret);
1555 }
1556
1557
1558 EGLContext EGLAPIENTRY
1559 eglGetCurrentContext(void)
1560 {
1561 _EGLContext *ctx = _eglGetCurrentContext();
1562 EGLContext ret;
1563
1564 ret = _eglGetContextHandle(ctx);
1565
1566 RETURN_EGL_SUCCESS(NULL, ret);
1567 }
1568
1569
1570 EGLSurface EGLAPIENTRY
1571 eglGetCurrentSurface(EGLint readdraw)
1572 {
1573 _EGLContext *ctx = _eglGetCurrentContext();
1574 EGLint err = EGL_SUCCESS;
1575 _EGLSurface *surf;
1576 EGLSurface ret;
1577
1578 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_NO_SURFACE);
1579
1580 if (!ctx)
1581 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1582
1583 switch (readdraw) {
1584 case EGL_DRAW:
1585 surf = ctx->DrawSurface;
1586 break;
1587 case EGL_READ:
1588 surf = ctx->ReadSurface;
1589 break;
1590 default:
1591 surf = NULL;
1592 err = EGL_BAD_PARAMETER;
1593 break;
1594 }
1595
1596 ret = _eglGetSurfaceHandle(surf);
1597
1598 RETURN_EGL_ERROR(NULL, err, ret);
1599 }
1600
1601
1602 EGLint EGLAPIENTRY
1603 eglGetError(void)
1604 {
1605 _EGLThreadInfo *t = _eglGetCurrentThread();
1606 EGLint e = t->LastError;
1607 if (!_eglIsCurrentThreadDummy())
1608 t->LastError = EGL_SUCCESS;
1609 return e;
1610 }
1611
1612
1613 /**
1614 ** EGL 1.2
1615 **/
1616
1617 /**
1618 * Specify the client API to use for subsequent calls including:
1619 * eglCreateContext()
1620 * eglGetCurrentContext()
1621 * eglGetCurrentDisplay()
1622 * eglGetCurrentSurface()
1623 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1624 * eglWaitClient()
1625 * eglWaitNative()
1626 * See section 3.7 "Rendering Context" in the EGL specification for details.
1627 */
1628 EGLBoolean EGLAPIENTRY
1629 eglBindAPI(EGLenum api)
1630 {
1631 _EGLThreadInfo *t;
1632
1633 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1634
1635 t = _eglGetCurrentThread();
1636 if (_eglIsCurrentThreadDummy())
1637 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1638
1639 if (!_eglIsApiValid(api))
1640 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1641
1642 t->CurrentAPI = api;
1643
1644 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1645 }
1646
1647
1648 /**
1649 * Return the last value set with eglBindAPI().
1650 */
1651 EGLenum EGLAPIENTRY
1652 eglQueryAPI(void)
1653 {
1654 _EGLThreadInfo *t = _eglGetCurrentThread();
1655 EGLenum ret;
1656
1657 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1658 ret = t->CurrentAPI;
1659
1660 RETURN_EGL_SUCCESS(NULL, ret);
1661 }
1662
1663
1664 EGLSurface EGLAPIENTRY
1665 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1666 EGLClientBuffer buffer, EGLConfig config,
1667 const EGLint *attrib_list)
1668 {
1669 _EGLDisplay *disp = _eglLockDisplay(dpy);
1670 _EGLConfig *conf = _eglLookupConfig(config, disp);
1671 _EGLDriver *drv;
1672 _EGLSurface *surf;
1673 EGLSurface ret;
1674
1675 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1676
1677 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1678
1679 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1680 conf, attrib_list);
1681 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1682
1683 RETURN_EGL_EVAL(disp, ret);
1684 }
1685
1686
1687 EGLBoolean EGLAPIENTRY
1688 eglReleaseThread(void)
1689 {
1690 /* unbind current contexts */
1691 if (!_eglIsCurrentThreadDummy()) {
1692 _EGLThreadInfo *t = _eglGetCurrentThread();
1693 _EGLContext *ctx = t->CurrentContext;
1694
1695 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1696
1697 if (ctx) {
1698 _EGLDisplay *disp = ctx->Resource.Display;
1699 _EGLDriver *drv;
1700
1701 mtx_lock(&disp->Mutex);
1702 drv = disp->Driver;
1703 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1704 mtx_unlock(&disp->Mutex);
1705 }
1706 }
1707
1708 _eglDestroyCurrentThread();
1709
1710 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1711 }
1712
1713
1714 static EGLImage
1715 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1716 EGLClientBuffer buffer, const EGLint *attr_list)
1717 {
1718 _EGLContext *context = _eglLookupContext(ctx, disp);
1719 _EGLDriver *drv;
1720 _EGLImage *img;
1721 EGLImage ret;
1722
1723 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1724 if (!disp->Extensions.KHR_image_base)
1725 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1726 if (!context && ctx != EGL_NO_CONTEXT)
1727 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1728 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1729 * <ctx> must be EGL_NO_CONTEXT..."
1730 */
1731 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1732 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1733
1734 img = drv->API.CreateImageKHR(drv, disp, context, target,
1735 buffer, attr_list);
1736 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1737
1738 RETURN_EGL_EVAL(disp, ret);
1739 }
1740
1741 static EGLImage EGLAPIENTRY
1742 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1743 EGLClientBuffer buffer, const EGLint *attr_list)
1744 {
1745 _EGLDisplay *disp = _eglLockDisplay(dpy);
1746 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1747 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1748 }
1749
1750
1751 EGLImage EGLAPIENTRY
1752 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1753 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1754 {
1755 _EGLDisplay *disp = _eglLockDisplay(dpy);
1756 EGLImage image;
1757 EGLint *int_attribs;
1758
1759 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1760
1761 int_attribs = _eglConvertAttribsToInt(attr_list);
1762 if (attr_list && !int_attribs)
1763 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1764
1765 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1766 free(int_attribs);
1767 return image;
1768 }
1769
1770
1771 static EGLBoolean
1772 _eglDestroyImageCommon(_EGLDisplay *disp, _EGLImage *img)
1773 {
1774 _EGLDriver *drv;
1775 EGLBoolean ret;
1776
1777 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1778 if (!disp->Extensions.KHR_image_base)
1779 RETURN_EGL_EVAL(disp, EGL_FALSE);
1780 if (!img)
1781 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1782
1783 _eglUnlinkImage(img);
1784 ret = drv->API.DestroyImageKHR(drv, disp, img);
1785
1786 RETURN_EGL_EVAL(disp, ret);
1787 }
1788
1789 EGLBoolean EGLAPIENTRY
1790 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1791 {
1792 _EGLDisplay *disp = _eglLockDisplay(dpy);
1793 _EGLImage *img = _eglLookupImage(image, disp);
1794 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1795 return _eglDestroyImageCommon(disp, img);
1796 }
1797
1798 static EGLBoolean EGLAPIENTRY
1799 eglDestroyImageKHR(EGLDisplay dpy, EGLImage image)
1800 {
1801 _EGLDisplay *disp = _eglLockDisplay(dpy);
1802 _EGLImage *img = _eglLookupImage(image, disp);
1803 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1804 return _eglDestroyImageCommon(disp, img);
1805 }
1806
1807
1808 static EGLSync
1809 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list,
1810 EGLBoolean orig_is_EGLAttrib,
1811 EGLenum invalid_type_error)
1812 {
1813 _EGLContext *ctx = _eglGetCurrentContext();
1814 _EGLDriver *drv;
1815 _EGLSync *sync;
1816 EGLSync ret;
1817
1818 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1819
1820 if (!disp->Extensions.KHR_cl_event2 && orig_is_EGLAttrib) {
1821 /* There exist two EGLAttrib variants of eglCreateSync*:
1822 * eglCreateSync64KHR which requires EGL_KHR_cl_event2, and eglCreateSync
1823 * which requires EGL 1.5. Here we use the presence of EGL_KHR_cl_event2
1824 * support as a proxy for EGL 1.5 support, even though that's not
1825 * entirely correct (though _eglComputeVersion does the same).
1826 *
1827 * The EGL spec provides no guidance on how to handle unsupported
1828 * functions. EGL_BAD_MATCH seems reasonable.
1829 */
1830 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1831 }
1832
1833 /* If type is EGL_SYNC_FENCE and no context is current for the bound API
1834 * (i.e., eglGetCurrentContext returns EGL_NO_CONTEXT ), an EGL_BAD_MATCH
1835 * error is generated.
1836 */
1837 if (!ctx &&
1838 (type == EGL_SYNC_FENCE_KHR || type == EGL_SYNC_NATIVE_FENCE_ANDROID))
1839 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1840
1841 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1842 if (ctx && (ctx->Resource.Display != disp ||
1843 ctx->ClientAPI != EGL_OPENGL_ES_API))
1844 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1845
1846 switch (type) {
1847 case EGL_SYNC_FENCE_KHR:
1848 if (!disp->Extensions.KHR_fence_sync)
1849 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1850 break;
1851 case EGL_SYNC_REUSABLE_KHR:
1852 if (!disp->Extensions.KHR_reusable_sync)
1853 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1854 break;
1855 case EGL_SYNC_CL_EVENT_KHR:
1856 if (!disp->Extensions.KHR_cl_event2)
1857 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1858 break;
1859 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1860 if (!disp->Extensions.ANDROID_native_fence_sync)
1861 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1862 break;
1863 default:
1864 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1865 }
1866
1867 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list);
1868 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1869
1870 RETURN_EGL_EVAL(disp, ret);
1871 }
1872
1873
1874 static EGLSync EGLAPIENTRY
1875 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *int_list)
1876 {
1877 _EGLDisplay *disp = _eglLockDisplay(dpy);
1878 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1879
1880 EGLSync sync;
1881 EGLAttrib *attrib_list;
1882 EGLint err;
1883
1884 if (sizeof(int_list[0]) == sizeof(attrib_list[0])) {
1885 attrib_list = (EGLAttrib *) int_list;
1886 } else {
1887 err = _eglConvertIntsToAttribs(int_list, &attrib_list);
1888 if (err != EGL_SUCCESS)
1889 RETURN_EGL_ERROR(disp, err, EGL_NO_SYNC);
1890 }
1891
1892 sync = _eglCreateSync(disp, type, attrib_list, EGL_FALSE,
1893 EGL_BAD_ATTRIBUTE);
1894
1895 if (sizeof(int_list[0]) != sizeof(attrib_list[0]))
1896 free(attrib_list);
1897
1898 /* Don't double-unlock the display. _eglCreateSync already unlocked it. */
1899 return sync;
1900 }
1901
1902
1903 static EGLSync EGLAPIENTRY
1904 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1905 {
1906 _EGLDisplay *disp = _eglLockDisplay(dpy);
1907 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1908 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1909 EGL_BAD_ATTRIBUTE);
1910 }
1911
1912
1913 EGLSync EGLAPIENTRY
1914 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1915 {
1916 _EGLDisplay *disp = _eglLockDisplay(dpy);
1917 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1918 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1919 EGL_BAD_PARAMETER);
1920 }
1921
1922
1923 static EGLBoolean
1924 _eglDestroySync(_EGLDisplay *disp, _EGLSync *s)
1925 {
1926 _EGLDriver *drv;
1927 EGLBoolean ret;
1928
1929 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1930 assert(disp->Extensions.KHR_reusable_sync ||
1931 disp->Extensions.KHR_fence_sync ||
1932 disp->Extensions.ANDROID_native_fence_sync);
1933
1934 _eglUnlinkSync(s);
1935 ret = drv->API.DestroySyncKHR(drv, disp, s);
1936
1937 RETURN_EGL_EVAL(disp, ret);
1938 }
1939
1940 EGLBoolean EGLAPIENTRY
1941 eglDestroySync(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 static EGLBoolean EGLAPIENTRY
1950 eglDestroySyncKHR(EGLDisplay dpy, EGLSync sync)
1951 {
1952 _EGLDisplay *disp = _eglLockDisplay(dpy);
1953 _EGLSync *s = _eglLookupSync(sync, disp);
1954 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1955 return _eglDestroySync(disp, s);
1956 }
1957
1958
1959 static EGLint
1960 _eglClientWaitSyncCommon(_EGLDisplay *disp, EGLDisplay dpy,
1961 _EGLSync *s, EGLint flags, EGLTime timeout)
1962 {
1963 _EGLDriver *drv;
1964 EGLint ret;
1965
1966 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1967 assert(disp->Extensions.KHR_reusable_sync ||
1968 disp->Extensions.KHR_fence_sync ||
1969 disp->Extensions.ANDROID_native_fence_sync);
1970
1971 if (s->SyncStatus == EGL_SIGNALED_KHR)
1972 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1973
1974 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1975 * unlocked here to allow other threads also to be able to
1976 * go into waiting state.
1977 */
1978
1979 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1980 _eglUnlockDisplay(dpy);
1981
1982 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1983
1984 /*
1985 * 'disp' is already unlocked for reusable sync type,
1986 * so passing 'NULL' to bypass unlocking display.
1987 */
1988 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1989 RETURN_EGL_EVAL(NULL, ret);
1990 else
1991 RETURN_EGL_EVAL(disp, ret);
1992 }
1993
1994 EGLint EGLAPIENTRY
1995 eglClientWaitSync(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 static EGLint EGLAPIENTRY
2005 eglClientWaitSyncKHR(EGLDisplay dpy, EGLSync sync,
2006 EGLint flags, EGLTime timeout)
2007 {
2008 _EGLDisplay *disp = _eglLockDisplay(dpy);
2009 _EGLSync *s = _eglLookupSync(sync, disp);
2010 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2011 return _eglClientWaitSyncCommon(disp, dpy, s, flags, timeout);
2012 }
2013
2014
2015 static EGLint
2016 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
2017 {
2018 _EGLContext *ctx = _eglGetCurrentContext();
2019 _EGLDriver *drv;
2020 EGLint ret;
2021
2022 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2023 assert(disp->Extensions.KHR_wait_sync);
2024
2025 /* return an error if the client API doesn't support GL_OES_EGL_sync */
2026 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
2027 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
2028
2029 /* the API doesn't allow any flags yet */
2030 if (flags != 0)
2031 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2032
2033 ret = drv->API.WaitSyncKHR(drv, disp, s);
2034
2035 RETURN_EGL_EVAL(disp, ret);
2036 }
2037
2038 static EGLint EGLAPIENTRY
2039 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
2040 {
2041 _EGLDisplay *disp = _eglLockDisplay(dpy);
2042 _EGLSync *s = _eglLookupSync(sync, disp);
2043 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2044 return _eglWaitSyncCommon(disp, s, flags);
2045 }
2046
2047
2048 EGLBoolean EGLAPIENTRY
2049 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
2050 {
2051 /* The KHR version returns EGLint, while the core version returns
2052 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
2053 * EGL_TRUE.
2054 */
2055 _EGLDisplay *disp = _eglLockDisplay(dpy);
2056 _EGLSync *s = _eglLookupSync(sync, disp);
2057 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2058 return _eglWaitSyncCommon(disp, s, flags);
2059 }
2060
2061
2062 static EGLBoolean EGLAPIENTRY
2063 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
2064 {
2065 _EGLDisplay *disp = _eglLockDisplay(dpy);
2066 _EGLSync *s = _eglLookupSync(sync, disp);
2067 _EGLDriver *drv;
2068 EGLBoolean ret;
2069
2070 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2071
2072 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2073 assert(disp->Extensions.KHR_reusable_sync);
2074 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
2075
2076 RETURN_EGL_EVAL(disp, ret);
2077 }
2078
2079
2080 static EGLBoolean
2081 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
2082 {
2083 _EGLDriver *drv;
2084 EGLBoolean ret;
2085
2086 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2087 assert(disp->Extensions.KHR_reusable_sync ||
2088 disp->Extensions.KHR_fence_sync ||
2089 disp->Extensions.ANDROID_native_fence_sync);
2090 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
2091
2092 RETURN_EGL_EVAL(disp, ret);
2093 }
2094
2095 EGLBoolean EGLAPIENTRY
2096 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
2097 {
2098 _EGLDisplay *disp = _eglLockDisplay(dpy);
2099 _EGLSync *s = _eglLookupSync(sync, disp);
2100 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2101 return _eglGetSyncAttribCommon(disp, s, attribute, value);
2102 }
2103
2104
2105 static EGLBoolean EGLAPIENTRY
2106 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
2107 {
2108 _EGLDisplay *disp = _eglLockDisplay(dpy);
2109 _EGLSync *s = _eglLookupSync(sync, disp);
2110 EGLAttrib attrib;
2111 EGLBoolean result;
2112
2113 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2114
2115 if (!value)
2116 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2117
2118 attrib = *value;
2119 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
2120
2121 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
2122 *
2123 * If any error occurs, <*value> is not modified.
2124 */
2125 if (result == EGL_FALSE)
2126 return result;
2127
2128 *value = attrib;
2129 return result;
2130 }
2131
2132 static EGLint EGLAPIENTRY
2133 eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSync sync)
2134 {
2135 _EGLDisplay *disp = _eglLockDisplay(dpy);
2136 _EGLSync *s = _eglLookupSync(sync, disp);
2137 _EGLDriver *drv;
2138 EGLBoolean ret;
2139
2140 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2141
2142 /* the spec doesn't seem to specify what happens if the fence
2143 * type is not EGL_SYNC_NATIVE_FENCE_ANDROID, but this seems
2144 * sensible:
2145 */
2146 if (!(s && (s->Type == EGL_SYNC_NATIVE_FENCE_ANDROID)))
2147 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_NATIVE_FENCE_FD_ANDROID);
2148
2149 _EGL_CHECK_SYNC(disp, s, EGL_NO_NATIVE_FENCE_FD_ANDROID, drv);
2150 assert(disp->Extensions.ANDROID_native_fence_sync);
2151 ret = drv->API.DupNativeFenceFDANDROID(drv, disp, s);
2152
2153 RETURN_EGL_EVAL(disp, ret);
2154 }
2155
2156 static EGLBoolean EGLAPIENTRY
2157 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
2158 EGLint numRects, const EGLint *rects)
2159 {
2160 _EGLContext *ctx = _eglGetCurrentContext();
2161 _EGLDisplay *disp = _eglLockDisplay(dpy);
2162 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2163 _EGLDriver *drv;
2164 EGLBoolean ret;
2165
2166 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2167
2168 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2169
2170 if (!disp->Extensions.NOK_swap_region)
2171 RETURN_EGL_EVAL(disp, EGL_FALSE);
2172
2173 /* surface must be bound to current context in EGL 1.4 */
2174 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
2175 surf != ctx->DrawSurface)
2176 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
2177
2178 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
2179
2180 RETURN_EGL_EVAL(disp, ret);
2181 }
2182
2183
2184 static EGLImage EGLAPIENTRY
2185 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
2186 {
2187 _EGLDisplay *disp = _eglLockDisplay(dpy);
2188 _EGLDriver *drv;
2189 _EGLImage *img;
2190 EGLImage ret;
2191
2192 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2193
2194 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
2195 if (!disp->Extensions.MESA_drm_image)
2196 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
2197
2198 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
2199 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
2200
2201 RETURN_EGL_EVAL(disp, ret);
2202 }
2203
2204 static EGLBoolean EGLAPIENTRY
2205 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
2206 EGLint *name, EGLint *handle, EGLint *stride)
2207 {
2208 _EGLDisplay *disp = _eglLockDisplay(dpy);
2209 _EGLImage *img = _eglLookupImage(image, disp);
2210 _EGLDriver *drv;
2211 EGLBoolean ret;
2212
2213 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2214
2215 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2216 assert(disp->Extensions.MESA_drm_image);
2217
2218 if (!img)
2219 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2220
2221 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
2222
2223 RETURN_EGL_EVAL(disp, ret);
2224 }
2225
2226
2227 struct wl_display;
2228
2229 static EGLBoolean EGLAPIENTRY
2230 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2231 {
2232 _EGLDisplay *disp = _eglLockDisplay(dpy);
2233 _EGLDriver *drv;
2234 EGLBoolean ret;
2235
2236 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2237
2238 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2239 assert(disp->Extensions.WL_bind_wayland_display);
2240
2241 if (!display)
2242 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2243
2244 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
2245
2246 RETURN_EGL_EVAL(disp, ret);
2247 }
2248
2249 static EGLBoolean EGLAPIENTRY
2250 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2251 {
2252 _EGLDisplay *disp = _eglLockDisplay(dpy);
2253 _EGLDriver *drv;
2254 EGLBoolean ret;
2255
2256 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2257
2258 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2259 assert(disp->Extensions.WL_bind_wayland_display);
2260
2261 if (!display)
2262 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2263
2264 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
2265
2266 RETURN_EGL_EVAL(disp, ret);
2267 }
2268
2269 static EGLBoolean EGLAPIENTRY
2270 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
2271 EGLint attribute, EGLint *value)
2272 {
2273 _EGLDisplay *disp = _eglLockDisplay(dpy);
2274 _EGLDriver *drv;
2275 EGLBoolean ret;
2276
2277 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2278
2279 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2280 assert(disp->Extensions.WL_bind_wayland_display);
2281
2282 if (!buffer)
2283 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2284
2285 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
2286
2287 RETURN_EGL_EVAL(disp, ret);
2288 }
2289
2290
2291 static struct wl_buffer * EGLAPIENTRY
2292 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
2293 {
2294 _EGLDisplay *disp = _eglLockDisplay(dpy);
2295 _EGLImage *img;
2296 _EGLDriver *drv;
2297 struct wl_buffer *ret;
2298
2299 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2300
2301 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2302 if (!disp->Extensions.WL_create_wayland_buffer_from_image)
2303 RETURN_EGL_EVAL(disp, NULL);
2304
2305 img = _eglLookupImage(image, disp);
2306
2307 if (!img)
2308 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
2309
2310 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
2311
2312 RETURN_EGL_EVAL(disp, ret);
2313 }
2314
2315 static EGLBoolean EGLAPIENTRY
2316 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
2317 EGLint x, EGLint y, EGLint width, EGLint height)
2318 {
2319 _EGLDisplay *disp = _eglLockDisplay(dpy);
2320 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2321 _EGLDriver *drv;
2322 EGLBoolean ret;
2323
2324 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2325
2326 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2327
2328 if (!disp->Extensions.NV_post_sub_buffer)
2329 RETURN_EGL_EVAL(disp, EGL_FALSE);
2330
2331 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
2332
2333 RETURN_EGL_EVAL(disp, ret);
2334 }
2335
2336 static EGLBoolean EGLAPIENTRY
2337 eglGetSyncValuesCHROMIUM(EGLDisplay dpy, EGLSurface surface,
2338 EGLuint64KHR *ust, EGLuint64KHR *msc,
2339 EGLuint64KHR *sbc)
2340 {
2341 _EGLDisplay *disp = _eglLockDisplay(dpy);
2342 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2343 _EGLDriver *drv;
2344 EGLBoolean ret;
2345
2346 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2347
2348 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2349 if (!disp->Extensions.CHROMIUM_sync_control)
2350 RETURN_EGL_EVAL(disp, EGL_FALSE);
2351
2352 if (!ust || !msc || !sbc)
2353 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2354
2355 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
2356
2357 RETURN_EGL_EVAL(disp, ret);
2358 }
2359
2360 static EGLBoolean EGLAPIENTRY
2361 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
2362 EGLint *fourcc, EGLint *nplanes,
2363 EGLuint64KHR *modifiers)
2364 {
2365 _EGLDisplay *disp = _eglLockDisplay(dpy);
2366 _EGLImage *img = _eglLookupImage(image, disp);
2367 _EGLDriver *drv;
2368 EGLBoolean ret;
2369
2370 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2371
2372 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2373 assert(disp->Extensions.MESA_image_dma_buf_export);
2374
2375 if (!img)
2376 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2377
2378 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
2379 modifiers);
2380
2381 RETURN_EGL_EVAL(disp, ret);
2382 }
2383
2384 static EGLBoolean EGLAPIENTRY
2385 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
2386 int *fds, EGLint *strides, EGLint *offsets)
2387 {
2388 _EGLDisplay *disp = _eglLockDisplay(dpy);
2389 _EGLImage *img = _eglLookupImage(image, disp);
2390 _EGLDriver *drv;
2391 EGLBoolean ret;
2392
2393 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2394
2395 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2396 assert(disp->Extensions.MESA_image_dma_buf_export);
2397
2398 if (!img)
2399 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2400
2401 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
2402
2403 RETURN_EGL_EVAL(disp, ret);
2404 }
2405
2406 static EGLint EGLAPIENTRY
2407 eglLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object,
2408 EGLLabelKHR label)
2409 {
2410 _EGLDisplay *disp = NULL;
2411 _EGLResourceType type;
2412
2413 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2414
2415 if (objectType == EGL_OBJECT_THREAD_KHR) {
2416 _EGLThreadInfo *t = _eglGetCurrentThread();
2417
2418 if (!_eglIsCurrentThreadDummy()) {
2419 t->Label = label;
2420 return EGL_SUCCESS;
2421 }
2422
2423 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_BAD_ALLOC);
2424 }
2425
2426 disp = _eglLockDisplay(dpy);
2427 if (disp == NULL)
2428 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_BAD_DISPLAY);
2429
2430 if (objectType == EGL_OBJECT_DISPLAY_KHR) {
2431 if (dpy != (EGLDisplay) object)
2432 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2433
2434 disp->Label = label;
2435 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2436 }
2437
2438 switch (objectType) {
2439 case EGL_OBJECT_CONTEXT_KHR:
2440 type = _EGL_RESOURCE_CONTEXT;
2441 break;
2442 case EGL_OBJECT_SURFACE_KHR:
2443 type = _EGL_RESOURCE_SURFACE;
2444 break;
2445 case EGL_OBJECT_IMAGE_KHR:
2446 type = _EGL_RESOURCE_IMAGE;
2447 break;
2448 case EGL_OBJECT_SYNC_KHR:
2449 type = _EGL_RESOURCE_SYNC;
2450 break;
2451 case EGL_OBJECT_STREAM_KHR:
2452 default:
2453 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2454 }
2455
2456 if (_eglCheckResource(object, type, disp)) {
2457 _EGLResource *res = (_EGLResource *) object;
2458
2459 res->Label = label;
2460 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2461 }
2462
2463 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2464 }
2465
2466 static EGLint EGLAPIENTRY
2467 eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
2468 const EGLAttrib *attrib_list)
2469 {
2470 unsigned int newEnabled;
2471
2472 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2473
2474 mtx_lock(_eglGlobal.Mutex);
2475
2476 newEnabled = _eglGlobal.debugTypesEnabled;
2477 if (attrib_list != NULL) {
2478 int i;
2479
2480 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
2481 switch (attrib_list[i]) {
2482 case EGL_DEBUG_MSG_CRITICAL_KHR:
2483 case EGL_DEBUG_MSG_ERROR_KHR:
2484 case EGL_DEBUG_MSG_WARN_KHR:
2485 case EGL_DEBUG_MSG_INFO_KHR:
2486 if (attrib_list[i + 1])
2487 newEnabled |= DebugBitFromType(attrib_list[i]);
2488 else
2489 newEnabled &= ~DebugBitFromType(attrib_list[i]);
2490 break;
2491 default:
2492 // On error, set the last error code, call the current
2493 // debug callback, and return the error code.
2494 mtx_unlock(_eglGlobal.Mutex);
2495 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2496 "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
2497 return EGL_BAD_ATTRIBUTE;
2498 }
2499 }
2500 }
2501
2502 if (callback != NULL) {
2503 _eglGlobal.debugCallback = callback;
2504 _eglGlobal.debugTypesEnabled = newEnabled;
2505 } else {
2506 _eglGlobal.debugCallback = NULL;
2507 _eglGlobal.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR;
2508 }
2509
2510 mtx_unlock(_eglGlobal.Mutex);
2511 return EGL_SUCCESS;
2512 }
2513
2514 static EGLBoolean EGLAPIENTRY
2515 eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
2516 {
2517 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2518
2519 mtx_lock(_eglGlobal.Mutex);
2520
2521 switch (attribute) {
2522 case EGL_DEBUG_MSG_CRITICAL_KHR:
2523 case EGL_DEBUG_MSG_ERROR_KHR:
2524 case EGL_DEBUG_MSG_WARN_KHR:
2525 case EGL_DEBUG_MSG_INFO_KHR:
2526 if (_eglGlobal.debugTypesEnabled & DebugBitFromType(attribute))
2527 *value = EGL_TRUE;
2528 else
2529 *value = EGL_FALSE;
2530 break;
2531 case EGL_DEBUG_CALLBACK_KHR:
2532 *value = (EGLAttrib) _eglGlobal.debugCallback;
2533 break;
2534 default:
2535 mtx_unlock(_eglGlobal.Mutex);
2536 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2537 "Invalid attribute 0x%04lx", (unsigned long) attribute);
2538 return EGL_FALSE;
2539 }
2540
2541 mtx_unlock(_eglGlobal.Mutex);
2542 return EGL_TRUE;
2543 }
2544
2545 static int
2546 _eglFunctionCompare(const void *key, const void *elem)
2547 {
2548 const char *procname = key;
2549 const struct _egl_entrypoint *entrypoint = elem;
2550 return strcmp(procname, entrypoint->name);
2551 }
2552
2553 static EGLBoolean EGLAPIENTRY
2554 eglQueryDmaBufFormatsEXT(EGLDisplay dpy, EGLint max_formats,
2555 EGLint *formats, EGLint *num_formats)
2556 {
2557 _EGLDisplay *disp = _eglLockDisplay(dpy);
2558 _EGLDriver *drv;
2559 EGLBoolean ret;
2560
2561 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2562
2563 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2564
2565 ret = drv->API.QueryDmaBufFormatsEXT(drv, disp, max_formats, formats,
2566 num_formats);
2567
2568 RETURN_EGL_EVAL(disp, ret);
2569 }
2570
2571 static EGLBoolean EGLAPIENTRY
2572 eglQueryDmaBufModifiersEXT(EGLDisplay dpy, EGLint format, EGLint max_modifiers,
2573 EGLuint64KHR *modifiers, EGLBoolean *external_only,
2574 EGLint *num_modifiers)
2575 {
2576 _EGLDisplay *disp = _eglLockDisplay(dpy);
2577 _EGLDriver *drv;
2578 EGLBoolean ret;
2579
2580 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2581
2582 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2583
2584 ret = drv->API.QueryDmaBufModifiersEXT(drv, disp, format, max_modifiers,
2585 modifiers, external_only,
2586 num_modifiers);
2587
2588 RETURN_EGL_EVAL(disp, ret);
2589 }
2590
2591 static void EGLAPIENTRY
2592 eglSetBlobCacheFuncsANDROID(EGLDisplay *dpy, EGLSetBlobFuncANDROID set,
2593 EGLGetBlobFuncANDROID get)
2594 {
2595 /* This function does not return anything so we cannot
2596 * utilize the helper macros _EGL_FUNC_START or _EGL_CHECK_DISPLAY.
2597 */
2598 _EGLDisplay *disp = _eglLockDisplay(dpy);
2599 if (!_eglSetFuncName(__func__, disp, EGL_OBJECT_DISPLAY_KHR, NULL)) {
2600 if (disp)
2601 _eglUnlockDisplay(disp);
2602 return;
2603 }
2604
2605 _EGLDriver *drv = _eglCheckDisplay(disp, __func__);
2606 if (!drv) {
2607 if (disp)
2608 _eglUnlockDisplay(disp);
2609 return;
2610 }
2611
2612 if (!set || !get) {
2613 _eglError(EGL_BAD_PARAMETER,
2614 "eglSetBlobCacheFuncsANDROID: NULL handler given");
2615 _eglUnlockDisplay(disp);
2616 return;
2617 }
2618
2619 if (disp->BlobCacheSet) {
2620 _eglError(EGL_BAD_PARAMETER,
2621 "eglSetBlobCacheFuncsANDROID: functions already set");
2622 _eglUnlockDisplay(disp);
2623 return;
2624 }
2625
2626 disp->BlobCacheSet = set;
2627 disp->BlobCacheGet = get;
2628
2629 drv->API.SetBlobCacheFuncsANDROID(drv, disp, set, get);
2630
2631 _eglUnlockDisplay(disp);
2632 }
2633
2634 static EGLBoolean EGLAPIENTRY
2635 eglQueryDeviceAttribEXT(EGLDeviceEXT device,
2636 EGLint attribute,
2637 EGLAttrib *value)
2638 {
2639 _EGLDevice *dev = _eglLookupDevice(device);
2640 EGLBoolean ret;
2641
2642 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2643 if (!dev)
2644 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, EGL_FALSE);
2645
2646 ret = _eglQueryDeviceAttribEXT(dev, attribute, value);
2647 RETURN_EGL_EVAL(NULL, ret);
2648 }
2649
2650 static const char * EGLAPIENTRY
2651 eglQueryDeviceStringEXT(EGLDeviceEXT device,
2652 EGLint name)
2653 {
2654 _EGLDevice *dev = _eglLookupDevice(device);
2655
2656 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2657 if (!dev)
2658 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, NULL);
2659
2660 RETURN_EGL_EVAL(NULL, _eglQueryDeviceStringEXT(dev, name));
2661 }
2662
2663 static EGLBoolean EGLAPIENTRY
2664 eglQueryDevicesEXT(EGLint max_devices,
2665 EGLDeviceEXT *devices,
2666 EGLint *num_devices)
2667 {
2668 EGLBoolean ret;
2669
2670 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2671 ret = _eglQueryDevicesEXT(max_devices, (_EGLDevice **) devices,
2672 num_devices);
2673 RETURN_EGL_EVAL(NULL, ret);
2674 }
2675
2676 static EGLBoolean EGLAPIENTRY
2677 eglQueryDisplayAttribEXT(EGLDisplay dpy,
2678 EGLint attribute,
2679 EGLAttrib *value)
2680 {
2681 _EGLDisplay *disp = _eglLockDisplay(dpy);
2682 _EGLDriver *drv;
2683
2684 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2685 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2686
2687 switch (attribute) {
2688 case EGL_DEVICE_EXT:
2689 *value = (EGLAttrib) disp->Device;
2690 break;
2691 default:
2692 RETURN_EGL_ERROR(disp, EGL_BAD_ATTRIBUTE, EGL_FALSE);
2693 }
2694 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
2695 }
2696
2697 static char * EGLAPIENTRY
2698 eglGetDisplayDriverConfig(EGLDisplay dpy)
2699 {
2700 _EGLDisplay *disp = _eglLockDisplay(dpy);
2701 _EGLDriver *drv;
2702 char *ret;
2703
2704 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2705 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2706
2707 assert(disp->Extensions.MESA_query_driver);
2708
2709 ret = drv->API.QueryDriverConfig(disp);
2710 RETURN_EGL_EVAL(disp, ret);
2711 }
2712
2713 static const char * EGLAPIENTRY
2714 eglGetDisplayDriverName(EGLDisplay dpy)
2715 {
2716 _EGLDisplay *disp = _eglLockDisplay(dpy);
2717 _EGLDriver *drv;
2718 const char *ret;
2719
2720 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2721 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2722
2723 assert(disp->Extensions.MESA_query_driver);
2724
2725 ret = drv->API.QueryDriverName(disp);
2726 RETURN_EGL_EVAL(disp, ret);
2727 }
2728
2729 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
2730 eglGetProcAddress(const char *procname)
2731 {
2732 static const struct _egl_entrypoint egl_functions[] = {
2733 #define EGL_ENTRYPOINT(f) { .name = #f, .function = (_EGLProc) f },
2734 #include "eglentrypoint.h"
2735 #undef EGL_ENTRYPOINT
2736 };
2737 _EGLProc ret = NULL;
2738
2739 if (!procname)
2740 RETURN_EGL_SUCCESS(NULL, NULL);
2741
2742 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2743
2744 if (strncmp(procname, "egl", 3) == 0) {
2745 const struct _egl_entrypoint *entrypoint =
2746 bsearch(procname,
2747 egl_functions, ARRAY_SIZE(egl_functions),
2748 sizeof(egl_functions[0]),
2749 _eglFunctionCompare);
2750 if (entrypoint)
2751 ret = entrypoint->function;
2752 }
2753
2754 if (!ret)
2755 ret = _eglGetDriverProc(procname);
2756
2757 RETURN_EGL_SUCCESS(NULL, ret);
2758 }
2759
2760 static int
2761 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
2762 _EGLDisplay **disp, _EGLDriver **drv,
2763 _EGLContext **ctx)
2764 {
2765
2766 *disp = _eglLockDisplay(dpy);
2767 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
2768 if (*disp)
2769 _eglUnlockDisplay(*disp);
2770 return MESA_GLINTEROP_INVALID_DISPLAY;
2771 }
2772
2773 *drv = (*disp)->Driver;
2774
2775 *ctx = _eglLookupContext(context, *disp);
2776 if (!*ctx ||
2777 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
2778 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
2779 _eglUnlockDisplay(*disp);
2780 return MESA_GLINTEROP_INVALID_CONTEXT;
2781 }
2782
2783 return MESA_GLINTEROP_SUCCESS;
2784 }
2785
2786 PUBLIC int
2787 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
2788 struct mesa_glinterop_device_info *out)
2789 {
2790 _EGLDisplay *disp;
2791 _EGLDriver *drv;
2792 _EGLContext *ctx;
2793 int ret;
2794
2795 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2796 if (ret != MESA_GLINTEROP_SUCCESS)
2797 return ret;
2798
2799 if (drv->API.GLInteropQueryDeviceInfo)
2800 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
2801 else
2802 ret = MESA_GLINTEROP_UNSUPPORTED;
2803
2804 _eglUnlockDisplay(disp);
2805 return ret;
2806 }
2807
2808 PUBLIC int
2809 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2810 struct mesa_glinterop_export_in *in,
2811 struct mesa_glinterop_export_out *out)
2812 {
2813 _EGLDisplay *disp;
2814 _EGLDriver *drv;
2815 _EGLContext *ctx;
2816 int ret;
2817
2818 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2819 if (ret != MESA_GLINTEROP_SUCCESS)
2820 return ret;
2821
2822 if (drv->API.GLInteropExportObject)
2823 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
2824 else
2825 ret = MESA_GLINTEROP_UNSUPPORTED;
2826
2827 _eglUnlockDisplay(disp);
2828 return ret;
2829 }