2 * Copyright © 2007 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
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
24 * Jesse Barnes <jesse.barnes@intel.com>
29 * The program is simple: it paints a window alternating colors (red &
30 * white) either as fast as possible or synchronized to vblank events
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).
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).
45 * Other options include '-w <width>' and '-h <height' to set the
55 #include <GL/glxext.h>
58 #include <X11/Xutil.h>
60 void (*video_sync_get
)();
62 void (*swap_interval
)();
64 static int GLXExtensionSupported(Display
*dpy
, const char *extension
)
66 const char *extensionsString
, *pos
;
68 extensionsString
= glXQueryExtensionsString(dpy
, DefaultScreen(dpy
));
70 pos
= strstr(extensionsString
, extension
);
72 if (pos
!= NULL
&& (pos
== extensionsString
|| pos
[-1] == ' ') &&
73 (pos
[strlen(extension
)] == ' ' || pos
[strlen(extension
)] == '\0'))
80 extern int optind
, opterr
, optopt
;
81 static char optstr
[] = "w:h:s:vi:";
89 static void usage(char *name
)
91 printf("usage: %s [-w <width>] [-h <height>] [-s<sync method>] "
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");
102 int main(int argc
, char *argv
[])
106 XSetWindowAttributes swa
;
107 GLint last_val
= -1, count
= 0;
112 enum sync_type waitforsync
= none
;
113 int width
= 500, height
= 500, verbose
= 0, interval
= 1;
116 int attribs
[] = { GLX_RGBA
,
121 int db_attribs
[] = { GLX_RGBA
,
128 XSizeHints sizehints
;
131 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
134 width
= atoi(optarg
);
137 height
= atoi(optarg
);
145 waitforsync
= sgi_video_sync
;
148 waitforsync
= buffer_swap
;
159 interval
= atoi(optarg
);
167 disp
= XOpenDisplay(NULL
);
169 fprintf(stderr
, "failed to open display\n");
173 if (!glXQueryExtension(disp
, &dummy
, &dummy
)) {
174 fprintf(stderr
, "glXQueryExtension failed\n");
178 if (!GLXExtensionSupported(disp
, "GLX_SGI_video_sync")) {
179 fprintf(stderr
, "GLX_SGI_video_sync not supported, exiting\n");
183 if (waitforsync
!= buffer_swap
) {
184 pvi
= glXChooseVisual(disp
, DefaultScreen(disp
), attribs
);
186 pvi
= glXChooseVisual(disp
, DefaultScreen(disp
), db_attribs
);
190 fprintf(stderr
, "failed to choose visual, exiting\n");
194 pvi
->screen
= DefaultScreen(disp
);
196 swa
.colormap
= XCreateColormap(disp
, RootWindow(disp
, pvi
->screen
),
197 pvi
->visual
, AllocNone
);
198 swa
.border_pixel
= 0;
199 swa
.event_mask
= ExposureMask
| KeyPressMask
| ButtonPressMask
|
201 winGL
= XCreateWindow(disp
, RootWindow(disp
, pvi
->screen
),
204 0, pvi
->depth
, InputOutput
, pvi
->visual
,
205 CWBorderPixel
| CWColormap
| CWEventMask
, &swa
);
207 fprintf(stderr
, "window creation failed\n");
210 wmDelete
= XInternAtom(disp
, "WM_DELETE_WINDOW", True
);
211 XSetWMProtocols(disp
, winGL
, &wmDelete
, 1);
215 sizehints
.width
= width
;
216 sizehints
.height
= height
;
217 sizehints
.flags
= USSize
| USPosition
;
219 XSetNormalHints(disp
, winGL
, &sizehints
);
220 XSetStandardProperties(disp
, winGL
, "glsync test", "glsync text",
221 None
, NULL
, 0, &sizehints
);
223 context
= glXCreateContext(disp
, pvi
, NULL
, GL_TRUE
);
225 fprintf(stderr
, "failed to create glx context\n");
229 XMapWindow(disp
, winGL
);
230 ret
= glXMakeCurrent(disp
, winGL
, context
);
232 fprintf(stderr
, "failed to make context current: %d\n", ret
);
235 video_sync_get
= glXGetProcAddress((unsigned char *)"glXGetVideoSyncSGI");
236 video_sync
= glXGetProcAddress((unsigned char *)"glXWaitVideoSyncSGI");
238 swap_interval
= glXGetProcAddress((unsigned char *)"glXSwapIntervalSGI");
240 if (!video_sync_get
|| !video_sync
|| !swap_interval
) {
241 fprintf(stderr
, "failed to get sync functions\n");
245 if (waitforsync
== buffer_swap
) {
246 swap_interval(interval
);
247 fprintf(stderr
, "set swap interval to %d\n", interval
);
249 video_sync_get(&count
);
251 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
253 /* Alternate colors to make tearing obvious */
255 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
256 glColor3f(1.0f
, 1.0f
, 1.0f
);
258 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
259 glColor3f(1.0f
, 0.0f
, 0.0f
);
262 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
263 glRectf(0, 0, width
, height
);
266 if (waitforsync
== sgi_video_sync
) {
268 fprintf(stderr
, "waiting on count %d\n", count
);
269 video_sync(2, (count
+ 1) % 2, &count
);
270 if (count
< last_val
)
271 fprintf(stderr
, "error: vblank count went backwards: %d -> %d\n", last_val
, count
);
272 if (count
== last_val
)
273 fprintf(stderr
, "error: count didn't change: %d\n", count
);
276 } else if (waitforsync
== buffer_swap
) {
277 glXSwapBuffers(disp
, winGL
);
279 video_sync_get(&count
);
285 video_sync_get(&count
);
286 fprintf(stderr
, "current count: %d\n", count
);
290 XDestroyWindow(disp
, winGL
);
291 glXDestroyContext(disp
, context
);