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