Update xlib driver to use newer state tracker context/framebuffer functions.
[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 "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"
40
41
42 extern _glthread_Mutex _xmesa_lock;
43
44 extern XMesaBuffer XMesaBufferList;
45
46 /* for PF_8R8G8B24 pixel format */
47 typedef struct {
48 GLubyte b;
49 GLubyte g;
50 GLubyte r;
51 } bgr_t;
52
53
54 struct xmesa_renderbuffer;
55
56
57
58 /** Framebuffer pixel formats */
59 enum pixel_format {
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 */
73 };
74
75
76 /**
77 * Visual inforation, derived from GLvisual.
78 * Basically corresponds to an XVisualInfo.
79 */
80 struct xmesa_visual {
81 GLvisual mesa_visual; /* Device independent visual parameters */
82 XMesaDisplay *display; /* The X11 display */
83 #ifdef XFree86Server
84 GLint ColormapEntries;
85 GLint nplanes;
86 #else
87 XMesaVisualInfo visinfo; /* X's visual info (pointer to private copy) */
88 XVisualInfo *vishandle; /* Only used in fakeglx.c */
89 #endif
90 GLint BitsPerPixel; /* True bits per pixel for XImages */
91
92 GLboolean ximage_flag; /* Use XImage for back buffer (not pixmap)? */
93
94 enum pixel_format dithered_pf; /* Pixel format when dithering */
95 enum pixel_format undithered_pf;/* Pixel format when not dithering */
96
97 GLfloat RedGamma; /* Gamma values, 1.0 is default */
98 GLfloat GreenGamma;
99 GLfloat BlueGamma;
100
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];
110
111 /* For PF_1BIT */
112 int bitFlip;
113 };
114
115
116 /**
117 * Context info, derived from GLcontext.
118 * Basically corresponds to a GLXContext.
119 */
120 struct xmesa_context {
121 struct st_context *st;
122
123 XMesaVisual xm_visual; /* Describes the buffers */
124 XMesaBuffer xm_buffer; /* current span/point/line/triangle buffer */
125
126 XMesaDisplay *display; /* == xm_visual->display */
127 GLboolean swapbytes; /* Host byte order != display byte order? */
128 GLboolean direct; /* Direct rendering context? */
129
130 enum pixel_format pixelformat;
131
132 GLubyte clearcolor[4]; /* current clearing color */
133 unsigned long clearpixel; /* current clearing pixel value */
134 };
135
136
137 /**
138 * Types of X/GLX drawables we might render into.
139 */
140 typedef enum {
141 WINDOW, /* An X window */
142 GLXWINDOW, /* GLX window */
143 PIXMAP, /* GLX pixmap */
144 PBUFFER /* GLX Pbuffer */
145 } BufferType;
146
147
148 /** Values for db_mode: */
149 /*@{*/
150 #define BACK_PIXMAP 1
151 #define BACK_XIMAGE 2
152 /*@}*/
153
154
155 /**
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.
165 */
166 struct xmesa_renderbuffer
167 {
168 struct st_renderbuffer St; /**< Base class (XXX temporary?) */
169
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 */
174
175 GLubyte *origin1; /* used for PIXEL_ADDR1 macro */
176 GLint width1;
177 GLushort *origin2; /* used for PIXEL_ADDR2 macro */
178 GLint width2;
179 GLubyte *origin3; /* used for PIXEL_ADDR3 macro */
180 GLint width3;
181 GLuint *origin4; /* used for PIXEL_ADDR4 macro */
182 GLint width4;
183
184 GLint bottom; /* used for FLIP macro, equals height - 1 */
185 };
186
187
188 /**
189 * Framebuffer information, derived from.
190 * Basically corresponds to a GLXDrawable.
191 */
192 struct xmesa_buffer {
193 struct st_framebuffer *stfb;
194
195 GLboolean wasCurrent; /* was ever the current buffer? */
196 XMesaVisual xm_visual; /* the X/Mesa visual */
197
198 XMesaDisplay *display;
199 BufferType type; /* window, pixmap, pbuffer or glxwindow */
200
201 struct xmesa_renderbuffer *frontxrb; /* front color renderbuffer */
202 struct xmesa_renderbuffer *backxrb; /* back color renderbuffer */
203
204 XMesaColormap cmap; /* the X colormap */
205
206 unsigned long selectedEvents;/* for pbuffers only */
207
208 GLint db_mode; /* 0 = single buffered */
209 /* BACK_PIXMAP = use Pixmap for back buffer */
210 /* BACK_XIMAGE = use XImage for back buffer */
211 GLboolean swAlpha;
212
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;
219 #endif
220
221 XMesaImage *rowimage; /* Used for optimized span writing */
222 XMesaPixmap stipple_pixmap; /* For polygon stippling */
223 XMesaGC stipple_gc; /* For polygon stippling */
224
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 */
228
229 /* The following are here instead of in the XMesaVisual
230 * because they depend on the window's colormap.
231 */
232
233 /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
234 unsigned long color_table[576]; /* RGB -> pixel value */
235
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 */
240
241 /* Used to do XAllocColor/XFreeColors accounting: */
242 int num_alloced;
243 #if defined(XFree86Server)
244 Pixel alloced_colors[256];
245 #else
246 unsigned long alloced_colors[256];
247 #endif
248
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 */
253
254 struct xmesa_buffer *Next; /* Linked list pointer: */
255 };
256
257
258 /**
259 * If pixelformat==PF_TRUECOLOR:
260 */
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]; \
265
266
267 /**
268 * If pixelformat==PF_TRUEDITHER:
269 */
270 #define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B ) \
271 { \
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]; \
276 }
277
278
279
280 /**
281 * If pixelformat==PF_8A8B8G8R:
282 */
283 #define PACK_8A8B8G8R( R, G, B, A ) \
284 ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
285
286
287 /**
288 * Like PACK_8A8B8G8R() but don't use alpha. This is usually an acceptable
289 * shortcut.
290 */
291 #define PACK_8B8G8R( R, G, B ) ( ((B) << 16) | ((G) << 8) | (R) )
292
293
294
295 /**
296 * If pixelformat==PF_8R8G8B:
297 */
298 #define PACK_8R8G8B( R, G, B) ( ((R) << 16) | ((G) << 8) | (B) )
299
300
301 /**
302 * If pixelformat==PF_5R6G5B:
303 */
304 #define PACK_5R6G5B( R, G, B) ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
305
306
307 /**
308 * If pixelformat==PF_8A8R8G8B:
309 */
310 #define PACK_8A8R8G8B( R, G, B, A ) \
311 ( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
312
313
314
315 /**
316 * If pixelformat==PF_DITHER:
317 *
318 * Improved 8-bit RGB dithering code contributed by Bob Mercier
319 * (mercier@hollywood.cinenet.net). Thanks Bob!
320 */
321 #ifdef DITHER666
322 # define DITH_R 6
323 # define DITH_G 6
324 # define DITH_B 6
325 # define DITH_MIX(r,g,b) (((r) * DITH_G + (g)) * DITH_B + (b))
326 #else
327 # define DITH_R 5
328 # define DITH_G 9
329 # define DITH_B 5
330 # define DITH_MIX(r,g,b) (((g) << 6) | ((b) << 3) | (r))
331 #endif
332 #define DITH_DX 4
333 #define DITH_DY 4
334 #define DITH_N (DITH_DX * DITH_DY)
335
336 #define _dither(C, c, d) (((unsigned)((DITH_N * (C - 1) + 1) * c + d)) >> 12)
337
338 #define MAXC 256
339 extern const int xmesa_kernel8[DITH_DY * DITH_DX];
340
341 /* Dither for random X,Y */
342 #define DITHER_SETUP \
343 int __d; \
344 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table;
345
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))])
351
352 /* Dither for random X, fixed Y */
353 #define XDITHER_SETUP(Y) \
354 int __d; \
355 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table; \
356 const int *kernel = &xmesa_kernel8[ ((Y)&3) << 2 ];
357
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))])
363
364
365
366 /*
367 * Dithering for flat-shaded triangles. Precompute all 16 possible
368 * pixel values given the triangle's RGB color. Contributed by Martin Shenk.
369 */
370 #define FLAT_DITHER_SETUP( R, G, B ) \
371 GLushort ditherValues[16]; \
372 { \
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); \
377 int i; \
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]; \
382 } \
383 }
384
385 #define FLAT_DITHER_ROW_SETUP(Y) \
386 GLushort *ditherRow = ditherValues + ( ((Y)&3) << 2);
387
388 #define FLAT_DITHER(X) ditherRow[(X)&3]
389
390
391
392 /**
393 * If pixelformat==PF_LOOKUP:
394 */
395 #define _dither_lookup(C, c) (((unsigned)((DITH_N * (C - 1) + 1) * c)) >> 12)
396
397 #define LOOKUP_SETUP \
398 unsigned long *ctable = XMESA_BUFFER(ctx->DrawBuffer)->color_table
399
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)))]
404
405
406
407 /**
408 * If pixelformat==PF_1BIT:
409 */
410 extern const int xmesa_kernel1[16];
411
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)
415
416
417
418 /**
419 * If pixelformat==PF_GRAYSCALE:
420 */
421 #define GRAY_RGB( R, G, B ) XMESA_BUFFER(ctx->DrawBuffer)->color_table[((R) + (G) + (B))/3]
422
423
424
425 /**
426 * Converts a GL window Y coord to an X window Y coord:
427 */
428 #define YFLIP(XRB, Y) ((XRB)->bottom - (Y))
429
430
431 /**
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.
434 */
435 #define PIXEL_ADDR1(XRB, X, Y) \
436 ( (XRB)->origin1 - (Y) * (XRB)->width1 + (X) )
437
438 #define PIXEL_ADDR2(XRB, X, Y) \
439 ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
440
441 #define PIXEL_ADDR3(XRB, X, Y) \
442 ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
443
444 #define PIXEL_ADDR4(XRB, X, Y) \
445 ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
446
447
448
449 /*
450 * External functions:
451 */
452
453 extern struct xmesa_renderbuffer *
454 xmesa_create_renderbuffer(GLcontext *ctx, GLuint name, const GLvisual *visual,
455 GLboolean backBuffer);
456
457 extern void
458 xmesa_delete_framebuffer(struct gl_framebuffer *fb);
459
460 extern XMesaBuffer
461 xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis);
462
463 extern unsigned long
464 xmesa_color_to_pixel( XMesaContext xmesa,
465 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
466 GLuint pixelFormat );
467
468 extern void
469 xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
470 GLuint *width, GLuint *height);
471
472 extern void
473 xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer);
474
475
476 extern void
477 xmesa_destroy_buffers_on_display(XMesaDisplay *dpy);
478
479
480 /**
481 * Using a function instead of an ordinary cast is safer.
482 */
483 static INLINE struct xmesa_renderbuffer *
484 xmesa_renderbuffer(struct gl_renderbuffer *rb)
485 {
486 return (struct xmesa_renderbuffer *) rb;
487 }
488
489
490 /**
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.
494 */
495 static INLINE XMesaContext
496 XMESA_CONTEXT(GLcontext *ctx)
497 {
498 return (XMesaContext) ctx->DriverCtx;
499 }
500
501
502 /**
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.
506 */
507 static INLINE XMesaBuffer
508 XMESA_BUFFER(GLframebuffer *fb)
509 {
510 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
511 return (XMesaBuffer) st_framebuffer_private(stfb);
512 }
513
514
515
516 struct pipe_context;
517
518 struct xmesa_surface
519 {
520 struct pipe_surface surface;
521 struct xmesa_renderbuffer *xrb;
522 XMesaDisplay *display;
523 BufferType type;
524 XMesaDrawable drawable;
525 XMesaImage *ximage;
526 XMesaGC gc;
527 };
528
529
530 /** Cast wrapper */
531 static INLINE struct xmesa_surface *
532 xmesa_surface(struct pipe_surface *ps)
533 {
534 return (struct xmesa_surface *) ps;
535 }
536
537
538 extern void
539 xmesa_clear(struct pipe_context *pipe, struct pipe_surface *ps, uint value);
540
541 extern struct pipe_context *
542 xmesa_create_softpipe(XMesaContext xm);
543
544 extern struct pipe_surface *
545 xmesa_surface_alloc(struct pipe_context *pipe, GLuint format);
546
547 extern struct pipe_surface *
548 xmesa_new_color_surface(struct pipe_context *pipe, GLuint format);
549
550
551 extern void
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);
554
555 extern void
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);
558
559 extern void
560 xmesa_get_tile_rgba(struct pipe_context *pipe, struct pipe_surface *ps,
561 uint x, uint y, uint w, uint h, float *p);
562
563 extern void
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);
566
567
568 extern struct pipe_surface *
569 xmesa_create_front_surface(XMesaVisual vis, Window win);
570
571 static INLINE GLuint
572 xmesa_buffer_width(XMesaBuffer b)
573 {
574 return b->stfb->Base.Width;
575 }
576
577 static INLINE GLuint
578 xmesa_buffer_height(XMesaBuffer b)
579 {
580 return b->stfb->Base.Height;
581 }
582
583
584 #endif