1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/*
* Copyright 2012 Intel Corporation
* Copyright 2010 LunarG Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <waffle_glx.h>
#include <waffle_x11_egl.h>
#include "priv/common.h"
#ifdef PIGLIT_HAS_X11
# include "priv/x11.h"
#endif
void
glutInitAPIMask(int mask)
{
switch (mask) {
case GLUT_OPENGL_BIT:
_glut->waffle_context_api = WAFFLE_CONTEXT_OPENGL;
break;
case GLUT_OPENGL_ES1_BIT:
_glut->waffle_context_api = WAFFLE_CONTEXT_OPENGL_ES1;
break;
case GLUT_OPENGL_ES2_BIT:
_glut->waffle_context_api = WAFFLE_CONTEXT_OPENGL_ES2;
break;
default:
glutFatal("api_mask has bad value %#x", mask);
break;
}
}
void
glutInit(int *argcp, char **argv)
{
const char *piglit_platform;
const char *display_name = NULL;
bool ok = true;
int i;
int32_t waffle_init_attrib_list[] = {
WAFFLE_PLATFORM, 0x31415925,
0,
};
for (i = 1; i < *argcp; i++) {
if (strcmp(argv[i], "-display") == 0)
display_name = argv[++i];
else if (strcmp(argv[i], "-info") == 0) {
printf("waffle_glut: ignoring -info\n");
}
}
_glut->waffle_context_api = WAFFLE_CONTEXT_OPENGL;
piglit_platform = getenv("PIGLIT_PLATFORM");
if (piglit_platform == NULL) {
_glut->waffle_platform = WAFFLE_PLATFORM_GLX;
} else if (!strcmp(piglit_platform, "glx")) {
_glut->waffle_platform = WAFFLE_PLATFORM_GLX;
} else if (!strcmp(piglit_platform, "x11_egl")) {
_glut->waffle_platform = WAFFLE_PLATFORM_X11_EGL;
} else if (!strcmp(piglit_platform, "wayland")) {
_glut->waffle_platform = WAFFLE_PLATFORM_WAYLAND;
} else {
glutFatal("environment var PIGLIT_PLATFORM has bad "
"value \"%s\"", piglit_platform);
}
#ifndef PIGLIT_HAS_X11
if (_glut->waffle_platform == WAFFLE_PLATFORM_GLX ||
_glut->waffle_platform == WAFFLE_PLATFORM_X11_EGL)
glutFatal("piglit was built without x11 support");
#endif
waffle_init_attrib_list[1] = _glut->waffle_platform;
ok = waffle_init(waffle_init_attrib_list);
if (!ok)
glutFatal("waffle_init() failed");
_glut->display = waffle_display_connect(display_name);
if (!_glut->display)
glutFatal("waffle_display_connect() failed");
}
void
glutInitDisplayMode(unsigned int mode)
{
_glut->display_mode = mode;
}
void
glutInitWindowPosition(int x, int y)
{
// empty
}
void
glutInitWindowSize(int width, int height)
{
_glut->window_width = width;
_glut->window_height = height;
}
static struct waffle_config*
glutChooseConfig(void)
{
struct waffle_config *config = NULL;
int32_t attrib_list[64];
int i = 0;
#define ADD_ATTR(name, value) \
do { \
attrib_list[i++] = name; \
attrib_list[i++] = value; \
} while (0)
ADD_ATTR(WAFFLE_CONTEXT_API, _glut->waffle_context_api);
/* It is impossible to not request RGBA because GLUT_RGB and
* GLUT_RGBA are both 0. That is, (display_mode & (GLUT_RGB
* | GLUT_RGBA)) is unconditonally true.
*/
ADD_ATTR(WAFFLE_RED_SIZE, 1);
ADD_ATTR(WAFFLE_GREEN_SIZE, 1);
ADD_ATTR(WAFFLE_BLUE_SIZE, 1);
ADD_ATTR(WAFFLE_ALPHA_SIZE, 1);
if (_glut->display_mode & GLUT_DEPTH) {
ADD_ATTR(WAFFLE_DEPTH_SIZE, 1);
}
if (_glut->display_mode & GLUT_STENCIL) {
ADD_ATTR(WAFFLE_STENCIL_SIZE, 1);
}
if (!(_glut->display_mode & GLUT_DOUBLE)) {
ADD_ATTR(WAFFLE_DOUBLE_BUFFERED, false);
}
if (_glut->display_mode & GLUT_ACCUM) {
ADD_ATTR(WAFFLE_ACCUM_BUFFER, true);
}
attrib_list[i++] = WAFFLE_NONE;
config = waffle_config_choose(_glut->display, attrib_list);
if (!config)
glutFatal("waffle_config_choose() failed");
return config;
}
void
glutPostRedisplay(void)
{
_glut->redisplay = 1;
}
static void
_glutDefaultKeyboard(unsigned char key, int x, int y)
{
if (key == 27)
exit(0);
}
int
glutCreateWindow(const char *title)
{
bool ok = true;
struct waffle_config *config = NULL;
union waffle_native_window *n_window = NULL;
if (_glut->window)
glutFatal("cannot create window; one already exists");
config = glutChooseConfig();
_glut->context = waffle_context_create(config, NULL);
if (!_glut->context)
glutFatal("waffle_context_create() failed");
_glut->window = calloc(1, sizeof(*_glut->window));
if (!_glut->window)
glutFatal("out of memory");
_glut->window->waffle = waffle_window_create(config,
_glut->window_width,
_glut->window_height);
if (!_glut->window->waffle)
glutFatal("waffle_window_create() failed");
n_window = waffle_window_get_native(_glut->window->waffle);
if (!n_window)
glutFatal("waffle_window_get_window() failed");
switch (_glut->waffle_platform) {
#ifdef PIGLIT_HAS_X11
case WAFFLE_PLATFORM_GLX:
_glut->window->x11.display = n_window->glx->xlib_display;
_glut->window->x11.window = n_window->glx->xlib_window;
break;
case WAFFLE_PLATFORM_X11_EGL:
_glut->window->x11.display = n_window->x11_egl->display.xlib_display;
_glut->window->x11.window = n_window->x11_egl->xlib_window;
break;
#endif
case WAFFLE_PLATFORM_WAYLAND:
printf("glut_waffle: warning: input is not yet "
"implemented for Wayland\n");
break;
default:
assert(0);
break;
}
ok = waffle_make_current(_glut->display, _glut->window->waffle,
_glut->context);
if (!ok)
glutFatal("waffle_make_current() failed");
_glut->window->id = ++_glut->window_id_pool;
_glut->window->keyboard_cb = _glutDefaultKeyboard;
return _glut->window->id;
}
void
glutDestroyWindow(int win)
{
bool ok = true;
if (!_glut->window || _glut->window->id != win)
glutFatal("bad window id");
ok = waffle_window_destroy(_glut->window->waffle);
if (!ok)
glutFatal("waffle_window_destroy() failed");
free(_glut->window);
_glut->window = NULL;
}
void
glutShowWindow(int win)
{
bool ok = true;
if (!_glut->window || _glut->window->id != win)
glutFatal("bad window id");
ok = waffle_window_show(_glut->window->waffle);
if (!ok)
glutFatal("waffle_window_show() failed");
}
void
glutDisplayFunc(GLUT_EGLdisplayCB func)
{
_glut->window->display_cb = func;
}
void
glutReshapeFunc(GLUT_EGLreshapeCB func)
{
_glut->window->reshape_cb = func;
}
void
glutKeyboardFunc(GLUT_EGLkeyboardCB func)
{
_glut->window->keyboard_cb = func;
}
void
glutMainLoop(void)
{
bool ok = true;
if (!_glut->window)
glutFatal("no window is created");
ok = waffle_window_show(_glut->window->waffle);
if (!ok)
glutFatal("waffle_window_show() failed");
if (_glut->window->reshape_cb)
_glut->window->reshape_cb(_glut->window_width,
_glut->window_height);
if (_glut->window->display_cb)
_glut->window->display_cb();
switch (_glut->waffle_platform) {
#ifdef PIGLIT_HAS_X11
case WAFFLE_PLATFORM_GLX:
case WAFFLE_PLATFORM_X11_EGL:
x11_event_loop();
break;
#endif
case WAFFLE_PLATFORM_WAYLAND:
/* The Wayland window fails to appear on the first call to
* swapBuffers (which occured in display_cb above). This is
* likely due to swapBuffers being called before receiving an
* expose event. Until piglit has proper Wayland support,
* call swapBuffers again as a workaround.
*/
if (_glut->window->display_cb)
_glut->window->display_cb();
/* FINISHME: Write event loop for Wayland. */
sleep(20);
break;
default:
assert(0);
break;
}
}
void
glutSwapBuffers(void)
{
bool ok = waffle_window_swap_buffers(_glut->window->waffle);
if (!ok)
glutFatal("waffle_window_swap_buffers() failed");
}
|