Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / drivers / dri / common / vblank.c
1 /* -*- mode: c; c-basic-offset: 3 -*- */
2 /*
3 * (c) Copyright IBM Corporation 2002
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Ian Romanick <idr@us.ibm.com>
27 */
28 /* $XFree86:$ */
29
30 #include "glheader.h"
31 #include "xf86drm.h"
32 #include "mtypes.h"
33 #include "macros.h"
34 #include "dd.h"
35 #include "vblank.h"
36 #include "xmlpool.h"
37
38 static unsigned int msc_to_vblank(__DRIdrawablePrivate * dPriv, int64_t msc)
39 {
40 return (unsigned int)(msc - dPriv->msc_base + dPriv->vblank_base);
41 }
42
43 static int64_t vblank_to_msc(__DRIdrawablePrivate * dPriv, unsigned int vblank)
44 {
45 return (int64_t)(vblank - dPriv->vblank_base + dPriv->msc_base);
46 }
47
48
49 /****************************************************************************/
50 /**
51 * Get the current MSC refresh counter.
52 *
53 * Stores the 64-bit count of vertical refreshes since some (arbitrary)
54 * point in time in \c count. Unless the value wraps around, which it
55 * may, it will never decrease for a given drawable.
56 *
57 * \warning This function is called from \c glXGetVideoSyncSGI, which expects
58 * a \c count of type \c unsigned (32-bit), and \c glXGetSyncValuesOML, which
59 * expects a \c count of type \c int64_t (signed 64-bit). The kernel ioctl
60 * currently always returns a \c sequence of type \c unsigned.
61 *
62 * \param priv Pointer to the DRI screen private struct.
63 * \param dPriv Pointer to the DRI drawable private struct
64 * \param count Storage to hold MSC counter.
65 * \return Zero is returned on success. A negative errno value
66 * is returned on failure.
67 */
68 int driDrawableGetMSC32( __DRIscreenPrivate * priv,
69 __DRIdrawablePrivate * dPriv,
70 int64_t * count)
71 {
72 drmVBlank vbl;
73 int ret;
74
75 /* Don't wait for anything. Just get the current refresh count. */
76
77 vbl.request.type = DRM_VBLANK_RELATIVE;
78 vbl.request.sequence = 0;
79 if ( dPriv && dPriv->vblFlags & VBLANK_FLAG_SECONDARY )
80 vbl.request.type |= DRM_VBLANK_SECONDARY;
81
82 ret = drmWaitVBlank( priv->fd, &vbl );
83
84 if (dPriv) {
85 *count = vblank_to_msc(dPriv, vbl.reply.sequence);
86 } else {
87 /* Old driver (no knowledge of drawable MSC callback) */
88 *count = vbl.reply.sequence;
89 }
90
91 return ret;
92 }
93
94 /**
95 * Get the current MSC refresh counter.
96 *
97 * Stores the 64-bit count of vertical refreshes since some (arbitrary)
98 * point in time in \c count. Unless the value wraps around, which it
99 * may, it will never decrease.
100 *
101 * \warning This function is called from \c glXGetVideoSyncSGI, which expects
102 * a \c count of type \c unsigned (32-bit), and \c glXGetSyncValuesOML, which
103 * expects a \c count of type \c int64_t (signed 64-bit). The kernel ioctl
104 * currently always returns a \c sequence of type \c unsigned.
105 *
106 * Since this function doesn't take a drawable, it may end up getting the MSC
107 * value from a pipe not associated with the caller's context, resuling in
108 * undesired behavior.
109 *
110 * \param priv Pointer to the DRI screen private struct.
111 * \param count Storage to hold MSC counter.
112 * \return Zero is returned on success. A negative errno value
113 * is returned on failure.
114 */
115 int driGetMSC32( __DRIscreenPrivate * priv, int64_t * count )
116 {
117 return driDrawableGetMSC32(priv, NULL, count);
118 }
119
120 /****************************************************************************/
121 /**
122 * Wait for a specified refresh count. This implements most of the
123 * functionality of \c glXWaitForMscOML from the GLX_OML_sync_control spec.
124 * Waits for the \c target_msc refresh. If that has already passed, it
125 * waits until \f$(MSC \bmod divisor)\f$ is equal to \c remainder. If
126 * \c target_msc is 0, use the behavior of glXWaitVideoSyncSGI(), which
127 * omits the initial check against a target MSC value.
128 *
129 * This function is actually something of a hack. The problem is that, at
130 * the time of this writing, none of the existing DRM modules support an
131 * ioctl that returns a 64-bit count (at least not on 32-bit platforms).
132 * However, this function exists to support a GLX function that requires
133 * the use of 64-bit counts. As such, there is a little bit of ugly
134 * hackery at the end of this function to make the 32-bit count act like
135 * a 64-bit count. There are still some cases where this will break, but
136 * I believe it catches the most common cases.
137 *
138 * The real solution is to provide an ioctl that uses a 64-bit count.
139 *
140 * \param dpy Pointer to the \c Display.
141 * \param priv Pointer to the DRI drawable private.
142 * \param target_msc Desired refresh count to wait for. A value of 0
143 * means to use the glXWaitVideoSyncSGI() behavior.
144 * \param divisor MSC divisor if \c target_msc is already reached.
145 * \param remainder Desired MSC remainder if \c target_msc is already
146 * reached.
147 * \param msc Buffer to hold MSC when done waiting.
148 *
149 * \return Zero on success or \c GLX_BAD_CONTEXT on failure.
150 */
151
152 int driWaitForMSC32( __DRIdrawablePrivate *priv,
153 int64_t target_msc, int64_t divisor, int64_t remainder,
154 int64_t * msc )
155 {
156 drmVBlank vbl;
157
158
159 if ( divisor != 0 ) {
160 unsigned int target = (unsigned int)target_msc;
161 unsigned int next = target;
162 unsigned int r;
163 int dont_wait = (target_msc == 0);
164
165 do {
166 /* dont_wait means we're using the glXWaitVideoSyncSGI() behavior.
167 * The first time around, just get the current count and proceed
168 * to the test for (MSC % divisor) == remainder.
169 */
170 vbl.request.type = dont_wait ? DRM_VBLANK_RELATIVE :
171 DRM_VBLANK_ABSOLUTE;
172 vbl.request.sequence = next ? msc_to_vblank(priv, next) : 0;
173 if ( priv->vblFlags & VBLANK_FLAG_SECONDARY )
174 vbl.request.type |= DRM_VBLANK_SECONDARY;
175
176 if ( drmWaitVBlank( priv->driScreenPriv->fd, &vbl ) != 0 ) {
177 /* FIXME: This doesn't seem like the right thing to return here.
178 */
179 return GLX_BAD_CONTEXT;
180 }
181
182 *msc = vblank_to_msc(priv, vbl.reply.sequence);
183
184 dont_wait = 0;
185 if (target_msc != 0 && *msc == target)
186 break;
187
188 /* Assuming the wait-done test fails, the next refresh to wait for
189 * will be one that satisfies (MSC % divisor) == remainder. The
190 * value (MSC - (MSC % divisor) + remainder) is the refresh value
191 * closest to the current value that would satisfy the equation.
192 * If this refresh has already happened, we add divisor to obtain
193 * the next refresh after the current one that will satisfy it.
194 */
195 r = (*msc % (unsigned int)divisor);
196 next = (*msc - r + (unsigned int)remainder);
197 if (next <= *msc) next += (unsigned int)divisor;
198
199 } while ( r != (unsigned int)remainder );
200 }
201 else {
202 /* If the \c divisor is zero, just wait until the MSC is greater
203 * than or equal to \c target_msc.
204 */
205
206 vbl.request.type = DRM_VBLANK_ABSOLUTE;
207 vbl.request.sequence = target_msc ? msc_to_vblank(priv, target_msc) : 0;
208
209 if ( priv->vblFlags & VBLANK_FLAG_SECONDARY )
210 vbl.request.type |= DRM_VBLANK_SECONDARY;
211
212 if ( drmWaitVBlank( priv->driScreenPriv->fd, &vbl ) != 0 ) {
213 /* FIXME: This doesn't seem like the right thing to return here.
214 */
215 return GLX_BAD_CONTEXT;
216 }
217 }
218
219 *msc = vblank_to_msc(priv, vbl.reply.sequence);
220
221 if ( *msc < target_msc ) {
222 *msc += 0x0000000100000000LL;
223 }
224
225 return 0;
226 }
227
228
229 /****************************************************************************/
230 /**
231 * Gets a set of default vertical-blank-wait flags based on the internal GLX
232 * API version and several configuration options.
233 */
234
235 GLuint driGetDefaultVBlankFlags( const driOptionCache *optionCache )
236 {
237 GLuint flags = VBLANK_FLAG_INTERVAL;
238 int vblank_mode;
239
240
241 if ( driCheckOption( optionCache, "vblank_mode", DRI_ENUM ) )
242 vblank_mode = driQueryOptioni( optionCache, "vblank_mode" );
243 else
244 vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
245
246 switch (vblank_mode) {
247 case DRI_CONF_VBLANK_NEVER:
248 flags = 0;
249 break;
250 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
251 break;
252 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
253 flags |= VBLANK_FLAG_THROTTLE;
254 break;
255 case DRI_CONF_VBLANK_ALWAYS_SYNC:
256 flags |= VBLANK_FLAG_SYNC;
257 break;
258 }
259
260 return flags;
261 }
262
263
264 /****************************************************************************/
265 /**
266 * Wrapper to call \c drmWaitVBlank. The main purpose of this function is to
267 * wrap the error message logging. The error message should only be logged
268 * the first time the \c drmWaitVBlank fails. If \c drmWaitVBlank is
269 * successful, \c vbl_seq will be set the sequence value in the reply.
270 *
271 * \param vbl Pointer to drmVBlank packet desribing how to wait.
272 * \param vbl_seq Location to store the current refresh counter.
273 * \param fd File descriptor use to call into the DRM.
274 * \return Zero on success or -1 on failure.
275 */
276
277 static int do_wait( drmVBlank * vbl, GLuint * vbl_seq, int fd )
278 {
279 int ret;
280
281
282 ret = drmWaitVBlank( fd, vbl );
283 if ( ret != 0 ) {
284 static GLboolean first_time = GL_TRUE;
285
286 if ( first_time ) {
287 fprintf(stderr,
288 "%s: drmWaitVBlank returned %d, IRQs don't seem to be"
289 " working correctly.\nTry running with LIBGL_THROTTLE_REFRESH"
290 " and LIBL_SYNC_REFRESH unset.\n", __FUNCTION__, ret);
291 first_time = GL_FALSE;
292 }
293
294 return -1;
295 }
296
297 *vbl_seq = vbl->reply.sequence;
298 return 0;
299 }
300
301
302 /****************************************************************************/
303 /**
304 * Sets the default swap interval when the drawable is first bound to a
305 * direct rendering context.
306 */
307
308 void driDrawableInitVBlank( __DRIdrawablePrivate *priv )
309 {
310 if ( priv->swap_interval == (unsigned)-1 ) {
311 /* Get current vertical blank sequence */
312 drmVBlank vbl;
313
314 vbl.request.type = DRM_VBLANK_RELATIVE;
315 if ( priv->vblFlags & VBLANK_FLAG_SECONDARY )
316 vbl.request.type |= DRM_VBLANK_SECONDARY;
317 vbl.request.sequence = 0;
318 do_wait( &vbl, &priv->vblSeq, priv->driScreenPriv->fd );
319 priv->vblank_base = priv->vblSeq;
320
321 priv->swap_interval =
322 (priv->vblFlags & (VBLANK_FLAG_THROTTLE | VBLANK_FLAG_SYNC)) ? 1 : 0;
323 }
324 }
325
326
327 /****************************************************************************/
328 /**
329 * Returns the current swap interval of the given drawable.
330 */
331
332 unsigned
333 driGetVBlankInterval( const __DRIdrawablePrivate *priv, GLuint flags )
334 {
335 if ( (flags & VBLANK_FLAG_INTERVAL) != 0 ) {
336 /* this must have been initialized when the drawable was first bound
337 * to a direct rendering context. */
338 assert ( priv->swap_interval != (unsigned)-1 );
339
340 return priv->swap_interval;
341 }
342 else if ( (flags & (VBLANK_FLAG_THROTTLE | VBLANK_FLAG_SYNC)) != 0 ) {
343 return 1;
344 }
345 else {
346 return 0;
347 }
348 }
349
350
351 /****************************************************************************/
352 /**
353 * Returns the current vertical blank sequence number of the given drawable.
354 */
355
356 void
357 driGetCurrentVBlank( const __DRIdrawablePrivate *priv, GLuint flags,
358 GLuint *vbl_seq )
359 {
360 drmVBlank vbl;
361
362 vbl.request.type = DRM_VBLANK_RELATIVE;
363 if ( flags & VBLANK_FLAG_SECONDARY ) {
364 vbl.request.type |= DRM_VBLANK_SECONDARY;
365 }
366 vbl.request.sequence = 0;
367
368 (void) do_wait( &vbl, vbl_seq, priv->driScreenPriv->fd );
369 }
370
371
372 /****************************************************************************/
373 /**
374 * Waits for the vertical blank for use with glXSwapBuffers.
375 *
376 * \param vbl_seq Vertical blank sequence number (MSC) after the last buffer
377 * swap. Updated after this wait.
378 * \param flags \c VBLANK_FLAG bits that control how long to wait.
379 * \param missed_deadline Set to \c GL_TRUE if the MSC after waiting is later
380 * than the "target" based on \c flags. The idea is that if
381 * \c missed_deadline is set, then the application is not
382 * achieving its desired framerate.
383 * \return Zero on success, -1 on error.
384 */
385
386 int
387 driWaitForVBlank( const __DRIdrawablePrivate *priv, GLuint * vbl_seq,
388 GLuint flags, GLboolean * missed_deadline )
389 {
390 drmVBlank vbl;
391 unsigned original_seq;
392 unsigned deadline;
393 unsigned interval;
394 unsigned diff;
395
396 *missed_deadline = GL_FALSE;
397 if ( (flags & (VBLANK_FLAG_INTERVAL |
398 VBLANK_FLAG_THROTTLE |
399 VBLANK_FLAG_SYNC)) == 0 ||
400 (flags & VBLANK_FLAG_NO_IRQ) != 0 ) {
401 return 0;
402 }
403
404
405 /* VBLANK_FLAG_SYNC means to wait for at least one vertical blank. If
406 * that flag is not set, do a fake wait for zero vertical blanking
407 * periods so that we can get the current MSC.
408 *
409 * VBLANK_FLAG_INTERVAL and VBLANK_FLAG_THROTTLE mean to wait for at
410 * least one vertical blank since the last wait. Since do_wait modifies
411 * vbl_seq, we have to save the original value of vbl_seq for the
412 * VBLANK_FLAG_INTERVAL / VBLANK_FLAG_THROTTLE calculation later.
413 */
414
415 original_seq = *vbl_seq;
416 interval = driGetVBlankInterval(priv, flags);
417 deadline = original_seq + interval;
418
419 vbl.request.type = DRM_VBLANK_RELATIVE;
420 if ( flags & VBLANK_FLAG_SECONDARY ) {
421 vbl.request.type |= DRM_VBLANK_SECONDARY;
422 }
423 vbl.request.sequence = ((flags & VBLANK_FLAG_SYNC) != 0) ? 1 : 0;
424
425 if ( do_wait( & vbl, vbl_seq, priv->driScreenPriv->fd ) != 0 ) {
426 return -1;
427 }
428
429 diff = *vbl_seq - deadline;
430
431 /* No need to wait again if we've already reached the target */
432 if (diff <= (1 << 23)) {
433 *missed_deadline = (flags & VBLANK_FLAG_SYNC) ? (diff > 0) : GL_TRUE;
434 return 0;
435 }
436
437 /* Wait until the target vertical blank. */
438 vbl.request.type = DRM_VBLANK_ABSOLUTE;
439 if ( flags & VBLANK_FLAG_SECONDARY ) {
440 vbl.request.type |= DRM_VBLANK_SECONDARY;
441 }
442 vbl.request.sequence = deadline;
443
444 if ( do_wait( & vbl, vbl_seq, priv->driScreenPriv->fd ) != 0 ) {
445 return -1;
446 }
447
448 diff = *vbl_seq - deadline;
449 *missed_deadline = diff > 0 && diff <= (1 << 23);
450
451 return 0;
452 }