egl: Factor out _eglGetPlatformDisplayCommon
[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
766 static EGLSurface EGLAPIENTRY
767 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
768 void *native_window,
769 const EGLint *attrib_list)
770 {
771 _EGLDisplay *disp = _eglLockDisplay(dpy);
772
773 #ifdef HAVE_X11_PLATFORM
774 if (disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
775 /* The `native_window` parameter for the X11 platform differs between
776 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
777 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
778 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
779 * `Window*`. Convert `Window*` to `Window` because that's what
780 * dri2_x11_create_window_surface() expects.
781 */
782 native_window = (void*) (* (Window*) native_window);
783 }
784 #endif
785
786 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
787 attrib_list);
788 }
789
790
791 EGLSurface EGLAPIENTRY
792 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
793 void *native_window,
794 const EGLAttrib *attrib_list)
795 {
796 EGLSurface surface;
797 EGLint *int_attribs = _eglConvertAttribsToInt(attrib_list);
798
799 if (attrib_list && !int_attribs)
800 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_NO_SURFACE);
801
802 surface = eglCreatePlatformWindowSurfaceEXT(dpy, config, native_window,
803 int_attribs);
804 free(int_attribs);
805 return surface;
806 }
807
808
809 static EGLSurface
810 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
811 void *native_pixmap, const EGLint *attrib_list)
812 {
813 _EGLConfig *conf = _eglLookupConfig(config, disp);
814 _EGLDriver *drv;
815 _EGLSurface *surf;
816 EGLSurface ret;
817
818 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
819 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
820 attrib_list);
821 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
822
823 RETURN_EGL_EVAL(disp, ret);
824 }
825
826
827 EGLSurface EGLAPIENTRY
828 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
829 EGLNativePixmapType pixmap, const EGLint *attrib_list)
830 {
831 _EGLDisplay *disp = _eglLockDisplay(dpy);
832 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
833 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
834 attrib_list);
835 }
836
837 static EGLSurface EGLAPIENTRY
838 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
839 void *native_pixmap,
840 const EGLint *attrib_list)
841 {
842 _EGLDisplay *disp = _eglLockDisplay(dpy);
843
844 #ifdef HAVE_X11_PLATFORM
845 /* The `native_pixmap` parameter for the X11 platform differs between
846 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
847 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
848 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
849 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
850 * dri2_x11_create_pixmap_surface() expects.
851 */
852 if (disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL) {
853 native_pixmap = (void*) (* (Pixmap*) native_pixmap);
854 }
855 #endif
856
857 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
858 attrib_list);
859 }
860
861
862 EGLSurface EGLAPIENTRY
863 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
864 void *native_pixmap,
865 const EGLAttrib *attrib_list)
866 {
867 EGLSurface surface;
868 EGLint *int_attribs = _eglConvertAttribsToInt(attrib_list);
869
870 if (attrib_list && !int_attribs)
871 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_NO_SURFACE);
872
873 surface = eglCreatePlatformPixmapSurfaceEXT(dpy, config, native_pixmap,
874 int_attribs);
875 free(int_attribs);
876 return surface;
877 }
878
879
880 EGLSurface EGLAPIENTRY
881 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
882 const EGLint *attrib_list)
883 {
884 _EGLDisplay *disp = _eglLockDisplay(dpy);
885 _EGLConfig *conf = _eglLookupConfig(config, disp);
886 _EGLDriver *drv;
887 _EGLSurface *surf;
888 EGLSurface ret;
889
890 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
891
892 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
893 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
894
895 RETURN_EGL_EVAL(disp, ret);
896 }
897
898
899 EGLBoolean EGLAPIENTRY
900 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
901 {
902 _EGLDisplay *disp = _eglLockDisplay(dpy);
903 _EGLSurface *surf = _eglLookupSurface(surface, disp);
904 _EGLDriver *drv;
905 EGLBoolean ret;
906
907 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
908 _eglUnlinkSurface(surf);
909 ret = drv->API.DestroySurface(drv, disp, surf);
910
911 RETURN_EGL_EVAL(disp, ret);
912 }
913
914 EGLBoolean EGLAPIENTRY
915 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
916 EGLint attribute, EGLint *value)
917 {
918 _EGLDisplay *disp = _eglLockDisplay(dpy);
919 _EGLSurface *surf = _eglLookupSurface(surface, disp);
920 _EGLDriver *drv;
921 EGLBoolean ret;
922
923 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
924 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
925
926 RETURN_EGL_EVAL(disp, ret);
927 }
928
929 EGLBoolean EGLAPIENTRY
930 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
931 EGLint attribute, EGLint value)
932 {
933 _EGLDisplay *disp = _eglLockDisplay(dpy);
934 _EGLSurface *surf = _eglLookupSurface(surface, disp);
935 _EGLDriver *drv;
936 EGLBoolean ret;
937
938 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
939 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
940
941 RETURN_EGL_EVAL(disp, ret);
942 }
943
944
945 EGLBoolean EGLAPIENTRY
946 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
947 {
948 _EGLDisplay *disp = _eglLockDisplay(dpy);
949 _EGLSurface *surf = _eglLookupSurface(surface, disp);
950 _EGLDriver *drv;
951 EGLBoolean ret;
952
953 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
954 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
955
956 RETURN_EGL_EVAL(disp, ret);
957 }
958
959
960 EGLBoolean EGLAPIENTRY
961 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
962 {
963 _EGLDisplay *disp = _eglLockDisplay(dpy);
964 _EGLSurface *surf = _eglLookupSurface(surface, disp);
965 _EGLDriver *drv;
966 EGLBoolean ret;
967
968 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
969 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
970
971 RETURN_EGL_EVAL(disp, ret);
972 }
973
974
975 EGLBoolean EGLAPIENTRY
976 eglSwapInterval(EGLDisplay dpy, EGLint interval)
977 {
978 _EGLDisplay *disp = _eglLockDisplay(dpy);
979 _EGLContext *ctx = _eglGetCurrentContext();
980 _EGLSurface *surf;
981 _EGLDriver *drv;
982 EGLBoolean ret;
983
984 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
985
986 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
987 ctx->Resource.Display != disp)
988 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
989
990 surf = ctx->DrawSurface;
991 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
992 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
993
994 ret = drv->API.SwapInterval(drv, disp, surf, interval);
995
996 RETURN_EGL_EVAL(disp, ret);
997 }
998
999
1000 EGLBoolean EGLAPIENTRY
1001 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1002 {
1003 _EGLContext *ctx = _eglGetCurrentContext();
1004 _EGLDisplay *disp = _eglLockDisplay(dpy);
1005 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1006 _EGLDriver *drv;
1007 EGLBoolean ret;
1008
1009 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1010
1011 /* surface must be bound to current context in EGL 1.4 */
1012 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1013 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1014 surf != ctx->DrawSurface)
1015 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1016 #endif
1017
1018 ret = drv->API.SwapBuffers(drv, disp, surf);
1019
1020 RETURN_EGL_EVAL(disp, ret);
1021 }
1022
1023
1024 static EGLBoolean EGLAPIENTRY
1025 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1026 EGLint *rects, EGLint n_rects)
1027 {
1028 _EGLContext *ctx = _eglGetCurrentContext();
1029 _EGLDisplay *disp = _eglLockDisplay(dpy);
1030 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1031 _EGLDriver *drv;
1032 EGLBoolean ret;
1033
1034 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1035
1036 /* surface must be bound to current context in EGL 1.4 */
1037 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1038 surf != ctx->DrawSurface)
1039 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1040
1041 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1042 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1043
1044 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1045
1046 RETURN_EGL_EVAL(disp, ret);
1047 }
1048
1049 EGLBoolean EGLAPIENTRY
1050 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1051 {
1052 _EGLDisplay *disp = _eglLockDisplay(dpy);
1053 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1054 _EGLDriver *drv;
1055 EGLBoolean ret;
1056 void *native_pixmap_ptr;
1057
1058 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1059 native_pixmap_ptr = (void*) target;
1060
1061 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1062 if (disp->Platform != _eglGetNativePlatform(disp->PlatformDisplay))
1063 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_FALSE);
1064 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1065
1066 RETURN_EGL_EVAL(disp, ret);
1067 }
1068
1069
1070 EGLBoolean EGLAPIENTRY
1071 eglWaitClient(void)
1072 {
1073 _EGLContext *ctx = _eglGetCurrentContext();
1074 _EGLDisplay *disp;
1075 _EGLDriver *drv;
1076 EGLBoolean ret;
1077
1078 if (!ctx)
1079 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1080
1081 disp = ctx->Resource.Display;
1082 mtx_lock(&disp->Mutex);
1083
1084 /* let bad current context imply bad current surface */
1085 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1086 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1087 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1088
1089 /* a valid current context implies an initialized current display */
1090 assert(disp->Initialized);
1091 drv = disp->Driver;
1092 ret = drv->API.WaitClient(drv, disp, ctx);
1093
1094 RETURN_EGL_EVAL(disp, ret);
1095 }
1096
1097
1098 EGLBoolean EGLAPIENTRY
1099 eglWaitGL(void)
1100 {
1101 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1102 return eglWaitClient();
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 /**
1200 ** EGL 1.2
1201 **/
1202
1203 /**
1204 * Specify the client API to use for subsequent calls including:
1205 * eglCreateContext()
1206 * eglGetCurrentContext()
1207 * eglGetCurrentDisplay()
1208 * eglGetCurrentSurface()
1209 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1210 * eglWaitClient()
1211 * eglWaitNative()
1212 * See section 3.7 "Rendering Context" in the EGL specification for details.
1213 */
1214 EGLBoolean EGLAPIENTRY
1215 eglBindAPI(EGLenum api)
1216 {
1217 _EGLThreadInfo *t = _eglGetCurrentThread();
1218
1219 if (_eglIsCurrentThreadDummy())
1220 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1221
1222 if (!_eglIsApiValid(api))
1223 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1224
1225 t->CurrentAPI = api;
1226
1227 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1228 }
1229
1230
1231 /**
1232 * Return the last value set with eglBindAPI().
1233 */
1234 EGLenum EGLAPIENTRY
1235 eglQueryAPI(void)
1236 {
1237 _EGLThreadInfo *t = _eglGetCurrentThread();
1238 EGLenum ret;
1239
1240 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1241 ret = t->CurrentAPI;
1242
1243 RETURN_EGL_SUCCESS(NULL, ret);
1244 }
1245
1246
1247 EGLSurface EGLAPIENTRY
1248 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1249 EGLClientBuffer buffer, EGLConfig config,
1250 const EGLint *attrib_list)
1251 {
1252 _EGLDisplay *disp = _eglLockDisplay(dpy);
1253 _EGLConfig *conf = _eglLookupConfig(config, disp);
1254 _EGLDriver *drv;
1255 _EGLSurface *surf;
1256 EGLSurface ret;
1257
1258 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1259
1260 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1261 conf, attrib_list);
1262 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1263
1264 RETURN_EGL_EVAL(disp, ret);
1265 }
1266
1267
1268 EGLBoolean EGLAPIENTRY
1269 eglReleaseThread(void)
1270 {
1271 /* unbind current contexts */
1272 if (!_eglIsCurrentThreadDummy()) {
1273 _EGLThreadInfo *t = _eglGetCurrentThread();
1274
1275 _EGLContext *ctx = t->CurrentContext;
1276 if (ctx) {
1277 _EGLDisplay *disp = ctx->Resource.Display;
1278 _EGLDriver *drv;
1279
1280 mtx_lock(&disp->Mutex);
1281 drv = disp->Driver;
1282 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1283 mtx_unlock(&disp->Mutex);
1284 }
1285 }
1286
1287 _eglDestroyCurrentThread();
1288
1289 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1290 }
1291
1292
1293 static EGLImage EGLAPIENTRY
1294 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1295 EGLClientBuffer buffer, const EGLint *attr_list)
1296 {
1297 _EGLDisplay *disp = _eglLockDisplay(dpy);
1298 _EGLContext *context = _eglLookupContext(ctx, disp);
1299 _EGLDriver *drv;
1300 _EGLImage *img;
1301 EGLImage ret;
1302
1303 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1304 if (!disp->Extensions.KHR_image_base)
1305 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1306 if (!context && ctx != EGL_NO_CONTEXT)
1307 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1308 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1309 * <ctx> must be EGL_NO_CONTEXT..."
1310 */
1311 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1312 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1313
1314 img = drv->API.CreateImageKHR(drv,
1315 disp, context, target, buffer, attr_list);
1316 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1317
1318 RETURN_EGL_EVAL(disp, ret);
1319 }
1320
1321
1322 EGLImage EGLAPIENTRY
1323 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1324 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1325 {
1326 EGLImage image;
1327 EGLint *int_attribs = _eglConvertAttribsToInt(attr_list);
1328
1329 if (attr_list && !int_attribs)
1330 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1331
1332 image = eglCreateImageKHR(dpy, ctx, target, buffer, int_attribs);
1333 free(int_attribs);
1334 return image;
1335 }
1336
1337
1338 EGLBoolean EGLAPIENTRY
1339 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1340 {
1341 _EGLDisplay *disp = _eglLockDisplay(dpy);
1342 _EGLImage *img = _eglLookupImage(image, disp);
1343 _EGLDriver *drv;
1344 EGLBoolean ret;
1345
1346 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1347 if (!disp->Extensions.KHR_image_base)
1348 RETURN_EGL_EVAL(disp, EGL_FALSE);
1349 if (!img)
1350 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1351
1352 _eglUnlinkImage(img);
1353 ret = drv->API.DestroyImageKHR(drv, disp, img);
1354
1355 RETURN_EGL_EVAL(disp, ret);
1356 }
1357
1358
1359 static EGLSync
1360 _eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list,
1361 const EGLAttrib *attrib_list64, EGLBoolean is64,
1362 EGLenum invalid_type_error)
1363 {
1364 _EGLDisplay *disp = _eglLockDisplay(dpy);
1365 _EGLContext *ctx = _eglGetCurrentContext();
1366 _EGLDriver *drv;
1367 _EGLSync *sync;
1368 EGLSync ret;
1369
1370 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1371
1372 if (!disp->Extensions.KHR_cl_event2 && is64)
1373 RETURN_EGL_EVAL(disp, EGL_NO_SYNC_KHR);
1374
1375 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1376 if (!ctx || ctx->Resource.Display != dpy ||
1377 ctx->ClientAPI != EGL_OPENGL_ES_API)
1378 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1379
1380 switch (type) {
1381 case EGL_SYNC_FENCE_KHR:
1382 if (!disp->Extensions.KHR_fence_sync)
1383 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1384 break;
1385 case EGL_SYNC_REUSABLE_KHR:
1386 if (!disp->Extensions.KHR_reusable_sync)
1387 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1388 break;
1389 case EGL_SYNC_CL_EVENT_KHR:
1390 if (!disp->Extensions.KHR_cl_event2)
1391 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1392 break;
1393 default:
1394 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1395 }
1396
1397 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list, attrib_list64);
1398 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1399
1400 RETURN_EGL_EVAL(disp, ret);
1401 }
1402
1403
1404 static EGLSync EGLAPIENTRY
1405 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1406 {
1407 return _eglCreateSync(dpy, type, attrib_list, NULL, EGL_FALSE,
1408 EGL_BAD_ATTRIBUTE);
1409 }
1410
1411
1412 static EGLSync EGLAPIENTRY
1413 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1414 {
1415 return _eglCreateSync(dpy, type, NULL, attrib_list, EGL_TRUE,
1416 EGL_BAD_ATTRIBUTE);
1417 }
1418
1419
1420 EGLSync EGLAPIENTRY
1421 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1422 {
1423 return _eglCreateSync(dpy, type, NULL, attrib_list, EGL_TRUE,
1424 EGL_BAD_PARAMETER);
1425 }
1426
1427
1428 EGLBoolean EGLAPIENTRY
1429 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1430 {
1431 _EGLDisplay *disp = _eglLockDisplay(dpy);
1432 _EGLSync *s = _eglLookupSync(sync, disp);
1433 _EGLDriver *drv;
1434 EGLBoolean ret;
1435
1436 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1437 assert(disp->Extensions.KHR_reusable_sync ||
1438 disp->Extensions.KHR_fence_sync);
1439
1440 _eglUnlinkSync(s);
1441 ret = drv->API.DestroySyncKHR(drv, disp, s);
1442
1443 RETURN_EGL_EVAL(disp, ret);
1444 }
1445
1446
1447 EGLint EGLAPIENTRY
1448 eglClientWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags, EGLTime timeout)
1449 {
1450 _EGLDisplay *disp = _eglLockDisplay(dpy);
1451 _EGLSync *s = _eglLookupSync(sync, disp);
1452 _EGLDriver *drv;
1453 EGLint ret;
1454
1455 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1456 assert(disp->Extensions.KHR_reusable_sync ||
1457 disp->Extensions.KHR_fence_sync);
1458
1459 if (s->SyncStatus == EGL_SIGNALED_KHR)
1460 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1461
1462 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1463 * unlocked here to allow other threads also to be able to
1464 * go into waiting state.
1465 */
1466
1467 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1468 _eglUnlockDisplay(dpy);
1469
1470 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1471
1472 /*
1473 * 'disp' is already unlocked for reusable sync type,
1474 * so passing 'NULL' to bypass unlocking display.
1475 */
1476 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1477 RETURN_EGL_EVAL(NULL, ret);
1478 else
1479 RETURN_EGL_EVAL(disp, ret);
1480 }
1481
1482
1483 static EGLint EGLAPIENTRY
1484 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
1485 {
1486 _EGLDisplay *disp = _eglLockDisplay(dpy);
1487 _EGLSync *s = _eglLookupSync(sync, disp);
1488 _EGLContext *ctx = _eglGetCurrentContext();
1489 _EGLDriver *drv;
1490 EGLint ret;
1491
1492 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1493 assert(disp->Extensions.KHR_wait_sync);
1494
1495 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1496 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
1497 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1498
1499 /* the API doesn't allow any flags yet */
1500 if (flags != 0)
1501 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1502
1503 ret = drv->API.WaitSyncKHR(drv, disp, s);
1504
1505 RETURN_EGL_EVAL(disp, ret);
1506 }
1507
1508
1509 EGLBoolean EGLAPIENTRY
1510 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
1511 {
1512 /* The KHR version returns EGLint, while the core version returns
1513 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
1514 * EGL_TRUE.
1515 */
1516 return eglWaitSyncKHR(dpy, sync, flags);
1517 }
1518
1519
1520 static EGLBoolean EGLAPIENTRY
1521 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
1522 {
1523 _EGLDisplay *disp = _eglLockDisplay(dpy);
1524 _EGLSync *s = _eglLookupSync(sync, disp);
1525 _EGLDriver *drv;
1526 EGLBoolean ret;
1527
1528 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1529 assert(disp->Extensions.KHR_reusable_sync);
1530 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
1531
1532 RETURN_EGL_EVAL(disp, ret);
1533 }
1534
1535
1536 EGLBoolean EGLAPIENTRY
1537 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
1538 {
1539 _EGLDisplay *disp = _eglLockDisplay(dpy);
1540 _EGLSync *s = _eglLookupSync(sync, disp);
1541 _EGLDriver *drv;
1542 EGLBoolean ret;
1543
1544 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1545 assert(disp->Extensions.KHR_reusable_sync ||
1546 disp->Extensions.KHR_fence_sync);
1547 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
1548
1549 RETURN_EGL_EVAL(disp, ret);
1550 }
1551
1552
1553 static EGLBoolean EGLAPIENTRY
1554 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
1555 {
1556 EGLAttrib attrib;
1557 EGLBoolean result;
1558
1559 if (!value)
1560 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1561
1562 attrib = *value;
1563 result = eglGetSyncAttrib(dpy, sync, attribute, &attrib);
1564
1565 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
1566 *
1567 * If any error occurs, <*value> is not modified.
1568 */
1569 if (result == EGL_FALSE)
1570 return result;
1571
1572 *value = attrib;
1573 return result;
1574 }
1575
1576
1577 static EGLBoolean EGLAPIENTRY
1578 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
1579 EGLint numRects, const EGLint *rects)
1580 {
1581 _EGLContext *ctx = _eglGetCurrentContext();
1582 _EGLDisplay *disp = _eglLockDisplay(dpy);
1583 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1584 _EGLDriver *drv;
1585 EGLBoolean ret;
1586
1587 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1588
1589 if (!disp->Extensions.NOK_swap_region)
1590 RETURN_EGL_EVAL(disp, EGL_FALSE);
1591
1592 /* surface must be bound to current context in EGL 1.4 */
1593 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1594 surf != ctx->DrawSurface)
1595 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1596
1597 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
1598
1599 RETURN_EGL_EVAL(disp, ret);
1600 }
1601
1602
1603 static EGLImage EGLAPIENTRY
1604 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
1605 {
1606 _EGLDisplay *disp = _eglLockDisplay(dpy);
1607 _EGLDriver *drv;
1608 _EGLImage *img;
1609 EGLImage ret;
1610
1611 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1612 if (!disp->Extensions.MESA_drm_image)
1613 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1614
1615 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
1616 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1617
1618 RETURN_EGL_EVAL(disp, ret);
1619 }
1620
1621 static EGLBoolean EGLAPIENTRY
1622 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
1623 EGLint *name, EGLint *handle, EGLint *stride)
1624 {
1625 _EGLDisplay *disp = _eglLockDisplay(dpy);
1626 _EGLImage *img = _eglLookupImage(image, disp);
1627 _EGLDriver *drv;
1628 EGLBoolean ret;
1629
1630 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1631 assert(disp->Extensions.MESA_drm_image);
1632
1633 if (!img)
1634 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1635
1636 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
1637
1638 RETURN_EGL_EVAL(disp, ret);
1639 }
1640
1641
1642 struct wl_display;
1643
1644 static EGLBoolean EGLAPIENTRY
1645 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1646 {
1647 _EGLDisplay *disp = _eglLockDisplay(dpy);
1648 _EGLDriver *drv;
1649 EGLBoolean ret;
1650
1651 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1652 assert(disp->Extensions.WL_bind_wayland_display);
1653
1654 if (!display)
1655 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1656
1657 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
1658
1659 RETURN_EGL_EVAL(disp, ret);
1660 }
1661
1662 static EGLBoolean EGLAPIENTRY
1663 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
1664 {
1665 _EGLDisplay *disp = _eglLockDisplay(dpy);
1666 _EGLDriver *drv;
1667 EGLBoolean ret;
1668
1669 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1670 assert(disp->Extensions.WL_bind_wayland_display);
1671
1672 if (!display)
1673 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1674
1675 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
1676
1677 RETURN_EGL_EVAL(disp, ret);
1678 }
1679
1680 static EGLBoolean EGLAPIENTRY
1681 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
1682 EGLint attribute, EGLint *value)
1683 {
1684 _EGLDisplay *disp = _eglLockDisplay(dpy);
1685 _EGLDriver *drv;
1686 EGLBoolean ret;
1687
1688 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1689 assert(disp->Extensions.WL_bind_wayland_display);
1690
1691 if (!buffer)
1692 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1693
1694 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
1695
1696 RETURN_EGL_EVAL(disp, ret);
1697 }
1698
1699
1700 static struct wl_buffer * EGLAPIENTRY
1701 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
1702 {
1703 _EGLDisplay *disp = _eglLockDisplay(dpy);
1704 _EGLImage *img;
1705 _EGLDriver *drv;
1706 struct wl_buffer *ret;
1707
1708 _EGL_CHECK_DISPLAY(disp, NULL, drv);
1709 assert(disp->Extensions.WL_create_wayland_buffer_from_image);
1710
1711 img = _eglLookupImage(image, disp);
1712
1713 if (!img)
1714 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
1715
1716 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
1717
1718 RETURN_EGL_EVAL(disp, ret);
1719 }
1720
1721 static EGLBoolean EGLAPIENTRY
1722 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
1723 EGLint x, EGLint y, EGLint width, EGLint height)
1724 {
1725 _EGLDisplay *disp = _eglLockDisplay(dpy);
1726 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1727 _EGLDriver *drv;
1728 EGLBoolean ret;
1729
1730 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1731
1732 if (!disp->Extensions.NV_post_sub_buffer)
1733 RETURN_EGL_EVAL(disp, EGL_FALSE);
1734
1735 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
1736
1737 RETURN_EGL_EVAL(disp, ret);
1738 }
1739
1740 static EGLBoolean EGLAPIENTRY
1741 eglGetSyncValuesCHROMIUM(EGLDisplay display, EGLSurface surface,
1742 EGLuint64KHR *ust, EGLuint64KHR *msc,
1743 EGLuint64KHR *sbc)
1744 {
1745 _EGLDisplay *disp = _eglLockDisplay(display);
1746 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1747 _EGLDriver *drv;
1748 EGLBoolean ret;
1749
1750 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1751 if (!disp->Extensions.CHROMIUM_sync_control)
1752 RETURN_EGL_EVAL(disp, EGL_FALSE);
1753
1754 if (!ust || !msc || !sbc)
1755 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1756
1757 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
1758
1759 RETURN_EGL_EVAL(disp, ret);
1760 }
1761
1762 static EGLBoolean EGLAPIENTRY
1763 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
1764 EGLint *fourcc, EGLint *nplanes,
1765 EGLuint64KHR *modifiers)
1766 {
1767 _EGLDisplay *disp = _eglLockDisplay(dpy);
1768 _EGLImage *img = _eglLookupImage(image, disp);
1769 _EGLDriver *drv;
1770 EGLBoolean ret;
1771
1772 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1773 assert(disp->Extensions.MESA_image_dma_buf_export);
1774
1775 if (!img)
1776 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1777
1778 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
1779 modifiers);
1780
1781 RETURN_EGL_EVAL(disp, ret);
1782 }
1783
1784 static EGLBoolean EGLAPIENTRY
1785 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
1786 int *fds, EGLint *strides, EGLint *offsets)
1787 {
1788 _EGLDisplay *disp = _eglLockDisplay(dpy);
1789 _EGLImage *img = _eglLookupImage(image, disp);
1790 _EGLDriver *drv;
1791 EGLBoolean ret;
1792
1793 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1794 assert(disp->Extensions.MESA_image_dma_buf_export);
1795
1796 if (!img)
1797 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1798
1799 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
1800
1801 RETURN_EGL_EVAL(disp, ret);
1802 }
1803
1804 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
1805 eglGetProcAddress(const char *procname)
1806 {
1807 static const struct {
1808 const char *name;
1809 _EGLProc function;
1810 } egl_functions[] = {
1811 /* core functions queryable in the presence of
1812 * EGL_KHR_get_all_proc_addresses or EGL 1.5
1813 */
1814 /* alphabetical order */
1815 { "eglBindAPI", (_EGLProc) eglBindAPI },
1816 { "eglBindTexImage", (_EGLProc) eglBindTexImage },
1817 { "eglChooseConfig", (_EGLProc) eglChooseConfig },
1818 { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
1819 { "eglCreateContext", (_EGLProc) eglCreateContext },
1820 { "eglCreatePbufferFromClientBuffer", (_EGLProc) eglCreatePbufferFromClientBuffer },
1821 { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
1822 { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
1823 { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
1824 { "eglDestroyContext", (_EGLProc) eglDestroyContext },
1825 { "eglDestroySurface", (_EGLProc) eglDestroySurface },
1826 { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
1827 { "eglGetConfigs", (_EGLProc) eglGetConfigs },
1828 { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
1829 { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
1830 { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
1831 { "eglGetDisplay", (_EGLProc) eglGetDisplay },
1832 { "eglGetError", (_EGLProc) eglGetError },
1833 { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
1834 { "eglInitialize", (_EGLProc) eglInitialize },
1835 { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
1836 { "eglQueryAPI", (_EGLProc) eglQueryAPI },
1837 { "eglQueryContext", (_EGLProc) eglQueryContext },
1838 { "eglQueryString", (_EGLProc) eglQueryString },
1839 { "eglQuerySurface", (_EGLProc) eglQuerySurface },
1840 { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
1841 { "eglReleaseThread", (_EGLProc) eglReleaseThread },
1842 { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
1843 { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
1844 { "eglSwapInterval", (_EGLProc) eglSwapInterval },
1845 { "eglTerminate", (_EGLProc) eglTerminate },
1846 { "eglWaitClient", (_EGLProc) eglWaitClient },
1847 { "eglWaitGL", (_EGLProc) eglWaitGL },
1848 { "eglWaitNative", (_EGLProc) eglWaitNative },
1849 { "eglCreateSync", (_EGLProc) eglCreateSync },
1850 { "eglDestroySync", (_EGLProc) eglDestroySync },
1851 { "eglClientWaitSync", (_EGLProc) eglClientWaitSync },
1852 { "eglGetSyncAttrib", (_EGLProc) eglGetSyncAttrib },
1853 { "eglWaitSync", (_EGLProc) eglWaitSync },
1854 { "eglCreateImage", (_EGLProc) eglCreateImage },
1855 { "eglDestroyImage", (_EGLProc) eglDestroyImage },
1856 { "eglGetPlatformDisplay", (_EGLProc) eglGetPlatformDisplay },
1857 { "eglCreatePlatformWindowSurface", (_EGLProc) eglCreatePlatformWindowSurface },
1858 { "eglCreatePlatformPixmapSurface", (_EGLProc) eglCreatePlatformPixmapSurface },
1859 { "eglCreateImageKHR", (_EGLProc) eglCreateImageKHR },
1860 { "eglDestroyImageKHR", (_EGLProc) eglDestroyImage },
1861 { "eglCreateSyncKHR", (_EGLProc) eglCreateSyncKHR },
1862 { "eglCreateSync64KHR", (_EGLProc) eglCreateSync64KHR },
1863 { "eglDestroySyncKHR", (_EGLProc) eglDestroySync },
1864 { "eglClientWaitSyncKHR", (_EGLProc) eglClientWaitSync },
1865 { "eglWaitSyncKHR", (_EGLProc) eglWaitSyncKHR },
1866 { "eglSignalSyncKHR", (_EGLProc) eglSignalSyncKHR },
1867 { "eglGetSyncAttribKHR", (_EGLProc) eglGetSyncAttribKHR },
1868 { "eglSwapBuffersRegionNOK", (_EGLProc) eglSwapBuffersRegionNOK },
1869 { "eglCreateDRMImageMESA", (_EGLProc) eglCreateDRMImageMESA },
1870 { "eglExportDRMImageMESA", (_EGLProc) eglExportDRMImageMESA },
1871 { "eglBindWaylandDisplayWL", (_EGLProc) eglBindWaylandDisplayWL },
1872 { "eglUnbindWaylandDisplayWL", (_EGLProc) eglUnbindWaylandDisplayWL },
1873 { "eglQueryWaylandBufferWL", (_EGLProc) eglQueryWaylandBufferWL },
1874 { "eglCreateWaylandBufferFromImageWL", (_EGLProc) eglCreateWaylandBufferFromImageWL },
1875 { "eglPostSubBufferNV", (_EGLProc) eglPostSubBufferNV },
1876 { "eglSwapBuffersWithDamageEXT", (_EGLProc) eglSwapBuffersWithDamageEXT },
1877 { "eglGetPlatformDisplayEXT", (_EGLProc) eglGetPlatformDisplayEXT },
1878 { "eglCreatePlatformWindowSurfaceEXT", (_EGLProc) eglCreatePlatformWindowSurfaceEXT },
1879 { "eglCreatePlatformPixmapSurfaceEXT", (_EGLProc) eglCreatePlatformPixmapSurfaceEXT },
1880 { "eglGetSyncValuesCHROMIUM", (_EGLProc) eglGetSyncValuesCHROMIUM },
1881 { "eglExportDMABUFImageQueryMESA", (_EGLProc) eglExportDMABUFImageQueryMESA },
1882 { "eglExportDMABUFImageMESA", (_EGLProc) eglExportDMABUFImageMESA },
1883 { NULL, NULL }
1884 };
1885 EGLint i;
1886 _EGLProc ret;
1887
1888 if (!procname)
1889 RETURN_EGL_SUCCESS(NULL, NULL);
1890
1891 ret = NULL;
1892 if (strncmp(procname, "egl", 3) == 0) {
1893 for (i = 0; egl_functions[i].name; i++) {
1894 if (strcmp(egl_functions[i].name, procname) == 0) {
1895 ret = egl_functions[i].function;
1896 break;
1897 }
1898 }
1899 }
1900 if (!ret)
1901 ret = _eglGetDriverProc(procname);
1902
1903 RETURN_EGL_SUCCESS(NULL, ret);
1904 }
1905
1906 static int
1907 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
1908 _EGLDisplay **disp, _EGLDriver **drv,
1909 _EGLContext **ctx)
1910 {
1911
1912 *disp = _eglLockDisplay(dpy);
1913 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
1914 if (*disp)
1915 _eglUnlockDisplay(*disp);
1916 return MESA_GLINTEROP_INVALID_DISPLAY;
1917 }
1918
1919 *drv = (*disp)->Driver;
1920
1921 *ctx = _eglLookupContext(context, *disp);
1922 if (!*ctx ||
1923 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
1924 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
1925 _eglUnlockDisplay(*disp);
1926 return MESA_GLINTEROP_INVALID_CONTEXT;
1927 }
1928
1929 return MESA_GLINTEROP_SUCCESS;
1930 }
1931
1932 int
1933 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
1934 struct mesa_glinterop_device_info *out)
1935 {
1936 _EGLDisplay *disp;
1937 _EGLDriver *drv;
1938 _EGLContext *ctx;
1939 int ret;
1940
1941 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
1942 if (ret != MESA_GLINTEROP_SUCCESS)
1943 return ret;
1944
1945 if (drv->API.GLInteropQueryDeviceInfo)
1946 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
1947 else
1948 ret = MESA_GLINTEROP_UNSUPPORTED;
1949
1950 _eglUnlockDisplay(disp);
1951 return ret;
1952 }
1953
1954 int
1955 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
1956 struct mesa_glinterop_export_in *in,
1957 struct mesa_glinterop_export_out *out)
1958 {
1959 _EGLDisplay *disp;
1960 _EGLDriver *drv;
1961 _EGLContext *ctx;
1962 int ret;
1963
1964 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
1965 if (ret != MESA_GLINTEROP_SUCCESS)
1966 return ret;
1967
1968 if (drv->API.GLInteropExportObject)
1969 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
1970 else
1971 ret = MESA_GLINTEROP_UNSUPPORTED;
1972
1973 _eglUnlockDisplay(disp);
1974 return ret;
1975 }