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