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"
35 #ifndef GLX_OML_sync_control
36 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRInativeDisplay
*dpy
, __DRIid drawable
, int32_t *numerator
, int32_t *denominator
);
39 /* This pointer *must* be set by the driver's __driCreateNewScreen funciton!
41 const __DRIinterfaceMethods
* dri_interface
= NULL
;
44 * This is used in a couple of places that call \c driCreateNewDrawable.
46 static const int empty_attribute_list
[1] = { None
};
50 * Cached copy of the internal API version used by libGL and the client-side
53 static int api_ver
= 0;
55 /* forward declarations */
56 static int driQueryFrameTracking( __DRInativeDisplay
*dpy
, void *priv
,
57 int64_t *sbc
, int64_t *missedFrames
,
58 float *lastMissedUsage
, float *usage
);
60 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
,
61 const __GLcontextModes
*modes
,
62 __DRIid draw
, __DRIdrawable
*pdraw
,
63 int renderType
, const int *attrs
);
65 static void driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
);
69 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
72 * Is called from the drivers.
74 * \param f \c printf like format string.
77 __driUtilMessage(const char *f
, ...)
81 if (getenv("LIBGL_DEBUG")) {
82 fprintf(stderr
, "libGL error: \n");
84 vfprintf(stderr
, f
, args
);
86 fprintf(stderr
, "\n");
91 /*****************************************************************/
92 /** \name Drawable list management */
93 /*****************************************************************/
96 static GLboolean
__driAddDrawable(void *drawHash
, __DRIdrawable
*pdraw
)
98 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
100 if (drmHashInsert(drawHash
, pdp
->draw
, pdraw
))
106 static __DRIdrawable
*__driFindDrawable(void *drawHash
, __DRIid draw
)
109 __DRIdrawable
*pdraw
;
111 retcode
= drmHashLookup(drawHash
, draw
, (void *)&pdraw
);
120 * Find drawables in the local hash that have been destroyed on the
123 * \param drawHash Hash-table containing all know drawables.
125 static void __driGarbageCollectDrawables(void *drawHash
)
128 __DRInativeDisplay
*dpy
;
129 __DRIdrawable
*pdraw
;
131 if (drmHashFirst(drawHash
, &draw
, (void *)&pdraw
) == 1) {
133 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
134 dpy
= pdp
->driScreenPriv
->display
;
135 if (! (*dri_interface
->windowExists
)(dpy
, draw
)) {
136 /* Destroy the local drawable data, if the drawable no
137 longer exists in the Xserver */
138 (*pdraw
->destroyDrawable
)(dpy
, pdraw
->private);
141 } while (drmHashNext(drawHash
, &draw
, (void *)&pdraw
) == 1);
148 /*****************************************************************/
149 /** \name Context (un)binding functions */
150 /*****************************************************************/
156 * \param dpy the display handle.
157 * \param scrn the screen number.
158 * \param draw drawable.
159 * \param read Current reading drawable.
162 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
165 * This function calls __DriverAPIRec::UnbindContext, and then decrements
166 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
169 * While casting the opaque private pointers associated with the parameters
170 * into their respective real types it also assures they are not \c NULL.
172 static GLboolean
driUnbindContext(__DRInativeDisplay
*dpy
, int scrn
,
173 __DRIid draw
, __DRIid read
,
176 __DRIscreen
*pDRIScreen
;
177 __DRIdrawable
*pdraw
;
178 __DRIdrawable
*pread
;
179 __DRIcontextPrivate
*pcp
;
180 __DRIscreenPrivate
*psp
;
181 __DRIdrawablePrivate
*pdp
;
182 __DRIdrawablePrivate
*prp
;
185 ** Assume error checking is done properly in glXMakeCurrent before
186 ** calling driUnbindContext.
189 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
194 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, scrn
);
195 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
200 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
201 pcp
= (__DRIcontextPrivate
*)ctx
->private;
203 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
208 pdp
= (__DRIdrawablePrivate
*)pdraw
->private;
210 pread
= __driFindDrawable(psp
->drawHash
, read
);
215 prp
= (__DRIdrawablePrivate
*)pread
->private;
218 /* Let driver unbind drawable from context */
219 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
222 if (pdp
->refcount
== 0) {
230 if (prp
->refcount
== 0) {
239 /* XXX this is disabled so that if we call SwapBuffers on an unbound
240 * window we can determine the last context bound to the window and
241 * use that context's lock. (BrianP, 2-Dec-2000)
244 /* Unbind the drawable */
245 pcp
->driDrawablePriv
= NULL
;
246 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
254 * This function takes both a read buffer and a draw buffer. This is needed
255 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
258 * \bug This function calls \c driCreateNewDrawable in two places with the
259 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
260 * be needed in those places when support for pbuffers and / or pixmaps
261 * is added. Is it safe to assume that the drawable is a window?
263 static GLboolean
DoBindContext(__DRInativeDisplay
*dpy
,
264 __DRIid draw
, __DRIid read
,
265 __DRIcontext
*ctx
, const __GLcontextModes
* modes
,
266 __DRIscreenPrivate
*psp
)
268 __DRIdrawable
*pdraw
;
269 __DRIdrawablePrivate
*pdp
;
270 __DRIdrawable
*pread
;
271 __DRIdrawablePrivate
*prp
;
272 __DRIcontextPrivate
* const pcp
= ctx
->private;
275 /* Find the _DRIdrawable which corresponds to the writing drawable. */
276 pdraw
= __driFindDrawable(psp
->drawHash
, draw
);
278 /* Allocate a new drawable */
279 pdraw
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
285 /* Create a new drawable */
286 driCreateNewDrawable(dpy
, modes
, draw
, pdraw
, GLX_WINDOW_BIT
,
287 empty_attribute_list
);
288 if (!pdraw
->private) {
295 pdp
= (__DRIdrawablePrivate
*) pdraw
->private;
297 /* Find the _DRIdrawable which corresponds to the reading drawable. */
299 /* read buffer == draw buffer */
303 pread
= __driFindDrawable(psp
->drawHash
, read
);
305 /* Allocate a new drawable */
306 pread
= (__DRIdrawable
*)_mesa_malloc(sizeof(__DRIdrawable
));
312 /* Create a new drawable */
313 driCreateNewDrawable(dpy
, modes
, read
, pread
, GLX_WINDOW_BIT
,
314 empty_attribute_list
);
315 if (!pread
->private) {
321 prp
= (__DRIdrawablePrivate
*) pread
->private;
324 /* Bind the drawable to the context */
325 pcp
->driDrawablePriv
= pdp
;
326 pdp
->driContextPriv
= pcp
;
333 ** Now that we have a context associated with this drawable, we can
334 ** initialize the drawable information if has not been done before.
336 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
337 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
338 __driUtilUpdateDrawableInfo(pdp
);
339 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
342 /* Call device-specific MakeCurrent */
343 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
350 * This function takes both a read buffer and a draw buffer. This is needed
351 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
354 static GLboolean
driBindContext(__DRInativeDisplay
*dpy
, int scrn
,
355 __DRIid draw
, __DRIid read
,
358 __DRIscreen
*pDRIScreen
;
361 ** Assume error checking is done properly in glXMakeCurrent before
362 ** calling driBindContext.
365 if (ctx
== NULL
|| draw
== None
|| read
== None
) {
370 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, scrn
);
371 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
376 return DoBindContext( dpy
, draw
, read
, ctx
, ctx
->mode
,
377 (__DRIscreenPrivate
*)pDRIScreen
->private );
382 /*****************************************************************/
383 /** \name Drawable handling functions */
384 /*****************************************************************/
388 * Update private drawable information.
390 * \param pdp pointer to the private drawable information to update.
392 * This function basically updates the __DRIdrawablePrivate struct's
393 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
394 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
395 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
396 * the values are different that means we have to update the clipping
400 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
402 __DRIscreenPrivate
*psp
;
403 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
405 if (!pcp
|| (pdp
!= pcp
->driDrawablePriv
)) {
410 psp
= pdp
->driScreenPriv
;
416 if (pdp
->pClipRects
) {
417 _mesa_free(pdp
->pClipRects
);
420 if (pdp
->pBackClipRects
) {
421 _mesa_free(pdp
->pBackClipRects
);
424 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
426 if (!__driFindDrawable(psp
->drawHash
, pdp
->draw
) ||
427 ! (*dri_interface
->getDrawableInfo
)(pdp
->display
, pdp
->screen
, pdp
->draw
,
428 &pdp
->index
, &pdp
->lastStamp
,
429 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
430 &pdp
->numClipRects
, &pdp
->pClipRects
,
433 &pdp
->numBackClipRects
,
434 &pdp
->pBackClipRects
)) {
435 /* Error -- eg the window may have been destroyed. Keep going
438 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
439 pdp
->numClipRects
= 0;
440 pdp
->pClipRects
= NULL
;
441 pdp
->numBackClipRects
= 0;
442 pdp
->pBackClipRects
= NULL
;
445 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
447 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
453 /*****************************************************************/
454 /** \name GLX callbacks */
455 /*****************************************************************/
461 * \param dpy the display handle.
462 * \param drawablePrivate opaque pointer to the per-drawable private info.
465 * This function calls __DRIdrawablePrivate::swapBuffers.
467 * Is called directly from glXSwapBuffers().
469 static void driSwapBuffers( __DRInativeDisplay
*dpy
, void *drawablePrivate
)
471 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
472 dPriv
->swapBuffers(dPriv
);
477 * Called directly from a number of higher-level GLX functions.
479 static int driGetMSC( void *screenPrivate
, int64_t *msc
)
481 __DRIscreenPrivate
*sPriv
= (__DRIscreenPrivate
*) screenPrivate
;
483 return sPriv
->DriverAPI
.GetMSC( sPriv
, msc
);
487 * Called directly from a number of higher-level GLX functions.
489 static int driGetSBC( __DRInativeDisplay
*dpy
, void *drawablePrivate
, int64_t *sbc
)
491 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePrivate
;
496 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
497 *sbc
= sInfo
.swap_count
;
502 static int driWaitForSBC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
504 int64_t * msc
, int64_t * sbc
)
506 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
508 return dPriv
->driScreenPriv
->DriverAPI
.WaitForSBC( dPriv
, target_sbc
,
512 static int driWaitForMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
514 int64_t divisor
, int64_t remainder
,
515 int64_t * msc
, int64_t * sbc
)
517 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
522 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
526 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
527 * is supported but GLX_OML_sync_control is not. Therefore, don't return
528 * an error value if GetSwapInfo() is not implemented.
531 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
532 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
533 *sbc
= sInfo
.swap_count
;
539 static int64_t driSwapBuffersMSC( __DRInativeDisplay
* dpy
, void *drawablePriv
,
541 int64_t divisor
, int64_t remainder
)
543 __DRIdrawablePrivate
*dPriv
= (__DRIdrawablePrivate
*) drawablePriv
;
545 return dPriv
->driScreenPriv
->DriverAPI
.SwapBuffersMSC( dPriv
, target_msc
,
552 * This is called via __DRIscreenRec's createNewDrawable pointer.
554 static void *driCreateNewDrawable(__DRInativeDisplay
*dpy
,
555 const __GLcontextModes
*modes
,
557 __DRIdrawable
*pdraw
,
561 __DRIscreen
* const pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, modes
->screen
);
562 __DRIscreenPrivate
*psp
;
563 __DRIdrawablePrivate
*pdp
;
566 pdraw
->private = NULL
;
568 /* Since pbuffers are not yet supported, no drawable attributes are
573 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
577 pdp
= (__DRIdrawablePrivate
*)_mesa_malloc(sizeof(__DRIdrawablePrivate
));
582 if (!(*dri_interface
->createDrawable
)(dpy
, modes
->screen
, draw
, &pdp
->hHWDrawable
)) {
597 pdp
->numClipRects
= 0;
598 pdp
->numBackClipRects
= 0;
599 pdp
->pClipRects
= NULL
;
600 pdp
->pBackClipRects
= NULL
;
602 pdp
->screen
= modes
->screen
;
604 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
605 pdp
->driScreenPriv
= psp
;
606 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
608 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, modes
,
609 renderType
== GLX_PIXMAP_BIT
)) {
610 (void)(*dri_interface
->destroyDrawable
)(dpy
, modes
->screen
, pdp
->draw
);
615 pdraw
->private = pdp
;
616 pdraw
->destroyDrawable
= driDestroyDrawable
;
617 pdraw
->swapBuffers
= driSwapBuffers
; /* called by glXSwapBuffers() */
619 pdraw
->getSBC
= driGetSBC
;
620 pdraw
->waitForSBC
= driWaitForSBC
;
621 pdraw
->waitForMSC
= driWaitForMSC
;
622 pdraw
->swapBuffersMSC
= driSwapBuffersMSC
;
623 pdraw
->frameTracking
= NULL
;
624 pdraw
->queryFrameTracking
= driQueryFrameTracking
;
626 /* This special default value is replaced with the configured
627 * default value when the drawable is first bound to a direct
630 pdraw
->swap_interval
= (unsigned)-1;
632 pdp
->swapBuffers
= psp
->DriverAPI
.SwapBuffers
;
634 /* Add pdraw to drawable list */
635 if (!__driAddDrawable(psp
->drawHash
, pdraw
)) {
637 (*pdraw
->destroyDrawable
)(dpy
, pdp
);
640 pdraw
->private = NULL
;
646 static __DRIdrawable
*
647 driGetDrawable(__DRInativeDisplay
*dpy
, __DRIid draw
, void *screenPrivate
)
649 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
652 ** Make sure this routine returns NULL if the drawable is not bound
653 ** to a direct rendering context!
655 return __driFindDrawable(psp
->drawHash
, draw
);
659 driDestroyDrawable(__DRInativeDisplay
*dpy
, void *drawablePrivate
)
661 __DRIdrawablePrivate
*pdp
= (__DRIdrawablePrivate
*) drawablePrivate
;
662 __DRIscreenPrivate
*psp
;
666 psp
= pdp
->driScreenPriv
;
668 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
669 if ((*dri_interface
->windowExists
)(dpy
, pdp
->draw
))
670 (void)(*dri_interface
->destroyDrawable
)(dpy
, scrn
, pdp
->draw
);
671 drmHashDelete(psp
->drawHash
, pdp
->draw
);
672 if (pdp
->pClipRects
) {
673 _mesa_free(pdp
->pClipRects
);
674 pdp
->pClipRects
= NULL
;
676 if (pdp
->pBackClipRects
) {
677 _mesa_free(pdp
->pBackClipRects
);
678 pdp
->pBackClipRects
= NULL
;
687 /*****************************************************************/
688 /** \name Context handling functions */
689 /*****************************************************************/
693 * Destroy the per-context private information.
695 * \param dpy the display handle.
696 * \param scrn the screen number.
697 * \param contextPrivate opaque pointer to the per-drawable private info.
700 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
701 * drmDestroyContext(), and finally frees \p contextPrivate.
704 driDestroyContext(__DRInativeDisplay
*dpy
, int scrn
, void *contextPrivate
)
706 __DRIcontextPrivate
*pcp
= (__DRIcontextPrivate
*) contextPrivate
;
709 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
710 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
711 (void) (*dri_interface
->destroyContext
)(dpy
, scrn
, pcp
->contextID
);
718 * Create the per-drawable private driver information.
720 * \param dpy The display handle.
721 * \param modes Mode used to create the new context.
722 * \param render_type Type of rendering target. \c GLX_RGBA is the only
723 * type likely to ever be supported for direct-rendering.
724 * \param sharedPrivate The shared context dependent methods or \c NULL if
726 * \param pctx DRI context to receive the context dependent methods.
728 * \returns An opaque pointer to the per-context private information on
729 * success, or \c NULL on failure.
732 * This function allocates and fills a __DRIcontextPrivateRec structure. It
733 * performs some device independent initialization and passes all the
734 * relevent information to __DriverAPIRec::CreateContext to create the
739 driCreateNewContext(__DRInativeDisplay
*dpy
, const __GLcontextModes
*modes
,
740 int render_type
, void *sharedPrivate
, __DRIcontext
*pctx
)
742 __DRIscreen
*pDRIScreen
;
743 __DRIcontextPrivate
*pcp
;
744 __DRIcontextPrivate
*pshare
= (__DRIcontextPrivate
*) sharedPrivate
;
745 __DRIscreenPrivate
*psp
;
746 void * const shareCtx
= (pshare
!= NULL
) ? pshare
->driverPrivate
: NULL
;
748 pDRIScreen
= (*dri_interface
->getScreen
)(dpy
, modes
->screen
);
749 if ( (pDRIScreen
== NULL
) || (pDRIScreen
->private == NULL
) ) {
754 psp
= (__DRIscreenPrivate
*)pDRIScreen
->private;
756 pcp
= (__DRIcontextPrivate
*)_mesa_malloc(sizeof(__DRIcontextPrivate
));
761 if (! (*dri_interface
->createContext
)(dpy
, modes
->screen
, modes
->fbconfigID
,
762 &pcp
->contextID
, &pcp
->hHWContext
)) {
768 pcp
->driScreenPriv
= psp
;
769 pcp
->driDrawablePriv
= NULL
;
771 /* When the first context is created for a screen, initialize a "dummy"
775 if (!psp
->dummyContextPriv
.driScreenPriv
) {
776 psp
->dummyContextPriv
.contextID
= 0;
777 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
778 psp
->dummyContextPriv
.driScreenPriv
= psp
;
779 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
780 psp
->dummyContextPriv
.driverPrivate
= NULL
;
781 /* No other fields should be used! */
784 pctx
->destroyContext
= driDestroyContext
;
785 pctx
->bindContext
= driBindContext
;
786 pctx
->unbindContext
= driUnbindContext
;
788 if ( !(*psp
->DriverAPI
.CreateContext
)(modes
, pcp
, shareCtx
) ) {
789 (void) (*dri_interface
->destroyContext
)(dpy
, modes
->screen
,
795 __driGarbageCollectDrawables(pcp
->driScreenPriv
->drawHash
);
802 /*****************************************************************/
803 /** \name Screen handling functions */
804 /*****************************************************************/
808 * Destroy the per-screen private information.
810 * \param dpy the display handle.
811 * \param scrn the screen number.
812 * \param screenPrivate opaque pointer to the per-screen private information.
815 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
816 * drmClose(), and finally frees \p screenPrivate.
818 static void driDestroyScreen(__DRInativeDisplay
*dpy
, int scrn
, void *screenPrivate
)
820 __DRIscreenPrivate
*psp
= (__DRIscreenPrivate
*) screenPrivate
;
823 /* No interaction with the X-server is possible at this point. This
824 * routine is called after XCloseDisplay, so there is no protocol
825 * stream open to the X-server anymore.
828 if (psp
->DriverAPI
.DestroyScreen
)
829 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
831 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
832 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
833 _mesa_free(psp
->pDevPriv
);
834 (void)drmClose(psp
->fd
);
835 if ( psp
->modes
!= NULL
) {
836 (*dri_interface
->destroyContextModes
)( psp
->modes
);
844 * Utility function used to create a new driver-private screen structure.
846 * \param dpy Display pointer
847 * \param scrn Index of the screen
848 * \param psc DRI screen data (not driver private)
849 * \param modes Linked list of known display modes. This list is, at a
850 * minimum, a list of modes based on the current display mode.
851 * These roughly match the set of available X11 visuals, but it
852 * need not be limited to X11! The calling libGL should create
853 * a list that will inform the driver of the current display
854 * mode (i.e., color buffer depth, depth buffer depth, etc.).
855 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
857 * \param dri_version Version of the "server-side" DRI.
858 * \param drm_version Version of the kernel DRM.
859 * \param frame_buffer Data describing the location and layout of the
861 * \param pSAREA Pointer the the SAREA.
862 * \param fd Device handle for the DRM.
863 * \param internal_api_version Version of the internal interface between the
865 * \param driverAPI Driver API functions used by other routines in dri_util.c.
868 * There is no need to check the minimum API version in this function. Since
869 * the \c __driCreateNewScreen function is versioned, it is impossible for a
870 * loader that is too old to even load this driver.
873 __driUtilCreateNewScreen(__DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
874 __GLcontextModes
* modes
,
875 const __DRIversion
* ddx_version
,
876 const __DRIversion
* dri_version
,
877 const __DRIversion
* drm_version
,
878 const __DRIframebuffer
* frame_buffer
,
881 int internal_api_version
,
882 const struct __DriverAPIRec
*driverAPI
)
884 __DRIscreenPrivate
*psp
;
887 api_ver
= internal_api_version
;
889 psp
= (__DRIscreenPrivate
*)_mesa_malloc(sizeof(__DRIscreenPrivate
));
894 /* Create the hash table */
895 psp
->drawHash
= drmHashCreate();
896 if ( psp
->drawHash
== NULL
) {
907 ** NOT_DONE: This is used by the X server to detect when the client
908 ** has died while holding the drawable lock. The client sets the
909 ** drawable lock to this value.
913 psp
->drmMajor
= drm_version
->major
;
914 psp
->drmMinor
= drm_version
->minor
;
915 psp
->drmPatch
= drm_version
->patch
;
916 psp
->ddxMajor
= ddx_version
->major
;
917 psp
->ddxMinor
= ddx_version
->minor
;
918 psp
->ddxPatch
= ddx_version
->patch
;
919 psp
->driMajor
= dri_version
->major
;
920 psp
->driMinor
= dri_version
->minor
;
921 psp
->driPatch
= dri_version
->patch
;
923 /* install driver's callback functions */
924 memcpy( &psp
->DriverAPI
, driverAPI
, sizeof(struct __DriverAPIRec
) );
926 psp
->pSAREA
= pSAREA
;
928 psp
->pFB
= frame_buffer
->base
;
929 psp
->fbSize
= frame_buffer
->size
;
930 psp
->fbStride
= frame_buffer
->stride
;
931 psp
->fbWidth
= frame_buffer
->width
;
932 psp
->fbHeight
= frame_buffer
->height
;
933 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
934 psp
->pDevPriv
= frame_buffer
->dev_priv
;
935 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
940 ** Do not init dummy context here; actual initialization will be
941 ** done when the first DRI context is created. Init screen priv ptr
942 ** to NULL to let CreateContext routine that it needs to be inited.
944 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
946 psc
->destroyScreen
= driDestroyScreen
;
947 psc
->createNewDrawable
= driCreateNewDrawable
;
948 psc
->getDrawable
= driGetDrawable
;
949 psc
->getMSC
= driGetMSC
;
950 psc
->createNewContext
= driCreateNewContext
;
952 if ( (psp
->DriverAPI
.InitDriver
!= NULL
)
953 && !(*psp
->DriverAPI
.InitDriver
)(psp
) ) {
964 * Compare the current GLX API version with a driver supplied required version.
966 * The minimum required version is compared with the API version exported by
967 * the \c __glXGetInternalVersion function (in libGL.so).
969 * \param required_version Minimum required internal GLX API version.
970 * \return A tri-value return, as from strcmp is returned. A value less
971 * than, equal to, or greater than zero will be returned if the
972 * internal GLX API version is less than, equal to, or greater
973 * than \c required_version.
975 * \sa __glXGetInternalVersion().
977 int driCompareGLXAPIVersion( GLint required_version
)
979 if ( api_ver
> required_version
) {
982 else if ( api_ver
== required_version
) {
991 driQueryFrameTracking( __DRInativeDisplay
* dpy
, void * priv
,
992 int64_t * sbc
, int64_t * missedFrames
,
993 float * lastMissedUsage
, float * usage
)
998 __DRIdrawablePrivate
* dpriv
= (__DRIdrawablePrivate
*) priv
;
1001 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1002 if ( status
== 0 ) {
1003 *sbc
= sInfo
.swap_count
;
1004 *missedFrames
= sInfo
.swap_missed_count
;
1005 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1007 (*dri_interface
->getUST
)( & ust
);
1008 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1016 * Calculate amount of swap interval used between GLX buffer swaps.
1018 * The usage value, on the range [0,max], is the fraction of total swap
1019 * interval time used between GLX buffer swaps is calculated.
1021 * \f$p = t_d / (i * t_r)\f$
1023 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1024 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1025 * required for a single vertical refresh period (as returned by \c
1026 * glXGetMscRateOML).
1028 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1031 * \param dPriv Pointer to the private drawable structure.
1032 * \return If less than a single swap interval time period was required
1033 * between GLX buffer swaps, a number greater than 0 and less than
1034 * 1.0 is returned. If exactly one swap interval time period is
1035 * required, 1.0 is returned, and if more than one is required then
1036 * a number greater than 1.0 will be returned.
1038 * \sa glXSwapIntervalSGI glXGetMscRateOML
1040 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1041 * be possible to cache the sync rate?
1044 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1045 int64_t current_ust
)
1053 if ( (*dri_interface
->getMSCRate
)( dPriv
->display
, dPriv
->draw
, &n
, &d
) ) {
1054 interval
= (dPriv
->pdraw
->swap_interval
!= 0)
1055 ? dPriv
->pdraw
->swap_interval
: 1;
1058 /* We want to calculate
1059 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1060 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1061 * dPriv->swap_ust. interval has already been calculated.
1063 * The only tricky part is us_per_refresh. us_per_refresh is
1064 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1065 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1066 * the denominator of the final calculation, we calculate
1067 * (interval * 1000000 * d) and move n into the numerator.
1070 usage
= (current_ust
- last_swap_ust
);
1072 usage
/= (interval
* d
);