3 * DRI utility functions.
5 * This module acts as glue between GLX and the actual hardware driver. A DRI
6 * driver doesn't really \e have to use any of this - it's optional. But, some
7 * useful stuff is done here that otherwise would have to be duplicated in most
10 * Basically, these utility functions take care of some of the dirty details of
11 * screen initialization, context creation, context binding, DRM setup, etc.
13 * These functions are compiled into each DRI driver so libGL.so knows nothing
25 #define MAP_FAILED ((void *)-1)
28 #include "main/imports.h"
32 #include "drm_sarea.h"
35 #ifndef GLX_OML_sync_control
36 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRIdrawable
*drawable
, int32_t *numerator
, int32_t *denominator
);
39 static void dri_get_drawable(__DRIdrawable
*pdp
);
40 static void dri_put_drawable(__DRIdrawable
*pdp
);
43 * This is just a token extension used to signal that the driver
44 * supports setting a read drawable.
46 const __DRIextension driReadDrawableExtension
= {
47 __DRI_READ_DRAWABLE
, __DRI_READ_DRAWABLE_VERSION
51 driIntersectArea( drm_clip_rect_t rect1
, drm_clip_rect_t rect2
)
53 if (rect2
.x1
> rect1
.x1
) rect1
.x1
= rect2
.x1
;
54 if (rect2
.x2
< rect1
.x2
) rect1
.x2
= rect2
.x2
;
55 if (rect2
.y1
> rect1
.y1
) rect1
.y1
= rect2
.y1
;
56 if (rect2
.y2
< rect1
.y2
) rect1
.y2
= rect2
.y2
;
58 if (rect1
.x1
> rect1
.x2
|| rect1
.y1
> rect1
.y2
) return 0;
60 return (rect1
.x2
- rect1
.x1
) * (rect1
.y2
- rect1
.y1
);
63 /*****************************************************************/
64 /** \name Context (un)binding functions */
65 /*****************************************************************/
71 * \param scrn the screen.
74 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
77 * This function calls __DriverAPIRec::UnbindContext, and then decrements
78 * __DRIdrawableRec::refcount which must be non-zero for a successful
81 * While casting the opaque private pointers associated with the parameters
82 * into their respective real types it also assures they are not \c NULL.
84 static int driUnbindContext(__DRIcontext
*pcp
)
91 ** Assume error checking is done properly in glXMakeCurrent before
92 ** calling driUnbindContext.
98 psp
= pcp
->driScreenPriv
;
99 pdp
= pcp
->driDrawablePriv
;
100 prp
= pcp
->driReadablePriv
;
102 /* already unbound */
105 /* Let driver unbind drawable from context */
106 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
109 if (pdp
->refcount
== 0) {
114 dri_put_drawable(pdp
);
117 if (prp
->refcount
== 0) {
122 dri_put_drawable(prp
);
126 /* XXX this is disabled so that if we call SwapBuffers on an unbound
127 * window we can determine the last context bound to the window and
128 * use that context's lock. (BrianP, 2-Dec-2000)
130 pcp
->driDrawablePriv
= pcp
->driReadablePriv
= NULL
;
136 * This function takes both a read buffer and a draw buffer. This is needed
137 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
140 static int driBindContext(__DRIcontext
*pcp
,
144 __DRIscreen
*psp
= NULL
;
146 /* Bind the drawable to the context */
149 psp
= pcp
->driScreenPriv
;
150 pcp
->driDrawablePriv
= pdp
;
151 pcp
->driReadablePriv
= prp
;
153 pdp
->driContextPriv
= pcp
;
154 dri_get_drawable(pdp
);
156 if ( prp
&& pdp
!= prp
) {
157 dri_get_drawable(prp
);
162 ** Now that we have a context associated with this drawable, we can
163 ** initialize the drawable information if has not been done before.
167 if (!psp
->dri2
.enabled
) {
168 if (pdp
&& !pdp
->pStamp
) {
169 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
170 __driUtilUpdateDrawableInfo(pdp
);
171 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
173 if (prp
&& pdp
!= prp
&& !prp
->pStamp
) {
174 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
175 __driUtilUpdateDrawableInfo(prp
);
176 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
180 /* Call device-specific MakeCurrent */
182 return (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
188 /*****************************************************************/
189 /** \name Drawable handling functions */
190 /*****************************************************************/
194 * Update private drawable information.
196 * \param pdp pointer to the private drawable information to update.
198 * This function basically updates the __DRIdrawable struct's
199 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
200 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
201 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
202 * the values are different that means we have to update the clipping
206 __driUtilUpdateDrawableInfo(__DRIdrawable
*pdp
)
208 __DRIscreen
*psp
= pdp
->driScreenPriv
;
209 __DRIcontext
*pcp
= pdp
->driContextPriv
;
212 || ((pdp
!= pcp
->driDrawablePriv
) && (pdp
!= pcp
->driReadablePriv
))) {
214 * ...but we must ignore it. There can be many contexts bound to a
219 if (pdp
->pClipRects
) {
220 free(pdp
->pClipRects
);
221 pdp
->pClipRects
= NULL
;
224 if (pdp
->pBackClipRects
) {
225 free(pdp
->pBackClipRects
);
226 pdp
->pBackClipRects
= NULL
;
229 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
231 if (! (*psp
->getDrawableInfo
->getDrawableInfo
)(pdp
,
232 &pdp
->index
, &pdp
->lastStamp
,
233 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
234 &pdp
->numClipRects
, &pdp
->pClipRects
,
237 &pdp
->numBackClipRects
,
238 &pdp
->pBackClipRects
,
239 pdp
->loaderPrivate
)) {
240 /* Error -- eg the window may have been destroyed. Keep going
243 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
244 pdp
->numClipRects
= 0;
245 pdp
->pClipRects
= NULL
;
246 pdp
->numBackClipRects
= 0;
247 pdp
->pBackClipRects
= NULL
;
250 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
252 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
257 /*****************************************************************/
258 /** \name GLX callbacks */
259 /*****************************************************************/
262 static void driReportDamage(__DRIdrawable
*pdp
,
263 struct drm_clip_rect
*pClipRects
, int numClipRects
)
265 __DRIscreen
*psp
= pdp
->driScreenPriv
;
267 /* Check that we actually have the new damage report method */
269 /* Report the damage. Currently, all our drivers draw
270 * directly to the front buffer, so we report the damage there
271 * rather than to the backing storein (if any).
273 (*psp
->damage
->reportDamage
)(pdp
,
275 pClipRects
, numClipRects
,
276 GL_TRUE
, pdp
->loaderPrivate
);
284 * \param drawablePrivate opaque pointer to the per-drawable private info.
287 * This function calls __DRIdrawable::swapBuffers.
289 * Is called directly from glXSwapBuffers().
291 static void driSwapBuffers(__DRIdrawable
*dPriv
)
293 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
294 drm_clip_rect_t
*rects
;
297 psp
->DriverAPI
.SwapBuffers(dPriv
);
299 if (!dPriv
->numClipRects
)
302 rects
= malloc(sizeof(*rects
) * dPriv
->numClipRects
);
307 for (i
= 0; i
< dPriv
->numClipRects
; i
++) {
308 rects
[i
].x1
= dPriv
->pClipRects
[i
].x1
- dPriv
->x
;
309 rects
[i
].y1
= dPriv
->pClipRects
[i
].y1
- dPriv
->y
;
310 rects
[i
].x2
= dPriv
->pClipRects
[i
].x2
- dPriv
->x
;
311 rects
[i
].y2
= dPriv
->pClipRects
[i
].y2
- dPriv
->y
;
314 driReportDamage(dPriv
, rects
, dPriv
->numClipRects
);
318 static int driDrawableGetMSC( __DRIscreen
*sPriv
, __DRIdrawable
*dPriv
,
321 return sPriv
->DriverAPI
.GetDrawableMSC(sPriv
, dPriv
, msc
);
325 static int driWaitForMSC(__DRIdrawable
*dPriv
, int64_t target_msc
,
326 int64_t divisor
, int64_t remainder
,
327 int64_t * msc
, int64_t * sbc
)
332 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
336 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
337 * is supported but GLX_OML_sync_control is not. Therefore, don't return
338 * an error value if GetSwapInfo() is not implemented.
341 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
342 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
343 *sbc
= sInfo
.swap_count
;
350 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension
= {
351 { __DRI_MEDIA_STREAM_COUNTER
, __DRI_MEDIA_STREAM_COUNTER_VERSION
},
357 static void driCopySubBuffer(__DRIdrawable
*dPriv
,
358 int x
, int y
, int w
, int h
)
360 drm_clip_rect_t rect
;
363 rect
.y1
= dPriv
->h
- y
- h
;
365 rect
.y2
= rect
.y1
+ h
;
366 driReportDamage(dPriv
, &rect
, 1);
368 dPriv
->driScreenPriv
->DriverAPI
.CopySubBuffer(dPriv
, x
, y
, w
, h
);
371 const __DRIcopySubBufferExtension driCopySubBufferExtension
= {
372 { __DRI_COPY_SUB_BUFFER
, __DRI_COPY_SUB_BUFFER_VERSION
},
376 static void driSetSwapInterval(__DRIdrawable
*dPriv
, unsigned int interval
)
378 dPriv
->swap_interval
= interval
;
381 static unsigned int driGetSwapInterval(__DRIdrawable
*dPriv
)
383 return dPriv
->swap_interval
;
386 const __DRIswapControlExtension driSwapControlExtension
= {
387 { __DRI_SWAP_CONTROL
, __DRI_SWAP_CONTROL_VERSION
},
394 * This is called via __DRIscreenRec's createNewDrawable pointer.
396 static __DRIdrawable
*
397 driCreateNewDrawable(__DRIscreen
*psp
, const __DRIconfig
*config
,
398 drm_drawable_t hwDrawable
, int renderType
,
399 const int *attrs
, void *data
)
403 /* Since pbuffers are not yet supported, no drawable attributes are
408 pdp
= malloc(sizeof *pdp
);
413 pdp
->loaderPrivate
= data
;
414 pdp
->hHWDrawable
= hwDrawable
;
423 pdp
->numClipRects
= 0;
424 pdp
->numBackClipRects
= 0;
425 pdp
->pClipRects
= NULL
;
426 pdp
->pBackClipRects
= NULL
;
430 pdp
->driScreenPriv
= psp
;
432 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, &config
->modes
,
433 renderType
== GLX_PIXMAP_BIT
)) {
440 /* This special default value is replaced with the configured
441 * default value when the drawable is first bound to a direct
444 pdp
->swap_interval
= (unsigned)-1;
450 static __DRIdrawable
*
451 dri2CreateNewDrawable(__DRIscreen
*screen
,
452 const __DRIconfig
*config
,
455 __DRIdrawable
*pdraw
;
457 pdraw
= driCreateNewDrawable(screen
, config
, 0, 0, NULL
, loaderPrivate
);
461 pdraw
->pClipRects
= &pdraw
->dri2
.clipRect
;
462 pdraw
->pBackClipRects
= &pdraw
->dri2
.clipRect
;
464 pdraw
->pStamp
= &pdraw
->dri2
.stamp
;
465 *pdraw
->pStamp
= pdraw
->lastStamp
+ 1;
470 static void dri_get_drawable(__DRIdrawable
*pdp
)
475 static void dri_put_drawable(__DRIdrawable
*pdp
)
484 psp
= pdp
->driScreenPriv
;
485 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
486 if (pdp
->pClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
487 free(pdp
->pClipRects
);
488 pdp
->pClipRects
= NULL
;
490 if (pdp
->pBackClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
491 free(pdp
->pBackClipRects
);
492 pdp
->pBackClipRects
= NULL
;
499 driDestroyDrawable(__DRIdrawable
*pdp
)
501 dri_put_drawable(pdp
);
507 /*****************************************************************/
508 /** \name Context handling functions */
509 /*****************************************************************/
513 * Destroy the per-context private information.
516 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
517 * drmDestroyContext(), and finally frees \p contextPrivate.
520 driDestroyContext(__DRIcontext
*pcp
)
523 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
530 * Create the per-drawable private driver information.
532 * \param render_type Type of rendering target. \c GLX_RGBA is the only
533 * type likely to ever be supported for direct-rendering.
534 * \param shared Context with which to share textures, etc. or NULL
536 * \returns An opaque pointer to the per-context private information on
537 * success, or \c NULL on failure.
540 * This function allocates and fills a __DRIcontextRec structure. It
541 * performs some device independent initialization and passes all the
542 * relevent information to __DriverAPIRec::CreateContext to create the
546 static __DRIcontext
*
547 driCreateNewContext(__DRIscreen
*psp
, const __DRIconfig
*config
,
548 int render_type
, __DRIcontext
*shared
,
549 drm_context_t hwContext
, void *data
)
552 void * const shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
554 pcp
= malloc(sizeof *pcp
);
558 pcp
->driScreenPriv
= psp
;
559 pcp
->driDrawablePriv
= NULL
;
560 pcp
->loaderPrivate
= data
;
562 pcp
->dri2
.draw_stamp
= 0;
563 pcp
->dri2
.read_stamp
= 0;
565 pcp
->hHWContext
= hwContext
;
567 if ( !(*psp
->DriverAPI
.CreateContext
)(API_OPENGL
,
568 &config
->modes
, pcp
, shareCtx
) ) {
577 dri2GetAPIMask(__DRIscreen
*screen
)
579 return screen
->api_mask
;
582 static __DRIcontext
*
583 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
584 const __DRIconfig
*config
,
585 __DRIcontext
*shared
, void *data
)
587 __DRIcontext
*context
;
588 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
591 if (!(screen
->api_mask
& (1 << api
)))
595 case __DRI_API_OPENGL
:
596 mesa_api
= API_OPENGL
;
599 mesa_api
= API_OPENGLES
;
601 case __DRI_API_GLES2
:
602 mesa_api
= API_OPENGLES2
;
606 context
= malloc(sizeof *context
);
610 context
->driScreenPriv
= screen
;
611 context
->driDrawablePriv
= NULL
;
612 context
->loaderPrivate
= data
;
614 if (!(*screen
->DriverAPI
.CreateContext
)(api
, &config
->modes
,
615 context
, shareCtx
) ) {
624 static __DRIcontext
*
625 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
626 __DRIcontext
*shared
, void *data
)
628 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
629 config
, shared
, data
);
633 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
641 /*****************************************************************/
642 /** \name Screen handling functions */
643 /*****************************************************************/
647 * Destroy the per-screen private information.
650 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
651 * drmClose(), and finally frees \p screenPrivate.
653 static void driDestroyScreen(__DRIscreen
*psp
)
656 /* No interaction with the X-server is possible at this point. This
657 * routine is called after XCloseDisplay, so there is no protocol
658 * stream open to the X-server anymore.
661 if (psp
->DriverAPI
.DestroyScreen
)
662 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
664 if (!psp
->dri2
.enabled
) {
665 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
666 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
667 (void)drmCloseOnce(psp
->fd
);
675 setupLoaderExtensions(__DRIscreen
*psp
,
676 const __DRIextension
**extensions
)
680 for (i
= 0; extensions
[i
]; i
++) {
681 if (strcmp(extensions
[i
]->name
, __DRI_GET_DRAWABLE_INFO
) == 0)
682 psp
->getDrawableInfo
= (__DRIgetDrawableInfoExtension
*) extensions
[i
];
683 if (strcmp(extensions
[i
]->name
, __DRI_DAMAGE
) == 0)
684 psp
->damage
= (__DRIdamageExtension
*) extensions
[i
];
685 if (strcmp(extensions
[i
]->name
, __DRI_SYSTEM_TIME
) == 0)
686 psp
->systemTime
= (__DRIsystemTimeExtension
*) extensions
[i
];
687 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
688 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
689 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
690 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
695 * This is the bootstrap function for the driver. libGL supplies all of the
696 * requisite information about the system, and the driver initializes itself.
697 * This routine also fills in the linked list pointed to by \c driver_modes
698 * with the \c __GLcontextModes that the driver can support for windows or
703 * \param scrn Index of the screen
704 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
706 * \param dri_version Version of the "server-side" DRI.
707 * \param drm_version Version of the kernel DRM.
708 * \param frame_buffer Data describing the location and layout of the
710 * \param pSAREA Pointer to the SAREA.
711 * \param fd Device handle for the DRM.
712 * \param extensions ??
713 * \param driver_modes Returns modes suppoted by the driver
714 * \param loaderPrivate ??
716 * \note There is no need to check the minimum API version in this
717 * function. Since the name of this function is versioned, it is
718 * impossible for a loader that is too old to even load this driver.
721 driCreateNewScreen(int scrn
,
722 const __DRIversion
*ddx_version
,
723 const __DRIversion
*dri_version
,
724 const __DRIversion
*drm_version
,
725 const __DRIframebuffer
*frame_buffer
,
726 drmAddress pSAREA
, int fd
,
727 const __DRIextension
**extensions
,
728 const __DRIconfig
***driver_modes
,
731 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
734 psp
= calloc(1, sizeof *psp
);
738 setupLoaderExtensions(psp
, extensions
);
741 ** NOT_DONE: This is used by the X server to detect when the client
742 ** has died while holding the drawable lock. The client sets the
743 ** drawable lock to this value.
747 psp
->drm_version
= *drm_version
;
748 psp
->ddx_version
= *ddx_version
;
749 psp
->dri_version
= *dri_version
;
751 psp
->pSAREA
= pSAREA
;
752 psp
->lock
= (drmLock
*) &psp
->pSAREA
->lock
;
754 psp
->pFB
= frame_buffer
->base
;
755 psp
->fbSize
= frame_buffer
->size
;
756 psp
->fbStride
= frame_buffer
->stride
;
757 psp
->fbWidth
= frame_buffer
->width
;
758 psp
->fbHeight
= frame_buffer
->height
;
759 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
760 psp
->pDevPriv
= frame_buffer
->dev_priv
;
761 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
763 psp
->extensions
= emptyExtensionList
;
766 psp
->dri2
.enabled
= GL_FALSE
;
768 psp
->DriverAPI
= driDriverAPI
;
769 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
771 *driver_modes
= driDriverAPI
.InitScreen(psp
);
772 if (*driver_modes
== NULL
) {
784 dri2CreateNewScreen(int scrn
, int fd
,
785 const __DRIextension
**extensions
,
786 const __DRIconfig
***driver_configs
, void *data
)
788 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
790 drmVersionPtr version
;
792 if (driDriverAPI
.InitScreen2
== NULL
)
795 psp
= calloc(1, sizeof(*psp
));
799 setupLoaderExtensions(psp
, extensions
);
801 version
= drmGetVersion(fd
);
803 psp
->drm_version
.major
= version
->version_major
;
804 psp
->drm_version
.minor
= version
->version_minor
;
805 psp
->drm_version
.patch
= version
->version_patchlevel
;
806 drmFreeVersion(version
);
809 psp
->extensions
= emptyExtensionList
;
812 psp
->dri2
.enabled
= GL_TRUE
;
814 psp
->DriverAPI
= driDriverAPI
;
815 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
816 *driver_configs
= driDriverAPI
.InitScreen2(psp
);
817 if (*driver_configs
== NULL
) {
822 psp
->DriverAPI
= driDriverAPI
;
827 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
829 return psp
->extensions
;
832 /** Core interface */
833 const __DRIcoreExtension driCoreExtension
= {
834 { __DRI_CORE
, __DRI_CORE_VERSION
},
839 driIndexConfigAttrib
,
850 /** Legacy DRI interface */
851 const __DRIlegacyExtension driLegacyExtension
= {
852 { __DRI_LEGACY
, __DRI_LEGACY_VERSION
},
854 driCreateNewDrawable
,
858 /** DRI2 interface */
859 const __DRIdri2Extension driDRI2Extension
= {
860 { __DRI_DRI2
, __DRI_DRI2_VERSION
},
862 dri2CreateNewDrawable
,
863 dri2CreateNewContext
,
865 dri2CreateNewContextForAPI
869 driFrameTracking(__DRIdrawable
*drawable
, GLboolean enable
)
871 return GLX_BAD_CONTEXT
;
875 driQueryFrameTracking(__DRIdrawable
*dpriv
,
876 int64_t * sbc
, int64_t * missedFrames
,
877 float * lastMissedUsage
, float * usage
)
882 __DRIscreen
*psp
= dpriv
->driScreenPriv
;
884 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
886 *sbc
= sInfo
.swap_count
;
887 *missedFrames
= sInfo
.swap_missed_count
;
888 *lastMissedUsage
= sInfo
.swap_missed_usage
;
890 (*psp
->systemTime
->getUST
)( & ust
);
891 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
897 const __DRIframeTrackingExtension driFrameTrackingExtension
= {
898 { __DRI_FRAME_TRACKING
, __DRI_FRAME_TRACKING_VERSION
},
900 driQueryFrameTracking
904 * Calculate amount of swap interval used between GLX buffer swaps.
906 * The usage value, on the range [0,max], is the fraction of total swap
907 * interval time used between GLX buffer swaps is calculated.
909 * \f$p = t_d / (i * t_r)\f$
911 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
912 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
913 * required for a single vertical refresh period (as returned by \c
916 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
919 * \param dPriv Pointer to the private drawable structure.
920 * \return If less than a single swap interval time period was required
921 * between GLX buffer swaps, a number greater than 0 and less than
922 * 1.0 is returned. If exactly one swap interval time period is
923 * required, 1.0 is returned, and if more than one is required then
924 * a number greater than 1.0 will be returned.
926 * \sa glXSwapIntervalSGI glXGetMscRateOML
928 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
929 * be possible to cache the sync rate?
932 driCalculateSwapUsage( __DRIdrawable
*dPriv
, int64_t last_swap_ust
,
933 int64_t current_ust
)
939 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
941 if ( (*psp
->systemTime
->getMSCRate
)(dPriv
, &n
, &d
, dPriv
->loaderPrivate
) ) {
942 interval
= (dPriv
->swap_interval
!= 0) ? dPriv
->swap_interval
: 1;
945 /* We want to calculate
946 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
947 * current_UST by calling __glXGetUST. last_swap_UST is stored in
948 * dPriv->swap_ust. interval has already been calculated.
950 * The only tricky part is us_per_refresh. us_per_refresh is
951 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
952 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
953 * the denominator of the final calculation, we calculate
954 * (interval * 1000000 * d) and move n into the numerator.
957 usage
= (current_ust
- last_swap_ust
);
959 usage
/= (interval
* d
);
967 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
969 drawable
->dri2
.stamp
++;