st/mesa: fix glCopyPixels bugs/crashes when src region need clipping
[mesa.git] / progs / xdemos / omlsync.c
1 /*
2 * Copyright © 2007-2010 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 omlsync.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 Bool (*glXGetSyncValuesOML)(Display *dpy, GLXDrawable drawable,
61 int64_t *ust, int64_t *msc, int64_t *sbc);
62 Bool (*glXGetMscRateOML)(Display *dpy, GLXDrawable drawable, int32_t *numerator,
63 int32_t *denominator);
64 int64_t (*glXSwapBuffersMscOML)(Display *dpy, GLXDrawable drawable,
65 int64_t target_msc, int64_t divisor,
66 int64_t remainder);
67 Bool (*glXWaitForMscOML)(Display *dpy, GLXDrawable drawable, int64_t target_msc,
68 int64_t divisor, int64_t remainder, int64_t *ust,
69 int64_t *msc, int64_t *sbc);
70 Bool (*glXWaitForSbcOML)(Display *dpy, GLXDrawable drawable, int64_t target_sbc,
71 int64_t *ust, int64_t *msc, int64_t *sbc);
72 int (*glXSwapInterval)(int interval);
73
74 static int GLXExtensionSupported(Display *dpy, const char *extension)
75 {
76 const char *extensionsString, *pos;
77
78 extensionsString = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
79
80 pos = strstr(extensionsString, extension);
81
82 if (pos != NULL && (pos == extensionsString || pos[-1] == ' ') &&
83 (pos[strlen(extension)] == ' ' || pos[strlen(extension)] == '\0'))
84 return 1;
85
86 return 0;
87 }
88
89 extern char *optarg;
90 extern int optind, opterr, optopt;
91 static char optstr[] = "w:h:vd:r:n:i:";
92
93 static void usage(char *name)
94 {
95 printf("usage: %s [-w <width>] [-h <height>] ...\n", name);
96 printf("\t-d<divisor> - divisor for OML swap\n");
97 printf("\t-r<remainder> - remainder for OML swap\n");
98 printf("\t-n<interval> - wait interval for OML WaitMSC\n");
99 printf("\t-i<swap interval> - swap at most once every n frames\n");
100 printf("\t-v: verbose (print count)\n");
101 exit(-1);
102 }
103
104 int main(int argc, char *argv[])
105 {
106 Display *disp;
107 XVisualInfo *pvi;
108 XSetWindowAttributes swa;
109 Window winGL;
110 GLXContext context;
111 int dummy;
112 Atom wmDelete;
113 int64_t ust, msc, sbc;
114 int width = 500, height = 500, verbose = 0, divisor = 0, remainder = 0,
115 wait_interval = 0, swap_interval = 1;
116 int c, i = 1;
117 int ret;
118 int db_attribs[] = { GLX_RGBA,
119 GLX_RED_SIZE, 1,
120 GLX_GREEN_SIZE, 1,
121 GLX_BLUE_SIZE, 1,
122 GLX_DOUBLEBUFFER,
123 GLX_DEPTH_SIZE, 1,
124 None };
125 XSizeHints sizehints;
126
127 opterr = 0;
128 while ((c = getopt(argc, argv, optstr)) != -1) {
129 switch (c) {
130 case 'w':
131 width = atoi(optarg);
132 break;
133 case 'h':
134 height = atoi(optarg);
135 break;
136 case 'v':
137 verbose = 1;
138 break;
139 case 'd':
140 divisor = atoi(optarg);
141 break;
142 case 'r':
143 remainder = atoi(optarg);
144 break;
145 case 'n':
146 wait_interval = atoi(optarg);
147 break;
148 case 'i':
149 swap_interval = atoi(optarg);
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_OML_sync_control")) {
169 fprintf(stderr, "GLX_OML_sync_control not supported\n");
170 return -1;
171 }
172
173 if (!GLXExtensionSupported(disp, "GLX_MESA_swap_control")) {
174 fprintf(stderr, "GLX_MESA_swap_control not supported\n");
175 return -1;
176 }
177
178 pvi = glXChooseVisual(disp, DefaultScreen(disp), db_attribs);
179
180 if (!pvi) {
181 fprintf(stderr, "failed to choose visual, exiting\n");
182 return -1;
183 }
184
185 pvi->screen = DefaultScreen(disp);
186
187 swa.colormap = XCreateColormap(disp, RootWindow(disp, pvi->screen),
188 pvi->visual, AllocNone);
189 swa.border_pixel = 0;
190 swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
191 StructureNotifyMask;
192 winGL = XCreateWindow(disp, RootWindow(disp, pvi->screen),
193 0, 0,
194 width, height,
195 0, pvi->depth, InputOutput, pvi->visual,
196 CWBorderPixel | CWColormap | CWEventMask, &swa);
197 if (!winGL) {
198 fprintf(stderr, "window creation failed\n");
199 return -1;
200 }
201 wmDelete = XInternAtom(disp, "WM_DELETE_WINDOW", True);
202 XSetWMProtocols(disp, winGL, &wmDelete, 1);
203
204 sizehints.x = 0;
205 sizehints.y = 0;
206 sizehints.width = width;
207 sizehints.height = height;
208 sizehints.flags = USSize | USPosition;
209
210 XSetNormalHints(disp, winGL, &sizehints);
211 XSetStandardProperties(disp, winGL, "glsync test", "glsync text",
212 None, NULL, 0, &sizehints);
213
214 context = glXCreateContext(disp, pvi, NULL, GL_TRUE);
215 if (!context) {
216 fprintf(stderr, "failed to create glx context\n");
217 return -1;
218 }
219
220 XMapWindow(disp, winGL);
221 ret = glXMakeCurrent(disp, winGL, context);
222 if (!ret) {
223 fprintf(stderr, "failed to make context current: %d\n", ret);
224 }
225
226 glXGetSyncValuesOML = (void *)glXGetProcAddress((unsigned char *)"glXGetSyncValuesOML");
227 glXGetMscRateOML = (void *)glXGetProcAddress((unsigned char *)"glXGetMscRateOML");
228 glXSwapBuffersMscOML = (void *)glXGetProcAddress((unsigned char *)"glXSwapBuffersMscOML");
229 glXWaitForMscOML = (void *)glXGetProcAddress((unsigned char *)"glXWaitForMscOML");
230 glXWaitForSbcOML = (void *)glXGetProcAddress((unsigned char *)"glXWaitForSbcOML");
231 glXSwapInterval = (void *)glXGetProcAddress((unsigned char *)"glXSwapIntervalMESA");
232
233 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
234
235 glXSwapInterval(swap_interval);
236 fprintf(stderr, "set swap interval to %d\n", swap_interval);
237
238 glXGetSyncValuesOML(disp, winGL, &ust, &msc, &sbc);
239 while (i++) {
240 /* Alternate colors to make tearing obvious */
241 if (i & 1) {
242 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
243 glColor3f(1.0f, 1.0f, 1.0f);
244 } else {
245 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
246 glColor3f(1.0f, 0.0f, 0.0f);
247 }
248
249 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
250 glRectf(0, 0, width, height);
251
252 glXSwapBuffersMscOML(disp, winGL, 0, divisor, remainder);
253
254 if (wait_interval) {
255 glXWaitForMscOML(disp, winGL, msc + wait_interval,
256 0, 0, &ust, &msc, &sbc);
257 }
258 }
259
260 XDestroyWindow(disp, winGL);
261 glXDestroyContext(disp, context);
262 XCloseDisplay(disp);
263
264 return 0;
265 }