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