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