Add helper function that returns the current vblank sequence of a drawable.
[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
39 /****************************************************************************/
40 /**
41 * Get the current MSC refresh counter.
42 *
43 * Stores the 64-bit count of vertical refreshes since some (arbitrary)
44 * point in time in \c count. Unless the value wraps around, which it
45 * may, it will never decrease.
46 *
47 * \warning This function is called from \c glXGetVideoSyncSGI, which expects
48 * a \c count of type \c unsigned (32-bit), and \c glXGetSyncValuesOML, which
49 * expects a \c count of type \c int64_t (signed 64-bit). The kernel ioctl
50 * currently always returns a \c sequence of type \c unsigned.
51 *
52 * \param priv Pointer to the DRI screen private struct.
53 * \param count Storage to hold MSC counter.
54 * \return Zero is returned on success. A negative errno value
55 * is returned on failure.
56 */
57 int driGetMSC32( __DRIscreenPrivate * priv, int64_t * count )
58 {
59 drmVBlank vbl;
60 int ret;
61
62 /* Don't wait for anything. Just get the current refresh count. */
63
64 vbl.request.type = DRM_VBLANK_RELATIVE;
65 vbl.request.sequence = 0;
66
67 ret = drmWaitVBlank( priv->fd, &vbl );
68 *count = (int64_t)vbl.reply.sequence;
69
70 return ret;
71 }
72
73
74 /****************************************************************************/
75 /**
76 * Wait for a specified refresh count. This implements most of the
77 * functionality of \c glXWaitForMscOML from the GLX_OML_sync_control spec.
78 * Waits for the \c target_msc refresh. If that has already passed, it
79 * waits until \f$(MSC \bmod divisor)\f$ is equal to \c remainder. If
80 * \c target_msc is 0, use the behavior of glXWaitVideoSyncSGI(), which
81 * omits the initial check against a target MSC value.
82 *
83 * This function is actually something of a hack. The problem is that, at
84 * the time of this writing, none of the existing DRM modules support an
85 * ioctl that returns a 64-bit count (at least not on 32-bit platforms).
86 * However, this function exists to support a GLX function that requires
87 * the use of 64-bit counts. As such, there is a little bit of ugly
88 * hackery at the end of this function to make the 32-bit count act like
89 * a 64-bit count. There are still some cases where this will break, but
90 * I believe it catches the most common cases.
91 *
92 * The real solution is to provide an ioctl that uses a 64-bit count.
93 *
94 * \param dpy Pointer to the \c Display.
95 * \param priv Pointer to the DRI drawable private.
96 * \param target_msc Desired refresh count to wait for. A value of 0
97 * means to use the glXWaitVideoSyncSGI() behavior.
98 * \param divisor MSC divisor if \c target_msc is already reached.
99 * \param remainder Desired MSC remainder if \c target_msc is already
100 * reached.
101 * \param msc Buffer to hold MSC when done waiting.
102 *
103 * \return Zero on success or \c GLX_BAD_CONTEXT on failure.
104 */
105
106 int driWaitForMSC32( __DRIdrawablePrivate *priv,
107 int64_t target_msc, int64_t divisor, int64_t remainder,
108 int64_t * msc )
109 {
110 drmVBlank vbl;
111
112
113 if ( divisor != 0 ) {
114 unsigned int target = (unsigned int)target_msc;
115 unsigned int next = target;
116 unsigned int r;
117 int dont_wait = (target_msc == 0);
118
119 do {
120 /* dont_wait means we're using the glXWaitVideoSyncSGI() behavior.
121 * The first time around, just get the current count and proceed
122 * to the test for (MSC % divisor) == remainder.
123 */
124 vbl.request.type = dont_wait ? DRM_VBLANK_RELATIVE :
125 DRM_VBLANK_ABSOLUTE;
126 vbl.request.sequence = next;
127
128 if ( drmWaitVBlank( priv->driScreenPriv->fd, &vbl ) != 0 ) {
129 /* FIXME: This doesn't seem like the right thing to return here.
130 */
131 return GLX_BAD_CONTEXT;
132 }
133
134 dont_wait = 0;
135 if (target_msc != 0 && vbl.reply.sequence == target)
136 break;
137
138 /* Assuming the wait-done test fails, the next refresh to wait for
139 * will be one that satisfies (MSC % divisor) == remainder. The
140 * value (MSC - (MSC % divisor) + remainder) is the refresh value
141 * closest to the current value that would satisfy the equation.
142 * If this refresh has already happened, we add divisor to obtain
143 * the next refresh after the current one that will satisfy it.
144 */
145 r = (vbl.reply.sequence % (unsigned int)divisor);
146 next = (vbl.reply.sequence - r + (unsigned int)remainder);
147 if (next <= vbl.reply.sequence) next += (unsigned int)divisor;
148
149 } while ( r != (unsigned int)remainder );
150 }
151 else {
152 /* If the \c divisor is zero, just wait until the MSC is greater
153 * than or equal to \c target_msc.
154 */
155
156 vbl.request.type = DRM_VBLANK_ABSOLUTE;
157 vbl.request.sequence = target_msc;
158
159 if ( drmWaitVBlank( priv->driScreenPriv->fd, &vbl ) != 0 ) {
160 /* FIXME: This doesn't seem like the right thing to return here.
161 */
162 return GLX_BAD_CONTEXT;
163 }
164 }
165
166 *msc = (target_msc & 0xffffffff00000000LL);
167 *msc |= vbl.reply.sequence;
168 if ( *msc < target_msc ) {
169 *msc += 0x0000000100000000LL;
170 }
171
172 return 0;
173 }
174
175
176 /****************************************************************************/
177 /**
178 * Gets a set of default vertical-blank-wait flags based on the internal GLX
179 * API version and several configuration options.
180 */
181
182 GLuint driGetDefaultVBlankFlags( const driOptionCache *optionCache )
183 {
184 GLuint flags = VBLANK_FLAG_INTERVAL;
185 int vblank_mode;
186
187
188 if ( driCheckOption( optionCache, "vblank_mode", DRI_ENUM ) )
189 vblank_mode = driQueryOptioni( optionCache, "vblank_mode" );
190 else
191 vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
192
193 switch (vblank_mode) {
194 case DRI_CONF_VBLANK_NEVER:
195 flags = 0;
196 break;
197 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
198 break;
199 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
200 flags |= VBLANK_FLAG_THROTTLE;
201 break;
202 case DRI_CONF_VBLANK_ALWAYS_SYNC:
203 flags |= VBLANK_FLAG_SYNC;
204 break;
205 }
206
207 return flags;
208 }
209
210
211 /****************************************************************************/
212 /**
213 * Wrapper to call \c drmWaitVBlank. The main purpose of this function is to
214 * wrap the error message logging. The error message should only be logged
215 * the first time the \c drmWaitVBlank fails. If \c drmWaitVBlank is
216 * successful, \c vbl_seq will be set the sequence value in the reply.
217 *
218 * \param vbl Pointer to drmVBlank packet desribing how to wait.
219 * \param vbl_seq Location to store the current refresh counter.
220 * \param fd File descriptor use to call into the DRM.
221 * \return Zero on success or -1 on failure.
222 */
223
224 static int do_wait( drmVBlank * vbl, GLuint * vbl_seq, int fd )
225 {
226 int ret;
227
228
229 ret = drmWaitVBlank( fd, vbl );
230 if ( ret != 0 ) {
231 static GLboolean first_time = GL_TRUE;
232
233 if ( first_time ) {
234 fprintf(stderr,
235 "%s: drmWaitVBlank returned %d, IRQs don't seem to be"
236 " working correctly.\nTry running with LIBGL_THROTTLE_REFRESH"
237 " and LIBL_SYNC_REFRESH unset.\n", __FUNCTION__, ret);
238 first_time = GL_FALSE;
239 }
240
241 return -1;
242 }
243
244 *vbl_seq = vbl->reply.sequence;
245 return 0;
246 }
247
248
249 /****************************************************************************/
250 /**
251 * Sets the default swap interval when the drawable is first bound to a
252 * direct rendering context.
253 */
254
255 void driDrawableInitVBlank( __DRIdrawablePrivate *priv, GLuint flags,
256 GLuint *vbl_seq )
257 {
258 if ( priv->pdraw->swap_interval == (unsigned)-1 ) {
259 /* Get current vertical blank sequence */
260 drmVBlank vbl = { .request={ .type = DRM_VBLANK_RELATIVE, .sequence = 0 } };
261 do_wait( &vbl, vbl_seq, priv->driScreenPriv->fd );
262
263 priv->pdraw->swap_interval = (flags & (VBLANK_FLAG_THROTTLE |
264 VBLANK_FLAG_SYNC)) != 0 ? 1 : 0;
265 }
266 }
267
268
269 /****************************************************************************/
270 /**
271 * Returns the current swap interval of the given drawable.
272 */
273
274 unsigned
275 driGetVBlankInterval( const __DRIdrawablePrivate *priv, GLuint flags )
276 {
277 if ( (flags & VBLANK_FLAG_INTERVAL) != 0 ) {
278 /* this must have been initialized when the drawable was first bound
279 * to a direct rendering context. */
280 assert ( priv->pdraw->swap_interval != (unsigned)-1 );
281
282 return priv->pdraw->swap_interval;
283 }
284 else if ( (flags & (VBLANK_FLAG_THROTTLE | VBLANK_FLAG_SYNC)) != 0 ) {
285 return 1;
286 }
287 else {
288 return 0;
289 }
290 }
291
292
293 /****************************************************************************/
294 /**
295 * Returns the current vertical blank sequence number of the given drawable.
296 */
297
298 void
299 driGetCurrentVBlank( const __DRIdrawablePrivate *priv, GLuint flags,
300 GLuint *vbl_seq )
301 {
302 drmVBlank vbl;
303
304 vbl.request.type = DRM_VBLANK_RELATIVE;
305 if ( flags & VBLANK_FLAG_SECONDARY ) {
306 vbl.request.type |= DRM_VBLANK_SECONDARY;
307 }
308 vbl.request.sequence = 0;
309
310 (void) do_wait( &vbl, vbl_seq, priv->driScreenPriv->fd );
311 }
312
313
314 /****************************************************************************/
315 /**
316 * Waits for the vertical blank for use with glXSwapBuffers.
317 *
318 * \param vbl_seq Vertical blank sequence number (MSC) after the last buffer
319 * swap. Updated after this wait.
320 * \param flags \c VBLANK_FLAG bits that control how long to wait.
321 * \param missed_deadline Set to \c GL_TRUE if the MSC after waiting is later
322 * than the "target" based on \c flags. The idea is that if
323 * \c missed_deadline is set, then the application is not
324 * achieving its desired framerate.
325 * \return Zero on success, -1 on error.
326 */
327
328 int
329 driWaitForVBlank( const __DRIdrawablePrivate *priv, GLuint * vbl_seq,
330 GLuint flags, GLboolean * missed_deadline )
331 {
332 drmVBlank vbl;
333 unsigned original_seq;
334 unsigned deadline;
335 unsigned interval;
336 unsigned diff;
337
338 *missed_deadline = GL_FALSE;
339 if ( (flags & (VBLANK_FLAG_INTERVAL |
340 VBLANK_FLAG_THROTTLE |
341 VBLANK_FLAG_SYNC)) == 0 ||
342 (flags & VBLANK_FLAG_NO_IRQ) != 0 ) {
343 return 0;
344 }
345
346
347 /* VBLANK_FLAG_SYNC means to wait for at least one vertical blank. If
348 * that flag is not set, do a fake wait for zero vertical blanking
349 * periods so that we can get the current MSC.
350 *
351 * VBLANK_FLAG_INTERVAL and VBLANK_FLAG_THROTTLE mean to wait for at
352 * least one vertical blank since the last wait. Since do_wait modifies
353 * vbl_seq, we have to save the original value of vbl_seq for the
354 * VBLANK_FLAG_INTERVAL / VBLANK_FLAG_THROTTLE calculation later.
355 */
356
357 original_seq = *vbl_seq;
358 interval = driGetVBlankInterval(priv, flags);
359 deadline = original_seq + interval;
360
361 vbl.request.type = DRM_VBLANK_RELATIVE;
362 if ( flags & VBLANK_FLAG_SECONDARY ) {
363 vbl.request.type |= DRM_VBLANK_SECONDARY;
364 }
365 vbl.request.sequence = ((flags & VBLANK_FLAG_SYNC) != 0) ? 1 : 0;
366
367 if ( do_wait( & vbl, vbl_seq, priv->driScreenPriv->fd ) != 0 ) {
368 return -1;
369 }
370
371 diff = *vbl_seq - deadline;
372
373 /* No need to wait again if we've already reached the target */
374 if (diff <= (1 << 23)) {
375 *missed_deadline = (flags & VBLANK_FLAG_SYNC) ? (diff > 0) : GL_TRUE;
376 return 0;
377 }
378
379 /* Wait until the target vertical blank. */
380 vbl.request.type = DRM_VBLANK_ABSOLUTE;
381 if ( flags & VBLANK_FLAG_SECONDARY ) {
382 vbl.request.type |= DRM_VBLANK_SECONDARY;
383 }
384 vbl.request.sequence = deadline;
385
386 if ( do_wait( & vbl, vbl_seq, priv->driScreenPriv->fd ) != 0 ) {
387 return -1;
388 }
389
390 diff = *vbl_seq - deadline;
391 *missed_deadline = diff > 0 && diff <= (1 << 23);
392
393 return 0;
394 }