Convert a left-over private void * to __DRIcontext *.
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
1 /* $XFree86: xc/lib/GL/dri/dri_util.c,v 1.7 2003/04/28 17:01:25 dawes Exp $ */
2 /**
3 * \file dri_util.c
4 * DRI utility functions.
5 *
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
9 * drivers.
10 *
11 * Basically, these utility functions take care of some of the dirty details of
12 * screen initialization, context creation, context binding, DRM setup, etc.
13 *
14 * These functions are compiled into each DRI driver so libGL.so knows nothing
15 * about them.
16 */
17
18
19 #include <assert.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <stdio.h>
24
25 #ifndef MAP_FAILED
26 #define MAP_FAILED ((void *)-1)
27 #endif
28
29 #include "imports.h"
30 #define None 0
31
32 #include "dri_util.h"
33 #include "drm_sarea.h"
34
35 #ifndef GLX_OML_sync_control
36 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
37 #endif
38
39 /* This pointer *must* be set by the driver's __driCreateNewScreen funciton!
40 */
41 const __DRIinterfaceMethods * dri_interface = NULL;
42
43 /**
44 * This is used in a couple of places that call \c driCreateNewDrawable.
45 */
46 static const int empty_attribute_list[1] = { None };
47
48
49 /**
50 * This is just a token extension used to signal that the driver
51 * supports setting a read drawable.
52 */
53 const __DRIextension driReadDrawableExtension = {
54 __DRI_READ_DRAWABLE
55 };
56
57 /**
58 * Cached copy of the internal API version used by libGL and the client-side
59 * DRI driver.
60 */
61 static int api_ver = 0;
62
63 static void *driCreateNewDrawable(__DRIscreen *screen,
64 const __GLcontextModes *modes,
65 __DRIdrawable *pdraw,
66 drm_drawable_t hwDrawable,
67 int renderType, const int *attrs);
68
69 static void driDestroyDrawable(__DRIdrawable *drawable);
70
71
72 /**
73 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
74 * is set.
75 *
76 * Is called from the drivers.
77 *
78 * \param f \c printf like format string.
79 */
80 void
81 __driUtilMessage(const char *f, ...)
82 {
83 va_list args;
84
85 if (getenv("LIBGL_DEBUG")) {
86 fprintf(stderr, "libGL error: \n");
87 va_start(args, f);
88 vfprintf(stderr, f, args);
89 va_end(args);
90 fprintf(stderr, "\n");
91 }
92 }
93
94
95 /*****************************************************************/
96 /** \name Context (un)binding functions */
97 /*****************************************************************/
98 /*@{*/
99
100 /**
101 * Unbind context.
102 *
103 * \param scrn the screen.
104 * \param gc context.
105 *
106 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
107 *
108 * \internal
109 * This function calls __DriverAPIRec::UnbindContext, and then decrements
110 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
111 * return.
112 *
113 * While casting the opaque private pointers associated with the parameters
114 * into their respective real types it also assures they are not \c NULL.
115 */
116 static GLboolean driUnbindContext(__DRIcontext *ctx)
117 {
118 __DRIcontextPrivate *pcp;
119 __DRIscreenPrivate *psp;
120 __DRIdrawablePrivate *pdp;
121 __DRIdrawablePrivate *prp;
122
123 /*
124 ** Assume error checking is done properly in glXMakeCurrent before
125 ** calling driUnbindContext.
126 */
127
128 if (ctx == NULL)
129 return GL_FALSE;
130
131 pcp = (__DRIcontextPrivate *)ctx->private;
132 psp = (__DRIscreenPrivate *)pcp->driScreenPriv;
133 pdp = (__DRIdrawablePrivate *)pcp->driDrawablePriv;
134 prp = (__DRIdrawablePrivate *)pcp->driReadablePriv;
135
136 /* Let driver unbind drawable from context */
137 (*psp->DriverAPI.UnbindContext)(pcp);
138
139 if (pdp->refcount == 0) {
140 /* ERROR!!! */
141 return GL_FALSE;
142 }
143
144 pdp->refcount--;
145
146 if (prp != pdp) {
147 if (prp->refcount == 0) {
148 /* ERROR!!! */
149 return GL_FALSE;
150 }
151
152 prp->refcount--;
153 }
154
155
156 /* XXX this is disabled so that if we call SwapBuffers on an unbound
157 * window we can determine the last context bound to the window and
158 * use that context's lock. (BrianP, 2-Dec-2000)
159 */
160 #if 0
161 /* Unbind the drawable */
162 pcp->driDrawablePriv = NULL;
163 pdp->driContextPriv = &psp->dummyContextPriv;
164 #endif
165
166 return GL_TRUE;
167 }
168
169
170 /**
171 * This function takes both a read buffer and a draw buffer. This is needed
172 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
173 * function.
174 */
175 static GLboolean DoBindContext(__DRIcontext *ctx,
176 __DRIdrawable *pdraw,
177 __DRIdrawable *pread)
178 {
179 __DRIdrawablePrivate *pdp;
180 __DRIdrawablePrivate *prp;
181 __DRIcontextPrivate * const pcp = ctx->private;
182 __DRIscreenPrivate *psp = pcp->driScreenPriv;
183
184 pdp = (__DRIdrawablePrivate *) pdraw->private;
185 prp = (__DRIdrawablePrivate *) pread->private;
186
187 /* Bind the drawable to the context */
188 pcp->driDrawablePriv = pdp;
189 pcp->driReadablePriv = prp;
190 pdp->driContextPriv = pcp;
191 pdp->refcount++;
192 if ( pdp != prp ) {
193 prp->refcount++;
194 }
195
196 /*
197 ** Now that we have a context associated with this drawable, we can
198 ** initialize the drawable information if has not been done before.
199 */
200 if (!pdp->pStamp || *pdp->pStamp != pdp->lastStamp) {
201 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
202 __driUtilUpdateDrawableInfo(pdp);
203 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
204 }
205
206 if ((pdp != prp) && (!prp->pStamp || *prp->pStamp != prp->lastStamp)) {
207 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
208 __driUtilUpdateDrawableInfo(prp);
209 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
210 }
211
212 /* Call device-specific MakeCurrent */
213 (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
214
215 return GL_TRUE;
216 }
217
218
219 /**
220 * This function takes both a read buffer and a draw buffer. This is needed
221 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
222 * function.
223 */
224 static GLboolean driBindContext(__DRIcontext * ctx,
225 __DRIdrawable *pdraw,
226 __DRIdrawable *pread)
227 {
228 /*
229 ** Assume error checking is done properly in glXMakeCurrent before
230 ** calling driBindContext.
231 */
232
233 if (ctx == NULL || pdraw == None || pread == None)
234 return GL_FALSE;
235
236 return DoBindContext( ctx, pdraw, pread );
237 }
238 /*@}*/
239
240
241 /*****************************************************************/
242 /** \name Drawable handling functions */
243 /*****************************************************************/
244 /*@{*/
245
246 /**
247 * Update private drawable information.
248 *
249 * \param pdp pointer to the private drawable information to update.
250 *
251 * This function basically updates the __DRIdrawablePrivate struct's
252 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
253 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
254 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
255 * the values are different that means we have to update the clipping
256 * info.
257 */
258 void
259 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
260 {
261 __DRIscreenPrivate *psp;
262 __DRIcontextPrivate *pcp = pdp->driContextPriv;
263
264 if (!pcp
265 || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
266 /* ERROR!!!
267 * ...but we must ignore it. There can be many contexts bound to a
268 * drawable.
269 */
270 }
271
272 psp = pdp->driScreenPriv;
273 if (!psp) {
274 /* ERROR!!! */
275 _mesa_problem(NULL, "Warning! Possible infinite loop due to bug "
276 "in file %s, line %d\n",
277 __FILE__, __LINE__);
278 return;
279 }
280
281 if (pdp->pClipRects) {
282 _mesa_free(pdp->pClipRects);
283 pdp->pClipRects = NULL;
284 }
285
286 if (pdp->pBackClipRects) {
287 _mesa_free(pdp->pBackClipRects);
288 pdp->pBackClipRects = NULL;
289 }
290
291 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
292
293 if (! (*dri_interface->getDrawableInfo)(pdp->pdraw,
294 &pdp->index, &pdp->lastStamp,
295 &pdp->x, &pdp->y, &pdp->w, &pdp->h,
296 &pdp->numClipRects, &pdp->pClipRects,
297 &pdp->backX,
298 &pdp->backY,
299 &pdp->numBackClipRects,
300 &pdp->pBackClipRects )) {
301 /* Error -- eg the window may have been destroyed. Keep going
302 * with no cliprects.
303 */
304 pdp->pStamp = &pdp->lastStamp; /* prevent endless loop */
305 pdp->numClipRects = 0;
306 pdp->pClipRects = NULL;
307 pdp->numBackClipRects = 0;
308 pdp->pBackClipRects = NULL;
309 }
310 else
311 pdp->pStamp = &(psp->pSAREA->drawableTable[pdp->index].stamp);
312
313 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
314
315 }
316
317 /*@}*/
318
319 /*****************************************************************/
320 /** \name GLX callbacks */
321 /*****************************************************************/
322 /*@{*/
323
324 /**
325 * Swap buffers.
326 *
327 * \param drawablePrivate opaque pointer to the per-drawable private info.
328 *
329 * \internal
330 * This function calls __DRIdrawablePrivate::swapBuffers.
331 *
332 * Is called directly from glXSwapBuffers().
333 */
334 static void driSwapBuffers(__DRIdrawable *drawable)
335 {
336 __DRIdrawablePrivate *dPriv = drawable->private;
337 drm_clip_rect_t rect;
338
339 dPriv->swapBuffers(dPriv);
340
341 /* Check that we actually have the new damage report method */
342 if (api_ver < 20070105 || dri_interface->reportDamage == NULL)
343 return;
344
345 /* Assume it's affecting the whole drawable for now */
346 rect.x1 = 0;
347 rect.y1 = 0;
348 rect.x2 = rect.x1 + dPriv->w;
349 rect.y2 = rect.y1 + dPriv->h;
350
351 /* Report the damage. Currently, all our drivers draw directly to the
352 * front buffer, so we report the damage there rather than to the backing
353 * store (if any).
354 */
355 (*dri_interface->reportDamage)(dPriv->pdraw, dPriv->x, dPriv->y,
356 &rect, 1, GL_TRUE);
357 }
358
359 /**
360 * Called directly from a number of higher-level GLX functions.
361 */
362 static int driGetMSC( __DRIscreen *screen, int64_t *msc )
363 {
364 __DRIscreenPrivate *sPriv = screen->private;
365
366 return sPriv->DriverAPI.GetMSC( sPriv, msc );
367 }
368
369 static int driWaitForMSC(__DRIdrawable *drawable, int64_t target_msc,
370 int64_t divisor, int64_t remainder,
371 int64_t * msc, int64_t * sbc)
372 {
373 __DRIdrawablePrivate *dPriv = drawable->private;
374 __DRIswapInfo sInfo;
375 int status;
376
377
378 status = dPriv->driScreenPriv->DriverAPI.WaitForMSC( dPriv, target_msc,
379 divisor, remainder,
380 msc );
381
382 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
383 * is supported but GLX_OML_sync_control is not. Therefore, don't return
384 * an error value if GetSwapInfo() is not implemented.
385 */
386 if ( status == 0
387 && dPriv->driScreenPriv->DriverAPI.GetSwapInfo ) {
388 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
389 *sbc = sInfo.swap_count;
390 }
391
392 return status;
393 }
394
395 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension = {
396 { __DRI_MEDIA_STREAM_COUNTER },
397 driGetMSC,
398 driWaitForMSC,
399 };
400
401 static void driCopySubBuffer(__DRIdrawable *drawable,
402 int x, int y, int w, int h)
403 {
404 __DRIdrawablePrivate *dPriv = drawable->private;
405 dPriv->driScreenPriv->DriverAPI.CopySubBuffer(dPriv, x, y, w, h);
406 }
407
408 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
409 { __DRI_COPY_SUB_BUFFER }, driCopySubBuffer
410 };
411
412 static void driSetSwapInterval(__DRIdrawable *drawable, unsigned int interval)
413 {
414 __DRIdrawablePrivate *dpriv = drawable->private;
415
416 dpriv->swap_interval = interval;
417 }
418
419 static unsigned int driGetSwapInterval(__DRIdrawable *drawable)
420 {
421 __DRIdrawablePrivate *dpriv = drawable->private;
422
423 return dpriv->swap_interval;
424 }
425
426 const __DRIswapControlExtension driSwapControlExtension = {
427 { __DRI_SWAP_CONTROL },
428 driSetSwapInterval,
429 driGetSwapInterval
430 };
431
432
433 /**
434 * This is called via __DRIscreenRec's createNewDrawable pointer.
435 */
436 static void *driCreateNewDrawable(__DRIscreen *screen,
437 const __GLcontextModes *modes,
438 __DRIdrawable *pdraw,
439 drm_drawable_t hwDrawable,
440 int renderType,
441 const int *attrs)
442 {
443 __DRIscreenPrivate *psp;
444 __DRIdrawablePrivate *pdp;
445
446
447 pdraw->private = NULL;
448
449 /* Since pbuffers are not yet supported, no drawable attributes are
450 * supported either.
451 */
452 (void) attrs;
453
454 pdp = (__DRIdrawablePrivate *)_mesa_malloc(sizeof(__DRIdrawablePrivate));
455 if (!pdp) {
456 return NULL;
457 }
458
459 pdp->hHWDrawable = hwDrawable;
460 pdp->pdraw = pdraw;
461 pdp->refcount = 0;
462 pdp->pStamp = NULL;
463 pdp->lastStamp = 0;
464 pdp->index = 0;
465 pdp->x = 0;
466 pdp->y = 0;
467 pdp->w = 0;
468 pdp->h = 0;
469 pdp->numClipRects = 0;
470 pdp->numBackClipRects = 0;
471 pdp->pClipRects = NULL;
472 pdp->pBackClipRects = NULL;
473
474 psp = (__DRIscreenPrivate *)screen->private;
475 pdp->driScreenPriv = psp;
476 pdp->driContextPriv = &psp->dummyContextPriv;
477
478 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, modes,
479 renderType == GLX_PIXMAP_BIT)) {
480 _mesa_free(pdp);
481 return NULL;
482 }
483
484 pdraw->private = pdp;
485 pdraw->destroyDrawable = driDestroyDrawable;
486 pdraw->swapBuffers = driSwapBuffers; /* called by glXSwapBuffers() */
487
488 /* This special default value is replaced with the configured
489 * default value when the drawable is first bound to a direct
490 * rendering context.
491 */
492 pdp->swap_interval = (unsigned)-1;
493
494 pdp->swapBuffers = psp->DriverAPI.SwapBuffers;
495
496 return (void *) pdp;
497 }
498
499 static void
500 driDestroyDrawable(__DRIdrawable *drawable)
501 {
502 __DRIdrawablePrivate *pdp = drawable->private;
503 __DRIscreenPrivate *psp;
504
505 if (pdp) {
506 psp = pdp->driScreenPriv;
507 (*psp->DriverAPI.DestroyBuffer)(pdp);
508 if (pdp->pClipRects) {
509 _mesa_free(pdp->pClipRects);
510 pdp->pClipRects = NULL;
511 }
512 if (pdp->pBackClipRects) {
513 _mesa_free(pdp->pBackClipRects);
514 pdp->pBackClipRects = NULL;
515 }
516 _mesa_free(pdp);
517 }
518 }
519
520 /*@}*/
521
522
523 /*****************************************************************/
524 /** \name Context handling functions */
525 /*****************************************************************/
526 /*@{*/
527
528 /**
529 * Destroy the per-context private information.
530 *
531 * \param contextPrivate opaque pointer to the per-drawable private info.
532 *
533 * \internal
534 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
535 * drmDestroyContext(), and finally frees \p contextPrivate.
536 */
537 static void
538 driDestroyContext(__DRIcontext *context)
539 {
540 __DRIcontextPrivate *pcp = context->private;
541
542 if (pcp) {
543 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
544 _mesa_free(pcp);
545 }
546 }
547
548
549 /**
550 * Create the per-drawable private driver information.
551 *
552 * \param dpy The display handle.
553 * \param modes Mode used to create the new context.
554 * \param render_type Type of rendering target. \c GLX_RGBA is the only
555 * type likely to ever be supported for direct-rendering.
556 * \param shared The shared context dependent methods or \c NULL if
557 * non-existent.
558 * \param pctx DRI context to receive the context dependent methods.
559 *
560 * \returns An opaque pointer to the per-context private information on
561 * success, or \c NULL on failure.
562 *
563 * \internal
564 * This function allocates and fills a __DRIcontextPrivateRec structure. It
565 * performs some device independent initialization and passes all the
566 * relevent information to __DriverAPIRec::CreateContext to create the
567 * context.
568 *
569 */
570 static void *
571 driCreateNewContext(__DRIscreen *screen, const __GLcontextModes *modes,
572 int render_type, __DRIcontext *shared,
573 drm_context_t hwContext, __DRIcontext *pctx)
574 {
575 __DRIcontextPrivate *pcp;
576 __DRIcontextPrivate *pshare = (shared != NULL) ? shared->private : NULL;
577 __DRIscreenPrivate *psp;
578 void * const shareCtx = (pshare != NULL) ? pshare->driverPrivate : NULL;
579
580 psp = (__DRIscreenPrivate *)screen->private;
581
582 pcp = (__DRIcontextPrivate *)_mesa_malloc(sizeof(__DRIcontextPrivate));
583 if (!pcp) {
584 return NULL;
585 }
586
587 pcp->hHWContext = hwContext;
588 pcp->driScreenPriv = psp;
589 pcp->driDrawablePriv = NULL;
590
591 /* When the first context is created for a screen, initialize a "dummy"
592 * context.
593 */
594
595 if (!psp->dummyContextPriv.driScreenPriv) {
596 psp->dummyContextPriv.hHWContext = psp->pSAREA->dummy_context;
597 psp->dummyContextPriv.driScreenPriv = psp;
598 psp->dummyContextPriv.driDrawablePriv = NULL;
599 psp->dummyContextPriv.driverPrivate = NULL;
600 /* No other fields should be used! */
601 }
602
603 pctx->destroyContext = driDestroyContext;
604 pctx->bindContext = driBindContext;
605 pctx->unbindContext = driUnbindContext;
606
607 if ( !(*psp->DriverAPI.CreateContext)(modes, pcp, shareCtx) ) {
608 _mesa_free(pcp);
609 return NULL;
610 }
611
612 return pcp;
613 }
614 /*@}*/
615
616
617 static const __DRIextension **
618 driGetExtensions(__DRIscreen *screen)
619 {
620 __DRIscreenPrivate *psp = screen->private;
621
622 return psp->extensions;
623 }
624
625 /*****************************************************************/
626 /** \name Screen handling functions */
627 /*****************************************************************/
628 /*@{*/
629
630 /**
631 * Destroy the per-screen private information.
632 *
633 * \param dpy the display handle.
634 * \param scrn the screen number.
635 * \param screenPrivate opaque pointer to the per-screen private information.
636 *
637 * \internal
638 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
639 * drmClose(), and finally frees \p screenPrivate.
640 */
641 static void driDestroyScreen(__DRIscreen *screen)
642 {
643 __DRIscreenPrivate *psp = screen->private;
644
645 if (psp) {
646 /* No interaction with the X-server is possible at this point. This
647 * routine is called after XCloseDisplay, so there is no protocol
648 * stream open to the X-server anymore.
649 */
650
651 if (psp->DriverAPI.DestroyScreen)
652 (*psp->DriverAPI.DestroyScreen)(psp);
653
654 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
655 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
656 (void)drmCloseOnce(psp->fd);
657
658 _mesa_free(psp);
659 }
660 }
661
662
663 /**
664 * This is the bootstrap function for the driver. libGL supplies all of the
665 * requisite information about the system, and the driver initializes itself.
666 * This routine also fills in the linked list pointed to by \c driver_modes
667 * with the \c __GLcontextModes that the driver can support for windows or
668 * pbuffers.
669 *
670 * \param scrn Index of the screen
671 * \param psc DRI screen data (not driver private)
672 * \param modes Linked list of known display modes. This list is, at a
673 * minimum, a list of modes based on the current display mode.
674 * These roughly match the set of available X11 visuals, but it
675 * need not be limited to X11! The calling libGL should create
676 * a list that will inform the driver of the current display
677 * mode (i.e., color buffer depth, depth buffer depth, etc.).
678 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
679 * all drivers.
680 * \param dri_version Version of the "server-side" DRI.
681 * \param drm_version Version of the kernel DRM.
682 * \param frame_buffer Data describing the location and layout of the
683 * framebuffer.
684 * \param pSAREA Pointer the the SAREA.
685 * \param fd Device handle for the DRM.
686 * \param internal_api_version Version of the internal interface between the
687 * driver and libGL.
688 * \param driverAPI Driver API functions used by other routines in dri_util.c.
689 *
690 * \note There is no need to check the minimum API version in this
691 * function. Since the name of this function is versioned, it is
692 * impossible for a loader that is too old to even load this driver.
693 */
694 PUBLIC
695 void * __DRI_CREATE_NEW_SCREEN( int scrn, __DRIscreen *psc,
696 const __DRIversion * ddx_version,
697 const __DRIversion * dri_version,
698 const __DRIversion * drm_version,
699 const __DRIframebuffer * frame_buffer,
700 drmAddress pSAREA, int fd,
701 int internal_api_version,
702 const __DRIinterfaceMethods * interface,
703 __GLcontextModes ** driver_modes )
704
705 {
706 __DRIscreenPrivate *psp;
707 static const __DRIextension *emptyExtensionList[] = { NULL };
708 dri_interface = interface;
709 api_ver = internal_api_version;
710
711 psp = _mesa_malloc(sizeof(*psp));
712 if (!psp)
713 return NULL;
714
715 psp->psc = psc;
716
717 /*
718 ** NOT_DONE: This is used by the X server to detect when the client
719 ** has died while holding the drawable lock. The client sets the
720 ** drawable lock to this value.
721 */
722 psp->drawLockID = 1;
723
724 psp->drm_version = *drm_version;
725 psp->ddx_version = *ddx_version;
726 psp->dri_version = *dri_version;
727
728 psp->pSAREA = pSAREA;
729
730 psp->pFB = frame_buffer->base;
731 psp->fbSize = frame_buffer->size;
732 psp->fbStride = frame_buffer->stride;
733 psp->fbWidth = frame_buffer->width;
734 psp->fbHeight = frame_buffer->height;
735 psp->devPrivSize = frame_buffer->dev_priv_size;
736 psp->pDevPriv = frame_buffer->dev_priv;
737 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
738
739 psp->extensions = emptyExtensionList;
740 psp->fd = fd;
741 psp->myNum = scrn;
742
743 /*
744 ** Do not init dummy context here; actual initialization will be
745 ** done when the first DRI context is created. Init screen priv ptr
746 ** to NULL to let CreateContext routine that it needs to be inited.
747 */
748 psp->dummyContextPriv.driScreenPriv = NULL;
749
750 psc->destroyScreen = driDestroyScreen;
751 psc->getExtensions = driGetExtensions;
752 psc->createNewDrawable = driCreateNewDrawable;
753 psc->createNewContext = driCreateNewContext;
754
755 if (internal_api_version >= 20070121)
756 psc->setTexOffset = psp->DriverAPI.setTexOffset;
757
758 *driver_modes = __driDriverInitScreen(psp);
759 if (*driver_modes == NULL) {
760 _mesa_free(psp);
761 return NULL;
762 }
763
764 return psp;
765 }
766
767 /**
768 * Compare the current GLX API version with a driver supplied required version.
769 *
770 * The minimum required version is compared with the API version exported by
771 * the \c __glXGetInternalVersion function (in libGL.so).
772 *
773 * \param required_version Minimum required internal GLX API version.
774 * \return A tri-value return, as from strcmp is returned. A value less
775 * than, equal to, or greater than zero will be returned if the
776 * internal GLX API version is less than, equal to, or greater
777 * than \c required_version.
778 *
779 * \sa __glXGetInternalVersion().
780 */
781 int driCompareGLXAPIVersion( GLint required_version )
782 {
783 if ( api_ver > required_version ) {
784 return 1;
785 }
786 else if ( api_ver == required_version ) {
787 return 0;
788 }
789
790 return -1;
791 }
792
793
794 static int
795 driFrameTracking(__DRIdrawable *drawable, GLboolean enable)
796 {
797 return GLX_BAD_CONTEXT;
798 }
799
800 static int
801 driQueryFrameTracking(__DRIdrawable *drawable,
802 int64_t * sbc, int64_t * missedFrames,
803 float * lastMissedUsage, float * usage)
804 {
805 __DRIswapInfo sInfo;
806 int status;
807 int64_t ust;
808 __DRIdrawablePrivate * dpriv = drawable->private;
809
810
811 status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
812 if ( status == 0 ) {
813 *sbc = sInfo.swap_count;
814 *missedFrames = sInfo.swap_missed_count;
815 *lastMissedUsage = sInfo.swap_missed_usage;
816
817 (*dri_interface->getUST)( & ust );
818 *usage = driCalculateSwapUsage( dpriv, sInfo.swap_ust, ust );
819 }
820
821 return status;
822 }
823
824 const __DRIframeTrackingExtension driFrameTrackingExtension = {
825 { __DRI_FRAME_TRACKING },
826 driFrameTracking,
827 driQueryFrameTracking
828 };
829
830 /**
831 * Calculate amount of swap interval used between GLX buffer swaps.
832 *
833 * The usage value, on the range [0,max], is the fraction of total swap
834 * interval time used between GLX buffer swaps is calculated.
835 *
836 * \f$p = t_d / (i * t_r)\f$
837 *
838 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
839 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
840 * required for a single vertical refresh period (as returned by \c
841 * glXGetMscRateOML).
842 *
843 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
844 * details.
845 *
846 * \param dPriv Pointer to the private drawable structure.
847 * \return If less than a single swap interval time period was required
848 * between GLX buffer swaps, a number greater than 0 and less than
849 * 1.0 is returned. If exactly one swap interval time period is
850 * required, 1.0 is returned, and if more than one is required then
851 * a number greater than 1.0 will be returned.
852 *
853 * \sa glXSwapIntervalSGI glXGetMscRateOML
854 *
855 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
856 * be possible to cache the sync rate?
857 */
858 float
859 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
860 int64_t current_ust )
861 {
862 int32_t n;
863 int32_t d;
864 int interval;
865 float usage = 1.0;
866
867
868 if ( (*dri_interface->getMSCRate)(dPriv->pdraw, &n, &d) ) {
869 interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
870
871
872 /* We want to calculate
873 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
874 * current_UST by calling __glXGetUST. last_swap_UST is stored in
875 * dPriv->swap_ust. interval has already been calculated.
876 *
877 * The only tricky part is us_per_refresh. us_per_refresh is
878 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
879 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
880 * the denominator of the final calculation, we calculate
881 * (interval * 1000000 * d) and move n into the numerator.
882 */
883
884 usage = (current_ust - last_swap_ust);
885 usage *= n;
886 usage /= (interval * d);
887 usage /= 1000000.0;
888 }
889
890 return usage;
891 }
892
893 /*@}*/