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
18 * When \c DRI_NEW_INTERFACE_ONLY is defined, code is built / not built so
19 * that only the "new" libGL-to-driver interfaces are supported. This breaks
20 * backwards compatability. However, this may be necessary when DRI drivers
21 * are built to be used in non-XFree86 environments.
23 * \todo There are still some places in the code that need to be wrapped with
24 * \c DRI_NEW_INTERFACE_ONLY.
28 #ifdef GLX_DIRECT_RENDERING
35 #include <X11/Xlibint.h>
42 #include "glcontextmodes.h"
44 /*#define DRI_NEW_INTERFACE_ONLY*/
46 #ifndef GLX_OML_sync_control
47 typedef Bool ( * PFNGLXGETMSCRATEOMLPROC
) (Display
*dpy
, GLXDrawable drawable
, int32_t *numerator
, int32_t *denominator
);
51 * This is used in a couple of places that call \c driCreateNewDrawable.
53 static const int empty_attribute_list
[1] = { None
};
56 * Function used to determine if a drawable (window) still exists. Ideally
57 * this function comes from libGL. With older versions of libGL from XFree86
58 * we can fall-back to an internal version.
60 * \sa __driWindowExists __glXWindowExists
62 static PFNGLXWINDOWEXISTSPROC window_exists
;
64 typedef Bool (*PFNGLXCREATECONTEXTWITHCONFIGPROC
)( Display
*, int, int, void *,
67 static PFNGLXCREATECONTEXTWITHCONFIGPROC create_context_with_config
;
70 * Cached copy of the internal API version used by libGL and the client-side
73 static int api_ver
= 0;
75 /* forward declarations */
76 static int driQueryFrameTracking( Display
* dpy
, void * priv
,
77 int64_t * sbc
, int64_t * missedFrames
, float * lastMissedUsage
,
80 static void *driCreateNewDrawable(Display
*dpy
, const __GLcontextModes
*modes
,
81 GLXDrawable draw
, __DRIdrawable
*pdraw
, int renderType
, const int *attrs
);
83 static void driDestroyDrawable(Display
*dpy
, void *drawablePrivate
);
89 static Bool
driFeatureOn(const char *name
)
91 char *env
= getenv(name
);
93 if (!env
) return GL_FALSE
;
94 if (!strcasecmp(env
, "enable")) return GL_TRUE
;
95 if (!strcasecmp(env
, "1")) return GL_TRUE
;
96 if (!strcasecmp(env
, "on")) return GL_TRUE
;
97 if (!strcasecmp(env
, "true")) return GL_TRUE
;
98 if (!strcasecmp(env
, "t")) return GL_TRUE
;
99 if (!strcasecmp(env
, "yes")) return GL_TRUE
;
100 if (!strcasecmp(env
, "y")) return GL_TRUE
;
104 #endif /* not_defined */
108 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
111 * Is called from the drivers.
113 * \param f \c printf like format string.
116 __driUtilMessage(const char *f
, ...)
120 if (getenv("LIBGL_DEBUG")) {
121 fprintf(stderr
, "libGL error: \n");
123 vfprintf(stderr
, f
, args
);
125 fprintf(stderr
, "\n");
130 /*****************************************************************/
131 /** \name Visual utility functions */
132 /*****************************************************************/
135 #ifndef DRI_NEW_INTERFACE_ONLY
137 * Find a \c __GLcontextModes structure matching the given visual ID.
139 * \param dpy Display to search for a matching configuration.
140 * \param scrn Screen number on \c dpy to be searched.
141 * \param vid Desired \c VisualID to find.
143 * \returns A pointer to a \c __GLcontextModes structure that matches \c vid,
144 * if found, or \c NULL if no match is found.
146 static const __GLcontextModes
*
147 findConfigMode(Display
*dpy
, int scrn
, VisualID vid
,
148 const __DRIscreen
* pDRIScreen
)
150 if ( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) ) {
151 const __DRIscreenPrivate
* const psp
=
152 (const __DRIscreenPrivate
*) pDRIScreen
->private;
154 return _gl_context_modes_find_visual( psp
->modes
, vid
);
162 * This function is a hack to work-around old versions of libGL.so that
163 * do not export \c XF86DRICreateContextWithConfig. I would modify the
164 * code to just use this function, but the stand-alone driver (i.e., DRI
165 * drivers that are built to work without XFree86) shouldn't have to know
166 * about X structures like a \c Visual.
169 fake_XF86DRICreateContextWithConfig( Display
* dpy
, int screen
, int configID
,
170 XID
* context
, drmContextPtr hHWContext
)
174 vis
.visualid
= configID
;
175 return XF86DRICreateContext( dpy
, screen
, & vis
, context
, hHWContext
);
177 #endif /* DRI_NEW_INTERFACE_ONLY */
182 /*****************************************************************/
183 /** \name Drawable list management */
184 /*****************************************************************/
187 static Bool
__driAddDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
189 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
191 if (drmHashInsert(drawHash
, pdp
->draw
, pdraw
))
197 static __DRIdrawable
*__driFindDrawable(void *drawHash
, GLXDrawable draw
)
200 __DRIdrawable
*pdraw
;
202 retcode
= drmHashLookup(drawHash
, draw
, (void **)&pdraw
);
209 static void __driRemoveDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
212 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
214 retcode
= drmHashLookup(drawHash
, pdp
->draw
, (void **)&pdraw
);
215 if (!retcode
) { /* Found */
216 drmHashDelete(drawHash
, pdp
->draw
);
220 #ifndef DRI_NEW_INTERFACE_ONLY
221 static Bool __driWindowExistsFlag
;
223 static int __driWindowExistsErrorHandler(Display
*dpy
, XErrorEvent
*xerr
)
225 if (xerr
->error_code
== BadWindow
) {
226 __driWindowExistsFlag
= GL_FALSE
;
232 * Determine if a window associated with a \c GLXDrawable exists on the
235 * \param dpy Display associated with the drawable to be queried.
236 * \param draw \c GLXDrawable to test.
238 * \returns \c GL_TRUE if a window exists that is associated with \c draw,
239 * otherwise \c GL_FALSE is returned.
241 * \warning This function is not currently thread-safe.
244 * \c __glXWindowExists (from libGL) is prefered over this function. Starting
245 * with the next major release of XFree86, this function will be removed.
246 * Even now this function is no longer directly called. Instead it is called
247 * via a function pointer if and only if \c __glXWindowExists does not exist.
249 * \sa __glXWindowExists glXGetProcAddress window_exists
251 static Bool
__driWindowExists(Display
*dpy
, GLXDrawable draw
)
253 XWindowAttributes xwa
;
254 int (*oldXErrorHandler
)(Display
*, XErrorEvent
*);
256 XSync(dpy
, GL_FALSE
);
257 __driWindowExistsFlag
= GL_TRUE
;
258 oldXErrorHandler
= XSetErrorHandler(__driWindowExistsErrorHandler
);
259 XGetWindowAttributes(dpy
, draw
, &xwa
); /* dummy request */
260 XSetErrorHandler(oldXErrorHandler
);
261 return __driWindowExistsFlag
;
263 #endif /* DRI_NEW_INTERFACE_ONLY */
266 * Find drawables in the local hash that have been destroyed on the
269 * \param drawHash Hash-table containing all know drawables.
271 static void __driGarbageCollectDrawables(void *drawHash
)
274 __DRIdrawable
*pdraw
;
277 if (drmHashFirst(drawHash
, &draw
, (void **)&pdraw
)) {
279 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
280 dpy
= pdp
->driScreenPriv
->display
;
281 if (! (*window_exists
)(dpy
, draw
)) {
282 /* Destroy the local drawable data in the hash table, if the
283 drawable no longer exists in the Xserver */
284 __driRemoveDrawable(drawHash
, pdraw
);
285 (*pdraw
->destroyDrawable
)(dpy
, pdraw
->private);
288 } while (drmHashNext(drawHash
, &draw
, (void **)&pdraw
));
295 /*****************************************************************/
296 /** \name Context (un)binding functions */
297 /*****************************************************************/
303 * \param dpy the display handle.
304 * \param scrn the screen number.
305 * \param draw drawable.
306 * \param read Current reading drawable.
309 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
312 * This function calls __DriverAPIRec::UnbindContext, and then decrements
313 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
316 * While casting the opaque private pointers associated with the parameters
317 * into their respective real types it also assures they are not \c NULL.
319 static Bool
driUnbindContext3(Display
*dpy
, int scrn
,
320 GLXDrawable draw
, GLXDrawable read
,
323 __DRIscreen
*pDRIScreen
;
324 __DRIdrawable
*pdraw
;
325 __DRIdrawable
*pread
;
326 __DRIcontextPrivate
*pcp
;
327 __DRIscreenPrivate
*psp
;
328 __DRIdrawablePrivate
*pdp
;
329 __DRIdrawablePrivate
*prp
;
332 ** Assume error checking is done properly in glXMakeCurrent before
333 ** calling driUnbindContext3.
336 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
341 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
342 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
347 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
348 pcp
= (__DRIcontextPrivate
*)ctx
->private;
350 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
355 pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
357 pread
= __driFindDrawable(psp
->drawHash
, read
);
362 prp
= (__DRIdrawablePrivate
*)pread
->private;
365 /* Let driver unbind drawable from context */
366 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
369 if (pdp
->refcount
== 0) {
377 if (prp
->refcount
== 0) {
386 /* XXX this is disabled so that if we call SwapBuffers on an unbound
387 * window we can determine the last context bound to the window and
388 * use that context's lock. (BrianP, 2-Dec-2000)
391 /* Unbind the drawable */
392 pcp
->driDrawablePriv
= NULL
;
393 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
401 * This function takes both a read buffer and a draw buffer. This is needed
402 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
405 * \bug This function calls \c driCreateNewDrawable in two places with the
406 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
407 * be needed in those places when support for pbuffers and / or pixmaps
408 * is added. Is it safe to assume that the drawable is a window?
410 static Bool
DoBindContext(Display
*dpy
,
411 GLXDrawable draw
, GLXDrawable read
,
412 __DRIcontext
*ctx
, const __GLcontextModes
* modes
,
413 __DRIscreenPrivate
*psp
)
415 __DRIdrawable
*pdraw
;
416 __DRIdrawablePrivate
*pdp
;
417 __DRIdrawable
*pread
;
418 __DRIdrawablePrivate
*prp
;
419 __DRIcontextPrivate
* const pcp
= ctx
->private;
422 /* Find the _DRIdrawable which corresponds to the writing GLXDrawable */
423 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
425 /* Allocate a new drawable */
426 pdraw
= (__DRIdrawable
*)Xmalloc(sizeof(__DRIdrawable
));
432 /* Create a new drawable */
433 driCreateNewDrawable(dpy
, modes
, draw
, pdraw
, GLX_WINDOW_BIT
,
434 empty_attribute_list
);
435 if (!pdraw
->private) {
442 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
444 /* Find the _DRIdrawable which corresponds to the reading GLXDrawable */
446 /* read buffer == draw buffer */
450 pread
= __driFindDrawable(psp
->drawHash
, read
);
452 /* Allocate a new drawable */
453 pread
= (__DRIdrawable
*)Xmalloc(sizeof(__DRIdrawable
));
459 /* Create a new drawable */
460 driCreateNewDrawable(dpy
, modes
, read
, pread
, GLX_WINDOW_BIT
,
461 empty_attribute_list
);
462 if (!pread
->private) {
468 prp
= (__DRIdrawablePrivate
*) pread
->private;
471 /* Bind the drawable to the context */
472 pcp
->driDrawablePriv
= pdp
;
473 pdp
->driContextPriv
= pcp
;
480 ** Now that we have a context associated with this drawable, we can
481 ** initialize the drawable information if has not been done before.
483 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
484 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
485 __driUtilUpdateDrawableInfo(pdp
);
486 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
489 /* Call device-specific MakeCurrent */
490 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
497 * This function takes both a read buffer and a draw buffer. This is needed
498 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
501 static Bool
driBindContext3(Display
*dpy
, int scrn
,
502 GLXDrawable draw
, GLXDrawable read
,
505 __DRIscreen
*pDRIScreen
;
508 ** Assume error checking is done properly in glXMakeCurrent before
509 ** calling driBindContext.
512 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
517 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
518 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
523 return DoBindContext( dpy
, draw
, read
, ctx
, ctx
->mode
,
524 (__DRIscreenPrivate
*)pDRIScreen
->private );
528 #ifndef DRI_NEW_INTERFACE_ONLY
530 * This function takes both a read buffer and a draw buffer. This is needed
531 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
534 static Bool
driBindContext2(Display
*dpy
, int scrn
,
535 GLXDrawable draw
, GLXDrawable read
,
538 __DRIscreen
*pDRIScreen
;
539 const __GLcontextModes
*modes
;
542 ** Assume error checking is done properly in glXMakeCurrent before
543 ** calling driBindContext.
546 if (gc
== NULL
|| draw
== None
|| read
== None
) {
551 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
552 modes
= (driCompareGLXAPIVersion( 20040317 ) >= 0)
553 ? gc
->driContext
.mode
554 : findConfigMode( dpy
, scrn
, gc
->vid
, pDRIScreen
);
556 if ( modes
== NULL
) {
561 /* findConfigMode will return NULL if the DRI screen or screen private
564 assert( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) );
566 return DoBindContext( dpy
, draw
, read
, & gc
->driContext
, modes
,
567 (__DRIscreenPrivate
*)pDRIScreen
->private );
570 static Bool
driUnbindContext2(Display
*dpy
, int scrn
,
571 GLXDrawable draw
, GLXDrawable read
,
574 return driUnbindContext3(dpy
, scrn
, draw
, read
, & gc
->driContext
);
578 * Simply call bind with the same GLXDrawable for the read and draw buffers.
580 static Bool
driBindContext(Display
*dpy
, int scrn
,
581 GLXDrawable draw
, GLXContext gc
)
583 return driBindContext2(dpy
, scrn
, draw
, draw
, gc
);
588 * Simply call bind with the same GLXDrawable for the read and draw buffers.
590 static Bool
driUnbindContext(Display
*dpy
, int scrn
,
591 GLXDrawable draw
, GLXContext gc
,
595 return driUnbindContext2( dpy
, scrn
, draw
, draw
, gc
);
597 #endif /* DRI_NEW_INTERFACE_ONLY */
602 /*****************************************************************/
603 /** \name Drawable handling functions */
604 /*****************************************************************/
608 * Update private drawable information.
610 * \param pdp pointer to the private drawable information to update.
612 * This function basically updates the __DRIdrawablePrivate struct's
613 * cliprect information by calling \c __DRIDrawablePrivate::getInfo. This is
614 * usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
615 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
616 * the values are different that means we have to update the clipping
620 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
622 __DRIscreenPrivate
*psp
;
623 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
625 if (!pcp
|| (pdp
!= pcp
->driDrawablePriv
)) {
630 psp
= pdp
->driScreenPriv
;
636 if (pdp
->pClipRects
) {
637 Xfree(pdp
->pClipRects
);
640 if (pdp
->pBackClipRects
) {
641 Xfree(pdp
->pBackClipRects
);
644 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
646 if (!__driFindDrawable(psp
->drawHash
, pdp
->draw
) ||
647 ! (*pdp
->getInfo
)(pdp
->display
, pdp
->screen
, pdp
->draw
,
648 &pdp
->index
, &pdp
->lastStamp
,
649 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
650 &pdp
->numClipRects
, &pdp
->pClipRects
,
653 &pdp
->numBackClipRects
,
654 &pdp
->pBackClipRects
)) {
655 /* Error -- eg the window may have been destroyed. Keep going
658 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
659 pdp
->numClipRects
= 0;
660 pdp
->pClipRects
= NULL
;
661 pdp
->numBackClipRects
= 0;
662 pdp
->pBackClipRects
= NULL
;
665 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
667 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
673 /*****************************************************************/
674 /** \name GLX callbacks */
675 /*****************************************************************/
681 * \param dpy the display handle.
682 * \param drawablePrivate opaque pointer to the per-drawable private info.
685 * This function calls __DRIdrawablePrivate::swapBuffers.
687 * Is called directly from glXSwapBuffers().
689 static void driSwapBuffers( Display
*dpy
, void *drawablePrivate
)
691 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
692 dPriv
->swapBuffers(dPriv
);
697 * Called directly from a number of higher-level GLX functions.
699 static int driGetMSC( void *screenPrivate
, int64_t *msc
)
701 __DRIscreenPrivate
*sPriv
= (__DRIscreenPrivate
*) screenPrivate
;
703 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
707 * Called directly from a number of higher-level GLX functions.
709 static int driGetSBC( Display
*dpy
, void *drawablePrivate
, int64_t *sbc
)
711 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
716 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
717 *sbc
= sInfo
.swap_count
;
722 static int driWaitForSBC( Display
* dpy
, void *drawablePriv
,
724 int64_t * msc
, int64_t * sbc
)
726 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
728 return dPriv
->driScreenPriv
->DriverAPI
.WaitForSBC( dPriv
, target_sbc
,
732 static int driWaitForMSC( Display
* dpy
, void *drawablePriv
,
734 int64_t divisor
, int64_t remainder
,
735 int64_t * msc
, int64_t * sbc
)
737 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
742 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
746 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
747 * is supported but GLX_OML_sync_control is not. Therefore, don't return
748 * an error value if GetSwapInfo() is not implemented.
751 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
752 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
753 *sbc
= sInfo
.swap_count
;
759 static int64_t driSwapBuffersMSC( Display
* dpy
, void *drawablePriv
,
761 int64_t divisor
, int64_t remainder
)
763 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
765 return dPriv
->driScreenPriv
->DriverAPI
.SwapBuffersMSC( dPriv
, target_msc
,
772 * This is called via __DRIscreenRec's createNewDrawable pointer.
774 static void *driCreateNewDrawable(Display
*dpy
,
775 const __GLcontextModes
*modes
,
777 __DRIdrawable
*pdraw
,
781 __DRIscreen
* const pDRIScreen
= __glXFindDRIScreen(dpy
, modes
->screen
);
782 __DRIscreenPrivate
*psp
;
783 __DRIdrawablePrivate
*pdp
;
786 /* Since pbuffers are not yet supported, no drawable attributes are
791 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
795 pdp
= (__DRIdrawablePrivate
*)Xmalloc(sizeof(__DRIdrawablePrivate
));
800 if (!XF86DRICreateDrawable(dpy
, modes
->screen
, draw
, &pdp
->hHWDrawable
)) {
815 pdp
->numClipRects
= 0;
816 pdp
->numBackClipRects
= 0;
817 pdp
->pClipRects
= NULL
;
818 pdp
->pBackClipRects
= NULL
;
820 pdp
->screen
= modes
->screen
;
822 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
823 pdp
->driScreenPriv
= psp
;
824 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
826 pdp
->getInfo
= (GetDrawableInfo
*)
827 glXGetProcAddress( (const GLubyte
*) "__glXGetDrawableInfo" );
828 if ( pdp
->getInfo
== NULL
) {
829 pdp
->getInfo
= XF86DRIGetDrawableInfo
;
832 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
833 renderType
== GLX_PIXMAP_BIT
)) {
834 (void)XF86DRIDestroyDrawable(dpy
, modes
->screen
, pdp
->draw
);
839 pdraw
->private = pdp
;
840 pdraw
->destroyDrawable
= driDestroyDrawable
;
841 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
843 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
844 pdraw
->getSBC
= driGetSBC
;
845 pdraw
->waitForSBC
= driWaitForSBC
;
846 pdraw
->waitForMSC
= driWaitForMSC
;
847 pdraw
->swapBuffersMSC
= driSwapBuffersMSC
;
848 pdraw
->frameTracking
= NULL
;
849 pdraw
->queryFrameTracking
= driQueryFrameTracking
;
851 /* This special default value is replaced with the configured
852 * default value when the drawable is first bound to a direct
853 * rendering context. */
854 pdraw
->swap_interval
= (unsigned)-1;
857 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
859 /* Add pdraw to drawable list */
860 if (!__driAddDrawable(psp
->drawHash
, pdraw
)) {
862 (*pdraw
->destroyDrawable
)(dpy
, pdp
);
865 pdraw
->private = NULL
;
871 static __DRIdrawable
*driGetDrawable(Display
*dpy
, GLXDrawable draw
,
874 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
877 ** Make sure this routine returns NULL if the drawable is not bound
878 ** to a direct rendering context!
880 return __driFindDrawable(psp
->drawHash
, draw
);
883 static void driDestroyDrawable(Display
*dpy
, void *drawablePrivate
)
885 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*) drawablePrivate
;
886 __DRIscreenPrivate
*psp
= pdp
->driScreenPriv
;
887 int scrn
= psp
->myNum
;
890 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
891 if ((*window_exists
)(dpy
, pdp
->draw
))
892 (void)XF86DRIDestroyDrawable(dpy
, scrn
, pdp
->draw
);
893 if (pdp
->pClipRects
) {
894 Xfree(pdp
->pClipRects
);
895 pdp
->pClipRects
= NULL
;
897 if (pdp
->pBackClipRects
) {
898 Xfree(pdp
->pBackClipRects
);
899 pdp
->pBackClipRects
= NULL
;
908 /*****************************************************************/
909 /** \name Context handling functions */
910 /*****************************************************************/
914 * Destroy the per-context private information.
916 * \param dpy the display handle.
917 * \param scrn the screen number.
918 * \param contextPrivate opaque pointer to the per-drawable private info.
921 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
922 * drmDestroyContext(), and finally frees \p contextPrivate.
924 static void driDestroyContext(Display
*dpy
, int scrn
, void *contextPrivate
)
926 __DRIcontextPrivate
*pcp
= (__DRIcontextPrivate
*) contextPrivate
;
929 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
930 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
931 (void)XF86DRIDestroyContext(dpy
, scrn
, pcp
->contextID
);
938 * Create the per-drawable private driver information.
940 * \param dpy The display handle.
941 * \param modes Mode used to create the new context.
942 * \param render_type Type of rendering target. \c GLX_RGBA is the only
943 * type likely to ever be supported for direct-rendering.
944 * \param sharedPrivate The shared context dependent methods or \c NULL if
946 * \param pctx DRI context to receive the context dependent methods.
948 * \returns An opaque pointer to the per-context private information on
949 * success, or \c NULL on failure.
952 * This function allocates and fills a __DRIcontextPrivateRec structure. It
953 * performs some device independent initialization and passes all the
954 * relevent information to __DriverAPIRec::CreateContext to create the
959 driCreateNewContext(Display
*dpy
, const __GLcontextModes
*modes
,
960 int render_type
, void *sharedPrivate
, __DRIcontext
*pctx
)
962 __DRIscreen
*pDRIScreen
;
963 __DRIcontextPrivate
*pcp
;
964 __DRIcontextPrivate
*pshare
= (__DRIcontextPrivate
*) sharedPrivate
;
965 __DRIscreenPrivate
*psp
;
966 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
968 pDRIScreen
= __glXFindDRIScreen(dpy
, modes
->screen
);
969 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
974 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
976 pcp
= (__DRIcontextPrivate
*)Xmalloc(sizeof(__DRIcontextPrivate
));
981 if (! (*create_context_with_config
)(dpy
, modes
->screen
, modes
->fbconfigID
,
982 &pcp
->contextID
, &pcp
->hHWContext
)) {
988 pcp
->driScreenPriv
= psp
;
989 pcp
->driDrawablePriv
= NULL
;
991 /* When the first context is created for a screen, initialize a "dummy"
995 if (!psp
->dummyContextPriv
.driScreenPriv
) {
996 psp
->dummyContextPriv
.contextID
= 0;
997 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
998 psp
->dummyContextPriv
.driScreenPriv
= psp
;
999 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
1000 psp
->dummyContextPriv
.driverPrivate
= NULL
;
1001 /* No other fields should be used! */
1004 pctx
->destroyContext
= driDestroyContext
;
1005 #ifdef DRI_NEW_INTERFACE_ONLY
1006 pctx
->bindContext
= NULL
;
1007 pctx
->unbindContext
= NULL
;
1008 pctx
->bindContext2
= NULL
;
1009 pctx
->unbindContext2
= NULL
;
1010 pctx
->bindContex3
= driBindContext3
;
1011 pctx
->unbindContext3
= driUnbindContext3
;
1013 pctx
->bindContext
= driBindContext
;
1014 pctx
->unbindContext
= driUnbindContext
;
1015 if ( driCompareGLXAPIVersion( 20030606 ) >= 0 ) {
1016 pctx
->bindContext2
= driBindContext2
;
1017 pctx
->unbindContext2
= driUnbindContext2
;
1020 if ( driCompareGLXAPIVersion( 20040415 ) >= 0 ) {
1021 pctx
->bindContext3
= driBindContext3
;
1022 pctx
->unbindContext3
= driUnbindContext3
;
1026 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
1027 (void)XF86DRIDestroyContext(dpy
, modes
->screen
, pcp
->contextID
);
1032 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
1038 #ifndef DRI_NEW_INTERFACE_ONLY
1040 * Create the per-drawable private driver information.
1042 * \param dpy the display handle.
1043 * \param vis the visual information.
1044 * \param sharedPrivate the shared context dependent methods or \c NULL if
1046 * \param pctx will receive the context dependent methods.
1048 * \returns a opaque pointer to the per-context private information on success, or \c NULL
1052 * This function has been replaced by \c driCreateNewContext. In drivers
1053 * built to work with XFree86, this function will continue to exist to support
1054 * older versions of libGL. Starting with the next major relelase of XFree86,
1055 * this function will be removed.
1058 * This function allocates and fills a __DRIcontextPrivateRec structure. It
1059 * gets the visual, converts it into a __GLcontextModesRec and passes it
1060 * to __DriverAPIRec::CreateContext to create the context.
1062 static void *driCreateContext(Display
*dpy
, XVisualInfo
*vis
,
1063 void *sharedPrivate
, __DRIcontext
*pctx
)
1065 __DRIscreen
*pDRIScreen
;
1066 const __GLcontextModes
*modes
;
1068 pDRIScreen
= __glXFindDRIScreen(dpy
, vis
->screen
);
1069 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
1075 /* Setup a __GLcontextModes struct corresponding to vis->visualid
1076 * and create the rendering context.
1079 modes
= findConfigMode(dpy
, vis
->screen
, vis
->visualid
, pDRIScreen
);
1080 return (modes
== NULL
)
1082 : driCreateNewContext( dpy
, modes
, GLX_RGBA_TYPE
,
1083 sharedPrivate
, pctx
);
1085 #endif /* DRI_NEW_INTERFACE_ONLY */
1090 /*****************************************************************/
1091 /** \name Screen handling functions */
1092 /*****************************************************************/
1096 * Destroy the per-screen private information.
1098 * \param dpy the display handle.
1099 * \param scrn the screen number.
1100 * \param screenPrivate opaque pointer to the per-screen private information.
1103 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
1104 * drmClose(), and finally frees \p screenPrivate.
1106 static void driDestroyScreen(Display
*dpy
, int scrn
, void *screenPrivate
)
1108 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
1111 /* No interaction with the X-server is possible at this point. This
1112 * routine is called after XCloseDisplay, so there is no protocol
1113 * stream open to the X-server anymore.
1116 if (psp
->DriverAPI
.DestroyScreen
)
1117 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
1119 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
1120 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
1121 Xfree(psp
->pDevPriv
);
1122 (void)drmClose(psp
->fd
);
1123 if ( psp
->modes
!= NULL
) {
1124 _gl_context_modes_destroy( psp
->modes
);
1132 * Utility function used to create a new driver-private screen structure.
1134 * \param dpy Display pointer
1135 * \param scrn Index of the screen
1136 * \param psc DRI screen data (not driver private)
1137 * \param modes Linked list of known display modes. This list is, at a
1138 * minimum, a list of modes based on the current display mode.
1139 * These roughly match the set of available X11 visuals, but it
1140 * need not be limited to X11! The calling libGL should create
1141 * a list that will inform the driver of the current display
1142 * mode (i.e., color buffer depth, depth buffer depth, etc.).
1143 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
1145 * \param dri_version Version of the "server-side" DRI.
1146 * \param drm_version Version of the kernel DRM.
1147 * \param frame_buffer Data describing the location and layout of the
1149 * \param pSAREA Pointer the the SAREA.
1150 * \param fd Device handle for the DRM.
1151 * \param internal_api_version Version of the internal interface between the
1153 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1155 __DRIscreenPrivate
*
1156 __driUtilCreateNewScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
1157 __GLcontextModes
* modes
,
1158 const __DRIversion
* ddx_version
,
1159 const __DRIversion
* dri_version
,
1160 const __DRIversion
* drm_version
,
1161 const __DRIframebuffer
* frame_buffer
,
1164 int internal_api_version
,
1165 const struct __DriverAPIRec
*driverAPI
)
1167 __DRIscreenPrivate
*psp
;
1170 #ifdef DRI_NEW_INTERFACE_ONLY
1171 if ( internal_api_version
< 20040415 ) {
1172 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1173 "20040415 or later is required.\n", internal_api_version
);
1177 if ( internal_api_version
== 20031201 ) {
1178 fprintf( stderr
, "libGL error: libGL version 20031201 has critical "
1179 "binary compatilibity bugs.\nlibGL error: You must upgrade "
1180 "to use direct-rendering!\n" );
1183 #endif /* DRI_NEW_INTERFACE_ONLY */
1186 window_exists
= (PFNGLXWINDOWEXISTSPROC
)
1187 glXGetProcAddress( (const GLubyte
*) "__glXWindowExists" );
1189 if ( window_exists
== NULL
) {
1190 #ifdef DRI_NEW_INTERFACE_ONLY
1191 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1192 "20021128 or later is required.\n", internal_api_version
);
1195 window_exists
= (PFNGLXWINDOWEXISTSPROC
) __driWindowExists
;
1196 #endif /* DRI_NEW_INTERFACE_ONLY */
1199 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1200 glXGetProcAddress( (const GLubyte
*) "__glXCreateContextWithConfig" );
1201 if ( create_context_with_config
== NULL
) {
1202 #ifdef DRI_NEW_INTERFACE_ONLY
1203 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1204 "20031201 or later is required.\n", internal_api_version
);
1207 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1208 fake_XF86DRICreateContextWithConfig
;
1209 #endif /* DRI_NEW_INTERFACE_ONLY */
1212 api_ver
= internal_api_version
;
1214 psp
= (__DRIscreenPrivate
*)Xmalloc(sizeof(__DRIscreenPrivate
));
1219 /* Create the hash table */
1220 psp
->drawHash
= drmHashCreate();
1221 if ( psp
->drawHash
== NULL
) {
1232 ** NOT_DONE: This is used by the X server to detect when the client
1233 ** has died while holding the drawable lock. The client sets the
1234 ** drawable lock to this value.
1236 psp
->drawLockID
= 1;
1238 psp
->drmMajor
= drm_version
->major
;
1239 psp
->drmMinor
= drm_version
->minor
;
1240 psp
->drmPatch
= drm_version
->patch
;
1241 psp
->ddxMajor
= ddx_version
->major
;
1242 psp
->ddxMinor
= ddx_version
->minor
;
1243 psp
->ddxPatch
= ddx_version
->patch
;
1244 psp
->driMajor
= dri_version
->major
;
1245 psp
->driMinor
= dri_version
->minor
;
1246 psp
->driPatch
= dri_version
->patch
;
1248 /* install driver's callback functions */
1249 memcpy( &psp
->DriverAPI
, driverAPI
, sizeof(struct __DriverAPIRec
) );
1251 psp
->pSAREA
= pSAREA
;
1253 psp
->pFB
= frame_buffer
->base
;
1254 psp
->fbSize
= frame_buffer
->size
;
1255 psp
->fbStride
= frame_buffer
->stride
;
1256 psp
->fbWidth
= frame_buffer
->width
;
1257 psp
->fbHeight
= frame_buffer
->height
;
1258 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
1259 psp
->pDevPriv
= frame_buffer
->dev_priv
;
1264 ** Do not init dummy context here; actual initialization will be
1265 ** done when the first DRI context is created. Init screen priv ptr
1266 ** to NULL to let CreateContext routine that it needs to be inited.
1268 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
1270 psc
->destroyScreen
= driDestroyScreen
;
1271 #ifndef DRI_NEW_INTERFACE_ONLY
1272 psc
->createContext
= driCreateContext
;
1274 psc
->createConteext
= NULL
;
1276 psc
->createNewDrawable
= driCreateNewDrawable
;
1277 psc
->getDrawable
= driGetDrawable
;
1278 #ifdef DRI_NEW_INTERFACE_ONLY
1279 psc
->getMSC
= driGetMSC
;
1280 psc
->createNewContext
= driCreateNewContext
;
1282 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
1283 psc
->getMSC
= driGetMSC
;
1285 if ( driCompareGLXAPIVersion( 20030824 ) >= 0 ) {
1286 psc
->createNewContext
= driCreateNewContext
;
1291 if ( (psp
->DriverAPI
.InitDriver
!= NULL
)
1292 && !(*psp
->DriverAPI
.InitDriver
)(psp
) ) {
1302 #ifndef DRI_NEW_INTERFACE_ONLY
1304 * Utility function used to create a new driver-private screen structure.
1306 * \param dpy Display pointer.
1307 * \param scrn Index of the screen.
1308 * \param psc DRI screen data (not driver private)
1309 * \param numConfigs Number of visual configs pointed to by \c configs.
1310 * \param configs Array of GLXvisualConfigs exported by the 2D driver.
1311 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1314 * This function has been replaced by \c __driUtilCreateNewScreen. In drivers
1315 * built to work with XFree86, this function will continue to exist to support
1316 * older versions of libGL. Starting with the next major relelase of XFree86,
1317 * this function will be removed.
1319 __DRIscreenPrivate
*
1320 __driUtilCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
1321 int numConfigs
, __GLXvisualConfig
*configs
,
1322 const struct __DriverAPIRec
*driverAPI
)
1325 __DRIscreenPrivate
*psp
= NULL
;
1329 __GLcontextModes
*modes
;
1330 __GLcontextModes
*temp
;
1332 __DRIversion ddx_version
;
1333 __DRIversion dri_version
;
1334 __DRIversion drm_version
;
1335 __DRIframebuffer framebuffer
;
1338 const char * err_msg
;
1339 const char * err_extra
;
1342 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrn
, &directCapable
)
1343 || !directCapable
) {
1348 /* Create the linked list of context modes, and populate it with the
1349 * GLX visual information passed in by libGL.
1352 modes
= _gl_context_modes_create( numConfigs
, sizeof(__GLcontextModes
) );
1353 if ( modes
== NULL
) {
1358 for ( i
= 0 ; i
< numConfigs
; i
++ ) {
1359 assert( temp
!= NULL
);
1360 _gl_copy_visual_to_context_mode( temp
, & configs
[i
] );
1361 temp
->screen
= scrn
;
1366 err_msg
= "XF86DRIOpenConnection";
1369 if (XF86DRIOpenConnection(dpy
, scrn
, &hSAREA
, &BusID
)) {
1370 fd
= drmOpen(NULL
,BusID
);
1371 Xfree(BusID
); /* No longer needed */
1373 err_msg
= "open DRM";
1374 err_extra
= strerror( -fd
);
1379 err_msg
= "drmGetMagic";
1382 if (!drmGetMagic(fd
, &magic
)) {
1383 drmVersionPtr version
= drmGetVersion(fd
);
1385 drm_version
.major
= version
->version_major
;
1386 drm_version
.minor
= version
->version_minor
;
1387 drm_version
.patch
= version
->version_patchlevel
;
1388 drmFreeVersion(version
);
1391 drm_version
.major
= -1;
1392 drm_version
.minor
= -1;
1393 drm_version
.patch
= -1;
1396 err_msg
= "XF86DRIAuthConnection";
1397 if (XF86DRIAuthConnection(dpy
, scrn
, magic
)) {
1401 * Get device name (like "tdfx") and the ddx version numbers.
1402 * We'll check the version in each DRI driver's "createScreen"
1405 err_msg
= "XF86DRIGetClientDriverName";
1406 if (XF86DRIGetClientDriverName(dpy
, scrn
,
1412 /* No longer needed. */
1413 Xfree( driverName
);
1416 * Get the DRI X extension version.
1418 err_msg
= "XF86DRIQueryVersion";
1419 if (XF86DRIQueryVersion(dpy
,
1422 &dri_version
.patch
)) {
1427 * Get device-specific info. pDevPriv will point to a struct
1428 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
1429 * that has information about the screen size, depth, pitch,
1430 * ancilliary buffers, DRM mmap handles, etc.
1432 err_msg
= "XF86DRIGetDeviceInfo";
1433 if (XF86DRIGetDeviceInfo(dpy
, scrn
,
1437 &framebuffer
.stride
,
1438 &framebuffer
.dev_priv_size
,
1439 &framebuffer
.dev_priv
)) {
1440 framebuffer
.width
= DisplayWidth(dpy
, scrn
);
1441 framebuffer
.height
= DisplayHeight(dpy
, scrn
);
1444 * Map the framebuffer region.
1446 status
= drmMap(fd
, hFB
, framebuffer
.size
,
1447 (drmAddressPtr
)&framebuffer
.base
);
1449 err_msg
= "drmMap of framebuffer";
1450 err_extra
= strerror( -status
);
1452 if ( status
== 0 ) {
1454 * Map the SAREA region. Further mmap regions may be setup in
1455 * each DRI driver's "createScreen" function.
1457 status
= drmMap(fd
, hSAREA
, SAREA_MAX
,
1460 err_msg
= "drmMap of sarea";
1461 err_extra
= strerror( -status
);
1463 if ( status
== 0 ) {
1464 PFNGLXGETINTERNALVERSIONPROC get_ver
;
1466 get_ver
= (PFNGLXGETINTERNALVERSIONPROC
)
1467 glXGetProcAddress( (const GLubyte
*) "__glXGetInternalVersion" );
1469 err_msg
= "InitDriver";
1471 psp
= __driUtilCreateNewScreen( dpy
, scrn
, psc
, modes
,
1478 (get_ver
!= NULL
) ? (*get_ver
)() : 1,
1490 if ( psp
== NULL
) {
1491 if ( pSAREA
!= MAP_FAILED
) {
1492 (void)drmUnmap(pSAREA
, SAREA_MAX
);
1495 if ( framebuffer
.base
!= MAP_FAILED
) {
1496 (void)drmUnmap((drmAddress
)framebuffer
.base
, framebuffer
.size
);
1499 if ( framebuffer
.dev_priv
!= NULL
) {
1500 Xfree(framebuffer
.dev_priv
);
1507 if ( modes
!= NULL
) {
1508 _gl_context_modes_destroy( modes
);
1511 (void)XF86DRICloseConnection(dpy
, scrn
);
1513 if ( err_extra
!= NULL
) {
1514 fprintf(stderr
, "libGL error: %s failed (%s)\n", err_msg
,
1518 fprintf(stderr
, "libGL error: %s failed\n", err_msg
);
1521 fprintf(stderr
, "libGL error: reverting to (slow) indirect rendering\n");
1526 #endif /* DRI_NEW_INTERFACE_ONLY */
1530 * Compare the current GLX API version with a driver supplied required version.
1532 * The minimum required version is compared with the API version exported by
1533 * the \c __glXGetInternalVersion function (in libGL.so).
1535 * \param required_version Minimum required internal GLX API version.
1536 * \return A tri-value return, as from strcmp is returned. A value less
1537 * than, equal to, or greater than zero will be returned if the
1538 * internal GLX API version is less than, equal to, or greater
1539 * than \c required_version.
1541 * \sa __glXGetInternalVersion().
1543 int driCompareGLXAPIVersion( GLuint required_version
)
1545 if ( api_ver
> required_version
) {
1548 else if ( api_ver
== required_version
) {
1557 driQueryFrameTracking( Display
* dpy
, void * priv
,
1558 int64_t * sbc
, int64_t * missedFrames
,
1559 float * lastMissedUsage
, float * usage
)
1561 static PFNGLXGETUSTPROC get_ust
;
1562 __DRIswapInfo sInfo
;
1565 __DRIdrawablePrivate
* dpriv
= (__DRIdrawablePrivate
*) priv
;
1567 if ( get_ust
== NULL
) {
1568 get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
1571 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1572 if ( status
== 0 ) {
1573 *sbc
= sInfo
.swap_count
;
1574 *missedFrames
= sInfo
.swap_missed_count
;
1575 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1577 (*get_ust
)( & ust
);
1578 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1586 * Calculate amount of swap interval used between GLX buffer swaps.
1588 * The usage value, on the range [0,max], is the fraction of total swap
1589 * interval time used between GLX buffer swaps is calculated.
1591 * \f$p = t_d / (i * t_r)\f$
1593 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1594 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1595 * required for a single vertical refresh period (as returned by \c
1596 * glXGetMscRateOML).
1598 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1601 * \param dPriv Pointer to the private drawable structure.
1602 * \return If less than a single swap interval time period was required
1603 * between GLX buffer swaps, a number greater than 0 and less than
1604 * 1.0 is returned. If exactly one swap interval time period is
1605 * required, 1.0 is returned, and if more than one is required then
1606 * a number greater than 1.0 will be returned.
1608 * \sa glXSwapIntervalSGI glXGetMscRateOML
1610 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1611 * be possible to cache the sync rate?
1614 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1615 int64_t current_ust
)
1617 static PFNGLXGETMSCRATEOMLPROC get_msc_rate
= NULL
;
1624 if ( get_msc_rate
== NULL
) {
1625 get_msc_rate
= (PFNGLXGETMSCRATEOMLPROC
)
1626 glXGetProcAddress( (const GLubyte
*) "glXGetMscRateOML" );
1629 if ( (get_msc_rate
!= NULL
)
1630 && get_msc_rate( dPriv
->display
, dPriv
->draw
, &n
, &d
) ) {
1631 interval
= (dPriv
->pdraw
->swap_interval
!= 0)
1632 ? dPriv
->pdraw
->swap_interval
: 1;
1635 /* We want to calculate
1636 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1637 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1638 * dPriv->swap_ust. interval has already been calculated.
1640 * The only tricky part is us_per_refresh. us_per_refresh is
1641 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1642 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1643 * the denominator of the final calculation, we calculate
1644 * (interval * 1000000 * d) and move n into the numerator.
1647 usage
= (current_ust
- last_swap_ust
);
1649 usage
/= (interval
* d
);
1658 #endif /* GLX_DIRECT_RENDERING */