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