2 * Copyright © 2009 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 * Simple test for MSC functionality.
38 #include <GL/glxext.h>
41 #include <X11/Xutil.h>
43 void (*get_sync_values
)(Display
*dpy
, Window winGL
, int64_t *ust
, int64_t *msc
, int64_t *sbc
);
44 void (*wait_sync
)(Display
*dpy
, Window winGL
, int64_t target_msc
, int64_t divisor
, int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
);
46 static int GLXExtensionSupported(Display
*dpy
, const char *extension
)
48 const char *extensionsString
, *pos
;
50 extensionsString
= glXQueryExtensionsString(dpy
, DefaultScreen(dpy
));
52 pos
= strstr(extensionsString
, extension
);
54 if (pos
!= NULL
&& (pos
== extensionsString
|| pos
[-1] == ' ') &&
55 (pos
[strlen(extension
)] == ' ' || pos
[strlen(extension
)] == '\0'))
62 extern int optind
, opterr
, optopt
;
63 static char optstr
[] = "v";
65 static void usage(char *name
)
67 printf("usage: %s\n", name
);
71 int main(int argc
, char *argv
[])
75 XSetWindowAttributes swa
;
81 int verbose
= 0, width
= 200, height
= 200;
83 int64_t ust
, msc
, sbc
;
86 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
97 disp
= XOpenDisplay(NULL
);
99 fprintf(stderr
, "failed to open display\n");
103 if (!glXQueryExtension(disp
, &dummy
, &dummy
)) {
104 fprintf(stderr
, "glXQueryExtension failed\n");
108 if (!GLXExtensionSupported(disp
, "GLX_OML_sync_control")) {
109 fprintf(stderr
, "GLX_OML_sync_control not supported, exiting\n");
113 attrib
[0] = GLX_RGBA
;
115 attrib
[2] = GLX_RED_SIZE
;
117 attrib
[4] = GLX_GREEN_SIZE
;
119 attrib
[6] = GLX_BLUE_SIZE
;
121 attrib
[8] = GLX_DOUBLEBUFFER
;
125 pvi
= glXChooseVisual(disp
, DefaultScreen(disp
), attrib
);
127 fprintf(stderr
, "failed to choose visual, exiting\n");
131 context
= glXCreateContext(disp
, pvi
, None
, GL_TRUE
);
133 fprintf(stderr
, "failed to create glx context\n");
137 pvi
->screen
= DefaultScreen(disp
);
139 swa
.colormap
= XCreateColormap(disp
, RootWindow(disp
, pvi
->screen
),
140 pvi
->visual
, AllocNone
);
141 swa
.border_pixel
= 0;
142 swa
.event_mask
= ExposureMask
| KeyPressMask
| ButtonPressMask
|
144 winGL
= XCreateWindow(disp
, RootWindow(disp
, pvi
->screen
),
147 0, pvi
->depth
, InputOutput
, pvi
->visual
,
148 CWBorderPixel
| CWColormap
| CWEventMask
, &swa
);
150 fprintf(stderr
, "window creation failed\n");
153 wmDelete
= XInternAtom(disp
, "WM_DELETE_WINDOW", True
);
154 XSetWMProtocols(disp
, winGL
, &wmDelete
, 1);
156 XSetStandardProperties(disp
, winGL
, "msc test", "msc text",
157 None
, NULL
, 0, NULL
);
159 XMapRaised(disp
, winGL
);
161 glXMakeCurrent(disp
, winGL
, context
);
163 get_sync_values
= (void *)glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
164 wait_sync
= (void *)glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
166 if (!get_sync_values
|| !wait_sync
) {
167 fprintf(stderr
, "failed to get sync values function\n");
172 get_sync_values(disp
, winGL
, &ust
, &msc
, &sbc
);
173 fprintf(stderr
, "ust: %llu, msc: %llu, sbc: %llu\n", ust
, msc
,
176 /* Alternate colors to make tearing obvious */
178 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
180 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
181 glClear(GL_COLOR_BUFFER_BIT
);
182 glXSwapBuffers(disp
, winGL
);
183 wait_sync(disp
, winGL
, 0, 60, 0, &ust
, &msc
, &sbc
);
185 "wait returned ust: %llu, msc: %llu, sbc: %llu\n",
190 XDestroyWindow(disp
, winGL
);
191 glXDestroyContext(disp
, context
);