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