glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / util / piglit-framework-gl.c
blobc804418700e200a9cd8941dd266200f13bda7ed2
1 /*
2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 #include <assert.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
31 #include "piglit-util-gl.h"
32 #include "piglit-framework-gl/piglit_gl_framework.h"
34 struct piglit_gl_framework *gl_fw;
36 const char *piglit_binary_name;
37 bool piglit_dump_png = false;
38 bool piglit_use_fbo = false;
39 bool piglit_khr_no_error = false;
40 int piglit_automatic = 0;
41 unsigned piglit_winsys_fbo = 0;
43 int piglit_width;
44 int piglit_height;
46 static void
47 process_args(int *argc, char *argv[], unsigned *force_samples,
48 struct piglit_gl_test_config *config);
50 bool
51 piglit_gl_test_config_override_size(struct piglit_gl_test_config *config)
53 const char *default_size;
54 unsigned int width;
55 unsigned int height;
57 default_size = getenv("PIGLIT_DEFAULT_SIZE");
58 if (!default_size)
59 return false;
61 if (sscanf(default_size, "%ux%u", &width, &height) != 2)
62 return false;
64 if (width == 0 || height == 0)
65 return false;
67 config->window_width = width;
68 config->window_height = height;
69 return true;
72 void
73 piglit_gl_test_config_init(struct piglit_gl_test_config *config)
75 memset(config, 0, sizeof(*config));
77 if (!piglit_gl_test_config_override_size(config)) {
78 /* Default window size. Note: Win8's min window width */
79 /* seems to be 160 pixels. When the window size is */
80 /* unexpectedly resized, tests are marked as "WARN". */
81 /* Let's use a larger default to avoid that. */
82 config->window_width = 160;
83 config->window_height = 160;
86 config->khr_no_error_support = PIGLIT_UNKNOWN_ERROR_STATUS;
89 static void
90 delete_arg(char *argv[], int *argc, int arg)
92 int i;
94 for (i = arg + 1; i < *argc; i++) {
95 argv[i - 1] = argv[i];
97 (*argc)--;
101 * Recognized arguments are removed from @a argv. The updated array
102 * length is returned in @a argc.
104 static void
105 process_args(int *argc, char *argv[], unsigned *force_samples,
106 struct piglit_gl_test_config *config)
108 int j;
110 piglit_binary_name = argv[0];
112 piglit_parse_subtest_args(argc, argv, config->subtests,
113 &config->selected_subtests,
114 &config->num_selected_subtests);
116 /* Find/remove "-auto" and "-fbo" from the argument vector.
118 for (j = 1; j < *argc; j++) {
119 if (!strcmp(argv[j], "-auto")) {
120 piglit_automatic = 1;
121 delete_arg(argv, argc, j--);
122 } else if (!strcmp(argv[j], "-fbo")) {
123 piglit_use_fbo = true;
124 delete_arg(argv, argc, j--);
125 } else if (!strcmp(argv[j], "-png")) {
126 piglit_dump_png = true;
127 delete_arg(argv, argc, j--);
128 } else if (!strcmp(argv[j], "-rlimit")) {
129 char *ptr;
130 unsigned long lim;
131 int i;
133 j++;
134 if (j >= *argc) {
135 fprintf(stderr,
136 "-rlimit requires an argument\n");
137 piglit_report_result(PIGLIT_FAIL);
140 lim = strtoul(argv[j], &ptr, 0);
141 if (ptr == argv[j]) {
142 fprintf(stderr,
143 "-rlimit requires an argument\n");
144 piglit_report_result(PIGLIT_FAIL);
147 piglit_set_rlimit(lim);
149 /* Remove 2 arguments (hence the 'i - 2') from the
150 * command line.
152 for (i = j + 1; i < *argc; i++) {
153 argv[i - 2] = argv[i];
155 *argc -= 2;
156 j -= 2;
157 } else if (!strncmp(argv[j], "-samples=", 9)) {
158 *force_samples = atoi(argv[j]+9);
159 delete_arg(argv, argc, j--);
160 } else if (!strcmp(argv[j], "-khr_no_error")) {
161 piglit_khr_no_error = true;
162 delete_arg(argv, argc, j--);
163 if (config->khr_no_error_support ==
164 PIGLIT_UNKNOWN_ERROR_STATUS) {
165 fprintf(stderr,
166 "khr_no_error_support unknown "
167 "skipping test!\n");
168 piglit_report_result(PIGLIT_SKIP);
169 } else if (config->khr_no_error_support ==
170 PIGLIT_HAS_ERRORS) {
171 piglit_report_result(PIGLIT_SKIP);
172 } else {
173 assert(config->khr_no_error_support ==
174 PIGLIT_NO_ERRORS);
176 } else if (!strcmp(argv[j], "-compat")) {
177 if (config->supports_gl_es_version) {
178 fprintf(stderr,
179 "-compat isn't allowed with ES tests!\n");
180 piglit_report_result(PIGLIT_FAIL);
182 config->supports_gl_compat_version = 10;
183 config->supports_gl_core_version = 0;
184 puts("The compatibility profile forced.");
185 delete_arg(argv, argc, j--);
190 void
191 piglit_gl_process_args(int *argc, char *argv[],
192 struct piglit_gl_test_config *config)
194 unsigned force_samples = 0;
196 process_args(argc, argv, &force_samples, config);
198 if (force_samples > 1)
199 config->window_samples = force_samples;
203 static void
204 destroy(void)
206 if (!gl_fw)
207 return;
209 if (gl_fw->destroy)
210 gl_fw->destroy(gl_fw);
211 gl_fw = NULL;
214 void
215 piglit_gl_test_run(int argc, char *argv[],
216 const struct piglit_gl_test_config *config)
218 piglit_width = config->window_width;
219 piglit_height = config->window_height;
221 gl_fw = piglit_gl_framework_factory(config);
222 if (gl_fw == NULL) {
223 printf("piglit: error: failed to create "
224 "piglit_gl_framework\n");
225 piglit_report_result(PIGLIT_FAIL);
228 atexit(destroy);
229 gl_fw->run_test(gl_fw, argc, argv);
230 assert(false);
233 void
234 piglit_post_redisplay(void)
236 if (gl_fw->post_redisplay)
237 gl_fw->post_redisplay(gl_fw);
240 void
241 piglit_set_keyboard_func(void (*func)(unsigned char key, int x, int y))
243 if (gl_fw->set_keyboard_func)
244 gl_fw->set_keyboard_func(gl_fw, func);
247 void
248 piglit_swap_buffers(void)
250 if (gl_fw->swap_buffers)
251 gl_fw->swap_buffers(gl_fw);
254 void
255 piglit_present_results(void)
257 if (piglit_dump_png) {
258 static char *fileprefix = NULL;
259 static int frame = 0;
260 char *filename;
261 GLenum base_format = GL_RGBA;
262 GLubyte *image;
263 if (fileprefix == NULL) {
264 int i;
265 fileprefix = strdup(piglit_binary_name);
266 fileprefix = basename(fileprefix);
267 /* Strip potentially bad characters */
268 for (i = 0; fileprefix[i]; i++) {
269 if (!isalnum(fileprefix[i]) && fileprefix[i] != '-')
270 fileprefix[i] = '_';
273 image = malloc(4 * piglit_width * piglit_height);
274 glReadPixels(0, 0, piglit_width, piglit_height,
275 base_format, GL_UNSIGNED_BYTE, image);
276 assert(glGetError() == GL_NO_ERROR);
278 (void)!asprintf(&filename, "%s%03d.png", fileprefix, frame++);
280 printf("Writing %s...\n", filename);
281 piglit_write_png(filename, base_format, piglit_width,
282 piglit_height, image, true);
283 free(filename);
284 free(image);
287 if (!piglit_automatic)
288 piglit_swap_buffers();
291 void
292 piglit_set_reshape_func(void (*func)(int w, int h))
294 if (!gl_fw->set_reshape_func)
295 gl_fw->set_reshape_func(gl_fw, func);
298 enum piglit_result
299 piglit_create_dma_buf(unsigned w, unsigned h, unsigned fourcc,
300 const void *src_data, struct piglit_dma_buf **buf)
302 if (!gl_fw->create_dma_buf)
303 return PIGLIT_SKIP;
305 return gl_fw->create_dma_buf(w, h, fourcc, src_data, buf);
308 void
309 piglit_destroy_dma_buf(struct piglit_dma_buf *buf)
311 if (gl_fw->destroy_dma_buf)
312 gl_fw->destroy_dma_buf(buf);
315 size_t
316 piglit_get_selected_tests(const char ***selected_subtests)
318 *selected_subtests = gl_fw->test_config->selected_subtests;
319 return gl_fw->test_config->num_selected_subtests;