1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg#ifndef XMESAP_H
27848b8605Smrg#define XMESAP_H
28848b8605Smrg
29848b8605Smrg
30848b8605Smrg#include "c11/threads.h"
31848b8605Smrg#include "xmesa.h"
32848b8605Smrg#include "main/mtypes.h"
33848b8605Smrg#include "swrast/s_context.h"
34848b8605Smrg
35848b8605Smrg
36848b8605Smrgextern mtx_t _xmesa_lock;
37848b8605Smrg
38848b8605Smrgextern XMesaBuffer XMesaBufferList;
39848b8605Smrg
40848b8605Smrg/* for PF_8R8G8B24 pixel format */
41848b8605Smrgtypedef struct {
42848b8605Smrg   GLubyte b;
43848b8605Smrg   GLubyte g;
44848b8605Smrg   GLubyte r;
45848b8605Smrg} bgr_t;
46848b8605Smrg
47848b8605Smrg
48848b8605Smrgstruct xmesa_renderbuffer;
49848b8605Smrg
50848b8605Smrg
51848b8605Smrg/* Function pointer for clearing color buffers */
52848b8605Smrgtypedef void (*ClearFunc)( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
53848b8605Smrg                           GLint x, GLint y, GLint width, GLint height );
54848b8605Smrg
55848b8605Smrg
56848b8605Smrg
57848b8605Smrg
58848b8605Smrg/** Framebuffer pixel formats */
59848b8605Smrgenum pixel_format {
60848b8605Smrg   PF_Truecolor,	/**< TrueColor or DirectColor, any depth */
61848b8605Smrg   PF_Dither_True,	/**< TrueColor with dithering */
62848b8605Smrg   PF_8A8R8G8B,		/**< 32-bit TrueColor:  8-A, 8-R, 8-G, 8-B bits */
63848b8605Smrg   PF_8A8B8G8R,		/**< 32-bit TrueColor:  8-A, 8-B, 8-G, 8-R bits */
64848b8605Smrg   PF_8R8G8B,		/**< 32-bit TrueColor:  8-R, 8-G, 8-B bits */
65848b8605Smrg   PF_8R8G8B24,		/**< 24-bit TrueColor:  8-R, 8-G, 8-B bits */
66848b8605Smrg   PF_5R6G5B,		/**< 16-bit TrueColor:  5-R, 6-G, 5-B bits */
67848b8605Smrg   PF_Dither_5R6G5B	/**< 16-bit dithered TrueColor: 5-R, 6-G, 5-B */
68848b8605Smrg};
69848b8605Smrg
70848b8605Smrg
71848b8605Smrg/**
72848b8605Smrg * Visual inforation, derived from struct gl_config.
73848b8605Smrg * Basically corresponds to an XVisualInfo.
74848b8605Smrg */
75848b8605Smrgstruct xmesa_visual {
76848b8605Smrg   struct gl_config mesa_visual;	/* Device independent visual parameters */
77848b8605Smrg   XMesaDisplay *display;	/* The X11 display */
78848b8605Smrg   int screen, visualID;
79848b8605Smrg   int visualType;
80848b8605Smrg   XMesaVisualInfo visinfo;	/* X's visual info (pointer to private copy) */
81848b8605Smrg   GLint BitsPerPixel;		/* True bits per pixel for XImages */
82848b8605Smrg
83848b8605Smrg   GLboolean ximage_flag;	/* Use XImage for back buffer (not pixmap)? */
84848b8605Smrg
85848b8605Smrg   enum pixel_format dithered_pf;  /* Pixel format when dithering */
86848b8605Smrg   enum pixel_format undithered_pf;/* Pixel format when not dithering */
87848b8605Smrg
88848b8605Smrg   GLfloat RedGamma;		/* Gamma values, 1.0 is default */
89848b8605Smrg   GLfloat GreenGamma;
90848b8605Smrg   GLfloat BlueGamma;
91848b8605Smrg
92848b8605Smrg   /* For PF_TRUECOLOR */
93848b8605Smrg   GLint rshift, gshift, bshift;/* Pixel color component shifts */
94848b8605Smrg   GLubyte Kernel[16];		/* Dither kernel */
95848b8605Smrg   unsigned long RtoPixel[512];	/* RGB to pixel conversion */
96848b8605Smrg   unsigned long GtoPixel[512];
97848b8605Smrg   unsigned long BtoPixel[512];
98848b8605Smrg   GLubyte PixelToR[256];	/* Pixel to RGB conversion */
99848b8605Smrg   GLubyte PixelToG[256];
100848b8605Smrg   GLubyte PixelToB[256];
101848b8605Smrg};
102848b8605Smrg
103848b8605Smrg
104848b8605Smrg/**
105848b8605Smrg * Context info, derived from struct gl_context.
106848b8605Smrg * Basically corresponds to a GLXContext.
107848b8605Smrg */
108848b8605Smrgstruct xmesa_context {
109848b8605Smrg   struct gl_context mesa;		/* the core library context (containment) */
110848b8605Smrg   XMesaVisual xm_visual;	/* Describes the buffers */
111848b8605Smrg   XMesaBuffer xm_buffer;	/* current span/point/line/triangle buffer */
112848b8605Smrg
113848b8605Smrg   XMesaDisplay *display;	/* == xm_visual->display */
114848b8605Smrg   GLboolean swapbytes;		/* Host byte order != display byte order? */
115848b8605Smrg   GLboolean direct;		/* Direct rendering context? */
116848b8605Smrg
117848b8605Smrg   enum pixel_format pixelformat;
118848b8605Smrg
119848b8605Smrg   GLubyte clearcolor[4];		/* current clearing color */
120848b8605Smrg   unsigned long clearpixel;		/* current clearing pixel value */
121848b8605Smrg};
122848b8605Smrg
123848b8605Smrg
124848b8605Smrg/**
125848b8605Smrg * Types of X/GLX drawables we might render into.
126848b8605Smrg */
127848b8605Smrgtypedef enum {
128848b8605Smrg   WINDOW,          /* An X window */
129848b8605Smrg   GLXWINDOW,       /* GLX window */
130848b8605Smrg   PIXMAP,          /* GLX pixmap */
131848b8605Smrg   PBUFFER          /* GLX Pbuffer */
132848b8605Smrg} BufferType;
133848b8605Smrg
134848b8605Smrg
135848b8605Smrg/** Values for db_mode: */
136848b8605Smrg/*@{*/
137848b8605Smrg#define BACK_PIXMAP	1
138848b8605Smrg#define BACK_XIMAGE	2
139848b8605Smrg/*@}*/
140848b8605Smrg
141848b8605Smrg
142848b8605Smrg/**
143848b8605Smrg * An xmesa_renderbuffer represents the back or front color buffer.
144848b8605Smrg * For the front color buffer:
145848b8605Smrg *    <drawable> is the X window
146848b8605Smrg * For the back color buffer:
147848b8605Smrg *    Either <ximage> or <pixmap> will be used, never both.
148848b8605Smrg * In any case, <drawable> always equals <pixmap>.
149848b8605Smrg * For stand-alone Mesa, we could merge <drawable> and <pixmap> into one
150848b8605Smrg * field.  We don't do that for the server-side GLcore module because
151848b8605Smrg * pixmaps and drawables are different and we'd need a bunch of casts.
152848b8605Smrg */
153848b8605Smrgstruct xmesa_renderbuffer
154848b8605Smrg{
155848b8605Smrg   struct swrast_renderbuffer Base;  /* Base class */
156848b8605Smrg
157848b8605Smrg   XMesaBuffer Parent;  /**< The XMesaBuffer this renderbuffer belongs to */
158848b8605Smrg   XMesaDrawable drawable;	/* Usually the X window ID */
159848b8605Smrg   XMesaPixmap pixmap;	/* Back color buffer */
160848b8605Smrg   XMesaImage *ximage;	/* The back buffer, if not using a Pixmap */
161848b8605Smrg
162848b8605Smrg   GLushort *origin2;	/* used for PIXEL_ADDR2 macro */
163848b8605Smrg   GLint width2;
164848b8605Smrg   GLubyte *origin3;	/* used for PIXEL_ADDR3 macro */
165848b8605Smrg   GLint width3;
166848b8605Smrg   GLuint *origin4;	/* used for PIXEL_ADDR4 macro */
167848b8605Smrg   GLint width4;
168848b8605Smrg
169848b8605Smrg   GLint bottom;	/* used for FLIP macro, equals height - 1 */
170848b8605Smrg
171848b8605Smrg   ClearFunc clearFunc;
172848b8605Smrg
173848b8605Smrg   GLuint map_x, map_y, map_w, map_h;
174848b8605Smrg   GLbitfield map_mode;
175848b8605Smrg   XMesaImage *map_ximage;
176848b8605Smrg};
177848b8605Smrg
178848b8605Smrg
179848b8605Smrg/**
180848b8605Smrg * Framebuffer information, derived from.
181848b8605Smrg * Basically corresponds to a GLXDrawable.
182848b8605Smrg */
183848b8605Smrgstruct xmesa_buffer {
184848b8605Smrg   struct gl_framebuffer mesa_buffer;	/* depth, stencil, accum, etc buffers */
185848b8605Smrg				/* This MUST BE FIRST! */
186848b8605Smrg   GLboolean wasCurrent;	/* was ever the current buffer? */
187848b8605Smrg   XMesaVisual xm_visual;	/* the X/Mesa visual */
188848b8605Smrg
189848b8605Smrg   XMesaDisplay *display;
190848b8605Smrg   BufferType type;             /* window, pixmap, pbuffer or glxwindow */
191848b8605Smrg
192848b8605Smrg   GLboolean largestPbuffer;    /**< for pbuffers */
193848b8605Smrg   GLboolean preservedContents; /**< for pbuffers */
194848b8605Smrg
195848b8605Smrg   struct xmesa_renderbuffer *frontxrb; /* front color renderbuffer */
196848b8605Smrg   struct xmesa_renderbuffer *backxrb;  /* back color renderbuffer */
197848b8605Smrg
198848b8605Smrg   XMesaColormap cmap;		/* the X colormap */
199848b8605Smrg
200848b8605Smrg   unsigned long selectedEvents;/* for pbuffers only */
201848b8605Smrg
202848b8605Smrg   GLint db_mode;		/* 0 = single buffered */
203848b8605Smrg				/* BACK_PIXMAP = use Pixmap for back buffer */
204848b8605Smrg				/* BACK_XIMAGE = use XImage for back buffer */
205848b8605Smrg   GLuint shm;			/* X Shared Memory extension status:	*/
206848b8605Smrg				/*    0 = not available			*/
207848b8605Smrg				/*    1 = XImage support available	*/
208848b8605Smrg				/*    2 = Pixmap support available too	*/
209848b8605Smrg#if defined(USE_XSHM)
210848b8605Smrg   XShmSegmentInfo shminfo;
211848b8605Smrg#endif
212848b8605Smrg
213848b8605Smrg   //   XMesaImage *rowimage;	/* Used for optimized span writing */
214848b8605Smrg   XMesaPixmap stipple_pixmap;	/* For polygon stippling */
215848b8605Smrg   XMesaGC stipple_gc;		/* For polygon stippling */
216848b8605Smrg
217848b8605Smrg   XMesaGC gc;			/* scratch GC for span, line, tri drawing */
218848b8605Smrg   XMesaGC cleargc;		/* GC for clearing the color buffer */
219848b8605Smrg   XMesaGC swapgc;		/* GC for swapping the color buffers */
220848b8605Smrg
221848b8605Smrg   /* The following are here instead of in the XMesaVisual
222848b8605Smrg    * because they depend on the window's colormap.
223848b8605Smrg    */
224848b8605Smrg
225848b8605Smrg   /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
226848b8605Smrg   unsigned long color_table[576];	/* RGB -> pixel value */
227848b8605Smrg
228848b8605Smrg   /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
229848b8605Smrg   GLubyte pixel_to_r[65536];		/* pixel value -> red */
230848b8605Smrg   GLubyte pixel_to_g[65536];		/* pixel value -> green */
231848b8605Smrg   GLubyte pixel_to_b[65536];		/* pixel value -> blue */
232848b8605Smrg
233848b8605Smrg   /* Used to do XAllocColor/XFreeColors accounting: */
234848b8605Smrg   int num_alloced;
235848b8605Smrg   unsigned long alloced_colors[256];
236848b8605Smrg
237848b8605Smrg   /* GLX_EXT_texture_from_pixmap */
238848b8605Smrg   GLint TextureTarget; /** GLX_TEXTURE_1D_EXT, for example */
239848b8605Smrg   GLint TextureFormat; /** GLX_TEXTURE_FORMAT_RGB_EXT, for example */
240848b8605Smrg   GLint TextureMipmap; /** 0 or 1 */
241848b8605Smrg
242848b8605Smrg   struct xmesa_buffer *Next;	/* Linked list pointer: */
243848b8605Smrg};
244848b8605Smrg
245848b8605Smrg
246848b8605Smrg/**
247848b8605Smrg * If pixelformat==PF_TRUECOLOR:
248848b8605Smrg */
249848b8605Smrg#define PACK_TRUECOLOR( PIXEL, R, G, B )	\
250848b8605Smrg   PIXEL = xmesa->xm_visual->RtoPixel[R]	\
251848b8605Smrg         | xmesa->xm_visual->GtoPixel[G]	\
252b8e80941Smrg         | xmesa->xm_visual->BtoPixel[B];
253848b8605Smrg
254848b8605Smrg
255848b8605Smrg/**
256848b8605Smrg * If pixelformat==PF_TRUEDITHER:
257848b8605Smrg */
258848b8605Smrg#define PACK_TRUEDITHER( PIXEL, X, Y, R, G, B )			\
259848b8605Smrg{								\
260848b8605Smrg   int d = xmesa->xm_visual->Kernel[((X)&3) | (((Y)&3)<<2)];	\
261848b8605Smrg   PIXEL = xmesa->xm_visual->RtoPixel[(R)+d]			\
262848b8605Smrg         | xmesa->xm_visual->GtoPixel[(G)+d]			\
263848b8605Smrg         | xmesa->xm_visual->BtoPixel[(B)+d];			\
264848b8605Smrg}
265848b8605Smrg
266848b8605Smrg
267848b8605Smrg
268848b8605Smrg/**
269848b8605Smrg * If pixelformat==PF_8A8B8G8R:
270848b8605Smrg */
271848b8605Smrg#define PACK_8A8B8G8R( R, G, B, A )	\
272848b8605Smrg	( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
273848b8605Smrg
274848b8605Smrg
275848b8605Smrg/**
276848b8605Smrg * Like PACK_8A8B8G8R() but don't use alpha.  This is usually an acceptable
277848b8605Smrg * shortcut.
278848b8605Smrg */
279848b8605Smrg#define PACK_8B8G8R( R, G, B )   ( ((B) << 16) | ((G) << 8) | (R) )
280848b8605Smrg
281848b8605Smrg
282848b8605Smrg
283848b8605Smrg/**
284848b8605Smrg * If pixelformat==PF_8R8G8B:
285848b8605Smrg */
286848b8605Smrg#define PACK_8R8G8B( R, G, B)	 ( ((R) << 16) | ((G) << 8) | (B) )
287848b8605Smrg
288848b8605Smrg
289848b8605Smrg/**
290848b8605Smrg * If pixelformat==PF_5R6G5B:
291848b8605Smrg */
292848b8605Smrg#define PACK_5R6G5B( R, G, B)	 ( (((R) & 0xf8) << 8) | (((G) & 0xfc) << 3) | ((B) >> 3) )
293848b8605Smrg
294848b8605Smrg
295848b8605Smrg/**
296848b8605Smrg * If pixelformat==PF_8A8R8G8B:
297848b8605Smrg */
298848b8605Smrg#define PACK_8A8R8G8B( R, G, B, A )	\
299848b8605Smrg	( ((A) << 24) | ((R) << 16) | ((G) << 8) | (B) )
300848b8605Smrg
301848b8605Smrg
302848b8605Smrg
303848b8605Smrg
304848b8605Smrg/**
305848b8605Smrg * Converts a GL window Y coord to an X window Y coord:
306848b8605Smrg */
307848b8605Smrg#define YFLIP(XRB, Y)  ((XRB)->bottom - (Y))
308848b8605Smrg
309848b8605Smrg
310848b8605Smrg/**
311848b8605Smrg * Return the address of a 2, 3 or 4-byte pixel in the buffer's XImage:
312848b8605Smrg * X==0 is left, Y==0 is bottom.
313848b8605Smrg */
314848b8605Smrg#define PIXEL_ADDR2(XRB, X, Y)  \
315848b8605Smrg   ( (XRB)->origin2 - (Y) * (XRB)->width2 + (X) )
316848b8605Smrg
317848b8605Smrg#define PIXEL_ADDR3(XRB, X, Y)  \
318848b8605Smrg   ( (bgr_t *) ( (XRB)->origin3 - (Y) * (XRB)->width3 + 3 * (X) ))
319848b8605Smrg
320848b8605Smrg#define PIXEL_ADDR4(XRB, X, Y)  \
321848b8605Smrg   ( (XRB)->origin4 - (Y) * (XRB)->width4 + (X) )
322848b8605Smrg
323848b8605Smrg
324848b8605Smrg
325848b8605Smrg/*
326848b8605Smrg * External functions:
327848b8605Smrg */
328848b8605Smrg
329848b8605Smrgextern struct xmesa_renderbuffer *
330848b8605Smrgxmesa_new_renderbuffer(struct gl_context *ctx, GLuint name,
331848b8605Smrg                       const struct xmesa_visual *xmvis,
332848b8605Smrg                       GLboolean backBuffer);
333848b8605Smrg
334848b8605Smrgextern void
335848b8605Smrgxmesa_delete_framebuffer(struct gl_framebuffer *fb);
336848b8605Smrg
337848b8605Smrgextern XMesaBuffer
338848b8605Smrgxmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis);
339848b8605Smrg
340848b8605Smrgextern unsigned long
341848b8605Smrgxmesa_color_to_pixel( struct gl_context *ctx,
342848b8605Smrg                      GLubyte r, GLubyte g, GLubyte b, GLubyte a,
343848b8605Smrg                      GLuint pixelFormat );
344848b8605Smrg
345848b8605Smrgextern void
346848b8605Smrgxmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
347848b8605Smrg                      GLuint *width, GLuint *height);
348848b8605Smrg
349848b8605Smrgextern void
350848b8605Smrgxmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer);
351848b8605Smrg
352848b8605Smrgextern void
353848b8605Smrgxmesa_init_driver_functions( XMesaVisual xmvisual,
354848b8605Smrg                             struct dd_function_table *driver );
355848b8605Smrg
356848b8605Smrgextern void
357848b8605Smrgxmesa_MapRenderbuffer(struct gl_context *ctx,
358848b8605Smrg                      struct gl_renderbuffer *rb,
359848b8605Smrg                      GLuint x, GLuint y, GLuint w, GLuint h,
360848b8605Smrg                      GLbitfield mode,
361b8e80941Smrg                      GLubyte **mapOut, GLint *rowStrideOut,
362b8e80941Smrg                      bool flip_y);
363848b8605Smrg
364848b8605Smrgextern void
365848b8605Smrgxmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb);
366848b8605Smrg
367848b8605Smrgextern void
368848b8605Smrgxmesa_destroy_buffers_on_display(XMesaDisplay *dpy);
369848b8605Smrg
370848b8605Smrg
371848b8605Smrg/**
372848b8605Smrg * Using a function instead of an ordinary cast is safer.
373848b8605Smrg */
374b8e80941Smrgstatic inline struct xmesa_renderbuffer *
375848b8605Smrgxmesa_renderbuffer(struct gl_renderbuffer *rb)
376848b8605Smrg{
377848b8605Smrg   return (struct xmesa_renderbuffer *) rb;
378848b8605Smrg}
379848b8605Smrg
380848b8605Smrg
381848b8605Smrg/**
382848b8605Smrg * Return pointer to XMesaContext corresponding to a Mesa struct gl_context.
383848b8605Smrg * Since we're using structure containment, it's just a cast!.
384848b8605Smrg */
385b8e80941Smrgstatic inline XMesaContext
386848b8605SmrgXMESA_CONTEXT(struct gl_context *ctx)
387848b8605Smrg{
388848b8605Smrg   return (XMesaContext) ctx;
389848b8605Smrg}
390848b8605Smrg
391848b8605Smrg
392848b8605Smrg/**
393848b8605Smrg * Return pointer to XMesaBuffer corresponding to a Mesa struct gl_framebuffer.
394848b8605Smrg * Since we're using structure containment, it's just a cast!.
395848b8605Smrg */
396b8e80941Smrgstatic inline XMesaBuffer
397848b8605SmrgXMESA_BUFFER(struct gl_framebuffer *b)
398848b8605Smrg{
399848b8605Smrg   return (XMesaBuffer) b;
400848b8605Smrg}
401848b8605Smrg
402848b8605Smrg
403848b8605Smrg/* Plugged into the software rasterizer.  Try to use internal
404848b8605Smrg * swrast-style point, line and triangle functions.
405848b8605Smrg */
406848b8605Smrgextern void xmesa_choose_point( struct gl_context *ctx );
407848b8605Smrgextern void xmesa_choose_line( struct gl_context *ctx );
408848b8605Smrgextern void xmesa_choose_triangle( struct gl_context *ctx );
409848b8605Smrg
410848b8605Smrg
411848b8605Smrgextern void xmesa_register_swrast_functions( struct gl_context *ctx );
412848b8605Smrg
413848b8605Smrg
414848b8605Smrg
415848b8605Smrg#if   defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
416848b8605Smrg#define ENABLE_EXT_timer_query 1 /* should have 64-bit GLuint64EXT */
417848b8605Smrg#else
418848b8605Smrg#define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
419848b8605Smrg#endif
420848b8605Smrg
421848b8605Smrg
422848b8605Smrg#define TEST_META_FUNCS 0
423848b8605Smrg
424848b8605Smrg
425848b8605Smrg#endif
426