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