aboutsummaryrefslogtreecommitdiff
path: root/src/glut_waffle/glut_waffle.c
blob: 0359074951b03a2f53dbbc47ca0ccfa3b8642dd9 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * 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 "glut_waffle.h"

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

#include <EGL/egl.h>
#include <waffle/waffle.h>

extern int piglit_automatic;

struct glut_waffle_window;

struct glut_waffle_state {
	/** \brief One of `WAFFLE_PLATFORM_*`. */
	int waffle_platform;

	/** \brief One of `WAFFLE_CONTEXT_OPENGL*`.
	 *
	 * The default value is `WAFFLE_CONTEXT_OPENGL`. To change the value,
	 * call glutInitAPIMask().
	 */
	int waffle_context_api;

	/** \brief A bitmask of enum glut_display_mode`. */
	int display_mode;

	int window_width;
	int window_height;

	int verbose;
	int init_time;

	GLUT_EGLidleCB idle_cb;

	struct waffle_display *display;
	struct waffle_context *context;
	struct glut_window *window;

	int redisplay;
	int window_id_pool;
};

static struct glut_waffle_state _glut_waffle_state = {
	.display_mode = GLUT_RGB,
	.window_width = 300,
	.window_height = 300,
	.verbose = 0,
	.window_id_pool = 0,
};

static struct glut_waffle_state *const _glut = &_glut_waffle_state;

struct glut_window {
	struct waffle_window *waffle;

	int id;

	GLUT_EGLreshapeCB reshape_cb;
	GLUT_EGLdisplayCB display_cb;
	GLUT_EGLkeyboardCB keyboard_cb;
	GLUT_EGLspecialCB special_cb;
};

static void
glutFatal(char *format, ...)
{
	va_list args;

	va_start(args, format);

	fflush(stdout);
	fprintf(stderr, "glut_waffle: ");
	vfprintf(stderr, format, args);
	va_end(args);
	putc('\n', stderr);

	exit(1);
}

/**  Return current time (in milliseconds). */
static int
glutNow(void)
{
	struct timeval tv;
#ifdef __VMS
	(void) gettimeofday(&tv, NULL );
#else
	struct timezone tz;
	(void) gettimeofday(&tv, &tz);
#endif
	return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

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);
	}

	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");

	_glut->init_time = glutNow();
}

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);

	if (_glut->display_mode & (GLUT_RGB | GLUT_RGBA)) {
		ADD_ATTR(WAFFLE_RED_SIZE,    1);
		ADD_ATTR(WAFFLE_GREEN_SIZE,  1);
		ADD_ATTR(WAFFLE_BLUE_SIZE,   1);
	}

	if (_glut->display_mode & (GLUT_ALPHA | GLUT_RGBA)) {
		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;
}

int
glutGet(int state)
{
	int val;

	switch (state) {
		case GLUT_ELAPSED_TIME:
			val = glutNow() - _glut->init_time;
			break;
		default:
			val = -1;
			break;
	}

	return val;
}

void
glutIdleFunc(GLUT_EGLidleCB func)
{
	_glut->idle_cb = func;
}

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;

	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");

	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
glutSpecialFunc(GLUT_EGLspecialCB func)
{
	_glut->window->special_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();

	// FIXME: Tests run without -auto require basic input.

	// Workaround for input:
	// Since glut_waffle doesn't handle input yet, it sleeps in order to
	// give the user a chance to see the test output. If the user wishes
	// the test to sleep for a shorter or longer time, he can use Ctrl-C
	// or Ctrl-Z.
	sleep(20);
}

void
glutSwapBuffers(void)
{
	bool ok = waffle_window_swap_buffers(_glut->window->waffle);
	if (!ok)
		glutFatal("waffle_window_swap_buffers() failed");
}