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