Merge branch '7.8' into master
[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, *pos;
49
50 extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
51
52 pos = strstr(extensionsString, extension);
53
54 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
55 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
56 return 1;
57
58 return 0;
59 }
60
61 extern char *optarg;
62 extern int optind, opterr, optopt;
63 static char optstr[] = "v";
64
65 static void usage(char *name)
66 {
67 printf("usage: %s\n", name);
68 exit(-1);
69 }
70
71 int main(int argc, char *argv[])
72 {
73 Display *disp;
74 XVisualInfo *pvi;
75 XSetWindowAttributes swa;
76 int attrib[14];
77 Window winGL;
78 GLXContext context;
79 int dummy;
80 Atom wmDelete;
81 int verbose = 0, width = 200, height = 200;
82 int c, i = 1;
83 int64_t ust, msc, sbc;
84
85 opterr = 0;
86 while ((c = getopt(argc, argv, optstr)) != -1) {
87 switch (c) {
88 case 'v':
89 verbose = 1;
90 break;
91 default:
92 usage(argv[0]);
93 break;
94 }
95 }
96
97 disp = XOpenDisplay(NULL);
98 if (!disp) {
99 fprintf(stderr, "failed to open display\n");
100 return -1;
101 }
102
103 if (!glXQueryExtension(disp, &dummy, &dummy)) {
104 fprintf(stderr, "glXQueryExtension failed\n");
105 return -1;
106 }
107
108 if (!GLXExtensionSupported(disp, "GLX_OML_sync_control")) {
109 fprintf(stderr, "GLX_OML_sync_control not supported, exiting\n");
110 return -1;
111 }
112
113 attrib[0] = GLX_RGBA;
114 attrib[1] = 1;
115 attrib[2] = GLX_RED_SIZE;
116 attrib[3] = 1;
117 attrib[4] = GLX_GREEN_SIZE;
118 attrib[5] = 1;
119 attrib[6] = GLX_BLUE_SIZE;
120 attrib[7] = 1;
121 attrib[8] = GLX_DOUBLEBUFFER;
122 attrib[9] = 1;
123 attrib[10] = None;
124
125 pvi = glXChooseVisual(disp, DefaultScreen(disp), attrib);
126 if (!pvi) {
127 fprintf(stderr, "failed to choose visual, exiting\n");
128 return -1;
129 }
130
131 context = glXCreateContext(disp, pvi, None, GL_TRUE);
132 if (!context) {
133 fprintf(stderr, "failed to create glx context\n");
134 return -1;
135 }
136
137 pvi->screen = DefaultScreen(disp);
138
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 |
143 StructureNotifyMask;
144 winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
145 0, 0,
146 width, height,
147 0, pvi->depth, InputOutput, pvi->visual,
148 CWBorderPixel | CWColormap | CWEventMask, &swa);
149 if (!winGL) {
150 fprintf(stderr, "window creation failed\n");
151 return -1;
152 }
153 wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True);
154 XSetWMProtocols(disp, winGL, &wmDelete, 1);
155
156 XSetStandardProperties(disp, winGL, "msc test", "msc text",
157 None, NULL, 0, NULL);
158
159 XMapRaised(disp, winGL);
160
161 glXMakeCurrent(disp, winGL, context);
162
163 get_sync_values = (void *)glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
164 wait_sync = (void *)glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
165
166 if (!get_sync_values || !wait_sync) {
167 fprintf(stderr, "failed to get sync values function\n");
168 return -1;
169 }
170
171 while (i++) {
172 get_sync_values(disp, winGL, &ust, &msc, &sbc);
173 fprintf(stderr, "ust: %llu, msc: %llu, sbc: %llu\n", ust, msc,
174 sbc);
175
176 /* Alternate colors to make tearing obvious */
177 if (i & 1)
178 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
179 else
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);
184 fprintf(stderr,
185 "wait returned ust: %llu, msc: %llu, sbc: %llu\n",
186 ust, msc, sbc);
187 sleep(1);
188 }
189
190 XDestroyWindow(disp, winGL);
191 glXDestroyContext(disp, context);
192 XCloseDisplay(disp);
193
194 return 0;
195 }