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