Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / common / dri_util.h
1 /*
2 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file dri_util.h
28 * DRI utility functions definitions.
29 *
30 * This module acts as glue between GLX and the actual hardware driver. A DRI
31 * driver doesn't really \e have to use any of this - it's optional. But, some
32 * useful stuff is done here that otherwise would have to be duplicated in most
33 * drivers.
34 *
35 * Basically, these utility functions take care of some of the dirty details of
36 * screen initialization, context creation, context binding, DRM setup, etc.
37 *
38 * These functions are compiled into each DRI driver so libGL.so knows nothing
39 * about them.
40 *
41 * \sa dri_util.c.
42 *
43 * \author Kevin E. Martin <kevin@precisioninsight.com>
44 * \author Brian Paul <brian@precisioninsight.com>
45 */
46
47 #ifndef _DRI_UTIL_H_
48 #define _DRI_UTIL_H_
49
50 #include <GL/gl.h>
51 #include <drm.h>
52 #include <drm_sarea.h>
53 #include <xf86drm.h>
54 #include "main/glheader.h"
55 #include "GL/internal/glcore.h"
56 #include "GL/internal/dri_interface.h"
57
58 #define GLX_BAD_CONTEXT 5
59
60 typedef struct __DRIswapInfoRec __DRIswapInfo;
61
62 /* Typedefs to avoid rewriting the world. */
63 typedef struct __DRIscreenRec __DRIscreenPrivate;
64 typedef struct __DRIdrawableRec __DRIdrawablePrivate;
65 typedef struct __DRIcontextRec __DRIcontextPrivate;
66
67 /**
68 * Extensions.
69 */
70 extern const __DRIlegacyExtension driLegacyExtension;
71 extern const __DRIcoreExtension driCoreExtension;
72 extern const __DRIextension driReadDrawableExtension;
73 extern const __DRIcopySubBufferExtension driCopySubBufferExtension;
74 extern const __DRIswapControlExtension driSwapControlExtension;
75 extern const __DRIframeTrackingExtension driFrameTrackingExtension;
76 extern const __DRImediaStreamCounterExtension driMediaStreamCounterExtension;
77
78 /**
79 * Used by DRI_VALIDATE_DRAWABLE_INFO
80 */
81 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
82 do { \
83 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
84 __driUtilUpdateDrawableInfo(pDrawPriv); \
85 } \
86 } while (0)
87
88
89 /**
90 * Utility macro to validate the drawable information.
91 *
92 * See __DRIdrawable::pStamp and __DRIdrawable::lastStamp.
93 */
94 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
95 do { \
96 while (*(pdp->pStamp) != pdp->lastStamp) { \
97 register unsigned int hwContext = psp->pSAREA->lock.lock & \
98 ~(DRM_LOCK_HELD | DRM_LOCK_CONT); \
99 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
100 \
101 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
102 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
103 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
104 \
105 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
106 } \
107 } while (0)
108
109
110 /**
111 * Driver callback functions.
112 *
113 * Each DRI driver must have one of these structures with all the pointers set
114 * to appropriate functions within the driver.
115 *
116 * When glXCreateContext() is called, for example, it'll call a helper function
117 * dri_util.c which in turn will jump through the \a CreateContext pointer in
118 * this structure.
119 */
120 struct __DriverAPIRec {
121 const __DRIconfig **(*InitScreen) (__DRIscreen * priv);
122
123 /**
124 * Screen destruction callback
125 */
126 void (*DestroyScreen)(__DRIscreen *driScrnPriv);
127
128 /**
129 * Context creation callback
130 */
131 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
132 __DRIcontext *driContextPriv,
133 void *sharedContextPrivate);
134
135 /**
136 * Context destruction callback
137 */
138 void (*DestroyContext)(__DRIcontext *driContextPriv);
139
140 /**
141 * Buffer (drawable) creation callback
142 */
143 GLboolean (*CreateBuffer)(__DRIscreen *driScrnPriv,
144 __DRIdrawable *driDrawPriv,
145 const __GLcontextModes *glVis,
146 GLboolean pixmapBuffer);
147
148 /**
149 * Buffer (drawable) destruction callback
150 */
151 void (*DestroyBuffer)(__DRIdrawable *driDrawPriv);
152
153 /**
154 * Buffer swapping callback
155 */
156 void (*SwapBuffers)(__DRIdrawable *driDrawPriv);
157
158 /**
159 * Context activation callback
160 */
161 GLboolean (*MakeCurrent)(__DRIcontext *driContextPriv,
162 __DRIdrawable *driDrawPriv,
163 __DRIdrawable *driReadPriv);
164
165 /**
166 * Context unbinding callback
167 */
168 GLboolean (*UnbindContext)(__DRIcontext *driContextPriv);
169
170 /**
171 * Retrieves statistics about buffer swap operations. Required if
172 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
173 */
174 int (*GetSwapInfo)( __DRIdrawable *dPriv, __DRIswapInfo * sInfo );
175
176
177 /**
178 * These are required if GLX_OML_sync_control is supported.
179 */
180 /*@{*/
181 int (*WaitForMSC)( __DRIdrawable *priv, int64_t target_msc,
182 int64_t divisor, int64_t remainder,
183 int64_t * msc );
184 int (*WaitForSBC)( __DRIdrawable *priv, int64_t target_sbc,
185 int64_t * msc, int64_t * sbc );
186
187 int64_t (*SwapBuffersMSC)( __DRIdrawable *priv, int64_t target_msc,
188 int64_t divisor, int64_t remainder );
189 /*@}*/
190 void (*CopySubBuffer)(__DRIdrawable *driDrawPriv,
191 int x, int y, int w, int h);
192
193 /**
194 * New version of GetMSC so we can pass drawable data to the low
195 * level DRM driver (e.g. pipe info). Required if
196 * GLX_SGI_video_sync or GLX_OML_sync_control is supported.
197 */
198 int (*GetDrawableMSC) ( __DRIscreen * priv,
199 __DRIdrawable *drawablePrivate,
200 int64_t *count);
201
202
203
204 /* DRI2 Entry point */
205 const __DRIconfig **(*InitScreen2) (__DRIscreen * priv);
206 };
207
208 extern const struct __DriverAPIRec driDriverAPI;
209
210
211 struct __DRIswapInfoRec {
212 /**
213 * Number of swapBuffers operations that have been *completed*.
214 */
215 uint64_t swap_count;
216
217 /**
218 * Unadjusted system time of the last buffer swap. This is the time
219 * when the swap completed, not the time when swapBuffers was called.
220 */
221 int64_t swap_ust;
222
223 /**
224 * Number of swap operations that occurred after the swap deadline. That
225 * is if a swap happens more than swap_interval frames after the previous
226 * swap, it has missed its deadline. If swap_interval is 0, then the
227 * swap deadline is 1 frame after the previous swap.
228 */
229 uint64_t swap_missed_count;
230
231 /**
232 * Amount of time used by the last swap that missed its deadline. This
233 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
234 * time_for_single_vrefresh)). If the actual value of swap_interval is
235 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
236 * should be greater-than 1.0.
237 */
238 float swap_missed_usage;
239 };
240
241
242 /**
243 * Per-drawable private DRI driver information.
244 */
245 struct __DRIdrawableRec {
246 /**
247 * Kernel drawable handle
248 */
249 drm_drawable_t hHWDrawable;
250
251 /**
252 * Driver's private drawable information.
253 *
254 * This structure is opaque.
255 */
256 void *driverPrivate;
257
258 /**
259 * Private data from the loader. We just hold on to it and pass
260 * it back when calling into loader provided functions.
261 */
262 void *loaderPrivate;
263
264 /**
265 * Reference count for number of context's currently bound to this
266 * drawable.
267 *
268 * Once it reaches zero, the drawable can be destroyed.
269 *
270 * \note This behavior will change with GLX 1.3.
271 */
272 int refcount;
273
274 /**
275 * Index of this drawable information in the SAREA.
276 */
277 unsigned int index;
278
279 /**
280 * Pointer to the "drawable has changed ID" stamp in the SAREA.
281 */
282 unsigned int *pStamp;
283
284 /**
285 * Last value of the stamp.
286 *
287 * If this differs from the value stored at __DRIdrawable::pStamp,
288 * then the drawable information has been modified by the X server, and the
289 * drawable information (below) should be retrieved from the X server.
290 */
291 unsigned int lastStamp;
292
293 /**
294 * \name Drawable
295 *
296 * Drawable information used in software fallbacks.
297 */
298 /*@{*/
299 int x;
300 int y;
301 int w;
302 int h;
303 int numClipRects;
304 drm_clip_rect_t *pClipRects;
305 /*@}*/
306
307 /**
308 * \name Back and depthbuffer
309 *
310 * Information about the back and depthbuffer where different from above.
311 */
312 /*@{*/
313 int backX;
314 int backY;
315 int backClipRectType;
316 int numBackClipRects;
317 drm_clip_rect_t *pBackClipRects;
318 /*@}*/
319
320 /**
321 * \name Vertical blank tracking information
322 * Used for waiting on vertical blank events.
323 */
324 /*@{*/
325 unsigned int vblSeq;
326 unsigned int vblFlags;
327 /*@}*/
328
329 /**
330 * \name Monotonic MSC tracking
331 *
332 * Low level driver is responsible for updating msc_base and
333 * vblSeq values so that higher level code can calculate
334 * a new msc value or msc target for a WaitMSC call. The new value
335 * will be:
336 * msc = msc_base + get_vblank_count() - vblank_base;
337 *
338 * And for waiting on a value, core code will use:
339 * actual_target = target_msc - msc_base + vblank_base;
340 */
341 /*@{*/
342 int64_t vblank_base;
343 int64_t msc_base;
344 /*@}*/
345
346 /**
347 * Pointer to context to which this drawable is currently bound.
348 */
349 __DRIcontext *driContextPriv;
350
351 /**
352 * Pointer to screen on which this drawable was created.
353 */
354 __DRIscreen *driScreenPriv;
355
356 /**
357 * Controls swap interval as used by GLX_SGI_swap_control and
358 * GLX_MESA_swap_control.
359 */
360 unsigned int swap_interval;
361 };
362
363 /**
364 * Per-context private driver information.
365 */
366 struct __DRIcontextRec {
367 /**
368 * Kernel context handle used to access the device lock.
369 */
370 drm_context_t hHWContext;
371
372 /**
373 * Device driver's private context data. This structure is opaque.
374 */
375 void *driverPrivate;
376
377 /**
378 * Pointer back to the \c __DRIcontext that contains this structure.
379 */
380 __DRIcontext *pctx;
381
382 /**
383 * Pointer to drawable currently bound to this context for drawing.
384 */
385 __DRIdrawable *driDrawablePriv;
386
387 /**
388 * Pointer to drawable currently bound to this context for reading.
389 */
390 __DRIdrawable *driReadablePriv;
391
392 /**
393 * Pointer to screen on which this context was created.
394 */
395 __DRIscreen *driScreenPriv;
396 };
397
398 /**
399 * Per-screen private driver information.
400 */
401 struct __DRIscreenRec {
402 /**
403 * Current screen's number
404 */
405 int myNum;
406
407 /**
408 * Callback functions into the hardware-specific DRI driver code.
409 */
410 struct __DriverAPIRec DriverAPI;
411
412 const __DRIextension **extensions;
413 /**
414 * DDX / 2D driver version information.
415 */
416 __DRIversion ddx_version;
417
418 /**
419 * DRI X extension version information.
420 */
421 __DRIversion dri_version;
422
423 /**
424 * DRM (kernel module) version information.
425 */
426 __DRIversion drm_version;
427
428 /**
429 * ID used when the client sets the drawable lock.
430 *
431 * The X server uses this value to detect if the client has died while
432 * holding the drawable lock.
433 */
434 int drawLockID;
435
436 /**
437 * File descriptor returned when the kernel device driver is opened.
438 *
439 * Used to:
440 * - authenticate client to kernel
441 * - map the frame buffer, SAREA, etc.
442 * - close the kernel device driver
443 */
444 int fd;
445
446 /**
447 * SAREA pointer
448 *
449 * Used to access:
450 * - the device lock
451 * - the device-independent per-drawable and per-context(?) information
452 */
453 drm_sarea_t *pSAREA;
454
455 /**
456 * \name Direct frame buffer access information
457 * Used for software fallbacks.
458 */
459 /*@{*/
460 unsigned char *pFB;
461 int fbSize;
462 int fbOrigin;
463 int fbStride;
464 int fbWidth;
465 int fbHeight;
466 int fbBPP;
467 /*@}*/
468
469 /**
470 * \name Device-dependent private information (stored in the SAREA).
471 *
472 * This data is accessed by the client driver only.
473 */
474 /*@{*/
475 void *pDevPriv;
476 int devPrivSize;
477 /*@}*/
478
479 /**
480 * Dummy context to which drawables are bound when not bound to any
481 * other context.
482 *
483 * A dummy hHWContext is created for this context, and is used by the GL
484 * core when a hardware lock is required but the drawable is not currently
485 * bound (e.g., potentially during a SwapBuffers request). The dummy
486 * context is created when the first "real" context is created on this
487 * screen.
488 */
489 __DRIcontext dummyContextPriv;
490
491 /**
492 * Device-dependent private information (not stored in the SAREA).
493 *
494 * This pointer is never touched by the DRI layer.
495 */
496 void *private;
497
498 /**
499 * Pointer back to the \c __DRIscreen that contains this structure.
500 */
501 __DRIscreen *psc;
502
503 /* Extensions provided by the loader. */
504 const __DRIgetDrawableInfoExtension *getDrawableInfo;
505 const __DRIsystemTimeExtension *systemTime;
506 const __DRIdamageExtension *damage;
507
508 struct {
509 /* Flag to indicate that this is a DRI2 screen. Many of the above
510 * fields will not be valid or initializaed in that case. */
511 int enabled;
512 __DRIdri2LoaderExtension *loader;
513 } dri2;
514
515 /* The lock actually in use, old sarea or DRI2 */
516 drmLock *lock;
517 };
518
519 extern void
520 __driUtilMessage(const char *f, ...);
521
522
523 extern void
524 __driUtilUpdateDrawableInfo(__DRIdrawable *pdp);
525
526 extern float
527 driCalculateSwapUsage( __DRIdrawable *dPriv,
528 int64_t last_swap_ust, int64_t current_ust );
529
530 extern GLint
531 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 );
532
533 #endif /* _DRI_UTIL_H_ */