egl: Factor out _eglGetSyncAttribCommon
[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 "GL/mesa_glinterop.h"
92 #include "eglcompiler.h"
93
94 #include "eglglobals.h"
95 #include "eglcontext.h"
96 #include "egldisplay.h"
97 #include "egltypedefs.h"
98 #include "eglcurrent.h"
99 #include "egldriver.h"
100 #include "eglsurface.h"
101 #include "eglconfig.h"
102 #include "eglimage.h"
103 #include "eglsync.h"
104
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 static inline _EGLDriver *
161 _eglCheckDisplay(_EGLDisplay *disp, const char *msg)
162 {
163 if (!disp) {
164 _eglError(EGL_BAD_DISPLAY, msg);
165 return NULL;
166 }
167 if (!disp->Initialized) {
168 _eglError(EGL_NOT_INITIALIZED, msg);
169 return NULL;
170 }
171 return disp->Driver;
172 }
173
174
175 static inline _EGLDriver *
176 _eglCheckSurface(_EGLDisplay *disp, _EGLSurface *surf, const char *msg)
177 {
178 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
179 if (!drv)
180 return NULL;
181 if (!surf) {
182 _eglError(EGL_BAD_SURFACE, msg);
183 return NULL;
184 }
185 return drv;
186 }
187
188
189 static inline _EGLDriver *
190 _eglCheckContext(_EGLDisplay *disp, _EGLContext *context, const char *msg)
191 {
192 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
193 if (!drv)
194 return NULL;
195 if (!context) {
196 _eglError(EGL_BAD_CONTEXT, msg);
197 return NULL;
198 }
199 return drv;
200 }
201
202
203 static inline _EGLDriver *
204 _eglCheckConfig(_EGLDisplay *disp, _EGLConfig *conf, const char *msg)
205 {
206 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
207 if (!drv)
208 return NULL;
209 if (!conf) {
210 _eglError(EGL_BAD_CONFIG, msg);
211 return NULL;
212 }
213 return drv;
214 }
215
216
217 static inline _EGLDriver *
218 _eglCheckSync(_EGLDisplay *disp, _EGLSync *s, const char *msg)
219 {
220 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
221 if (!drv)
222 return NULL;
223 if (!s) {
224 _eglError(EGL_BAD_PARAMETER, msg);
225 return NULL;
226 }
227 return drv;
228 }
229
230
231 /**
232 * Lookup and lock a display.
233 */
234 static inline _EGLDisplay *
235 _eglLockDisplay(EGLDisplay display)
236 {
237 _EGLDisplay *dpy = _eglLookupDisplay(display);
238 if (dpy)
239 mtx_lock(&dpy->Mutex);
240 return dpy;
241 }
242
243
244 /**
245 * Unlock a display.
246 */
247 static inline void
248 _eglUnlockDisplay(_EGLDisplay *dpy)
249 {
250 mtx_unlock(&dpy->Mutex);
251 }
252
253
254 static EGLint *
255 _eglConvertAttribsToInt(const EGLAttrib *attr_list)
256 {
257 EGLint *int_attribs = NULL;
258
259 /* Convert attributes from EGLAttrib[] to EGLint[] */
260 if (attr_list) {
261 int i, size = 0;
262
263 while (attr_list[size] != EGL_NONE)
264 size += 2;
265
266 size += 1; /* add space for EGL_NONE */
267
268 int_attribs = calloc(size, sizeof(int_attribs[0]));
269 if (!int_attribs)
270 return NULL;
271
272 for (i = 0; i < size; i++)
273 int_attribs[i] = attr_list[i];
274 }
275 return int_attribs;
276 }
277
278
279 /**
280 * This is typically the first EGL function that an application calls.
281 * It associates a private _EGLDisplay object to the native display.
282 */
283 EGLDisplay EGLAPIENTRY
284 eglGetDisplay(EGLNativeDisplayType nativeDisplay)
285 {
286 _EGLPlatformType plat;
287 _EGLDisplay *dpy;
288 void *native_display_ptr;
289
290 STATIC_ASSERT(sizeof(void*) == sizeof(nativeDisplay));
291 native_display_ptr = (void*) nativeDisplay;
292
293 plat = _eglGetNativePlatform(native_display_ptr);
294 dpy = _eglFindDisplay(plat, native_display_ptr);
295 return _eglGetDisplayHandle(dpy);
296 }
297
298 static EGLDisplay
299 _eglGetPlatformDisplayCommon(EGLenum platform, void *native_display,
300 const EGLint *attrib_list)
301 {
302 _EGLDisplay *dpy;
303
304 switch (platform) {
305 #ifdef HAVE_X11_PLATFORM
306 case EGL_PLATFORM_X11_EXT:
307 dpy = _eglGetX11Display((Display*) native_display, attrib_list);
308 break;
309 #endif
310 #ifdef HAVE_DRM_PLATFORM
311 case EGL_PLATFORM_GBM_MESA:
312 dpy = _eglGetGbmDisplay((struct gbm_device*) native_display,
313 attrib_list);
314 break;
315 #endif
316 #ifdef HAVE_WAYLAND_PLATFORM
317 case EGL_PLATFORM_WAYLAND_EXT:
318 dpy = _eglGetWaylandDisplay((struct wl_display*) native_display,
319 attrib_list);
320 break;
321 #endif
322 default:
323 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, NULL);
324 }
325
326 return _eglGetDisplayHandle(dpy);
327 }
328
329 static EGLDisplay EGLAPIENTRY
330 eglGetPlatformDisplayEXT(EGLenum platform, void *native_display,
331 const EGLint *attrib_list)
332 {
333 return _eglGetPlatformDisplayCommon(platform, native_display, attrib_list);
334 }
335
336 EGLDisplay EGLAPIENTRY
337 eglGetPlatformDisplay(EGLenum platform, void *native_display,
338 const EGLAttrib *attrib_list)
339 {
340 EGLDisplay display;
341 EGLint *int_attribs;
342
343 int_attribs = _eglConvertAttribsToInt(attrib_list);
344 if (attrib_list && !int_attribs)
345 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, NULL);
346
347 display = _eglGetPlatformDisplayCommon(platform, native_display, int_attribs);
348 free(int_attribs);
349 return display;
350 }
351
352 /**
353 * Copy the extension into the string and update the string pointer.
354 */
355 static EGLint
356 _eglAppendExtension(char **str, const char *ext)
357 {
358 char *s = *str;
359 size_t len = strlen(ext);
360
361 if (s) {
362 memcpy(s, ext, len);
363 s[len++] = ' ';
364 s[len] = '\0';
365
366 *str += len;
367 }
368 else {
369 len++;
370 }
371
372 return (EGLint) len;
373 }
374
375 /**
376 * Examine the individual extension enable/disable flags and recompute
377 * the driver's Extensions string.
378 */
379 static void
380 _eglCreateExtensionsString(_EGLDisplay *dpy)
381 {
382 #define _EGL_CHECK_EXTENSION(ext) \
383 do { \
384 if (dpy->Extensions.ext) { \
385 _eglAppendExtension(&exts, "EGL_" #ext); \
386 assert(exts <= dpy->ExtensionsString + _EGL_MAX_EXTENSIONS_LEN); \
387 } \
388 } while (0)
389
390 char *exts = dpy->ExtensionsString;
391
392 /* Please keep these sorted alphabetically. */
393 _EGL_CHECK_EXTENSION(ANDROID_framebuffer_target);
394 _EGL_CHECK_EXTENSION(ANDROID_image_native_buffer);
395 _EGL_CHECK_EXTENSION(ANDROID_recordable);
396
397 _EGL_CHECK_EXTENSION(CHROMIUM_sync_control);
398
399 _EGL_CHECK_EXTENSION(EXT_buffer_age);
400 _EGL_CHECK_EXTENSION(EXT_create_context_robustness);
401 _EGL_CHECK_EXTENSION(EXT_image_dma_buf_import);
402 _EGL_CHECK_EXTENSION(EXT_swap_buffers_with_damage);
403
404 _EGL_CHECK_EXTENSION(KHR_cl_event2);
405 _EGL_CHECK_EXTENSION(KHR_create_context);
406 _EGL_CHECK_EXTENSION(KHR_fence_sync);
407 _EGL_CHECK_EXTENSION(KHR_get_all_proc_addresses);
408 _EGL_CHECK_EXTENSION(KHR_gl_colorspace);
409 _EGL_CHECK_EXTENSION(KHR_gl_renderbuffer_image);
410 _EGL_CHECK_EXTENSION(KHR_gl_texture_2D_image);
411 _EGL_CHECK_EXTENSION(KHR_gl_texture_3D_image);
412 _EGL_CHECK_EXTENSION(KHR_gl_texture_cubemap_image);
413 if (dpy->Extensions.KHR_image_base && dpy->Extensions.KHR_image_pixmap)
414 _eglAppendExtension(&exts, "EGL_KHR_image");
415 _EGL_CHECK_EXTENSION(KHR_image_base);
416 _EGL_CHECK_EXTENSION(KHR_image_pixmap);
417 _EGL_CHECK_EXTENSION(KHR_no_config_context);
418 _EGL_CHECK_EXTENSION(KHR_reusable_sync);
419 _EGL_CHECK_EXTENSION(KHR_surfaceless_context);
420 _EGL_CHECK_EXTENSION(KHR_wait_sync);
421
422 if (dpy->Extensions.KHR_no_config_context)
423 _eglAppendExtension(&exts, "EGL_MESA_configless_context");
424 _EGL_CHECK_EXTENSION(MESA_drm_image);
425 _EGL_CHECK_EXTENSION(MESA_image_dma_buf_export);
426
427 _EGL_CHECK_EXTENSION(NOK_swap_region);
428 _EGL_CHECK_EXTENSION(NOK_texture_from_pixmap);
429
430 _EGL_CHECK_EXTENSION(NV_post_sub_buffer);
431
432 _EGL_CHECK_EXTENSION(WL_bind_wayland_display);
433 _EGL_CHECK_EXTENSION(WL_create_wayland_buffer_from_image);
434
435 #undef _EGL_CHECK_EXTENSION
436 }
437
438 static void
439 _eglCreateAPIsString(_EGLDisplay *dpy)
440 {
441 if (dpy->ClientAPIs & EGL_OPENGL_BIT)
442 strcat(dpy->ClientAPIsString, "OpenGL ");
443
444 if (dpy->ClientAPIs & EGL_OPENGL_ES_BIT ||
445 dpy->ClientAPIs & EGL_OPENGL_ES2_BIT ||
446 dpy->ClientAPIs & EGL_OPENGL_ES3_BIT_KHR) {
447 strcat(dpy->ClientAPIsString, "OpenGL_ES ");
448 }
449
450 if (dpy->ClientAPIs & EGL_OPENVG_BIT)
451 strcat(dpy->ClientAPIsString, "OpenVG ");
452
453 assert(strlen(dpy->ClientAPIsString) < sizeof(dpy->ClientAPIsString));
454 }
455
456 static void
457 _eglComputeVersion(_EGLDisplay *disp)
458 {
459 disp->Version = 14;
460
461 if (disp->Extensions.KHR_fence_sync &&
462 disp->Extensions.KHR_cl_event2 &&
463 disp->Extensions.KHR_wait_sync &&
464 disp->Extensions.KHR_image_base &&
465 disp->Extensions.KHR_gl_texture_2D_image &&
466 disp->Extensions.KHR_gl_texture_3D_image &&
467 disp->Extensions.KHR_gl_texture_cubemap_image &&
468 disp->Extensions.KHR_gl_renderbuffer_image &&
469 disp->Extensions.KHR_create_context &&
470 disp->Extensions.EXT_create_context_robustness &&
471 disp->Extensions.KHR_get_all_proc_addresses &&
472 disp->Extensions.KHR_gl_colorspace &&
473 disp->Extensions.KHR_surfaceless_context)
474 disp->Version = 15;
475 }
476
477 /**
478 * This is typically the second EGL function that an application calls.
479 * Here we load/initialize the actual hardware driver.
480 */
481 EGLBoolean EGLAPIENTRY
482 eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
483 {
484 _EGLDisplay *disp = _eglLockDisplay(dpy);
485
486 if (!disp)
487 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
488
489 if (!disp->Initialized) {
490 if (!_eglMatchDriver(disp, EGL_FALSE))
491 RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
492
493 /* limit to APIs supported by core */
494 disp->ClientAPIs &= _EGL_API_ALL_BITS;
495
496 /* EGL_KHR_get_all_proc_addresses is a corner-case extension. The spec
497 * classifies it as an EGL display extension, though conceptually it's an
498 * EGL client extension.
499 *
500 * From the EGL_KHR_get_all_proc_addresses spec:
501 *
502 * The EGL implementation must expose the name
503 * EGL_KHR_client_get_all_proc_addresses if and only if it exposes
504 * EGL_KHR_get_all_proc_addresses and supports
505 * EGL_EXT_client_extensions.
506 *
507 * Mesa unconditionally exposes both client extensions mentioned above,
508 * so the spec requires that each EGLDisplay unconditionally expose
509 * EGL_KHR_get_all_proc_addresses also.
510 */
511 disp->Extensions.KHR_get_all_proc_addresses = EGL_TRUE;
512
513 _eglComputeVersion(disp);
514 _eglCreateExtensionsString(disp);
515 _eglCreateAPIsString(disp);
516 snprintf(disp->VersionString, sizeof(disp->VersionString),
517 "%d.%d (%s)", disp->Version / 10, disp->Version % 10,
518 disp->Driver->Name);
519 }
520
521 /* Update applications version of major and minor if not NULL */
522 if ((major != NULL) && (minor != NULL)) {
523 *major = disp->Version / 10;
524 *minor = disp->Version % 10;
525 }
526
527 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
528 }
529
530
531 EGLBoolean EGLAPIENTRY
532 eglTerminate(EGLDisplay dpy)
533 {
534 _EGLDisplay *disp = _eglLockDisplay(dpy);
535
536 if (!disp)
537 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
538
539 if (disp->Initialized) {
540 _EGLDriver *drv = disp->Driver;
541
542 drv->API.Terminate(drv, disp);
543 /* do not reset disp->Driver */
544 disp->ClientAPIsString[0] = 0;
545 disp->Initialized = EGL_FALSE;
546 }
547
548 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
549 }
550
551
552 const char * EGLAPIENTRY
553 eglQueryString(EGLDisplay dpy, EGLint name)
554 {
555 _EGLDisplay *disp;
556 _EGLDriver *drv;
557
558 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS) {
559 RETURN_EGL_SUCCESS(NULL, _eglGlobal.ClientExtensionString);
560 }
561
562 disp = _eglLockDisplay(dpy);
563 _EGL_CHECK_DISPLAY(disp, NULL, drv);
564
565 switch (name) {
566 case EGL_VENDOR:
567 RETURN_EGL_SUCCESS(disp, _EGL_VENDOR_STRING);
568 case EGL_VERSION:
569 RETURN_EGL_SUCCESS(disp, disp->VersionString);
570 case EGL_EXTENSIONS:
571 RETURN_EGL_SUCCESS(disp, disp->ExtensionsString);
572 case EGL_CLIENT_APIS:
573 RETURN_EGL_SUCCESS(disp, disp->ClientAPIsString);
574 default:
575 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
576 }
577 }
578
579
580 EGLBoolean EGLAPIENTRY
581 eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
582 EGLint config_size, EGLint *num_config)
583 {
584 _EGLDisplay *disp = _eglLockDisplay(dpy);
585 _EGLDriver *drv;
586 EGLBoolean ret;
587
588 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
589 ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
590
591 RETURN_EGL_EVAL(disp, ret);
592 }
593
594
595 EGLBoolean EGLAPIENTRY
596 eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
597 EGLint config_size, EGLint *num_config)
598 {
599 _EGLDisplay *disp = _eglLockDisplay(dpy);
600 _EGLDriver *drv;
601 EGLBoolean ret;
602
603 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
604 ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
605 config_size, num_config);
606
607 RETURN_EGL_EVAL(disp, ret);
608 }
609
610
611 EGLBoolean EGLAPIENTRY
612 eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
613 EGLint attribute, EGLint *value)
614 {
615 _EGLDisplay *disp = _eglLockDisplay(dpy);
616 _EGLConfig *conf = _eglLookupConfig(config, disp);
617 _EGLDriver *drv;
618 EGLBoolean ret;
619
620 _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
621 ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
622
623 RETURN_EGL_EVAL(disp, ret);
624 }
625
626
627 EGLContext EGLAPIENTRY
628 eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
629 const EGLint *attrib_list)
630 {
631 _EGLDisplay *disp = _eglLockDisplay(dpy);
632 _EGLConfig *conf = _eglLookupConfig(config, disp);
633 _EGLContext *share = _eglLookupContext(share_list, disp);
634 _EGLDriver *drv;
635 _EGLContext *context;
636 EGLContext ret;
637
638 _EGL_CHECK_DISPLAY(disp, EGL_NO_CONTEXT, drv);
639
640 if (!config && !disp->Extensions.KHR_no_config_context)
641 RETURN_EGL_ERROR(disp, EGL_BAD_CONFIG, EGL_NO_CONTEXT);
642
643 if (!share && share_list != EGL_NO_CONTEXT)
644 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
645
646 context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
647 ret = (context) ? _eglLinkContext(context) : EGL_NO_CONTEXT;
648
649 RETURN_EGL_EVAL(disp, ret);
650 }
651
652
653 EGLBoolean EGLAPIENTRY
654 eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
655 {
656 _EGLDisplay *disp = _eglLockDisplay(dpy);
657 _EGLContext *context = _eglLookupContext(ctx, disp);
658 _EGLDriver *drv;
659 EGLBoolean ret;
660
661 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
662 _eglUnlinkContext(context);
663 ret = drv->API.DestroyContext(drv, disp, context);
664
665 RETURN_EGL_EVAL(disp, ret);
666 }
667
668
669 EGLBoolean EGLAPIENTRY
670 eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
671 EGLContext ctx)
672 {
673 _EGLDisplay *disp = _eglLockDisplay(dpy);
674 _EGLContext *context = _eglLookupContext(ctx, disp);
675 _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
676 _EGLSurface *read_surf = _eglLookupSurface(read, disp);
677 _EGLDriver *drv;
678 EGLBoolean ret;
679
680 if (!disp)
681 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
682 drv = disp->Driver;
683
684 /* display is allowed to be uninitialized under certain condition */
685 if (!disp->Initialized) {
686 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
687 ctx != EGL_NO_CONTEXT)
688 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
689 }
690 if (!drv)
691 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
692
693 if (!context && ctx != EGL_NO_CONTEXT)
694 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
695 if (!draw_surf || !read_surf) {
696 /* From the EGL 1.4 (20130211) spec:
697 *
698 * To release the current context without assigning a new one, set ctx
699 * to EGL_NO_CONTEXT and set draw and read to EGL_NO_SURFACE.
700 */
701 if (!disp->Extensions.KHR_surfaceless_context && ctx != EGL_NO_CONTEXT)
702 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
703
704 if ((!draw_surf && draw != EGL_NO_SURFACE) ||
705 (!read_surf && read != EGL_NO_SURFACE))
706 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
707 if (draw_surf || read_surf)
708 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
709 }
710
711 ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
712
713 RETURN_EGL_EVAL(disp, ret);
714 }
715
716
717 EGLBoolean EGLAPIENTRY
718 eglQueryContext(EGLDisplay dpy, EGLContext ctx,
719 EGLint attribute, EGLint *value)
720 {
721 _EGLDisplay *disp = _eglLockDisplay(dpy);
722 _EGLContext *context = _eglLookupContext(ctx, disp);
723 _EGLDriver *drv;
724 EGLBoolean ret;
725
726 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
727 ret = drv->API.QueryContext(drv, disp, context, attribute, value);
728
729 RETURN_EGL_EVAL(disp, ret);
730 }
731
732
733 static EGLSurface
734 _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
735 void *native_window, const EGLint *attrib_list)
736 {
737 _EGLConfig *conf = _eglLookupConfig(config, disp);
738 _EGLDriver *drv;
739 _EGLSurface *surf;
740 EGLSurface ret;
741
742 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
743
744 if (native_window == NULL)
745 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
746
747 surf = drv->API.CreateWindowSurface(drv, disp, conf, native_window,
748 attrib_list);
749 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
750
751 RETURN_EGL_EVAL(disp, ret);
752 }
753
754
755 EGLSurface EGLAPIENTRY
756 eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
757 EGLNativeWindowType window, const EGLint *attrib_list)
758 {
759 _EGLDisplay *disp = _eglLockDisplay(dpy);
760 STATIC_ASSERT(sizeof(void*) == sizeof(window));
761 return _eglCreateWindowSurfaceCommon(disp, config, (void*) window,
762 attrib_list);
763 }
764
765 static void *
766 fixupNativeWindow(_EGLDisplay *disp, void *native_window)
767 {
768 #ifdef HAVE_X11_PLATFORM
769 if (disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
770 /* The `native_window` parameter for the X11 platform differs between
771 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
772 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
773 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
774 * `Window*`. Convert `Window*` to `Window` because that's what
775 * dri2_x11_create_window_surface() expects.
776 */
777 return (void *)(* (Window*) native_window);
778 }
779 #endif
780 return native_window;
781 }
782
783 static EGLSurface EGLAPIENTRY
784 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
785 void *native_window,
786 const EGLint *attrib_list)
787 {
788 _EGLDisplay *disp = _eglLockDisplay(dpy);
789
790 native_window = fixupNativeWindow(disp, native_window);
791
792 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
793 attrib_list);
794 }
795
796
797 EGLSurface EGLAPIENTRY
798 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
799 void *native_window,
800 const EGLAttrib *attrib_list)
801 {
802 _EGLDisplay *disp = _eglLockDisplay(dpy);
803 EGLSurface surface;
804 EGLint *int_attribs = _eglConvertAttribsToInt(attrib_list);
805
806 if (attrib_list && !int_attribs)
807 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_NO_SURFACE);
808
809 native_window = fixupNativeWindow(disp, native_window);
810 surface = _eglCreateWindowSurfaceCommon(disp, config, native_window,
811 int_attribs);
812 free(int_attribs);
813 return surface;
814 }
815
816 static void *
817 fixupNativePixmap(_EGLDisplay *disp, void *native_pixmap)
818 {
819 #ifdef HAVE_X11_PLATFORM
820 /* The `native_pixmap` parameter for the X11 platform differs between
821 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
822 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
823 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
824 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
825 * dri2_x11_create_pixmap_surface() expects.
826 */
827 if (disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL)
828 return (void *)(* (Pixmap*) native_pixmap);
829 #endif
830 return native_pixmap;
831 }
832
833 static EGLSurface
834 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
835 void *native_pixmap, const EGLint *attrib_list)
836 {
837 _EGLConfig *conf = _eglLookupConfig(config, disp);
838 _EGLDriver *drv;
839 _EGLSurface *surf;
840 EGLSurface ret;
841
842 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
843 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
844 attrib_list);
845 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
846
847 RETURN_EGL_EVAL(disp, ret);
848 }
849
850
851 EGLSurface EGLAPIENTRY
852 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
853 EGLNativePixmapType pixmap, const EGLint *attrib_list)
854 {
855 _EGLDisplay *disp = _eglLockDisplay(dpy);
856 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
857 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
858 attrib_list);
859 }
860
861 static EGLSurface EGLAPIENTRY
862 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
863 void *native_pixmap,
864 const EGLint *attrib_list)
865 {
866 _EGLDisplay *disp = _eglLockDisplay(dpy);
867
868 native_pixmap = fixupNativePixmap(disp, native_pixmap);
869 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
870 attrib_list);
871 }
872
873
874 EGLSurface EGLAPIENTRY
875 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
876 void *native_pixmap,
877 const EGLAttrib *attrib_list)
878 {
879 _EGLDisplay *disp = _eglLockDisplay(dpy);
880 EGLSurface surface;
881 EGLint *int_attribs = _eglConvertAttribsToInt(attrib_list);
882
883 if (attrib_list && !int_attribs)
884 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_NO_SURFACE);
885
886 native_pixmap = fixupNativePixmap(disp, native_pixmap);
887 surface = _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
888 int_attribs);
889 free(int_attribs);
890 return surface;
891 }
892
893
894 EGLSurface EGLAPIENTRY
895 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
896 const EGLint *attrib_list)
897 {
898 _EGLDisplay *disp = _eglLockDisplay(dpy);
899 _EGLConfig *conf = _eglLookupConfig(config, disp);
900 _EGLDriver *drv;
901 _EGLSurface *surf;
902 EGLSurface ret;
903
904 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
905
906 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
907 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
908
909 RETURN_EGL_EVAL(disp, ret);
910 }
911
912
913 EGLBoolean EGLAPIENTRY
914 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
915 {
916 _EGLDisplay *disp = _eglLockDisplay(dpy);
917 _EGLSurface *surf = _eglLookupSurface(surface, disp);
918 _EGLDriver *drv;
919 EGLBoolean ret;
920
921 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
922 _eglUnlinkSurface(surf);
923 ret = drv->API.DestroySurface(drv, disp, surf);
924
925 RETURN_EGL_EVAL(disp, ret);
926 }
927
928 EGLBoolean EGLAPIENTRY
929 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
930 EGLint attribute, EGLint *value)
931 {
932 _EGLDisplay *disp = _eglLockDisplay(dpy);
933 _EGLSurface *surf = _eglLookupSurface(surface, disp);
934 _EGLDriver *drv;
935 EGLBoolean ret;
936
937 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
938 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
939
940 RETURN_EGL_EVAL(disp, ret);
941 }
942
943 EGLBoolean EGLAPIENTRY
944 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
945 EGLint attribute, EGLint value)
946 {
947 _EGLDisplay *disp = _eglLockDisplay(dpy);
948 _EGLSurface *surf = _eglLookupSurface(surface, disp);
949 _EGLDriver *drv;
950 EGLBoolean ret;
951
952 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
953 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
954
955 RETURN_EGL_EVAL(disp, ret);
956 }
957
958
959 EGLBoolean EGLAPIENTRY
960 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
961 {
962 _EGLDisplay *disp = _eglLockDisplay(dpy);
963 _EGLSurface *surf = _eglLookupSurface(surface, disp);
964 _EGLDriver *drv;
965 EGLBoolean ret;
966
967 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
968 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
969
970 RETURN_EGL_EVAL(disp, ret);
971 }
972
973
974 EGLBoolean EGLAPIENTRY
975 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
976 {
977 _EGLDisplay *disp = _eglLockDisplay(dpy);
978 _EGLSurface *surf = _eglLookupSurface(surface, disp);
979 _EGLDriver *drv;
980 EGLBoolean ret;
981
982 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
983 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
984
985 RETURN_EGL_EVAL(disp, ret);
986 }
987
988
989 EGLBoolean EGLAPIENTRY
990 eglSwapInterval(EGLDisplay dpy, EGLint interval)
991 {
992 _EGLDisplay *disp = _eglLockDisplay(dpy);
993 _EGLContext *ctx = _eglGetCurrentContext();
994 _EGLSurface *surf;
995 _EGLDriver *drv;
996 EGLBoolean ret;
997
998 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
999
1000 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1001 ctx->Resource.Display != disp)
1002 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
1003
1004 surf = ctx->DrawSurface;
1005 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
1006 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1007
1008 ret = drv->API.SwapInterval(drv, disp, surf, interval);
1009
1010 RETURN_EGL_EVAL(disp, ret);
1011 }
1012
1013
1014 EGLBoolean EGLAPIENTRY
1015 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1016 {
1017 _EGLContext *ctx = _eglGetCurrentContext();
1018 _EGLDisplay *disp = _eglLockDisplay(dpy);
1019 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1020 _EGLDriver *drv;
1021 EGLBoolean ret;
1022
1023 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1024
1025 /* surface must be bound to current context in EGL 1.4 */
1026 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1027 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1028 surf != ctx->DrawSurface)
1029 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1030 #endif
1031
1032 ret = drv->API.SwapBuffers(drv, disp, surf);
1033
1034 RETURN_EGL_EVAL(disp, ret);
1035 }
1036
1037
1038 static EGLBoolean EGLAPIENTRY
1039 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1040 EGLint *rects, EGLint n_rects)
1041 {
1042 _EGLContext *ctx = _eglGetCurrentContext();
1043 _EGLDisplay *disp = _eglLockDisplay(dpy);
1044 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1045 _EGLDriver *drv;
1046 EGLBoolean ret;
1047
1048 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1049
1050 /* surface must be bound to current context in EGL 1.4 */
1051 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1052 surf != ctx->DrawSurface)
1053 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1054
1055 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1056 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1057
1058 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1059
1060 RETURN_EGL_EVAL(disp, ret);
1061 }
1062
1063 EGLBoolean EGLAPIENTRY
1064 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1065 {
1066 _EGLDisplay *disp = _eglLockDisplay(dpy);
1067 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1068 _EGLDriver *drv;
1069 EGLBoolean ret;
1070 void *native_pixmap_ptr;
1071
1072 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1073 native_pixmap_ptr = (void*) target;
1074
1075 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1076 if (disp->Platform != _eglGetNativePlatform(disp->PlatformDisplay))
1077 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_FALSE);
1078 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1079
1080 RETURN_EGL_EVAL(disp, ret);
1081 }
1082
1083
1084 static EGLBoolean
1085 _eglWaitClientCommon(void)
1086 {
1087 _EGLContext *ctx = _eglGetCurrentContext();
1088 _EGLDisplay *disp;
1089 _EGLDriver *drv;
1090 EGLBoolean ret;
1091
1092 if (!ctx)
1093 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1094
1095 disp = ctx->Resource.Display;
1096 mtx_lock(&disp->Mutex);
1097
1098 /* let bad current context imply bad current surface */
1099 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1100 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1101 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1102
1103 /* a valid current context implies an initialized current display */
1104 assert(disp->Initialized);
1105 drv = disp->Driver;
1106 ret = drv->API.WaitClient(drv, disp, ctx);
1107
1108 RETURN_EGL_EVAL(disp, ret);
1109 }
1110
1111 EGLBoolean EGLAPIENTRY
1112 eglWaitClient(void)
1113 {
1114 return _eglWaitClientCommon();
1115 }
1116
1117 EGLBoolean EGLAPIENTRY
1118 eglWaitGL(void)
1119 {
1120 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1121 return _eglWaitClientCommon();
1122 }
1123
1124
1125 EGLBoolean EGLAPIENTRY
1126 eglWaitNative(EGLint engine)
1127 {
1128 _EGLContext *ctx = _eglGetCurrentContext();
1129 _EGLDisplay *disp;
1130 _EGLDriver *drv;
1131 EGLBoolean ret;
1132
1133 if (!ctx)
1134 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1135
1136 disp = ctx->Resource.Display;
1137 mtx_lock(&disp->Mutex);
1138
1139 /* let bad current context imply bad current surface */
1140 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1141 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1142 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1143
1144 /* a valid current context implies an initialized current display */
1145 assert(disp->Initialized);
1146 drv = disp->Driver;
1147 ret = drv->API.WaitNative(drv, disp, engine);
1148
1149 RETURN_EGL_EVAL(disp, ret);
1150 }
1151
1152
1153 EGLDisplay EGLAPIENTRY
1154 eglGetCurrentDisplay(void)
1155 {
1156 _EGLContext *ctx = _eglGetCurrentContext();
1157 EGLDisplay ret;
1158
1159 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1160
1161 RETURN_EGL_SUCCESS(NULL, ret);
1162 }
1163
1164
1165 EGLContext EGLAPIENTRY
1166 eglGetCurrentContext(void)
1167 {
1168 _EGLContext *ctx = _eglGetCurrentContext();
1169 EGLContext ret;
1170
1171 ret = _eglGetContextHandle(ctx);
1172
1173 RETURN_EGL_SUCCESS(NULL, ret);
1174 }
1175
1176
1177 EGLSurface EGLAPIENTRY
1178 eglGetCurrentSurface(EGLint readdraw)
1179 {
1180 _EGLContext *ctx = _eglGetCurrentContext();
1181 EGLint err = EGL_SUCCESS;
1182 _EGLSurface *surf;
1183 EGLSurface ret;
1184
1185 if (!ctx)
1186 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1187
1188 switch (readdraw) {
1189 case EGL_DRAW:
1190 surf = ctx->DrawSurface;
1191 break;
1192 case EGL_READ:
1193 surf = ctx->ReadSurface;
1194 break;
1195 default:
1196 surf = NULL;
1197 err = EGL_BAD_PARAMETER;
1198 break;
1199 }
1200
1201 ret = _eglGetSurfaceHandle(surf);
1202
1203 RETURN_EGL_ERROR(NULL, err, ret);
1204 }
1205
1206
1207 EGLint EGLAPIENTRY
1208 eglGetError(void)
1209 {
1210 _EGLThreadInfo *t = _eglGetCurrentThread();
1211 EGLint e = t->LastError;
1212 if (!_eglIsCurrentThreadDummy())
1213 t->LastError = EGL_SUCCESS;
1214 return e;
1215 }
1216
1217
1218 /**
1219 ** EGL 1.2
1220 **/
1221
1222 /**
1223 * Specify the client API to use for subsequent calls including:
1224 * eglCreateContext()
1225 * eglGetCurrentContext()
1226 * eglGetCurrentDisplay()
1227 * eglGetCurrentSurface()
1228 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1229 * eglWaitClient()
1230 * eglWaitNative()
1231 * See section 3.7 "Rendering Context" in the EGL specification for details.
1232 */
1233 EGLBoolean EGLAPIENTRY
1234 eglBindAPI(EGLenum api)
1235 {
1236 _EGLThreadInfo *t = _eglGetCurrentThread();
1237
1238 if (_eglIsCurrentThreadDummy())
1239 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1240
1241 if (!_eglIsApiValid(api))
1242 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1243
1244 t->CurrentAPI = api;
1245
1246 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1247 }
1248
1249
1250 /**
1251 * Return the last value set with eglBindAPI().
1252 */
1253 EGLenum EGLAPIENTRY
1254 eglQueryAPI(void)
1255 {
1256 _EGLThreadInfo *t = _eglGetCurrentThread();
1257 EGLenum ret;
1258
1259 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1260 ret = t->CurrentAPI;
1261
1262 RETURN_EGL_SUCCESS(NULL, ret);
1263 }
1264
1265
1266 EGLSurface EGLAPIENTRY
1267 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1268 EGLClientBuffer buffer, EGLConfig config,
1269 const EGLint *attrib_list)
1270 {
1271 _EGLDisplay *disp = _eglLockDisplay(dpy);
1272 _EGLConfig *conf = _eglLookupConfig(config, disp);
1273 _EGLDriver *drv;
1274 _EGLSurface *surf;
1275 EGLSurface ret;
1276
1277 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1278
1279 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1280 conf, attrib_list);
1281 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1282
1283 RETURN_EGL_EVAL(disp, ret);
1284 }
1285
1286
1287 EGLBoolean EGLAPIENTRY
1288 eglReleaseThread(void)
1289 {
1290 /* unbind current contexts */
1291 if (!_eglIsCurrentThreadDummy()) {
1292 _EGLThreadInfo *t = _eglGetCurrentThread();
1293
1294 _EGLContext *ctx = t->CurrentContext;
1295 if (ctx) {
1296 _EGLDisplay *disp = ctx->Resource.Display;
1297 _EGLDriver *drv;
1298
1299 mtx_lock(&disp->Mutex);
1300 drv = disp->Driver;
1301 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1302 mtx_unlock(&disp->Mutex);
1303 }
1304 }
1305
1306 _eglDestroyCurrentThread();
1307
1308 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1309 }
1310
1311
1312 static EGLImage
1313 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1314 EGLClientBuffer buffer, const EGLint *attr_list)
1315 {
1316 _EGLContext *context = _eglLookupContext(ctx, disp);
1317 _EGLDriver *drv;
1318 _EGLImage *img;
1319 EGLImage ret;
1320
1321 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1322 if (!disp->Extensions.KHR_image_base)
1323 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1324 if (!context && ctx != EGL_NO_CONTEXT)
1325 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1326 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1327 * <ctx> must be EGL_NO_CONTEXT..."
1328 */
1329 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1330 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1331
1332 img = drv->API.CreateImageKHR(drv,
1333 disp, context, target, buffer, attr_list);
1334 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1335
1336 RETURN_EGL_EVAL(disp, ret);
1337 }
1338
1339 static EGLImage EGLAPIENTRY
1340 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1341 EGLClientBuffer buffer, const EGLint *attr_list)
1342 {
1343 _EGLDisplay *disp = _eglLockDisplay(dpy);
1344 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1345 }
1346
1347
1348 EGLImage EGLAPIENTRY
1349 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1350 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1351 {
1352 _EGLDisplay *disp = _eglLockDisplay(dpy);
1353 EGLImage image;
1354 EGLint *int_attribs = _eglConvertAttribsToInt(attr_list);
1355
1356 if (attr_list && !int_attribs)
1357 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1358
1359 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1360 free(int_attribs);
1361 return image;
1362 }
1363
1364
1365 EGLBoolean EGLAPIENTRY
1366 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1367 {
1368 _EGLDisplay *disp = _eglLockDisplay(dpy);
1369 _EGLImage *img = _eglLookupImage(image, disp);
1370 _EGLDriver *drv;
1371 EGLBoolean ret;
1372
1373 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1374 if (!disp->Extensions.KHR_image_base)
1375 RETURN_EGL_EVAL(disp, EGL_FALSE);
1376 if (!img)
1377 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1378
1379 _eglUnlinkImage(img);
1380 ret = drv->API.DestroyImageKHR(drv, disp, img);
1381
1382 RETURN_EGL_EVAL(disp, ret);
1383 }
1384
1385
1386 static EGLSync
1387 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLint *attrib_list,
1388 const EGLAttrib *attrib_list64, EGLBoolean is64,
1389 EGLenum invalid_type_error)
1390 {
1391 _EGLContext *ctx = _eglGetCurrentContext();
1392 _EGLDriver *drv;
1393 _EGLSync *sync;
1394 EGLSync ret;
1395
1396 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1397
1398 if (!disp->Extensions.KHR_cl_event2 && is64)
1399 RETURN_EGL_EVAL(disp, EGL_NO_SYNC_KHR);
1400
1401 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1402 if (!ctx || ctx->Resource.Display != disp ||
1403 ctx->ClientAPI != EGL_OPENGL_ES_API)
1404 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1405
1406 switch (type) {
1407 case EGL_SYNC_FENCE_KHR:
1408 if (!disp->Extensions.KHR_fence_sync)
1409 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1410 break;
1411 case EGL_SYNC_REUSABLE_KHR:
1412 if (!disp->Extensions.KHR_reusable_sync)
1413 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1414 break;
1415 case EGL_SYNC_CL_EVENT_KHR:
1416 if (!disp->Extensions.KHR_cl_event2)
1417 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1418 break;
1419 default:
1420 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1421 }
1422
1423 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list, attrib_list64);
1424 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1425
1426 RETURN_EGL_EVAL(disp, ret);
1427 }
1428
1429
1430 static EGLSync EGLAPIENTRY
1431 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1432 {
1433 _EGLDisplay *disp = _eglLockDisplay(dpy);
1434 return _eglCreateSync(disp, type, attrib_list, NULL, EGL_FALSE,
1435 EGL_BAD_ATTRIBUTE);
1436 }
1437
1438
1439 static EGLSync EGLAPIENTRY
1440 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1441 {
1442 _EGLDisplay *disp = _eglLockDisplay(dpy);
1443 return _eglCreateSync(disp, type, NULL, attrib_list, EGL_TRUE,
1444 EGL_BAD_ATTRIBUTE);
1445 }
1446
1447
1448 EGLSync EGLAPIENTRY
1449 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1450 {
1451 _EGLDisplay *disp = _eglLockDisplay(dpy);
1452 return _eglCreateSync(disp, type, NULL, attrib_list, EGL_TRUE,
1453 EGL_BAD_PARAMETER);
1454 }
1455
1456
1457 EGLBoolean EGLAPIENTRY
1458 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1459 {
1460 _EGLDisplay *disp = _eglLockDisplay(dpy);
1461 _EGLSync *s = _eglLookupSync(sync, disp);
1462 _EGLDriver *drv;
1463 EGLBoolean ret;
1464
1465 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1466 assert(disp->Extensions.KHR_reusable_sync ||
1467 disp->Extensions.KHR_fence_sync);
1468
1469 _eglUnlinkSync(s);
1470 ret = drv->API.DestroySyncKHR(drv, disp, s);
1471
1472 RETURN_EGL_EVAL(disp, ret);
1473 }
1474
1475
1476 EGLint EGLAPIENTRY
1477 eglClientWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags, EGLTime timeout)
1478 {
1479 _EGLDisplay *disp = _eglLockDisplay(dpy);
1480 _EGLSync *s = _eglLookupSync(sync, disp);
1481 _EGLDriver *drv;
1482 EGLint ret;
1483
1484 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1485 assert(disp->Extensions.KHR_reusable_sync ||
1486 disp->Extensions.KHR_fence_sync);
1487
1488 if (s->SyncStatus == EGL_SIGNALED_KHR)
1489 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1490
1491 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1492 * unlocked here to allow other threads also to be able to
1493 * go into waiting state.
1494 */
1495
1496 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1497 _eglUnlockDisplay(dpy);
1498
1499 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1500
1501 /*
1502 * 'disp' is already unlocked for reusable sync type,
1503 * so passing 'NULL' to bypass unlocking display.
1504 */
1505 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1506 RETURN_EGL_EVAL(NULL, ret);
1507 else
1508 RETURN_EGL_EVAL(disp, ret);
1509 }
1510
1511
1512 static EGLint
1513 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
1514 {
1515 _EGLContext *ctx = _eglGetCurrentContext();
1516 _EGLDriver *drv;
1517 EGLint ret;
1518
1519 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1520 assert(disp->Extensions.KHR_wait_sync);
1521
1522 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1523 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
1524 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1525
1526 /* the API doesn't allow any flags yet */
1527 if (flags != 0)
1528 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1529
1530 ret = drv->API.WaitSyncKHR(drv, disp, s);
1531
1532 RETURN_EGL_EVAL(disp, ret);
1533 }
1534
1535 static EGLint EGLAPIENTRY
1536 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
1537 {
1538 _EGLDisplay *disp = _eglLockDisplay(dpy);
1539 _EGLSync *s = _eglLookupSync(sync, disp);
1540 return _eglWaitSyncCommon(disp, s, flags);
1541 }
1542
1543
1544 EGLBoolean EGLAPIENTRY
1545 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
1546 {
1547 /* The KHR version returns EGLint, while the core version returns
1548 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
1549 * EGL_TRUE.
1550 */
1551 _EGLDisplay *disp = _eglLockDisplay(dpy);
1552 _EGLSync *s = _eglLookupSync(sync, disp);
1553 return _eglWaitSyncCommon(disp, s, flags);
1554 }
1555
1556
1557 static EGLBoolean EGLAPIENTRY
1558 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
1559 {
1560 _EGLDisplay *disp = _eglLockDisplay(dpy);
1561 _EGLSync *s = _eglLookupSync(sync, disp);
1562 _EGLDriver *drv;
1563 EGLBoolean ret;
1564
1565 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1566 assert(disp->Extensions.KHR_reusable_sync);
1567 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
1568
1569 RETURN_EGL_EVAL(disp, ret);
1570 }
1571
1572
1573 static EGLBoolean
1574 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
1575 {
1576 _EGLDriver *drv;
1577 EGLBoolean ret;
1578
1579 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1580 assert(disp->Extensions.KHR_reusable_sync ||
1581 disp->Extensions.KHR_fence_sync);
1582 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
1583
1584 RETURN_EGL_EVAL(disp, ret);
1585 }
1586
1587 EGLBoolean EGLAPIENTRY
1588 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
1589 {
1590 _EGLDisplay *disp = _eglLockDisplay(dpy);
1591 _EGLSync *s = _eglLookupSync(sync, disp);
1592 return _eglGetSyncAttribCommon(disp, s, attribute, value);
1593 }
1594
1595
1596 static EGLBoolean EGLAPIENTRY
1597 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
1598 {
1599 _EGLDisplay *disp = _eglLockDisplay(dpy);
1600 _EGLSync *s = _eglLookupSync(sync, disp);
1601 EGLAttrib attrib;
1602 EGLBoolean result;
1603
1604 if (!value)
1605 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1606
1607 attrib = *value;
1608 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
1609
1610 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
1611 *
1612 * If any error occurs, <*value> is not modified.
1613 */
1614 if (result == EGL_FALSE)
1615 return result;
1616
1617 *value = attrib;
1618 return result;
1619 }
1620
1621
1622 static EGLBoolean EGLAPIENTRY
1623 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
1624 EGLint numRects, const EGLint *rects)
1625 {
1626 _EGLContext *ctx = _eglGetCurrentContext();
1627 _EGLDisplay *disp = _eglLockDisplay(dpy);
1628 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1629 _EGLDriver *drv;
1630 EGLBoolean ret;
1631
1632 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1633
1634 if (!disp->Extensions.NOK_swap_region)
1635 RETURN_EGL_EVAL(disp, EGL_FALSE);
1636
1637 /* surface must be bound to current context in EGL 1.4 */
1638 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1639 surf != ctx->DrawSurface)
1640 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1641
1642 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
1643
1644 RETURN_EGL_EVAL(disp, ret);
1645 }
1646
1647
1648 static EGLImage EGLAPIENTRY
1649 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
1650 {
1651 _EGLDisplay *disp = _eglLockDisplay(dpy);
1652 _EGLDriver *drv;
1653 _EGLImage *img;
1654 EGLImage ret;
1655
1656 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1657 if (!disp->Extensions.MESA_drm_image)
1658 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1659
1660 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
1661 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1662
1663 RETURN_EGL_EVAL(disp, ret);
1664 }
1665
1666 static EGLBoolean EGLAPIENTRY
1667 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
1668 EGLint *name, EGLint *handle, EGLint *stride)
1669 {
1670 _EGLDisplay *disp = _eglLockDisplay(dpy);
1671 _EGLImage *img = _eglLookupImage(image, disp);
1672 _EGLDriver *drv;
1673 EGLBoolean ret;
1674
1675 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1676 assert(disp->Extensions.MESA_drm_image);
1677
1678 if (!img)
1679 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1680
1681 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
1682
1683 RETURN_EGL_EVAL(disp, ret);
1684 }
1685
1686
1687 struct wl_display;
1688
1689 static EGLBoolean EGLAPIENTRY
1690 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1691 {
1692 _EGLDisplay *disp = _eglLockDisplay(dpy);
1693 _EGLDriver *drv;
1694 EGLBoolean ret;
1695
1696 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1697 assert(disp->Extensions.WL_bind_wayland_display);
1698
1699 if (!display)
1700 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1701
1702 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
1703
1704 RETURN_EGL_EVAL(disp, ret);
1705 }
1706
1707 static EGLBoolean EGLAPIENTRY
1708 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1709 {
1710 _EGLDisplay *disp = _eglLockDisplay(dpy);
1711 _EGLDriver *drv;
1712 EGLBoolean ret;
1713
1714 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1715 assert(disp->Extensions.WL_bind_wayland_display);
1716
1717 if (!display)
1718 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1719
1720 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
1721
1722 RETURN_EGL_EVAL(disp, ret);
1723 }
1724
1725 static EGLBoolean EGLAPIENTRY
1726 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
1727 EGLint attribute, EGLint *value)
1728 {
1729 _EGLDisplay *disp = _eglLockDisplay(dpy);
1730 _EGLDriver *drv;
1731 EGLBoolean ret;
1732
1733 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1734 assert(disp->Extensions.WL_bind_wayland_display);
1735
1736 if (!buffer)
1737 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1738
1739 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
1740
1741 RETURN_EGL_EVAL(disp, ret);
1742 }
1743
1744
1745 static struct wl_buffer * EGLAPIENTRY
1746 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
1747 {
1748 _EGLDisplay *disp = _eglLockDisplay(dpy);
1749 _EGLImage *img;
1750 _EGLDriver *drv;
1751 struct wl_buffer *ret;
1752
1753 _EGL_CHECK_DISPLAY(disp, NULL, drv);
1754 assert(disp->Extensions.WL_create_wayland_buffer_from_image);
1755
1756 img = _eglLookupImage(image, disp);
1757
1758 if (!img)
1759 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
1760
1761 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
1762
1763 RETURN_EGL_EVAL(disp, ret);
1764 }
1765
1766 static EGLBoolean EGLAPIENTRY
1767 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
1768 EGLint x, EGLint y, EGLint width, EGLint height)
1769 {
1770 _EGLDisplay *disp = _eglLockDisplay(dpy);
1771 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1772 _EGLDriver *drv;
1773 EGLBoolean ret;
1774
1775 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1776
1777 if (!disp->Extensions.NV_post_sub_buffer)
1778 RETURN_EGL_EVAL(disp, EGL_FALSE);
1779
1780 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
1781
1782 RETURN_EGL_EVAL(disp, ret);
1783 }
1784
1785 static EGLBoolean EGLAPIENTRY
1786 eglGetSyncValuesCHROMIUM(EGLDisplay display, EGLSurface surface,
1787 EGLuint64KHR *ust, EGLuint64KHR *msc,
1788 EGLuint64KHR *sbc)
1789 {
1790 _EGLDisplay *disp = _eglLockDisplay(display);
1791 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1792 _EGLDriver *drv;
1793 EGLBoolean ret;
1794
1795 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1796 if (!disp->Extensions.CHROMIUM_sync_control)
1797 RETURN_EGL_EVAL(disp, EGL_FALSE);
1798
1799 if (!ust || !msc || !sbc)
1800 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1801
1802 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
1803
1804 RETURN_EGL_EVAL(disp, ret);
1805 }
1806
1807 static EGLBoolean EGLAPIENTRY
1808 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
1809 EGLint *fourcc, EGLint *nplanes,
1810 EGLuint64KHR *modifiers)
1811 {
1812 _EGLDisplay *disp = _eglLockDisplay(dpy);
1813 _EGLImage *img = _eglLookupImage(image, disp);
1814 _EGLDriver *drv;
1815 EGLBoolean ret;
1816
1817 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1818 assert(disp->Extensions.MESA_image_dma_buf_export);
1819
1820 if (!img)
1821 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1822
1823 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
1824 modifiers);
1825
1826 RETURN_EGL_EVAL(disp, ret);
1827 }
1828
1829 static EGLBoolean EGLAPIENTRY
1830 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
1831 int *fds, EGLint *strides, EGLint *offsets)
1832 {
1833 _EGLDisplay *disp = _eglLockDisplay(dpy);
1834 _EGLImage *img = _eglLookupImage(image, disp);
1835 _EGLDriver *drv;
1836 EGLBoolean ret;
1837
1838 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1839 assert(disp->Extensions.MESA_image_dma_buf_export);
1840
1841 if (!img)
1842 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1843
1844 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
1845
1846 RETURN_EGL_EVAL(disp, ret);
1847 }
1848
1849 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
1850 eglGetProcAddress(const char *procname)
1851 {
1852 static const struct {
1853 const char *name;
1854 _EGLProc function;
1855 } egl_functions[] = {
1856 /* core functions queryable in the presence of
1857 * EGL_KHR_get_all_proc_addresses or EGL 1.5
1858 */
1859 /* alphabetical order */
1860 { "eglBindAPI", (_EGLProc) eglBindAPI },
1861 { "eglBindTexImage", (_EGLProc) eglBindTexImage },
1862 { "eglChooseConfig", (_EGLProc) eglChooseConfig },
1863 { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
1864 { "eglCreateContext", (_EGLProc) eglCreateContext },
1865 { "eglCreatePbufferFromClientBuffer", (_EGLProc) eglCreatePbufferFromClientBuffer },
1866 { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
1867 { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
1868 { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
1869 { "eglDestroyContext", (_EGLProc) eglDestroyContext },
1870 { "eglDestroySurface", (_EGLProc) eglDestroySurface },
1871 { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
1872 { "eglGetConfigs", (_EGLProc) eglGetConfigs },
1873 { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
1874 { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
1875 { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
1876 { "eglGetDisplay", (_EGLProc) eglGetDisplay },
1877 { "eglGetError", (_EGLProc) eglGetError },
1878 { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
1879 { "eglInitialize", (_EGLProc) eglInitialize },
1880 { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
1881 { "eglQueryAPI", (_EGLProc) eglQueryAPI },
1882 { "eglQueryContext", (_EGLProc) eglQueryContext },
1883 { "eglQueryString", (_EGLProc) eglQueryString },
1884 { "eglQuerySurface", (_EGLProc) eglQuerySurface },
1885 { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
1886 { "eglReleaseThread", (_EGLProc) eglReleaseThread },
1887 { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
1888 { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
1889 { "eglSwapInterval", (_EGLProc) eglSwapInterval },
1890 { "eglTerminate", (_EGLProc) eglTerminate },
1891 { "eglWaitClient", (_EGLProc) eglWaitClient },
1892 { "eglWaitGL", (_EGLProc) eglWaitGL },
1893 { "eglWaitNative", (_EGLProc) eglWaitNative },
1894 { "eglCreateSync", (_EGLProc) eglCreateSync },
1895 { "eglDestroySync", (_EGLProc) eglDestroySync },
1896 { "eglClientWaitSync", (_EGLProc) eglClientWaitSync },
1897 { "eglGetSyncAttrib", (_EGLProc) eglGetSyncAttrib },
1898 { "eglWaitSync", (_EGLProc) eglWaitSync },
1899 { "eglCreateImage", (_EGLProc) eglCreateImage },
1900 { "eglDestroyImage", (_EGLProc) eglDestroyImage },
1901 { "eglGetPlatformDisplay", (_EGLProc) eglGetPlatformDisplay },
1902 { "eglCreatePlatformWindowSurface", (_EGLProc) eglCreatePlatformWindowSurface },
1903 { "eglCreatePlatformPixmapSurface", (_EGLProc) eglCreatePlatformPixmapSurface },
1904 { "eglCreateImageKHR", (_EGLProc) eglCreateImageKHR },
1905 { "eglDestroyImageKHR", (_EGLProc) eglDestroyImage },
1906 { "eglCreateSyncKHR", (_EGLProc) eglCreateSyncKHR },
1907 { "eglCreateSync64KHR", (_EGLProc) eglCreateSync64KHR },
1908 { "eglDestroySyncKHR", (_EGLProc) eglDestroySync },
1909 { "eglClientWaitSyncKHR", (_EGLProc) eglClientWaitSync },
1910 { "eglWaitSyncKHR", (_EGLProc) eglWaitSyncKHR },
1911 { "eglSignalSyncKHR", (_EGLProc) eglSignalSyncKHR },
1912 { "eglGetSyncAttribKHR", (_EGLProc) eglGetSyncAttribKHR },
1913 { "eglSwapBuffersRegionNOK", (_EGLProc) eglSwapBuffersRegionNOK },
1914 { "eglCreateDRMImageMESA", (_EGLProc) eglCreateDRMImageMESA },
1915 { "eglExportDRMImageMESA", (_EGLProc) eglExportDRMImageMESA },
1916 { "eglBindWaylandDisplayWL", (_EGLProc) eglBindWaylandDisplayWL },
1917 { "eglUnbindWaylandDisplayWL", (_EGLProc) eglUnbindWaylandDisplayWL },
1918 { "eglQueryWaylandBufferWL", (_EGLProc) eglQueryWaylandBufferWL },
1919 { "eglCreateWaylandBufferFromImageWL", (_EGLProc) eglCreateWaylandBufferFromImageWL },
1920 { "eglPostSubBufferNV", (_EGLProc) eglPostSubBufferNV },
1921 { "eglSwapBuffersWithDamageEXT", (_EGLProc) eglSwapBuffersWithDamageEXT },
1922 { "eglGetPlatformDisplayEXT", (_EGLProc) eglGetPlatformDisplayEXT },
1923 { "eglCreatePlatformWindowSurfaceEXT", (_EGLProc) eglCreatePlatformWindowSurfaceEXT },
1924 { "eglCreatePlatformPixmapSurfaceEXT", (_EGLProc) eglCreatePlatformPixmapSurfaceEXT },
1925 { "eglGetSyncValuesCHROMIUM", (_EGLProc) eglGetSyncValuesCHROMIUM },
1926 { "eglExportDMABUFImageQueryMESA", (_EGLProc) eglExportDMABUFImageQueryMESA },
1927 { "eglExportDMABUFImageMESA", (_EGLProc) eglExportDMABUFImageMESA },
1928 { NULL, NULL }
1929 };
1930 EGLint i;
1931 _EGLProc ret;
1932
1933 if (!procname)
1934 RETURN_EGL_SUCCESS(NULL, NULL);
1935
1936 ret = NULL;
1937 if (strncmp(procname, "egl", 3) == 0) {
1938 for (i = 0; egl_functions[i].name; i++) {
1939 if (strcmp(egl_functions[i].name, procname) == 0) {
1940 ret = egl_functions[i].function;
1941 break;
1942 }
1943 }
1944 }
1945 if (!ret)
1946 ret = _eglGetDriverProc(procname);
1947
1948 RETURN_EGL_SUCCESS(NULL, ret);
1949 }
1950
1951 static int
1952 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
1953 _EGLDisplay **disp, _EGLDriver **drv,
1954 _EGLContext **ctx)
1955 {
1956
1957 *disp = _eglLockDisplay(dpy);
1958 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
1959 if (*disp)
1960 _eglUnlockDisplay(*disp);
1961 return MESA_GLINTEROP_INVALID_DISPLAY;
1962 }
1963
1964 *drv = (*disp)->Driver;
1965
1966 *ctx = _eglLookupContext(context, *disp);
1967 if (!*ctx ||
1968 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
1969 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
1970 _eglUnlockDisplay(*disp);
1971 return MESA_GLINTEROP_INVALID_CONTEXT;
1972 }
1973
1974 return MESA_GLINTEROP_SUCCESS;
1975 }
1976
1977 int
1978 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
1979 struct mesa_glinterop_device_info *out)
1980 {
1981 _EGLDisplay *disp;
1982 _EGLDriver *drv;
1983 _EGLContext *ctx;
1984 int ret;
1985
1986 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
1987 if (ret != MESA_GLINTEROP_SUCCESS)
1988 return ret;
1989
1990 if (drv->API.GLInteropQueryDeviceInfo)
1991 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
1992 else
1993 ret = MESA_GLINTEROP_UNSUPPORTED;
1994
1995 _eglUnlockDisplay(disp);
1996 return ret;
1997 }
1998
1999 int
2000 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2001 struct mesa_glinterop_export_in *in,
2002 struct mesa_glinterop_export_out *out)
2003 {
2004 _EGLDisplay *disp;
2005 _EGLDriver *drv;
2006 _EGLContext *ctx;
2007 int ret;
2008
2009 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2010 if (ret != MESA_GLINTEROP_SUCCESS)
2011 return ret;
2012
2013 if (drv->API.GLInteropExportObject)
2014 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
2015 else
2016 ret = MESA_GLINTEROP_UNSUPPORTED;
2017
2018 _eglUnlockDisplay(disp);
2019 return ret;
2020 }