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