2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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.
26 * This file contains the implementations of all the XMesa* functions.
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.
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.
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.
63 #include "glxheader.h"
67 #include "extensions.h"
73 #include "texformat.h"
76 #include "swrast/swrast.h"
77 #include "swrast_setup/swrast_setup.h"
78 #include "array_cache/acache.h"
80 #include "tnl/t_context.h"
81 #include "tnl/t_pipeline.h"
82 #include "drivers/common/driverfuncs.h"
85 #include <GL/glxtokens.h>
89 * Global X driver lock
91 _glthread_Mutex _xmesa_lock
;
96 * Lookup tables for HPCR pixel format:
98 static short hpcr_rgbTbl
[3][256] = {
100 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
101 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
102 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
103 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
104 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
105 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
106 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
107 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
108 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
109 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
110 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
111 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
112 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
113 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
114 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
115 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
118 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
119 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
120 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
121 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
122 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
123 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
124 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
125 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
126 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
127 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
128 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
129 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
130 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
131 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
132 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
133 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
136 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
137 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
138 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
139 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
140 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
141 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
142 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
143 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
144 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
145 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
146 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
147 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
148 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
149 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
150 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
151 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
157 /**********************************************************************/
158 /***** X Utility Functions *****/
159 /**********************************************************************/
163 * Return the host's byte order as LSBFirst or MSBFirst ala X.
165 #ifndef XFree86Server
166 static int host_byte_order( void )
169 char *cptr
= (char *) &i
;
170 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
178 #ifndef XFree86Server
179 static volatile int mesaXErrorFlag
= 0;
181 static int mesaHandleXError( XMesaDisplay
*dpy
, XErrorEvent
*event
)
192 * Check if the X Shared Memory extension is available.
193 * Return: 0 = not available
194 * 1 = shared XImage support available
195 * 2 = shared Pixmap support available also
197 #ifndef XFree86Server
198 static int check_for_xshm( XMesaDisplay
*display
)
201 int major
, minor
, ignore
;
204 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
205 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
206 return (pixmaps
==True
) ? 2 : 1;
216 /* Can't compile XSHM support */
224 * Apply gamma correction to an intensity value in [0..max]. Return the
225 * new intensity value.
227 static GLint
gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
233 double x
= (double) value
/ (double) max
;
234 return IROUND_POS((GLfloat
) max
* _mesa_pow(x
, 1.0F
/gamma
));
241 * Return the true number of bits per pixel for XImages.
242 * For example, if we request a 24-bit deep visual we may actually need/get
243 * 32bpp XImages. This function returns the appropriate bpp.
244 * Input: dpy - the X display
245 * visinfo - desribes the visual to be used for XImages
246 * Return: true number of bits per pixel for XImages
250 static int bits_per_pixel( XMesaVisual xmv
)
252 const int depth
= xmv
->nplanes
;
254 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
255 if (screenInfo
.formats
[i
].depth
== depth
)
256 return screenInfo
.formats
[i
].bitsPerPixel
;
258 return depth
; /* should never get here, but this should be safe */
263 static int bits_per_pixel( XMesaVisual xmv
)
265 XMesaDisplay
*dpy
= xmv
->display
;
266 XMesaVisualInfo visinfo
= xmv
->visinfo
;
269 /* Create a temporary XImage */
270 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
271 ZPixmap
, 0, /*format, offset*/
272 (char*) MALLOC(8), /*data*/
273 1, 1, /*width, height*/
278 /* grab the bits/pixel value */
279 bitsPerPixel
= img
->bits_per_pixel
;
280 /* free the XImage */
283 XMesaDestroyImage( img
);
291 * Determine if a given X window ID is valid (window exists).
292 * Do this by calling XGetWindowAttributes() for the window and
293 * checking if we catch an X error.
294 * Input: dpy - the display
295 * win - the window to check for existance
296 * Return: GL_TRUE - window exists
297 * GL_FALSE - window doesn't exist
299 #ifndef XFree86Server
300 static GLboolean WindowExistsFlag
;
302 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
305 if (xerr
->error_code
== BadWindow
) {
306 WindowExistsFlag
= GL_FALSE
;
311 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
313 XWindowAttributes wa
;
314 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
315 WindowExistsFlag
= GL_TRUE
;
316 old_handler
= XSetErrorHandler(window_exists_err_handler
);
317 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
318 XSetErrorHandler(old_handler
);
319 return WindowExistsFlag
;
325 /**********************************************************************/
326 /***** Linked list of XMesaBuffers *****/
327 /**********************************************************************/
329 static XMesaBuffer XMesaBufferList
= NULL
;
332 /* Allocate a new XMesaBuffer, add to linked list */
333 static XMesaBuffer
alloc_xmesa_buffer(void)
335 XMesaBuffer b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
337 b
->Next
= XMesaBufferList
;
345 * Find an XMesaBuffer by matching X display and colormap but NOT matching
346 * the notThis buffer.
348 static XMesaBuffer
find_xmesa_buffer(XMesaDisplay
*dpy
,
353 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
354 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
363 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
366 static void free_xmesa_buffer(int client
, XMesaBuffer buffer
)
368 XMesaBuffer prev
= NULL
, b
;
370 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
372 /* unlink bufer from list */
374 prev
->Next
= buffer
->Next
;
376 XMesaBufferList
= buffer
->Next
;
377 /* Check to free X colors */
378 if (buffer
->num_alloced
>0) {
379 /* If no other buffer uses this X colormap then free the colors. */
380 if (!find_xmesa_buffer(buffer
->display
, buffer
->cmap
, buffer
)) {
382 (void)FreeColors(buffer
->cmap
, client
,
383 buffer
->num_alloced
, buffer
->alloced_colors
,
386 XFreeColors(buffer
->display
, buffer
->cmap
,
387 buffer
->alloced_colors
, buffer
->num_alloced
, 0);
392 _mesa_free_framebuffer_data(&buffer
->mesa_buffer
);
397 /* continue search */
400 /* buffer not found in XMesaBufferList */
401 _mesa_problem(NULL
,"free_xmesa_buffer() - buffer not found\n");
405 /* Copy X color table stuff from one XMesaBuffer to another. */
406 static void copy_colortable_info(XMesaBuffer dst
, const XMesaBuffer src
)
408 MEMCPY(dst
->color_table
, src
->color_table
, sizeof(src
->color_table
));
409 MEMCPY(dst
->pixel_to_r
, src
->pixel_to_r
, sizeof(src
->pixel_to_r
));
410 MEMCPY(dst
->pixel_to_g
, src
->pixel_to_g
, sizeof(src
->pixel_to_g
));
411 MEMCPY(dst
->pixel_to_b
, src
->pixel_to_b
, sizeof(src
->pixel_to_b
));
412 dst
->num_alloced
= src
->num_alloced
;
413 MEMCPY(dst
->alloced_colors
, src
->alloced_colors
,
414 sizeof(src
->alloced_colors
));
419 /**********************************************************************/
420 /***** Misc Private Functions *****/
421 /**********************************************************************/
425 * Return number of bits set in n.
427 static int bitcount( unsigned long n
)
430 for (bits
=0; n
>0; n
=n
>>1) {
441 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
442 * Return: GL_TRUE if success, GL_FALSE if error
444 #ifndef XFree86Server
445 static GLboolean
alloc_shm_back_buffer( XMesaBuffer b
)
449 * We have to do a _lot_ of error checking here to be sure we can
450 * really use the XSHM extension. It seems different servers trigger
451 * errors at different points if the extension won't work. Therefore
452 * we have to be very careful...
455 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
457 if (b
->mesa_buffer
.Width
== 0 || b
->mesa_buffer
.Height
== 0) {
458 /* this will be true the first time we're called on 'b' */
462 b
->backimage
= XShmCreateImage(b
->xm_visual
->display
,
463 b
->xm_visual
->visinfo
->visual
,
464 b
->xm_visual
->visinfo
->depth
,
465 ZPixmap
, NULL
, &b
->shminfo
,
466 b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
467 if (b
->backimage
== NULL
) {
468 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
473 b
->shminfo
.shmid
= shmget( IPC_PRIVATE
, b
->backimage
->bytes_per_line
474 * b
->backimage
->height
, IPC_CREAT
|0777 );
475 if (b
->shminfo
.shmid
< 0) {
476 _mesa_warning(NULL
, "shmget failed while allocating back buffer");
477 XDestroyImage( b
->backimage
);
479 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmget), disabling.");
484 b
->shminfo
.shmaddr
= b
->backimage
->data
485 = (char*)shmat( b
->shminfo
.shmid
, 0, 0 );
486 if (b
->shminfo
.shmaddr
== (char *) -1) {
487 _mesa_warning(NULL
, "shmat() failed while allocating back buffer");
488 XDestroyImage( b
->backimage
);
489 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
491 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmat), disabling.");
496 b
->shminfo
.readOnly
= False
;
498 old_handler
= XSetErrorHandler( mesaHandleXError
);
499 /* This may trigger the X protocol error we're ready to catch: */
500 XShmAttach( b
->xm_visual
->display
, &b
->shminfo
);
501 XSync( b
->xm_visual
->display
, False
);
503 if (mesaXErrorFlag
) {
504 /* we are on a remote display, this error is normal, don't print it */
505 XFlush( b
->xm_visual
->display
);
507 XDestroyImage( b
->backimage
);
508 shmdt( b
->shminfo
.shmaddr
);
509 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
512 (void) XSetErrorHandler( old_handler
);
516 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 ); /* nobody else needs it */
518 /* Finally, try an XShmPutImage to be really sure the extension works */
519 gc
= XCreateGC( b
->xm_visual
->display
, b
->frontbuffer
, 0, NULL
);
520 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
, gc
,
521 b
->backimage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
522 XSync( b
->xm_visual
->display
, False
);
523 XFreeGC( b
->xm_visual
->display
, gc
);
524 (void) XSetErrorHandler( old_handler
);
525 if (mesaXErrorFlag
) {
526 XFlush( b
->xm_visual
->display
);
528 XDestroyImage( b
->backimage
);
529 shmdt( b
->shminfo
.shmaddr
);
530 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
537 int height
= b
->backimage
->height
;
538 /* Needed by PIXELADDR1 macro */
539 b
->ximage_width1
= b
->backimage
->bytes_per_line
;
540 b
->ximage_origin1
= (GLubyte
*) b
->backimage
->data
541 + b
->ximage_width1
* (height
-1);
542 /* Needed by PIXELADDR2 macro */
543 b
->ximage_width2
= b
->backimage
->bytes_per_line
/ 2;
544 b
->ximage_origin2
= (GLushort
*) b
->backimage
->data
545 + b
->ximage_width2
* (height
-1);
546 /* Needed by PIXELADDR3 macro */
547 b
->ximage_width3
= b
->backimage
->bytes_per_line
;
548 b
->ximage_origin3
= (GLubyte
*) b
->backimage
->data
549 + b
->ximage_width3
* (height
-1);
550 /* Needed by PIXELADDR4 macro */
551 b
->ximage_width4
= b
->backimage
->width
;
552 b
->ximage_origin4
= (GLuint
*) b
->backimage
->data
553 + b
->ximage_width4
* (height
-1);
558 /* Can't compile XSHM support */
567 * Setup an off-screen pixmap or Ximage to use as the back buffer.
568 * Input: b - the X/Mesa buffer
570 void xmesa_alloc_back_buffer( XMesaBuffer b
)
572 if (b
->db_state
==BACK_XIMAGE
) {
573 /* Deallocate the old backimage, if any */
575 #if defined(USE_XSHM) && !defined(XFree86Server)
577 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
578 XDestroyImage( b
->backimage
);
579 shmdt( b
->shminfo
.shmaddr
);
583 XMesaDestroyImage( b
->backimage
);
587 /* Allocate new back buffer */
590 /* Allocate a regular XImage for the back buffer. */
591 b
->backimage
= XMesaCreateImage(b
->xm_visual
->BitsPerPixel
,
592 b
->mesa_buffer
.Width
,
593 b
->mesa_buffer
.Height
, NULL
);
595 if (b
->shm
==0 || alloc_shm_back_buffer(b
)==GL_FALSE
) {
596 /* Allocate a regular XImage for the back buffer. */
597 b
->backimage
= XCreateImage( b
->xm_visual
->display
,
598 b
->xm_visual
->visinfo
->visual
,
599 GET_VISUAL_DEPTH(b
->xm_visual
),
600 ZPixmap
, 0, /* format, offset */
602 b
->mesa_buffer
.Width
,
603 b
->mesa_buffer
.Height
,
604 8, 0 ); /* pad, bytes_per_line */
607 _mesa_warning(NULL
, "alloc_back_buffer: XCreateImage failed.");
609 b
->backimage
->data
= (char *) MALLOC( b
->backimage
->height
610 * b
->backimage
->bytes_per_line
);
611 if (!b
->backimage
->data
) {
612 _mesa_warning(NULL
, "alloc_back_buffer: MALLOC failed.");
613 XMesaDestroyImage( b
->backimage
);
617 b
->backpixmap
= None
;
619 else if (b
->db_state
==BACK_PIXMAP
) {
620 XMesaPixmap old_pixmap
= b
->backpixmap
;
621 /* Free the old back pixmap */
623 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
625 /* Allocate new back pixmap */
626 b
->backpixmap
= XMesaCreatePixmap( b
->xm_visual
->display
, b
->frontbuffer
,
627 b
->mesa_buffer
.Width
,
628 b
->mesa_buffer
.Height
,
629 GET_VISUAL_DEPTH(b
->xm_visual
) );
631 /* update other references to backpixmap */
632 if (b
->buffer
==(XMesaDrawable
)old_pixmap
) {
633 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
641 * A replacement for XAllocColor. This function should never
642 * fail to allocate a color. When XAllocColor fails, we return
643 * the nearest matching color. If we have to allocate many colors
644 * this function isn't too efficient; the XQueryColors() could be
646 * Written by Michael Pichler, Brian Paul, Mark Kilgard
647 * Input: dpy - X display
649 * cmapSize - size of colormap
650 * In/Out: color - the XColor struct
651 * Output: exact - 1=exact color match, 0=closest match
652 * alloced - 1=XAlloc worked, 0=XAlloc failed
655 noFaultXAllocColor( int client
,
660 int *exact
, int *alloced
)
666 /* we'll try to cache ctable for better remote display performance */
667 static Display
*prevDisplay
= NULL
;
668 static XMesaColormap prevCmap
= 0;
669 static int prevCmapSize
= 0;
670 static XMesaColor
*ctable
= NULL
;
674 double mindist
; /* 3*2^16^2 exceeds long int precision. */
678 /* First try just using XAllocColor. */
681 &color
->red
, &color
->green
, &color
->blue
,
683 client
) == Success
) {
685 if (XAllocColor(dpy
, cmap
, color
)) {
692 /* Alloc failed, search for closest match */
694 /* Retrieve color table entries. */
695 /* XXX alloca candidate. */
697 ppixIn
= (Pixel
*) MALLOC(cmapSize
* sizeof(Pixel
));
698 ctable
= (xrgb
*) MALLOC(cmapSize
* sizeof(xrgb
));
699 for (i
= 0; i
< cmapSize
; i
++) {
702 QueryColors(cmap
, cmapSize
, ppixIn
, ctable
);
704 if (prevDisplay
!= dpy
|| prevCmap
!= cmap
705 || prevCmapSize
!= cmapSize
|| !ctable
) {
706 /* free previously cached color table */
709 /* Get the color table from X */
710 ctable
= (XMesaColor
*) MALLOC(cmapSize
* sizeof(XMesaColor
));
712 for (i
= 0; i
< cmapSize
; i
++) {
715 XQueryColors(dpy
, cmap
, ctable
, cmapSize
);
718 prevCmapSize
= cmapSize
;
722 /* Find best match. */
725 for (i
= 0; i
< cmapSize
; i
++) {
726 double dr
= 0.30 * ((double) color
->red
- (double) ctable
[i
].red
);
727 double dg
= 0.59 * ((double) color
->green
- (double) ctable
[i
].green
);
728 double db
= 0.11 * ((double) color
->blue
- (double) ctable
[i
].blue
);
729 double dist
= dr
* dr
+ dg
* dg
+ db
* db
;
730 if (bestmatch
< 0 || dist
< mindist
) {
737 subColor
.red
= ctable
[bestmatch
].red
;
738 subColor
.green
= ctable
[bestmatch
].green
;
739 subColor
.blue
= ctable
[bestmatch
].blue
;
740 /* Try to allocate the closest match color. This should only
741 * fail if the cell is read/write. Otherwise, we're incrementing
742 * the cell's reference count.
746 &subColor
.red
, &subColor
.green
, &subColor
.blue
,
748 client
) == Success
) {
750 if (XAllocColor(dpy
, cmap
, &subColor
)) {
755 /* do this to work around a problem reported by Frank Ortega */
756 subColor
.pixel
= (unsigned long) bestmatch
;
757 subColor
.red
= ctable
[bestmatch
].red
;
758 subColor
.green
= ctable
[bestmatch
].green
;
759 subColor
.blue
= ctable
[bestmatch
].blue
;
760 subColor
.flags
= DoRed
| DoGreen
| DoBlue
;
767 /* don't free table, save it for next time */
778 * Do setup for PF_GRAYSCALE pixel format.
779 * Note that buffer may be NULL.
781 static GLboolean
setup_grayscale( int client
, XMesaVisual v
,
782 XMesaBuffer buffer
, XMesaColormap cmap
)
784 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
789 XMesaBuffer prevBuffer
;
795 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
797 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
798 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
799 /* Copy colormap stuff from previous XMesaBuffer which uses same
800 * X colormap. Do this to avoid time spent in noFaultXAllocColor.
802 copy_colortable_info(buffer
, prevBuffer
);
805 /* Allocate 256 shades of gray */
807 int colorsfailed
= 0;
808 for (gray
=0;gray
<256;gray
++) {
809 GLint r
= gamma_adjust( v
->RedGamma
, gray
, 255 );
810 GLint g
= gamma_adjust( v
->GreenGamma
, gray
, 255 );
811 GLint b
= gamma_adjust( v
->BlueGamma
, gray
, 255 );
814 xcol
.red
= (r
<< 8) | r
;
815 xcol
.green
= (g
<< 8) | g
;
816 xcol
.blue
= (b
<< 8) | b
;
817 noFaultXAllocColor( client
, v
->display
,
818 cmap
, GET_COLORMAP_SIZE(v
),
819 &xcol
, &exact
, &alloced
);
824 assert(buffer
->num_alloced
<256);
825 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
826 buffer
->num_alloced
++;
831 buffer->color_table[gray*3+0] = xcol.pixel;
832 buffer->color_table[gray*3+1] = xcol.pixel;
833 buffer->color_table[gray*3+2] = xcol.pixel;
834 assert(xcol.pixel < 65536);
835 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
836 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
837 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
839 buffer
->color_table
[gray
] = xcol
.pixel
;
840 assert(xcol
.pixel
< 65536);
841 buffer
->pixel_to_r
[xcol
.pixel
] = gray
;
842 buffer
->pixel_to_g
[xcol
.pixel
] = gray
;
843 buffer
->pixel_to_b
[xcol
.pixel
] = gray
;
846 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
848 "Note: %d out of 256 needed colors do not match exactly.\n",
854 v
->dithered_pf
= PF_Grayscale
;
855 v
->undithered_pf
= PF_Grayscale
;
862 * Setup RGB rendering for a window with a PseudoColor, StaticColor,
863 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
864 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
865 * color. While this function was originally designed just for 8-bit
866 * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
867 * Dithering code contributed by Bob Mercier.
869 static GLboolean
setup_dithered_color( int client
, XMesaVisual v
,
870 XMesaBuffer buffer
, XMesaColormap cmap
)
872 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
877 XMesaBuffer prevBuffer
;
883 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
885 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
886 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
887 /* Copy colormap stuff from previous, matching XMesaBuffer.
888 * Do this to avoid time spent in noFaultXAllocColor.
890 copy_colortable_info(buffer
, prevBuffer
);
893 /* Allocate X colors and initialize color_table[], red_table[], etc */
895 int colorsfailed
= 0;
896 for (r
= 0; r
< DITH_R
; r
++) {
897 for (g
= 0; g
< DITH_G
; g
++) {
898 for (b
= 0; b
< DITH_B
; b
++) {
901 xcol
.red
=gamma_adjust(v
->RedGamma
, r
*65535/(DITH_R
-1),65535);
902 xcol
.green
=gamma_adjust(v
->GreenGamma
, g
*65535/(DITH_G
-1),65535);
903 xcol
.blue
=gamma_adjust(v
->BlueGamma
, b
*65535/(DITH_B
-1),65535);
904 noFaultXAllocColor( client
, v
->display
,
905 cmap
, GET_COLORMAP_SIZE(v
),
906 &xcol
, &exact
, &alloced
);
911 assert(buffer
->num_alloced
<256);
912 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
913 buffer
->num_alloced
++;
915 i
= DITH_MIX( r
, g
, b
);
917 buffer
->color_table
[i
] = xcol
.pixel
;
918 assert(xcol
.pixel
< 65536);
919 buffer
->pixel_to_r
[xcol
.pixel
] = r
* 255 / (DITH_R
-1);
920 buffer
->pixel_to_g
[xcol
.pixel
] = g
* 255 / (DITH_G
-1);
921 buffer
->pixel_to_b
[xcol
.pixel
] = b
* 255 / (DITH_B
-1);
926 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
928 "Note: %d out of %d needed colors do not match exactly.\n",
929 colorsfailed
, DITH_R
* DITH_G
* DITH_B
);
934 v
->dithered_pf
= PF_Dither
;
935 v
->undithered_pf
= PF_Lookup
;
941 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
942 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
943 * Special dithering tables have to be initialized.
945 static void setup_8bit_hpcr( XMesaVisual v
)
947 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
948 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
949 * on the root window AND the colormap obtainable by XGetRGBColormaps
950 * for that atom must be set on the window. (see also tkInitWindow)
951 * If that colormap is not set, the output will look stripy.
954 /* Setup color tables with gamma correction */
958 g
= 1.0 / v
->RedGamma
;
959 for (i
=0; i
<256; i
++) {
960 GLint red
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[0][i
]/255.0, g
));
961 v
->hpcr_rgbTbl
[0][i
] = CLAMP( red
, 16, 239 );
964 g
= 1.0 / v
->GreenGamma
;
965 for (i
=0; i
<256; i
++) {
966 GLint green
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[1][i
]/255.0, g
));
967 v
->hpcr_rgbTbl
[1][i
] = CLAMP( green
, 16, 239 );
970 g
= 1.0 / v
->BlueGamma
;
971 for (i
=0; i
<256; i
++) {
972 GLint blue
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[2][i
]/255.0, g
));
973 v
->hpcr_rgbTbl
[2][i
] = CLAMP( blue
, 32, 223 );
975 v
->undithered_pf
= PF_HPCR
; /* can't really disable dithering for now */
976 v
->dithered_pf
= PF_HPCR
;
978 /* which method should I use to clear */
979 /* GL_FALSE: keep the ordinary method */
980 /* GL_TRUE : clear with dither pattern */
981 v
->hpcr_clear_flag
= _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE
: GL_FALSE
;
983 if (v
->hpcr_clear_flag
) {
984 v
->hpcr_clear_pixmap
= XMesaCreatePixmap(v
->display
,
985 DefaultRootWindow(v
->display
),
987 #ifndef XFree86Server
988 v
->hpcr_clear_ximage
= XGetImage(v
->display
, v
->hpcr_clear_pixmap
,
989 0, 0, 16, 2, AllPlanes
, ZPixmap
);
996 * Setup RGB rendering for a window with a True/DirectColor visual.
998 static void setup_truecolor( XMesaVisual v
, XMesaBuffer buffer
,
1001 unsigned long rmask
, gmask
, bmask
;
1005 /* Compute red multiplier (mask) and bit shift */
1007 rmask
= GET_REDMASK(v
);
1008 while ((rmask
& 1)==0) {
1013 /* Compute green multiplier (mask) and bit shift */
1015 gmask
= GET_GREENMASK(v
);
1016 while ((gmask
& 1)==0) {
1021 /* Compute blue multiplier (mask) and bit shift */
1023 bmask
= GET_BLUEMASK(v
);
1024 while ((bmask
& 1)==0) {
1030 * Compute component-to-pixel lookup tables and dithering kernel
1033 static GLubyte kernel
[16] = {
1034 0*16, 8*16, 2*16, 10*16,
1035 12*16, 4*16, 14*16, 6*16,
1036 3*16, 11*16, 1*16, 9*16,
1037 15*16, 7*16, 13*16, 5*16,
1039 GLint rBits
= bitcount(rmask
);
1040 GLint gBits
= bitcount(gmask
);
1041 GLint bBits
= bitcount(bmask
);
1045 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
1046 for (i
=0; i
<=rmask
; i
++)
1047 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
1048 for (i
=0; i
<=gmask
; i
++)
1049 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
1050 for (i
=0; i
<=bmask
; i
++)
1051 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
1053 /* convert RGB values from [0,255] to pixel components */
1055 for (i
=0;i
<256;i
++) {
1056 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
1057 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
1058 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
1059 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
1060 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
1061 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
1063 /* overflow protection */
1064 for (i
=256;i
<512;i
++) {
1065 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
1066 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
1067 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
1070 /* setup dithering kernel */
1072 if (gBits
> maxBits
) maxBits
= gBits
;
1073 if (bBits
> maxBits
) maxBits
= bBits
;
1074 for (i
=0;i
<16;i
++) {
1075 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
1078 v
->undithered_pf
= PF_Truecolor
;
1079 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
1083 * Now check for TrueColor visuals which we can optimize.
1085 if ( GET_REDMASK(v
) ==0x0000ff
1086 && GET_GREENMASK(v
)==0x00ff00
1087 && GET_BLUEMASK(v
) ==0xff0000
1088 && CHECK_BYTE_ORDER(v
)
1089 && v
->BitsPerPixel
==32
1090 && sizeof(GLuint
)==4
1091 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1092 /* common 32 bpp config used on SGI, Sun */
1093 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
;
1095 else if (GET_REDMASK(v
) ==0xff0000
1096 && GET_GREENMASK(v
)==0x00ff00
1097 && GET_BLUEMASK(v
) ==0x0000ff
1098 && CHECK_BYTE_ORDER(v
)
1099 && v
->BitsPerPixel
==32
1100 && sizeof(GLuint
)==4
1101 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1102 /* common 32 bpp config used on Linux, HP, IBM */
1103 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
;
1105 else if (GET_REDMASK(v
) ==0xff0000
1106 && GET_GREENMASK(v
)==0x00ff00
1107 && GET_BLUEMASK(v
) ==0x0000ff
1108 && CHECK_BYTE_ORDER(v
)
1109 && v
->BitsPerPixel
==24
1110 && sizeof(GLuint
)==4
1111 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1112 /* common packed 24 bpp config used on Linux */
1113 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
;
1115 else if (GET_REDMASK(v
) ==0xf800
1116 && GET_GREENMASK(v
)==0x07e0
1117 && GET_BLUEMASK(v
) ==0x001f
1118 && CHECK_BYTE_ORDER(v
)
1119 && v
->BitsPerPixel
==16
1120 && sizeof(GLushort
)==2
1121 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1122 /* 5-6-5 color weight on common PC VGA boards */
1123 v
->undithered_pf
= PF_5R6G5B
;
1124 v
->dithered_pf
= PF_Dither_5R6G5B
;
1126 else if (GET_REDMASK(v
) ==0xe0
1127 && GET_GREENMASK(v
)==0x1c
1128 && GET_BLUEMASK(v
) ==0x03
1129 && CHECK_FOR_HPCR(v
)) {
1130 setup_8bit_hpcr( v
);
1137 * Setup RGB rendering for a window with a monochrome visual.
1139 static void setup_monochrome( XMesaVisual v
, XMesaBuffer b
)
1142 v
->dithered_pf
= v
->undithered_pf
= PF_1Bit
;
1143 /* if black=1 then we must flip pixel values */
1144 v
->bitFlip
= (GET_BLACK_PIXEL(v
) != 0);
1150 * When a context is "made current" for the first time, we can finally
1151 * finish initializing the context's visual and buffer information.
1152 * Input: v - the XMesaVisual to initialize
1153 * b - the XMesaBuffer to initialize (may be NULL)
1154 * rgb_flag - TRUE = RGBA mode, FALSE = color index mode
1155 * window - the window/pixmap we're rendering into
1156 * cmap - the colormap associated with the window/pixmap
1157 * Return: GL_TRUE=success, GL_FALSE=failure
1159 static GLboolean
initialize_visual_and_buffer( int client
,
1163 XMesaDrawable window
,
1164 XMesaColormap cmap
)
1166 #ifndef XFree86Server
1171 assert(b
->xm_visual
== v
);
1174 /* Save true bits/pixel */
1175 v
->BitsPerPixel
= bits_per_pixel(v
);
1176 assert(v
->BitsPerPixel
> 0);
1179 if (rgb_flag
==GL_FALSE
) {
1180 /* COLOR-INDEXED WINDOW:
1181 * Even if the visual is TrueColor or DirectColor we treat it as
1182 * being color indexed. This is weird but might be useful to someone.
1184 v
->dithered_pf
= v
->undithered_pf
= PF_Index
;
1185 v
->mesa_visual
.indexBits
= GET_VISUAL_DEPTH(v
);
1189 * We support RGB rendering into almost any kind of visual.
1191 const int xclass
= v
->mesa_visual
.visualType
;
1192 if (xclass
==GLX_TRUE_COLOR
|| xclass
==GLX_DIRECT_COLOR
) {
1193 setup_truecolor( v
, b
, cmap
);
1195 else if (xclass
==GLX_STATIC_GRAY
&& GET_VISUAL_DEPTH(v
)==1) {
1196 setup_monochrome( v
, b
);
1198 else if (xclass
==GLX_GRAY_SCALE
|| xclass
==GLX_STATIC_GRAY
) {
1199 if (!setup_grayscale( client
, v
, b
, cmap
)) {
1203 else if ((xclass
==GLX_PSEUDO_COLOR
|| xclass
==GLX_STATIC_COLOR
)
1204 && GET_VISUAL_DEPTH(v
)>=4 && GET_VISUAL_DEPTH(v
)<=16) {
1205 if (!setup_dithered_color( client
, v
, b
, cmap
)) {
1210 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.");
1213 v
->mesa_visual
.indexBits
= 0;
1215 if (_mesa_getenv("MESA_NO_DITHER")) {
1216 v
->dithered_pf
= v
->undithered_pf
;
1222 * If MESA_INFO env var is set print out some debugging info
1223 * which can help Brian figure out what's going on when a user
1226 if (_mesa_getenv("MESA_INFO")) {
1227 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
1228 _mesa_printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
1229 _mesa_printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
1230 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
1231 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
1232 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
1236 /* Do window-specific initializations */
1238 b
->frontbuffer
= window
;
1240 /* Setup for single/double buffering */
1241 if (v
->mesa_visual
.doubleBufferMode
) {
1242 /* Double buffered */
1243 #ifndef XFree86Server
1244 b
->shm
= check_for_xshm( v
->display
);
1246 xmesa_alloc_back_buffer( b
);
1247 if (b
->db_state
==BACK_PIXMAP
) {
1248 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
1255 /* Single Buffered */
1256 b
->buffer
= b
->frontbuffer
;
1259 /* X11 graphics contexts */
1260 #ifdef XFree86Server
1261 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
1263 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
1265 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
1267 /* cleargc - for glClear() */
1268 #ifdef XFree86Server
1269 b
->cleargc
= CreateScratchGC(v
->display
, window
->depth
);
1271 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
1273 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
1276 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1277 * Patch contributed by Michael Pichler May 15, 1995.
1279 #ifdef XFree86Server
1280 b
->swapgc
= CreateScratchGC(v
->display
, window
->depth
);
1284 dixChangeGC(NullClient
, b
->swapgc
, GCGraphicsExposures
, v
, NULL
);
1287 gcvalues
.graphics_exposures
= False
;
1288 b
->swapgc
= XCreateGC( v
->display
, window
,
1289 GCGraphicsExposures
, &gcvalues
);
1291 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
1293 * Set fill style and tile pixmap once for all for HPCR stuff
1294 * (instead of doing it each time in clear_color_HPCR_pixmap())
1295 * Initialize whole stuff
1296 * Patch contributed by Jacques Leroy March 8, 1998.
1298 if (v
->hpcr_clear_flag
&& b
->buffer
!=XIMAGE
) {
1300 for (i
=0; i
<16; i
++)
1302 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 0, 0);
1303 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 1, 0);
1305 XMesaPutImage(b
->display
, (XMesaDrawable
)v
->hpcr_clear_pixmap
,
1306 b
->cleargc
, v
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1307 XMesaSetFillStyle( v
->display
, b
->cleargc
, FillTiled
);
1308 XMesaSetTile( v
->display
, b
->cleargc
, v
->hpcr_clear_pixmap
);
1311 /* Initialize the row buffer XImage for use in write_color_span() */
1312 #ifdef XFree86Server
1313 b
->rowimage
= XMesaCreateImage(GET_VISUAL_DEPTH(v
), MAX_WIDTH
, 1,
1314 (char *)MALLOC(MAX_WIDTH
*4));
1316 b
->rowimage
= XCreateImage( v
->display
,
1319 ZPixmap
, 0, /*format, offset*/
1320 (char*) MALLOC(MAX_WIDTH
*4), /*data*/
1321 MAX_WIDTH
, 1, /*width, height*/
1323 0 /*bytes_per_line*/ );
1335 * Convert an RGBA color to a pixel value.
1338 xmesa_color_to_pixel( XMesaContext xmesa
, GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
1341 switch (pixelFormat
) {
1347 PACK_TRUECOLOR( p
, r
, g
, b
);
1351 return PACK_8A8B8G8R( r
, g
, b
, a
);
1355 return PACK_8R8G8B( r
, g
, b
);
1357 return PACK_5R6G5B( r
, g
, b
);
1361 return DITHER( 1, 0, r
, g
, b
);
1364 /* 382 = (3*255)/2 */
1365 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
1367 return DITHER_HPCR(1, 1, r
, g
, b
);
1371 return LOOKUP( r
, g
, b
);
1374 return GRAY_RGB( r
, g
, b
);
1375 case PF_Dither_True
:
1377 case PF_Dither_5R6G5B
:
1380 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
1384 _mesa_problem(NULL
, "Bad pixel format in xmesa_color_to_pixel");
1390 #define NUM_VISUAL_TYPES 6
1393 * Convert an X visual type to a GLX visual type.
1395 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
1397 * \return If \c visualType is a valid X visual type, a GLX visual type will
1398 * be returned. Otherwise \c GLX_NONE will be returned.
1401 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
1405 xmesa_convert_from_x_visual_type( int visualType
)
1407 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
1408 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
1409 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
1410 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
1413 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
1414 ? glx_visual_types
[ visualType
] : GLX_NONE
;
1418 /**********************************************************************/
1419 /***** Public Functions *****/
1420 /**********************************************************************/
1424 * Create a new X/Mesa visual.
1425 * Input: display - X11 display
1426 * visinfo - an XVisualInfo pointer
1427 * rgb_flag - GL_TRUE = RGB mode,
1428 * GL_FALSE = color index mode
1429 * alpha_flag - alpha buffer requested?
1430 * db_flag - GL_TRUE = double-buffered,
1431 * GL_FALSE = single buffered
1432 * stereo_flag - stereo visual?
1433 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1434 * GL_FALSE = use an off-screen pixmap for back buffer
1435 * depth_size - requested bits/depth values, or zero
1436 * stencil_size - requested bits/stencil values, or zero
1437 * accum_red_size - requested bits/red accum values, or zero
1438 * accum_green_size - requested bits/green accum values, or zero
1439 * accum_blue_size - requested bits/blue accum values, or zero
1440 * accum_alpha_size - requested bits/alpha accum values, or zero
1441 * num_samples - number of samples/pixel if multisampling, or zero
1442 * level - visual level, usually 0
1443 * visualCaveat - ala the GLX extension, usually GLX_NONE
1444 * Return; a new XMesaVisual or 0 if error.
1446 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
1447 XMesaVisualInfo visinfo
,
1449 GLboolean alpha_flag
,
1451 GLboolean stereo_flag
,
1452 GLboolean ximage_flag
,
1455 GLint accum_red_size
,
1456 GLint accum_green_size
,
1457 GLint accum_blue_size
,
1458 GLint accum_alpha_size
,
1461 GLint visualCaveat
)
1465 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
1467 /* For debugging only */
1468 if (_mesa_getenv("MESA_XSYNC")) {
1469 /* This makes debugging X easier.
1470 * In your debugger, set a breakpoint on _XError to stop when an
1471 * X protocol error is generated.
1473 #ifdef XFree86Server
1476 XSynchronize( display
, 1 );
1480 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
1486 * In the X server, NULL is passed in for the display. It will have
1487 * to be set before using this visual. See XMesaSetVisualDisplay()
1490 v
->display
= display
;
1492 /* Save a copy of the XVisualInfo struct because the user may XFREE()
1493 * the struct but we may need some of the information contained in it
1496 #ifndef XFree86Server
1497 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
1502 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
1505 /* check for MESA_GAMMA environment variable */
1506 gamma
= _mesa_getenv("MESA_GAMMA");
1508 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
1509 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
1510 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
1511 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
1512 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
1515 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
1518 v
->ximage_flag
= ximage_flag
;
1520 #ifdef XFree86Server
1521 /* We could calculate these values by ourselves. nplanes is either the sum
1522 * of the red, green, and blue bits or the number index bits.
1523 * ColormapEntries is either (1U << index_bits) or
1524 * (1U << max(redBits, greenBits, blueBits)).
1526 v
->nplanes
= visinfo
->nplanes
;
1527 v
->ColormapEntries
= visinfo
->ColormapEntries
;
1529 v
->mesa_visual
.redMask
= visinfo
->redMask
;
1530 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
1531 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
1532 v
->mesa_visual
.visualID
= visinfo
->vid
;
1533 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
1535 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
1536 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
1537 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
1538 v
->mesa_visual
.visualID
= visinfo
->visualid
;
1539 v
->mesa_visual
.screen
= visinfo
->screen
;
1542 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
1543 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
1545 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
1548 v
->mesa_visual
.visualRating
= visualCaveat
;
1550 (void) initialize_visual_and_buffer( 0, v
, NULL
, rgb_flag
, 0, 0 );
1553 const int xclass
= v
->mesa_visual
.visualType
;
1554 if (xclass
==GLX_TRUE_COLOR
|| xclass
==GLX_DIRECT_COLOR
) {
1555 red_bits
= bitcount(GET_REDMASK(v
));
1556 green_bits
= bitcount(GET_GREENMASK(v
));
1557 blue_bits
= bitcount(GET_BLUEMASK(v
));
1561 /* this is an approximation */
1563 depth
= GET_VISUAL_DEPTH(v
);
1564 red_bits
= depth
/ 3;
1566 green_bits
= depth
/ 2;
1567 depth
-= green_bits
;
1570 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
1574 if (alpha_flag
&& alpha_bits
== 0)
1577 _mesa_initialize_visual( &v
->mesa_visual
,
1578 rgb_flag
, db_flag
, stereo_flag
,
1579 red_bits
, green_bits
,
1580 blue_bits
, alpha_bits
,
1581 v
->mesa_visual
.indexBits
,
1584 accum_red_size
, accum_green_size
,
1585 accum_blue_size
, accum_alpha_size
,
1588 /* XXX minor hack */
1589 v
->mesa_visual
.level
= level
;
1594 void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
)
1600 void XMesaDestroyVisual( XMesaVisual v
)
1602 #ifndef XFree86Server
1611 * Create a new XMesaContext.
1612 * \param v the XMesaVisual
1613 * \param share_list another XMesaContext with which to share display
1614 * lists or NULL if no sharing is wanted.
1615 * \return an XMesaContext or NULL if error.
1617 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
1619 static GLboolean firstTime
= GL_TRUE
;
1622 struct dd_function_table functions
;
1626 _glthread_INIT_MUTEX(_xmesa_lock
);
1627 firstTime
= GL_FALSE
;
1630 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
1631 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
1635 mesaCtx
= &(c
->mesa
);
1637 /* initialize with default driver functions, then plug in XMesa funcs */
1638 _mesa_init_driver_functions(&functions
);
1639 xmesa_init_driver_functions(v
, &functions
);
1640 if (!_mesa_initialize_context(mesaCtx
, &v
->mesa_visual
,
1641 share_list
? &(share_list
->mesa
) : (GLcontext
*) NULL
,
1642 &functions
, (void *) c
)) {
1647 _mesa_enable_sw_extensions(mesaCtx
);
1648 _mesa_enable_1_3_extensions(mesaCtx
);
1649 _mesa_enable_1_4_extensions(mesaCtx
);
1650 _mesa_enable_1_5_extensions(mesaCtx
);
1651 _mesa_enable_2_0_extensions(mesaCtx
);
1654 _mesa_enable_extension(c
, "GL_EXT_texture_compression_s3tc");
1655 _mesa_enable_extension(c
, "GL_S3_s3tc");
1657 _mesa_enable_extension(c
, "GL_3DFX_texture_compression_FXT1");
1660 /* finish up xmesa context initializations */
1661 c
->swapbytes
= CHECK_BYTE_ORDER(v
) ? GL_FALSE
: GL_TRUE
;
1663 c
->xm_draw_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1664 c
->xm_read_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1665 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1666 c
->display
= v
->display
;
1667 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
1669 /* Initialize the software rasterizer and helper modules.
1671 if (!_swrast_CreateContext( mesaCtx
) ||
1672 !_ac_CreateContext( mesaCtx
) ||
1673 !_tnl_CreateContext( mesaCtx
) ||
1674 !_swsetup_CreateContext( mesaCtx
)) {
1675 _mesa_free_context_data(&c
->mesa
);
1681 tnl
= TNL_CONTEXT(mesaCtx
);
1682 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1684 xmesa_register_swrast_functions( mesaCtx
);
1685 _swsetup_Wakeup(mesaCtx
);
1692 void XMesaDestroyContext( XMesaContext c
)
1694 GLcontext
*mesaCtx
= &c
->mesa
;
1696 if (c
->xm_draw_buffer
&& c
->xm_buffer
->FXctx
)
1697 fxMesaDestroyContext(c
->xm_draw_buffer
->FXctx
);
1699 _swsetup_DestroyContext( mesaCtx
);
1700 _swrast_DestroyContext( mesaCtx
);
1701 _tnl_DestroyContext( mesaCtx
);
1702 _ac_DestroyContext( mesaCtx
);
1703 _mesa_free_context_data( mesaCtx
);
1710 * XXX this isn't a public function! It's a hack for the 3Dfx driver.
1711 * Create a new XMesaBuffer from an X window.
1712 * Input: v - the XMesaVisual
1715 * Return: new XMesaBuffer or NULL if error
1717 XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
, XMesaWindow w
,
1720 #ifndef XFree86Server
1721 XWindowAttributes attr
;
1728 XMesaBuffer b
= alloc_xmesa_buffer();
1735 #ifdef XFree86Server
1736 client
= CLIENT_ID(((XMesaDrawable
)w
)->id
);
1741 #ifdef XFree86Server
1742 if (GET_VISUAL_DEPTH(v
) != ((XMesaDrawable
)w
)->depth
) {
1743 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual and window! (%d != %d)\n", GET_VISUAL_DEPTH(v
) , ((XMesaDrawable
)w
)->depth
);
1747 XGetWindowAttributes( v
->display
, w
, &attr
);
1749 if (GET_VISUAL_DEPTH(v
) != attr
.depth
) {
1750 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual and window! (%d != %d)\n", GET_VISUAL_DEPTH(v
) , attr
.depth
);
1757 b
->display
= v
->display
;
1758 #ifdef XFree86Server
1759 b
->cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
1761 if (attr
.colormap
) {
1762 b
->cmap
= attr
.colormap
;
1765 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1766 /* this is weird, a window w/out a colormap!? */
1767 /* OK, let's just allocate a new one and hope for the best */
1768 b
->cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1772 /* determine back buffer implementation */
1773 if (v
->mesa_visual
.doubleBufferMode
) {
1774 if (v
->ximage_flag
) {
1775 b
->db_state
= BACK_XIMAGE
;
1778 b
->db_state
= BACK_PIXMAP
;
1785 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1787 v
->mesa_visual
.depthBits
> 0,
1788 v
->mesa_visual
.stencilBits
> 0,
1789 v
->mesa_visual
.accumRedBits
> 0,
1790 v
->mesa_visual
.alphaBits
> 0 );
1792 b
->mesa_buffer
.UseSoftwareAuxBuffers
= GL_TRUE
;
1794 if (!initialize_visual_and_buffer( client
, v
, b
, v
->mesa_visual
.rgbMode
,
1795 (XMesaDrawable
)w
, b
->cmap
)) {
1796 free_xmesa_buffer(client
, b
);
1801 fxEnvVar
= _mesa_getenv("MESA_GLX_FX");
1803 if (fxEnvVar
[0]!='d') {
1807 if (v
->mesa_visual
.depthBits
> 0) {
1808 attribs
[numAttribs
++] = FXMESA_DEPTH_SIZE
;
1809 attribs
[numAttribs
++] = v
->mesa_visual
.depthBits
;
1811 if (v
->mesa_visual
.doubleBufferMode
) {
1812 attribs
[numAttribs
++] = FXMESA_DOUBLEBUFFER
;
1814 if (v
->mesa_visual
.accumRedBits
> 0) {
1815 attribs
[numAttribs
++] = FXMESA_ACCUM_SIZE
;
1816 attribs
[numAttribs
++] = v
->mesa_visual
.accumRedBits
;
1818 if (v
->mesa_visual
.stencilBits
> 0) {
1819 attribs
[numAttribs
++] = FXMESA_STENCIL_SIZE
;
1820 attribs
[numAttribs
++] = v
->mesa_visual
.stencilBits
;
1822 if (v
->mesa_visual
.alphaBits
> 0) {
1823 attribs
[numAttribs
++] = FXMESA_ALPHA_SIZE
;
1824 attribs
[numAttribs
++] = v
->mesa_visual
.alphaBits
;
1827 attribs
[numAttribs
++] = FXMESA_SHARE_CONTEXT
;
1828 attribs
[numAttribs
++] = (int) &(c
->mesa
);
1830 attribs
[numAttribs
++] = FXMESA_NONE
;
1832 /* [dBorca] we should take an envvar for `fxMesaSelectCurrentBoard'!!! */
1833 hw
= fxMesaSelectCurrentBoard(0);
1835 /* if these fail, there's a new bug somewhere */
1836 ASSERT(b
->mesa_buffer
.Width
> 0);
1837 ASSERT(b
->mesa_buffer
.Height
> 0);
1839 if ((hw
== GR_SSTTYPE_VOODOO
) || (hw
== GR_SSTTYPE_Voodoo2
)) {
1840 b
->FXctx
= fxMesaCreateBestContext(0, b
->mesa_buffer
.Width
,
1841 b
->mesa_buffer
.Height
, attribs
);
1842 if ((v
->undithered_pf
!=PF_Index
) && (b
->backimage
)) {
1843 b
->FXisHackUsable
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1844 if (b
->FXctx
&& (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')) {
1845 b
->FXwindowHack
= GL_TRUE
;
1846 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
1849 b
->FXwindowHack
= GL_FALSE
;
1854 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1855 b
->FXctx
= fxMesaCreateContext(w
, GR_RESOLUTION_NONE
,
1856 GR_REFRESH_75Hz
, attribs
);
1858 b
->FXctx
= fxMesaCreateBestContext(0, b
->mesa_buffer
.Width
,
1859 b
->mesa_buffer
.Height
, attribs
);
1860 b
->FXisHackUsable
= GL_FALSE
;
1861 b
->FXwindowHack
= GL_FALSE
;
1865 "voodoo %d, wid %d height %d hack: usable %d active %d\n",
1866 hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack);
1871 _mesa_warning(NULL
, "WARNING: This Mesa Library includes the Glide driver but\n");
1872 _mesa_warning(NULL
, " you have not defined the MESA_GLX_FX env. var.\n");
1873 _mesa_warning(NULL
, " (check the README.3DFX file for more information).\n\n");
1874 _mesa_warning(NULL
, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
1882 XMesaBuffer
XMesaCreateWindowBuffer( XMesaVisual v
, XMesaWindow w
)
1884 return XMesaCreateWindowBuffer2( v
, w
, NULL
);
1889 * Create a new XMesaBuffer from an X pixmap.
1891 * \param v the XMesaVisual
1892 * \param p the pixmap
1893 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1894 * \c GLX_DIRECT_COLOR visual for the pixmap
1895 * \returns new XMesaBuffer or NULL if error
1897 XMesaBuffer
XMesaCreatePixmapBuffer( XMesaVisual v
,
1898 XMesaPixmap p
, XMesaColormap cmap
)
1901 XMesaBuffer b
= alloc_xmesa_buffer();
1907 #ifdef XFree86Server
1908 client
= CLIENT_ID(((XMesaDrawable
)p
)->id
);
1915 b
->display
= v
->display
;
1918 /* determine back buffer implementation */
1919 if (v
->mesa_visual
.doubleBufferMode
) {
1920 if (v
->ximage_flag
) {
1921 b
->db_state
= BACK_XIMAGE
;
1924 b
->db_state
= BACK_PIXMAP
;
1931 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1933 v
->mesa_visual
.depthBits
> 0,
1934 v
->mesa_visual
.stencilBits
> 0,
1935 v
->mesa_visual
.accumRedBits
+
1936 v
->mesa_visual
.accumGreenBits
+
1937 v
->mesa_visual
.accumBlueBits
> 0,
1938 v
->mesa_visual
.alphaBits
> 0 );
1940 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1941 (XMesaDrawable
)p
, cmap
)) {
1942 free_xmesa_buffer(client
, b
);
1951 XMesaBuffer
XMesaCreatePBuffer( XMesaVisual v
, XMesaColormap cmap
,
1952 unsigned int width
, unsigned int height
)
1954 #ifdef XFree86Server
1959 XMesaDrawable drawable
; /* X Pixmap Drawable */
1960 XMesaBuffer b
= alloc_xmesa_buffer();
1967 b
->display
= v
->display
;
1970 /* allocate pixmap for front buffer */
1971 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1972 drawable
= XCreatePixmap( v
->display
, root
, width
, height
, v
->visinfo
->depth
);
1974 /* determine back buffer implementation */
1975 if (v
->mesa_visual
.doubleBufferMode
) {
1976 if (v
->ximage_flag
) {
1977 b
->db_state
= BACK_XIMAGE
;
1980 b
->db_state
= BACK_PIXMAP
;
1987 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1989 v
->mesa_visual
.depthBits
> 0,
1990 v
->mesa_visual
.stencilBits
> 0,
1991 v
->mesa_visual
.accumRedBits
+
1992 v
->mesa_visual
.accumGreenBits
+
1993 v
->mesa_visual
.accumBlueBits
> 0,
1994 v
->mesa_visual
.alphaBits
> 0 );
1996 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1998 free_xmesa_buffer(client
, b
);
2009 * Deallocate an XMesaBuffer structure and all related info.
2011 void XMesaDestroyBuffer( XMesaBuffer b
)
2015 #ifdef XFree86Server
2017 client
= CLIENT_ID(b
->frontbuffer
->id
);
2020 if (b
->gc
) XMesaFreeGC( b
->xm_visual
->display
, b
->gc
);
2021 if (b
->cleargc
) XMesaFreeGC( b
->xm_visual
->display
, b
->cleargc
);
2022 if (b
->swapgc
) XMesaFreeGC( b
->xm_visual
->display
, b
->swapgc
);
2025 #if defined(USE_XSHM) && !defined(XFree86Server)
2027 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
2028 XDestroyImage( b
->backimage
);
2029 shmdt( b
->shminfo
.shmaddr
);
2033 XMesaDestroyImage( b
->backimage
);
2035 if (b
->backpixmap
) {
2036 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
2037 if (b
->xm_visual
->hpcr_clear_flag
) {
2038 XMesaFreePixmap( b
->xm_visual
->display
,
2039 b
->xm_visual
->hpcr_clear_pixmap
);
2040 XMesaDestroyImage( b
->xm_visual
->hpcr_clear_ximage
);
2044 FREE( b
->rowimage
->data
);
2045 b
->rowimage
->data
= NULL
;
2046 XMesaDestroyImage( b
->rowimage
);
2049 free_xmesa_buffer(client
, b
);
2055 * Bind buffer b to context c and make c the current rendering context.
2057 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
2059 return XMesaMakeCurrent2( c
, b
, b
);
2064 * Bind buffer b to context c and make c the current rendering context.
2066 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
2067 XMesaBuffer readBuffer
)
2070 if (!drawBuffer
|| !readBuffer
)
2071 return GL_FALSE
; /* must specify buffers! */
2074 if (drawBuffer
->FXctx
) {
2075 fxMesaMakeCurrent(drawBuffer
->FXctx
);
2077 c
->xm_draw_buffer
= drawBuffer
;
2078 c
->xm_read_buffer
= readBuffer
;
2079 c
->xm_buffer
= drawBuffer
;
2084 if (&(c
->mesa
) == _mesa_get_current_context()
2085 && c
->xm_draw_buffer
== drawBuffer
2086 && c
->xm_read_buffer
== readBuffer
2087 && c
->xm_draw_buffer
->wasCurrent
) {
2088 /* same context and buffer, do nothing */
2092 c
->xm_draw_buffer
= drawBuffer
;
2093 c
->xm_read_buffer
= readBuffer
;
2094 c
->xm_buffer
= drawBuffer
;
2096 _mesa_make_current2(&(c
->mesa
),
2097 &drawBuffer
->mesa_buffer
,
2098 &readBuffer
->mesa_buffer
);
2100 if (c
->xm_visual
->mesa_visual
.rgbMode
) {
2102 * Must recompute and set these pixel values because colormap
2103 * can be different for different windows.
2105 c
->clearpixel
= xmesa_color_to_pixel( c
,
2110 c
->xm_visual
->undithered_pf
);
2111 XMesaSetForeground(c
->display
, c
->xm_draw_buffer
->cleargc
, c
->clearpixel
);
2114 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
2115 c
->xm_draw_buffer
->wasCurrent
= GL_TRUE
;
2119 _mesa_make_current2( NULL
, NULL
, NULL
);
2126 * Unbind the context c from its buffer.
2128 GLboolean
XMesaUnbindContext( XMesaContext c
)
2130 /* A no-op for XFree86 integration purposes */
2135 XMesaContext
XMesaGetCurrentContext( void )
2137 GET_CURRENT_CONTEXT(ctx
);
2139 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2148 XMesaBuffer
XMesaGetCurrentBuffer( void )
2150 GET_CURRENT_CONTEXT(ctx
);
2152 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2153 return xmesa
->xm_draw_buffer
;
2161 /* New in Mesa 3.1 */
2162 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
2164 GET_CURRENT_CONTEXT(ctx
);
2166 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2167 return xmesa
->xm_read_buffer
;
2175 GLboolean
XMesaForceCurrent(XMesaContext c
)
2178 if (&(c
->mesa
) != _mesa_get_current_context()) {
2179 _mesa_make_current(&(c
->mesa
), &c
->xm_draw_buffer
->mesa_buffer
);
2183 _mesa_make_current(NULL
, NULL
);
2189 GLboolean
XMesaLoseCurrent(XMesaContext c
)
2192 _mesa_make_current(NULL
, NULL
);
2198 * Switch 3Dfx support hack between window and full-screen mode.
2200 GLboolean
XMesaSetFXmode( GLint mode
)
2203 const char *fx
= _mesa_getenv("MESA_GLX_FX");
2204 if (fx
&& fx
[0] != 'd') {
2205 GET_CURRENT_CONTEXT(ctx
);
2206 GrHwConfiguration hw
;
2207 if (!FX_grSstQueryHardware(&hw
)) {
2208 /*fprintf(stderr, "!grSstQueryHardware\n");*/
2211 if (hw
.num_sst
< 1) {
2212 /*fprintf(stderr, "hw.num_sst < 1\n");*/
2216 /* [dBorca] Hack alert:
2217 * oh, this is sooo wrong: ctx above is
2218 * really an fxMesaContext, not an XMesaContext
2220 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2221 if (mode
== XMESA_FX_WINDOW
) {
2222 if (xmesa
->xm_draw_buffer
->FXisHackUsable
) {
2223 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
2224 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_TRUE
;
2228 else if (mode
== XMESA_FX_FULLSCREEN
) {
2229 FX_grSstControl(GR_CONTROL_ACTIVATE
);
2230 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_FALSE
;
2234 /* Error: Bad mode value */
2238 /*fprintf(stderr, "fallthrough\n");*/
2249 * Read image from VooDoo frame buffer into X/Mesa's back XImage.
2251 static void FXgetImage( XMesaBuffer b
)
2253 GET_CURRENT_CONTEXT(ctx
);
2254 static unsigned short pixbuf
[MAX_WIDTH
];
2258 unsigned int bw
, depth
, width
, height
;
2259 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2261 #ifdef XFree86Server
2262 x
= b
->frontbuffer
->x
;
2263 y
= b
->frontbuffer
->y
;
2264 width
= b
->frontbuffer
->width
;
2265 height
= b
->frontbuffer
->height
;
2266 depth
= b
->frontbuffer
->depth
;
2268 XGetGeometry( b
->xm_visual
->display
, b
->frontbuffer
,
2269 &root
, &xpos
, &ypos
, &width
, &height
, &bw
, &depth
);
2271 if (b
->mesa_buffer
.Width
!= width
|| b
->mesa_buffer
.Height
!= height
) {
2272 b
->mesa_buffer
.Width
= MIN2((int)width
, b
->FXctx
->width
);
2273 b
->mesa_buffer
.Height
= MIN2((int)height
, b
->FXctx
->height
);
2274 if (b
->mesa_buffer
.Width
& 1)
2275 b
->mesa_buffer
.Width
--; /* prevent odd width */
2276 xmesa_alloc_back_buffer( b
);
2279 /* [dBorca] we're always in the right GR_COLORFORMAT... aren't we? */
2280 /* grLfbWriteColorFormat(GR_COLORFORMAT_ARGB); */
2281 if (b
->xm_visual
->undithered_pf
==PF_5R6G5B
) {
2282 /* Special case: 16bpp RGB */
2283 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
, /* src buffer */
2284 0, b
->FXctx
->height
- b
->mesa_buffer
.Height
, /*pos*/
2285 b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
, /* size */
2286 b
->mesa_buffer
.Width
* sizeof(GLushort
), /* stride */
2287 b
->backimage
->data
); /* dest buffer */
2289 else if (b
->xm_visual
->dithered_pf
==PF_Dither
2290 && GET_VISUAL_DEPTH(b
->xm_visual
)==8) {
2291 /* Special case: 8bpp RGB */
2292 for (y
=0;y
<b
->mesa_buffer
.Height
;y
++) {
2293 GLubyte
*ptr
= (GLubyte
*) b
->backimage
->data
2294 + b
->backimage
->bytes_per_line
* y
;
2297 /* read row from 3Dfx frame buffer */
2298 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2299 0, b
->FXctx
->height
-(b
->mesa_buffer
.Height
-y
),
2300 b
->mesa_buffer
.Width
, 1,
2304 /* write to XImage back buffer */
2305 for (x
=0;x
<b
->mesa_buffer
.Width
;x
++) {
2306 GLubyte r
= (pixbuf
[x
] & 0xf800) >> 8;
2307 GLubyte g
= (pixbuf
[x
] & 0x07e0) >> 3;
2308 GLubyte b
= (pixbuf
[x
] & 0x001f) << 3;
2309 *ptr
++ = XDITHER( x
, r
, g
, b
);
2314 /* General case: slow! */
2315 for (y
=0;y
<b
->mesa_buffer
.Height
;y
++) {
2316 /* read row from 3Dfx frame buffer */
2317 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2318 0, b
->FXctx
->height
-(b
->mesa_buffer
.Height
-y
),
2319 b
->mesa_buffer
.Width
, 1,
2323 /* write to XImage back buffer */
2324 for (x
=0;x
<b
->mesa_buffer
.Width
;x
++) {
2325 XMesaPutPixel(b
->backimage
,x
,y
,
2326 xmesa_color_to_pixel(xmesa
,
2327 (pixbuf
[x
] & 0xf800) >> 8,
2328 (pixbuf
[x
] & 0x07e0) >> 3,
2329 (pixbuf
[x
] & 0x001f) << 3,
2331 b
->xm_visual
->undithered_pf
));
2335 /* grLfbWriteColorFormat(GR_COLORFORMAT_ABGR); */
2341 * Copy the back buffer to the front buffer. If there's no back buffer
2344 void XMesaSwapBuffers( XMesaBuffer b
)
2346 GET_CURRENT_CONTEXT(ctx
);
2348 /* If we're swapping the buffer associated with the current context
2349 * we have to flush any pending rendering commands first.
2351 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2352 _mesa_notifySwapBuffers(ctx
);
2357 fxMesaSwapBuffers();
2359 if (b
->FXwindowHack
)
2366 /* Copy Ximage from host's memory to server's window */
2367 #if defined(USE_XSHM) && !defined(XFree86Server)
2369 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2370 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2373 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2375 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2380 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2381 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2384 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
2385 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2389 /* Copy pixmap to window on server */
2390 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2391 XMesaCopyArea( b
->xm_visual
->display
,
2392 b
->backpixmap
, /* source drawable */
2393 b
->frontbuffer
, /* dest. drawable */
2395 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2396 0, 0 /* dest region */
2398 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2401 #if !defined(XFree86Server)
2402 XSync( b
->xm_visual
->display
, False
);
2409 * Copy sub-region of back buffer to front buffer
2411 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
2413 GET_CURRENT_CONTEXT(ctx
);
2415 /* If we're swapping the buffer associated with the current context
2416 * we have to flush any pending rendering commands first.
2418 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2419 _mesa_notifySwapBuffers(ctx
);
2422 int yTop
= b
->mesa_buffer
.Height
- y
- height
;
2425 fxMesaSwapBuffers();
2426 if (b
->FXwindowHack
)
2433 /* Copy Ximage from host's memory to server's window */
2434 #if defined(USE_XSHM) && !defined(XFree86Server)
2436 /* XXX assuming width and height aren't too large! */
2437 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2439 b
->backimage
, x
, yTop
,
2440 x
, yTop
, width
, height
, False
);
2441 /* wait for finished event??? */
2446 /* XXX assuming width and height aren't too large! */
2447 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2449 b
->backimage
, x
, yTop
,
2450 x
, yTop
, width
, height
);
2454 /* Copy pixmap to window on server */
2455 XMesaCopyArea( b
->xm_visual
->display
,
2456 b
->backpixmap
, /* source drawable */
2457 b
->frontbuffer
, /* dest. drawable */
2459 x
, yTop
, width
, height
, /* source region */
2460 x
, yTop
/* dest region */
2468 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2469 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2470 * back buffer directly.
2471 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2472 * ximage - pointer to back buffer's XImage, or NULL
2473 * Return: GL_TRUE = context is double buffered
2474 * GL_FALSE = context is single buffered
2476 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
2477 XMesaPixmap
*pixmap
,
2478 XMesaImage
**ximage
)
2481 if (pixmap
) *pixmap
= b
->backpixmap
;
2482 if (ximage
) *ximage
= b
->backimage
;
2494 * Return the depth buffer associated with an XMesaBuffer.
2495 * Input: b - the XMesa buffer handle
2496 * Output: width, height - size of buffer in pixels
2497 * bytesPerValue - bytes per depth value (2 or 4)
2498 * buffer - pointer to depth buffer values
2499 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2501 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
2502 GLint
*bytesPerValue
, void **buffer
)
2504 if (!b
->mesa_buffer
.DepthBuffer
) {
2512 *width
= b
->mesa_buffer
.Width
;
2513 *height
= b
->mesa_buffer
.Height
;
2514 *bytesPerValue
= b
->mesa_buffer
.Visual
.depthBits
<= 16
2515 ? sizeof(GLushort
) : sizeof(GLuint
);
2516 *buffer
= b
->mesa_buffer
.DepthBuffer
;
2522 void XMesaFlush( XMesaContext c
)
2524 if (c
&& c
->xm_visual
) {
2525 #ifdef XFree86Server
2528 XSync( c
->xm_visual
->display
, False
);
2535 const char *XMesaGetString( XMesaContext c
, int name
)
2538 if (name
==XMESA_VERSION
) {
2541 else if (name
==XMESA_EXTENSIONS
) {
2551 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
2554 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
2555 if (b
->frontbuffer
==d
&& b
->display
==dpy
) {
2565 * Look for XMesaBuffers whose X window has been destroyed.
2566 * Deallocate any such XMesaBuffers.
2568 void XMesaGarbageCollect( void )
2570 XMesaBuffer b
, next
;
2571 for (b
=XMesaBufferList
; b
; b
=next
) {
2573 if (b
->display
&& b
->frontbuffer
&& b
->type
== WINDOW
) {
2574 #ifdef XFree86Server
2577 XSync(b
->display
, False
);
2578 if (!window_exists( b
->display
, b
->frontbuffer
)) {
2579 /* found a dead window, free the ancillary info */
2580 XMesaDestroyBuffer( b
);
2588 void XMesaReset( void )
2590 while (XMesaBufferList
)
2591 XMesaDestroyBuffer(XMesaBufferList
);
2593 XMesaBufferList
= NULL
;
2597 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
2598 GLfloat red
, GLfloat green
,
2599 GLfloat blue
, GLfloat alpha
)
2601 GLint r
= (GLint
) (red
* 255.0F
);
2602 GLint g
= (GLint
) (green
* 255.0F
);
2603 GLint b
= (GLint
) (blue
* 255.0F
);
2604 GLint a
= (GLint
) (alpha
* 255.0F
);
2606 switch (xmesa
->pixelformat
) {
2612 PACK_TRUECOLOR( p
, r
, g
, b
);
2616 return PACK_8A8B8G8R( r
, g
, b
, a
);
2618 return PACK_8R8G8B( r
, g
, b
);
2620 return PACK_5R6G5B( r
, g
, b
);
2624 return DITHER( x
, y
, r
, g
, b
);
2627 /* 382 = (3*255)/2 */
2628 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
2630 return DITHER_HPCR(x
, y
, r
, g
, b
);
2634 return LOOKUP( r
, g
, b
);
2637 return GRAY_RGB( r
, g
, b
);
2638 case PF_Dither_5R6G5B
:
2640 case PF_Dither_True
:
2643 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
2647 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
2654 * This is typically called when the window size changes and we need
2655 * to reallocate the buffer's back/depth/stencil/accum buffers.
2657 void XMesaResizeBuffers( XMesaBuffer b
)
2659 xmesa_resize_buffers( &(b
->mesa_buffer
) );