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