2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
35 #include "state_tracker/st_cb_fbo.h"
36 #include "state_tracker/st_context.h"
37 #include "state_tracker/st_public.h"
38 #include "pipe/softpipe/sp_context.h"
39 #include "pipe/softpipe/sp_surface.h"
42 extern _glthread_Mutex _xmesa_lock
;
44 extern XMesaBuffer XMesaBufferList
;
46 /* for PF_8R8G8B24 pixel format */
54 struct xmesa_renderbuffer
;
58 /** Framebuffer pixel formats */
60 PF_Index
, /**< Color Index mode */
61 PF_Truecolor
, /**< TrueColor or DirectColor, any depth */
62 PF_Dither_True
, /**< TrueColor with dithering */
63 PF_8A8R8G8B
, /**< 32-bit TrueColor: 8-A, 8-R, 8-G, 8-B bits */
64 PF_8A8B8G8R
, /**< 32-bit TrueColor: 8-A, 8-B, 8-G, 8-R bits */
65 PF_8R8G8B
, /**< 32-bit TrueColor: 8-R, 8-G, 8-B bits */
66 PF_8R8G8B24
, /**< 24-bit TrueColor: 8-R, 8-G, 8-B bits */
67 PF_5R6G5B
, /**< 16-bit TrueColor: 5-R, 6-G, 5-B bits */
68 PF_Dither
, /**< Color-mapped RGB with dither */
69 PF_Lookup
, /**< Color-mapped RGB without dither */
70 PF_1Bit
, /**< monochrome dithering of RGB */
71 PF_Grayscale
, /**< Grayscale or StaticGray */
72 PF_Dither_5R6G5B
/**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
77 * Visual inforation, derived from GLvisual.
78 * Basically corresponds to an XVisualInfo.
81 GLvisual mesa_visual
; /* Device independent visual parameters */
82 XMesaDisplay
*display
; /* The X11 display */
84 GLint ColormapEntries
;
87 XMesaVisualInfo visinfo
; /* X's visual info (pointer to private copy) */
88 XVisualInfo
*vishandle
; /* Only used in fakeglx.c */
90 GLint BitsPerPixel
; /* True bits per pixel for XImages */
92 GLboolean ximage_flag
; /* Use XImage for back buffer (not pixmap)? */
94 enum pixel_format dithered_pf
; /* Pixel format when dithering */
95 enum pixel_format undithered_pf
;/* Pixel format when not dithering */
97 GLfloat RedGamma
; /* Gamma values, 1.0 is default */
101 /* For PF_TRUECOLOR */
102 GLint rshift
, gshift
, bshift
;/* Pixel color component shifts */
103 GLubyte Kernel
[16]; /* Dither kernel */
104 unsigned long RtoPixel
[512]; /* RGB to pixel conversion */
105 unsigned long GtoPixel
[512];
106 unsigned long BtoPixel
[512];
107 GLubyte PixelToR
[256]; /* Pixel to RGB conversion */
108 GLubyte PixelToG
[256];
109 GLubyte PixelToB
[256];
117 * Context info, derived from GLcontext.
118 * Basically corresponds to a GLXContext.
120 struct xmesa_context
{
121 struct st_context
*st
;
123 XMesaVisual xm_visual
; /* Describes the buffers */
124 XMesaBuffer xm_buffer
; /* current span/point/line/triangle buffer */
126 XMesaDisplay
*display
; /* == xm_visual->display */
127 GLboolean swapbytes
; /* Host byte order != display byte order? */
128 GLboolean direct
; /* Direct rendering context? */
130 enum pixel_format pixelformat
;
132 GLubyte clearcolor
[4]; /* current clearing color */
133 unsigned long clearpixel
; /* current clearing pixel value */
138 * Types of X/GLX drawables we might render into.
141 WINDOW
, /* An X window */
142 GLXWINDOW
, /* GLX window */
143 PIXMAP
, /* GLX pixmap */
144 PBUFFER
/* GLX Pbuffer */
148 /** Values for db_mode: */
150 #define BACK_PIXMAP 1
151 #define BACK_XIMAGE 2
156 * An xmesa_renderbuffer represents the back or front color buffer.
157 * For the front color buffer:
158 * <drawable> is the X window
159 * For the back color buffer:
160 * Either <ximage> or <pixmap> will be used, never both.
161 * In any case, <drawable> always equals <pixmap>.
162 * For stand-alone Mesa, we could merge <drawable> and <pixmap> into one
163 * field. We don't do that for the server-side GLcore module because
164 * pixmaps and drawables are different and we'd need a bunch of casts.
166 struct xmesa_renderbuffer
168 struct st_renderbuffer St
; /**< Base class (XXX temporary?) */
170 XMesaBuffer Parent
; /**< The XMesaBuffer this renderbuffer belongs to */
171 XMesaDrawable drawable
; /* Usually the X window ID */
172 XMesaPixmap pixmap
; /* Back color buffer */
173 XMesaImage
*ximage
; /* The back buffer, if not using a Pixmap */
175 GLubyte
*origin1
; /* used for PIXEL_ADDR1 macro */
177 GLushort
*origin2
; /* used for PIXEL_ADDR2 macro */
179 GLubyte
*origin3
; /* used for PIXEL_ADDR3 macro */
181 GLuint
*origin4
; /* used for PIXEL_ADDR4 macro */
184 GLint bottom
; /* used for FLIP macro, equals height - 1 */
189 * Framebuffer information, derived from.
190 * Basically corresponds to a GLXDrawable.
192 struct xmesa_buffer
{
193 struct st_framebuffer
*stfb
;
195 GLboolean wasCurrent
; /* was ever the current buffer? */
196 XMesaVisual xm_visual
; /* the X/Mesa visual */
198 XMesaDisplay
*display
;
199 BufferType type
; /* window, pixmap, pbuffer or glxwindow */
201 struct xmesa_renderbuffer
*frontxrb
; /* front color renderbuffer */
202 struct xmesa_renderbuffer
*backxrb
; /* back color renderbuffer */
204 XMesaColormap cmap
; /* the X colormap */
206 unsigned long selectedEvents
;/* for pbuffers only */
208 GLint db_mode
; /* 0 = single buffered */
209 /* BACK_PIXMAP = use Pixmap for back buffer */
210 /* BACK_XIMAGE = use XImage for back buffer */
213 GLuint shm
; /* X Shared Memory extension status: */
214 /* 0 = not available */
215 /* 1 = XImage support available */
216 /* 2 = Pixmap support available too */
217 #if defined(USE_XSHM) && !defined(XFree86Server)
218 XShmSegmentInfo shminfo
;
221 XMesaImage
*rowimage
; /* Used for optimized span writing */
222 XMesaPixmap stipple_pixmap
; /* For polygon stippling */
223 XMesaGC stipple_gc
; /* For polygon stippling */
225 XMesaGC gc
; /* scratch GC for span, line, tri drawing */
226 XMesaGC cleargc
; /* GC for clearing the color buffer */
227 XMesaGC swapgc
; /* GC for swapping the color buffers */
229 /* The following are here instead of in the XMesaVisual
230 * because they depend on the window's colormap.
233 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
234 unsigned long color_table
[576]; /* RGB -> pixel value */
236 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
237 GLubyte pixel_to_r
[65536]; /* pixel value -> red */
238 GLubyte pixel_to_g
[65536]; /* pixel value -> green */
239 GLubyte pixel_to_b
[65536]; /* pixel value -> blue */
241 /* Used to do XAllocColor/XFreeColors accounting: */
243 #if defined(XFree86Server)
244 Pixel alloced_colors
[256];
246 unsigned long alloced_colors
[256];
249 /* GLX_EXT_texture_from_pixmap */
250 GLint TextureTarget
; /** GLX_TEXTURE_1D_EXT, for example */
251 GLint TextureFormat
; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
252 GLint TextureMipmap
; /** 0 or 1 */
254 struct xmesa_buffer
*Next
; /* Linked list pointer: */
259 * If pixelformat==PF_TRUECOLOR:
261 #define PACK_TRUECOLOR( PIXEL, R, G, B ) \
262 PIXEL = xmesa->xm_visual->RtoPixel[R] \
263 | xmesa->xm_visual->GtoPixel[G] \
264 | xmesa->xm_visual->BtoPixel[B]; \
268 * If pixelformat==PF_TRUEDITHER:
270 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
272 int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)]; \
273 PIXEL = xmesa->xm_visual->RtoPixel[(R)+d] \
274 | xmesa->xm_visual->GtoPixel[(G)+d] \
275 | xmesa->xm_visual->BtoPixel[(B)+d]; \
281 * If pixelformat==PF_8A8B8G8R:
283 #define PACK_8A8B8G8R( R, G, B, A ) \
284 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
288 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
291 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
296 * If pixelformat==PF_8R8G8B:
298 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
302 * If pixelformat==PF_5R6G5B:
304 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
308 * If pixelformat==PF_8A8R8G8B:
310 #define PACK_8A8R8G8B( R, G, B, A ) \
311 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
316 * If pixelformat==PF_DITHER:
318 * Improved 8-bit RGB dithering code contributed by Bob Mercier
319 * (mercier@hollywood.cinenet.net). Thanks Bob!
325 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
330 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
334 #define DITH_N (DITH_DX * DITH_DY)
336 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
339 extern const int xmesa_kernel8
[DITH_DY
* DITH_DX
];
341 /* Dither for random X,Y */
342 #define DITHER_SETUP \
344 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
346 #define DITHER( X, Y, R, G, B ) \
347 (__d = xmesa_kernel8[(((Y)&3)<<2) | ((X)&3)], \
348 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
349 _dither(DITH_G, (G), __d), \
350 _dither(DITH_B, (B), __d))])
352 /* Dither for random X, fixed Y */
353 #define XDITHER_SETUP(Y) \
355 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
356 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
358 #define XDITHER( X, R, G, B ) \
359 (__d = kernel[(X)&3], \
360 ctable[DITH_MIX(_dither(DITH_R, (R), __d), \
361 _dither(DITH_G, (G), __d), \
362 _dither(DITH_B, (B), __d))])
367 * Dithering for flat-shaded triangles. Precompute all 16 possible
368 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
370 #define FLAT_DITHER_SETUP( R, G, B ) \
371 GLushort ditherValues[16]; \
373 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
374 int msdr = (DITH_N*((DITH_R)-1)+1) * (R); \
375 int msdg = (DITH_N*((DITH_G)-1)+1) * (G); \
376 int msdb = (DITH_N*((DITH_B)-1)+1) * (B); \
378 for (i=0;i<16;i++) { \
379 int k = xmesa_kernel8[i]; \
380 int j = DITH_MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );\
381 ditherValues[i] = (GLushort) ctable[j]; \
385 #define FLAT_DITHER_ROW_SETUP(Y) \
386 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
388 #define FLAT_DITHER(X) ditherRow[(X)&3]
393 * If pixelformat==PF_LOOKUP:
395 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
397 #define LOOKUP_SETUP \
398 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
400 #define LOOKUP( R, G, B ) \
401 ctable[DITH_MIX(_dither_lookup(DITH_R, (R)), \
402 _dither_lookup(DITH_G, (G)), \
403 _dither_lookup(DITH_B, (B)))]
408 * If pixelformat==PF_1BIT:
410 extern const int xmesa_kernel1
[16];
412 #define SETUP_1BIT int bitFlip = xmesa->xm_visual->bitFlip
413 #define DITHER_1BIT( X, Y, R, G, B ) \
414 (( ((int)(R)+(int)(G)+(int)(B)) > xmesa_kernel1[(((Y)&3) << 2) | ((X)&3)] ) ^ bitFlip)
419 * If pixelformat==PF_GRAYSCALE:
421 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
426 * Converts a GL window Y coord to an X window Y coord:
428 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
432 * Return the address of a 1, 2 or 4-byte pixel in the buffer's XImage:
433 * X==0 is left, Y==0 is bottom.
435 #define PIXEL_ADDR1(XRB, X, Y) \
436 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
438 #define PIXEL_ADDR2(XRB, X, Y) \
439 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
441 #define PIXEL_ADDR3(XRB, X, Y) \
442 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
444 #define PIXEL_ADDR4(XRB, X, Y) \
445 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
450 * External functions:
453 extern struct xmesa_renderbuffer
*
454 xmesa_create_renderbuffer(GLcontext
*ctx
, GLuint name
, const GLvisual
*visual
,
455 GLboolean backBuffer
);
458 xmesa_delete_framebuffer(struct gl_framebuffer
*fb
);
461 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
);
464 xmesa_color_to_pixel( XMesaContext xmesa
,
465 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
466 GLuint pixelFormat
);
469 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
470 GLuint
*width
, GLuint
*height
);
473 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
);
477 xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
);
481 * Using a function instead of an ordinary cast is safer.
483 static INLINE
struct xmesa_renderbuffer
*
484 xmesa_renderbuffer(struct gl_renderbuffer
*rb
)
486 return (struct xmesa_renderbuffer
*) rb
;
491 * Return pointer to XMesaContext corresponding to a Mesa GLcontext.
492 * Since we're using structure containment, it's just a cast!.
493 * XXX should use inlined function for better type safety.
495 static INLINE XMesaContext
496 XMESA_CONTEXT(GLcontext
*ctx
)
498 return (XMesaContext
) ctx
->DriverCtx
;
503 * Return pointer to XMesaBuffer corresponding to a Mesa GLframebuffer.
504 * Since we're using structure containment, it's just a cast!.
505 * XXX should use inlined function for better type safety.
507 static INLINE XMesaBuffer
508 XMESA_BUFFER(GLframebuffer
*fb
)
510 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
511 return (XMesaBuffer
) st_framebuffer_private(stfb
);
520 struct pipe_surface surface
;
521 struct xmesa_renderbuffer
*xrb
;
522 XMesaDisplay
*display
;
524 XMesaDrawable drawable
;
531 static INLINE
struct xmesa_surface
*
532 xmesa_surface(struct pipe_surface
*ps
)
534 return (struct xmesa_surface
*) ps
;
539 xmesa_clear(struct pipe_context
*pipe
, struct pipe_surface
*ps
, uint value
);
541 extern struct pipe_context
*
542 xmesa_create_softpipe(XMesaContext xm
);
544 extern struct pipe_surface
*
545 xmesa_surface_alloc(struct pipe_context
*pipe
, GLuint format
);
547 extern struct pipe_surface
*
548 xmesa_new_color_surface(struct pipe_context
*pipe
, GLuint format
);
552 xmesa_get_tile(struct pipe_context
*pipe
, struct pipe_surface
*ps
,
553 uint x
, uint y
, uint w
, uint h
, void *p
, int dst_stride
);
556 xmesa_put_tile(struct pipe_context
*pipe
, struct pipe_surface
*ps
,
557 uint x
, uint y
, uint w
, uint h
, const void *p
, int src_stride
);
560 xmesa_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*ps
,
561 uint x
, uint y
, uint w
, uint h
, float *p
);
564 xmesa_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*ps
,
565 uint x
, uint y
, uint w
, uint h
, const float *p
);
568 extern struct pipe_surface
*
569 xmesa_create_front_surface(XMesaVisual vis
, Window win
);
572 xmesa_buffer_width(XMesaBuffer b
)
574 return b
->stfb
->Base
.Width
;
578 xmesa_buffer_height(XMesaBuffer b
)
580 return b
->stfb
->Base
.Height
;