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>
29 #include "main/glheader.h"
31 #include "main/mtypes.h"
32 #include "main/macros.h"
37 static unsigned int msc_to_vblank(__DRIdrawable
* dPriv
, int64_t msc
)
39 return (unsigned int)(msc
- dPriv
->msc_base
+ dPriv
->vblank_base
);
42 static int64_t vblank_to_msc(__DRIdrawable
* dPriv
, unsigned int vblank
)
44 return (int64_t)(vblank
- dPriv
->vblank_base
+ dPriv
->msc_base
);
48 /****************************************************************************/
50 * Get the current MSC refresh counter.
52 * Stores the 64-bit count of vertical refreshes since some (arbitrary)
53 * point in time in \c count. Unless the value wraps around, which it
54 * may, it will never decrease for a given drawable.
56 * \warning This function is called from \c glXGetVideoSyncSGI, which expects
57 * a \c count of type \c unsigned (32-bit), and \c glXGetSyncValuesOML, which
58 * expects a \c count of type \c int64_t (signed 64-bit). The kernel ioctl
59 * currently always returns a \c sequence of type \c unsigned.
61 * \param priv Pointer to the DRI screen private struct.
62 * \param dPriv Pointer to the DRI drawable private struct
63 * \param count Storage to hold MSC counter.
64 * \return Zero is returned on success. A negative errno value
65 * is returned on failure.
67 int driDrawableGetMSC32( __DRIscreen
* priv
,
68 __DRIdrawable
* dPriv
,
74 /* Don't wait for anything. Just get the current refresh count. */
76 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
77 vbl
.request
.sequence
= 0;
78 if ( dPriv
&& dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
)
79 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
81 ret
= drmWaitVBlank( priv
->fd
, &vbl
);
84 *count
= vblank_to_msc(dPriv
, vbl
.reply
.sequence
);
86 /* Old driver (no knowledge of drawable MSC callback) */
87 *count
= vbl
.reply
.sequence
;
93 /****************************************************************************/
95 * Wait for a specified refresh count. This implements most of the
96 * functionality of \c glXWaitForMscOML from the GLX_OML_sync_control spec.
97 * Waits for the \c target_msc refresh. If that has already passed, it
98 * waits until \f$(MSC \bmod divisor)\f$ is equal to \c remainder. If
99 * \c target_msc is 0, use the behavior of glXWaitVideoSyncSGI(), which
100 * omits the initial check against a target MSC value.
102 * This function is actually something of a hack. The problem is that, at
103 * the time of this writing, none of the existing DRM modules support an
104 * ioctl that returns a 64-bit count (at least not on 32-bit platforms).
105 * However, this function exists to support a GLX function that requires
106 * the use of 64-bit counts. As such, there is a little bit of ugly
107 * hackery at the end of this function to make the 32-bit count act like
108 * a 64-bit count. There are still some cases where this will break, but
109 * I believe it catches the most common cases.
111 * The real solution is to provide an ioctl that uses a 64-bit count.
113 * \param dpy Pointer to the \c Display.
114 * \param priv Pointer to the DRI drawable private.
115 * \param target_msc Desired refresh count to wait for. A value of 0
116 * means to use the glXWaitVideoSyncSGI() behavior.
117 * \param divisor MSC divisor if \c target_msc is already reached.
118 * \param remainder Desired MSC remainder if \c target_msc is already
120 * \param msc Buffer to hold MSC when done waiting.
122 * \return Zero on success or \c GLX_BAD_CONTEXT on failure.
125 int driWaitForMSC32( __DRIdrawable
*priv
,
126 int64_t target_msc
, int64_t divisor
, int64_t remainder
,
132 if ( divisor
!= 0 ) {
133 int64_t next
= target_msc
;
135 int dont_wait
= (target_msc
== 0);
138 /* dont_wait means we're using the glXWaitVideoSyncSGI() behavior.
139 * The first time around, just get the current count and proceed
140 * to the test for (MSC % divisor) == remainder.
142 vbl
.request
.type
= dont_wait
? DRM_VBLANK_RELATIVE
:
144 vbl
.request
.sequence
= next
? msc_to_vblank(priv
, next
) : 0;
145 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
)
146 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
148 if ( drmWaitVBlank( priv
->driScreenPriv
->fd
, &vbl
) != 0 ) {
149 /* FIXME: This doesn't seem like the right thing to return here.
151 return GLX_BAD_CONTEXT
;
154 *msc
= vblank_to_msc(priv
, vbl
.reply
.sequence
);
156 if (!dont_wait
&& *msc
== next
)
160 /* Assuming the wait-done test fails, the next refresh to wait for
161 * will be one that satisfies (MSC % divisor) == remainder. The
162 * value (MSC - (MSC % divisor) + remainder) is the refresh value
163 * closest to the current value that would satisfy the equation.
164 * If this refresh has already happened, we add divisor to obtain
165 * the next refresh after the current one that will satisfy it.
167 r
= ((uint64_t)*msc
% divisor
);
168 next
= (*msc
- r
+ remainder
);
172 } while (r
!= remainder
);
175 /* If the \c divisor is zero, just wait until the MSC is greater
176 * than or equal to \c target_msc.
179 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
180 vbl
.request
.sequence
= target_msc
? msc_to_vblank(priv
, target_msc
) : 0;
182 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
)
183 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
185 if ( drmWaitVBlank( priv
->driScreenPriv
->fd
, &vbl
) != 0 ) {
186 /* FIXME: This doesn't seem like the right thing to return here.
188 return GLX_BAD_CONTEXT
;
192 *msc
= vblank_to_msc(priv
, vbl
.reply
.sequence
);
194 if ( *msc
< target_msc
) {
195 *msc
+= 0x0000000100000000LL
;
202 /****************************************************************************/
204 * Gets a set of default vertical-blank-wait flags based on the internal GLX
205 * API version and several configuration options.
208 GLuint
driGetDefaultVBlankFlags( const driOptionCache
*optionCache
)
210 GLuint flags
= VBLANK_FLAG_INTERVAL
;
214 if ( driCheckOption( optionCache
, "vblank_mode", DRI_ENUM
) )
215 vblank_mode
= driQueryOptioni( optionCache
, "vblank_mode" );
217 vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
219 switch (vblank_mode
) {
220 case DRI_CONF_VBLANK_NEVER
:
223 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
225 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
226 flags
|= VBLANK_FLAG_THROTTLE
;
228 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
229 flags
|= VBLANK_FLAG_SYNC
;
237 /****************************************************************************/
239 * Wrapper to call \c drmWaitVBlank. The main purpose of this function is to
240 * wrap the error message logging. The error message should only be logged
241 * the first time the \c drmWaitVBlank fails. If \c drmWaitVBlank is
242 * successful, \c vbl_seq will be set the sequence value in the reply.
244 * \param vbl Pointer to drmVBlank packet describing how to wait.
245 * \param vbl_seq Location to store the current refresh counter.
246 * \param fd File descriptor use to call into the DRM.
247 * \return Zero on success or -1 on failure.
250 static int do_wait( drmVBlank
* vbl
, GLuint
* vbl_seq
, int fd
)
255 ret
= drmWaitVBlank( fd
, vbl
);
257 static GLboolean first_time
= GL_TRUE
;
261 "%s: drmWaitVBlank returned %d, IRQs don't seem to be"
262 " working correctly.\nTry adjusting the vblank_mode"
263 " configuration parameter.\n", __FUNCTION__
, ret
);
264 first_time
= GL_FALSE
;
270 *vbl_seq
= vbl
->reply
.sequence
;
275 /****************************************************************************/
277 * Returns the default swap interval of the given drawable.
281 driGetDefaultVBlankInterval( const __DRIdrawable
*priv
)
283 if ( (priv
->vblFlags
& (VBLANK_FLAG_THROTTLE
| VBLANK_FLAG_SYNC
)) != 0 ) {
292 /****************************************************************************/
294 * Sets the default swap interval when the drawable is first bound to a
295 * direct rendering context.
298 void driDrawableInitVBlank( __DRIdrawable
*priv
)
300 if ( priv
->swap_interval
== (unsigned)-1 &&
301 !( priv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ) {
302 /* Get current vertical blank sequence */
305 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
306 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
)
307 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
308 vbl
.request
.sequence
= 0;
309 do_wait( &vbl
, &priv
->vblSeq
, priv
->driScreenPriv
->fd
);
310 priv
->vblank_base
= priv
->vblSeq
;
312 priv
->swap_interval
= driGetDefaultVBlankInterval( priv
);
317 /****************************************************************************/
319 * Returns the current swap interval of the given drawable.
323 driGetVBlankInterval( const __DRIdrawable
*priv
)
325 if ( (priv
->vblFlags
& VBLANK_FLAG_INTERVAL
) != 0 ) {
326 /* this must have been initialized when the drawable was first bound
327 * to a direct rendering context. */
328 assert ( priv
->swap_interval
!= (unsigned)-1 );
330 return priv
->swap_interval
;
333 return driGetDefaultVBlankInterval( priv
);
337 /****************************************************************************/
339 * Returns the current vertical blank sequence number of the given drawable.
343 driGetCurrentVBlank( __DRIdrawable
*priv
)
347 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
348 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
349 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
351 vbl
.request
.sequence
= 0;
353 (void) do_wait( &vbl
, &priv
->vblSeq
, priv
->driScreenPriv
->fd
);
357 /****************************************************************************/
359 * Waits for the vertical blank for use with glXSwapBuffers.
361 * \param missed_deadline Set to \c GL_TRUE if the MSC after waiting is later
362 * than the "target" based on \c priv->vblFlags. The idea is
363 * that if \c missed_deadline is set, then the application is
364 * not achieving its desired framerate.
365 * \return Zero on success, -1 on error.
369 driWaitForVBlank( __DRIdrawable
*priv
, GLboolean
* missed_deadline
)
372 unsigned original_seq
;
377 *missed_deadline
= GL_FALSE
;
378 if ( (priv
->vblFlags
& (VBLANK_FLAG_INTERVAL
|
379 VBLANK_FLAG_THROTTLE
|
380 VBLANK_FLAG_SYNC
)) == 0 ||
381 (priv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) != 0 ) {
386 /* VBLANK_FLAG_SYNC means to wait for at least one vertical blank. If
387 * that flag is not set, do a fake wait for zero vertical blanking
388 * periods so that we can get the current MSC.
390 * VBLANK_FLAG_INTERVAL and VBLANK_FLAG_THROTTLE mean to wait for at
391 * least one vertical blank since the last wait. Since do_wait modifies
392 * priv->vblSeq, we have to save the original value of priv->vblSeq for the
393 * VBLANK_FLAG_INTERVAL / VBLANK_FLAG_THROTTLE calculation later.
396 original_seq
= priv
->vblSeq
;
397 interval
= driGetVBlankInterval(priv
);
398 deadline
= original_seq
+ interval
;
400 vbl
.request
.type
= DRM_VBLANK_RELATIVE
;
401 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
402 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
404 vbl
.request
.sequence
= ((priv
->vblFlags
& VBLANK_FLAG_SYNC
) != 0) ? 1 : 0;
406 if ( do_wait( & vbl
, &priv
->vblSeq
, priv
->driScreenPriv
->fd
) != 0 ) {
410 diff
= priv
->vblSeq
- deadline
;
412 /* No need to wait again if we've already reached the target */
413 if (diff
<= (1 << 23)) {
414 *missed_deadline
= (priv
->vblFlags
& VBLANK_FLAG_SYNC
) ? (diff
> 0) :
419 /* Wait until the target vertical blank. */
420 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
421 if ( priv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
422 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
424 vbl
.request
.sequence
= deadline
;
426 if ( do_wait( & vbl
, &priv
->vblSeq
, priv
->driScreenPriv
->fd
) != 0 ) {
430 diff
= priv
->vblSeq
- deadline
;
431 *missed_deadline
= diff
> 0 && diff
<= (1 << 23);