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