Check for NULL pointer
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
1 /* $XFree86: xc/lib/GL/dri/dri_util.c,v 1.7 2003/04/28 17:01:25 dawes Exp $ */
2 /**
3 * \file dri_util.c
4 * DRI utility functions.
5 *
6 * This module acts as glue between GLX and the actual hardware driver. A DRI
7 * driver doesn't really \e have to use any of this - it's optional. But, some
8 * useful stuff is done here that otherwise would have to be duplicated in most
9 * drivers.
10 *
11 * Basically, these utility functions take care of some of the dirty details of
12 * screen initialization, context creation, context binding, DRM setup, etc.
13 *
14 * These functions are compiled into each DRI driver so libGL.so knows nothing
15 * about them.
16 */
17
18
19 #include <assert.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <stdio.h>
24
25 #ifndef MAP_FAILED
26 #define MAP_FAILED ((void *)-1)
27 #endif
28
29 #include "imports.h"
30 #define None 0
31
32 #include "dri_util.h"
33 #include "drm_sarea.h"
34 #include "utils.h"
35
36 #ifndef GLX_OML_sync_control
37 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
38 #endif
39
40 /**
41 * This is just a token extension used to signal that the driver
42 * supports setting a read drawable.
43 */
44 const __DRIextension driReadDrawableExtension = {
45 __DRI_READ_DRAWABLE, __DRI_READ_DRAWABLE_VERSION
46 };
47
48 /**
49 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
50 * is set.
51 *
52 * Is called from the drivers.
53 *
54 * \param f \c printf like format string.
55 */
56 void
57 __driUtilMessage(const char *f, ...)
58 {
59 va_list args;
60
61 if (getenv("LIBGL_DEBUG")) {
62 fprintf(stderr, "libGL error: \n");
63 va_start(args, f);
64 vfprintf(stderr, f, args);
65 va_end(args);
66 fprintf(stderr, "\n");
67 }
68 }
69
70 GLint
71 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
72 {
73 if (rect2.x1 > rect1.x1) rect1.x1 = rect2.x1;
74 if (rect2.x2 < rect1.x2) rect1.x2 = rect2.x2;
75 if (rect2.y1 > rect1.y1) rect1.y1 = rect2.y1;
76 if (rect2.y2 < rect1.y2) rect1.y2 = rect2.y2;
77
78 if (rect1.x1 > rect1.x2 || rect1.y1 > rect1.y2) return 0;
79
80 return (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
81 }
82
83 /*****************************************************************/
84 /** \name Context (un)binding functions */
85 /*****************************************************************/
86 /*@{*/
87
88 /**
89 * Unbind context.
90 *
91 * \param scrn the screen.
92 * \param gc context.
93 *
94 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
95 *
96 * \internal
97 * This function calls __DriverAPIRec::UnbindContext, and then decrements
98 * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
99 * return.
100 *
101 * While casting the opaque private pointers associated with the parameters
102 * into their respective real types it also assures they are not \c NULL.
103 */
104 static int driUnbindContext(__DRIcontext *pcp)
105 {
106 __DRIscreen *psp;
107 __DRIdrawable *pdp;
108 __DRIdrawable *prp;
109
110 /*
111 ** Assume error checking is done properly in glXMakeCurrent before
112 ** calling driUnbindContext.
113 */
114
115 if (pcp == NULL)
116 return GL_FALSE;
117
118 psp = pcp->driScreenPriv;
119 pdp = pcp->driDrawablePriv;
120 prp = pcp->driReadablePriv;
121
122 /* Let driver unbind drawable from context */
123 (*psp->DriverAPI.UnbindContext)(pcp);
124
125 if (pdp->refcount == 0) {
126 /* ERROR!!! */
127 return GL_FALSE;
128 }
129
130 pdp->refcount--;
131
132 if (prp != pdp) {
133 if (prp->refcount == 0) {
134 /* ERROR!!! */
135 return GL_FALSE;
136 }
137
138 prp->refcount--;
139 }
140
141
142 /* XXX this is disabled so that if we call SwapBuffers on an unbound
143 * window we can determine the last context bound to the window and
144 * use that context's lock. (BrianP, 2-Dec-2000)
145 */
146 #if 0
147 /* Unbind the drawable */
148 pcp->driDrawablePriv = NULL;
149 pdp->driContextPriv = &psp->dummyContextPriv;
150 #endif
151
152 return GL_TRUE;
153 }
154
155
156 /**
157 * This function takes both a read buffer and a draw buffer. This is needed
158 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
159 * function.
160 */
161 static int driBindContext(__DRIcontext *pcp,
162 __DRIdrawable *pdp,
163 __DRIdrawable *prp)
164 {
165 __DRIscreenPrivate *psp = pcp->driScreenPriv;
166
167 /*
168 ** Assume error checking is done properly in glXMakeCurrent before
169 ** calling driBindContext.
170 */
171
172 if (pcp == NULL || pdp == None || prp == None)
173 return GL_FALSE;
174
175 /* Bind the drawable to the context */
176 pcp->driDrawablePriv = pdp;
177 pcp->driReadablePriv = prp;
178 pdp->driContextPriv = pcp;
179 pdp->refcount++;
180 if ( pdp != prp ) {
181 prp->refcount++;
182 }
183
184 /*
185 ** Now that we have a context associated with this drawable, we can
186 ** initialize the drawable information if has not been done before.
187 */
188
189 if (psp->dri2.enabled) {
190 __driParseEvents(pcp, pdp);
191 __driParseEvents(pcp, prp);
192 } else {
193 if (!pdp->pStamp || *pdp->pStamp != pdp->lastStamp) {
194 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
195 __driUtilUpdateDrawableInfo(pdp);
196 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
197 }
198
199 if ((pdp != prp) && (!prp->pStamp || *prp->pStamp != prp->lastStamp)) {
200 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
201 __driUtilUpdateDrawableInfo(prp);
202 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
203 }
204 }
205
206 /* Call device-specific MakeCurrent */
207 (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
208
209 return GL_TRUE;
210 }
211
212 /*@}*/
213
214
215 /*****************************************************************/
216 /** \name Drawable handling functions */
217 /*****************************************************************/
218 /*@{*/
219
220 /**
221 * Update private drawable information.
222 *
223 * \param pdp pointer to the private drawable information to update.
224 *
225 * This function basically updates the __DRIdrawablePrivate struct's
226 * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
227 * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
228 * compares the __DRIdrwablePrivate pStamp and lastStamp values. If
229 * the values are different that means we have to update the clipping
230 * info.
231 */
232 void
233 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
234 {
235 __DRIscreenPrivate *psp = pdp->driScreenPriv;
236 __DRIcontextPrivate *pcp = pdp->driContextPriv;
237
238 if (!pcp
239 || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
240 /* ERROR!!!
241 * ...but we must ignore it. There can be many contexts bound to a
242 * drawable.
243 */
244 }
245
246 if (pdp->pClipRects) {
247 _mesa_free(pdp->pClipRects);
248 pdp->pClipRects = NULL;
249 }
250
251 if (pdp->pBackClipRects) {
252 _mesa_free(pdp->pBackClipRects);
253 pdp->pBackClipRects = NULL;
254 }
255
256 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
257
258 if (! (*psp->getDrawableInfo->getDrawableInfo)(pdp,
259 &pdp->index, &pdp->lastStamp,
260 &pdp->x, &pdp->y, &pdp->w, &pdp->h,
261 &pdp->numClipRects, &pdp->pClipRects,
262 &pdp->backX,
263 &pdp->backY,
264 &pdp->numBackClipRects,
265 &pdp->pBackClipRects,
266 pdp->loaderPrivate)) {
267 /* Error -- eg the window may have been destroyed. Keep going
268 * with no cliprects.
269 */
270 pdp->pStamp = &pdp->lastStamp; /* prevent endless loop */
271 pdp->numClipRects = 0;
272 pdp->pClipRects = NULL;
273 pdp->numBackClipRects = 0;
274 pdp->pBackClipRects = NULL;
275 }
276 else
277 pdp->pStamp = &(psp->pSAREA->drawableTable[pdp->index].stamp);
278
279 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
280
281 }
282
283 int
284 __driParseEvents(__DRIcontextPrivate *pcp, __DRIdrawablePrivate *pdp)
285 {
286 __DRIscreenPrivate *psp = pcp->driScreenPriv;
287 __DRIDrawableConfigEvent *dc, *last_dc;
288 __DRIBufferAttachEvent *ba, *last_ba;
289 unsigned int tail, mask, *p, end, total, size, changed;
290 unsigned char *data;
291 size_t rect_size;
292
293 /* Check for wraparound. */
294 if (psp->dri2.buffer->prealloc - pdp->dri2.tail > psp->dri2.buffer->size) {
295 /* If prealloc overlaps into what we just parsed, the
296 * server overwrote it and we have to reset our tail
297 * pointer. */
298 DRM_UNLOCK(psp->fd, psp->lock, pcp->hHWContext);
299 (*psp->dri2.loader->reemitDrawableInfo)(pdp, &pdp->dri2.tail,
300 pdp->loaderPrivate);
301 DRM_LIGHT_LOCK(psp->fd, psp->lock, pcp->hHWContext);
302 }
303
304 total = psp->dri2.buffer->head - pdp->dri2.tail;
305 mask = psp->dri2.buffer->size - 1;
306 end = psp->dri2.buffer->head;
307 data = psp->dri2.buffer->data;
308
309 changed = 0;
310 last_dc = NULL;
311 last_ba = NULL;
312
313 for (tail = pdp->dri2.tail; tail != end; tail += size) {
314 p = (unsigned int *) (data + (tail & mask));
315 size = DRI2_EVENT_SIZE(*p);
316 if (size > total || (tail & mask) + size > psp->dri2.buffer->size) {
317 /* illegal data, bail out. */
318 fprintf(stderr, "illegal event size\n");
319 break;
320 }
321
322 switch (DRI2_EVENT_TYPE(*p)) {
323 case DRI2_EVENT_DRAWABLE_CONFIG:
324 dc = (__DRIDrawableConfigEvent *) p;
325 if (dc->drawable == pdp->dri2.drawable_id)
326 last_dc = dc;
327 break;
328
329 case DRI2_EVENT_BUFFER_ATTACH:
330 ba = (__DRIBufferAttachEvent *) p;
331 if (ba->drawable == pdp->dri2.drawable_id &&
332 ba->buffer.attachment == DRI_DRAWABLE_BUFFER_FRONT_LEFT)
333 last_ba = ba;
334 break;
335 }
336 }
337
338 if (last_dc) {
339 if (pdp->w != last_dc->width || pdp->h != last_dc->height)
340 changed = 1;
341
342 pdp->x = last_dc->x;
343 pdp->y = last_dc->y;
344 pdp->w = last_dc->width;
345 pdp->h = last_dc->height;
346
347 pdp->backX = 0;
348 pdp->backY = 0;
349 pdp->numBackClipRects = 1;
350 pdp->pBackClipRects[0].x1 = 0;
351 pdp->pBackClipRects[0].y1 = 0;
352 pdp->pBackClipRects[0].x2 = pdp->w;
353 pdp->pBackClipRects[0].y2 = pdp->h;
354
355 pdp->numClipRects = last_dc->num_rects;
356 _mesa_free(pdp->pClipRects);
357 rect_size = last_dc->num_rects * sizeof last_dc->rects[0];
358 pdp->pClipRects = _mesa_malloc(rect_size);
359 memcpy(pdp->pClipRects, last_dc->rects, rect_size);
360 }
361
362 /* We only care about the most recent drawable config. */
363 if (last_dc && changed)
364 (*psp->DriverAPI.HandleDrawableConfig)(pdp, pcp, last_dc);
365
366 /* Front buffer attachments are special, they typically mean that
367 * we're rendering to a redirected window (or a child window of a
368 * redirected window) and that it got resized. Resizing the root
369 * window on randr events is a special case of this. Other causes
370 * may be a window transitioning between redirected and
371 * non-redirected, or a window getting reparented between parents
372 * with different window pixmaps (eg two redirected windows).
373 * These events are special in that the X server allocates the
374 * buffer and that the buffer may be shared by other child
375 * windows. When our window share the window pixmap with its
376 * parent, drawable config events doesn't affect the front buffer.
377 * We only care about the last such event in the buffer; in fact,
378 * older events will refer to invalid buffer objects.*/
379 if (last_ba)
380 (*psp->DriverAPI.HandleBufferAttach)(pdp, pcp, last_ba);
381
382 /* If there was a drawable config event in the buffer and it
383 * changed the size of the window, all buffer auxillary buffer
384 * attachments prior to that are invalid (as opposed to the front
385 * buffer case discussed above). In that case we can start
386 * looking for buffer attachment after the last drawable config
387 * event. If there is no drawable config event in this batch of
388 * events, we have to assume that the last batch might have had
389 * one and process all buffer attach events.*/
390 if (last_dc && changed)
391 tail = (unsigned char *) last_dc - data;
392 else
393 tail = pdp->dri2.tail;
394
395 for ( ; tail != end; tail += size) {
396 ba = (__DRIBufferAttachEvent *) (data + (tail & mask));
397 size = DRI2_EVENT_SIZE(ba->event_header);
398
399 if (DRI2_EVENT_TYPE(ba->event_header) != DRI2_EVENT_BUFFER_ATTACH)
400 continue;
401 if (ba->drawable != pdp->dri2.drawable_id)
402 continue;
403 if (last_ba == ba)
404 continue;
405
406 (*psp->DriverAPI.HandleBufferAttach)(pdp, pcp, ba);
407 changed = 1;
408 }
409
410 pdp->dri2.tail = tail;
411
412 return changed || last_ba;
413 }
414
415 /*@}*/
416
417 /*****************************************************************/
418 /** \name GLX callbacks */
419 /*****************************************************************/
420 /*@{*/
421
422 static void driReportDamage(__DRIdrawable *pdp,
423 struct drm_clip_rect *pClipRects, int numClipRects)
424 {
425 __DRIscreen *psp = pdp->driScreenPriv;
426
427 /* Check that we actually have the new damage report method */
428 if (psp->dri2.enabled) {
429 (*psp->dri2.loader->postDamage)(pdp,
430 pClipRects,
431 numClipRects,
432 pdp->loaderPrivate);
433 } else if (psp->damage) {
434 /* Report the damage. Currently, all our drivers draw
435 * directly to the front buffer, so we report the damage there
436 * rather than to the backing storein (if any).
437 */
438 (*psp->damage->reportDamage)(pdp,
439 pdp->x, pdp->y,
440 pClipRects, numClipRects,
441 GL_TRUE, pdp->loaderPrivate);
442 }
443 }
444
445
446 /**
447 * Swap buffers.
448 *
449 * \param drawablePrivate opaque pointer to the per-drawable private info.
450 *
451 * \internal
452 * This function calls __DRIdrawablePrivate::swapBuffers.
453 *
454 * Is called directly from glXSwapBuffers().
455 */
456 static void driSwapBuffers(__DRIdrawable *dPriv)
457 {
458 __DRIscreen *psp = dPriv->driScreenPriv;
459
460 if (!dPriv->numClipRects)
461 return;
462
463 psp->DriverAPI.SwapBuffers(dPriv);
464
465 driReportDamage(dPriv, dPriv->pClipRects, dPriv->numClipRects);
466 }
467
468 static int driDrawableGetMSC( __DRIscreen *sPriv, __DRIdrawable *dPriv,
469 int64_t *msc )
470 {
471 return sPriv->DriverAPI.GetDrawableMSC(sPriv, dPriv, msc);
472 }
473
474 static int driWaitForMSC(__DRIdrawable *dPriv, int64_t target_msc,
475 int64_t divisor, int64_t remainder,
476 int64_t * msc, int64_t * sbc)
477 {
478 __DRIswapInfo sInfo;
479 int status;
480
481
482 status = dPriv->driScreenPriv->DriverAPI.WaitForMSC( dPriv, target_msc,
483 divisor, remainder,
484 msc );
485
486 /* GetSwapInfo() may not be provided by the driver if GLX_SGI_video_sync
487 * is supported but GLX_OML_sync_control is not. Therefore, don't return
488 * an error value if GetSwapInfo() is not implemented.
489 */
490 if ( status == 0
491 && dPriv->driScreenPriv->DriverAPI.GetSwapInfo ) {
492 status = dPriv->driScreenPriv->DriverAPI.GetSwapInfo( dPriv, & sInfo );
493 *sbc = sInfo.swap_count;
494 }
495
496 return status;
497 }
498
499 const __DRImediaStreamCounterExtension driMediaStreamCounterExtension = {
500 { __DRI_MEDIA_STREAM_COUNTER, __DRI_MEDIA_STREAM_COUNTER_VERSION },
501 driWaitForMSC,
502 driDrawableGetMSC,
503 };
504
505 static void driCopySubBuffer(__DRIdrawable *dPriv,
506 int x, int y, int w, int h)
507 {
508 drm_clip_rect_t rect;
509
510 dPriv->driScreenPriv->DriverAPI.CopySubBuffer(dPriv, x, y, w, h);
511
512 rect.x1 = x;
513 rect.y1 = dPriv->h - y - h;
514 rect.x2 = x + w;
515 rect.y2 = rect.y1 + h;
516 driReportDamage(dPriv, &rect, 1);
517 }
518
519 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
520 { __DRI_COPY_SUB_BUFFER, __DRI_COPY_SUB_BUFFER_VERSION },
521 driCopySubBuffer
522 };
523
524 static void driSetSwapInterval(__DRIdrawable *dPriv, unsigned int interval)
525 {
526 dPriv->swap_interval = interval;
527 }
528
529 static unsigned int driGetSwapInterval(__DRIdrawable *dPriv)
530 {
531 return dPriv->swap_interval;
532 }
533
534 const __DRIswapControlExtension driSwapControlExtension = {
535 { __DRI_SWAP_CONTROL, __DRI_SWAP_CONTROL_VERSION },
536 driSetSwapInterval,
537 driGetSwapInterval
538 };
539
540
541 /**
542 * This is called via __DRIscreenRec's createNewDrawable pointer.
543 */
544 static __DRIdrawable *
545 driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
546 drm_drawable_t hwDrawable, int renderType,
547 const int *attrs, void *data)
548 {
549 __DRIdrawable *pdp;
550
551 /* Since pbuffers are not yet supported, no drawable attributes are
552 * supported either.
553 */
554 (void) attrs;
555
556 pdp = _mesa_malloc(sizeof *pdp);
557 if (!pdp) {
558 return NULL;
559 }
560
561 pdp->loaderPrivate = data;
562 pdp->hHWDrawable = hwDrawable;
563 pdp->refcount = 0;
564 pdp->pStamp = NULL;
565 pdp->lastStamp = 0;
566 pdp->index = 0;
567 pdp->x = 0;
568 pdp->y = 0;
569 pdp->w = 0;
570 pdp->h = 0;
571 pdp->numClipRects = 0;
572 pdp->numBackClipRects = 0;
573 pdp->pClipRects = NULL;
574 pdp->pBackClipRects = NULL;
575 pdp->vblSeq = 0;
576 pdp->vblFlags = 0;
577
578 pdp->driScreenPriv = psp;
579 pdp->driContextPriv = &psp->dummyContextPriv;
580
581 if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes,
582 renderType == GLX_PIXMAP_BIT)) {
583 _mesa_free(pdp);
584 return NULL;
585 }
586
587 pdp->msc_base = 0;
588
589 /* This special default value is replaced with the configured
590 * default value when the drawable is first bound to a direct
591 * rendering context.
592 */
593 pdp->swap_interval = (unsigned)-1;
594
595 return pdp;
596 }
597
598 static __DRIdrawable *
599 dri2CreateNewDrawable(__DRIscreen *screen, const __DRIconfig *config,
600 unsigned int drawable_id, unsigned int head, void *data)
601 {
602 __DRIdrawable *pdraw;
603
604 pdraw = driCreateNewDrawable(screen, config, 0, 0, NULL, data);
605 if (!pdraw)
606 return NULL;
607
608 pdraw->dri2.drawable_id = drawable_id;
609 pdraw->dri2.tail = head;
610 pdraw->pBackClipRects = _mesa_malloc(sizeof *pdraw->pBackClipRects);
611
612 return pdraw;
613 }
614
615
616 static void
617 driDestroyDrawable(__DRIdrawable *pdp)
618 {
619 __DRIscreenPrivate *psp;
620
621 if (pdp) {
622 psp = pdp->driScreenPriv;
623 (*psp->DriverAPI.DestroyBuffer)(pdp);
624 if (pdp->pClipRects) {
625 _mesa_free(pdp->pClipRects);
626 pdp->pClipRects = NULL;
627 }
628 if (pdp->pBackClipRects) {
629 _mesa_free(pdp->pBackClipRects);
630 pdp->pBackClipRects = NULL;
631 }
632 _mesa_free(pdp);
633 }
634 }
635
636 /*@}*/
637
638
639 /*****************************************************************/
640 /** \name Context handling functions */
641 /*****************************************************************/
642 /*@{*/
643
644 /**
645 * Destroy the per-context private information.
646 *
647 * \param contextPrivate opaque pointer to the per-drawable private info.
648 *
649 * \internal
650 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
651 * drmDestroyContext(), and finally frees \p contextPrivate.
652 */
653 static void
654 driDestroyContext(__DRIcontext *pcp)
655 {
656 if (pcp) {
657 (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
658 _mesa_free(pcp);
659 }
660 }
661
662
663 /**
664 * Create the per-drawable private driver information.
665 *
666 * \param dpy The display handle.
667 * \param modes Mode used to create the new context.
668 * \param render_type Type of rendering target. \c GLX_RGBA is the only
669 * type likely to ever be supported for direct-rendering.
670 * \param shared The shared context dependent methods or \c NULL if
671 * non-existent.
672 * \param pctx DRI context to receive the context dependent methods.
673 *
674 * \returns An opaque pointer to the per-context private information on
675 * success, or \c NULL on failure.
676 *
677 * \internal
678 * This function allocates and fills a __DRIcontextPrivateRec structure. It
679 * performs some device independent initialization and passes all the
680 * relevent information to __DriverAPIRec::CreateContext to create the
681 * context.
682 *
683 */
684 static __DRIcontext *
685 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
686 int render_type, __DRIcontext *shared,
687 drm_context_t hwContext, void *data)
688 {
689 __DRIcontext *pcp;
690 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
691
692 pcp = _mesa_malloc(sizeof *pcp);
693 if (!pcp)
694 return NULL;
695
696 pcp->driScreenPriv = psp;
697 pcp->driDrawablePriv = NULL;
698
699 /* When the first context is created for a screen, initialize a "dummy"
700 * context.
701 */
702
703 if (!psp->dri2.enabled && !psp->dummyContextPriv.driScreenPriv) {
704 psp->dummyContextPriv.hHWContext = psp->pSAREA->dummy_context;
705 psp->dummyContextPriv.driScreenPriv = psp;
706 psp->dummyContextPriv.driDrawablePriv = NULL;
707 psp->dummyContextPriv.driverPrivate = NULL;
708 /* No other fields should be used! */
709 }
710
711 pcp->hHWContext = hwContext;
712
713 if ( !(*psp->DriverAPI.CreateContext)(&config->modes, pcp, shareCtx) ) {
714 _mesa_free(pcp);
715 return NULL;
716 }
717
718 return pcp;
719 }
720
721 static __DRIcontext *
722 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
723 __DRIcontext *shared, void *data)
724 {
725 drm_context_t hwContext;
726 DRM_CAS_RESULT(ret);
727
728 /* DRI2 doesn't use kernel with context IDs, we just need an ID that's
729 * different from the kernel context ID to make drmLock() happy. */
730
731 do {
732 hwContext = screen->dri2.lock->next_id;
733 DRM_CAS(&screen->dri2.lock->next_id, hwContext, hwContext + 1, ret);
734 } while (ret);
735
736 return driCreateNewContext(screen, config, 0, shared, hwContext, data);
737 }
738
739 static int
740 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
741 {
742 return GL_FALSE;
743 }
744
745 /*@}*/
746
747
748 /*****************************************************************/
749 /** \name Screen handling functions */
750 /*****************************************************************/
751 /*@{*/
752
753 /**
754 * Destroy the per-screen private information.
755 *
756 * \param dpy the display handle.
757 * \param scrn the screen number.
758 * \param screenPrivate opaque pointer to the per-screen private information.
759 *
760 * \internal
761 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
762 * drmClose(), and finally frees \p screenPrivate.
763 */
764 static void driDestroyScreen(__DRIscreen *psp)
765 {
766 if (psp) {
767 /* No interaction with the X-server is possible at this point. This
768 * routine is called after XCloseDisplay, so there is no protocol
769 * stream open to the X-server anymore.
770 */
771
772 if (psp->DriverAPI.DestroyScreen)
773 (*psp->DriverAPI.DestroyScreen)(psp);
774
775 if (psp->dri2.enabled) {
776 #ifdef TTM_API
777 drmBOUnmap(psp->fd, &psp->dri2.sareaBO);
778 drmBOUnreference(psp->fd, &psp->dri2.sareaBO);
779 #endif
780 } else {
781 (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
782 (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
783 (void)drmCloseOnce(psp->fd);
784 }
785
786 _mesa_free(psp);
787 }
788 }
789
790 static void
791 setupLoaderExtensions(__DRIscreen *psp,
792 const __DRIextension **extensions)
793 {
794 int i;
795
796 for (i = 0; extensions[i]; i++) {
797 if (strcmp(extensions[i]->name, __DRI_GET_DRAWABLE_INFO) == 0)
798 psp->getDrawableInfo = (__DRIgetDrawableInfoExtension *) extensions[i];
799 if (strcmp(extensions[i]->name, __DRI_DAMAGE) == 0)
800 psp->damage = (__DRIdamageExtension *) extensions[i];
801 if (strcmp(extensions[i]->name, __DRI_SYSTEM_TIME) == 0)
802 psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
803 if (strcmp(extensions[i]->name, __DRI_LOADER) == 0)
804 psp->dri2.loader = (__DRIloaderExtension *) extensions[i];
805 }
806 }
807
808 /**
809 * This is the bootstrap function for the driver. libGL supplies all of the
810 * requisite information about the system, and the driver initializes itself.
811 * This routine also fills in the linked list pointed to by \c driver_modes
812 * with the \c __GLcontextModes that the driver can support for windows or
813 * pbuffers.
814 *
815 * \param scrn Index of the screen
816 * \param psc DRI screen data (not driver private)
817 * \param modes Linked list of known display modes. This list is, at a
818 * minimum, a list of modes based on the current display mode.
819 * These roughly match the set of available X11 visuals, but it
820 * need not be limited to X11! The calling libGL should create
821 * a list that will inform the driver of the current display
822 * mode (i.e., color buffer depth, depth buffer depth, etc.).
823 * \param ddx_version Version of the 2D DDX. This may not be meaningful for
824 * all drivers.
825 * \param dri_version Version of the "server-side" DRI.
826 * \param drm_version Version of the kernel DRM.
827 * \param frame_buffer Data describing the location and layout of the
828 * framebuffer.
829 * \param pSAREA Pointer the the SAREA.
830 * \param fd Device handle for the DRM.
831 * \param internal_api_version Version of the internal interface between the
832 * driver and libGL.
833 * \param driverAPI Driver API functions used by other routines in dri_util.c.
834 *
835 * \note There is no need to check the minimum API version in this
836 * function. Since the name of this function is versioned, it is
837 * impossible for a loader that is too old to even load this driver.
838 */
839 static __DRIscreen *
840 driCreateNewScreen(int scrn,
841 const __DRIversion *ddx_version,
842 const __DRIversion *dri_version,
843 const __DRIversion *drm_version,
844 const __DRIframebuffer *frame_buffer,
845 drmAddress pSAREA, int fd,
846 const __DRIextension **extensions,
847 const __DRIconfig ***driver_modes,
848 void *loaderPrivate)
849 {
850 static const __DRIextension *emptyExtensionList[] = { NULL };
851 __DRIscreen *psp;
852
853 psp = _mesa_malloc(sizeof *psp);
854 if (!psp)
855 return NULL;
856
857 setupLoaderExtensions(psp, extensions);
858
859 /*
860 ** NOT_DONE: This is used by the X server to detect when the client
861 ** has died while holding the drawable lock. The client sets the
862 ** drawable lock to this value.
863 */
864 psp->drawLockID = 1;
865
866 psp->drm_version = *drm_version;
867 psp->ddx_version = *ddx_version;
868 psp->dri_version = *dri_version;
869
870 psp->pSAREA = pSAREA;
871 psp->lock = (drmLock *) &psp->pSAREA->lock;
872
873 psp->pFB = frame_buffer->base;
874 psp->fbSize = frame_buffer->size;
875 psp->fbStride = frame_buffer->stride;
876 psp->fbWidth = frame_buffer->width;
877 psp->fbHeight = frame_buffer->height;
878 psp->devPrivSize = frame_buffer->dev_priv_size;
879 psp->pDevPriv = frame_buffer->dev_priv;
880 psp->fbBPP = psp->fbStride * 8 / frame_buffer->width;
881
882 psp->extensions = emptyExtensionList;
883 psp->fd = fd;
884 psp->myNum = scrn;
885 psp->dri2.enabled = GL_FALSE;
886
887 /*
888 ** Do not init dummy context here; actual initialization will be
889 ** done when the first DRI context is created. Init screen priv ptr
890 ** to NULL to let CreateContext routine that it needs to be inited.
891 */
892 psp->dummyContextPriv.driScreenPriv = NULL;
893
894 psp->DriverAPI = driDriverAPI;
895
896 *driver_modes = driDriverAPI.InitScreen(psp);
897 if (*driver_modes == NULL) {
898 _mesa_free(psp);
899 return NULL;
900 }
901
902 return psp;
903 }
904
905
906 static __DRIscreen *
907 dri2CreateNewScreen(int scrn, int fd, unsigned int sarea_handle,
908 const __DRIextension **extensions,
909 const __DRIconfig ***driver_configs, void *data)
910 {
911 #ifdef TTM_API
912 static const __DRIextension *emptyExtensionList[] = { NULL };
913 __DRIscreen *psp;
914 unsigned int *p;
915 drmVersionPtr version;
916
917 if (driDriverAPI.InitScreen2 == NULL)
918 return NULL;
919
920 psp = _mesa_malloc(sizeof(*psp));
921 if (!psp)
922 return NULL;
923
924 setupLoaderExtensions(psp, extensions);
925
926 version = drmGetVersion(fd);
927 if (version) {
928 psp->drm_version.major = version->version_major;
929 psp->drm_version.minor = version->version_minor;
930 psp->drm_version.patch = version->version_patchlevel;
931 drmFreeVersion(version);
932 }
933
934 psp->extensions = emptyExtensionList;
935 psp->fd = fd;
936 psp->myNum = scrn;
937 psp->dri2.enabled = GL_TRUE;
938
939 if (drmBOReference(psp->fd, sarea_handle, &psp->dri2.sareaBO)) {
940 fprintf(stderr, "Failed to reference DRI2 sarea BO\n");
941 _mesa_free(psp);
942 return NULL;
943 }
944
945 if (drmBOMap(psp->fd, &psp->dri2.sareaBO,
946 DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, &psp->dri2.sarea)) {
947 drmBOUnreference(psp->fd, &psp->dri2.sareaBO);
948 _mesa_free(psp);
949 return NULL;
950 }
951
952 p = psp->dri2.sarea;
953 while (DRI2_SAREA_BLOCK_TYPE(*p)) {
954 switch (DRI2_SAREA_BLOCK_TYPE(*p)) {
955 case DRI2_SAREA_BLOCK_LOCK:
956 psp->dri2.lock = (__DRILock *) p;
957 break;
958 case DRI2_SAREA_BLOCK_EVENT_BUFFER:
959 psp->dri2.buffer = (__DRIEventBuffer *) p;
960 break;
961 }
962 p = DRI2_SAREA_BLOCK_NEXT(p);
963 }
964
965 psp->lock = (drmLock *) &psp->dri2.lock->lock;
966
967 psp->DriverAPI = driDriverAPI;
968 *driver_configs = driDriverAPI.InitScreen2(psp);
969 if (*driver_configs == NULL) {
970 drmBOUnmap(psp->fd, &psp->dri2.sareaBO);
971 drmBOUnreference(psp->fd, &psp->dri2.sareaBO);
972 _mesa_free(psp);
973 return NULL;
974 }
975
976 psp->DriverAPI = driDriverAPI;
977
978 return psp;
979 #else
980 return NULL;
981 #endif
982 }
983
984 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
985 {
986 return psp->extensions;
987 }
988
989 const __DRIlegacyExtension driLegacyExtension = {
990 { __DRI_LEGACY, __DRI_LEGACY_VERSION },
991 driCreateNewScreen,
992 driCreateNewDrawable,
993 driCreateNewContext
994 };
995
996 const __DRIcoreExtension driCoreExtension = {
997 { __DRI_CORE, __DRI_CORE_VERSION },
998 dri2CreateNewScreen,
999 driDestroyScreen,
1000 driGetExtensions,
1001 driGetConfigAttrib,
1002 driIndexConfigAttrib,
1003 dri2CreateNewDrawable,
1004 driDestroyDrawable,
1005 driSwapBuffers,
1006 dri2CreateNewContext,
1007 driCopyContext,
1008 driDestroyContext,
1009 driBindContext,
1010 driUnbindContext
1011 };
1012
1013 /* This is the table of extensions that the loader will dlsym() for. */
1014 PUBLIC const __DRIextension *__driDriverExtensions[] = {
1015 &driCoreExtension.base,
1016 &driLegacyExtension.base,
1017 NULL
1018 };
1019
1020 static int
1021 driFrameTracking(__DRIdrawable *drawable, GLboolean enable)
1022 {
1023 return GLX_BAD_CONTEXT;
1024 }
1025
1026 static int
1027 driQueryFrameTracking(__DRIdrawable *dpriv,
1028 int64_t * sbc, int64_t * missedFrames,
1029 float * lastMissedUsage, float * usage)
1030 {
1031 __DRIswapInfo sInfo;
1032 int status;
1033 int64_t ust;
1034 __DRIscreenPrivate *psp = dpriv->driScreenPriv;
1035
1036 status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
1037 if ( status == 0 ) {
1038 *sbc = sInfo.swap_count;
1039 *missedFrames = sInfo.swap_missed_count;
1040 *lastMissedUsage = sInfo.swap_missed_usage;
1041
1042 (*psp->systemTime->getUST)( & ust );
1043 *usage = driCalculateSwapUsage( dpriv, sInfo.swap_ust, ust );
1044 }
1045
1046 return status;
1047 }
1048
1049 const __DRIframeTrackingExtension driFrameTrackingExtension = {
1050 { __DRI_FRAME_TRACKING, __DRI_FRAME_TRACKING_VERSION },
1051 driFrameTracking,
1052 driQueryFrameTracking
1053 };
1054
1055 /**
1056 * Calculate amount of swap interval used between GLX buffer swaps.
1057 *
1058 * The usage value, on the range [0,max], is the fraction of total swap
1059 * interval time used between GLX buffer swaps is calculated.
1060 *
1061 * \f$p = t_d / (i * t_r)\f$
1062 *
1063 * Where \f$t_d\f$ is the time since the last GLX buffer swap, \f$i\f$ is the
1064 * swap interval (as set by \c glXSwapIntervalSGI), and \f$t_r\f$ time
1065 * required for a single vertical refresh period (as returned by \c
1066 * glXGetMscRateOML).
1067 *
1068 * See the documentation for the GLX_MESA_swap_frame_usage extension for more
1069 * details.
1070 *
1071 * \param dPriv Pointer to the private drawable structure.
1072 * \return If less than a single swap interval time period was required
1073 * between GLX buffer swaps, a number greater than 0 and less than
1074 * 1.0 is returned. If exactly one swap interval time period is
1075 * required, 1.0 is returned, and if more than one is required then
1076 * a number greater than 1.0 will be returned.
1077 *
1078 * \sa glXSwapIntervalSGI glXGetMscRateOML
1079 *
1080 * \todo Instead of caching the \c glXGetMscRateOML function pointer, would it
1081 * be possible to cache the sync rate?
1082 */
1083 float
1084 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
1085 int64_t current_ust )
1086 {
1087 int32_t n;
1088 int32_t d;
1089 int interval;
1090 float usage = 1.0;
1091 __DRIscreenPrivate *psp = dPriv->driScreenPriv;
1092
1093 if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
1094 interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
1095
1096
1097 /* We want to calculate
1098 * (current_UST - last_swap_UST) / (interval * us_per_refresh). We get
1099 * current_UST by calling __glXGetUST. last_swap_UST is stored in
1100 * dPriv->swap_ust. interval has already been calculated.
1101 *
1102 * The only tricky part is us_per_refresh. us_per_refresh is
1103 * 1000000 / MSC_rate. We know the MSC_rate is n / d. We can flip it
1104 * around and say us_per_refresh = 1000000 * d / n. Since this goes in
1105 * the denominator of the final calculation, we calculate
1106 * (interval * 1000000 * d) and move n into the numerator.
1107 */
1108
1109 usage = (current_ust - last_swap_ust);
1110 usage *= n;
1111 usage /= (interval * d);
1112 usage /= 1000000.0;
1113 }
1114
1115 return usage;
1116 }
1117
1118 /*@}*/