29f070aa040f5f0eeed3986f8a603b0285961e5a
[mesa.git] / src / mesa / drivers / x11 / xmesaP.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 #ifndef XMESAP_H
27 #define XMESAP_H
28
29
30 #include "xmesa.h"
31 #include "main/mtypes.h"
32
33
34 extern _glthread_Mutex _xmesa_lock;
35
36 extern XMesaBuffer XMesaBufferList;
37
38 /* for PF_8R8G8B24 pixel format */
39 typedef struct {
40 GLubyte b;
41 GLubyte g;
42 GLubyte r;
43 } bgr_t;
44
45
46 struct xmesa_renderbuffer;
47
48
49 /* Function pointer for clearing color buffers */
50 typedef void (*ClearFunc)( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
51 GLint x, GLint y, GLint width, GLint height );
52
53
54
55
56 /** Framebuffer pixel formats */
57 enum pixel_format {
58 PF_Index, /**< Color Index mode */
59 PF_Truecolor, /**< TrueColor or DirectColor, any depth */
60 PF_Dither_True, /**< TrueColor with dithering */
61 PF_8A8R8G8B, /**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B bits */
62 PF_8A8B8G8R, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R bits */
63 PF_8R8G8B, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
64 PF_8R8G8B24, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
65 PF_5R6G5B, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
66 PF_Dither, /**< Color-mapped RGB with dither */
67 PF_Lookup, /**< Color-mapped RGB without dither */
68 PF_HPCR, /**< HP Color Recovery (ad@lms.be 30/08/95) */
69 PF_1Bit, /**< monochrome dithering of RGB */
70 PF_Grayscale, /**< Grayscale or StaticGray */
71 PF_Dither_5R6G5B /**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
72 };
73
74
75 /**
76 * Visual inforation, derived from struct gl_config.
77 * Basically corresponds to an XVisualInfo.
78 */
79 struct xmesa_visual {
80 struct gl_config mesa_visual; /* Device independent visual parameters */
81 XMesaDisplay *display; /* The X11 display */
82 int screen, visualID;
83 int visualType;
84 XMesaVisualInfo visinfo; /* X's visual info (pointer to private copy) */
85 XVisualInfo *vishandle; /* Only used in fakeglx.c */
86 GLint BitsPerPixel; /* True bits per pixel for XImages */
87
88 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
89
90 enum pixel_format dithered_pf; /* Pixel format when dithering */
91 enum pixel_format undithered_pf;/* Pixel format when not dithering */
92
93 GLfloat RedGamma; /* Gamma values, 1.0 is default */
94 GLfloat GreenGamma;
95 GLfloat BlueGamma;
96
97 /* For PF_TRUECOLOR */
98 GLint rshift, gshift, bshift;/* Pixel color component shifts */
99 GLubyte Kernel[16]; /* Dither kernel */
100 unsigned long RtoPixel[512]; /* RGB to pixel conversion */
101 unsigned long GtoPixel[512];
102 unsigned long BtoPixel[512];
103 GLubyte PixelToR[256]; /* Pixel to RGB conversion */
104 GLubyte PixelToG[256];
105 GLubyte PixelToB[256];
106
107 /* For PF_HPCR */
108 short hpcr_rgbTbl[3][256];
109 GLboolean hpcr_clear_flag;
110 GLubyte hpcr_clear_ximage_pattern[2][16];
111 XMesaImage *hpcr_clear_ximage;
112 XMesaPixmap hpcr_clear_pixmap;
113
114 /* For PF_1BIT */
115 int bitFlip;
116 };
117
118
119 /**
120 * Context info, derived from struct gl_context.
121 * Basically corresponds to a GLXContext.
122 */
123 struct xmesa_context {
124 struct gl_context mesa; /* the core library context (containment) */
125 XMesaVisual xm_visual; /* Describes the buffers */
126 XMesaBuffer xm_buffer; /* current span/point/line/triangle buffer */
127
128 XMesaDisplay *display; /* == xm_visual->display */
129 GLboolean swapbytes; /* Host byte order != display byte order? */
130 GLboolean direct; /* Direct rendering context? */
131
132 enum pixel_format pixelformat;
133
134 GLubyte clearcolor[4]; /* current clearing color */
135 unsigned long clearpixel; /* current clearing pixel value */
136 };
137
138
139 /**
140 * Types of X/GLX drawables we might render into.
141 */
142 typedef enum {
143 WINDOW, /* An X window */
144 GLXWINDOW, /* GLX window */
145 PIXMAP, /* GLX pixmap */
146 PBUFFER /* GLX Pbuffer */
147 } BufferType;
148
149
150 /** Values for db_mode: */
151 /*@{*/
152 #define BACK_PIXMAP 1
153 #define BACK_XIMAGE 2
154 /*@}*/
155
156
157 /**
158 * An xmesa_renderbuffer represents the back or front color buffer.
159 * For the front color buffer:
160 * <drawable> is the X window
161 * For the back color buffer:
162 * Either <ximage> or <pixmap> will be used, never both.
163 * In any case, <drawable> always equals <pixmap>.
164 * For stand-alone Mesa, we could merge <drawable> and <pixmap> into one
165 * field. We don't do that for the server-side GLcore module because
166 * pixmaps and drawables are different and we'd need a bunch of casts.
167 */
168 struct xmesa_renderbuffer
169 {
170 struct gl_renderbuffer Base; /* Base class */
171
172 XMesaBuffer Parent; /**< The XMesaBuffer this renderbuffer belongs to */
173 XMesaDrawable drawable; /* Usually the X window ID */
174 XMesaPixmap pixmap; /* Back color buffer */
175 XMesaImage *ximage; /* The back buffer, if not using a Pixmap */
176
177 GLubyte *origin1; /* used for PIXEL_ADDR1 macro */
178 GLint width1;
179 GLushort *origin2; /* used for PIXEL_ADDR2 macro */
180 GLint width2;
181 GLubyte *origin3; /* used for PIXEL_ADDR3 macro */
182 GLint width3;
183 GLuint *origin4; /* used for PIXEL_ADDR4 macro */
184 GLint width4;
185
186 GLint bottom; /* used for FLIP macro, equals height - 1 */
187
188 ClearFunc clearFunc;
189
190 GLuint map_x, map_y, map_w, map_h;
191 GLbitfield map_mode;
192 XMesaImage *map_ximage;
193 };
194
195
196 /**
197 * Framebuffer information, derived from.
198 * Basically corresponds to a GLXDrawable.
199 */
200 struct xmesa_buffer {
201 struct gl_framebuffer mesa_buffer; /* depth, stencil, accum, etc buffers */
202 /* This MUST BE FIRST! */
203 GLboolean wasCurrent; /* was ever the current buffer? */
204 XMesaVisual xm_visual; /* the X/Mesa visual */
205
206 XMesaDisplay *display;
207 BufferType type; /* window, pixmap, pbuffer or glxwindow */
208
209 GLboolean largestPbuffer; /**< for pbuffers */
210 GLboolean preservedContents; /**< for pbuffers */
211
212 struct xmesa_renderbuffer *frontxrb; /* front color renderbuffer */
213 struct xmesa_renderbuffer *backxrb; /* back color renderbuffer */
214
215 XMesaColormap cmap; /* the X colormap */
216
217 unsigned long selectedEvents;/* for pbuffers only */
218
219 GLint db_mode; /* 0 = single buffered */
220 /* BACK_PIXMAP = use Pixmap for back buffer */
221 /* BACK_XIMAGE = use XImage for back buffer */
222 GLboolean swAlpha;
223
224 GLuint shm; /* X Shared Memory extension status: */
225 /* 0 = not available */
226 /* 1 = XImage support available */
227 /* 2 = Pixmap support available too */
228 #if defined(USE_XSHM)
229 XShmSegmentInfo shminfo;
230 #endif
231
232 XMesaImage *rowimage; /* Used for optimized span writing */
233 XMesaPixmap stipple_pixmap; /* For polygon stippling */
234 XMesaGC stipple_gc; /* For polygon stippling */
235
236 XMesaGC gc; /* scratch GC for span, line, tri drawing */
237 XMesaGC cleargc; /* GC for clearing the color buffer */
238 XMesaGC swapgc; /* GC for swapping the color buffers */
239
240 /* The following are here instead of in the XMesaVisual
241 * because they depend on the window's colormap.
242 */
243
244 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
245 unsigned long color_table[576]; /* RGB -> pixel value */
246
247 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
248 GLubyte pixel_to_r[65536]; /* pixel value -> red */
249 GLubyte pixel_to_g[65536]; /* pixel value -> green */
250 GLubyte pixel_to_b[65536]; /* pixel value -> blue */
251
252 /* Used to do XAllocColor/XFreeColors accounting: */
253 int num_alloced;
254 unsigned long alloced_colors[256];
255
256 /* GLX_EXT_texture_from_pixmap */
257 GLint TextureTarget; /** GLX_TEXTURE_1D_EXT, for example */
258 GLint TextureFormat; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
259 GLint TextureMipmap; /** 0 or 1 */
260
261 struct xmesa_buffer *Next; /* Linked list pointer: */
262 };
263
264
265 /**
266 * If pixelformat==PF_TRUECOLOR:
267 */
268 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
269 PIXEL = xmesa->xm_visual->RtoPixel[R] \
270 | xmesa->xm_visual->GtoPixel[G] \
271 | xmesa->xm_visual->BtoPixel[B]; \
272
273
274 /**
275 * If pixelformat==PF_TRUEDITHER:
276 */
277 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
278 { \
279 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
280 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
281 | xmesa->xm_visual->GtoPixel[(G)+d] \
282 | xmesa->xm_visual->BtoPixel[(B)+d]; \
283 }
284
285
286
287 /**
288 * If pixelformat==PF_8A8B8G8R:
289 */
290 #define PACK_8A8B8G8R( R, G, B, A ) \
291 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
292
293
294 /**
295 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
296 * shortcut.
297 */
298 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
299
300
301
302 /**
303 * If pixelformat==PF_8R8G8B:
304 */
305 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
306
307
308 /**
309 * If pixelformat==PF_5R6G5B:
310 */
311 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
312
313
314 /**
315 * If pixelformat==PF_8A8R8G8B:
316 */
317 #define PACK_8A8R8G8B( R, G, B, A ) \
318 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
319
320
321
322 /**
323 * If pixelformat==PF_DITHER:
324 *
325 * Improved 8-bit RGB dithering code contributed by Bob Mercier
326 * (mercier@hollywood.cinenet.net). Thanks Bob!
327 */
328 #ifdef DITHER666
329 # define DITH_R 6
330 # define DITH_G 6
331 # define DITH_B 6
332 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
333 #else
334 # define DITH_R 5
335 # define DITH_G 9
336 # define DITH_B 5
337 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
338 #endif
339 #define DITH_DX 4
340 #define DITH_DY 4
341 #define DITH_N (DITH_DX * DITH_DY)
342
343 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
344
345 #define MAXC 256
346 extern const int xmesa_kernel8[DITH_DY * DITH_DX];
347
348 /* Dither for random X,Y */
349 #define DITHER_SETUP \
350 int __d; \
351 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
352
353 #define DITHER( X, Y, R, G, B ) \
354 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
355 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
356 _dither(DITH_G, (G), __d), \
357 _dither(DITH_B, (B), __d))])
358
359 /* Dither for random X, fixed Y */
360 #define XDITHER_SETUP(Y) \
361 int __d; \
362 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
363 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
364
365 #define XDITHER( X, R, G, B ) \
366 (__d = kernel[(X)&3], \
367 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
368 _dither(DITH_G, (G), __d), \
369 _dither(DITH_B, (B), __d))])
370
371
372
373 /*
374 * Dithering for flat-shaded triangles. Precompute all 16 possible
375 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
376 */
377 #define FLAT_DITHER_SETUP( R, G, B ) \
378 GLushort ditherValues[16]; \
379 { \
380 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
381 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
382 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
383 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
384 int i; \
385 for (i=0;i<16;i++) { \
386 int k = xmesa_kernel8[i]; \
387 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
388 ditherValues[i] = (GLushort) ctable[j]; \
389 } \
390 }
391
392 #define FLAT_DITHER_ROW_SETUP(Y) \
393 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
394
395 #define FLAT_DITHER(X) ditherRow[(X)&3]
396
397
398
399 /**
400 * If pixelformat==PF_LOOKUP:
401 */
402 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
403
404 #define LOOKUP_SETUP \
405 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
406
407 #define LOOKUP( R, G, B ) \
408 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
409 _dither_lookup(DITH_G, (G)), \
410 _dither_lookup(DITH_B, (B)))]
411
412
413 /**
414 * If pixelformat==PF_HPCR:
415 *
416 * HP Color Recovery dithering (ad@lms.be 30/08/95)
417 * HP has on its 8-bit 700-series computers, a feature called
418 * 'Color Recovery'. This allows near 24-bit output (so they say).
419 * It is enabled by selecting the 8-bit TrueColor visual AND
420 * corresponding colormap (see tkInitWindow) AND doing some special
421 * dither.
422 */
423 extern const short xmesa_HPCR_DRGB[3][2][16];
424
425 #define DITHER_HPCR( X, Y, R, G, B ) \
426 ( ((xmesa->xm_visual->hpcr_rgbTbl[0][R] + xmesa_HPCR_DRGB[0][(Y)&1][(X)&15]) & 0xE0) \
427 |(((xmesa->xm_visual->hpcr_rgbTbl[1][G] + xmesa_HPCR_DRGB[1][(Y)&1][(X)&15]) & 0xE0)>>3) \
428 | ((xmesa->xm_visual->hpcr_rgbTbl[2][B] + xmesa_HPCR_DRGB[2][(Y)&1][(X)&15])>>6) \
429 )
430
431
432
433 /**
434 * If pixelformat==PF_1BIT:
435 */
436 extern const int xmesa_kernel1[16];
437
438 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
439 #define DITHER_1BIT( X, Y, R, G, B ) \
440 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
441
442
443
444 /**
445 * If pixelformat==PF_GRAYSCALE:
446 */
447 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
448
449
450
451 /**
452 * Converts a GL window Y coord to an X window Y coord:
453 */
454 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
455
456
457 /**
458 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
459 * X==0 is left, Y==0 is bottom.
460 */
461 #define PIXEL_ADDR1(XRB, X, Y) \
462 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
463
464 #define PIXEL_ADDR2(XRB, X, Y) \
465 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
466
467 #define PIXEL_ADDR3(XRB, X, Y) \
468 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
469
470 #define PIXEL_ADDR4(XRB, X, Y) \
471 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
472
473
474
475 /*
476 * External functions:
477 */
478
479 extern struct xmesa_renderbuffer *
480 xmesa_new_renderbuffer(struct gl_context *ctx, GLuint name,
481 const struct xmesa_visual *xmvis,
482 GLboolean backBuffer);
483
484 extern void
485 xmesa_delete_framebuffer(struct gl_framebuffer *fb);
486
487 extern XMesaBuffer
488 xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis);
489
490 extern unsigned long
491 xmesa_color_to_pixel( struct gl_context *ctx,
492 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
493 GLuint pixelFormat );
494
495 extern void
496 xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
497 GLuint *width, GLuint *height);
498
499 extern void
500 xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer);
501
502 extern void
503 xmesa_init_driver_functions( XMesaVisual xmvisual,
504 struct dd_function_table *driver );
505
506 extern void
507 xmesa_update_state( struct gl_context *ctx, GLbitfield new_state );
508
509 extern void
510 xmesa_set_renderbuffer_funcs(struct xmesa_renderbuffer *xrb,
511 enum pixel_format pixelformat, GLint depth);
512
513 extern void
514 xmesa_MapRenderbuffer(struct gl_context *ctx,
515 struct gl_renderbuffer *rb,
516 GLuint x, GLuint y, GLuint w, GLuint h,
517 GLbitfield mode,
518 GLubyte **mapOut, GLint *rowStrideOut);
519
520 extern void
521 xmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb);
522
523 extern void
524 xmesa_destroy_buffers_on_display(XMesaDisplay *dpy);
525
526
527 /**
528 * Using a function instead of an ordinary cast is safer.
529 */
530 static INLINE struct xmesa_renderbuffer *
531 xmesa_renderbuffer(struct gl_renderbuffer *rb)
532 {
533 return (struct xmesa_renderbuffer *) rb;
534 }
535
536
537 /**
538 * Return pointer to XMesaContext corresponding to a Mesa struct gl_context.
539 * Since we're using structure containment, it's just a cast!.
540 */
541 static INLINE XMesaContext
542 XMESA_CONTEXT(struct gl_context *ctx)
543 {
544 return (XMesaContext) ctx;
545 }
546
547
548 /**
549 * Return pointer to XMesaBuffer corresponding to a Mesa struct gl_framebuffer.
550 * Since we're using structure containment, it's just a cast!.
551 */
552 static INLINE XMesaBuffer
553 XMESA_BUFFER(struct gl_framebuffer *b)
554 {
555 return (XMesaBuffer) b;
556 }
557
558
559 /* Plugged into the software rasterizer. Try to use internal
560 * swrast-style point, line and triangle functions.
561 */
562 extern void xmesa_choose_point( struct gl_context *ctx );
563 extern void xmesa_choose_line( struct gl_context *ctx );
564 extern void xmesa_choose_triangle( struct gl_context *ctx );
565
566
567 extern void xmesa_register_swrast_functions( struct gl_context *ctx );
568
569
570
571 #define ENABLE_EXT_texure_compression_s3tc 0 /* SW texture compression */
572
573 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
574 #define ENABLE_EXT_timer_query 1 /* should have 64-bit GLuint64EXT */
575 #else
576 #define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
577 #endif
578
579
580 #define TEST_META_FUNCS 0
581
582
583 #endif