glsync.c revision 32001f49
1/*
2 * Copyright © 2007 Intel Corporation
3 *
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:
10 *
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.
14 *
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.
22 *
23 * Authors:
24 *    Jesse Barnes <jesse.barnes@intel.com>
25 *
26 */
27
28/** @file glsync.c
29 * The program is simple:  it paints a window alternating colors (red &
30 * white) either as fast as possible or synchronized to vblank events
31 *
32 * If run normally, the program should display a window that exhibits
33 * significant tearing between red and white colors (e.g. you might get
34 * a "waterfall" effect of red and white horizontal bars).
35 *
36 * If run with the '-s b' option, the program should synchronize the
37 * window color changes with the vertical blank period, resulting in a
38 * window that looks orangish with a high frequency flicker (which may
39 * be invisible).  If the window is moved to another screen, this
40 * property should be preserved.  If the window spans two screens, it
41 * shouldn't tear on whichever screen most of the window is on; the
42 * portion on the other screen may show some tearing (like the
43 * waterfall effect above).
44 *
45 * Other options include '-w <width>' and '-h <height' to set the
46 * window size.
47 */
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include <GL/gl.h>
53#include <GL/glx.h>
54#include <GL/glxext.h>
55#include <X11/X.h>
56#include <X11/Xlib.h>
57#include <X11/Xutil.h>
58
59static PFNGLXGETVIDEOSYNCSGIPROC video_sync_get;
60static PFNGLXSWAPINTERVALSGIPROC swap_interval;
61static PFNGLXWAITVIDEOSYNCSGIPROC video_sync;
62
63
64static int GLXExtensionSupported(Display *dpy, const char *extension)
65{
66	const char *extensionsString, *pos;
67
68	extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
69
70	pos = strstr(extensionsString, extension);
71
72	if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
73	    (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
74		return 1;
75
76	return 0;
77}
78
79extern char *optarg;
80extern int optind, opterr, optopt;
81static char optstr[] = "w:h:s:vi:";
82
83enum sync_type {
84	none = 0,
85	sgi_video_sync,
86	buffer_swap
87};
88
89static void usage(char *name)
90{
91	printf("usage: %s [-w <width>] [-h <height>] [-s<sync method>] "
92	       "[-v]\n", name);
93	printf("\t-s<sync method>:\n");
94	printf("\t\tn: none\n");
95	printf("\t\ts: SGI video sync extension\n");
96	printf("\t\tb: buffer swap\n");
97	printf("\t-i<swap interval>\n");
98	printf("\t-v: verbose (print count)\n");
99	exit(-1);
100}
101
102int main(int argc, char *argv[])
103{
104	Display *disp;
105	XVisualInfo *pvi;
106	XSetWindowAttributes swa;
107	GLint last_val = -1;
108        unsigned int count = 0;
109	Window winGL;
110	GLXContext context;
111	int dummy;
112	Atom wmDelete;
113	enum sync_type waitforsync = none;
114	int width = 500, height = 500, verbose = 0, interval = 1;
115	int c, i = 1;
116	int ret;
117	int attribs[] = { GLX_RGBA,
118                     GLX_RED_SIZE, 1,
119                     GLX_GREEN_SIZE, 1,
120                     GLX_BLUE_SIZE, 1,
121                     None };
122	int db_attribs[] = { GLX_RGBA,
123                     GLX_RED_SIZE, 1,
124                     GLX_GREEN_SIZE, 1,
125                     GLX_BLUE_SIZE, 1,
126                     GLX_DOUBLEBUFFER,
127                     GLX_DEPTH_SIZE, 1,
128                     None };
129	XSizeHints sizehints;
130
131	opterr = 0;
132	while ((c = getopt(argc, argv, optstr)) != -1) {
133		switch (c) {
134		case 'w':
135			width = atoi(optarg);
136			break;
137		case 'h':
138			height = atoi(optarg);
139			break;
140		case 's':
141			switch (optarg[0]) {
142			case 'n':
143				waitforsync = none;
144				break;
145			case 's':
146				waitforsync = sgi_video_sync;
147				break;
148			case 'b':
149				waitforsync = buffer_swap;
150				break;
151			default:
152				usage(argv[0]);
153				break;
154			}
155			break;
156		case 'v':
157			verbose = 1;
158			break;
159		case 'i':
160			interval = atoi(optarg);
161			break;
162		default:
163			usage(argv[0]);
164			break;
165		}
166	}
167
168	disp = XOpenDisplay(NULL);
169	if (!disp) {
170		fprintf(stderr, "failed to open display\n");
171		return -1;
172	}
173
174	if (!glXQueryExtension(disp, &dummy, &dummy)) {
175		fprintf(stderr, "glXQueryExtension failed\n");
176		return -1;
177	}
178
179	if (!GLXExtensionSupported(disp, "GLX_SGI_video_sync")) {
180		fprintf(stderr, "GLX_SGI_video_sync not supported, exiting\n");
181		return -1;
182	}
183
184	if (waitforsync != buffer_swap) {
185		pvi = glXChooseVisual(disp, DefaultScreen(disp), attribs);
186	} else {
187		pvi = glXChooseVisual(disp, DefaultScreen(disp), db_attribs);
188	}
189
190	if (!pvi) {
191		fprintf(stderr, "failed to choose visual, exiting\n");
192		return -1;
193	}
194
195	pvi->screen = DefaultScreen(disp);
196
197	swa.colormap = XCreateColormap(disp, RootWindow(disp, pvi->screen),
198				       pvi->visual, AllocNone);
199	swa.border_pixel = 0;
200	swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
201		StructureNotifyMask;
202	winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
203			      0, 0,
204			      width, height,
205			      0, pvi->depth, InputOutput, pvi->visual,
206			      CWBorderPixel | CWColormap | CWEventMask, &swa);
207	if (!winGL) {
208		fprintf(stderr, "window creation failed\n");
209		return -1;
210	}
211        wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True);
212        XSetWMProtocols(disp, winGL, &wmDelete, 1);
213
214	sizehints.x = 0;
215	sizehints.y = 0;
216	sizehints.width  = width;
217	sizehints.height = height;
218	sizehints.flags = USSize | USPosition;
219
220	XSetNormalHints(disp, winGL, &sizehints);
221	XSetStandardProperties(disp, winGL, "glsync test", "glsync text",
222			       None, NULL, 0, &sizehints);
223
224	context = glXCreateContext(disp, pvi, NULL, GL_TRUE);
225	if (!context) {
226		fprintf(stderr, "failed to create glx context\n");
227		return -1;
228	}
229
230	XMapWindow(disp, winGL);
231	ret = glXMakeCurrent(disp, winGL, context);
232	if (!ret) {
233		fprintf(stderr, "failed to make context current: %d\n", ret);
234	}
235
236	video_sync_get = (PFNGLXGETVIDEOSYNCSGIPROC) glXGetProcAddress((unsigned char *)"glXGetVideoSyncSGI");
237	video_sync = (PFNGLXWAITVIDEOSYNCSGIPROC) glXGetProcAddress((unsigned char *)"glXWaitVideoSyncSGI");
238
239	swap_interval = (PFNGLXSWAPINTERVALSGIPROC) glXGetProcAddress((unsigned char *)"glXSwapIntervalSGI");
240
241	if (!video_sync_get || !video_sync || !swap_interval) {
242		fprintf(stderr, "failed to get sync functions\n");
243		return -1;
244	}
245
246	if (waitforsync == buffer_swap) {
247		swap_interval(interval);
248		fprintf(stderr, "set swap interval to %d\n", interval);
249	}
250	video_sync_get(&count);
251	count++;
252	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253	while (i++) {
254		/* Alternate colors to make tearing obvious */
255		if (i & 1) {
256			glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
257			glColor3f(1.0f, 1.0f, 1.0f);
258		} else {
259			glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
260			glColor3f(1.0f, 0.0f, 0.0f);
261		}
262
263		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
264		glRectf(0, 0, width, height);
265
266		/* Wait for vsync */
267		if (waitforsync == sgi_video_sync) {
268			if (verbose)
269				fprintf(stderr, "waiting on count %d\n", count);
270			video_sync(2, (count + 1) % 2, &count);
271			if (count < last_val)
272				fprintf(stderr, "error:  vblank count went backwards: %d -> %d\n", last_val, count);
273			if (count == last_val)
274				fprintf(stderr, "error:  count didn't change: %d\n", count);
275			last_val = count;
276			glFlush();
277		} else if (waitforsync == buffer_swap) {
278			glXSwapBuffers(disp, winGL);
279		} else {
280			video_sync_get(&count);
281			sleep(1);
282			glFinish();
283		}
284
285		if (verbose) {
286			video_sync_get(&count);
287			fprintf(stderr, "current count: %d\n", count);
288		}
289	}
290
291	XDestroyWindow(disp, winGL);
292	glXDestroyContext(disp, context);
293	XCloseDisplay(disp);
294
295	return 0;
296}
297