Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / xdemos / msctest.c
1 /*
2 * Copyright © 2009 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 msctest.c
29 * Simple test for MSC functionality.
30 */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <GL/gl.h>
36 #include <GL/glu.h>
37 #include <GL/glx.h>
38 #include <GL/glxext.h>
39 #include <X11/X.h>
40 #include <X11/Xlib.h>
41 #include <X11/Xutil.h>
42
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);
45
46 static int GLXExtensionSupported(Display *dpy, const char *extension)
47 {
48 const char *extensionsString, *client_extensions, *pos;
49
50 extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
51 client_extensions = glXGetClientString(dpy, GLX_EXTENSIONS);
52
53 pos = strstr(extensionsString, extension);
54
55 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
56 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
57 return 1;
58
59 pos = strstr(client_extensions, extension);
60
61 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
62 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
63 return 1;
64
65 return 0;
66 }
67
68 extern char *optarg;
69 extern int optind, opterr, optopt;
70 static char optstr[] = "v";
71
72 static void usage(char *name)
73 {
74 printf("usage: %s\n", name);
75 exit(-1);
76 }
77
78 int main(int argc, char *argv[])
79 {
80 Display *disp;
81 XVisualInfo *pvi;
82 XSetWindowAttributes swa;
83 int attrib[14];
84 Window winGL;
85 GLXContext context;
86 int dummy;
87 Atom wmDelete;
88 int verbose = 0, width = 200, height = 200;
89 int c, i = 1;
90 int64_t ust, msc, sbc;
91
92 opterr = 0;
93 while ((c = getopt(argc, argv, optstr)) != -1) {
94 switch (c) {
95 case 'v':
96 verbose = 1;
97 break;
98 default:
99 usage(argv[0]);
100 break;
101 }
102 }
103
104 disp = XOpenDisplay(NULL);
105 if (!disp) {
106 fprintf(stderr, "failed to open display\n");
107 return -1;
108 }
109
110 if (!glXQueryExtension(disp, &dummy, &dummy)) {
111 fprintf(stderr, "glXQueryExtension failed\n");
112 return -1;
113 }
114
115 if (!GLXExtensionSupported(disp, "GLX_OML_sync_control")) {
116 fprintf(stderr, "GLX_OML_sync_control not supported, exiting\n");
117 return -1;
118 }
119
120 attrib[0] = GLX_RGBA;
121 attrib[1] = 1;
122 attrib[2] = GLX_RED_SIZE;
123 attrib[3] = 1;
124 attrib[4] = GLX_GREEN_SIZE;
125 attrib[5] = 1;
126 attrib[6] = GLX_BLUE_SIZE;
127 attrib[7] = 1;
128 attrib[8] = GLX_DOUBLEBUFFER;
129 attrib[9] = 1;
130 attrib[10] = None;
131
132 pvi = glXChooseVisual(disp, DefaultScreen(disp), attrib);
133 if (!pvi) {
134 fprintf(stderr, "failed to choose visual, exiting\n");
135 return -1;
136 }
137
138 context = glXCreateContext(disp, pvi, None, GL_TRUE);
139 if (!context) {
140 fprintf(stderr, "failed to create glx context\n");
141 return -1;
142 }
143
144 pvi->screen = DefaultScreen(disp);
145
146 swa.colormap = XCreateColormap(disp, RootWindow(disp, pvi->screen),
147 pvi->visual, AllocNone);
148 swa.border_pixel = 0;
149 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
150 StructureNotifyMask;
151 winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
152 0, 0,
153 width, height,
154 0, pvi->depth, InputOutput, pvi->visual,
155 CWBorderPixel | CWColormap | CWEventMask, &swa);
156 if (!winGL) {
157 fprintf(stderr, "window creation failed\n");
158 return -1;
159 }
160 wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True);
161 XSetWMProtocols(disp, winGL, &wmDelete, 1);
162
163 XSetStandardProperties(disp, winGL, "msc test", "msc text",
164 None, NULL, 0, NULL);
165
166 XMapRaised(disp, winGL);
167
168 glXMakeCurrent(disp, winGL, context);
169
170 get_sync_values = glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
171 wait_sync = glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
172
173 if (!get_sync_values || !wait_sync) {
174 fprintf(stderr, "failed to get sync values function\n");
175 return -1;
176 }
177
178 while (i++) {
179 get_sync_values(disp, winGL, &ust, &msc, &sbc);
180 fprintf(stderr, "ust: %llu, msc: %llu, sbc: %llu\n", ust, msc,
181 sbc);
182
183 /* Alternate colors to make tearing obvious */
184 if (i & 1)
185 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
186 else
187 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
188 glClear(GL_COLOR_BUFFER_BIT);
189 glXSwapBuffers(disp, winGL);
190 wait_sync(disp, winGL, 0, 60, 0, &ust, &msc, &sbc);
191 fprintf(stderr,
192 "wait returned ust: %llu, msc: %llu, sbc: %llu\n",
193 ust, msc, sbc);
194 sleep(1);
195 }
196
197 XDestroyWindow(disp, winGL);
198 glXDestroyContext(disp, context);
199 XCloseDisplay(disp);
200
201 return 0;
202 }