VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/dri_util.c@ 63206

Last change on this file since 63206 was 62061, checked in by vboxsync, 8 years ago

crOpenGL: don't use a variable before checking it for NULL.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use