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