1 /* -*- mode: c; c-basic-offset: 3 -*- */
3 * (c) Copyright IBM Corporation 2002
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:
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
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.
26 * Ian Romanick <idr@us.ibm.com>
38 static unsigned int msc_to_vblank(__DRIdrawablePrivate
* dPriv
, int64_t msc
)
40 return (unsigned int)(msc
- dPriv
->msc_base
+ dPriv
->vblank_base
);
43 static int64_t vblank_to_msc(__DRIdrawablePrivate
* dPriv
, unsigned int vblank
)
45 return (int64_t)(vblank
- dPriv
->vblank_base
+ dPriv
->msc_base
);
49 /****************************************************************************/
51 * Get the current MSC refresh counter.
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.
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.
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.
68 int driDrawableGetMSC32( __DRIscreenPrivate
* priv
,
69 __DRIdrawablePrivate
* dPriv
,
75 /* Don't wait for anything. Just get the current refresh count. */
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
;
82 ret
= drmWaitVBlank( priv
->fd
, &vbl
);
85 *count
= vblank_to_msc(dPriv
, vbl
.reply
.sequence
);
87 /* Old driver (no knowledge of drawable MSC callback) */
88 *count
= vbl
.reply
.sequence
;
95 * Get the current MSC refresh counter.
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.
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.
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.
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.
115 int driGetMSC32( __DRIscreenPrivate
* priv
, int64_t * count
)
117 return driDrawableGetMSC32(priv
, NULL
, count
);
120 /****************************************************************************/
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.
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.
138 * The real solution is to provide an ioctl that uses a 64-bit count.
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
147 * \param msc Buffer to hold MSC when done waiting.
149 * \return Zero on success or \c GLX_BAD_CONTEXT on failure.
152 int driWaitForMSC32( __DRIdrawablePrivate
*priv
,
153 int64_t target_msc
, int64_t divisor
, int64_t remainder
,
159 if ( divisor
!= 0 ) {
160 unsigned int target
= (unsigned int)target_msc
;
161 unsigned int next
= target
;
163 int dont_wait
= (target_msc
== 0);
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.
170 vbl
.request
.type
= dont_wait
? DRM_VBLANK_RELATIVE
:
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
;
176 if ( drmWaitVBlank( priv
->driScreenPriv
->fd
, &vbl
) != 0 ) {
177 /* FIXME: This doesn't seem like the right thing to return here.
179 return GLX_BAD_CONTEXT
;
182 *msc
= vblank_to_msc(priv
, vbl
.reply
.sequence
);
185 if (target_msc
!= 0 && *msc
== target
)
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.
195 r
= (*msc
% (unsigned int)divisor
);
196 next
= (*msc
- r
+ (unsigned int)remainder
);
197 if (next
<= *msc
) next
+= (unsigned int)divisor
;
199 } while ( r
!= (unsigned int)remainder
);
202 /* If the \c divisor is zero, just wait until the MSC is greater
203 * than or equal to \c target_msc.
206 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
207 vbl
.request
.sequence
= target_msc
? msc_to_vblank(priv
, target_msc
) : 0;
209 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
)
210 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
212 if ( drmWaitVBlank( priv
->driScreenPriv
->fd
, &vbl
) != 0 ) {
213 /* FIXME: This doesn't seem like the right thing to return here.
215 return GLX_BAD_CONTEXT
;
219 *msc
= vblank_to_msc(priv
, vbl
.reply
.sequence
);
221 if ( *msc
< target_msc
) {
222 *msc
+= 0x0000000100000000LL
;
229 /****************************************************************************/
231 * Gets a set of default vertical-blank-wait flags based on the internal GLX
232 * API version and several configuration options.
235 GLuint
driGetDefaultVBlankFlags( const driOptionCache
*optionCache
)
237 GLuint flags
= VBLANK_FLAG_INTERVAL
;
241 if ( driCheckOption( optionCache
, "vblank_mode", DRI_ENUM
) )
242 vblank_mode
= driQueryOptioni( optionCache
, "vblank_mode" );
244 vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
246 switch (vblank_mode
) {
247 case DRI_CONF_VBLANK_NEVER
:
250 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
252 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
253 flags
|= VBLANK_FLAG_THROTTLE
;
255 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
256 flags
|= VBLANK_FLAG_SYNC
;
264 /****************************************************************************/
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.
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.
277 static int do_wait( drmVBlank
* vbl
, GLuint
* vbl_seq
, int fd
)
282 ret
= drmWaitVBlank( fd
, vbl
);
284 static GLboolean first_time
= GL_TRUE
;
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
;
297 *vbl_seq
= vbl
->reply
.sequence
;
302 /****************************************************************************/
304 * Sets the default swap interval when the drawable is first bound to a
305 * direct rendering context.
308 void driDrawableInitVBlank( __DRIdrawablePrivate
*priv
)
310 if ( priv
->swap_interval
== (unsigned)-1 ) {
311 /* Get current vertical blank sequence */
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
;
321 priv
->swap_interval
=
322 (priv
->vblFlags
& (VBLANK_FLAG_THROTTLE
| VBLANK_FLAG_SYNC
)) ? 1 : 0;
327 /****************************************************************************/
329 * Returns the current swap interval of the given drawable.
333 driGetVBlankInterval( const __DRIdrawablePrivate
*priv
, GLuint flags
)
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 );
340 return priv
->swap_interval
;
342 else if ( (flags
& (VBLANK_FLAG_THROTTLE
| VBLANK_FLAG_SYNC
)) != 0 ) {
351 /****************************************************************************/
353 * Returns the current vertical blank sequence number of the given drawable.
357 driGetCurrentVBlank( const __DRIdrawablePrivate
*priv
, GLuint flags
,
362 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
363 if ( flags
& VBLANK_FLAG_SECONDARY
) {
364 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
366 vbl
.request
.sequence
= 0;
368 (void) do_wait( &vbl
, vbl_seq
, priv
->driScreenPriv
->fd
);
372 /****************************************************************************/
374 * Waits for the vertical blank for use with glXSwapBuffers.
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.
387 driWaitForVBlank( const __DRIdrawablePrivate
*priv
, GLuint
* vbl_seq
,
388 GLuint flags
, GLboolean
* missed_deadline
)
391 unsigned original_seq
;
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 ) {
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.
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.
415 original_seq
= *vbl_seq
;
416 interval
= driGetVBlankInterval(priv
, flags
);
417 deadline
= original_seq
+ interval
;
419 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
420 if ( flags
& VBLANK_FLAG_SECONDARY
) {
421 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
423 vbl
.request
.sequence
= ((flags
& VBLANK_FLAG_SYNC
) != 0) ? 1 : 0;
425 if ( do_wait( & vbl
, vbl_seq
, priv
->driScreenPriv
->fd
) != 0 ) {
429 diff
= *vbl_seq
- deadline
;
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
;
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
;
442 vbl
.request
.sequence
= deadline
;
444 if ( do_wait( & vbl
, vbl_seq
, priv
->driScreenPriv
->fd
) != 0 ) {
448 diff
= *vbl_seq
- deadline
;
449 *missed_deadline
= diff
> 0 && diff
<= (1 << 23);