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