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 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
54 * Is called from the drivers.
56 * \param f \c printf like format string.
59 __driUtilMessage(const char *f
, ...)
63 if (getenv("LIBGL_DEBUG")) {
64 fprintf(stderr
, "libGL: ");
66 vfprintf(stderr
, f
, args
);
68 fprintf(stderr
, "\n");
73 driIntersectArea( drm_clip_rect_t rect1
, drm_clip_rect_t rect2
)
75 if (rect2
.x1
> rect1
.x1
) rect1
.x1
= rect2
.x1
;
76 if (rect2
.x2
< rect1
.x2
) rect1
.x2
= rect2
.x2
;
77 if (rect2
.y1
> rect1
.y1
) rect1
.y1
= rect2
.y1
;
78 if (rect2
.y2
< rect1
.y2
) rect1
.y2
= rect2
.y2
;
80 if (rect1
.x1
> rect1
.x2
|| rect1
.y1
> rect1
.y2
) return 0;
82 return (rect1
.x2
- rect1
.x1
) * (rect1
.y2
- rect1
.y1
);
85 /*****************************************************************/
86 /** \name Context (un)binding functions */
87 /*****************************************************************/
93 * \param scrn the screen.
96 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
99 * This function calls __DriverAPIRec::UnbindContext, and then decrements
100 * __DRIdrawableRec::refcount which must be non-zero for a successful
103 * While casting the opaque private pointers associated with the parameters
104 * into their respective real types it also assures they are not \c NULL.
106 static int driUnbindContext(__DRIcontext
*pcp
)
113 ** Assume error checking is done properly in glXMakeCurrent before
114 ** calling driUnbindContext.
120 psp
= pcp
->driScreenPriv
;
121 pdp
= pcp
->driDrawablePriv
;
122 prp
= pcp
->driReadablePriv
;
124 /* already unbound */
127 /* Let driver unbind drawable from context */
128 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
131 if (pdp
->refcount
== 0) {
136 dri_put_drawable(pdp
);
139 if (prp
->refcount
== 0) {
144 dri_put_drawable(prp
);
148 /* XXX this is disabled so that if we call SwapBuffers on an unbound
149 * window we can determine the last context bound to the window and
150 * use that context's lock. (BrianP, 2-Dec-2000)
152 pcp
->driDrawablePriv
= pcp
->driReadablePriv
= NULL
;
155 /* Unbind the drawable */
156 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
163 * This function takes both a read buffer and a draw buffer. This is needed
164 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
167 static int driBindContext(__DRIcontext
*pcp
,
171 __DRIscreen
*psp
= NULL
;
173 /* Bind the drawable to the context */
176 psp
= pcp
->driScreenPriv
;
177 pcp
->driDrawablePriv
= pdp
;
178 pcp
->driReadablePriv
= prp
;
180 pdp
->driContextPriv
= pcp
;
181 dri_get_drawable(pdp
);
183 if ( prp
&& pdp
!= prp
) {
184 dri_get_drawable(prp
);
189 ** Now that we have a context associated with this drawable, we can
190 ** initialize the drawable information if has not been done before.
194 if (!psp
->dri2
.enabled
) {
195 if (pdp
&& !pdp
->pStamp
) {
196 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
197 __driUtilUpdateDrawableInfo(pdp
);
198 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
200 if (prp
&& pdp
!= prp
&& !prp
->pStamp
) {
201 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
202 __driUtilUpdateDrawableInfo(prp
);
203 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
207 /* Call device-specific MakeCurrent */
209 return (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
215 /*****************************************************************/
216 /** \name Drawable handling functions */
217 /*****************************************************************/
221 * Update private drawable information.
223 * \param pdp pointer to the private drawable information to update.
225 * This function basically updates the __DRIdrawable struct's
226 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
227 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
228 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
229 * the values are different that means we have to update the clipping
233 __driUtilUpdateDrawableInfo(__DRIdrawable
*pdp
)
235 __DRIscreen
*psp
= pdp
->driScreenPriv
;
236 __DRIcontext
*pcp
= pdp
->driContextPriv
;
239 || ((pdp
!= pcp
->driDrawablePriv
) && (pdp
!= pcp
->driReadablePriv
))) {
241 * ...but we must ignore it. There can be many contexts bound to a
246 if (pdp
->pClipRects
) {
247 free(pdp
->pClipRects
);
248 pdp
->pClipRects
= NULL
;
251 if (pdp
->pBackClipRects
) {
252 free(pdp
->pBackClipRects
);
253 pdp
->pBackClipRects
= NULL
;
256 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
258 if (! (*psp
->getDrawableInfo
->getDrawableInfo
)(pdp
,
259 &pdp
->index
, &pdp
->lastStamp
,
260 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
261 &pdp
->numClipRects
, &pdp
->pClipRects
,
264 &pdp
->numBackClipRects
,
265 &pdp
->pBackClipRects
,
266 pdp
->loaderPrivate
)) {
267 /* Error -- eg the window may have been destroyed. Keep going
270 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
271 pdp
->numClipRects
= 0;
272 pdp
->pClipRects
= NULL
;
273 pdp
->numBackClipRects
= 0;
274 pdp
->pBackClipRects
= NULL
;
277 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
279 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
284 /*****************************************************************/
285 /** \name GLX callbacks */
286 /*****************************************************************/
289 static void driReportDamage(__DRIdrawable
*pdp
,
290 struct drm_clip_rect
*pClipRects
, int numClipRects
)
292 __DRIscreen
*psp
= pdp
->driScreenPriv
;
294 /* Check that we actually have the new damage report method */
296 /* Report the damage. Currently, all our drivers draw
297 * directly to the front buffer, so we report the damage there
298 * rather than to the backing storein (if any).
300 (*psp
->damage
->reportDamage
)(pdp
,
302 pClipRects
, numClipRects
,
303 GL_TRUE
, pdp
->loaderPrivate
);
311 * \param drawablePrivate opaque pointer to the per-drawable private info.
314 * This function calls __DRIdrawable::swapBuffers.
316 * Is called directly from glXSwapBuffers().
318 static void driSwapBuffers(__DRIdrawable
*dPriv
)
320 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
321 drm_clip_rect_t
*rects
;
324 psp
->DriverAPI
.SwapBuffers(dPriv
);
326 if (!dPriv
->numClipRects
)
329 rects
= malloc(sizeof(*rects
) * dPriv
->numClipRects
);
334 for (i
= 0; i
< dPriv
->numClipRects
; i
++) {
335 rects
[i
].x1
= dPriv
->pClipRects
[i
].x1
- dPriv
->x
;
336 rects
[i
].y1
= dPriv
->pClipRects
[i
].y1
- dPriv
->y
;
337 rects
[i
].x2
= dPriv
->pClipRects
[i
].x2
- dPriv
->x
;
338 rects
[i
].y2
= dPriv
->pClipRects
[i
].y2
- dPriv
->y
;
341 driReportDamage(dPriv
, rects
, dPriv
->numClipRects
);
345 static int driDrawableGetMSC( __DRIscreen
*sPriv
, __DRIdrawable
*dPriv
,
348 return sPriv
->DriverAPI
.GetDrawableMSC(sPriv
, dPriv
, msc
);
352 static int driWaitForMSC(__DRIdrawable
*dPriv
, int64_t target_msc
,
353 int64_t divisor
, int64_t remainder
,
354 int64_t * msc
, int64_t * sbc
)
359 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
363 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
364 * is supported but GLX_OML_sync_control is not. Therefore, don't return
365 * an error value if GetSwapInfo() is not implemented.
368 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
369 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
370 *sbc
= sInfo
.swap_count
;
377 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension
= {
378 { __DRI_MEDIA_STREAM_COUNTER
, __DRI_MEDIA_STREAM_COUNTER_VERSION
},
384 static void driCopySubBuffer(__DRIdrawable
*dPriv
,
385 int x
, int y
, int w
, int h
)
387 drm_clip_rect_t rect
;
390 rect
.y1
= dPriv
->h
- y
- h
;
392 rect
.y2
= rect
.y1
+ h
;
393 driReportDamage(dPriv
, &rect
, 1);
395 dPriv
->driScreenPriv
->DriverAPI
.CopySubBuffer(dPriv
, x
, y
, w
, h
);
398 const __DRIcopySubBufferExtension driCopySubBufferExtension
= {
399 { __DRI_COPY_SUB_BUFFER
, __DRI_COPY_SUB_BUFFER_VERSION
},
403 static void driSetSwapInterval(__DRIdrawable
*dPriv
, unsigned int interval
)
405 dPriv
->swap_interval
= interval
;
408 static unsigned int driGetSwapInterval(__DRIdrawable
*dPriv
)
410 return dPriv
->swap_interval
;
413 const __DRIswapControlExtension driSwapControlExtension
= {
414 { __DRI_SWAP_CONTROL
, __DRI_SWAP_CONTROL_VERSION
},
421 * This is called via __DRIscreenRec's createNewDrawable pointer.
423 static __DRIdrawable
*
424 driCreateNewDrawable(__DRIscreen
*psp
, const __DRIconfig
*config
,
425 drm_drawable_t hwDrawable
, int renderType
,
426 const int *attrs
, void *data
)
430 /* Since pbuffers are not yet supported, no drawable attributes are
435 pdp
= malloc(sizeof *pdp
);
440 pdp
->loaderPrivate
= data
;
441 pdp
->hHWDrawable
= hwDrawable
;
450 pdp
->numClipRects
= 0;
451 pdp
->numBackClipRects
= 0;
452 pdp
->pClipRects
= NULL
;
453 pdp
->pBackClipRects
= NULL
;
457 pdp
->driScreenPriv
= psp
;
458 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
460 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, &config
->modes
,
461 renderType
== GLX_PIXMAP_BIT
)) {
468 /* This special default value is replaced with the configured
469 * default value when the drawable is first bound to a direct
472 pdp
->swap_interval
= (unsigned)-1;
478 static __DRIdrawable
*
479 dri2CreateNewDrawable(__DRIscreen
*screen
,
480 const __DRIconfig
*config
,
483 __DRIdrawable
*pdraw
;
485 pdraw
= driCreateNewDrawable(screen
, config
, 0, 0, NULL
, loaderPrivate
);
489 pdraw
->pClipRects
= &pdraw
->dri2
.clipRect
;
490 pdraw
->pBackClipRects
= &pdraw
->dri2
.clipRect
;
492 pdraw
->pStamp
= &pdraw
->dri2
.stamp
;
493 *pdraw
->pStamp
= pdraw
->lastStamp
+ 1;
498 static void dri_get_drawable(__DRIdrawable
*pdp
)
503 static void dri_put_drawable(__DRIdrawable
*pdp
)
512 psp
= pdp
->driScreenPriv
;
513 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
514 if (pdp
->pClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
515 free(pdp
->pClipRects
);
516 pdp
->pClipRects
= NULL
;
518 if (pdp
->pBackClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
519 free(pdp
->pBackClipRects
);
520 pdp
->pBackClipRects
= NULL
;
527 driDestroyDrawable(__DRIdrawable
*pdp
)
529 dri_put_drawable(pdp
);
535 /*****************************************************************/
536 /** \name Context handling functions */
537 /*****************************************************************/
541 * Destroy the per-context private information.
544 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
545 * drmDestroyContext(), and finally frees \p contextPrivate.
548 driDestroyContext(__DRIcontext
*pcp
)
551 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
558 * Create the per-drawable private driver information.
560 * \param render_type Type of rendering target. \c GLX_RGBA is the only
561 * type likely to ever be supported for direct-rendering.
562 * \param shared Context with which to share textures, etc. or NULL
564 * \returns An opaque pointer to the per-context private information on
565 * success, or \c NULL on failure.
568 * This function allocates and fills a __DRIcontextRec structure. It
569 * performs some device independent initialization and passes all the
570 * relevent information to __DriverAPIRec::CreateContext to create the
574 static __DRIcontext
*
575 driCreateNewContext(__DRIscreen
*psp
, const __DRIconfig
*config
,
576 int render_type
, __DRIcontext
*shared
,
577 drm_context_t hwContext
, void *data
)
580 void * const shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
582 pcp
= malloc(sizeof *pcp
);
586 pcp
->driScreenPriv
= psp
;
587 pcp
->driDrawablePriv
= NULL
;
588 pcp
->loaderPrivate
= data
;
590 pcp
->dri2
.draw_stamp
= 0;
591 pcp
->dri2
.read_stamp
= 0;
592 /* When the first context is created for a screen, initialize a "dummy"
596 if (!psp
->dri2
.enabled
&& !psp
->dummyContextPriv
.driScreenPriv
) {
597 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
598 psp
->dummyContextPriv
.driScreenPriv
= psp
;
599 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
600 psp
->dummyContextPriv
.driverPrivate
= NULL
;
601 /* No other fields should be used! */
604 pcp
->hHWContext
= hwContext
;
606 if ( !(*psp
->DriverAPI
.CreateContext
)(&config
->modes
, pcp
, shareCtx
) ) {
615 static __DRIcontext
*
616 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
617 __DRIcontext
*shared
, void *data
)
619 return driCreateNewContext(screen
, config
, 0, shared
, 0, data
);
624 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
632 /*****************************************************************/
633 /** \name Screen handling functions */
634 /*****************************************************************/
638 * Destroy the per-screen private information.
641 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
642 * drmClose(), and finally frees \p screenPrivate.
644 static void driDestroyScreen(__DRIscreen
*psp
)
647 /* No interaction with the X-server is possible at this point. This
648 * routine is called after XCloseDisplay, so there is no protocol
649 * stream open to the X-server anymore.
652 if (psp
->DriverAPI
.DestroyScreen
)
653 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
655 if (!psp
->dri2
.enabled
) {
656 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
657 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
658 (void)drmCloseOnce(psp
->fd
);
666 setupLoaderExtensions(__DRIscreen
*psp
,
667 const __DRIextension
**extensions
)
671 for (i
= 0; extensions
[i
]; i
++) {
672 if (strcmp(extensions
[i
]->name
, __DRI_GET_DRAWABLE_INFO
) == 0)
673 psp
->getDrawableInfo
= (__DRIgetDrawableInfoExtension
*) extensions
[i
];
674 if (strcmp(extensions
[i
]->name
, __DRI_DAMAGE
) == 0)
675 psp
->damage
= (__DRIdamageExtension
*) extensions
[i
];
676 if (strcmp(extensions
[i
]->name
, __DRI_SYSTEM_TIME
) == 0)
677 psp
->systemTime
= (__DRIsystemTimeExtension
*) extensions
[i
];
678 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
679 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
680 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
681 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
686 * This is the bootstrap function for the driver. libGL supplies all of the
687 * requisite information about the system, and the driver initializes itself.
688 * This routine also fills in the linked list pointed to by \c driver_modes
689 * with the \c __GLcontextModes that the driver can support for windows or
694 * \param scrn Index of the screen
695 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
697 * \param dri_version Version of the "server-side" DRI.
698 * \param drm_version Version of the kernel DRM.
699 * \param frame_buffer Data describing the location and layout of the
701 * \param pSAREA Pointer to the SAREA.
702 * \param fd Device handle for the DRM.
703 * \param extensions ??
704 * \param driver_modes Returns modes suppoted by the driver
705 * \param loaderPrivate ??
707 * \note There is no need to check the minimum API version in this
708 * function. Since the name of this function is versioned, it is
709 * impossible for a loader that is too old to even load this driver.
712 driCreateNewScreen(int scrn
,
713 const __DRIversion
*ddx_version
,
714 const __DRIversion
*dri_version
,
715 const __DRIversion
*drm_version
,
716 const __DRIframebuffer
*frame_buffer
,
717 drmAddress pSAREA
, int fd
,
718 const __DRIextension
**extensions
,
719 const __DRIconfig
***driver_modes
,
722 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
725 psp
= calloc(1, sizeof *psp
);
729 setupLoaderExtensions(psp
, extensions
);
732 ** NOT_DONE: This is used by the X server to detect when the client
733 ** has died while holding the drawable lock. The client sets the
734 ** drawable lock to this value.
738 psp
->drm_version
= *drm_version
;
739 psp
->ddx_version
= *ddx_version
;
740 psp
->dri_version
= *dri_version
;
742 psp
->pSAREA
= pSAREA
;
743 psp
->lock
= (drmLock
*) &psp
->pSAREA
->lock
;
745 psp
->pFB
= frame_buffer
->base
;
746 psp
->fbSize
= frame_buffer
->size
;
747 psp
->fbStride
= frame_buffer
->stride
;
748 psp
->fbWidth
= frame_buffer
->width
;
749 psp
->fbHeight
= frame_buffer
->height
;
750 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
751 psp
->pDevPriv
= frame_buffer
->dev_priv
;
752 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
754 psp
->extensions
= emptyExtensionList
;
757 psp
->dri2
.enabled
= GL_FALSE
;
760 ** Do not init dummy context here; actual initialization will be
761 ** done when the first DRI context is created. Init screen priv ptr
762 ** to NULL to let CreateContext routine that it needs to be inited.
764 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
766 psp
->DriverAPI
= driDriverAPI
;
768 *driver_modes
= driDriverAPI
.InitScreen(psp
);
769 if (*driver_modes
== NULL
) {
781 dri2CreateNewScreen(int scrn
, int fd
,
782 const __DRIextension
**extensions
,
783 const __DRIconfig
***driver_configs
, void *data
)
785 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
787 drmVersionPtr version
;
789 if (driDriverAPI
.InitScreen2
== NULL
)
792 psp
= calloc(1, sizeof(*psp
));
796 setupLoaderExtensions(psp
, extensions
);
798 version
= drmGetVersion(fd
);
800 psp
->drm_version
.major
= version
->version_major
;
801 psp
->drm_version
.minor
= version
->version_minor
;
802 psp
->drm_version
.patch
= version
->version_patchlevel
;
803 drmFreeVersion(version
);
806 psp
->extensions
= emptyExtensionList
;
809 psp
->dri2
.enabled
= GL_TRUE
;
811 psp
->DriverAPI
= driDriverAPI
;
812 *driver_configs
= driDriverAPI
.InitScreen2(psp
);
813 if (*driver_configs
== NULL
) {
818 psp
->DriverAPI
= driDriverAPI
;
823 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
825 return psp
->extensions
;
828 /** Core interface */
829 const __DRIcoreExtension driCoreExtension
= {
830 { __DRI_CORE
, __DRI_CORE_VERSION
},
835 driIndexConfigAttrib
,
846 /** Legacy DRI interface */
847 const __DRIlegacyExtension driLegacyExtension
= {
848 { __DRI_LEGACY
, __DRI_LEGACY_VERSION
},
850 driCreateNewDrawable
,
854 /** DRI2 interface */
855 const __DRIdri2Extension driDRI2Extension
= {
856 { __DRI_DRI2
, __DRI_DRI2_VERSION
},
858 dri2CreateNewDrawable
,
859 dri2CreateNewContext
,
863 driFrameTracking(__DRIdrawable
*drawable
, GLboolean enable
)
865 return GLX_BAD_CONTEXT
;
869 driQueryFrameTracking(__DRIdrawable
*dpriv
,
870 int64_t * sbc
, int64_t * missedFrames
,
871 float * lastMissedUsage
, float * usage
)
876 __DRIscreen
*psp
= dpriv
->driScreenPriv
;
878 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
880 *sbc
= sInfo
.swap_count
;
881 *missedFrames
= sInfo
.swap_missed_count
;
882 *lastMissedUsage
= sInfo
.swap_missed_usage
;
884 (*psp
->systemTime
->getUST
)( & ust
);
885 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
891 const __DRIframeTrackingExtension driFrameTrackingExtension
= {
892 { __DRI_FRAME_TRACKING
, __DRI_FRAME_TRACKING_VERSION
},
894 driQueryFrameTracking
898 * Calculate amount of swap interval used between GLX buffer swaps.
900 * The usage value, on the range [0,max], is the fraction of total swap
901 * interval time used between GLX buffer swaps is calculated.
903 * \f$p = t_d / (i * t_r)\f$
905 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
906 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
907 * required for a single vertical refresh period (as returned by \c
910 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
913 * \param dPriv Pointer to the private drawable structure.
914 * \return If less than a single swap interval time period was required
915 * between GLX buffer swaps, a number greater than 0 and less than
916 * 1.0 is returned. If exactly one swap interval time period is
917 * required, 1.0 is returned, and if more than one is required then
918 * a number greater than 1.0 will be returned.
920 * \sa glXSwapIntervalSGI glXGetMscRateOML
922 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
923 * be possible to cache the sync rate?
926 driCalculateSwapUsage( __DRIdrawable
*dPriv
, int64_t last_swap_ust
,
927 int64_t current_ust
)
933 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
935 if ( (*psp
->systemTime
->getMSCRate
)(dPriv
, &n
, &d
, dPriv
->loaderPrivate
) ) {
936 interval
= (dPriv
->swap_interval
!= 0) ? dPriv
->swap_interval
: 1;
939 /* We want to calculate
940 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
941 * current_UST by calling __glXGetUST. last_swap_UST is stored in
942 * dPriv->swap_ust. interval has already been calculated.
944 * The only tricky part is us_per_refresh. us_per_refresh is
945 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
946 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
947 * the denominator of the final calculation, we calculate
948 * (interval * 1000000 * d) and move n into the numerator.
951 usage
= (current_ust
- last_swap_ust
);
953 usage
/= (interval
* d
);
961 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
963 drawable
->dri2
.stamp
++;