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.
37 #include <GL/glxext.h>
40 #include <X11/Xutil.h>
42 void (*get_sync_values
)(Display
*dpy
, Window winGL
, int64_t *ust
, int64_t *msc
, int64_t *sbc
);
43 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
);
45 static int GLXExtensionSupported(Display
*dpy
, const char *extension
)
47 const char *extensionsString
, *pos
;
49 extensionsString
= glXQueryExtensionsString(dpy
, DefaultScreen(dpy
));
51 pos
= strstr(extensionsString
, extension
);
53 if (pos
!= NULL
&& (pos
== extensionsString
|| pos
[-1] == ' ') &&
54 (pos
[strlen(extension
)] == ' ' || pos
[strlen(extension
)] == '\0'))
61 extern int optind
, opterr
, optopt
;
62 static char optstr
[] = "v";
64 static void usage(char *name
)
66 printf("usage: %s\n", name
);
70 int main(int argc
, char *argv
[])
74 XSetWindowAttributes swa
;
80 int verbose
= 0, width
= 200, height
= 200;
82 int64_t ust
, msc
, sbc
;
85 while ((c
= getopt(argc
, argv
, optstr
)) != -1) {
96 disp
= XOpenDisplay(NULL
);
98 fprintf(stderr
, "failed to open display\n");
102 if (!glXQueryExtension(disp
, &dummy
, &dummy
)) {
103 fprintf(stderr
, "glXQueryExtension failed\n");
107 if (!GLXExtensionSupported(disp
, "GLX_OML_sync_control")) {
108 fprintf(stderr
, "GLX_OML_sync_control not supported, exiting\n");
112 attrib
[0] = GLX_RGBA
;
114 attrib
[2] = GLX_RED_SIZE
;
116 attrib
[4] = GLX_GREEN_SIZE
;
118 attrib
[6] = GLX_BLUE_SIZE
;
120 attrib
[8] = GLX_DOUBLEBUFFER
;
124 pvi
= glXChooseVisual(disp
, DefaultScreen(disp
), attrib
);
126 fprintf(stderr
, "failed to choose visual, exiting\n");
130 context
= glXCreateContext(disp
, pvi
, None
, GL_TRUE
);
132 fprintf(stderr
, "failed to create glx context\n");
136 pvi
->screen
= DefaultScreen(disp
);
138 swa
.colormap
= XCreateColormap(disp
, RootWindow(disp
, pvi
->screen
),
139 pvi
->visual
, AllocNone
);
140 swa
.border_pixel
= 0;
141 swa
.event_mask
= ExposureMask
| KeyPressMask
| ButtonPressMask
|
143 winGL
= XCreateWindow(disp
, RootWindow(disp
, pvi
->screen
),
146 0, pvi
->depth
, InputOutput
, pvi
->visual
,
147 CWBorderPixel
| CWColormap
| CWEventMask
, &swa
);
149 fprintf(stderr
, "window creation failed\n");
152 wmDelete
= XInternAtom(disp
, "WM_DELETE_WINDOW", True
);
153 XSetWMProtocols(disp
, winGL
, &wmDelete
, 1);
155 XSetStandardProperties(disp
, winGL
, "msc test", "msc text",
156 None
, NULL
, 0, NULL
);
158 XMapRaised(disp
, winGL
);
160 glXMakeCurrent(disp
, winGL
, context
);
162 get_sync_values
= (void *)glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
163 wait_sync
= (void *)glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
165 if (!get_sync_values
|| !wait_sync
) {
166 fprintf(stderr
, "failed to get sync values function\n");
171 get_sync_values(disp
, winGL
, &ust
, &msc
, &sbc
);
172 fprintf(stderr
, "ust: %llu, msc: %llu, sbc: %llu\n", ust
, msc
,
175 /* Alternate colors to make tearing obvious */
177 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
179 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
180 glClear(GL_COLOR_BUFFER_BIT
);
181 glXSwapBuffers(disp
, winGL
);
182 wait_sync(disp
, winGL
, 0, 60, 0, &ust
, &msc
, &sbc
);
184 "wait returned ust: %llu, msc: %llu, sbc: %llu\n",
189 XDestroyWindow(disp
, winGL
);
190 glXDestroyContext(disp
, context
);