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