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