gitlab-ci: enable sanitizers for the meson builds
[mesa-waffle.git] / src / waffle / egl / wegl_display.c
blob4a5e1740bc027830614ca9b75850256806b66119
1 // SPDX-FileCopyrightText: Copyright 2012 Intel Corporation
2 // SPDX-License-Identifier: BSD-2-Clause
4 #include <assert.h>
6 #include "wcore_error.h"
7 #include "wcore_platform.h"
9 #include "wegl_display.h"
10 #include "wegl_imports.h"
11 #include "wegl_util.h"
12 #include "wegl_platform.h"
14 static bool
15 get_apis(struct wegl_display *dpy)
17 struct wegl_platform *plat = wegl_platform(dpy->wcore.platform);
18 const char *apis = plat->eglQueryString(dpy->egl, EGL_CLIENT_APIS);
20 // Our minimum requirement - EGL 1.2 ...
21 if (dpy->major_version != 1 || dpy->minor_version < 2) {
22 wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
23 "EGL 1.2 or later is required");
24 return false;
27 // ... plus working eglQueryString(EGL_CLIENT_APIS).
28 if (!apis) {
29 wegl_emit_error(plat, "eglQueryString(EGL_CLIENT_APIS)");
30 return false;
33 // waffle_is_extension_in_string() resets the error state. That's ok,
34 // however, because if we've reached this point then no error should be
35 // pending emission.
36 assert(wcore_error_get_code() == 0);
38 if (waffle_is_extension_in_string(apis, "OpenGL_ES"))
39 dpy->api_mask |= WEGL_OPENGL_ES_API;
41 // Check for "OpenGL" if we're running EGL 1.4 or later.
42 if (dpy->major_version == 1 && dpy->minor_version >= 4)
43 if (waffle_is_extension_in_string(apis, "OpenGL"))
44 dpy->api_mask |= WEGL_OPENGL_API;
46 return true;
49 static bool
50 get_extensions(struct wegl_display *dpy)
52 struct wegl_platform *plat = wegl_platform(dpy->wcore.platform);
53 const char *extensions = plat->eglQueryString(dpy->egl, EGL_EXTENSIONS);
55 if (!extensions) {
56 wegl_emit_error(plat, "eglQueryString(EGL_EXTENSIONS)");
57 return false;
60 // waffle_is_extension_in_string() resets the error state. That's ok,
61 // however, because if we've reached this point then no error should be
62 // pending emission.
63 assert(wcore_error_get_code() == 0);
65 #define CHECK_EXTENSION(ext) \
66 dpy->ext = waffle_is_extension_in_string(extensions, "EGL_" #ext)
68 CHECK_EXTENSION(EXT_create_context_robustness);
69 CHECK_EXTENSION(KHR_create_context);
70 CHECK_EXTENSION(EXT_image_dma_buf_import_modifiers);
71 CHECK_EXTENSION(EXT_create_context_robustness);
72 #undef CHECK_EXTENSION
74 return true;
77 /// On Linux, according to eglplatform.h, EGLNativeDisplayType and intptr_t
78 /// have the same size regardless of platform.
79 bool
80 wegl_display_init(struct wegl_display *dpy,
81 struct wcore_platform *wc_plat,
82 void *native_display)
84 struct wegl_platform *plat = wegl_platform(wc_plat);
86 wcore_display_init(&dpy->wcore, wc_plat);
88 if (wegl_platform_can_use_eglGetPlatformDisplay(plat)) {
89 dpy->egl = plat->eglGetPlatformDisplay(plat->egl_platform,
90 native_display, NULL);
91 if (!dpy->egl) {
92 wegl_emit_error(plat, "eglGetPlatformDisplay");
93 goto fail;
95 } else if (wegl_platform_can_use_eglGetPlatformDisplayEXT(plat)) {
96 dpy->egl = plat->eglGetPlatformDisplayEXT(plat->egl_platform,
97 native_display, NULL);
98 if (!dpy->egl) {
99 wegl_emit_error(plat, "eglGetPlatformDisplayEXT");
100 goto fail;
102 } else {
103 dpy->egl = plat->eglGetDisplay((EGLNativeDisplayType) native_display);
104 if (!dpy->egl) {
105 wegl_emit_error(plat, "eglGetDisplay");
106 goto fail;
110 bool ok =
111 plat->eglInitialize(dpy->egl, &dpy->major_version, &dpy->minor_version);
112 if (!ok) {
113 wegl_emit_error(plat, "eglInitialize");
114 goto fail;
117 ok = get_apis(dpy);
118 if (!ok)
119 goto fail;
121 ok = get_extensions(dpy);
122 if (!ok)
123 goto fail;
125 return true;
127 fail:
128 wegl_display_teardown(dpy);
129 return false;
132 bool
133 wegl_display_teardown(struct wegl_display *dpy)
135 struct wegl_platform *plat = wegl_platform(dpy->wcore.platform);
136 bool ok = true;
138 if (dpy->egl) {
139 ok = plat->eglTerminate(dpy->egl);
140 if (!ok)
141 wegl_emit_error(plat, "eglTerminate");
144 return ok;
147 bool
148 wegl_display_supports_context_api(struct wcore_display *wc_dpy,
149 int32_t waffle_context_api)
151 struct wegl_display *dpy = wegl_display(wc_dpy);
153 switch (waffle_context_api) {
154 case WAFFLE_CONTEXT_OPENGL:
155 return !!(dpy->api_mask & WEGL_OPENGL_API);
156 case WAFFLE_CONTEXT_OPENGL_ES1:
157 case WAFFLE_CONTEXT_OPENGL_ES2:
158 return !!(dpy->api_mask & WEGL_OPENGL_ES_API);
159 case WAFFLE_CONTEXT_OPENGL_ES3:
160 return !!(dpy->api_mask & WEGL_OPENGL_ES_API) &&
161 dpy->KHR_create_context;
162 default:
163 assert(false);
164 return false;