2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /* $XFree86: xc/extras/Mesa/src/X/xm_api.c,v 1.2 2002/02/26 23:37:31 tsi Exp $ */
27 * This file contains the implementations of all the XMesa* functions.
32 * The window coordinate system origin (0,0) is in the lower-left corner
33 * of the window. X11's window coordinate origin is in the upper-left
34 * corner of the window. Therefore, most drawing functions in this
35 * file have to flip Y coordinates.
37 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
38 * in support for the MIT Shared Memory extension. If enabled, when you
39 * use an Ximage for the back buffer in double buffered mode, the "swap"
40 * operation will be faster. You must also link with -lXext.
42 * Byte swapping: If the Mesa host and the X display use a different
43 * byte order then there's some trickiness to be aware of when using
44 * XImages. The byte ordering used for the XImage is that of the X
45 * display, not the Mesa host.
46 * The color-to-pixel encoding for True/DirectColor must be done
47 * according to the display's visual red_mask, green_mask, and blue_mask.
48 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
49 * do byte swapping if needed. If one wants to directly "poke" the pixel
50 * into the XImage's buffer then the pixel must be byte swapped first. In
51 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
52 * and use XPutPixel everywhere except in the implementation of
53 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
54 * instead of using XPutPixel we "poke" our values after byte-swapping
55 * the clear pixel value if needed.
64 #include "glxheader.h"
68 #include "extensions.h"
74 #include "texformat.h"
77 #include "swrast/swrast.h"
78 #include "swrast_setup/swrast_setup.h"
79 #include "array_cache/acache.h"
83 #define GLX_NONE_EXT 0x8000
88 * Global X driver lock
90 _glthread_Mutex _xmesa_lock
;
95 * Lookup tables for HPCR pixel format:
97 static short hpcr_rgbTbl
[3][256] = {
99 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
100 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
101 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
102 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
103 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
104 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
105 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
106 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
107 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
108 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
109 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
110 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
111 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
112 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
113 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
114 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
117 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
118 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
119 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
120 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
121 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
122 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
123 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
124 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
125 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
126 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
127 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
128 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
129 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
130 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
131 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
132 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
135 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
136 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
137 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
138 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
139 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
140 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
141 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
142 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
143 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
144 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
145 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
146 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
147 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
148 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
149 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
150 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
156 /**********************************************************************/
157 /***** X Utility Functions *****/
158 /**********************************************************************/
162 * Return the host's byte order as LSBFirst or MSBFirst ala X.
164 #ifndef XFree86Server
165 static int host_byte_order( void )
168 char *cptr
= (char *) &i
;
169 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
177 #ifndef XFree86Server
178 static int mesaXErrorFlag
= 0;
180 static int mesaHandleXError( XMesaDisplay
*dpy
, XErrorEvent
*event
)
191 * Check if the X Shared Memory extension is available.
192 * Return: 0 = not available
193 * 1 = shared XImage support available
194 * 2 = shared Pixmap support available also
196 #ifndef XFree86Server
197 static int check_for_xshm( XMesaDisplay
*display
)
200 int major
, minor
, ignore
;
203 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
204 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
205 return (pixmaps
==True
) ? 2 : 1;
215 /* Can't compile XSHM support */
223 * Return the width and height of the given drawable.
225 static void get_drawable_size( XMesaDisplay
*dpy
, XMesaDrawable d
,
226 unsigned int *width
, unsigned int *height
)
235 unsigned int bw
, depth
;
237 _glthread_LOCK_MUTEX(_xmesa_lock
);
238 XGetGeometry( dpy
, d
, &root
, &x
, &y
, width
, height
, &bw
, &depth
);
239 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
245 * Apply gamma correction to an intensity value in [0..max]. Return the
246 * new intensity value.
248 static GLint
gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
254 double x
= (double) value
/ (double) max
;
255 return IROUND_POS((GLfloat
) max
* _mesa_pow(x
, 1.0F
/gamma
));
262 * Return the true number of bits per pixel for XImages.
263 * For example, if we request a 24-bit deep visual we may actually need/get
264 * 32bpp XImages. This function returns the appropriate bpp.
265 * Input: dpy - the X display
266 * visinfo - desribes the visual to be used for XImages
267 * Return: true number of bits per pixel for XImages
271 static int bits_per_pixel( XMesaVisual xmv
)
273 XMesaVisualInfo visinfo
= xmv
->visinfo
;
274 const int depth
= visinfo
->nplanes
;
276 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
277 if (screenInfo
.formats
[i
].depth
== depth
)
278 return screenInfo
.formats
[i
].bitsPerPixel
;
280 return depth
; /* should never get here, but this should be safe */
285 static int bits_per_pixel( XMesaVisual xmv
)
287 XMesaDisplay
*dpy
= xmv
->display
;
288 XMesaVisualInfo visinfo
= xmv
->visinfo
;
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*/
300 /* grab the bits/pixel value */
301 bitsPerPixel
= img
->bits_per_pixel
;
302 /* free the XImage */
305 XMesaDestroyImage( img
);
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
321 #ifndef XFree86Server
322 static GLboolean WindowExistsFlag
;
324 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
327 if (xerr
->error_code
== BadWindow
) {
328 WindowExistsFlag
= GL_FALSE
;
333 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
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
;
347 /**********************************************************************/
348 /***** Linked list of XMesaBuffers *****/
349 /**********************************************************************/
351 static XMesaBuffer XMesaBufferList
= NULL
;
354 /* Allocate a new XMesaBuffer, add to linked list */
355 static XMesaBuffer
alloc_xmesa_buffer(void)
357 XMesaBuffer b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
359 b
->Next
= XMesaBufferList
;
367 * Find an XMesaBuffer by matching X display and colormap but NOT matching
368 * the notThis buffer.
370 static XMesaBuffer
find_xmesa_buffer(XMesaDisplay
*dpy
,
375 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
376 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
385 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
388 static void free_xmesa_buffer(int client
, XMesaBuffer buffer
)
390 XMesaBuffer prev
= NULL
, b
;
392 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
394 /* unlink bufer from list */
396 prev
->Next
= buffer
->Next
;
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
)) {
404 (void)FreeColors(buffer
->cmap
, client
,
405 buffer
->num_alloced
, buffer
->alloced_colors
,
408 XFreeColors(buffer
->display
, buffer
->cmap
,
409 buffer
->alloced_colors
, buffer
->num_alloced
, 0);
414 _mesa_free_framebuffer_data(&buffer
->mesa_buffer
);
419 /* continue search */
422 /* buffer not found in XMesaBufferList */
423 _mesa_problem(NULL
,"free_xmesa_buffer() - buffer not found\n");
427 /* Copy X color table stuff from one XMesaBuffer to another. */
428 static void copy_colortable_info(XMesaBuffer dst
, const XMesaBuffer src
)
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
));
441 /**********************************************************************/
442 /***** Misc Private Functions *****/
443 /**********************************************************************/
447 * Return number of bits set in n.
449 static int bitcount( unsigned long n
)
452 for (bits
=0; n
>0; n
=n
>>1) {
463 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
464 * Return: GL_TRUE if success, GL_FALSE if error
466 #ifndef XFree86Server
467 static GLboolean
alloc_shm_back_buffer( XMesaBuffer b
)
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...
477 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
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.");
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
);
496 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmget), disabling.");
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 );
508 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmat), disabling.");
513 b
->shminfo
.readOnly
= False
;
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
);
520 if (mesaXErrorFlag
) {
521 /* we are on a remote display, this error is normal, don't print it */
522 XFlush( b
->xm_visual
->display
);
524 XDestroyImage( b
->backimage
);
525 shmdt( b
->shminfo
.shmaddr
);
526 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
529 (void) XSetErrorHandler( old_handler
);
533 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 ); /* nobody else needs it */
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
);
545 XDestroyImage( b
->backimage
);
546 shmdt( b
->shminfo
.shmaddr
);
547 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
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);
575 /* Can't compile XSHM support */
584 * Setup an off-screen pixmap or Ximage to use as the back buffer.
585 * Input: b - the X/Mesa buffer
587 void xmesa_alloc_back_buffer( XMesaBuffer b
)
589 if (b
->db_state
==BACK_XIMAGE
) {
590 /* Deallocate the old backimage, if any */
592 #if defined(USE_XSHM) && !defined(XFree86Server)
594 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
595 XDestroyImage( b
->backimage
);
596 shmdt( b
->shminfo
.shmaddr
);
600 XMesaDestroyImage( b
->backimage
);
604 /* Allocate new back buffer */
607 /* Allocate a regular XImage for the back buffer. */
608 b
->backimage
= XMesaCreateImage(b
->xm_visual
->BitsPerPixel
,
609 b
->width
, b
->height
, NULL
);
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 */
621 _mesa_warning(NULL
, "alloc_back_buffer: XCreateImage failed.");
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
);
631 b
->backpixmap
= None
;
633 else if (b
->db_state
==BACK_PIXMAP
) {
634 XMesaPixmap old_pixmap
= b
->backpixmap
;
635 /* Free the old back pixmap */
637 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
639 /* Allocate new back pixmap */
640 b
->backpixmap
= XMesaCreatePixmap( b
->xm_visual
->display
, b
->frontbuffer
,
642 GET_VISUAL_DEPTH(b
->xm_visual
) );
644 /* update other references to backpixmap */
645 if (b
->buffer
==(XMesaDrawable
)old_pixmap
) {
646 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
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
659 * Written by Michael Pichler, Brian Paul, Mark Kilgard
660 * Input: dpy - X display
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
668 noFaultXAllocColor( int client
,
673 int *exact
, int *alloced
)
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
;
687 double mindist
; /* 3*2^16^2 exceeds long int precision. */
691 /* First try just using XAllocColor. */
694 &color
->red
, &color
->green
, &color
->blue
,
696 client
) == Success
) {
698 if (XAllocColor(dpy
, cmap
, color
)) {
705 /* Alloc failed, search for closest match */
707 /* Retrieve color table entries. */
708 /* XXX alloca candidate. */
710 ppixIn
= (Pixel
*) MALLOC(cmapSize
* sizeof(Pixel
));
711 ctable
= (xrgb
*) MALLOC(cmapSize
* sizeof(xrgb
));
712 for (i
= 0; i
< cmapSize
; i
++) {
715 QueryColors(cmap
, cmapSize
, ppixIn
, ctable
);
717 if (prevDisplay
!= dpy
|| prevCmap
!= cmap
718 || prevCmapSize
!= cmapSize
|| !ctable
) {
719 /* free previously cached color table */
722 /* Get the color table from X */
723 ctable
= (XMesaColor
*) MALLOC(cmapSize
* sizeof(XMesaColor
));
725 for (i
= 0; i
< cmapSize
; i
++) {
728 XQueryColors(dpy
, cmap
, ctable
, cmapSize
);
731 prevCmapSize
= cmapSize
;
735 /* Find best match. */
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
) {
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.
759 &subColor
.red
, &subColor
.green
, &subColor
.blue
,
761 client
) == Success
) {
763 if (XAllocColor(dpy
, cmap
, &subColor
)) {
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
;
780 /* don't free table, save it for next time */
791 * Do setup for PF_GRAYSCALE pixel format.
792 * Note that buffer may be NULL.
794 static GLboolean
setup_grayscale( int client
, XMesaVisual v
,
795 XMesaBuffer buffer
, XMesaColormap cmap
)
797 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
802 XMesaBuffer prevBuffer
;
808 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
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.
815 copy_colortable_info(buffer
, prevBuffer
);
818 /* Allocate 256 shades of 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 );
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
);
837 assert(buffer
->num_alloced
<256);
838 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
839 buffer
->num_alloced
++;
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;
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
;
859 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
861 "Note: %d out of 256 needed colors do not match exactly.\n",
867 v
->dithered_pf
= PF_Grayscale
;
868 v
->undithered_pf
= PF_Grayscale
;
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.
882 static GLboolean
setup_dithered_color( int client
, XMesaVisual v
,
883 XMesaBuffer buffer
, XMesaColormap cmap
)
885 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
890 XMesaBuffer prevBuffer
;
896 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
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.
903 copy_colortable_info(buffer
, prevBuffer
);
906 /* Allocate X colors and initialize color_table[], red_table[], etc */
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
++) {
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
);
924 assert(buffer
->num_alloced
<256);
925 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
926 buffer
->num_alloced
++;
928 i
= DITH_MIX( r
, g
, b
);
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);
939 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
941 "Note: %d out of %d needed colors do not match exactly.\n",
942 colorsfailed
, DITH_R
* DITH_G
* DITH_B
);
947 v
->dithered_pf
= PF_Dither
;
948 v
->undithered_pf
= PF_Lookup
;
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.
958 static void setup_8bit_hpcr( XMesaVisual v
)
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.
967 /* Setup color tables with gamma correction */
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 );
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 );
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 );
988 v
->undithered_pf
= PF_HPCR
; /* can't really disable dithering for now */
989 v
->dithered_pf
= PF_HPCR
;
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
;
996 if (v
->hpcr_clear_flag
) {
997 v
->hpcr_clear_pixmap
= XMesaCreatePixmap(v
->display
,
998 DefaultRootWindow(v
->display
),
1000 #ifndef XFree86Server
1001 v
->hpcr_clear_ximage
= XGetImage(v
->display
, v
->hpcr_clear_pixmap
,
1002 0, 0, 16, 2, AllPlanes
, ZPixmap
);
1009 * Setup RGB rendering for a window with a True/DirectColor visual.
1011 static void setup_truecolor( XMesaVisual v
, XMesaBuffer buffer
,
1012 XMesaColormap cmap
)
1014 unsigned long rmask
, gmask
, bmask
;
1018 /* Compute red multiplier (mask) and bit shift */
1020 rmask
= GET_REDMASK(v
);
1021 while ((rmask
& 1)==0) {
1026 /* Compute green multiplier (mask) and bit shift */
1028 gmask
= GET_GREENMASK(v
);
1029 while ((gmask
& 1)==0) {
1034 /* Compute blue multiplier (mask) and bit shift */
1036 bmask
= GET_BLUEMASK(v
);
1037 while ((bmask
& 1)==0) {
1043 * Compute component-to-pixel lookup tables and dithering kernel
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,
1052 GLint rBits
= bitcount(rmask
);
1053 GLint gBits
= bitcount(gmask
);
1054 GLint bBits
= bitcount(bmask
);
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
);
1066 /* convert RGB values from [0,255] to pixel components */
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
;
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];
1083 /* setup dithering kernel */
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
;
1091 v
->undithered_pf
= PF_Truecolor
;
1092 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
1096 * Now check for TrueColor visuals which we can optimize.
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
;
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
;
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
;
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
;
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
);
1150 * Setup RGB rendering for a window with a monochrome visual.
1152 static void setup_monochrome( XMesaVisual v
, XMesaBuffer 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);
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
1172 static GLboolean
initialize_visual_and_buffer( int client
,
1176 XMesaDrawable window
,
1177 XMesaColormap cmap
)
1179 #ifndef XFree86Server
1184 assert(b
->xm_visual
== v
);
1187 /* Save true bits/pixel */
1188 v
->BitsPerPixel
= bits_per_pixel(v
);
1189 assert(v
->BitsPerPixel
> 0);
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.
1197 v
->dithered_pf
= v
->undithered_pf
= PF_Index
;
1198 v
->index_bits
= GET_VISUAL_DEPTH(v
);
1202 * We support RGB rendering into almost any kind of visual.
1205 xclass
= GET_VISUAL_CLASS(v
);
1206 if (xclass
==TrueColor
|| xclass
==DirectColor
) {
1207 setup_truecolor( v
, b
, cmap
);
1209 else if (xclass
==StaticGray
&& GET_VISUAL_DEPTH(v
)==1) {
1210 setup_monochrome( v
, b
);
1212 else if (xclass
==GrayScale
|| xclass
==StaticGray
) {
1213 if (!setup_grayscale( client
, v
, b
, cmap
)) {
1217 else if ((xclass
==PseudoColor
|| xclass
==StaticColor
)
1218 && GET_VISUAL_DEPTH(v
)>=4 && GET_VISUAL_DEPTH(v
)<=16) {
1219 if (!setup_dithered_color( client
, v
, b
, cmap
)) {
1224 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.");
1229 if (_mesa_getenv("MESA_NO_DITHER")) {
1230 v
->dithered_pf
= v
->undithered_pf
;
1236 * If MESA_INFO env var is set print out some debugging info
1237 * which can help Brian figure out what's going on when a user
1240 if (_mesa_getenv("MESA_INFO")) {
1241 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
1242 _mesa_printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
1243 _mesa_printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
1244 _mesa_printf("X/Mesa level = %d\n", v
->level
);
1245 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
1246 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
1250 /* Do window-specific initializations */
1252 /* Window dimensions */
1254 get_drawable_size( v
->display
, window
, &w
, &h
);
1258 b
->frontbuffer
= window
;
1260 /* Setup for single/double buffering */
1261 if (v
->mesa_visual
.doubleBufferMode
) {
1262 /* Double buffered */
1263 #ifndef XFree86Server
1264 b
->shm
= check_for_xshm( v
->display
);
1266 xmesa_alloc_back_buffer( b
);
1267 if (b
->db_state
==BACK_PIXMAP
) {
1268 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
1275 /* Single Buffered */
1276 b
->buffer
= b
->frontbuffer
;
1279 /* X11 graphics contexts */
1280 #ifdef XFree86Server
1281 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
1283 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
1285 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
1287 /* cleargc - for glClear() */
1288 #ifdef XFree86Server
1289 b
->cleargc
= CreateScratchGC(v
->display
, window
->depth
);
1291 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
1293 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
1296 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1297 * Patch contributed by Michael Pichler May 15, 1995.
1299 #ifdef XFree86Server
1300 b
->swapgc
= CreateScratchGC(v
->display
, window
->depth
);
1304 dixChangeGC(NullClient
, b
->swapgc
, GCGraphicsExposures
, v
, NULL
);
1307 gcvalues
.graphics_exposures
= False
;
1308 b
->swapgc
= XCreateGC( v
->display
, window
,
1309 GCGraphicsExposures
, &gcvalues
);
1311 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
1313 * Set fill style and tile pixmap once for all for HPCR stuff
1314 * (instead of doing it each time in clear_color_HPCR_pixmap())
1315 * Initialize whole stuff
1316 * Patch contributed by Jacques Leroy March 8, 1998.
1318 if (v
->hpcr_clear_flag
&& b
->buffer
!=XIMAGE
) {
1320 for (i
=0; i
<16; i
++)
1322 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 0, 0);
1323 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 1, 0);
1325 XMesaPutImage(b
->display
, (XMesaDrawable
)v
->hpcr_clear_pixmap
,
1326 b
->cleargc
, v
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1327 XMesaSetFillStyle( v
->display
, b
->cleargc
, FillTiled
);
1328 XMesaSetTile( v
->display
, b
->cleargc
, v
->hpcr_clear_pixmap
);
1331 /* Initialize the row buffer XImage for use in write_color_span() */
1332 #ifdef XFree86Server
1333 b
->rowimage
= XMesaCreateImage(GET_VISUAL_DEPTH(v
), MAX_WIDTH
, 1,
1334 (char *)MALLOC(MAX_WIDTH
*4));
1336 b
->rowimage
= XCreateImage( v
->display
,
1339 ZPixmap
, 0, /*format, offset*/
1340 (char*) MALLOC(MAX_WIDTH
*4), /*data*/
1341 MAX_WIDTH
, 1, /*width, height*/
1343 0 /*bytes_per_line*/ );
1353 * Convert an RGBA color to a pixel value.
1356 xmesa_color_to_pixel( XMesaContext xmesa
, GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
1359 switch (pixelFormat
) {
1365 PACK_TRUECOLOR( p
, r
, g
, b
);
1369 return PACK_8A8B8G8R( r
, g
, b
, a
);
1373 return PACK_8R8G8B( r
, g
, b
);
1375 return PACK_5R6G5B( r
, g
, b
);
1379 return DITHER( 1, 0, r
, g
, b
);
1382 /* 382 = (3*255)/2 */
1383 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
1385 return DITHER_HPCR(1, 1, r
, g
, b
);
1389 return LOOKUP( r
, g
, b
);
1392 return GRAY_RGB( r
, g
, b
);
1393 case PF_Dither_True
:
1395 case PF_Dither_5R6G5B
:
1398 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
1402 _mesa_problem(NULL
, "Bad pixel format in xmesa_color_to_pixel");
1408 /**********************************************************************/
1409 /***** Public Functions *****/
1410 /**********************************************************************/
1414 * Create a new X/Mesa visual.
1415 * Input: display - X11 display
1416 * visinfo - an XVisualInfo pointer
1417 * rgb_flag - GL_TRUE = RGB mode,
1418 * GL_FALSE = color index mode
1419 * alpha_flag - alpha buffer requested?
1420 * db_flag - GL_TRUE = double-buffered,
1421 * GL_FALSE = single buffered
1422 * stereo_flag - stereo visual?
1423 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1424 * GL_FALSE = use an off-screen pixmap for back buffer
1425 * depth_size - requested bits/depth values, or zero
1426 * stencil_size - requested bits/stencil values, or zero
1427 * accum_red_size - requested bits/red accum values, or zero
1428 * accum_green_size - requested bits/green accum values, or zero
1429 * accum_blue_size - requested bits/blue accum values, or zero
1430 * accum_alpha_size - requested bits/alpha accum values, or zero
1431 * num_samples - number of samples/pixel if multisampling, or zero
1432 * level - visual level, usually 0
1433 * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
1434 * Return; a new XMesaVisual or 0 if error.
1436 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
1437 XMesaVisualInfo visinfo
,
1439 GLboolean alpha_flag
,
1441 GLboolean stereo_flag
,
1442 GLboolean ximage_flag
,
1445 GLint accum_red_size
,
1446 GLint accum_green_size
,
1447 GLint accum_blue_size
,
1448 GLint accum_alpha_size
,
1451 GLint visualCaveat
)
1455 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
1457 /* For debugging only */
1458 if (_mesa_getenv("MESA_XSYNC")) {
1459 /* This makes debugging X easier.
1460 * In your debugger, set a breakpoint on _XError to stop when an
1461 * X protocol error is generated.
1463 #ifdef XFree86Server
1466 XSynchronize( display
, 1 );
1470 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
1476 * In the X server, NULL is passed in for the display. It will have
1477 * to be set before using this visual. See XMesaSetVisualDisplay()
1480 v
->display
= display
;
1482 /* Save a copy of the XVisualInfo struct because the user may XFREE()
1483 * the struct but we may need some of the information contained in it
1486 #ifdef XFree86Server
1487 v
->visinfo
= visinfo
;
1489 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
1494 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
1497 #ifdef XFree86Server
1498 /* Initialize the depth of the screen */
1500 PixmapFormatRec
*format
;
1502 for (format
= screenInfo
.formats
;
1503 format
->depth
!= display
->rootDepth
;
1506 v
->screen_depth
= format
->bitsPerPixel
;
1510 /* check for MESA_GAMMA environment variable */
1511 gamma
= _mesa_getenv("MESA_GAMMA");
1513 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
1514 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
1515 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
1516 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
1517 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
1520 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
1523 v
->ximage_flag
= ximage_flag
;
1525 v
->VisualCaveat
= visualCaveat
;
1527 (void) initialize_visual_and_buffer( 0, v
, NULL
, rgb_flag
, 0, 0 );
1531 xclass
= GET_VISUAL_CLASS(v
);
1532 if (xclass
==TrueColor
|| xclass
==DirectColor
) {
1533 red_bits
= bitcount(GET_REDMASK(v
));
1534 green_bits
= bitcount(GET_GREENMASK(v
));
1535 blue_bits
= bitcount(GET_BLUEMASK(v
));
1539 /* this is an approximation */
1541 depth
= GET_VISUAL_DEPTH(v
);
1542 red_bits
= depth
/ 3;
1544 green_bits
= depth
/ 2;
1545 depth
-= green_bits
;
1548 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
1552 if (alpha_flag
&& alpha_bits
== 0)
1555 _mesa_initialize_visual( &v
->mesa_visual
,
1556 rgb_flag
, db_flag
, stereo_flag
,
1557 red_bits
, green_bits
,
1558 blue_bits
, alpha_bits
,
1562 accum_red_size
, accum_green_size
,
1563 accum_blue_size
, accum_alpha_size
,
1569 void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
)
1575 void XMesaDestroyVisual( XMesaVisual v
)
1577 #ifndef XFree86Server
1586 * Create a new XMesaContext.
1587 * Input: v - XMesaVisual
1588 * share_list - another XMesaContext with which to share display
1589 * lists or NULL if no sharing is wanted.
1590 * Return: an XMesaContext or NULL if error.
1592 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
1594 static GLboolean firstTime
= GL_TRUE
;
1596 GLboolean direct
= GL_TRUE
; /* not really */
1600 _glthread_INIT_MUTEX(_xmesa_lock
);
1601 firstTime
= GL_FALSE
;
1604 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
1609 mesaCtx
= &(c
->mesa
);
1611 /* Setup these pointers here since they're using for making the default
1612 * and proxy texture objects. Actually, we don't really need to do
1613 * this since we're using the default fallback functions which
1614 * _mesa_initialize_context() would plug in if needed.
1616 mesaCtx
->Driver
.NewTextureObject
= _mesa_new_texture_object
;
1617 mesaCtx
->Driver
.DeleteTexture
= _mesa_delete_texture_object
;
1619 if (!_mesa_initialize_context(mesaCtx
, &v
->mesa_visual
,
1620 share_list
? &(share_list
->mesa
) : (GLcontext
*) NULL
,
1621 (void *) c
, direct
)) {
1626 _mesa_enable_sw_extensions(mesaCtx
);
1627 _mesa_enable_1_3_extensions(mesaCtx
);
1628 _mesa_enable_1_4_extensions(mesaCtx
);
1629 _mesa_enable_1_5_extensions(mesaCtx
);
1631 if (CHECK_BYTE_ORDER(v
)) {
1632 c
->swapbytes
= GL_FALSE
;
1635 c
->swapbytes
= GL_TRUE
;
1639 c
->xm_draw_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1640 c
->xm_read_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1641 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1642 c
->display
= v
->display
;
1643 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
1645 mesaCtx
->Driver
.UpdateState
= xmesa_update_state
;
1647 /* Initialize the software rasterizer and helper modules.
1649 _swrast_CreateContext( mesaCtx
);
1650 _ac_CreateContext( mesaCtx
);
1651 _tnl_CreateContext( mesaCtx
);
1652 _swsetup_CreateContext( mesaCtx
);
1654 xmesa_register_swrast_functions( mesaCtx
);
1656 /* Set up some constant pointers:
1658 xmesa_init_pointers( mesaCtx
);
1666 void XMesaDestroyContext( XMesaContext c
)
1668 GLcontext
*mesaCtx
= &c
->mesa
;
1670 if (c
->xm_draw_buffer
&& c
->xm_buffer
->FXctx
)
1671 fxMesaDestroyContext(c
->xm_draw_buffer
->FXctx
);
1673 _swsetup_DestroyContext( mesaCtx
);
1674 _swrast_DestroyContext( mesaCtx
);
1675 _tnl_DestroyContext( mesaCtx
);
1676 _ac_DestroyContext( mesaCtx
);
1677 _mesa_free_context_data( mesaCtx
);
1684 * XXX this isn't a public function! It's a hack for the 3Dfx driver.
1685 * Create a new XMesaBuffer from an X window.
1686 * Input: v - the XMesaVisual
1689 * Return: new XMesaBuffer or NULL if error
1691 XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
, XMesaWindow w
,
1694 #ifndef XFree86Server
1695 XWindowAttributes attr
;
1702 XMesaBuffer b
= alloc_xmesa_buffer();
1709 #ifdef XFree86Server
1710 client
= CLIENT_ID(((XMesaDrawable
)w
)->id
);
1715 #ifdef XFree86Server
1716 if (GET_VISUAL_DEPTH(v
) != ((XMesaDrawable
)w
)->depth
) {
1718 XGetWindowAttributes( v
->display
, w
, &attr
);
1720 if (GET_VISUAL_DEPTH(v
) != attr
.depth
) {
1722 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual and window!\n");
1728 b
->display
= v
->display
;
1729 #ifdef XFree86Server
1730 b
->cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
1732 if (attr
.colormap
) {
1733 b
->cmap
= attr
.colormap
;
1736 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1737 /* this is weird, a window w/out a colormap!? */
1738 /* OK, let's just allocate a new one and hope for the best */
1739 b
->cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1743 /* determine back buffer implementation */
1744 if (v
->mesa_visual
.doubleBufferMode
) {
1745 if (v
->ximage_flag
) {
1746 b
->db_state
= BACK_XIMAGE
;
1749 b
->db_state
= BACK_PIXMAP
;
1756 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1758 v
->mesa_visual
.depthBits
> 0,
1759 v
->mesa_visual
.stencilBits
> 0,
1760 v
->mesa_visual
.accumRedBits
> 0,
1761 v
->mesa_visual
.alphaBits
> 0 );
1763 if (!initialize_visual_and_buffer( client
, v
, b
, v
->mesa_visual
.rgbMode
,
1764 (XMesaDrawable
)w
, b
->cmap
)) {
1765 free_xmesa_buffer(client
, b
);
1770 fxEnvVar
= _mesa_getenv("MESA_GLX_FX");
1772 if (fxEnvVar
[0]!='d') {
1776 if (v
->mesa_visual
.depthBits
> 0) {
1777 attribs
[numAttribs
++] = FXMESA_DEPTH_SIZE
;
1778 attribs
[numAttribs
++] = 1;
1780 if (v
->mesa_visual
.doubleBufferMode
) {
1781 attribs
[numAttribs
++] = FXMESA_DOUBLEBUFFER
;
1783 if (v
->mesa_visual
.accumRedBits
> 0) {
1784 attribs
[numAttribs
++] = FXMESA_ACCUM_SIZE
;
1785 attribs
[numAttribs
++] = v
->mesa_visual
.accumRedBits
;
1787 if (v
->mesa_visual
.stencilBits
> 0) {
1788 attribs
[numAttribs
++] = FXMESA_STENCIL_SIZE
;
1789 attribs
[numAttribs
++] = v
->mesa_visual
.stencilBits
;
1791 if (v
->mesa_visual
.alphaBits
> 0) {
1792 attribs
[numAttribs
++] = FXMESA_ALPHA_SIZE
;
1793 attribs
[numAttribs
++] = 1;
1796 #define FXMESA_SHARE_CONTEXT 990099 /* keep in sync with fxapi.c! */
1797 attribs
[numAttribs
++] = FXMESA_SHARE_CONTEXT
;
1798 attribs
[numAttribs
++] = (int) &(c
->mesa
);
1800 attribs
[numAttribs
++] = FXMESA_NONE
;
1802 if ((hw
= fxQueryHardware())==GR_SSTTYPE_VOODOO
) {
1803 b
->FXctx
= fxMesaCreateBestContext(0, b
->width
, b
->height
, attribs
);
1804 if ((v
->undithered_pf
!=PF_Index
) && (b
->backimage
)) {
1805 b
->FXisHackUsable
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1806 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1807 b
->FXwindowHack
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1809 b
->FXwindowHack
= GL_FALSE
;
1813 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1814 b
->FXctx
= fxMesaCreateContext(w
, GR_RESOLUTION_NONE
,
1815 GR_REFRESH_75Hz
, attribs
);
1817 b
->FXctx
= fxMesaCreateBestContext(0, b
->width
, b
->height
, attribs
);
1818 b
->FXisHackUsable
= GL_FALSE
;
1819 b
->FXwindowHack
= GL_FALSE
;
1823 "voodoo %d, wid %d height %d hack: usable %d active %d\n",
1824 hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack);
1829 _mesa_warning(NULL
, "WARNING: This Mesa Library includes the Glide driver but\n");
1830 _mesa_warning(NULL
, " you have not defined the MESA_GLX_FX env. var.\n");
1831 _mesa_warning(NULL
, " (check the README.3DFX file for more information).\n\n");
1832 _mesa_warning(NULL
, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
1840 XMesaBuffer
XMesaCreateWindowBuffer( XMesaVisual v
, XMesaWindow w
)
1842 return XMesaCreateWindowBuffer2( v
, w
, NULL
);
1847 * Create a new XMesaBuffer from an X pixmap.
1848 * Input: v - the XMesaVisual
1850 * cmap - the colormap, may be 0 if using a TrueColor or DirectColor
1851 * visual for the pixmap
1852 * Return: new XMesaBuffer or NULL if error
1854 XMesaBuffer
XMesaCreatePixmapBuffer( XMesaVisual v
,
1855 XMesaPixmap p
, XMesaColormap cmap
)
1858 XMesaBuffer b
= alloc_xmesa_buffer();
1864 #ifdef XFree86Server
1865 client
= CLIENT_ID(((XMesaDrawable
)p
)->id
);
1872 b
->display
= v
->display
;
1875 /* determine back buffer implementation */
1876 if (v
->mesa_visual
.doubleBufferMode
) {
1877 if (v
->ximage_flag
) {
1878 b
->db_state
= BACK_XIMAGE
;
1881 b
->db_state
= BACK_PIXMAP
;
1888 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1890 v
->mesa_visual
.depthBits
> 0,
1891 v
->mesa_visual
.stencilBits
> 0,
1892 v
->mesa_visual
.accumRedBits
+
1893 v
->mesa_visual
.accumGreenBits
+
1894 v
->mesa_visual
.accumBlueBits
> 0,
1895 v
->mesa_visual
.alphaBits
> 0 );
1897 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1898 (XMesaDrawable
)p
, cmap
)) {
1899 free_xmesa_buffer(client
, b
);
1908 XMesaBuffer
XMesaCreatePBuffer( XMesaVisual v
, XMesaColormap cmap
,
1909 unsigned int width
, unsigned int height
)
1911 #ifdef XFree86Server
1916 XMesaDrawable drawable
; /* X Pixmap Drawable */
1917 XMesaBuffer b
= alloc_xmesa_buffer();
1924 b
->display
= v
->display
;
1927 /* allocate pixmap for front buffer */
1928 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1929 drawable
= XCreatePixmap( v
->display
, root
, width
, height
, v
->visinfo
->depth
);
1931 /* determine back buffer implementation */
1932 if (v
->mesa_visual
.doubleBufferMode
) {
1933 if (v
->ximage_flag
) {
1934 b
->db_state
= BACK_XIMAGE
;
1937 b
->db_state
= BACK_PIXMAP
;
1944 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1946 v
->mesa_visual
.depthBits
> 0,
1947 v
->mesa_visual
.stencilBits
> 0,
1948 v
->mesa_visual
.accumRedBits
+
1949 v
->mesa_visual
.accumGreenBits
+
1950 v
->mesa_visual
.accumBlueBits
> 0,
1951 v
->mesa_visual
.alphaBits
> 0 );
1953 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1955 free_xmesa_buffer(client
, b
);
1966 * Deallocate an XMesaBuffer structure and all related info.
1968 void XMesaDestroyBuffer( XMesaBuffer b
)
1972 #ifdef XFree86Server
1974 client
= CLIENT_ID(b
->frontbuffer
->id
);
1977 if (b
->gc
) XMesaFreeGC( b
->xm_visual
->display
, b
->gc
);
1978 if (b
->cleargc
) XMesaFreeGC( b
->xm_visual
->display
, b
->cleargc
);
1979 if (b
->swapgc
) XMesaFreeGC( b
->xm_visual
->display
, b
->swapgc
);
1982 #if defined(USE_XSHM) && !defined(XFree86Server)
1984 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
1985 XDestroyImage( b
->backimage
);
1986 shmdt( b
->shminfo
.shmaddr
);
1990 XMesaDestroyImage( b
->backimage
);
1992 if (b
->backpixmap
) {
1993 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
1994 if (b
->xm_visual
->hpcr_clear_flag
) {
1995 XMesaFreePixmap( b
->xm_visual
->display
,
1996 b
->xm_visual
->hpcr_clear_pixmap
);
1997 XMesaDestroyImage( b
->xm_visual
->hpcr_clear_ximage
);
2001 FREE( b
->rowimage
->data
);
2002 b
->rowimage
->data
= NULL
;
2003 XMesaDestroyImage( b
->rowimage
);
2006 free_xmesa_buffer(client
, b
);
2012 * Bind buffer b to context c and make c the current rendering context.
2014 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
2016 return XMesaMakeCurrent2( c
, b
, b
);
2021 * Bind buffer b to context c and make c the current rendering context.
2023 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
2024 XMesaBuffer readBuffer
)
2027 if (!drawBuffer
|| !readBuffer
)
2028 return GL_FALSE
; /* must specify buffers! */
2031 if (drawBuffer
->FXctx
) {
2032 fxMesaMakeCurrent(drawBuffer
->FXctx
);
2034 c
->xm_draw_buffer
= drawBuffer
;
2035 c
->xm_read_buffer
= readBuffer
;
2036 c
->xm_buffer
= drawBuffer
;
2041 if (&(c
->mesa
) == _mesa_get_current_context()
2042 && c
->xm_draw_buffer
== drawBuffer
2043 && c
->xm_read_buffer
== readBuffer
2044 && c
->xm_draw_buffer
->wasCurrent
) {
2045 /* same context and buffer, do nothing */
2049 c
->xm_draw_buffer
= drawBuffer
;
2050 c
->xm_read_buffer
= readBuffer
;
2051 c
->xm_buffer
= drawBuffer
;
2053 _mesa_make_current2(&(c
->mesa
),
2054 &drawBuffer
->mesa_buffer
,
2055 &readBuffer
->mesa_buffer
);
2057 if (c
->mesa
.Viewport
.Width
== 0) {
2058 /* initialize viewport to window size */
2059 _mesa_Viewport( 0, 0, drawBuffer
->width
, drawBuffer
->height
);
2060 c
->mesa
.Scissor
.Width
= drawBuffer
->width
;
2061 c
->mesa
.Scissor
.Height
= drawBuffer
->height
;
2064 if (c
->xm_visual
->mesa_visual
.rgbMode
) {
2066 * Must recompute and set these pixel values because colormap
2067 * can be different for different windows.
2069 c
->clearpixel
= xmesa_color_to_pixel( c
,
2074 c
->xm_visual
->undithered_pf
);
2075 XMesaSetForeground(c
->display
, c
->xm_draw_buffer
->cleargc
, c
->clearpixel
);
2078 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
2079 c
->xm_draw_buffer
->wasCurrent
= GL_TRUE
;
2083 _mesa_make_current2( NULL
, NULL
, NULL
);
2090 * Unbind the context c from its buffer.
2092 GLboolean
XMesaUnbindContext( XMesaContext c
)
2094 /* A no-op for XFree86 integration purposes */
2099 XMesaContext
XMesaGetCurrentContext( void )
2101 GET_CURRENT_CONTEXT(ctx
);
2103 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2112 XMesaBuffer
XMesaGetCurrentBuffer( void )
2114 GET_CURRENT_CONTEXT(ctx
);
2116 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2117 return xmesa
->xm_draw_buffer
;
2125 /* New in Mesa 3.1 */
2126 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
2128 GET_CURRENT_CONTEXT(ctx
);
2130 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2131 return xmesa
->xm_read_buffer
;
2139 GLboolean
XMesaForceCurrent(XMesaContext c
)
2142 if (&(c
->mesa
) != _mesa_get_current_context()) {
2143 _mesa_make_current(&(c
->mesa
), &c
->xm_draw_buffer
->mesa_buffer
);
2147 _mesa_make_current(NULL
, NULL
);
2153 GLboolean
XMesaLoseCurrent(XMesaContext c
)
2156 _mesa_make_current(NULL
, NULL
);
2162 * Switch 3Dfx support hack between window and full-screen mode.
2164 GLboolean
XMesaSetFXmode( GLint mode
)
2167 const char *fx
= _mesa_getenv("MESA_GLX_FX");
2168 if (fx
&& fx
[0] != 'd') {
2169 GET_CURRENT_CONTEXT(ctx
);
2170 GrHwConfiguration hw
;
2171 if (!FX_grSstQueryHardware(&hw
)) {
2172 /*fprintf(stderr, "!grSstQueryHardware\n");*/
2175 if (hw
.num_sst
< 1) {
2176 /*fprintf(stderr, "hw.num_sst < 1\n");*/
2180 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2181 if (mode
== XMESA_FX_WINDOW
) {
2182 if (xmesa
->xm_draw_buffer
->FXisHackUsable
) {
2183 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
2184 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_TRUE
;
2188 else if (mode
== XMESA_FX_FULLSCREEN
) {
2189 FX_grSstControl(GR_CONTROL_ACTIVATE
);
2190 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_FALSE
;
2194 /* Error: Bad mode value */
2198 /*fprintf(stderr, "fallthrough\n");*/
2209 * Read image from VooDoo frame buffer into X/Mesa's back XImage.
2211 static void FXgetImage( XMesaBuffer b
)
2213 GET_CURRENT_CONTEXT(ctx
);
2214 static unsigned short pixbuf
[MAX_WIDTH
];
2218 unsigned int bw
, depth
, width
, height
;
2219 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2221 #ifdef XFree86Server
2222 x
= b
->frontbuffer
->x
;
2223 y
= b
->frontbuffer
->y
;
2224 width
= b
->frontbuffer
->width
;
2225 height
= b
->frontbuffer
->height
;
2226 depth
= b
->frontbuffer
->depth
;
2228 XGetGeometry( b
->xm_visual
->display
, b
->frontbuffer
,
2229 &root
, &xpos
, &ypos
, &width
, &height
, &bw
, &depth
);
2231 if (b
->width
!= width
|| b
->height
!= height
) {
2232 b
->width
= MIN2((int)width
, b
->FXctx
->width
);
2233 b
->height
= MIN2((int)height
, b
->FXctx
->height
);
2235 b
->width
--; /* prevent odd width */
2236 xmesa_alloc_back_buffer( b
);
2239 grLfbWriteColorFormat(GR_COLORFORMAT_ARGB
);
2240 if (b
->xm_visual
->undithered_pf
==PF_5R6G5B
) {
2241 /* Special case: 16bpp RGB */
2242 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
, /* src buffer */
2243 0, b
->FXctx
->height
- b
->height
, /*pos*/
2244 b
->width
, b
->height
, /* size */
2245 b
->width
* sizeof(GLushort
), /* stride */
2246 b
->backimage
->data
); /* dest buffer */
2248 else if (b
->xm_visual
->dithered_pf
==PF_Dither
2249 && GET_VISUAL_DEPTH(b
->xm_visual
)==8) {
2250 /* Special case: 8bpp RGB */
2251 for (y
=0;y
<b
->height
;y
++) {
2252 GLubyte
*ptr
= (GLubyte
*) b
->backimage
->data
2253 + b
->backimage
->bytes_per_line
* y
;
2256 /* read row from 3Dfx frame buffer */
2257 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2258 0, b
->FXctx
->height
-(b
->height
-y
),
2263 /* write to XImage back buffer */
2264 for (x
=0;x
<b
->width
;x
++) {
2265 GLubyte r
= (pixbuf
[x
] & 0xf800) >> 8;
2266 GLubyte g
= (pixbuf
[x
] & 0x07e0) >> 3;
2267 GLubyte b
= (pixbuf
[x
] & 0x001f) << 3;
2268 *ptr
++ = XDITHER( x
, r
, g
, b
);
2273 /* General case: slow! */
2274 for (y
=0;y
<b
->height
;y
++) {
2275 /* read row from 3Dfx frame buffer */
2276 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2277 0, b
->FXctx
->height
-(b
->height
-y
),
2282 /* write to XImage back buffer */
2283 for (x
=0;x
<b
->width
;x
++) {
2284 XMesaPutPixel(b
->backimage
,x
,y
,
2285 xmesa_color_to_pixel(xmesa
,
2286 (pixbuf
[x
] & 0xf800) >> 8,
2287 (pixbuf
[x
] & 0x07e0) >> 3,
2288 (pixbuf
[x
] & 0x001f) << 3,
2290 b
->xm_visual
->undithered_pf
));
2294 grLfbWriteColorFormat(GR_COLORFORMAT_ABGR
);
2300 * Copy the back buffer to the front buffer. If there's no back buffer
2303 void XMesaSwapBuffers( XMesaBuffer b
)
2305 GET_CURRENT_CONTEXT(ctx
);
2307 /* If we're swapping the buffer associated with the current context
2308 * we have to flush any pending rendering commands first.
2310 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2311 _mesa_notifySwapBuffers(ctx
);
2316 fxMesaSwapBuffers();
2318 if (b
->FXwindowHack
)
2325 /* Copy Ximage from host's memory to server's window */
2326 #if defined(USE_XSHM) && !defined(XFree86Server)
2328 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2329 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2332 0, 0, b
->width
, b
->height
, False
);
2333 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2338 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2339 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2342 0, 0, b
->width
, b
->height
);
2343 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2347 /* Copy pixmap to window on server */
2348 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2349 XMesaCopyArea( b
->xm_visual
->display
,
2350 b
->backpixmap
, /* source drawable */
2351 b
->frontbuffer
, /* dest. drawable */
2353 0, 0, b
->width
, b
->height
, /* source region */
2354 0, 0 /* dest region */
2356 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2359 #if !defined(XFree86Server)
2360 XSync( b
->xm_visual
->display
, False
);
2367 * Copy sub-region of back buffer to front buffer
2369 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
2371 GET_CURRENT_CONTEXT(ctx
);
2373 /* If we're swapping the buffer associated with the current context
2374 * we have to flush any pending rendering commands first.
2376 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2377 _mesa_notifySwapBuffers(ctx
);
2380 int yTop
= b
->height
- y
- height
;
2383 fxMesaSwapBuffers();
2384 if (b
->FXwindowHack
)
2391 /* Copy Ximage from host's memory to server's window */
2392 #if defined(USE_XSHM) && !defined(XFree86Server)
2394 /* XXX assuming width and height aren't too large! */
2395 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2397 b
->backimage
, x
, yTop
,
2398 x
, yTop
, width
, height
, False
);
2399 /* wait for finished event??? */
2404 /* XXX assuming width and height aren't too large! */
2405 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2407 b
->backimage
, x
, yTop
,
2408 x
, yTop
, width
, height
);
2412 /* Copy pixmap to window on server */
2413 XMesaCopyArea( b
->xm_visual
->display
,
2414 b
->backpixmap
, /* source drawable */
2415 b
->frontbuffer
, /* dest. drawable */
2417 x
, yTop
, width
, height
, /* source region */
2418 x
, yTop
/* dest region */
2426 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2427 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2428 * back buffer directly.
2429 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2430 * ximage - pointer to back buffer's XImage, or NULL
2431 * Return: GL_TRUE = context is double buffered
2432 * GL_FALSE = context is single buffered
2434 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
2435 XMesaPixmap
*pixmap
,
2436 XMesaImage
**ximage
)
2439 if (pixmap
) *pixmap
= b
->backpixmap
;
2440 if (ximage
) *ximage
= b
->backimage
;
2452 * Return the depth buffer associated with an XMesaBuffer.
2453 * Input: b - the XMesa buffer handle
2454 * Output: width, height - size of buffer in pixels
2455 * bytesPerValue - bytes per depth value (2 or 4)
2456 * buffer - pointer to depth buffer values
2457 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2459 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
2460 GLint
*bytesPerValue
, void **buffer
)
2462 if (!b
->mesa_buffer
.DepthBuffer
) {
2470 *width
= b
->mesa_buffer
.Width
;
2471 *height
= b
->mesa_buffer
.Height
;
2472 *bytesPerValue
= sizeof(GLdepth
);
2473 *buffer
= b
->mesa_buffer
.DepthBuffer
;
2479 void XMesaFlush( XMesaContext c
)
2481 if (c
&& c
->xm_visual
) {
2482 #ifdef XFree86Server
2485 XSync( c
->xm_visual
->display
, False
);
2492 const char *XMesaGetString( XMesaContext c
, int name
)
2495 if (name
==XMESA_VERSION
) {
2498 else if (name
==XMESA_EXTENSIONS
) {
2508 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
2511 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
2512 if (b
->frontbuffer
==d
&& b
->display
==dpy
) {
2522 * Look for XMesaBuffers whose X window has been destroyed.
2523 * Deallocate any such XMesaBuffers.
2525 void XMesaGarbageCollect( void )
2527 XMesaBuffer b
, next
;
2528 for (b
=XMesaBufferList
; b
; b
=next
) {
2530 if (b
->display
&& b
->frontbuffer
&& b
->type
== WINDOW
) {
2531 #ifdef XFree86Server
2534 XSync(b
->display
, False
);
2535 if (!window_exists( b
->display
, b
->frontbuffer
)) {
2536 /* found a dead window, free the ancillary info */
2537 XMesaDestroyBuffer( b
);
2545 void XMesaReset( void )
2547 while (XMesaBufferList
)
2548 XMesaDestroyBuffer(XMesaBufferList
);
2550 XMesaBufferList
= NULL
;
2554 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
2555 GLfloat red
, GLfloat green
,
2556 GLfloat blue
, GLfloat alpha
)
2558 GLint r
= (GLint
) (red
* 255.0F
);
2559 GLint g
= (GLint
) (green
* 255.0F
);
2560 GLint b
= (GLint
) (blue
* 255.0F
);
2561 GLint a
= (GLint
) (alpha
* 255.0F
);
2563 switch (xmesa
->pixelformat
) {
2569 PACK_TRUECOLOR( p
, r
, g
, b
);
2573 return PACK_8A8B8G8R( r
, g
, b
, a
);
2575 return PACK_8R8G8B( r
, g
, b
);
2577 return PACK_5R6G5B( r
, g
, b
);
2581 return DITHER( x
, y
, r
, g
, b
);
2584 /* 382 = (3*255)/2 */
2585 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
2587 return DITHER_HPCR(x
, y
, r
, g
, b
);
2591 return LOOKUP( r
, g
, b
);
2594 return GRAY_RGB( r
, g
, b
);
2595 case PF_Dither_5R6G5B
:
2597 case PF_Dither_True
:
2600 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
2604 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
2611 * This is typically called when the window size changes and we need
2612 * to reallocate the buffer's back/depth/stencil/accum buffers.
2614 void XMesaResizeBuffers( XMesaBuffer b
)
2616 xmesa_resize_buffers( &(b
->mesa_buffer
) );