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