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