05c529ba0a78226f82e32c913373097f2a0e2590
[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 #ifdef USE_LIBGLVND
87 #define EGLAPI
88 #undef PUBLIC
89 #define PUBLIC
90 #endif
91
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <string.h>
95 #include "c99_compat.h"
96 #include "c11/threads.h"
97 #include "util/macros.h"
98
99 #include "eglapi.h"
100 #include "egldefines.h"
101 #include "eglglobals.h"
102 #include "eglcontext.h"
103 #include "egldisplay.h"
104 #include "egltypedefs.h"
105 #include "eglcurrent.h"
106 #include "egldevice.h"
107 #include "egldriver.h"
108 #include "eglsurface.h"
109 #include "eglconfig.h"
110 #include "eglimage.h"
111 #include "eglsync.h"
112
113 #include "GL/mesa_glinterop.h"
114
115 /**
116 * Macros to help return an API entrypoint.
117 *
118 * These macros will unlock the display and record the error code.
119 */
120 #define RETURN_EGL_ERROR(disp, err, ret) \
121 do { \
122 if (disp) \
123 _eglUnlockDisplay(disp); \
124 /* EGL error codes are non-zero */ \
125 if (err) \
126 _eglError(err, __func__); \
127 return ret; \
128 } while (0)
129
130 #define RETURN_EGL_SUCCESS(disp, ret) \
131 RETURN_EGL_ERROR(disp, EGL_SUCCESS, ret)
132
133 /* record EGL_SUCCESS only when ret evaluates to true */
134 #define RETURN_EGL_EVAL(disp, ret) \
135 RETURN_EGL_ERROR(disp, (ret) ? EGL_SUCCESS : 0, ret)
136
137
138 /*
139 * A bunch of macros and checks to simplify error checking.
140 */
141
142 #define _EGL_CHECK_DISPLAY(disp, ret, drv) \
143 do { \
144 drv = _eglCheckDisplay(disp, __func__); \
145 if (!drv) \
146 RETURN_EGL_ERROR(disp, 0, ret); \
147 } while (0)
148
149 #define _EGL_CHECK_OBJECT(disp, type, obj, ret, drv) \
150 do { \
151 drv = _eglCheck ## type(disp, obj, __func__); \
152 if (!drv) \
153 RETURN_EGL_ERROR(disp, 0, ret); \
154 } while (0)
155
156 #define _EGL_CHECK_SURFACE(disp, surf, ret, drv) \
157 _EGL_CHECK_OBJECT(disp, Surface, surf, ret, drv)
158
159 #define _EGL_CHECK_CONTEXT(disp, context, ret, drv) \
160 _EGL_CHECK_OBJECT(disp, Context, context, ret, drv)
161
162 #define _EGL_CHECK_CONFIG(disp, conf, ret, drv) \
163 _EGL_CHECK_OBJECT(disp, Config, conf, ret, drv)
164
165 #define _EGL_CHECK_SYNC(disp, s, ret, drv) \
166 _EGL_CHECK_OBJECT(disp, Sync, s, ret, drv)
167
168
169 struct _egl_entrypoint {
170 const char *name;
171 _EGLProc function;
172 };
173
174
175 static inline _EGLDriver *
176 _eglCheckDisplay(_EGLDisplay *disp, const char *msg)
177 {
178 if (!disp) {
179 _eglError(EGL_BAD_DISPLAY, msg);
180 return NULL;
181 }
182 if (!disp->Initialized) {
183 _eglError(EGL_NOT_INITIALIZED, msg);
184 return NULL;
185 }
186 return disp->Driver;
187 }
188
189
190 static inline _EGLDriver *
191 _eglCheckSurface(_EGLDisplay *disp, _EGLSurface *surf, const char *msg)
192 {
193 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
194 if (!drv)
195 return NULL;
196 if (!surf) {
197 _eglError(EGL_BAD_SURFACE, msg);
198 return NULL;
199 }
200 return drv;
201 }
202
203
204 static inline _EGLDriver *
205 _eglCheckContext(_EGLDisplay *disp, _EGLContext *context, const char *msg)
206 {
207 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
208 if (!drv)
209 return NULL;
210 if (!context) {
211 _eglError(EGL_BAD_CONTEXT, msg);
212 return NULL;
213 }
214 return drv;
215 }
216
217
218 static inline _EGLDriver *
219 _eglCheckConfig(_EGLDisplay *disp, _EGLConfig *conf, const char *msg)
220 {
221 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
222 if (!drv)
223 return NULL;
224 if (!conf) {
225 _eglError(EGL_BAD_CONFIG, msg);
226 return NULL;
227 }
228 return drv;
229 }
230
231
232 static inline _EGLDriver *
233 _eglCheckSync(_EGLDisplay *disp, _EGLSync *s, const char *msg)
234 {
235 _EGLDriver *drv = _eglCheckDisplay(disp, msg);
236 if (!drv)
237 return NULL;
238 if (!s) {
239 _eglError(EGL_BAD_PARAMETER, msg);
240 return NULL;
241 }
242 return drv;
243 }
244
245
246 /**
247 * Lookup and lock a display.
248 */
249 static inline _EGLDisplay *
250 _eglLockDisplay(EGLDisplay dpy)
251 {
252 _EGLDisplay *disp = _eglLookupDisplay(dpy);
253 if (disp)
254 mtx_lock(&disp->Mutex);
255 return disp;
256 }
257
258
259 /**
260 * Unlock a display.
261 */
262 static inline void
263 _eglUnlockDisplay(_EGLDisplay *disp)
264 {
265 mtx_unlock(&disp->Mutex);
266 }
267
268 static EGLBoolean
269 _eglSetFuncName(const char *funcName, _EGLDisplay *disp, EGLenum objectType, _EGLResource *object)
270 {
271 _EGLThreadInfo *thr = _eglGetCurrentThread();
272 if (!_eglIsCurrentThreadDummy()) {
273 thr->CurrentFuncName = funcName;
274 thr->CurrentObjectLabel = NULL;
275
276 if (objectType == EGL_OBJECT_THREAD_KHR)
277 thr->CurrentObjectLabel = thr->Label;
278 else if (objectType == EGL_OBJECT_DISPLAY_KHR && disp)
279 thr->CurrentObjectLabel = disp->Label;
280 else if (object)
281 thr->CurrentObjectLabel = object->Label;
282
283 return EGL_TRUE;
284 }
285
286 _eglDebugReport(EGL_BAD_ALLOC, funcName, EGL_DEBUG_MSG_CRITICAL_KHR, NULL);
287 return EGL_FALSE;
288 }
289
290 #define _EGL_FUNC_START(disp, objectType, object, ret) \
291 do { \
292 if (!_eglSetFuncName(__func__, disp, objectType, (_EGLResource *) object)) { \
293 if (disp) \
294 _eglUnlockDisplay(disp); \
295 return ret; \
296 } \
297 } while(0)
298
299 /**
300 * Convert an attribute list from EGLint[] to EGLAttrib[].
301 *
302 * Return an EGL error code. The output parameter out_attrib_list is modified
303 * only on success.
304 */
305 static EGLint
306 _eglConvertIntsToAttribs(const EGLint *int_list, EGLAttrib **out_attrib_list)
307 {
308 size_t len = 0;
309 EGLAttrib *attrib_list;
310
311 if (int_list) {
312 while (int_list[2*len] != EGL_NONE)
313 ++len;
314 }
315
316 if (len == 0) {
317 *out_attrib_list = NULL;
318 return EGL_SUCCESS;
319 }
320
321 if (2*len + 1 > SIZE_MAX / sizeof(EGLAttrib))
322 return EGL_BAD_ALLOC;
323
324 attrib_list = malloc((2*len + 1) * sizeof(EGLAttrib));
325 if (!attrib_list)
326 return EGL_BAD_ALLOC;
327
328 for (size_t i = 0; i < len; ++i) {
329 attrib_list[2*i + 0] = int_list[2*i + 0];
330 attrib_list[2*i + 1] = int_list[2*i + 1];
331 }
332
333 attrib_list[2*len] = EGL_NONE;
334
335 *out_attrib_list = attrib_list;
336 return EGL_SUCCESS;
337 }
338
339
340 static EGLint *
341 _eglConvertAttribsToInt(const EGLAttrib *attr_list)
342 {
343 size_t size = _eglNumAttribs(attr_list);
344 EGLint *int_attribs = NULL;
345
346 /* Convert attributes from EGLAttrib[] to EGLint[] */
347 if (size) {
348 int_attribs = calloc(size, sizeof(int_attribs[0]));
349 if (!int_attribs)
350 return NULL;
351
352 for (size_t i = 0; i < size; i++)
353 int_attribs[i] = attr_list[i];
354 }
355 return int_attribs;
356 }
357
358
359 /**
360 * This is typically the first EGL function that an application calls.
361 * It associates a private _EGLDisplay object to the native display.
362 */
363 EGLDisplay EGLAPIENTRY
364 eglGetDisplay(EGLNativeDisplayType nativeDisplay)
365 {
366 _EGLPlatformType plat;
367 _EGLDisplay *disp;
368 void *native_display_ptr;
369
370 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
371
372 STATIC_ASSERT(sizeof(void*) == sizeof(nativeDisplay));
373 native_display_ptr = (void*) nativeDisplay;
374
375 plat = _eglGetNativePlatform(native_display_ptr);
376 disp = _eglFindDisplay(plat, native_display_ptr, NULL);
377 return _eglGetDisplayHandle(disp);
378 }
379
380 static EGLDisplay
381 _eglGetPlatformDisplayCommon(EGLenum platform, void *native_display,
382 const EGLAttrib *attrib_list)
383 {
384 _EGLDisplay *disp;
385
386 switch (platform) {
387 #ifdef HAVE_X11_PLATFORM
388 case EGL_PLATFORM_X11_EXT:
389 disp = _eglGetX11Display((Display*) native_display, attrib_list);
390 break;
391 #endif
392 #ifdef HAVE_DRM_PLATFORM
393 case EGL_PLATFORM_GBM_MESA:
394 disp = _eglGetGbmDisplay((struct gbm_device*) native_display,
395 attrib_list);
396 break;
397 #endif
398 #ifdef HAVE_WAYLAND_PLATFORM
399 case EGL_PLATFORM_WAYLAND_EXT:
400 disp = _eglGetWaylandDisplay((struct wl_display*) native_display,
401 attrib_list);
402 break;
403 #endif
404 #ifdef HAVE_SURFACELESS_PLATFORM
405 case EGL_PLATFORM_SURFACELESS_MESA:
406 disp = _eglGetSurfacelessDisplay(native_display, attrib_list);
407 break;
408 #endif
409 default:
410 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, NULL);
411 }
412
413 return _eglGetDisplayHandle(disp);
414 }
415
416 static EGLDisplay EGLAPIENTRY
417 eglGetPlatformDisplayEXT(EGLenum platform, void *native_display,
418 const EGLint *int_attribs)
419 {
420 EGLAttrib *attrib_list;
421 EGLDisplay disp;
422
423 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
424
425 if (_eglConvertIntsToAttribs(int_attribs, &attrib_list) != EGL_SUCCESS)
426 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, NULL);
427
428 disp = _eglGetPlatformDisplayCommon(platform, native_display, attrib_list);
429 free(attrib_list);
430 return disp;
431 }
432
433 EGLDisplay EGLAPIENTRY
434 eglGetPlatformDisplay(EGLenum platform, void *native_display,
435 const EGLAttrib *attrib_list)
436 {
437 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_NO_DISPLAY);
438 return _eglGetPlatformDisplayCommon(platform, native_display, attrib_list);
439 }
440
441 /**
442 * Copy the extension into the string and update the string pointer.
443 */
444 static EGLint
445 _eglAppendExtension(char **str, const char *ext)
446 {
447 char *s = *str;
448 size_t len = strlen(ext);
449
450 if (s) {
451 memcpy(s, ext, len);
452 s[len++] = ' ';
453 s[len] = '\0';
454
455 *str += len;
456 }
457 else {
458 len++;
459 }
460
461 return (EGLint) len;
462 }
463
464 /**
465 * Examine the individual extension enable/disable flags and recompute
466 * the driver's Extensions string.
467 */
468 static void
469 _eglCreateExtensionsString(_EGLDisplay *disp)
470 {
471 #define _EGL_CHECK_EXTENSION(ext) \
472 do { \
473 if (disp->Extensions.ext) { \
474 _eglAppendExtension(&exts, "EGL_" #ext); \
475 assert(exts <= disp->ExtensionsString + _EGL_MAX_EXTENSIONS_LEN); \
476 } \
477 } while (0)
478
479 char *exts = disp->ExtensionsString;
480
481 /* Please keep these sorted alphabetically. */
482 _EGL_CHECK_EXTENSION(ANDROID_blob_cache);
483 _EGL_CHECK_EXTENSION(ANDROID_framebuffer_target);
484 _EGL_CHECK_EXTENSION(ANDROID_image_native_buffer);
485 _EGL_CHECK_EXTENSION(ANDROID_native_fence_sync);
486 _EGL_CHECK_EXTENSION(ANDROID_recordable);
487
488 _EGL_CHECK_EXTENSION(CHROMIUM_sync_control);
489
490 _EGL_CHECK_EXTENSION(EXT_buffer_age);
491 _EGL_CHECK_EXTENSION(EXT_create_context_robustness);
492 _EGL_CHECK_EXTENSION(EXT_image_dma_buf_import);
493 _EGL_CHECK_EXTENSION(EXT_image_dma_buf_import_modifiers);
494 _EGL_CHECK_EXTENSION(EXT_surface_CTA861_3_metadata);
495 _EGL_CHECK_EXTENSION(EXT_surface_SMPTE2086_metadata);
496 _EGL_CHECK_EXTENSION(EXT_swap_buffers_with_damage);
497
498 _EGL_CHECK_EXTENSION(IMG_context_priority);
499
500 _EGL_CHECK_EXTENSION(KHR_cl_event2);
501 _EGL_CHECK_EXTENSION(KHR_config_attribs);
502 _EGL_CHECK_EXTENSION(KHR_context_flush_control);
503 _EGL_CHECK_EXTENSION(KHR_create_context);
504 _EGL_CHECK_EXTENSION(KHR_create_context_no_error);
505 _EGL_CHECK_EXTENSION(KHR_fence_sync);
506 _EGL_CHECK_EXTENSION(KHR_get_all_proc_addresses);
507 _EGL_CHECK_EXTENSION(KHR_gl_colorspace);
508 _EGL_CHECK_EXTENSION(KHR_gl_renderbuffer_image);
509 _EGL_CHECK_EXTENSION(KHR_gl_texture_2D_image);
510 _EGL_CHECK_EXTENSION(KHR_gl_texture_3D_image);
511 _EGL_CHECK_EXTENSION(KHR_gl_texture_cubemap_image);
512 if (disp->Extensions.KHR_image_base && disp->Extensions.KHR_image_pixmap)
513 disp->Extensions.KHR_image = EGL_TRUE;
514 _EGL_CHECK_EXTENSION(KHR_image);
515 _EGL_CHECK_EXTENSION(KHR_image_base);
516 _EGL_CHECK_EXTENSION(KHR_image_pixmap);
517 _EGL_CHECK_EXTENSION(KHR_mutable_render_buffer);
518 _EGL_CHECK_EXTENSION(KHR_no_config_context);
519 _EGL_CHECK_EXTENSION(KHR_partial_update);
520 _EGL_CHECK_EXTENSION(KHR_reusable_sync);
521 _EGL_CHECK_EXTENSION(KHR_surfaceless_context);
522 if (disp->Extensions.EXT_swap_buffers_with_damage)
523 _eglAppendExtension(&exts, "EGL_KHR_swap_buffers_with_damage");
524 _EGL_CHECK_EXTENSION(EXT_pixel_format_float);
525 _EGL_CHECK_EXTENSION(KHR_wait_sync);
526
527 if (disp->Extensions.KHR_no_config_context)
528 _eglAppendExtension(&exts, "EGL_MESA_configless_context");
529 _EGL_CHECK_EXTENSION(MESA_drm_image);
530 _EGL_CHECK_EXTENSION(MESA_image_dma_buf_export);
531 _EGL_CHECK_EXTENSION(MESA_query_driver);
532
533 _EGL_CHECK_EXTENSION(NOK_swap_region);
534 _EGL_CHECK_EXTENSION(NOK_texture_from_pixmap);
535
536 _EGL_CHECK_EXTENSION(NV_post_sub_buffer);
537
538 _EGL_CHECK_EXTENSION(WL_bind_wayland_display);
539 _EGL_CHECK_EXTENSION(WL_create_wayland_buffer_from_image);
540
541 #undef _EGL_CHECK_EXTENSION
542 }
543
544 static void
545 _eglCreateAPIsString(_EGLDisplay *disp)
546 {
547 #define addstr(str) \
548 { \
549 const size_t old_len = strlen(disp->ClientAPIsString); \
550 const size_t add_len = sizeof(str); \
551 const size_t max_len = sizeof(disp->ClientAPIsString) - 1; \
552 if (old_len + add_len <= max_len) \
553 strcat(disp->ClientAPIsString, str " "); \
554 else \
555 assert(!"disp->ClientAPIsString is not large enough"); \
556 }
557
558 if (disp->ClientAPIs & EGL_OPENGL_BIT)
559 addstr("OpenGL");
560
561 if (disp->ClientAPIs & EGL_OPENGL_ES_BIT ||
562 disp->ClientAPIs & EGL_OPENGL_ES2_BIT ||
563 disp->ClientAPIs & EGL_OPENGL_ES3_BIT_KHR) {
564 addstr("OpenGL_ES");
565 }
566
567 if (disp->ClientAPIs & EGL_OPENVG_BIT)
568 addstr("OpenVG");
569
570 #undef addstr
571 }
572
573 static void
574 _eglComputeVersion(_EGLDisplay *disp)
575 {
576 disp->Version = 14;
577
578 if (disp->Extensions.KHR_fence_sync &&
579 disp->Extensions.KHR_cl_event2 &&
580 disp->Extensions.KHR_wait_sync &&
581 disp->Extensions.KHR_image_base &&
582 disp->Extensions.KHR_gl_texture_2D_image &&
583 disp->Extensions.KHR_gl_texture_3D_image &&
584 disp->Extensions.KHR_gl_texture_cubemap_image &&
585 disp->Extensions.KHR_gl_renderbuffer_image &&
586 disp->Extensions.KHR_create_context &&
587 disp->Extensions.EXT_create_context_robustness &&
588 disp->Extensions.KHR_get_all_proc_addresses &&
589 disp->Extensions.KHR_gl_colorspace &&
590 disp->Extensions.KHR_surfaceless_context)
591 disp->Version = 15;
592 }
593
594 /**
595 * This is typically the second EGL function that an application calls.
596 * Here we load/initialize the actual hardware driver.
597 */
598 EGLBoolean EGLAPIENTRY
599 eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
600 {
601 _EGLDisplay *disp = _eglLockDisplay(dpy);
602
603 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
604
605 if (!disp)
606 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
607
608 if (!disp->Initialized) {
609 if (!_eglMatchDriver(disp))
610 RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
611
612 /* limit to APIs supported by core */
613 disp->ClientAPIs &= _EGL_API_ALL_BITS;
614
615 /* EGL_KHR_get_all_proc_addresses is a corner-case extension. The spec
616 * classifies it as an EGL display extension, though conceptually it's an
617 * EGL client extension.
618 *
619 * From the EGL_KHR_get_all_proc_addresses spec:
620 *
621 * The EGL implementation must expose the name
622 * EGL_KHR_client_get_all_proc_addresses if and only if it exposes
623 * EGL_KHR_get_all_proc_addresses and supports
624 * EGL_EXT_client_extensions.
625 *
626 * Mesa unconditionally exposes both client extensions mentioned above,
627 * so the spec requires that each EGLDisplay unconditionally expose
628 * EGL_KHR_get_all_proc_addresses also.
629 */
630 disp->Extensions.KHR_get_all_proc_addresses = EGL_TRUE;
631
632 /* Extensions is used to provide EGL 1.3 functionality for 1.2 aware
633 * programs. It is driver agnostic and handled in the main EGL code.
634 */
635 disp->Extensions.KHR_config_attribs = EGL_TRUE;
636
637 _eglComputeVersion(disp);
638 _eglCreateExtensionsString(disp);
639 _eglCreateAPIsString(disp);
640 snprintf(disp->VersionString, sizeof(disp->VersionString),
641 "%d.%d", disp->Version / 10, disp->Version % 10);
642 }
643
644 /* Update applications version of major and minor if not NULL */
645 if ((major != NULL) && (minor != NULL)) {
646 *major = disp->Version / 10;
647 *minor = disp->Version % 10;
648 }
649
650 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
651 }
652
653
654 EGLBoolean EGLAPIENTRY
655 eglTerminate(EGLDisplay dpy)
656 {
657 _EGLDisplay *disp = _eglLockDisplay(dpy);
658
659 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
660
661 if (!disp)
662 RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
663
664 if (disp->Initialized) {
665 _EGLDriver *drv = disp->Driver;
666
667 drv->API.Terminate(drv, disp);
668 /* do not reset disp->Driver */
669 disp->ClientAPIsString[0] = 0;
670 disp->Initialized = EGL_FALSE;
671 }
672
673 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
674 }
675
676
677 const char * EGLAPIENTRY
678 eglQueryString(EGLDisplay dpy, EGLint name)
679 {
680 _EGLDisplay *disp;
681 _EGLDriver *drv;
682
683 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS) {
684 const char *ret = _eglGetClientExtensionString();
685 if (ret != NULL)
686 RETURN_EGL_SUCCESS(NULL, ret);
687 else
688 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, NULL);
689 }
690
691 disp = _eglLockDisplay(dpy);
692 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, NULL);
693 _EGL_CHECK_DISPLAY(disp, NULL, drv);
694
695 switch (name) {
696 case EGL_VENDOR:
697 RETURN_EGL_SUCCESS(disp, _EGL_VENDOR_STRING);
698 case EGL_VERSION:
699 RETURN_EGL_SUCCESS(disp, disp->VersionString);
700 case EGL_EXTENSIONS:
701 RETURN_EGL_SUCCESS(disp, disp->ExtensionsString);
702 case EGL_CLIENT_APIS:
703 RETURN_EGL_SUCCESS(disp, disp->ClientAPIsString);
704 default:
705 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
706 }
707 }
708
709
710 EGLBoolean EGLAPIENTRY
711 eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
712 EGLint config_size, EGLint *num_config)
713 {
714 _EGLDisplay *disp = _eglLockDisplay(dpy);
715 _EGLDriver *drv;
716 EGLBoolean ret;
717
718 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
719
720 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
721 ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
722
723 RETURN_EGL_EVAL(disp, ret);
724 }
725
726
727 EGLBoolean EGLAPIENTRY
728 eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
729 EGLint config_size, EGLint *num_config)
730 {
731 _EGLDisplay *disp = _eglLockDisplay(dpy);
732 _EGLDriver *drv;
733 EGLBoolean ret;
734
735 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
736
737 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
738 ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
739 config_size, num_config);
740
741 RETURN_EGL_EVAL(disp, ret);
742 }
743
744
745 EGLBoolean EGLAPIENTRY
746 eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
747 EGLint attribute, EGLint *value)
748 {
749 _EGLDisplay *disp = _eglLockDisplay(dpy);
750 _EGLConfig *conf = _eglLookupConfig(config, disp);
751 _EGLDriver *drv;
752 EGLBoolean ret;
753
754 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
755
756 _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
757 ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
758
759 RETURN_EGL_EVAL(disp, ret);
760 }
761
762
763 EGLContext EGLAPIENTRY
764 eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
765 const EGLint *attrib_list)
766 {
767 _EGLDisplay *disp = _eglLockDisplay(dpy);
768 _EGLConfig *conf = _eglLookupConfig(config, disp);
769 _EGLContext *share = _eglLookupContext(share_list, disp);
770 _EGLDriver *drv;
771 _EGLContext *context;
772 EGLContext ret;
773
774 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_CONTEXT);
775
776 _EGL_CHECK_DISPLAY(disp, EGL_NO_CONTEXT, drv);
777
778 if (config != EGL_NO_CONFIG_KHR)
779 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_CONTEXT, drv);
780 else if (!disp->Extensions.KHR_no_config_context)
781 RETURN_EGL_ERROR(disp, EGL_BAD_CONFIG, EGL_NO_CONTEXT);
782
783 if (!share && share_list != EGL_NO_CONTEXT)
784 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
785
786 context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
787 ret = (context) ? _eglLinkContext(context) : EGL_NO_CONTEXT;
788
789 RETURN_EGL_EVAL(disp, ret);
790 }
791
792
793 EGLBoolean EGLAPIENTRY
794 eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
795 {
796 _EGLDisplay *disp = _eglLockDisplay(dpy);
797 _EGLContext *context = _eglLookupContext(ctx, disp);
798 _EGLDriver *drv;
799 EGLBoolean ret;
800
801 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
802
803 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
804 _eglUnlinkContext(context);
805 ret = drv->API.DestroyContext(drv, disp, context);
806
807 RETURN_EGL_EVAL(disp, ret);
808 }
809
810
811 EGLBoolean EGLAPIENTRY
812 eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
813 EGLContext ctx)
814 {
815 _EGLDisplay *disp = _eglLockDisplay(dpy);
816 _EGLContext *context = _eglLookupContext(ctx, disp);
817 _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
818 _EGLSurface *read_surf = _eglLookupSurface(read, disp);
819 _EGLDriver *drv;
820 EGLBoolean ret;
821
822 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
823
824 if (!disp)
825 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
826 drv = disp->Driver;
827
828 /* display is allowed to be uninitialized under certain condition */
829 if (!disp->Initialized) {
830 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
831 ctx != EGL_NO_CONTEXT)
832 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
833 }
834 if (!drv)
835 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
836
837 if (!context && ctx != EGL_NO_CONTEXT)
838 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
839 if (!draw_surf || !read_surf) {
840 /* From the EGL 1.4 (20130211) spec:
841 *
842 * To release the current context without assigning a new one, set ctx
843 * to EGL_NO_CONTEXT and set draw and read to EGL_NO_SURFACE.
844 */
845 if (!disp->Extensions.KHR_surfaceless_context && ctx != EGL_NO_CONTEXT)
846 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
847
848 if ((!draw_surf && draw != EGL_NO_SURFACE) ||
849 (!read_surf && read != EGL_NO_SURFACE))
850 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
851 if (draw_surf || read_surf)
852 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
853 }
854
855 /* If a native window underlying either draw or read is no longer valid,
856 * an EGL_BAD_NATIVE_WINDOW error is generated.
857 */
858 if (draw_surf && draw_surf->Lost)
859 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
860 if (read_surf && read_surf->Lost)
861 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
862
863 ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
864
865 RETURN_EGL_EVAL(disp, ret);
866 }
867
868
869 EGLBoolean EGLAPIENTRY
870 eglQueryContext(EGLDisplay dpy, EGLContext ctx,
871 EGLint attribute, EGLint *value)
872 {
873 _EGLDisplay *disp = _eglLockDisplay(dpy);
874 _EGLContext *context = _eglLookupContext(ctx, disp);
875 _EGLDriver *drv;
876 EGLBoolean ret;
877
878 _EGL_FUNC_START(disp, EGL_OBJECT_CONTEXT_KHR, context, EGL_FALSE);
879
880 _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
881 ret = drv->API.QueryContext(drv, disp, context, attribute, value);
882
883 RETURN_EGL_EVAL(disp, ret);
884 }
885
886
887 /* In EGL specs 1.4 and 1.5, at the end of sections 3.5.1 and 3.5.4, it says
888 * that if native_surface was already used to create a window or pixmap, we
889 * can't create a new one. This is what this function checks for.
890 */
891 static bool
892 _eglNativeSurfaceAlreadyUsed(_EGLDisplay *disp, void *native_surface)
893 {
894 _EGLResource *list;
895
896 list = disp->ResourceLists[_EGL_RESOURCE_SURFACE];
897 while (list) {
898 _EGLSurface *surf = (_EGLSurface *) list;
899
900 list = list->Next;
901
902 if (surf->Type == EGL_PBUFFER_BIT)
903 continue;
904
905 if (surf->NativeSurface == native_surface)
906 return true;
907 }
908
909 return false;
910 }
911
912
913 static EGLSurface
914 _eglCreateWindowSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
915 void *native_window, const EGLint *attrib_list)
916 {
917 _EGLConfig *conf = _eglLookupConfig(config, disp);
918 _EGLDriver *drv;
919 _EGLSurface *surf;
920 EGLSurface ret;
921
922
923 if (native_window == NULL)
924 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
925
926 #ifdef HAVE_SURFACELESS_PLATFORM
927 if (disp && disp->Platform == _EGL_PLATFORM_SURFACELESS) {
928 /* From the EGL_MESA_platform_surfaceless spec (v1):
929 *
930 * eglCreatePlatformWindowSurface fails when called with a <display>
931 * that belongs to the surfaceless platform. It returns
932 * EGL_NO_SURFACE and generates EGL_BAD_NATIVE_WINDOW. The
933 * justification for this unconditional failure is that the
934 * surfaceless platform has no native windows, and therefore the
935 * <native_window> parameter is always invalid.
936 *
937 * This check must occur before checking the EGLConfig, which emits
938 * EGL_BAD_CONFIG.
939 */
940 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
941 }
942 #endif
943
944 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
945
946 if ((conf->SurfaceType & EGL_WINDOW_BIT) == 0)
947 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
948
949 if (_eglNativeSurfaceAlreadyUsed(disp, native_window))
950 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
951
952 surf = drv->API.CreateWindowSurface(drv, disp, conf, native_window,
953 attrib_list);
954 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
955
956 RETURN_EGL_EVAL(disp, ret);
957 }
958
959
960 EGLSurface EGLAPIENTRY
961 eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
962 EGLNativeWindowType window, const EGLint *attrib_list)
963 {
964 _EGLDisplay *disp = _eglLockDisplay(dpy);
965
966 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
967 STATIC_ASSERT(sizeof(void*) == sizeof(window));
968 return _eglCreateWindowSurfaceCommon(disp, config, (void*) window,
969 attrib_list);
970 }
971
972 static void *
973 _fixupNativeWindow(_EGLDisplay *disp, void *native_window)
974 {
975 #ifdef HAVE_X11_PLATFORM
976 if (disp && disp->Platform == _EGL_PLATFORM_X11 && native_window != NULL) {
977 /* The `native_window` parameter for the X11 platform differs between
978 * eglCreateWindowSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
979 * eglCreateWindowSurface(), the type of `native_window` is an Xlib
980 * `Window`. In eglCreatePlatformWindowSurfaceEXT(), the type is
981 * `Window*`. Convert `Window*` to `Window` because that's what
982 * dri2_x11_create_window_surface() expects.
983 */
984 return (void *)(* (Window*) native_window);
985 }
986 #endif
987 return native_window;
988 }
989
990 static EGLSurface EGLAPIENTRY
991 eglCreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config,
992 void *native_window,
993 const EGLint *attrib_list)
994 {
995 _EGLDisplay *disp = _eglLockDisplay(dpy);
996
997 native_window = _fixupNativeWindow(disp, native_window);
998
999 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1000 return _eglCreateWindowSurfaceCommon(disp, config, native_window,
1001 attrib_list);
1002 }
1003
1004
1005 EGLSurface EGLAPIENTRY
1006 eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
1007 void *native_window,
1008 const EGLAttrib *attrib_list)
1009 {
1010 _EGLDisplay *disp = _eglLockDisplay(dpy);
1011 EGLSurface surface;
1012 EGLint *int_attribs;
1013
1014 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1015
1016 int_attribs = _eglConvertAttribsToInt(attrib_list);
1017 if (attrib_list && !int_attribs)
1018 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1019
1020 native_window = _fixupNativeWindow(disp, native_window);
1021 surface = _eglCreateWindowSurfaceCommon(disp, config, native_window,
1022 int_attribs);
1023 free(int_attribs);
1024 return surface;
1025 }
1026
1027 static void *
1028 _fixupNativePixmap(_EGLDisplay *disp, void *native_pixmap)
1029 {
1030 #ifdef HAVE_X11_PLATFORM
1031 /* The `native_pixmap` parameter for the X11 platform differs between
1032 * eglCreatePixmapSurface() and eglCreatePlatformPixmapSurfaceEXT(). In
1033 * eglCreatePixmapSurface(), the type of `native_pixmap` is an Xlib
1034 * `Pixmap`. In eglCreatePlatformPixmapSurfaceEXT(), the type is
1035 * `Pixmap*`. Convert `Pixmap*` to `Pixmap` because that's what
1036 * dri2_x11_create_pixmap_surface() expects.
1037 */
1038 if (disp && disp->Platform == _EGL_PLATFORM_X11 && native_pixmap != NULL)
1039 return (void *)(* (Pixmap*) native_pixmap);
1040 #endif
1041 return native_pixmap;
1042 }
1043
1044 static EGLSurface
1045 _eglCreatePixmapSurfaceCommon(_EGLDisplay *disp, EGLConfig config,
1046 void *native_pixmap, const EGLint *attrib_list)
1047 {
1048 _EGLConfig *conf = _eglLookupConfig(config, disp);
1049 _EGLDriver *drv;
1050 _EGLSurface *surf;
1051 EGLSurface ret;
1052
1053 #if HAVE_SURFACELESS_PLATFORM
1054 if (disp && disp->Platform == _EGL_PLATFORM_SURFACELESS) {
1055 /* From the EGL_MESA_platform_surfaceless spec (v1):
1056 *
1057 * [Like eglCreatePlatformWindowSurface,] eglCreatePlatformPixmapSurface
1058 * also fails when called with a <display> that belongs to the
1059 * surfaceless platform. It returns EGL_NO_SURFACE and generates
1060 * EGL_BAD_NATIVE_PIXMAP.
1061 *
1062 * This check must occur before checking the EGLConfig, which emits
1063 * EGL_BAD_CONFIG.
1064 */
1065 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1066 }
1067 #endif
1068
1069 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1070
1071 if ((conf->SurfaceType & EGL_PIXMAP_BIT) == 0)
1072 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
1073
1074 if (native_pixmap == NULL)
1075 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1076
1077 if (_eglNativeSurfaceAlreadyUsed(disp, native_pixmap))
1078 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1079
1080 surf = drv->API.CreatePixmapSurface(drv, disp, conf, native_pixmap,
1081 attrib_list);
1082 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1083
1084 RETURN_EGL_EVAL(disp, ret);
1085 }
1086
1087
1088 EGLSurface EGLAPIENTRY
1089 eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
1090 EGLNativePixmapType pixmap, const EGLint *attrib_list)
1091 {
1092 _EGLDisplay *disp = _eglLockDisplay(dpy);
1093
1094 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1095 STATIC_ASSERT(sizeof(void*) == sizeof(pixmap));
1096 return _eglCreatePixmapSurfaceCommon(disp, config, (void*) pixmap,
1097 attrib_list);
1098 }
1099
1100 static EGLSurface EGLAPIENTRY
1101 eglCreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config,
1102 void *native_pixmap,
1103 const EGLint *attrib_list)
1104 {
1105 _EGLDisplay *disp = _eglLockDisplay(dpy);
1106
1107 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1108 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1109 return _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1110 attrib_list);
1111 }
1112
1113
1114 EGLSurface EGLAPIENTRY
1115 eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config,
1116 void *native_pixmap,
1117 const EGLAttrib *attrib_list)
1118 {
1119 _EGLDisplay *disp = _eglLockDisplay(dpy);
1120 EGLSurface surface;
1121 EGLint *int_attribs;
1122
1123 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1124
1125 int_attribs = _eglConvertAttribsToInt(attrib_list);
1126 if (attrib_list && !int_attribs)
1127 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_SURFACE);
1128
1129 native_pixmap = _fixupNativePixmap(disp, native_pixmap);
1130 surface = _eglCreatePixmapSurfaceCommon(disp, config, native_pixmap,
1131 int_attribs);
1132 free(int_attribs);
1133 return surface;
1134 }
1135
1136
1137 EGLSurface EGLAPIENTRY
1138 eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
1139 const EGLint *attrib_list)
1140 {
1141 _EGLDisplay *disp = _eglLockDisplay(dpy);
1142 _EGLConfig *conf = _eglLookupConfig(config, disp);
1143 _EGLDriver *drv;
1144 _EGLSurface *surf;
1145 EGLSurface ret;
1146
1147 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1148 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1149
1150 if ((conf->SurfaceType & EGL_PBUFFER_BIT) == 0)
1151 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SURFACE);
1152
1153 surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
1154 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1155
1156 RETURN_EGL_EVAL(disp, ret);
1157 }
1158
1159
1160 EGLBoolean EGLAPIENTRY
1161 eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1162 {
1163 _EGLDisplay *disp = _eglLockDisplay(dpy);
1164 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1165 _EGLDriver *drv;
1166 EGLBoolean ret;
1167
1168 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1169 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1170 _eglUnlinkSurface(surf);
1171 ret = drv->API.DestroySurface(drv, disp, surf);
1172
1173 RETURN_EGL_EVAL(disp, ret);
1174 }
1175
1176 EGLBoolean EGLAPIENTRY
1177 eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
1178 EGLint attribute, EGLint *value)
1179 {
1180 _EGLDisplay *disp = _eglLockDisplay(dpy);
1181 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1182 _EGLDriver *drv;
1183 EGLBoolean ret;
1184
1185 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1186 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1187 ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
1188
1189 RETURN_EGL_EVAL(disp, ret);
1190 }
1191
1192 EGLBoolean EGLAPIENTRY
1193 eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
1194 EGLint attribute, EGLint value)
1195 {
1196 _EGLDisplay *disp = _eglLockDisplay(dpy);
1197 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1198 _EGLDriver *drv;
1199 EGLBoolean ret;
1200
1201 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1202 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1203 ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
1204
1205 RETURN_EGL_EVAL(disp, ret);
1206 }
1207
1208
1209 EGLBoolean EGLAPIENTRY
1210 eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1211 {
1212 _EGLDisplay *disp = _eglLockDisplay(dpy);
1213 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1214 _EGLDriver *drv;
1215 EGLBoolean ret;
1216
1217 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1218 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1219 ret = drv->API.BindTexImage(drv, disp, surf, buffer);
1220
1221 RETURN_EGL_EVAL(disp, ret);
1222 }
1223
1224
1225 EGLBoolean EGLAPIENTRY
1226 eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1227 {
1228 _EGLDisplay *disp = _eglLockDisplay(dpy);
1229 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1230 _EGLDriver *drv;
1231 EGLBoolean ret;
1232
1233 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1234 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1235 ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
1236
1237 RETURN_EGL_EVAL(disp, ret);
1238 }
1239
1240
1241 EGLBoolean EGLAPIENTRY
1242 eglSwapInterval(EGLDisplay dpy, EGLint interval)
1243 {
1244 _EGLDisplay *disp = _eglLockDisplay(dpy);
1245 _EGLContext *ctx = _eglGetCurrentContext();
1246 _EGLSurface *surf = ctx ? ctx->DrawSurface : NULL;
1247 _EGLDriver *drv;
1248 EGLBoolean ret;
1249
1250 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1251 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1252
1253 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1254 ctx->Resource.Display != disp)
1255 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
1256
1257 if (_eglGetSurfaceHandle(surf) == EGL_NO_SURFACE)
1258 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1259
1260 if (surf->Type != EGL_WINDOW_BIT)
1261 RETURN_EGL_EVAL(disp, EGL_TRUE);
1262
1263 interval = CLAMP(interval,
1264 surf->Config->MinSwapInterval,
1265 surf->Config->MaxSwapInterval);
1266
1267 if (surf->SwapInterval != interval)
1268 ret = drv->API.SwapInterval(drv, disp, surf, interval);
1269 else
1270 ret = EGL_TRUE;
1271
1272 if (ret)
1273 surf->SwapInterval = interval;
1274
1275 RETURN_EGL_EVAL(disp, ret);
1276 }
1277
1278
1279 EGLBoolean EGLAPIENTRY
1280 eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1281 {
1282 _EGLContext *ctx = _eglGetCurrentContext();
1283 _EGLDisplay *disp = _eglLockDisplay(dpy);
1284 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1285 _EGLDriver *drv;
1286 EGLBoolean ret;
1287
1288 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1289 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1290
1291 /* surface must be bound to current context in EGL 1.4 */
1292 #ifndef _EGL_BUILT_IN_DRIVER_HAIKU
1293 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1294 surf != ctx->DrawSurface)
1295 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1296 #endif
1297
1298 if (surf->Type != EGL_WINDOW_BIT)
1299 RETURN_EGL_EVAL(disp, EGL_TRUE);
1300
1301 /* From the EGL 1.5 spec:
1302 *
1303 * If eglSwapBuffers is called and the native window associated with
1304 * surface is no longer valid, an EGL_BAD_NATIVE_WINDOW error is
1305 * generated.
1306 */
1307 if (surf->Lost)
1308 RETURN_EGL_ERROR(disp, EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
1309
1310 ret = drv->API.SwapBuffers(drv, disp, surf);
1311
1312 /* EGL_KHR_partial_update
1313 * Frame boundary successfully reached,
1314 * reset damage region and reset BufferAgeRead
1315 */
1316 if (ret) {
1317 surf->SetDamageRegionCalled = EGL_FALSE;
1318 surf->BufferAgeRead = EGL_FALSE;
1319 }
1320
1321 RETURN_EGL_EVAL(disp, ret);
1322 }
1323
1324
1325 static EGLBoolean
1326 _eglSwapBuffersWithDamageCommon(_EGLDisplay *disp, _EGLSurface *surf,
1327 EGLint *rects, EGLint n_rects)
1328 {
1329 _EGLContext *ctx = _eglGetCurrentContext();
1330 _EGLDriver *drv;
1331 EGLBoolean ret;
1332
1333 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1334
1335 /* surface must be bound to current context in EGL 1.4 */
1336 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1337 surf != ctx->DrawSurface)
1338 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
1339
1340 if (surf->Type != EGL_WINDOW_BIT)
1341 RETURN_EGL_EVAL(disp, EGL_TRUE);
1342
1343 if ((n_rects > 0 && rects == NULL) || n_rects < 0)
1344 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1345
1346 ret = drv->API.SwapBuffersWithDamageEXT(drv, disp, surf, rects, n_rects);
1347
1348 /* EGL_KHR_partial_update
1349 * Frame boundary successfully reached,
1350 * reset damage region and reset BufferAgeRead
1351 */
1352 if (ret) {
1353 surf->SetDamageRegionCalled = EGL_FALSE;
1354 surf->BufferAgeRead = EGL_FALSE;
1355 }
1356
1357 RETURN_EGL_EVAL(disp, ret);
1358 }
1359
1360 static EGLBoolean EGLAPIENTRY
1361 eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
1362 EGLint *rects, EGLint n_rects)
1363 {
1364 _EGLDisplay *disp = _eglLockDisplay(dpy);
1365 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1366 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1367 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1368 }
1369
1370 static EGLBoolean EGLAPIENTRY
1371 eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
1372 EGLint *rects, EGLint n_rects)
1373 {
1374 _EGLDisplay *disp = _eglLockDisplay(dpy);
1375 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1376 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1377 return _eglSwapBuffersWithDamageCommon(disp, surf, rects, n_rects);
1378 }
1379
1380 /**
1381 * Clamp the rectangles so that they lie within the surface.
1382 */
1383
1384 static void
1385 _eglSetDamageRegionKHRClampRects(_EGLDisplay* disp, _EGLSurface* surf,
1386 EGLint *rects, EGLint n_rects)
1387 {
1388 EGLint i;
1389 EGLint surf_height = surf->Height;
1390 EGLint surf_width = surf->Width;
1391
1392 for (i = 0; i < (4 * n_rects); i += 4) {
1393 EGLint x1, y1, x2, y2;
1394 x1 = rects[i];
1395 y1 = rects[i + 1];
1396 x2 = rects[i + 2] + x1;
1397 y2 = rects[i + 3] + y1;
1398
1399 rects[i] = CLAMP(x1, 0, surf_width);
1400 rects[i + 1] = CLAMP(y1, 0, surf_height);
1401 rects[i + 2] = CLAMP(x2, 0, surf_width) - rects[i];
1402 rects[i + 3] = CLAMP(y2, 0, surf_height) - rects[i + 1];
1403 }
1404 }
1405
1406 static EGLBoolean EGLAPIENTRY
1407 eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1408 EGLint *rects, EGLint n_rects)
1409 {
1410 _EGLDisplay *disp = _eglLockDisplay(dpy);
1411 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1412 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1413 _EGLContext *ctx = _eglGetCurrentContext();
1414 _EGLDriver *drv;
1415 EGLBoolean ret;
1416 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1417
1418 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1419 surf->Type != EGL_WINDOW_BIT ||
1420 ctx->DrawSurface != surf ||
1421 surf->SwapBehavior != EGL_BUFFER_DESTROYED)
1422 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
1423
1424 /* If the damage region is already set or
1425 * buffer age is not queried between
1426 * frame boundaries, throw bad access error
1427 */
1428
1429 if (surf->SetDamageRegionCalled || !surf->BufferAgeRead)
1430 RETURN_EGL_ERROR(disp, EGL_BAD_ACCESS, EGL_FALSE);
1431
1432 _eglSetDamageRegionKHRClampRects(disp, surf, rects, n_rects);
1433 ret = drv->API.SetDamageRegion(drv, disp, surf, rects, n_rects);
1434
1435 if (ret)
1436 surf->SetDamageRegionCalled = EGL_TRUE;
1437
1438 RETURN_EGL_EVAL(disp, ret);
1439 }
1440
1441 EGLBoolean EGLAPIENTRY
1442 eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1443 {
1444 _EGLDisplay *disp = _eglLockDisplay(dpy);
1445 _EGLSurface *surf = _eglLookupSurface(surface, disp);
1446 _EGLDriver *drv;
1447 EGLBoolean ret;
1448 void *native_pixmap_ptr;
1449
1450 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
1451 STATIC_ASSERT(sizeof(void*) == sizeof(target));
1452 native_pixmap_ptr = (void*) target;
1453
1454 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
1455 ret = drv->API.CopyBuffers(drv, disp, surf, native_pixmap_ptr);
1456
1457 RETURN_EGL_EVAL(disp, ret);
1458 }
1459
1460
1461 static EGLBoolean
1462 _eglWaitClientCommon(void)
1463 {
1464 _EGLContext *ctx = _eglGetCurrentContext();
1465 _EGLDisplay *disp;
1466 _EGLDriver *drv;
1467 EGLBoolean ret;
1468
1469 if (!ctx)
1470 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1471
1472 disp = ctx->Resource.Display;
1473 mtx_lock(&disp->Mutex);
1474
1475 /* let bad current context imply bad current surface */
1476 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1477 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1478 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1479
1480 /* a valid current context implies an initialized current display */
1481 assert(disp->Initialized);
1482 drv = disp->Driver;
1483 ret = drv->API.WaitClient(drv, disp, ctx);
1484
1485 RETURN_EGL_EVAL(disp, ret);
1486 }
1487
1488 EGLBoolean EGLAPIENTRY
1489 eglWaitClient(void)
1490 {
1491 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1492 return _eglWaitClientCommon();
1493 }
1494
1495 EGLBoolean EGLAPIENTRY
1496 eglWaitGL(void)
1497 {
1498 /* Since we only support OpenGL and GLES, eglWaitGL is equivalent to eglWaitClient. */
1499 _EGL_FUNC_START(NULL, EGL_OBJECT_CONTEXT_KHR, _eglGetCurrentContext(), EGL_FALSE);
1500 return _eglWaitClientCommon();
1501 }
1502
1503
1504 EGLBoolean EGLAPIENTRY
1505 eglWaitNative(EGLint engine)
1506 {
1507 _EGLContext *ctx = _eglGetCurrentContext();
1508 _EGLDisplay *disp;
1509 _EGLDriver *drv;
1510 EGLBoolean ret;
1511
1512 if (!ctx)
1513 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1514
1515 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1516
1517 disp = ctx->Resource.Display;
1518 mtx_lock(&disp->Mutex);
1519
1520 /* let bad current context imply bad current surface */
1521 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
1522 _eglGetSurfaceHandle(ctx->DrawSurface) == EGL_NO_SURFACE)
1523 RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
1524
1525 /* a valid current context implies an initialized current display */
1526 assert(disp->Initialized);
1527 drv = disp->Driver;
1528 ret = drv->API.WaitNative(drv, disp, engine);
1529
1530 RETURN_EGL_EVAL(disp, ret);
1531 }
1532
1533
1534 EGLDisplay EGLAPIENTRY
1535 eglGetCurrentDisplay(void)
1536 {
1537 _EGLContext *ctx = _eglGetCurrentContext();
1538 EGLDisplay ret;
1539
1540 ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
1541
1542 RETURN_EGL_SUCCESS(NULL, ret);
1543 }
1544
1545
1546 EGLContext EGLAPIENTRY
1547 eglGetCurrentContext(void)
1548 {
1549 _EGLContext *ctx = _eglGetCurrentContext();
1550 EGLContext ret;
1551
1552 ret = _eglGetContextHandle(ctx);
1553
1554 RETURN_EGL_SUCCESS(NULL, ret);
1555 }
1556
1557
1558 EGLSurface EGLAPIENTRY
1559 eglGetCurrentSurface(EGLint readdraw)
1560 {
1561 _EGLContext *ctx = _eglGetCurrentContext();
1562 EGLint err = EGL_SUCCESS;
1563 _EGLSurface *surf;
1564 EGLSurface ret;
1565
1566 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_NO_SURFACE);
1567
1568 if (!ctx)
1569 RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
1570
1571 switch (readdraw) {
1572 case EGL_DRAW:
1573 surf = ctx->DrawSurface;
1574 break;
1575 case EGL_READ:
1576 surf = ctx->ReadSurface;
1577 break;
1578 default:
1579 surf = NULL;
1580 err = EGL_BAD_PARAMETER;
1581 break;
1582 }
1583
1584 ret = _eglGetSurfaceHandle(surf);
1585
1586 RETURN_EGL_ERROR(NULL, err, ret);
1587 }
1588
1589
1590 EGLint EGLAPIENTRY
1591 eglGetError(void)
1592 {
1593 _EGLThreadInfo *t = _eglGetCurrentThread();
1594 EGLint e = t->LastError;
1595 if (!_eglIsCurrentThreadDummy())
1596 t->LastError = EGL_SUCCESS;
1597 return e;
1598 }
1599
1600
1601 /**
1602 ** EGL 1.2
1603 **/
1604
1605 /**
1606 * Specify the client API to use for subsequent calls including:
1607 * eglCreateContext()
1608 * eglGetCurrentContext()
1609 * eglGetCurrentDisplay()
1610 * eglGetCurrentSurface()
1611 * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
1612 * eglWaitClient()
1613 * eglWaitNative()
1614 * See section 3.7 "Rendering Context" in the EGL specification for details.
1615 */
1616 EGLBoolean EGLAPIENTRY
1617 eglBindAPI(EGLenum api)
1618 {
1619 _EGLThreadInfo *t;
1620
1621 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1622
1623 t = _eglGetCurrentThread();
1624 if (_eglIsCurrentThreadDummy())
1625 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
1626
1627 if (!_eglIsApiValid(api))
1628 RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
1629
1630 t->CurrentAPI = api;
1631
1632 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1633 }
1634
1635
1636 /**
1637 * Return the last value set with eglBindAPI().
1638 */
1639 EGLenum EGLAPIENTRY
1640 eglQueryAPI(void)
1641 {
1642 _EGLThreadInfo *t = _eglGetCurrentThread();
1643 EGLenum ret;
1644
1645 /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
1646 ret = t->CurrentAPI;
1647
1648 RETURN_EGL_SUCCESS(NULL, ret);
1649 }
1650
1651
1652 EGLSurface EGLAPIENTRY
1653 eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
1654 EGLClientBuffer buffer, EGLConfig config,
1655 const EGLint *attrib_list)
1656 {
1657 _EGLDisplay *disp = _eglLockDisplay(dpy);
1658 _EGLConfig *conf = _eglLookupConfig(config, disp);
1659 _EGLDriver *drv;
1660 _EGLSurface *surf;
1661 EGLSurface ret;
1662
1663 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_SURFACE);
1664
1665 _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
1666
1667 surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
1668 conf, attrib_list);
1669 ret = (surf) ? _eglLinkSurface(surf) : EGL_NO_SURFACE;
1670
1671 RETURN_EGL_EVAL(disp, ret);
1672 }
1673
1674
1675 EGLBoolean EGLAPIENTRY
1676 eglReleaseThread(void)
1677 {
1678 /* unbind current contexts */
1679 if (!_eglIsCurrentThreadDummy()) {
1680 _EGLThreadInfo *t = _eglGetCurrentThread();
1681 _EGLContext *ctx = t->CurrentContext;
1682
1683 _EGL_FUNC_START(NULL, EGL_OBJECT_THREAD_KHR, NULL, EGL_FALSE);
1684
1685 if (ctx) {
1686 _EGLDisplay *disp = ctx->Resource.Display;
1687 _EGLDriver *drv;
1688
1689 mtx_lock(&disp->Mutex);
1690 drv = disp->Driver;
1691 (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
1692 mtx_unlock(&disp->Mutex);
1693 }
1694 }
1695
1696 _eglDestroyCurrentThread();
1697
1698 RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
1699 }
1700
1701
1702 static EGLImage
1703 _eglCreateImageCommon(_EGLDisplay *disp, EGLContext ctx, EGLenum target,
1704 EGLClientBuffer buffer, const EGLint *attr_list)
1705 {
1706 _EGLContext *context = _eglLookupContext(ctx, disp);
1707 _EGLDriver *drv;
1708 _EGLImage *img;
1709 EGLImage ret;
1710
1711 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
1712 if (!disp->Extensions.KHR_image_base)
1713 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
1714 if (!context && ctx != EGL_NO_CONTEXT)
1715 RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1716 /* "If <target> is EGL_LINUX_DMA_BUF_EXT, <dpy> must be a valid display,
1717 * <ctx> must be EGL_NO_CONTEXT..."
1718 */
1719 if (ctx != EGL_NO_CONTEXT && target == EGL_LINUX_DMA_BUF_EXT)
1720 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1721
1722 img = drv->API.CreateImageKHR(drv, disp, context, target,
1723 buffer, attr_list);
1724 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
1725
1726 RETURN_EGL_EVAL(disp, ret);
1727 }
1728
1729 static EGLImage EGLAPIENTRY
1730 eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1731 EGLClientBuffer buffer, const EGLint *attr_list)
1732 {
1733 _EGLDisplay *disp = _eglLockDisplay(dpy);
1734 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1735 return _eglCreateImageCommon(disp, ctx, target, buffer, attr_list);
1736 }
1737
1738
1739 EGLImage EGLAPIENTRY
1740 eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1741 EGLClientBuffer buffer, const EGLAttrib *attr_list)
1742 {
1743 _EGLDisplay *disp = _eglLockDisplay(dpy);
1744 EGLImage image;
1745 EGLint *int_attribs;
1746
1747 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_NO_IMAGE_KHR);
1748
1749 int_attribs = _eglConvertAttribsToInt(attr_list);
1750 if (attr_list && !int_attribs)
1751 RETURN_EGL_ERROR(disp, EGL_BAD_ALLOC, EGL_NO_IMAGE);
1752
1753 image = _eglCreateImageCommon(disp, ctx, target, buffer, int_attribs);
1754 free(int_attribs);
1755 return image;
1756 }
1757
1758
1759 static EGLBoolean
1760 _eglDestroyImageCommon(_EGLDisplay *disp, _EGLImage *img)
1761 {
1762 _EGLDriver *drv;
1763 EGLBoolean ret;
1764
1765 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
1766 if (!disp->Extensions.KHR_image_base)
1767 RETURN_EGL_EVAL(disp, EGL_FALSE);
1768 if (!img)
1769 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
1770
1771 _eglUnlinkImage(img);
1772 ret = drv->API.DestroyImageKHR(drv, disp, img);
1773
1774 RETURN_EGL_EVAL(disp, ret);
1775 }
1776
1777 EGLBoolean EGLAPIENTRY
1778 eglDestroyImage(EGLDisplay dpy, EGLImage image)
1779 {
1780 _EGLDisplay *disp = _eglLockDisplay(dpy);
1781 _EGLImage *img = _eglLookupImage(image, disp);
1782 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1783 return _eglDestroyImageCommon(disp, img);
1784 }
1785
1786 static EGLBoolean EGLAPIENTRY
1787 eglDestroyImageKHR(EGLDisplay dpy, EGLImage image)
1788 {
1789 _EGLDisplay *disp = _eglLockDisplay(dpy);
1790 _EGLImage *img = _eglLookupImage(image, disp);
1791 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
1792 return _eglDestroyImageCommon(disp, img);
1793 }
1794
1795
1796 static EGLSync
1797 _eglCreateSync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list,
1798 EGLBoolean orig_is_EGLAttrib,
1799 EGLenum invalid_type_error)
1800 {
1801 _EGLContext *ctx = _eglGetCurrentContext();
1802 _EGLDriver *drv;
1803 _EGLSync *sync;
1804 EGLSync ret;
1805
1806 _EGL_CHECK_DISPLAY(disp, EGL_NO_SYNC_KHR, drv);
1807
1808 if (!disp->Extensions.KHR_cl_event2 && orig_is_EGLAttrib) {
1809 /* There exist two EGLAttrib variants of eglCreateSync*:
1810 * eglCreateSync64KHR which requires EGL_KHR_cl_event2, and eglCreateSync
1811 * which requires EGL 1.5. Here we use the presence of EGL_KHR_cl_event2
1812 * support as a proxy for EGL 1.5 support, even though that's not
1813 * entirely correct (though _eglComputeVersion does the same).
1814 *
1815 * The EGL spec provides no guidance on how to handle unsupported
1816 * functions. EGL_BAD_MATCH seems reasonable.
1817 */
1818 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1819 }
1820
1821 /* If type is EGL_SYNC_FENCE and no context is current for the bound API
1822 * (i.e., eglGetCurrentContext returns EGL_NO_CONTEXT ), an EGL_BAD_MATCH
1823 * error is generated.
1824 */
1825 if (!ctx &&
1826 (type == EGL_SYNC_FENCE_KHR || type == EGL_SYNC_NATIVE_FENCE_ANDROID))
1827 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1828
1829 /* return an error if the client API doesn't support GL_OES_EGL_sync */
1830 if (ctx && (ctx->Resource.Display != disp ||
1831 ctx->ClientAPI != EGL_OPENGL_ES_API))
1832 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1833
1834 switch (type) {
1835 case EGL_SYNC_FENCE_KHR:
1836 if (!disp->Extensions.KHR_fence_sync)
1837 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1838 break;
1839 case EGL_SYNC_REUSABLE_KHR:
1840 if (!disp->Extensions.KHR_reusable_sync)
1841 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1842 break;
1843 case EGL_SYNC_CL_EVENT_KHR:
1844 if (!disp->Extensions.KHR_cl_event2)
1845 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1846 break;
1847 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1848 if (!disp->Extensions.ANDROID_native_fence_sync)
1849 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1850 break;
1851 default:
1852 RETURN_EGL_ERROR(disp, invalid_type_error, EGL_NO_SYNC_KHR);
1853 }
1854
1855 sync = drv->API.CreateSyncKHR(drv, disp, type, attrib_list);
1856 ret = (sync) ? _eglLinkSync(sync) : EGL_NO_SYNC_KHR;
1857
1858 RETURN_EGL_EVAL(disp, ret);
1859 }
1860
1861
1862 static EGLSync EGLAPIENTRY
1863 eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *int_list)
1864 {
1865 _EGLDisplay *disp = _eglLockDisplay(dpy);
1866 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1867
1868 EGLSync sync;
1869 EGLAttrib *attrib_list;
1870 EGLint err;
1871
1872 if (sizeof(int_list[0]) == sizeof(attrib_list[0])) {
1873 attrib_list = (EGLAttrib *) int_list;
1874 } else {
1875 err = _eglConvertIntsToAttribs(int_list, &attrib_list);
1876 if (err != EGL_SUCCESS)
1877 RETURN_EGL_ERROR(disp, err, EGL_NO_SYNC);
1878 }
1879
1880 sync = _eglCreateSync(disp, type, attrib_list, EGL_FALSE,
1881 EGL_BAD_ATTRIBUTE);
1882
1883 if (sizeof(int_list[0]) != sizeof(attrib_list[0]))
1884 free(attrib_list);
1885
1886 /* Don't double-unlock the display. _eglCreateSync already unlocked it. */
1887 return sync;
1888 }
1889
1890
1891 static EGLSync EGLAPIENTRY
1892 eglCreateSync64KHR(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1893 {
1894 _EGLDisplay *disp = _eglLockDisplay(dpy);
1895 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1896 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1897 EGL_BAD_ATTRIBUTE);
1898 }
1899
1900
1901 EGLSync EGLAPIENTRY
1902 eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)
1903 {
1904 _EGLDisplay *disp = _eglLockDisplay(dpy);
1905 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
1906 return _eglCreateSync(disp, type, attrib_list, EGL_TRUE,
1907 EGL_BAD_PARAMETER);
1908 }
1909
1910
1911 static EGLBoolean
1912 _eglDestroySync(_EGLDisplay *disp, _EGLSync *s)
1913 {
1914 _EGLDriver *drv;
1915 EGLBoolean ret;
1916
1917 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1918 assert(disp->Extensions.KHR_reusable_sync ||
1919 disp->Extensions.KHR_fence_sync ||
1920 disp->Extensions.ANDROID_native_fence_sync);
1921
1922 _eglUnlinkSync(s);
1923 ret = drv->API.DestroySyncKHR(drv, disp, s);
1924
1925 RETURN_EGL_EVAL(disp, ret);
1926 }
1927
1928 EGLBoolean EGLAPIENTRY
1929 eglDestroySync(EGLDisplay dpy, EGLSync sync)
1930 {
1931 _EGLDisplay *disp = _eglLockDisplay(dpy);
1932 _EGLSync *s = _eglLookupSync(sync, disp);
1933 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1934 return _eglDestroySync(disp, s);
1935 }
1936
1937 static EGLBoolean EGLAPIENTRY
1938 eglDestroySyncKHR(EGLDisplay dpy, EGLSync sync)
1939 {
1940 _EGLDisplay *disp = _eglLockDisplay(dpy);
1941 _EGLSync *s = _eglLookupSync(sync, disp);
1942 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1943 return _eglDestroySync(disp, s);
1944 }
1945
1946
1947 static EGLint
1948 _eglClientWaitSyncCommon(_EGLDisplay *disp, EGLDisplay dpy,
1949 _EGLSync *s, EGLint flags, EGLTime timeout)
1950 {
1951 _EGLDriver *drv;
1952 EGLint ret;
1953
1954 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
1955 assert(disp->Extensions.KHR_reusable_sync ||
1956 disp->Extensions.KHR_fence_sync ||
1957 disp->Extensions.ANDROID_native_fence_sync);
1958
1959 if (s->SyncStatus == EGL_SIGNALED_KHR)
1960 RETURN_EGL_EVAL(disp, EGL_CONDITION_SATISFIED_KHR);
1961
1962 /* if sync type is EGL_SYNC_REUSABLE_KHR, dpy should be
1963 * unlocked here to allow other threads also to be able to
1964 * go into waiting state.
1965 */
1966
1967 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1968 _eglUnlockDisplay(dpy);
1969
1970 ret = drv->API.ClientWaitSyncKHR(drv, disp, s, flags, timeout);
1971
1972 /*
1973 * 'disp' is already unlocked for reusable sync type,
1974 * so passing 'NULL' to bypass unlocking display.
1975 */
1976 if (s->Type == EGL_SYNC_REUSABLE_KHR)
1977 RETURN_EGL_EVAL(NULL, ret);
1978 else
1979 RETURN_EGL_EVAL(disp, ret);
1980 }
1981
1982 EGLint EGLAPIENTRY
1983 eglClientWaitSync(EGLDisplay dpy, EGLSync sync,
1984 EGLint flags, EGLTime timeout)
1985 {
1986 _EGLDisplay *disp = _eglLockDisplay(dpy);
1987 _EGLSync *s = _eglLookupSync(sync, disp);
1988 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1989 return _eglClientWaitSyncCommon(disp, dpy, s, flags, timeout);
1990 }
1991
1992 static EGLint EGLAPIENTRY
1993 eglClientWaitSyncKHR(EGLDisplay dpy, EGLSync sync,
1994 EGLint flags, EGLTime timeout)
1995 {
1996 _EGLDisplay *disp = _eglLockDisplay(dpy);
1997 _EGLSync *s = _eglLookupSync(sync, disp);
1998 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
1999 return _eglClientWaitSyncCommon(disp, dpy, s, flags, timeout);
2000 }
2001
2002
2003 static EGLint
2004 _eglWaitSyncCommon(_EGLDisplay *disp, _EGLSync *s, EGLint flags)
2005 {
2006 _EGLContext *ctx = _eglGetCurrentContext();
2007 _EGLDriver *drv;
2008 EGLint ret;
2009
2010 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2011 assert(disp->Extensions.KHR_wait_sync);
2012
2013 /* return an error if the client API doesn't support GL_OES_EGL_sync */
2014 if (ctx == EGL_NO_CONTEXT || ctx->ClientAPI != EGL_OPENGL_ES_API)
2015 RETURN_EGL_ERROR(disp, EGL_BAD_MATCH, EGL_FALSE);
2016
2017 /* the API doesn't allow any flags yet */
2018 if (flags != 0)
2019 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2020
2021 ret = drv->API.WaitSyncKHR(drv, disp, s);
2022
2023 RETURN_EGL_EVAL(disp, ret);
2024 }
2025
2026 static EGLint EGLAPIENTRY
2027 eglWaitSyncKHR(EGLDisplay dpy, EGLSync sync, EGLint flags)
2028 {
2029 _EGLDisplay *disp = _eglLockDisplay(dpy);
2030 _EGLSync *s = _eglLookupSync(sync, disp);
2031 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2032 return _eglWaitSyncCommon(disp, s, flags);
2033 }
2034
2035
2036 EGLBoolean EGLAPIENTRY
2037 eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags)
2038 {
2039 /* The KHR version returns EGLint, while the core version returns
2040 * EGLBoolean. In both cases, the return values can only be EGL_FALSE and
2041 * EGL_TRUE.
2042 */
2043 _EGLDisplay *disp = _eglLockDisplay(dpy);
2044 _EGLSync *s = _eglLookupSync(sync, disp);
2045 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2046 return _eglWaitSyncCommon(disp, s, flags);
2047 }
2048
2049
2050 static EGLBoolean EGLAPIENTRY
2051 eglSignalSyncKHR(EGLDisplay dpy, EGLSync sync, EGLenum mode)
2052 {
2053 _EGLDisplay *disp = _eglLockDisplay(dpy);
2054 _EGLSync *s = _eglLookupSync(sync, disp);
2055 _EGLDriver *drv;
2056 EGLBoolean ret;
2057
2058 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2059
2060 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2061 assert(disp->Extensions.KHR_reusable_sync);
2062 ret = drv->API.SignalSyncKHR(drv, disp, s, mode);
2063
2064 RETURN_EGL_EVAL(disp, ret);
2065 }
2066
2067
2068 static EGLBoolean
2069 _eglGetSyncAttribCommon(_EGLDisplay *disp, _EGLSync *s, EGLint attribute, EGLAttrib *value)
2070 {
2071 _EGLDriver *drv;
2072 EGLBoolean ret;
2073
2074 _EGL_CHECK_SYNC(disp, s, EGL_FALSE, drv);
2075 assert(disp->Extensions.KHR_reusable_sync ||
2076 disp->Extensions.KHR_fence_sync ||
2077 disp->Extensions.ANDROID_native_fence_sync);
2078 ret = drv->API.GetSyncAttrib(drv, disp, s, attribute, value);
2079
2080 RETURN_EGL_EVAL(disp, ret);
2081 }
2082
2083 EGLBoolean EGLAPIENTRY
2084 eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib *value)
2085 {
2086 _EGLDisplay *disp = _eglLockDisplay(dpy);
2087 _EGLSync *s = _eglLookupSync(sync, disp);
2088 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2089 return _eglGetSyncAttribCommon(disp, s, attribute, value);
2090 }
2091
2092
2093 static EGLBoolean EGLAPIENTRY
2094 eglGetSyncAttribKHR(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLint *value)
2095 {
2096 _EGLDisplay *disp = _eglLockDisplay(dpy);
2097 _EGLSync *s = _eglLookupSync(sync, disp);
2098 EGLAttrib attrib;
2099 EGLBoolean result;
2100
2101 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2102
2103 if (!value)
2104 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2105
2106 attrib = *value;
2107 result = _eglGetSyncAttribCommon(disp, s, attribute, &attrib);
2108
2109 /* The EGL_KHR_fence_sync spec says this about eglGetSyncAttribKHR:
2110 *
2111 * If any error occurs, <*value> is not modified.
2112 */
2113 if (result == EGL_FALSE)
2114 return result;
2115
2116 *value = attrib;
2117 return result;
2118 }
2119
2120 static EGLint EGLAPIENTRY
2121 eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSync sync)
2122 {
2123 _EGLDisplay *disp = _eglLockDisplay(dpy);
2124 _EGLSync *s = _eglLookupSync(sync, disp);
2125 _EGLDriver *drv;
2126 EGLBoolean ret;
2127
2128 _EGL_FUNC_START(disp, EGL_OBJECT_SYNC_KHR, s, EGL_FALSE);
2129
2130 /* the spec doesn't seem to specify what happens if the fence
2131 * type is not EGL_SYNC_NATIVE_FENCE_ANDROID, but this seems
2132 * sensible:
2133 */
2134 if (!(s && (s->Type == EGL_SYNC_NATIVE_FENCE_ANDROID)))
2135 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_NO_NATIVE_FENCE_FD_ANDROID);
2136
2137 _EGL_CHECK_SYNC(disp, s, EGL_NO_NATIVE_FENCE_FD_ANDROID, drv);
2138 assert(disp->Extensions.ANDROID_native_fence_sync);
2139 ret = drv->API.DupNativeFenceFDANDROID(drv, disp, s);
2140
2141 RETURN_EGL_EVAL(disp, ret);
2142 }
2143
2144 static EGLBoolean EGLAPIENTRY
2145 eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
2146 EGLint numRects, const EGLint *rects)
2147 {
2148 _EGLContext *ctx = _eglGetCurrentContext();
2149 _EGLDisplay *disp = _eglLockDisplay(dpy);
2150 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2151 _EGLDriver *drv;
2152 EGLBoolean ret;
2153
2154 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2155
2156 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2157
2158 if (!disp->Extensions.NOK_swap_region)
2159 RETURN_EGL_EVAL(disp, EGL_FALSE);
2160
2161 /* surface must be bound to current context in EGL 1.4 */
2162 if (_eglGetContextHandle(ctx) == EGL_NO_CONTEXT ||
2163 surf != ctx->DrawSurface)
2164 RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
2165
2166 ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
2167
2168 RETURN_EGL_EVAL(disp, ret);
2169 }
2170
2171
2172 static EGLImage EGLAPIENTRY
2173 eglCreateDRMImageMESA(EGLDisplay dpy, const EGLint *attr_list)
2174 {
2175 _EGLDisplay *disp = _eglLockDisplay(dpy);
2176 _EGLDriver *drv;
2177 _EGLImage *img;
2178 EGLImage ret;
2179
2180 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2181
2182 _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
2183 if (!disp->Extensions.MESA_drm_image)
2184 RETURN_EGL_EVAL(disp, EGL_NO_IMAGE_KHR);
2185
2186 img = drv->API.CreateDRMImageMESA(drv, disp, attr_list);
2187 ret = (img) ? _eglLinkImage(img) : EGL_NO_IMAGE_KHR;
2188
2189 RETURN_EGL_EVAL(disp, ret);
2190 }
2191
2192 static EGLBoolean EGLAPIENTRY
2193 eglExportDRMImageMESA(EGLDisplay dpy, EGLImage image,
2194 EGLint *name, EGLint *handle, EGLint *stride)
2195 {
2196 _EGLDisplay *disp = _eglLockDisplay(dpy);
2197 _EGLImage *img = _eglLookupImage(image, disp);
2198 _EGLDriver *drv;
2199 EGLBoolean ret;
2200
2201 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2202
2203 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2204 assert(disp->Extensions.MESA_drm_image);
2205
2206 if (!img)
2207 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2208
2209 ret = drv->API.ExportDRMImageMESA(drv, disp, img, name, handle, stride);
2210
2211 RETURN_EGL_EVAL(disp, ret);
2212 }
2213
2214
2215 struct wl_display;
2216
2217 static EGLBoolean EGLAPIENTRY
2218 eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2219 {
2220 _EGLDisplay *disp = _eglLockDisplay(dpy);
2221 _EGLDriver *drv;
2222 EGLBoolean ret;
2223
2224 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2225
2226 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2227 assert(disp->Extensions.WL_bind_wayland_display);
2228
2229 if (!display)
2230 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2231
2232 ret = drv->API.BindWaylandDisplayWL(drv, disp, display);
2233
2234 RETURN_EGL_EVAL(disp, ret);
2235 }
2236
2237 static EGLBoolean EGLAPIENTRY
2238 eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display)
2239 {
2240 _EGLDisplay *disp = _eglLockDisplay(dpy);
2241 _EGLDriver *drv;
2242 EGLBoolean ret;
2243
2244 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2245
2246 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2247 assert(disp->Extensions.WL_bind_wayland_display);
2248
2249 if (!display)
2250 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2251
2252 ret = drv->API.UnbindWaylandDisplayWL(drv, disp, display);
2253
2254 RETURN_EGL_EVAL(disp, ret);
2255 }
2256
2257 static EGLBoolean EGLAPIENTRY
2258 eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer,
2259 EGLint attribute, EGLint *value)
2260 {
2261 _EGLDisplay *disp = _eglLockDisplay(dpy);
2262 _EGLDriver *drv;
2263 EGLBoolean ret;
2264
2265 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2266
2267 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2268 assert(disp->Extensions.WL_bind_wayland_display);
2269
2270 if (!buffer)
2271 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2272
2273 ret = drv->API.QueryWaylandBufferWL(drv, disp, buffer, attribute, value);
2274
2275 RETURN_EGL_EVAL(disp, ret);
2276 }
2277
2278
2279 static struct wl_buffer * EGLAPIENTRY
2280 eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImage image)
2281 {
2282 _EGLDisplay *disp = _eglLockDisplay(dpy);
2283 _EGLImage *img;
2284 _EGLDriver *drv;
2285 struct wl_buffer *ret;
2286
2287 _EGL_FUNC_START(disp, EGL_OBJECT_DISPLAY_KHR, NULL, EGL_FALSE);
2288
2289 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2290 if (!disp->Extensions.WL_create_wayland_buffer_from_image)
2291 RETURN_EGL_EVAL(disp, NULL);
2292
2293 img = _eglLookupImage(image, disp);
2294
2295 if (!img)
2296 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, NULL);
2297
2298 ret = drv->API.CreateWaylandBufferFromImageWL(drv, disp, img);
2299
2300 RETURN_EGL_EVAL(disp, ret);
2301 }
2302
2303 static EGLBoolean EGLAPIENTRY
2304 eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface,
2305 EGLint x, EGLint y, EGLint width, EGLint height)
2306 {
2307 _EGLDisplay *disp = _eglLockDisplay(dpy);
2308 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2309 _EGLDriver *drv;
2310 EGLBoolean ret;
2311
2312 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2313
2314 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2315
2316 if (!disp->Extensions.NV_post_sub_buffer)
2317 RETURN_EGL_EVAL(disp, EGL_FALSE);
2318
2319 ret = drv->API.PostSubBufferNV(drv, disp, surf, x, y, width, height);
2320
2321 RETURN_EGL_EVAL(disp, ret);
2322 }
2323
2324 static EGLBoolean EGLAPIENTRY
2325 eglGetSyncValuesCHROMIUM(EGLDisplay dpy, EGLSurface surface,
2326 EGLuint64KHR *ust, EGLuint64KHR *msc,
2327 EGLuint64KHR *sbc)
2328 {
2329 _EGLDisplay *disp = _eglLockDisplay(dpy);
2330 _EGLSurface *surf = _eglLookupSurface(surface, disp);
2331 _EGLDriver *drv;
2332 EGLBoolean ret;
2333
2334 _EGL_FUNC_START(disp, EGL_OBJECT_SURFACE_KHR, surf, EGL_FALSE);
2335
2336 _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
2337 if (!disp->Extensions.CHROMIUM_sync_control)
2338 RETURN_EGL_EVAL(disp, EGL_FALSE);
2339
2340 if (!ust || !msc || !sbc)
2341 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2342
2343 ret = drv->API.GetSyncValuesCHROMIUM(disp, surf, ust, msc, sbc);
2344
2345 RETURN_EGL_EVAL(disp, ret);
2346 }
2347
2348 static EGLBoolean EGLAPIENTRY
2349 eglExportDMABUFImageQueryMESA(EGLDisplay dpy, EGLImage image,
2350 EGLint *fourcc, EGLint *nplanes,
2351 EGLuint64KHR *modifiers)
2352 {
2353 _EGLDisplay *disp = _eglLockDisplay(dpy);
2354 _EGLImage *img = _eglLookupImage(image, disp);
2355 _EGLDriver *drv;
2356 EGLBoolean ret;
2357
2358 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2359
2360 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2361 assert(disp->Extensions.MESA_image_dma_buf_export);
2362
2363 if (!img)
2364 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2365
2366 ret = drv->API.ExportDMABUFImageQueryMESA(drv, disp, img, fourcc, nplanes,
2367 modifiers);
2368
2369 RETURN_EGL_EVAL(disp, ret);
2370 }
2371
2372 static EGLBoolean EGLAPIENTRY
2373 eglExportDMABUFImageMESA(EGLDisplay dpy, EGLImage image,
2374 int *fds, EGLint *strides, EGLint *offsets)
2375 {
2376 _EGLDisplay *disp = _eglLockDisplay(dpy);
2377 _EGLImage *img = _eglLookupImage(image, disp);
2378 _EGLDriver *drv;
2379 EGLBoolean ret;
2380
2381 _EGL_FUNC_START(disp, EGL_OBJECT_IMAGE_KHR, img, EGL_FALSE);
2382
2383 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2384 assert(disp->Extensions.MESA_image_dma_buf_export);
2385
2386 if (!img)
2387 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
2388
2389 ret = drv->API.ExportDMABUFImageMESA(drv, disp, img, fds, strides, offsets);
2390
2391 RETURN_EGL_EVAL(disp, ret);
2392 }
2393
2394 static EGLint EGLAPIENTRY
2395 eglLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object,
2396 EGLLabelKHR label)
2397 {
2398 _EGLDisplay *disp = NULL;
2399 _EGLResourceType type;
2400
2401 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2402
2403 if (objectType == EGL_OBJECT_THREAD_KHR) {
2404 _EGLThreadInfo *t = _eglGetCurrentThread();
2405
2406 if (!_eglIsCurrentThreadDummy()) {
2407 t->Label = label;
2408 return EGL_SUCCESS;
2409 }
2410
2411 RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_BAD_ALLOC);
2412 }
2413
2414 disp = _eglLockDisplay(dpy);
2415 if (disp == NULL)
2416 RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_BAD_DISPLAY);
2417
2418 if (objectType == EGL_OBJECT_DISPLAY_KHR) {
2419 if (dpy != (EGLDisplay) object)
2420 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2421
2422 disp->Label = label;
2423 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2424 }
2425
2426 switch (objectType) {
2427 case EGL_OBJECT_CONTEXT_KHR:
2428 type = _EGL_RESOURCE_CONTEXT;
2429 break;
2430 case EGL_OBJECT_SURFACE_KHR:
2431 type = _EGL_RESOURCE_SURFACE;
2432 break;
2433 case EGL_OBJECT_IMAGE_KHR:
2434 type = _EGL_RESOURCE_IMAGE;
2435 break;
2436 case EGL_OBJECT_SYNC_KHR:
2437 type = _EGL_RESOURCE_SYNC;
2438 break;
2439 case EGL_OBJECT_STREAM_KHR:
2440 default:
2441 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2442 }
2443
2444 if (_eglCheckResource(object, type, disp)) {
2445 _EGLResource *res = (_EGLResource *) object;
2446
2447 res->Label = label;
2448 RETURN_EGL_EVAL(disp, EGL_SUCCESS);
2449 }
2450
2451 RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_BAD_PARAMETER);
2452 }
2453
2454 static EGLint EGLAPIENTRY
2455 eglDebugMessageControlKHR(EGLDEBUGPROCKHR callback,
2456 const EGLAttrib *attrib_list)
2457 {
2458 unsigned int newEnabled;
2459
2460 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2461
2462 mtx_lock(_eglGlobal.Mutex);
2463
2464 newEnabled = _eglGlobal.debugTypesEnabled;
2465 if (attrib_list != NULL) {
2466 int i;
2467
2468 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
2469 switch (attrib_list[i]) {
2470 case EGL_DEBUG_MSG_CRITICAL_KHR:
2471 case EGL_DEBUG_MSG_ERROR_KHR:
2472 case EGL_DEBUG_MSG_WARN_KHR:
2473 case EGL_DEBUG_MSG_INFO_KHR:
2474 if (attrib_list[i + 1])
2475 newEnabled |= DebugBitFromType(attrib_list[i]);
2476 else
2477 newEnabled &= ~DebugBitFromType(attrib_list[i]);
2478 break;
2479 default:
2480 // On error, set the last error code, call the current
2481 // debug callback, and return the error code.
2482 mtx_unlock(_eglGlobal.Mutex);
2483 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2484 "Invalid attribute 0x%04lx", (unsigned long) attrib_list[i]);
2485 return EGL_BAD_ATTRIBUTE;
2486 }
2487 }
2488 }
2489
2490 if (callback != NULL) {
2491 _eglGlobal.debugCallback = callback;
2492 _eglGlobal.debugTypesEnabled = newEnabled;
2493 } else {
2494 _eglGlobal.debugCallback = NULL;
2495 _eglGlobal.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR;
2496 }
2497
2498 mtx_unlock(_eglGlobal.Mutex);
2499 return EGL_SUCCESS;
2500 }
2501
2502 static EGLBoolean EGLAPIENTRY
2503 eglQueryDebugKHR(EGLint attribute, EGLAttrib *value)
2504 {
2505 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_BAD_ALLOC);
2506
2507 mtx_lock(_eglGlobal.Mutex);
2508
2509 switch (attribute) {
2510 case EGL_DEBUG_MSG_CRITICAL_KHR:
2511 case EGL_DEBUG_MSG_ERROR_KHR:
2512 case EGL_DEBUG_MSG_WARN_KHR:
2513 case EGL_DEBUG_MSG_INFO_KHR:
2514 if (_eglGlobal.debugTypesEnabled & DebugBitFromType(attribute))
2515 *value = EGL_TRUE;
2516 else
2517 *value = EGL_FALSE;
2518 break;
2519 case EGL_DEBUG_CALLBACK_KHR:
2520 *value = (EGLAttrib) _eglGlobal.debugCallback;
2521 break;
2522 default:
2523 mtx_unlock(_eglGlobal.Mutex);
2524 _eglReportError(EGL_BAD_ATTRIBUTE, NULL,
2525 "Invalid attribute 0x%04lx", (unsigned long) attribute);
2526 return EGL_FALSE;
2527 }
2528
2529 mtx_unlock(_eglGlobal.Mutex);
2530 return EGL_TRUE;
2531 }
2532
2533 static int
2534 _eglFunctionCompare(const void *key, const void *elem)
2535 {
2536 const char *procname = key;
2537 const struct _egl_entrypoint *entrypoint = elem;
2538 return strcmp(procname, entrypoint->name);
2539 }
2540
2541 static EGLBoolean EGLAPIENTRY
2542 eglQueryDmaBufFormatsEXT(EGLDisplay dpy, EGLint max_formats,
2543 EGLint *formats, EGLint *num_formats)
2544 {
2545 _EGLDisplay *disp = _eglLockDisplay(dpy);
2546 _EGLDriver *drv;
2547 EGLBoolean ret;
2548
2549 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2550
2551 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2552
2553 ret = drv->API.QueryDmaBufFormatsEXT(drv, disp, max_formats, formats,
2554 num_formats);
2555
2556 RETURN_EGL_EVAL(disp, ret);
2557 }
2558
2559 static EGLBoolean EGLAPIENTRY
2560 eglQueryDmaBufModifiersEXT(EGLDisplay dpy, EGLint format, EGLint max_modifiers,
2561 EGLuint64KHR *modifiers, EGLBoolean *external_only,
2562 EGLint *num_modifiers)
2563 {
2564 _EGLDisplay *disp = _eglLockDisplay(dpy);
2565 _EGLDriver *drv;
2566 EGLBoolean ret;
2567
2568 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2569
2570 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2571
2572 ret = drv->API.QueryDmaBufModifiersEXT(drv, disp, format, max_modifiers,
2573 modifiers, external_only,
2574 num_modifiers);
2575
2576 RETURN_EGL_EVAL(disp, ret);
2577 }
2578
2579 static void EGLAPIENTRY
2580 eglSetBlobCacheFuncsANDROID(EGLDisplay *dpy, EGLSetBlobFuncANDROID set,
2581 EGLGetBlobFuncANDROID get)
2582 {
2583 /* This function does not return anything so we cannot
2584 * utilize the helper macros _EGL_FUNC_START or _EGL_CHECK_DISPLAY.
2585 */
2586 _EGLDisplay *disp = _eglLockDisplay(dpy);
2587 if (!_eglSetFuncName(__func__, disp, EGL_OBJECT_DISPLAY_KHR, NULL)) {
2588 if (disp)
2589 _eglUnlockDisplay(disp);
2590 return;
2591 }
2592
2593 _EGLDriver *drv = _eglCheckDisplay(disp, __func__);
2594 if (!drv) {
2595 if (disp)
2596 _eglUnlockDisplay(disp);
2597 return;
2598 }
2599
2600 if (!set || !get) {
2601 _eglError(EGL_BAD_PARAMETER,
2602 "eglSetBlobCacheFuncsANDROID: NULL handler given");
2603 _eglUnlockDisplay(disp);
2604 return;
2605 }
2606
2607 if (disp->BlobCacheSet) {
2608 _eglError(EGL_BAD_PARAMETER,
2609 "eglSetBlobCacheFuncsANDROID: functions already set");
2610 _eglUnlockDisplay(disp);
2611 return;
2612 }
2613
2614 disp->BlobCacheSet = set;
2615 disp->BlobCacheGet = get;
2616
2617 drv->API.SetBlobCacheFuncsANDROID(drv, disp, set, get);
2618
2619 _eglUnlockDisplay(disp);
2620 }
2621
2622 static EGLBoolean EGLAPIENTRY
2623 eglQueryDeviceAttribEXT(EGLDeviceEXT device,
2624 EGLint attribute,
2625 EGLAttrib *value)
2626 {
2627 _EGLDevice *dev = _eglLookupDevice(device);
2628 EGLBoolean ret;
2629
2630 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2631 if (!dev)
2632 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, EGL_FALSE);
2633
2634 ret = _eglQueryDeviceAttribEXT(dev, attribute, value);
2635 RETURN_EGL_EVAL(NULL, ret);
2636 }
2637
2638 static const char * EGLAPIENTRY
2639 eglQueryDeviceStringEXT(EGLDeviceEXT device,
2640 EGLint name)
2641 {
2642 _EGLDevice *dev = _eglLookupDevice(device);
2643
2644 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2645 if (!dev)
2646 RETURN_EGL_ERROR(NULL, EGL_BAD_DEVICE_EXT, NULL);
2647
2648 RETURN_EGL_EVAL(NULL, _eglQueryDeviceStringEXT(dev, name));
2649 }
2650
2651 static EGLBoolean EGLAPIENTRY
2652 eglQueryDevicesEXT(EGLint max_devices,
2653 EGLDeviceEXT *devices,
2654 EGLint *num_devices)
2655 {
2656 EGLBoolean ret;
2657
2658 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2659 ret = _eglQueryDevicesEXT(max_devices, (_EGLDevice **) devices,
2660 num_devices);
2661 RETURN_EGL_EVAL(NULL, ret);
2662 }
2663
2664 static EGLBoolean EGLAPIENTRY
2665 eglQueryDisplayAttribEXT(EGLDisplay dpy,
2666 EGLint attribute,
2667 EGLAttrib *value)
2668 {
2669 _EGLDisplay *disp = _eglLockDisplay(dpy);
2670 _EGLDriver *drv;
2671
2672 _EGL_FUNC_START(NULL, EGL_NONE, NULL, EGL_FALSE);
2673 _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
2674
2675 switch (attribute) {
2676 case EGL_DEVICE_EXT:
2677 *value = (EGLAttrib) disp->Device;
2678 break;
2679 default:
2680 RETURN_EGL_ERROR(disp, EGL_BAD_ATTRIBUTE, EGL_FALSE);
2681 }
2682 RETURN_EGL_SUCCESS(disp, EGL_TRUE);
2683 }
2684
2685 static char * EGLAPIENTRY
2686 eglGetDisplayDriverConfig(EGLDisplay dpy)
2687 {
2688 _EGLDisplay *disp = _eglLockDisplay(dpy);
2689 _EGLDriver *drv;
2690 char *ret;
2691
2692 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2693 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2694
2695 assert(disp->Extensions.MESA_query_driver);
2696
2697 ret = drv->API.QueryDriverConfig(disp);
2698 RETURN_EGL_EVAL(disp, ret);
2699 }
2700
2701 static const char * EGLAPIENTRY
2702 eglGetDisplayDriverName(EGLDisplay dpy)
2703 {
2704 _EGLDisplay *disp = _eglLockDisplay(dpy);
2705 _EGLDriver *drv;
2706 const char *ret;
2707
2708 _EGL_FUNC_START(disp, EGL_NONE, NULL, NULL);
2709 _EGL_CHECK_DISPLAY(disp, NULL, drv);
2710
2711 assert(disp->Extensions.MESA_query_driver);
2712
2713 ret = drv->API.QueryDriverName(disp);
2714 RETURN_EGL_EVAL(disp, ret);
2715 }
2716
2717 __eglMustCastToProperFunctionPointerType EGLAPIENTRY
2718 eglGetProcAddress(const char *procname)
2719 {
2720 static const struct _egl_entrypoint egl_functions[] = {
2721 #define EGL_ENTRYPOINT(f) { .name = #f, .function = (_EGLProc) f },
2722 #include "eglentrypoint.h"
2723 #undef EGL_ENTRYPOINT
2724 };
2725 _EGLProc ret = NULL;
2726
2727 if (!procname)
2728 RETURN_EGL_SUCCESS(NULL, NULL);
2729
2730 _EGL_FUNC_START(NULL, EGL_NONE, NULL, NULL);
2731
2732 if (strncmp(procname, "egl", 3) == 0) {
2733 const struct _egl_entrypoint *entrypoint =
2734 bsearch(procname,
2735 egl_functions, ARRAY_SIZE(egl_functions),
2736 sizeof(egl_functions[0]),
2737 _eglFunctionCompare);
2738 if (entrypoint)
2739 ret = entrypoint->function;
2740 }
2741
2742 if (!ret)
2743 ret = _eglGetDriverProc(procname);
2744
2745 RETURN_EGL_SUCCESS(NULL, ret);
2746 }
2747
2748 static int
2749 _eglLockDisplayInterop(EGLDisplay dpy, EGLContext context,
2750 _EGLDisplay **disp, _EGLDriver **drv,
2751 _EGLContext **ctx)
2752 {
2753
2754 *disp = _eglLockDisplay(dpy);
2755 if (!*disp || !(*disp)->Initialized || !(*disp)->Driver) {
2756 if (*disp)
2757 _eglUnlockDisplay(*disp);
2758 return MESA_GLINTEROP_INVALID_DISPLAY;
2759 }
2760
2761 *drv = (*disp)->Driver;
2762
2763 *ctx = _eglLookupContext(context, *disp);
2764 if (!*ctx ||
2765 ((*ctx)->ClientAPI != EGL_OPENGL_API &&
2766 (*ctx)->ClientAPI != EGL_OPENGL_ES_API)) {
2767 _eglUnlockDisplay(*disp);
2768 return MESA_GLINTEROP_INVALID_CONTEXT;
2769 }
2770
2771 return MESA_GLINTEROP_SUCCESS;
2772 }
2773
2774 PUBLIC int
2775 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
2776 struct mesa_glinterop_device_info *out)
2777 {
2778 _EGLDisplay *disp;
2779 _EGLDriver *drv;
2780 _EGLContext *ctx;
2781 int ret;
2782
2783 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2784 if (ret != MESA_GLINTEROP_SUCCESS)
2785 return ret;
2786
2787 if (drv->API.GLInteropQueryDeviceInfo)
2788 ret = drv->API.GLInteropQueryDeviceInfo(disp, ctx, out);
2789 else
2790 ret = MESA_GLINTEROP_UNSUPPORTED;
2791
2792 _eglUnlockDisplay(disp);
2793 return ret;
2794 }
2795
2796 PUBLIC int
2797 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
2798 struct mesa_glinterop_export_in *in,
2799 struct mesa_glinterop_export_out *out)
2800 {
2801 _EGLDisplay *disp;
2802 _EGLDriver *drv;
2803 _EGLContext *ctx;
2804 int ret;
2805
2806 ret = _eglLockDisplayInterop(dpy, context, &disp, &drv, &ctx);
2807 if (ret != MESA_GLINTEROP_SUCCESS)
2808 return ret;
2809
2810 if (drv->API.GLInteropExportObject)
2811 ret = drv->API.GLInteropExportObject(disp, ctx, in, out);
2812 else
2813 ret = MESA_GLINTEROP_UNSUPPORTED;
2814
2815 _eglUnlockDisplay(disp);
2816 return ret;
2817 }