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