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"
36 #ifndef GLX_OML_sync_control
37 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC
) (__DRIdrawable
*drawable
, int32_t *numerator
, int32_t *denominator
);
41 * This is just a token extension used to signal that the driver
42 * supports setting a read drawable.
44 const __DRIextension driReadDrawableExtension
= {
45 __DRI_READ_DRAWABLE
, __DRI_READ_DRAWABLE_VERSION
49 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
52 * Is called from the drivers.
54 * \param f \c printf like format string.
57 __driUtilMessage(const char *f
, ...)
61 if (getenv("LIBGL_DEBUG")) {
62 fprintf(stderr
, "libGL error: \n");
64 vfprintf(stderr
, f
, args
);
66 fprintf(stderr
, "\n");
71 driIntersectArea( drm_clip_rect_t rect1
, drm_clip_rect_t rect2
)
73 if (rect2
.x1
> rect1
.x1
) rect1
.x1
= rect2
.x1
;
74 if (rect2
.x2
< rect1
.x2
) rect1
.x2
= rect2
.x2
;
75 if (rect2
.y1
> rect1
.y1
) rect1
.y1
= rect2
.y1
;
76 if (rect2
.y2
< rect1
.y2
) rect1
.y2
= rect2
.y2
;
78 if (rect1
.x1
> rect1
.x2
|| rect1
.y1
> rect1
.y2
) return 0;
80 return (rect1
.x2
- rect1
.x1
) * (rect1
.y2
- rect1
.y1
);
83 /*****************************************************************/
84 /** \name Context (un)binding functions */
85 /*****************************************************************/
91 * \param scrn the screen.
94 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
97 * This function calls __DriverAPIRec::UnbindContext, and then decrements
98 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
101 * While casting the opaque private pointers associated with the parameters
102 * into their respective real types it also assures they are not \c NULL.
104 static int driUnbindContext(__DRIcontext
*pcp
)
111 ** Assume error checking is done properly in glXMakeCurrent before
112 ** calling driUnbindContext.
118 psp
= pcp
->driScreenPriv
;
119 pdp
= pcp
->driDrawablePriv
;
120 prp
= pcp
->driReadablePriv
;
122 /* Let driver unbind drawable from context */
123 (*psp
->DriverAPI
.UnbindContext
)(pcp
);
125 if (pdp
->refcount
== 0) {
133 if (prp
->refcount
== 0) {
142 /* XXX this is disabled so that if we call SwapBuffers on an unbound
143 * window we can determine the last context bound to the window and
144 * use that context's lock. (BrianP, 2-Dec-2000)
147 /* Unbind the drawable */
148 pcp
->driDrawablePriv
= NULL
;
149 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
157 * This function takes both a read buffer and a draw buffer. This is needed
158 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
161 static int driBindContext(__DRIcontext
*pcp
,
165 __DRIscreenPrivate
*psp
= pcp
->driScreenPriv
;
168 ** Assume error checking is done properly in glXMakeCurrent before
169 ** calling driBindContext.
172 if (pcp
== NULL
|| pdp
== None
|| prp
== None
)
175 /* Bind the drawable to the context */
176 pcp
->driDrawablePriv
= pdp
;
177 pcp
->driReadablePriv
= prp
;
178 pdp
->driContextPriv
= pcp
;
185 ** Now that we have a context associated with this drawable, we can
186 ** initialize the drawable information if has not been done before.
189 if (psp
->dri2
.enabled
) {
190 __driParseEvents(pcp
, pdp
);
191 __driParseEvents(pcp
, prp
);
193 if (!pdp
->pStamp
|| *pdp
->pStamp
!= pdp
->lastStamp
) {
194 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
195 __driUtilUpdateDrawableInfo(pdp
);
196 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
199 if ((pdp
!= prp
) && (!prp
->pStamp
|| *prp
->pStamp
!= prp
->lastStamp
)) {
200 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
201 __driUtilUpdateDrawableInfo(prp
);
202 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
206 /* Call device-specific MakeCurrent */
207 (*psp
->DriverAPI
.MakeCurrent
)(pcp
, pdp
, prp
);
215 /*****************************************************************/
216 /** \name Drawable handling functions */
217 /*****************************************************************/
221 * Update private drawable information.
223 * \param pdp pointer to the private drawable information to update.
225 * This function basically updates the __DRIdrawablePrivate struct's
226 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
227 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
228 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
229 * the values are different that means we have to update the clipping
233 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate
*pdp
)
235 __DRIscreenPrivate
*psp
= pdp
->driScreenPriv
;
236 __DRIcontextPrivate
*pcp
= pdp
->driContextPriv
;
239 || ((pdp
!= pcp
->driDrawablePriv
) && (pdp
!= pcp
->driReadablePriv
))) {
241 * ...but we must ignore it. There can be many contexts bound to a
246 if (pdp
->pClipRects
) {
247 _mesa_free(pdp
->pClipRects
);
248 pdp
->pClipRects
= NULL
;
251 if (pdp
->pBackClipRects
) {
252 _mesa_free(pdp
->pBackClipRects
);
253 pdp
->pBackClipRects
= NULL
;
256 DRM_SPINUNLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
258 if (! (*psp
->getDrawableInfo
->getDrawableInfo
)(pdp
,
259 &pdp
->index
, &pdp
->lastStamp
,
260 &pdp
->x
, &pdp
->y
, &pdp
->w
, &pdp
->h
,
261 &pdp
->numClipRects
, &pdp
->pClipRects
,
264 &pdp
->numBackClipRects
,
265 &pdp
->pBackClipRects
,
266 pdp
->loaderPrivate
)) {
267 /* Error -- eg the window may have been destroyed. Keep going
270 pdp
->pStamp
= &pdp
->lastStamp
; /* prevent endless loop */
271 pdp
->numClipRects
= 0;
272 pdp
->pClipRects
= NULL
;
273 pdp
->numBackClipRects
= 0;
274 pdp
->pBackClipRects
= NULL
;
277 pdp
->pStamp
= &(psp
->pSAREA
->drawableTable
[pdp
->index
].stamp
);
279 DRM_SPINLOCK(&psp
->pSAREA
->drawable_lock
, psp
->drawLockID
);
284 __driParseEvents(__DRIcontextPrivate
*pcp
, __DRIdrawablePrivate
*pdp
)
286 __DRIscreenPrivate
*psp
= pcp
->driScreenPriv
;
287 __DRIDrawableConfigEvent
*dc
, *last_dc
;
288 __DRIBufferAttachEvent
*ba
, *last_ba
;
289 unsigned int tail
, mask
, *p
, end
, total
, size
, changed
;
293 /* Check for wraparound. */
294 if (psp
->dri2
.buffer
->prealloc
- pdp
->dri2
.tail
> psp
->dri2
.buffer
->size
) {
295 /* If prealloc overlaps into what we just parsed, the
296 * server overwrote it and we have to reset our tail
298 DRM_UNLOCK(psp
->fd
, psp
->lock
, pcp
->hHWContext
);
299 (*psp
->dri2
.loader
->reemitDrawableInfo
)(pdp
, &pdp
->dri2
.tail
,
301 DRM_LIGHT_LOCK(psp
->fd
, psp
->lock
, pcp
->hHWContext
);
304 total
= psp
->dri2
.buffer
->head
- pdp
->dri2
.tail
;
305 mask
= psp
->dri2
.buffer
->size
- 1;
306 end
= psp
->dri2
.buffer
->head
;
307 data
= psp
->dri2
.buffer
->data
;
313 for (tail
= pdp
->dri2
.tail
; tail
!= end
; tail
+= size
) {
314 p
= (unsigned int *) (data
+ (tail
& mask
));
315 size
= DRI2_EVENT_SIZE(*p
);
316 if (size
> total
|| (tail
& mask
) + size
> psp
->dri2
.buffer
->size
) {
317 /* illegal data, bail out. */
318 fprintf(stderr
, "illegal event size\n");
322 switch (DRI2_EVENT_TYPE(*p
)) {
323 case DRI2_EVENT_DRAWABLE_CONFIG
:
324 dc
= (__DRIDrawableConfigEvent
*) p
;
325 if (dc
->drawable
== pdp
->dri2
.drawable_id
)
329 case DRI2_EVENT_BUFFER_ATTACH
:
330 ba
= (__DRIBufferAttachEvent
*) p
;
331 if (ba
->drawable
== pdp
->dri2
.drawable_id
&&
332 ba
->buffer
.attachment
== DRI_DRAWABLE_BUFFER_FRONT_LEFT
)
339 if (pdp
->w
!= last_dc
->width
|| pdp
->h
!= last_dc
->height
)
344 pdp
->w
= last_dc
->width
;
345 pdp
->h
= last_dc
->height
;
349 pdp
->numBackClipRects
= 1;
350 pdp
->pBackClipRects
[0].x1
= 0;
351 pdp
->pBackClipRects
[0].y1
= 0;
352 pdp
->pBackClipRects
[0].x2
= pdp
->w
;
353 pdp
->pBackClipRects
[0].y2
= pdp
->h
;
355 pdp
->numClipRects
= last_dc
->num_rects
;
356 _mesa_free(pdp
->pClipRects
);
357 rect_size
= last_dc
->num_rects
* sizeof last_dc
->rects
[0];
358 pdp
->pClipRects
= _mesa_malloc(rect_size
);
359 memcpy(pdp
->pClipRects
, last_dc
->rects
, rect_size
);
362 /* We only care about the most recent drawable config. */
363 if (last_dc
&& changed
)
364 (*psp
->DriverAPI
.HandleDrawableConfig
)(pdp
, pcp
, last_dc
);
366 /* Front buffer attachments are special, they typically mean that
367 * we're rendering to a redirected window (or a child window of a
368 * redirected window) and that it got resized. Resizing the root
369 * window on randr events is a special case of this. Other causes
370 * may be a window transitioning between redirected and
371 * non-redirected, or a window getting reparented between parents
372 * with different window pixmaps (eg two redirected windows).
373 * These events are special in that the X server allocates the
374 * buffer and that the buffer may be shared by other child
375 * windows. When our window share the window pixmap with its
376 * parent, drawable config events doesn't affect the front buffer.
377 * We only care about the last such event in the buffer; in fact,
378 * older events will refer to invalid buffer objects.*/
380 (*psp
->DriverAPI
.HandleBufferAttach
)(pdp
, pcp
, last_ba
);
382 /* If there was a drawable config event in the buffer and it
383 * changed the size of the window, all buffer auxillary buffer
384 * attachments prior to that are invalid (as opposed to the front
385 * buffer case discussed above). In that case we can start
386 * looking for buffer attachment after the last drawable config
387 * event. If there is no drawable config event in this batch of
388 * events, we have to assume that the last batch might have had
389 * one and process all buffer attach events.*/
390 if (last_dc
&& changed
)
391 tail
= (unsigned char *) last_dc
- data
;
393 tail
= pdp
->dri2
.tail
;
395 for ( ; tail
!= end
; tail
+= size
) {
396 ba
= (__DRIBufferAttachEvent
*) (data
+ (tail
& mask
));
397 size
= DRI2_EVENT_SIZE(ba
->event_header
);
399 if (DRI2_EVENT_TYPE(ba
->event_header
) != DRI2_EVENT_BUFFER_ATTACH
)
401 if (ba
->drawable
!= pdp
->dri2
.drawable_id
)
406 (*psp
->DriverAPI
.HandleBufferAttach
)(pdp
, pcp
, ba
);
410 pdp
->dri2
.tail
= tail
;
412 return changed
|| last_ba
;
417 /*****************************************************************/
418 /** \name GLX callbacks */
419 /*****************************************************************/
422 static void driReportDamage(__DRIdrawable
*pdp
,
423 struct drm_clip_rect
*pClipRects
, int numClipRects
)
425 __DRIscreen
*psp
= pdp
->driScreenPriv
;
427 /* Check that we actually have the new damage report method */
428 if (psp
->dri2
.enabled
) {
429 (*psp
->dri2
.loader
->postDamage
)(pdp
,
433 } else if (psp
->damage
) {
434 /* Report the damage. Currently, all our drivers draw
435 * directly to the front buffer, so we report the damage there
436 * rather than to the backing storein (if any).
438 (*psp
->damage
->reportDamage
)(pdp
,
440 pClipRects
, numClipRects
,
441 GL_TRUE
, pdp
->loaderPrivate
);
449 * \param drawablePrivate opaque pointer to the per-drawable private info.
452 * This function calls __DRIdrawablePrivate::swapBuffers.
454 * Is called directly from glXSwapBuffers().
456 static void driSwapBuffers(__DRIdrawable
*dPriv
)
458 __DRIscreen
*psp
= dPriv
->driScreenPriv
;
460 if (!dPriv
->numClipRects
)
463 psp
->DriverAPI
.SwapBuffers(dPriv
);
465 driReportDamage(dPriv
, dPriv
->pClipRects
, dPriv
->numClipRects
);
468 static int driDrawableGetMSC( __DRIscreen
*sPriv
, __DRIdrawable
*dPriv
,
471 return sPriv
->DriverAPI
.GetDrawableMSC(sPriv
, dPriv
, msc
);
474 static int driWaitForMSC(__DRIdrawable
*dPriv
, int64_t target_msc
,
475 int64_t divisor
, int64_t remainder
,
476 int64_t * msc
, int64_t * sbc
)
482 status
= dPriv
->driScreenPriv
->DriverAPI
.WaitForMSC( dPriv
, target_msc
,
486 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
487 * is supported but GLX_OML_sync_control is not. Therefore, don't return
488 * an error value if GetSwapInfo() is not implemented.
491 && dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo
) {
492 status
= dPriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dPriv
, & sInfo
);
493 *sbc
= sInfo
.swap_count
;
499 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension
= {
500 { __DRI_MEDIA_STREAM_COUNTER
, __DRI_MEDIA_STREAM_COUNTER_VERSION
},
505 static void driCopySubBuffer(__DRIdrawable
*dPriv
,
506 int x
, int y
, int w
, int h
)
508 drm_clip_rect_t rect
;
510 dPriv
->driScreenPriv
->DriverAPI
.CopySubBuffer(dPriv
, x
, y
, w
, h
);
513 rect
.y1
= dPriv
->h
- y
- h
;
515 rect
.y2
= rect
.y1
+ h
;
516 driReportDamage(dPriv
, &rect
, 1);
519 const __DRIcopySubBufferExtension driCopySubBufferExtension
= {
520 { __DRI_COPY_SUB_BUFFER
, __DRI_COPY_SUB_BUFFER_VERSION
},
524 static void driSetSwapInterval(__DRIdrawable
*dPriv
, unsigned int interval
)
526 dPriv
->swap_interval
= interval
;
529 static unsigned int driGetSwapInterval(__DRIdrawable
*dPriv
)
531 return dPriv
->swap_interval
;
534 const __DRIswapControlExtension driSwapControlExtension
= {
535 { __DRI_SWAP_CONTROL
, __DRI_SWAP_CONTROL_VERSION
},
542 * This is called via __DRIscreenRec's createNewDrawable pointer.
544 static __DRIdrawable
*
545 driCreateNewDrawable(__DRIscreen
*psp
, const __DRIconfig
*config
,
546 drm_drawable_t hwDrawable
, int renderType
,
547 const int *attrs
, void *data
)
551 /* Since pbuffers are not yet supported, no drawable attributes are
556 pdp
= _mesa_malloc(sizeof *pdp
);
561 pdp
->loaderPrivate
= data
;
562 pdp
->hHWDrawable
= hwDrawable
;
571 pdp
->numClipRects
= 0;
572 pdp
->numBackClipRects
= 0;
573 pdp
->pClipRects
= NULL
;
574 pdp
->pBackClipRects
= NULL
;
578 pdp
->driScreenPriv
= psp
;
579 pdp
->driContextPriv
= &psp
->dummyContextPriv
;
581 if (!(*psp
->DriverAPI
.CreateBuffer
)(psp
, pdp
, &config
->modes
,
582 renderType
== GLX_PIXMAP_BIT
)) {
589 /* This special default value is replaced with the configured
590 * default value when the drawable is first bound to a direct
593 pdp
->swap_interval
= (unsigned)-1;
598 static __DRIdrawable
*
599 dri2CreateNewDrawable(__DRIscreen
*screen
, const __DRIconfig
*config
,
600 unsigned int drawable_id
, unsigned int head
, void *data
)
602 __DRIdrawable
*pdraw
;
604 pdraw
= driCreateNewDrawable(screen
, config
, 0, 0, NULL
, data
);
606 pdraw
->dri2
.drawable_id
= drawable_id
;
607 pdraw
->dri2
.tail
= head
;
608 pdraw
->pBackClipRects
= _mesa_malloc(sizeof *pdraw
->pBackClipRects
);
615 driDestroyDrawable(__DRIdrawable
*pdp
)
617 __DRIscreenPrivate
*psp
;
620 psp
= pdp
->driScreenPriv
;
621 (*psp
->DriverAPI
.DestroyBuffer
)(pdp
);
622 if (pdp
->pClipRects
) {
623 _mesa_free(pdp
->pClipRects
);
624 pdp
->pClipRects
= NULL
;
626 if (pdp
->pBackClipRects
) {
627 _mesa_free(pdp
->pBackClipRects
);
628 pdp
->pBackClipRects
= NULL
;
637 /*****************************************************************/
638 /** \name Context handling functions */
639 /*****************************************************************/
643 * Destroy the per-context private information.
645 * \param contextPrivate opaque pointer to the per-drawable private info.
648 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
649 * drmDestroyContext(), and finally frees \p contextPrivate.
652 driDestroyContext(__DRIcontext
*pcp
)
655 (*pcp
->driScreenPriv
->DriverAPI
.DestroyContext
)(pcp
);
662 * Create the per-drawable private driver information.
664 * \param dpy The display handle.
665 * \param modes Mode used to create the new context.
666 * \param render_type Type of rendering target. \c GLX_RGBA is the only
667 * type likely to ever be supported for direct-rendering.
668 * \param shared The shared context dependent methods or \c NULL if
670 * \param pctx DRI context to receive the context dependent methods.
672 * \returns An opaque pointer to the per-context private information on
673 * success, or \c NULL on failure.
676 * This function allocates and fills a __DRIcontextPrivateRec structure. It
677 * performs some device independent initialization and passes all the
678 * relevent information to __DriverAPIRec::CreateContext to create the
682 static __DRIcontext
*
683 driCreateNewContext(__DRIscreen
*psp
, const __DRIconfig
*config
,
684 int render_type
, __DRIcontext
*shared
,
685 drm_context_t hwContext
, void *data
)
688 void * const shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
690 pcp
= _mesa_malloc(sizeof *pcp
);
694 pcp
->driScreenPriv
= psp
;
695 pcp
->driDrawablePriv
= NULL
;
697 /* When the first context is created for a screen, initialize a "dummy"
701 if (!psp
->dri2
.enabled
&& !psp
->dummyContextPriv
.driScreenPriv
) {
702 psp
->dummyContextPriv
.hHWContext
= psp
->pSAREA
->dummy_context
;
703 psp
->dummyContextPriv
.driScreenPriv
= psp
;
704 psp
->dummyContextPriv
.driDrawablePriv
= NULL
;
705 psp
->dummyContextPriv
.driverPrivate
= NULL
;
706 /* No other fields should be used! */
709 pcp
->hHWContext
= hwContext
;
711 if ( !(*psp
->DriverAPI
.CreateContext
)(&config
->modes
, pcp
, shareCtx
) ) {
719 static __DRIcontext
*
720 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
721 __DRIcontext
*shared
, void *data
)
723 drm_context_t hwContext
;
726 /* DRI2 doesn't use kernel with context IDs, we just need an ID that's
727 * different from the kernel context ID to make drmLock() happy. */
730 hwContext
= screen
->dri2
.lock
->next_id
;
731 DRM_CAS(&screen
->dri2
.lock
->next_id
, hwContext
, hwContext
+ 1, ret
);
734 return driCreateNewContext(screen
, config
, 0, shared
, hwContext
, data
);
738 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
746 /*****************************************************************/
747 /** \name Screen handling functions */
748 /*****************************************************************/
752 * Destroy the per-screen private information.
754 * \param dpy the display handle.
755 * \param scrn the screen number.
756 * \param screenPrivate opaque pointer to the per-screen private information.
759 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
760 * drmClose(), and finally frees \p screenPrivate.
762 static void driDestroyScreen(__DRIscreen
*psp
)
765 /* No interaction with the X-server is possible at this point. This
766 * routine is called after XCloseDisplay, so there is no protocol
767 * stream open to the X-server anymore.
770 if (psp
->DriverAPI
.DestroyScreen
)
771 (*psp
->DriverAPI
.DestroyScreen
)(psp
);
773 if (psp
->dri2
.enabled
) {
775 drmBOUnmap(psp
->fd
, &psp
->dri2
.sareaBO
);
776 drmBOUnreference(psp
->fd
, &psp
->dri2
.sareaBO
);
779 (void)drmUnmap((drmAddress
)psp
->pSAREA
, SAREA_MAX
);
780 (void)drmUnmap((drmAddress
)psp
->pFB
, psp
->fbSize
);
781 (void)drmCloseOnce(psp
->fd
);
789 setupLoaderExtensions(__DRIscreen
*psp
,
790 const __DRIextension
**extensions
)
794 for (i
= 0; extensions
[i
]; i
++) {
795 if (strcmp(extensions
[i
]->name
, __DRI_GET_DRAWABLE_INFO
) == 0)
796 psp
->getDrawableInfo
= (__DRIgetDrawableInfoExtension
*) extensions
[i
];
797 if (strcmp(extensions
[i
]->name
, __DRI_DAMAGE
) == 0)
798 psp
->damage
= (__DRIdamageExtension
*) extensions
[i
];
799 if (strcmp(extensions
[i
]->name
, __DRI_SYSTEM_TIME
) == 0)
800 psp
->systemTime
= (__DRIsystemTimeExtension
*) extensions
[i
];
801 if (strcmp(extensions
[i
]->name
, __DRI_LOADER
) == 0)
802 psp
->dri2
.loader
= (__DRIloaderExtension
*) extensions
[i
];
807 * This is the bootstrap function for the driver. libGL supplies all of the
808 * requisite information about the system, and the driver initializes itself.
809 * This routine also fills in the linked list pointed to by \c driver_modes
810 * with the \c __GLcontextModes that the driver can support for windows or
813 * \param scrn Index of the screen
814 * \param psc DRI screen data (not driver private)
815 * \param modes Linked list of known display modes. This list is, at a
816 * minimum, a list of modes based on the current display mode.
817 * These roughly match the set of available X11 visuals, but it
818 * need not be limited to X11! The calling libGL should create
819 * a list that will inform the driver of the current display
820 * mode (i.e., color buffer depth, depth buffer depth, etc.).
821 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
823 * \param dri_version Version of the "server-side" DRI.
824 * \param drm_version Version of the kernel DRM.
825 * \param frame_buffer Data describing the location and layout of the
827 * \param pSAREA Pointer the the SAREA.
828 * \param fd Device handle for the DRM.
829 * \param internal_api_version Version of the internal interface between the
831 * \param driverAPI Driver API functions used by other routines in dri_util.c.
833 * \note There is no need to check the minimum API version in this
834 * function. Since the name of this function is versioned, it is
835 * impossible for a loader that is too old to even load this driver.
838 driCreateNewScreen(int scrn
,
839 const __DRIversion
*ddx_version
,
840 const __DRIversion
*dri_version
,
841 const __DRIversion
*drm_version
,
842 const __DRIframebuffer
*frame_buffer
,
843 drmAddress pSAREA
, int fd
,
844 const __DRIextension
**extensions
,
845 const __DRIconfig
***driver_modes
,
848 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
851 psp
= _mesa_malloc(sizeof *psp
);
855 setupLoaderExtensions(psp
, extensions
);
858 ** NOT_DONE: This is used by the X server to detect when the client
859 ** has died while holding the drawable lock. The client sets the
860 ** drawable lock to this value.
864 psp
->drm_version
= *drm_version
;
865 psp
->ddx_version
= *ddx_version
;
866 psp
->dri_version
= *dri_version
;
868 psp
->pSAREA
= pSAREA
;
869 psp
->lock
= (drmLock
*) &psp
->pSAREA
->lock
;
871 psp
->pFB
= frame_buffer
->base
;
872 psp
->fbSize
= frame_buffer
->size
;
873 psp
->fbStride
= frame_buffer
->stride
;
874 psp
->fbWidth
= frame_buffer
->width
;
875 psp
->fbHeight
= frame_buffer
->height
;
876 psp
->devPrivSize
= frame_buffer
->dev_priv_size
;
877 psp
->pDevPriv
= frame_buffer
->dev_priv
;
878 psp
->fbBPP
= psp
->fbStride
* 8 / frame_buffer
->width
;
880 psp
->extensions
= emptyExtensionList
;
883 psp
->dri2
.enabled
= GL_FALSE
;
886 ** Do not init dummy context here; actual initialization will be
887 ** done when the first DRI context is created. Init screen priv ptr
888 ** to NULL to let CreateContext routine that it needs to be inited.
890 psp
->dummyContextPriv
.driScreenPriv
= NULL
;
892 psp
->DriverAPI
= driDriverAPI
;
894 *driver_modes
= driDriverAPI
.InitScreen(psp
);
895 if (*driver_modes
== NULL
) {
905 dri2CreateNewScreen(int scrn
, int fd
, unsigned int sarea_handle
,
906 const __DRIextension
**extensions
,
907 const __DRIconfig
***driver_configs
, void *data
)
910 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
913 drmVersionPtr version
;
915 if (driDriverAPI
.InitScreen2
== NULL
)
918 psp
= _mesa_malloc(sizeof(*psp
));
922 setupLoaderExtensions(psp
, extensions
);
924 version
= drmGetVersion(fd
);
926 psp
->drm_version
.major
= version
->version_major
;
927 psp
->drm_version
.minor
= version
->version_minor
;
928 psp
->drm_version
.patch
= version
->version_patchlevel
;
929 drmFreeVersion(version
);
932 psp
->extensions
= emptyExtensionList
;
935 psp
->dri2
.enabled
= GL_TRUE
;
937 if (drmBOReference(psp
->fd
, sarea_handle
, &psp
->dri2
.sareaBO
)) {
938 fprintf(stderr
, "Failed to reference DRI2 sarea BO\n");
943 if (drmBOMap(psp
->fd
, &psp
->dri2
.sareaBO
,
944 DRM_BO_FLAG_READ
| DRM_BO_FLAG_WRITE
, 0, &psp
->dri2
.sarea
)) {
945 drmBOUnreference(psp
->fd
, &psp
->dri2
.sareaBO
);
951 while (DRI2_SAREA_BLOCK_TYPE(*p
)) {
952 switch (DRI2_SAREA_BLOCK_TYPE(*p
)) {
953 case DRI2_SAREA_BLOCK_LOCK
:
954 psp
->dri2
.lock
= (__DRILock
*) p
;
956 case DRI2_SAREA_BLOCK_EVENT_BUFFER
:
957 psp
->dri2
.buffer
= (__DRIEventBuffer
*) p
;
960 p
= DRI2_SAREA_BLOCK_NEXT(p
);
963 psp
->lock
= (drmLock
*) &psp
->dri2
.lock
->lock
;
965 psp
->DriverAPI
= driDriverAPI
;
966 *driver_configs
= driDriverAPI
.InitScreen2(psp
);
967 if (*driver_configs
== NULL
) {
968 drmBOUnmap(psp
->fd
, &psp
->dri2
.sareaBO
);
969 drmBOUnreference(psp
->fd
, &psp
->dri2
.sareaBO
);
974 psp
->DriverAPI
= driDriverAPI
;
982 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
984 return psp
->extensions
;
987 const __DRIlegacyExtension driLegacyExtension
= {
988 { __DRI_LEGACY
, __DRI_LEGACY_VERSION
},
990 driCreateNewDrawable
,
994 const __DRIcoreExtension driCoreExtension
= {
995 { __DRI_CORE
, __DRI_CORE_VERSION
},
1000 driIndexConfigAttrib
,
1001 dri2CreateNewDrawable
,
1004 dri2CreateNewContext
,
1011 /* This is the table of extensions that the loader will dlsym() for. */
1012 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
1013 &driCoreExtension
.base
,
1014 &driLegacyExtension
.base
,
1019 driFrameTracking(__DRIdrawable
*drawable
, GLboolean enable
)
1021 return GLX_BAD_CONTEXT
;
1025 driQueryFrameTracking(__DRIdrawable
*dpriv
,
1026 int64_t * sbc
, int64_t * missedFrames
,
1027 float * lastMissedUsage
, float * usage
)
1029 __DRIswapInfo sInfo
;
1032 __DRIscreenPrivate
*psp
= dpriv
->driScreenPriv
;
1034 status
= dpriv
->driScreenPriv
->DriverAPI
.GetSwapInfo( dpriv
, & sInfo
);
1035 if ( status
== 0 ) {
1036 *sbc
= sInfo
.swap_count
;
1037 *missedFrames
= sInfo
.swap_missed_count
;
1038 *lastMissedUsage
= sInfo
.swap_missed_usage
;
1040 (*psp
->systemTime
->getUST
)( & ust
);
1041 *usage
= driCalculateSwapUsage( dpriv
, sInfo
.swap_ust
, ust
);
1047 const __DRIframeTrackingExtension driFrameTrackingExtension
= {
1048 { __DRI_FRAME_TRACKING
, __DRI_FRAME_TRACKING_VERSION
},
1050 driQueryFrameTracking
1054 * Calculate amount of swap interval used between GLX buffer swaps.
1056 * The usage value, on the range [0,max], is the fraction of total swap
1057 * interval time used between GLX buffer swaps is calculated.
1059 * \f$p = t_d / (i * t_r)\f$
1061 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1062 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1063 * required for a single vertical refresh period (as returned by \c
1064 * glXGetMscRateOML).
1066 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1069 * \param dPriv Pointer to the private drawable structure.
1070 * \return If less than a single swap interval time period was required
1071 * between GLX buffer swaps, a number greater than 0 and less than
1072 * 1.0 is returned. If exactly one swap interval time period is
1073 * required, 1.0 is returned, and if more than one is required then
1074 * a number greater than 1.0 will be returned.
1076 * \sa glXSwapIntervalSGI glXGetMscRateOML
1078 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1079 * be possible to cache the sync rate?
1082 driCalculateSwapUsage( __DRIdrawablePrivate
*dPriv
, int64_t last_swap_ust
,
1083 int64_t current_ust
)
1089 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
1091 if ( (*psp
->systemTime
->getMSCRate
)(dPriv
, &n
, &d
, dPriv
->loaderPrivate
) ) {
1092 interval
= (dPriv
->swap_interval
!= 0) ? dPriv
->swap_interval
: 1;
1095 /* We want to calculate
1096 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1097 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1098 * dPriv->swap_ust. interval has already been calculated.
1100 * The only tricky part is us_per_refresh. us_per_refresh is
1101 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1102 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1103 * the denominator of the final calculation, we calculate
1104 * (interval * 1000000 * d) and move n into the numerator.
1107 usage
= (current_ust
- last_swap_ust
);
1109 usage
/= (interval
* d
);