gitlab-ci: enable sanitizers for the meson builds
[mesa-waffle.git] / src / waffle / api / waffle_init.c
blobb9d2fedfb1252e6d0b12f166c287365f9517fa98
1 // SPDX-FileCopyrightText: Copyright 2012 Intel Corporation
2 // SPDX-License-Identifier: BSD-2-Clause
4 #include <assert.h>
6 #include "api_priv.h"
8 #include "wcore_error.h"
9 #include "wcore_platform.h"
11 struct wcore_platform* cgl_platform_create(void);
12 struct wcore_platform* droid_platform_create(void);
13 struct wcore_platform* glx_platform_create(void);
14 struct wcore_platform* wayland_platform_create(void);
15 struct wcore_platform* xegl_platform_create(void);
16 struct wcore_platform* wgbm_platform_create(void);
17 struct wcore_platform* wgl_platform_create(void);
18 struct wcore_platform* sl_platform_create(void);
20 static bool
21 waffle_init_parse_attrib_list(
22 const int32_t attrib_list[],
23 int *platform)
25 bool found_platform = false;
27 for (const int32_t *i = attrib_list; *i != 0; i += 2) {
28 const int32_t attr = i[0];
29 const int32_t value = i[1];
31 switch (attr) {
32 case WAFFLE_PLATFORM:
33 switch (value) {
34 #define CASE_DEFINED_PLATFORM(name) \
35 case WAFFLE_PLATFORM_##name : \
36 found_platform = true; \
37 *platform = value; \
38 break;
40 #define CASE_UNDEFINED_PLATFORM(name) \
41 case WAFFLE_PLATFORM_##name : \
42 wcore_errorf(WAFFLE_ERROR_BUILT_WITHOUT_SUPPORT, \
43 "waffle was built without support for WAFFLE_PLATFORM_" #name); \
44 return false;
46 #ifdef WAFFLE_HAS_ANDROID
47 CASE_DEFINED_PLATFORM(ANDROID)
48 #else
49 CASE_UNDEFINED_PLATFORM(ANDROID)
50 #endif
52 #ifdef WAFFLE_HAS_CGL
53 CASE_DEFINED_PLATFORM(CGL)
54 #else
55 CASE_UNDEFINED_PLATFORM(CGL)
56 #endif
58 #ifdef WAFFLE_HAS_GLX
59 CASE_DEFINED_PLATFORM(GLX)
60 #else
61 CASE_UNDEFINED_PLATFORM(GLX)
62 #endif
64 #ifdef WAFFLE_HAS_WAYLAND
65 CASE_DEFINED_PLATFORM(WAYLAND)
66 #else
67 CASE_UNDEFINED_PLATFORM(WAYLAND)
68 #endif
70 #ifdef WAFFLE_HAS_X11_EGL
71 CASE_DEFINED_PLATFORM(X11_EGL)
72 #else
73 CASE_UNDEFINED_PLATFORM(X11_EGL)
74 #endif
76 #ifdef WAFFLE_HAS_GBM
77 CASE_DEFINED_PLATFORM(GBM)
78 #else
79 CASE_UNDEFINED_PLATFORM(GBM)
80 #endif
82 #ifdef WAFFLE_HAS_WGL
83 CASE_DEFINED_PLATFORM(WGL)
84 #else
85 CASE_UNDEFINED_PLATFORM(WGL)
86 #endif
88 #ifdef WAFFLE_HAS_SURFACELESS_EGL
89 CASE_DEFINED_PLATFORM(SURFACELESS_EGL)
90 #else
91 CASE_UNDEFINED_PLATFORM(SURFACELESS_EGL)
92 #endif
94 default:
95 wcore_errorf(WAFFLE_ERROR_BAD_ATTRIBUTE,
96 "WAFFLE_PLATFORM has bad value 0x%x",
97 value);
98 return false;
100 #undef CASE_DEFINED_PLATFORM
101 #undef CASE_UNDEFINED_PLATFORM
104 break;
105 default:
106 wcore_errorf(WAFFLE_ERROR_BAD_ATTRIBUTE,
107 "bad attribute name %#x", attr);
108 return false;
109 break;
113 if (!found_platform) {
114 wcore_errorf(WAFFLE_ERROR_BAD_ATTRIBUTE,
115 "attribute list is missing WAFFLE_PLATFORM");
116 return false;
119 return true;
122 static struct wcore_platform*
123 waffle_init_create_platform(int32_t waffle_platform)
125 struct wcore_platform *wc_platform = NULL;
127 switch (waffle_platform) {
128 #ifdef WAFFLE_HAS_ANDROID
129 case WAFFLE_PLATFORM_ANDROID:
130 wc_platform = droid_platform_create();
131 break;
132 #endif
133 #ifdef WAFFLE_HAS_CGL
134 case WAFFLE_PLATFORM_CGL:
135 wc_platform = cgl_platform_create();
136 break;
137 #endif
138 #ifdef WAFFLE_HAS_GLX
139 case WAFFLE_PLATFORM_GLX:
140 wc_platform = glx_platform_create();
141 break;
142 #endif
143 #ifdef WAFFLE_HAS_WAYLAND
144 case WAFFLE_PLATFORM_WAYLAND:
145 wc_platform = wayland_platform_create();
146 break;
147 #endif
148 #ifdef WAFFLE_HAS_X11_EGL
149 case WAFFLE_PLATFORM_X11_EGL:
150 wc_platform = xegl_platform_create();
151 break;
152 #endif
153 #ifdef WAFFLE_HAS_GBM
154 case WAFFLE_PLATFORM_GBM:
155 wc_platform = wgbm_platform_create();
156 break;
157 #endif
158 #ifdef WAFFLE_HAS_WGL
159 case WAFFLE_PLATFORM_WGL:
160 wc_platform = wgl_platform_create();
161 break;
162 #endif
163 #ifdef WAFFLE_HAS_SURFACELESS_EGL
164 case WAFFLE_PLATFORM_SURFACELESS_EGL:
165 wc_platform = sl_platform_create();
166 break;
167 #endif
168 default:
169 assert(false);
170 return NULL;
173 if (wc_platform)
174 wc_platform->waffle_platform = waffle_platform;
176 return wc_platform;
179 WAFFLE_API bool
180 waffle_init(const int32_t *attrib_list)
182 bool ok = true;
183 int platform;
185 wcore_error_reset();
187 if (api_platform) {
188 wcore_error(WAFFLE_ERROR_ALREADY_INITIALIZED);
189 return false;
192 ok &= waffle_init_parse_attrib_list(attrib_list, &platform);
193 if (!ok)
194 return false;
196 api_platform = waffle_init_create_platform(platform);
197 if (!api_platform)
198 return false;
200 return true;
203 WAFFLE_API bool
204 waffle_teardown(void)
206 bool ok = true;
208 wcore_error_reset();
210 if (!api_platform) {
211 wcore_error(WAFFLE_ERROR_NOT_INITIALIZED);
212 return false;
215 ok &= api_platform->vtbl->destroy(api_platform);
216 if (!ok)
217 return false;
219 api_platform = NULL;
220 return true;