15f600c03480a03b2113548625908f47d4ca55cf
[profile/ivi/mesa.git] / src / mesa / drivers / dri / common / dri_util.c
1 /**
2  * \file dri_util.c
3  * DRI utility functions.
4  *
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
8  * drivers.
9  * 
10  * Basically, these utility functions take care of some of the dirty details of
11  * screen initialization, context creation, context binding, DRM setup, etc.
12  *
13  * These functions are compiled into each DRI driver so libGL.so knows nothing
14  * about them.
15  */
16
17
18 #include <assert.h>
19 #include <stdarg.h>
20 #include <unistd.h>
21 #include <sys/mman.h>
22 #include <stdio.h>
23
24 #ifndef MAP_FAILED
25 #define MAP_FAILED ((void *)-1)
26 #endif
27
28 #include "main/imports.h"
29 #define None 0
30
31 #include "dri_util.h"
32 #include "drm_sarea.h"
33 #include "utils.h"
34 #include "xmlpool.h"
35 #include "../glsl/glsl_parser_extras.h"
36
37 PUBLIC const char __dri2ConfigOptions[] =
38    DRI_CONF_BEGIN
39       DRI_CONF_SECTION_PERFORMANCE
40          DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
41       DRI_CONF_SECTION_END
42    DRI_CONF_END;
43
44 static const uint __dri2NConfigOptions = 1;
45
46 #ifndef GLX_OML_sync_control
47 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
48 #endif
49
50 static void dri_get_drawable(__DRIdrawable *pdp);
51 static void dri_put_drawable(__DRIdrawable *pdp);
52
53 GLint
54 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
55 {
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;
60
61    if (rect1.x1 > rect1.x2 || rect1.y1 > rect1.y2) return 0;
62
63    return (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
64 }
65
66 /*****************************************************************/
67 /** \name Context (un)binding functions                          */
68 /*****************************************************************/
69 /*@{*/
70
71 /**
72  * Unbind context.
73  * 
74  * \param scrn the screen.
75  * \param gc context.
76  *
77  * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
78  * 
79  * \internal
80  * This function calls __DriverAPIRec::UnbindContext, and then decrements
81  * __DRIdrawableRec::refcount which must be non-zero for a successful
82  * return.
83  * 
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. 
86  */
87 static int driUnbindContext(__DRIcontext *pcp)
88 {
89     __DRIscreen *psp;
90     __DRIdrawable *pdp;
91     __DRIdrawable *prp;
92
93     /*
94     ** Assume error checking is done properly in glXMakeCurrent before
95     ** calling driUnbindContext.
96     */
97
98     if (pcp == NULL)
99         return GL_FALSE;
100
101     psp = pcp->driScreenPriv;
102     pdp = pcp->driDrawablePriv;
103     prp = pcp->driReadablePriv;
104
105     /* already unbound */
106     if (!pdp && !prp)
107       return GL_TRUE;
108     /* Let driver unbind drawable from context */
109     (*psp->DriverAPI.UnbindContext)(pcp);
110
111     assert(pdp);
112     if (pdp->refcount == 0) {
113         /* ERROR!!! */
114         return GL_FALSE;
115     }
116
117     dri_put_drawable(pdp);
118
119     if (prp != pdp) {
120         if (prp->refcount == 0) {
121             /* ERROR!!! */
122             return GL_FALSE;
123         }
124
125         dri_put_drawable(prp);
126     }
127
128
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)
132      */
133     pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
134
135     return GL_TRUE;
136 }
137
138 /**
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
141  * function.
142  */
143 static int driBindContext(__DRIcontext *pcp,
144                           __DRIdrawable *pdp,
145                           __DRIdrawable *prp)
146 {
147     __DRIscreen *psp = NULL;
148
149     /*
150     ** Assume error checking is done properly in glXMakeCurrent before
151     ** calling driUnbindContext.
152     */
153
154     if (!pcp)
155         return GL_FALSE;
156
157     /* Bind the drawable to the context */
158     psp = pcp->driScreenPriv;
159     pcp->driDrawablePriv = pdp;
160     pcp->driReadablePriv = prp;
161     if (pdp) {
162         pdp->driContextPriv = pcp;
163         dri_get_drawable(pdp);
164     }
165     if (prp && pdp != prp) {
166         dri_get_drawable(prp);
167     }
168
169     /*
170     ** Now that we have a context associated with this drawable, we can
171     ** initialize the drawable information if has not been done before.
172     */
173
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);
179         }
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);
184         }
185     }
186
187     /* Call device-specific MakeCurrent */
188     return (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
189 }
190
191 /*@}*/
192
193
194 /*****************************************************************/
195 /** \name Drawable handling functions                            */
196 /*****************************************************************/
197 /*@{*/
198
199 /**
200  * Update private drawable information.
201  *
202  * \param pdp pointer to the private drawable information to update.
203  * 
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
209  * info.
210  */
211 void
212 __driUtilUpdateDrawableInfo(__DRIdrawable *pdp)
213 {
214     __DRIscreen *psp = pdp->driScreenPriv;
215     __DRIcontext *pcp = pdp->driContextPriv;
216     
217     if (!pcp 
218         || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
219         /* ERROR!!! 
220          * ...but we must ignore it. There can be many contexts bound to a
221          * drawable.
222          */
223     }
224
225     if (pdp->pClipRects) {
226         free(pdp->pClipRects); 
227         pdp->pClipRects = NULL;
228     }
229
230     if (pdp->pBackClipRects) {
231         free(pdp->pBackClipRects); 
232         pdp->pBackClipRects = NULL;
233     }
234
235     DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
236
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,
241                           &pdp->backX,
242                           &pdp->backY,
243                           &pdp->numBackClipRects,
244                           &pdp->pBackClipRects,
245                           pdp->loaderPrivate)) {
246         /* Error -- eg the window may have been destroyed.  Keep going
247          * with no cliprects.
248          */
249         pdp->pStamp = &pdp->lastStamp; /* prevent endless loop */
250         pdp->numClipRects = 0;
251         pdp->pClipRects = NULL;
252         pdp->numBackClipRects = 0;
253         pdp->pBackClipRects = NULL;
254     }
255     else
256        pdp->pStamp = &(psp->pSAREA->drawableTable[pdp->index].stamp);
257
258     DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
259 }
260
261 /*@}*/
262
263 /*****************************************************************/
264 /** \name GLX callbacks                                          */
265 /*****************************************************************/
266 /*@{*/
267
268 static void driReportDamage(__DRIdrawable *pdp,
269                             struct drm_clip_rect *pClipRects, int numClipRects)
270 {
271     __DRIscreen *psp = pdp->driScreenPriv;
272
273     /* Check that we actually have the new damage report method */
274     if (psp->damage) {
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).
278          */
279         (*psp->damage->reportDamage)(pdp,
280                                      pdp->x, pdp->y,
281                                      pClipRects, numClipRects,
282                                      GL_TRUE, pdp->loaderPrivate);
283     }
284 }
285
286
287 /**
288  * Swap buffers.
289  *
290  * \param drawablePrivate opaque pointer to the per-drawable private info.
291  * 
292  * \internal
293  * This function calls __DRIdrawable::swapBuffers.
294  * 
295  * Is called directly from glXSwapBuffers().
296  */
297 static void driSwapBuffers(__DRIdrawable *dPriv)
298 {
299     __DRIscreen *psp = dPriv->driScreenPriv;
300     drm_clip_rect_t *rects;
301     int i;
302
303     psp->DriverAPI.SwapBuffers(dPriv);
304
305     if (!dPriv->numClipRects)
306         return;
307
308     rects = malloc(sizeof(*rects) * dPriv->numClipRects);
309
310     if (!rects)
311         return;
312
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;
318     }
319
320     driReportDamage(dPriv, rects, dPriv->numClipRects);
321     free(rects);
322 }
323
324 static __DRIdrawable *
325 dri2CreateNewDrawable(__DRIscreen *screen,
326                       const __DRIconfig *config,
327                       void *loaderPrivate)
328 {
329     __DRIdrawable *pdraw;
330
331     pdraw = malloc(sizeof *pdraw);
332     if (!pdraw)
333         return NULL;
334
335     pdraw->driContextPriv = NULL;
336     pdraw->loaderPrivate = loaderPrivate;
337     pdraw->hHWDrawable = 0;
338     pdraw->refcount = 1;
339     pdraw->pStamp = NULL;
340     pdraw->lastStamp = 0;
341     pdraw->index = 0;
342     pdraw->x = 0;
343     pdraw->y = 0;
344     pdraw->w = 0;
345     pdraw->h = 0;
346     pdraw->numClipRects = 0;
347     pdraw->numBackClipRects = 0;
348     pdraw->pClipRects = NULL;
349     pdraw->pBackClipRects = NULL;
350     pdraw->vblSeq = 0;
351     pdraw->vblFlags = 0;
352
353     pdraw->driScreenPriv = screen;
354
355     if (!(*screen->DriverAPI.CreateBuffer)(screen, pdraw, &config->modes, 0)) {
356        free(pdraw);
357        return NULL;
358     }
359
360     pdraw->msc_base = 0;
361
362     /* This special default value is replaced with the configured
363      * default value when the drawable is first bound to a direct
364      * rendering context. 
365      */
366     pdraw->swap_interval = (unsigned)-1;
367
368     pdraw->pClipRects = &pdraw->dri2.clipRect;
369     pdraw->pBackClipRects = &pdraw->dri2.clipRect;
370
371     pdraw->pStamp = &pdraw->dri2.stamp;
372     *pdraw->pStamp = pdraw->lastStamp + 1;
373
374     return pdraw;
375 }
376
377 static __DRIbuffer *
378 dri2AllocateBuffer(__DRIscreen *screen,
379                    unsigned int attachment, unsigned int format,
380                    int width, int height)
381 {
382     return (*screen->DriverAPI.AllocateBuffer)(screen, attachment, format,
383                                                width, height);
384 }
385
386 static void
387 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
388 {
389    (*screen->DriverAPI.ReleaseBuffer)(screen, buffer);
390 }
391
392
393 static int
394 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
395 {
396    if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
397       return -1;
398
399    *val = driQueryOptionb(&screen->optionCache, var);
400
401    return 0;
402 }
403
404 static int
405 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
406 {
407    if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
408        !driCheckOption(&screen->optionCache, var, DRI_ENUM))
409       return -1;
410
411     *val = driQueryOptioni(&screen->optionCache, var);
412
413     return 0;
414 }
415
416 static int
417 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
418 {
419    if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
420       return -1;
421
422     *val = driQueryOptionf(&screen->optionCache, var);
423
424     return 0;
425 }
426
427
428 static void dri_get_drawable(__DRIdrawable *pdp)
429 {
430     pdp->refcount++;
431 }
432         
433 static void dri_put_drawable(__DRIdrawable *pdp)
434 {
435     __DRIscreen *psp;
436
437     if (pdp) {
438         pdp->refcount--;
439         if (pdp->refcount)
440             return;
441
442         psp = pdp->driScreenPriv;
443         (*psp->DriverAPI.DestroyBuffer)(pdp);
444         if (pdp->pClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
445             free(pdp->pClipRects);
446             pdp->pClipRects = NULL;
447         }
448         if (pdp->pBackClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
449             free(pdp->pBackClipRects);
450             pdp->pBackClipRects = NULL;
451         }
452         free(pdp);
453     }
454 }
455
456 static void
457 driDestroyDrawable(__DRIdrawable *pdp)
458 {
459     dri_put_drawable(pdp);
460 }
461
462 /*@}*/
463
464
465 /*****************************************************************/
466 /** \name Context handling functions                             */
467 /*****************************************************************/
468 /*@{*/
469
470 /**
471  * Destroy the per-context private information.
472  * 
473  * \internal
474  * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
475  * drmDestroyContext(), and finally frees \p contextPrivate.
476  */
477 static void
478 driDestroyContext(__DRIcontext *pcp)
479 {
480     if (pcp) {
481         (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
482         free(pcp);
483     }
484 }
485
486
487 /**
488  * Create the per-drawable private driver information.
489  * 
490  * \param render_type   Type of rendering target.  \c GLX_RGBA_TYPE is the only
491  *                      type likely to ever be supported for direct-rendering.
492  *                      However, \c GLX_RGBA_FLOAT_TYPE_ARB may eventually be
493  *                      supported by some drivers.
494  * \param shared        Context with which to share textures, etc. or NULL
495  *
496  * \returns An opaque pointer to the per-context private information on
497  *          success, or \c NULL on failure.
498  * 
499  * \internal
500  * This function allocates and fills a __DRIcontextRec structure.  It
501  * performs some device independent initialization and passes all the
502  * relevent information to __DriverAPIRec::CreateContext to create the
503  * context.
504  *
505  */
506 static __DRIcontext *
507 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
508                     int render_type, __DRIcontext *shared, 
509                     drm_context_t hwContext, void *data)
510 {
511     __DRIcontext *pcp;
512     void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
513
514     (void) render_type;
515
516     pcp = malloc(sizeof *pcp);
517     if (!pcp)
518         return NULL;
519
520     pcp->driScreenPriv = psp;
521     pcp->driDrawablePriv = NULL;
522     pcp->loaderPrivate = data;
523     
524     pcp->dri2.draw_stamp = 0;
525     pcp->dri2.read_stamp = 0;
526
527     pcp->hHWContext = hwContext;
528
529     if ( !(*psp->DriverAPI.CreateContext)(API_OPENGL,
530                                           &config->modes, pcp, shareCtx) ) {
531         free(pcp);
532         return NULL;
533     }
534
535     return pcp;
536 }
537
538 static unsigned int
539 dri2GetAPIMask(__DRIscreen *screen)
540 {
541     return screen->api_mask;
542 }
543
544 static __DRIcontext *
545 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
546                            const __DRIconfig *config,
547                            __DRIcontext *shared, void *data)
548 {
549     __DRIcontext *context;
550     const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
551     void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
552     gl_api mesa_api;
553
554     if (!(screen->api_mask & (1 << api)))
555         return NULL;
556
557     switch (api) {
558     case __DRI_API_OPENGL:
559             mesa_api = API_OPENGL;
560             break;
561     case __DRI_API_GLES:
562             mesa_api = API_OPENGLES;
563             break;
564     case __DRI_API_GLES2:
565             mesa_api = API_OPENGLES2;
566             break;
567     default:
568             return NULL;
569     }
570
571     context = malloc(sizeof *context);
572     if (!context)
573         return NULL;
574
575     context->driScreenPriv = screen;
576     context->driDrawablePriv = NULL;
577     context->loaderPrivate = data;
578     
579     if (!(*screen->DriverAPI.CreateContext)(mesa_api, modes,
580                                             context, shareCtx) ) {
581         free(context);
582         return NULL;
583     }
584
585     return context;
586 }
587
588
589 static __DRIcontext *
590 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
591                       __DRIcontext *shared, void *data)
592 {
593    return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
594                                      config, shared, data);
595 }
596
597 static int
598 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
599 {
600     (void) dest;
601     (void) src;
602     (void) mask;
603     return GL_FALSE;
604 }
605
606 /*@}*/
607
608
609 /*****************************************************************/
610 /** \name Screen handling functions                              */
611 /*****************************************************************/
612 /*@{*/
613
614 /**
615  * Destroy the per-screen private information.
616  * 
617  * \internal
618  * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
619  * drmClose(), and finally frees \p screenPrivate.
620  */
621 static void driDestroyScreen(__DRIscreen *psp)
622 {
623     if (psp) {
624         /* No interaction with the X-server is possible at this point.  This
625          * routine is called after XCloseDisplay, so there is no protocol
626          * stream open to the X-server anymore.
627          */
628
629        _mesa_destroy_shader_compiler();
630
631         if (psp->DriverAPI.DestroyScreen)
632             (*psp->DriverAPI.DestroyScreen)(psp);
633
634         if (!psp->dri2.enabled) {
635            (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
636            (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
637            (void)drmCloseOnce(psp->fd);
638         } else {
639            driDestroyOptionCache(&psp->optionCache);
640            driDestroyOptionInfo(&psp->optionInfo);
641         }
642
643         free(psp);
644     }
645 }
646
647 static void
648 setupLoaderExtensions(__DRIscreen *psp,
649                       const __DRIextension **extensions)
650 {
651     int i;
652
653     for (i = 0; extensions[i]; i++) {
654         if (strcmp(extensions[i]->name, __DRI_GET_DRAWABLE_INFO) == 0)
655             psp->getDrawableInfo = (__DRIgetDrawableInfoExtension *) extensions[i];
656         if (strcmp(extensions[i]->name, __DRI_DAMAGE) == 0)
657             psp->damage = (__DRIdamageExtension *) extensions[i];
658         if (strcmp(extensions[i]->name, __DRI_SYSTEM_TIME) == 0)
659             psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
660         if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
661             psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
662         if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
663             psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
664         if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
665             psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
666     }
667 }
668
669 /**
670  * DRI2
671  */
672 static __DRIscreen *
673 dri2CreateNewScreen(int scrn, int fd,
674                     const __DRIextension **extensions,
675                     const __DRIconfig ***driver_configs, void *data)
676 {
677     static const __DRIextension *emptyExtensionList[] = { NULL };
678     __DRIscreen *psp;
679     drmVersionPtr version;
680
681     if (driDriverAPI.InitScreen2 == NULL)
682         return NULL;
683
684     psp = calloc(1, sizeof(*psp));
685     if (!psp)
686         return NULL;
687
688     setupLoaderExtensions(psp, extensions);
689
690     version = drmGetVersion(fd);
691     if (version) {
692         psp->drm_version.major = version->version_major;
693         psp->drm_version.minor = version->version_minor;
694         psp->drm_version.patch = version->version_patchlevel;
695         drmFreeVersion(version);
696     }
697
698     psp->extensions = emptyExtensionList;
699     psp->fd = fd;
700     psp->myNum = scrn;
701     psp->dri2.enabled = GL_TRUE;
702
703     psp->DriverAPI = driDriverAPI;
704     psp->api_mask = (1 << __DRI_API_OPENGL);
705     *driver_configs = driDriverAPI.InitScreen2(psp);
706     if (*driver_configs == NULL) {
707         free(psp);
708         return NULL;
709     }
710
711     psp->DriverAPI = driDriverAPI;
712     psp->loaderPrivate = data;
713
714     driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions,
715                        __dri2NConfigOptions);
716     driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum,
717                         "dri2");
718
719     return psp;
720 }
721
722 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
723 {
724     return psp->extensions;
725 }
726
727 /** Core interface */
728 const __DRIcoreExtension driCoreExtension = {
729     { __DRI_CORE, __DRI_CORE_VERSION },
730     NULL,
731     driDestroyScreen,
732     driGetExtensions,
733     driGetConfigAttrib,
734     driIndexConfigAttrib,
735     NULL,
736     driDestroyDrawable,
737     driSwapBuffers,
738     NULL,
739     driCopyContext,
740     driDestroyContext,
741     driBindContext,
742     driUnbindContext
743 };
744
745 /** DRI2 interface */
746 const __DRIdri2Extension driDRI2Extension = {
747     { __DRI_DRI2, __DRI_DRI2_VERSION },
748     dri2CreateNewScreen,
749     dri2CreateNewDrawable,
750     dri2CreateNewContext,
751     dri2GetAPIMask,
752     dri2CreateNewContextForAPI,
753     dri2AllocateBuffer,
754     dri2ReleaseBuffer
755 };
756
757 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
758    { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
759    dri2ConfigQueryb,
760    dri2ConfigQueryi,
761    dri2ConfigQueryf,
762 };
763
764 /**
765  * Calculate amount of swap interval used between GLX buffer swaps.
766  * 
767  * The usage value, on the range [0,max], is the fraction of total swap
768  * interval time used between GLX buffer swaps is calculated.
769  *
770  *            \f$p = t_d / (i * t_r)\f$
771  * 
772  * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
773  * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
774  * required for a single vertical refresh period (as returned by \c
775  * glXGetMscRateOML).
776  * 
777  * See the documentation for the GLX_MESA_swap_frame_usage extension for more
778  * details.
779  *
780  * \param   dPriv  Pointer to the private drawable structure.
781  * \return  If less than a single swap interval time period was required
782  *          between GLX buffer swaps, a number greater than 0 and less than
783  *          1.0 is returned.  If exactly one swap interval time period is
784  *          required, 1.0 is returned, and if more than one is required then
785  *          a number greater than 1.0 will be returned.
786  *
787  * \sa glXSwapIntervalSGI glXGetMscRateOML
788  * 
789  * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
790  *       be possible to cache the sync rate?
791  */
792 float
793 driCalculateSwapUsage( __DRIdrawable *dPriv, int64_t last_swap_ust,
794                        int64_t current_ust )
795 {
796    int32_t   n;
797    int32_t   d;
798    int       interval;
799    float     usage = 1.0;
800    __DRIscreen *psp = dPriv->driScreenPriv;
801
802    if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
803       interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
804
805
806       /* We want to calculate
807        * (current_UST - last_swap_UST) / (interval * us_per_refresh).  We get
808        * current_UST by calling __glXGetUST.  last_swap_UST is stored in
809        * dPriv->swap_ust.  interval has already been calculated.
810        *
811        * The only tricky part is us_per_refresh.  us_per_refresh is
812        * 1000000 / MSC_rate.  We know the MSC_rate is n / d.  We can flip it
813        * around and say us_per_refresh = 1000000 * d / n.  Since this goes in
814        * the denominator of the final calculation, we calculate
815        * (interval * 1000000 * d) and move n into the numerator.
816        */
817
818       usage = (current_ust - last_swap_ust);
819       usage *= n;
820       usage /= (interval * d);
821       usage /= 1000000.0;
822    }
823    
824    return usage;
825 }
826
827 void
828 dri2InvalidateDrawable(__DRIdrawable *drawable)
829 {
830     drawable->dri2.stamp++;
831 }
832
833 /*@}*/