summaryrefslogtreecommitdiff
path: root/tests/glx/glx-fbconfig-sanity.c
blob: 9af89bf925320d1a2b700d2a75972166dddf02aa (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
/*
 * Copyright © 2011 Intel Corporation
 *
 * 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 (including the next
 * paragraph) 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.
 */

/** @file glx-fbconfig-sanity.c
 * Verify that the list of fbconfigs conforms to GLX 1.4 section 3.3.3.
 *
 * This reproduces X.org bugzilla #34265, among much else.
 */

#include "piglit-util-gl.h"
#include "piglit-glx-util.h"

int piglit_width = 10;
int piglit_height = 10;

static PFNGLXGETFBCONFIGSPROC GetFBConfigs = NULL;
static PFNGLXGETFBCONFIGATTRIBPROC GetFBConfigAttrib = NULL;
static PFNGLXGETVISUALFROMFBCONFIGPROC GetVisualFromFBConfig = NULL;

static void
fbconfig_sanity_warn(int *result)
{
	if (*result != PIGLIT_FAIL)
		*result = PIGLIT_WARN;
}

int
main(int argc, char **argv)
{
	Display *dpy;
	int i;
	int result = PIGLIT_PASS;
	GLXFBConfig *configs;
	int num_configs;

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL) {
		fprintf(stderr, "couldn't open display\n");
		piglit_report_result(PIGLIT_FAIL);
	}

	/* Test requires at least GLX version 1.3.  Otherwise there is no
	 * glXGetFBConfigs function.
	 */
	piglit_require_glx_version(dpy, 1, 3);
	piglit_require_glx_extension(dpy, "GLX_ARB_get_proc_address");

	GetFBConfigs = (PFNGLXGETFBCONFIGSPROC)
		glXGetProcAddressARB((GLubyte *) "glXGetFBConfigs");
	GetFBConfigAttrib = (PFNGLXGETFBCONFIGATTRIBPROC)
		glXGetProcAddressARB((GLubyte *) "glXGetFBConfigAttrib");
	GetVisualFromFBConfig = (PFNGLXGETVISUALFROMFBCONFIGPROC)
		glXGetProcAddressARB((GLubyte *) "glXGetVisualFromFBConfig");

	configs = GetFBConfigs(dpy, DefaultScreen(dpy), &num_configs);

	/* Iterate over the list of fbconfigs.  Check that each fbconfig that
	 * has the GLX_WINDOW_BIT or GLX_PIXMAP_BIT set also has a non-zero
	 * X visual.  Also verify that glXGetVisualFromFBConfig returns NULL
	 * if and only if the X visual is 0.
	 */
	for (i = 0; i < num_configs; i++) {
		int draw_type;
		int visual_id;
		int config_id;
		int sample_buffers;
		int render_type;
		int x_renderable;
		int caveat;
		int vtype;
		int transparency;
		XVisualInfo *vinfo;

		GetFBConfigAttrib(dpy, configs[i], GLX_FBCONFIG_ID,
				  &config_id);
		GetFBConfigAttrib(dpy, configs[i], GLX_DRAWABLE_TYPE,
				  &draw_type);
		GetFBConfigAttrib(dpy, configs[i], GLX_VISUAL_ID,
				  &visual_id);
		GetFBConfigAttrib(dpy, configs[i], GLX_SAMPLE_BUFFERS,
				  &sample_buffers);
		GetFBConfigAttrib(dpy, configs[i], GLX_RENDER_TYPE,
				  &render_type);
		GetFBConfigAttrib(dpy, configs[i], GLX_X_RENDERABLE,
				  &x_renderable);
		GetFBConfigAttrib(dpy, configs[i], GLX_CONFIG_CAVEAT,
				  &caveat);
		GetFBConfigAttrib(dpy, configs[i], GLX_TRANSPARENT_TYPE,
				  &transparency);
		GetFBConfigAttrib(dpy, configs[i], GLX_X_VISUAL_TYPE,
				  &vtype);

		if (!draw_type) {
			fprintf(stderr, "FBConfig 0x%x supports no "
				"drawables\n", config_id);
			fbconfig_sanity_warn(&result);
		}

		if ((draw_type & GLX_WINDOW_BIT) != 0
		    && visual_id == 0) {
			fprintf(stderr, "FBconfig 0x%x has GLX_WINDOW_BIT "
				"set, but the Visual ID is 0!\n",
				config_id);
			result = PIGLIT_FAIL;
		}

		GetFBConfigAttrib(dpy, configs[i], GLX_TRANSPARENT_TYPE,
				  &transparency);
		vinfo = GetVisualFromFBConfig(dpy, configs[i]);
		if ((vinfo == NULL) != (visual_id == 0)) {
			fprintf(stderr, "FBconfig 0x%x has vinfo = %p and "
				"visual ID = 0x%x.  Both or neither must be "
				"NULL / zero.\n",
				config_id, vinfo, visual_id);
			result = PIGLIT_FAIL;
		}

		if (vinfo != NULL && vinfo->visualid != visual_id) {
			fprintf(stderr, "FBconfig 0x%x has "
				"vinfo->visualid = 0x%x and visual ID = 0x%x. "
				"These should match!\n",
				config_id, (int) vinfo->visualid, visual_id);
			result = PIGLIT_FAIL;
		}

		if (vinfo) {
			int depth;
			GetFBConfigAttrib(dpy, configs[i], GLX_BUFFER_SIZE,
					  &depth);
			if (vinfo->class == StaticColor ||
			    vinfo->class == PseudoColor) {
				if (depth != vinfo->depth) {
					fprintf(stderr, "FBConfig 0x%x has "
						"depth %d but visual 0x%x has "
						"depth %d.  These should "
						"match!\n", config_id, depth,
						(int)vinfo->visualid,
						vinfo->depth);
					result = PIGLIT_FAIL;
				}
			}
			if (vinfo->class == TrueColor ||
			    vinfo->class == DirectColor) {
				if (depth < vinfo->depth) {
					fprintf(stderr, "FBConfig 0x%x has "
						"depth %d < visual 0x%x depth "
						"%d.  Should be >= visual "
						"depth!\n",
						config_id, depth,
						(int)vinfo->visualid,
						vinfo->depth);
					result = PIGLIT_FAIL;
				}
			}
			if (vtype == GLX_NONE) {
				fprintf(stderr, "FBConfig 0x%x supports "
					"windows but has no visual type\n",
					config_id);
				result = PIGLIT_FAIL;
			} else {
				int fail = 0;
				int ci_fail = 0;
				switch (vinfo->class) {
					case TrueColor:
						if (vtype != GLX_TRUE_COLOR)
							fail = 1;
						if (render_type &
						    GLX_COLOR_INDEX_BIT)
							ci_fail = 1;
						break;
					case DirectColor:
						if (vtype != GLX_DIRECT_COLOR)
							fail = 1;
						if (render_type &
						    GLX_COLOR_INDEX_BIT)
							ci_fail = 1;
						break;
					case PseudoColor:
						if (vtype != GLX_PSEUDO_COLOR)
							fail = 1;
						break;
					case StaticColor:
						if (vtype != GLX_STATIC_COLOR)
							fail = 1;
						break;
					case GrayScale:
						if (vtype != GLX_GRAY_SCALE)
							fail = 1;
						break;
					case StaticGray:
						if (vtype != GLX_STATIC_GRAY)
							fail = 1;
						break;
					default:
						fail = 2;
						break;
				}
				if (ci_fail) {
					fprintf(stderr, "FBConfig 0x%x is "
						"{True,Direct}Color but claims "
						"support for color-index\n",
						config_id);
					result = PIGLIT_FAIL;
				}
				if (fail == 2) {
					fprintf(stderr, "FBConfig 0x%x has "
						"visual with unknown class "
						"%d\n", config_id,
						vinfo->class);
					result = PIGLIT_FAIL;
				} else if (fail == 1) {
					fprintf(stderr, "FBConfig 0x%x claims "
						"visual class that does not "
						"match visual 0x%x\n",
						config_id,
						(int)vinfo->visualid);
					result = PIGLIT_FAIL;
				}
			}
		}

		if (sample_buffers == 0) {
			int samples;
			GetFBConfigAttrib(dpy, configs[i],
					  GLX_SAMPLES, &samples);
			if (samples != 0) {
				fprintf(stderr, "FBConfig 0x%x has "
					"0 sample buffers but %d "
					"samples, should be 0\n",
					config_id, samples);
				result = PIGLIT_FAIL;
			}
		} else if (sample_buffers == 1) {
			/* TODO check color/depth/stencil bits per sample */
		} else {
			fprintf(stderr, "FBConfig 0x%x has bizarre "
				"GLX_SAMPLE_BUFFERS of %d, should be "
				"0 or 1\n", config_id, sample_buffers);
			result = PIGLIT_FAIL;
		}

		if (render_type == 0) {
			fprintf(stderr, "FBConfig 0x%x can be bound to "
				"neither RGBA nor color-index contexts\n",
				config_id);
			result = PIGLIT_FAIL;
		} else if
		    (render_type & ~(GLX_RGBA_BIT | GLX_COLOR_INDEX_BIT)) {
			fprintf(stderr, "FBConfig 0x%x supports rendering "
				"to something other than RGBA or CI, "
				"piglit needs to be fixed\n", config_id);
			fbconfig_sanity_warn(&result);
		}

		if (x_renderable &&
		    !(draw_type & (GLX_WINDOW_BIT | GLX_PIXMAP_BIT))) {
			fprintf(stderr, "FBConfig 0x%x claims to be X "
				"renderable (0x%x), but does not support "
				"windows or pixmaps\n", config_id, draw_type);
			fbconfig_sanity_warn(&result);
		} else if (!x_renderable &&
			   (draw_type & (GLX_WINDOW_BIT | GLX_PIXMAP_BIT))) {
			fprintf(stderr, "FBConfig 0x%x claims to not be X "
				"renderable but claims to support windows "
				"and/or pixmaps\n", config_id);
			result = PIGLIT_FAIL;
		}

		switch (caveat) {
			case GLX_NONE:
			case GLX_SLOW_CONFIG:
			case GLX_NON_CONFORMANT_CONFIG:
				break;
			default:
				fprintf(stderr, "FBConfig 0x%x has unknown "
					"caveat 0x%x\n", config_id, caveat);
				result = PIGLIT_FAIL;
				break;
		}

		switch (transparency) {
			case GLX_NONE:
				break;
			case GLX_TRANSPARENT_RGB:
				if (vtype != GLX_TRUE_COLOR &&
				    vtype != GLX_DIRECT_COLOR) {
					fprintf(stderr, "FBConfig 0x%x is rgb "
						"transparent but not an rgb "
						"visual type\n", config_id);
					result = PIGLIT_FAIL;
				}
				break;
			case GLX_TRANSPARENT_INDEX:
				if (vtype == GLX_TRUE_COLOR ||
				    vtype == GLX_DIRECT_COLOR) {
					fprintf(stderr, "FBConfig 0x%x is ci "
						"transparent but not a ci "
						"visual type\n", config_id);
					result = PIGLIT_FAIL;
				}
				break;
			default:
				fprintf(stderr, "FBConfig 0x%x has unknown "
					"transparency type 0x%x\n", config_id,
					transparency);
				result = PIGLIT_FAIL;
				break;
		}
	}

	piglit_report_result(result);
	return 0;
}