1 /* $Id: xm_api.c,v 1.54 2003/03/01 01:50:24 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 /* $XFree86: xc/extras/Mesa/src/X/xm_api.c,v 1.2 2002/02/26 23:37:31 tsi Exp $ */
29 * This file contains the implementations of all the XMesa* functions.
34 * The window coordinate system origin (0,0) is in the lower-left corner
35 * of the window. X11's window coordinate origin is in the upper-left
36 * corner of the window. Therefore, most drawing functions in this
37 * file have to flip Y coordinates.
39 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
40 * in support for the MIT Shared Memory extension. If enabled, when you
41 * use an Ximage for the back buffer in double buffered mode, the "swap"
42 * operation will be faster. You must also link with -lXext.
44 * Byte swapping: If the Mesa host and the X display use a different
45 * byte order then there's some trickiness to be aware of when using
46 * XImages. The byte ordering used for the XImage is that of the X
47 * display, not the Mesa host.
48 * The color-to-pixel encoding for True/DirectColor must be done
49 * according to the display's visual red_mask, green_mask, and blue_mask.
50 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
51 * do byte swapping if needed. If one wants to directly "poke" the pixel
52 * into the XImage's buffer then the pixel must be byte swapped first. In
53 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
54 * and use XPutPixel everywhere except in the implementation of
55 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
56 * instead of using XPutPixel we "poke" our values after byte-swapping
57 * the clear pixel value if needed.
66 #include "glxheader.h"
70 #include "extensions.h"
76 #include "texformat.h"
78 #include "swrast/swrast.h"
79 #include "swrast_setup/swrast_setup.h"
80 #include "array_cache/acache.h"
84 #define GLX_NONE_EXT 0x8000
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 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 * Return the width and height of the given drawable.
226 static void get_drawable_size( XMesaDisplay
*dpy
, XMesaDrawable d
,
227 unsigned int *width
, unsigned int *height
)
236 unsigned int bw
, depth
;
238 _glthread_LOCK_MUTEX(_xmesa_lock
);
239 XGetGeometry( dpy
, d
, &root
, &x
, &y
, width
, height
, &bw
, &depth
);
240 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
246 * Apply gamma correction to an intensity value in [0..max]. Return the
247 * new intensity value.
249 static GLint
gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
255 double x
= (double) value
/ (double) max
;
256 return IROUND_POS((GLfloat
) max
* _mesa_pow(x
, 1.0F
/gamma
));
263 * Return the true number of bits per pixel for XImages.
264 * For example, if we request a 24-bit deep visual we may actually need/get
265 * 32bpp XImages. This function returns the appropriate bpp.
266 * Input: dpy - the X display
267 * visinfo - desribes the visual to be used for XImages
268 * Return: true number of bits per pixel for XImages
272 static int bits_per_pixel( XMesaVisual xmv
)
274 XMesaVisualInfo visinfo
= xmv
->visinfo
;
275 const int depth
= visinfo
->nplanes
;
277 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
278 if (screenInfo
.formats
[i
].depth
== depth
)
279 return screenInfo
.formats
[i
].bitsPerPixel
;
281 return depth
; /* should never get here, but this should be safe */
286 static int bits_per_pixel( XMesaVisual xmv
)
288 XMesaDisplay
*dpy
= xmv
->display
;
289 XMesaVisualInfo visinfo
= xmv
->visinfo
;
292 /* Create a temporary XImage */
293 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
294 ZPixmap
, 0, /*format, offset*/
295 (char*) MALLOC(8), /*data*/
296 1, 1, /*width, height*/
301 /* grab the bits/pixel value */
302 bitsPerPixel
= img
->bits_per_pixel
;
303 /* free the XImage */
306 XMesaDestroyImage( img
);
314 * Determine if a given X window ID is valid (window exists).
315 * Do this by calling XGetWindowAttributes() for the window and
316 * checking if we catch an X error.
317 * Input: dpy - the display
318 * win - the window to check for existance
319 * Return: GL_TRUE - window exists
320 * GL_FALSE - window doesn't exist
322 #ifndef XFree86Server
323 static GLboolean WindowExistsFlag
;
325 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
328 if (xerr
->error_code
== BadWindow
) {
329 WindowExistsFlag
= GL_FALSE
;
334 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
336 XWindowAttributes wa
;
337 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
338 WindowExistsFlag
= GL_TRUE
;
339 old_handler
= XSetErrorHandler(window_exists_err_handler
);
340 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
341 XSetErrorHandler(old_handler
);
342 return WindowExistsFlag
;
348 /**********************************************************************/
349 /***** Linked list of XMesaBuffers *****/
350 /**********************************************************************/
352 static XMesaBuffer XMesaBufferList
= NULL
;
355 /* Allocate a new XMesaBuffer, add to linked list */
356 static XMesaBuffer
alloc_xmesa_buffer(void)
358 XMesaBuffer b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
360 b
->Next
= XMesaBufferList
;
368 * Find an XMesaBuffer by matching X display and colormap but NOT matching
369 * the notThis buffer.
371 static XMesaBuffer
find_xmesa_buffer(XMesaDisplay
*dpy
,
376 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
377 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
386 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
389 static void free_xmesa_buffer(int client
, XMesaBuffer buffer
)
391 XMesaBuffer prev
= NULL
, b
;
393 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
395 /* unlink bufer from list */
397 prev
->Next
= buffer
->Next
;
399 XMesaBufferList
= buffer
->Next
;
400 /* Check to free X colors */
401 if (buffer
->num_alloced
>0) {
402 /* If no other buffer uses this X colormap then free the colors. */
403 if (!find_xmesa_buffer(buffer
->display
, buffer
->cmap
, buffer
)) {
405 (void)FreeColors(buffer
->cmap
, client
,
406 buffer
->num_alloced
, buffer
->alloced_colors
,
409 XFreeColors(buffer
->display
, buffer
->cmap
,
410 buffer
->alloced_colors
, buffer
->num_alloced
, 0);
415 _mesa_free_framebuffer_data(&buffer
->mesa_buffer
);
420 /* continue search */
423 /* buffer not found in XMesaBufferList */
424 _mesa_problem(NULL
,"free_xmesa_buffer() - buffer not found\n");
428 /* Copy X color table stuff from one XMesaBuffer to another. */
429 static void copy_colortable_info(XMesaBuffer dst
, const XMesaBuffer src
)
431 MEMCPY(dst
->color_table
, src
->color_table
, sizeof(src
->color_table
));
432 MEMCPY(dst
->pixel_to_r
, src
->pixel_to_r
, sizeof(src
->pixel_to_r
));
433 MEMCPY(dst
->pixel_to_g
, src
->pixel_to_g
, sizeof(src
->pixel_to_g
));
434 MEMCPY(dst
->pixel_to_b
, src
->pixel_to_b
, sizeof(src
->pixel_to_b
));
435 dst
->num_alloced
= src
->num_alloced
;
436 MEMCPY(dst
->alloced_colors
, src
->alloced_colors
,
437 sizeof(src
->alloced_colors
));
442 /**********************************************************************/
443 /***** Misc Private Functions *****/
444 /**********************************************************************/
448 * Return number of bits set in n.
450 static int bitcount( unsigned long n
)
453 for (bits
=0; n
>0; n
=n
>>1) {
464 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
465 * Return: GL_TRUE if success, GL_FALSE if error
467 #ifndef XFree86Server
468 static GLboolean
alloc_shm_back_buffer( XMesaBuffer b
)
472 * We have to do a _lot_ of error checking here to be sure we can
473 * really use the XSHM extension. It seems different servers trigger
474 * errors at different points if the extension won't work. Therefore
475 * we have to be very careful...
478 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
480 b
->backimage
= XShmCreateImage( b
->xm_visual
->display
,
481 b
->xm_visual
->visinfo
->visual
,
482 b
->xm_visual
->visinfo
->depth
,
483 ZPixmap
, NULL
, &b
->shminfo
,
484 b
->width
, b
->height
);
485 if (b
->backimage
== NULL
) {
486 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
491 b
->shminfo
.shmid
= shmget( IPC_PRIVATE
, b
->backimage
->bytes_per_line
492 * b
->backimage
->height
, IPC_CREAT
|0777 );
493 if (b
->shminfo
.shmid
< 0) {
494 _mesa_warning(NULL
, "shmget failed while allocating back buffer");
495 XDestroyImage( b
->backimage
);
497 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmget), disabling.");
502 b
->shminfo
.shmaddr
= b
->backimage
->data
503 = (char*)shmat( b
->shminfo
.shmid
, 0, 0 );
504 if (b
->shminfo
.shmaddr
== (char *) -1) {
505 _mesa_warning(NULL
, "shmat() failed while allocating back buffer");
506 XDestroyImage( b
->backimage
);
507 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
509 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmat), disabling.");
514 b
->shminfo
.readOnly
= False
;
516 old_handler
= XSetErrorHandler( mesaHandleXError
);
517 /* This may trigger the X protocol error we're ready to catch: */
518 XShmAttach( b
->xm_visual
->display
, &b
->shminfo
);
519 XSync( b
->xm_visual
->display
, False
);
521 if (mesaXErrorFlag
) {
522 /* we are on a remote display, this error is normal, don't print it */
523 XFlush( b
->xm_visual
->display
);
525 XDestroyImage( b
->backimage
);
526 shmdt( b
->shminfo
.shmaddr
);
527 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
530 (void) XSetErrorHandler( old_handler
);
534 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 ); /* nobody else needs it */
536 /* Finally, try an XShmPutImage to be really sure the extension works */
537 gc
= XCreateGC( b
->xm_visual
->display
, b
->frontbuffer
, 0, NULL
);
538 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
, gc
,
539 b
->backimage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
540 XSync( b
->xm_visual
->display
, False
);
541 XFreeGC( b
->xm_visual
->display
, gc
);
542 (void) XSetErrorHandler( old_handler
);
543 if (mesaXErrorFlag
) {
544 XFlush( b
->xm_visual
->display
);
546 XDestroyImage( b
->backimage
);
547 shmdt( b
->shminfo
.shmaddr
);
548 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
555 int height
= b
->backimage
->height
;
556 /* Needed by PIXELADDR1 macro */
557 b
->ximage_width1
= b
->backimage
->bytes_per_line
;
558 b
->ximage_origin1
= (GLubyte
*) b
->backimage
->data
559 + b
->ximage_width1
* (height
-1);
560 /* Needed by PIXELADDR2 macro */
561 b
->ximage_width2
= b
->backimage
->bytes_per_line
/ 2;
562 b
->ximage_origin2
= (GLushort
*) b
->backimage
->data
563 + b
->ximage_width2
* (height
-1);
564 /* Needed by PIXELADDR3 macro */
565 b
->ximage_width3
= b
->backimage
->bytes_per_line
;
566 b
->ximage_origin3
= (GLubyte
*) b
->backimage
->data
567 + b
->ximage_width3
* (height
-1);
568 /* Needed by PIXELADDR4 macro */
569 b
->ximage_width4
= b
->backimage
->width
;
570 b
->ximage_origin4
= (GLuint
*) b
->backimage
->data
571 + b
->ximage_width4
* (height
-1);
576 /* Can't compile XSHM support */
585 * Setup an off-screen pixmap or Ximage to use as the back buffer.
586 * Input: b - the X/Mesa buffer
588 void xmesa_alloc_back_buffer( XMesaBuffer b
)
590 if (b
->db_state
==BACK_XIMAGE
) {
591 /* Deallocate the old backimage, if any */
593 #if defined(USE_XSHM) && !defined(XFree86Server)
595 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
596 XDestroyImage( b
->backimage
);
597 shmdt( b
->shminfo
.shmaddr
);
601 XMesaDestroyImage( b
->backimage
);
605 /* Allocate new back buffer */
608 /* Allocate a regular XImage for the back buffer. */
609 b
->backimage
= XMesaCreateImage(b
->xm_visual
->BitsPerPixel
,
610 b
->width
, b
->height
, NULL
);
612 if (b
->shm
==0 || alloc_shm_back_buffer(b
)==GL_FALSE
) {
613 /* Allocate a regular XImage for the back buffer. */
614 b
->backimage
= XCreateImage( b
->xm_visual
->display
,
615 b
->xm_visual
->visinfo
->visual
,
616 GET_VISUAL_DEPTH(b
->xm_visual
),
617 ZPixmap
, 0, /* format, offset */
618 NULL
, b
->width
, b
->height
,
619 8, 0 ); /* pad, bytes_per_line */
622 _mesa_warning(NULL
, "alloc_back_buffer: XCreateImage failed.");
624 b
->backimage
->data
= (char *) MALLOC( b
->backimage
->height
625 * b
->backimage
->bytes_per_line
);
626 if (!b
->backimage
->data
) {
627 _mesa_warning(NULL
, "alloc_back_buffer: MALLOC failed.");
628 XMesaDestroyImage( b
->backimage
);
632 b
->backpixmap
= None
;
634 else if (b
->db_state
==BACK_PIXMAP
) {
635 XMesaPixmap old_pixmap
= b
->backpixmap
;
636 /* Free the old back pixmap */
638 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
640 /* Allocate new back pixmap */
641 b
->backpixmap
= XMesaCreatePixmap( b
->xm_visual
->display
, b
->frontbuffer
,
643 GET_VISUAL_DEPTH(b
->xm_visual
) );
645 /* update other references to backpixmap */
646 if (b
->buffer
==(XMesaDrawable
)old_pixmap
) {
647 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
655 * A replacement for XAllocColor. This function should never
656 * fail to allocate a color. When XAllocColor fails, we return
657 * the nearest matching color. If we have to allocate many colors
658 * this function isn't too efficient; the XQueryColors() could be
660 * Written by Michael Pichler, Brian Paul, Mark Kilgard
661 * Input: dpy - X display
663 * cmapSize - size of colormap
664 * In/Out: color - the XColor struct
665 * Output: exact - 1=exact color match, 0=closest match
666 * alloced - 1=XAlloc worked, 0=XAlloc failed
669 noFaultXAllocColor( int client
,
674 int *exact
, int *alloced
)
680 /* we'll try to cache ctable for better remote display performance */
681 static Display
*prevDisplay
= NULL
;
682 static XMesaColormap prevCmap
= 0;
683 static int prevCmapSize
= 0;
684 static XMesaColor
*ctable
= NULL
;
688 double mindist
; /* 3*2^16^2 exceeds long int precision. */
692 /* First try just using XAllocColor. */
695 &color
->red
, &color
->green
, &color
->blue
,
697 client
) == Success
) {
699 if (XAllocColor(dpy
, cmap
, color
)) {
706 /* Alloc failed, search for closest match */
708 /* Retrieve color table entries. */
709 /* XXX alloca candidate. */
711 ppixIn
= (Pixel
*) MALLOC(cmapSize
* sizeof(Pixel
));
712 ctable
= (xrgb
*) MALLOC(cmapSize
* sizeof(xrgb
));
713 for (i
= 0; i
< cmapSize
; i
++) {
716 QueryColors(cmap
, cmapSize
, ppixIn
, ctable
);
718 if (prevDisplay
!= dpy
|| prevCmap
!= cmap
719 || prevCmapSize
!= cmapSize
|| !ctable
) {
720 /* free previously cached color table */
723 /* Get the color table from X */
724 ctable
= (XMesaColor
*) MALLOC(cmapSize
* sizeof(XMesaColor
));
726 for (i
= 0; i
< cmapSize
; i
++) {
729 XQueryColors(dpy
, cmap
, ctable
, cmapSize
);
732 prevCmapSize
= cmapSize
;
736 /* Find best match. */
739 for (i
= 0; i
< cmapSize
; i
++) {
740 double dr
= 0.30 * ((double) color
->red
- (double) ctable
[i
].red
);
741 double dg
= 0.59 * ((double) color
->green
- (double) ctable
[i
].green
);
742 double db
= 0.11 * ((double) color
->blue
- (double) ctable
[i
].blue
);
743 double dist
= dr
* dr
+ dg
* dg
+ db
* db
;
744 if (bestmatch
< 0 || dist
< mindist
) {
751 subColor
.red
= ctable
[bestmatch
].red
;
752 subColor
.green
= ctable
[bestmatch
].green
;
753 subColor
.blue
= ctable
[bestmatch
].blue
;
754 /* Try to allocate the closest match color. This should only
755 * fail if the cell is read/write. Otherwise, we're incrementing
756 * the cell's reference count.
760 &subColor
.red
, &subColor
.green
, &subColor
.blue
,
762 client
) == Success
) {
764 if (XAllocColor(dpy
, cmap
, &subColor
)) {
769 /* do this to work around a problem reported by Frank Ortega */
770 subColor
.pixel
= (unsigned long) bestmatch
;
771 subColor
.red
= ctable
[bestmatch
].red
;
772 subColor
.green
= ctable
[bestmatch
].green
;
773 subColor
.blue
= ctable
[bestmatch
].blue
;
774 subColor
.flags
= DoRed
| DoGreen
| DoBlue
;
781 /* don't free table, save it for next time */
792 * Do setup for PF_GRAYSCALE pixel format.
793 * Note that buffer may be NULL.
795 static GLboolean
setup_grayscale( int client
, XMesaVisual v
,
796 XMesaBuffer buffer
, XMesaColormap cmap
)
798 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
803 XMesaBuffer prevBuffer
;
809 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
811 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
812 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
813 /* Copy colormap stuff from previous XMesaBuffer which uses same
814 * X colormap. Do this to avoid time spent in noFaultXAllocColor.
816 copy_colortable_info(buffer
, prevBuffer
);
819 /* Allocate 256 shades of gray */
821 int colorsfailed
= 0;
822 for (gray
=0;gray
<256;gray
++) {
823 GLint r
= gamma_adjust( v
->RedGamma
, gray
, 255 );
824 GLint g
= gamma_adjust( v
->GreenGamma
, gray
, 255 );
825 GLint b
= gamma_adjust( v
->BlueGamma
, gray
, 255 );
828 xcol
.red
= (r
<< 8) | r
;
829 xcol
.green
= (g
<< 8) | g
;
830 xcol
.blue
= (b
<< 8) | b
;
831 noFaultXAllocColor( client
, v
->display
,
832 cmap
, GET_COLORMAP_SIZE(v
),
833 &xcol
, &exact
, &alloced
);
838 assert(buffer
->num_alloced
<256);
839 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
840 buffer
->num_alloced
++;
845 buffer->color_table[gray*3+0] = xcol.pixel;
846 buffer->color_table[gray*3+1] = xcol.pixel;
847 buffer->color_table[gray*3+2] = xcol.pixel;
848 assert(xcol.pixel < 65536);
849 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
850 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
851 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
853 buffer
->color_table
[gray
] = xcol
.pixel
;
854 assert(xcol
.pixel
< 65536);
855 buffer
->pixel_to_r
[xcol
.pixel
] = gray
;
856 buffer
->pixel_to_g
[xcol
.pixel
] = gray
;
857 buffer
->pixel_to_b
[xcol
.pixel
] = gray
;
860 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
862 "Note: %d out of 256 needed colors do not match exactly.\n",
868 v
->dithered_pf
= PF_Grayscale
;
869 v
->undithered_pf
= PF_Grayscale
;
876 * Setup RGB rendering for a window with a PseudoColor, StaticColor,
877 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
878 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
879 * color. While this function was originally designed just for 8-bit
880 * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
881 * Dithering code contributed by Bob Mercier.
883 static GLboolean
setup_dithered_color( int client
, XMesaVisual v
,
884 XMesaBuffer buffer
, XMesaColormap cmap
)
886 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
891 XMesaBuffer prevBuffer
;
897 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
899 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
900 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
901 /* Copy colormap stuff from previous, matching XMesaBuffer.
902 * Do this to avoid time spent in noFaultXAllocColor.
904 copy_colortable_info(buffer
, prevBuffer
);
907 /* Allocate X colors and initialize color_table[], red_table[], etc */
909 int colorsfailed
= 0;
910 for (r
= 0; r
< DITH_R
; r
++) {
911 for (g
= 0; g
< DITH_G
; g
++) {
912 for (b
= 0; b
< DITH_B
; b
++) {
915 xcol
.red
=gamma_adjust(v
->RedGamma
, r
*65535/(DITH_R
-1),65535);
916 xcol
.green
=gamma_adjust(v
->GreenGamma
, g
*65535/(DITH_G
-1),65535);
917 xcol
.blue
=gamma_adjust(v
->BlueGamma
, b
*65535/(DITH_B
-1),65535);
918 noFaultXAllocColor( client
, v
->display
,
919 cmap
, GET_COLORMAP_SIZE(v
),
920 &xcol
, &exact
, &alloced
);
925 assert(buffer
->num_alloced
<256);
926 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
927 buffer
->num_alloced
++;
929 i
= DITH_MIX( r
, g
, b
);
931 buffer
->color_table
[i
] = xcol
.pixel
;
932 assert(xcol
.pixel
< 65536);
933 buffer
->pixel_to_r
[xcol
.pixel
] = r
* 255 / (DITH_R
-1);
934 buffer
->pixel_to_g
[xcol
.pixel
] = g
* 255 / (DITH_G
-1);
935 buffer
->pixel_to_b
[xcol
.pixel
] = b
* 255 / (DITH_B
-1);
940 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
942 "Note: %d out of %d needed colors do not match exactly.\n",
943 colorsfailed
, DITH_R
* DITH_G
* DITH_B
);
948 v
->dithered_pf
= PF_Dither
;
949 v
->undithered_pf
= PF_Lookup
;
955 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
956 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
957 * Special dithering tables have to be initialized.
959 static void setup_8bit_hpcr( XMesaVisual v
)
961 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
962 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
963 * on the root window AND the colormap obtainable by XGetRGBColormaps
964 * for that atom must be set on the window. (see also tkInitWindow)
965 * If that colormap is not set, the output will look stripy.
968 /* Setup color tables with gamma correction */
972 g
= 1.0 / v
->RedGamma
;
973 for (i
=0; i
<256; i
++) {
974 GLint red
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[0][i
]/255.0, g
));
975 v
->hpcr_rgbTbl
[0][i
] = CLAMP( red
, 16, 239 );
978 g
= 1.0 / v
->GreenGamma
;
979 for (i
=0; i
<256; i
++) {
980 GLint green
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[1][i
]/255.0, g
));
981 v
->hpcr_rgbTbl
[1][i
] = CLAMP( green
, 16, 239 );
984 g
= 1.0 / v
->BlueGamma
;
985 for (i
=0; i
<256; i
++) {
986 GLint blue
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[2][i
]/255.0, g
));
987 v
->hpcr_rgbTbl
[2][i
] = CLAMP( blue
, 32, 223 );
989 v
->undithered_pf
= PF_HPCR
; /* can't really disable dithering for now */
990 v
->dithered_pf
= PF_HPCR
;
992 /* which method should I use to clear */
993 /* GL_FALSE: keep the ordinary method */
994 /* GL_TRUE : clear with dither pattern */
995 v
->hpcr_clear_flag
= _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE
: GL_FALSE
;
997 if (v
->hpcr_clear_flag
) {
998 v
->hpcr_clear_pixmap
= XMesaCreatePixmap(v
->display
,
999 DefaultRootWindow(v
->display
),
1001 #ifndef XFree86Server
1002 v
->hpcr_clear_ximage
= XGetImage(v
->display
, v
->hpcr_clear_pixmap
,
1003 0, 0, 16, 2, AllPlanes
, ZPixmap
);
1010 * Setup RGB rendering for a window with a True/DirectColor visual.
1012 static void setup_truecolor( XMesaVisual v
, XMesaBuffer buffer
,
1013 XMesaColormap cmap
)
1015 unsigned long rmask
, gmask
, bmask
;
1019 /* Compute red multiplier (mask) and bit shift */
1021 rmask
= GET_REDMASK(v
);
1022 while ((rmask
& 1)==0) {
1027 /* Compute green multiplier (mask) and bit shift */
1029 gmask
= GET_GREENMASK(v
);
1030 while ((gmask
& 1)==0) {
1035 /* Compute blue multiplier (mask) and bit shift */
1037 bmask
= GET_BLUEMASK(v
);
1038 while ((bmask
& 1)==0) {
1044 * Compute component-to-pixel lookup tables and dithering kernel
1047 static GLubyte kernel
[16] = {
1048 0*16, 8*16, 2*16, 10*16,
1049 12*16, 4*16, 14*16, 6*16,
1050 3*16, 11*16, 1*16, 9*16,
1051 15*16, 7*16, 13*16, 5*16,
1053 GLint rBits
= bitcount(rmask
);
1054 GLint gBits
= bitcount(gmask
);
1055 GLint bBits
= bitcount(bmask
);
1059 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
1060 for (i
=0; i
<=rmask
; i
++)
1061 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
1062 for (i
=0; i
<=gmask
; i
++)
1063 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
1064 for (i
=0; i
<=bmask
; i
++)
1065 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
1067 /* convert RGB values from [0,255] to pixel components */
1069 for (i
=0;i
<256;i
++) {
1070 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
1071 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
1072 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
1073 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
1074 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
1075 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
1077 /* overflow protection */
1078 for (i
=256;i
<512;i
++) {
1079 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
1080 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
1081 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
1084 /* setup dithering kernel */
1086 if (gBits
> maxBits
) maxBits
= gBits
;
1087 if (bBits
> maxBits
) maxBits
= bBits
;
1088 for (i
=0;i
<16;i
++) {
1089 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
1092 v
->undithered_pf
= PF_Truecolor
;
1093 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
1097 * Now check for TrueColor visuals which we can optimize.
1099 if ( GET_REDMASK(v
) ==0x0000ff
1100 && GET_GREENMASK(v
)==0x00ff00
1101 && GET_BLUEMASK(v
) ==0xff0000
1102 && CHECK_BYTE_ORDER(v
)
1103 && v
->BitsPerPixel
==32
1104 && sizeof(GLuint
)==4
1105 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1106 /* common 32 bpp config used on SGI, Sun */
1107 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
;
1109 else if (GET_REDMASK(v
) ==0xff0000
1110 && GET_GREENMASK(v
)==0x00ff00
1111 && GET_BLUEMASK(v
) ==0x0000ff
1112 && CHECK_BYTE_ORDER(v
)
1113 && v
->BitsPerPixel
==32
1114 && sizeof(GLuint
)==4
1115 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1116 /* common 32 bpp config used on Linux, HP, IBM */
1117 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
;
1119 else if (GET_REDMASK(v
) ==0xff0000
1120 && GET_GREENMASK(v
)==0x00ff00
1121 && GET_BLUEMASK(v
) ==0x0000ff
1122 && CHECK_BYTE_ORDER(v
)
1123 && v
->BitsPerPixel
==24
1124 && sizeof(GLuint
)==4
1125 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1126 /* common packed 24 bpp config used on Linux */
1127 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
;
1129 else if (GET_REDMASK(v
) ==0xf800
1130 && GET_GREENMASK(v
)==0x07e0
1131 && GET_BLUEMASK(v
) ==0x001f
1132 && CHECK_BYTE_ORDER(v
)
1133 && v
->BitsPerPixel
==16
1134 && sizeof(GLushort
)==2
1135 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1136 /* 5-6-5 color weight on common PC VGA boards */
1137 v
->undithered_pf
= PF_5R6G5B
;
1138 v
->dithered_pf
= PF_Dither_5R6G5B
;
1140 else if (GET_REDMASK(v
) ==0xe0
1141 && GET_GREENMASK(v
)==0x1c
1142 && GET_BLUEMASK(v
) ==0x03
1143 && CHECK_FOR_HPCR(v
)) {
1144 setup_8bit_hpcr( v
);
1151 * Setup RGB rendering for a window with a monochrome visual.
1153 static void setup_monochrome( XMesaVisual v
, XMesaBuffer b
)
1156 v
->dithered_pf
= v
->undithered_pf
= PF_1Bit
;
1157 /* if black=1 then we must flip pixel values */
1158 v
->bitFlip
= (GET_BLACK_PIXEL(v
) != 0);
1164 * When a context is "made current" for the first time, we can finally
1165 * finish initializing the context's visual and buffer information.
1166 * Input: v - the XMesaVisual to initialize
1167 * b - the XMesaBuffer to initialize (may be NULL)
1168 * rgb_flag - TRUE = RGBA mode, FALSE = color index mode
1169 * window - the window/pixmap we're rendering into
1170 * cmap - the colormap associated with the window/pixmap
1171 * Return: GL_TRUE=success, GL_FALSE=failure
1173 static GLboolean
initialize_visual_and_buffer( int client
,
1177 XMesaDrawable window
,
1178 XMesaColormap cmap
)
1180 #ifndef XFree86Server
1185 assert(b
->xm_visual
== v
);
1188 /* Save true bits/pixel */
1189 v
->BitsPerPixel
= bits_per_pixel(v
);
1190 assert(v
->BitsPerPixel
> 0);
1193 if (rgb_flag
==GL_FALSE
) {
1194 /* COLOR-INDEXED WINDOW:
1195 * Even if the visual is TrueColor or DirectColor we treat it as
1196 * being color indexed. This is weird but might be useful to someone.
1198 v
->dithered_pf
= v
->undithered_pf
= PF_Index
;
1199 v
->index_bits
= GET_VISUAL_DEPTH(v
);
1203 * We support RGB rendering into almost any kind of visual.
1206 xclass
= GET_VISUAL_CLASS(v
);
1207 if (xclass
==TrueColor
|| xclass
==DirectColor
) {
1208 setup_truecolor( v
, b
, cmap
);
1210 else if (xclass
==StaticGray
&& GET_VISUAL_DEPTH(v
)==1) {
1211 setup_monochrome( v
, b
);
1213 else if (xclass
==GrayScale
|| xclass
==StaticGray
) {
1214 if (!setup_grayscale( client
, v
, b
, cmap
)) {
1218 else if ((xclass
==PseudoColor
|| xclass
==StaticColor
)
1219 && GET_VISUAL_DEPTH(v
)>=4 && GET_VISUAL_DEPTH(v
)<=16) {
1220 if (!setup_dithered_color( client
, v
, b
, cmap
)) {
1225 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.");
1230 if (_mesa_getenv("MESA_NO_DITHER")) {
1231 v
->dithered_pf
= v
->undithered_pf
;
1237 * If MESA_INFO env var is set print out some debugging info
1238 * which can help Brian figure out what's going on when a user
1241 if (_mesa_getenv("MESA_INFO")) {
1242 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
1243 _mesa_printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
1244 _mesa_printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
1245 _mesa_printf("X/Mesa level = %d\n", v
->level
);
1246 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
1247 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
1251 /* Do window-specific initializations */
1253 /* Window dimensions */
1255 get_drawable_size( v
->display
, window
, &w
, &h
);
1259 b
->frontbuffer
= window
;
1261 /* Setup for single/double buffering */
1262 if (v
->mesa_visual
.doubleBufferMode
) {
1263 /* Double buffered */
1264 #ifndef XFree86Server
1265 b
->shm
= check_for_xshm( v
->display
);
1267 xmesa_alloc_back_buffer( b
);
1268 if (b
->db_state
==BACK_PIXMAP
) {
1269 b
->buffer
= (XMesaDrawable
)b
->backpixmap
;
1276 /* Single Buffered */
1277 b
->buffer
= b
->frontbuffer
;
1280 /* X11 graphics contexts */
1281 #ifdef XFree86Server
1282 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
1284 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
1286 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
1288 /* cleargc - for glClear() */
1289 #ifdef XFree86Server
1290 b
->cleargc
= CreateScratchGC(v
->display
, window
->depth
);
1292 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
1294 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
1297 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1298 * Patch contributed by Michael Pichler May 15, 1995.
1300 #ifdef XFree86Server
1301 b
->swapgc
= CreateScratchGC(v
->display
, window
->depth
);
1305 dixChangeGC(NullClient
, b
->swapgc
, GCGraphicsExposures
, v
, NULL
);
1308 gcvalues
.graphics_exposures
= False
;
1309 b
->swapgc
= XCreateGC( v
->display
, window
,
1310 GCGraphicsExposures
, &gcvalues
);
1312 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
1314 * Set fill style and tile pixmap once for all for HPCR stuff
1315 * (instead of doing it each time in clear_color_HPCR_pixmap())
1316 * Initialize whole stuff
1317 * Patch contributed by Jacques Leroy March 8, 1998.
1319 if (v
->hpcr_clear_flag
&& b
->buffer
!=XIMAGE
) {
1321 for (i
=0; i
<16; i
++)
1323 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 0, 0);
1324 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 1, 0);
1326 XMesaPutImage(b
->display
, (XMesaDrawable
)v
->hpcr_clear_pixmap
,
1327 b
->cleargc
, v
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1328 XMesaSetFillStyle( v
->display
, b
->cleargc
, FillTiled
);
1329 XMesaSetTile( v
->display
, b
->cleargc
, v
->hpcr_clear_pixmap
);
1332 /* Initialize the row buffer XImage for use in write_color_span() */
1333 #ifdef XFree86Server
1334 b
->rowimage
= XMesaCreateImage(GET_VISUAL_DEPTH(v
), MAX_WIDTH
, 1,
1335 (char *)MALLOC(MAX_WIDTH
*4));
1337 b
->rowimage
= XCreateImage( v
->display
,
1340 ZPixmap
, 0, /*format, offset*/
1341 (char*) MALLOC(MAX_WIDTH
*4), /*data*/
1342 MAX_WIDTH
, 1, /*width, height*/
1344 0 /*bytes_per_line*/ );
1354 * Convert an RGBA color to a pixel value.
1357 xmesa_color_to_pixel( XMesaContext xmesa
, GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
1360 switch (pixelFormat
) {
1366 PACK_TRUECOLOR( p
, r
, g
, b
);
1370 return PACK_8A8B8G8R( r
, g
, b
, a
);
1374 return PACK_8R8G8B( r
, g
, b
);
1376 return PACK_5R6G5B( r
, g
, b
);
1380 return DITHER( 1, 0, r
, g
, b
);
1383 /* 382 = (3*255)/2 */
1384 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
1386 return DITHER_HPCR(1, 1, r
, g
, b
);
1390 return LOOKUP( r
, g
, b
);
1393 return GRAY_RGB( r
, g
, b
);
1394 case PF_Dither_True
:
1396 case PF_Dither_5R6G5B
:
1399 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
1403 _mesa_problem(NULL
, "Bad pixel format in xmesa_color_to_pixel");
1409 /**********************************************************************/
1410 /***** Public Functions *****/
1411 /**********************************************************************/
1415 * Create a new X/Mesa visual.
1416 * Input: display - X11 display
1417 * visinfo - an XVisualInfo pointer
1418 * rgb_flag - GL_TRUE = RGB mode,
1419 * GL_FALSE = color index mode
1420 * alpha_flag - alpha buffer requested?
1421 * db_flag - GL_TRUE = double-buffered,
1422 * GL_FALSE = single buffered
1423 * stereo_flag - stereo visual?
1424 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1425 * GL_FALSE = use an off-screen pixmap for back buffer
1426 * depth_size - requested bits/depth values, or zero
1427 * stencil_size - requested bits/stencil values, or zero
1428 * accum_red_size - requested bits/red accum values, or zero
1429 * accum_green_size - requested bits/green accum values, or zero
1430 * accum_blue_size - requested bits/blue accum values, or zero
1431 * accum_alpha_size - requested bits/alpha accum values, or zero
1432 * num_samples - number of samples/pixel if multisampling, or zero
1433 * level - visual level, usually 0
1434 * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT
1435 * Return; a new XMesaVisual or 0 if error.
1437 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
1438 XMesaVisualInfo visinfo
,
1440 GLboolean alpha_flag
,
1442 GLboolean stereo_flag
,
1443 GLboolean ximage_flag
,
1446 GLint accum_red_size
,
1447 GLint accum_green_size
,
1448 GLint accum_blue_size
,
1449 GLint accum_alpha_size
,
1452 GLint visualCaveat
)
1456 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
1458 /* For debugging only */
1459 if (_mesa_getenv("MESA_XSYNC")) {
1460 /* This makes debugging X easier.
1461 * In your debugger, set a breakpoint on _XError to stop when an
1462 * X protocol error is generated.
1464 #ifdef XFree86Server
1467 XSynchronize( display
, 1 );
1471 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
1477 * In the X server, NULL is passed in for the display. It will have
1478 * to be set before using this visual. See XMesaSetVisualDisplay()
1481 v
->display
= display
;
1483 /* Save a copy of the XVisualInfo struct because the user may XFREE()
1484 * the struct but we may need some of the information contained in it
1487 #ifdef XFree86Server
1488 v
->visinfo
= visinfo
;
1490 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
1495 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
1498 #ifdef XFree86Server
1499 /* Initialize the depth of the screen */
1501 PixmapFormatRec
*format
;
1503 for (format
= screenInfo
.formats
;
1504 format
->depth
!= display
->rootDepth
;
1507 v
->screen_depth
= format
->bitsPerPixel
;
1511 /* check for MESA_GAMMA environment variable */
1512 gamma
= _mesa_getenv("MESA_GAMMA");
1514 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
1515 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
1516 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
1517 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
1518 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
1521 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
1524 v
->ximage_flag
= ximage_flag
;
1526 v
->VisualCaveat
= visualCaveat
;
1528 (void) initialize_visual_and_buffer( 0, v
, NULL
, rgb_flag
, 0, 0 );
1532 xclass
= GET_VISUAL_CLASS(v
);
1533 if (xclass
==TrueColor
|| xclass
==DirectColor
) {
1534 red_bits
= bitcount(GET_REDMASK(v
));
1535 green_bits
= bitcount(GET_GREENMASK(v
));
1536 blue_bits
= bitcount(GET_BLUEMASK(v
));
1540 /* this is an approximation */
1542 depth
= GET_VISUAL_DEPTH(v
);
1543 red_bits
= depth
/ 3;
1545 green_bits
= depth
/ 2;
1546 depth
-= green_bits
;
1549 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
1553 if (alpha_flag
&& alpha_bits
== 0)
1556 _mesa_initialize_visual( &v
->mesa_visual
,
1557 rgb_flag
, db_flag
, stereo_flag
,
1558 red_bits
, green_bits
,
1559 blue_bits
, alpha_bits
,
1563 accum_red_size
, accum_green_size
,
1564 accum_blue_size
, accum_alpha_size
,
1570 void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
)
1576 void XMesaDestroyVisual( XMesaVisual v
)
1578 #ifndef XFree86Server
1587 * Create a new XMesaContext.
1588 * Input: v - XMesaVisual
1589 * share_list - another XMesaContext with which to share display
1590 * lists or NULL if no sharing is wanted.
1591 * Return: an XMesaContext or NULL if error.
1593 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
1595 static GLboolean firstTime
= GL_TRUE
;
1598 GLboolean direct
= GL_TRUE
; /* not really */
1601 _glthread_INIT_MUTEX(_xmesa_lock
);
1602 firstTime
= GL_FALSE
;
1605 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
1610 ctx
= c
->gl_ctx
= _mesa_create_context( &v
->mesa_visual
,
1611 share_list
? share_list
->gl_ctx
: (GLcontext
*) NULL
,
1612 (void *) c
, direct
);
1618 _mesa_enable_sw_extensions(ctx
);
1619 _mesa_enable_1_3_extensions(ctx
);
1620 _mesa_enable_1_4_extensions(ctx
);
1622 if (CHECK_BYTE_ORDER(v
)) {
1623 c
->swapbytes
= GL_FALSE
;
1626 c
->swapbytes
= GL_TRUE
;
1630 c
->xm_draw_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1631 c
->xm_read_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1632 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1633 c
->display
= v
->display
;
1634 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
1636 ctx
->Driver
.UpdateState
= xmesa_update_state
;
1638 /* Initialize the software rasterizer and helper modules.
1640 _swrast_CreateContext( ctx
);
1641 _ac_CreateContext( ctx
);
1642 _tnl_CreateContext( ctx
);
1643 _swsetup_CreateContext( ctx
);
1645 xmesa_register_swrast_functions( ctx
);
1647 /* Set up some constant pointers:
1649 xmesa_init_pointers( ctx
);
1657 void XMesaDestroyContext( XMesaContext c
)
1660 if (c
->xm_draw_buffer
&& c
->xm_buffer
->FXctx
)
1661 fxMesaDestroyContext(c
->xm_draw_buffer
->FXctx
);
1664 _swsetup_DestroyContext( c
->gl_ctx
);
1665 _swrast_DestroyContext( c
->gl_ctx
);
1666 _tnl_DestroyContext( c
->gl_ctx
);
1667 _ac_DestroyContext( c
->gl_ctx
);
1668 _mesa_destroy_context( c
->gl_ctx
);
1677 * XXX this isn't a public function! It's a hack for the 3Dfx driver.
1678 * Create a new XMesaBuffer from an X window.
1679 * Input: v - the XMesaVisual
1682 * Return: new XMesaBuffer or NULL if error
1684 XMesaBuffer
XMesaCreateWindowBuffer2( XMesaVisual v
, XMesaWindow w
,
1687 #ifndef XFree86Server
1688 XWindowAttributes attr
;
1695 XMesaBuffer b
= alloc_xmesa_buffer();
1702 #ifdef XFree86Server
1703 client
= CLIENT_ID(((XMesaDrawable
)w
)->id
);
1708 #ifdef XFree86Server
1709 if (GET_VISUAL_DEPTH(v
) != ((XMesaDrawable
)w
)->depth
) {
1711 XGetWindowAttributes( v
->display
, w
, &attr
);
1713 if (GET_VISUAL_DEPTH(v
) != attr
.depth
) {
1715 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual and window!\n");
1721 b
->display
= v
->display
;
1722 #ifdef XFree86Server
1723 b
->cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
1725 if (attr
.colormap
) {
1726 b
->cmap
= attr
.colormap
;
1729 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1730 /* this is weird, a window w/out a colormap!? */
1731 /* OK, let's just allocate a new one and hope for the best */
1732 b
->cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1736 /* determine back buffer implementation */
1737 if (v
->mesa_visual
.doubleBufferMode
) {
1738 if (v
->ximage_flag
) {
1739 b
->db_state
= BACK_XIMAGE
;
1742 b
->db_state
= BACK_PIXMAP
;
1749 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1751 v
->mesa_visual
.depthBits
> 0,
1752 v
->mesa_visual
.stencilBits
> 0,
1753 v
->mesa_visual
.accumRedBits
> 0,
1754 v
->mesa_visual
.alphaBits
> 0 );
1756 if (!initialize_visual_and_buffer( client
, v
, b
, v
->mesa_visual
.rgbMode
,
1757 (XMesaDrawable
)w
, b
->cmap
)) {
1758 free_xmesa_buffer(client
, b
);
1763 fxEnvVar
= _mesa_getenv("MESA_GLX_FX");
1765 if (fxEnvVar
[0]!='d') {
1769 if (v
->mesa_visual
.depthBits
> 0) {
1770 attribs
[numAttribs
++] = FXMESA_DEPTH_SIZE
;
1771 attribs
[numAttribs
++] = 1;
1773 if (v
->mesa_visual
.doubleBufferMode
) {
1774 attribs
[numAttribs
++] = FXMESA_DOUBLEBUFFER
;
1776 if (v
->mesa_visual
.accumRedBits
> 0) {
1777 attribs
[numAttribs
++] = FXMESA_ACCUM_SIZE
;
1778 attribs
[numAttribs
++] = v
->mesa_visual
.accumRedBits
;
1780 if (v
->mesa_visual
.stencilBits
> 0) {
1781 attribs
[numAttribs
++] = FXMESA_STENCIL_SIZE
;
1782 attribs
[numAttribs
++] = v
->mesa_visual
.stencilBits
;
1784 if (v
->mesa_visual
.alphaBits
> 0) {
1785 attribs
[numAttribs
++] = FXMESA_ALPHA_SIZE
;
1786 attribs
[numAttribs
++] = 1;
1789 #define FXMESA_SHARE_CONTEXT 990099 /* keep in sync with fxapi.c! */
1790 attribs
[numAttribs
++] = FXMESA_SHARE_CONTEXT
;
1791 attribs
[numAttribs
++] = (int) c
->gl_ctx
;
1793 attribs
[numAttribs
++] = FXMESA_NONE
;
1795 if ((hw
= fxQueryHardware())==GR_SSTTYPE_VOODOO
) {
1796 b
->FXctx
= fxMesaCreateBestContext(0, b
->width
, b
->height
, attribs
);
1797 if ((v
->undithered_pf
!=PF_Index
) && (b
->backimage
)) {
1798 b
->FXisHackUsable
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1799 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1800 b
->FXwindowHack
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1802 b
->FXwindowHack
= GL_FALSE
;
1806 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1807 b
->FXctx
= fxMesaCreateContext(w
, GR_RESOLUTION_NONE
,
1808 GR_REFRESH_75Hz
, attribs
);
1810 b
->FXctx
= fxMesaCreateBestContext(0, b
->width
, b
->height
, attribs
);
1811 b
->FXisHackUsable
= GL_FALSE
;
1812 b
->FXwindowHack
= GL_FALSE
;
1816 "voodoo %d, wid %d height %d hack: usable %d active %d\n",
1817 hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack);
1822 _mesa_warning(NULL
, "WARNING: This Mesa Library includes the Glide driver but\n");
1823 _mesa_warning(NULL
, " you have not defined the MESA_GLX_FX env. var.\n");
1824 _mesa_warning(NULL
, " (check the README.3DFX file for more information).\n\n");
1825 _mesa_warning(NULL
, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
1833 XMesaBuffer
XMesaCreateWindowBuffer( XMesaVisual v
, XMesaWindow w
)
1835 return XMesaCreateWindowBuffer2( v
, w
, NULL
);
1840 * Create a new XMesaBuffer from an X pixmap.
1841 * Input: v - the XMesaVisual
1843 * cmap - the colormap, may be 0 if using a TrueColor or DirectColor
1844 * visual for the pixmap
1845 * Return: new XMesaBuffer or NULL if error
1847 XMesaBuffer
XMesaCreatePixmapBuffer( XMesaVisual v
,
1848 XMesaPixmap p
, XMesaColormap cmap
)
1851 XMesaBuffer b
= alloc_xmesa_buffer();
1857 #ifdef XFree86Server
1858 client
= CLIENT_ID(((XMesaDrawable
)p
)->id
);
1865 b
->display
= v
->display
;
1868 /* determine back buffer implementation */
1869 if (v
->mesa_visual
.doubleBufferMode
) {
1870 if (v
->ximage_flag
) {
1871 b
->db_state
= BACK_XIMAGE
;
1874 b
->db_state
= BACK_PIXMAP
;
1881 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1883 v
->mesa_visual
.depthBits
> 0,
1884 v
->mesa_visual
.stencilBits
> 0,
1885 v
->mesa_visual
.accumRedBits
+
1886 v
->mesa_visual
.accumGreenBits
+
1887 v
->mesa_visual
.accumBlueBits
> 0,
1888 v
->mesa_visual
.alphaBits
> 0 );
1890 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1891 (XMesaDrawable
)p
, cmap
)) {
1892 free_xmesa_buffer(client
, b
);
1901 XMesaBuffer
XMesaCreatePBuffer( XMesaVisual v
, XMesaColormap cmap
,
1902 unsigned int width
, unsigned int height
)
1904 #ifdef XFree86Server
1909 XMesaDrawable drawable
; /* X Pixmap Drawable */
1910 XMesaBuffer b
= alloc_xmesa_buffer();
1917 b
->display
= v
->display
;
1920 /* allocate pixmap for front buffer */
1921 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1922 drawable
= XCreatePixmap( v
->display
, root
, width
, height
, v
->visinfo
->depth
);
1924 /* determine back buffer implementation */
1925 if (v
->mesa_visual
.doubleBufferMode
) {
1926 if (v
->ximage_flag
) {
1927 b
->db_state
= BACK_XIMAGE
;
1930 b
->db_state
= BACK_PIXMAP
;
1937 _mesa_initialize_framebuffer(&b
->mesa_buffer
,
1939 v
->mesa_visual
.depthBits
> 0,
1940 v
->mesa_visual
.stencilBits
> 0,
1941 v
->mesa_visual
.accumRedBits
+
1942 v
->mesa_visual
.accumGreenBits
+
1943 v
->mesa_visual
.accumBlueBits
> 0,
1944 v
->mesa_visual
.alphaBits
> 0 );
1946 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1948 free_xmesa_buffer(client
, b
);
1959 * Deallocate an XMesaBuffer structure and all related info.
1961 void XMesaDestroyBuffer( XMesaBuffer b
)
1965 #ifdef XFree86Server
1967 client
= CLIENT_ID(b
->frontbuffer
->id
);
1970 if (b
->gc
) XMesaFreeGC( b
->xm_visual
->display
, b
->gc
);
1971 if (b
->cleargc
) XMesaFreeGC( b
->xm_visual
->display
, b
->cleargc
);
1972 if (b
->swapgc
) XMesaFreeGC( b
->xm_visual
->display
, b
->swapgc
);
1975 #if defined(USE_XSHM) && !defined(XFree86Server)
1977 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
1978 XDestroyImage( b
->backimage
);
1979 shmdt( b
->shminfo
.shmaddr
);
1983 XMesaDestroyImage( b
->backimage
);
1985 if (b
->backpixmap
) {
1986 XMesaFreePixmap( b
->xm_visual
->display
, b
->backpixmap
);
1987 if (b
->xm_visual
->hpcr_clear_flag
) {
1988 XMesaFreePixmap( b
->xm_visual
->display
,
1989 b
->xm_visual
->hpcr_clear_pixmap
);
1990 XMesaDestroyImage( b
->xm_visual
->hpcr_clear_ximage
);
1994 FREE( b
->rowimage
->data
);
1995 b
->rowimage
->data
= NULL
;
1996 XMesaDestroyImage( b
->rowimage
);
1999 free_xmesa_buffer(client
, b
);
2005 * Bind buffer b to context c and make c the current rendering context.
2007 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
2009 return XMesaMakeCurrent2( c
, b
, b
);
2014 * Bind buffer b to context c and make c the current rendering context.
2016 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
2017 XMesaBuffer readBuffer
)
2020 if (!drawBuffer
|| !readBuffer
)
2021 return GL_FALSE
; /* must specify buffers! */
2024 if (drawBuffer
->FXctx
) {
2025 fxMesaMakeCurrent(drawBuffer
->FXctx
);
2027 c
->xm_draw_buffer
= drawBuffer
;
2028 c
->xm_read_buffer
= readBuffer
;
2029 c
->xm_buffer
= drawBuffer
;
2034 if (c
->gl_ctx
== _mesa_get_current_context()
2035 && c
->xm_draw_buffer
== drawBuffer
2036 && c
->xm_read_buffer
== readBuffer
2037 && c
->xm_draw_buffer
->wasCurrent
) {
2038 /* same context and buffer, do nothing */
2042 c
->xm_draw_buffer
= drawBuffer
;
2043 c
->xm_read_buffer
= readBuffer
;
2044 c
->xm_buffer
= drawBuffer
;
2046 _mesa_make_current2(c
->gl_ctx
,
2047 &drawBuffer
->mesa_buffer
,
2048 &readBuffer
->mesa_buffer
);
2050 if (c
->gl_ctx
->Viewport
.Width
== 0) {
2051 /* initialize viewport to window size */
2052 _mesa_Viewport( 0, 0, drawBuffer
->width
, drawBuffer
->height
);
2053 c
->gl_ctx
->Scissor
.Width
= drawBuffer
->width
;
2054 c
->gl_ctx
->Scissor
.Height
= drawBuffer
->height
;
2057 if (c
->xm_visual
->mesa_visual
.rgbMode
) {
2059 * Must recompute and set these pixel values because colormap
2060 * can be different for different windows.
2062 c
->clearpixel
= xmesa_color_to_pixel( c
,
2067 c
->xm_visual
->undithered_pf
);
2068 XMesaSetForeground(c
->display
, c
->xm_draw_buffer
->cleargc
, c
->clearpixel
);
2071 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
2072 c
->xm_draw_buffer
->wasCurrent
= GL_TRUE
;
2076 _mesa_make_current2( NULL
, NULL
, NULL
);
2083 * Unbind the context c from its buffer.
2085 GLboolean
XMesaUnbindContext( XMesaContext c
)
2087 /* A no-op for XFree86 integration purposes */
2092 XMesaContext
XMesaGetCurrentContext( void )
2094 GET_CURRENT_CONTEXT(ctx
);
2096 XMesaContext xmesa
= (XMesaContext
) ctx
->DriverCtx
;
2105 XMesaBuffer
XMesaGetCurrentBuffer( void )
2107 GET_CURRENT_CONTEXT(ctx
);
2109 XMesaContext xmesa
= (XMesaContext
) ctx
->DriverCtx
;
2110 return xmesa
->xm_draw_buffer
;
2118 /* New in Mesa 3.1 */
2119 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
2121 GET_CURRENT_CONTEXT(ctx
);
2123 XMesaContext xmesa
= (XMesaContext
) ctx
->DriverCtx
;
2124 return xmesa
->xm_read_buffer
;
2132 GLboolean
XMesaForceCurrent(XMesaContext c
)
2135 if (c
->gl_ctx
!= _mesa_get_current_context()) {
2136 _mesa_make_current(c
->gl_ctx
, &c
->xm_draw_buffer
->mesa_buffer
);
2140 _mesa_make_current(NULL
, NULL
);
2146 GLboolean
XMesaLoseCurrent(XMesaContext c
)
2149 _mesa_make_current(NULL
, NULL
);
2155 * Switch 3Dfx support hack between window and full-screen mode.
2157 GLboolean
XMesaSetFXmode( GLint mode
)
2160 const char *fx
= _mesa_getenv("MESA_GLX_FX");
2161 if (fx
&& fx
[0] != 'd') {
2162 GET_CURRENT_CONTEXT(ctx
);
2163 GrHwConfiguration hw
;
2164 if (!FX_grSstQueryHardware(&hw
)) {
2165 /*fprintf(stderr, "!grSstQueryHardware\n");*/
2168 if (hw
.num_sst
< 1) {
2169 /*fprintf(stderr, "hw.num_sst < 1\n");*/
2173 XMesaContext xmesa
= (XMesaContext
) ctx
->DriverCtx
;
2174 if (mode
== XMESA_FX_WINDOW
) {
2175 if (xmesa
->xm_draw_buffer
->FXisHackUsable
) {
2176 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
2177 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_TRUE
;
2181 else if (mode
== XMESA_FX_FULLSCREEN
) {
2182 FX_grSstControl(GR_CONTROL_ACTIVATE
);
2183 xmesa
->xm_draw_buffer
->FXwindowHack
= GL_FALSE
;
2187 /* Error: Bad mode value */
2191 /*fprintf(stderr, "fallthrough\n");*/
2202 * Read image from VooDoo frame buffer into X/Mesa's back XImage.
2204 static void FXgetImage( XMesaBuffer b
)
2206 GET_CURRENT_CONTEXT(ctx
);
2207 static unsigned short pixbuf
[MAX_WIDTH
];
2211 unsigned int bw
, depth
, width
, height
;
2212 XMesaContext xmesa
= (XMesaContext
) ctx
->DriverCtx
;
2214 #ifdef XFree86Server
2215 x
= b
->frontbuffer
->x
;
2216 y
= b
->frontbuffer
->y
;
2217 width
= b
->frontbuffer
->width
;
2218 height
= b
->frontbuffer
->height
;
2219 depth
= b
->frontbuffer
->depth
;
2221 XGetGeometry( b
->xm_visual
->display
, b
->frontbuffer
,
2222 &root
, &xpos
, &ypos
, &width
, &height
, &bw
, &depth
);
2224 if (b
->width
!= width
|| b
->height
!= height
) {
2225 b
->width
= MIN2((int)width
, b
->FXctx
->width
);
2226 b
->height
= MIN2((int)height
, b
->FXctx
->height
);
2228 b
->width
--; /* prevent odd width */
2229 xmesa_alloc_back_buffer( b
);
2232 grLfbWriteColorFormat(GR_COLORFORMAT_ARGB
);
2233 if (b
->xm_visual
->undithered_pf
==PF_5R6G5B
) {
2234 /* Special case: 16bpp RGB */
2235 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
, /* src buffer */
2236 0, b
->FXctx
->height
- b
->height
, /*pos*/
2237 b
->width
, b
->height
, /* size */
2238 b
->width
* sizeof(GLushort
), /* stride */
2239 b
->backimage
->data
); /* dest buffer */
2241 else if (b
->xm_visual
->dithered_pf
==PF_Dither
2242 && GET_VISUAL_DEPTH(b
->xm_visual
)==8) {
2243 /* Special case: 8bpp RGB */
2244 for (y
=0;y
<b
->height
;y
++) {
2245 GLubyte
*ptr
= (GLubyte
*) b
->backimage
->data
2246 + b
->backimage
->bytes_per_line
* y
;
2249 /* read row from 3Dfx frame buffer */
2250 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2251 0, b
->FXctx
->height
-(b
->height
-y
),
2256 /* write to XImage back buffer */
2257 for (x
=0;x
<b
->width
;x
++) {
2258 GLubyte r
= (pixbuf
[x
] & 0xf800) >> 8;
2259 GLubyte g
= (pixbuf
[x
] & 0x07e0) >> 3;
2260 GLubyte b
= (pixbuf
[x
] & 0x001f) << 3;
2261 *ptr
++ = XDITHER( x
, r
, g
, b
);
2266 /* General case: slow! */
2267 for (y
=0;y
<b
->height
;y
++) {
2268 /* read row from 3Dfx frame buffer */
2269 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2270 0, b
->FXctx
->height
-(b
->height
-y
),
2275 /* write to XImage back buffer */
2276 for (x
=0;x
<b
->width
;x
++) {
2277 XMesaPutPixel(b
->backimage
,x
,y
,
2278 xmesa_color_to_pixel(xmesa
,
2279 (pixbuf
[x
] & 0xf800) >> 8,
2280 (pixbuf
[x
] & 0x07e0) >> 3,
2281 (pixbuf
[x
] & 0x001f) << 3,
2283 b
->xm_visual
->undithered_pf
));
2287 grLfbWriteColorFormat(GR_COLORFORMAT_ABGR
);
2293 * Copy the back buffer to the front buffer. If there's no back buffer
2296 void XMesaSwapBuffers( XMesaBuffer b
)
2298 GET_CURRENT_CONTEXT(ctx
);
2300 /* If we're swapping the buffer associated with the current context
2301 * we have to flush any pending rendering commands first.
2303 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2304 _mesa_notifySwapBuffers(ctx
);
2309 fxMesaSwapBuffers();
2311 if (b
->FXwindowHack
)
2318 /* Copy Ximage from host's memory to server's window */
2319 #if defined(USE_XSHM) && !defined(XFree86Server)
2321 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2322 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2325 0, 0, b
->width
, b
->height
, False
);
2326 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2331 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2332 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2335 0, 0, b
->width
, b
->height
);
2336 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2340 /* Copy pixmap to window on server */
2341 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2342 XMesaCopyArea( b
->xm_visual
->display
,
2343 b
->backpixmap
, /* source drawable */
2344 b
->frontbuffer
, /* dest. drawable */
2346 0, 0, b
->width
, b
->height
, /* source region */
2347 0, 0 /* dest region */
2349 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2352 #if !defined(XFree86Server)
2353 XSync( b
->xm_visual
->display
, False
);
2360 * Copy sub-region of back buffer to front buffer
2362 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
2364 GET_CURRENT_CONTEXT(ctx
);
2366 /* If we're swapping the buffer associated with the current context
2367 * we have to flush any pending rendering commands first.
2369 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2370 _mesa_notifySwapBuffers(ctx
);
2373 int yTop
= b
->height
- y
- height
;
2376 fxMesaSwapBuffers();
2377 if (b
->FXwindowHack
)
2384 /* Copy Ximage from host's memory to server's window */
2385 #if defined(USE_XSHM) && !defined(XFree86Server)
2387 /* XXX assuming width and height aren't too large! */
2388 XShmPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2390 b
->backimage
, x
, yTop
,
2391 x
, yTop
, width
, height
, False
);
2392 /* wait for finished event??? */
2397 /* XXX assuming width and height aren't too large! */
2398 XMesaPutImage( b
->xm_visual
->display
, b
->frontbuffer
,
2400 b
->backimage
, x
, yTop
,
2401 x
, yTop
, width
, height
);
2405 /* Copy pixmap to window on server */
2406 XMesaCopyArea( b
->xm_visual
->display
,
2407 b
->backpixmap
, /* source drawable */
2408 b
->frontbuffer
, /* dest. drawable */
2410 x
, yTop
, width
, height
, /* source region */
2411 x
, yTop
/* dest region */
2419 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2420 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2421 * back buffer directly.
2422 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2423 * ximage - pointer to back buffer's XImage, or NULL
2424 * Return: GL_TRUE = context is double buffered
2425 * GL_FALSE = context is single buffered
2427 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
2428 XMesaPixmap
*pixmap
,
2429 XMesaImage
**ximage
)
2432 if (pixmap
) *pixmap
= b
->backpixmap
;
2433 if (ximage
) *ximage
= b
->backimage
;
2445 * Return the depth buffer associated with an XMesaBuffer.
2446 * Input: b - the XMesa buffer handle
2447 * Output: width, height - size of buffer in pixels
2448 * bytesPerValue - bytes per depth value (2 or 4)
2449 * buffer - pointer to depth buffer values
2450 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2452 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
2453 GLint
*bytesPerValue
, void **buffer
)
2455 if (!b
->mesa_buffer
.DepthBuffer
) {
2463 *width
= b
->mesa_buffer
.Width
;
2464 *height
= b
->mesa_buffer
.Height
;
2465 *bytesPerValue
= sizeof(GLdepth
);
2466 *buffer
= b
->mesa_buffer
.DepthBuffer
;
2472 void XMesaFlush( XMesaContext c
)
2474 if (c
&& c
->xm_visual
) {
2475 #ifdef XFree86Server
2478 XSync( c
->xm_visual
->display
, False
);
2485 const char *XMesaGetString( XMesaContext c
, int name
)
2488 if (name
==XMESA_VERSION
) {
2491 else if (name
==XMESA_EXTENSIONS
) {
2501 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
2504 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
2505 if (b
->frontbuffer
==d
&& b
->display
==dpy
) {
2515 * Look for XMesaBuffers whose X window has been destroyed.
2516 * Deallocate any such XMesaBuffers.
2518 void XMesaGarbageCollect( void )
2520 XMesaBuffer b
, next
;
2521 for (b
=XMesaBufferList
; b
; b
=next
) {
2523 if (b
->display
&& b
->frontbuffer
&& b
->type
== WINDOW
) {
2524 #ifdef XFree86Server
2527 XSync(b
->display
, False
);
2528 if (!window_exists( b
->display
, b
->frontbuffer
)) {
2529 /* found a dead window, free the ancillary info */
2530 XMesaDestroyBuffer( b
);
2538 void XMesaReset( void )
2540 while (XMesaBufferList
)
2541 XMesaDestroyBuffer(XMesaBufferList
);
2543 XMesaBufferList
= NULL
;
2547 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
2548 GLfloat red
, GLfloat green
,
2549 GLfloat blue
, GLfloat alpha
)
2551 GLint r
= (GLint
) (red
* 255.0F
);
2552 GLint g
= (GLint
) (green
* 255.0F
);
2553 GLint b
= (GLint
) (blue
* 255.0F
);
2554 GLint a
= (GLint
) (alpha
* 255.0F
);
2556 switch (xmesa
->pixelformat
) {
2562 PACK_TRUECOLOR( p
, r
, g
, b
);
2566 return PACK_8A8B8G8R( r
, g
, b
, a
);
2568 return PACK_8R8G8B( r
, g
, b
);
2570 return PACK_5R6G5B( r
, g
, b
);
2574 return DITHER( x
, y
, r
, g
, b
);
2577 /* 382 = (3*255)/2 */
2578 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
2580 return DITHER_HPCR(x
, y
, r
, g
, b
);
2584 return LOOKUP( r
, g
, b
);
2587 return GRAY_RGB( r
, g
, b
);
2588 case PF_Dither_5R6G5B
:
2590 case PF_Dither_True
:
2593 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
2597 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
2604 * This is typically called when the window size changes and we need
2605 * to reallocate the buffer's back/depth/stencil/accum buffers.
2607 void XMesaResizeBuffers( XMesaBuffer b
)
2609 xmesa_resize_buffers( &(b
->mesa_buffer
) );