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