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