xm_api.h revision 1675d05f911fbd569efb5248674aa71cb755c75b
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
29In addition to the usual X calls to select a visual, create a colormap
30and create a window, you must do the following to use the X/Mesa interface:
31
321. Call XMesaCreateVisual() to make an XMesaVisual from an XVisualInfo.
33
342. Call XMesaCreateContext() to create an X/Mesa rendering context, given
35   the XMesaVisual.
36
373. Call XMesaCreateWindowBuffer() to create an XMesaBuffer from an X window
38   and XMesaVisual.
39
404. Call XMesaMakeCurrent() to bind the XMesaBuffer to an XMesaContext and
41   to make the context the current one.
42
435. Make gl* calls to render your graphics.
44
456. Use XMesaSwapBuffers() when double buffering to swap front/back buffers.
46
477. Before the X window is destroyed, call XMesaDestroyBuffer().
48
498. 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_context.h"
62#include "state_tracker/st_public.h"
63#include "os/os_thread.h"
64
65#include "state_tracker/xlib_sw_winsys.h"
66
67# include <X11/Xlib.h>
68# include <X11/Xlibint.h>
69# include <X11/Xutil.h>
70
71typedef struct xmesa_buffer *XMesaBuffer;
72typedef struct xmesa_context *XMesaContext;
73typedef struct xmesa_visual *XMesaVisual;
74
75
76
77/*
78 * Create a new X/Mesa visual.
79 * Input:  display - X11 display
80 *         visinfo - an XVisualInfo pointer
81 *         rgb_flag - GL_TRUE = RGB mode,
82 *                    GL_FALSE = color index mode
83 *         alpha_flag - alpha buffer requested?
84 *         db_flag - GL_TRUE = double-buffered,
85 *                   GL_FALSE = single buffered
86 *         stereo_flag - stereo visual?
87 *         ximage_flag - GL_TRUE = use an XImage for back buffer,
88 *                       GL_FALSE = use an off-screen pixmap for back buffer
89 *         depth_size - requested bits/depth values, or zero
90 *         stencil_size - requested bits/stencil values, or zero
91 *         accum_red_size - requested bits/red accum values, or zero
92 *         accum_green_size - requested bits/green accum values, or zero
93 *         accum_blue_size - requested bits/blue accum values, or zero
94 *         accum_alpha_size - requested bits/alpha accum values, or zero
95 *         num_samples - number of samples/pixel if multisampling, or zero
96 *         level - visual level, usually 0
97 *         visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
98 * Return;  a new XMesaVisual or 0 if error.
99 */
100extern XMesaVisual XMesaCreateVisual( Display *display,
101                                      XVisualInfo * visinfo,
102                                      GLboolean rgb_flag,
103                                      GLboolean alpha_flag,
104                                      GLboolean db_flag,
105                                      GLboolean stereo_flag,
106                                      GLboolean ximage_flag,
107                                      GLint depth_size,
108                                      GLint stencil_size,
109                                      GLint accum_red_size,
110                                      GLint accum_green_size,
111                                      GLint accum_blue_size,
112                                      GLint accum_alpha_size,
113                                      GLint num_samples,
114                                      GLint level,
115                                      GLint visualCaveat );
116
117/*
118 * Destroy an XMesaVisual, but not the associated XVisualInfo.
119 */
120extern void XMesaDestroyVisual( XMesaVisual v );
121
122
123
124/*
125 * Create a new XMesaContext for rendering into an X11 window.
126 *
127 * Input:  visual - an XMesaVisual
128 *         share_list - another XMesaContext with which to share display
129 *                      lists or NULL if no sharing is wanted.
130 * Return:  an XMesaContext or NULL if error.
131 */
132extern XMesaContext XMesaCreateContext( XMesaVisual v,
133					XMesaContext share_list );
134
135
136/*
137 * Destroy a rendering context as returned by XMesaCreateContext()
138 */
139extern void XMesaDestroyContext( XMesaContext c );
140
141
142
143/*
144 * Create an XMesaBuffer from an X window.
145 */
146extern XMesaBuffer XMesaCreateWindowBuffer( XMesaVisual v, Window w );
147
148
149/*
150 * Create an XMesaBuffer from an X pixmap.
151 */
152extern XMesaBuffer XMesaCreatePixmapBuffer( XMesaVisual v,
153					    Pixmap p,
154					    Colormap cmap );
155
156
157/*
158 * Destroy an XMesaBuffer, but not the corresponding window or pixmap.
159 */
160extern void XMesaDestroyBuffer( XMesaBuffer b );
161
162
163/*
164 * Return the XMesaBuffer handle which corresponds to an X drawable, if any.
165 *
166 * New in Mesa 2.3.
167 */
168extern XMesaBuffer XMesaFindBuffer( Display *dpy,
169				    Drawable d );
170
171
172
173/*
174 * Bind two buffers (read and draw) to a context and make the
175 * context the current one.
176 * New in Mesa 3.3
177 */
178extern GLboolean XMesaMakeCurrent2( XMesaContext c,
179                                    XMesaBuffer drawBuffer,
180                                    XMesaBuffer readBuffer );
181
182
183/*
184 * Unbind the current context from its buffer.
185 */
186extern GLboolean XMesaUnbindContext( XMesaContext c );
187
188
189/*
190 * Return a handle to the current context.
191 */
192extern XMesaContext XMesaGetCurrentContext( void );
193
194
195/*
196 * Swap the front and back buffers for the given buffer.  No action is
197 * taken if the buffer is not double buffered.
198 */
199extern void XMesaSwapBuffers( XMesaBuffer b );
200
201
202/*
203 * Copy a sub-region of the back buffer to the front buffer.
204 *
205 * New in Mesa 2.6
206 */
207extern void XMesaCopySubBuffer( XMesaBuffer b,
208				int x,
209				int y,
210				int width,
211				int height );
212
213
214
215
216
217/*
218 * Flush/sync a context
219 */
220extern void XMesaFlush( XMesaContext c );
221
222
223
224/*
225 * Scan for XMesaBuffers whose window/pixmap has been destroyed, then free
226 * any memory used by that buffer.
227 *
228 * New in Mesa 2.3.
229 */
230extern void XMesaGarbageCollect( void );
231
232
233
234/*
235 * Create a pbuffer.
236 * New in Mesa 4.1
237 */
238extern XMesaBuffer XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
239                                      unsigned int width, unsigned int height);
240
241
242
243/*
244 * Texture from Pixmap
245 * New in Mesa 7.1
246 */
247extern void
248XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
249                  const int *attrib_list);
250
251extern void
252XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer);
253
254
255extern XMesaBuffer
256XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
257                               Colormap cmap,
258                               int format, int target, int mipmap);
259
260
261
262
263/***********************************************************************
264 */
265
266extern pipe_mutex _xmesa_lock;
267
268extern struct xmesa_buffer *XMesaBufferList;
269
270
271/**
272 * Visual inforation, derived from GLvisual.
273 * Basically corresponds to an XVisualInfo.
274 */
275struct xmesa_visual {
276   GLvisual mesa_visual;	/* Device independent visual parameters */
277   Display *display;	/* The X11 display */
278   XVisualInfo * visinfo;	/* X's visual info (pointer to private copy) */
279   XVisualInfo *vishandle;	/* Only used in fakeglx.c */
280   GLint BitsPerPixel;		/* True bits per pixel for XImages */
281
282   GLboolean ximage_flag;	/* Use XImage for back buffer (not pixmap)? */
283};
284
285
286/**
287 * Context info, derived from st_context.
288 * Basically corresponds to a GLXContext.
289 */
290struct xmesa_context {
291   struct st_context *st;
292   XMesaVisual xm_visual;	/** pixel format info */
293   XMesaBuffer xm_buffer;	/** current drawbuffer */
294   XMesaBuffer xm_read_buffer;  /** current readbuffer */
295};
296
297
298/**
299 * Types of X/GLX drawables we might render into.
300 */
301typedef enum {
302   WINDOW,          /* An X window */
303   GLXWINDOW,       /* GLX window */
304   PIXMAP,          /* GLX pixmap */
305   PBUFFER          /* GLX Pbuffer */
306} BufferType;
307
308
309/**
310 * Framebuffer information, derived from.
311 * Basically corresponds to a GLXDrawable.
312 */
313struct xmesa_buffer {
314   struct st_framebuffer *stfb;
315   struct xlib_drawable ws;
316
317   GLboolean wasCurrent;	/* was ever the current buffer? */
318   XMesaVisual xm_visual;	/* the X/Mesa visual */
319   Drawable drawable;	/* Usually the X window ID */
320   Colormap cmap;		/* the X colormap */
321   BufferType type;             /* window, pixmap, pbuffer or glxwindow */
322
323   GLboolean largestPbuffer;    /**< for pbuffers */
324   GLboolean preservedContents; /**< for pbuffers */
325
326   XImage *tempImage;
327   unsigned long selectedEvents;/* for pbuffers only */
328
329
330   GC gc;			/* scratch GC for span, line, tri drawing */
331
332   /* GLX_EXT_texture_from_pixmap */
333   GLint TextureTarget; /** GLX_TEXTURE_1D_EXT, for example */
334   GLint TextureFormat; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
335   GLint TextureMipmap; /** 0 or 1 */
336
337   struct xmesa_buffer *Next;	/* Linked list pointer: */
338};
339
340
341
342/** cast wrapper */
343static INLINE XMesaContext
344xmesa_context(GLcontext *ctx)
345{
346   return (XMesaContext) ctx->DriverCtx;
347}
348
349
350/** cast wrapper */
351static INLINE XMesaBuffer
352xmesa_buffer(GLframebuffer *fb)
353{
354   struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
355   return (XMesaBuffer) st_framebuffer_private(stfb);
356}
357
358
359extern void
360xmesa_init(Display *dpy);
361
362extern void
363xmesa_delete_framebuffer(struct gl_framebuffer *fb);
364
365extern XMesaBuffer
366xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis);
367
368extern void
369xmesa_get_window_size(Display *dpy, XMesaBuffer b,
370                      GLuint *width, GLuint *height);
371
372extern void
373xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer);
374
375extern void
376xmesa_destroy_buffers_on_display(Display *dpy);
377
378static INLINE GLuint
379xmesa_buffer_width(XMesaBuffer b)
380{
381   return b->stfb->Base.Width;
382}
383
384static INLINE GLuint
385xmesa_buffer_height(XMesaBuffer b)
386{
387   return b->stfb->Base.Height;
388}
389
390
391
392#endif
393