uint*t -> u_int*t changes
[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 #ifdef DRI_NEW_INTERFACE_ONLY
57 # include <GL/gl.h>
58 #else
59 # include "glxclient.h"
60 #endif /* DRI_NEW_INTERFACE_ONLY */
61 #include "drm.h"
62 #include "drm_sarea.h"
63 #include "GL/internal/glcore.h"
64 #include "GL/internal/dri_interface.h"
65
66 #define GLX_BAD_CONTEXT 5
67
68 /* This is a temporary relic. Once all drivers are converted to support
69 * the new interface, it can go away.
70 */
71 #ifdef DRI_NEW_INTERFACE_ONLY
72 #define USE_NEW_INTERFACE
73 #endif
74
75 typedef struct __DRIdisplayPrivateRec __DRIdisplayPrivate;
76 typedef struct __DRIscreenPrivateRec __DRIscreenPrivate;
77 typedef struct __DRIcontextPrivateRec __DRIcontextPrivate;
78 typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate;
79 typedef struct __DRIswapInfoRec __DRIswapInfo;
80
81
82 /**
83 * Used by DRI_VALIDATE_DRAWABLE_INFO
84 */
85 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
86 do { \
87 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
88 __driUtilUpdateDrawableInfo(pDrawPriv); \
89 } \
90 } while (0)
91
92
93 /**
94 * Utility macro to validate the drawable information.
95 *
96 * See __DRIdrawablePrivate::pStamp and __DRIdrawablePrivate::lastStamp.
97 */
98 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
99 do { \
100 while (*(pdp->pStamp) != pdp->lastStamp) { \
101 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, \
102 pdp->driContextPriv->hHWContext); \
103 \
104 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
105 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
106 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
107 \
108 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, \
109 pdp->driContextPriv->hHWContext); \
110 } \
111 } while (0)
112
113
114 /**
115 * Driver callback functions.
116 *
117 * Each DRI driver must have one of these structures with all the pointers set
118 * to appropriate functions within the driver.
119 *
120 * When glXCreateContext() is called, for example, it'll call a helper function
121 * dri_util.c which in turn will jump through the \a CreateContext pointer in
122 * this structure.
123 */
124 struct __DriverAPIRec {
125 /**
126 * Driver initialization callback
127 */
128 GLboolean (*InitDriver)(__DRIscreenPrivate *driScrnPriv);
129
130 /**
131 * Screen destruction callback
132 */
133 void (*DestroyScreen)(__DRIscreenPrivate *driScrnPriv);
134
135 /**
136 * Context creation callback
137 */
138 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
139 __DRIcontextPrivate *driContextPriv,
140 void *sharedContextPrivate);
141
142 /**
143 * Context destruction callback
144 */
145 void (*DestroyContext)(__DRIcontextPrivate *driContextPriv);
146
147 /**
148 * Buffer (drawable) creation callback
149 */
150 GLboolean (*CreateBuffer)(__DRIscreenPrivate *driScrnPriv,
151 __DRIdrawablePrivate *driDrawPriv,
152 const __GLcontextModes *glVis,
153 GLboolean pixmapBuffer);
154
155 /**
156 * Buffer (drawable) destruction callback
157 */
158 void (*DestroyBuffer)(__DRIdrawablePrivate *driDrawPriv);
159
160 /**
161 * Buffer swapping callback
162 */
163 void (*SwapBuffers)(__DRIdrawablePrivate *driDrawPriv);
164
165 /**
166 * Context activation callback
167 */
168 GLboolean (*MakeCurrent)(__DRIcontextPrivate *driContextPriv,
169 __DRIdrawablePrivate *driDrawPriv,
170 __DRIdrawablePrivate *driReadPriv);
171
172 /**
173 * Context unbinding callback
174 */
175 GLboolean (*UnbindContext)(__DRIcontextPrivate *driContextPriv);
176
177 /**
178 * Retrieves statistics about buffer swap operations. Required if
179 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
180 */
181 int (*GetSwapInfo)( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
182
183
184 /**
185 * Required if GLX_SGI_video_sync or GLX_OML_sync_control is
186 * supported.
187 */
188 int (*GetMSC)( __DRIscreenPrivate * priv, int64_t * count );
189
190 /**
191 * These are required if GLX_OML_sync_control is supported.
192 */
193 /*@{*/
194 int (*WaitForMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
195 int64_t divisor, int64_t remainder,
196 int64_t * msc );
197 int (*WaitForSBC)( __DRIdrawablePrivate *priv, int64_t target_sbc,
198 int64_t * msc, int64_t * sbc );
199
200 int64_t (*SwapBuffersMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
201 int64_t divisor, int64_t remainder );
202 /*@}*/
203 };
204
205
206 struct __DRIswapInfoRec {
207 /**
208 * Number of swapBuffers operations that have been *completed*.
209 */
210 u_int64_t swap_count;
211
212 /**
213 * Unadjusted system time of the last buffer swap. This is the time
214 * when the swap completed, not the time when swapBuffers was called.
215 */
216 int64_t swap_ust;
217
218 /**
219 * Number of swap operations that occurred after the swap deadline. That
220 * is if a swap happens more than swap_interval frames after the previous
221 * swap, it has missed its deadline. If swap_interval is 0, then the
222 * swap deadline is 1 frame after the previous swap.
223 */
224 u_int64_t swap_missed_count;
225
226 /**
227 * Amount of time used by the last swap that missed its deadline. This
228 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
229 * time_for_single_vrefresh)). If the actual value of swap_interval is
230 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
231 * should be greater-than 1.0.
232 */
233 float swap_missed_usage;
234 };
235
236
237 /**
238 * Per-drawable private DRI driver information.
239 */
240 struct __DRIdrawablePrivateRec {
241 /**
242 * Kernel drawable handle
243 */
244 drm_drawable_t hHWDrawable;
245
246 /**
247 * Driver's private drawable information.
248 *
249 * This structure is opaque.
250 */
251 void *driverPrivate;
252
253 /**
254 * X's drawable ID associated with this private drawable.
255 */
256 __DRIid draw;
257 __DRIdrawable *pdraw;
258
259 /**
260 * Reference count for number of context's currently bound to this
261 * drawable.
262 *
263 * Once it reaches zero, the drawable can be destroyed.
264 *
265 * \note This behavior will change with GLX 1.3.
266 */
267 int refcount;
268
269 /**
270 * Index of this drawable information in the SAREA.
271 */
272 unsigned int index;
273
274 /**
275 * Pointer to the "drawable has changed ID" stamp in the SAREA.
276 */
277 unsigned int *pStamp;
278
279 /**
280 * Last value of the stamp.
281 *
282 * If this differs from the value stored at __DRIdrawablePrivate::pStamp,
283 * then the drawable information has been modified by the X server, and the
284 * drawable information (below) should be retrieved from the X server.
285 */
286 unsigned int lastStamp;
287
288 /**
289 * \name Drawable
290 *
291 * Drawable information used in software fallbacks.
292 */
293 /*@{*/
294 int x;
295 int y;
296 int w;
297 int h;
298 int numClipRects;
299 drm_clip_rect_t *pClipRects;
300 /*@}*/
301
302 /**
303 * \name Back and depthbuffer
304 *
305 * Information about the back and depthbuffer where different from above.
306 */
307 /*@{*/
308 int backX;
309 int backY;
310 int backClipRectType;
311 int numBackClipRects;
312 drm_clip_rect_t *pBackClipRects;
313 /*@}*/
314
315 /**
316 * Pointer to context to which this drawable is currently bound.
317 */
318 __DRIcontextPrivate *driContextPriv;
319
320 /**
321 * Pointer to screen on which this drawable was created.
322 */
323 __DRIscreenPrivate *driScreenPriv;
324
325 /**
326 * \name Display and screen information.
327 *
328 * Basically just need these for when the locking code needs to call
329 * __driUtilUpdateDrawableInfo() which calls XF86DRIGetDrawableInfo().
330 */
331 /*@{*/
332 __DRInativeDisplay *display;
333 int screen;
334 /*@}*/
335
336 /**
337 * Called via glXSwapBuffers().
338 */
339 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
340
341 /**
342 * Get information about the location, size, and clip rects of the
343 * drawable within the display.
344 */
345 PFNGLXGETDRAWABLEINFOPROC getInfo;
346 };
347
348 /**
349 * Per-context private driver information.
350 */
351 struct __DRIcontextPrivateRec {
352 /**
353 * Kernel context handle used to access the device lock.
354 */
355 __DRIid contextID;
356
357 /**
358 * Kernel context handle used to access the device lock.
359 */
360 drm_context_t hHWContext;
361
362 /**
363 * Device driver's private context data. This structure is opaque.
364 */
365 void *driverPrivate;
366
367 /**
368 * This context's display pointer.
369 */
370 __DRInativeDisplay *display;
371
372 /**
373 * Pointer to drawable currently bound to this context.
374 */
375 __DRIdrawablePrivate *driDrawablePriv;
376
377 /**
378 * Pointer to screen on which this context was created.
379 */
380 __DRIscreenPrivate *driScreenPriv;
381 };
382
383 /**
384 * Per-screen private driver information.
385 */
386 struct __DRIscreenPrivateRec {
387 /**
388 * Display for this screen
389 */
390 __DRInativeDisplay *display;
391
392 /**
393 * Current screen's number
394 */
395 int myNum;
396
397 /**
398 * Callback functions into the hardware-specific DRI driver code.
399 */
400 struct __DriverAPIRec DriverAPI;
401
402 /**
403 * \name DDX version
404 * DDX / 2D driver version information.
405 * \todo Replace these fields with a \c __DRIversionRec.
406 */
407 /*@{*/
408 int ddxMajor;
409 int ddxMinor;
410 int ddxPatch;
411 /*@}*/
412
413 /**
414 * \name DRI version
415 * DRI X extension version information.
416 * \todo Replace these fields with a \c __DRIversionRec.
417 */
418 /*@{*/
419 int driMajor;
420 int driMinor;
421 int driPatch;
422 /*@}*/
423
424 /**
425 * \name DRM version
426 * DRM (kernel module) version information.
427 * \todo Replace these fields with a \c __DRIversionRec.
428 */
429 /*@{*/
430 int drmMajor;
431 int drmMinor;
432 int drmPatch;
433 /*@}*/
434
435 /**
436 * ID used when the client sets the drawable lock.
437 *
438 * The X server uses this value to detect if the client has died while
439 * holding the drawable lock.
440 */
441 int drawLockID;
442
443 /**
444 * File descriptor returned when the kernel device driver is opened.
445 *
446 * Used to:
447 * - authenticate client to kernel
448 * - map the frame buffer, SAREA, etc.
449 * - close the kernel device driver
450 */
451 int fd;
452
453 /**
454 * SAREA pointer
455 *
456 * Used to access:
457 * - the device lock
458 * - the device-independent per-drawable and per-context(?) information
459 */
460 drm_sarea_t *pSAREA;
461
462 /**
463 * \name Direct frame buffer access information
464 * Used for software fallbacks.
465 */
466 /*@{*/
467 unsigned char *pFB;
468 int fbSize;
469 int fbOrigin;
470 int fbStride;
471 int fbWidth;
472 int fbHeight;
473 int fbBPP;
474 /*@}*/
475
476 /**
477 * \name Device-dependent private information (stored in the SAREA).
478 *
479 * This data is accessed by the client driver only.
480 */
481 /*@{*/
482 void *pDevPriv;
483 int devPrivSize;
484 /*@}*/
485
486 /**
487 * Dummy context to which drawables are bound when not bound to any
488 * other context.
489 *
490 * A dummy hHWContext is created for this context, and is used by the GL
491 * core when a hardware lock is required but the drawable is not currently
492 * bound (e.g., potentially during a SwapBuffers request). The dummy
493 * context is created when the first "real" context is created on this
494 * screen.
495 */
496 __DRIcontextPrivate dummyContextPriv;
497
498 /**
499 * Hash table to hold the drawable information for this screen.
500 */
501 void *drawHash;
502
503 /**
504 * Device-dependent private information (not stored in the SAREA).
505 *
506 * This pointer is never touched by the DRI layer.
507 */
508 void *private;
509
510 /**
511 * GLX visuals / FBConfigs for this screen. These are stored as a
512 * linked list.
513 *
514 * \note
515 * This field is \b only used in conjunction with the old interfaces. If
516 * the new interfaces are used, this field will be set to \c NULL and will
517 * not be dereferenced.
518 */
519 __GLcontextModes *modes;
520
521 /**
522 * Pointer back to the \c __DRIscreen that contains this structure.
523 */
524
525 __DRIscreen *psc;
526 };
527
528
529
530 extern void
531 __driUtilMessage(const char *f, ...);
532
533
534 extern void
535 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
536
537
538 extern __DRIscreenPrivate * __driUtilCreateNewScreen( __DRInativeDisplay *dpy,
539 int scrn, __DRIscreen *psc, __GLcontextModes * modes,
540 const __DRIversion * ddx_version, const __DRIversion * dri_version,
541 const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
542 drm_sarea_t *pSAREA, int fd, int internal_api_version,
543 const struct __DriverAPIRec *driverAPI );
544
545 #ifndef DRI_NEW_INTERFACE_ONLY
546 extern __DRIscreenPrivate *
547 __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
548 int numConfigs, __GLXvisualConfig *config,
549 const struct __DriverAPIRec *driverAPI);
550 #endif /* DRI_NEW_INTERFACE_ONLY */
551
552 /* Test the version of the internal GLX API. Returns a value like strcmp. */
553 extern int
554 driCompareGLXAPIVersion( GLuint required_version );
555
556 extern float
557 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
558 int64_t last_swap_ust, int64_t current_ust );
559
560 /* Test for the GLX header glx.h */
561 #ifndef GLX
562 extern void
563 (*glXGetProcAddress(const GLubyte *procname))( void );
564 #endif
565
566 #endif /* GLX_DIRECT_RENDERING */
567
568 #endif /* _DRI_UTIL_H_ */