glxinfo: test GL_ATI_meminfo and GL_NVX_gpu_memory_info (v2)
[mesa-demos.git] / src / xdemos / msctest.c
blob662814f9cbae4e76d2a3a6173afeb207c6f392fc
1 /*
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
13 * Software.
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.
23 * Authors:
24 * Jesse Barnes <jesse.barnes@intel.com>
28 /** @file msctest.c
29 * Simple test for MSC functionality.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <GL/gl.h>
36 #include <GL/glx.h>
37 #include <GL/glxext.h>
38 #include <X11/X.h>
39 #include <X11/Xlib.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'))
55 return 1;
57 return 0;
60 extern char *optarg;
61 extern int optind, opterr, optopt;
62 static char optstr[] = "v";
64 static void usage(char *name)
66 printf("usage: %s\n", name);
67 exit(-1);
70 int main(int argc, char *argv[])
72 Display *disp;
73 XVisualInfo *pvi;
74 XSetWindowAttributes swa;
75 int attrib[14];
76 Window winGL;
77 GLXContext context;
78 int dummy;
79 Atom wmDelete;
80 int verbose = 0, width = 200, height = 200;
81 int c, i = 1;
82 int64_t ust, msc, sbc;
84 opterr = 0;
85 while ((c = getopt(argc, argv, optstr)) != -1) {
86 switch (c) {
87 case 'v':
88 verbose = 1;
89 break;
90 default:
91 usage(argv[0]);
92 break;
96 disp = XOpenDisplay(NULL);
97 if (!disp) {
98 fprintf(stderr, "failed to open display\n");
99 return -1;
102 if (!glXQueryExtension(disp, &dummy, &dummy)) {
103 fprintf(stderr, "glXQueryExtension failed\n");
104 return -1;
107 if (!GLXExtensionSupported(disp, "GLX_OML_sync_control")) {
108 fprintf(stderr, "GLX_OML_sync_control not supported, exiting\n");
109 return -1;
112 attrib[0] = GLX_RGBA;
113 attrib[1] = 1;
114 attrib[2] = GLX_RED_SIZE;
115 attrib[3] = 1;
116 attrib[4] = GLX_GREEN_SIZE;
117 attrib[5] = 1;
118 attrib[6] = GLX_BLUE_SIZE;
119 attrib[7] = 1;
120 attrib[8] = GLX_DOUBLEBUFFER;
121 attrib[9] = 1;
122 attrib[10] = None;
124 pvi = glXChooseVisual(disp, DefaultScreen(disp), attrib);
125 if (!pvi) {
126 fprintf(stderr, "failed to choose visual, exiting\n");
127 return -1;
130 context = glXCreateContext(disp, pvi, None, GL_TRUE);
131 if (!context) {
132 fprintf(stderr, "failed to create glx context\n");
133 return -1;
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 |
142 StructureNotifyMask;
143 winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
144 0, 0,
145 width, height,
146 0, pvi->depth, InputOutput, pvi->visual,
147 CWBorderPixel | CWColormap | CWEventMask, &swa);
148 if (!winGL) {
149 fprintf(stderr, "window creation failed\n");
150 return -1;
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");
167 return -1;
170 while (i++) {
171 get_sync_values(disp, winGL, &ust, &msc, &sbc);
172 fprintf(stderr, "ust: %llu, msc: %llu, sbc: %llu\n", ust, msc,
173 sbc);
175 /* Alternate colors to make tearing obvious */
176 if (i & 1)
177 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
178 else
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);
183 fprintf(stderr,
184 "wait returned ust: %llu, msc: %llu, sbc: %llu\n",
185 ust, msc, sbc);
186 sleep(1);
189 XDestroyWindow(disp, winGL);
190 glXDestroyContext(disp, context);
191 XCloseDisplay(disp);
193 return 0;