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