Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / xdemos / glsync.c
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/glu.h>
54 #include <GL/glx.h>
55 #include <GL/glxext.h>
56 #include <X11/X.h>
57 #include <X11/Xlib.h>
58 #include <X11/Xutil.h>
59
60 void (*video_sync_get)();
61 void (*video_sync)();
62 void (*swap_interval)();
63
64 static int GLXExtensionSupported(Display *dpy, const char *extension)
65 {
66 const char *extensionsString, *client_extensions, *pos;
67
68 extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
69 client_extensions = glXGetClientString(dpy, GLX_EXTENSIONS);
70
71 pos = strstr(extensionsString, extension);
72
73 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
74 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
75 return 1;
76
77 pos = strstr(client_extensions, extension);
78
79 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
80 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
81 return 1;
82
83 return 0;
84 }
85
86 extern char *optarg;
87 extern int optind, opterr, optopt;
88 static char optstr[] = "w:h:s:vi:";
89
90 enum sync_type {
91 none = 0,
92 sgi_video_sync,
93 buffer_swap
94 };
95
96 static void usage(char *name)
97 {
98 printf("usage: %s [-w <width>] [-h <height>] [-s<sync method>] "
99 "[-v]\n", name);
100 printf("\t-s<sync method>:\n");
101 printf("\t\tn: none\n");
102 printf("\t\ts: SGI video sync extension\n");
103 printf("\t\tb: buffer swap\n");
104 printf("\t-i<swap interval>\n");
105 printf("\t-v: verbose (print count)\n");
106 exit(-1);
107 }
108
109 int main(int argc, char *argv[])
110 {
111 Display *disp;
112 XVisualInfo *pvi;
113 XSetWindowAttributes swa;
114 GLint last_val = -1, count = 0;
115 Window winGL;
116 GLXContext context;
117 int dummy;
118 Atom wmDelete;
119 enum sync_type waitforsync = none;
120 int width = 500, height = 500, verbose = 0, interval = 1;
121 int c, i = 1;
122 int ret;
123 int attribs[] = { GLX_RGBA,
124 GLX_RED_SIZE, 1,
125 GLX_GREEN_SIZE, 1,
126 GLX_BLUE_SIZE, 1,
127 None };
128 int db_attribs[] = { GLX_RGBA,
129 GLX_RED_SIZE, 1,
130 GLX_GREEN_SIZE, 1,
131 GLX_BLUE_SIZE, 1,
132 GLX_DOUBLEBUFFER,
133 GLX_DEPTH_SIZE, 1,
134 None };
135 XSizeHints sizehints;
136
137 opterr = 0;
138 while ((c = getopt(argc, argv, optstr)) != -1) {
139 switch (c) {
140 case 'w':
141 width = atoi(optarg);
142 break;
143 case 'h':
144 height = atoi(optarg);
145 break;
146 case 's':
147 switch (optarg[0]) {
148 case 'n':
149 waitforsync = none;
150 break;
151 case 's':
152 waitforsync = sgi_video_sync;
153 break;
154 case 'b':
155 waitforsync = buffer_swap;
156 break;
157 default:
158 usage(argv[0]);
159 break;
160 }
161 break;
162 case 'v':
163 verbose = 1;
164 break;
165 case 'i':
166 interval = atoi(optarg);
167 break;
168 default:
169 usage(argv[0]);
170 break;
171 }
172 }
173
174 disp = XOpenDisplay(NULL);
175 if (!disp) {
176 fprintf(stderr, "failed to open display\n");
177 return -1;
178 }
179
180 if (!glXQueryExtension(disp, &dummy, &dummy)) {
181 fprintf(stderr, "glXQueryExtension failed\n");
182 return -1;
183 }
184
185 if (!GLXExtensionSupported(disp, "GLX_SGI_video_sync")) {
186 fprintf(stderr, "GLX_SGI_video_sync not supported, exiting\n");
187 return -1;
188 }
189
190 if (waitforsync != buffer_swap) {
191 pvi = glXChooseVisual(disp, DefaultScreen(disp), attribs);
192 } else {
193 pvi = glXChooseVisual(disp, DefaultScreen(disp), db_attribs);
194 }
195
196 if (!pvi) {
197 fprintf(stderr, "failed to choose visual, exiting\n");
198 return -1;
199 }
200
201 pvi->screen = DefaultScreen(disp);
202
203 swa.colormap = XCreateColormap(disp, RootWindow(disp, pvi->screen),
204 pvi->visual, AllocNone);
205 swa.border_pixel = 0;
206 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
207 StructureNotifyMask;
208 winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
209 0, 0,
210 width, height,
211 0, pvi->depth, InputOutput, pvi->visual,
212 CWBorderPixel | CWColormap | CWEventMask, &swa);
213 if (!winGL) {
214 fprintf(stderr, "window creation failed\n");
215 return -1;
216 }
217 wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True);
218 XSetWMProtocols(disp, winGL, &wmDelete, 1);
219
220 sizehints.x = 0;
221 sizehints.y = 0;
222 sizehints.width = width;
223 sizehints.height = height;
224 sizehints.flags = USSize | USPosition;
225
226 XSetNormalHints(disp, winGL, &sizehints);
227 XSetStandardProperties(disp, winGL, "glsync test", "glsync text",
228 None, NULL, 0, &sizehints);
229
230 context = glXCreateContext(disp, pvi, NULL, GL_TRUE);
231 if (!context) {
232 fprintf(stderr, "failed to create glx context\n");
233 return -1;
234 }
235
236 XMapWindow(disp, winGL);
237 ret = glXMakeCurrent(disp, winGL, context);
238 if (ret) {
239 fprintf(stderr, "failed to make context current: %d\n", ret);
240 }
241
242 video_sync_get = glXGetProcAddress((unsigned char *)"glXGetVideoSyncSGI");
243 video_sync = glXGetProcAddress((unsigned char *)"glXWaitVideoSyncSGI");
244
245 swap_interval = glXGetProcAddress((unsigned char *)"glXSwapIntervalSGI");
246
247 if (!video_sync_get || !video_sync || !swap_interval) {
248 fprintf(stderr, "failed to get sync functions\n");
249 return -1;
250 }
251
252 if (waitforsync == buffer_swap) {
253 swap_interval(interval);
254 fprintf(stderr, "set swap interval to %d\n", interval);
255 }
256 video_sync_get(&count);
257 count++;
258 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
259 while (i++) {
260 /* Alternate colors to make tearing obvious */
261 if (i & 1) {
262 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
263 glColor3f(1.0f, 1.0f, 1.0f);
264 } else {
265 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
266 glColor3f(1.0f, 0.0f, 0.0f);
267 }
268
269 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
270 glRectf(0, 0, width, height);
271
272 /* Wait for vsync */
273 if (waitforsync == sgi_video_sync) {
274 if (verbose)
275 fprintf(stderr, "waiting on count %d\n", count);
276 video_sync(2, (count + 1) % 2, &count);
277 if (count < last_val)
278 fprintf(stderr, "error: vblank count went backwards: %d -> %d\n", last_val, count);
279 if (count == last_val)
280 fprintf(stderr, "error: count didn't change: %d\n", count);
281 last_val = count;
282 glFlush();
283 } else if (waitforsync == buffer_swap) {
284 glXSwapBuffers(disp, winGL);
285 } else {
286 video_sync_get(&count);
287 sleep(1);
288 glFinish();
289 }
290
291 if (verbose) {
292 video_sync_get(&count);
293 fprintf(stderr, "current count: %d\n", count);
294 }
295 }
296
297 XDestroyWindow(disp, winGL);
298 glXDestroyContext(disp, context);
299 XCloseDisplay(disp);
300
301 return 0;
302 }