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