Eric Anholt | 72bbc89 | 2008-11-04 12:34:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2007 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 glsync.c |
| 29 | * The program is simple: it paints a window alternating colors (red & |
| 30 | * white) either as fast as possible or synchronized to vblank events |
| 31 | * |
| 32 | * If run normally, the program should display a window that exhibits |
| 33 | * significant tearing between red and white colors (e.g. you might get |
| 34 | * a "waterfall" effect of red and white horizontal bars). |
| 35 | * |
| 36 | * If run with the '-s b' option, the program should synchronize the |
| 37 | * window color changes with the vertical blank period, resulting in a |
| 38 | * window that looks orangish with a high frequency flicker (which may |
| 39 | * be invisible). If the window is moved to another screen, this |
| 40 | * property should be preserved. If the window spans two screens, it |
| 41 | * shouldn't tear on whichever screen most of the window is on; the |
| 42 | * portion on the other screen may show some tearing (like the |
| 43 | * waterfall effect above). |
| 44 | * |
| 45 | * Other options include '-w <width>' and '-h <height' to set the |
| 46 | * window size. |
| 47 | */ |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
| 51 | #include <unistd.h> |
| 52 | #include <GL/gl.h> |
| 53 | #include <GL/glu.h> |
| 54 | #include <GL/glx.h> |
| 55 | #include <GL/glxext.h> |
| 56 | #include <X11/X.h> |
| 57 | #include <X11/Xlib.h> |
| 58 | #include <X11/Xutil.h> |
| 59 | |
| 60 | void (*video_sync_get)(); |
| 61 | void (*video_sync)(); |
| 62 | |
| 63 | static int GLXExtensionSupported(Display *dpy, const char *extension) |
| 64 | { |
| 65 | const char *extensionsString, *client_extensions, *pos; |
| 66 | |
| 67 | extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy)); |
| 68 | client_extensions = glXGetClientString(dpy, GLX_EXTENSIONS); |
| 69 | |
| 70 | pos = strstr(extensionsString, extension); |
| 71 | |
| 72 | if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') && |
| 73 | (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0')) |
| 74 | return 1; |
| 75 | |
| 76 | pos = strstr(client_extensions, extension); |
| 77 | |
| 78 | if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') && |
| 79 | (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0')) |
| 80 | return 1; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | extern char *optarg; |
| 86 | extern int optind, opterr, optopt; |
| 87 | static char optstr[] = "w:h:s:v"; |
| 88 | |
| 89 | enum sync_type { |
| 90 | none = 0, |
| 91 | sgi_video_sync, |
| 92 | buffer_swap, |
| 93 | }; |
| 94 | |
| 95 | static void usage(char *name) |
| 96 | { |
| 97 | printf("usage: %s [-w <width>] [-h <height>] [-s<sync method>] " |
Brian Paul | 61a96a2 | 2009-10-22 17:11:59 -0600 | [diff] [blame^] | 98 | "[-v]\n", name); |
Eric Anholt | 72bbc89 | 2008-11-04 12:34:29 -0800 | [diff] [blame] | 99 | printf("\t-s<sync method>:\n"); |
| 100 | printf("\t\tn: none\n"); |
| 101 | printf("\t\ts: SGI video sync extension\n"); |
| 102 | printf("\t\tb: buffer swap\n"); |
| 103 | printf("\t-v: verbose (print count)\n"); |
| 104 | exit(-1); |
| 105 | } |
| 106 | |
| 107 | int main(int argc, char *argv[]) |
| 108 | { |
| 109 | Display *disp; |
| 110 | XVisualInfo *pvi; |
| 111 | XSetWindowAttributes swa; |
| 112 | int attrib[14]; |
| 113 | GLint last_val = -1, count = 0; |
| 114 | Window winGL; |
Brian Paul | 8b2ecfd | 2009-03-12 17:21:05 -0600 | [diff] [blame] | 115 | GLXContext context; |
Eric Anholt | 72bbc89 | 2008-11-04 12:34:29 -0800 | [diff] [blame] | 116 | int dummy; |
| 117 | Atom wmDelete; |
| 118 | enum sync_type waitforsync = none; |
| 119 | int width = 500, height = 500, verbose = 0, |
| 120 | countonly = 0; |
| 121 | int c, i = 1; |
| 122 | |
| 123 | opterr = 0; |
| 124 | while ((c = getopt(argc, argv, optstr)) != -1) { |
| 125 | switch (c) { |
| 126 | case 'w': |
| 127 | width = atoi(optarg); |
| 128 | break; |
| 129 | case 'h': |
| 130 | height = atoi(optarg); |
| 131 | break; |
| 132 | case 's': |
| 133 | switch (optarg[0]) { |
| 134 | case 'n': |
| 135 | waitforsync = none; |
| 136 | break; |
| 137 | case 's': |
| 138 | waitforsync = sgi_video_sync; |
| 139 | break; |
| 140 | case 'b': |
| 141 | waitforsync = buffer_swap; |
| 142 | break; |
| 143 | default: |
| 144 | usage(argv[0]); |
| 145 | break; |
| 146 | } |
| 147 | break; |
| 148 | case 'v': |
| 149 | verbose = 1; |
| 150 | break; |
| 151 | default: |
| 152 | usage(argv[0]); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | disp = XOpenDisplay(NULL); |
| 158 | if (!disp) { |
| 159 | fprintf(stderr, "failed to open display\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | if (!glXQueryExtension(disp, &dummy, &dummy)) { |
| 164 | fprintf(stderr, "glXQueryExtension failed\n"); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | if (!GLXExtensionSupported(disp, "GLX_SGI_video_sync")) { |
| 169 | fprintf(stderr, "GLX_SGI_video_sync not supported, exiting\n"); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | attrib[0] = GLX_RGBA; |
| 174 | attrib[1] = 1; |
| 175 | attrib[2] = GLX_RED_SIZE; |
| 176 | attrib[3] = 1; |
| 177 | attrib[4] = GLX_GREEN_SIZE; |
| 178 | attrib[5] = 1; |
| 179 | attrib[6] = GLX_BLUE_SIZE; |
| 180 | attrib[7] = 1; |
| 181 | if (waitforsync != buffer_swap) |
| 182 | attrib[8] = None; |
| 183 | else { |
| 184 | attrib[8] = GLX_DOUBLEBUFFER; |
| 185 | attrib[9] = 1; |
| 186 | attrib[10] = None; |
| 187 | } |
| 188 | |
Eric Anholt | 72bbc89 | 2008-11-04 12:34:29 -0800 | [diff] [blame] | 189 | pvi = glXChooseVisual(disp, DefaultScreen(disp), attrib); |
| 190 | if (!pvi) { |
| 191 | fprintf(stderr, "failed to choose visual, exiting\n"); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | context = glXCreateContext(disp, pvi, None, GL_TRUE); |
| 196 | if (!context) { |
| 197 | fprintf(stderr, "failed to create glx context\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | pvi->screen = DefaultScreen(disp); |
| 202 | |
| 203 | swa.colormap = XCreateColormap(disp, RootWindow(disp, pvi->screen), |
| 204 | pvi->visual, AllocNone); |
| 205 | swa.border_pixel = 0; |
| 206 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | |
| 207 | StructureNotifyMask; |
| 208 | winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen), |
| 209 | 0, 0, |
| 210 | width, height, |
| 211 | 0, pvi->depth, InputOutput, pvi->visual, |
| 212 | CWBorderPixel | CWColormap | CWEventMask, &swa); |
| 213 | if (!winGL) { |
| 214 | fprintf(stderr, "window creation failed\n"); |
| 215 | return -1; |
| 216 | } |
| 217 | wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True); |
| 218 | XSetWMProtocols(disp, winGL, &wmDelete, 1); |
| 219 | |
| 220 | XSetStandardProperties(disp, winGL, "glsync test", "glsync text", |
| 221 | None, NULL, 0, NULL); |
| 222 | |
| 223 | XMapRaised(disp, winGL); |
| 224 | |
| 225 | glXMakeCurrent(disp, winGL, context); |
| 226 | |
| 227 | video_sync_get = glXGetProcAddress((unsigned char *)"glXGetVideoSyncSGI"); |
| 228 | video_sync = glXGetProcAddress((unsigned char *)"glXWaitVideoSyncSGI"); |
| 229 | |
| 230 | if (!video_sync_get || !video_sync) { |
| 231 | fprintf(stderr, "failed to get sync functions\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | video_sync_get(&count); |
| 236 | count++; |
| 237 | while (i++) { |
| 238 | /* Wait for vsync */ |
| 239 | if (waitforsync == sgi_video_sync) { |
| 240 | if (verbose) |
| 241 | fprintf(stderr, "waiting on count %d\n", count); |
| 242 | video_sync(2, (count + 1) % 2, &count); |
| 243 | if (count < last_val) |
| 244 | fprintf(stderr, "error: vblank count went backwards: %d -> %d\n", last_val, count); |
| 245 | if (count == last_val) |
| 246 | fprintf(stderr, "error: count didn't change: %d\n", count); |
| 247 | last_val = count; |
| 248 | } else if (waitforsync == buffer_swap) { |
| 249 | glXSwapBuffers(disp, winGL); |
| 250 | } |
| 251 | |
| 252 | if (countonly) { |
| 253 | video_sync(2, 1, &count); |
| 254 | fprintf(stderr, "current count: %d\n", count); |
| 255 | sleep(1); |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | /* Alternate colors to make tearing obvious */ |
| 260 | if (i & 1) |
| 261 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
| 262 | else |
| 263 | glClearColor(1.0f, 0.0f, 0.0f, 0.0f); |
| 264 | glClear(GL_COLOR_BUFFER_BIT); |
| 265 | glFlush(); |
| 266 | } |
| 267 | |
| 268 | XDestroyWindow(disp, winGL); |
| 269 | glXDestroyContext(disp, context); |
| 270 | XCloseDisplay(disp); |
| 271 | |
| 272 | return 0; |
| 273 | } |