st/glx: Add xmesa_display to hold per-display variables.
[mesa.git] / src / gallium / state_trackers / glx / xlib / xm_api.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions 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 MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26
27 /* Sample Usage:
28
29 In addition to the usual X calls to select a visual, create a colormap
30 and create a window, you must do the following to use the X/Mesa interface:
31
32 1. Call XMesaCreateVisual() to make an XMesaVisual from an XVisualInfo.
33
34 2. Call XMesaCreateContext() to create an X/Mesa rendering context, given
35 the XMesaVisual.
36
37 3. Call XMesaCreateWindowBuffer() to create an XMesaBuffer from an X window
38 and XMesaVisual.
39
40 4. Call XMesaMakeCurrent() to bind the XMesaBuffer to an XMesaContext and
41 to make the context the current one.
42
43 5. Make gl* calls to render your graphics.
44
45 6. Use XMesaSwapBuffers() when double buffering to swap front/back buffers.
46
47 7. Before the X window is destroyed, call XMesaDestroyBuffer().
48
49 8. Before exiting, call XMesaDestroyVisual and XMesaDestroyContext.
50
51 */
52
53
54
55
56 #ifndef XMESA_H
57 #define XMESA_H
58
59
60 #include "main/mtypes.h"
61 #include "state_tracker/st_api.h"
62 #include "os/os_thread.h"
63
64 #include "state_tracker/xlib_sw_winsys.h"
65
66 # include <X11/Xlib.h>
67 # include <X11/Xlibint.h>
68 # include <X11/Xutil.h>
69
70 typedef struct xmesa_display *XMesaDisplay;
71 typedef struct xmesa_buffer *XMesaBuffer;
72 typedef struct xmesa_context *XMesaContext;
73 typedef struct xmesa_visual *XMesaVisual;
74
75
76 struct xmesa_display {
77 pipe_mutex mutex;
78
79 Display *display;
80 struct pipe_screen *screen;
81 struct st_manager *smapi;
82 };
83
84
85 /*
86 * Create a new X/Mesa visual.
87 * Input: display - X11 display
88 * visinfo - an XVisualInfo pointer
89 * rgb_flag - GL_TRUE = RGB mode,
90 * GL_FALSE = color index mode
91 * alpha_flag - alpha buffer requested?
92 * db_flag - GL_TRUE = double-buffered,
93 * GL_FALSE = single buffered
94 * stereo_flag - stereo visual?
95 * ximage_flag - GL_TRUE = use an XImage for back buffer,
96 * GL_FALSE = use an off-screen pixmap for back buffer
97 * depth_size - requested bits/depth values, or zero
98 * stencil_size - requested bits/stencil values, or zero
99 * accum_red_size - requested bits/red accum values, or zero
100 * accum_green_size - requested bits/green accum values, or zero
101 * accum_blue_size - requested bits/blue accum values, or zero
102 * accum_alpha_size - requested bits/alpha accum values, or zero
103 * num_samples - number of samples/pixel if multisampling, or zero
104 * level - visual level, usually 0
105 * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
106 * Return; a new XMesaVisual or 0 if error.
107 */
108 extern XMesaVisual XMesaCreateVisual( Display *display,
109 XVisualInfo * visinfo,
110 GLboolean rgb_flag,
111 GLboolean alpha_flag,
112 GLboolean db_flag,
113 GLboolean stereo_flag,
114 GLboolean ximage_flag,
115 GLint depth_size,
116 GLint stencil_size,
117 GLint accum_red_size,
118 GLint accum_green_size,
119 GLint accum_blue_size,
120 GLint accum_alpha_size,
121 GLint num_samples,
122 GLint level,
123 GLint visualCaveat );
124
125 /*
126 * Destroy an XMesaVisual, but not the associated XVisualInfo.
127 */
128 extern void XMesaDestroyVisual( XMesaVisual v );
129
130
131
132 /*
133 * Create a new XMesaContext for rendering into an X11 window.
134 *
135 * Input: visual - an XMesaVisual
136 * share_list - another XMesaContext with which to share display
137 * lists or NULL if no sharing is wanted.
138 * Return: an XMesaContext or NULL if error.
139 */
140 extern XMesaContext XMesaCreateContext( XMesaVisual v,
141 XMesaContext share_list );
142
143
144 /*
145 * Destroy a rendering context as returned by XMesaCreateContext()
146 */
147 extern void XMesaDestroyContext( XMesaContext c );
148
149
150
151 /*
152 * Create an XMesaBuffer from an X window.
153 */
154 extern XMesaBuffer XMesaCreateWindowBuffer( XMesaVisual v, Window w );
155
156
157 /*
158 * Create an XMesaBuffer from an X pixmap.
159 */
160 extern XMesaBuffer XMesaCreatePixmapBuffer( XMesaVisual v,
161 Pixmap p,
162 Colormap cmap );
163
164
165 /*
166 * Destroy an XMesaBuffer, but not the corresponding window or pixmap.
167 */
168 extern void XMesaDestroyBuffer( XMesaBuffer b );
169
170
171 /*
172 * Return the XMesaBuffer handle which corresponds to an X drawable, if any.
173 *
174 * New in Mesa 2.3.
175 */
176 extern XMesaBuffer XMesaFindBuffer( Display *dpy,
177 Drawable d );
178
179
180
181 /*
182 * Bind two buffers (read and draw) to a context and make the
183 * context the current one.
184 * New in Mesa 3.3
185 */
186 extern GLboolean XMesaMakeCurrent2( XMesaContext c,
187 XMesaBuffer drawBuffer,
188 XMesaBuffer readBuffer );
189
190
191 /*
192 * Unbind the current context from its buffer.
193 */
194 extern GLboolean XMesaUnbindContext( XMesaContext c );
195
196
197 /*
198 * Return a handle to the current context.
199 */
200 extern XMesaContext XMesaGetCurrentContext( void );
201
202
203 /*
204 * Swap the front and back buffers for the given buffer. No action is
205 * taken if the buffer is not double buffered.
206 */
207 extern void XMesaSwapBuffers( XMesaBuffer b );
208
209
210 /*
211 * Copy a sub-region of the back buffer to the front buffer.
212 *
213 * New in Mesa 2.6
214 */
215 extern void XMesaCopySubBuffer( XMesaBuffer b,
216 int x,
217 int y,
218 int width,
219 int height );
220
221
222
223
224
225 /*
226 * Flush/sync a context
227 */
228 extern void XMesaFlush( XMesaContext c );
229
230
231
232 /*
233 * Scan for XMesaBuffers whose window/pixmap has been destroyed, then free
234 * any memory used by that buffer.
235 *
236 * New in Mesa 2.3.
237 */
238 extern void XMesaGarbageCollect( void );
239
240
241
242 /*
243 * Create a pbuffer.
244 * New in Mesa 4.1
245 */
246 extern XMesaBuffer XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
247 unsigned int width, unsigned int height);
248
249
250
251 /*
252 * Texture from Pixmap
253 * New in Mesa 7.1
254 */
255 extern void
256 XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
257 const int *attrib_list);
258
259 extern void
260 XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer);
261
262
263 extern XMesaBuffer
264 XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
265 Colormap cmap,
266 int format, int target, int mipmap);
267
268
269 extern void
270 XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask);
271
272
273 /***********************************************************************
274 */
275
276 /**
277 * Visual inforation, derived from GLvisual.
278 * Basically corresponds to an XVisualInfo.
279 */
280 struct xmesa_visual {
281 GLvisual mesa_visual; /* Device independent visual parameters */
282 Display *display; /* The X11 display */
283 XVisualInfo * visinfo; /* X's visual info (pointer to private copy) */
284 XVisualInfo *vishandle; /* Only used in fakeglx.c */
285 GLint BitsPerPixel; /* True bits per pixel for XImages */
286
287 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
288
289 struct st_visual stvis;
290 };
291
292
293 /**
294 * Context info, derived from st_context.
295 * Basically corresponds to a GLXContext.
296 */
297 struct xmesa_context {
298 struct st_context_iface *st;
299 XMesaVisual xm_visual; /** pixel format info */
300 XMesaBuffer xm_buffer; /** current drawbuffer */
301 XMesaBuffer xm_read_buffer; /** current readbuffer */
302 };
303
304
305 /**
306 * Types of X/GLX drawables we might render into.
307 */
308 typedef enum {
309 WINDOW, /* An X window */
310 GLXWINDOW, /* GLX window */
311 PIXMAP, /* GLX pixmap */
312 PBUFFER /* GLX Pbuffer */
313 } BufferType;
314
315
316 /**
317 * Framebuffer information, derived from.
318 * Basically corresponds to a GLXDrawable.
319 */
320 struct xmesa_buffer {
321 struct st_framebuffer_iface *stfb;
322 struct xlib_drawable ws;
323
324 GLboolean wasCurrent; /* was ever the current buffer? */
325 XMesaVisual xm_visual; /* the X/Mesa visual */
326 Drawable drawable; /* Usually the X window ID */
327 Colormap cmap; /* the X colormap */
328 BufferType type; /* window, pixmap, pbuffer or glxwindow */
329
330 GLboolean largestPbuffer; /**< for pbuffers */
331 GLboolean preservedContents; /**< for pbuffers */
332
333 XImage *tempImage;
334 unsigned long selectedEvents;/* for pbuffers only */
335
336
337 GC gc; /* scratch GC for span, line, tri drawing */
338
339 /* GLX_EXT_texture_from_pixmap */
340 GLint TextureTarget; /** GLX_TEXTURE_1D_EXT, for example */
341 GLint TextureFormat; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
342 GLint TextureMipmap; /** 0 or 1 */
343
344 struct xmesa_buffer *Next; /* Linked list pointer: */
345
346 unsigned width, height;
347 };
348
349
350
351 extern void
352 xmesa_init(Display *dpy);
353
354 extern XMesaBuffer
355 xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis);
356
357 extern void
358 xmesa_get_window_size(Display *dpy, XMesaBuffer b,
359 GLuint *width, GLuint *height);
360
361 extern void
362 xmesa_check_buffer_size(XMesaBuffer b);
363
364 extern void
365 xmesa_destroy_buffers_on_display(Display *dpy);
366
367 static INLINE GLuint
368 xmesa_buffer_width(XMesaBuffer b)
369 {
370 return b->width;
371 }
372
373 static INLINE GLuint
374 xmesa_buffer_height(XMesaBuffer b)
375 {
376 return b->height;
377 }
378
379
380
381 #endif