Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/drivers/x11/xm_api.c

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