1cc5f7cc54608931ff9fa0123ed764359a7f1817
[mesa.git] / src / mesa / drivers / dri / common / dri_util.h
1 /* $XFree86: xc/lib/GL/dri/dri_util.h,v 1.1 2002/02/22 21:32:52 dawes Exp $ */
2 /**
3 * \file dri_util.h
4 * DRI utility functions definitions.
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 * \sa dri_util.c.
18 *
19 * \author Kevin E. Martin <kevin@precisioninsight.com>
20 * \author Brian Paul <brian@precisioninsight.com>
21 */
22
23 /*
24 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
25 * All Rights Reserved.
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the
29 * "Software"), to deal in the Software without restriction, including
30 * without limitation the rights to use, copy, modify, merge, publish,
31 * distribute, sub license, and/or sell copies of the Software, and to
32 * permit persons to whom the Software is furnished to do so, subject to
33 * the following conditions:
34 *
35 * The above copyright notice and this permission notice (including the
36 * next paragraph) shall be included in all copies or substantial portions
37 * of the Software.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
42 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
43 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 */
47
48
49 #ifndef _DRI_UTIL_H_
50 #define _DRI_UTIL_H_
51
52 #ifdef GLX_DIRECT_RENDERING
53
54 #define CAPI /* XXX this should be globally defined somewhere */
55
56 #include <inttypes.h>
57 #include "glxclient.h" /* for GLXDrawable */
58 /* temporary */
59 /* typedef Bool ( * PFNGLXGETMSCRATEOMLPROC) (Display *dpy, GLXDrawable drawable, int32_t *numerator, int32_t *denominator); */
60 #include "drm.h" /* for drm_clip_rect_t */
61 #include "sarea.h" /* for XF86DRISAREAPtr */
62 #include "GL/internal/glcore.h" /* for __GLcontextModes */
63
64
65 typedef struct __DRIdisplayPrivateRec __DRIdisplayPrivate;
66 typedef struct __DRIscreenPrivateRec __DRIscreenPrivate;
67 typedef struct __DRIcontextPrivateRec __DRIcontextPrivate;
68 typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate;
69 typedef struct __DRIswapInfoRec __DRIswapInfo;
70
71
72 /**
73 * Used by DRI_VALIDATE_DRAWABLE_INFO
74 */
75 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
76 do { \
77 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
78 __driUtilUpdateDrawableInfo(pDrawPriv); \
79 } \
80 } while (0)
81
82
83 /**
84 * Utility macro to validate the drawable information.
85 *
86 * See __DRIdrawablePrivate::pStamp and __DRIdrawablePrivate::lastStamp.
87 */
88 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
89 do { \
90 while (*(pdp->pStamp) != pdp->lastStamp) { \
91 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, \
92 pdp->driContextPriv->hHWContext); \
93 \
94 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
95 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
96 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
97 \
98 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, \
99 pdp->driContextPriv->hHWContext); \
100 } \
101 } while (0)
102
103
104 /**
105 * Driver callback functions.
106 *
107 * Each DRI driver must have one of these structures with all the pointers set
108 * to appropriate functions within the driver.
109 *
110 * When glXCreateContext() is called, for example, it'll call a helper function
111 * dri_util.c which in turn will jump through the \a CreateContext pointer in
112 * this structure.
113 */
114 struct __DriverAPIRec {
115 /**
116 * Driver initialization callback
117 */
118 GLboolean (*InitDriver)(__DRIscreenPrivate *driScrnPriv);
119
120 /**
121 * Screen destruction callback
122 */
123 void (*DestroyScreen)(__DRIscreenPrivate *driScrnPriv);
124
125 /**
126 * Context creation callback
127 */
128 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
129 __DRIcontextPrivate *driContextPriv,
130 void *sharedContextPrivate);
131
132 /**
133 * Context destruction callback
134 */
135 void (*DestroyContext)(__DRIcontextPrivate *driContextPriv);
136
137 /**
138 * Buffer (drawable) creation callback
139 */
140 GLboolean (*CreateBuffer)(__DRIscreenPrivate *driScrnPriv,
141 __DRIdrawablePrivate *driDrawPriv,
142 const __GLcontextModes *glVis,
143 GLboolean pixmapBuffer);
144
145 /**
146 * Buffer (drawable) destruction callback
147 */
148 void (*DestroyBuffer)(__DRIdrawablePrivate *driDrawPriv);
149
150 /**
151 * Buffer swapping callback
152 */
153 void (*SwapBuffers)(__DRIdrawablePrivate *driDrawPriv);
154
155 /**
156 * Context activation callback
157 */
158 GLboolean (*MakeCurrent)(__DRIcontextPrivate *driContextPriv,
159 __DRIdrawablePrivate *driDrawPriv,
160 __DRIdrawablePrivate *driReadPriv);
161
162 /**
163 * Context unbinding callback
164 */
165 GLboolean (*UnbindContext)(__DRIcontextPrivate *driContextPriv);
166
167 /**
168 * Full screen mode opening callback.
169 *
170 * \deprecated
171 * Full screen functionality is no longer used by DRI. Drivers should
172 * simply install a function returning \c GL_TRUE for backwards
173 * compatability.
174 *
175 * \todo
176 * Nothing calls this function anymore. Since this data structure is only
177 * accessed with in the driver (i.e., libGL never sees it), we need to
178 * remove the other cases that set this field and remove the field.
179 */
180 GLboolean (*OpenFullScreen)(__DRIcontextPrivate *driContextPriv);
181
182 /**
183 * Full screen mode closing callback.
184 *
185 * \deprecated
186 * Full screen functionality is no longer used by DRI. Drivers should
187 * simply install a function returning \c GL_TRUE for backwards
188 * compatability.
189 *
190 * \todo
191 * Nothing calls this function anymore. Since this data structure is only
192 * accessed with in the driver (i.e., libGL never sees it), we need to
193 * remove the other cases that set this field and remove the field.
194 */
195 GLboolean (*CloseFullScreen)(__DRIcontextPrivate *driContextPriv);
196
197 /**
198 * Retrieves statistics about buffer swap operations. Required if
199 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
200 */
201 int (*GetSwapInfo)( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
202
203
204 /**
205 * Required if GLX_SGI_video_sync or GLX_OML_sync_control is
206 * supported.
207 */
208 int (*GetMSC)( __DRIscreenPrivate * priv, int64_t * count );
209
210 /**
211 * These are required if GLX_OML_sync_control is supported.
212 */
213 /*@{*/
214 int (*WaitForMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
215 int64_t divisor, int64_t remainder,
216 int64_t * msc );
217 int (*WaitForSBC)( __DRIdrawablePrivate *priv, int64_t target_sbc,
218 int64_t * msc, int64_t * sbc );
219
220 int64_t (*SwapBuffersMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
221 int64_t divisor, int64_t remainder );
222 /*@}*/
223 };
224
225
226 struct __DRIswapInfoRec {
227 /**
228 * Number of swapBuffers operations that have been *completed*.
229 */
230 uint64_t swap_count;
231
232 /**
233 * Unadjusted system time of the last buffer swap. This is the time
234 * when the swap completed, not the time when swapBuffers was called.
235 */
236 int64_t swap_ust;
237
238 /**
239 * Number of swap operations that occurred after the swap deadline. That
240 * is if a swap happens more than swap_interval frames after the previous
241 * swap, it has missed its deadline. If swap_interval is 0, then the
242 * swap deadline is 1 frame after the previous swap.
243 */
244 uint64_t swap_missed_count;
245
246 /**
247 * Amount of time used by the last swap that missed its deadline. This
248 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
249 * time_for_single_vrefresh)). If the actual value of swap_interval is
250 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
251 * should be greater-than 1.0.
252 */
253 float swap_missed_usage;
254 };
255
256
257 /**
258 * Per-drawable private DRI driver information.
259 */
260 struct __DRIdrawablePrivateRec {
261 /**
262 * Kernel drawable handle
263 */
264 drmDrawable hHWDrawable;
265
266 /**
267 * Driver's private drawable information.
268 *
269 * This structure is opaque.
270 */
271 void *driverPrivate;
272
273 /**
274 * X's drawable ID associated with this private drawable.
275 */
276 __DRIid draw;
277 __DRIdrawable *pdraw;
278
279 /**
280 * Reference count for number of context's currently bound to this
281 * drawable.
282 *
283 * Once it reaches zero, the drawable can be destroyed.
284 *
285 * \note This behavior will change with GLX 1.3.
286 */
287 int refcount;
288
289 /**
290 * Index of this drawable information in the SAREA.
291 */
292 unsigned int index;
293
294 /**
295 * Pointer to the "drawable has changed ID" stamp in the SAREA.
296 */
297 unsigned int *pStamp;
298
299 /**
300 * Last value of the stamp.
301 *
302 * If this differs from the value stored at __DRIdrawablePrivate::pStamp,
303 * then the drawable information has been modified by the X server, and the
304 * drawable information (below) should be retrieved from the X server.
305 */
306 unsigned int lastStamp;
307
308 /**
309 * \name Drawable
310 *
311 * Drawable information used in software fallbacks.
312 */
313 /*@{*/
314 int x;
315 int y;
316 int w;
317 int h;
318 int numClipRects;
319 drm_clip_rect_t *pClipRects;
320 /*@}*/
321
322 /**
323 * \name Back and depthbuffer
324 *
325 * Information about the back and depthbuffer where different from above.
326 */
327 /*@{*/
328 int backX;
329 int backY;
330 int backClipRectType;
331 int numBackClipRects;
332 drm_clip_rect_t *pBackClipRects;
333 /*@}*/
334
335 /**
336 * Pointer to context to which this drawable is currently bound.
337 */
338 __DRIcontextPrivate *driContextPriv;
339
340 /**
341 * Pointer to screen on which this drawable was created.
342 */
343 __DRIscreenPrivate *driScreenPriv;
344
345 /**
346 * \name Display and screen information.
347 *
348 * Basically just need these for when the locking code needs to call
349 * __driUtilUpdateDrawableInfo() which calls XF86DRIGetDrawableInfo().
350 */
351 /*@{*/
352 __DRInativeDisplay *display;
353 int screen;
354 /*@}*/
355
356 /**
357 * Called via glXSwapBuffers().
358 */
359 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
360
361 /**
362 * Get information about the location, size, and clip rects of the
363 * drawable within the display.
364 */
365 PFNGLXGETDRAWABLEINFOPROC getInfo;
366 };
367
368 /**
369 * Per-context private driver information.
370 */
371 struct __DRIcontextPrivateRec {
372 /**
373 * Kernel context handle used to access the device lock.
374 */
375 XID contextID;
376
377 /**
378 * Kernel context handle used to access the device lock.
379 */
380 drmContext hHWContext;
381
382 /**
383 * Device driver's private context data. This structure is opaque.
384 */
385 void *driverPrivate;
386
387 /**
388 * This context's display pointer.
389 */
390 __DRInativeDisplay *display;
391
392 /**
393 * Pointer to drawable currently bound to this context.
394 */
395 __DRIdrawablePrivate *driDrawablePriv;
396
397 /**
398 * Pointer to screen on which this context was created.
399 */
400 __DRIscreenPrivate *driScreenPriv;
401 };
402
403 /**
404 * Per-screen private driver information.
405 */
406 struct __DRIscreenPrivateRec {
407 /**
408 * Display for this screen
409 */
410 __DRInativeDisplay *display;
411
412 /**
413 * Current screen's number
414 */
415 int myNum;
416
417 /**
418 * Callback functions into the hardware-specific DRI driver code.
419 */
420 struct __DriverAPIRec DriverAPI;
421
422 /**
423 * \name DDX version
424 * DDX / 2D driver version information.
425 * \todo Replace these fields with a \c __DRIversionRec.
426 */
427 /*@{*/
428 int ddxMajor;
429 int ddxMinor;
430 int ddxPatch;
431 /*@}*/
432
433 /**
434 * \name DRI version
435 * DRI X extension version information.
436 * \todo Replace these fields with a \c __DRIversionRec.
437 */
438 /*@{*/
439 int driMajor;
440 int driMinor;
441 int driPatch;
442 /*@}*/
443
444 /**
445 * \name DRM version
446 * DRM (kernel module) version information.
447 * \todo Replace these fields with a \c __DRIversionRec.
448 */
449 /*@{*/
450 int drmMajor;
451 int drmMinor;
452 int drmPatch;
453 /*@}*/
454
455 /**
456 * ID used when the client sets the drawable lock.
457 *
458 * The X server uses this value to detect if the client has died while
459 * holding the drawable lock.
460 */
461 int drawLockID;
462
463 /**
464 * File descriptor returned when the kernel device driver is opened.
465 *
466 * Used to:
467 * - authenticate client to kernel
468 * - map the frame buffer, SAREA, etc.
469 * - close the kernel device driver
470 */
471 int fd;
472
473 /**
474 * SAREA pointer
475 *
476 * Used to access:
477 * - the device lock
478 * - the device-independent per-drawable and per-context(?) information
479 */
480 XF86DRISAREAPtr pSAREA;
481
482 /**
483 * \name Direct frame buffer access information
484 * Used for software fallbacks.
485 */
486 /*@{*/
487 unsigned char *pFB;
488 int fbSize;
489 int fbOrigin;
490 int fbStride;
491 int fbWidth;
492 int fbHeight;
493 int fbBPP;
494 /*@}*/
495
496 /**
497 * \name Device-dependent private information (stored in the SAREA).
498 *
499 * This data is accessed by the client driver only.
500 */
501 /*@{*/
502 void *pDevPriv;
503 int devPrivSize;
504 /*@}*/
505
506 /**
507 * Dummy context to which drawables are bound when not bound to any
508 * other context.
509 *
510 * A dummy hHWContext is created for this context, and is used by the GL
511 * core when a hardware lock is required but the drawable is not currently
512 * bound (e.g., potentially during a SwapBuffers request). The dummy
513 * context is created when the first "real" context is created on this
514 * screen.
515 */
516 __DRIcontextPrivate dummyContextPriv;
517
518 /**
519 * Hash table to hold the drawable information for this screen.
520 */
521 void *drawHash;
522
523 /**
524 * Device-dependent private information (not stored in the SAREA).
525 *
526 * This pointer is never touched by the DRI layer.
527 */
528 void *private;
529
530 /**
531 * GLX visuals / FBConfigs for this screen. These are stored as a
532 * linked list.
533 *
534 * \note
535 * This field is \b only used in conjunction with the old interfaces. If
536 * the new interfaces are used, this field will be set to \c NULL and will
537 * not be dereferenced.
538 */
539 __GLcontextModes *modes;
540
541 /**
542 * Pointer back to the \c __DRIscreen that contains this structure.
543 */
544
545 __DRIscreen *psc;
546 };
547
548
549
550 extern void
551 __driUtilMessage(const char *f, ...);
552
553
554 extern void
555 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
556
557
558 extern __DRIscreenPrivate * __driUtilCreateNewScreen( __DRInativeDisplay *dpy,
559 int scrn, __DRIscreen *psc, __GLcontextModes * modes,
560 const __DRIversion * ddx_version, const __DRIversion * dri_version,
561 const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
562 drmAddress pSAREA, int fd, int internal_api_version,
563 const struct __DriverAPIRec *driverAPI );
564
565 #ifndef DRI_NEW_INTERFACE_ONLY
566 extern __DRIscreenPrivate *
567 __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
568 int numConfigs, __GLXvisualConfig *config,
569 const struct __DriverAPIRec *driverAPI);
570 #endif /* DRI_NEW_INTERFACE_ONLY */
571
572 /* Test the version of the internal GLX API. Returns a value like strcmp. */
573 extern int
574 driCompareGLXAPIVersion( GLuint required_version );
575
576 extern float
577 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
578 int64_t last_swap_ust, int64_t current_ust );
579
580 #endif /* GLX_DIRECT_RENDERING */
581
582 #endif /* _DRI_UTIL_H_ */