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