Merge branch 'master' of git+ssh://brianp@git.freedesktop.org/git/mesa/mesa
[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) (__DRInativeDisplay *dpy, __DRIid 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 * Cached copy of the internal API version used by libGL and the client-side
51 * DRI driver.
52 */
53 static int api_ver = 0;
54
55 /* forward declarations */
56 static int driQueryFrameTracking( __DRInativeDisplay *dpy, void *priv,
57 int64_t *sbc, int64_t *missedFrames,
58 float *lastMissedUsage, float *usage );
59
60 static void *driCreateNewDrawable(__DRInativeDisplay *dpy,
61 const __GLcontextModes *modes,
62 __DRIid draw, __DRIdrawable *pdraw,
63 int renderType, const int *attrs);
64
65 static void driDestroyDrawable(__DRInativeDisplay *dpy, void *drawablePrivate);
66
67
68 /**
69 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
70 * is set.
71 *
72 * Is called from the drivers.
73 *
74 * \param f \c printf like format string.
75 */
76 void
77 __driUtilMessage(const char *f, ...)
78 {
79 va_list args;
80
81 if (getenv("LIBGL_DEBUG")) {
82 fprintf(stderr, "libGL error: \n");
83 va_start(args, f);
84 vfprintf(stderr, f, args);
85 va_end(args);
86 fprintf(stderr, "\n");
87 }
88 }
89
90
91 /*****************************************************************/
92 /** \name Drawable list management */
93 /*****************************************************************/
94 /*@{*/
95
96 static GLboolean __driAddDrawable(void *drawHash, __DRIdrawable *pdraw)
97 {
98 __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *)pdraw->private;
99
100 if (drmHashInsert(drawHash, pdp->draw, pdraw))
101 return GL_FALSE;
102
103 return GL_TRUE;
104 }
105
106 static __DRIdrawable *__driFindDrawable(void *drawHash, __DRIid draw)
107 {
108 int retcode;
109 __DRIdrawable *pdraw;
110
111 retcode = drmHashLookup(drawHash, draw, (void *)&pdraw);
112 if (retcode)
113 return NULL;
114
115 return pdraw;
116 }
117
118
119 /**
120 * Find drawables in the local hash that have been destroyed on the
121 * server.
122 *
123 * \param drawHash Hash-table containing all know drawables.
124 */
125 static void __driGarbageCollectDrawables(void *drawHash)
126 {
127 __DRIid draw;
128 __DRInativeDisplay *dpy;
129 __DRIdrawable *pdraw;
130
131 if (drmHashFirst(drawHash, &draw, (void *)&pdraw) == 1) {
132 do {
133 __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *)pdraw->private;
134 dpy = pdp->driScreenPriv->display;
135 if (! (*dri_interface->windowExists)(dpy, draw)) {
136 /* Destroy the local drawable data, if the drawable no
137 longer exists in the Xserver */
138 (*pdraw->destroyDrawable)(dpy, pdraw->private);
139 _mesa_free(pdraw);
140 }
141 } while (drmHashNext(drawHash, &draw, (void *)&pdraw) == 1);
142 }
143 }
144
145 /*@}*/
146
147
148 /*****************************************************************/
149 /** \name Context (un)binding functions */
150 /*****************************************************************/
151 /*@{*/
152
153 /**
154 * Unbind context.
155 *
156 * \param dpy the display handle.
157 * \param scrn the screen number.
158 * \param draw drawable.
159 * \param read Current reading drawable.
160 * \param gc context.
161 *
162 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
163 *
164 * \internal
165 * This function calls __DriverAPIRec::UnbindContext, and then decrements
166 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
167 * return.
168 *
169 * While casting the opaque private pointers associated with the parameters
170 * into their respective real types it also assures they are not \c NULL.
171 */
172 static GLboolean driUnbindContext(__DRInativeDisplay *dpy, int scrn,
173 __DRIid draw, __DRIid read,
174 __DRIcontext *ctx)
175 {
176 __DRIscreen *pDRIScreen;
177 __DRIdrawable *pdraw;
178 __DRIdrawable *pread;
179 __DRIcontextPrivate *pcp;
180 __DRIscreenPrivate *psp;
181 __DRIdrawablePrivate *pdp;
182 __DRIdrawablePrivate *prp;
183
184 /*
185 ** Assume error checking is done properly in glXMakeCurrent before
186 ** calling driUnbindContext.
187 */
188
189 if (ctx == NULL || draw == None || read == None) {
190 /* ERROR!!! */
191 return GL_FALSE;
192 }
193
194 pDRIScreen = (*dri_interface->getScreen)(dpy, scrn);
195 if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
196 /* ERROR!!! */
197 return GL_FALSE;
198 }
199
200 psp = (__DRIscreenPrivate *)pDRIScreen->private;
201 pcp = (__DRIcontextPrivate *)ctx->private;
202
203 pdraw = __driFindDrawable(psp->drawHash, draw);
204 if (!pdraw) {
205 /* ERROR!!! */
206 return GL_FALSE;
207 }
208 pdp = (__DRIdrawablePrivate *)pdraw->private;
209
210 pread = __driFindDrawable(psp->drawHash, read);
211 if (!pread) {
212 /* ERROR!!! */
213 return GL_FALSE;
214 }
215 prp = (__DRIdrawablePrivate *)pread->private;
216
217
218 /* Let driver unbind drawable from context */
219 (*psp->DriverAPI.UnbindContext)(pcp);
220
221
222 if (pdp->refcount == 0) {
223 /* ERROR!!! */
224 return GL_FALSE;
225 }
226
227 pdp->refcount--;
228
229 if (prp != pdp) {
230 if (prp->refcount == 0) {
231 /* ERROR!!! */
232 return GL_FALSE;
233 }
234
235 prp->refcount--;
236 }
237
238
239 /* XXX this is disabled so that if we call SwapBuffers on an unbound
240 * window we can determine the last context bound to the window and
241 * use that context's lock. (BrianP, 2-Dec-2000)
242 */
243 #if 0
244 /* Unbind the drawable */
245 pcp->driDrawablePriv = NULL;
246 pdp->driContextPriv = &psp->dummyContextPriv;
247 #endif
248
249 return GL_TRUE;
250 }
251
252
253 /**
254 * This function takes both a read buffer and a draw buffer. This is needed
255 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
256 * function.
257 *
258 * \bug This function calls \c driCreateNewDrawable in two places with the
259 * \c renderType hard-coded to \c GLX_WINDOW_BIT. Some checking might
260 * be needed in those places when support for pbuffers and / or pixmaps
261 * is added. Is it safe to assume that the drawable is a window?
262 */
263 static GLboolean DoBindContext(__DRInativeDisplay *dpy,
264 __DRIid draw, __DRIid read,
265 __DRIcontext *ctx, const __GLcontextModes * modes,
266 __DRIscreenPrivate *psp)
267 {
268 __DRIdrawable *pdraw;
269 __DRIdrawablePrivate *pdp;
270 __DRIdrawable *pread;
271 __DRIdrawablePrivate *prp;
272 __DRIcontextPrivate * const pcp = ctx->private;
273
274
275 /* Find the _DRIdrawable which corresponds to the writing drawable. */
276 pdraw = __driFindDrawable(psp->drawHash, draw);
277 if (!pdraw) {
278 /* Allocate a new drawable */
279 pdraw = (__DRIdrawable *)_mesa_malloc(sizeof(__DRIdrawable));
280 if (!pdraw) {
281 /* ERROR!!! */
282 return GL_FALSE;
283 }
284
285 /* Create a new drawable */
286 driCreateNewDrawable(dpy, modes, draw, pdraw, GLX_WINDOW_BIT,
287 empty_attribute_list);
288 if (!pdraw->private) {
289 /* ERROR!!! */
290 _mesa_free(pdraw);
291 return GL_FALSE;
292 }
293
294 }
295 pdp = (__DRIdrawablePrivate *) pdraw->private;
296
297 /* Find the _DRIdrawable which corresponds to the reading drawable. */
298 if (read == draw) {
299 /* read buffer == draw buffer */
300 prp = pdp;
301 }
302 else {
303 pread = __driFindDrawable(psp->drawHash, read);
304 if (!pread) {
305 /* Allocate a new drawable */
306 pread = (__DRIdrawable *)_mesa_malloc(sizeof(__DRIdrawable));
307 if (!pread) {
308 /* ERROR!!! */
309 return GL_FALSE;
310 }
311
312 /* Create a new drawable */
313 driCreateNewDrawable(dpy, modes, read, pread, GLX_WINDOW_BIT,
314 empty_attribute_list);
315 if (!pread->private) {
316 /* ERROR!!! */
317 _mesa_free(pread);
318 return GL_FALSE;
319 }
320 }
321 prp = (__DRIdrawablePrivate *) pread->private;
322 }
323
324 /* Bind the drawable to the context */
325 pcp->driDrawablePriv = pdp;
326 pcp->driReadablePriv = prp;
327 pdp->driContextPriv = pcp;
328 pdp->refcount++;
329 if ( pdp != prp ) {
330 prp->refcount++;
331 }
332
333 /*
334 ** Now that we have a context associated with this drawable, we can
335 ** initialize the drawable information if has not been done before.
336 */
337 if (!pdp->pStamp || *pdp->pStamp != pdp->lastStamp) {
338 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
339 __driUtilUpdateDrawableInfo(pdp);
340 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
341 }
342
343 if ((pdp != prp) && (!pdp->pStamp || *pdp->pStamp != pdp->lastStamp)) {
344 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
345 __driUtilUpdateDrawableInfo(prp);
346 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
347 }
348
349 /* Call device-specific MakeCurrent */
350 (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
351
352 return GL_TRUE;
353 }
354
355
356 /**
357 * This function takes both a read buffer and a draw buffer. This is needed
358 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
359 * function.
360 */
361 static GLboolean driBindContext(__DRInativeDisplay *dpy, int scrn,
362 __DRIid draw, __DRIid read,
363 __DRIcontext * ctx)
364 {
365 __DRIscreen *pDRIScreen;
366
367 /*
368 ** Assume error checking is done properly in glXMakeCurrent before
369 ** calling driBindContext.
370 */
371
372 if (ctx == NULL || draw == None || read == None) {
373 /* ERROR!!! */
374 return GL_FALSE;
375 }
376
377 pDRIScreen = (*dri_interface->getScreen)(dpy, scrn);
378 if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
379 /* ERROR!!! */
380 return GL_FALSE;
381 }
382
383 return DoBindContext( dpy, draw, read, ctx, ctx->mode,
384 (__DRIscreenPrivate *)pDRIScreen->private );
385 }
386 /*@}*/
387
388
389 /*****************************************************************/
390 /** \name Drawable handling functions */
391 /*****************************************************************/
392 /*@{*/
393
394 /**
395 * Update private drawable information.
396 *
397 * \param pdp pointer to the private drawable information to update.
398 *
399 * This function basically updates the __DRIdrawablePrivate struct's
400 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
401 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
402 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
403 * the values are different that means we have to update the clipping
404 * info.
405 */
406 void
407 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
408 {
409 __DRIscreenPrivate *psp;
410 __DRIcontextPrivate *pcp = pdp->driContextPriv;
411
412 if (!pcp
413 || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
414 /* ERROR!!!
415 * ...but we must ignore it. There can be many contexts bound to a
416 * drawable.
417 */
418 }
419
420 psp = pdp->driScreenPriv;
421 if (!psp) {
422 /* ERROR!!! */
423 _mesa_problem("Warning! Possible infinite loop due to bug "
424 "in file %s, line %d\n",
425 __FILE__, __LINE__);
426 return;
427 }
428
429 if (pdp->pClipRects) {
430 _mesa_free(pdp->pClipRects);
431 }
432
433 if (pdp->pBackClipRects) {
434 _mesa_free(pdp->pBackClipRects);
435 }
436
437 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
438
439 if (!__driFindDrawable(psp->drawHash, pdp->draw) ||
440 ! (*dri_interface->getDrawableInfo)(pdp->display, pdp->screen, pdp->draw,
441 &pdp->index, &pdp->lastStamp,
442 &pdp->x, &pdp->y, &pdp->w, &pdp->h,
443 &pdp->numClipRects, &pdp->pClipRects,
444 &pdp->backX,
445 &pdp->backY,
446 &pdp->numBackClipRects,
447 &pdp->pBackClipRects )) {
448 /* Error -- eg the window may have been destroyed. Keep going
449 * with no cliprects.
450 */
451 pdp->pStamp = &pdp->lastStamp; /* prevent endless loop */
452 pdp->numClipRects = 0;
453 pdp->pClipRects = NULL;
454 pdp->numBackClipRects = 0;
455 pdp->pBackClipRects = NULL;
456 }
457 else
458 pdp->pStamp = &(psp->pSAREA->drawableTable[pdp->index].stamp);
459
460 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
461
462 }
463
464 /*@}*/
465
466 /*****************************************************************/
467 /** \name GLX callbacks */
468 /*****************************************************************/
469 /*@{*/
470
471 /**
472 * Swap buffers.
473 *
474 * \param dpy the display handle.
475 * \param drawablePrivate opaque pointer to the per-drawable private info.
476 *
477 * \internal
478 * This function calls __DRIdrawablePrivate::swapBuffers.
479 *
480 * Is called directly from glXSwapBuffers().
481 */
482 static void driSwapBuffers( __DRInativeDisplay *dpy, void *drawablePrivate )
483 {
484 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
485 drm_clip_rect_t rect;
486
487 dPriv->swapBuffers(dPriv);
488
489 /* Check that we actually have the new damage report method */
490 if (api_ver < 20070105 || dri_interface->reportDamage == NULL)
491 return;
492
493 /* Assume it's affecting the whole drawable for now */
494 rect.x1 = 0;
495 rect.y1 = 0;
496 rect.x2 = rect.x1 + dPriv->w;
497 rect.y2 = rect.y1 + dPriv->h;
498
499 /* Report the damage. Currently, all our drivers draw directly to the
500 * front buffer, so we report the damage there rather than to the backing
501 * store (if any).
502 */
503 (*dri_interface->reportDamage)(dpy, dPriv->screen, dPriv->draw,
504 dPriv->x, dPriv->y,
505 &rect, 1, GL_TRUE);
506 }
507
508 /**
509 * Called directly from a number of higher-level GLX functions.
510 */
511 static int driGetMSC( void *screenPrivate, int64_t *msc )
512 {
513 __DRIscreenPrivate *sPriv = (__DRIscreenPrivate *) screenPrivate;
514
515 return sPriv->DriverAPI.GetMSC( sPriv, msc );
516 }
517
518 /**
519 * Called directly from a number of higher-level GLX functions.
520 */
521 static int driGetSBC( __DRInativeDisplay *dpy, void *drawablePrivate, int64_t *sbc )
522 {
523 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
524 __DRIswapInfo sInfo;
525 int status;
526
527
528 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
529 *sbc = sInfo.swap_count;
530
531 return status;
532 }
533
534 static int driWaitForSBC( __DRInativeDisplay * dpy, void *drawablePriv,
535 int64_t target_sbc,
536 int64_t * msc, int64_t * sbc )
537 {
538 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePriv;
539
540 return dPriv->driScreenPriv->DriverAPI.WaitForSBC( dPriv, target_sbc,
541 msc, sbc );
542 }
543
544 static int driWaitForMSC( __DRInativeDisplay * dpy, void *drawablePriv,
545 int64_t target_msc,
546 int64_t divisor, int64_t remainder,
547 int64_t * msc, int64_t * sbc )
548 {
549 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePriv;
550 __DRIswapInfo sInfo;
551 int status;
552
553
554 status = dPriv->driScreenPriv->DriverAPI.WaitForMSC( dPriv, target_msc,
555 divisor, remainder,
556 msc );
557
558 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
559 * is supported but GLX_OML_sync_control is not. Therefore, don't return
560 * an error value if GetSwapInfo() is not implemented.
561 */
562 if ( status == 0
563 && dPriv->driScreenPriv->DriverAPI.GetSwapInfo ) {
564 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
565 *sbc = sInfo.swap_count;
566 }
567
568 return status;
569 }
570
571 static int64_t driSwapBuffersMSC( __DRInativeDisplay * dpy, void *drawablePriv,
572 int64_t target_msc,
573 int64_t divisor, int64_t remainder )
574 {
575 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePriv;
576
577 return dPriv->driScreenPriv->DriverAPI.SwapBuffersMSC( dPriv, target_msc,
578 divisor,
579 remainder );
580 }
581
582 static void driCopySubBuffer( __DRInativeDisplay *dpy, void *drawablePrivate,
583 int x, int y, int w, int h)
584 {
585 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
586 dPriv->driScreenPriv->DriverAPI.CopySubBuffer(dPriv, x, y, w, h);
587 (void) dpy;
588 }
589
590 /**
591 * This is called via __DRIscreenRec's createNewDrawable pointer.
592 */
593 static void *driCreateNewDrawable(__DRInativeDisplay *dpy,
594 const __GLcontextModes *modes,
595 __DRIid draw,
596 __DRIdrawable *pdraw,
597 int renderType,
598 const int *attrs)
599 {
600 __DRIscreen * const pDRIScreen = (*dri_interface->getScreen)(dpy, modes->screen);
601 __DRIscreenPrivate *psp;
602 __DRIdrawablePrivate *pdp;
603
604
605 pdraw->private = NULL;
606
607 /* Since pbuffers are not yet supported, no drawable attributes are
608 * supported either.
609 */
610 (void) attrs;
611
612 if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
613 return NULL;
614 }
615
616 pdp = (__DRIdrawablePrivate *)_mesa_malloc(sizeof(__DRIdrawablePrivate));
617 if (!pdp) {
618 return NULL;
619 }
620
621 if (!(*dri_interface->createDrawable)(dpy, modes->screen, draw, &pdp->hHWDrawable)) {
622 _mesa_free(pdp);
623 return NULL;
624 }
625
626 pdp->draw = draw;
627 pdp->pdraw = pdraw;
628 pdp->refcount = 0;
629 pdp->pStamp = NULL;
630 pdp->lastStamp = 0;
631 pdp->index = 0;
632 pdp->x = 0;
633 pdp->y = 0;
634 pdp->w = 0;
635 pdp->h = 0;
636 pdp->numClipRects = 0;
637 pdp->numBackClipRects = 0;
638 pdp->pClipRects = NULL;
639 pdp->pBackClipRects = NULL;
640 pdp->display = dpy;
641 pdp->screen = modes->screen;
642
643 psp = (__DRIscreenPrivate *)pDRIScreen->private;
644 pdp->driScreenPriv = psp;
645 pdp->driContextPriv = &psp->dummyContextPriv;
646
647 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, modes,
648 renderType == GLX_PIXMAP_BIT)) {
649 (void)(*dri_interface->destroyDrawable)(dpy, modes->screen, pdp->draw);
650 _mesa_free(pdp);
651 return NULL;
652 }
653
654 pdraw->private = pdp;
655 pdraw->destroyDrawable = driDestroyDrawable;
656 pdraw->swapBuffers = driSwapBuffers; /* called by glXSwapBuffers() */
657
658 pdraw->getSBC = driGetSBC;
659 pdraw->waitForSBC = driWaitForSBC;
660 pdraw->waitForMSC = driWaitForMSC;
661 pdraw->swapBuffersMSC = driSwapBuffersMSC;
662 pdraw->frameTracking = NULL;
663 pdraw->queryFrameTracking = driQueryFrameTracking;
664
665 if (driCompareGLXAPIVersion (20060314) >= 0)
666 pdraw->copySubBuffer = driCopySubBuffer;
667
668 /* This special default value is replaced with the configured
669 * default value when the drawable is first bound to a direct
670 * rendering context.
671 */
672 pdraw->swap_interval = (unsigned)-1;
673
674 pdp->swapBuffers = psp->DriverAPI.SwapBuffers;
675
676 /* Add pdraw to drawable list */
677 if (!__driAddDrawable(psp->drawHash, pdraw)) {
678 /* ERROR!!! */
679 (*pdraw->destroyDrawable)(dpy, pdp);
680 _mesa_free(pdp);
681 pdp = NULL;
682 pdraw->private = NULL;
683 }
684
685 return (void *) pdp;
686 }
687
688 static __DRIdrawable *
689 driGetDrawable(__DRInativeDisplay *dpy, __DRIid draw, void *screenPrivate)
690 {
691 __DRIscreenPrivate *psp = (__DRIscreenPrivate *) screenPrivate;
692
693 /*
694 ** Make sure this routine returns NULL if the drawable is not bound
695 ** to a direct rendering context!
696 */
697 return __driFindDrawable(psp->drawHash, draw);
698 }
699
700 static void
701 driDestroyDrawable(__DRInativeDisplay *dpy, void *drawablePrivate)
702 {
703 __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *) drawablePrivate;
704 __DRIscreenPrivate *psp;
705 int scrn;
706
707 if (pdp) {
708 psp = pdp->driScreenPriv;
709 scrn = psp->myNum;
710 (*psp->DriverAPI.DestroyBuffer)(pdp);
711 if ((*dri_interface->windowExists)(dpy, pdp->draw))
712 (void)(*dri_interface->destroyDrawable)(dpy, scrn, pdp->draw);
713 drmHashDelete(psp->drawHash, pdp->draw);
714 if (pdp->pClipRects) {
715 _mesa_free(pdp->pClipRects);
716 pdp->pClipRects = NULL;
717 }
718 if (pdp->pBackClipRects) {
719 _mesa_free(pdp->pBackClipRects);
720 pdp->pBackClipRects = NULL;
721 }
722 _mesa_free(pdp);
723 }
724 }
725
726 /*@}*/
727
728
729 /*****************************************************************/
730 /** \name Context handling functions */
731 /*****************************************************************/
732 /*@{*/
733
734 /**
735 * Destroy the per-context private information.
736 *
737 * \param dpy the display handle.
738 * \param scrn the screen number.
739 * \param contextPrivate opaque pointer to the per-drawable private info.
740 *
741 * \internal
742 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
743 * drmDestroyContext(), and finally frees \p contextPrivate.
744 */
745 static void
746 driDestroyContext(__DRInativeDisplay *dpy, int scrn, void *contextPrivate)
747 {
748 __DRIcontextPrivate *pcp = (__DRIcontextPrivate *) contextPrivate;
749
750 if (pcp) {
751 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
752 __driGarbageCollectDrawables(pcp->driScreenPriv->drawHash);
753 (void) (*dri_interface->destroyContext)(dpy, scrn, pcp->contextID);
754 _mesa_free(pcp);
755 }
756 }
757
758
759 /**
760 * Create the per-drawable private driver information.
761 *
762 * \param dpy The display handle.
763 * \param modes Mode used to create the new context.
764 * \param render_type Type of rendering target. \c GLX_RGBA is the only
765 * type likely to ever be supported for direct-rendering.
766 * \param sharedPrivate The shared context dependent methods or \c NULL if
767 * non-existent.
768 * \param pctx DRI context to receive the context dependent methods.
769 *
770 * \returns An opaque pointer to the per-context private information on
771 * success, or \c NULL on failure.
772 *
773 * \internal
774 * This function allocates and fills a __DRIcontextPrivateRec structure. It
775 * performs some device independent initialization and passes all the
776 * relevent information to __DriverAPIRec::CreateContext to create the
777 * context.
778 *
779 */
780 static void *
781 driCreateNewContext(__DRInativeDisplay *dpy, const __GLcontextModes *modes,
782 int render_type, void *sharedPrivate, __DRIcontext *pctx)
783 {
784 __DRIscreen *pDRIScreen;
785 __DRIcontextPrivate *pcp;
786 __DRIcontextPrivate *pshare = (__DRIcontextPrivate *) sharedPrivate;
787 __DRIscreenPrivate *psp;
788 void * const shareCtx = (pshare != NULL) ? pshare->driverPrivate : NULL;
789
790 pDRIScreen = (*dri_interface->getScreen)(dpy, modes->screen);
791 if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
792 /* ERROR!!! */
793 return NULL;
794 }
795
796 psp = (__DRIscreenPrivate *)pDRIScreen->private;
797
798 pcp = (__DRIcontextPrivate *)_mesa_malloc(sizeof(__DRIcontextPrivate));
799 if (!pcp) {
800 return NULL;
801 }
802
803 if (! (*dri_interface->createContext)(dpy, modes->screen, modes->fbconfigID,
804 &pcp->contextID, &pcp->hHWContext)) {
805 _mesa_free(pcp);
806 return NULL;
807 }
808
809 pcp->display = dpy;
810 pcp->driScreenPriv = psp;
811 pcp->driDrawablePriv = NULL;
812
813 /* When the first context is created for a screen, initialize a "dummy"
814 * context.
815 */
816
817 if (!psp->dummyContextPriv.driScreenPriv) {
818 psp->dummyContextPriv.contextID = 0;
819 psp->dummyContextPriv.hHWContext = psp->pSAREA->dummy_context;
820 psp->dummyContextPriv.driScreenPriv = psp;
821 psp->dummyContextPriv.driDrawablePriv = NULL;
822 psp->dummyContextPriv.driverPrivate = NULL;
823 /* No other fields should be used! */
824 }
825
826 pctx->destroyContext = driDestroyContext;
827 pctx->bindContext = driBindContext;
828 pctx->unbindContext = driUnbindContext;
829
830 if ( !(*psp->DriverAPI.CreateContext)(modes, pcp, shareCtx) ) {
831 (void) (*dri_interface->destroyContext)(dpy, modes->screen,
832 pcp->contextID);
833 _mesa_free(pcp);
834 return NULL;
835 }
836
837 __driGarbageCollectDrawables(pcp->driScreenPriv->drawHash);
838
839 return pcp;
840 }
841 /*@}*/
842
843
844 /*****************************************************************/
845 /** \name Screen handling functions */
846 /*****************************************************************/
847 /*@{*/
848
849 /**
850 * Destroy the per-screen private information.
851 *
852 * \param dpy the display handle.
853 * \param scrn the screen number.
854 * \param screenPrivate opaque pointer to the per-screen private information.
855 *
856 * \internal
857 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
858 * drmClose(), and finally frees \p screenPrivate.
859 */
860 static void driDestroyScreen(__DRInativeDisplay *dpy, int scrn, void *screenPrivate)
861 {
862 __DRIscreenPrivate *psp = (__DRIscreenPrivate *) screenPrivate;
863
864 if (psp) {
865 /* No interaction with the X-server is possible at this point. This
866 * routine is called after XCloseDisplay, so there is no protocol
867 * stream open to the X-server anymore.
868 */
869
870 if (psp->DriverAPI.DestroyScreen)
871 (*psp->DriverAPI.DestroyScreen)(psp);
872
873 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
874 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
875 _mesa_free(psp->pDevPriv);
876 (void)drmCloseOnce(psp->fd);
877 if ( psp->modes != NULL ) {
878 (*dri_interface->destroyContextModes)( psp->modes );
879 }
880
881 assert(psp->drawHash);
882 drmHashDestroy(psp->drawHash);
883
884 _mesa_free(psp);
885 }
886 }
887
888
889 /**
890 * Utility function used to create a new driver-private screen structure.
891 *
892 * \param dpy Display pointer
893 * \param scrn Index of the screen
894 * \param psc DRI screen data (not driver private)
895 * \param modes Linked list of known display modes. This list is, at a
896 * minimum, a list of modes based on the current display mode.
897 * These roughly match the set of available X11 visuals, but it
898 * need not be limited to X11! The calling libGL should create
899 * a list that will inform the driver of the current display
900 * mode (i.e., color buffer depth, depth buffer depth, etc.).
901 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
902 * all drivers.
903 * \param dri_version Version of the "server-side" DRI.
904 * \param drm_version Version of the kernel DRM.
905 * \param frame_buffer Data describing the location and layout of the
906 * framebuffer.
907 * \param pSAREA Pointer the the SAREA.
908 * \param fd Device handle for the DRM.
909 * \param internal_api_version Version of the internal interface between the
910 * driver and libGL.
911 * \param driverAPI Driver API functions used by other routines in dri_util.c.
912 *
913 * \note
914 * There is no need to check the minimum API version in this function. Since
915 * the \c __driCreateNewScreen function is versioned, it is impossible for a
916 * loader that is too old to even load this driver.
917 */
918 __DRIscreenPrivate *
919 __driUtilCreateNewScreen(__DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
920 __GLcontextModes * modes,
921 const __DRIversion * ddx_version,
922 const __DRIversion * dri_version,
923 const __DRIversion * drm_version,
924 const __DRIframebuffer * frame_buffer,
925 drm_sarea_t *pSAREA,
926 int fd,
927 int internal_api_version,
928 const struct __DriverAPIRec *driverAPI)
929 {
930 __DRIscreenPrivate *psp;
931
932
933 api_ver = internal_api_version;
934
935 psp = (__DRIscreenPrivate *)_mesa_malloc(sizeof(__DRIscreenPrivate));
936 if (!psp) {
937 return NULL;
938 }
939
940 /* Create the hash table */
941 psp->drawHash = drmHashCreate();
942 if ( psp->drawHash == NULL ) {
943 _mesa_free( psp );
944 return NULL;
945 }
946
947 psp->display = dpy;
948 psp->myNum = scrn;
949 psp->psc = psc;
950 psp->modes = modes;
951
952 /*
953 ** NOT_DONE: This is used by the X server to detect when the client
954 ** has died while holding the drawable lock. The client sets the
955 ** drawable lock to this value.
956 */
957 psp->drawLockID = 1;
958
959 psp->drmMajor = drm_version->major;
960 psp->drmMinor = drm_version->minor;
961 psp->drmPatch = drm_version->patch;
962 psp->ddxMajor = ddx_version->major;
963 psp->ddxMinor = ddx_version->minor;
964 psp->ddxPatch = ddx_version->patch;
965 psp->driMajor = dri_version->major;
966 psp->driMinor = dri_version->minor;
967 psp->driPatch = dri_version->patch;
968
969 /* install driver's callback functions */
970 memcpy( &psp->DriverAPI, driverAPI, sizeof(struct __DriverAPIRec) );
971
972 psp->pSAREA = pSAREA;
973
974 psp->pFB = frame_buffer->base;
975 psp->fbSize = frame_buffer->size;
976 psp->fbStride = frame_buffer->stride;
977 psp->fbWidth = frame_buffer->width;
978 psp->fbHeight = frame_buffer->height;
979 psp->devPrivSize = frame_buffer->dev_priv_size;
980 psp->pDevPriv = frame_buffer->dev_priv;
981 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
982
983 psp->fd = fd;
984
985 /*
986 ** Do not init dummy context here; actual initialization will be
987 ** done when the first DRI context is created. Init screen priv ptr
988 ** to NULL to let CreateContext routine that it needs to be inited.
989 */
990 psp->dummyContextPriv.driScreenPriv = NULL;
991
992 psc->destroyScreen = driDestroyScreen;
993 psc->createNewDrawable = driCreateNewDrawable;
994 psc->getDrawable = driGetDrawable;
995 psc->getMSC = driGetMSC;
996 psc->createNewContext = driCreateNewContext;
997
998 if ( (psp->DriverAPI.InitDriver != NULL)
999 && !(*psp->DriverAPI.InitDriver)(psp) ) {
1000 _mesa_free( psp );
1001 return NULL;
1002 }
1003
1004
1005 return psp;
1006 }
1007
1008
1009 /**
1010 * Compare the current GLX API version with a driver supplied required version.
1011 *
1012 * The minimum required version is compared with the API version exported by
1013 * the \c __glXGetInternalVersion function (in libGL.so).
1014 *
1015 * \param required_version Minimum required internal GLX API version.
1016 * \return A tri-value return, as from strcmp is returned. A value less
1017 * than, equal to, or greater than zero will be returned if the
1018 * internal GLX API version is less than, equal to, or greater
1019 * than \c required_version.
1020 *
1021 * \sa __glXGetInternalVersion().
1022 */
1023 int driCompareGLXAPIVersion( GLint required_version )
1024 {
1025 if ( api_ver > required_version ) {
1026 return 1;
1027 }
1028 else if ( api_ver == required_version ) {
1029 return 0;
1030 }
1031
1032 return -1;
1033 }
1034
1035
1036 static int
1037 driQueryFrameTracking( __DRInativeDisplay * dpy, void * priv,
1038 int64_t * sbc, int64_t * missedFrames,
1039 float * lastMissedUsage, float * usage )
1040 {
1041 __DRIswapInfo sInfo;
1042 int status;
1043 int64_t ust;
1044 __DRIdrawablePrivate * dpriv = (__DRIdrawablePrivate *) priv;
1045
1046
1047 status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
1048 if ( status == 0 ) {
1049 *sbc = sInfo.swap_count;
1050 *missedFrames = sInfo.swap_missed_count;
1051 *lastMissedUsage = sInfo.swap_missed_usage;
1052
1053 (*dri_interface->getUST)( & ust );
1054 *usage = driCalculateSwapUsage( dpriv, sInfo.swap_ust, ust );
1055 }
1056
1057 return status;
1058 }
1059
1060
1061 /**
1062 * Calculate amount of swap interval used between GLX buffer swaps.
1063 *
1064 * The usage value, on the range [0,max], is the fraction of total swap
1065 * interval time used between GLX buffer swaps is calculated.
1066 *
1067 * \f$p = t_d / (i * t_r)\f$
1068 *
1069 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1070 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1071 * required for a single vertical refresh period (as returned by \c
1072 * glXGetMscRateOML).
1073 *
1074 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1075 * details.
1076 *
1077 * \param dPriv Pointer to the private drawable structure.
1078 * \return If less than a single swap interval time period was required
1079 * between GLX buffer swaps, a number greater than 0 and less than
1080 * 1.0 is returned. If exactly one swap interval time period is
1081 * required, 1.0 is returned, and if more than one is required then
1082 * a number greater than 1.0 will be returned.
1083 *
1084 * \sa glXSwapIntervalSGI glXGetMscRateOML
1085 *
1086 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1087 * be possible to cache the sync rate?
1088 */
1089 float
1090 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
1091 int64_t current_ust )
1092 {
1093 int32_t n;
1094 int32_t d;
1095 int interval;
1096 float usage = 1.0;
1097
1098
1099 if ( (*dri_interface->getMSCRate)( dPriv->display, dPriv->draw, &n, &d ) ) {
1100 interval = (dPriv->pdraw->swap_interval != 0)
1101 ? dPriv->pdraw->swap_interval : 1;
1102
1103
1104 /* We want to calculate
1105 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1106 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1107 * dPriv->swap_ust. interval has already been calculated.
1108 *
1109 * The only tricky part is us_per_refresh. us_per_refresh is
1110 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1111 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1112 * the denominator of the final calculation, we calculate
1113 * (interval * 1000000 * d) and move n into the numerator.
1114 */
1115
1116 usage = (current_ust - last_swap_ust);
1117 usage *= n;
1118 usage /= (interval * d);
1119 usage /= 1000000.0;
1120 }
1121
1122 return usage;
1123 }
1124
1125 /*@}*/