2 * Copyright © 2007-2010 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 Bool (*glXGetSyncValuesOML
)(Display
*dpy
, GLXDrawable drawable
,
61 int64_t *ust
, int64_t *msc
, int64_t *sbc
);
62 Bool (*glXGetMscRateOML
)(Display
*dpy
, GLXDrawable drawable
, int32_t *numerator
,
63 int32_t *denominator
);
64 int64_t (*glXSwapBuffersMscOML
)(Display
*dpy
, GLXDrawable drawable
,
65 int64_t target_msc
, int64_t divisor
,
67 Bool (*glXWaitForMscOML
)(Display
*dpy
, GLXDrawable drawable
, int64_t target_msc
,
68 int64_t divisor
, int64_t remainder
, int64_t *ust
,
69 int64_t *msc
, int64_t *sbc
);
70 Bool (*glXWaitForSbcOML
)(Display
*dpy
, GLXDrawable drawable
, int64_t target_sbc
,
71 int64_t *ust
, int64_t *msc
, int64_t *sbc
);
73 static int GLXExtensionSupported(Display
*dpy
, const char *extension
)
75 const char *extensionsString
, *client_extensions
, *pos
;
77 extensionsString
= glXQueryExtensionsString(dpy
, DefaultScreen(dpy
));
78 client_extensions
= glXGetClientString(dpy
, GLX_EXTENSIONS
);
80 pos
= strstr(extensionsString
, extension
);
82 if (pos
!= NULL
&& (pos
== extensionsString
|| pos
[-1] == ' ') &&
83 (pos
[strlen(extension
)] == ' ' || pos
[strlen(extension
)] == '\0'))
86 pos
= strstr(client_extensions
, extension
);
88 if (pos
!= NULL
&& (pos
== extensionsString
|| pos
[-1] == ' ') &&
89 (pos
[strlen(extension
)] == ' ' || pos
[strlen(extension
)] == '\0'))
96 extern int optind
, opterr
, optopt
;
97 static char optstr
[] = "w:h:vd:r:n:";
99 static void usage(char *name
)
101 printf("usage: %s [-w <width>] [-h <height>] [-i<swap interval>] "
102 "[-n<wait interval>] [-v]\n", name
);
103 printf("\t-i<swap interval> - swap at most once every n frames\n");
104 printf("\t-n<wait interval> - wait n frames between swaps\n");
105 printf("\t-v: verbose (print count)\n");
109 int main(int argc
, char *argv
[])
113 XSetWindowAttributes swa
;
118 int width
= 500, height
= 500, verbose
= 0, divisor
= 0, remainder
= 0,
122 int db_attribs
[] = { GLX_RGBA
,
129 XSizeHints sizehints
;
132 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
135 width
= atoi(optarg
);
138 height
= atoi(optarg
);
144 divisor
= atoi(optarg
);
147 remainder
= atoi(optarg
);
150 wait_interval
= atoi(optarg
);
158 disp
= XOpenDisplay(NULL
);
160 fprintf(stderr
, "failed to open display\n");
164 if (!glXQueryExtension(disp
, &dummy
, &dummy
)) {
165 fprintf(stderr
, "glXQueryExtension failed\n");
169 if (!GLXExtensionSupported(disp
, "GLX_OML_sync_control")) {
170 fprintf(stderr
, "GLX_OML_sync_control not supported\n");
174 pvi
= glXChooseVisual(disp
, DefaultScreen(disp
), db_attribs
);
177 fprintf(stderr
, "failed to choose visual, exiting\n");
181 pvi
->screen
= DefaultScreen(disp
);
183 swa
.colormap
= XCreateColormap(disp
, RootWindow(disp
, pvi
->screen
),
184 pvi
->visual
, AllocNone
);
185 swa
.border_pixel
= 0;
186 swa
.event_mask
= ExposureMask
| KeyPressMask
| ButtonPressMask
|
188 winGL
= XCreateWindow(disp
, RootWindow(disp
, pvi
->screen
),
191 0, pvi
->depth
, InputOutput
, pvi
->visual
,
192 CWBorderPixel
| CWColormap
| CWEventMask
, &swa
);
194 fprintf(stderr
, "window creation failed\n");
197 wmDelete
= XInternAtom(disp
, "WM_DELETE_WINDOW", True
);
198 XSetWMProtocols(disp
, winGL
, &wmDelete
, 1);
202 sizehints
.width
= width
;
203 sizehints
.height
= height
;
204 sizehints
.flags
= USSize
| USPosition
;
206 XSetNormalHints(disp
, winGL
, &sizehints
);
207 XSetStandardProperties(disp
, winGL
, "glsync test", "glsync text",
208 None
, NULL
, 0, &sizehints
);
210 context
= glXCreateContext(disp
, pvi
, NULL
, GL_TRUE
);
212 fprintf(stderr
, "failed to create glx context\n");
216 XMapWindow(disp
, winGL
);
217 ret
= glXMakeCurrent(disp
, winGL
, context
);
219 fprintf(stderr
, "failed to make context current: %d\n", ret
);
222 glXGetSyncValuesOML
= (void *)glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
223 glXGetMscRateOML
= (void *)glXGetProcAddress((unsigned char *)"glXGetMscRateOML");
224 glXSwapBuffersMscOML
= (void *)glXGetProcAddress((unsigned char *)"glXSwapBuffersMscOML");
225 glXWaitForMscOML
= (void *)glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
226 glXWaitForSbcOML
= (void *)glXGetProcAddress((unsigned char *)"glXWaitForSbcOML");
228 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
230 /* Alternate colors to make tearing obvious */
232 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
233 glColor3f(1.0f
, 1.0f
, 1.0f
);
235 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
236 glColor3f(1.0f
, 0.0f
, 0.0f
);
239 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
240 glRectf(0, 0, width
, height
);
242 glXSwapBuffersMscOML(disp
, winGL
, 0, divisor
, remainder
);
245 XDestroyWindow(disp
, winGL
);
246 glXDestroyContext(disp
, context
);