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