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
37 #ifndef DRI_NEW_INTERFACE_ONLY
38 # include <X11/Xlibint.h>
42 # define _mesa_malloc(b) Xmalloc(b)
43 # define _mesa_free(m) Xfree(m)
47 #endif /* DRI_NEW_INTERFACE_ONLY */
50 #include "drm_sarea.h"
51 #include "glcontextmodes.h"
53 #ifndef PFNGLXGETMSCRATEOMLPROC
54 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRInativeDisplay
*dpy
, __DRIid drawable
, int32_t *numerator
, int32_t *denominator
);
58 * Weak thread-safety dispatch pointer. Older versions of libGL will not have
59 * this symbol, so a "weak" version is included here so that the driver will
60 * dynamically link properly. The value is set to \c NULL. This forces the
61 * driver to fall back to the old dispatch interface.
63 struct _glapi_table
*_glapi_DispatchTSD
__attribute__((weak
)) = NULL
;
66 * This is used in a couple of places that call \c driCreateNewDrawable.
68 static const int empty_attribute_list
[1] = { None
};
71 * Function used to determine if a drawable (window) still exists. Ideally
72 * this function comes from libGL. With older versions of libGL from XFree86
73 * we can fall-back to an internal version.
75 * \sa __driWindowExists __glXWindowExists
77 static PFNGLXWINDOWEXISTSPROC window_exists
;
79 typedef GLboolean (*PFNGLXCREATECONTEXTWITHCONFIGPROC
)( __DRInativeDisplay
*, int, int, void *,
82 static PFNGLXCREATECONTEXTWITHCONFIGPROC create_context_with_config
;
85 * Cached copy of the internal API version used by libGL and the client-side
88 static int api_ver
= 0;
90 /* forward declarations */
91 static int driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
92 int64_t * sbc
, int64_t * missedFrames
, float * lastMissedUsage
,
95 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
96 __DRIid draw
, __DRIdrawable
*pdraw
, int renderType
, const int *attrs
);
98 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
);
104 static GLboolean
driFeatureOn(const char *name
)
106 char *env
= getenv(name
);
108 if (!env
) return GL_FALSE
;
109 if (!strcasecmp(env
, "enable")) return GL_TRUE
;
110 if (!strcasecmp(env
, "1")) return GL_TRUE
;
111 if (!strcasecmp(env
, "on")) return GL_TRUE
;
112 if (!strcasecmp(env
, "true")) return GL_TRUE
;
113 if (!strcasecmp(env
, "t")) return GL_TRUE
;
114 if (!strcasecmp(env
, "yes")) return GL_TRUE
;
115 if (!strcasecmp(env
, "y")) return GL_TRUE
;
119 #endif /* not_defined */
123 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
126 * Is called from the drivers.
128 * \param f \c printf like format string.
131 __driUtilMessage(const char *f
, ...)
135 if (getenv("LIBGL_DEBUG")) {
136 fprintf(stderr
, "libGL error: \n");
138 vfprintf(stderr
, f
, args
);
140 fprintf(stderr
, "\n");
145 /*****************************************************************/
146 /** \name Visual utility functions */
147 /*****************************************************************/
150 #ifndef DRI_NEW_INTERFACE_ONLY
152 * Find a \c __GLcontextModes structure matching the given visual ID.
154 * \param dpy Display to search for a matching configuration.
155 * \param scrn Screen number on \c dpy to be searched.
156 * \param vid Desired \c VisualID to find.
158 * \returns A pointer to a \c __GLcontextModes structure that matches \c vid,
159 * if found, or \c NULL if no match is found.
161 static const __GLcontextModes
*
162 findConfigMode(__DRInativeDisplay
*dpy
, int scrn
, VisualID vid
,
163 const __DRIscreen
* pDRIScreen
)
165 if ( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) ) {
166 const __DRIscreenPrivate
* const psp
=
167 (const __DRIscreenPrivate
*) pDRIScreen
->private;
169 return _gl_context_modes_find_visual( psp
->modes
, vid
);
177 * This function is a hack to work-around old versions of libGL.so that
178 * do not export \c XF86DRICreateContextWithConfig. I would modify the
179 * code to just use this function, but the stand-alone driver (i.e., DRI
180 * drivers that are built to work without XFree86) shouldn't have to know
181 * about X structures like a \c Visual.
184 fake_XF86DRICreateContextWithConfig( __DRInativeDisplay
* dpy
, int screen
, int configID
,
185 XID
* context
, drm_context_t
* hHWContext
)
189 vis
.visualid
= configID
;
190 return XF86DRICreateContext( dpy
, screen
, & vis
, context
, hHWContext
);
192 #endif /* DRI_NEW_INTERFACE_ONLY */
197 /*****************************************************************/
198 /** \name Drawable list management */
199 /*****************************************************************/
202 static GLboolean
__driAddDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
204 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
206 if (drmHashInsert(drawHash
, pdp
->draw
, pdraw
))
212 static __DRIdrawable
*__driFindDrawable(void *drawHash
, __DRIid draw
)
215 __DRIdrawable
*pdraw
;
217 retcode
= drmHashLookup(drawHash
, draw
, (void **)&pdraw
);
224 static void __driRemoveDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
227 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
229 retcode
= drmHashLookup(drawHash
, pdp
->draw
, (void **)&pdraw
);
230 if (!retcode
) { /* Found */
231 drmHashDelete(drawHash
, pdp
->draw
);
235 #ifndef DRI_NEW_INTERFACE_ONLY
236 static GLboolean __driWindowExistsFlag
;
238 static int __driWindowExistsErrorHandler(Display
*dpy
, XErrorEvent
*xerr
)
240 if (xerr
->error_code
== BadWindow
) {
241 __driWindowExistsFlag
= GL_FALSE
;
247 * Determine if a window associated with a \c GLXDrawable exists on the
250 * \param dpy Display associated with the drawable to be queried.
251 * \param draw \c GLXDrawable to test.
253 * \returns \c GL_TRUE if a window exists that is associated with \c draw,
254 * otherwise \c GL_FALSE is returned.
256 * \warning This function is not currently thread-safe.
259 * \c __glXWindowExists (from libGL) is prefered over this function. Starting
260 * with the next major release of XFree86, this function will be removed.
261 * Even now this function is no longer directly called. Instead it is called
262 * via a function pointer if and only if \c __glXWindowExists does not exist.
264 * \sa __glXWindowExists glXGetProcAddress window_exists
266 static GLboolean
__driWindowExists(Display
*dpy
, GLXDrawable draw
)
268 XWindowAttributes xwa
;
269 int (*oldXErrorHandler
)(Display
*, XErrorEvent
*);
271 XSync(dpy
, GL_FALSE
);
272 __driWindowExistsFlag
= GL_TRUE
;
273 oldXErrorHandler
= XSetErrorHandler(__driWindowExistsErrorHandler
);
274 XGetWindowAttributes(dpy
, draw
, &xwa
); /* dummy request */
275 XSetErrorHandler(oldXErrorHandler
);
276 return __driWindowExistsFlag
;
278 #endif /* DRI_NEW_INTERFACE_ONLY */
281 * Find drawables in the local hash that have been destroyed on the
284 * \param drawHash Hash-table containing all know drawables.
286 static void __driGarbageCollectDrawables(void *drawHash
)
289 __DRIdrawable
*pdraw
;
290 __DRInativeDisplay
*dpy
;
292 if (drmHashFirst(drawHash
, &draw
, (void **)&pdraw
)) {
294 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
295 dpy
= pdp
->driScreenPriv
->display
;
296 if (! (*window_exists
)(dpy
, draw
)) {
297 /* Destroy the local drawable data in the hash table, if the
298 drawable no longer exists in the Xserver */
299 __driRemoveDrawable(drawHash
, pdraw
);
300 (*pdraw
->destroyDrawable
)(dpy
, pdraw
->private);
303 } while (drmHashNext(drawHash
, &draw
, (void **)&pdraw
));
310 /*****************************************************************/
311 /** \name Context (un)binding functions */
312 /*****************************************************************/
318 * \param dpy the display handle.
319 * \param scrn the screen number.
320 * \param draw drawable.
321 * \param read Current reading drawable.
324 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
327 * This function calls __DriverAPIRec::UnbindContext, and then decrements
328 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
331 * While casting the opaque private pointers associated with the parameters
332 * into their respective real types it also assures they are not \c NULL.
334 static GLboolean
driUnbindContext3(__DRInativeDisplay
*dpy
, int scrn
,
335 __DRIid draw
, __DRIid read
,
338 __DRIscreen
*pDRIScreen
;
339 __DRIdrawable
*pdraw
;
340 __DRIdrawable
*pread
;
341 __DRIcontextPrivate
*pcp
;
342 __DRIscreenPrivate
*psp
;
343 __DRIdrawablePrivate
*pdp
;
344 __DRIdrawablePrivate
*prp
;
347 ** Assume error checking is done properly in glXMakeCurrent before
348 ** calling driUnbindContext3.
351 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
356 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
357 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
362 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
363 pcp
= (__DRIcontextPrivate
*)ctx
->private;
365 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
370 pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
372 pread
= __driFindDrawable(psp
->drawHash
, read
);
377 prp
= (__DRIdrawablePrivate
*)pread
->private;
380 /* Let driver unbind drawable from context */
381 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
384 if (pdp
->refcount
== 0) {
392 if (prp
->refcount
== 0) {
401 /* XXX this is disabled so that if we call SwapBuffers on an unbound
402 * window we can determine the last context bound to the window and
403 * use that context's lock. (BrianP, 2-Dec-2000)
406 /* Unbind the drawable */
407 pcp
->driDrawablePriv
= NULL
;
408 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
416 * This function takes both a read buffer and a draw buffer. This is needed
417 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
420 * \bug This function calls \c driCreateNewDrawable in two places with the
421 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
422 * be needed in those places when support for pbuffers and / or pixmaps
423 * is added. Is it safe to assume that the drawable is a window?
425 static GLboolean
DoBindContext(__DRInativeDisplay
*dpy
,
426 __DRIid draw
, __DRIid read
,
427 __DRIcontext
*ctx
, const __GLcontextModes
* modes
,
428 __DRIscreenPrivate
*psp
)
430 __DRIdrawable
*pdraw
;
431 __DRIdrawablePrivate
*pdp
;
432 __DRIdrawable
*pread
;
433 __DRIdrawablePrivate
*prp
;
434 __DRIcontextPrivate
* const pcp
= ctx
->private;
437 /* Find the _DRIdrawable which corresponds to the writing drawable. */
438 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
440 /* Allocate a new drawable */
441 pdraw
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
447 /* Create a new drawable */
448 driCreateNewDrawable(dpy
, modes
, draw
, pdraw
, GLX_WINDOW_BIT
,
449 empty_attribute_list
);
450 if (!pdraw
->private) {
457 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
459 /* Find the _DRIdrawable which corresponds to the reading drawable. */
461 /* read buffer == draw buffer */
465 pread
= __driFindDrawable(psp
->drawHash
, read
);
467 /* Allocate a new drawable */
468 pread
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
474 /* Create a new drawable */
475 driCreateNewDrawable(dpy
, modes
, read
, pread
, GLX_WINDOW_BIT
,
476 empty_attribute_list
);
477 if (!pread
->private) {
483 prp
= (__DRIdrawablePrivate
*) pread
->private;
486 /* Bind the drawable to the context */
487 pcp
->driDrawablePriv
= pdp
;
488 pdp
->driContextPriv
= pcp
;
495 ** Now that we have a context associated with this drawable, we can
496 ** initialize the drawable information if has not been done before.
498 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
499 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
500 __driUtilUpdateDrawableInfo(pdp
);
501 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
504 /* Call device-specific MakeCurrent */
505 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
512 * This function takes both a read buffer and a draw buffer. This is needed
513 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
516 static GLboolean
driBindContext3(__DRInativeDisplay
*dpy
, int scrn
,
517 __DRIid draw
, __DRIid read
,
520 __DRIscreen
*pDRIScreen
;
523 ** Assume error checking is done properly in glXMakeCurrent before
524 ** calling driBindContext.
527 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
532 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
533 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
538 return DoBindContext( dpy
, draw
, read
, ctx
, ctx
->mode
,
539 (__DRIscreenPrivate
*)pDRIScreen
->private );
543 #ifndef DRI_NEW_INTERFACE_ONLY
545 * This function takes both a read buffer and a draw buffer. This is needed
546 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
549 static GLboolean
driBindContext2(Display
*dpy
, int scrn
,
550 GLXDrawable draw
, GLXDrawable read
,
553 __DRIscreen
*pDRIScreen
;
554 const __GLcontextModes
*modes
;
557 ** Assume error checking is done properly in glXMakeCurrent before
558 ** calling driBindContext.
561 if (gc
== NULL
|| draw
== None
|| read
== None
) {
566 pDRIScreen
= __glXFindDRIScreen(dpy
, scrn
);
567 modes
= (driCompareGLXAPIVersion( 20040317 ) >= 0)
568 ? gc
->driContext
.mode
569 : findConfigMode( dpy
, scrn
, gc
->vid
, pDRIScreen
);
571 if ( modes
== NULL
) {
576 /* findConfigMode will return NULL if the DRI screen or screen private
579 assert( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) );
581 return DoBindContext( dpy
, draw
, read
, & gc
->driContext
, modes
,
582 (__DRIscreenPrivate
*)pDRIScreen
->private );
585 static GLboolean
driUnbindContext2(Display
*dpy
, int scrn
,
586 GLXDrawable draw
, GLXDrawable read
,
589 return driUnbindContext3(dpy
, scrn
, draw
, read
, & gc
->driContext
);
593 * Simply call bind with the same GLXDrawable for the read and draw buffers.
595 static GLboolean
driBindContext(Display
*dpy
, int scrn
,
596 GLXDrawable draw
, GLXContext gc
)
598 return driBindContext2(dpy
, scrn
, draw
, draw
, gc
);
603 * Simply call bind with the same GLXDrawable for the read and draw buffers.
605 static GLboolean
driUnbindContext(Display
*dpy
, int scrn
,
606 GLXDrawable draw
, GLXContext gc
,
610 return driUnbindContext2( dpy
, scrn
, draw
, draw
, gc
);
612 #endif /* DRI_NEW_INTERFACE_ONLY */
617 /*****************************************************************/
618 /** \name Drawable handling functions */
619 /*****************************************************************/
623 * Update private drawable information.
625 * \param pdp pointer to the private drawable information to update.
627 * This function basically updates the __DRIdrawablePrivate struct's
628 * cliprect information by calling \c __DRIDrawablePrivate::getInfo. This is
629 * usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
630 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
631 * the values are different that means we have to update the clipping
635 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
637 __DRIscreenPrivate
*psp
;
638 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
640 if (!pcp
|| (pdp
!= pcp
->driDrawablePriv
)) {
645 psp
= pdp
->driScreenPriv
;
651 if (pdp
->pClipRects
) {
652 _mesa_free(pdp
->pClipRects
);
655 if (pdp
->pBackClipRects
) {
656 _mesa_free(pdp
->pBackClipRects
);
659 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
661 if (!__driFindDrawable(psp
->drawHash
, pdp
->draw
) ||
662 ! (*pdp
->getInfo
)(pdp
->display
, pdp
->screen
, pdp
->draw
,
663 &pdp
->index
, &pdp
->lastStamp
,
664 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
665 &pdp
->numClipRects
, &pdp
->pClipRects
,
668 &pdp
->numBackClipRects
,
669 &pdp
->pBackClipRects
)) {
670 /* Error -- eg the window may have been destroyed. Keep going
673 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
674 pdp
->numClipRects
= 0;
675 pdp
->pClipRects
= NULL
;
676 pdp
->numBackClipRects
= 0;
677 pdp
->pBackClipRects
= NULL
;
680 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
682 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
688 /*****************************************************************/
689 /** \name GLX callbacks */
690 /*****************************************************************/
696 * \param dpy the display handle.
697 * \param drawablePrivate opaque pointer to the per-drawable private info.
700 * This function calls __DRIdrawablePrivate::swapBuffers.
702 * Is called directly from glXSwapBuffers().
704 static void driSwapBuffers( __DRInativeDisplay
*dpy
, void *drawablePrivate
)
706 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
707 dPriv
->swapBuffers(dPriv
);
712 * Called directly from a number of higher-level GLX functions.
714 static int driGetMSC( void *screenPrivate
, int64_t *msc
)
716 __DRIscreenPrivate
*sPriv
= (__DRIscreenPrivate
*) screenPrivate
;
718 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
722 * Called directly from a number of higher-level GLX functions.
724 static int driGetSBC( __DRInativeDisplay
*dpy
, void *drawablePrivate
, int64_t *sbc
)
726 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
731 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
732 *sbc
= sInfo
.swap_count
;
737 static int driWaitForSBC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
739 int64_t * msc
, int64_t * sbc
)
741 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
743 return dPriv
->driScreenPriv
->DriverAPI
.WaitForSBC( dPriv
, target_sbc
,
747 static int driWaitForMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
749 int64_t divisor
, int64_t remainder
,
750 int64_t * msc
, int64_t * sbc
)
752 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
757 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
761 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
762 * is supported but GLX_OML_sync_control is not. Therefore, don't return
763 * an error value if GetSwapInfo() is not implemented.
766 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
767 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
768 *sbc
= sInfo
.swap_count
;
774 static int64_t driSwapBuffersMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
776 int64_t divisor
, int64_t remainder
)
778 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
780 return dPriv
->driScreenPriv
->DriverAPI
.SwapBuffersMSC( dPriv
, target_msc
,
787 * This is called via __DRIscreenRec's createNewDrawable pointer.
789 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
,
790 const __GLcontextModes
*modes
,
792 __DRIdrawable
*pdraw
,
796 __DRIscreen
* const pDRIScreen
= __glXFindDRIScreen(dpy
, modes
->screen
);
797 __DRIscreenPrivate
*psp
;
798 __DRIdrawablePrivate
*pdp
;
801 pdraw
->private = NULL
;
803 /* Since pbuffers are not yet supported, no drawable attributes are
808 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
812 pdp
= (__DRIdrawablePrivate
*)_mesa_malloc(sizeof(__DRIdrawablePrivate
));
817 if (!XF86DRICreateDrawable(dpy
, modes
->screen
, draw
, &pdp
->hHWDrawable
)) {
832 pdp
->numClipRects
= 0;
833 pdp
->numBackClipRects
= 0;
834 pdp
->pClipRects
= NULL
;
835 pdp
->pBackClipRects
= NULL
;
837 pdp
->screen
= modes
->screen
;
839 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
840 pdp
->driScreenPriv
= psp
;
841 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
843 pdp
->getInfo
= (PFNGLXGETDRAWABLEINFOPROC
)
844 glXGetProcAddress( (const GLubyte
*) "__glXGetDrawableInfo" );
845 if ( pdp
->getInfo
== NULL
) {
846 #ifdef DRI_NEW_INTERFACE_ONLY
847 (void)XF86DRIDestroyDrawable(dpy
, modes
->screen
, pdp
->draw
);
851 pdp
->getInfo
= XF86DRIGetDrawableInfo
;
852 #endif /* DRI_NEW_INTERFACE_ONLY */
855 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
856 renderType
== GLX_PIXMAP_BIT
)) {
857 (void)XF86DRIDestroyDrawable(dpy
, modes
->screen
, pdp
->draw
);
862 pdraw
->private = pdp
;
863 pdraw
->destroyDrawable
= driDestroyDrawable
;
864 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
866 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
867 pdraw
->getSBC
= driGetSBC
;
868 pdraw
->waitForSBC
= driWaitForSBC
;
869 pdraw
->waitForMSC
= driWaitForMSC
;
870 pdraw
->swapBuffersMSC
= driSwapBuffersMSC
;
871 pdraw
->frameTracking
= NULL
;
872 pdraw
->queryFrameTracking
= driQueryFrameTracking
;
874 /* This special default value is replaced with the configured
875 * default value when the drawable is first bound to a direct
876 * rendering context. */
877 pdraw
->swap_interval
= (unsigned)-1;
880 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
882 /* Add pdraw to drawable list */
883 if (!__driAddDrawable(psp
->drawHash
, pdraw
)) {
885 (*pdraw
->destroyDrawable
)(dpy
, pdp
);
888 pdraw
->private = NULL
;
894 static __DRIdrawable
*driGetDrawable(__DRInativeDisplay
*dpy
, __DRIid draw
,
897 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
900 ** Make sure this routine returns NULL if the drawable is not bound
901 ** to a direct rendering context!
903 return __driFindDrawable(psp
->drawHash
, draw
);
906 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
)
908 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*) drawablePrivate
;
909 __DRIscreenPrivate
*psp
= pdp
->driScreenPriv
;
910 int scrn
= psp
->myNum
;
913 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
914 if ((*window_exists
)(dpy
, pdp
->draw
))
915 (void)XF86DRIDestroyDrawable(dpy
, scrn
, pdp
->draw
);
916 if (pdp
->pClipRects
) {
917 _mesa_free(pdp
->pClipRects
);
918 pdp
->pClipRects
= NULL
;
920 if (pdp
->pBackClipRects
) {
921 _mesa_free(pdp
->pBackClipRects
);
922 pdp
->pBackClipRects
= NULL
;
931 /*****************************************************************/
932 /** \name Context handling functions */
933 /*****************************************************************/
937 * Destroy the per-context private information.
939 * \param dpy the display handle.
940 * \param scrn the screen number.
941 * \param contextPrivate opaque pointer to the per-drawable private info.
944 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
945 * drmDestroyContext(), and finally frees \p contextPrivate.
947 static void driDestroyContext(__DRInativeDisplay
*dpy
, int scrn
, void *contextPrivate
)
949 __DRIcontextPrivate
*pcp
= (__DRIcontextPrivate
*) contextPrivate
;
952 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
953 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
954 (void)XF86DRIDestroyContext(dpy
, scrn
, pcp
->contextID
);
961 * Create the per-drawable private driver information.
963 * \param dpy The display handle.
964 * \param modes Mode used to create the new context.
965 * \param render_type Type of rendering target. \c GLX_RGBA is the only
966 * type likely to ever be supported for direct-rendering.
967 * \param sharedPrivate The shared context dependent methods or \c NULL if
969 * \param pctx DRI context to receive the context dependent methods.
971 * \returns An opaque pointer to the per-context private information on
972 * success, or \c NULL on failure.
975 * This function allocates and fills a __DRIcontextPrivateRec structure. It
976 * performs some device independent initialization and passes all the
977 * relevent information to __DriverAPIRec::CreateContext to create the
982 driCreateNewContext(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
983 int render_type
, void *sharedPrivate
, __DRIcontext
*pctx
)
985 __DRIscreen
*pDRIScreen
;
986 __DRIcontextPrivate
*pcp
;
987 __DRIcontextPrivate
*pshare
= (__DRIcontextPrivate
*) sharedPrivate
;
988 __DRIscreenPrivate
*psp
;
989 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
991 pDRIScreen
= __glXFindDRIScreen(dpy
, modes
->screen
);
992 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
997 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
999 pcp
= (__DRIcontextPrivate
*)_mesa_malloc(sizeof(__DRIcontextPrivate
));
1004 if (! (*create_context_with_config
)(dpy
, modes
->screen
, modes
->fbconfigID
,
1005 &pcp
->contextID
, &pcp
->hHWContext
)) {
1011 pcp
->driScreenPriv
= psp
;
1012 pcp
->driDrawablePriv
= NULL
;
1014 /* When the first context is created for a screen, initialize a "dummy"
1018 if (!psp
->dummyContextPriv
.driScreenPriv
) {
1019 psp
->dummyContextPriv
.contextID
= 0;
1020 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
1021 psp
->dummyContextPriv
.driScreenPriv
= psp
;
1022 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
1023 psp
->dummyContextPriv
.driverPrivate
= NULL
;
1024 /* No other fields should be used! */
1027 pctx
->destroyContext
= driDestroyContext
;
1028 #ifdef DRI_NEW_INTERFACE_ONLY
1029 pctx
->bindContext
= NULL
;
1030 pctx
->unbindContext
= NULL
;
1031 pctx
->bindContext2
= NULL
;
1032 pctx
->unbindContext2
= NULL
;
1033 pctx
->bindContext3
= driBindContext3
;
1034 pctx
->unbindContext3
= driUnbindContext3
;
1036 pctx
->bindContext
= driBindContext
;
1037 pctx
->unbindContext
= driUnbindContext
;
1038 if ( driCompareGLXAPIVersion( 20030606 ) >= 0 ) {
1039 pctx
->bindContext2
= driBindContext2
;
1040 pctx
->unbindContext2
= driUnbindContext2
;
1043 if ( driCompareGLXAPIVersion( 20040415 ) >= 0 ) {
1044 pctx
->bindContext3
= driBindContext3
;
1045 pctx
->unbindContext3
= driUnbindContext3
;
1049 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
1050 (void)XF86DRIDestroyContext(dpy
, modes
->screen
, pcp
->contextID
);
1055 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
1061 #ifndef DRI_NEW_INTERFACE_ONLY
1063 * Create the per-drawable private driver information.
1065 * \param dpy the display handle.
1066 * \param vis the visual information.
1067 * \param sharedPrivate the shared context dependent methods or \c NULL if
1069 * \param pctx will receive the context dependent methods.
1071 * \returns a opaque pointer to the per-context private information on success, or \c NULL
1075 * This function has been replaced by \c driCreateNewContext. In drivers
1076 * built to work with XFree86, this function will continue to exist to support
1077 * older versions of libGL. Starting with the next major relelase of XFree86,
1078 * this function will be removed.
1081 * This function allocates and fills a __DRIcontextPrivateRec structure. It
1082 * gets the visual, converts it into a __GLcontextModesRec and passes it
1083 * to __DriverAPIRec::CreateContext to create the context.
1085 static void *driCreateContext(Display
*dpy
, XVisualInfo
*vis
,
1086 void *sharedPrivate
, __DRIcontext
*pctx
)
1088 __DRIscreen
*pDRIScreen
;
1089 const __GLcontextModes
*modes
;
1091 pDRIScreen
= __glXFindDRIScreen(dpy
, vis
->screen
);
1092 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
1098 /* Setup a __GLcontextModes struct corresponding to vis->visualid
1099 * and create the rendering context.
1102 modes
= findConfigMode(dpy
, vis
->screen
, vis
->visualid
, pDRIScreen
);
1103 return (modes
== NULL
)
1105 : driCreateNewContext( dpy
, modes
, GLX_RGBA_TYPE
,
1106 sharedPrivate
, pctx
);
1108 #endif /* DRI_NEW_INTERFACE_ONLY */
1113 /*****************************************************************/
1114 /** \name Screen handling functions */
1115 /*****************************************************************/
1119 * Destroy the per-screen private information.
1121 * \param dpy the display handle.
1122 * \param scrn the screen number.
1123 * \param screenPrivate opaque pointer to the per-screen private information.
1126 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
1127 * drmClose(), and finally frees \p screenPrivate.
1129 static void driDestroyScreen(__DRInativeDisplay
*dpy
, int scrn
, void *screenPrivate
)
1131 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
1134 /* No interaction with the X-server is possible at this point. This
1135 * routine is called after XCloseDisplay, so there is no protocol
1136 * stream open to the X-server anymore.
1139 if (psp
->DriverAPI
.DestroyScreen
)
1140 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
1142 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
1143 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
1144 _mesa_free(psp
->pDevPriv
);
1145 (void)drmClose(psp
->fd
);
1146 if ( psp
->modes
!= NULL
) {
1147 _gl_context_modes_destroy( psp
->modes
);
1155 * Utility function used to create a new driver-private screen structure.
1157 * \param dpy Display pointer
1158 * \param scrn Index of the screen
1159 * \param psc DRI screen data (not driver private)
1160 * \param modes Linked list of known display modes. This list is, at a
1161 * minimum, a list of modes based on the current display mode.
1162 * These roughly match the set of available X11 visuals, but it
1163 * need not be limited to X11! The calling libGL should create
1164 * a list that will inform the driver of the current display
1165 * mode (i.e., color buffer depth, depth buffer depth, etc.).
1166 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
1168 * \param dri_version Version of the "server-side" DRI.
1169 * \param drm_version Version of the kernel DRM.
1170 * \param frame_buffer Data describing the location and layout of the
1172 * \param pSAREA Pointer the the SAREA.
1173 * \param fd Device handle for the DRM.
1174 * \param internal_api_version Version of the internal interface between the
1176 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1178 __DRIscreenPrivate
*
1179 __driUtilCreateNewScreen(__DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
1180 __GLcontextModes
* modes
,
1181 const __DRIversion
* ddx_version
,
1182 const __DRIversion
* dri_version
,
1183 const __DRIversion
* drm_version
,
1184 const __DRIframebuffer
* frame_buffer
,
1185 drm_sarea_t
*pSAREA
,
1187 int internal_api_version
,
1188 const struct __DriverAPIRec
*driverAPI
)
1190 __DRIscreenPrivate
*psp
;
1193 #ifdef DRI_NEW_INTERFACE_ONLY
1194 if ( internal_api_version
< 20040602 ) {
1195 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1196 "20040602 or later is required.\n", internal_api_version
);
1200 if ( internal_api_version
== 20031201 ) {
1201 fprintf( stderr
, "libGL error: libGL version 20031201 has critical "
1202 "binary compatilibity bugs.\nlibGL error: You must upgrade "
1203 "to use direct-rendering!\n" );
1206 #endif /* DRI_NEW_INTERFACE_ONLY */
1209 window_exists
= (PFNGLXWINDOWEXISTSPROC
)
1210 glXGetProcAddress( (const GLubyte
*) "__glXWindowExists" );
1212 if ( window_exists
== NULL
) {
1213 #ifdef DRI_NEW_INTERFACE_ONLY
1214 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1215 "20021128 or later is required.\n", internal_api_version
);
1218 window_exists
= (PFNGLXWINDOWEXISTSPROC
) __driWindowExists
;
1219 #endif /* DRI_NEW_INTERFACE_ONLY */
1222 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1223 glXGetProcAddress( (const GLubyte
*) "__glXCreateContextWithConfig" );
1224 if ( create_context_with_config
== NULL
) {
1225 #ifdef DRI_NEW_INTERFACE_ONLY
1226 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1227 "20031201 or later is required.\n", internal_api_version
);
1230 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1231 fake_XF86DRICreateContextWithConfig
;
1232 #endif /* DRI_NEW_INTERFACE_ONLY */
1235 api_ver
= internal_api_version
;
1237 psp
= (__DRIscreenPrivate
*)_mesa_malloc(sizeof(__DRIscreenPrivate
));
1242 /* Create the hash table */
1243 psp
->drawHash
= drmHashCreate();
1244 if ( psp
->drawHash
== NULL
) {
1255 ** NOT_DONE: This is used by the X server to detect when the client
1256 ** has died while holding the drawable lock. The client sets the
1257 ** drawable lock to this value.
1259 psp
->drawLockID
= 1;
1261 psp
->drmMajor
= drm_version
->major
;
1262 psp
->drmMinor
= drm_version
->minor
;
1263 psp
->drmPatch
= drm_version
->patch
;
1264 psp
->ddxMajor
= ddx_version
->major
;
1265 psp
->ddxMinor
= ddx_version
->minor
;
1266 psp
->ddxPatch
= ddx_version
->patch
;
1267 psp
->driMajor
= dri_version
->major
;
1268 psp
->driMinor
= dri_version
->minor
;
1269 psp
->driPatch
= dri_version
->patch
;
1271 /* install driver's callback functions */
1272 memcpy( &psp
->DriverAPI
, driverAPI
, sizeof(struct __DriverAPIRec
) );
1274 psp
->pSAREA
= pSAREA
;
1276 psp
->pFB
= frame_buffer
->base
;
1277 psp
->fbSize
= frame_buffer
->size
;
1278 psp
->fbStride
= frame_buffer
->stride
;
1279 psp
->fbWidth
= frame_buffer
->width
;
1280 psp
->fbHeight
= frame_buffer
->height
;
1281 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
1282 psp
->pDevPriv
= frame_buffer
->dev_priv
;
1287 ** Do not init dummy context here; actual initialization will be
1288 ** done when the first DRI context is created. Init screen priv ptr
1289 ** to NULL to let CreateContext routine that it needs to be inited.
1291 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
1293 psc
->destroyScreen
= driDestroyScreen
;
1294 #ifndef DRI_NEW_INTERFACE_ONLY
1295 psc
->createContext
= driCreateContext
;
1297 psc
->createContext
= NULL
;
1299 psc
->createNewDrawable
= driCreateNewDrawable
;
1300 psc
->getDrawable
= driGetDrawable
;
1301 #ifdef DRI_NEW_INTERFACE_ONLY
1302 psc
->getMSC
= driGetMSC
;
1303 psc
->createNewContext
= driCreateNewContext
;
1305 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
1306 psc
->getMSC
= driGetMSC
;
1308 if ( driCompareGLXAPIVersion( 20030824 ) >= 0 ) {
1309 psc
->createNewContext
= driCreateNewContext
;
1314 if ( (psp
->DriverAPI
.InitDriver
!= NULL
)
1315 && !(*psp
->DriverAPI
.InitDriver
)(psp
) ) {
1325 #ifndef DRI_NEW_INTERFACE_ONLY
1327 * Utility function used to create a new driver-private screen structure.
1329 * \param dpy Display pointer.
1330 * \param scrn Index of the screen.
1331 * \param psc DRI screen data (not driver private)
1332 * \param numConfigs Number of visual configs pointed to by \c configs.
1333 * \param configs Array of GLXvisualConfigs exported by the 2D driver.
1334 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1337 * This function has been replaced by \c __driUtilCreateNewScreen. In drivers
1338 * built to work with XFree86, this function will continue to exist to support
1339 * older versions of libGL. Starting with the next major relelase of XFree86,
1340 * this function will be removed.
1342 __DRIscreenPrivate
*
1343 __driUtilCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
1344 int numConfigs
, __GLXvisualConfig
*configs
,
1345 const struct __DriverAPIRec
*driverAPI
)
1348 __DRIscreenPrivate
*psp
= NULL
;
1349 drm_handle_t hSAREA
;
1352 __GLcontextModes
*modes
;
1353 __GLcontextModes
*temp
;
1355 __DRIversion ddx_version
;
1356 __DRIversion dri_version
;
1357 __DRIversion drm_version
;
1358 __DRIframebuffer framebuffer
;
1361 const char * err_msg
;
1362 const char * err_extra
;
1365 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrn
, &directCapable
)
1366 || !directCapable
) {
1371 /* Create the linked list of context modes, and populate it with the
1372 * GLX visual information passed in by libGL.
1375 modes
= _gl_context_modes_create( numConfigs
, sizeof(__GLcontextModes
) );
1376 if ( modes
== NULL
) {
1381 for ( i
= 0 ; i
< numConfigs
; i
++ ) {
1382 assert( temp
!= NULL
);
1383 _gl_copy_visual_to_context_mode( temp
, & configs
[i
] );
1384 temp
->screen
= scrn
;
1389 err_msg
= "XF86DRIOpenConnection";
1392 if (XF86DRIOpenConnection(dpy
, scrn
, &hSAREA
, &BusID
)) {
1393 fd
= drmOpen(NULL
,BusID
);
1394 _mesa_free(BusID
); /* No longer needed */
1396 err_msg
= "open DRM";
1397 err_extra
= strerror( -fd
);
1402 err_msg
= "drmGetMagic";
1405 if (!drmGetMagic(fd
, &magic
)) {
1406 drmVersionPtr version
= drmGetVersion(fd
);
1408 drm_version
.major
= version
->version_major
;
1409 drm_version
.minor
= version
->version_minor
;
1410 drm_version
.patch
= version
->version_patchlevel
;
1411 drmFreeVersion(version
);
1414 drm_version
.major
= -1;
1415 drm_version
.minor
= -1;
1416 drm_version
.patch
= -1;
1419 err_msg
= "XF86DRIAuthConnection";
1420 if (XF86DRIAuthConnection(dpy
, scrn
, magic
)) {
1424 * Get device name (like "tdfx") and the ddx version numbers.
1425 * We'll check the version in each DRI driver's "createScreen"
1428 err_msg
= "XF86DRIGetClientDriverName";
1429 if (XF86DRIGetClientDriverName(dpy
, scrn
,
1435 /* No longer needed. */
1436 _mesa_free( driverName
);
1439 * Get the DRI X extension version.
1441 err_msg
= "XF86DRIQueryVersion";
1442 if (XF86DRIQueryVersion(dpy
,
1445 &dri_version
.patch
)) {
1450 * Get device-specific info. pDevPriv will point to a struct
1451 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
1452 * that has information about the screen size, depth, pitch,
1453 * ancilliary buffers, DRM mmap handles, etc.
1455 err_msg
= "XF86DRIGetDeviceInfo";
1456 if (XF86DRIGetDeviceInfo(dpy
, scrn
,
1460 &framebuffer
.stride
,
1461 &framebuffer
.dev_priv_size
,
1462 &framebuffer
.dev_priv
)) {
1463 framebuffer
.width
= DisplayWidth(dpy
, scrn
);
1464 framebuffer
.height
= DisplayHeight(dpy
, scrn
);
1467 * Map the framebuffer region.
1469 status
= drmMap(fd
, hFB
, framebuffer
.size
,
1470 (drmAddressPtr
)&framebuffer
.base
);
1472 err_msg
= "drmMap of framebuffer";
1473 err_extra
= strerror( -status
);
1475 if ( status
== 0 ) {
1477 * Map the SAREA region. Further mmap regions may be setup in
1478 * each DRI driver's "createScreen" function.
1480 status
= drmMap(fd
, hSAREA
, SAREA_MAX
,
1483 err_msg
= "drmMap of sarea";
1484 err_extra
= strerror( -status
);
1486 if ( status
== 0 ) {
1487 PFNGLXGETINTERNALVERSIONPROC get_ver
;
1489 get_ver
= (PFNGLXGETINTERNALVERSIONPROC
)
1490 glXGetProcAddress( (const GLubyte
*) "__glXGetInternalVersion" );
1492 err_msg
= "InitDriver";
1494 psp
= __driUtilCreateNewScreen( dpy
, scrn
, psc
, modes
,
1501 (get_ver
!= NULL
) ? (*get_ver
)() : 1,
1513 if ( psp
== NULL
) {
1514 if ( pSAREA
!= MAP_FAILED
) {
1515 (void)drmUnmap(pSAREA
, SAREA_MAX
);
1518 if ( framebuffer
.base
!= MAP_FAILED
) {
1519 (void)drmUnmap((drmAddress
)framebuffer
.base
, framebuffer
.size
);
1522 if ( framebuffer
.dev_priv
!= NULL
) {
1523 _mesa_free(framebuffer
.dev_priv
);
1530 if ( modes
!= NULL
) {
1531 _gl_context_modes_destroy( modes
);
1534 (void)XF86DRICloseConnection(dpy
, scrn
);
1536 if ( err_extra
!= NULL
) {
1537 fprintf(stderr
, "libGL error: %s failed (%s)\n", err_msg
,
1541 fprintf(stderr
, "libGL error: %s failed\n", err_msg
);
1544 fprintf(stderr
, "libGL error: reverting to (slow) indirect rendering\n");
1549 #endif /* DRI_NEW_INTERFACE_ONLY */
1553 * Compare the current GLX API version with a driver supplied required version.
1555 * The minimum required version is compared with the API version exported by
1556 * the \c __glXGetInternalVersion function (in libGL.so).
1558 * \param required_version Minimum required internal GLX API version.
1559 * \return A tri-value return, as from strcmp is returned. A value less
1560 * than, equal to, or greater than zero will be returned if the
1561 * internal GLX API version is less than, equal to, or greater
1562 * than \c required_version.
1564 * \sa __glXGetInternalVersion().
1566 int driCompareGLXAPIVersion( GLuint required_version
)
1568 if ( api_ver
> required_version
) {
1571 else if ( api_ver
== required_version
) {
1580 driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
1581 int64_t * sbc
, int64_t * missedFrames
,
1582 float * lastMissedUsage
, float * usage
)
1584 static PFNGLXGETUSTPROC get_ust
;
1585 __DRIswapInfo sInfo
;
1588 __DRIdrawablePrivate
* dpriv
= (__DRIdrawablePrivate
*) priv
;
1590 if ( get_ust
== NULL
) {
1591 get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
1594 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1595 if ( status
== 0 ) {
1596 *sbc
= sInfo
.swap_count
;
1597 *missedFrames
= sInfo
.swap_missed_count
;
1598 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1600 (*get_ust
)( & ust
);
1601 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1609 * Calculate amount of swap interval used between GLX buffer swaps.
1611 * The usage value, on the range [0,max], is the fraction of total swap
1612 * interval time used between GLX buffer swaps is calculated.
1614 * \f$p = t_d / (i * t_r)\f$
1616 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1617 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1618 * required for a single vertical refresh period (as returned by \c
1619 * glXGetMscRateOML).
1621 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1624 * \param dPriv Pointer to the private drawable structure.
1625 * \return If less than a single swap interval time period was required
1626 * between GLX buffer swaps, a number greater than 0 and less than
1627 * 1.0 is returned. If exactly one swap interval time period is
1628 * required, 1.0 is returned, and if more than one is required then
1629 * a number greater than 1.0 will be returned.
1631 * \sa glXSwapIntervalSGI glXGetMscRateOML
1633 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1634 * be possible to cache the sync rate?
1637 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1638 int64_t current_ust
)
1640 static PFNGLXGETMSCRATEOMLPROC get_msc_rate
= NULL
;
1647 if ( get_msc_rate
== NULL
) {
1648 get_msc_rate
= (PFNGLXGETMSCRATEOMLPROC
)
1649 glXGetProcAddress( (const GLubyte
*) "glXGetMscRateOML" );
1652 if ( (get_msc_rate
!= NULL
)
1653 && get_msc_rate( dPriv
->display
, dPriv
->draw
, &n
, &d
) ) {
1654 interval
= (dPriv
->pdraw
->swap_interval
!= 0)
1655 ? dPriv
->pdraw
->swap_interval
: 1;
1658 /* We want to calculate
1659 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1660 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1661 * dPriv->swap_ust. interval has already been calculated.
1663 * The only tricky part is us_per_refresh. us_per_refresh is
1664 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1665 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1666 * the denominator of the final calculation, we calculate
1667 * (interval * 1000000 * d) and move n into the numerator.
1670 usage
= (current_ust
- last_swap_ust
);
1672 usage
/= (interval
* d
);
1681 #endif /* GLX_DIRECT_RENDERING */