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