Add support for optimized versions of the code underlying ReadPixels
[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 #ifdef GLX_DIRECT_RENDERING
53
54 #define CAPI /* XXX this should be globally defined somewhere */
55
56 #include <inttypes.h>
57 #ifdef DRI_NEW_INTERFACE_ONLY
58 # include <GL/gl.h>
59 #else
60 # include "glxclient.h"
61 #endif /* DRI_NEW_INTERFACE_ONLY */
62 #include "drm.h"
63 #include "drm_sarea.h"
64 #include "GL/internal/glcore.h"
65 #include "GL/internal/dri_interface.h"
66
67 #define GLX_BAD_CONTEXT 5
68
69 /* This is a temporary relic. Once all drivers are converted to support
70 * the new interface, it can go away.
71 */
72 #ifdef DRI_NEW_INTERFACE_ONLY
73 #define USE_NEW_INTERFACE
74 #endif
75
76 typedef struct __DRIdisplayPrivateRec __DRIdisplayPrivate;
77 typedef struct __DRIscreenPrivateRec __DRIscreenPrivate;
78 typedef struct __DRIcontextPrivateRec __DRIcontextPrivate;
79 typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate;
80 typedef struct __DRIswapInfoRec __DRIswapInfo;
81
82
83 /**
84 * Used by DRI_VALIDATE_DRAWABLE_INFO
85 */
86 #define DRI_VALIDATE_DRAWABLE_INFO_ONCE(pDrawPriv) \
87 do { \
88 if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) { \
89 __driUtilUpdateDrawableInfo(pDrawPriv); \
90 } \
91 } while (0)
92
93
94 /**
95 * Utility macro to validate the drawable information.
96 *
97 * See __DRIdrawablePrivate::pStamp and __DRIdrawablePrivate::lastStamp.
98 */
99 #define DRI_VALIDATE_DRAWABLE_INFO(psp, pdp) \
100 do { \
101 while (*(pdp->pStamp) != pdp->lastStamp) { \
102 DRM_UNLOCK(psp->fd, &psp->pSAREA->lock, \
103 pdp->driContextPriv->hHWContext); \
104 \
105 DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
106 DRI_VALIDATE_DRAWABLE_INFO_ONCE(pdp); \
107 DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID); \
108 \
109 DRM_LIGHT_LOCK(psp->fd, &psp->pSAREA->lock, \
110 pdp->driContextPriv->hHWContext); \
111 } \
112 } while (0)
113
114
115 /**
116 * Driver callback functions.
117 *
118 * Each DRI driver must have one of these structures with all the pointers set
119 * to appropriate functions within the driver.
120 *
121 * When glXCreateContext() is called, for example, it'll call a helper function
122 * dri_util.c which in turn will jump through the \a CreateContext pointer in
123 * this structure.
124 */
125 struct __DriverAPIRec {
126 /**
127 * Driver initialization callback
128 */
129 GLboolean (*InitDriver)(__DRIscreenPrivate *driScrnPriv);
130
131 /**
132 * Screen destruction callback
133 */
134 void (*DestroyScreen)(__DRIscreenPrivate *driScrnPriv);
135
136 /**
137 * Context creation callback
138 */
139 GLboolean (*CreateContext)(const __GLcontextModes *glVis,
140 __DRIcontextPrivate *driContextPriv,
141 void *sharedContextPrivate);
142
143 /**
144 * Context destruction callback
145 */
146 void (*DestroyContext)(__DRIcontextPrivate *driContextPriv);
147
148 /**
149 * Buffer (drawable) creation callback
150 */
151 GLboolean (*CreateBuffer)(__DRIscreenPrivate *driScrnPriv,
152 __DRIdrawablePrivate *driDrawPriv,
153 const __GLcontextModes *glVis,
154 GLboolean pixmapBuffer);
155
156 /**
157 * Buffer (drawable) destruction callback
158 */
159 void (*DestroyBuffer)(__DRIdrawablePrivate *driDrawPriv);
160
161 /**
162 * Buffer swapping callback
163 */
164 void (*SwapBuffers)(__DRIdrawablePrivate *driDrawPriv);
165
166 /**
167 * Context activation callback
168 */
169 GLboolean (*MakeCurrent)(__DRIcontextPrivate *driContextPriv,
170 __DRIdrawablePrivate *driDrawPriv,
171 __DRIdrawablePrivate *driReadPriv);
172
173 /**
174 * Context unbinding callback
175 */
176 GLboolean (*UnbindContext)(__DRIcontextPrivate *driContextPriv);
177
178 /**
179 * Retrieves statistics about buffer swap operations. Required if
180 * GLX_OML_sync_control or GLX_MESA_swap_frame_usage is supported.
181 */
182 int (*GetSwapInfo)( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
183
184
185 /**
186 * Required if GLX_SGI_video_sync or GLX_OML_sync_control is
187 * supported.
188 */
189 int (*GetMSC)( __DRIscreenPrivate * priv, int64_t * count );
190
191 /**
192 * These are required if GLX_OML_sync_control is supported.
193 */
194 /*@{*/
195 int (*WaitForMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
196 int64_t divisor, int64_t remainder,
197 int64_t * msc );
198 int (*WaitForSBC)( __DRIdrawablePrivate *priv, int64_t target_sbc,
199 int64_t * msc, int64_t * sbc );
200
201 int64_t (*SwapBuffersMSC)( __DRIdrawablePrivate *priv, int64_t target_msc,
202 int64_t divisor, int64_t remainder );
203 /*@}*/
204 };
205
206
207 struct __DRIswapInfoRec {
208 /**
209 * Number of swapBuffers operations that have been *completed*.
210 */
211 uint64_t swap_count;
212
213 /**
214 * Unadjusted system time of the last buffer swap. This is the time
215 * when the swap completed, not the time when swapBuffers was called.
216 */
217 int64_t swap_ust;
218
219 /**
220 * Number of swap operations that occurred after the swap deadline. That
221 * is if a swap happens more than swap_interval frames after the previous
222 * swap, it has missed its deadline. If swap_interval is 0, then the
223 * swap deadline is 1 frame after the previous swap.
224 */
225 uint64_t swap_missed_count;
226
227 /**
228 * Amount of time used by the last swap that missed its deadline. This
229 * is calculated as (__glXGetUST() - swap_ust) / (swap_interval *
230 * time_for_single_vrefresh)). If the actual value of swap_interval is
231 * 0, then 1 is used instead. If swap_missed_count is non-zero, this
232 * should be greater-than 1.0.
233 */
234 float swap_missed_usage;
235 };
236
237
238 /**
239 * Per-drawable private DRI driver information.
240 */
241 struct __DRIdrawablePrivateRec {
242 /**
243 * Kernel drawable handle
244 */
245 drm_drawable_t hHWDrawable;
246
247 /**
248 * Driver's private drawable information.
249 *
250 * This structure is opaque.
251 */
252 void *driverPrivate;
253
254 /**
255 * X's drawable ID associated with this private drawable.
256 */
257 __DRIid draw;
258 __DRIdrawable *pdraw;
259
260 /**
261 * Reference count for number of context's currently bound to this
262 * drawable.
263 *
264 * Once it reaches zero, the drawable can be destroyed.
265 *
266 * \note This behavior will change with GLX 1.3.
267 */
268 int refcount;
269
270 /**
271 * Index of this drawable information in the SAREA.
272 */
273 unsigned int index;
274
275 /**
276 * Pointer to the "drawable has changed ID" stamp in the SAREA.
277 */
278 unsigned int *pStamp;
279
280 /**
281 * Last value of the stamp.
282 *
283 * If this differs from the value stored at __DRIdrawablePrivate::pStamp,
284 * then the drawable information has been modified by the X server, and the
285 * drawable information (below) should be retrieved from the X server.
286 */
287 unsigned int lastStamp;
288
289 /**
290 * \name Drawable
291 *
292 * Drawable information used in software fallbacks.
293 */
294 /*@{*/
295 int x;
296 int y;
297 int w;
298 int h;
299 int numClipRects;
300 drm_clip_rect_t *pClipRects;
301 /*@}*/
302
303 /**
304 * \name Back and depthbuffer
305 *
306 * Information about the back and depthbuffer where different from above.
307 */
308 /*@{*/
309 int backX;
310 int backY;
311 int backClipRectType;
312 int numBackClipRects;
313 drm_clip_rect_t *pBackClipRects;
314 /*@}*/
315
316 /**
317 * Pointer to context to which this drawable is currently bound.
318 */
319 __DRIcontextPrivate *driContextPriv;
320
321 /**
322 * Pointer to screen on which this drawable was created.
323 */
324 __DRIscreenPrivate *driScreenPriv;
325
326 /**
327 * \name Display and screen information.
328 *
329 * Basically just need these for when the locking code needs to call
330 * __driUtilUpdateDrawableInfo() which calls XF86DRIGetDrawableInfo().
331 */
332 /*@{*/
333 __DRInativeDisplay *display;
334 int screen;
335 /*@}*/
336
337 /**
338 * Called via glXSwapBuffers().
339 */
340 void (*swapBuffers)( __DRIdrawablePrivate *dPriv );
341
342 /**
343 * Get information about the location, size, and clip rects of the
344 * drawable within the display.
345 */
346 PFNGLXGETDRAWABLEINFOPROC getInfo;
347 };
348
349 /**
350 * Per-context private driver information.
351 */
352 struct __DRIcontextPrivateRec {
353 /**
354 * Kernel context handle used to access the device lock.
355 */
356 __DRIid contextID;
357
358 /**
359 * Kernel context handle used to access the device lock.
360 */
361 drm_context_t hHWContext;
362
363 /**
364 * Device driver's private context data. This structure is opaque.
365 */
366 void *driverPrivate;
367
368 /**
369 * This context's display pointer.
370 */
371 __DRInativeDisplay *display;
372
373 /**
374 * Pointer to drawable currently bound to this context.
375 */
376 __DRIdrawablePrivate *driDrawablePriv;
377
378 /**
379 * Pointer to screen on which this context was created.
380 */
381 __DRIscreenPrivate *driScreenPriv;
382 };
383
384 /**
385 * Per-screen private driver information.
386 */
387 struct __DRIscreenPrivateRec {
388 /**
389 * Display for this screen
390 */
391 __DRInativeDisplay *display;
392
393 /**
394 * Current screen's number
395 */
396 int myNum;
397
398 /**
399 * Callback functions into the hardware-specific DRI driver code.
400 */
401 struct __DriverAPIRec DriverAPI;
402
403 /**
404 * \name DDX version
405 * DDX / 2D driver version information.
406 * \todo Replace these fields with a \c __DRIversionRec.
407 */
408 /*@{*/
409 int ddxMajor;
410 int ddxMinor;
411 int ddxPatch;
412 /*@}*/
413
414 /**
415 * \name DRI version
416 * DRI X extension version information.
417 * \todo Replace these fields with a \c __DRIversionRec.
418 */
419 /*@{*/
420 int driMajor;
421 int driMinor;
422 int driPatch;
423 /*@}*/
424
425 /**
426 * \name DRM version
427 * DRM (kernel module) version information.
428 * \todo Replace these fields with a \c __DRIversionRec.
429 */
430 /*@{*/
431 int drmMajor;
432 int drmMinor;
433 int drmPatch;
434 /*@}*/
435
436 /**
437 * ID used when the client sets the drawable lock.
438 *
439 * The X server uses this value to detect if the client has died while
440 * holding the drawable lock.
441 */
442 int drawLockID;
443
444 /**
445 * File descriptor returned when the kernel device driver is opened.
446 *
447 * Used to:
448 * - authenticate client to kernel
449 * - map the frame buffer, SAREA, etc.
450 * - close the kernel device driver
451 */
452 int fd;
453
454 /**
455 * SAREA pointer
456 *
457 * Used to access:
458 * - the device lock
459 * - the device-independent per-drawable and per-context(?) information
460 */
461 drm_sarea_t *pSAREA;
462
463 /**
464 * \name Direct frame buffer access information
465 * Used for software fallbacks.
466 */
467 /*@{*/
468 unsigned char *pFB;
469 int fbSize;
470 int fbOrigin;
471 int fbStride;
472 int fbWidth;
473 int fbHeight;
474 int fbBPP;
475 /*@}*/
476
477 /**
478 * \name Device-dependent private information (stored in the SAREA).
479 *
480 * This data is accessed by the client driver only.
481 */
482 /*@{*/
483 void *pDevPriv;
484 int devPrivSize;
485 /*@}*/
486
487 /**
488 * Dummy context to which drawables are bound when not bound to any
489 * other context.
490 *
491 * A dummy hHWContext is created for this context, and is used by the GL
492 * core when a hardware lock is required but the drawable is not currently
493 * bound (e.g., potentially during a SwapBuffers request). The dummy
494 * context is created when the first "real" context is created on this
495 * screen.
496 */
497 __DRIcontextPrivate dummyContextPriv;
498
499 /**
500 * Hash table to hold the drawable information for this screen.
501 */
502 void *drawHash;
503
504 /**
505 * Device-dependent private information (not stored in the SAREA).
506 *
507 * This pointer is never touched by the DRI layer.
508 */
509 void *private;
510
511 /**
512 * GLX visuals / FBConfigs for this screen. These are stored as a
513 * linked list.
514 *
515 * \note
516 * This field is \b only used in conjunction with the old interfaces. If
517 * the new interfaces are used, this field will be set to \c NULL and will
518 * not be dereferenced.
519 */
520 __GLcontextModes *modes;
521
522 /**
523 * Pointer back to the \c __DRIscreen that contains this structure.
524 */
525
526 __DRIscreen *psc;
527 };
528
529
530
531 extern void
532 __driUtilMessage(const char *f, ...);
533
534
535 extern void
536 __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp);
537
538
539 extern __DRIscreenPrivate * __driUtilCreateNewScreen( __DRInativeDisplay *dpy,
540 int scrn, __DRIscreen *psc, __GLcontextModes * modes,
541 const __DRIversion * ddx_version, const __DRIversion * dri_version,
542 const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
543 drm_sarea_t *pSAREA, int fd, int internal_api_version,
544 const struct __DriverAPIRec *driverAPI );
545
546 #ifndef DRI_NEW_INTERFACE_ONLY
547 extern __DRIscreenPrivate *
548 __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
549 int numConfigs, __GLXvisualConfig *config,
550 const struct __DriverAPIRec *driverAPI);
551 #endif /* DRI_NEW_INTERFACE_ONLY */
552
553 /* Test the version of the internal GLX API. Returns a value like strcmp. */
554 extern int
555 driCompareGLXAPIVersion( GLuint required_version );
556
557 extern float
558 driCalculateSwapUsage( __DRIdrawablePrivate *dPriv,
559 int64_t last_swap_ust, int64_t current_ust );
560
561 extern void
562 (*glXGetProcAddress(const GLubyte *procname))( void );
563
564 #endif /* GLX_DIRECT_RENDERING */
565
566 #endif /* _DRI_UTIL_H_ */