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