Merge branch 'master' of ssh://people.freedesktop.org/~jbarnes/mesa
[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 /**
63 * Extensions.
64 */
65 extern const __DRIlegacyExtension driLegacyExtension;
66 extern const __DRIcoreExtension driCoreExtension;
67 extern const __DRIdri2Extension driDRI2Extension;
68 extern const __DRIextension driReadDrawableExtension;
69 extern const __DRIcopySubBufferExtension driCopySubBufferExtension;
70 extern const __DRIswapControlExtension driSwapControlExtension;
71 extern const __DRIframeTrackingExtension driFrameTrackingExtension;
72 extern const __DRImediaStreamCounterExtension driMediaStreamCounterExtension;
73
74 /**
75 * Used by DRI_VALIDATE_DRAWABLE_INFO
76 */
77 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
78 do { \
79 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
80 __driUtilUpdateDrawableInfo(pDrawPriv); \
81 } \
82 } while (0)
83
84
85 /**
86 * Utility macro to validate the drawable information.
87 *
88 * See __DRIdrawable::pStamp and __DRIdrawable::lastStamp.
89 */
90 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
91 do { \
92 while (*(pdp->pStamp) != pdp->lastStamp) { \
93 register unsigned int hwContext = psp->pSAREA->lock.lock & \
94 ~(DRM_LOCK_HELD | DRM_LOCK_CONT); \
95 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
96 \
97 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
98 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
99 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
100 \
101 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
102 } \
103 } while (0)
104
105 /**
106 * Same as above, but for two drawables simultaneously.
107 *
108 */
109
110 #define DRI_VALIDATE_TWO_DRAWABLES_INFO(psp, pdp, prp) \
111 do { \
112 while (*((pdp)->pStamp) != (pdp)->lastStamp || \
113 *((prp)->pStamp) != (prp)->lastStamp) { \
114 register unsigned int hwContext = (psp)->pSAREA->lock.lock & \
115 ~(DRM_LOCK_HELD | DRM_LOCK_CONT); \
116 DRM_UNLOCK((psp)->fd, &(psp)->pSAREA->lock, hwContext); \
117 \
118 DRM_SPINLOCK(&(psp)->pSAREA->drawable_lock, (psp)->drawLockID); \
119 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
120 DRI_VALIDATE_DRAWABLE_INFO_ONCE(prp); \
121 DRM_SPINUNLOCK(&(psp)->pSAREA->drawable_lock, (psp)->drawLockID); \
122 \
123 DRM_LIGHT_LOCK((psp)->fd, &(psp)->pSAREA->lock, hwContext); \
124 } \
125 } while (0)
126
127
128 /**
129 * Driver callback functions.
130 *
131 * Each DRI driver must have one of these structures with all the pointers set
132 * to appropriate functions within the driver.
133 *
134 * When glXCreateContext() is called, for example, it'll call a helper function
135 * dri_util.c which in turn will jump through the \a CreateContext pointer in
136 * this structure.
137 */
138 struct __DriverAPIRec {
139 const __DRIconfig **(*InitScreen) (__DRIscreen * priv);
140
141 /**
142 * Screen destruction callback
143 */
144 void (*DestroyScreen)(__DRIscreen *driScrnPriv);
145
146 /**
147 * Context creation callback
148 */
149 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
150 __DRIcontext *driContextPriv,
151 void *sharedContextPrivate);
152
153 /**
154 * Context destruction callback
155 */
156 void (*DestroyContext)(__DRIcontext *driContextPriv);
157
158 /**
159 * Buffer (drawable) creation callback
160 */
161 GLboolean (*CreateBuffer)(__DRIscreen *driScrnPriv,
162 __DRIdrawable *driDrawPriv,
163 const __GLcontextModes *glVis,
164 GLboolean pixmapBuffer);
165
166 /**
167 * Buffer (drawable) destruction callback
168 */
169 void (*DestroyBuffer)(__DRIdrawable *driDrawPriv);
170
171 /**
172 * Buffer swapping callback
173 */
174 void (*SwapBuffers)(__DRIdrawable *driDrawPriv);
175
176 /**
177 * Context activation callback
178 */
179 GLboolean (*MakeCurrent)(__DRIcontext *driContextPriv,
180 __DRIdrawable *driDrawPriv,
181 __DRIdrawable *driReadPriv);
182
183 /**
184 * Context unbinding callback
185 */
186 GLboolean (*UnbindContext)(__DRIcontext *driContextPriv);
187
188 /**
189 * Retrieves statistics about buffer swap operations. Required if
190 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
191 */
192 int (*GetSwapInfo)( __DRIdrawable *dPriv, __DRIswapInfo * sInfo );
193
194
195 /**
196 * These are required if GLX_OML_sync_control is supported.
197 */
198 /*@{*/
199 int (*WaitForMSC)( __DRIdrawable *priv, int64_t target_msc,
200 int64_t divisor, int64_t remainder,
201 int64_t * msc );
202 int (*WaitForSBC)( __DRIdrawable *priv, int64_t target_sbc,
203 int64_t * msc, int64_t * sbc );
204
205 int64_t (*SwapBuffersMSC)( __DRIdrawable *priv, int64_t target_msc,
206 int64_t divisor, int64_t remainder );
207 /*@}*/
208 void (*CopySubBuffer)(__DRIdrawable *driDrawPriv,
209 int x, int y, int w, int h);
210
211 /**
212 * New version of GetMSC so we can pass drawable data to the low
213 * level DRM driver (e.g. pipe info). Required if
214 * GLX_SGI_video_sync or GLX_OML_sync_control is supported.
215 */
216 int (*GetDrawableMSC) ( __DRIscreen * priv,
217 __DRIdrawable *drawablePrivate,
218 int64_t *count);
219
220
221
222 /* DRI2 Entry point */
223 const __DRIconfig **(*InitScreen2) (__DRIscreen * priv);
224 };
225
226 extern const struct __DriverAPIRec driDriverAPI;
227
228
229 struct __DRIswapInfoRec {
230 /**
231 * Number of swapBuffers operations that have been *completed*.
232 */
233 uint64_t swap_count;
234
235 /**
236 * Unadjusted system time of the last buffer swap. This is the time
237 * when the swap completed, not the time when swapBuffers was called.
238 */
239 int64_t swap_ust;
240
241 /**
242 * Number of swap operations that occurred after the swap deadline. That
243 * is if a swap happens more than swap_interval frames after the previous
244 * swap, it has missed its deadline. If swap_interval is 0, then the
245 * swap deadline is 1 frame after the previous swap.
246 */
247 uint64_t swap_missed_count;
248
249 /**
250 * Amount of time used by the last swap that missed its deadline. This
251 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
252 * time_for_single_vrefresh)). If the actual value of swap_interval is
253 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
254 * should be greater-than 1.0.
255 */
256 float swap_missed_usage;
257 };
258
259
260 /**
261 * Per-drawable private DRI driver information.
262 */
263 struct __DRIdrawableRec {
264 /**
265 * Kernel drawable handle
266 */
267 drm_drawable_t hHWDrawable;
268
269 /**
270 * Driver's private drawable information.
271 *
272 * This structure is opaque.
273 */
274 void *driverPrivate;
275
276 /**
277 * Private data from the loader. We just hold on to it and pass
278 * it back when calling into loader provided functions.
279 */
280 void *loaderPrivate;
281
282 /**
283 * Reference count for number of context's currently bound to this
284 * drawable.
285 *
286 * Once it reaches zero, the drawable can be destroyed.
287 *
288 * \note This behavior will change with GLX 1.3.
289 */
290 int refcount;
291
292 /**
293 * Index of this drawable information in the SAREA.
294 */
295 unsigned int index;
296
297 /**
298 * Pointer to the "drawable has changed ID" stamp in the SAREA.
299 */
300 unsigned int *pStamp;
301
302 /**
303 * Last value of the stamp.
304 *
305 * If this differs from the value stored at __DRIdrawable::pStamp,
306 * then the drawable information has been modified by the X server, and the
307 * drawable information (below) should be retrieved from the X server.
308 */
309 unsigned int lastStamp;
310
311 /**
312 * \name Drawable
313 *
314 * Drawable information used in software fallbacks.
315 */
316 /*@{*/
317 int x;
318 int y;
319 int w;
320 int h;
321 int numClipRects;
322 drm_clip_rect_t *pClipRects;
323 /*@}*/
324
325 /**
326 * \name Back and depthbuffer
327 *
328 * Information about the back and depthbuffer where different from above.
329 */
330 /*@{*/
331 int backX;
332 int backY;
333 int backClipRectType;
334 int numBackClipRects;
335 drm_clip_rect_t *pBackClipRects;
336 /*@}*/
337
338 /**
339 * \name Vertical blank tracking information
340 * Used for waiting on vertical blank events.
341 */
342 /*@{*/
343 unsigned int vblSeq;
344 unsigned int vblFlags;
345 /*@}*/
346
347 /**
348 * \name Monotonic MSC tracking
349 *
350 * Low level driver is responsible for updating msc_base and
351 * vblSeq values so that higher level code can calculate
352 * a new msc value or msc target for a WaitMSC call. The new value
353 * will be:
354 * msc = msc_base + get_vblank_count() - vblank_base;
355 *
356 * And for waiting on a value, core code will use:
357 * actual_target = target_msc - msc_base + vblank_base;
358 */
359 /*@{*/
360 int64_t vblank_base;
361 int64_t msc_base;
362 /*@}*/
363
364 /**
365 * Pointer to context to which this drawable is currently bound.
366 */
367 __DRIcontext *driContextPriv;
368
369 /**
370 * Pointer to screen on which this drawable was created.
371 */
372 __DRIscreen *driScreenPriv;
373
374 /**
375 * Controls swap interval as used by GLX_SGI_swap_control and
376 * GLX_MESA_swap_control.
377 */
378 unsigned int swap_interval;
379
380 GLboolean validBuffers;
381 };
382
383 /**
384 * Per-context private driver information.
385 */
386 struct __DRIcontextRec {
387 /**
388 * Kernel context handle used to access the device lock.
389 */
390 drm_context_t hHWContext;
391
392 /**
393 * Device driver's private context data. This structure is opaque.
394 */
395 void *driverPrivate;
396
397 /**
398 * Pointer back to the \c __DRIcontext that contains this structure.
399 */
400 __DRIcontext *pctx;
401
402 /**
403 * Pointer to drawable currently bound to this context for drawing.
404 */
405 __DRIdrawable *driDrawablePriv;
406
407 /**
408 * Pointer to drawable currently bound to this context for reading.
409 */
410 __DRIdrawable *driReadablePriv;
411
412 /**
413 * Pointer to screen on which this context was created.
414 */
415 __DRIscreen *driScreenPriv;
416 };
417
418 /**
419 * Per-screen private driver information.
420 */
421 struct __DRIscreenRec {
422 /**
423 * Current screen's number
424 */
425 int myNum;
426
427 /**
428 * Callback functions into the hardware-specific DRI driver code.
429 */
430 struct __DriverAPIRec DriverAPI;
431
432 const __DRIextension **extensions;
433 /**
434 * DDX / 2D driver version information.
435 */
436 __DRIversion ddx_version;
437
438 /**
439 * DRI X extension version information.
440 */
441 __DRIversion dri_version;
442
443 /**
444 * DRM (kernel module) version information.
445 */
446 __DRIversion drm_version;
447
448 /**
449 * ID used when the client sets the drawable lock.
450 *
451 * The X server uses this value to detect if the client has died while
452 * holding the drawable lock.
453 */
454 int drawLockID;
455
456 /**
457 * File descriptor returned when the kernel device driver is opened.
458 *
459 * Used to:
460 * - authenticate client to kernel
461 * - map the frame buffer, SAREA, etc.
462 * - close the kernel device driver
463 */
464 int fd;
465
466 /**
467 * SAREA pointer
468 *
469 * Used to access:
470 * - the device lock
471 * - the device-independent per-drawable and per-context(?) information
472 */
473 drm_sarea_t *pSAREA;
474
475 /**
476 * \name Direct frame buffer access information
477 * Used for software fallbacks.
478 */
479 /*@{*/
480 unsigned char *pFB;
481 int fbSize;
482 int fbOrigin;
483 int fbStride;
484 int fbWidth;
485 int fbHeight;
486 int fbBPP;
487 /*@}*/
488
489 /**
490 * \name Device-dependent private information (stored in the SAREA).
491 *
492 * This data is accessed by the client driver only.
493 */
494 /*@{*/
495 void *pDevPriv;
496 int devPrivSize;
497 /*@}*/
498
499 /**
500 * Dummy context to which drawables are bound when not bound to any
501 * other context.
502 *
503 * A dummy hHWContext is created for this context, and is used by the GL
504 * core when a hardware lock is required but the drawable is not currently
505 * bound (e.g., potentially during a SwapBuffers request). The dummy
506 * context is created when the first "real" context is created on this
507 * screen.
508 */
509 __DRIcontext dummyContextPriv;
510
511 /**
512 * Device-dependent private information (not stored in the SAREA).
513 *
514 * This pointer is never touched by the DRI layer.
515 */
516 void *private;
517
518 /**
519 * Pointer back to the \c __DRIscreen that contains this structure.
520 */
521 __DRIscreen *psc;
522
523 /* Extensions provided by the loader. */
524 const __DRIgetDrawableInfoExtension *getDrawableInfo;
525 const __DRIsystemTimeExtension *systemTime;
526 const __DRIdamageExtension *damage;
527
528 struct {
529 /* Flag to indicate that this is a DRI2 screen. Many of the above
530 * fields will not be valid or initializaed in that case. */
531 int enabled;
532 __DRIdri2LoaderExtension *loader;
533 } dri2;
534
535 /* The lock actually in use, old sarea or DRI2 */
536 drmLock *lock;
537 };
538
539 extern void
540 __driUtilMessage(const char *f, ...);
541
542
543 extern void
544 __driUtilUpdateDrawableInfo(__DRIdrawable *pdp);
545
546 extern float
547 driCalculateSwapUsage( __DRIdrawable *dPriv,
548 int64_t last_swap_ust, int64_t current_ust );
549
550 extern GLint
551 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 );
552
553 #endif /* _DRI_UTIL_H_ */