xlib: do not cache return value of glXChooseVisual/glXGetVisualFromFBConfig
[mesa.git] / src / mesa / drivers / x11 / xmesaP.h
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 #ifndef XMESAP_H
27 #define XMESAP_H
28
29
30 #include "c11/threads.h"
31 #include "xmesa.h"
32 #include "main/mtypes.h"
33 #include "swrast/s_context.h"
34
35
36 extern mtx_t _xmesa_lock;
37
38 extern XMesaBuffer XMesaBufferList;
39
40 /* for PF_8R8G8B24 pixel format */
41 typedef struct {
42 GLubyte b;
43 GLubyte g;
44 GLubyte r;
45 } bgr_t;
46
47
48 struct xmesa_renderbuffer;
49
50
51 /* Function pointer for clearing color buffers */
52 typedef void (*ClearFunc)( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
53 GLint x, GLint y, GLint width, GLint height );
54
55
56
57
58 /** Framebuffer pixel formats */
59 enum pixel_format {
60 PF_Truecolor, /**< TrueColor or DirectColor, any depth */
61 PF_Dither_True, /**< TrueColor with dithering */
62 PF_8A8R8G8B, /**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B bits */
63 PF_8A8B8G8R, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R bits */
64 PF_8R8G8B, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
65 PF_8R8G8B24, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
66 PF_5R6G5B, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
67 PF_Dither_5R6G5B /**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
68 };
69
70
71 /**
72 * Visual inforation, derived from struct gl_config.
73 * Basically corresponds to an XVisualInfo.
74 */
75 struct xmesa_visual {
76 struct gl_config mesa_visual; /* Device independent visual parameters */
77 XMesaDisplay *display; /* The X11 display */
78 int screen, visualID;
79 int visualType;
80 XMesaVisualInfo visinfo; /* X's visual info (pointer to private copy) */
81 XVisualInfo *vishandle; /* Only used in fakeglx.c */
82 GLint BitsPerPixel; /* True bits per pixel for XImages */
83
84 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
85
86 enum pixel_format dithered_pf; /* Pixel format when dithering */
87 enum pixel_format undithered_pf;/* Pixel format when not dithering */
88
89 GLfloat RedGamma; /* Gamma values, 1.0 is default */
90 GLfloat GreenGamma;
91 GLfloat BlueGamma;
92
93 /* For PF_TRUECOLOR */
94 GLint rshift, gshift, bshift;/* Pixel color component shifts */
95 GLubyte Kernel[16]; /* Dither kernel */
96 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
97 unsigned long GtoPixel[512];
98 unsigned long BtoPixel[512];
99 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
100 GLubyte PixelToG[256];
101 GLubyte PixelToB[256];
102 };
103
104
105 /**
106 * Context info, derived from struct gl_context.
107 * Basically corresponds to a GLXContext.
108 */
109 struct xmesa_context {
110 struct gl_context mesa; /* the core library context (containment) */
111 XMesaVisual xm_visual; /* Describes the buffers */
112 XMesaBuffer xm_buffer; /* current span/point/line/triangle buffer */
113
114 XMesaDisplay *display; /* == xm_visual->display */
115 GLboolean swapbytes; /* Host byte order != display byte order? */
116 GLboolean direct; /* Direct rendering context? */
117
118 enum pixel_format pixelformat;
119
120 GLubyte clearcolor[4]; /* current clearing color */
121 unsigned long clearpixel; /* current clearing pixel value */
122 };
123
124
125 /**
126 * Types of X/GLX drawables we might render into.
127 */
128 typedef enum {
129 WINDOW, /* An X window */
130 GLXWINDOW, /* GLX window */
131 PIXMAP, /* GLX pixmap */
132 PBUFFER /* GLX Pbuffer */
133 } BufferType;
134
135
136 /** Values for db_mode: */
137 /*@{*/
138 #define BACK_PIXMAP 1
139 #define BACK_XIMAGE 2
140 /*@}*/
141
142
143 /**
144 * An xmesa_renderbuffer represents the back or front color buffer.
145 * For the front color buffer:
146 * <drawable> is the X window
147 * For the back color buffer:
148 * Either <ximage> or <pixmap> will be used, never both.
149 * In any case, <drawable> always equals <pixmap>.
150 * For stand-alone Mesa, we could merge <drawable> and <pixmap> into one
151 * field. We don't do that for the server-side GLcore module because
152 * pixmaps and drawables are different and we'd need a bunch of casts.
153 */
154 struct xmesa_renderbuffer
155 {
156 struct swrast_renderbuffer Base; /* Base class */
157
158 XMesaBuffer Parent; /**< The XMesaBuffer this renderbuffer belongs to */
159 XMesaDrawable drawable; /* Usually the X window ID */
160 XMesaPixmap pixmap; /* Back color buffer */
161 XMesaImage *ximage; /* The back buffer, if not using a Pixmap */
162
163 GLushort *origin2; /* used for PIXEL_ADDR2 macro */
164 GLint width2;
165 GLubyte *origin3; /* used for PIXEL_ADDR3 macro */
166 GLint width3;
167 GLuint *origin4; /* used for PIXEL_ADDR4 macro */
168 GLint width4;
169
170 GLint bottom; /* used for FLIP macro, equals height - 1 */
171
172 ClearFunc clearFunc;
173
174 GLuint map_x, map_y, map_w, map_h;
175 GLbitfield map_mode;
176 XMesaImage *map_ximage;
177 };
178
179
180 /**
181 * Framebuffer information, derived from.
182 * Basically corresponds to a GLXDrawable.
183 */
184 struct xmesa_buffer {
185 struct gl_framebuffer mesa_buffer; /* depth, stencil, accum, etc buffers */
186 /* This MUST BE FIRST! */
187 GLboolean wasCurrent; /* was ever the current buffer? */
188 XMesaVisual xm_visual; /* the X/Mesa visual */
189
190 XMesaDisplay *display;
191 BufferType type; /* window, pixmap, pbuffer or glxwindow */
192
193 GLboolean largestPbuffer; /**< for pbuffers */
194 GLboolean preservedContents; /**< for pbuffers */
195
196 struct xmesa_renderbuffer *frontxrb; /* front color renderbuffer */
197 struct xmesa_renderbuffer *backxrb; /* back color renderbuffer */
198
199 XMesaColormap cmap; /* the X colormap */
200
201 unsigned long selectedEvents;/* for pbuffers only */
202
203 GLint db_mode; /* 0 = single buffered */
204 /* BACK_PIXMAP = use Pixmap for back buffer */
205 /* BACK_XIMAGE = use XImage for back buffer */
206 GLuint shm; /* X Shared Memory extension status: */
207 /* 0 = not available */
208 /* 1 = XImage support available */
209 /* 2 = Pixmap support available too */
210 #if defined(USE_XSHM)
211 XShmSegmentInfo shminfo;
212 #endif
213
214 // XMesaImage *rowimage; /* Used for optimized span writing */
215 XMesaPixmap stipple_pixmap; /* For polygon stippling */
216 XMesaGC stipple_gc; /* For polygon stippling */
217
218 XMesaGC gc; /* scratch GC for span, line, tri drawing */
219 XMesaGC cleargc; /* GC for clearing the color buffer */
220 XMesaGC swapgc; /* GC for swapping the color buffers */
221
222 /* The following are here instead of in the XMesaVisual
223 * because they depend on the window's colormap.
224 */
225
226 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
227 unsigned long color_table[576]; /* RGB -> pixel value */
228
229 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
230 GLubyte pixel_to_r[65536]; /* pixel value -> red */
231 GLubyte pixel_to_g[65536]; /* pixel value -> green */
232 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
233
234 /* Used to do XAllocColor/XFreeColors accounting: */
235 int num_alloced;
236 unsigned long alloced_colors[256];
237
238 /* GLX_EXT_texture_from_pixmap */
239 GLint TextureTarget; /** GLX_TEXTURE_1D_EXT, for example */
240 GLint TextureFormat; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
241 GLint TextureMipmap; /** 0 or 1 */
242
243 struct xmesa_buffer *Next; /* Linked list pointer: */
244 };
245
246
247 /**
248 * If pixelformat==PF_TRUECOLOR:
249 */
250 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
251 PIXEL = xmesa->xm_visual->RtoPixel[R] \
252 | xmesa->xm_visual->GtoPixel[G] \
253 | xmesa->xm_visual->BtoPixel[B]; \
254
255
256 /**
257 * If pixelformat==PF_TRUEDITHER:
258 */
259 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
260 { \
261 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
262 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
263 | xmesa->xm_visual->GtoPixel[(G)+d] \
264 | xmesa->xm_visual->BtoPixel[(B)+d]; \
265 }
266
267
268
269 /**
270 * If pixelformat==PF_8A8B8G8R:
271 */
272 #define PACK_8A8B8G8R( R, G, B, A ) \
273 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
274
275
276 /**
277 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
278 * shortcut.
279 */
280 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
281
282
283
284 /**
285 * If pixelformat==PF_8R8G8B:
286 */
287 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
288
289
290 /**
291 * If pixelformat==PF_5R6G5B:
292 */
293 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
294
295
296 /**
297 * If pixelformat==PF_8A8R8G8B:
298 */
299 #define PACK_8A8R8G8B( R, G, B, A ) \
300 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
301
302
303
304
305 /**
306 * Converts a GL window Y coord to an X window Y coord:
307 */
308 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
309
310
311 /**
312 * Return the address of a 2, 3 or 4-byte pixel in the buffer's XImage:
313 * X==0 is left, Y==0 is bottom.
314 */
315 #define PIXEL_ADDR2(XRB, X, Y) \
316 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
317
318 #define PIXEL_ADDR3(XRB, X, Y) \
319 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
320
321 #define PIXEL_ADDR4(XRB, X, Y) \
322 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
323
324
325
326 /*
327 * External functions:
328 */
329
330 extern struct xmesa_renderbuffer *
331 xmesa_new_renderbuffer(struct gl_context *ctx, GLuint name,
332 const struct xmesa_visual *xmvis,
333 GLboolean backBuffer);
334
335 extern void
336 xmesa_delete_framebuffer(struct gl_framebuffer *fb);
337
338 extern XMesaBuffer
339 xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis);
340
341 extern unsigned long
342 xmesa_color_to_pixel( struct gl_context *ctx,
343 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
344 GLuint pixelFormat );
345
346 extern void
347 xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
348 GLuint *width, GLuint *height);
349
350 extern void
351 xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer);
352
353 extern void
354 xmesa_init_driver_functions( XMesaVisual xmvisual,
355 struct dd_function_table *driver );
356
357 extern void
358 xmesa_update_state( struct gl_context *ctx, GLbitfield new_state );
359
360
361 extern void
362 xmesa_MapRenderbuffer(struct gl_context *ctx,
363 struct gl_renderbuffer *rb,
364 GLuint x, GLuint y, GLuint w, GLuint h,
365 GLbitfield mode,
366 GLubyte **mapOut, GLint *rowStrideOut);
367
368 extern void
369 xmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb);
370
371 extern void
372 xmesa_destroy_buffers_on_display(XMesaDisplay *dpy);
373
374
375 /**
376 * Using a function instead of an ordinary cast is safer.
377 */
378 static inline struct xmesa_renderbuffer *
379 xmesa_renderbuffer(struct gl_renderbuffer *rb)
380 {
381 return (struct xmesa_renderbuffer *) rb;
382 }
383
384
385 /**
386 * Return pointer to XMesaContext corresponding to a Mesa struct gl_context.
387 * Since we're using structure containment, it's just a cast!.
388 */
389 static inline XMesaContext
390 XMESA_CONTEXT(struct gl_context *ctx)
391 {
392 return (XMesaContext) ctx;
393 }
394
395
396 /**
397 * Return pointer to XMesaBuffer corresponding to a Mesa struct gl_framebuffer.
398 * Since we're using structure containment, it's just a cast!.
399 */
400 static inline XMesaBuffer
401 XMESA_BUFFER(struct gl_framebuffer *b)
402 {
403 return (XMesaBuffer) b;
404 }
405
406
407 /* Plugged into the software rasterizer. Try to use internal
408 * swrast-style point, line and triangle functions.
409 */
410 extern void xmesa_choose_point( struct gl_context *ctx );
411 extern void xmesa_choose_line( struct gl_context *ctx );
412 extern void xmesa_choose_triangle( struct gl_context *ctx );
413
414
415 extern void xmesa_register_swrast_functions( struct gl_context *ctx );
416
417
418
419 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
420 #define ENABLE_EXT_timer_query 1 /* should have 64-bit GLuint64EXT */
421 #else
422 #define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
423 #endif
424
425
426 #define TEST_META_FUNCS 0
427
428
429 #endif