1 /* $XFree86: xc/lib/GL/dri/dri_util.c,v 1.7 2003/04/28 17:01:25 dawes Exp $ */
4 * DRI utility functions.
6 * This module acts as glue between GLX and the actual hardware driver. A DRI
7 * driver doesn't really \e have to use any of this - it's optional. But, some
8 * useful stuff is done here that otherwise would have to be duplicated in most
11 * Basically, these utility functions take care of some of the dirty details of
12 * screen initialization, context creation, context binding, DRM setup, etc.
14 * These functions are compiled into each DRI driver so libGL.so knows nothing
26 #define MAP_FAILED ((void *)-1)
33 #include "drm_sarea.h"
35 #ifndef GLX_OML_sync_control
36 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRIdrawable
*drawable
, int32_t *numerator
, int32_t *denominator
);
39 /* This pointer *must* be set by the driver's __driCreateNewScreen funciton!
41 const __DRIinterfaceMethods
* dri_interface
= NULL
;
44 * This is used in a couple of places that call \c driCreateNewDrawable.
46 static const int empty_attribute_list
[1] = { None
};
50 * This is just a token extension used to signal that the driver
51 * supports setting a read drawable.
53 const __DRIextension driReadDrawableExtension
= {
58 * Cached copy of the internal API version used by libGL and the client-side
61 static int api_ver
= 0;
63 static void *driCreateNewDrawable(__DRIscreen
*screen
,
64 const __GLcontextModes
*modes
,
66 drm_drawable_t hwDrawable
,
67 int renderType
, const int *attrs
);
69 static void driDestroyDrawable(__DRIdrawable
*drawable
);
73 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
76 * Is called from the drivers.
78 * \param f \c printf like format string.
81 __driUtilMessage(const char *f
, ...)
85 if (getenv("LIBGL_DEBUG")) {
86 fprintf(stderr
, "libGL error: \n");
88 vfprintf(stderr
, f
, args
);
90 fprintf(stderr
, "\n");
95 /*****************************************************************/
96 /** \name Context (un)binding functions */
97 /*****************************************************************/
103 * \param scrn the screen.
106 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
109 * This function calls __DriverAPIRec::UnbindContext, and then decrements
110 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
113 * While casting the opaque private pointers associated with the parameters
114 * into their respective real types it also assures they are not \c NULL.
116 static GLboolean
driUnbindContext(__DRIcontext
*ctx
)
118 __DRIcontextPrivate
*pcp
;
119 __DRIscreenPrivate
*psp
;
120 __DRIdrawablePrivate
*pdp
;
121 __DRIdrawablePrivate
*prp
;
124 ** Assume error checking is done properly in glXMakeCurrent before
125 ** calling driUnbindContext.
131 pcp
= (__DRIcontextPrivate
*)ctx
->private;
132 psp
= (__DRIscreenPrivate
*)pcp
->driScreenPriv
;
133 pdp
= (__DRIdrawablePrivate
*)pcp
->driDrawablePriv
;
134 prp
= (__DRIdrawablePrivate
*)pcp
->driReadablePriv
;
136 /* Let driver unbind drawable from context */
137 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
139 if (pdp
->refcount
== 0) {
147 if (prp
->refcount
== 0) {
156 /* XXX this is disabled so that if we call SwapBuffers on an unbound
157 * window we can determine the last context bound to the window and
158 * use that context's lock. (BrianP, 2-Dec-2000)
161 /* Unbind the drawable */
162 pcp
->driDrawablePriv
= NULL
;
163 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
171 * This function takes both a read buffer and a draw buffer. This is needed
172 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
175 static GLboolean
DoBindContext(__DRIcontext
*ctx
,
176 __DRIdrawable
*pdraw
,
177 __DRIdrawable
*pread
)
179 __DRIdrawablePrivate
*pdp
;
180 __DRIdrawablePrivate
*prp
;
181 __DRIcontextPrivate
* const pcp
= ctx
->private;
182 __DRIscreenPrivate
*psp
= pcp
->driScreenPriv
;
184 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
185 prp
= (__DRIdrawablePrivate
*) pread
->private;
187 /* Bind the drawable to the context */
188 pcp
->driDrawablePriv
= pdp
;
189 pcp
->driReadablePriv
= prp
;
190 pdp
->driContextPriv
= pcp
;
197 ** Now that we have a context associated with this drawable, we can
198 ** initialize the drawable information if has not been done before.
200 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
201 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
202 __driUtilUpdateDrawableInfo(pdp
);
203 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
206 if ((pdp
!= prp
) && (!prp
->pStamp
|| *prp
->pStamp
!= prp
->lastStamp
)) {
207 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
208 __driUtilUpdateDrawableInfo(prp
);
209 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
212 /* Call device-specific MakeCurrent */
213 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
220 * This function takes both a read buffer and a draw buffer. This is needed
221 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
224 static GLboolean
driBindContext(__DRIcontext
* ctx
,
225 __DRIdrawable
*pdraw
,
226 __DRIdrawable
*pread
)
229 ** Assume error checking is done properly in glXMakeCurrent before
230 ** calling driBindContext.
233 if (ctx
== NULL
|| pdraw
== None
|| pread
== None
)
236 return DoBindContext( ctx
, pdraw
, pread
);
241 /*****************************************************************/
242 /** \name Drawable handling functions */
243 /*****************************************************************/
247 * Update private drawable information.
249 * \param pdp pointer to the private drawable information to update.
251 * This function basically updates the __DRIdrawablePrivate struct's
252 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
253 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
254 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
255 * the values are different that means we have to update the clipping
259 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
261 __DRIscreenPrivate
*psp
;
262 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
265 || ((pdp
!= pcp
->driDrawablePriv
) && (pdp
!= pcp
->driReadablePriv
))) {
267 * ...but we must ignore it. There can be many contexts bound to a
272 psp
= pdp
->driScreenPriv
;
275 _mesa_problem(NULL
, "Warning! Possible infinite loop due to bug "
276 "in file %s, line %d\n",
281 if (pdp
->pClipRects
) {
282 _mesa_free(pdp
->pClipRects
);
283 pdp
->pClipRects
= NULL
;
286 if (pdp
->pBackClipRects
) {
287 _mesa_free(pdp
->pBackClipRects
);
288 pdp
->pBackClipRects
= NULL
;
291 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
293 if (! (*dri_interface
->getDrawableInfo
)(pdp
->pdraw
,
294 &pdp
->index
, &pdp
->lastStamp
,
295 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
296 &pdp
->numClipRects
, &pdp
->pClipRects
,
299 &pdp
->numBackClipRects
,
300 &pdp
->pBackClipRects
)) {
301 /* Error -- eg the window may have been destroyed. Keep going
304 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
305 pdp
->numClipRects
= 0;
306 pdp
->pClipRects
= NULL
;
307 pdp
->numBackClipRects
= 0;
308 pdp
->pBackClipRects
= NULL
;
311 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
313 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
319 /*****************************************************************/
320 /** \name GLX callbacks */
321 /*****************************************************************/
327 * \param drawablePrivate opaque pointer to the per-drawable private info.
330 * This function calls __DRIdrawablePrivate::swapBuffers.
332 * Is called directly from glXSwapBuffers().
334 static void driSwapBuffers(__DRIdrawable
*drawable
)
336 __DRIdrawablePrivate
*dPriv
= drawable
->private;
337 drm_clip_rect_t rect
;
339 dPriv
->swapBuffers(dPriv
);
341 /* Check that we actually have the new damage report method */
342 if (api_ver
< 20070105 || dri_interface
->reportDamage
== NULL
)
345 /* Assume it's affecting the whole drawable for now */
348 rect
.x2
= rect
.x1
+ dPriv
->w
;
349 rect
.y2
= rect
.y1
+ dPriv
->h
;
351 /* Report the damage. Currently, all our drivers draw directly to the
352 * front buffer, so we report the damage there rather than to the backing
355 (*dri_interface
->reportDamage
)(dPriv
->pdraw
, dPriv
->x
, dPriv
->y
,
360 * Called directly from a number of higher-level GLX functions.
362 static int driGetMSC( __DRIscreen
*screen
, int64_t *msc
)
364 __DRIscreenPrivate
*sPriv
= screen
->private;
366 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
369 static int driWaitForMSC(__DRIdrawable
*drawable
, int64_t target_msc
,
370 int64_t divisor
, int64_t remainder
,
371 int64_t * msc
, int64_t * sbc
)
373 __DRIdrawablePrivate
*dPriv
= drawable
->private;
378 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
382 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
383 * is supported but GLX_OML_sync_control is not. Therefore, don't return
384 * an error value if GetSwapInfo() is not implemented.
387 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
388 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
389 *sbc
= sInfo
.swap_count
;
395 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension
= {
396 { __DRI_MEDIA_STREAM_COUNTER
},
401 static void driCopySubBuffer(__DRIdrawable
*drawable
,
402 int x
, int y
, int w
, int h
)
404 __DRIdrawablePrivate
*dPriv
= drawable
->private;
405 dPriv
->driScreenPriv
->DriverAPI
.CopySubBuffer(dPriv
, x
, y
, w
, h
);
408 const __DRIcopySubBufferExtension driCopySubBufferExtension
= {
409 { __DRI_COPY_SUB_BUFFER
}, driCopySubBuffer
412 static void driSetSwapInterval(__DRIdrawable
*drawable
, unsigned int interval
)
414 __DRIdrawablePrivate
*dpriv
= drawable
->private;
416 dpriv
->swap_interval
= interval
;
419 static unsigned int driGetSwapInterval(__DRIdrawable
*drawable
)
421 __DRIdrawablePrivate
*dpriv
= drawable
->private;
423 return dpriv
->swap_interval
;
426 const __DRIswapControlExtension driSwapControlExtension
= {
427 { __DRI_SWAP_CONTROL
},
434 * This is called via __DRIscreenRec's createNewDrawable pointer.
436 static void *driCreateNewDrawable(__DRIscreen
*screen
,
437 const __GLcontextModes
*modes
,
438 __DRIdrawable
*pdraw
,
439 drm_drawable_t hwDrawable
,
443 __DRIscreenPrivate
*psp
;
444 __DRIdrawablePrivate
*pdp
;
447 pdraw
->private = NULL
;
449 /* Since pbuffers are not yet supported, no drawable attributes are
454 pdp
= (__DRIdrawablePrivate
*)_mesa_malloc(sizeof(__DRIdrawablePrivate
));
459 pdp
->hHWDrawable
= hwDrawable
;
469 pdp
->numClipRects
= 0;
470 pdp
->numBackClipRects
= 0;
471 pdp
->pClipRects
= NULL
;
472 pdp
->pBackClipRects
= NULL
;
474 psp
= (__DRIscreenPrivate
*)screen
->private;
475 pdp
->driScreenPriv
= psp
;
476 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
478 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
479 renderType
== GLX_PIXMAP_BIT
)) {
484 pdraw
->private = pdp
;
485 pdraw
->destroyDrawable
= driDestroyDrawable
;
486 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
488 /* This special default value is replaced with the configured
489 * default value when the drawable is first bound to a direct
492 pdp
->swap_interval
= (unsigned)-1;
494 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
500 driDestroyDrawable(__DRIdrawable
*drawable
)
502 __DRIdrawablePrivate
*pdp
= drawable
->private;
503 __DRIscreenPrivate
*psp
;
506 psp
= pdp
->driScreenPriv
;
507 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
508 if (pdp
->pClipRects
) {
509 _mesa_free(pdp
->pClipRects
);
510 pdp
->pClipRects
= NULL
;
512 if (pdp
->pBackClipRects
) {
513 _mesa_free(pdp
->pBackClipRects
);
514 pdp
->pBackClipRects
= NULL
;
523 /*****************************************************************/
524 /** \name Context handling functions */
525 /*****************************************************************/
529 * Destroy the per-context private information.
531 * \param contextPrivate opaque pointer to the per-drawable private info.
534 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
535 * drmDestroyContext(), and finally frees \p contextPrivate.
538 driDestroyContext(__DRIcontext
*context
)
540 __DRIcontextPrivate
*pcp
= context
->private;
543 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
550 * Create the per-drawable private driver information.
552 * \param dpy The display handle.
553 * \param modes Mode used to create the new context.
554 * \param render_type Type of rendering target. \c GLX_RGBA is the only
555 * type likely to ever be supported for direct-rendering.
556 * \param shared The shared context dependent methods or \c NULL if
558 * \param pctx DRI context to receive the context dependent methods.
560 * \returns An opaque pointer to the per-context private information on
561 * success, or \c NULL on failure.
564 * This function allocates and fills a __DRIcontextPrivateRec structure. It
565 * performs some device independent initialization and passes all the
566 * relevent information to __DriverAPIRec::CreateContext to create the
571 driCreateNewContext(__DRIscreen
*screen
, const __GLcontextModes
*modes
,
572 int render_type
, __DRIcontext
*shared
,
573 drm_context_t hwContext
, __DRIcontext
*pctx
)
575 __DRIcontextPrivate
*pcp
;
576 __DRIcontextPrivate
*pshare
= (shared
!= NULL
) ? shared
->private : NULL
;
577 __DRIscreenPrivate
*psp
;
578 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
580 psp
= (__DRIscreenPrivate
*)screen
->private;
582 pcp
= (__DRIcontextPrivate
*)_mesa_malloc(sizeof(__DRIcontextPrivate
));
587 pcp
->hHWContext
= hwContext
;
588 pcp
->driScreenPriv
= psp
;
589 pcp
->driDrawablePriv
= NULL
;
591 /* When the first context is created for a screen, initialize a "dummy"
595 if (!psp
->dummyContextPriv
.driScreenPriv
) {
596 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
597 psp
->dummyContextPriv
.driScreenPriv
= psp
;
598 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
599 psp
->dummyContextPriv
.driverPrivate
= NULL
;
600 /* No other fields should be used! */
603 pctx
->destroyContext
= driDestroyContext
;
604 pctx
->bindContext
= driBindContext
;
605 pctx
->unbindContext
= driUnbindContext
;
607 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
617 static const __DRIextension
**
618 driGetExtensions(__DRIscreen
*screen
)
620 __DRIscreenPrivate
*psp
= screen
->private;
622 return psp
->extensions
;
625 /*****************************************************************/
626 /** \name Screen handling functions */
627 /*****************************************************************/
631 * Destroy the per-screen private information.
633 * \param dpy the display handle.
634 * \param scrn the screen number.
635 * \param screenPrivate opaque pointer to the per-screen private information.
638 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
639 * drmClose(), and finally frees \p screenPrivate.
641 static void driDestroyScreen(__DRIscreen
*screen
)
643 __DRIscreenPrivate
*psp
= screen
->private;
646 /* No interaction with the X-server is possible at this point. This
647 * routine is called after XCloseDisplay, so there is no protocol
648 * stream open to the X-server anymore.
651 if (psp
->DriverAPI
.DestroyScreen
)
652 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
654 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
655 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
656 (void)drmCloseOnce(psp
->fd
);
664 * This is the bootstrap function for the driver. libGL supplies all of the
665 * requisite information about the system, and the driver initializes itself.
666 * This routine also fills in the linked list pointed to by \c driver_modes
667 * with the \c __GLcontextModes that the driver can support for windows or
670 * \param scrn Index of the screen
671 * \param psc DRI screen data (not driver private)
672 * \param modes Linked list of known display modes. This list is, at a
673 * minimum, a list of modes based on the current display mode.
674 * These roughly match the set of available X11 visuals, but it
675 * need not be limited to X11! The calling libGL should create
676 * a list that will inform the driver of the current display
677 * mode (i.e., color buffer depth, depth buffer depth, etc.).
678 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
680 * \param dri_version Version of the "server-side" DRI.
681 * \param drm_version Version of the kernel DRM.
682 * \param frame_buffer Data describing the location and layout of the
684 * \param pSAREA Pointer the the SAREA.
685 * \param fd Device handle for the DRM.
686 * \param internal_api_version Version of the internal interface between the
688 * \param driverAPI Driver API functions used by other routines in dri_util.c.
690 * \note There is no need to check the minimum API version in this
691 * function. Since the name of this function is versioned, it is
692 * impossible for a loader that is too old to even load this driver.
695 void * __DRI_CREATE_NEW_SCREEN( int scrn
, __DRIscreen
*psc
,
696 const __DRIversion
* ddx_version
,
697 const __DRIversion
* dri_version
,
698 const __DRIversion
* drm_version
,
699 const __DRIframebuffer
* frame_buffer
,
700 drmAddress pSAREA
, int fd
,
701 int internal_api_version
,
702 const __DRIinterfaceMethods
* interface
,
703 __GLcontextModes
** driver_modes
)
706 __DRIscreenPrivate
*psp
;
707 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
708 dri_interface
= interface
;
709 api_ver
= internal_api_version
;
711 psp
= _mesa_malloc(sizeof(*psp
));
718 ** NOT_DONE: This is used by the X server to detect when the client
719 ** has died while holding the drawable lock. The client sets the
720 ** drawable lock to this value.
724 psp
->drm_version
= *drm_version
;
725 psp
->ddx_version
= *ddx_version
;
726 psp
->dri_version
= *dri_version
;
728 psp
->pSAREA
= pSAREA
;
730 psp
->pFB
= frame_buffer
->base
;
731 psp
->fbSize
= frame_buffer
->size
;
732 psp
->fbStride
= frame_buffer
->stride
;
733 psp
->fbWidth
= frame_buffer
->width
;
734 psp
->fbHeight
= frame_buffer
->height
;
735 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
736 psp
->pDevPriv
= frame_buffer
->dev_priv
;
737 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
739 psp
->extensions
= emptyExtensionList
;
744 ** Do not init dummy context here; actual initialization will be
745 ** done when the first DRI context is created. Init screen priv ptr
746 ** to NULL to let CreateContext routine that it needs to be inited.
748 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
750 psc
->destroyScreen
= driDestroyScreen
;
751 psc
->getExtensions
= driGetExtensions
;
752 psc
->createNewDrawable
= driCreateNewDrawable
;
753 psc
->createNewContext
= driCreateNewContext
;
755 if (internal_api_version
>= 20070121)
756 psc
->setTexOffset
= psp
->DriverAPI
.setTexOffset
;
758 *driver_modes
= __driDriverInitScreen(psp
);
759 if (*driver_modes
== NULL
) {
768 * Compare the current GLX API version with a driver supplied required version.
770 * The minimum required version is compared with the API version exported by
771 * the \c __glXGetInternalVersion function (in libGL.so).
773 * \param required_version Minimum required internal GLX API version.
774 * \return A tri-value return, as from strcmp is returned. A value less
775 * than, equal to, or greater than zero will be returned if the
776 * internal GLX API version is less than, equal to, or greater
777 * than \c required_version.
779 * \sa __glXGetInternalVersion().
781 int driCompareGLXAPIVersion( GLint required_version
)
783 if ( api_ver
> required_version
) {
786 else if ( api_ver
== required_version
) {
795 driFrameTracking(__DRIdrawable
*drawable
, GLboolean enable
)
797 return GLX_BAD_CONTEXT
;
801 driQueryFrameTracking(__DRIdrawable
*drawable
,
802 int64_t * sbc
, int64_t * missedFrames
,
803 float * lastMissedUsage
, float * usage
)
808 __DRIdrawablePrivate
* dpriv
= drawable
->private;
811 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
813 *sbc
= sInfo
.swap_count
;
814 *missedFrames
= sInfo
.swap_missed_count
;
815 *lastMissedUsage
= sInfo
.swap_missed_usage
;
817 (*dri_interface
->getUST
)( & ust
);
818 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
824 const __DRIframeTrackingExtension driFrameTrackingExtension
= {
825 { __DRI_FRAME_TRACKING
},
827 driQueryFrameTracking
831 * Calculate amount of swap interval used between GLX buffer swaps.
833 * The usage value, on the range [0,max], is the fraction of total swap
834 * interval time used between GLX buffer swaps is calculated.
836 * \f$p = t_d / (i * t_r)\f$
838 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
839 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
840 * required for a single vertical refresh period (as returned by \c
843 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
846 * \param dPriv Pointer to the private drawable structure.
847 * \return If less than a single swap interval time period was required
848 * between GLX buffer swaps, a number greater than 0 and less than
849 * 1.0 is returned. If exactly one swap interval time period is
850 * required, 1.0 is returned, and if more than one is required then
851 * a number greater than 1.0 will be returned.
853 * \sa glXSwapIntervalSGI glXGetMscRateOML
855 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
856 * be possible to cache the sync rate?
859 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
860 int64_t current_ust
)
868 if ( (*dri_interface
->getMSCRate
)(dPriv
->pdraw
, &n
, &d
) ) {
869 interval
= (dPriv
->swap_interval
!= 0) ? dPriv
->swap_interval
: 1;
872 /* We want to calculate
873 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
874 * current_UST by calling __glXGetUST. last_swap_UST is stored in
875 * dPriv->swap_ust. interval has already been calculated.
877 * The only tricky part is us_per_refresh. us_per_refresh is
878 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
879 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
880 * the denominator of the final calculation, we calculate
881 * (interval * 1000000 * d) and move n into the numerator.
884 usage
= (current_ust
- last_swap_ust
);
886 usage
/= (interval
* d
);