2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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:
32 1. Call XMesaCreateVisual() to make an XMesaVisual from an XVisualInfo.
34 2. Call XMesaCreateContext() to create an X/Mesa rendering context, given
37 3. Call XMesaCreateWindowBuffer() to create an XMesaBuffer from an X window
40 4. Call XMesaMakeCurrent() to bind the XMesaBuffer to an XMesaContext and
41 to make the context the current one.
43 5. Make gl* calls to render your graphics.
45 6. Use XMesaSwapBuffers() when double buffering to swap front/back buffers.
47 7. Before the X window is destroyed, call XMesaDestroyBuffer().
49 8. Before exiting, call XMesaDestroyVisual and XMesaDestroyContext.
61 #include "state_tracker/st_context.h"
62 #include "state_tracker/st_public.h"
63 #include "pipe/p_thread.h"
66 # include <X11/Xlib.h>
67 # include <X11/Xlibint.h>
68 # include <X11/Xutil.h>
69 # ifdef USE_XSHM /* was SHM */
72 # include <X11/extensions/XShm.h>
75 typedef struct xmesa_buffer
*XMesaBuffer
;
76 typedef struct xmesa_context
*XMesaContext
;
77 typedef struct xmesa_visual
*XMesaVisual
;
82 * Create a new X/Mesa visual.
83 * Input: display - X11 display
84 * visinfo - an XVisualInfo pointer
85 * rgb_flag - GL_TRUE = RGB mode,
86 * GL_FALSE = color index mode
87 * alpha_flag - alpha buffer requested?
88 * db_flag - GL_TRUE = double-buffered,
89 * GL_FALSE = single buffered
90 * stereo_flag - stereo visual?
91 * ximage_flag - GL_TRUE = use an XImage for back buffer,
92 * GL_FALSE = use an off-screen pixmap for back buffer
93 * depth_size - requested bits/depth values, or zero
94 * stencil_size - requested bits/stencil values, or zero
95 * accum_red_size - requested bits/red accum values, or zero
96 * accum_green_size - requested bits/green accum values, or zero
97 * accum_blue_size - requested bits/blue accum values, or zero
98 * accum_alpha_size - requested bits/alpha accum values, or zero
99 * num_samples - number of samples/pixel if multisampling, or zero
100 * level - visual level, usually 0
101 * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
102 * Return; a new XMesaVisual or 0 if error.
104 extern XMesaVisual
XMesaCreateVisual( Display
*display
,
105 XVisualInfo
* visinfo
,
107 GLboolean alpha_flag
,
109 GLboolean stereo_flag
,
110 GLboolean ximage_flag
,
113 GLint accum_red_size
,
114 GLint accum_green_size
,
115 GLint accum_blue_size
,
116 GLint accum_alpha_size
,
119 GLint visualCaveat
);
122 * Destroy an XMesaVisual, but not the associated XVisualInfo.
124 extern void XMesaDestroyVisual( XMesaVisual v
);
129 * Create a new XMesaContext for rendering into an X11 window.
131 * Input: visual - an XMesaVisual
132 * share_list - another XMesaContext with which to share display
133 * lists or NULL if no sharing is wanted.
134 * Return: an XMesaContext or NULL if error.
136 extern XMesaContext
XMesaCreateContext( XMesaVisual v
,
137 XMesaContext share_list
);
141 * Destroy a rendering context as returned by XMesaCreateContext()
143 extern void XMesaDestroyContext( XMesaContext c
);
148 * Create an XMesaBuffer from an X window.
150 extern XMesaBuffer
XMesaCreateWindowBuffer( XMesaVisual v
, Window w
);
154 * Create an XMesaBuffer from an X pixmap.
156 extern XMesaBuffer
XMesaCreatePixmapBuffer( XMesaVisual v
,
162 * Destroy an XMesaBuffer, but not the corresponding window or pixmap.
164 extern void XMesaDestroyBuffer( XMesaBuffer b
);
168 * Return the XMesaBuffer handle which corresponds to an X drawable, if any.
172 extern XMesaBuffer
XMesaFindBuffer( Display
*dpy
,
178 * Bind two buffers (read and draw) to a context and make the
179 * context the current one.
182 extern GLboolean
XMesaMakeCurrent2( XMesaContext c
,
183 XMesaBuffer drawBuffer
,
184 XMesaBuffer readBuffer
);
188 * Unbind the current context from its buffer.
190 extern GLboolean
XMesaUnbindContext( XMesaContext c
);
194 * Return a handle to the current context.
196 extern XMesaContext
XMesaGetCurrentContext( void );
200 * Swap the front and back buffers for the given buffer. No action is
201 * taken if the buffer is not double buffered.
203 extern void XMesaSwapBuffers( XMesaBuffer b
);
207 * Copy a sub-region of the back buffer to the front buffer.
211 extern void XMesaCopySubBuffer( XMesaBuffer b
,
222 * Flush/sync a context
224 extern void XMesaFlush( XMesaContext c
);
229 * Scan for XMesaBuffers whose window/pixmap has been destroyed, then free
230 * any memory used by that buffer.
234 extern void XMesaGarbageCollect( void );
242 extern XMesaBuffer
XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
243 unsigned int width
, unsigned int height
);
248 * Texture from Pixmap
252 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
253 const int *attrib_list
);
256 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
);
260 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
262 int format
, int target
, int mipmap
);
267 /***********************************************************************
270 extern pipe_mutex _xmesa_lock
;
272 extern struct xmesa_buffer
*XMesaBufferList
;
276 * Visual inforation, derived from GLvisual.
277 * Basically corresponds to an XVisualInfo.
279 struct xmesa_visual
{
280 GLvisual mesa_visual
; /* Device independent visual parameters */
281 Display
*display
; /* The X11 display */
282 XVisualInfo
* visinfo
; /* X's visual info (pointer to private copy) */
283 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
284 GLint BitsPerPixel
; /* True bits per pixel for XImages */
286 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
291 * Context info, derived from st_context.
292 * Basically corresponds to a GLXContext.
294 struct xmesa_context
{
295 struct st_context
*st
;
296 XMesaVisual xm_visual
; /** pixel format info */
297 XMesaBuffer xm_buffer
; /** current drawbuffer */
298 XMesaBuffer xm_read_buffer
; /** current readbuffer */
303 * Types of X/GLX drawables we might render into.
306 WINDOW
, /* An X window */
307 GLXWINDOW
, /* GLX window */
308 PIXMAP
, /* GLX pixmap */
309 PBUFFER
/* GLX Pbuffer */
314 * Framebuffer information, derived from.
315 * Basically corresponds to a GLXDrawable.
317 struct xmesa_buffer
{
318 struct st_framebuffer
*stfb
;
320 GLboolean wasCurrent
; /* was ever the current buffer? */
321 XMesaVisual xm_visual
; /* the X/Mesa visual */
322 Drawable drawable
; /* Usually the X window ID */
323 Colormap cmap
; /* the X colormap */
324 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
327 unsigned long selectedEvents
;/* for pbuffers only */
329 GLuint shm
; /* X Shared Memory extension status: */
330 /* 0 = not available */
331 /* 1 = XImage support available */
332 /* 2 = Pixmap support available too */
333 #if defined(USE_XSHM)
334 XShmSegmentInfo shminfo
;
337 GC gc
; /* scratch GC for span, line, tri drawing */
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 */
344 struct xmesa_buffer
*Next
; /* Linked list pointer: */
350 static INLINE XMesaContext
351 xmesa_context(GLcontext
*ctx
)
353 return (XMesaContext
) ctx
->DriverCtx
;
358 static INLINE XMesaBuffer
359 xmesa_buffer(GLframebuffer
*fb
)
361 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
362 return (XMesaBuffer
) st_framebuffer_private(stfb
);
367 xmesa_delete_framebuffer(struct gl_framebuffer
*fb
);
370 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
);
373 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
);
376 xmesa_destroy_buffers_on_display(Display
*dpy
);
379 xmesa_buffer_width(XMesaBuffer b
)
381 return b
->stfb
->Base
.Width
;
385 xmesa_buffer_height(XMesaBuffer b
)
387 return b
->stfb
->Base
.Height
;
391 xmesa_check_for_xshm(Display
*display
);