[intel] Assert against conflicting relocation emits in bufmgr_fake.c
[mesa.git] / src / mesa / drivers / dri / common / dri_util.h
1 /* $XFree86: xc/lib/GL/dri/dri_util.h,v 1.1 2002/02/22 21:32:52 dawes Exp $ */
2 /**
3 * \file dri_util.h
4 * DRI utility functions definitions.
5 *
6 * This module acts as glue between GLX and the actual hardware driver. A DRI
7 * driver doesn't really \e have to use any of this - it's optional. But, some
8 * useful stuff is done here that otherwise would have to be duplicated in most
9 * drivers.
10 *
11 * Basically, these utility functions take care of some of the dirty details of
12 * screen initialization, context creation, context binding, DRM setup, etc.
13 *
14 * These functions are compiled into each DRI driver so libGL.so knows nothing
15 * about them.
16 *
17 * \sa dri_util.c.
18 *
19 * \author Kevin E. Martin <kevin@precisioninsight.com>
20 * \author Brian Paul <brian@precisioninsight.com>
21 */
22
23 /*
24 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
25 * All Rights Reserved.
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the
29 * "Software"), to deal in the Software without restriction, including
30 * without limitation the rights to use, copy, modify, merge, publish,
31 * distribute, sub license, and/or sell copies of the Software, and to
32 * permit persons to whom the Software is furnished to do so, subject to
33 * the following conditions:
34 *
35 * The above copyright notice and this permission notice (including the
36 * next paragraph) shall be included in all copies or substantial portions
37 * of the Software.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
42 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
43 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 */
47
48
49 #ifndef _DRI_UTIL_H_
50 #define _DRI_UTIL_H_
51
52 #include <GL/gl.h>
53 #include "drm.h"
54 #include "drm_sarea.h"
55 #include "xf86drm.h"
56 #include "GL/internal/glcore.h"
57 #include "GL/internal/dri_interface.h"
58
59 #define GLX_BAD_CONTEXT 5
60
61 typedef struct __DRIdisplayPrivateRec __DRIdisplayPrivate;
62 typedef struct __DRIscreenPrivateRec __DRIscreenPrivate;
63 typedef struct __DRIcontextPrivateRec __DRIcontextPrivate;
64 typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate;
65 typedef struct __DRIswapInfoRec __DRIswapInfo;
66 typedef struct __DRIutilversionRec2 __DRIutilversion2;
67
68
69 /**
70 * Driver specific entry point. Implemented by the driver. Called
71 * from the top level createNewScreen entry point to initialize the
72 * __DRIscreenPrivate struct.
73 */
74 extern __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp);
75
76 /**
77 * Extensions.
78 */
79 extern const __DRIextension driReadDrawableExtension;
80 extern const __DRIcopySubBufferExtension driCopySubBufferExtension;
81 extern const __DRIswapControlExtension driSwapControlExtension;
82 extern const __DRIframeTrackingExtension driFrameTrackingExtension;
83 extern const __DRImediaStreamCounterExtension driMediaStreamCounterExtension;
84
85 /**
86 * Used by DRI_VALIDATE_DRAWABLE_INFO
87 */
88 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
89 do { \
90 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
91 __driUtilUpdateDrawableInfo(pDrawPriv); \
92 } \
93 } while (0)
94
95
96 /**
97 * Utility macro to validate the drawable information.
98 *
99 * See __DRIdrawablePrivate::pStamp and __DRIdrawablePrivate::lastStamp.
100 */
101 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
102 do { \
103 while (*(pdp->pStamp) != pdp->lastStamp) { \
104 register unsigned int hwContext = psp->pSAREA->lock.lock & \
105 ~(DRM_LOCK_HELD | DRM_LOCK_CONT); \
106 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
107 \
108 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
109 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
110 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
111 \
112 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, hwContext); \
113 } \
114 } while (0)
115
116
117 /**
118 * Driver callback functions.
119 *
120 * Each DRI driver must have one of these structures with all the pointers set
121 * to appropriate functions within the driver.
122 *
123 * When glXCreateContext() is called, for example, it'll call a helper function
124 * dri_util.c which in turn will jump through the \a CreateContext pointer in
125 * this structure.
126 */
127 struct __DriverAPIRec {
128 /**
129 * Screen destruction callback
130 */
131 void (*DestroyScreen)(__DRIscreenPrivate *driScrnPriv);
132
133 /**
134 * Context creation callback
135 */
136 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
137 __DRIcontextPrivate *driContextPriv,
138 void *sharedContextPrivate);
139
140 /**
141 * Context destruction callback
142 */
143 void (*DestroyContext)(__DRIcontextPrivate *driContextPriv);
144
145 /**
146 * Buffer (drawable) creation callback
147 */
148 GLboolean (*CreateBuffer)(__DRIscreenPrivate *driScrnPriv,
149 __DRIdrawablePrivate *driDrawPriv,
150 const __GLcontextModes *glVis,
151 GLboolean pixmapBuffer);
152
153 /**
154 * Buffer (drawable) destruction callback
155 */
156 void (*DestroyBuffer)(__DRIdrawablePrivate *driDrawPriv);
157
158 /**
159 * Buffer swapping callback
160 */
161 void (*SwapBuffers)(__DRIdrawablePrivate *driDrawPriv);
162
163 /**
164 * Context activation callback
165 */
166 GLboolean (*MakeCurrent)(__DRIcontextPrivate *driContextPriv,
167 __DRIdrawablePrivate *driDrawPriv,
168 __DRIdrawablePrivate *driReadPriv);
169
170 /**
171 * Context unbinding callback
172 */
173 GLboolean (*UnbindContext)(__DRIcontextPrivate *driContextPriv);
174
175 /**
176 * Retrieves statistics about buffer swap operations. Required if
177 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
178 */
179 int (*GetSwapInfo)( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
180
181
182 /**
183 * Required if GLX_SGI_video_sync or GLX_OML_sync_control is
184 * supported.
185 */
186 int (*GetMSC)( __DRIscreenPrivate * priv, int64_t * count );
187
188 /**
189 * These are required if GLX_OML_sync_control is supported.
190 */
191 /*@{*/
192 int (*WaitForMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
193 int64_t divisor, int64_t remainder,
194 int64_t * msc );
195 int (*WaitForSBC)( __DRIdrawablePrivate *priv, int64_t target_sbc,
196 int64_t * msc, int64_t * sbc );
197
198 int64_t (*SwapBuffersMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
199 int64_t divisor, int64_t remainder );
200 /*@}*/
201 void (*CopySubBuffer)(__DRIdrawablePrivate *driDrawPriv,
202 int x, int y, int w, int h);
203
204 /**
205 * See corresponding field in \c __DRIscreenRec.
206 */
207 void (*setTexOffset)(__DRIcontext *pDRICtx, GLint texname,
208 unsigned long long offset, GLint depth, GLuint pitch);
209
210 /**
211 * New version of GetMSC so we can pass drawable data to the low level
212 * DRM driver (e.g. pipe info).
213 */
214 int (*GetDrawableMSC) ( __DRIscreenPrivate * priv,
215 __DRIdrawablePrivate *drawablePrivate,
216 int64_t *count);
217 };
218
219
220 struct __DRIswapInfoRec {
221 /**
222 * Number of swapBuffers operations that have been *completed*.
223 */
224 u_int64_t swap_count;
225
226 /**
227 * Unadjusted system time of the last buffer swap. This is the time
228 * when the swap completed, not the time when swapBuffers was called.
229 */
230 int64_t swap_ust;
231
232 /**
233 * Number of swap operations that occurred after the swap deadline. That
234 * is if a swap happens more than swap_interval frames after the previous
235 * swap, it has missed its deadline. If swap_interval is 0, then the
236 * swap deadline is 1 frame after the previous swap.
237 */
238 u_int64_t swap_missed_count;
239
240 /**
241 * Amount of time used by the last swap that missed its deadline. This
242 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
243 * time_for_single_vrefresh)). If the actual value of swap_interval is
244 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
245 * should be greater-than 1.0.
246 */
247 float swap_missed_usage;
248 };
249
250
251 /**
252 * Per-drawable private DRI driver information.
253 */
254 struct __DRIdrawablePrivateRec {
255 /**
256 * Kernel drawable handle
257 */
258 drm_drawable_t hHWDrawable;
259
260 /**
261 * Driver's private drawable information.
262 *
263 * This structure is opaque.
264 */
265 void *driverPrivate;
266
267 /**
268 * X's drawable ID associated with this private drawable.
269 */
270 __DRIdrawable *pdraw;
271
272 /**
273 * Reference count for number of context's currently bound to this
274 * drawable.
275 *
276 * Once it reaches zero, the drawable can be destroyed.
277 *
278 * \note This behavior will change with GLX 1.3.
279 */
280 int refcount;
281
282 /**
283 * Index of this drawable information in the SAREA.
284 */
285 unsigned int index;
286
287 /**
288 * Pointer to the "drawable has changed ID" stamp in the SAREA.
289 */
290 unsigned int *pStamp;
291
292 /**
293 * Last value of the stamp.
294 *
295 * If this differs from the value stored at __DRIdrawablePrivate::pStamp,
296 * then the drawable information has been modified by the X server, and the
297 * drawable information (below) should be retrieved from the X server.
298 */
299 unsigned int lastStamp;
300
301 /**
302 * \name Drawable
303 *
304 * Drawable information used in software fallbacks.
305 */
306 /*@{*/
307 int x;
308 int y;
309 int w;
310 int h;
311 int numClipRects;
312 drm_clip_rect_t *pClipRects;
313 /*@}*/
314
315 /**
316 * \name Back and depthbuffer
317 *
318 * Information about the back and depthbuffer where different from above.
319 */
320 /*@{*/
321 int backX;
322 int backY;
323 int backClipRectType;
324 int numBackClipRects;
325 drm_clip_rect_t *pBackClipRects;
326 /*@}*/
327
328 /**
329 * \name Vertical blank tracking information
330 * Used for waiting on vertical blank events.
331 */
332 /*@{*/
333 unsigned int vblSeq;
334 unsigned int vblFlags;
335 /*@}*/
336
337 /**
338 * \name Monotonic MSC tracking
339 *
340 * Low level driver is responsible for updating msc_base and
341 * vblSeq values so that higher level code can calculate
342 * a new msc value or msc target for a WaitMSC call. The new value
343 * will be:
344 * msc = msc_base + get_vblank_count() - vblank_base;
345 *
346 * And for waiting on a value, core code will use:
347 * actual_target = target_msc - msc_base + vblank_base;
348 */
349 /*@{*/
350 int64_t vblank_base;
351 int64_t msc_base;
352 /*@}*/
353
354 /**
355 * Pointer to context to which this drawable is currently bound.
356 */
357 __DRIcontextPrivate *driContextPriv;
358
359 /**
360 * Pointer to screen on which this drawable was created.
361 */
362 __DRIscreenPrivate *driScreenPriv;
363
364 /**
365 * Called via glXSwapBuffers().
366 */
367 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
368
369 /**
370 * Controls swap interval as used by GLX_SGI_swap_control and
371 * GLX_MESA_swap_control.
372 */
373 unsigned int swap_interval;
374 };
375
376 /**
377 * Per-context private driver information.
378 */
379 struct __DRIcontextPrivateRec {
380 /**
381 * Kernel context handle used to access the device lock.
382 */
383 drm_context_t hHWContext;
384
385 /**
386 * Device driver's private context data. This structure is opaque.
387 */
388 void *driverPrivate;
389
390 /**
391 * Pointer back to the \c __DRIcontext that contains this structure.
392 */
393 __DRIcontext *pctx;
394
395 /**
396 * Pointer to drawable currently bound to this context for drawing.
397 */
398 __DRIdrawablePrivate *driDrawablePriv;
399
400 /**
401 * Pointer to drawable currently bound to this context for reading.
402 */
403 __DRIdrawablePrivate *driReadablePriv;
404
405 /**
406 * Pointer to screen on which this context was created.
407 */
408 __DRIscreenPrivate *driScreenPriv;
409 };
410
411 /**
412 * Per-screen private driver information.
413 */
414 struct __DRIscreenPrivateRec {
415 /**
416 * Current screen's number
417 */
418 int myNum;
419
420 /**
421 * Callback functions into the hardware-specific DRI driver code.
422 */
423 struct __DriverAPIRec DriverAPI;
424
425 /**
426 * DDX / 2D driver version information.
427 */
428 __DRIversion ddx_version;
429
430 /**
431 * DRI X extension version information.
432 */
433 __DRIversion dri_version;
434
435 /**
436 * DRM (kernel module) version information.
437 */
438 __DRIversion drm_version;
439
440 /**
441 * ID used when the client sets the drawable lock.
442 *
443 * The X server uses this value to detect if the client has died while
444 * holding the drawable lock.
445 */
446 int drawLockID;
447
448 /**
449 * File descriptor returned when the kernel device driver is opened.
450 *
451 * Used to:
452 * - authenticate client to kernel
453 * - map the frame buffer, SAREA, etc.
454 * - close the kernel device driver
455 */
456 int fd;
457
458 /**
459 * SAREA pointer
460 *
461 * Used to access:
462 * - the device lock
463 * - the device-independent per-drawable and per-context(?) information
464 */
465 drm_sarea_t *pSAREA;
466
467 /**
468 * \name Direct frame buffer access information
469 * Used for software fallbacks.
470 */
471 /*@{*/
472 unsigned char *pFB;
473 int fbSize;
474 int fbOrigin;
475 int fbStride;
476 int fbWidth;
477 int fbHeight;
478 int fbBPP;
479 /*@}*/
480
481 /**
482 * \name Device-dependent private information (stored in the SAREA).
483 *
484 * This data is accessed by the client driver only.
485 */
486 /*@{*/
487 void *pDevPriv;
488 int devPrivSize;
489 /*@}*/
490
491 /**
492 * Dummy context to which drawables are bound when not bound to any
493 * other context.
494 *
495 * A dummy hHWContext is created for this context, and is used by the GL
496 * core when a hardware lock is required but the drawable is not currently
497 * bound (e.g., potentially during a SwapBuffers request). The dummy
498 * context is created when the first "real" context is created on this
499 * screen.
500 */
501 __DRIcontextPrivate dummyContextPriv;
502
503 /**
504 * Device-dependent private information (not stored in the SAREA).
505 *
506 * This pointer is never touched by the DRI layer.
507 */
508 void *private;
509
510 /**
511 * Pointer back to the \c __DRIscreen that contains this structure.
512 */
513 __DRIscreen *psc;
514
515 /**
516 * Extensions provided by this driver.
517 */
518 const __DRIextension **extensions;
519 };
520
521
522 /**
523 * Used to store a version which includes a major range instead of a single
524 * major version number.
525 */
526 struct __DRIutilversionRec2 {
527 int major_min; /** min allowed Major version number. */
528 int major_max; /** max allowed Major version number. */
529 int minor; /**< Minor version number. */
530 int patch; /**< Patch-level. */
531 };
532
533
534 extern void
535 __driUtilMessage(const char *f, ...);
536
537
538 extern void
539 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
540
541
542 extern __DRIscreenPrivate * __driUtilCreateNewScreen( int scr, __DRIscreen *psc,
543 __GLcontextModes * modes,
544 const __DRIversion * ddx_version, const __DRIversion * dri_version,
545 const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
546 drm_sarea_t *pSAREA, int fd, int internal_api_version,
547 const struct __DriverAPIRec *driverAPI );
548
549 /* Test the version of the internal GLX API. Returns a value like strcmp. */
550 extern int
551 driCompareGLXAPIVersion( GLint required_version );
552
553 extern float
554 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
555 int64_t last_swap_ust, int64_t current_ust );
556
557 /**
558 * Pointer to the \c __DRIinterfaceMethods passed to the driver by the loader.
559 *
560 * This pointer is set in the driver's \c __driCreateNewScreen function and
561 * is defined in dri_util.c.
562 */
563 extern const __DRIinterfaceMethods * dri_interface;
564
565 #endif /* _DRI_UTIL_H_ */