Allow *_dri.so to build in Mesa tree with the 'linux-dri' target.
[mesa.git] / src / mesa / drivers / dri / dri_client / 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 "xf86dri.h" /* for XF86DRIClipRectPtr */
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 typedef Bool (GetDrawableInfo)( Display *dpy, int scrn, Drawable draw,
258 unsigned int * index, unsigned int * stamp,
259 int * x, int * y, int * width, int * height,
260 int * numClipRects, XF86DRIClipRectPtr * pClipRects,
261 int * backX, int * backY,
262 int * numBackClipRects, XF86DRIClipRectPtr * pBackClipRects );
263
264
265 /**
266 * Per-drawable private DRI driver information.
267 */
268 struct __DRIdrawablePrivateRec {
269 /**
270 * Kernel drawable handle
271 */
272 drmDrawable hHWDrawable;
273
274 /**
275 * Driver's private drawable information.
276 *
277 * This structure is opaque.
278 */
279 void *driverPrivate;
280
281 /**
282 * X's drawable ID associated with this private drawable.
283 */
284 GLXDrawable draw;
285 __DRIdrawable *pdraw;
286
287 /**
288 * Reference count for number of context's currently bound to this
289 * drawable.
290 *
291 * Once it reaches zero, the drawable can be destroyed.
292 *
293 * \note This behavior will change with GLX 1.3.
294 */
295 int refcount;
296
297 /**
298 * Index of this drawable information in the SAREA.
299 */
300 unsigned int index;
301
302 /**
303 * Pointer to the "drawable has changed ID" stamp in the SAREA.
304 */
305 unsigned int *pStamp;
306
307 /**
308 * Last value of the stamp.
309 *
310 * If this differs from the value stored at __DRIdrawablePrivate::pStamp,
311 * then the drawable information has been modified by the X server, and the
312 * drawable information (below) should be retrieved from the X server.
313 */
314 unsigned int lastStamp;
315
316 /**
317 * \name Drawable
318 *
319 * Drawable information used in software fallbacks.
320 */
321 /*@{*/
322 int x;
323 int y;
324 int w;
325 int h;
326 int numClipRects;
327 XF86DRIClipRectPtr pClipRects;
328 /*@}*/
329
330 /**
331 * \name Back and depthbuffer
332 *
333 * Information about the back and depthbuffer where different from above.
334 */
335 /*@{*/
336 int backX;
337 int backY;
338 int backClipRectType;
339 int numBackClipRects;
340 XF86DRIClipRectPtr pBackClipRects;
341 /*@}*/
342
343 /**
344 * Pointer to context to which this drawable is currently bound.
345 */
346 __DRIcontextPrivate *driContextPriv;
347
348 /**
349 * Pointer to screen on which this drawable was created.
350 */
351 __DRIscreenPrivate *driScreenPriv;
352
353 /**
354 * \name Display and screen information.
355 *
356 * Basically just need these for when the locking code needs to call
357 * __driUtilUpdateDrawableInfo() which calls XF86DRIGetDrawableInfo().
358 */
359 /*@{*/
360 Display *display;
361 int screen;
362 /*@}*/
363
364 /**
365 * Called via glXSwapBuffers().
366 */
367 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
368
369 /**
370 * Get information about the location, size, and clip rects of the
371 * drawable within the display.
372 */
373 GetDrawableInfo * getInfo;
374 };
375
376 /**
377 * Per-context private driver information.
378 */
379 struct __DRIcontextPrivateRec {
380 /**
381 * Kernel context handle used to access the device lock.
382 */
383 XID contextID;
384
385 /**
386 * Kernel context handle used to access the device lock.
387 */
388 drmContext hHWContext;
389
390 /**
391 * Device driver's private context data. This structure is opaque.
392 */
393 void *driverPrivate;
394
395 /**
396 * This context's display pointer.
397 */
398 Display *display;
399
400 /**
401 * Pointer to drawable currently bound to this context.
402 */
403 __DRIdrawablePrivate *driDrawablePriv;
404
405 /**
406 * Pointer to screen on which this context was created.
407 */
408 __DRIscreenPrivate *driScreenPriv;
409 };
410
411 /**
412 * Per-screen private driver information.
413 */
414 struct __DRIscreenPrivateRec {
415 /**
416 * Display for this screen
417 */
418 Display *display;
419
420 /**
421 * Current screen's number
422 */
423 int myNum;
424
425 /**
426 * Callback functions into the hardware-specific DRI driver code.
427 */
428 struct __DriverAPIRec DriverAPI;
429
430 /**
431 * \name DDX version
432 * DDX / 2D driver version information.
433 * \todo Replace these fields with a \c __DRIversionRec.
434 */
435 /*@{*/
436 int ddxMajor;
437 int ddxMinor;
438 int ddxPatch;
439 /*@}*/
440
441 /**
442 * \name DRI version
443 * DRI X extension version information.
444 * \todo Replace these fields with a \c __DRIversionRec.
445 */
446 /*@{*/
447 int driMajor;
448 int driMinor;
449 int driPatch;
450 /*@}*/
451
452 /**
453 * \name DRM version
454 * DRM (kernel module) version information.
455 * \todo Replace these fields with a \c __DRIversionRec.
456 */
457 /*@{*/
458 int drmMajor;
459 int drmMinor;
460 int drmPatch;
461 /*@}*/
462
463 /**
464 * ID used when the client sets the drawable lock.
465 *
466 * The X server uses this value to detect if the client has died while
467 * holding the drawable lock.
468 */
469 int drawLockID;
470
471 /**
472 * File descriptor returned when the kernel device driver is opened.
473 *
474 * Used to:
475 * - authenticate client to kernel
476 * - map the frame buffer, SAREA, etc.
477 * - close the kernel device driver
478 */
479 int fd;
480
481 /**
482 * SAREA pointer
483 *
484 * Used to access:
485 * - the device lock
486 * - the device-independent per-drawable and per-context(?) information
487 */
488 XF86DRISAREAPtr pSAREA;
489
490 /**
491 * \name Direct frame buffer access information
492 * Used for software fallbacks.
493 */
494 /*@{*/
495 unsigned char *pFB;
496 int fbSize;
497 int fbOrigin;
498 int fbStride;
499 int fbWidth;
500 int fbHeight;
501 int fbBPP;
502 /*@}*/
503
504 /**
505 * \name Device-dependent private information (stored in the SAREA).
506 *
507 * This data is accessed by the client driver only.
508 */
509 /*@{*/
510 void *pDevPriv;
511 int devPrivSize;
512 /*@}*/
513
514 /**
515 * Dummy context to which drawables are bound when not bound to any
516 * other context.
517 *
518 * A dummy hHWContext is created for this context, and is used by the GL
519 * core when a hardware lock is required but the drawable is not currently
520 * bound (e.g., potentially during a SwapBuffers request). The dummy
521 * context is created when the first "real" context is created on this
522 * screen.
523 */
524 __DRIcontextPrivate dummyContextPriv;
525
526 /**
527 * Hash table to hold the drawable information for this screen.
528 */
529 void *drawHash;
530
531 /**
532 * Device-dependent private information (not stored in the SAREA).
533 *
534 * This pointer is never touched by the DRI layer.
535 */
536 void *private;
537
538 /**
539 * GLX visuals / FBConfigs for this screen. These are stored as a
540 * linked list.
541 *
542 * \note
543 * This field is \b only used in conjunction with the old interfaces. If
544 * the new interfaces are used, this field will be set to \c NULL and will
545 * not be dereferenced.
546 */
547 __GLcontextModes *modes;
548
549 /**
550 * Pointer back to the \c __DRIscreen that contains this structure.
551 */
552
553 __DRIscreen *psc;
554 };
555
556
557
558 extern void
559 __driUtilMessage(const char *f, ...);
560
561
562 extern void
563 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
564
565
566 extern __DRIscreenPrivate * __driUtilCreateNewScreen( Display *dpy,
567 int scrn, __DRIscreen *psc, __GLcontextModes * modes,
568 const __DRIversion * ddx_version, const __DRIversion * dri_version,
569 const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
570 drmAddress pSAREA, int fd, int internal_api_version,
571 const struct __DriverAPIRec *driverAPI );
572
573 extern __DRIscreenPrivate *
574 __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
575 int numConfigs, __GLXvisualConfig *config,
576 const struct __DriverAPIRec *driverAPI);
577
578 /* Test the version of the internal GLX API. Returns a value like strcmp. */
579 extern int
580 driCompareGLXAPIVersion( GLuint required_version );
581
582 extern float
583 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
584 int64_t last_swap_ust, int64_t current_ust );
585
586 #endif /* GLX_DIRECT_RENDERING */
587
588 #endif /* _DRI_UTIL_H_ */