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