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