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