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