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