Adjust mga drivers to remove redundant h file for sarea and IOCTLs
[mesa.git] / src / mesa / drivers / x11 / xm_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 * This file contains the implementations of all the XMesa* functions.
27 *
28 *
29 * NOTES:
30 *
31 * The window coordinate system origin (0,0) is in the lower-left corner
32 * of the window. X11's window coordinate origin is in the upper-left
33 * corner of the window. Therefore, most drawing functions in this
34 * file have to flip Y coordinates.
35 *
36 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
37 * in support for the MIT Shared Memory extension. If enabled, when you
38 * use an Ximage for the back buffer in double buffered mode, the "swap"
39 * operation will be faster. You must also link with -lXext.
40 *
41 * Byte swapping: If the Mesa host and the X display use a different
42 * byte order then there's some trickiness to be aware of when using
43 * XImages. The byte ordering used for the XImage is that of the X
44 * display, not the Mesa host.
45 * The color-to-pixel encoding for True/DirectColor must be done
46 * according to the display's visual red_mask, green_mask, and blue_mask.
47 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
48 * do byte swapping if needed. If one wants to directly "poke" the pixel
49 * into the XImage's buffer then the pixel must be byte swapped first. In
50 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
51 * and use XPutPixel everywhere except in the implementation of
52 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
53 * instead of using XPutPixel we "poke" our values after byte-swapping
54 * the clear pixel value if needed.
55 *
56 */
57
58 #ifdef __CYGWIN__
59 #undef WIN32
60 #undef __WIN32__
61 #endif
62
63 #include "glxheader.h"
64 #include "GL/xmesa.h"
65 #include "xmesaP.h"
66 #include "context.h"
67 #include "extensions.h"
68 #include "glthread.h"
69 #include "imports.h"
70 #include "matrix.h"
71 #include "mtypes.h"
72 #include "macros.h"
73 #include "texformat.h"
74 #include "texobj.h"
75 #include "texstore.h"
76 #include "swrast/swrast.h"
77 #include "swrast_setup/swrast_setup.h"
78 #include "array_cache/acache.h"
79 #include "tnl/tnl.h"
80 #include "tnl/t_context.h"
81 #include "tnl/t_pipeline.h"
82 #include "drivers/common/driverfuncs.h"
83
84 #ifdef XFree86Server
85 #include <GL/glxtokens.h>
86 #endif
87
88 /*
89 * Global X driver lock
90 */
91 _glthread_Mutex _xmesa_lock;
92
93
94
95 /*
96 * Lookup tables for HPCR pixel format:
97 */
98 static short hpcr_rgbTbl[3][256] = {
99 {
100 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
101 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
102 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
103 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
104 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
105 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
106 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
107 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
108 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
109 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
110 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
111 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
112 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
113 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
114 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
115 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
116 },
117 {
118 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
119 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
120 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
121 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
122 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
123 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
124 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
125 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
126 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
127 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
128 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
129 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
130 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
131 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
132 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
133 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
134 },
135 {
136 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
137 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
138 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
139 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
140 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
141 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
142 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
143 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
144 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
145 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
146 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
147 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
148 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
149 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
150 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
151 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
152 }
153 };
154
155
156
157 /**********************************************************************/
158 /***** X Utility Functions *****/
159 /**********************************************************************/
160
161
162 /*
163 * Return the host's byte order as LSBFirst or MSBFirst ala X.
164 */
165 #ifndef XFree86Server
166 static int host_byte_order( void )
167 {
168 int i = 1;
169 char *cptr = (char *) &i;
170 return (*cptr==1) ? LSBFirst : MSBFirst;
171 }
172 #endif
173
174
175 /*
176 * Error handling.
177 */
178 #ifndef XFree86Server
179 static volatile int mesaXErrorFlag = 0;
180
181 static int mesaHandleXError( XMesaDisplay *dpy, XErrorEvent *event )
182 {
183 (void) dpy;
184 (void) event;
185 mesaXErrorFlag = 1;
186 return 0;
187 }
188 #endif
189
190
191 /*
192 * Check if the X Shared Memory extension is available.
193 * Return: 0 = not available
194 * 1 = shared XImage support available
195 * 2 = shared Pixmap support available also
196 */
197 #ifndef XFree86Server
198 static int check_for_xshm( XMesaDisplay *display )
199 {
200 #ifdef USE_XSHM
201 int major, minor, ignore;
202 Bool pixmaps;
203
204 if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) {
205 if (XShmQueryVersion( display, &major, &minor, &pixmaps )==True) {
206 return (pixmaps==True) ? 2 : 1;
207 }
208 else {
209 return 0;
210 }
211 }
212 else {
213 return 0;
214 }
215 #else
216 /* Can't compile XSHM support */
217 return 0;
218 #endif
219 }
220 #endif
221
222
223 /*
224 * Return the width and height of the given drawable.
225 */
226 static void get_drawable_size( XMesaDisplay *dpy, XMesaDrawable d,
227 unsigned int *width, unsigned int *height)
228 {
229 #ifdef XFree86Server
230 (void) dpy;
231 *width = d->width;
232 *height = d->height;
233 #else
234 Window root;
235 int x, y;
236 unsigned int bw, depth;
237
238 _glthread_LOCK_MUTEX(_xmesa_lock);
239 XGetGeometry( dpy, d, &root, &x, &y, width, height, &bw, &depth );
240 _glthread_UNLOCK_MUTEX(_xmesa_lock);
241 #endif
242 }
243
244
245 /*
246 * Apply gamma correction to an intensity value in [0..max]. Return the
247 * new intensity value.
248 */
249 static GLint gamma_adjust( GLfloat gamma, GLint value, GLint max )
250 {
251 if (gamma == 1.0) {
252 return value;
253 }
254 else {
255 double x = (double) value / (double) max;
256 return IROUND_POS((GLfloat) max * _mesa_pow(x, 1.0F/gamma));
257 }
258 }
259
260
261
262 /*
263 * Return the true number of bits per pixel for XImages.
264 * For example, if we request a 24-bit deep visual we may actually need/get
265 * 32bpp XImages. This function returns the appropriate bpp.
266 * Input: dpy - the X display
267 * visinfo - desribes the visual to be used for XImages
268 * Return: true number of bits per pixel for XImages
269 */
270 #ifdef XFree86Server
271
272 static int bits_per_pixel( XMesaVisual xmv )
273 {
274 const int depth = xmv->nplanes;
275 int i;
276 for (i = 0; i < screenInfo.numPixmapFormats; i++) {
277 if (screenInfo.formats[i].depth == depth)
278 return screenInfo.formats[i].bitsPerPixel;
279 }
280 return depth; /* should never get here, but this should be safe */
281 }
282
283 #else
284
285 static int bits_per_pixel( XMesaVisual xmv )
286 {
287 XMesaDisplay *dpy = xmv->display;
288 XMesaVisualInfo visinfo = xmv->visinfo;
289 XMesaImage *img;
290 int bitsPerPixel;
291 /* Create a temporary XImage */
292 img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
293 ZPixmap, 0, /*format, offset*/
294 (char*) MALLOC(8), /*data*/
295 1, 1, /*width, height*/
296 32, /*bitmap_pad*/
297 0 /*bytes_per_line*/
298 );
299 assert(img);
300 /* grab the bits/pixel value */
301 bitsPerPixel = img->bits_per_pixel;
302 /* free the XImage */
303 FREE( img->data );
304 img->data = NULL;
305 XMesaDestroyImage( img );
306 return bitsPerPixel;
307 }
308 #endif
309
310
311
312 /*
313 * Determine if a given X window ID is valid (window exists).
314 * Do this by calling XGetWindowAttributes() for the window and
315 * checking if we catch an X error.
316 * Input: dpy - the display
317 * win - the window to check for existance
318 * Return: GL_TRUE - window exists
319 * GL_FALSE - window doesn't exist
320 */
321 #ifndef XFree86Server
322 static GLboolean WindowExistsFlag;
323
324 static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr )
325 {
326 (void) dpy;
327 if (xerr->error_code == BadWindow) {
328 WindowExistsFlag = GL_FALSE;
329 }
330 return 0;
331 }
332
333 static GLboolean window_exists( XMesaDisplay *dpy, Window win )
334 {
335 XWindowAttributes wa;
336 int (*old_handler)( XMesaDisplay*, XErrorEvent* );
337 WindowExistsFlag = GL_TRUE;
338 old_handler = XSetErrorHandler(window_exists_err_handler);
339 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
340 XSetErrorHandler(old_handler);
341 return WindowExistsFlag;
342 }
343 #endif
344
345
346
347 /**********************************************************************/
348 /***** Linked list of XMesaBuffers *****/
349 /**********************************************************************/
350
351 static XMesaBuffer XMesaBufferList = NULL;
352
353
354 /* Allocate a new XMesaBuffer, add to linked list */
355 static XMesaBuffer alloc_xmesa_buffer(void)
356 {
357 XMesaBuffer b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
358 if (b) {
359 b->Next = XMesaBufferList;
360 XMesaBufferList = b;
361 }
362 return b;
363 }
364
365
366 /*
367 * Find an XMesaBuffer by matching X display and colormap but NOT matching
368 * the notThis buffer.
369 */
370 static XMesaBuffer find_xmesa_buffer(XMesaDisplay *dpy,
371 XMesaColormap cmap,
372 XMesaBuffer notThis)
373 {
374 XMesaBuffer b;
375 for (b=XMesaBufferList; b; b=b->Next) {
376 if (b->display==dpy && b->cmap==cmap && b!=notThis) {
377 return b;
378 }
379 }
380 return NULL;
381 }
382
383
384 /*
385 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
386 * entries.
387 */
388 static void free_xmesa_buffer(int client, XMesaBuffer buffer)
389 {
390 XMesaBuffer prev = NULL, b;
391 (void) client;
392 for (b=XMesaBufferList; b; b=b->Next) {
393 if (b==buffer) {
394 /* unlink bufer from list */
395 if (prev)
396 prev->Next = buffer->Next;
397 else
398 XMesaBufferList = buffer->Next;
399 /* Check to free X colors */
400 if (buffer->num_alloced>0) {
401 /* If no other buffer uses this X colormap then free the colors. */
402 if (!find_xmesa_buffer(buffer->display, buffer->cmap, buffer)) {
403 #ifdef XFree86Server
404 (void)FreeColors(buffer->cmap, client,
405 buffer->num_alloced, buffer->alloced_colors,
406 0);
407 #else
408 XFreeColors(buffer->display, buffer->cmap,
409 buffer->alloced_colors, buffer->num_alloced, 0);
410 #endif
411 }
412 }
413
414 _mesa_free_framebuffer_data(&buffer->mesa_buffer);
415 FREE(buffer);
416
417 return;
418 }
419 /* continue search */
420 prev = b;
421 }
422 /* buffer not found in XMesaBufferList */
423 _mesa_problem(NULL,"free_xmesa_buffer() - buffer not found\n");
424 }
425
426
427 /* Copy X color table stuff from one XMesaBuffer to another. */
428 static void copy_colortable_info(XMesaBuffer dst, const XMesaBuffer src)
429 {
430 MEMCPY(dst->color_table, src->color_table, sizeof(src->color_table));
431 MEMCPY(dst->pixel_to_r, src->pixel_to_r, sizeof(src->pixel_to_r));
432 MEMCPY(dst->pixel_to_g, src->pixel_to_g, sizeof(src->pixel_to_g));
433 MEMCPY(dst->pixel_to_b, src->pixel_to_b, sizeof(src->pixel_to_b));
434 dst->num_alloced = src->num_alloced;
435 MEMCPY(dst->alloced_colors, src->alloced_colors,
436 sizeof(src->alloced_colors));
437 }
438
439
440
441 /**********************************************************************/
442 /***** Misc Private Functions *****/
443 /**********************************************************************/
444
445
446 /*
447 * Return number of bits set in n.
448 */
449 static int bitcount( unsigned long n )
450 {
451 int bits;
452 for (bits=0; n>0; n=n>>1) {
453 if (n&1) {
454 bits++;
455 }
456 }
457 return bits;
458 }
459
460
461
462 /*
463 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
464 * Return: GL_TRUE if success, GL_FALSE if error
465 */
466 #ifndef XFree86Server
467 static GLboolean alloc_shm_back_buffer( XMesaBuffer b )
468 {
469 #ifdef USE_XSHM
470 /*
471 * We have to do a _lot_ of error checking here to be sure we can
472 * really use the XSHM extension. It seems different servers trigger
473 * errors at different points if the extension won't work. Therefore
474 * we have to be very careful...
475 */
476 GC gc;
477 int (*old_handler)( XMesaDisplay *, XErrorEvent * );
478
479 b->backimage = XShmCreateImage( b->xm_visual->display,
480 b->xm_visual->visinfo->visual,
481 b->xm_visual->visinfo->depth,
482 ZPixmap, NULL, &b->shminfo,
483 b->width, b->height );
484 if (b->backimage == NULL) {
485 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
486 b->shm = 0;
487 return GL_FALSE;
488 }
489
490 b->shminfo.shmid = shmget( IPC_PRIVATE, b->backimage->bytes_per_line
491 * b->backimage->height, IPC_CREAT|0777 );
492 if (b->shminfo.shmid < 0) {
493 _mesa_warning(NULL, "shmget failed while allocating back buffer");
494 XDestroyImage( b->backimage );
495 b->backimage = NULL;
496 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmget), disabling.");
497 b->shm = 0;
498 return GL_FALSE;
499 }
500
501 b->shminfo.shmaddr = b->backimage->data
502 = (char*)shmat( b->shminfo.shmid, 0, 0 );
503 if (b->shminfo.shmaddr == (char *) -1) {
504 _mesa_warning(NULL, "shmat() failed while allocating back buffer");
505 XDestroyImage( b->backimage );
506 shmctl( b->shminfo.shmid, IPC_RMID, 0 );
507 b->backimage = NULL;
508 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmat), disabling.");
509 b->shm = 0;
510 return GL_FALSE;
511 }
512
513 b->shminfo.readOnly = False;
514 mesaXErrorFlag = 0;
515 old_handler = XSetErrorHandler( mesaHandleXError );
516 /* This may trigger the X protocol error we're ready to catch: */
517 XShmAttach( b->xm_visual->display, &b->shminfo );
518 XSync( b->xm_visual->display, False );
519
520 if (mesaXErrorFlag) {
521 /* we are on a remote display, this error is normal, don't print it */
522 XFlush( b->xm_visual->display );
523 mesaXErrorFlag = 0;
524 XDestroyImage( b->backimage );
525 shmdt( b->shminfo.shmaddr );
526 shmctl( b->shminfo.shmid, IPC_RMID, 0 );
527 b->backimage = NULL;
528 b->shm = 0;
529 (void) XSetErrorHandler( old_handler );
530 return GL_FALSE;
531 }
532
533 shmctl( b->shminfo.shmid, IPC_RMID, 0 ); /* nobody else needs it */
534
535 /* Finally, try an XShmPutImage to be really sure the extension works */
536 gc = XCreateGC( b->xm_visual->display, b->frontbuffer, 0, NULL );
537 XShmPutImage( b->xm_visual->display, b->frontbuffer, gc,
538 b->backimage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False );
539 XSync( b->xm_visual->display, False );
540 XFreeGC( b->xm_visual->display, gc );
541 (void) XSetErrorHandler( old_handler );
542 if (mesaXErrorFlag) {
543 XFlush( b->xm_visual->display );
544 mesaXErrorFlag = 0;
545 XDestroyImage( b->backimage );
546 shmdt( b->shminfo.shmaddr );
547 shmctl( b->shminfo.shmid, IPC_RMID, 0 );
548 b->backimage = NULL;
549 b->shm = 0;
550 return GL_FALSE;
551 }
552
553 if (b->backimage) {
554 int height = b->backimage->height;
555 /* Needed by PIXELADDR1 macro */
556 b->ximage_width1 = b->backimage->bytes_per_line;
557 b->ximage_origin1 = (GLubyte *) b->backimage->data
558 + b->ximage_width1 * (height-1);
559 /* Needed by PIXELADDR2 macro */
560 b->ximage_width2 = b->backimage->bytes_per_line / 2;
561 b->ximage_origin2 = (GLushort *) b->backimage->data
562 + b->ximage_width2 * (height-1);
563 /* Needed by PIXELADDR3 macro */
564 b->ximage_width3 = b->backimage->bytes_per_line;
565 b->ximage_origin3 = (GLubyte *) b->backimage->data
566 + b->ximage_width3 * (height-1);
567 /* Needed by PIXELADDR4 macro */
568 b->ximage_width4 = b->backimage->width;
569 b->ximage_origin4 = (GLuint *) b->backimage->data
570 + b->ximage_width4 * (height-1);
571 }
572
573 return GL_TRUE;
574 #else
575 /* Can't compile XSHM support */
576 return GL_FALSE;
577 #endif
578 }
579 #endif
580
581
582
583 /*
584 * Setup an off-screen pixmap or Ximage to use as the back buffer.
585 * Input: b - the X/Mesa buffer
586 */
587 void xmesa_alloc_back_buffer( XMesaBuffer b )
588 {
589 if (b->db_state==BACK_XIMAGE) {
590 /* Deallocate the old backimage, if any */
591 if (b->backimage) {
592 #if defined(USE_XSHM) && !defined(XFree86Server)
593 if (b->shm) {
594 XShmDetach( b->xm_visual->display, &b->shminfo );
595 XDestroyImage( b->backimage );
596 shmdt( b->shminfo.shmaddr );
597 }
598 else
599 #endif
600 XMesaDestroyImage( b->backimage );
601 b->backimage = NULL;
602 }
603
604 /* Allocate new back buffer */
605 #ifdef XFree86Server
606 {
607 /* Allocate a regular XImage for the back buffer. */
608 b->backimage = XMesaCreateImage(b->xm_visual->BitsPerPixel,
609 b->width, b->height, NULL);
610 #else
611 if (b->shm==0 || alloc_shm_back_buffer(b)==GL_FALSE) {
612 /* Allocate a regular XImage for the back buffer. */
613 b->backimage = XCreateImage( b->xm_visual->display,
614 b->xm_visual->visinfo->visual,
615 GET_VISUAL_DEPTH(b->xm_visual),
616 ZPixmap, 0, /* format, offset */
617 NULL, b->width, b->height,
618 8, 0 ); /* pad, bytes_per_line */
619 #endif
620 if (!b->backimage) {
621 _mesa_warning(NULL, "alloc_back_buffer: XCreateImage failed.");
622 }
623 b->backimage->data = (char *) MALLOC( b->backimage->height
624 * b->backimage->bytes_per_line );
625 if (!b->backimage->data) {
626 _mesa_warning(NULL, "alloc_back_buffer: MALLOC failed.");
627 XMesaDestroyImage( b->backimage );
628 b->backimage = NULL;
629 }
630 }
631 b->backpixmap = None;
632 }
633 else if (b->db_state==BACK_PIXMAP) {
634 XMesaPixmap old_pixmap = b->backpixmap;
635 /* Free the old back pixmap */
636 if (b->backpixmap) {
637 XMesaFreePixmap( b->xm_visual->display, b->backpixmap );
638 }
639 /* Allocate new back pixmap */
640 b->backpixmap = XMesaCreatePixmap( b->xm_visual->display, b->frontbuffer,
641 b->width, b->height,
642 GET_VISUAL_DEPTH(b->xm_visual) );
643 b->backimage = NULL;
644 /* update other references to backpixmap */
645 if (b->buffer==(XMesaDrawable)old_pixmap) {
646 b->buffer = (XMesaDrawable)b->backpixmap;
647 }
648 }
649 }
650
651
652
653 /*
654 * A replacement for XAllocColor. This function should never
655 * fail to allocate a color. When XAllocColor fails, we return
656 * the nearest matching color. If we have to allocate many colors
657 * this function isn't too efficient; the XQueryColors() could be
658 * done just once.
659 * Written by Michael Pichler, Brian Paul, Mark Kilgard
660 * Input: dpy - X display
661 * cmap - X colormap
662 * cmapSize - size of colormap
663 * In/Out: color - the XColor struct
664 * Output: exact - 1=exact color match, 0=closest match
665 * alloced - 1=XAlloc worked, 0=XAlloc failed
666 */
667 static void
668 noFaultXAllocColor( int client,
669 XMesaDisplay *dpy,
670 XMesaColormap cmap,
671 int cmapSize,
672 XMesaColor *color,
673 int *exact, int *alloced )
674 {
675 #ifdef XFree86Server
676 Pixel *ppixIn;
677 xrgb *ctable;
678 #else
679 /* we'll try to cache ctable for better remote display performance */
680 static Display *prevDisplay = NULL;
681 static XMesaColormap prevCmap = 0;
682 static int prevCmapSize = 0;
683 static XMesaColor *ctable = NULL;
684 #endif
685 XMesaColor subColor;
686 int i, bestmatch;
687 double mindist; /* 3*2^16^2 exceeds long int precision. */
688
689 (void) client;
690
691 /* First try just using XAllocColor. */
692 #ifdef XFree86Server
693 if (AllocColor(cmap,
694 &color->red, &color->green, &color->blue,
695 &color->pixel,
696 client) == Success) {
697 #else
698 if (XAllocColor(dpy, cmap, color)) {
699 #endif
700 *exact = 1;
701 *alloced = 1;
702 return;
703 }
704
705 /* Alloc failed, search for closest match */
706
707 /* Retrieve color table entries. */
708 /* XXX alloca candidate. */
709 #ifdef XFree86Server
710 ppixIn = (Pixel *) MALLOC(cmapSize * sizeof(Pixel));
711 ctable = (xrgb *) MALLOC(cmapSize * sizeof(xrgb));
712 for (i = 0; i < cmapSize; i++) {
713 ppixIn[i] = i;
714 }
715 QueryColors(cmap, cmapSize, ppixIn, ctable);
716 #else
717 if (prevDisplay != dpy || prevCmap != cmap
718 || prevCmapSize != cmapSize || !ctable) {
719 /* free previously cached color table */
720 if (ctable)
721 FREE(ctable);
722 /* Get the color table from X */
723 ctable = (XMesaColor *) MALLOC(cmapSize * sizeof(XMesaColor));
724 assert(ctable);
725 for (i = 0; i < cmapSize; i++) {
726 ctable[i].pixel = i;
727 }
728 XQueryColors(dpy, cmap, ctable, cmapSize);
729 prevDisplay = dpy;
730 prevCmap = cmap;
731 prevCmapSize = cmapSize;
732 }
733 #endif
734
735 /* Find best match. */
736 bestmatch = -1;
737 mindist = 0.0;
738 for (i = 0; i < cmapSize; i++) {
739 double dr = 0.30 * ((double) color->red - (double) ctable[i].red);
740 double dg = 0.59 * ((double) color->green - (double) ctable[i].green);
741 double db = 0.11 * ((double) color->blue - (double) ctable[i].blue);
742 double dist = dr * dr + dg * dg + db * db;
743 if (bestmatch < 0 || dist < mindist) {
744 bestmatch = i;
745 mindist = dist;
746 }
747 }
748
749 /* Return result. */
750 subColor.red = ctable[bestmatch].red;
751 subColor.green = ctable[bestmatch].green;
752 subColor.blue = ctable[bestmatch].blue;
753 /* Try to allocate the closest match color. This should only
754 * fail if the cell is read/write. Otherwise, we're incrementing
755 * the cell's reference count.
756 */
757 #ifdef XFree86Server
758 if (AllocColor(cmap,
759 &subColor.red, &subColor.green, &subColor.blue,
760 &subColor.pixel,
761 client) == Success) {
762 #else
763 if (XAllocColor(dpy, cmap, &subColor)) {
764 #endif
765 *alloced = 1;
766 }
767 else {
768 /* do this to work around a problem reported by Frank Ortega */
769 subColor.pixel = (unsigned long) bestmatch;
770 subColor.red = ctable[bestmatch].red;
771 subColor.green = ctable[bestmatch].green;
772 subColor.blue = ctable[bestmatch].blue;
773 subColor.flags = DoRed | DoGreen | DoBlue;
774 *alloced = 0;
775 }
776 #ifdef XFree86Server
777 FREE(ppixIn);
778 FREE(ctable);
779 #else
780 /* don't free table, save it for next time */
781 #endif
782
783 *color = subColor;
784 *exact = 0;
785 }
786
787
788
789
790 /*
791 * Do setup for PF_GRAYSCALE pixel format.
792 * Note that buffer may be NULL.
793 */
794 static GLboolean setup_grayscale( int client, XMesaVisual v,
795 XMesaBuffer buffer, XMesaColormap cmap )
796 {
797 if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) {
798 return GL_FALSE;
799 }
800
801 if (buffer) {
802 XMesaBuffer prevBuffer;
803
804 if (!cmap) {
805 return GL_FALSE;
806 }
807
808 prevBuffer = find_xmesa_buffer(v->display, cmap, buffer);
809 if (prevBuffer &&
810 (buffer->xm_visual->mesa_visual.rgbMode ==
811 prevBuffer->xm_visual->mesa_visual.rgbMode)) {
812 /* Copy colormap stuff from previous XMesaBuffer which uses same
813 * X colormap. Do this to avoid time spent in noFaultXAllocColor.
814 */
815 copy_colortable_info(buffer, prevBuffer);
816 }
817 else {
818 /* Allocate 256 shades of gray */
819 int gray;
820 int colorsfailed = 0;
821 for (gray=0;gray<256;gray++) {
822 GLint r = gamma_adjust( v->RedGamma, gray, 255 );
823 GLint g = gamma_adjust( v->GreenGamma, gray, 255 );
824 GLint b = gamma_adjust( v->BlueGamma, gray, 255 );
825 int exact, alloced;
826 XMesaColor xcol;
827 xcol.red = (r << 8) | r;
828 xcol.green = (g << 8) | g;
829 xcol.blue = (b << 8) | b;
830 noFaultXAllocColor( client, v->display,
831 cmap, GET_COLORMAP_SIZE(v),
832 &xcol, &exact, &alloced );
833 if (!exact) {
834 colorsfailed++;
835 }
836 if (alloced) {
837 assert(buffer->num_alloced<256);
838 buffer->alloced_colors[buffer->num_alloced] = xcol.pixel;
839 buffer->num_alloced++;
840 }
841
842 /*OLD
843 assert(gray < 576);
844 buffer->color_table[gray*3+0] = xcol.pixel;
845 buffer->color_table[gray*3+1] = xcol.pixel;
846 buffer->color_table[gray*3+2] = xcol.pixel;
847 assert(xcol.pixel < 65536);
848 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
849 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
850 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
851 */
852 buffer->color_table[gray] = xcol.pixel;
853 assert(xcol.pixel < 65536);
854 buffer->pixel_to_r[xcol.pixel] = gray;
855 buffer->pixel_to_g[xcol.pixel] = gray;
856 buffer->pixel_to_b[xcol.pixel] = gray;
857 }
858
859 if (colorsfailed && _mesa_getenv("MESA_DEBUG")) {
860 _mesa_warning(NULL,
861 "Note: %d out of 256 needed colors do not match exactly.\n",
862 colorsfailed );
863 }
864 }
865 }
866
867 v->dithered_pf = PF_Grayscale;
868 v->undithered_pf = PF_Grayscale;
869 return GL_TRUE;
870 }
871
872
873
874 /*
875 * Setup RGB rendering for a window with a PseudoColor, StaticColor,
876 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
877 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
878 * color. While this function was originally designed just for 8-bit
879 * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
880 * Dithering code contributed by Bob Mercier.
881 */
882 static GLboolean setup_dithered_color( int client, XMesaVisual v,
883 XMesaBuffer buffer, XMesaColormap cmap )
884 {
885 if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) {
886 return GL_FALSE;
887 }
888
889 if (buffer) {
890 XMesaBuffer prevBuffer;
891
892 if (!cmap) {
893 return GL_FALSE;
894 }
895
896 prevBuffer = find_xmesa_buffer(v->display, cmap, buffer);
897 if (prevBuffer &&
898 (buffer->xm_visual->mesa_visual.rgbMode ==
899 prevBuffer->xm_visual->mesa_visual.rgbMode)) {
900 /* Copy colormap stuff from previous, matching XMesaBuffer.
901 * Do this to avoid time spent in noFaultXAllocColor.
902 */
903 copy_colortable_info(buffer, prevBuffer);
904 }
905 else {
906 /* Allocate X colors and initialize color_table[], red_table[], etc */
907 int r, g, b, i;
908 int colorsfailed = 0;
909 for (r = 0; r < DITH_R; r++) {
910 for (g = 0; g < DITH_G; g++) {
911 for (b = 0; b < DITH_B; b++) {
912 XMesaColor xcol;
913 int exact, alloced;
914 xcol.red =gamma_adjust(v->RedGamma, r*65535/(DITH_R-1),65535);
915 xcol.green=gamma_adjust(v->GreenGamma, g*65535/(DITH_G-1),65535);
916 xcol.blue =gamma_adjust(v->BlueGamma, b*65535/(DITH_B-1),65535);
917 noFaultXAllocColor( client, v->display,
918 cmap, GET_COLORMAP_SIZE(v),
919 &xcol, &exact, &alloced );
920 if (!exact) {
921 colorsfailed++;
922 }
923 if (alloced) {
924 assert(buffer->num_alloced<256);
925 buffer->alloced_colors[buffer->num_alloced] = xcol.pixel;
926 buffer->num_alloced++;
927 }
928 i = DITH_MIX( r, g, b );
929 assert(i < 576);
930 buffer->color_table[i] = xcol.pixel;
931 assert(xcol.pixel < 65536);
932 buffer->pixel_to_r[xcol.pixel] = r * 255 / (DITH_R-1);
933 buffer->pixel_to_g[xcol.pixel] = g * 255 / (DITH_G-1);
934 buffer->pixel_to_b[xcol.pixel] = b * 255 / (DITH_B-1);
935 }
936 }
937 }
938
939 if (colorsfailed && _mesa_getenv("MESA_DEBUG")) {
940 _mesa_warning(NULL,
941 "Note: %d out of %d needed colors do not match exactly.\n",
942 colorsfailed, DITH_R * DITH_G * DITH_B );
943 }
944 }
945 }
946
947 v->dithered_pf = PF_Dither;
948 v->undithered_pf = PF_Lookup;
949 return GL_TRUE;
950 }
951
952
953 /*
954 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
955 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
956 * Special dithering tables have to be initialized.
957 */
958 static void setup_8bit_hpcr( XMesaVisual v )
959 {
960 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
961 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
962 * on the root window AND the colormap obtainable by XGetRGBColormaps
963 * for that atom must be set on the window. (see also tkInitWindow)
964 * If that colormap is not set, the output will look stripy.
965 */
966
967 /* Setup color tables with gamma correction */
968 int i;
969 double g;
970
971 g = 1.0 / v->RedGamma;
972 for (i=0; i<256; i++) {
973 GLint red = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[0][i]/255.0, g ));
974 v->hpcr_rgbTbl[0][i] = CLAMP( red, 16, 239 );
975 }
976
977 g = 1.0 / v->GreenGamma;
978 for (i=0; i<256; i++) {
979 GLint green = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[1][i]/255.0, g ));
980 v->hpcr_rgbTbl[1][i] = CLAMP( green, 16, 239 );
981 }
982
983 g = 1.0 / v->BlueGamma;
984 for (i=0; i<256; i++) {
985 GLint blue = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[2][i]/255.0, g ));
986 v->hpcr_rgbTbl[2][i] = CLAMP( blue, 32, 223 );
987 }
988 v->undithered_pf = PF_HPCR; /* can't really disable dithering for now */
989 v->dithered_pf = PF_HPCR;
990
991 /* which method should I use to clear */
992 /* GL_FALSE: keep the ordinary method */
993 /* GL_TRUE : clear with dither pattern */
994 v->hpcr_clear_flag = _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE : GL_FALSE;
995
996 if (v->hpcr_clear_flag) {
997 v->hpcr_clear_pixmap = XMesaCreatePixmap(v->display,
998 DefaultRootWindow(v->display),
999 16, 2, 8);
1000 #ifndef XFree86Server
1001 v->hpcr_clear_ximage = XGetImage(v->display, v->hpcr_clear_pixmap,
1002 0, 0, 16, 2, AllPlanes, ZPixmap);
1003 #endif
1004 }
1005 }
1006
1007
1008 /*
1009 * Setup RGB rendering for a window with a True/DirectColor visual.
1010 */
1011 static void setup_truecolor( XMesaVisual v, XMesaBuffer buffer,
1012 XMesaColormap cmap )
1013 {
1014 unsigned long rmask, gmask, bmask;
1015 (void) buffer;
1016 (void) cmap;
1017
1018 /* Compute red multiplier (mask) and bit shift */
1019 v->rshift = 0;
1020 rmask = GET_REDMASK(v);
1021 while ((rmask & 1)==0) {
1022 v->rshift++;
1023 rmask = rmask >> 1;
1024 }
1025
1026 /* Compute green multiplier (mask) and bit shift */
1027 v->gshift = 0;
1028 gmask = GET_GREENMASK(v);
1029 while ((gmask & 1)==0) {
1030 v->gshift++;
1031 gmask = gmask >> 1;
1032 }
1033
1034 /* Compute blue multiplier (mask) and bit shift */
1035 v->bshift = 0;
1036 bmask = GET_BLUEMASK(v);
1037 while ((bmask & 1)==0) {
1038 v->bshift++;
1039 bmask = bmask >> 1;
1040 }
1041
1042 /*
1043 * Compute component-to-pixel lookup tables and dithering kernel
1044 */
1045 {
1046 static GLubyte kernel[16] = {
1047 0*16, 8*16, 2*16, 10*16,
1048 12*16, 4*16, 14*16, 6*16,
1049 3*16, 11*16, 1*16, 9*16,
1050 15*16, 7*16, 13*16, 5*16,
1051 };
1052 GLint rBits = bitcount(rmask);
1053 GLint gBits = bitcount(gmask);
1054 GLint bBits = bitcount(bmask);
1055 GLint maxBits;
1056 GLuint i;
1057
1058 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
1059 for (i=0; i<=rmask; i++)
1060 v->PixelToR[i] = (unsigned char) ((i * 255) / rmask);
1061 for (i=0; i<=gmask; i++)
1062 v->PixelToG[i] = (unsigned char) ((i * 255) / gmask);
1063 for (i=0; i<=bmask; i++)
1064 v->PixelToB[i] = (unsigned char) ((i * 255) / bmask);
1065
1066 /* convert RGB values from [0,255] to pixel components */
1067
1068 for (i=0;i<256;i++) {
1069 GLint r = gamma_adjust(v->RedGamma, i, 255);
1070 GLint g = gamma_adjust(v->GreenGamma, i, 255);
1071 GLint b = gamma_adjust(v->BlueGamma, i, 255);
1072 v->RtoPixel[i] = (r >> (8-rBits)) << v->rshift;
1073 v->GtoPixel[i] = (g >> (8-gBits)) << v->gshift;
1074 v->BtoPixel[i] = (b >> (8-bBits)) << v->bshift;
1075 }
1076 /* overflow protection */
1077 for (i=256;i<512;i++) {
1078 v->RtoPixel[i] = v->RtoPixel[255];
1079 v->GtoPixel[i] = v->GtoPixel[255];
1080 v->BtoPixel[i] = v->BtoPixel[255];
1081 }
1082
1083 /* setup dithering kernel */
1084 maxBits = rBits;
1085 if (gBits > maxBits) maxBits = gBits;
1086 if (bBits > maxBits) maxBits = bBits;
1087 for (i=0;i<16;i++) {
1088 v->Kernel[i] = kernel[i] >> maxBits;
1089 }
1090
1091 v->undithered_pf = PF_Truecolor;
1092 v->dithered_pf = (GET_VISUAL_DEPTH(v)<24) ? PF_Dither_True : PF_Truecolor;
1093 }
1094
1095 /*
1096 * Now check for TrueColor visuals which we can optimize.
1097 */
1098 if ( GET_REDMASK(v) ==0x0000ff
1099 && GET_GREENMASK(v)==0x00ff00
1100 && GET_BLUEMASK(v) ==0xff0000
1101 && CHECK_BYTE_ORDER(v)
1102 && v->BitsPerPixel==32
1103 && sizeof(GLuint)==4
1104 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
1105 /* common 32 bpp config used on SGI, Sun */
1106 v->undithered_pf = v->dithered_pf = PF_8A8B8G8R;
1107 }
1108 else if (GET_REDMASK(v) ==0xff0000
1109 && GET_GREENMASK(v)==0x00ff00
1110 && GET_BLUEMASK(v) ==0x0000ff
1111 && CHECK_BYTE_ORDER(v)
1112 && v->BitsPerPixel==32
1113 && sizeof(GLuint)==4
1114 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
1115 /* common 32 bpp config used on Linux, HP, IBM */
1116 v->undithered_pf = v->dithered_pf = PF_8R8G8B;
1117 }
1118 else if (GET_REDMASK(v) ==0xff0000
1119 && GET_GREENMASK(v)==0x00ff00
1120 && GET_BLUEMASK(v) ==0x0000ff
1121 && CHECK_BYTE_ORDER(v)
1122 && v->BitsPerPixel==24
1123 && sizeof(GLuint)==4
1124 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
1125 /* common packed 24 bpp config used on Linux */
1126 v->undithered_pf = v->dithered_pf = PF_8R8G8B24;
1127 }
1128 else if (GET_REDMASK(v) ==0xf800
1129 && GET_GREENMASK(v)==0x07e0
1130 && GET_BLUEMASK(v) ==0x001f
1131 && CHECK_BYTE_ORDER(v)
1132 && v->BitsPerPixel==16
1133 && sizeof(GLushort)==2
1134 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
1135 /* 5-6-5 color weight on common PC VGA boards */
1136 v->undithered_pf = PF_5R6G5B;
1137 v->dithered_pf = PF_Dither_5R6G5B;
1138 }
1139 else if (GET_REDMASK(v) ==0xe0
1140 && GET_GREENMASK(v)==0x1c
1141 && GET_BLUEMASK(v) ==0x03
1142 && CHECK_FOR_HPCR(v)) {
1143 setup_8bit_hpcr( v );
1144 }
1145 }
1146
1147
1148
1149 /*
1150 * Setup RGB rendering for a window with a monochrome visual.
1151 */
1152 static void setup_monochrome( XMesaVisual v, XMesaBuffer b )
1153 {
1154 (void) b;
1155 v->dithered_pf = v->undithered_pf = PF_1Bit;
1156 /* if black=1 then we must flip pixel values */
1157 v->bitFlip = (GET_BLACK_PIXEL(v) != 0);
1158 }
1159
1160
1161
1162 /*
1163 * When a context is "made current" for the first time, we can finally
1164 * finish initializing the context's visual and buffer information.
1165 * Input: v - the XMesaVisual to initialize
1166 * b - the XMesaBuffer to initialize (may be NULL)
1167 * rgb_flag - TRUE = RGBA mode, FALSE = color index mode
1168 * window - the window/pixmap we're rendering into
1169 * cmap - the colormap associated with the window/pixmap
1170 * Return: GL_TRUE=success, GL_FALSE=failure
1171 */
1172 static GLboolean initialize_visual_and_buffer( int client,
1173 XMesaVisual v,
1174 XMesaBuffer b,
1175 GLboolean rgb_flag,
1176 XMesaDrawable window,
1177 XMesaColormap cmap )
1178 {
1179 #ifndef XFree86Server
1180 XGCValues gcvalues;
1181 #endif
1182
1183 if (b) {
1184 assert(b->xm_visual == v);
1185 }
1186
1187 /* Save true bits/pixel */
1188 v->BitsPerPixel = bits_per_pixel(v);
1189 assert(v->BitsPerPixel > 0);
1190
1191
1192 if (rgb_flag==GL_FALSE) {
1193 /* COLOR-INDEXED WINDOW:
1194 * Even if the visual is TrueColor or DirectColor we treat it as
1195 * being color indexed. This is weird but might be useful to someone.
1196 */
1197 v->dithered_pf = v->undithered_pf = PF_Index;
1198 v->mesa_visual.indexBits = GET_VISUAL_DEPTH(v);
1199 }
1200 else {
1201 /* RGB WINDOW:
1202 * We support RGB rendering into almost any kind of visual.
1203 */
1204 const int xclass = v->mesa_visual.visualType;
1205 if (xclass==GLX_TRUE_COLOR || xclass==GLX_DIRECT_COLOR) {
1206 setup_truecolor( v, b, cmap );
1207 }
1208 else if (xclass==GLX_STATIC_GRAY && GET_VISUAL_DEPTH(v)==1) {
1209 setup_monochrome( v, b );
1210 }
1211 else if (xclass==GLX_GRAY_SCALE || xclass==GLX_STATIC_GRAY) {
1212 if (!setup_grayscale( client, v, b, cmap )) {
1213 return GL_FALSE;
1214 }
1215 }
1216 else if ((xclass==GLX_PSEUDO_COLOR || xclass==GLX_STATIC_COLOR)
1217 && GET_VISUAL_DEPTH(v)>=4 && GET_VISUAL_DEPTH(v)<=16) {
1218 if (!setup_dithered_color( client, v, b, cmap )) {
1219 return GL_FALSE;
1220 }
1221 }
1222 else {
1223 _mesa_warning(NULL, "XMesa: RGB mode rendering not supported in given visual.");
1224 return GL_FALSE;
1225 }
1226 v->mesa_visual.indexBits = 0;
1227
1228 if (_mesa_getenv("MESA_NO_DITHER")) {
1229 v->dithered_pf = v->undithered_pf;
1230 }
1231 }
1232
1233
1234 /*
1235 * If MESA_INFO env var is set print out some debugging info
1236 * which can help Brian figure out what's going on when a user
1237 * reports bugs.
1238 */
1239 if (_mesa_getenv("MESA_INFO")) {
1240 _mesa_printf("X/Mesa visual = %p\n", (void *) v);
1241 _mesa_printf("X/Mesa dithered pf = %u\n", v->dithered_pf);
1242 _mesa_printf("X/Mesa undithered pf = %u\n", v->undithered_pf);
1243 _mesa_printf("X/Mesa level = %d\n", v->mesa_visual.level);
1244 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v));
1245 _mesa_printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
1246 }
1247
1248 if (b && window) {
1249 /* Do window-specific initializations */
1250
1251 /* Window dimensions */
1252 unsigned int w, h;
1253 get_drawable_size( v->display, window, &w, &h );
1254 b->width = w;
1255 b->height = h;
1256
1257 b->frontbuffer = window;
1258
1259 /* Setup for single/double buffering */
1260 if (v->mesa_visual.doubleBufferMode) {
1261 /* Double buffered */
1262 #ifndef XFree86Server
1263 b->shm = check_for_xshm( v->display );
1264 #endif
1265 xmesa_alloc_back_buffer( b );
1266 if (b->db_state==BACK_PIXMAP) {
1267 b->buffer = (XMesaDrawable)b->backpixmap;
1268 }
1269 else {
1270 b->buffer = XIMAGE;
1271 }
1272 }
1273 else {
1274 /* Single Buffered */
1275 b->buffer = b->frontbuffer;
1276 }
1277
1278 /* X11 graphics contexts */
1279 #ifdef XFree86Server
1280 b->gc = CreateScratchGC(v->display, window->depth);
1281 #else
1282 b->gc = XCreateGC( v->display, window, 0, NULL );
1283 #endif
1284 XMesaSetFunction( v->display, b->gc, GXcopy );
1285
1286 /* cleargc - for glClear() */
1287 #ifdef XFree86Server
1288 b->cleargc = CreateScratchGC(v->display, window->depth);
1289 #else
1290 b->cleargc = XCreateGC( v->display, window, 0, NULL );
1291 #endif
1292 XMesaSetFunction( v->display, b->cleargc, GXcopy );
1293
1294 /*
1295 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1296 * Patch contributed by Michael Pichler May 15, 1995.
1297 */
1298 #ifdef XFree86Server
1299 b->swapgc = CreateScratchGC(v->display, window->depth);
1300 {
1301 CARD32 v[1];
1302 v[0] = FALSE;
1303 dixChangeGC(NullClient, b->swapgc, GCGraphicsExposures, v, NULL);
1304 }
1305 #else
1306 gcvalues.graphics_exposures = False;
1307 b->swapgc = XCreateGC( v->display, window,
1308 GCGraphicsExposures, &gcvalues);
1309 #endif
1310 XMesaSetFunction( v->display, b->swapgc, GXcopy );
1311 /*
1312 * Set fill style and tile pixmap once for all for HPCR stuff
1313 * (instead of doing it each time in clear_color_HPCR_pixmap())
1314 * Initialize whole stuff
1315 * Patch contributed by Jacques Leroy March 8, 1998.
1316 */
1317 if (v->hpcr_clear_flag && b->buffer!=XIMAGE) {
1318 int i;
1319 for (i=0; i<16; i++)
1320 {
1321 XMesaPutPixel(v->hpcr_clear_ximage, i, 0, 0);
1322 XMesaPutPixel(v->hpcr_clear_ximage, i, 1, 0);
1323 }
1324 XMesaPutImage(b->display, (XMesaDrawable)v->hpcr_clear_pixmap,
1325 b->cleargc, v->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2);
1326 XMesaSetFillStyle( v->display, b->cleargc, FillTiled);
1327 XMesaSetTile( v->display, b->cleargc, v->hpcr_clear_pixmap );
1328 }
1329
1330 /* Initialize the row buffer XImage for use in write_color_span() */
1331 #ifdef XFree86Server
1332 b->rowimage = XMesaCreateImage(GET_VISUAL_DEPTH(v), MAX_WIDTH, 1,
1333 (char *)MALLOC(MAX_WIDTH*4));
1334 #else
1335 b->rowimage = XCreateImage( v->display,
1336 v->visinfo->visual,
1337 v->visinfo->depth,
1338 ZPixmap, 0, /*format, offset*/
1339 (char*) MALLOC(MAX_WIDTH*4), /*data*/
1340 MAX_WIDTH, 1, /*width, height*/
1341 32, /*bitmap_pad*/
1342 0 /*bytes_per_line*/ );
1343 #endif
1344 }
1345
1346 return GL_TRUE;
1347 }
1348
1349
1350
1351 /*
1352 * Convert an RGBA color to a pixel value.
1353 */
1354 unsigned long
1355 xmesa_color_to_pixel( XMesaContext xmesa, GLubyte r, GLubyte g, GLubyte b, GLubyte a,
1356 GLuint pixelFormat)
1357 {
1358 switch (pixelFormat) {
1359 case PF_Index:
1360 return 0;
1361 case PF_Truecolor:
1362 {
1363 unsigned long p;
1364 PACK_TRUECOLOR( p, r, g, b );
1365 return p;
1366 }
1367 case PF_8A8B8G8R:
1368 return PACK_8A8B8G8R( r, g, b, a );
1369 case PF_8R8G8B:
1370 /* fall through */
1371 case PF_8R8G8B24:
1372 return PACK_8R8G8B( r, g, b );
1373 case PF_5R6G5B:
1374 return PACK_5R6G5B( r, g, b );
1375 case PF_Dither:
1376 {
1377 DITHER_SETUP;
1378 return DITHER( 1, 0, r, g, b );
1379 }
1380 case PF_1Bit:
1381 /* 382 = (3*255)/2 */
1382 return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip;
1383 case PF_HPCR:
1384 return DITHER_HPCR(1, 1, r, g, b);
1385 case PF_Lookup:
1386 {
1387 LOOKUP_SETUP;
1388 return LOOKUP( r, g, b );
1389 }
1390 case PF_Grayscale:
1391 return GRAY_RGB( r, g, b );
1392 case PF_Dither_True:
1393 /* fall through */
1394 case PF_Dither_5R6G5B:
1395 {
1396 unsigned long p;
1397 PACK_TRUEDITHER(p, 1, 0, r, g, b);
1398 return p;
1399 }
1400 default:
1401 _mesa_problem(NULL, "Bad pixel format in xmesa_color_to_pixel");
1402 }
1403 return 0;
1404 }
1405
1406
1407 #define NUM_VISUAL_TYPES 6
1408
1409 /**
1410 * Convert an X visual type to a GLX visual type.
1411 *
1412 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
1413 * to be converted.
1414 * \return If \c visualType is a valid X visual type, a GLX visual type will
1415 * be returned. Otherwise \c GLX_NONE will be returned.
1416 *
1417 * \note
1418 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
1419 * DRI CVS tree.
1420 */
1421 static GLint
1422 xmesa_convert_from_x_visual_type( int visualType )
1423 {
1424 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
1425 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
1426 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
1427 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
1428 };
1429
1430 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
1431 ? glx_visual_types[ visualType ] : GLX_NONE;
1432 }
1433
1434
1435 /**********************************************************************/
1436 /***** Public Functions *****/
1437 /**********************************************************************/
1438
1439
1440 /*
1441 * Create a new X/Mesa visual.
1442 * Input: display - X11 display
1443 * visinfo - an XVisualInfo pointer
1444 * rgb_flag - GL_TRUE = RGB mode,
1445 * GL_FALSE = color index mode
1446 * alpha_flag - alpha buffer requested?
1447 * db_flag - GL_TRUE = double-buffered,
1448 * GL_FALSE = single buffered
1449 * stereo_flag - stereo visual?
1450 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1451 * GL_FALSE = use an off-screen pixmap for back buffer
1452 * depth_size - requested bits/depth values, or zero
1453 * stencil_size - requested bits/stencil values, or zero
1454 * accum_red_size - requested bits/red accum values, or zero
1455 * accum_green_size - requested bits/green accum values, or zero
1456 * accum_blue_size - requested bits/blue accum values, or zero
1457 * accum_alpha_size - requested bits/alpha accum values, or zero
1458 * num_samples - number of samples/pixel if multisampling, or zero
1459 * level - visual level, usually 0
1460 * visualCaveat - ala the GLX extension, usually GLX_NONE
1461 * Return; a new XMesaVisual or 0 if error.
1462 */
1463 XMesaVisual XMesaCreateVisual( XMesaDisplay *display,
1464 XMesaVisualInfo visinfo,
1465 GLboolean rgb_flag,
1466 GLboolean alpha_flag,
1467 GLboolean db_flag,
1468 GLboolean stereo_flag,
1469 GLboolean ximage_flag,
1470 GLint depth_size,
1471 GLint stencil_size,
1472 GLint accum_red_size,
1473 GLint accum_green_size,
1474 GLint accum_blue_size,
1475 GLint accum_alpha_size,
1476 GLint num_samples,
1477 GLint level,
1478 GLint visualCaveat )
1479 {
1480 char *gamma;
1481 XMesaVisual v;
1482 GLint red_bits, green_bits, blue_bits, alpha_bits;
1483
1484 /* For debugging only */
1485 if (_mesa_getenv("MESA_XSYNC")) {
1486 /* This makes debugging X easier.
1487 * In your debugger, set a breakpoint on _XError to stop when an
1488 * X protocol error is generated.
1489 */
1490 #ifdef XFree86Server
1491 /* NOT_NEEDED */
1492 #else
1493 XSynchronize( display, 1 );
1494 #endif
1495 }
1496
1497 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
1498 if (!v) {
1499 return NULL;
1500 }
1501
1502 /*
1503 * In the X server, NULL is passed in for the display. It will have
1504 * to be set before using this visual. See XMesaSetVisualDisplay()
1505 * below.
1506 */
1507 v->display = display;
1508
1509 /* Save a copy of the XVisualInfo struct because the user may XFREE()
1510 * the struct but we may need some of the information contained in it
1511 * at a later time.
1512 */
1513 #ifndef XFree86Server
1514 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
1515 if(!v->visinfo) {
1516 FREE(v);
1517 return NULL;
1518 }
1519 MEMCPY(v->visinfo, visinfo, sizeof(*visinfo));
1520 #endif
1521
1522 /* check for MESA_GAMMA environment variable */
1523 gamma = _mesa_getenv("MESA_GAMMA");
1524 if (gamma) {
1525 v->RedGamma = v->GreenGamma = v->BlueGamma = 0.0;
1526 sscanf( gamma, "%f %f %f", &v->RedGamma, &v->GreenGamma, &v->BlueGamma );
1527 if (v->RedGamma<=0.0) v->RedGamma = 1.0;
1528 if (v->GreenGamma<=0.0) v->GreenGamma = v->RedGamma;
1529 if (v->BlueGamma<=0.0) v->BlueGamma = v->RedGamma;
1530 }
1531 else {
1532 v->RedGamma = v->GreenGamma = v->BlueGamma = 1.0;
1533 }
1534
1535 v->ximage_flag = ximage_flag;
1536
1537 #ifdef XFree86Server
1538 /* We could calculate these values by ourselves. nplanes is either the sum
1539 * of the red, green, and blue bits or the number index bits.
1540 * ColormapEntries is either (1U << index_bits) or
1541 * (1U << max(redBits, greenBits, blueBits)).
1542 */
1543 v->nplanes = visinfo->nplanes;
1544 v->ColormapEntries = visinfo->ColormapEntries;
1545
1546 v->mesa_visual.redMask = visinfo->redMask;
1547 v->mesa_visual.greenMask = visinfo->greenMask;
1548 v->mesa_visual.blueMask = visinfo->blueMask;
1549 v->mesa_visual.visualID = visinfo->vid;
1550 v->mesa_visual.screen = 0; /* FIXME: What should be done here? */
1551 #else
1552 v->mesa_visual.redMask = visinfo->red_mask;
1553 v->mesa_visual.greenMask = visinfo->green_mask;
1554 v->mesa_visual.blueMask = visinfo->blue_mask;
1555 v->mesa_visual.visualID = visinfo->visualid;
1556 v->mesa_visual.screen = visinfo->screen;
1557 #endif
1558
1559 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
1560 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->class);
1561 #else
1562 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
1563 #endif
1564
1565 v->mesa_visual.visualRating = visualCaveat;
1566
1567 (void) initialize_visual_and_buffer( 0, v, NULL, rgb_flag, 0, 0 );
1568
1569 {
1570 const int xclass = v->mesa_visual.visualType;
1571 if (xclass==GLX_TRUE_COLOR || xclass==GLX_DIRECT_COLOR) {
1572 red_bits = bitcount(GET_REDMASK(v));
1573 green_bits = bitcount(GET_GREENMASK(v));
1574 blue_bits = bitcount(GET_BLUEMASK(v));
1575 alpha_bits = 0;
1576 }
1577 else {
1578 /* this is an approximation */
1579 int depth;
1580 depth = GET_VISUAL_DEPTH(v);
1581 red_bits = depth / 3;
1582 depth -= red_bits;
1583 green_bits = depth / 2;
1584 depth -= green_bits;
1585 blue_bits = depth;
1586 alpha_bits = 0;
1587 assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) );
1588 }
1589 }
1590
1591 if (alpha_flag && alpha_bits == 0)
1592 alpha_bits = 8;
1593
1594 _mesa_initialize_visual( &v->mesa_visual,
1595 rgb_flag, db_flag, stereo_flag,
1596 red_bits, green_bits,
1597 blue_bits, alpha_bits,
1598 v->mesa_visual.indexBits,
1599 depth_size,
1600 stencil_size,
1601 accum_red_size, accum_green_size,
1602 accum_blue_size, accum_alpha_size,
1603 0 );
1604
1605 v->mesa_visual.level = level;
1606 return v;
1607 }
1608
1609
1610 void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v )
1611 {
1612 v->display = dpy;
1613 }
1614
1615
1616 void XMesaDestroyVisual( XMesaVisual v )
1617 {
1618 #ifndef XFree86Server
1619 FREE(v->visinfo);
1620 #endif
1621 FREE(v);
1622 }
1623
1624
1625
1626 /**
1627 * Create a new XMesaContext.
1628 * \param v the XMesaVisual
1629 * \param share_list another XMesaContext with which to share display
1630 * lists or NULL if no sharing is wanted.
1631 * \return an XMesaContext or NULL if error.
1632 */
1633 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
1634 {
1635 static GLboolean firstTime = GL_TRUE;
1636 XMesaContext c;
1637 GLcontext *mesaCtx;
1638 struct dd_function_table functions;
1639 TNLcontext *tnl;
1640
1641 if (firstTime) {
1642 _glthread_INIT_MUTEX(_xmesa_lock);
1643 firstTime = GL_FALSE;
1644 }
1645
1646 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
1647 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
1648 if (!c)
1649 return NULL;
1650
1651 mesaCtx = &(c->mesa);
1652
1653 /* initialize with default driver functions, then plug in XMesa funcs */
1654 _mesa_init_driver_functions(&functions);
1655 xmesa_init_driver_functions(v, &functions);
1656 if (!_mesa_initialize_context(mesaCtx, &v->mesa_visual,
1657 share_list ? &(share_list->mesa) : (GLcontext *) NULL,
1658 &functions, (void *) c)) {
1659 FREE(c);
1660 return NULL;
1661 }
1662
1663 _mesa_enable_sw_extensions(mesaCtx);
1664 _mesa_enable_1_3_extensions(mesaCtx);
1665 _mesa_enable_1_4_extensions(mesaCtx);
1666 _mesa_enable_1_5_extensions(mesaCtx);
1667
1668 /* finish up xmesa context initializations */
1669 c->swapbytes = CHECK_BYTE_ORDER(v) ? GL_FALSE : GL_TRUE;
1670 c->xm_visual = v;
1671 c->xm_draw_buffer = NULL; /* set later by XMesaMakeCurrent */
1672 c->xm_read_buffer = NULL; /* set later by XMesaMakeCurrent */
1673 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
1674 c->display = v->display;
1675 c->pixelformat = v->dithered_pf; /* Dithering is enabled by default */
1676
1677 /* Initialize the software rasterizer and helper modules.
1678 */
1679 _swrast_CreateContext( mesaCtx );
1680 _ac_CreateContext( mesaCtx );
1681 _tnl_CreateContext( mesaCtx );
1682 _swsetup_CreateContext( mesaCtx );
1683
1684 /* tnl setup */
1685 tnl = TNL_CONTEXT(mesaCtx);
1686 tnl->Driver.RunPipeline = _tnl_run_pipeline;
1687 /* swrast setup */
1688 xmesa_register_swrast_functions( mesaCtx );
1689 _swsetup_Wakeup(mesaCtx);
1690
1691 return c;
1692 }
1693
1694
1695
1696 void XMesaDestroyContext( XMesaContext c )
1697 {
1698 GLcontext *mesaCtx = &c->mesa;
1699 #ifdef FX
1700 if (c->xm_draw_buffer && c->xm_buffer->FXctx)
1701 fxMesaDestroyContext(c->xm_draw_buffer->FXctx);
1702 #endif
1703 _swsetup_DestroyContext( mesaCtx );
1704 _swrast_DestroyContext( mesaCtx );
1705 _tnl_DestroyContext( mesaCtx );
1706 _ac_DestroyContext( mesaCtx );
1707 _mesa_free_context_data( mesaCtx );
1708 _mesa_free( c );
1709 }
1710
1711
1712
1713 /*
1714 * XXX this isn't a public function! It's a hack for the 3Dfx driver.
1715 * Create a new XMesaBuffer from an X window.
1716 * Input: v - the XMesaVisual
1717 * w - the window
1718 * c - the context
1719 * Return: new XMesaBuffer or NULL if error
1720 */
1721 XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w,
1722 XMesaContext c )
1723 {
1724 #ifndef XFree86Server
1725 XWindowAttributes attr;
1726 #endif
1727 #ifdef FX
1728 char *fxEnvVar;
1729 #endif
1730 int client = 0;
1731
1732 XMesaBuffer b = alloc_xmesa_buffer();
1733 if (!b) {
1734 return NULL;
1735 }
1736
1737 (void) c;
1738
1739 #ifdef XFree86Server
1740 client = CLIENT_ID(((XMesaDrawable)w)->id);
1741 #endif
1742
1743 assert(v);
1744
1745 #ifdef XFree86Server
1746 if (GET_VISUAL_DEPTH(v) != ((XMesaDrawable)w)->depth) {
1747 #else
1748 XGetWindowAttributes( v->display, w, &attr );
1749
1750 if (GET_VISUAL_DEPTH(v) != attr.depth) {
1751 #endif
1752 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual and window!\n");
1753 return NULL;
1754 }
1755
1756 b->xm_visual = v;
1757 b->type = WINDOW;
1758 b->display = v->display;
1759 #ifdef XFree86Server
1760 b->cmap = (ColormapPtr)LookupIDByType(wColormap(w), RT_COLORMAP);
1761 #else
1762 if (attr.colormap) {
1763 b->cmap = attr.colormap;
1764 }
1765 else {
1766 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
1767 /* this is weird, a window w/out a colormap!? */
1768 /* OK, let's just allocate a new one and hope for the best */
1769 b->cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
1770 }
1771 #endif
1772
1773 /* determine back buffer implementation */
1774 if (v->mesa_visual.doubleBufferMode) {
1775 if (v->ximage_flag) {
1776 b->db_state = BACK_XIMAGE;
1777 }
1778 else {
1779 b->db_state = BACK_PIXMAP;
1780 }
1781 }
1782 else {
1783 b->db_state = 0;
1784 }
1785
1786 _mesa_initialize_framebuffer(&b->mesa_buffer,
1787 &v->mesa_visual,
1788 v->mesa_visual.depthBits > 0,
1789 v->mesa_visual.stencilBits > 0,
1790 v->mesa_visual.accumRedBits > 0,
1791 v->mesa_visual.alphaBits > 0 );
1792
1793 if (!initialize_visual_and_buffer( client, v, b, v->mesa_visual.rgbMode,
1794 (XMesaDrawable)w, b->cmap )) {
1795 free_xmesa_buffer(client, b);
1796 return NULL;
1797 }
1798
1799 #ifdef FX
1800 fxEnvVar = _mesa_getenv("MESA_GLX_FX");
1801 if (fxEnvVar) {
1802 if (fxEnvVar[0]!='d') {
1803 int attribs[100];
1804 int numAttribs = 0;
1805 int hw;
1806 if (v->mesa_visual.depthBits > 0) {
1807 attribs[numAttribs++] = FXMESA_DEPTH_SIZE;
1808 attribs[numAttribs++] = v->mesa_visual.depthBits;
1809 }
1810 if (v->mesa_visual.doubleBufferMode) {
1811 attribs[numAttribs++] = FXMESA_DOUBLEBUFFER;
1812 }
1813 if (v->mesa_visual.accumRedBits > 0) {
1814 attribs[numAttribs++] = FXMESA_ACCUM_SIZE;
1815 attribs[numAttribs++] = v->mesa_visual.accumRedBits;
1816 }
1817 if (v->mesa_visual.stencilBits > 0) {
1818 attribs[numAttribs++] = FXMESA_STENCIL_SIZE;
1819 attribs[numAttribs++] = v->mesa_visual.stencilBits;
1820 }
1821 if (v->mesa_visual.alphaBits > 0) {
1822 attribs[numAttribs++] = FXMESA_ALPHA_SIZE;
1823 attribs[numAttribs++] = v->mesa_visual.alphaBits;
1824 }
1825 if (1) {
1826 attribs[numAttribs++] = FXMESA_SHARE_CONTEXT;
1827 attribs[numAttribs++] = (int) &(c->mesa);
1828 }
1829 attribs[numAttribs++] = FXMESA_NONE;
1830
1831 /* [dBorca] we should take an envvar for `fxMesaSelectCurrentBoard'!!! */
1832 hw = fxMesaSelectCurrentBoard(0);
1833 if ((hw == GR_SSTTYPE_VOODOO) || (hw == GR_SSTTYPE_Voodoo2)) {
1834 b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
1835 if ((v->undithered_pf!=PF_Index) && (b->backimage)) {
1836 b->FXisHackUsable = b->FXctx ? GL_TRUE : GL_FALSE;
1837 if (b->FXctx && (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')) {
1838 b->FXwindowHack = GL_TRUE;
1839 FX_grSstControl(GR_CONTROL_DEACTIVATE);
1840 }
1841 else {
1842 b->FXwindowHack = GL_FALSE;
1843 }
1844 }
1845 }
1846 else {
1847 if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')
1848 b->FXctx = fxMesaCreateContext(w, GR_RESOLUTION_NONE,
1849 GR_REFRESH_75Hz, attribs);
1850 else
1851 b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
1852 b->FXisHackUsable = GL_FALSE;
1853 b->FXwindowHack = GL_FALSE;
1854 }
1855 /*
1856 fprintf(stderr,
1857 "voodoo %d, wid %d height %d hack: usable %d active %d\n",
1858 hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack);
1859 */
1860 }
1861 }
1862 else {
1863 _mesa_warning(NULL, "WARNING: This Mesa Library includes the Glide driver but\n");
1864 _mesa_warning(NULL, " you have not defined the MESA_GLX_FX env. var.\n");
1865 _mesa_warning(NULL, " (check the README.3DFX file for more information).\n\n");
1866 _mesa_warning(NULL, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
1867 }
1868 #endif
1869
1870 return b;
1871 }
1872
1873
1874 XMesaBuffer XMesaCreateWindowBuffer( XMesaVisual v, XMesaWindow w )
1875 {
1876 return XMesaCreateWindowBuffer2( v, w, NULL );
1877 }
1878
1879
1880 /**
1881 * Create a new XMesaBuffer from an X pixmap.
1882 *
1883 * \param v the XMesaVisual
1884 * \param p the pixmap
1885 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1886 * \c GLX_DIRECT_COLOR visual for the pixmap
1887 * \returns new XMesaBuffer or NULL if error
1888 */
1889 XMesaBuffer XMesaCreatePixmapBuffer( XMesaVisual v,
1890 XMesaPixmap p, XMesaColormap cmap )
1891 {
1892 int client = 0;
1893 XMesaBuffer b = alloc_xmesa_buffer();
1894 if (!b) {
1895 return NULL;
1896 }
1897
1898
1899 #ifdef XFree86Server
1900 client = CLIENT_ID(((XMesaDrawable)p)->id);
1901 #endif
1902
1903 assert(v);
1904
1905 b->xm_visual = v;
1906 b->type = PIXMAP;
1907 b->display = v->display;
1908 b->cmap = cmap;
1909
1910 /* determine back buffer implementation */
1911 if (v->mesa_visual.doubleBufferMode) {
1912 if (v->ximage_flag) {
1913 b->db_state = BACK_XIMAGE;
1914 }
1915 else {
1916 b->db_state = BACK_PIXMAP;
1917 }
1918 }
1919 else {
1920 b->db_state = 0;
1921 }
1922
1923 _mesa_initialize_framebuffer(&b->mesa_buffer,
1924 &v->mesa_visual,
1925 v->mesa_visual.depthBits > 0,
1926 v->mesa_visual.stencilBits > 0,
1927 v->mesa_visual.accumRedBits +
1928 v->mesa_visual.accumGreenBits +
1929 v->mesa_visual.accumBlueBits > 0,
1930 v->mesa_visual.alphaBits > 0 );
1931
1932 if (!initialize_visual_and_buffer(client, v, b, v->mesa_visual.rgbMode,
1933 (XMesaDrawable)p, cmap)) {
1934 free_xmesa_buffer(client, b);
1935 return NULL;
1936 }
1937
1938 return b;
1939 }
1940
1941
1942
1943 XMesaBuffer XMesaCreatePBuffer( XMesaVisual v, XMesaColormap cmap,
1944 unsigned int width, unsigned int height )
1945 {
1946 #ifdef XFree86Server
1947 return 0;
1948 #else
1949 int client = 0;
1950 XMesaWindow root;
1951 XMesaDrawable drawable; /* X Pixmap Drawable */
1952 XMesaBuffer b = alloc_xmesa_buffer();
1953 if (!b) {
1954 return NULL;
1955 }
1956
1957 b->xm_visual = v;
1958 b->type = PBUFFER;
1959 b->display = v->display;
1960 b->cmap = cmap;
1961
1962 /* allocate pixmap for front buffer */
1963 root = RootWindow( v->display, v->visinfo->screen );
1964 drawable = XCreatePixmap( v->display, root, width, height, v->visinfo->depth );
1965
1966 /* determine back buffer implementation */
1967 if (v->mesa_visual.doubleBufferMode) {
1968 if (v->ximage_flag) {
1969 b->db_state = BACK_XIMAGE;
1970 }
1971 else {
1972 b->db_state = BACK_PIXMAP;
1973 }
1974 }
1975 else {
1976 b->db_state = 0;
1977 }
1978
1979 _mesa_initialize_framebuffer(&b->mesa_buffer,
1980 &v->mesa_visual,
1981 v->mesa_visual.depthBits > 0,
1982 v->mesa_visual.stencilBits > 0,
1983 v->mesa_visual.accumRedBits +
1984 v->mesa_visual.accumGreenBits +
1985 v->mesa_visual.accumBlueBits > 0,
1986 v->mesa_visual.alphaBits > 0 );
1987
1988 if (!initialize_visual_and_buffer(client, v, b, v->mesa_visual.rgbMode,
1989 drawable, cmap)) {
1990 free_xmesa_buffer(client, b);
1991 return NULL;
1992 }
1993
1994 return b;
1995 #endif
1996 }
1997
1998
1999
2000 /*
2001 * Deallocate an XMesaBuffer structure and all related info.
2002 */
2003 void XMesaDestroyBuffer( XMesaBuffer b )
2004 {
2005 int client = 0;
2006
2007 #ifdef XFree86Server
2008 if (b->frontbuffer)
2009 client = CLIENT_ID(b->frontbuffer->id);
2010 #endif
2011
2012 if (b->gc) XMesaFreeGC( b->xm_visual->display, b->gc );
2013 if (b->cleargc) XMesaFreeGC( b->xm_visual->display, b->cleargc );
2014 if (b->swapgc) XMesaFreeGC( b->xm_visual->display, b->swapgc );
2015
2016 if (b->backimage) {
2017 #if defined(USE_XSHM) && !defined(XFree86Server)
2018 if (b->shm) {
2019 XShmDetach( b->xm_visual->display, &b->shminfo );
2020 XDestroyImage( b->backimage );
2021 shmdt( b->shminfo.shmaddr );
2022 }
2023 else
2024 #endif
2025 XMesaDestroyImage( b->backimage );
2026 }
2027 if (b->backpixmap) {
2028 XMesaFreePixmap( b->xm_visual->display, b->backpixmap );
2029 if (b->xm_visual->hpcr_clear_flag) {
2030 XMesaFreePixmap( b->xm_visual->display,
2031 b->xm_visual->hpcr_clear_pixmap );
2032 XMesaDestroyImage( b->xm_visual->hpcr_clear_ximage );
2033 }
2034 }
2035 if (b->rowimage) {
2036 FREE( b->rowimage->data );
2037 b->rowimage->data = NULL;
2038 XMesaDestroyImage( b->rowimage );
2039 }
2040
2041 free_xmesa_buffer(client, b);
2042 }
2043
2044
2045
2046 /*
2047 * Bind buffer b to context c and make c the current rendering context.
2048 */
2049 GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b )
2050 {
2051 return XMesaMakeCurrent2( c, b, b );
2052 }
2053
2054
2055 /*
2056 * Bind buffer b to context c and make c the current rendering context.
2057 */
2058 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
2059 XMesaBuffer readBuffer )
2060 {
2061 if (c) {
2062 if (!drawBuffer || !readBuffer)
2063 return GL_FALSE; /* must specify buffers! */
2064
2065 #ifdef FX
2066 if (drawBuffer->FXctx) {
2067 fxMesaMakeCurrent(drawBuffer->FXctx);
2068
2069 c->xm_draw_buffer = drawBuffer;
2070 c->xm_read_buffer = readBuffer;
2071 c->xm_buffer = drawBuffer;
2072
2073 return GL_TRUE;
2074 }
2075 #endif
2076 if (&(c->mesa) == _mesa_get_current_context()
2077 && c->xm_draw_buffer == drawBuffer
2078 && c->xm_read_buffer == readBuffer
2079 && c->xm_draw_buffer->wasCurrent) {
2080 /* same context and buffer, do nothing */
2081 return GL_TRUE;
2082 }
2083
2084 c->xm_draw_buffer = drawBuffer;
2085 c->xm_read_buffer = readBuffer;
2086 c->xm_buffer = drawBuffer;
2087
2088 _mesa_make_current2(&(c->mesa),
2089 &drawBuffer->mesa_buffer,
2090 &readBuffer->mesa_buffer);
2091
2092 if (c->mesa.Viewport.Width == 0) {
2093 /* initialize viewport to window size */
2094 _mesa_Viewport( 0, 0, drawBuffer->width, drawBuffer->height );
2095 c->mesa.Scissor.Width = drawBuffer->width;
2096 c->mesa.Scissor.Height = drawBuffer->height;
2097 }
2098
2099 if (c->xm_visual->mesa_visual.rgbMode) {
2100 /*
2101 * Must recompute and set these pixel values because colormap
2102 * can be different for different windows.
2103 */
2104 c->clearpixel = xmesa_color_to_pixel( c,
2105 c->clearcolor[0],
2106 c->clearcolor[1],
2107 c->clearcolor[2],
2108 c->clearcolor[3],
2109 c->xm_visual->undithered_pf);
2110 XMesaSetForeground(c->display, c->xm_draw_buffer->cleargc, c->clearpixel);
2111 }
2112
2113 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
2114 c->xm_draw_buffer->wasCurrent = GL_TRUE;
2115 }
2116 else {
2117 /* Detach */
2118 _mesa_make_current2( NULL, NULL, NULL );
2119 }
2120 return GL_TRUE;
2121 }
2122
2123
2124 /*
2125 * Unbind the context c from its buffer.
2126 */
2127 GLboolean XMesaUnbindContext( XMesaContext c )
2128 {
2129 /* A no-op for XFree86 integration purposes */
2130 return GL_TRUE;
2131 }
2132
2133
2134 XMesaContext XMesaGetCurrentContext( void )
2135 {
2136 GET_CURRENT_CONTEXT(ctx);
2137 if (ctx) {
2138 XMesaContext xmesa = XMESA_CONTEXT(ctx);
2139 return xmesa;
2140 }
2141 else {
2142 return 0;
2143 }
2144 }
2145
2146
2147 XMesaBuffer XMesaGetCurrentBuffer( void )
2148 {
2149 GET_CURRENT_CONTEXT(ctx);
2150 if (ctx) {
2151 XMesaContext xmesa = XMESA_CONTEXT(ctx);
2152 return xmesa->xm_draw_buffer;
2153 }
2154 else {
2155 return 0;
2156 }
2157 }
2158
2159
2160 /* New in Mesa 3.1 */
2161 XMesaBuffer XMesaGetCurrentReadBuffer( void )
2162 {
2163 GET_CURRENT_CONTEXT(ctx);
2164 if (ctx) {
2165 XMesaContext xmesa = XMESA_CONTEXT(ctx);
2166 return xmesa->xm_read_buffer;
2167 }
2168 else {
2169 return 0;
2170 }
2171 }
2172
2173
2174 GLboolean XMesaForceCurrent(XMesaContext c)
2175 {
2176 if (c) {
2177 if (&(c->mesa) != _mesa_get_current_context()) {
2178 _mesa_make_current(&(c->mesa), &c->xm_draw_buffer->mesa_buffer);
2179 }
2180 }
2181 else {
2182 _mesa_make_current(NULL, NULL);
2183 }
2184 return GL_TRUE;
2185 }
2186
2187
2188 GLboolean XMesaLoseCurrent(XMesaContext c)
2189 {
2190 (void) c;
2191 _mesa_make_current(NULL, NULL);
2192 return GL_TRUE;
2193 }
2194
2195
2196 /*
2197 * Switch 3Dfx support hack between window and full-screen mode.
2198 */
2199 GLboolean XMesaSetFXmode( GLint mode )
2200 {
2201 #ifdef FX
2202 const char *fx = _mesa_getenv("MESA_GLX_FX");
2203 if (fx && fx[0] != 'd') {
2204 GET_CURRENT_CONTEXT(ctx);
2205 GrHwConfiguration hw;
2206 if (!FX_grSstQueryHardware(&hw)) {
2207 /*fprintf(stderr, "!grSstQueryHardware\n");*/
2208 return GL_FALSE;
2209 }
2210 if (hw.num_sst < 1) {
2211 /*fprintf(stderr, "hw.num_sst < 1\n");*/
2212 return GL_FALSE;
2213 }
2214 if (ctx) {
2215 /* [dBorca] Hack alert:
2216 * oh, this is sooo wrong: ctx above is
2217 * really an fxMesaContext, not an XMesaContext
2218 */
2219 XMesaContext xmesa = XMESA_CONTEXT(ctx);
2220 if (mode == XMESA_FX_WINDOW) {
2221 if (xmesa->xm_draw_buffer->FXisHackUsable) {
2222 FX_grSstControl(GR_CONTROL_DEACTIVATE);
2223 xmesa->xm_draw_buffer->FXwindowHack = GL_TRUE;
2224 return GL_TRUE;
2225 }
2226 }
2227 else if (mode == XMESA_FX_FULLSCREEN) {
2228 FX_grSstControl(GR_CONTROL_ACTIVATE);
2229 xmesa->xm_draw_buffer->FXwindowHack = GL_FALSE;
2230 return GL_TRUE;
2231 }
2232 else {
2233 /* Error: Bad mode value */
2234 }
2235 }
2236 }
2237 /*fprintf(stderr, "fallthrough\n");*/
2238 #else
2239 (void) mode;
2240 #endif
2241 return GL_FALSE;
2242 }
2243
2244
2245
2246 #ifdef FX
2247 /*
2248 * Read image from VooDoo frame buffer into X/Mesa's back XImage.
2249 */
2250 static void FXgetImage( XMesaBuffer b )
2251 {
2252 GET_CURRENT_CONTEXT(ctx);
2253 static unsigned short pixbuf[MAX_WIDTH];
2254 GLuint x, y;
2255 int xpos, ypos;
2256 XMesaWindow root;
2257 unsigned int bw, depth, width, height;
2258 XMesaContext xmesa = XMESA_CONTEXT(ctx);
2259
2260 #ifdef XFree86Server
2261 x = b->frontbuffer->x;
2262 y = b->frontbuffer->y;
2263 width = b->frontbuffer->width;
2264 height = b->frontbuffer->height;
2265 depth = b->frontbuffer->depth;
2266 #else
2267 XGetGeometry( b->xm_visual->display, b->frontbuffer,
2268 &root, &xpos, &ypos, &width, &height, &bw, &depth);
2269 #endif
2270 if (b->width != width || b->height != height) {
2271 b->width = MIN2((int)width, b->FXctx->width);
2272 b->height = MIN2((int)height, b->FXctx->height);
2273 if (b->width & 1)
2274 b->width--; /* prevent odd width */
2275 xmesa_alloc_back_buffer( b );
2276 }
2277
2278 /* [dBorca] we're always in the right GR_COLORFORMAT... aren't we? */
2279 /* grLfbWriteColorFormat(GR_COLORFORMAT_ARGB); */
2280 if (b->xm_visual->undithered_pf==PF_5R6G5B) {
2281 /* Special case: 16bpp RGB */
2282 grLfbReadRegion( GR_BUFFER_FRONTBUFFER, /* src buffer */
2283 0, b->FXctx->height - b->height, /*pos*/
2284 b->width, b->height, /* size */
2285 b->width * sizeof(GLushort), /* stride */
2286 b->backimage->data); /* dest buffer */
2287 }
2288 else if (b->xm_visual->dithered_pf==PF_Dither
2289 && GET_VISUAL_DEPTH(b->xm_visual)==8) {
2290 /* Special case: 8bpp RGB */
2291 for (y=0;y<b->height;y++) {
2292 GLubyte *ptr = (GLubyte*) b->backimage->data
2293 + b->backimage->bytes_per_line * y;
2294 XDITHER_SETUP(y);
2295
2296 /* read row from 3Dfx frame buffer */
2297 grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
2298 0, b->FXctx->height-(b->height-y),
2299 b->width, 1,
2300 0,
2301 pixbuf );
2302
2303 /* write to XImage back buffer */
2304 for (x=0;x<b->width;x++) {
2305 GLubyte r = (pixbuf[x] & 0xf800) >> 8;
2306 GLubyte g = (pixbuf[x] & 0x07e0) >> 3;
2307 GLubyte b = (pixbuf[x] & 0x001f) << 3;
2308 *ptr++ = XDITHER( x, r, g, b);
2309 }
2310 }
2311 }
2312 else {
2313 /* General case: slow! */
2314 for (y=0;y<b->height;y++) {
2315 /* read row from 3Dfx frame buffer */
2316 grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
2317 0, b->FXctx->height-(b->height-y),
2318 b->width, 1,
2319 0,
2320 pixbuf );
2321
2322 /* write to XImage back buffer */
2323 for (x=0;x<b->width;x++) {
2324 XMesaPutPixel(b->backimage,x,y,
2325 xmesa_color_to_pixel(xmesa,
2326 (pixbuf[x] & 0xf800) >> 8,
2327 (pixbuf[x] & 0x07e0) >> 3,
2328 (pixbuf[x] & 0x001f) << 3,
2329 0xff,
2330 b->xm_visual->undithered_pf));
2331 }
2332 }
2333 }
2334 /* grLfbWriteColorFormat(GR_COLORFORMAT_ABGR); */
2335 }
2336 #endif
2337
2338
2339 /*
2340 * Copy the back buffer to the front buffer. If there's no back buffer
2341 * this is a no-op.
2342 */
2343 void XMesaSwapBuffers( XMesaBuffer b )
2344 {
2345 GET_CURRENT_CONTEXT(ctx);
2346
2347 /* If we're swapping the buffer associated with the current context
2348 * we have to flush any pending rendering commands first.
2349 */
2350 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer))
2351 _mesa_notifySwapBuffers(ctx);
2352
2353 if (b->db_state) {
2354 #ifdef FX
2355 if (b->FXctx) {
2356 fxMesaSwapBuffers();
2357
2358 if (b->FXwindowHack)
2359 FXgetImage(b);
2360 else
2361 return;
2362 }
2363 #endif
2364 if (b->backimage) {
2365 /* Copy Ximage from host's memory to server's window */
2366 #if defined(USE_XSHM) && !defined(XFree86Server)
2367 if (b->shm) {
2368 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2369 XShmPutImage( b->xm_visual->display, b->frontbuffer,
2370 b->swapgc,
2371 b->backimage, 0, 0,
2372 0, 0, b->width, b->height, False );
2373 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2374 }
2375 else
2376 #endif
2377 {
2378 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2379 XMesaPutImage( b->xm_visual->display, b->frontbuffer,
2380 b->swapgc,
2381 b->backimage, 0, 0,
2382 0, 0, b->width, b->height );
2383 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2384 }
2385 }
2386 else {
2387 /* Copy pixmap to window on server */
2388 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2389 XMesaCopyArea( b->xm_visual->display,
2390 b->backpixmap, /* source drawable */
2391 b->frontbuffer, /* dest. drawable */
2392 b->swapgc,
2393 0, 0, b->width, b->height, /* source region */
2394 0, 0 /* dest region */
2395 );
2396 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2397 }
2398 }
2399 #if !defined(XFree86Server)
2400 XSync( b->xm_visual->display, False );
2401 #endif
2402 }
2403
2404
2405
2406 /*
2407 * Copy sub-region of back buffer to front buffer
2408 */
2409 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
2410 {
2411 GET_CURRENT_CONTEXT(ctx);
2412
2413 /* If we're swapping the buffer associated with the current context
2414 * we have to flush any pending rendering commands first.
2415 */
2416 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer))
2417 _mesa_notifySwapBuffers(ctx);
2418
2419 if (b->db_state) {
2420 int yTop = b->height - y - height;
2421 #ifdef FX
2422 if (b->FXctx) {
2423 fxMesaSwapBuffers();
2424 if (b->FXwindowHack)
2425 FXgetImage(b);
2426 else
2427 return;
2428 }
2429 #endif
2430 if (b->backimage) {
2431 /* Copy Ximage from host's memory to server's window */
2432 #if defined(USE_XSHM) && !defined(XFree86Server)
2433 if (b->shm) {
2434 /* XXX assuming width and height aren't too large! */
2435 XShmPutImage( b->xm_visual->display, b->frontbuffer,
2436 b->swapgc,
2437 b->backimage, x, yTop,
2438 x, yTop, width, height, False );
2439 /* wait for finished event??? */
2440 }
2441 else
2442 #endif
2443 {
2444 /* XXX assuming width and height aren't too large! */
2445 XMesaPutImage( b->xm_visual->display, b->frontbuffer,
2446 b->swapgc,
2447 b->backimage, x, yTop,
2448 x, yTop, width, height );
2449 }
2450 }
2451 else {
2452 /* Copy pixmap to window on server */
2453 XMesaCopyArea( b->xm_visual->display,
2454 b->backpixmap, /* source drawable */
2455 b->frontbuffer, /* dest. drawable */
2456 b->swapgc,
2457 x, yTop, width, height, /* source region */
2458 x, yTop /* dest region */
2459 );
2460 }
2461 }
2462 }
2463
2464
2465 /*
2466 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2467 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2468 * back buffer directly.
2469 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2470 * ximage - pointer to back buffer's XImage, or NULL
2471 * Return: GL_TRUE = context is double buffered
2472 * GL_FALSE = context is single buffered
2473 */
2474 GLboolean XMesaGetBackBuffer( XMesaBuffer b,
2475 XMesaPixmap *pixmap,
2476 XMesaImage **ximage )
2477 {
2478 if (b->db_state) {
2479 if (pixmap) *pixmap = b->backpixmap;
2480 if (ximage) *ximage = b->backimage;
2481 return GL_TRUE;
2482 }
2483 else {
2484 *pixmap = 0;
2485 *ximage = NULL;
2486 return GL_FALSE;
2487 }
2488 }
2489
2490
2491 /*
2492 * Return the depth buffer associated with an XMesaBuffer.
2493 * Input: b - the XMesa buffer handle
2494 * Output: width, height - size of buffer in pixels
2495 * bytesPerValue - bytes per depth value (2 or 4)
2496 * buffer - pointer to depth buffer values
2497 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2498 */
2499 GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height,
2500 GLint *bytesPerValue, void **buffer )
2501 {
2502 if (!b->mesa_buffer.DepthBuffer) {
2503 *width = 0;
2504 *height = 0;
2505 *bytesPerValue = 0;
2506 *buffer = 0;
2507 return GL_FALSE;
2508 }
2509 else {
2510 *width = b->mesa_buffer.Width;
2511 *height = b->mesa_buffer.Height;
2512 *bytesPerValue = sizeof(GLdepth);
2513 *buffer = b->mesa_buffer.DepthBuffer;
2514 return GL_TRUE;
2515 }
2516 }
2517
2518
2519 void XMesaFlush( XMesaContext c )
2520 {
2521 if (c && c->xm_visual) {
2522 #ifdef XFree86Server
2523 /* NOT_NEEDED */
2524 #else
2525 XSync( c->xm_visual->display, False );
2526 #endif
2527 }
2528 }
2529
2530
2531
2532 const char *XMesaGetString( XMesaContext c, int name )
2533 {
2534 (void) c;
2535 if (name==XMESA_VERSION) {
2536 return "5.0";
2537 }
2538 else if (name==XMESA_EXTENSIONS) {
2539 return "";
2540 }
2541 else {
2542 return NULL;
2543 }
2544 }
2545
2546
2547
2548 XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d )
2549 {
2550 XMesaBuffer b;
2551 for (b=XMesaBufferList; b; b=b->Next) {
2552 if (b->frontbuffer==d && b->display==dpy) {
2553 return b;
2554 }
2555 }
2556 return NULL;
2557 }
2558
2559
2560
2561 /*
2562 * Look for XMesaBuffers whose X window has been destroyed.
2563 * Deallocate any such XMesaBuffers.
2564 */
2565 void XMesaGarbageCollect( void )
2566 {
2567 XMesaBuffer b, next;
2568 for (b=XMesaBufferList; b; b=next) {
2569 next = b->Next;
2570 if (b->display && b->frontbuffer && b->type == WINDOW) {
2571 #ifdef XFree86Server
2572 /* NOT_NEEDED */
2573 #else
2574 XSync(b->display, False);
2575 if (!window_exists( b->display, b->frontbuffer )) {
2576 /* found a dead window, free the ancillary info */
2577 XMesaDestroyBuffer( b );
2578 }
2579 #endif
2580 }
2581 }
2582 }
2583
2584
2585 void XMesaReset( void )
2586 {
2587 while (XMesaBufferList)
2588 XMesaDestroyBuffer(XMesaBufferList);
2589
2590 XMesaBufferList = NULL;
2591 }
2592
2593
2594 unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y,
2595 GLfloat red, GLfloat green,
2596 GLfloat blue, GLfloat alpha )
2597 {
2598 GLint r = (GLint) (red * 255.0F);
2599 GLint g = (GLint) (green * 255.0F);
2600 GLint b = (GLint) (blue * 255.0F);
2601 GLint a = (GLint) (alpha * 255.0F);
2602
2603 switch (xmesa->pixelformat) {
2604 case PF_Index:
2605 return 0;
2606 case PF_Truecolor:
2607 {
2608 unsigned long p;
2609 PACK_TRUECOLOR( p, r, g, b );
2610 return p;
2611 }
2612 case PF_8A8B8G8R:
2613 return PACK_8A8B8G8R( r, g, b, a );
2614 case PF_8R8G8B:
2615 return PACK_8R8G8B( r, g, b );
2616 case PF_5R6G5B:
2617 return PACK_5R6G5B( r, g, b );
2618 case PF_Dither:
2619 {
2620 DITHER_SETUP;
2621 return DITHER( x, y, r, g, b );
2622 }
2623 case PF_1Bit:
2624 /* 382 = (3*255)/2 */
2625 return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip;
2626 case PF_HPCR:
2627 return DITHER_HPCR(x, y, r, g, b);
2628 case PF_Lookup:
2629 {
2630 LOOKUP_SETUP;
2631 return LOOKUP( r, g, b );
2632 }
2633 case PF_Grayscale:
2634 return GRAY_RGB( r, g, b );
2635 case PF_Dither_5R6G5B:
2636 /* fall through */
2637 case PF_Dither_True:
2638 {
2639 unsigned long p;
2640 PACK_TRUEDITHER(p, x, y, r, g, b);
2641 return p;
2642 }
2643 default:
2644 _mesa_problem(NULL, "Bad pixel format in XMesaDitherColor");
2645 }
2646 return 0;
2647 }
2648
2649
2650 /*
2651 * This is typically called when the window size changes and we need
2652 * to reallocate the buffer's back/depth/stencil/accum buffers.
2653 */
2654 void XMesaResizeBuffers( XMesaBuffer b )
2655 {
2656 xmesa_resize_buffers( &(b->mesa_buffer) );
2657
2658 }
2659