dri: Drop driCopySubBufferExtension
[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 driSetSwapInterval(__DRIdrawable *dPriv, unsigned int interval)
364 {
365 dPriv->swap_interval = interval;
366 }
367
368 static unsigned int driGetSwapInterval(__DRIdrawable *dPriv)
369 {
370 return dPriv->swap_interval;
371 }
372
373 const __DRIswapControlExtension driSwapControlExtension = {
374 { __DRI_SWAP_CONTROL, __DRI_SWAP_CONTROL_VERSION },
375 driSetSwapInterval,
376 driGetSwapInterval
377 };
378
379
380 /**
381 * This is called via __DRIscreenRec's createNewDrawable pointer.
382 */
383 static __DRIdrawable *
384 driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
385 drm_drawable_t hwDrawable, int renderType,
386 const int *attrs, void *data)
387 {
388 __DRIdrawable *pdp;
389
390 /* Since pbuffers are not yet supported, no drawable attributes are
391 * supported either.
392 */
393 (void) attrs;
394 (void) renderType;
395
396 pdp = malloc(sizeof *pdp);
397 if (!pdp) {
398 return NULL;
399 }
400
401 pdp->driContextPriv = NULL;
402 pdp->loaderPrivate = data;
403 pdp->hHWDrawable = hwDrawable;
404 pdp->refcount = 1;
405 pdp->pStamp = NULL;
406 pdp->lastStamp = 0;
407 pdp->index = 0;
408 pdp->x = 0;
409 pdp->y = 0;
410 pdp->w = 0;
411 pdp->h = 0;
412 pdp->numClipRects = 0;
413 pdp->numBackClipRects = 0;
414 pdp->pClipRects = NULL;
415 pdp->pBackClipRects = NULL;
416 pdp->vblSeq = 0;
417 pdp->vblFlags = 0;
418
419 pdp->driScreenPriv = psp;
420
421 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes, 0)) {
422 free(pdp);
423 return NULL;
424 }
425
426 pdp->msc_base = 0;
427
428 /* This special default value is replaced with the configured
429 * default value when the drawable is first bound to a direct
430 * rendering context.
431 */
432 pdp->swap_interval = (unsigned)-1;
433
434 return pdp;
435 }
436
437
438 static __DRIdrawable *
439 dri2CreateNewDrawable(__DRIscreen *screen,
440 const __DRIconfig *config,
441 void *loaderPrivate)
442 {
443 __DRIdrawable *pdraw;
444
445 pdraw = driCreateNewDrawable(screen, config, 0, 0, NULL, loaderPrivate);
446 if (!pdraw)
447 return NULL;
448
449 pdraw->pClipRects = &pdraw->dri2.clipRect;
450 pdraw->pBackClipRects = &pdraw->dri2.clipRect;
451
452 pdraw->pStamp = &pdraw->dri2.stamp;
453 *pdraw->pStamp = pdraw->lastStamp + 1;
454
455 return pdraw;
456 }
457
458 static __DRIbuffer *
459 dri2AllocateBuffer(__DRIscreen *screen,
460 unsigned int attachment, unsigned int format,
461 int width, int height)
462 {
463 return (*screen->DriverAPI.AllocateBuffer)(screen, attachment, format,
464 width, height);
465 }
466
467 static void
468 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
469 {
470 (*screen->DriverAPI.ReleaseBuffer)(screen, buffer);
471 }
472
473
474 static int
475 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
476 {
477 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
478 return -1;
479
480 *val = driQueryOptionb(&screen->optionCache, var);
481
482 return 0;
483 }
484
485 static int
486 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
487 {
488 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
489 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
490 return -1;
491
492 *val = driQueryOptioni(&screen->optionCache, var);
493
494 return 0;
495 }
496
497 static int
498 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
499 {
500 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
501 return -1;
502
503 *val = driQueryOptionf(&screen->optionCache, var);
504
505 return 0;
506 }
507
508
509 static void dri_get_drawable(__DRIdrawable *pdp)
510 {
511 pdp->refcount++;
512 }
513
514 static void dri_put_drawable(__DRIdrawable *pdp)
515 {
516 __DRIscreen *psp;
517
518 if (pdp) {
519 pdp->refcount--;
520 if (pdp->refcount)
521 return;
522
523 psp = pdp->driScreenPriv;
524 (*psp->DriverAPI.DestroyBuffer)(pdp);
525 if (pdp->pClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
526 free(pdp->pClipRects);
527 pdp->pClipRects = NULL;
528 }
529 if (pdp->pBackClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
530 free(pdp->pBackClipRects);
531 pdp->pBackClipRects = NULL;
532 }
533 free(pdp);
534 }
535 }
536
537 static void
538 driDestroyDrawable(__DRIdrawable *pdp)
539 {
540 dri_put_drawable(pdp);
541 }
542
543 /*@}*/
544
545
546 /*****************************************************************/
547 /** \name Context handling functions */
548 /*****************************************************************/
549 /*@{*/
550
551 /**
552 * Destroy the per-context private information.
553 *
554 * \internal
555 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
556 * drmDestroyContext(), and finally frees \p contextPrivate.
557 */
558 static void
559 driDestroyContext(__DRIcontext *pcp)
560 {
561 if (pcp) {
562 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
563 free(pcp);
564 }
565 }
566
567
568 /**
569 * Create the per-drawable private driver information.
570 *
571 * \param render_type Type of rendering target. \c GLX_RGBA_TYPE is the only
572 * type likely to ever be supported for direct-rendering.
573 * However, \c GLX_RGBA_FLOAT_TYPE_ARB may eventually be
574 * supported by some drivers.
575 * \param shared Context with which to share textures, etc. or NULL
576 *
577 * \returns An opaque pointer to the per-context private information on
578 * success, or \c NULL on failure.
579 *
580 * \internal
581 * This function allocates and fills a __DRIcontextRec structure. It
582 * performs some device independent initialization and passes all the
583 * relevent information to __DriverAPIRec::CreateContext to create the
584 * context.
585 *
586 */
587 static __DRIcontext *
588 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
589 int render_type, __DRIcontext *shared,
590 drm_context_t hwContext, void *data)
591 {
592 __DRIcontext *pcp;
593 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
594
595 (void) render_type;
596
597 pcp = malloc(sizeof *pcp);
598 if (!pcp)
599 return NULL;
600
601 pcp->driScreenPriv = psp;
602 pcp->driDrawablePriv = NULL;
603 pcp->loaderPrivate = data;
604
605 pcp->dri2.draw_stamp = 0;
606 pcp->dri2.read_stamp = 0;
607
608 pcp->hHWContext = hwContext;
609
610 if ( !(*psp->DriverAPI.CreateContext)(API_OPENGL,
611 &config->modes, pcp, shareCtx) ) {
612 free(pcp);
613 return NULL;
614 }
615
616 return pcp;
617 }
618
619 static unsigned int
620 dri2GetAPIMask(__DRIscreen *screen)
621 {
622 return screen->api_mask;
623 }
624
625 static __DRIcontext *
626 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
627 const __DRIconfig *config,
628 __DRIcontext *shared, void *data)
629 {
630 __DRIcontext *context;
631 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
632 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
633 gl_api mesa_api;
634
635 if (!(screen->api_mask & (1 << api)))
636 return NULL;
637
638 switch (api) {
639 case __DRI_API_OPENGL:
640 mesa_api = API_OPENGL;
641 break;
642 case __DRI_API_GLES:
643 mesa_api = API_OPENGLES;
644 break;
645 case __DRI_API_GLES2:
646 mesa_api = API_OPENGLES2;
647 break;
648 default:
649 return NULL;
650 }
651
652 context = malloc(sizeof *context);
653 if (!context)
654 return NULL;
655
656 context->driScreenPriv = screen;
657 context->driDrawablePriv = NULL;
658 context->loaderPrivate = data;
659
660 if (!(*screen->DriverAPI.CreateContext)(mesa_api, modes,
661 context, shareCtx) ) {
662 free(context);
663 return NULL;
664 }
665
666 return context;
667 }
668
669
670 static __DRIcontext *
671 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
672 __DRIcontext *shared, void *data)
673 {
674 return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
675 config, shared, data);
676 }
677
678 static int
679 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
680 {
681 (void) dest;
682 (void) src;
683 (void) mask;
684 return GL_FALSE;
685 }
686
687 /*@}*/
688
689
690 /*****************************************************************/
691 /** \name Screen handling functions */
692 /*****************************************************************/
693 /*@{*/
694
695 /**
696 * Destroy the per-screen private information.
697 *
698 * \internal
699 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
700 * drmClose(), and finally frees \p screenPrivate.
701 */
702 static void driDestroyScreen(__DRIscreen *psp)
703 {
704 if (psp) {
705 /* No interaction with the X-server is possible at this point. This
706 * routine is called after XCloseDisplay, so there is no protocol
707 * stream open to the X-server anymore.
708 */
709
710 _mesa_destroy_shader_compiler();
711
712 if (psp->DriverAPI.DestroyScreen)
713 (*psp->DriverAPI.DestroyScreen)(psp);
714
715 if (!psp->dri2.enabled) {
716 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
717 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
718 (void)drmCloseOnce(psp->fd);
719 } else {
720 driDestroyOptionCache(&psp->optionCache);
721 driDestroyOptionInfo(&psp->optionInfo);
722 }
723
724 free(psp);
725 }
726 }
727
728 static void
729 setupLoaderExtensions(__DRIscreen *psp,
730 const __DRIextension **extensions)
731 {
732 int i;
733
734 for (i = 0; extensions[i]; i++) {
735 if (strcmp(extensions[i]->name, __DRI_GET_DRAWABLE_INFO) == 0)
736 psp->getDrawableInfo = (__DRIgetDrawableInfoExtension *) extensions[i];
737 if (strcmp(extensions[i]->name, __DRI_DAMAGE) == 0)
738 psp->damage = (__DRIdamageExtension *) extensions[i];
739 if (strcmp(extensions[i]->name, __DRI_SYSTEM_TIME) == 0)
740 psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
741 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
742 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
743 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
744 psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
745 if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
746 psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
747 }
748 }
749
750 /**
751 * This is the bootstrap function for the driver. libGL supplies all of the
752 * requisite information about the system, and the driver initializes itself.
753 * This routine also fills in the linked list pointed to by \c driver_modes
754 * with the \c struct gl_config that the driver can support for windows or
755 * pbuffers.
756 *
757 * For legacy DRI.
758 *
759 * \param scrn Index of the screen
760 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
761 * all drivers.
762 * \param dri_version Version of the "server-side" DRI.
763 * \param drm_version Version of the kernel DRM.
764 * \param frame_buffer Data describing the location and layout of the
765 * framebuffer.
766 * \param pSAREA Pointer to the SAREA.
767 * \param fd Device handle for the DRM.
768 * \param extensions ??
769 * \param driver_modes Returns modes suppoted by the driver
770 * \param loaderPrivate ??
771 *
772 * \note There is no need to check the minimum API version in this
773 * function. Since the name of this function is versioned, it is
774 * impossible for a loader that is too old to even load this driver.
775 */
776 static __DRIscreen *
777 driCreateNewScreen(int scrn,
778 const __DRIversion *ddx_version,
779 const __DRIversion *dri_version,
780 const __DRIversion *drm_version,
781 const __DRIframebuffer *frame_buffer,
782 drmAddress pSAREA, int fd,
783 const __DRIextension **extensions,
784 const __DRIconfig ***driver_modes,
785 void *loaderPrivate)
786 {
787 static const __DRIextension *emptyExtensionList[] = { NULL };
788 __DRIscreen *psp;
789
790 (void) loaderPrivate;
791
792 if (driDriverAPI.InitScreen == NULL) {
793 __driUtilMessage("driver does not support DRI1");
794 return NULL;
795 }
796
797 psp = calloc(1, sizeof *psp);
798 if (!psp)
799 return NULL;
800
801 setupLoaderExtensions(psp, extensions);
802
803 /*
804 ** NOT_DONE: This is used by the X server to detect when the client
805 ** has died while holding the drawable lock. The client sets the
806 ** drawable lock to this value.
807 */
808 psp->drawLockID = 1;
809
810 psp->drm_version = *drm_version;
811 psp->ddx_version = *ddx_version;
812 psp->dri_version = *dri_version;
813
814 psp->pSAREA = pSAREA;
815 psp->lock = (drmLock *) &psp->pSAREA->lock;
816
817 psp->pFB = frame_buffer->base;
818 psp->fbSize = frame_buffer->size;
819 psp->fbStride = frame_buffer->stride;
820 psp->fbWidth = frame_buffer->width;
821 psp->fbHeight = frame_buffer->height;
822 psp->devPrivSize = frame_buffer->dev_priv_size;
823 psp->pDevPriv = frame_buffer->dev_priv;
824 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
825
826 psp->extensions = emptyExtensionList;
827 psp->fd = fd;
828 psp->myNum = scrn;
829 psp->dri2.enabled = GL_FALSE;
830
831 psp->DriverAPI = driDriverAPI;
832 psp->api_mask = (1 << __DRI_API_OPENGL);
833
834 *driver_modes = driDriverAPI.InitScreen(psp);
835 if (*driver_modes == NULL) {
836 free(psp);
837 return NULL;
838 }
839
840 return psp;
841 }
842
843 /**
844 * DRI2
845 */
846 static __DRIscreen *
847 dri2CreateNewScreen(int scrn, int fd,
848 const __DRIextension **extensions,
849 const __DRIconfig ***driver_configs, void *data)
850 {
851 static const __DRIextension *emptyExtensionList[] = { NULL };
852 __DRIscreen *psp;
853 drmVersionPtr version;
854
855 if (driDriverAPI.InitScreen2 == NULL)
856 return NULL;
857
858 psp = calloc(1, sizeof(*psp));
859 if (!psp)
860 return NULL;
861
862 setupLoaderExtensions(psp, extensions);
863
864 version = drmGetVersion(fd);
865 if (version) {
866 psp->drm_version.major = version->version_major;
867 psp->drm_version.minor = version->version_minor;
868 psp->drm_version.patch = version->version_patchlevel;
869 drmFreeVersion(version);
870 }
871
872 psp->extensions = emptyExtensionList;
873 psp->fd = fd;
874 psp->myNum = scrn;
875 psp->dri2.enabled = GL_TRUE;
876
877 psp->DriverAPI = driDriverAPI;
878 psp->api_mask = (1 << __DRI_API_OPENGL);
879 *driver_configs = driDriverAPI.InitScreen2(psp);
880 if (*driver_configs == NULL) {
881 free(psp);
882 return NULL;
883 }
884
885 psp->DriverAPI = driDriverAPI;
886 psp->loaderPrivate = data;
887
888 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions,
889 __dri2NConfigOptions);
890 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum,
891 "dri2");
892
893 return psp;
894 }
895
896 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
897 {
898 return psp->extensions;
899 }
900
901 /** Core interface */
902 const __DRIcoreExtension driCoreExtension = {
903 { __DRI_CORE, __DRI_CORE_VERSION },
904 NULL,
905 driDestroyScreen,
906 driGetExtensions,
907 driGetConfigAttrib,
908 driIndexConfigAttrib,
909 NULL,
910 driDestroyDrawable,
911 driSwapBuffers,
912 NULL,
913 driCopyContext,
914 driDestroyContext,
915 driBindContext,
916 driUnbindContext
917 };
918
919 /** DRI2 interface */
920 const __DRIdri2Extension driDRI2Extension = {
921 { __DRI_DRI2, __DRI_DRI2_VERSION },
922 dri2CreateNewScreen,
923 dri2CreateNewDrawable,
924 dri2CreateNewContext,
925 dri2GetAPIMask,
926 dri2CreateNewContextForAPI,
927 dri2AllocateBuffer,
928 dri2ReleaseBuffer
929 };
930
931 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
932 { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
933 dri2ConfigQueryb,
934 dri2ConfigQueryi,
935 dri2ConfigQueryf,
936 };
937
938 /**
939 * Calculate amount of swap interval used between GLX buffer swaps.
940 *
941 * The usage value, on the range [0,max], is the fraction of total swap
942 * interval time used between GLX buffer swaps is calculated.
943 *
944 * \f$p = t_d / (i * t_r)\f$
945 *
946 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
947 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
948 * required for a single vertical refresh period (as returned by \c
949 * glXGetMscRateOML).
950 *
951 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
952 * details.
953 *
954 * \param dPriv Pointer to the private drawable structure.
955 * \return If less than a single swap interval time period was required
956 * between GLX buffer swaps, a number greater than 0 and less than
957 * 1.0 is returned. If exactly one swap interval time period is
958 * required, 1.0 is returned, and if more than one is required then
959 * a number greater than 1.0 will be returned.
960 *
961 * \sa glXSwapIntervalSGI glXGetMscRateOML
962 *
963 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
964 * be possible to cache the sync rate?
965 */
966 float
967 driCalculateSwapUsage( __DRIdrawable *dPriv, int64_t last_swap_ust,
968 int64_t current_ust )
969 {
970 int32_t n;
971 int32_t d;
972 int interval;
973 float usage = 1.0;
974 __DRIscreen *psp = dPriv->driScreenPriv;
975
976 if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
977 interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
978
979
980 /* We want to calculate
981 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
982 * current_UST by calling __glXGetUST. last_swap_UST is stored in
983 * dPriv->swap_ust. interval has already been calculated.
984 *
985 * The only tricky part is us_per_refresh. us_per_refresh is
986 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
987 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
988 * the denominator of the final calculation, we calculate
989 * (interval * 1000000 * d) and move n into the numerator.
990 */
991
992 usage = (current_ust - last_swap_ust);
993 usage *= n;
994 usage /= (interval * d);
995 usage /= 1000000.0;
996 }
997
998 return usage;
999 }
1000
1001 void
1002 dri2InvalidateDrawable(__DRIdrawable *drawable)
1003 {
1004 drawable->dri2.stamp++;
1005 }
1006
1007 /*@}*/