patch to import Jon Smirl's work from Bitkeeper
[mesa.git] / src / glx / mini / dri_util.h
1 /**
2 * \file dri_util.h
3 * \brief DRI utility functions definitions.
4 *
5 * This module acts as glue between GLX and the actual hardware driver. A DRI
6 * driver doesn't really \e have to use any of this - it's optional. But, some
7 * useful stuff is done here that otherwise would have to be duplicated in most
8 * drivers.
9 *
10 * Basically, these utility functions take care of some of the dirty details of
11 * screen initialization, context creation, context binding, DRM setup, etc.
12 *
13 * These functions are compiled into each DRI driver so libGL.so knows nothing
14 * about them.
15 *
16 * Look for more comments in the dri_util.c file.
17 *
18 * \author Kevin E. Martin <kevin@precisioninsight.com>
19 * \author Brian Paul <brian@precisioninsight.com>
20 */
21
22 /*
23 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
24 * All Rights Reserved.
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a
27 * copy of this software and associated documentation files (the
28 * "Software"), to deal in the Software without restriction, including
29 * without limitation the rights to use, copy, modify, merge, publish,
30 * distribute, sub license, and/or sell copies of the Software, and to
31 * permit persons to whom the Software is furnished to do so, subject to
32 * the following conditions:
33 *
34 * The above copyright notice and this permission notice (including the
35 * next paragraph) shall be included in all copies or substantial portions
36 * of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
39 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
41 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
42 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
43 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 */
46
47
48 #ifndef _DRI_UTIL_H_
49 #define _DRI_UTIL_H_
50
51 #include <inttypes.h>
52 #include "dri.h" /* public entry points */
53 #include "sarea.h" /* for XF86DRISAREAPtr */
54
55 #define _SOLO
56
57 typedef unsigned long CARD32;
58 typedef int (* PFNGLXGETUSTPROC) ( uint64_t * ust );
59
60 typedef struct __DRIdisplayPrivateRec __DRIdisplayPrivate; /**< \brief Alias for __DRIdisplayPrivateRec */
61 typedef struct __DRIscreenPrivateRec __DRIscreenPrivate; /**< \brief Alias for __DRIscreenPrivateRec */
62 typedef struct __DRIcontextPrivateRec __DRIcontextPrivate; /**< \brief Alias for __DRIcontextPrivateRec */
63 typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate; /**< \brief Alias for __DRIdrawablePrivateRec */
64 typedef struct __DRIswapInfoRec __DRIswapInfo; /**< \brief Alias for __DRIswapInfoPrivateRec */
65
66
67 /**
68 * Used by DRI_VALIDATE_DRAWABLE_INFO
69 */
70 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
71 do { \
72 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
73 __driUtilUpdateDrawableInfo(pDrawPriv); \
74 } \
75 } while (0)
76
77
78 /**
79 * \brief Utility macro to validate the drawable information.
80 *
81 * See __DRIdrawablePrivateRec::pStamp and __DRIdrawablePrivateRec::lastStamp.
82 */
83 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
84 do { \
85 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
86 } while (0)
87
88
89 /**
90 * Driver callback functions.
91 *
92 * Each DRI driver must have one of these structures with all the pointers set
93 * to appropriate functions within the driver.
94 *
95 * When glXCreateContext() is called, for example, it'll call a helper function
96 * dri_util.c which in turn will jump through the \a CreateContext pointer in
97 * this structure.
98 */
99 struct __DriverAPIRec {
100 /**
101 * Driver initialization callback
102 */
103 GLboolean (*InitDriver)(__DRIscreenPrivate *driScrnPriv);
104
105 /**
106 * Screen destruction callback
107 */
108 void (*DestroyScreen)(__DRIscreenPrivate *driScrnPriv);
109
110 /**
111 * Context creation callback
112 */
113 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
114 __DRIcontextPrivate *driContextPriv,
115 void *sharedContextPrivate);
116
117 /**
118 * Context destruction callback
119 */
120 void (*DestroyContext)(__DRIcontextPrivate *driContextPriv);
121
122 /**
123 * Buffer (drawable) creation callback
124 */
125 GLboolean (*CreateBuffer)(__DRIscreenPrivate *driScrnPriv,
126 __DRIdrawablePrivate *driDrawPriv,
127 const __GLcontextModes *glVis,
128 GLboolean pixmapBuffer);
129
130 /**
131 * Buffer (drawable) destruction callback
132 */
133 void (*DestroyBuffer)(__DRIdrawablePrivate *driDrawPriv);
134
135 /**
136 * Buffer swapping callback
137 */
138 void (*SwapBuffers)(__DRIdrawablePrivate *driDrawPriv);
139
140 /**
141 * Context activation callback
142 */
143 GLboolean (*MakeCurrent)(__DRIcontextPrivate *driContextPriv,
144 __DRIdrawablePrivate *driDrawPriv,
145 __DRIdrawablePrivate *driReadPriv);
146
147 /**
148 * Context unbinding callback
149 */
150 GLboolean (*UnbindContext)(__DRIcontextPrivate *driContextPriv);
151
152 /**
153 * Full screen mode opening callback.
154 *
155 * \deprecated Full screen functionality is no longer used by DRI.
156 * Drivers should simply install a function returning
157 * \c GL_TRUE for backwards compatability.
158 */
159 GLboolean (*OpenFullScreen)(__DRIcontextPrivate *driContextPriv);
160
161 /**
162 * Full screen mode closing callback.
163 *
164 * \deprecated Full screen functionality is no longer used by DRI.
165 * Drivers should simply install a function returning
166 * \c GL_TRUE for backwards compatability.
167 */
168 GLboolean (*CloseFullScreen)(__DRIcontextPrivate *driContextPriv);
169
170 /* Retrieves statistics about buffer swap operations. Required if
171 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
172 */
173 int (*GetSwapInfo)( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
174
175
176 /* Required if GLX_SGI_video_sync or GLX_OML_sync_control is
177 * supported.
178 */
179 int (*GetMSC)( __DRIscreenPrivate * priv, int64_t * count );
180
181 /**
182 * These are required if GLX_OML_sync_control is supported.
183 */
184 /*@{*/
185 int (*WaitForMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
186 int64_t divisor, int64_t remainder,
187 int64_t * msc );
188 int (*WaitForSBC)( __DRIdrawablePrivate *priv, int64_t target_sbc,
189 int64_t * msc, int64_t * sbc );
190
191 int64_t (*SwapBuffersMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
192 int64_t divisor, int64_t remainder );
193 /*@}*/
194 };
195
196
197 struct __DRIswapInfoRec {
198 /**
199 * Number of swapBuffers operations that have been *completed*.
200 */
201 uint64_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 uint64_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 * \brief Per-drawable private DRI driver information.
230 *
231 */
232 struct __DRIdrawablePrivateRec {
233
234 /**
235 * \brief Public entry points
236 */
237 __DRIdrawable entry;
238
239 /**
240 * \brief Kernel drawable handle
241 *
242 * \note Not currently used.
243 */
244 drmDrawable hHWDrawable;
245
246 /**
247 * \brief Driver's private drawable information.
248 *
249 * This structure is opaque.
250 */
251 void *driverPrivate;
252
253 /**
254 * \brief Reference count for number of context's currently bound to this
255 * drawable.
256 *
257 * Once it reaches zero, the drawable can be destroyed.
258 *
259 * \note This behavior will change with GLX 1.3.
260 */
261 int refcount;
262
263 /**
264 * \brief Index of this drawable information in the SAREA.
265 */
266 unsigned int index;
267
268 /**
269 * \brief Pointer to the "drawable has changed ID" stamp in the SAREA.
270 */
271 unsigned int *pStamp;
272
273 /**
274 * \brief Last value of the stamp.
275 *
276 * If this differs from the value stored at
277 * __DRIdrawablePrivateRec::pStamp, then the drawable information has been
278 * modified by the X server, and the drawable information (below) should be
279 * retrieved from the X server.
280 */
281 unsigned int lastStamp;
282
283 /**
284 * \name Drawable
285 * Drawable information used in software fallbacks.
286 */
287 /*@{*/
288 int x;
289 int y;
290 int w;
291 int h;
292 int numClipRects;
293 XF86DRIClipRectPtr pClipRects;
294 /*@}*/
295
296 /**
297 * \name Back and depthbuffer
298 * Information about the back and depthbuffer where different from above.
299 */
300 /*@{*/
301 int backX;
302 int backY;
303 int backClipRectType;
304 int numBackClipRects;
305 XF86DRIClipRectPtr pBackClipRects;
306 /*@}*/
307
308 /**
309 * \brief Pointer to context to which this drawable is currently bound.
310 */
311 __DRIcontextPrivate *driContextPriv;
312
313 /**
314 * \brief Pointer to screen on which this drawable was created.
315 */
316 __DRIscreenPrivate *driScreenPriv;
317
318 int cpp;
319 void *frontBuffer;
320 void *backBuffer;
321 void *currentBuffer;
322 int currentPitch;
323
324 int depthCpp;
325 void *depthBuffer;
326 int depthPitch;
327
328 /**
329 * \brief Called via glXSwapBuffers().
330 */
331 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
332 };
333
334 /**
335 * \brief Per-context private driver information.
336 */
337 struct __DRIcontextPrivateRec {
338 /**
339 * \brief Public entry points
340 */
341 __DRIcontext entry;
342
343 /**
344 * \brief Kernel context handle used to access the device lock.
345 */
346 drmContext hHWContext;
347
348 /**
349 * \brief Device driver's private context data. This structure is opaque.
350 */
351 void *driverPrivate;
352
353 /**
354 * \brief Pointer to drawable currently bound to this context.
355 */
356 __DRIdrawablePrivate *driDrawablePriv;
357
358 /**
359 * \brief Pointer to screen on which this context was created.
360 */
361 __DRIscreenPrivate *driScreenPriv;
362 };
363
364 /**
365 * \brief Per-screen private driver information.
366 */
367 struct __DRIscreenPrivateRec {
368
369 /**
370 * \brief Public entry points
371 */
372 __DRIscreen entry;
373
374 /**
375 * \brief Callback functions into the hardware-specific DRI driver code.
376 */
377 struct __DriverAPIRec DriverAPI;
378
379 /**
380 * \name DDX version
381 * DDX / 2D driver version information.
382 */
383 /*@{*/
384 int ddxMajor;
385 int ddxMinor;
386 int ddxPatch;
387 /*@}*/
388
389 /**
390 * \name DRI version
391 * DRI X extension version information.
392 */
393 /*@{*/
394 int driMajor;
395 int driMinor;
396 int driPatch;
397 /*@}*/
398
399 /**
400 * \name DRM version
401 * DRM (kernel module) version information.
402 */
403 /*@{*/
404 int drmMajor;
405 int drmMinor;
406 int drmPatch;
407 /*@}*/
408
409 /**
410 * \brief ID used when the client sets the drawable lock.
411 *
412 * The X server uses this value to detect if the client has died while
413 * holding the drawable lock.
414 */
415 int drawLockID;
416
417 /**
418 * \brief File descriptor returned when the kernel device driver is opened.
419 *
420 * Used to:
421 * - authenticate client to kernel
422 * - map the frame buffer, SAREA, etc.
423 * - close the kernel device driver
424 */
425 int fd;
426
427 /**
428 * \brief SAREA pointer
429 *
430 * Used to access:
431 * - the device lock
432 * - the device-independent per-drawable and per-context(?) information
433 */
434 XF86DRISAREAPtr pSAREA;
435
436 /**
437 * \name Direct frame buffer access information
438 * Used for software fallbacks.
439 */
440 /*@{*/
441 unsigned char *pFB;
442 int fbSize;
443 int fbOrigin;
444 int fbStride;
445 int fbWidth;
446 int fbHeight;
447 int fbBPP;
448 /*@}*/
449
450 /**
451 * \name Device-dependent private information (stored in the SAREA).
452 *
453 * This data is accessed by the client driver only.
454 */
455 /*@{*/
456 void *pDevPriv;
457 int devPrivSize;
458 /*@}*/
459
460 /**
461 * \brief Dummy context to which drawables are bound when not bound to any
462 * other context.
463 *
464 * A dummy hHWContext is created for this context, and is used by the GL
465 * core when a hardware lock is required but the drawable is not currently
466 * bound (e.g., potentially during a SwapBuffers request). The dummy
467 * context is created when the first "real" context is created on this
468 * screen.
469 */
470 __DRIcontextPrivate dummyContextPriv;
471
472 /**
473 * \brief Hash table to hold the drawable information for this screen.
474 */
475 void *drawHash;
476
477 /**
478 * \brief Device-dependent private information (not stored in the SAREA).
479 *
480 * This pointer is never touched by the DRI layer.
481 */
482 void *private;
483
484 /**
485 * \brief Full screen mode.
486 *
487 * If we're in full screen mode (via DRIOpenFullScreen()), this points to
488 * the drawable that was bound. Otherwise, this is NULL.
489 */
490 __DRIdrawablePrivate *fullscreen;
491 };
492
493 extern void
494 __driUtilMessage(const char *f, ...);
495
496
497 extern void
498 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
499
500
501 extern __DRIscreenPrivate *
502 __driUtilCreateScreen(struct DRIDriverRec *driver,
503 struct DRIDriverContextRec *driverContext,
504 const struct __DriverAPIRec *driverAPI);
505
506 __DRIscreenPrivate *
507 __driUtilCreateScreenNoDRM(struct DRIDriverRec *driver,
508 struct DRIDriverContextRec *driverContext,
509 const struct __DriverAPIRec *driverAPI);
510
511 extern float
512 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
513 int64_t last_swap_ust, int64_t current_ust );
514
515 /* Test the version of the internal GLX API. Returns a value like strcmp. */
516 extern int
517 driCompareGLXAPIVersion( GLuint required_version );
518
519 /** This is optionally implemented in each driver */
520 extern void
521 __driRegisterExtensions( void );
522
523 #endif /* _DRI_UTIL_H_ */