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.
35 #define MAP_FAILED ((void *)-1)
38 #ifndef DRI_NEW_INTERFACE_ONLY
39 # include <X11/Xlibint.h>
43 # define _mesa_malloc(b) Xmalloc(b)
44 # define _mesa_free(m) Xfree(m)
48 #endif /* DRI_NEW_INTERFACE_ONLY */
51 #include "drm_sarea.h"
52 #include "glcontextmodes.h"
54 #ifndef PFNGLXGETMSCRATEOMLPROC
55 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRInativeDisplay
*dpy
, __DRIid drawable
, int32_t *numerator
, int32_t *denominator
);
59 * Weak thread-safety dispatch pointer. Older versions of libGL will not have
60 * this symbol, so a "weak" version is included here so that the driver will
61 * dynamically link properly. The value is set to \c NULL. This forces the
62 * driver to fall back to the old dispatch interface.
64 struct _glapi_table
*_glapi_DispatchTSD
__attribute__((weak
)) = NULL
;
67 * This is used in a couple of places that call \c driCreateNewDrawable.
69 static const int empty_attribute_list
[1] = { None
};
72 * Function used to determine if a drawable (window) still exists. Ideally
73 * this function comes from libGL. With older versions of libGL from XFree86
74 * we can fall-back to an internal version.
76 * \sa __driWindowExists __glXWindowExists
78 static PFNGLXWINDOWEXISTSPROC window_exists
;
80 typedef GLboolean (*PFNGLXCREATECONTEXTWITHCONFIGPROC
)( __DRInativeDisplay
*, int, int, void *,
83 static PFNGLXCREATECONTEXTWITHCONFIGPROC create_context_with_config
;
86 * Cached copy of the internal API version used by libGL and the client-side
89 static int api_ver
= 0;
91 /* forward declarations */
92 static int driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
93 int64_t * sbc
, int64_t * missedFrames
, float * lastMissedUsage
,
96 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
97 __DRIid draw
, __DRIdrawable
*pdraw
, int renderType
, const int *attrs
);
99 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
);
105 static GLboolean
driFeatureOn(const char *name
)
107 char *env
= getenv(name
);
109 if (!env
) return GL_FALSE
;
110 if (!strcasecmp(env
, "enable")) return GL_TRUE
;
111 if (!strcasecmp(env
, "1")) return GL_TRUE
;
112 if (!strcasecmp(env
, "on")) return GL_TRUE
;
113 if (!strcasecmp(env
, "true")) return GL_TRUE
;
114 if (!strcasecmp(env
, "t")) return GL_TRUE
;
115 if (!strcasecmp(env
, "yes")) return GL_TRUE
;
116 if (!strcasecmp(env
, "y")) return GL_TRUE
;
120 #endif /* not_defined */
124 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
127 * Is called from the drivers.
129 * \param f \c printf like format string.
132 __driUtilMessage(const char *f
, ...)
136 if (getenv("LIBGL_DEBUG")) {
137 fprintf(stderr
, "libGL error: \n");
139 vfprintf(stderr
, f
, args
);
141 fprintf(stderr
, "\n");
146 * fd.o bug #1713: Some rare libGL's have __glXFindDRIScreen defined but do not
147 * export it via glXGetProcAddress. These are not supported anymore, so print
148 * an error message to that effect. - ajax 2004-10-26
150 typedef __DRIscreen
*(*PFNGLXFINDDRISCREEN
)(__DRInativeDisplay
*, int);
152 static __DRIscreen
*glx_find_dri_screen(__DRInativeDisplay
*d
, int i
)
154 PFNGLXFINDDRISCREEN findscreen
=
155 (PFNGLXFINDDRISCREEN
)glXGetProcAddress("__glXFindDRIScreen");
159 __driUtilMessage("glXGetProcAddress(\"__glXFindDRIScreen\") failed!");
160 __driUtilMessage("Your libGL is too old, please upgrade.");
163 else return findscreen(d
, i
);
166 /*****************************************************************/
167 /** \name Visual utility functions */
168 /*****************************************************************/
171 #ifndef DRI_NEW_INTERFACE_ONLY
173 * Find a \c __GLcontextModes structure matching the given visual ID.
175 * \param dpy Display to search for a matching configuration.
176 * \param scrn Screen number on \c dpy to be searched.
177 * \param vid Desired \c VisualID to find.
179 * \returns A pointer to a \c __GLcontextModes structure that matches \c vid,
180 * if found, or \c NULL if no match is found.
182 static const __GLcontextModes
*
183 findConfigMode(__DRInativeDisplay
*dpy
, int scrn
, VisualID vid
,
184 const __DRIscreen
* pDRIScreen
)
186 if ( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) ) {
187 const __DRIscreenPrivate
* const psp
=
188 (const __DRIscreenPrivate
*) pDRIScreen
->private;
190 return _gl_context_modes_find_visual( psp
->modes
, vid
);
198 * This function is a hack to work-around old versions of libGL.so that
199 * do not export \c XF86DRICreateContextWithConfig. I would modify the
200 * code to just use this function, but the stand-alone driver (i.e., DRI
201 * drivers that are built to work without XFree86) shouldn't have to know
202 * about X structures like a \c Visual.
205 fake_XF86DRICreateContextWithConfig( __DRInativeDisplay
* dpy
, int screen
, int configID
,
206 XID
* context
, drm_context_t
* hHWContext
)
210 vis
.visualid
= configID
;
211 return XF86DRICreateContext( dpy
, screen
, & vis
, context
, hHWContext
);
213 #endif /* DRI_NEW_INTERFACE_ONLY */
218 /*****************************************************************/
219 /** \name Drawable list management */
220 /*****************************************************************/
223 static GLboolean
__driAddDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
225 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
227 if (drmHashInsert(drawHash
, pdp
->draw
, pdraw
))
233 static __DRIdrawable
*__driFindDrawable(void *drawHash
, __DRIid draw
)
236 __DRIdrawable
*pdraw
;
238 retcode
= drmHashLookup(drawHash
, draw
, (void *)&pdraw
);
245 #ifndef DRI_NEW_INTERFACE_ONLY
246 static GLboolean __driWindowExistsFlag
;
248 static int __driWindowExistsErrorHandler(Display
*dpy
, XErrorEvent
*xerr
)
250 if (xerr
->error_code
== BadWindow
) {
251 __driWindowExistsFlag
= GL_FALSE
;
257 * Determine if a window associated with a \c GLXDrawable exists on the
260 * \param dpy Display associated with the drawable to be queried.
261 * \param draw \c GLXDrawable to test.
263 * \returns \c GL_TRUE if a window exists that is associated with \c draw,
264 * otherwise \c GL_FALSE is returned.
266 * \warning This function is not currently thread-safe.
269 * \c __glXWindowExists (from libGL) is prefered over this function. Starting
270 * with the next major release of XFree86, this function will be removed.
271 * Even now this function is no longer directly called. Instead it is called
272 * via a function pointer if and only if \c __glXWindowExists does not exist.
274 * \sa __glXWindowExists glXGetProcAddress window_exists
276 static GLboolean
__driWindowExists(Display
*dpy
, GLXDrawable draw
)
278 XWindowAttributes xwa
;
279 int (*oldXErrorHandler
)(Display
*, XErrorEvent
*);
281 XSync(dpy
, GL_FALSE
);
282 __driWindowExistsFlag
= GL_TRUE
;
283 oldXErrorHandler
= XSetErrorHandler(__driWindowExistsErrorHandler
);
284 XGetWindowAttributes(dpy
, draw
, &xwa
); /* dummy request */
285 XSetErrorHandler(oldXErrorHandler
);
286 return __driWindowExistsFlag
;
288 #endif /* DRI_NEW_INTERFACE_ONLY */
291 * Find drawables in the local hash that have been destroyed on the
294 * \param drawHash Hash-table containing all know drawables.
296 static void __driGarbageCollectDrawables(void *drawHash
)
299 __DRInativeDisplay
*dpy
;
300 __DRIdrawable
*pdraw
;
302 if (drmHashFirst(drawHash
, &draw
, (void *)&pdraw
) == 1) {
304 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
305 dpy
= pdp
->driScreenPriv
->display
;
306 if (! (*window_exists
)(dpy
, draw
)) {
307 /* Destroy the local drawable data in the hash table, if the
308 drawable no longer exists in the Xserver */
309 drmHashDelete(drawHash
, draw
);
310 (*pdraw
->destroyDrawable
)(dpy
, pdraw
->private);
313 } while (drmHashNext(drawHash
, &draw
, (void *)&pdraw
) == 1);
320 /*****************************************************************/
321 /** \name Context (un)binding functions */
322 /*****************************************************************/
328 * \param dpy the display handle.
329 * \param scrn the screen number.
330 * \param draw drawable.
331 * \param read Current reading drawable.
334 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
337 * This function calls __DriverAPIRec::UnbindContext, and then decrements
338 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
341 * While casting the opaque private pointers associated with the parameters
342 * into their respective real types it also assures they are not \c NULL.
344 static GLboolean
driUnbindContext3(__DRInativeDisplay
*dpy
, int scrn
,
345 __DRIid draw
, __DRIid read
,
348 __DRIscreen
*pDRIScreen
;
349 __DRIdrawable
*pdraw
;
350 __DRIdrawable
*pread
;
351 __DRIcontextPrivate
*pcp
;
352 __DRIscreenPrivate
*psp
;
353 __DRIdrawablePrivate
*pdp
;
354 __DRIdrawablePrivate
*prp
;
357 ** Assume error checking is done properly in glXMakeCurrent before
358 ** calling driUnbindContext3.
361 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
366 pDRIScreen
= glx_find_dri_screen(dpy
, scrn
);
367 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
372 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
373 pcp
= (__DRIcontextPrivate
*)ctx
->private;
375 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
380 pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
382 pread
= __driFindDrawable(psp
->drawHash
, read
);
387 prp
= (__DRIdrawablePrivate
*)pread
->private;
390 /* Let driver unbind drawable from context */
391 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
394 if (pdp
->refcount
== 0) {
402 if (prp
->refcount
== 0) {
411 /* XXX this is disabled so that if we call SwapBuffers on an unbound
412 * window we can determine the last context bound to the window and
413 * use that context's lock. (BrianP, 2-Dec-2000)
416 /* Unbind the drawable */
417 pcp
->driDrawablePriv
= NULL
;
418 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
426 * This function takes both a read buffer and a draw buffer. This is needed
427 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
430 * \bug This function calls \c driCreateNewDrawable in two places with the
431 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
432 * be needed in those places when support for pbuffers and / or pixmaps
433 * is added. Is it safe to assume that the drawable is a window?
435 static GLboolean
DoBindContext(__DRInativeDisplay
*dpy
,
436 __DRIid draw
, __DRIid read
,
437 __DRIcontext
*ctx
, const __GLcontextModes
* modes
,
438 __DRIscreenPrivate
*psp
)
440 __DRIdrawable
*pdraw
;
441 __DRIdrawablePrivate
*pdp
;
442 __DRIdrawable
*pread
;
443 __DRIdrawablePrivate
*prp
;
444 __DRIcontextPrivate
* const pcp
= ctx
->private;
447 /* Find the _DRIdrawable which corresponds to the writing drawable. */
448 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
450 /* Allocate a new drawable */
451 pdraw
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
457 /* Create a new drawable */
458 driCreateNewDrawable(dpy
, modes
, draw
, pdraw
, GLX_WINDOW_BIT
,
459 empty_attribute_list
);
460 if (!pdraw
->private) {
467 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
469 /* Find the _DRIdrawable which corresponds to the reading drawable. */
471 /* read buffer == draw buffer */
475 pread
= __driFindDrawable(psp
->drawHash
, read
);
477 /* Allocate a new drawable */
478 pread
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
484 /* Create a new drawable */
485 driCreateNewDrawable(dpy
, modes
, read
, pread
, GLX_WINDOW_BIT
,
486 empty_attribute_list
);
487 if (!pread
->private) {
493 prp
= (__DRIdrawablePrivate
*) pread
->private;
496 /* Bind the drawable to the context */
497 pcp
->driDrawablePriv
= pdp
;
498 pdp
->driContextPriv
= pcp
;
505 ** Now that we have a context associated with this drawable, we can
506 ** initialize the drawable information if has not been done before.
508 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
509 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
510 __driUtilUpdateDrawableInfo(pdp
);
511 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
514 /* Call device-specific MakeCurrent */
515 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
522 * This function takes both a read buffer and a draw buffer. This is needed
523 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
526 static GLboolean
driBindContext3(__DRInativeDisplay
*dpy
, int scrn
,
527 __DRIid draw
, __DRIid read
,
530 __DRIscreen
*pDRIScreen
;
533 ** Assume error checking is done properly in glXMakeCurrent before
534 ** calling driBindContext.
537 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
542 pDRIScreen
= glx_find_dri_screen(dpy
, scrn
);
543 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
548 return DoBindContext( dpy
, draw
, read
, ctx
, ctx
->mode
,
549 (__DRIscreenPrivate
*)pDRIScreen
->private );
553 #ifndef DRI_NEW_INTERFACE_ONLY
555 * This function takes both a read buffer and a draw buffer. This is needed
556 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
559 static GLboolean
driBindContext2(Display
*dpy
, int scrn
,
560 GLXDrawable draw
, GLXDrawable read
,
563 __DRIscreen
*pDRIScreen
;
564 const __GLcontextModes
*modes
;
567 ** Assume error checking is done properly in glXMakeCurrent before
568 ** calling driBindContext.
571 if (gc
== NULL
|| draw
== None
|| read
== None
) {
576 pDRIScreen
= glx_find_dri_screen(dpy
, scrn
);
577 modes
= (driCompareGLXAPIVersion( 20040317 ) >= 0)
578 ? gc
->driContext
.mode
579 : findConfigMode( dpy
, scrn
, gc
->vid
, pDRIScreen
);
581 if ( modes
== NULL
) {
586 /* findConfigMode will return NULL if the DRI screen or screen private
589 assert( (pDRIScreen
!= NULL
) && (pDRIScreen
->private != NULL
) );
591 return DoBindContext( dpy
, draw
, read
, & gc
->driContext
, modes
,
592 (__DRIscreenPrivate
*)pDRIScreen
->private );
595 static GLboolean
driUnbindContext2(Display
*dpy
, int scrn
,
596 GLXDrawable draw
, GLXDrawable read
,
599 return driUnbindContext3(dpy
, scrn
, draw
, read
, & gc
->driContext
);
603 * Simply call bind with the same GLXDrawable for the read and draw buffers.
605 static GLboolean
driBindContext(Display
*dpy
, int scrn
,
606 GLXDrawable draw
, GLXContext gc
)
608 return driBindContext2(dpy
, scrn
, draw
, draw
, gc
);
613 * Simply call bind with the same GLXDrawable for the read and draw buffers.
615 static GLboolean
driUnbindContext(Display
*dpy
, int scrn
,
616 GLXDrawable draw
, GLXContext gc
,
620 return driUnbindContext2( dpy
, scrn
, draw
, draw
, gc
);
622 #endif /* DRI_NEW_INTERFACE_ONLY */
627 /*****************************************************************/
628 /** \name Drawable handling functions */
629 /*****************************************************************/
633 * Update private drawable information.
635 * \param pdp pointer to the private drawable information to update.
637 * This function basically updates the __DRIdrawablePrivate struct's
638 * cliprect information by calling \c __DRIDrawablePrivate::getInfo. This is
639 * usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
640 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
641 * the values are different that means we have to update the clipping
645 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
647 __DRIscreenPrivate
*psp
;
648 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
650 if (!pcp
|| (pdp
!= pcp
->driDrawablePriv
)) {
655 psp
= pdp
->driScreenPriv
;
661 if (pdp
->pClipRects
) {
662 _mesa_free(pdp
->pClipRects
);
665 if (pdp
->pBackClipRects
) {
666 _mesa_free(pdp
->pBackClipRects
);
669 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
671 if (!__driFindDrawable(psp
->drawHash
, pdp
->draw
) ||
672 ! (*pdp
->getInfo
)(pdp
->display
, pdp
->screen
, pdp
->draw
,
673 &pdp
->index
, &pdp
->lastStamp
,
674 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
675 &pdp
->numClipRects
, &pdp
->pClipRects
,
678 &pdp
->numBackClipRects
,
679 &pdp
->pBackClipRects
)) {
680 /* Error -- eg the window may have been destroyed. Keep going
683 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
684 pdp
->numClipRects
= 0;
685 pdp
->pClipRects
= NULL
;
686 pdp
->numBackClipRects
= 0;
687 pdp
->pBackClipRects
= NULL
;
690 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
692 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
698 /*****************************************************************/
699 /** \name GLX callbacks */
700 /*****************************************************************/
706 * \param dpy the display handle.
707 * \param drawablePrivate opaque pointer to the per-drawable private info.
710 * This function calls __DRIdrawablePrivate::swapBuffers.
712 * Is called directly from glXSwapBuffers().
714 static void driSwapBuffers( __DRInativeDisplay
*dpy
, void *drawablePrivate
)
716 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
717 dPriv
->swapBuffers(dPriv
);
722 * Called directly from a number of higher-level GLX functions.
724 static int driGetMSC( void *screenPrivate
, int64_t *msc
)
726 __DRIscreenPrivate
*sPriv
= (__DRIscreenPrivate
*) screenPrivate
;
728 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
732 * Called directly from a number of higher-level GLX functions.
734 static int driGetSBC( __DRInativeDisplay
*dpy
, void *drawablePrivate
, int64_t *sbc
)
736 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
741 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
742 *sbc
= sInfo
.swap_count
;
747 static int driWaitForSBC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
749 int64_t * msc
, int64_t * sbc
)
751 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
753 return dPriv
->driScreenPriv
->DriverAPI
.WaitForSBC( dPriv
, target_sbc
,
757 static int driWaitForMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
759 int64_t divisor
, int64_t remainder
,
760 int64_t * msc
, int64_t * sbc
)
762 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
767 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
771 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
772 * is supported but GLX_OML_sync_control is not. Therefore, don't return
773 * an error value if GetSwapInfo() is not implemented.
776 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
777 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
778 *sbc
= sInfo
.swap_count
;
784 static int64_t driSwapBuffersMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
786 int64_t divisor
, int64_t remainder
)
788 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
790 return dPriv
->driScreenPriv
->DriverAPI
.SwapBuffersMSC( dPriv
, target_msc
,
797 * This is called via __DRIscreenRec's createNewDrawable pointer.
799 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
,
800 const __GLcontextModes
*modes
,
802 __DRIdrawable
*pdraw
,
806 __DRIscreen
* const pDRIScreen
= glx_find_dri_screen(dpy
, modes
->screen
);
807 __DRIscreenPrivate
*psp
;
808 __DRIdrawablePrivate
*pdp
;
811 pdraw
->private = NULL
;
813 /* Since pbuffers are not yet supported, no drawable attributes are
818 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
822 pdp
= (__DRIdrawablePrivate
*)_mesa_malloc(sizeof(__DRIdrawablePrivate
));
827 if (!XF86DRICreateDrawable(dpy
, modes
->screen
, draw
, &pdp
->hHWDrawable
)) {
842 pdp
->numClipRects
= 0;
843 pdp
->numBackClipRects
= 0;
844 pdp
->pClipRects
= NULL
;
845 pdp
->pBackClipRects
= NULL
;
847 pdp
->screen
= modes
->screen
;
849 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
850 pdp
->driScreenPriv
= psp
;
851 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
853 pdp
->getInfo
= (PFNGLXGETDRAWABLEINFOPROC
)
854 glXGetProcAddress( (const GLubyte
*) "__glXGetDrawableInfo" );
855 if ( pdp
->getInfo
== NULL
) {
856 #ifdef DRI_NEW_INTERFACE_ONLY
857 (void)XF86DRIDestroyDrawable(dpy
, modes
->screen
, pdp
->draw
);
861 pdp
->getInfo
= (PFNGLXGETDRAWABLEINFOPROC
) XF86DRIGetDrawableInfo
;
862 #endif /* DRI_NEW_INTERFACE_ONLY */
865 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
866 renderType
== GLX_PIXMAP_BIT
)) {
867 (void)XF86DRIDestroyDrawable(dpy
, modes
->screen
, pdp
->draw
);
872 pdraw
->private = pdp
;
873 pdraw
->destroyDrawable
= driDestroyDrawable
;
874 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
876 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
877 pdraw
->getSBC
= driGetSBC
;
878 pdraw
->waitForSBC
= driWaitForSBC
;
879 pdraw
->waitForMSC
= driWaitForMSC
;
880 pdraw
->swapBuffersMSC
= driSwapBuffersMSC
;
881 pdraw
->frameTracking
= NULL
;
882 pdraw
->queryFrameTracking
= driQueryFrameTracking
;
884 /* This special default value is replaced with the configured
885 * default value when the drawable is first bound to a direct
886 * rendering context. */
887 pdraw
->swap_interval
= (unsigned)-1;
890 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
892 /* Add pdraw to drawable list */
893 if (!__driAddDrawable(psp
->drawHash
, pdraw
)) {
895 (*pdraw
->destroyDrawable
)(dpy
, pdp
);
898 pdraw
->private = NULL
;
904 static __DRIdrawable
*driGetDrawable(__DRInativeDisplay
*dpy
, __DRIid draw
,
907 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
910 ** Make sure this routine returns NULL if the drawable is not bound
911 ** to a direct rendering context!
913 return __driFindDrawable(psp
->drawHash
, draw
);
916 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
)
918 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*) drawablePrivate
;
919 __DRIscreenPrivate
*psp
= pdp
->driScreenPriv
;
920 int scrn
= psp
->myNum
;
923 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
924 if ((*window_exists
)(dpy
, pdp
->draw
))
925 (void)XF86DRIDestroyDrawable(dpy
, scrn
, pdp
->draw
);
926 if (pdp
->pClipRects
) {
927 _mesa_free(pdp
->pClipRects
);
928 pdp
->pClipRects
= NULL
;
930 if (pdp
->pBackClipRects
) {
931 _mesa_free(pdp
->pBackClipRects
);
932 pdp
->pBackClipRects
= NULL
;
941 /*****************************************************************/
942 /** \name Context handling functions */
943 /*****************************************************************/
947 * Destroy the per-context private information.
949 * \param dpy the display handle.
950 * \param scrn the screen number.
951 * \param contextPrivate opaque pointer to the per-drawable private info.
954 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
955 * drmDestroyContext(), and finally frees \p contextPrivate.
957 static void driDestroyContext(__DRInativeDisplay
*dpy
, int scrn
, void *contextPrivate
)
959 __DRIcontextPrivate
*pcp
= (__DRIcontextPrivate
*) contextPrivate
;
962 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
963 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
964 (void)XF86DRIDestroyContext(dpy
, scrn
, pcp
->contextID
);
971 * Create the per-drawable private driver information.
973 * \param dpy The display handle.
974 * \param modes Mode used to create the new context.
975 * \param render_type Type of rendering target. \c GLX_RGBA is the only
976 * type likely to ever be supported for direct-rendering.
977 * \param sharedPrivate The shared context dependent methods or \c NULL if
979 * \param pctx DRI context to receive the context dependent methods.
981 * \returns An opaque pointer to the per-context private information on
982 * success, or \c NULL on failure.
985 * This function allocates and fills a __DRIcontextPrivateRec structure. It
986 * performs some device independent initialization and passes all the
987 * relevent information to __DriverAPIRec::CreateContext to create the
992 driCreateNewContext(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
993 int render_type
, void *sharedPrivate
, __DRIcontext
*pctx
)
995 __DRIscreen
*pDRIScreen
;
996 __DRIcontextPrivate
*pcp
;
997 __DRIcontextPrivate
*pshare
= (__DRIcontextPrivate
*) sharedPrivate
;
998 __DRIscreenPrivate
*psp
;
999 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
1001 pDRIScreen
= glx_find_dri_screen(dpy
, modes
->screen
);
1002 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
1007 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
1009 pcp
= (__DRIcontextPrivate
*)_mesa_malloc(sizeof(__DRIcontextPrivate
));
1014 if (! (*create_context_with_config
)(dpy
, modes
->screen
, modes
->fbconfigID
,
1015 &pcp
->contextID
, &pcp
->hHWContext
)) {
1021 pcp
->driScreenPriv
= psp
;
1022 pcp
->driDrawablePriv
= NULL
;
1024 /* When the first context is created for a screen, initialize a "dummy"
1028 if (!psp
->dummyContextPriv
.driScreenPriv
) {
1029 psp
->dummyContextPriv
.contextID
= 0;
1030 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
1031 psp
->dummyContextPriv
.driScreenPriv
= psp
;
1032 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
1033 psp
->dummyContextPriv
.driverPrivate
= NULL
;
1034 /* No other fields should be used! */
1037 pctx
->destroyContext
= driDestroyContext
;
1038 #ifdef DRI_NEW_INTERFACE_ONLY
1039 pctx
->bindContext
= NULL
;
1040 pctx
->unbindContext
= NULL
;
1041 pctx
->bindContext2
= NULL
;
1042 pctx
->unbindContext2
= NULL
;
1043 pctx
->bindContext3
= driBindContext3
;
1044 pctx
->unbindContext3
= driUnbindContext3
;
1046 pctx
->bindContext
= (void *)driBindContext
;
1047 pctx
->unbindContext
= (void *)driUnbindContext
;
1048 if ( driCompareGLXAPIVersion( 20030606 ) >= 0 ) {
1049 pctx
->bindContext2
= (void *)driBindContext2
;
1050 pctx
->unbindContext2
= (void *)driUnbindContext2
;
1053 if ( driCompareGLXAPIVersion( 20040415 ) >= 0 ) {
1054 pctx
->bindContext3
= (void *)driBindContext3
;
1055 pctx
->unbindContext3
= (void *)driUnbindContext3
;
1059 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
1060 (void)XF86DRIDestroyContext(dpy
, modes
->screen
, pcp
->contextID
);
1065 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
1071 #ifndef DRI_NEW_INTERFACE_ONLY
1073 * Create the per-drawable private driver information.
1075 * \param dpy the display handle.
1076 * \param vis the visual information.
1077 * \param sharedPrivate the shared context dependent methods or \c NULL if
1079 * \param pctx will receive the context dependent methods.
1081 * \returns a opaque pointer to the per-context private information on success, or \c NULL
1085 * This function has been replaced by \c driCreateNewContext. In drivers
1086 * built to work with XFree86, this function will continue to exist to support
1087 * older versions of libGL. Starting with the next major relelase of XFree86,
1088 * this function will be removed.
1091 * This function allocates and fills a __DRIcontextPrivateRec structure. It
1092 * gets the visual, converts it into a __GLcontextModesRec and passes it
1093 * to __DriverAPIRec::CreateContext to create the context.
1095 static void *driCreateContext(Display
*dpy
, XVisualInfo
*vis
,
1096 void *sharedPrivate
, __DRIcontext
*pctx
)
1098 __DRIscreen
*pDRIScreen
;
1099 const __GLcontextModes
*modes
;
1101 pDRIScreen
= glx_find_dri_screen(dpy
, vis
->screen
);
1102 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
1108 /* Setup a __GLcontextModes struct corresponding to vis->visualid
1109 * and create the rendering context.
1112 modes
= findConfigMode(dpy
, vis
->screen
, vis
->visualid
, pDRIScreen
);
1113 return (modes
== NULL
)
1115 : driCreateNewContext( dpy
, modes
, GLX_RGBA_TYPE
,
1116 sharedPrivate
, pctx
);
1118 #endif /* DRI_NEW_INTERFACE_ONLY */
1123 /*****************************************************************/
1124 /** \name Screen handling functions */
1125 /*****************************************************************/
1129 * Destroy the per-screen private information.
1131 * \param dpy the display handle.
1132 * \param scrn the screen number.
1133 * \param screenPrivate opaque pointer to the per-screen private information.
1136 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
1137 * drmClose(), and finally frees \p screenPrivate.
1139 static void driDestroyScreen(__DRInativeDisplay
*dpy
, int scrn
, void *screenPrivate
)
1141 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
1144 /* No interaction with the X-server is possible at this point. This
1145 * routine is called after XCloseDisplay, so there is no protocol
1146 * stream open to the X-server anymore.
1149 if (psp
->DriverAPI
.DestroyScreen
)
1150 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
1152 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
1153 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
1154 _mesa_free(psp
->pDevPriv
);
1155 (void)drmClose(psp
->fd
);
1156 if ( psp
->modes
!= NULL
) {
1157 _gl_context_modes_destroy( psp
->modes
);
1165 * Utility function used to create a new driver-private screen structure.
1167 * \param dpy Display pointer
1168 * \param scrn Index of the screen
1169 * \param psc DRI screen data (not driver private)
1170 * \param modes Linked list of known display modes. This list is, at a
1171 * minimum, a list of modes based on the current display mode.
1172 * These roughly match the set of available X11 visuals, but it
1173 * need not be limited to X11! The calling libGL should create
1174 * a list that will inform the driver of the current display
1175 * mode (i.e., color buffer depth, depth buffer depth, etc.).
1176 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
1178 * \param dri_version Version of the "server-side" DRI.
1179 * \param drm_version Version of the kernel DRM.
1180 * \param frame_buffer Data describing the location and layout of the
1182 * \param pSAREA Pointer the the SAREA.
1183 * \param fd Device handle for the DRM.
1184 * \param internal_api_version Version of the internal interface between the
1186 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1188 __DRIscreenPrivate
*
1189 __driUtilCreateNewScreen(__DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
1190 __GLcontextModes
* modes
,
1191 const __DRIversion
* ddx_version
,
1192 const __DRIversion
* dri_version
,
1193 const __DRIversion
* drm_version
,
1194 const __DRIframebuffer
* frame_buffer
,
1195 drm_sarea_t
*pSAREA
,
1197 int internal_api_version
,
1198 const struct __DriverAPIRec
*driverAPI
)
1200 __DRIscreenPrivate
*psp
;
1203 #ifdef DRI_NEW_INTERFACE_ONLY
1204 if ( internal_api_version
< 20040602 ) {
1205 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1206 "20040602 or later is required.\n", internal_api_version
);
1210 if ( internal_api_version
== 20031201 ) {
1211 fprintf( stderr
, "libGL error: libGL version 20031201 has critical "
1212 "binary compatilibity bugs.\nlibGL error: You must upgrade "
1213 "to use direct-rendering!\n" );
1216 #endif /* DRI_NEW_INTERFACE_ONLY */
1219 window_exists
= (PFNGLXWINDOWEXISTSPROC
)
1220 glXGetProcAddress( (const GLubyte
*) "__glXWindowExists" );
1222 if ( window_exists
== NULL
) {
1223 #ifdef DRI_NEW_INTERFACE_ONLY
1224 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1225 "20021128 or later is required.\n", internal_api_version
);
1228 window_exists
= (PFNGLXWINDOWEXISTSPROC
) __driWindowExists
;
1229 #endif /* DRI_NEW_INTERFACE_ONLY */
1232 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1233 glXGetProcAddress( (const GLubyte
*) "__glXCreateContextWithConfig" );
1234 if ( create_context_with_config
== NULL
) {
1235 #ifdef DRI_NEW_INTERFACE_ONLY
1236 fprintf( stderr
, "libGL error: libGL.so version (%08u) is too old. "
1237 "20031201 or later is required.\n", internal_api_version
);
1240 create_context_with_config
= (PFNGLXCREATECONTEXTWITHCONFIGPROC
)
1241 fake_XF86DRICreateContextWithConfig
;
1242 #endif /* DRI_NEW_INTERFACE_ONLY */
1245 api_ver
= internal_api_version
;
1247 psp
= (__DRIscreenPrivate
*)_mesa_malloc(sizeof(__DRIscreenPrivate
));
1252 /* Create the hash table */
1253 psp
->drawHash
= drmHashCreate();
1254 if ( psp
->drawHash
== NULL
) {
1265 ** NOT_DONE: This is used by the X server to detect when the client
1266 ** has died while holding the drawable lock. The client sets the
1267 ** drawable lock to this value.
1269 psp
->drawLockID
= 1;
1271 psp
->drmMajor
= drm_version
->major
;
1272 psp
->drmMinor
= drm_version
->minor
;
1273 psp
->drmPatch
= drm_version
->patch
;
1274 psp
->ddxMajor
= ddx_version
->major
;
1275 psp
->ddxMinor
= ddx_version
->minor
;
1276 psp
->ddxPatch
= ddx_version
->patch
;
1277 psp
->driMajor
= dri_version
->major
;
1278 psp
->driMinor
= dri_version
->minor
;
1279 psp
->driPatch
= dri_version
->patch
;
1281 /* install driver's callback functions */
1282 memcpy( &psp
->DriverAPI
, driverAPI
, sizeof(struct __DriverAPIRec
) );
1284 psp
->pSAREA
= pSAREA
;
1286 psp
->pFB
= frame_buffer
->base
;
1287 psp
->fbSize
= frame_buffer
->size
;
1288 psp
->fbStride
= frame_buffer
->stride
;
1289 psp
->fbWidth
= frame_buffer
->width
;
1290 psp
->fbHeight
= frame_buffer
->height
;
1291 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
1292 psp
->pDevPriv
= frame_buffer
->dev_priv
;
1297 ** Do not init dummy context here; actual initialization will be
1298 ** done when the first DRI context is created. Init screen priv ptr
1299 ** to NULL to let CreateContext routine that it needs to be inited.
1301 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
1303 psc
->destroyScreen
= driDestroyScreen
;
1304 #ifndef DRI_NEW_INTERFACE_ONLY
1305 psc
->createContext
= driCreateContext
;
1307 psc
->createContext
= NULL
;
1309 psc
->createNewDrawable
= driCreateNewDrawable
;
1310 psc
->getDrawable
= driGetDrawable
;
1311 #ifdef DRI_NEW_INTERFACE_ONLY
1312 psc
->getMSC
= driGetMSC
;
1313 psc
->createNewContext
= driCreateNewContext
;
1315 if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
1316 psc
->getMSC
= driGetMSC
;
1318 if ( driCompareGLXAPIVersion( 20030824 ) >= 0 ) {
1319 psc
->createNewContext
= driCreateNewContext
;
1324 if ( (psp
->DriverAPI
.InitDriver
!= NULL
)
1325 && !(*psp
->DriverAPI
.InitDriver
)(psp
) ) {
1335 #ifndef DRI_NEW_INTERFACE_ONLY
1337 * Utility function used to create a new driver-private screen structure.
1339 * \param dpy Display pointer.
1340 * \param scrn Index of the screen.
1341 * \param psc DRI screen data (not driver private)
1342 * \param numConfigs Number of visual configs pointed to by \c configs.
1343 * \param configs Array of GLXvisualConfigs exported by the 2D driver.
1344 * \param driverAPI Driver API functions used by other routines in dri_util.c.
1347 * This function has been replaced by \c __driUtilCreateNewScreen. In drivers
1348 * built to work with XFree86, this function will continue to exist to support
1349 * older versions of libGL. Starting with the next major relelase of XFree86,
1350 * this function will be removed.
1352 __DRIscreenPrivate
*
1353 __driUtilCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
1354 int numConfigs
, __GLXvisualConfig
*configs
,
1355 const struct __DriverAPIRec
*driverAPI
)
1358 __DRIscreenPrivate
*psp
= NULL
;
1359 drm_handle_t hSAREA
;
1362 __GLcontextModes
*modes
;
1363 __GLcontextModes
*temp
;
1365 __DRIversion ddx_version
;
1366 __DRIversion dri_version
;
1367 __DRIversion drm_version
;
1368 __DRIframebuffer framebuffer
;
1371 const char * err_msg
;
1372 const char * err_extra
;
1375 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrn
, &directCapable
)
1376 || !directCapable
) {
1381 /* Create the linked list of context modes, and populate it with the
1382 * GLX visual information passed in by libGL.
1385 modes
= _gl_context_modes_create( numConfigs
, sizeof(__GLcontextModes
) );
1386 if ( modes
== NULL
) {
1391 for ( i
= 0 ; i
< numConfigs
; i
++ ) {
1392 assert( temp
!= NULL
);
1393 _gl_copy_visual_to_context_mode( temp
, & configs
[i
] );
1394 temp
->screen
= scrn
;
1399 err_msg
= "XF86DRIOpenConnection";
1402 if (XF86DRIOpenConnection(dpy
, scrn
, &hSAREA
, &BusID
)) {
1403 fd
= drmOpen(NULL
,BusID
);
1404 _mesa_free(BusID
); /* No longer needed */
1406 err_msg
= "open DRM";
1407 err_extra
= strerror( -fd
);
1412 err_msg
= "drmGetMagic";
1415 if (!drmGetMagic(fd
, &magic
)) {
1416 drmVersionPtr version
= drmGetVersion(fd
);
1418 drm_version
.major
= version
->version_major
;
1419 drm_version
.minor
= version
->version_minor
;
1420 drm_version
.patch
= version
->version_patchlevel
;
1421 drmFreeVersion(version
);
1424 drm_version
.major
= -1;
1425 drm_version
.minor
= -1;
1426 drm_version
.patch
= -1;
1429 err_msg
= "XF86DRIAuthConnection";
1430 if (XF86DRIAuthConnection(dpy
, scrn
, magic
)) {
1434 * Get device name (like "tdfx") and the ddx version numbers.
1435 * We'll check the version in each DRI driver's "createScreen"
1438 err_msg
= "XF86DRIGetClientDriverName";
1439 if (XF86DRIGetClientDriverName(dpy
, scrn
,
1445 /* No longer needed. */
1446 _mesa_free( driverName
);
1449 * Get the DRI X extension version.
1451 err_msg
= "XF86DRIQueryVersion";
1452 if (XF86DRIQueryVersion(dpy
,
1455 &dri_version
.patch
)) {
1460 * Get device-specific info. pDevPriv will point to a struct
1461 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
1462 * that has information about the screen size, depth, pitch,
1463 * ancilliary buffers, DRM mmap handles, etc.
1465 err_msg
= "XF86DRIGetDeviceInfo";
1466 if (XF86DRIGetDeviceInfo(dpy
, scrn
,
1470 &framebuffer
.stride
,
1471 &framebuffer
.dev_priv_size
,
1472 &framebuffer
.dev_priv
)) {
1473 framebuffer
.width
= DisplayWidth(dpy
, scrn
);
1474 framebuffer
.height
= DisplayHeight(dpy
, scrn
);
1477 * Map the framebuffer region.
1479 status
= drmMap(fd
, hFB
, framebuffer
.size
,
1480 (drmAddressPtr
)&framebuffer
.base
);
1482 err_msg
= "drmMap of framebuffer";
1483 err_extra
= strerror( -status
);
1485 if ( status
== 0 ) {
1487 * Map the SAREA region. Further mmap regions may be setup in
1488 * each DRI driver's "createScreen" function.
1490 status
= drmMap(fd
, hSAREA
, SAREA_MAX
,
1493 err_msg
= "drmMap of sarea";
1494 err_extra
= strerror( -status
);
1496 if ( status
== 0 ) {
1497 PFNGLXGETINTERNALVERSIONPROC get_ver
;
1499 get_ver
= (PFNGLXGETINTERNALVERSIONPROC
)
1500 glXGetProcAddress( (const GLubyte
*) "__glXGetInternalVersion" );
1502 err_msg
= "InitDriver";
1504 psp
= __driUtilCreateNewScreen( dpy
, scrn
, psc
, modes
,
1511 (get_ver
!= NULL
) ? (*get_ver
)() : 1,
1523 if ( psp
== NULL
) {
1524 if ( pSAREA
!= MAP_FAILED
) {
1525 (void)drmUnmap(pSAREA
, SAREA_MAX
);
1528 if ( framebuffer
.base
!= MAP_FAILED
) {
1529 (void)drmUnmap((drmAddress
)framebuffer
.base
, framebuffer
.size
);
1532 if ( framebuffer
.dev_priv
!= NULL
) {
1533 _mesa_free(framebuffer
.dev_priv
);
1540 if ( modes
!= NULL
) {
1541 _gl_context_modes_destroy( modes
);
1544 (void)XF86DRICloseConnection(dpy
, scrn
);
1546 if ( err_extra
!= NULL
) {
1547 fprintf(stderr
, "libGL error: %s failed (%s)\n", err_msg
,
1551 fprintf(stderr
, "libGL error: %s failed\n", err_msg
);
1554 fprintf(stderr
, "libGL error: reverting to (slow) indirect rendering\n");
1559 #endif /* DRI_NEW_INTERFACE_ONLY */
1563 * Compare the current GLX API version with a driver supplied required version.
1565 * The minimum required version is compared with the API version exported by
1566 * the \c __glXGetInternalVersion function (in libGL.so).
1568 * \param required_version Minimum required internal GLX API version.
1569 * \return A tri-value return, as from strcmp is returned. A value less
1570 * than, equal to, or greater than zero will be returned if the
1571 * internal GLX API version is less than, equal to, or greater
1572 * than \c required_version.
1574 * \sa __glXGetInternalVersion().
1576 int driCompareGLXAPIVersion( GLuint required_version
)
1578 if ( api_ver
> required_version
) {
1581 else if ( api_ver
== required_version
) {
1590 driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
1591 int64_t * sbc
, int64_t * missedFrames
,
1592 float * lastMissedUsage
, float * usage
)
1594 static PFNGLXGETUSTPROC get_ust
;
1595 __DRIswapInfo sInfo
;
1598 __DRIdrawablePrivate
* dpriv
= (__DRIdrawablePrivate
*) priv
;
1600 if ( get_ust
== NULL
) {
1601 get_ust
= (PFNGLXGETUSTPROC
) glXGetProcAddress( (const GLubyte
*) "__glXGetUST" );
1604 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1605 if ( status
== 0 ) {
1606 *sbc
= sInfo
.swap_count
;
1607 *missedFrames
= sInfo
.swap_missed_count
;
1608 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1610 (*get_ust
)( & ust
);
1611 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1619 * Calculate amount of swap interval used between GLX buffer swaps.
1621 * The usage value, on the range [0,max], is the fraction of total swap
1622 * interval time used between GLX buffer swaps is calculated.
1624 * \f$p = t_d / (i * t_r)\f$
1626 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1627 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1628 * required for a single vertical refresh period (as returned by \c
1629 * glXGetMscRateOML).
1631 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1634 * \param dPriv Pointer to the private drawable structure.
1635 * \return If less than a single swap interval time period was required
1636 * between GLX buffer swaps, a number greater than 0 and less than
1637 * 1.0 is returned. If exactly one swap interval time period is
1638 * required, 1.0 is returned, and if more than one is required then
1639 * a number greater than 1.0 will be returned.
1641 * \sa glXSwapIntervalSGI glXGetMscRateOML
1643 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1644 * be possible to cache the sync rate?
1647 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1648 int64_t current_ust
)
1650 static PFNGLXGETMSCRATEOMLPROC get_msc_rate
= NULL
;
1657 if ( get_msc_rate
== NULL
) {
1658 get_msc_rate
= (PFNGLXGETMSCRATEOMLPROC
)
1659 glXGetProcAddress( (const GLubyte
*) "glXGetMscRateOML" );
1662 if ( (get_msc_rate
!= NULL
)
1663 && get_msc_rate( dPriv
->display
, dPriv
->draw
, &n
, &d
) ) {
1664 interval
= (dPriv
->pdraw
->swap_interval
!= 0)
1665 ? dPriv
->pdraw
->swap_interval
: 1;
1668 /* We want to calculate
1669 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1670 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1671 * dPriv->swap_ust. interval has already been calculated.
1673 * The only tricky part is us_per_refresh. us_per_refresh is
1674 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1675 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1676 * the denominator of the final calculation, we calculate
1677 * (interval * 1000000 * d) and move n into the numerator.
1680 usage
= (current_ust
- last_swap_ust
);
1682 usage
/= (interval
* d
);