3 * DRI utility functions.
5 * This module acts as glue between GLX and the actual hardware driver. A DRI
6 * driver doesn't really \e have to use any of this - it's optional. But, some
7 * useful stuff is done here that otherwise would have to be duplicated in most
10 * Basically, these utility functions take care of some of the dirty details of
11 * screen initialization, context creation, context binding, DRM setup, etc.
13 * These functions are compiled into each DRI driver so libGL.so knows nothing
25 #define MAP_FAILED ((void *)-1)
28 #include "main/imports.h"
32 #include "drm_sarea.h"
35 #include "../glsl/glsl_parser_extras.h"
37 PUBLIC const char __dri2ConfigOptions[] =
39 DRI_CONF_SECTION_PERFORMANCE
40 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
44 static const uint __dri2NConfigOptions = 1;
46 #ifndef GLX_OML_sync_control
47 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
50 static void dri_get_drawable(__DRIdrawable *pdp);
51 static void dri_put_drawable(__DRIdrawable *pdp);
54 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
56 if (rect2.x1 > rect1.x1) rect1.x1 = rect2.x1;
57 if (rect2.x2 < rect1.x2) rect1.x2 = rect2.x2;
58 if (rect2.y1 > rect1.y1) rect1.y1 = rect2.y1;
59 if (rect2.y2 < rect1.y2) rect1.y2 = rect2.y2;
61 if (rect1.x1 > rect1.x2 || rect1.y1 > rect1.y2) return 0;
63 return (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
66 /*****************************************************************/
67 /** \name Context (un)binding functions */
68 /*****************************************************************/
74 * \param scrn the screen.
77 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
80 * This function calls __DriverAPIRec::UnbindContext, and then decrements
81 * __DRIdrawableRec::refcount which must be non-zero for a successful
84 * While casting the opaque private pointers associated with the parameters
85 * into their respective real types it also assures they are not \c NULL.
87 static int driUnbindContext(__DRIcontext *pcp)
94 ** Assume error checking is done properly in glXMakeCurrent before
95 ** calling driUnbindContext.
101 psp = pcp->driScreenPriv;
102 pdp = pcp->driDrawablePriv;
103 prp = pcp->driReadablePriv;
105 /* already unbound */
108 /* Let driver unbind drawable from context */
109 (*psp->DriverAPI.UnbindContext)(pcp);
112 if (pdp->refcount == 0) {
117 dri_put_drawable(pdp);
120 if (prp->refcount == 0) {
125 dri_put_drawable(prp);
129 /* XXX this is disabled so that if we call SwapBuffers on an unbound
130 * window we can determine the last context bound to the window and
131 * use that context's lock. (BrianP, 2-Dec-2000)
133 pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
139 * This function takes both a read buffer and a draw buffer. This is needed
140 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
143 static int driBindContext(__DRIcontext *pcp,
147 __DRIscreen *psp = NULL;
150 ** Assume error checking is done properly in glXMakeCurrent before
151 ** calling driUnbindContext.
157 /* Bind the drawable to the context */
158 psp = pcp->driScreenPriv;
159 pcp->driDrawablePriv = pdp;
160 pcp->driReadablePriv = prp;
162 pdp->driContextPriv = pcp;
163 dri_get_drawable(pdp);
165 if (prp && pdp != prp) {
166 dri_get_drawable(prp);
170 ** Now that we have a context associated with this drawable, we can
171 ** initialize the drawable information if has not been done before.
174 if (!psp->dri2.enabled) {
175 if (pdp && !pdp->pStamp) {
176 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
177 __driUtilUpdateDrawableInfo(pdp);
178 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
180 if (prp && pdp != prp && !prp->pStamp) {
181 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
182 __driUtilUpdateDrawableInfo(prp);
183 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
187 /* Call device-specific MakeCurrent */
188 return (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
194 /*****************************************************************/
195 /** \name Drawable handling functions */
196 /*****************************************************************/
200 * Update private drawable information.
202 * \param pdp pointer to the private drawable information to update.
204 * This function basically updates the __DRIdrawable struct's
205 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
206 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
207 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
208 * the values are different that means we have to update the clipping
212 __driUtilUpdateDrawableInfo(__DRIdrawable *pdp)
214 __DRIscreen *psp = pdp->driScreenPriv;
215 __DRIcontext *pcp = pdp->driContextPriv;
218 || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
220 * ...but we must ignore it. There can be many contexts bound to a
225 if (pdp->pClipRects) {
226 free(pdp->pClipRects);
227 pdp->pClipRects = NULL;
230 if (pdp->pBackClipRects) {
231 free(pdp->pBackClipRects);
232 pdp->pBackClipRects = NULL;
235 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
237 if (! (*psp->getDrawableInfo->getDrawableInfo)(pdp,
238 &pdp->index, &pdp->lastStamp,
239 &pdp->x, &pdp->y, &pdp->w, &pdp->h,
240 &pdp->numClipRects, &pdp->pClipRects,
243 &pdp->numBackClipRects,
244 &pdp->pBackClipRects,
245 pdp->loaderPrivate)) {
246 /* Error -- eg the window may have been destroyed. Keep going
249 pdp->pStamp = &pdp->lastStamp; /* prevent endless loop */
250 pdp->numClipRects = 0;
251 pdp->pClipRects = NULL;
252 pdp->numBackClipRects = 0;
253 pdp->pBackClipRects = NULL;
256 pdp->pStamp = &(psp->pSAREA->drawableTable[pdp->index].stamp);
258 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
263 /*****************************************************************/
264 /** \name GLX callbacks */
265 /*****************************************************************/
268 static void driReportDamage(__DRIdrawable *pdp,
269 struct drm_clip_rect *pClipRects, int numClipRects)
271 __DRIscreen *psp = pdp->driScreenPriv;
273 /* Check that we actually have the new damage report method */
275 /* Report the damage. Currently, all our drivers draw
276 * directly to the front buffer, so we report the damage there
277 * rather than to the backing storein (if any).
279 (*psp->damage->reportDamage)(pdp,
281 pClipRects, numClipRects,
282 GL_TRUE, pdp->loaderPrivate);
290 * \param drawablePrivate opaque pointer to the per-drawable private info.
293 * This function calls __DRIdrawable::swapBuffers.
295 * Is called directly from glXSwapBuffers().
297 static void driSwapBuffers(__DRIdrawable *dPriv)
299 __DRIscreen *psp = dPriv->driScreenPriv;
300 drm_clip_rect_t *rects;
303 psp->DriverAPI.SwapBuffers(dPriv);
305 if (!dPriv->numClipRects)
308 rects = malloc(sizeof(*rects) * dPriv->numClipRects);
313 for (i = 0; i < dPriv->numClipRects; i++) {
314 rects[i].x1 = dPriv->pClipRects[i].x1 - dPriv->x;
315 rects[i].y1 = dPriv->pClipRects[i].y1 - dPriv->y;
316 rects[i].x2 = dPriv->pClipRects[i].x2 - dPriv->x;
317 rects[i].y2 = dPriv->pClipRects[i].y2 - dPriv->y;
320 driReportDamage(dPriv, rects, dPriv->numClipRects);
324 static int driDrawableGetMSC( __DRIscreen *sPriv, __DRIdrawable *dPriv,
327 return sPriv->DriverAPI.GetDrawableMSC(sPriv, dPriv, msc);
331 static int driWaitForMSC(__DRIdrawable *dPriv, int64_t target_msc,
332 int64_t divisor, int64_t remainder,
333 int64_t * msc, int64_t * sbc)
338 status = dPriv->driScreenPriv->DriverAPI.WaitForMSC( dPriv, target_msc,
342 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
343 * is supported but GLX_OML_sync_control is not. Therefore, don't return
344 * an error value if GetSwapInfo() is not implemented.
347 && dPriv->driScreenPriv->DriverAPI.GetSwapInfo ) {
348 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
349 *sbc = sInfo.swap_count;
356 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension = {
357 { __DRI_MEDIA_STREAM_COUNTER, __DRI_MEDIA_STREAM_COUNTER_VERSION },
363 static void driCopySubBuffer(__DRIdrawable *dPriv,
364 int x, int y, int w, int h)
366 drm_clip_rect_t rect;
369 rect.y1 = dPriv->h - y - h;
371 rect.y2 = rect.y1 + h;
372 driReportDamage(dPriv, &rect, 1);
374 dPriv->driScreenPriv->DriverAPI.CopySubBuffer(dPriv, x, y, w, h);
377 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
378 { __DRI_COPY_SUB_BUFFER, __DRI_COPY_SUB_BUFFER_VERSION },
382 static void driSetSwapInterval(__DRIdrawable *dPriv, unsigned int interval)
384 dPriv->swap_interval = interval;
387 static unsigned int driGetSwapInterval(__DRIdrawable *dPriv)
389 return dPriv->swap_interval;
392 const __DRIswapControlExtension driSwapControlExtension = {
393 { __DRI_SWAP_CONTROL, __DRI_SWAP_CONTROL_VERSION },
400 * This is called via __DRIscreenRec's createNewDrawable pointer.
402 static __DRIdrawable *
403 driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
404 drm_drawable_t hwDrawable, int renderType,
405 const int *attrs, void *data)
409 /* Since pbuffers are not yet supported, no drawable attributes are
415 pdp = malloc(sizeof *pdp);
420 pdp->driContextPriv = NULL;
421 pdp->loaderPrivate = data;
422 pdp->hHWDrawable = hwDrawable;
431 pdp->numClipRects = 0;
432 pdp->numBackClipRects = 0;
433 pdp->pClipRects = NULL;
434 pdp->pBackClipRects = NULL;
438 pdp->driScreenPriv = psp;
440 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes, 0)) {
447 /* This special default value is replaced with the configured
448 * default value when the drawable is first bound to a direct
451 pdp->swap_interval = (unsigned)-1;
457 static __DRIdrawable *
458 dri2CreateNewDrawable(__DRIscreen *screen,
459 const __DRIconfig *config,
462 __DRIdrawable *pdraw;
464 pdraw = driCreateNewDrawable(screen, config, 0, 0, NULL, loaderPrivate);
468 pdraw->pClipRects = &pdraw->dri2.clipRect;
469 pdraw->pBackClipRects = &pdraw->dri2.clipRect;
471 pdraw->pStamp = &pdraw->dri2.stamp;
472 *pdraw->pStamp = pdraw->lastStamp + 1;
478 dri2AllocateBuffer(__DRIscreen *screen,
479 unsigned int attachment, unsigned int format,
480 int width, int height)
482 return (*screen->DriverAPI.AllocateBuffer)(screen, attachment, format,
487 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
489 (*screen->DriverAPI.ReleaseBuffer)(screen, buffer);
494 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
496 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
499 *val = driQueryOptionb(&screen->optionCache, var);
505 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
507 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
508 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
511 *val = driQueryOptioni(&screen->optionCache, var);
517 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
519 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
522 *val = driQueryOptionf(&screen->optionCache, var);
528 static void dri_get_drawable(__DRIdrawable *pdp)
533 static void dri_put_drawable(__DRIdrawable *pdp)
542 psp = pdp->driScreenPriv;
543 (*psp->DriverAPI.DestroyBuffer)(pdp);
544 if (pdp->pClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
545 free(pdp->pClipRects);
546 pdp->pClipRects = NULL;
548 if (pdp->pBackClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
549 free(pdp->pBackClipRects);
550 pdp->pBackClipRects = NULL;
557 driDestroyDrawable(__DRIdrawable *pdp)
559 dri_put_drawable(pdp);
565 /*****************************************************************/
566 /** \name Context handling functions */
567 /*****************************************************************/
571 * Destroy the per-context private information.
574 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
575 * drmDestroyContext(), and finally frees \p contextPrivate.
578 driDestroyContext(__DRIcontext *pcp)
581 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
588 * Create the per-drawable private driver information.
590 * \param render_type Type of rendering target. \c GLX_RGBA_TYPE is the only
591 * type likely to ever be supported for direct-rendering.
592 * However, \c GLX_RGBA_FLOAT_TYPE_ARB may eventually be
593 * supported by some drivers.
594 * \param shared Context with which to share textures, etc. or NULL
596 * \returns An opaque pointer to the per-context private information on
597 * success, or \c NULL on failure.
600 * This function allocates and fills a __DRIcontextRec structure. It
601 * performs some device independent initialization and passes all the
602 * relevent information to __DriverAPIRec::CreateContext to create the
606 static __DRIcontext *
607 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
608 int render_type, __DRIcontext *shared,
609 drm_context_t hwContext, void *data)
612 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
616 pcp = malloc(sizeof *pcp);
620 pcp->driScreenPriv = psp;
621 pcp->driDrawablePriv = NULL;
622 pcp->loaderPrivate = data;
624 pcp->dri2.draw_stamp = 0;
625 pcp->dri2.read_stamp = 0;
627 pcp->hHWContext = hwContext;
629 if ( !(*psp->DriverAPI.CreateContext)(API_OPENGL,
630 &config->modes, pcp, shareCtx) ) {
639 dri2GetAPIMask(__DRIscreen *screen)
641 return screen->api_mask;
644 static __DRIcontext *
645 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
646 const __DRIconfig *config,
647 __DRIcontext *shared, void *data)
649 __DRIcontext *context;
650 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
651 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
654 if (!(screen->api_mask & (1 << api)))
658 case __DRI_API_OPENGL:
659 mesa_api = API_OPENGL;
662 mesa_api = API_OPENGLES;
664 case __DRI_API_GLES2:
665 mesa_api = API_OPENGLES2;
671 context = malloc(sizeof *context);
675 context->driScreenPriv = screen;
676 context->driDrawablePriv = NULL;
677 context->loaderPrivate = data;
679 if (!(*screen->DriverAPI.CreateContext)(mesa_api, modes,
680 context, shareCtx) ) {
689 static __DRIcontext *
690 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
691 __DRIcontext *shared, void *data)
693 return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
694 config, shared, data);
698 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
709 /*****************************************************************/
710 /** \name Screen handling functions */
711 /*****************************************************************/
715 * Destroy the per-screen private information.
718 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
719 * drmClose(), and finally frees \p screenPrivate.
721 static void driDestroyScreen(__DRIscreen *psp)
724 /* No interaction with the X-server is possible at this point. This
725 * routine is called after XCloseDisplay, so there is no protocol
726 * stream open to the X-server anymore.
729 _mesa_destroy_shader_compiler();
731 if (psp->DriverAPI.DestroyScreen)
732 (*psp->DriverAPI.DestroyScreen)(psp);
734 if (!psp->dri2.enabled) {
735 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
736 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
737 (void)drmCloseOnce(psp->fd);
739 driDestroyOptionCache(&psp->optionCache);
740 driDestroyOptionInfo(&psp->optionInfo);
748 setupLoaderExtensions(__DRIscreen *psp,
749 const __DRIextension **extensions)
753 for (i = 0; extensions[i]; i++) {
754 if (strcmp(extensions[i]->name, __DRI_GET_DRAWABLE_INFO) == 0)
755 psp->getDrawableInfo = (__DRIgetDrawableInfoExtension *) extensions[i];
756 if (strcmp(extensions[i]->name, __DRI_DAMAGE) == 0)
757 psp->damage = (__DRIdamageExtension *) extensions[i];
758 if (strcmp(extensions[i]->name, __DRI_SYSTEM_TIME) == 0)
759 psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
760 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
761 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
762 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
763 psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
764 if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
765 psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
770 * This is the bootstrap function for the driver. libGL supplies all of the
771 * requisite information about the system, and the driver initializes itself.
772 * This routine also fills in the linked list pointed to by \c driver_modes
773 * with the \c struct gl_config that the driver can support for windows or
778 * \param scrn Index of the screen
779 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
781 * \param dri_version Version of the "server-side" DRI.
782 * \param drm_version Version of the kernel DRM.
783 * \param frame_buffer Data describing the location and layout of the
785 * \param pSAREA Pointer to the SAREA.
786 * \param fd Device handle for the DRM.
787 * \param extensions ??
788 * \param driver_modes Returns modes suppoted by the driver
789 * \param loaderPrivate ??
791 * \note There is no need to check the minimum API version in this
792 * function. Since the name of this function is versioned, it is
793 * impossible for a loader that is too old to even load this driver.
796 driCreateNewScreen(int scrn,
797 const __DRIversion *ddx_version,
798 const __DRIversion *dri_version,
799 const __DRIversion *drm_version,
800 const __DRIframebuffer *frame_buffer,
801 drmAddress pSAREA, int fd,
802 const __DRIextension **extensions,
803 const __DRIconfig ***driver_modes,
806 static const __DRIextension *emptyExtensionList[] = { NULL };
809 (void) loaderPrivate;
811 if (driDriverAPI.InitScreen == NULL) {
812 __driUtilMessage("driver does not support DRI1");
816 psp = calloc(1, sizeof *psp);
820 setupLoaderExtensions(psp, extensions);
823 ** NOT_DONE: This is used by the X server to detect when the client
824 ** has died while holding the drawable lock. The client sets the
825 ** drawable lock to this value.
829 psp->drm_version = *drm_version;
830 psp->ddx_version = *ddx_version;
831 psp->dri_version = *dri_version;
833 psp->pSAREA = pSAREA;
834 psp->lock = (drmLock *) &psp->pSAREA->lock;
836 psp->pFB = frame_buffer->base;
837 psp->fbSize = frame_buffer->size;
838 psp->fbStride = frame_buffer->stride;
839 psp->fbWidth = frame_buffer->width;
840 psp->fbHeight = frame_buffer->height;
841 psp->devPrivSize = frame_buffer->dev_priv_size;
842 psp->pDevPriv = frame_buffer->dev_priv;
843 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
845 psp->extensions = emptyExtensionList;
848 psp->dri2.enabled = GL_FALSE;
850 psp->DriverAPI = driDriverAPI;
851 psp->api_mask = (1 << __DRI_API_OPENGL);
853 *driver_modes = driDriverAPI.InitScreen(psp);
854 if (*driver_modes == NULL) {
866 dri2CreateNewScreen(int scrn, int fd,
867 const __DRIextension **extensions,
868 const __DRIconfig ***driver_configs, void *data)
870 static const __DRIextension *emptyExtensionList[] = { NULL };
872 drmVersionPtr version;
874 if (driDriverAPI.InitScreen2 == NULL)
877 psp = calloc(1, sizeof(*psp));
881 setupLoaderExtensions(psp, extensions);
883 version = drmGetVersion(fd);
885 psp->drm_version.major = version->version_major;
886 psp->drm_version.minor = version->version_minor;
887 psp->drm_version.patch = version->version_patchlevel;
888 drmFreeVersion(version);
891 psp->extensions = emptyExtensionList;
894 psp->dri2.enabled = GL_TRUE;
896 psp->DriverAPI = driDriverAPI;
897 psp->api_mask = (1 << __DRI_API_OPENGL);
898 *driver_configs = driDriverAPI.InitScreen2(psp);
899 if (*driver_configs == NULL) {
904 psp->DriverAPI = driDriverAPI;
905 psp->loaderPrivate = data;
907 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions,
908 __dri2NConfigOptions);
909 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum,
915 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
917 return psp->extensions;
920 /** Core interface */
921 const __DRIcoreExtension driCoreExtension = {
922 { __DRI_CORE, __DRI_CORE_VERSION },
927 driIndexConfigAttrib,
938 /** DRI2 interface */
939 const __DRIdri2Extension driDRI2Extension = {
940 { __DRI_DRI2, __DRI_DRI2_VERSION },
942 dri2CreateNewDrawable,
943 dri2CreateNewContext,
945 dri2CreateNewContextForAPI,
950 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
951 { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
958 * Calculate amount of swap interval used between GLX buffer swaps.
960 * The usage value, on the range [0,max], is the fraction of total swap
961 * interval time used between GLX buffer swaps is calculated.
963 * \f$p = t_d / (i * t_r)\f$
965 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
966 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
967 * required for a single vertical refresh period (as returned by \c
970 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
973 * \param dPriv Pointer to the private drawable structure.
974 * \return If less than a single swap interval time period was required
975 * between GLX buffer swaps, a number greater than 0 and less than
976 * 1.0 is returned. If exactly one swap interval time period is
977 * required, 1.0 is returned, and if more than one is required then
978 * a number greater than 1.0 will be returned.
980 * \sa glXSwapIntervalSGI glXGetMscRateOML
982 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
983 * be possible to cache the sync rate?
986 driCalculateSwapUsage( __DRIdrawable *dPriv, int64_t last_swap_ust,
987 int64_t current_ust )
993 __DRIscreen *psp = dPriv->driScreenPriv;
995 if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
996 interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
999 /* We want to calculate
1000 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1001 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1002 * dPriv->swap_ust. interval has already been calculated.
1004 * The only tricky part is us_per_refresh. us_per_refresh is
1005 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1006 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1007 * the denominator of the final calculation, we calculate
1008 * (interval * 1000000 * d) and move n into the numerator.
1011 usage = (current_ust - last_swap_ust);
1013 usage /= (interval * d);
1021 dri2InvalidateDrawable(__DRIdrawable *drawable)
1023 drawable->dri2.stamp++;