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