glx: Make drawables persistent untill they are changed by glXMakeCurrent
[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
35 #ifndef GLX_OML_sync_control
36 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
37 #endif
38
39 static void dri_get_drawable(__DRIdrawable *pdp);
40 static void dri_put_drawable(__DRIdrawable *pdp);
41
42 /**
43 * This is just a token extension used to signal that the driver
44 * supports setting a read drawable.
45 */
46 const __DRIextension driReadDrawableExtension = {
47 __DRI_READ_DRAWABLE, __DRI_READ_DRAWABLE_VERSION
48 };
49
50 /**
51 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
52 * is set.
53 *
54 * Is called from the drivers.
55 *
56 * \param f \c printf like format string.
57 */
58 void
59 __driUtilMessage(const char *f, ...)
60 {
61 va_list args;
62
63 if (getenv("LIBGL_DEBUG")) {
64 fprintf(stderr, "libGL: ");
65 va_start(args, f);
66 vfprintf(stderr, f, args);
67 va_end(args);
68 fprintf(stderr, "\n");
69 }
70 }
71
72 GLint
73 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
74 {
75 if (rect2.x1 > rect1.x1) rect1.x1 = rect2.x1;
76 if (rect2.x2 < rect1.x2) rect1.x2 = rect2.x2;
77 if (rect2.y1 > rect1.y1) rect1.y1 = rect2.y1;
78 if (rect2.y2 < rect1.y2) rect1.y2 = rect2.y2;
79
80 if (rect1.x1 > rect1.x2 || rect1.y1 > rect1.y2) return 0;
81
82 return (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
83 }
84
85 static int driFreeDrawable(__DRIcontext *pcp)
86 {
87 __DRIdrawable *pdp;
88 __DRIdrawable *prp;
89
90 if (pcp == NULL)
91 return GL_FALSE;
92
93 pdp = pcp->driDrawablePriv;
94 prp = pcp->driReadablePriv;
95
96 /* already unbound */
97 if (!pdp && !prp)
98 return GL_TRUE;
99
100 if (pdp->refcount == 0) {
101 /* ERROR!!! */
102 return GL_FALSE;
103 }
104
105 dri_put_drawable(pdp);
106
107 if (prp != pdp) {
108 if (prp->refcount == 0) {
109 /* ERROR!!! */
110 return GL_FALSE;
111 }
112
113 dri_put_drawable(prp);
114 }
115
116
117 /* XXX this is disabled so that if we call SwapBuffers on an unbound
118 * window we can determine the last context bound to the window and
119 * use that context's lock. (BrianP, 2-Dec-2000)
120 */
121 pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
122 return GL_TRUE;
123 }
124
125 /*****************************************************************/
126 /** \name Context (un)binding functions */
127 /*****************************************************************/
128 /*@{*/
129
130 /**
131 * Unbind context.
132 *
133 * \param scrn the screen.
134 * \param gc context.
135 *
136 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
137 *
138 * \internal
139 * This function calls __DriverAPIRec::UnbindContext, and then decrements
140 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
141 * return.
142 *
143 * While casting the opaque private pointers associated with the parameters
144 * into their respective real types it also assures they are not \c NULL.
145 */
146 static int driUnbindContext(__DRIcontext *pcp)
147 {
148 __DRIscreen *psp;
149
150 /*
151 ** Assume error checking is done properly in glXMakeCurrent before
152 ** calling driUnbindContext.
153 */
154
155 if (pcp == NULL)
156 return GL_FALSE;
157
158 psp = pcp->driScreenPriv;
159
160 /* Let driver unbind drawable from context */
161 (*psp->DriverAPI.UnbindContext)(pcp);
162
163 #if 0
164 /* Unbind the drawable */
165 pdp->driContextPriv = &psp->dummyContextPriv;
166 #endif
167
168 return GL_TRUE;
169 }
170
171 /**
172 * This function takes both a read buffer and a draw buffer. This is needed
173 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
174 * function.
175 */
176 static int driBindContext(__DRIcontext *pcp,
177 __DRIdrawable *pdp,
178 __DRIdrawable *prp)
179 {
180 __DRIscreenPrivate *psp = pcp->driScreenPriv;
181
182 /* Bind the drawable to the context */
183
184 if (pcp) {
185
186 if (pcp->driDrawablePriv != pdp
187 || pcp->driReadablePriv != prp)
188 {
189 /* first increment ref count for new drawables */
190
191 if (pdp)
192 {
193 pdp->driContextPriv = pcp;
194 dri_get_drawable(pdp);
195 }
196
197 if (prp && prp != pdp)
198 {
199 dri_get_drawable(prp);
200 }
201
202 /* free old drawables */
203
204 if (pcp->driReadablePriv
205 && pcp->driReadablePriv != pcp->driDrawablePriv)
206 {
207 dri_put_drawable(pcp->driReadablePriv);
208 }
209
210 if (pcp->driDrawablePriv)
211 {
212 dri_put_drawable(pcp->driDrawablePriv);
213 }
214
215 /* assign new drawables to context */
216
217 pcp->driDrawablePriv = pdp;
218 pcp->driReadablePriv = prp;
219
220 }
221 }
222
223 /*
224 ** Now that we have a context associated with this drawable, we can
225 ** initialize the drawable information if has not been done before.
226 */
227
228 if (!psp->dri2.enabled) {
229 if (pdp && !pdp->pStamp) {
230 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
231 __driUtilUpdateDrawableInfo(pdp);
232 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
233 }
234 if (prp && pdp != prp && !prp->pStamp) {
235 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
236 __driUtilUpdateDrawableInfo(prp);
237 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
238 }
239 }
240
241 /* Call device-specific MakeCurrent */
242
243 return (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
244 }
245
246 /*@}*/
247
248
249 /*****************************************************************/
250 /** \name Drawable handling functions */
251 /*****************************************************************/
252 /*@{*/
253
254 /**
255 * Update private drawable information.
256 *
257 * \param pdp pointer to the private drawable information to update.
258 *
259 * This function basically updates the __DRIdrawablePrivate struct's
260 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
261 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
262 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
263 * the values are different that means we have to update the clipping
264 * info.
265 */
266 void
267 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
268 {
269 __DRIscreenPrivate *psp = pdp->driScreenPriv;
270 __DRIcontextPrivate *pcp = pdp->driContextPriv;
271
272 if (!pcp
273 || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
274 /* ERROR!!!
275 * ...but we must ignore it. There can be many contexts bound to a
276 * drawable.
277 */
278 }
279
280 if (pdp->pClipRects) {
281 _mesa_free(pdp->pClipRects);
282 pdp->pClipRects = NULL;
283 }
284
285 if (pdp->pBackClipRects) {
286 _mesa_free(pdp->pBackClipRects);
287 pdp->pBackClipRects = NULL;
288 }
289
290 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
291
292 if (! (*psp->getDrawableInfo->getDrawableInfo)(pdp,
293 &pdp->index, &pdp->lastStamp,
294 &pdp->x, &pdp->y, &pdp->w, &pdp->h,
295 &pdp->numClipRects, &pdp->pClipRects,
296 &pdp->backX,
297 &pdp->backY,
298 &pdp->numBackClipRects,
299 &pdp->pBackClipRects,
300 pdp->loaderPrivate)) {
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 /** \name GLX callbacks */
320 /*****************************************************************/
321 /*@{*/
322
323 static void driReportDamage(__DRIdrawable *pdp,
324 struct drm_clip_rect *pClipRects, int numClipRects)
325 {
326 __DRIscreen *psp = pdp->driScreenPriv;
327
328 /* Check that we actually have the new damage report method */
329 if (psp->damage) {
330 /* Report the damage. Currently, all our drivers draw
331 * directly to the front buffer, so we report the damage there
332 * rather than to the backing storein (if any).
333 */
334 (*psp->damage->reportDamage)(pdp,
335 pdp->x, pdp->y,
336 pClipRects, numClipRects,
337 GL_TRUE, pdp->loaderPrivate);
338 }
339 }
340
341
342 /**
343 * Swap buffers.
344 *
345 * \param drawablePrivate opaque pointer to the per-drawable private info.
346 *
347 * \internal
348 * This function calls __DRIdrawablePrivate::swapBuffers.
349 *
350 * Is called directly from glXSwapBuffers().
351 */
352 static void driSwapBuffers(__DRIdrawable *dPriv)
353 {
354 __DRIscreen *psp = dPriv->driScreenPriv;
355 drm_clip_rect_t *rects;
356 int i;
357
358 psp->DriverAPI.SwapBuffers(dPriv);
359
360 if (!dPriv->numClipRects)
361 return;
362
363 rects = _mesa_malloc(sizeof(*rects) * dPriv->numClipRects);
364
365 if (!rects)
366 return;
367
368 for (i = 0; i < dPriv->numClipRects; i++) {
369 rects[i].x1 = dPriv->pClipRects[i].x1 - dPriv->x;
370 rects[i].y1 = dPriv->pClipRects[i].y1 - dPriv->y;
371 rects[i].x2 = dPriv->pClipRects[i].x2 - dPriv->x;
372 rects[i].y2 = dPriv->pClipRects[i].y2 - dPriv->y;
373 }
374
375 driReportDamage(dPriv, rects, dPriv->numClipRects);
376 _mesa_free(rects);
377 }
378
379 static int driDrawableGetMSC( __DRIscreen *sPriv, __DRIdrawable *dPriv,
380 int64_t *msc )
381 {
382 return sPriv->DriverAPI.GetDrawableMSC(sPriv, dPriv, msc);
383 }
384
385
386 static int driWaitForMSC(__DRIdrawable *dPriv, int64_t target_msc,
387 int64_t divisor, int64_t remainder,
388 int64_t * msc, int64_t * sbc)
389 {
390 __DRIswapInfo sInfo;
391 int status;
392
393 status = dPriv->driScreenPriv->DriverAPI.WaitForMSC( dPriv, target_msc,
394 divisor, remainder,
395 msc );
396
397 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
398 * is supported but GLX_OML_sync_control is not. Therefore, don't return
399 * an error value if GetSwapInfo() is not implemented.
400 */
401 if ( status == 0
402 && dPriv->driScreenPriv->DriverAPI.GetSwapInfo ) {
403 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
404 *sbc = sInfo.swap_count;
405 }
406
407 return status;
408 }
409
410
411 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension = {
412 { __DRI_MEDIA_STREAM_COUNTER, __DRI_MEDIA_STREAM_COUNTER_VERSION },
413 driWaitForMSC,
414 driDrawableGetMSC,
415 };
416
417
418 static void driCopySubBuffer(__DRIdrawable *dPriv,
419 int x, int y, int w, int h)
420 {
421 drm_clip_rect_t rect;
422
423 rect.x1 = x;
424 rect.y1 = dPriv->h - y - h;
425 rect.x2 = x + w;
426 rect.y2 = rect.y1 + h;
427 driReportDamage(dPriv, &rect, 1);
428
429 dPriv->driScreenPriv->DriverAPI.CopySubBuffer(dPriv, x, y, w, h);
430 }
431
432 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
433 { __DRI_COPY_SUB_BUFFER, __DRI_COPY_SUB_BUFFER_VERSION },
434 driCopySubBuffer
435 };
436
437 static void driSetSwapInterval(__DRIdrawable *dPriv, unsigned int interval)
438 {
439 dPriv->swap_interval = interval;
440 }
441
442 static unsigned int driGetSwapInterval(__DRIdrawable *dPriv)
443 {
444 return dPriv->swap_interval;
445 }
446
447 const __DRIswapControlExtension driSwapControlExtension = {
448 { __DRI_SWAP_CONTROL, __DRI_SWAP_CONTROL_VERSION },
449 driSetSwapInterval,
450 driGetSwapInterval
451 };
452
453
454 /**
455 * This is called via __DRIscreenRec's createNewDrawable pointer.
456 */
457 static __DRIdrawable *
458 driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
459 drm_drawable_t hwDrawable, int renderType,
460 const int *attrs, void *data)
461 {
462 __DRIdrawable *pdp;
463
464 /* Since pbuffers are not yet supported, no drawable attributes are
465 * supported either.
466 */
467 (void) attrs;
468
469 pdp = _mesa_malloc(sizeof *pdp);
470 if (!pdp) {
471 return NULL;
472 }
473
474 pdp->loaderPrivate = data;
475 pdp->hHWDrawable = hwDrawable;
476 pdp->refcount = 1;
477 pdp->pStamp = NULL;
478 pdp->lastStamp = 0;
479 pdp->index = 0;
480 pdp->x = 0;
481 pdp->y = 0;
482 pdp->w = 0;
483 pdp->h = 0;
484 pdp->numClipRects = 0;
485 pdp->numBackClipRects = 0;
486 pdp->pClipRects = NULL;
487 pdp->pBackClipRects = NULL;
488 pdp->vblSeq = 0;
489 pdp->vblFlags = 0;
490
491 pdp->driScreenPriv = psp;
492 pdp->driContextPriv = &psp->dummyContextPriv;
493
494 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes,
495 renderType == GLX_PIXMAP_BIT)) {
496 _mesa_free(pdp);
497 return NULL;
498 }
499
500 pdp->msc_base = 0;
501
502 /* This special default value is replaced with the configured
503 * default value when the drawable is first bound to a direct
504 * rendering context.
505 */
506 pdp->swap_interval = (unsigned)-1;
507
508 return pdp;
509 }
510
511
512 static __DRIdrawable *
513 dri2CreateNewDrawable(__DRIscreen *screen,
514 const __DRIconfig *config,
515 void *loaderPrivate)
516 {
517 __DRIdrawable *pdraw;
518
519 pdraw = driCreateNewDrawable(screen, config, 0, 0, NULL, loaderPrivate);
520 if (!pdraw)
521 return NULL;
522
523 pdraw->pClipRects = _mesa_malloc(sizeof *pdraw->pBackClipRects);
524 pdraw->pBackClipRects = _mesa_malloc(sizeof *pdraw->pBackClipRects);
525
526 return pdraw;
527 }
528
529 static void dri_get_drawable(__DRIdrawable *pdp)
530 {
531 pdp->refcount++;
532 }
533
534 static void dri_put_drawable(__DRIdrawable *pdp)
535 {
536 __DRIscreenPrivate *psp;
537
538 pdp->refcount--;
539 if (pdp->refcount)
540 return;
541
542 if (pdp) {
543 psp = pdp->driScreenPriv;
544 (*psp->DriverAPI.DestroyBuffer)(pdp);
545 if (pdp->pClipRects) {
546 _mesa_free(pdp->pClipRects);
547 pdp->pClipRects = NULL;
548 }
549 if (pdp->pBackClipRects) {
550 _mesa_free(pdp->pBackClipRects);
551 pdp->pBackClipRects = NULL;
552 }
553 _mesa_free(pdp);
554 }
555 }
556
557 static void
558 driDestroyDrawable(__DRIdrawable *pdp)
559 {
560 dri_put_drawable(pdp);
561 }
562
563 /*@}*/
564
565
566 /*****************************************************************/
567 /** \name Context handling functions */
568 /*****************************************************************/
569 /*@{*/
570
571 /**
572 * Destroy the per-context private information.
573 *
574 * \internal
575 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
576 * drmDestroyContext(), and finally frees \p contextPrivate.
577 */
578 static void
579 driDestroyContext(__DRIcontext *pcp)
580 {
581 if (pcp) {
582 driFreeDrawable(pcp);
583 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
584 _mesa_free(pcp);
585 }
586 }
587
588
589 /**
590 * Create the per-drawable private driver information.
591 *
592 * \param render_type Type of rendering target. \c GLX_RGBA is the only
593 * type likely to ever be supported for direct-rendering.
594 * \param shared Context with which to share textures, etc. or NULL
595 *
596 * \returns An opaque pointer to the per-context private information on
597 * success, or \c NULL on failure.
598 *
599 * \internal
600 * This function allocates and fills a __DRIcontextPrivateRec structure. It
601 * performs some device independent initialization and passes all the
602 * relevent information to __DriverAPIRec::CreateContext to create the
603 * context.
604 *
605 */
606 static __DRIcontext *
607 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
608 int render_type, __DRIcontext *shared,
609 drm_context_t hwContext, void *data)
610 {
611 __DRIcontext *pcp;
612 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
613
614 pcp = _mesa_malloc(sizeof *pcp);
615 if (!pcp)
616 return NULL;
617
618 pcp->driScreenPriv = psp;
619 pcp->driDrawablePriv = NULL;
620 pcp->driReadablePriv = NULL;
621
622 /* When the first context is created for a screen, initialize a "dummy"
623 * context.
624 */
625
626 if (!psp->dri2.enabled && !psp->dummyContextPriv.driScreenPriv) {
627 psp->dummyContextPriv.hHWContext = psp->pSAREA->dummy_context;
628 psp->dummyContextPriv.driScreenPriv = psp;
629 psp->dummyContextPriv.driDrawablePriv = NULL;
630 psp->dummyContextPriv.driverPrivate = NULL;
631 /* No other fields should be used! */
632 }
633
634 pcp->hHWContext = hwContext;
635
636 if ( !(*psp->DriverAPI.CreateContext)(&config->modes, pcp, shareCtx) ) {
637 _mesa_free(pcp);
638 return NULL;
639 }
640
641 return pcp;
642 }
643
644
645 static __DRIcontext *
646 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
647 __DRIcontext *shared, void *data)
648 {
649 return driCreateNewContext(screen, config, 0, shared, 0, data);
650 }
651
652
653 static int
654 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
655 {
656 return GL_FALSE;
657 }
658
659 /*@}*/
660
661
662 /*****************************************************************/
663 /** \name Screen handling functions */
664 /*****************************************************************/
665 /*@{*/
666
667 /**
668 * Destroy the per-screen private information.
669 *
670 * \internal
671 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
672 * drmClose(), and finally frees \p screenPrivate.
673 */
674 static void driDestroyScreen(__DRIscreen *psp)
675 {
676 if (psp) {
677 /* No interaction with the X-server is possible at this point. This
678 * routine is called after XCloseDisplay, so there is no protocol
679 * stream open to the X-server anymore.
680 */
681
682 if (psp->DriverAPI.DestroyScreen)
683 (*psp->DriverAPI.DestroyScreen)(psp);
684
685 if (!psp->dri2.enabled) {
686 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
687 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
688 (void)drmCloseOnce(psp->fd);
689 }
690
691 _mesa_free(psp);
692 }
693 }
694
695 static void
696 setupLoaderExtensions(__DRIscreen *psp,
697 const __DRIextension **extensions)
698 {
699 int i;
700
701 for (i = 0; extensions[i]; i++) {
702 if (strcmp(extensions[i]->name, __DRI_GET_DRAWABLE_INFO) == 0)
703 psp->getDrawableInfo = (__DRIgetDrawableInfoExtension *) extensions[i];
704 if (strcmp(extensions[i]->name, __DRI_DAMAGE) == 0)
705 psp->damage = (__DRIdamageExtension *) extensions[i];
706 if (strcmp(extensions[i]->name, __DRI_SYSTEM_TIME) == 0)
707 psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
708 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
709 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
710 }
711 }
712
713 /**
714 * This is the bootstrap function for the driver. libGL supplies all of the
715 * requisite information about the system, and the driver initializes itself.
716 * This routine also fills in the linked list pointed to by \c driver_modes
717 * with the \c __GLcontextModes that the driver can support for windows or
718 * pbuffers.
719 *
720 * For legacy DRI.
721 *
722 * \param scrn Index of the screen
723 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
724 * all drivers.
725 * \param dri_version Version of the "server-side" DRI.
726 * \param drm_version Version of the kernel DRM.
727 * \param frame_buffer Data describing the location and layout of the
728 * framebuffer.
729 * \param pSAREA Pointer the the SAREA.
730 * \param fd Device handle for the DRM.
731 * \param extensions ??
732 * \param driver_modes Returns modes suppoted by the driver
733 * \param loaderPrivate ??
734 *
735 * \note There is no need to check the minimum API version in this
736 * function. Since the name of this function is versioned, it is
737 * impossible for a loader that is too old to even load this driver.
738 */
739 static __DRIscreen *
740 driCreateNewScreen(int scrn,
741 const __DRIversion *ddx_version,
742 const __DRIversion *dri_version,
743 const __DRIversion *drm_version,
744 const __DRIframebuffer *frame_buffer,
745 drmAddress pSAREA, int fd,
746 const __DRIextension **extensions,
747 const __DRIconfig ***driver_modes,
748 void *loaderPrivate)
749 {
750 static const __DRIextension *emptyExtensionList[] = { NULL };
751 __DRIscreen *psp;
752
753 psp = _mesa_calloc(sizeof *psp);
754 if (!psp)
755 return NULL;
756
757 setupLoaderExtensions(psp, extensions);
758
759 /*
760 ** NOT_DONE: This is used by the X server to detect when the client
761 ** has died while holding the drawable lock. The client sets the
762 ** drawable lock to this value.
763 */
764 psp->drawLockID = 1;
765
766 psp->drm_version = *drm_version;
767 psp->ddx_version = *ddx_version;
768 psp->dri_version = *dri_version;
769
770 psp->pSAREA = pSAREA;
771 psp->lock = (drmLock *) &psp->pSAREA->lock;
772
773 psp->pFB = frame_buffer->base;
774 psp->fbSize = frame_buffer->size;
775 psp->fbStride = frame_buffer->stride;
776 psp->fbWidth = frame_buffer->width;
777 psp->fbHeight = frame_buffer->height;
778 psp->devPrivSize = frame_buffer->dev_priv_size;
779 psp->pDevPriv = frame_buffer->dev_priv;
780 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
781
782 psp->extensions = emptyExtensionList;
783 psp->fd = fd;
784 psp->myNum = scrn;
785 psp->dri2.enabled = GL_FALSE;
786
787 /*
788 ** Do not init dummy context here; actual initialization will be
789 ** done when the first DRI context is created. Init screen priv ptr
790 ** to NULL to let CreateContext routine that it needs to be inited.
791 */
792 psp->dummyContextPriv.driScreenPriv = NULL;
793
794 psp->DriverAPI = driDriverAPI;
795
796 *driver_modes = driDriverAPI.InitScreen(psp);
797 if (*driver_modes == NULL) {
798 _mesa_free(psp);
799 return NULL;
800 }
801
802 return psp;
803 }
804
805 /**
806 * DRI2
807 */
808 static __DRIscreen *
809 dri2CreateNewScreen(int scrn, int fd,
810 const __DRIextension **extensions,
811 const __DRIconfig ***driver_configs, void *data)
812 {
813 static const __DRIextension *emptyExtensionList[] = { NULL };
814 __DRIscreen *psp;
815 drmVersionPtr version;
816
817 if (driDriverAPI.InitScreen2 == NULL)
818 return NULL;
819
820 psp = _mesa_calloc(sizeof(*psp));
821 if (!psp)
822 return NULL;
823
824 setupLoaderExtensions(psp, extensions);
825
826 version = drmGetVersion(fd);
827 if (version) {
828 psp->drm_version.major = version->version_major;
829 psp->drm_version.minor = version->version_minor;
830 psp->drm_version.patch = version->version_patchlevel;
831 drmFreeVersion(version);
832 }
833
834 psp->extensions = emptyExtensionList;
835 psp->fd = fd;
836 psp->myNum = scrn;
837 psp->dri2.enabled = GL_TRUE;
838
839 psp->DriverAPI = driDriverAPI;
840 *driver_configs = driDriverAPI.InitScreen2(psp);
841 if (*driver_configs == NULL) {
842 _mesa_free(psp);
843 return NULL;
844 }
845
846 psp->DriverAPI = driDriverAPI;
847
848 return psp;
849 }
850
851 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
852 {
853 return psp->extensions;
854 }
855
856 /** Core interface */
857 const __DRIcoreExtension driCoreExtension = {
858 { __DRI_CORE, __DRI_CORE_VERSION },
859 NULL,
860 driDestroyScreen,
861 driGetExtensions,
862 driGetConfigAttrib,
863 driIndexConfigAttrib,
864 NULL,
865 driDestroyDrawable,
866 driSwapBuffers,
867 NULL,
868 driCopyContext,
869 driDestroyContext,
870 driBindContext,
871 driUnbindContext
872 };
873
874 /** Legacy DRI interface */
875 const __DRIlegacyExtension driLegacyExtension = {
876 { __DRI_LEGACY, __DRI_LEGACY_VERSION },
877 driCreateNewScreen,
878 driCreateNewDrawable,
879 driCreateNewContext,
880 };
881
882 /** Legacy DRI interface */
883 const __DRIdri2Extension driDRI2Extension = {
884 { __DRI_DRI2, __DRI_DRI2_VERSION },
885 dri2CreateNewScreen,
886 dri2CreateNewDrawable,
887 dri2CreateNewContext,
888 };
889
890 /* This is the table of extensions that the loader will dlsym() for. */
891 PUBLIC const __DRIextension *__driDriverExtensions[] = {
892 &driCoreExtension.base,
893 &driLegacyExtension.base,
894 &driDRI2Extension.base,
895 NULL
896 };
897
898 static int
899 driFrameTracking(__DRIdrawable *drawable, GLboolean enable)
900 {
901 return GLX_BAD_CONTEXT;
902 }
903
904 static int
905 driQueryFrameTracking(__DRIdrawable *dpriv,
906 int64_t * sbc, int64_t * missedFrames,
907 float * lastMissedUsage, float * usage)
908 {
909 __DRIswapInfo sInfo;
910 int status;
911 int64_t ust;
912 __DRIscreenPrivate *psp = dpriv->driScreenPriv;
913
914 status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
915 if ( status == 0 ) {
916 *sbc = sInfo.swap_count;
917 *missedFrames = sInfo.swap_missed_count;
918 *lastMissedUsage = sInfo.swap_missed_usage;
919
920 (*psp->systemTime->getUST)( & ust );
921 *usage = driCalculateSwapUsage( dpriv, sInfo.swap_ust, ust );
922 }
923
924 return status;
925 }
926
927 const __DRIframeTrackingExtension driFrameTrackingExtension = {
928 { __DRI_FRAME_TRACKING, __DRI_FRAME_TRACKING_VERSION },
929 driFrameTracking,
930 driQueryFrameTracking
931 };
932
933 /**
934 * Calculate amount of swap interval used between GLX buffer swaps.
935 *
936 * The usage value, on the range [0,max], is the fraction of total swap
937 * interval time used between GLX buffer swaps is calculated.
938 *
939 * \f$p = t_d / (i * t_r)\f$
940 *
941 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
942 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
943 * required for a single vertical refresh period (as returned by \c
944 * glXGetMscRateOML).
945 *
946 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
947 * details.
948 *
949 * \param dPriv Pointer to the private drawable structure.
950 * \return If less than a single swap interval time period was required
951 * between GLX buffer swaps, a number greater than 0 and less than
952 * 1.0 is returned. If exactly one swap interval time period is
953 * required, 1.0 is returned, and if more than one is required then
954 * a number greater than 1.0 will be returned.
955 *
956 * \sa glXSwapIntervalSGI glXGetMscRateOML
957 *
958 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
959 * be possible to cache the sync rate?
960 */
961 float
962 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
963 int64_t current_ust )
964 {
965 int32_t n;
966 int32_t d;
967 int interval;
968 float usage = 1.0;
969 __DRIscreenPrivate *psp = dPriv->driScreenPriv;
970
971 if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
972 interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
973
974
975 /* We want to calculate
976 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
977 * current_UST by calling __glXGetUST. last_swap_UST is stored in
978 * dPriv->swap_ust. interval has already been calculated.
979 *
980 * The only tricky part is us_per_refresh. us_per_refresh is
981 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
982 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
983 * the denominator of the final calculation, we calculate
984 * (interval * 1000000 * d) and move n into the numerator.
985 */
986
987 usage = (current_ust - last_swap_ust);
988 usage *= n;
989 usage /= (interval * d);
990 usage /= 1000000.0;
991 }
992
993 return usage;
994 }
995
996 /*@}*/