tests/gl_basic_test: add GL_CONTEXT_PROFILE_MASK check
[mesa-waffle.git] / tests / functional / gl_basic_test.c
blob5c9a08a92fc7164e217218101828bfb581621605
1 // Copyright 2012 Intel Corporation
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /// @file
27 /// @brief Test basic OpenGL rendering with all platform/gl_api combinations.
28 ///
29 /// Each test does the following:
30 /// 1. Initialize waffle with a platform and gl_api.
31 /// 2. Create a context and window.
32 /// 3. On the window call waffle_make_current, glClear,
33 /// and waffle_swap_buffers.
34 /// 4. Verify the window contents with glReadPixels.
35 /// 5. Tear down all waffle state.
37 #include <stdarg.h> // for va_start, va_end
38 #include <setjmp.h> // for cmocka.h
39 #include <ctype.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <getopt.h>
44 #include <sys/types.h>
45 #if !defined(_WIN32)
46 #include <unistd.h>
47 #include <sys/wait.h>
48 #else
49 #include <windows.h>
50 #endif
52 #include <cmocka.h>
53 #include "waffle.h"
55 #include "gl_basic_cocoa.h"
57 enum {
58 // Choosing a smaller window would shorten the execution time of pixel
59 // validation, but Windows 7 enforces a minimum size.
60 WINDOW_WIDTH = 320,
61 WINDOW_HEIGHT = 240,
64 static const float RED_F = 1.00;
65 static const float GREEN_F = 0.00;
66 static const float BLUE_F = 1.00;
67 static const float ALPHA_F = 1.00;
69 static const uint8_t RED_UB = 0xff;
70 static const uint8_t GREEN_UB = 0x00;
71 static const uint8_t BLUE_UB = 0xff;
72 static const uint8_t ALPHA_UB = 0xff;
74 struct test_state_gl_basic {
75 bool initialized;
76 struct waffle_display *dpy;
77 struct waffle_config *config;
78 struct waffle_window *window;
79 struct waffle_context *ctx;
81 uint8_t actual_pixels[4 * WINDOW_WIDTH * WINDOW_HEIGHT];
82 uint8_t expect_pixels[4 * WINDOW_WIDTH * WINDOW_HEIGHT];
85 #define ASSERT_GL(statement) \
86 do { \
87 statement; \
88 assert_false(glGetError()); \
89 } while (0)
91 typedef unsigned int GLenum;
92 typedef unsigned char GLboolean;
93 typedef unsigned int GLbitfield;
94 typedef void GLvoid;
95 typedef signed char GLbyte; /* 1-byte signed */
96 typedef short GLshort; /* 2-byte signed */
97 typedef int GLint; /* 4-byte signed */
98 typedef unsigned char GLubyte; /* 1-byte unsigned */
99 typedef unsigned short GLushort; /* 2-byte unsigned */
100 typedef unsigned int GLuint; /* 4-byte unsigned */
101 typedef int GLsizei; /* 4-byte signed */
102 typedef float GLfloat; /* single precision float */
103 typedef float GLclampf; /* single precision float in [0,1] */
104 typedef double GLdouble; /* double precision float */
105 typedef double GLclampd; /* double precision float in [0,1] */
107 #define GL_NO_ERROR 0x0000
108 #define GL_VERSION 0x1F02
109 #define GL_UNSIGNED_BYTE 0x1401
110 #define GL_UNSIGNED_INT 0x1405
111 #define GL_FLOAT 0x1406
112 #define GL_RGB 0x1907
113 #define GL_RGBA 0x1908
114 #define GL_COLOR_BUFFER_BIT 0x00004000
115 #define GL_CONTEXT_FLAGS 0x821e
116 #define GL_CONTEXT_ROBUST_ACCESS 0x90F3
118 #define GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT 0x00000001
119 #define GL_CONTEXT_FLAG_DEBUG_BIT 0x00000002
121 #define GL_CONTEXT_PROFILE_MASK 0x9126
122 #define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
123 #define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
125 #ifndef _WIN32
126 #define APIENTRY
127 #else
128 #ifndef APIENTRY
129 #define APIENTRY __stdcall
130 #endif
131 #endif
133 static GLenum (APIENTRY *glGetError)(void);
134 static const GLubyte *(APIENTRY *glGetString)(GLenum name);
135 static void (APIENTRY *glGetIntegerv)(GLenum pname, GLint *params);
136 static void (APIENTRY *glClearColor)(GLclampf red,
137 GLclampf green,
138 GLclampf blue,
139 GLclampf alpha);
140 static void (APIENTRY *glClear)(GLbitfield mask);
141 static void (APIENTRY *glReadPixels)(GLint x, GLint y,
142 GLsizei width, GLsizei height,
143 GLenum format, GLenum type,
144 GLvoid *pixels );
146 static int
147 setup(void **state)
149 struct test_state_gl_basic *ts;
151 ts = calloc(1, sizeof(*ts));
152 if (!ts)
153 return -1;
155 for (int y = 0 ; y < WINDOW_HEIGHT; ++y) {
156 for (int x = 0; x < WINDOW_WIDTH; ++x) {
157 uint8_t *p = &ts->expect_pixels[4 * (y * WINDOW_WIDTH + x)];
158 p[0] = RED_UB;
159 p[1] = GREEN_UB;
160 p[2] = BLUE_UB;
161 p[3] = ALPHA_UB;
164 // Fill actual_pixels with canaries.
165 memset(&ts->actual_pixels, 0x99, sizeof(ts->actual_pixels));
167 *state = ts;
168 return 0;
171 static int
172 teardown(void **state)
174 free(*state);
175 return 0;
178 // The rules that dictate how to properly query a GL symbol are complex. The
179 // rules depend on the OS, on the winsys API, and even on the particular driver
180 // being used. The rules differ between EGL 1.4 and EGL 1.5; differ between
181 // Linux, Windows, and Mac; and differ between Mesa and Mali.
183 // This function hides that complexity with a naive heuristic: try, then try
184 // again.
185 static void *
186 get_gl_symbol(enum waffle_enum context_api, const char *name)
188 void *sym = NULL;
189 enum waffle_enum dl = 0;
191 switch (context_api) {
192 case WAFFLE_CONTEXT_OPENGL: dl = WAFFLE_DL_OPENGL; break;
193 case WAFFLE_CONTEXT_OPENGL_ES1: dl = WAFFLE_DL_OPENGL_ES1; break;
194 case WAFFLE_CONTEXT_OPENGL_ES2: dl = WAFFLE_DL_OPENGL_ES2; break;
195 case WAFFLE_CONTEXT_OPENGL_ES3: dl = WAFFLE_DL_OPENGL_ES3; break;
196 default: assert_true(0); break;
199 if (waffle_dl_can_open(dl)) {
200 sym = waffle_dl_sym(dl, name);
203 if (!sym) {
204 sym = waffle_get_proc_address(name);
207 return sym;
210 static int
211 gl_basic_init(void **state, int32_t waffle_platform)
213 struct test_state_gl_basic *ts;
214 int ret;
216 ret = setup((void **)&ts);
217 if (ret)
218 return -1;
220 const int32_t init_attrib_list[] = {
221 WAFFLE_PLATFORM, waffle_platform,
225 ts->initialized = waffle_init(init_attrib_list);
226 if (!ts->initialized) {
227 // XXX: does cmocka call teardown if setup fails ?
228 teardown(state);
229 return -1;
232 *state = ts;
233 return 0;
236 static int
237 gl_basic_fini(void **state)
239 struct test_state_gl_basic *ts = *state;
240 bool ret = false;
242 // XXX: return immediately on error or attempt to finish the teardown ?
243 if (ts->dpy) // XXX: keep track if we've had current ctx ?
244 ret = waffle_make_current(ts->dpy, NULL, NULL);
245 if (ts->window)
246 ret = waffle_window_destroy(ts->window);
247 if (ts->ctx)
248 ret = waffle_context_destroy(ts->ctx);
249 if (ts->config)
250 ret = waffle_config_destroy(ts->config);
251 if (ts->dpy)
252 ret = waffle_display_disconnect(ts->dpy);
254 if (ts->initialized)
255 ret = waffle_teardown();
257 teardown(state);
258 return ret ? 0 : -1;
261 #define gl_basic_draw(state, ...) \
263 gl_basic_draw__(state, (struct gl_basic_draw_args__) { \
264 .api = 0, \
265 .version = WAFFLE_DONT_CARE, \
266 .profile = WAFFLE_DONT_CARE, \
267 .forward_compatible = false, \
268 .debug = false, \
269 .robust = false, \
270 .alpha = false, \
271 .expect_error = WAFFLE_NO_ERROR, \
272 __VA_ARGS__ \
275 struct gl_basic_draw_args__ {
276 int32_t api;
277 int32_t version;
278 int32_t profile;
279 int32_t expect_error;
280 bool forward_compatible;
281 bool debug;
282 bool robust;
283 bool alpha;
286 #define assert_int_equal_print(_a, _b) \
287 if (_a != _b) {\
288 fprintf(stderr, #_a " (%d) != " #_b "(%d)\n", _a, _b); \
289 assert_true(0 && "Integer comparison failed"); \
292 #define assert_int_ge(_a, _b) \
293 if (_a < _b) {\
294 fprintf(stderr, #_a " (%d) < " #_b "(%d)\n", _a, _b); \
295 assert_true(0 && "Integer comparison failed"); \
298 #define assert_in_range_print(_a, _min, _max) \
299 if (_a < _min || _a > _max) {\
300 fprintf(stderr, #_a " (%d) outside range " #_min "(%d) - " #_max "(%d)\n", \
301 _a, _min, _max); \
302 assert_true(0 && "Integer comparison failed"); \
305 #define assert_string_len_equal(_actual, _expected, _len) \
306 if (strncmp(_actual, _expected, _len) != 0) {\
307 fprintf(stderr, "Expected (" #_expected "): \"%.*s\"\n", (int)_len, _expected); \
308 fprintf(stderr, "Received (" #_actual "): \"%.*s\"\n", (int)_len, _actual); \
309 assert_true(0 && "String comparison failed"); \
312 static void
313 error_waffle(void)
315 const struct waffle_error_info *info = waffle_error_get_info();
316 const char *code = waffle_error_to_string(info->code);
318 if (info->message_length > 0)
319 fprintf(stderr, "Waffle error: 0x%x %s: %s\n", info->code, code, info->message);
320 else
321 fprintf(stderr, "Waffle error: 0x%x %s\n", info->code, code);
324 #define assert_true_with_wfl_error(_arg) \
325 if (!(_arg)) { \
326 error_waffle(); \
327 assert_true(0); \
330 static void
331 gl_basic_draw__(void **state, struct gl_basic_draw_args__ args)
333 struct test_state_gl_basic *ts = *state;
334 int32_t waffle_context_api = args.api;
335 int32_t context_version = args.version;
336 int32_t context_profile = args.profile;
337 int32_t expect_error = args.expect_error;
338 bool context_forward_compatible = args.forward_compatible;
339 bool context_debug = args.debug;
340 bool context_robust = args.robust;
341 bool alpha = args.alpha;
342 bool ret;
344 int32_t config_attrib_list[64];
345 int i;
347 const intptr_t window_attrib_list[] = {
348 WAFFLE_WINDOW_WIDTH, WINDOW_WIDTH,
349 WAFFLE_WINDOW_HEIGHT, WINDOW_HEIGHT,
353 i = 0;
354 config_attrib_list[i++] = WAFFLE_CONTEXT_API;
355 config_attrib_list[i++] = waffle_context_api;
356 if (context_version != WAFFLE_DONT_CARE) {
357 config_attrib_list[i++] = WAFFLE_CONTEXT_MAJOR_VERSION;
358 config_attrib_list[i++] = context_version / 10;
359 config_attrib_list[i++] = WAFFLE_CONTEXT_MINOR_VERSION;
360 config_attrib_list[i++] = context_version % 10;
362 if (context_profile != WAFFLE_DONT_CARE) {
363 config_attrib_list[i++] = WAFFLE_CONTEXT_PROFILE;
364 config_attrib_list[i++] = context_profile;
366 if (context_forward_compatible) {
367 config_attrib_list[i++] = WAFFLE_CONTEXT_FORWARD_COMPATIBLE;
368 config_attrib_list[i++] = true;
370 if (context_debug) {
371 config_attrib_list[i++] = WAFFLE_CONTEXT_DEBUG;
372 config_attrib_list[i++] = true;
374 if (context_robust) {
375 config_attrib_list[i++] = WAFFLE_CONTEXT_ROBUST_ACCESS;
376 config_attrib_list[i++] = true;
378 config_attrib_list[i++] = WAFFLE_RED_SIZE;
379 config_attrib_list[i++] = 8;
380 config_attrib_list[i++] = WAFFLE_GREEN_SIZE;
381 config_attrib_list[i++] = 8;
382 config_attrib_list[i++] = WAFFLE_BLUE_SIZE;
383 config_attrib_list[i++] = 8;
384 config_attrib_list[i++] = WAFFLE_ALPHA_SIZE;
385 config_attrib_list[i++] = alpha;
386 config_attrib_list[i++] = 0;
388 // Create objects.
389 ts->dpy = waffle_display_connect(NULL);
390 assert_true_with_wfl_error(ts->dpy);
392 ts->config = waffle_config_choose(ts->dpy, config_attrib_list);
393 if (expect_error) {
394 assert_true(ts->config == NULL);
395 assert_true(waffle_error_get_code() == expect_error);
396 return;
397 } else if (ts->config == NULL) {
398 switch (waffle_error_get_code()) {
399 case WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM:
400 // fall-through
401 case WAFFLE_ERROR_UNKNOWN:
402 // Assume that the native platform rejected the requested
403 // config flavor.
404 skip();
405 default:
406 assert_true_with_wfl_error(ts->config);
410 ts->window = waffle_window_create2(ts->config, window_attrib_list);
411 assert_true_with_wfl_error(ts->window);
413 ret = waffle_window_show(ts->window);
414 assert_true_with_wfl_error(ret);
416 ts->ctx = waffle_context_create(ts->config, NULL);
417 if (ts->ctx == NULL) {
418 switch (waffle_error_get_code()) {
419 case WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM:
420 // fall-through
421 case WAFFLE_ERROR_UNKNOWN:
422 // Assume that the native platform rejected the requested
423 // context flavor.
424 skip();
425 default:
426 assert_true_with_wfl_error(ts->ctx);
430 // Get OpenGL functions.
431 assert_true(glClear = get_gl_symbol(waffle_context_api, "glClear"));
432 assert_true(glClearColor = get_gl_symbol(waffle_context_api, "glClearColor"));
433 assert_true(glGetError = get_gl_symbol(waffle_context_api, "glGetError"));
434 assert_true(glGetIntegerv = get_gl_symbol(waffle_context_api, "glGetIntegerv"));
435 assert_true(glReadPixels = get_gl_symbol(waffle_context_api, "glReadPixels"));
436 assert_true(glGetString = get_gl_symbol(waffle_context_api, "glGetString"));
438 ret = waffle_make_current(ts->dpy, ts->window, ts->ctx);
439 assert_true_with_wfl_error(ret);
441 assert_true(waffle_get_current_display() == ts->dpy);
442 assert_true(waffle_get_current_window() == ts->window);
443 assert_true(waffle_get_current_context() == ts->ctx);
445 const char *version_str, *expected_version_str;
446 int major, minor, count;
448 ASSERT_GL(version_str = (const char *) glGetString(GL_VERSION));
449 assert_true(version_str != NULL);
451 switch (waffle_context_api) {
452 case WAFFLE_CONTEXT_OPENGL:
453 expected_version_str = "";
454 break;
455 case WAFFLE_CONTEXT_OPENGL_ES1:
456 expected_version_str = "OpenGL ES-CM ";
457 break;
458 case WAFFLE_CONTEXT_OPENGL_ES2:
459 case WAFFLE_CONTEXT_OPENGL_ES3:
460 expected_version_str = "OpenGL ES ";
461 break;
462 default:
463 assert_true(0);
464 break;
467 const size_t version_str_len = strlen(expected_version_str);
468 assert_string_len_equal(version_str, expected_version_str, version_str_len);
469 version_str += version_str_len;
471 count = sscanf(version_str, "%d.%d", &major, &minor);
472 assert_int_equal_print(count, 2);
473 assert_int_ge(major, 0);
474 assert_in_range_print(minor, 0, 10);
476 if (context_version != WAFFLE_DONT_CARE) {
477 int expected_major = context_version / 10;
478 int expected_minor = context_version % 10;
480 assert_int_ge(major, expected_major);
481 if (major == expected_major)
482 assert_int_ge(minor, expected_minor);
485 const char *profile_suffix = "";
487 if (waffle_context_api == WAFFLE_CONTEXT_OPENGL) {
488 switch (context_profile) {
489 case WAFFLE_CONTEXT_CORE_PROFILE:
490 profile_suffix = " (Core Profile)";
491 break;
492 case WAFFLE_CONTEXT_COMPATIBILITY_PROFILE:
493 profile_suffix = " (Compatibility Profile)";
494 break;
495 case WAFFLE_DONT_CARE:
496 break;
497 default:
498 assert_true(0);
499 break;
503 char profile_str[30]; // 30 should be enough ;-)
504 sprintf(profile_str, "%d.%d%s", major, minor, profile_suffix);
506 const size_t profile_str_len = strlen(profile_str);
507 assert_string_len_equal(version_str, profile_str, profile_str_len);
509 int version_10x = 10 * major + minor;
511 // Another profile check
512 if (waffle_context_api == WAFFLE_CONTEXT_OPENGL && version_10x >= 32) {
513 GLint profile_mask = 0;
514 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile_mask);
516 switch (context_profile) {
517 case WAFFLE_CONTEXT_CORE_PROFILE:
518 assert_true(profile_mask & GL_CONTEXT_CORE_PROFILE_BIT);
519 break;
520 case WAFFLE_CONTEXT_COMPATIBILITY_PROFILE:
521 assert_true(profile_mask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT);
522 case WAFFLE_DONT_CARE:
523 break;
524 default:
525 assert_true(0);
526 break;
530 if ((waffle_context_api == WAFFLE_CONTEXT_OPENGL && version_10x >= 30) ||
531 (waffle_context_api != WAFFLE_CONTEXT_OPENGL && version_10x >= 32)) {
532 GLint context_flags = 0;
533 if (context_forward_compatible || context_debug) {
534 glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags);
537 if (context_forward_compatible) {
538 assert_true(context_flags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT);
541 if (context_debug) {
542 assert_true(context_flags & GL_CONTEXT_FLAG_DEBUG_BIT);
546 // GL_ROBUST_ACCESS comes with the following extensions
547 // GL_ARB_robustness
548 // GL_EXT_robustness
549 // GL_KHR_robustness
551 // To keep it simple, assume the correct extension is there if
552 // glGetError is happy ;-)
553 if (context_robust) {
554 GLint robust_flag = 0;
555 glGetIntegerv(GL_CONTEXT_ROBUST_ACCESS, &robust_flag);
557 if (glGetError() == GL_NO_ERROR)
558 assert_true(robust_flag);
561 // Draw.
562 ASSERT_GL(glClearColor(RED_F, GREEN_F, BLUE_F, ALPHA_F));
563 ASSERT_GL(glClear(GL_COLOR_BUFFER_BIT));
564 ASSERT_GL(glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
565 GL_RGBA, GL_UNSIGNED_BYTE,
566 ts->actual_pixels));
567 ret = waffle_window_swap_buffers(ts->window);
568 assert_true_with_wfl_error(ret);
570 assert_memory_equal(&ts->actual_pixels, &ts->expect_pixels,
571 sizeof(ts->expect_pixels));
575 // List of tests common to all platforms.
578 #define test_XX_rgb(context_api, waffle_api, error) \
579 static void test_gl_basic_##context_api##_rgb(void **state) \
581 gl_basic_draw(state, \
582 .api=WAFFLE_CONTEXT_##waffle_api, \
583 .expect_error=WAFFLE_##error); \
586 #define test_XX_rgba(context_api, waffle_api, error) \
587 static void test_gl_basic_##context_api##_rgba(void **state) \
589 gl_basic_draw(state, \
590 .api=WAFFLE_CONTEXT_##waffle_api, \
591 .alpha=true, \
592 .expect_error=WAFFLE_##error); \
595 #define test_XX_fwdcompat(context_api, waffle_api, error) \
596 static void test_gl_basic_##context_api##_fwdcompat(void **state) \
598 gl_basic_draw(state, \
599 .api=WAFFLE_CONTEXT_##waffle_api, \
600 .forward_compatible=true, \
601 .expect_error=WAFFLE_##error); \
604 #define test_XX_debug(context_api, waffle_api, error) \
605 static void test_gl_basic_##context_api##_debug(void **state) \
607 gl_basic_draw(state, \
608 .api=WAFFLE_CONTEXT_##waffle_api, \
609 .debug=true, \
610 .expect_error=WAFFLE_##error); \
613 #define test_XX_robust(context_api, waffle_api, error) \
614 static void test_gl_basic_##context_api##_robust(void **state) \
616 gl_basic_draw(state, \
617 .api=WAFFLE_CONTEXT_##waffle_api, \
618 .robust=true, \
619 .expect_error=WAFFLE_##error); \
622 #define test_glXX(waffle_version, error) \
623 static void test_gl_basic_gl##waffle_version(void **state) \
625 gl_basic_draw(state, \
626 .api=WAFFLE_CONTEXT_OPENGL, \
627 .version=waffle_version, \
628 .expect_error=WAFFLE_##error); \
631 #define test_glXX_fwdcompat(waffle_version, error) \
632 static void test_gl_basic_gl##waffle_version##_fwdcompat(void **state) \
634 gl_basic_draw(state, \
635 .api=WAFFLE_CONTEXT_OPENGL, \
636 .version=waffle_version, \
637 .forward_compatible=true, \
638 .expect_error=WAFFLE_##error); \
641 #define test_glXX_core(waffle_version, error) \
642 static void test_gl_basic_gl##waffle_version##_core(void **state) \
644 gl_basic_draw(state, \
645 .api=WAFFLE_CONTEXT_OPENGL, \
646 .version=waffle_version, \
647 .profile=WAFFLE_CONTEXT_CORE_PROFILE, \
648 .expect_error=WAFFLE_##error); \
651 #define test_glXX_core_fwdcompat(waffle_version, error) \
652 static void test_gl_basic_gl##waffle_version##_core_fwdcompat(void **state) \
654 gl_basic_draw(state, \
655 .api=WAFFLE_CONTEXT_OPENGL, \
656 .version=waffle_version, \
657 .profile=WAFFLE_CONTEXT_CORE_PROFILE, \
658 .forward_compatible=true, \
659 .expect_error=WAFFLE_##error); \
662 #define test_glXX_core_debug(waffle_version, error) \
663 static void test_gl_basic_gl##waffle_version##_core_debug(void **state) \
665 gl_basic_draw(state, \
666 .api=WAFFLE_CONTEXT_OPENGL, \
667 .version=waffle_version, \
668 .profile=WAFFLE_CONTEXT_CORE_PROFILE, \
669 .debug=true, \
670 .expect_error=WAFFLE_##error); \
673 #define test_glXX_core_robust(waffle_version, error) \
674 static void test_gl_basic_gl##waffle_version##_core_robust(void **state) \
676 gl_basic_draw(state, \
677 .api=WAFFLE_CONTEXT_OPENGL, \
678 .version=waffle_version, \
679 .profile=WAFFLE_CONTEXT_CORE_PROFILE, \
680 .robust=true, \
681 .expect_error=WAFFLE_##error); \
684 #define test_glXX_compat(waffle_version, error) \
685 static void test_gl_basic_gl##waffle_version##_compat(void **state) \
687 gl_basic_draw(state, \
688 .api=WAFFLE_CONTEXT_OPENGL, \
689 .version=waffle_version, \
690 .profile=WAFFLE_CONTEXT_COMPATIBILITY_PROFILE, \
691 .expect_error=WAFFLE_##error); \
694 #define test_glXX_compat_debug(waffle_version, error) \
695 static void test_gl_basic_gl##waffle_version##_compat_debug(void **state) \
697 gl_basic_draw(state, \
698 .api=WAFFLE_CONTEXT_OPENGL, \
699 .version=waffle_version, \
700 .profile=WAFFLE_CONTEXT_COMPATIBILITY_PROFILE, \
701 .debug=true, \
702 .expect_error=WAFFLE_##error); \
705 #define test_glXX_compat_robust(waffle_version, error) \
706 static void test_gl_basic_gl##waffle_version##_compat_robust(void **state) \
708 gl_basic_draw(state, \
709 .api=WAFFLE_CONTEXT_OPENGL, \
710 .version=waffle_version, \
711 .profile=WAFFLE_CONTEXT_COMPATIBILITY_PROFILE, \
712 .robust=true, \
713 .expect_error=WAFFLE_##error); \
716 #define test_glesXX(api_version, waffle_version, error) \
717 static void test_gl_basic_gles##waffle_version(void **state) \
719 gl_basic_draw(state, \
720 .api=WAFFLE_CONTEXT_OPENGL_ES##api_version, \
721 .version=waffle_version, \
722 .expect_error=WAFFLE_##error); \
726 #define CREATE_TESTSUITE(waffle_platform, platform) \
728 static int \
729 setup_##platform(void **state) \
731 return gl_basic_init(state, waffle_platform); \
734 static int \
735 testsuite_##platform(void) \
737 const struct CMUnitTest tests[] = { \
739 unit_test_make(test_gl_basic_gl_rgb), \
740 unit_test_make(test_gl_basic_gl_rgba), \
741 unit_test_make(test_gl_basic_gl_fwdcompat), \
742 unit_test_make(test_gl_basic_gl_debug), \
743 unit_test_make(test_gl_basic_gl_robust), \
745 unit_test_make(test_gl_basic_gl10), \
746 unit_test_make(test_gl_basic_gl11), \
747 unit_test_make(test_gl_basic_gl12), \
748 unit_test_make(test_gl_basic_gl13), \
749 unit_test_make(test_gl_basic_gl14), \
750 unit_test_make(test_gl_basic_gl15), \
751 unit_test_make(test_gl_basic_gl20), \
752 unit_test_make(test_gl_basic_gl21), \
753 unit_test_make(test_gl_basic_gl21_fwdcompat), \
755 unit_test_make(test_gl_basic_gl30), \
756 unit_test_make(test_gl_basic_gl30_fwdcompat), \
757 unit_test_make(test_gl_basic_gl31), \
758 unit_test_make(test_gl_basic_gl31_fwdcompat), \
760 unit_test_make(test_gl_basic_gl32_core), \
761 unit_test_make(test_gl_basic_gl32_core_fwdcompat), \
762 unit_test_make(test_gl_basic_gl32_core_debug), \
763 unit_test_make(test_gl_basic_gl32_core_robust), \
764 unit_test_make(test_gl_basic_gl33_core), \
765 unit_test_make(test_gl_basic_gl40_core), \
766 unit_test_make(test_gl_basic_gl41_core), \
767 unit_test_make(test_gl_basic_gl42_core), \
768 unit_test_make(test_gl_basic_gl43_core), \
770 unit_test_make(test_gl_basic_gl32_compat), \
771 unit_test_make(test_gl_basic_gl32_compat_debug), \
772 unit_test_make(test_gl_basic_gl32_compat_robust), \
773 unit_test_make(test_gl_basic_gl33_compat), \
774 unit_test_make(test_gl_basic_gl40_compat), \
775 unit_test_make(test_gl_basic_gl41_compat), \
776 unit_test_make(test_gl_basic_gl42_compat), \
777 unit_test_make(test_gl_basic_gl43_compat), \
779 unit_test_make(test_gl_basic_gles1_rgb), \
780 unit_test_make(test_gl_basic_gles1_rgba), \
781 unit_test_make(test_gl_basic_gles1_fwdcompat), \
782 unit_test_make(test_gl_basic_gles1_robust), \
783 unit_test_make(test_gl_basic_gles1_debug), \
784 unit_test_make(test_gl_basic_gles10), \
785 unit_test_make(test_gl_basic_gles11), \
787 unit_test_make(test_gl_basic_gles2_rgb), \
788 unit_test_make(test_gl_basic_gles2_rgba), \
789 unit_test_make(test_gl_basic_gles2_fwdcompat), \
790 unit_test_make(test_gl_basic_gles2_debug), \
791 unit_test_make(test_gl_basic_gles2_robust), \
792 unit_test_make(test_gl_basic_gles20), \
794 unit_test_make(test_gl_basic_gles3_rgb), \
795 unit_test_make(test_gl_basic_gles3_rgba), \
796 unit_test_make(test_gl_basic_gles3_fwdcompat), \
797 unit_test_make(test_gl_basic_gles3_debug), \
798 unit_test_make(test_gl_basic_gles3_robust), \
799 unit_test_make(test_gl_basic_gles30), \
801 }; \
803 return cmocka_run_group_tests_name(#platform, tests, NULL, NULL); \
807 // Most of the following tests will return ERROR_UNSUPPORTED_ON_PLATFORM
808 // on Apple/CGL, where NO_ERROR is expected.
809 // This is safe, as the test is skipped when the said error occurs.
811 // As BAD_ATTRIBUTE (core validation) takes greater precedence over
812 // UNSUPPORTED_ON_PLATFORM (platform specific one).
813 // Thus we're safe to use the former here, eventhough CGL has support
814 // for neither GLES* nor fwdcompa "CGL and everyone else".
816 test_XX_rgb(gl, OPENGL, NO_ERROR)
817 test_XX_rgba(gl, OPENGL, NO_ERROR)
818 test_XX_fwdcompat(gl, OPENGL, ERROR_BAD_ATTRIBUTE)
819 test_XX_debug(gl, OPENGL, NO_ERROR)
820 test_XX_robust(gl, OPENGL, NO_ERROR)
822 test_glXX(10, NO_ERROR)
823 test_glXX(11, NO_ERROR)
824 test_glXX(12, NO_ERROR)
825 test_glXX(13, NO_ERROR)
826 test_glXX(14, NO_ERROR)
827 test_glXX(15, NO_ERROR)
828 test_glXX(20, NO_ERROR)
829 test_glXX(21, NO_ERROR)
830 test_glXX_fwdcompat(21, ERROR_BAD_ATTRIBUTE)
831 test_glXX(30, NO_ERROR)
832 test_glXX_fwdcompat(30, NO_ERROR)
833 test_glXX(31, NO_ERROR)
834 test_glXX_fwdcompat(31, NO_ERROR)
836 test_glXX_core(32, NO_ERROR)
837 test_glXX_core_fwdcompat(32, NO_ERROR)
838 test_glXX_core_debug(32, NO_ERROR)
839 test_glXX_core_robust(32, NO_ERROR)
840 test_glXX_core(33, NO_ERROR)
841 test_glXX_core(40, NO_ERROR)
842 test_glXX_core(41, NO_ERROR)
843 test_glXX_core(42, NO_ERROR)
844 test_glXX_core(43, NO_ERROR)
846 test_glXX_compat(32, NO_ERROR)
847 test_glXX_compat_debug(32, NO_ERROR)
848 test_glXX_compat_robust(32, NO_ERROR)
849 test_glXX_compat(33, NO_ERROR)
850 test_glXX_compat(40, NO_ERROR)
851 test_glXX_compat(41, NO_ERROR)
852 test_glXX_compat(42, NO_ERROR)
853 test_glXX_compat(43, NO_ERROR)
855 test_XX_rgb(gles1, OPENGL_ES1, NO_ERROR)
856 test_XX_rgba(gles1, OPENGL_ES1, NO_ERROR)
857 test_XX_fwdcompat(gles1, OPENGL_ES1, ERROR_BAD_ATTRIBUTE)
858 test_XX_debug(gles1, OPENGL_ES1, NO_ERROR)
859 test_XX_robust(gles1, OPENGL_ES1, NO_ERROR)
860 test_glesXX(1, 10, NO_ERROR)
861 test_glesXX(1, 11, NO_ERROR)
863 test_XX_rgb(gles2, OPENGL_ES2, NO_ERROR)
864 test_XX_rgba(gles2, OPENGL_ES2, NO_ERROR)
865 test_XX_fwdcompat(gles2, OPENGL_ES2, ERROR_BAD_ATTRIBUTE)
866 test_XX_debug(gles2, OPENGL_ES2, NO_ERROR)
867 test_XX_robust(gles2, OPENGL_ES2, NO_ERROR)
868 test_glesXX(2, 20, NO_ERROR)
870 test_XX_rgb(gles3, OPENGL_ES3, NO_ERROR)
871 test_XX_rgba(gles3, OPENGL_ES3, NO_ERROR)
872 test_XX_fwdcompat(gles3, OPENGL_ES3, ERROR_BAD_ATTRIBUTE)
873 test_XX_debug(gles3, OPENGL_ES3, NO_ERROR)
874 test_XX_robust(gles3, OPENGL_ES3, NO_ERROR)
875 test_glesXX(3, 30, NO_ERROR)
878 #ifdef WAFFLE_HAS_CGL
880 #define unit_test_make(name) \
881 cmocka_unit_test_setup_teardown(name, setup_cgl, gl_basic_fini)
883 CREATE_TESTSUITE(WAFFLE_PLATFORM_CGL, cgl)
885 #undef unit_test_make
887 #endif // WAFFLE_HAS_CGL
889 #ifdef WAFFLE_HAS_GBM
891 #define unit_test_make(name) \
892 cmocka_unit_test_setup_teardown(name, setup_gbm, gl_basic_fini)
894 CREATE_TESTSUITE(WAFFLE_PLATFORM_GBM, gbm)
896 #undef unit_test_make
898 #endif // WAFFLE_HAS_GBM
900 #ifdef WAFFLE_HAS_GLX
902 #define unit_test_make(name) \
903 cmocka_unit_test_setup_teardown(name, setup_glx, gl_basic_fini)
905 CREATE_TESTSUITE(WAFFLE_PLATFORM_GLX, glx)
907 #undef unit_test_make
909 #endif // WAFFLE_HAS_GLX
911 #ifdef WAFFLE_HAS_SURFACELESS_EGL
913 #define unit_test_make(name) \
914 cmocka_unit_test_setup_teardown(name, setup_surfaceless_egl, gl_basic_fini)
916 CREATE_TESTSUITE(WAFFLE_PLATFORM_SURFACELESS_EGL, surfaceless_egl)
918 #undef unit_test_make
920 #endif // WAFFLE_HAS_SURFACELESS_EGL
922 #ifdef WAFFLE_HAS_WAYLAND
924 #define unit_test_make(name) \
925 cmocka_unit_test_setup_teardown(name, setup_wayland, gl_basic_fini)
927 CREATE_TESTSUITE(WAFFLE_PLATFORM_WAYLAND, wayland)
929 #undef unit_test_make
931 #endif // WAFFLE_HAS_WAYLAND
933 #ifdef WAFFLE_HAS_X11_EGL
935 #define unit_test_make(name) \
936 cmocka_unit_test_setup_teardown(name, setup_x11_egl, gl_basic_fini)
938 CREATE_TESTSUITE(WAFFLE_PLATFORM_X11_EGL, x11_egl)
940 #undef unit_test_make
942 #endif // WAFFLE_HAS_X11_EGL
944 #ifdef WAFFLE_HAS_WGL
946 #define unit_test_make(name) \
947 cmocka_unit_test_setup_teardown(name, setup_wgl, gl_basic_fini)
949 CREATE_TESTSUITE(WAFFLE_PLATFORM_WGL, wgl)
951 #undef unit_test_make
953 #endif // WAFFLE_HAS_WGL
955 #undef test_glesXX
957 #undef test_glXX_compat_robust
958 #undef test_glXX_compat_debug
959 #undef test_glXX_compat
960 #undef test_glXX_core_robust
961 #undef test_glXX_core_debug
962 #undef test_glXX_core_fwdcompat
963 #undef test_glXX_core
964 #undef test_glXX_fwdcompat
965 #undef test_glXX
967 #undef test_XX_robust
968 #undef test_XX_debug
969 #undef test_XX_fwdcompat
970 #undef test_XX_rgba
971 #undef test_XX_rgb
973 #ifdef __APPLE__
975 static void
976 removeArg(int index, int *argc, char **argv)
978 --*argc;
979 for (; index < *argc; ++index)
980 argv[index] = argv[index + 1];
983 static void
984 removeXcodeArgs(int *argc, char **argv)
986 // Xcode sometimes adds additional arguments.
987 for (int i = 1; i < *argc; )
989 if (strcmp(argv[i], "-NSDocumentRevisionsDebugMode") == 0 ||
990 strcmp(argv[i], "-ApplePersistenceIgnoreState" ) == 0)
992 removeArg(i, argc, argv);
993 removeArg(i, argc, argv);
994 } else
995 ++i;
999 #endif // __APPLE__
1001 static const char *usage_message =
1002 "Usage:\n"
1003 " gl_basic_test <Required Parameter> [Options]\n"
1004 "\n"
1005 "Description:\n"
1006 " Run the basic functionality tests.\n"
1007 "\n"
1008 "Required Parameter:\n"
1009 " -p, --platform\n"
1010 " One of: cgl, gbm, glx, wayland, wgl or x11_egl\n"
1011 "\n"
1012 "Options:\n"
1013 " -h, --help\n"
1014 " Print gl_basic_test usage information.\n"
1017 #if defined(__GNUC__)
1018 #define NORETURN __attribute__((noreturn))
1019 #elif defined(_MSC_VER)
1020 #define NORETURN __declspec(noreturn)
1021 #else
1022 #define NORETURN
1023 #endif
1025 static void NORETURN
1026 write_usage_and_exit(FILE *f, int exit_code)
1028 fprintf(f, "%s", usage_message);
1029 exit(exit_code);
1032 enum {
1033 OPT_PLATFORM = 'p',
1034 OPT_HELP = 'h',
1037 static const struct option get_opts[] = {
1038 { .name = "platform", .has_arg = required_argument, .val = OPT_PLATFORM },
1039 { .name = "help", .has_arg = no_argument, .val = OPT_HELP },
1042 static void NORETURN
1043 usage_error_printf(const char *fmt, ...)
1045 fprintf(stderr, "gl_basic_test usage error: ");
1047 if (fmt) {
1048 va_list ap;
1049 va_start(ap, fmt);
1050 vfprintf(stderr, fmt, ap);
1051 va_end(ap);
1052 fprintf(stderr, " ");
1055 fprintf(stderr, "(see gl_basic_test --help)\n");
1056 exit(EXIT_FAILURE);
1059 struct enum_map {
1060 int i;
1061 const char *s;
1064 static const struct enum_map platform_map[] = {
1065 {WAFFLE_PLATFORM_CGL, "cgl", },
1066 {WAFFLE_PLATFORM_GBM, "gbm" },
1067 {WAFFLE_PLATFORM_GLX, "glx" },
1068 {WAFFLE_PLATFORM_WAYLAND, "wayland" },
1069 {WAFFLE_PLATFORM_WGL, "wgl" },
1070 {WAFFLE_PLATFORM_X11_EGL, "x11_egl" },
1071 {WAFFLE_PLATFORM_SURFACELESS_EGL, "surfaceless_egl" },
1072 {WAFFLE_PLATFORM_SURFACELESS_EGL, "sl" },
1073 {0, 0 },
1076 /// @brief Translate string to `enum waffle_enum`.
1078 /// @param self is a list of map items. The last item must be zero-filled.
1079 /// @param result is altered only if @a s if found.
1080 /// @return true if @a s was found in @a map.
1081 static bool
1082 enum_map_translate_str(
1083 const struct enum_map *self,
1084 const char *s,
1085 int *result)
1087 for (const struct enum_map *i = self; i->i != 0; ++i) {
1088 if (!strncmp(s, i->s, strlen(i->s) + 1)) {
1089 *result = i->i;
1090 return true;
1094 return false;
1097 /// @return true on success.
1098 static bool
1099 parse_args(int argc, char *argv[], int *out_platform)
1101 bool ok;
1102 bool loop_get_opt = true;
1103 int platform = 0;
1105 #ifdef __APPLE__
1106 removeXcodeArgs(&argc, argv);
1107 #endif
1109 // prevent getopt_long from printing an error message
1110 opterr = 0;
1112 while (loop_get_opt) {
1113 int opt = getopt_long(argc, argv, "hp:", get_opts, NULL);
1114 switch (opt) {
1115 case -1:
1116 loop_get_opt = false;
1117 break;
1118 case '?':
1119 goto error_unrecognized_arg;
1120 case OPT_PLATFORM:
1121 ok = enum_map_translate_str(platform_map, optarg, &platform);
1122 if (!ok) {
1123 usage_error_printf("'%s' is not a valid platform",
1124 optarg);
1126 break;
1127 case OPT_HELP:
1128 write_usage_and_exit(stdout, EXIT_SUCCESS);
1129 break;
1130 default:
1131 loop_get_opt = false;
1132 break;
1136 if (optind < argc) {
1137 goto error_unrecognized_arg;
1140 if (!platform) {
1141 usage_error_printf("--platform is required");
1144 *out_platform = platform;
1145 return true;
1147 error_unrecognized_arg:
1148 if (optarg)
1149 usage_error_printf("unrecognized option '%s'", optarg);
1150 else if (optopt)
1151 usage_error_printf("unrecognized option '-%c'", optopt);
1152 else
1153 usage_error_printf("unrecognized option");
1157 main(int argc, char *argv[])
1159 int platform;
1160 bool ok;
1162 ok = parse_args(argc, argv, &platform);
1163 if (!ok)
1164 exit(EXIT_FAILURE);
1166 switch (platform) {
1167 #ifdef WAFFLE_HAS_CGL
1168 case WAFFLE_PLATFORM_CGL:
1169 return testsuite_cgl();
1170 #endif
1171 #ifdef WAFFLE_HAS_GBM
1172 case WAFFLE_PLATFORM_GBM:
1173 return testsuite_gbm();
1174 #endif
1175 #ifdef WAFFLE_HAS_GLX
1176 case WAFFLE_PLATFORM_GLX:
1177 return testsuite_glx();
1178 #endif
1179 #ifdef WAFFLE_HAS_SURFACELESS_EGL
1180 case WAFFLE_PLATFORM_SURFACELESS_EGL:
1181 return testsuite_surfaceless_egl();
1182 #endif
1183 #ifdef WAFFLE_HAS_WAYLAND
1184 case WAFFLE_PLATFORM_WAYLAND:
1185 return testsuite_wayland();
1186 #endif
1187 #ifdef WAFFLE_HAS_WGL
1188 case WAFFLE_PLATFORM_WGL:
1189 return testsuite_wgl();
1190 #endif
1191 #ifdef WAFFLE_HAS_X11_EGL
1192 case WAFFLE_PLATFORM_X11_EGL:
1193 return testsuite_x11_egl();
1194 #endif
1195 default:
1196 abort();
1197 break;
1200 return 0;