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
);
130 if (pdp
->refcount
== 0) {
135 dri_put_drawable(pdp
);
138 if (prp
->refcount
== 0) {
143 dri_put_drawable(prp
);
147 /* XXX this is disabled so that if we call SwapBuffers on an unbound
148 * window we can determine the last context bound to the window and
149 * use that context's lock. (BrianP, 2-Dec-2000)
151 pcp
->driDrawablePriv
= pcp
->driReadablePriv
= NULL
;
154 /* Unbind the drawable */
155 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
162 * This function takes both a read buffer and a draw buffer. This is needed
163 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
166 static int driBindContext(__DRIcontext
*pcp
,
170 __DRIscreen
*psp
= NULL
;
172 /* Bind the drawable to the context */
175 psp
= pcp
->driScreenPriv
;
176 pcp
->driDrawablePriv
= pdp
;
177 pcp
->driReadablePriv
= prp
;
179 pdp
->driContextPriv
= pcp
;
180 dri_get_drawable(pdp
);
182 if ( prp
&& pdp
!= prp
) {
183 dri_get_drawable(prp
);
188 ** Now that we have a context associated with this drawable, we can
189 ** initialize the drawable information if has not been done before.
192 if (!psp
->dri2
.enabled
) {
193 if (pdp
&& !pdp
->pStamp
) {
194 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
195 __driUtilUpdateDrawableInfo(pdp
);
196 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
198 if (prp
&& pdp
!= prp
&& !prp
->pStamp
) {
199 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
200 __driUtilUpdateDrawableInfo(prp
);
201 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
205 /* Call device-specific MakeCurrent */
207 return (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
213 /*****************************************************************/
214 /** \name Drawable handling functions */
215 /*****************************************************************/
219 * Update private drawable information.
221 * \param pdp pointer to the private drawable information to update.
223 * This function basically updates the __DRIdrawable struct's
224 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
225 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
226 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
227 * the values are different that means we have to update the clipping
231 __driUtilUpdateDrawableInfo(__DRIdrawable
*pdp
)
233 __DRIscreen
*psp
= pdp
->driScreenPriv
;
234 __DRIcontext
*pcp
= pdp
->driContextPriv
;
237 || ((pdp
!= pcp
->driDrawablePriv
) && (pdp
!= pcp
->driReadablePriv
))) {
239 * ...but we must ignore it. There can be many contexts bound to a
244 if (pdp
->pClipRects
) {
245 free(pdp
->pClipRects
);
246 pdp
->pClipRects
= NULL
;
249 if (pdp
->pBackClipRects
) {
250 free(pdp
->pBackClipRects
);
251 pdp
->pBackClipRects
= NULL
;
254 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
256 if (! (*psp
->getDrawableInfo
->getDrawableInfo
)(pdp
,
257 &pdp
->index
, &pdp
->lastStamp
,
258 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
259 &pdp
->numClipRects
, &pdp
->pClipRects
,
262 &pdp
->numBackClipRects
,
263 &pdp
->pBackClipRects
,
264 pdp
->loaderPrivate
)) {
265 /* Error -- eg the window may have been destroyed. Keep going
268 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
269 pdp
->numClipRects
= 0;
270 pdp
->pClipRects
= NULL
;
271 pdp
->numBackClipRects
= 0;
272 pdp
->pBackClipRects
= NULL
;
275 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
277 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
282 /*****************************************************************/
283 /** \name GLX callbacks */
284 /*****************************************************************/
287 static void driReportDamage(__DRIdrawable
*pdp
,
288 struct drm_clip_rect
*pClipRects
, int numClipRects
)
290 __DRIscreen
*psp
= pdp
->driScreenPriv
;
292 /* Check that we actually have the new damage report method */
294 /* Report the damage. Currently, all our drivers draw
295 * directly to the front buffer, so we report the damage there
296 * rather than to the backing storein (if any).
298 (*psp
->damage
->reportDamage
)(pdp
,
300 pClipRects
, numClipRects
,
301 GL_TRUE
, pdp
->loaderPrivate
);
309 * \param drawablePrivate opaque pointer to the per-drawable private info.
312 * This function calls __DRIdrawable::swapBuffers.
314 * Is called directly from glXSwapBuffers().
316 static void driSwapBuffers(__DRIdrawable
*dPriv
)
318 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
319 drm_clip_rect_t
*rects
;
322 psp
->DriverAPI
.SwapBuffers(dPriv
);
324 if (!dPriv
->numClipRects
)
327 rects
= malloc(sizeof(*rects
) * dPriv
->numClipRects
);
332 for (i
= 0; i
< dPriv
->numClipRects
; i
++) {
333 rects
[i
].x1
= dPriv
->pClipRects
[i
].x1
- dPriv
->x
;
334 rects
[i
].y1
= dPriv
->pClipRects
[i
].y1
- dPriv
->y
;
335 rects
[i
].x2
= dPriv
->pClipRects
[i
].x2
- dPriv
->x
;
336 rects
[i
].y2
= dPriv
->pClipRects
[i
].y2
- dPriv
->y
;
339 driReportDamage(dPriv
, rects
, dPriv
->numClipRects
);
343 static int driDrawableGetMSC( __DRIscreen
*sPriv
, __DRIdrawable
*dPriv
,
346 return sPriv
->DriverAPI
.GetDrawableMSC(sPriv
, dPriv
, msc
);
350 static int driWaitForMSC(__DRIdrawable
*dPriv
, int64_t target_msc
,
351 int64_t divisor
, int64_t remainder
,
352 int64_t * msc
, int64_t * sbc
)
357 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
361 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
362 * is supported but GLX_OML_sync_control is not. Therefore, don't return
363 * an error value if GetSwapInfo() is not implemented.
366 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
367 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
368 *sbc
= sInfo
.swap_count
;
375 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension
= {
376 { __DRI_MEDIA_STREAM_COUNTER
, __DRI_MEDIA_STREAM_COUNTER_VERSION
},
382 static void driCopySubBuffer(__DRIdrawable
*dPriv
,
383 int x
, int y
, int w
, int h
)
385 drm_clip_rect_t rect
;
388 rect
.y1
= dPriv
->h
- y
- h
;
390 rect
.y2
= rect
.y1
+ h
;
391 driReportDamage(dPriv
, &rect
, 1);
393 dPriv
->driScreenPriv
->DriverAPI
.CopySubBuffer(dPriv
, x
, y
, w
, h
);
396 const __DRIcopySubBufferExtension driCopySubBufferExtension
= {
397 { __DRI_COPY_SUB_BUFFER
, __DRI_COPY_SUB_BUFFER_VERSION
},
401 static void driSetSwapInterval(__DRIdrawable
*dPriv
, unsigned int interval
)
403 dPriv
->swap_interval
= interval
;
406 static unsigned int driGetSwapInterval(__DRIdrawable
*dPriv
)
408 return dPriv
->swap_interval
;
411 const __DRIswapControlExtension driSwapControlExtension
= {
412 { __DRI_SWAP_CONTROL
, __DRI_SWAP_CONTROL_VERSION
},
419 * This is called via __DRIscreenRec's createNewDrawable pointer.
421 static __DRIdrawable
*
422 driCreateNewDrawable(__DRIscreen
*psp
, const __DRIconfig
*config
,
423 drm_drawable_t hwDrawable
, int renderType
,
424 const int *attrs
, void *data
)
428 /* Since pbuffers are not yet supported, no drawable attributes are
433 pdp
= malloc(sizeof *pdp
);
438 pdp
->loaderPrivate
= data
;
439 pdp
->hHWDrawable
= hwDrawable
;
448 pdp
->numClipRects
= 0;
449 pdp
->numBackClipRects
= 0;
450 pdp
->pClipRects
= NULL
;
451 pdp
->pBackClipRects
= NULL
;
455 pdp
->driScreenPriv
= psp
;
456 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
458 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, &config
->modes
,
459 renderType
== GLX_PIXMAP_BIT
)) {
466 /* This special default value is replaced with the configured
467 * default value when the drawable is first bound to a direct
470 pdp
->swap_interval
= (unsigned)-1;
476 static __DRIdrawable
*
477 dri2CreateNewDrawable(__DRIscreen
*screen
,
478 const __DRIconfig
*config
,
481 __DRIdrawable
*pdraw
;
483 pdraw
= driCreateNewDrawable(screen
, config
, 0, 0, NULL
, loaderPrivate
);
487 pdraw
->pClipRects
= &pdraw
->dri2
.clipRect
;
488 pdraw
->pBackClipRects
= &pdraw
->dri2
.clipRect
;
490 pdraw
->pStamp
= &pdraw
->dri2
.stamp
;
491 *pdraw
->pStamp
= pdraw
->lastStamp
+ 1;
496 static void dri_get_drawable(__DRIdrawable
*pdp
)
501 static void dri_put_drawable(__DRIdrawable
*pdp
)
510 psp
= pdp
->driScreenPriv
;
511 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
512 if (pdp
->pClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
513 free(pdp
->pClipRects
);
514 pdp
->pClipRects
= NULL
;
516 if (pdp
->pBackClipRects
&& pdp
->pClipRects
!= &pdp
->dri2
.clipRect
) {
517 free(pdp
->pBackClipRects
);
518 pdp
->pBackClipRects
= NULL
;
525 driDestroyDrawable(__DRIdrawable
*pdp
)
527 dri_put_drawable(pdp
);
533 /*****************************************************************/
534 /** \name Context handling functions */
535 /*****************************************************************/
539 * Destroy the per-context private information.
542 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
543 * drmDestroyContext(), and finally frees \p contextPrivate.
546 driDestroyContext(__DRIcontext
*pcp
)
549 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
556 * Create the per-drawable private driver information.
558 * \param render_type Type of rendering target. \c GLX_RGBA is the only
559 * type likely to ever be supported for direct-rendering.
560 * \param shared Context with which to share textures, etc. or NULL
562 * \returns An opaque pointer to the per-context private information on
563 * success, or \c NULL on failure.
566 * This function allocates and fills a __DRIcontextRec structure. It
567 * performs some device independent initialization and passes all the
568 * relevent information to __DriverAPIRec::CreateContext to create the
572 static __DRIcontext
*
573 driCreateNewContext(__DRIscreen
*psp
, const __DRIconfig
*config
,
574 int render_type
, __DRIcontext
*shared
,
575 drm_context_t hwContext
, void *data
)
578 void * const shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
580 pcp
= malloc(sizeof *pcp
);
584 pcp
->driScreenPriv
= psp
;
585 pcp
->driDrawablePriv
= NULL
;
586 pcp
->loaderPrivate
= data
;
588 pcp
->dri2
.draw_stamp
= 0;
589 pcp
->dri2
.read_stamp
= 0;
590 /* When the first context is created for a screen, initialize a "dummy"
594 if (!psp
->dri2
.enabled
&& !psp
->dummyContextPriv
.driScreenPriv
) {
595 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
596 psp
->dummyContextPriv
.driScreenPriv
= psp
;
597 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
598 psp
->dummyContextPriv
.driverPrivate
= NULL
;
599 /* No other fields should be used! */
602 pcp
->hHWContext
= hwContext
;
604 if ( !(*psp
->DriverAPI
.CreateContext
)(&config
->modes
, pcp
, shareCtx
) ) {
613 static __DRIcontext
*
614 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
615 __DRIcontext
*shared
, void *data
)
617 return driCreateNewContext(screen
, config
, 0, shared
, 0, data
);
622 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
630 /*****************************************************************/
631 /** \name Screen handling functions */
632 /*****************************************************************/
636 * Destroy the per-screen private information.
639 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
640 * drmClose(), and finally frees \p screenPrivate.
642 static void driDestroyScreen(__DRIscreen
*psp
)
645 /* No interaction with the X-server is possible at this point. This
646 * routine is called after XCloseDisplay, so there is no protocol
647 * stream open to the X-server anymore.
650 if (psp
->DriverAPI
.DestroyScreen
)
651 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
653 if (!psp
->dri2
.enabled
) {
654 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
655 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
656 (void)drmCloseOnce(psp
->fd
);
664 setupLoaderExtensions(__DRIscreen
*psp
,
665 const __DRIextension
**extensions
)
669 for (i
= 0; extensions
[i
]; i
++) {
670 if (strcmp(extensions
[i
]->name
, __DRI_GET_DRAWABLE_INFO
) == 0)
671 psp
->getDrawableInfo
= (__DRIgetDrawableInfoExtension
*) extensions
[i
];
672 if (strcmp(extensions
[i
]->name
, __DRI_DAMAGE
) == 0)
673 psp
->damage
= (__DRIdamageExtension
*) extensions
[i
];
674 if (strcmp(extensions
[i
]->name
, __DRI_SYSTEM_TIME
) == 0)
675 psp
->systemTime
= (__DRIsystemTimeExtension
*) extensions
[i
];
676 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
677 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
678 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
679 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
684 * This is the bootstrap function for the driver. libGL supplies all of the
685 * requisite information about the system, and the driver initializes itself.
686 * This routine also fills in the linked list pointed to by \c driver_modes
687 * with the \c __GLcontextModes that the driver can support for windows or
692 * \param scrn Index of the screen
693 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
695 * \param dri_version Version of the "server-side" DRI.
696 * \param drm_version Version of the kernel DRM.
697 * \param frame_buffer Data describing the location and layout of the
699 * \param pSAREA Pointer the the SAREA.
700 * \param fd Device handle for the DRM.
701 * \param extensions ??
702 * \param driver_modes Returns modes suppoted by the driver
703 * \param loaderPrivate ??
705 * \note There is no need to check the minimum API version in this
706 * function. Since the name of this function is versioned, it is
707 * impossible for a loader that is too old to even load this driver.
710 driCreateNewScreen(int scrn
,
711 const __DRIversion
*ddx_version
,
712 const __DRIversion
*dri_version
,
713 const __DRIversion
*drm_version
,
714 const __DRIframebuffer
*frame_buffer
,
715 drmAddress pSAREA
, int fd
,
716 const __DRIextension
**extensions
,
717 const __DRIconfig
***driver_modes
,
720 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
723 psp
= calloc(1, sizeof *psp
);
727 setupLoaderExtensions(psp
, extensions
);
730 ** NOT_DONE: This is used by the X server to detect when the client
731 ** has died while holding the drawable lock. The client sets the
732 ** drawable lock to this value.
736 psp
->drm_version
= *drm_version
;
737 psp
->ddx_version
= *ddx_version
;
738 psp
->dri_version
= *dri_version
;
740 psp
->pSAREA
= pSAREA
;
741 psp
->lock
= (drmLock
*) &psp
->pSAREA
->lock
;
743 psp
->pFB
= frame_buffer
->base
;
744 psp
->fbSize
= frame_buffer
->size
;
745 psp
->fbStride
= frame_buffer
->stride
;
746 psp
->fbWidth
= frame_buffer
->width
;
747 psp
->fbHeight
= frame_buffer
->height
;
748 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
749 psp
->pDevPriv
= frame_buffer
->dev_priv
;
750 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
752 psp
->extensions
= emptyExtensionList
;
755 psp
->dri2
.enabled
= GL_FALSE
;
758 ** Do not init dummy context here; actual initialization will be
759 ** done when the first DRI context is created. Init screen priv ptr
760 ** to NULL to let CreateContext routine that it needs to be inited.
762 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
764 psp
->DriverAPI
= driDriverAPI
;
766 *driver_modes
= driDriverAPI
.InitScreen(psp
);
767 if (*driver_modes
== NULL
) {
779 dri2CreateNewScreen(int scrn
, int fd
,
780 const __DRIextension
**extensions
,
781 const __DRIconfig
***driver_configs
, void *data
)
783 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
785 drmVersionPtr version
;
787 if (driDriverAPI
.InitScreen2
== NULL
)
790 psp
= calloc(1, sizeof(*psp
));
794 setupLoaderExtensions(psp
, extensions
);
796 version
= drmGetVersion(fd
);
798 psp
->drm_version
.major
= version
->version_major
;
799 psp
->drm_version
.minor
= version
->version_minor
;
800 psp
->drm_version
.patch
= version
->version_patchlevel
;
801 drmFreeVersion(version
);
804 psp
->extensions
= emptyExtensionList
;
807 psp
->dri2
.enabled
= GL_TRUE
;
809 psp
->DriverAPI
= driDriverAPI
;
810 *driver_configs
= driDriverAPI
.InitScreen2(psp
);
811 if (*driver_configs
== NULL
) {
816 psp
->DriverAPI
= driDriverAPI
;
821 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
823 return psp
->extensions
;
826 /** Core interface */
827 const __DRIcoreExtension driCoreExtension
= {
828 { __DRI_CORE
, __DRI_CORE_VERSION
},
833 driIndexConfigAttrib
,
844 /** Legacy DRI interface */
845 const __DRIlegacyExtension driLegacyExtension
= {
846 { __DRI_LEGACY
, __DRI_LEGACY_VERSION
},
848 driCreateNewDrawable
,
852 /** DRI2 interface */
853 const __DRIdri2Extension driDRI2Extension
= {
854 { __DRI_DRI2
, __DRI_DRI2_VERSION
},
856 dri2CreateNewDrawable
,
857 dri2CreateNewContext
,
861 driFrameTracking(__DRIdrawable
*drawable
, GLboolean enable
)
863 return GLX_BAD_CONTEXT
;
867 driQueryFrameTracking(__DRIdrawable
*dpriv
,
868 int64_t * sbc
, int64_t * missedFrames
,
869 float * lastMissedUsage
, float * usage
)
874 __DRIscreen
*psp
= dpriv
->driScreenPriv
;
876 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
878 *sbc
= sInfo
.swap_count
;
879 *missedFrames
= sInfo
.swap_missed_count
;
880 *lastMissedUsage
= sInfo
.swap_missed_usage
;
882 (*psp
->systemTime
->getUST
)( & ust
);
883 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
889 const __DRIframeTrackingExtension driFrameTrackingExtension
= {
890 { __DRI_FRAME_TRACKING
, __DRI_FRAME_TRACKING_VERSION
},
892 driQueryFrameTracking
896 * Calculate amount of swap interval used between GLX buffer swaps.
898 * The usage value, on the range [0,max], is the fraction of total swap
899 * interval time used between GLX buffer swaps is calculated.
901 * \f$p = t_d / (i * t_r)\f$
903 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
904 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
905 * required for a single vertical refresh period (as returned by \c
908 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
911 * \param dPriv Pointer to the private drawable structure.
912 * \return If less than a single swap interval time period was required
913 * between GLX buffer swaps, a number greater than 0 and less than
914 * 1.0 is returned. If exactly one swap interval time period is
915 * required, 1.0 is returned, and if more than one is required then
916 * a number greater than 1.0 will be returned.
918 * \sa glXSwapIntervalSGI glXGetMscRateOML
920 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
921 * be possible to cache the sync rate?
924 driCalculateSwapUsage( __DRIdrawable
*dPriv
, int64_t last_swap_ust
,
925 int64_t current_ust
)
931 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
933 if ( (*psp
->systemTime
->getMSCRate
)(dPriv
, &n
, &d
, dPriv
->loaderPrivate
) ) {
934 interval
= (dPriv
->swap_interval
!= 0) ? dPriv
->swap_interval
: 1;
937 /* We want to calculate
938 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
939 * current_UST by calling __glXGetUST. last_swap_UST is stored in
940 * dPriv->swap_ust. interval has already been calculated.
942 * The only tricky part is us_per_refresh. us_per_refresh is
943 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
944 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
945 * the denominator of the final calculation, we calculate
946 * (interval * 1000000 * d) and move n into the numerator.
949 usage
= (current_ust
- last_swap_ust
);
951 usage
/= (interval
* d
);
959 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
961 drawable
->dri2
.stamp
++;