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
26 #define MAP_FAILED ((void *)-1)
33 #include "drm_sarea.h"
34 #include "glcontextmodes.h"
36 #ifndef GLX_OML_sync_control
37 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRInativeDisplay
*dpy
, __DRIid drawable
, int32_t *numerator
, int32_t *denominator
);
40 /* This pointer *must* be set by the driver's __driCreateNewScreen funciton!
42 const __DRIinterfaceMethods
* dri_interface
= NULL
;
45 * Weak thread-safety dispatch pointer. Older versions of libGL will not have
46 * this symbol, so a "weak" version is included here so that the driver will
47 * dynamically link properly. The value is set to \c NULL. This forces the
48 * driver to fall back to the old dispatch interface.
50 struct _glapi_table
*_glapi_DispatchTSD
__attribute__((weak
)) = NULL
;
53 * This is used in a couple of places that call \c driCreateNewDrawable.
55 static const int empty_attribute_list
[1] = { None
};
59 * Cached copy of the internal API version used by libGL and the client-side
62 static int api_ver
= 0;
64 /* forward declarations */
65 static int driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
66 int64_t * sbc
, int64_t * missedFrames
, float * lastMissedUsage
,
69 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
70 __DRIid draw
, __DRIdrawable
*pdraw
, int renderType
, const int *attrs
);
72 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
);
76 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
79 * Is called from the drivers.
81 * \param f \c printf like format string.
84 __driUtilMessage(const char *f
, ...)
88 if (getenv("LIBGL_DEBUG")) {
89 fprintf(stderr
, "libGL error: \n");
91 vfprintf(stderr
, f
, args
);
93 fprintf(stderr
, "\n");
98 /*****************************************************************/
99 /** \name Drawable list management */
100 /*****************************************************************/
103 static GLboolean
__driAddDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
105 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
107 if (drmHashInsert(drawHash
, pdp
->draw
, pdraw
))
113 static __DRIdrawable
*__driFindDrawable(void *drawHash
, __DRIid draw
)
116 __DRIdrawable
*pdraw
;
118 retcode
= drmHashLookup(drawHash
, draw
, (void *)&pdraw
);
127 * Find drawables in the local hash that have been destroyed on the
130 * \param drawHash Hash-table containing all know drawables.
132 static void __driGarbageCollectDrawables(void *drawHash
)
135 __DRInativeDisplay
*dpy
;
136 __DRIdrawable
*pdraw
;
138 if (drmHashFirst(drawHash
, &draw
, (void *)&pdraw
) == 1) {
140 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
141 dpy
= pdp
->driScreenPriv
->display
;
142 if (! (*dri_interface
->windowExists
)(dpy
, draw
)) {
143 /* Destroy the local drawable data in the hash table, if the
144 drawable no longer exists in the Xserver */
145 drmHashDelete(drawHash
, draw
);
146 (*pdraw
->destroyDrawable
)(dpy
, pdraw
->private);
149 } while (drmHashNext(drawHash
, &draw
, (void *)&pdraw
) == 1);
156 /*****************************************************************/
157 /** \name Context (un)binding functions */
158 /*****************************************************************/
164 * \param dpy the display handle.
165 * \param scrn the screen number.
166 * \param draw drawable.
167 * \param read Current reading drawable.
170 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
173 * This function calls __DriverAPIRec::UnbindContext, and then decrements
174 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
177 * While casting the opaque private pointers associated with the parameters
178 * into their respective real types it also assures they are not \c NULL.
180 static GLboolean
driUnbindContext(__DRInativeDisplay
*dpy
, int scrn
,
181 __DRIid draw
, __DRIid read
,
184 __DRIscreen
*pDRIScreen
;
185 __DRIdrawable
*pdraw
;
186 __DRIdrawable
*pread
;
187 __DRIcontextPrivate
*pcp
;
188 __DRIscreenPrivate
*psp
;
189 __DRIdrawablePrivate
*pdp
;
190 __DRIdrawablePrivate
*prp
;
193 ** Assume error checking is done properly in glXMakeCurrent before
194 ** calling driUnbindContext.
197 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
202 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, scrn
);
203 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
208 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
209 pcp
= (__DRIcontextPrivate
*)ctx
->private;
211 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
216 pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
218 pread
= __driFindDrawable(psp
->drawHash
, read
);
223 prp
= (__DRIdrawablePrivate
*)pread
->private;
226 /* Let driver unbind drawable from context */
227 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
230 if (pdp
->refcount
== 0) {
238 if (prp
->refcount
== 0) {
247 /* XXX this is disabled so that if we call SwapBuffers on an unbound
248 * window we can determine the last context bound to the window and
249 * use that context's lock. (BrianP, 2-Dec-2000)
252 /* Unbind the drawable */
253 pcp
->driDrawablePriv
= NULL
;
254 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
262 * This function takes both a read buffer and a draw buffer. This is needed
263 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
266 * \bug This function calls \c driCreateNewDrawable in two places with the
267 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
268 * be needed in those places when support for pbuffers and / or pixmaps
269 * is added. Is it safe to assume that the drawable is a window?
271 static GLboolean
DoBindContext(__DRInativeDisplay
*dpy
,
272 __DRIid draw
, __DRIid read
,
273 __DRIcontext
*ctx
, const __GLcontextModes
* modes
,
274 __DRIscreenPrivate
*psp
)
276 __DRIdrawable
*pdraw
;
277 __DRIdrawablePrivate
*pdp
;
278 __DRIdrawable
*pread
;
279 __DRIdrawablePrivate
*prp
;
280 __DRIcontextPrivate
* const pcp
= ctx
->private;
283 /* Find the _DRIdrawable which corresponds to the writing drawable. */
284 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
286 /* Allocate a new drawable */
287 pdraw
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
293 /* Create a new drawable */
294 driCreateNewDrawable(dpy
, modes
, draw
, pdraw
, GLX_WINDOW_BIT
,
295 empty_attribute_list
);
296 if (!pdraw
->private) {
303 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
305 /* Find the _DRIdrawable which corresponds to the reading drawable. */
307 /* read buffer == draw buffer */
311 pread
= __driFindDrawable(psp
->drawHash
, read
);
313 /* Allocate a new drawable */
314 pread
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
320 /* Create a new drawable */
321 driCreateNewDrawable(dpy
, modes
, read
, pread
, GLX_WINDOW_BIT
,
322 empty_attribute_list
);
323 if (!pread
->private) {
329 prp
= (__DRIdrawablePrivate
*) pread
->private;
332 /* Bind the drawable to the context */
333 pcp
->driDrawablePriv
= pdp
;
334 pdp
->driContextPriv
= pcp
;
341 ** Now that we have a context associated with this drawable, we can
342 ** initialize the drawable information if has not been done before.
344 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
345 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
346 __driUtilUpdateDrawableInfo(pdp
);
347 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
350 /* Call device-specific MakeCurrent */
351 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
358 * This function takes both a read buffer and a draw buffer. This is needed
359 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
362 static GLboolean
driBindContext(__DRInativeDisplay
*dpy
, int scrn
,
363 __DRIid draw
, __DRIid read
,
366 __DRIscreen
*pDRIScreen
;
369 ** Assume error checking is done properly in glXMakeCurrent before
370 ** calling driBindContext.
373 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
378 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, scrn
);
379 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
384 return DoBindContext( dpy
, draw
, read
, ctx
, ctx
->mode
,
385 (__DRIscreenPrivate
*)pDRIScreen
->private );
390 /*****************************************************************/
391 /** \name Drawable handling functions */
392 /*****************************************************************/
396 * Update private drawable information.
398 * \param pdp pointer to the private drawable information to update.
400 * This function basically updates the __DRIdrawablePrivate struct's
401 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
402 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
403 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
404 * the values are different that means we have to update the clipping
408 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
410 __DRIscreenPrivate
*psp
;
411 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
413 if (!pcp
|| (pdp
!= pcp
->driDrawablePriv
)) {
418 psp
= pdp
->driScreenPriv
;
424 if (pdp
->pClipRects
) {
425 _mesa_free(pdp
->pClipRects
);
428 if (pdp
->pBackClipRects
) {
429 _mesa_free(pdp
->pBackClipRects
);
432 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
434 if (!__driFindDrawable(psp
->drawHash
, pdp
->draw
) ||
435 ! (*dri_interface
->getDrawableInfo
)(pdp
->display
, pdp
->screen
, pdp
->draw
,
436 &pdp
->index
, &pdp
->lastStamp
,
437 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
438 &pdp
->numClipRects
, &pdp
->pClipRects
,
441 &pdp
->numBackClipRects
,
442 &pdp
->pBackClipRects
)) {
443 /* Error -- eg the window may have been destroyed. Keep going
446 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
447 pdp
->numClipRects
= 0;
448 pdp
->pClipRects
= NULL
;
449 pdp
->numBackClipRects
= 0;
450 pdp
->pBackClipRects
= NULL
;
453 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
455 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
461 /*****************************************************************/
462 /** \name GLX callbacks */
463 /*****************************************************************/
469 * \param dpy the display handle.
470 * \param drawablePrivate opaque pointer to the per-drawable private info.
473 * This function calls __DRIdrawablePrivate::swapBuffers.
475 * Is called directly from glXSwapBuffers().
477 static void driSwapBuffers( __DRInativeDisplay
*dpy
, void *drawablePrivate
)
479 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
480 dPriv
->swapBuffers(dPriv
);
485 * Called directly from a number of higher-level GLX functions.
487 static int driGetMSC( void *screenPrivate
, int64_t *msc
)
489 __DRIscreenPrivate
*sPriv
= (__DRIscreenPrivate
*) screenPrivate
;
491 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
495 * Called directly from a number of higher-level GLX functions.
497 static int driGetSBC( __DRInativeDisplay
*dpy
, void *drawablePrivate
, int64_t *sbc
)
499 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
504 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
505 *sbc
= sInfo
.swap_count
;
510 static int driWaitForSBC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
512 int64_t * msc
, int64_t * sbc
)
514 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
516 return dPriv
->driScreenPriv
->DriverAPI
.WaitForSBC( dPriv
, target_sbc
,
520 static int driWaitForMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
522 int64_t divisor
, int64_t remainder
,
523 int64_t * msc
, int64_t * sbc
)
525 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
530 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
534 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
535 * is supported but GLX_OML_sync_control is not. Therefore, don't return
536 * an error value if GetSwapInfo() is not implemented.
539 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
540 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
541 *sbc
= sInfo
.swap_count
;
547 static int64_t driSwapBuffersMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
549 int64_t divisor
, int64_t remainder
)
551 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
553 return dPriv
->driScreenPriv
->DriverAPI
.SwapBuffersMSC( dPriv
, target_msc
,
560 * This is called via __DRIscreenRec's createNewDrawable pointer.
562 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
,
563 const __GLcontextModes
*modes
,
565 __DRIdrawable
*pdraw
,
569 __DRIscreen
* const pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, modes
->screen
);
570 __DRIscreenPrivate
*psp
;
571 __DRIdrawablePrivate
*pdp
;
574 pdraw
->private = NULL
;
576 /* Since pbuffers are not yet supported, no drawable attributes are
581 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
585 pdp
= (__DRIdrawablePrivate
*)_mesa_malloc(sizeof(__DRIdrawablePrivate
));
590 if (!(*dri_interface
->createDrawable
)(dpy
, modes
->screen
, draw
, &pdp
->hHWDrawable
)) {
605 pdp
->numClipRects
= 0;
606 pdp
->numBackClipRects
= 0;
607 pdp
->pClipRects
= NULL
;
608 pdp
->pBackClipRects
= NULL
;
610 pdp
->screen
= modes
->screen
;
612 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
613 pdp
->driScreenPriv
= psp
;
614 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
616 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
617 renderType
== GLX_PIXMAP_BIT
)) {
618 (void)(*dri_interface
->destroyDrawable
)(dpy
, modes
->screen
, pdp
->draw
);
623 pdraw
->private = pdp
;
624 pdraw
->destroyDrawable
= driDestroyDrawable
;
625 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
627 pdraw
->getSBC
= driGetSBC
;
628 pdraw
->waitForSBC
= driWaitForSBC
;
629 pdraw
->waitForMSC
= driWaitForMSC
;
630 pdraw
->swapBuffersMSC
= driSwapBuffersMSC
;
631 pdraw
->frameTracking
= NULL
;
632 pdraw
->queryFrameTracking
= driQueryFrameTracking
;
634 /* This special default value is replaced with the configured
635 * default value when the drawable is first bound to a direct
638 pdraw
->swap_interval
= (unsigned)-1;
640 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
642 /* Add pdraw to drawable list */
643 if (!__driAddDrawable(psp
->drawHash
, pdraw
)) {
645 (*pdraw
->destroyDrawable
)(dpy
, pdp
);
648 pdraw
->private = NULL
;
654 static __DRIdrawable
*driGetDrawable(__DRInativeDisplay
*dpy
, __DRIid draw
,
657 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
660 ** Make sure this routine returns NULL if the drawable is not bound
661 ** to a direct rendering context!
663 return __driFindDrawable(psp
->drawHash
, draw
);
666 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
)
668 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*) drawablePrivate
;
669 __DRIscreenPrivate
*psp
= pdp
->driScreenPriv
;
670 int scrn
= psp
->myNum
;
673 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
674 if ((*dri_interface
->windowExists
)(dpy
, pdp
->draw
))
675 (void)(*dri_interface
->destroyDrawable
)(dpy
, scrn
, pdp
->draw
);
676 if (pdp
->pClipRects
) {
677 _mesa_free(pdp
->pClipRects
);
678 pdp
->pClipRects
= NULL
;
680 if (pdp
->pBackClipRects
) {
681 _mesa_free(pdp
->pBackClipRects
);
682 pdp
->pBackClipRects
= NULL
;
691 /*****************************************************************/
692 /** \name Context handling functions */
693 /*****************************************************************/
697 * Destroy the per-context private information.
699 * \param dpy the display handle.
700 * \param scrn the screen number.
701 * \param contextPrivate opaque pointer to the per-drawable private info.
704 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
705 * drmDestroyContext(), and finally frees \p contextPrivate.
707 static void driDestroyContext(__DRInativeDisplay
*dpy
, int scrn
, void *contextPrivate
)
709 __DRIcontextPrivate
*pcp
= (__DRIcontextPrivate
*) contextPrivate
;
712 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
713 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
714 (void) (*dri_interface
->destroyContext
)(dpy
, scrn
, pcp
->contextID
);
721 * Create the per-drawable private driver information.
723 * \param dpy The display handle.
724 * \param modes Mode used to create the new context.
725 * \param render_type Type of rendering target. \c GLX_RGBA is the only
726 * type likely to ever be supported for direct-rendering.
727 * \param sharedPrivate The shared context dependent methods or \c NULL if
729 * \param pctx DRI context to receive the context dependent methods.
731 * \returns An opaque pointer to the per-context private information on
732 * success, or \c NULL on failure.
735 * This function allocates and fills a __DRIcontextPrivateRec structure. It
736 * performs some device independent initialization and passes all the
737 * relevent information to __DriverAPIRec::CreateContext to create the
742 driCreateNewContext(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
743 int render_type
, void *sharedPrivate
, __DRIcontext
*pctx
)
745 __DRIscreen
*pDRIScreen
;
746 __DRIcontextPrivate
*pcp
;
747 __DRIcontextPrivate
*pshare
= (__DRIcontextPrivate
*) sharedPrivate
;
748 __DRIscreenPrivate
*psp
;
749 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
751 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, modes
->screen
);
752 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
757 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
759 pcp
= (__DRIcontextPrivate
*)_mesa_malloc(sizeof(__DRIcontextPrivate
));
764 if (! (*dri_interface
->createContext
)(dpy
, modes
->screen
, modes
->fbconfigID
,
765 &pcp
->contextID
, &pcp
->hHWContext
)) {
771 pcp
->driScreenPriv
= psp
;
772 pcp
->driDrawablePriv
= NULL
;
774 /* When the first context is created for a screen, initialize a "dummy"
778 if (!psp
->dummyContextPriv
.driScreenPriv
) {
779 psp
->dummyContextPriv
.contextID
= 0;
780 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
781 psp
->dummyContextPriv
.driScreenPriv
= psp
;
782 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
783 psp
->dummyContextPriv
.driverPrivate
= NULL
;
784 /* No other fields should be used! */
787 pctx
->destroyContext
= driDestroyContext
;
788 pctx
->bindContext
= driBindContext
;
789 pctx
->unbindContext
= driUnbindContext
;
791 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
792 (void) (*dri_interface
->destroyContext
)(dpy
, modes
->screen
,
798 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
805 /*****************************************************************/
806 /** \name Screen handling functions */
807 /*****************************************************************/
811 * Destroy the per-screen private information.
813 * \param dpy the display handle.
814 * \param scrn the screen number.
815 * \param screenPrivate opaque pointer to the per-screen private information.
818 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
819 * drmClose(), and finally frees \p screenPrivate.
821 static void driDestroyScreen(__DRInativeDisplay
*dpy
, int scrn
, void *screenPrivate
)
823 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
826 /* No interaction with the X-server is possible at this point. This
827 * routine is called after XCloseDisplay, so there is no protocol
828 * stream open to the X-server anymore.
831 if (psp
->DriverAPI
.DestroyScreen
)
832 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
834 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
835 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
836 _mesa_free(psp
->pDevPriv
);
837 (void)drmClose(psp
->fd
);
838 if ( psp
->modes
!= NULL
) {
839 _gl_context_modes_destroy( psp
->modes
);
847 * Utility function used to create a new driver-private screen structure.
849 * \param dpy Display pointer
850 * \param scrn Index of the screen
851 * \param psc DRI screen data (not driver private)
852 * \param modes Linked list of known display modes. This list is, at a
853 * minimum, a list of modes based on the current display mode.
854 * These roughly match the set of available X11 visuals, but it
855 * need not be limited to X11! The calling libGL should create
856 * a list that will inform the driver of the current display
857 * mode (i.e., color buffer depth, depth buffer depth, etc.).
858 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
860 * \param dri_version Version of the "server-side" DRI.
861 * \param drm_version Version of the kernel DRM.
862 * \param frame_buffer Data describing the location and layout of the
864 * \param pSAREA Pointer the the SAREA.
865 * \param fd Device handle for the DRM.
866 * \param internal_api_version Version of the internal interface between the
868 * \param driverAPI Driver API functions used by other routines in dri_util.c.
871 * There is no need to check the minimum API version in this function. Since
872 * the \c __driCreateNewScreen function is versioned, it is impossible for a
873 * loader that is too old to even load this driver.
876 __driUtilCreateNewScreen(__DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
877 __GLcontextModes
* modes
,
878 const __DRIversion
* ddx_version
,
879 const __DRIversion
* dri_version
,
880 const __DRIversion
* drm_version
,
881 const __DRIframebuffer
* frame_buffer
,
884 int internal_api_version
,
885 const struct __DriverAPIRec
*driverAPI
)
887 __DRIscreenPrivate
*psp
;
890 api_ver
= internal_api_version
;
892 psp
= (__DRIscreenPrivate
*)_mesa_malloc(sizeof(__DRIscreenPrivate
));
897 /* Create the hash table */
898 psp
->drawHash
= drmHashCreate();
899 if ( psp
->drawHash
== NULL
) {
910 ** NOT_DONE: This is used by the X server to detect when the client
911 ** has died while holding the drawable lock. The client sets the
912 ** drawable lock to this value.
916 psp
->drmMajor
= drm_version
->major
;
917 psp
->drmMinor
= drm_version
->minor
;
918 psp
->drmPatch
= drm_version
->patch
;
919 psp
->ddxMajor
= ddx_version
->major
;
920 psp
->ddxMinor
= ddx_version
->minor
;
921 psp
->ddxPatch
= ddx_version
->patch
;
922 psp
->driMajor
= dri_version
->major
;
923 psp
->driMinor
= dri_version
->minor
;
924 psp
->driPatch
= dri_version
->patch
;
926 /* install driver's callback functions */
927 memcpy( &psp
->DriverAPI
, driverAPI
, sizeof(struct __DriverAPIRec
) );
929 psp
->pSAREA
= pSAREA
;
931 psp
->pFB
= frame_buffer
->base
;
932 psp
->fbSize
= frame_buffer
->size
;
933 psp
->fbStride
= frame_buffer
->stride
;
934 psp
->fbWidth
= frame_buffer
->width
;
935 psp
->fbHeight
= frame_buffer
->height
;
936 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
937 psp
->pDevPriv
= frame_buffer
->dev_priv
;
938 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
943 ** Do not init dummy context here; actual initialization will be
944 ** done when the first DRI context is created. Init screen priv ptr
945 ** to NULL to let CreateContext routine that it needs to be inited.
947 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
949 psc
->destroyScreen
= driDestroyScreen
;
950 psc
->createNewDrawable
= driCreateNewDrawable
;
951 psc
->getDrawable
= driGetDrawable
;
952 psc
->getMSC
= driGetMSC
;
953 psc
->createNewContext
= driCreateNewContext
;
955 if ( (psp
->DriverAPI
.InitDriver
!= NULL
)
956 && !(*psp
->DriverAPI
.InitDriver
)(psp
) ) {
967 * Compare the current GLX API version with a driver supplied required version.
969 * The minimum required version is compared with the API version exported by
970 * the \c __glXGetInternalVersion function (in libGL.so).
972 * \param required_version Minimum required internal GLX API version.
973 * \return A tri-value return, as from strcmp is returned. A value less
974 * than, equal to, or greater than zero will be returned if the
975 * internal GLX API version is less than, equal to, or greater
976 * than \c required_version.
978 * \sa __glXGetInternalVersion().
980 int driCompareGLXAPIVersion( GLint required_version
)
982 if ( api_ver
> required_version
) {
985 else if ( api_ver
== required_version
) {
994 driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
995 int64_t * sbc
, int64_t * missedFrames
,
996 float * lastMissedUsage
, float * usage
)
1001 __DRIdrawablePrivate
* dpriv
= (__DRIdrawablePrivate
*) priv
;
1004 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1005 if ( status
== 0 ) {
1006 *sbc
= sInfo
.swap_count
;
1007 *missedFrames
= sInfo
.swap_missed_count
;
1008 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1010 (*dri_interface
->getUST
)( & ust
);
1011 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1019 * Calculate amount of swap interval used between GLX buffer swaps.
1021 * The usage value, on the range [0,max], is the fraction of total swap
1022 * interval time used between GLX buffer swaps is calculated.
1024 * \f$p = t_d / (i * t_r)\f$
1026 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1027 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1028 * required for a single vertical refresh period (as returned by \c
1029 * glXGetMscRateOML).
1031 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1034 * \param dPriv Pointer to the private drawable structure.
1035 * \return If less than a single swap interval time period was required
1036 * between GLX buffer swaps, a number greater than 0 and less than
1037 * 1.0 is returned. If exactly one swap interval time period is
1038 * required, 1.0 is returned, and if more than one is required then
1039 * a number greater than 1.0 will be returned.
1041 * \sa glXSwapIntervalSGI glXGetMscRateOML
1043 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1044 * be possible to cache the sync rate?
1047 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1048 int64_t current_ust
)
1056 if ( (*dri_interface
->getMSCRate
)( dPriv
->display
, dPriv
->draw
, &n
, &d
) ) {
1057 interval
= (dPriv
->pdraw
->swap_interval
!= 0)
1058 ? dPriv
->pdraw
->swap_interval
: 1;
1061 /* We want to calculate
1062 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1063 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1064 * dPriv->swap_ust. interval has already been calculated.
1066 * The only tricky part is us_per_refresh. us_per_refresh is
1067 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1068 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1069 * the denominator of the final calculation, we calculate
1070 * (interval * 1000000 * d) and move n into the numerator.
1073 usage
= (current_ust
- last_swap_ust
);
1075 usage
/= (interval
* d
);