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"
68 #include "framebuffer.h"
74 #include "renderbuffer.h"
75 #include "texformat.h"
78 #include "swrast/swrast.h"
79 #include "swrast_setup/swrast_setup.h"
80 #include "array_cache/acache.h"
82 #include "tnl/t_context.h"
83 #include "tnl/t_pipeline.h"
84 #include "drivers/common/driverfuncs.h"
87 #include <GL/glxtokens.h>
91 * Global X driver lock
93 _glthread_Mutex _xmesa_lock
;
98 * Lookup tables for HPCR pixel format:
100 static short hpcr_rgbTbl
[3][256] = {
102 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
103 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
104 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
105 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
106 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
107 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
108 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
109 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
110 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
111 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
112 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
113 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
114 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
115 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
116 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
117 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
120 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
121 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
122 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
123 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
124 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
125 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
126 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
127 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
128 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
129 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
130 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
131 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
132 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
133 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
134 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
135 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
138 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
139 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
140 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
141 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
142 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
143 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
144 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
145 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
146 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
147 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
148 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
149 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
150 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
151 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
152 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
153 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
159 /**********************************************************************/
160 /***** X Utility Functions *****/
161 /**********************************************************************/
165 * Return the host's byte order as LSBFirst or MSBFirst ala X.
167 #ifndef XFree86Server
168 static int host_byte_order( void )
171 char *cptr
= (char *) &i
;
172 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
180 #ifndef XFree86Server
181 static volatile int mesaXErrorFlag
= 0;
183 static int mesaHandleXError( XMesaDisplay
*dpy
, XErrorEvent
*event
)
194 * Check if the X Shared Memory extension is available.
195 * Return: 0 = not available
196 * 1 = shared XImage support available
197 * 2 = shared Pixmap support available also
199 static int check_for_xshm( XMesaDisplay
*display
)
201 #if defined(XFree86Server)
203 #elif defined(USE_XSHM)
204 int major
, minor
, ignore
;
207 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
208 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
209 return (pixmaps
==True
) ? 2 : 1;
219 /* No XSHM support */
226 * Apply gamma correction to an intensity value in [0..max]. Return the
227 * new intensity value.
229 static GLint
gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
235 double x
= (double) value
/ (double) max
;
236 return IROUND_POS((GLfloat
) max
* _mesa_pow(x
, 1.0F
/gamma
));
243 * Return the true number of bits per pixel for XImages.
244 * For example, if we request a 24-bit deep visual we may actually need/get
245 * 32bpp XImages. This function returns the appropriate bpp.
246 * Input: dpy - the X display
247 * visinfo - desribes the visual to be used for XImages
248 * Return: true number of bits per pixel for XImages
252 static int bits_per_pixel( XMesaVisual xmv
)
254 const int depth
= xmv
->nplanes
;
256 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
257 if (screenInfo
.formats
[i
].depth
== depth
)
258 return screenInfo
.formats
[i
].bitsPerPixel
;
260 return depth
; /* should never get here, but this should be safe */
265 static int bits_per_pixel( XMesaVisual xmv
)
267 XMesaDisplay
*dpy
= xmv
->display
;
268 XMesaVisualInfo visinfo
= xmv
->visinfo
;
271 /* Create a temporary XImage */
272 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
273 ZPixmap
, 0, /*format, offset*/
274 (char*) MALLOC(8), /*data*/
275 1, 1, /*width, height*/
280 /* grab the bits/pixel value */
281 bitsPerPixel
= img
->bits_per_pixel
;
282 /* free the XImage */
283 _mesa_free( img
->data
);
285 XMesaDestroyImage( img
);
293 * Determine if a given X window ID is valid (window exists).
294 * Do this by calling XGetWindowAttributes() for the window and
295 * checking if we catch an X error.
296 * Input: dpy - the display
297 * win - the window to check for existance
298 * Return: GL_TRUE - window exists
299 * GL_FALSE - window doesn't exist
301 #ifndef XFree86Server
302 static GLboolean WindowExistsFlag
;
304 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
307 if (xerr
->error_code
== BadWindow
) {
308 WindowExistsFlag
= GL_FALSE
;
313 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
315 XWindowAttributes wa
;
316 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
317 WindowExistsFlag
= GL_TRUE
;
318 old_handler
= XSetErrorHandler(window_exists_err_handler
);
319 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
320 XSetErrorHandler(old_handler
);
321 return WindowExistsFlag
;
327 /**********************************************************************/
328 /***** Linked list of XMesaBuffers *****/
329 /**********************************************************************/
331 static XMesaBuffer XMesaBufferList
= NULL
;
335 * Allocate a new XMesaBuffer, initialize basic fields and add to
336 * the list of all buffers.
339 alloc_xmesa_buffer(XMesaVisual vis
, BufferType type
, XMesaColormap cmap
)
341 XMesaBuffer b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
345 b
->display
= vis
->display
;
350 _mesa_initialize_framebuffer(&b
->mesa_buffer
, &vis
->mesa_visual
);
352 /* Allocate the framebuffer's renderbuffers */
353 assert(!b
->mesa_buffer
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
354 assert(!b
->mesa_buffer
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
356 /* front renderbuffer */
357 b
->frontxrb
= xmesa_new_renderbuffer(NULL
, 0, vis
->mesa_visual
.rgbMode
,
359 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_FRONT_LEFT
,
362 /* back renderbuffer */
363 if (vis
->mesa_visual
.doubleBufferMode
) {
364 b
->backxrb
= xmesa_new_renderbuffer(NULL
, 0,
365 vis
->mesa_visual
.rgbMode
,
367 /* determine back buffer implementation */
368 b
->db_mode
= vis
->ximage_flag
? BACK_XIMAGE
: BACK_PIXMAP
;
370 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_BACK_LEFT
,
374 /* determine if we need software alpha planes */
375 if (vis
->mesa_visual
.alphaBits
> 0
376 && vis
->undithered_pf
!= PF_8A8B8G8R
377 && vis
->undithered_pf
!= PF_8A8R8G8B
) {
378 /* Visual has alpha, but pixel format doesn't support it.
379 * We'll use an alpha renderbuffer wrapper.
387 _mesa_add_soft_renderbuffers(&b
->mesa_buffer
,
388 GL_FALSE
, /* color */
389 vis
->mesa_visual
.haveDepthBuffer
,
390 vis
->mesa_visual
.haveStencilBuffer
,
391 vis
->mesa_visual
.haveAccumBuffer
,
393 vis
->mesa_visual
.numAuxBuffers
> 0 );
395 /* insert into linked list */
396 b
->Next
= XMesaBufferList
;
404 * Find an XMesaBuffer by matching X display and colormap but NOT matching
405 * the notThis buffer.
407 static XMesaBuffer
find_xmesa_buffer(XMesaDisplay
*dpy
,
412 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
413 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
422 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap
425 static void free_xmesa_buffer(int client
, XMesaBuffer buffer
)
427 XMesaBuffer prev
= NULL
, b
;
429 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
431 /* unlink bufer from list */
433 prev
->Next
= buffer
->Next
;
435 XMesaBufferList
= buffer
->Next
;
436 /* Check to free X colors */
437 if (buffer
->num_alloced
>0) {
438 /* If no other buffer uses this X colormap then free the colors. */
439 if (!find_xmesa_buffer(buffer
->display
, buffer
->cmap
, buffer
)) {
441 (void)FreeColors(buffer
->cmap
, client
,
442 buffer
->num_alloced
, buffer
->alloced_colors
,
445 XFreeColors(buffer
->display
, buffer
->cmap
,
446 buffer
->alloced_colors
, buffer
->num_alloced
, 0);
451 _mesa_free_framebuffer_data(&buffer
->mesa_buffer
);
456 /* continue search */
459 /* buffer not found in XMesaBufferList */
460 _mesa_problem(NULL
,"free_xmesa_buffer() - buffer not found\n");
464 /* Copy X color table stuff from one XMesaBuffer to another. */
465 static void copy_colortable_info(XMesaBuffer dst
, const XMesaBuffer src
)
467 MEMCPY(dst
->color_table
, src
->color_table
, sizeof(src
->color_table
));
468 MEMCPY(dst
->pixel_to_r
, src
->pixel_to_r
, sizeof(src
->pixel_to_r
));
469 MEMCPY(dst
->pixel_to_g
, src
->pixel_to_g
, sizeof(src
->pixel_to_g
));
470 MEMCPY(dst
->pixel_to_b
, src
->pixel_to_b
, sizeof(src
->pixel_to_b
));
471 dst
->num_alloced
= src
->num_alloced
;
472 MEMCPY(dst
->alloced_colors
, src
->alloced_colors
,
473 sizeof(src
->alloced_colors
));
478 /**********************************************************************/
479 /***** Misc Private Functions *****/
480 /**********************************************************************/
484 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
485 * Return: GL_TRUE if success, GL_FALSE if error
487 #ifndef XFree86Server
489 alloc_shm_back_buffer(XMesaBuffer b
, GLuint width
, GLuint height
)
493 * We have to do a _lot_ of error checking here to be sure we can
494 * really use the XSHM extension. It seems different servers trigger
495 * errors at different points if the extension won't work. Therefore
496 * we have to be very careful...
499 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
501 if (width
== 0 || height
== 0) {
502 /* this will be true the first time we're called on 'b' */
506 b
->backxrb
->ximage
= XShmCreateImage(b
->xm_visual
->display
,
507 b
->xm_visual
->visinfo
->visual
,
508 b
->xm_visual
->visinfo
->depth
,
509 ZPixmap
, NULL
, &b
->shminfo
,
511 if (b
->backxrb
->ximage
== NULL
) {
512 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
517 b
->shminfo
.shmid
= shmget( IPC_PRIVATE
, b
->backxrb
->ximage
->bytes_per_line
518 * b
->backxrb
->ximage
->height
, IPC_CREAT
|0777 );
519 if (b
->shminfo
.shmid
< 0) {
520 _mesa_warning(NULL
, "shmget failed while allocating back buffer");
521 XDestroyImage( b
->backxrb
->ximage
);
522 b
->backxrb
->ximage
= NULL
;
523 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmget), disabling.");
528 b
->shminfo
.shmaddr
= b
->backxrb
->ximage
->data
529 = (char*)shmat( b
->shminfo
.shmid
, 0, 0 );
530 if (b
->shminfo
.shmaddr
== (char *) -1) {
531 _mesa_warning(NULL
, "shmat() failed while allocating back buffer");
532 XDestroyImage( b
->backxrb
->ximage
);
533 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
534 b
->backxrb
->ximage
= NULL
;
535 _mesa_warning(NULL
, "alloc_back_buffer: Shared memory error (shmat), disabling.");
540 b
->shminfo
.readOnly
= False
;
542 old_handler
= XSetErrorHandler( mesaHandleXError
);
543 /* This may trigger the X protocol error we're ready to catch: */
544 XShmAttach( b
->xm_visual
->display
, &b
->shminfo
);
545 XSync( b
->xm_visual
->display
, False
);
547 if (mesaXErrorFlag
) {
548 /* we are on a remote display, this error is normal, don't print it */
549 XFlush( b
->xm_visual
->display
);
551 XDestroyImage( b
->backxrb
->ximage
);
552 shmdt( b
->shminfo
.shmaddr
);
553 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
554 b
->backxrb
->ximage
= NULL
;
556 (void) XSetErrorHandler( old_handler
);
560 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 ); /* nobody else needs it */
562 /* Finally, try an XShmPutImage to be really sure the extension works */
563 gc
= XCreateGC( b
->xm_visual
->display
, b
->frontxrb
->drawable
, 0, NULL
);
564 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
, gc
,
565 b
->backxrb
->ximage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
566 XSync( b
->xm_visual
->display
, False
);
567 XFreeGC( b
->xm_visual
->display
, gc
);
568 (void) XSetErrorHandler( old_handler
);
569 if (mesaXErrorFlag
) {
570 XFlush( b
->xm_visual
->display
);
572 XDestroyImage( b
->backxrb
->ximage
);
573 shmdt( b
->shminfo
.shmaddr
);
574 shmctl( b
->shminfo
.shmid
, IPC_RMID
, 0 );
575 b
->backxrb
->ximage
= NULL
;
582 /* Can't compile XSHM support */
592 * Setup an off-screen pixmap or Ximage to use as the back buffer.
593 * Input: b - the X/Mesa buffer
596 xmesa_alloc_back_buffer( XMesaBuffer b
, GLuint width
, GLuint height
)
598 if (width
== 0 || height
== 0)
601 if (b
->db_mode
== BACK_XIMAGE
) {
602 /* Deallocate the old backxrb->ximage, if any */
603 if (b
->backxrb
->ximage
) {
604 #if defined(USE_XSHM) && !defined(XFree86Server)
606 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
607 XDestroyImage( b
->backxrb
->ximage
);
608 shmdt( b
->shminfo
.shmaddr
);
612 XMesaDestroyImage( b
->backxrb
->ximage
);
613 b
->backxrb
->ximage
= NULL
;
616 /* Allocate new back buffer */
618 /* Allocate a regular XImage for the back buffer. */
619 b
->backxrb
->ximage
= XMesaCreateImage(b
->xm_visual
->BitsPerPixel
,
620 width
, height
, NULL
);
623 if (b
->shm
== 0 || !alloc_shm_back_buffer(b
, width
, height
)) {
624 /* Allocate a regular XImage for the back buffer. */
625 b
->backxrb
->ximage
= XCreateImage( b
->xm_visual
->display
,
626 b
->xm_visual
->visinfo
->visual
,
627 GET_VISUAL_DEPTH(b
->xm_visual
),
628 ZPixmap
, 0, /* format, offset */
631 8, 0 ); /* pad, bytes_per_line */
633 if (!b
->backxrb
->ximage
) {
634 _mesa_warning(NULL
, "alloc_back_buffer: XCreateImage failed.");
637 b
->backxrb
->ximage
->data
= (char *) MALLOC( b
->backxrb
->ximage
->height
638 * b
->backxrb
->ximage
->bytes_per_line
);
639 if (!b
->backxrb
->ximage
->data
) {
640 _mesa_warning(NULL
, "alloc_back_buffer: MALLOC failed.");
641 XMesaDestroyImage( b
->backxrb
->ximage
);
642 b
->backxrb
->ximage
= NULL
;
644 /* this call just updates the width/origin fields in the xrb */
645 b
->backxrb
->Base
.AllocStorage(NULL
, &b
->backxrb
->Base
,
646 b
->backxrb
->Base
.InternalFormat
,
647 b
->backxrb
->ximage
->width
,
648 b
->backxrb
->ximage
->height
);
650 b
->backxrb
->pixmap
= None
;
652 else if (b
->db_mode
== BACK_PIXMAP
) {
658 /* Free the old back pixmap */
659 if (b
->backxrb
->pixmap
) {
660 XMesaFreePixmap( b
->xm_visual
->display
, b
->backxrb
->pixmap
);
662 /* Allocate new back pixmap */
663 b
->backxrb
->pixmap
= XMesaCreatePixmap( b
->xm_visual
->display
,
664 b
->frontxrb
->drawable
,
666 GET_VISUAL_DEPTH(b
->xm_visual
) );
667 b
->backxrb
->ximage
= NULL
;
674 * A replacement for XAllocColor. This function should never
675 * fail to allocate a color. When XAllocColor fails, we return
676 * the nearest matching color. If we have to allocate many colors
677 * this function isn't too efficient; the XQueryColors() could be
679 * Written by Michael Pichler, Brian Paul, Mark Kilgard
680 * Input: dpy - X display
682 * cmapSize - size of colormap
683 * In/Out: color - the XColor struct
684 * Output: exact - 1=exact color match, 0=closest match
685 * alloced - 1=XAlloc worked, 0=XAlloc failed
688 noFaultXAllocColor( int client
,
693 int *exact
, int *alloced
)
699 /* we'll try to cache ctable for better remote display performance */
700 static Display
*prevDisplay
= NULL
;
701 static XMesaColormap prevCmap
= 0;
702 static int prevCmapSize
= 0;
703 static XMesaColor
*ctable
= NULL
;
707 double mindist
; /* 3*2^16^2 exceeds long int precision. */
711 /* First try just using XAllocColor. */
714 &color
->red
, &color
->green
, &color
->blue
,
716 client
) == Success
) {
718 if (XAllocColor(dpy
, cmap
, color
)) {
725 /* Alloc failed, search for closest match */
727 /* Retrieve color table entries. */
728 /* XXX alloca candidate. */
730 ppixIn
= (Pixel
*) MALLOC(cmapSize
* sizeof(Pixel
));
731 ctable
= (xrgb
*) MALLOC(cmapSize
* sizeof(xrgb
));
732 for (i
= 0; i
< cmapSize
; i
++) {
735 QueryColors(cmap
, cmapSize
, ppixIn
, ctable
);
737 if (prevDisplay
!= dpy
|| prevCmap
!= cmap
738 || prevCmapSize
!= cmapSize
|| !ctable
) {
739 /* free previously cached color table */
742 /* Get the color table from X */
743 ctable
= (XMesaColor
*) MALLOC(cmapSize
* sizeof(XMesaColor
));
745 for (i
= 0; i
< cmapSize
; i
++) {
748 XQueryColors(dpy
, cmap
, ctable
, cmapSize
);
751 prevCmapSize
= cmapSize
;
755 /* Find best match. */
758 for (i
= 0; i
< cmapSize
; i
++) {
759 double dr
= 0.30 * ((double) color
->red
- (double) ctable
[i
].red
);
760 double dg
= 0.59 * ((double) color
->green
- (double) ctable
[i
].green
);
761 double db
= 0.11 * ((double) color
->blue
- (double) ctable
[i
].blue
);
762 double dist
= dr
* dr
+ dg
* dg
+ db
* db
;
763 if (bestmatch
< 0 || dist
< mindist
) {
770 subColor
.red
= ctable
[bestmatch
].red
;
771 subColor
.green
= ctable
[bestmatch
].green
;
772 subColor
.blue
= ctable
[bestmatch
].blue
;
773 /* Try to allocate the closest match color. This should only
774 * fail if the cell is read/write. Otherwise, we're incrementing
775 * the cell's reference count.
779 &subColor
.red
, &subColor
.green
, &subColor
.blue
,
781 client
) == Success
) {
783 if (XAllocColor(dpy
, cmap
, &subColor
)) {
788 /* do this to work around a problem reported by Frank Ortega */
789 subColor
.pixel
= (unsigned long) bestmatch
;
790 subColor
.red
= ctable
[bestmatch
].red
;
791 subColor
.green
= ctable
[bestmatch
].green
;
792 subColor
.blue
= ctable
[bestmatch
].blue
;
793 subColor
.flags
= DoRed
| DoGreen
| DoBlue
;
800 /* don't free table, save it for next time */
811 * Do setup for PF_GRAYSCALE pixel format.
812 * Note that buffer may be NULL.
814 static GLboolean
setup_grayscale( int client
, XMesaVisual v
,
815 XMesaBuffer buffer
, XMesaColormap cmap
)
817 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
822 XMesaBuffer prevBuffer
;
828 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
830 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
831 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
832 /* Copy colormap stuff from previous XMesaBuffer which uses same
833 * X colormap. Do this to avoid time spent in noFaultXAllocColor.
835 copy_colortable_info(buffer
, prevBuffer
);
838 /* Allocate 256 shades of gray */
840 int colorsfailed
= 0;
841 for (gray
=0;gray
<256;gray
++) {
842 GLint r
= gamma_adjust( v
->RedGamma
, gray
, 255 );
843 GLint g
= gamma_adjust( v
->GreenGamma
, gray
, 255 );
844 GLint b
= gamma_adjust( v
->BlueGamma
, gray
, 255 );
847 xcol
.red
= (r
<< 8) | r
;
848 xcol
.green
= (g
<< 8) | g
;
849 xcol
.blue
= (b
<< 8) | b
;
850 noFaultXAllocColor( client
, v
->display
,
851 cmap
, GET_COLORMAP_SIZE(v
),
852 &xcol
, &exact
, &alloced
);
857 assert(buffer
->num_alloced
<256);
858 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
859 buffer
->num_alloced
++;
864 buffer->color_table[gray*3+0] = xcol.pixel;
865 buffer->color_table[gray*3+1] = xcol.pixel;
866 buffer->color_table[gray*3+2] = xcol.pixel;
867 assert(xcol.pixel < 65536);
868 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
869 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
870 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
872 buffer
->color_table
[gray
] = xcol
.pixel
;
873 assert(xcol
.pixel
< 65536);
874 buffer
->pixel_to_r
[xcol
.pixel
] = gray
;
875 buffer
->pixel_to_g
[xcol
.pixel
] = gray
;
876 buffer
->pixel_to_b
[xcol
.pixel
] = gray
;
879 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
881 "Note: %d out of 256 needed colors do not match exactly.\n",
887 v
->dithered_pf
= PF_Grayscale
;
888 v
->undithered_pf
= PF_Grayscale
;
895 * Setup RGB rendering for a window with a PseudoColor, StaticColor,
896 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
897 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
898 * color. While this function was originally designed just for 8-bit
899 * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
900 * Dithering code contributed by Bob Mercier.
902 static GLboolean
setup_dithered_color( int client
, XMesaVisual v
,
903 XMesaBuffer buffer
, XMesaColormap cmap
)
905 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
910 XMesaBuffer prevBuffer
;
916 prevBuffer
= find_xmesa_buffer(v
->display
, cmap
, buffer
);
918 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
919 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
920 /* Copy colormap stuff from previous, matching XMesaBuffer.
921 * Do this to avoid time spent in noFaultXAllocColor.
923 copy_colortable_info(buffer
, prevBuffer
);
926 /* Allocate X colors and initialize color_table[], red_table[], etc */
928 int colorsfailed
= 0;
929 for (r
= 0; r
< DITH_R
; r
++) {
930 for (g
= 0; g
< DITH_G
; g
++) {
931 for (b
= 0; b
< DITH_B
; b
++) {
934 xcol
.red
=gamma_adjust(v
->RedGamma
, r
*65535/(DITH_R
-1),65535);
935 xcol
.green
=gamma_adjust(v
->GreenGamma
, g
*65535/(DITH_G
-1),65535);
936 xcol
.blue
=gamma_adjust(v
->BlueGamma
, b
*65535/(DITH_B
-1),65535);
937 noFaultXAllocColor( client
, v
->display
,
938 cmap
, GET_COLORMAP_SIZE(v
),
939 &xcol
, &exact
, &alloced
);
944 assert(buffer
->num_alloced
<256);
945 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
946 buffer
->num_alloced
++;
948 i
= DITH_MIX( r
, g
, b
);
950 buffer
->color_table
[i
] = xcol
.pixel
;
951 assert(xcol
.pixel
< 65536);
952 buffer
->pixel_to_r
[xcol
.pixel
] = r
* 255 / (DITH_R
-1);
953 buffer
->pixel_to_g
[xcol
.pixel
] = g
* 255 / (DITH_G
-1);
954 buffer
->pixel_to_b
[xcol
.pixel
] = b
* 255 / (DITH_B
-1);
959 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
961 "Note: %d out of %d needed colors do not match exactly.\n",
962 colorsfailed
, DITH_R
* DITH_G
* DITH_B
);
967 v
->dithered_pf
= PF_Dither
;
968 v
->undithered_pf
= PF_Lookup
;
974 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
975 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
976 * Special dithering tables have to be initialized.
978 static void setup_8bit_hpcr( XMesaVisual v
)
980 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
981 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
982 * on the root window AND the colormap obtainable by XGetRGBColormaps
983 * for that atom must be set on the window. (see also tkInitWindow)
984 * If that colormap is not set, the output will look stripy.
987 /* Setup color tables with gamma correction */
991 g
= 1.0 / v
->RedGamma
;
992 for (i
=0; i
<256; i
++) {
993 GLint red
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[0][i
]/255.0, g
));
994 v
->hpcr_rgbTbl
[0][i
] = CLAMP( red
, 16, 239 );
997 g
= 1.0 / v
->GreenGamma
;
998 for (i
=0; i
<256; i
++) {
999 GLint green
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[1][i
]/255.0, g
));
1000 v
->hpcr_rgbTbl
[1][i
] = CLAMP( green
, 16, 239 );
1003 g
= 1.0 / v
->BlueGamma
;
1004 for (i
=0; i
<256; i
++) {
1005 GLint blue
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[2][i
]/255.0, g
));
1006 v
->hpcr_rgbTbl
[2][i
] = CLAMP( blue
, 32, 223 );
1008 v
->undithered_pf
= PF_HPCR
; /* can't really disable dithering for now */
1009 v
->dithered_pf
= PF_HPCR
;
1011 /* which method should I use to clear */
1012 /* GL_FALSE: keep the ordinary method */
1013 /* GL_TRUE : clear with dither pattern */
1014 v
->hpcr_clear_flag
= _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE
: GL_FALSE
;
1016 if (v
->hpcr_clear_flag
) {
1017 v
->hpcr_clear_pixmap
= XMesaCreatePixmap(v
->display
,
1018 DefaultRootWindow(v
->display
),
1020 #ifndef XFree86Server
1021 v
->hpcr_clear_ximage
= XGetImage(v
->display
, v
->hpcr_clear_pixmap
,
1022 0, 0, 16, 2, AllPlanes
, ZPixmap
);
1029 * Setup RGB rendering for a window with a True/DirectColor visual.
1031 static void setup_truecolor( XMesaVisual v
, XMesaBuffer buffer
,
1032 XMesaColormap cmap
)
1034 unsigned long rmask
, gmask
, bmask
;
1038 /* Compute red multiplier (mask) and bit shift */
1040 rmask
= GET_REDMASK(v
);
1041 while ((rmask
& 1)==0) {
1046 /* Compute green multiplier (mask) and bit shift */
1048 gmask
= GET_GREENMASK(v
);
1049 while ((gmask
& 1)==0) {
1054 /* Compute blue multiplier (mask) and bit shift */
1056 bmask
= GET_BLUEMASK(v
);
1057 while ((bmask
& 1)==0) {
1063 * Compute component-to-pixel lookup tables and dithering kernel
1066 static GLubyte kernel
[16] = {
1067 0*16, 8*16, 2*16, 10*16,
1068 12*16, 4*16, 14*16, 6*16,
1069 3*16, 11*16, 1*16, 9*16,
1070 15*16, 7*16, 13*16, 5*16,
1072 GLint rBits
= _mesa_bitcount(rmask
);
1073 GLint gBits
= _mesa_bitcount(gmask
);
1074 GLint bBits
= _mesa_bitcount(bmask
);
1078 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
1079 for (i
=0; i
<=rmask
; i
++)
1080 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
1081 for (i
=0; i
<=gmask
; i
++)
1082 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
1083 for (i
=0; i
<=bmask
; i
++)
1084 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
1086 /* convert RGB values from [0,255] to pixel components */
1088 for (i
=0;i
<256;i
++) {
1089 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
1090 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
1091 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
1092 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
1093 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
1094 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
1096 /* overflow protection */
1097 for (i
=256;i
<512;i
++) {
1098 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
1099 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
1100 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
1103 /* setup dithering kernel */
1105 if (gBits
> maxBits
) maxBits
= gBits
;
1106 if (bBits
> maxBits
) maxBits
= bBits
;
1107 for (i
=0;i
<16;i
++) {
1108 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
1111 v
->undithered_pf
= PF_Truecolor
;
1112 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
1116 * Now check for TrueColor visuals which we can optimize.
1118 if ( GET_REDMASK(v
) ==0x0000ff
1119 && GET_GREENMASK(v
)==0x00ff00
1120 && GET_BLUEMASK(v
) ==0xff0000
1121 && CHECK_BYTE_ORDER(v
)
1122 && v
->BitsPerPixel
==32
1123 && sizeof(GLuint
)==4
1124 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1125 /* common 32 bpp config used on SGI, Sun */
1126 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
;
1128 else if (GET_REDMASK(v
) ==0xff0000
1129 && GET_GREENMASK(v
)==0x00ff00
1130 && GET_BLUEMASK(v
) ==0x0000ff
1131 && CHECK_BYTE_ORDER(v
)
1132 && v
->BitsPerPixel
==32
1133 && sizeof(GLuint
)==4
1134 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1135 /* common 32 bpp config used on Linux, HP, IBM */
1136 if (GET_VISUAL_DEPTH(v
)==32)
1137 v
->undithered_pf
= v
->dithered_pf
= PF_8A8R8G8B
;
1139 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
;
1141 else if (GET_REDMASK(v
) ==0xff0000
1142 && GET_GREENMASK(v
)==0x00ff00
1143 && GET_BLUEMASK(v
) ==0x0000ff
1144 && CHECK_BYTE_ORDER(v
)
1145 && v
->BitsPerPixel
==24
1146 && sizeof(GLuint
)==4
1147 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1148 /* common packed 24 bpp config used on Linux */
1149 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
;
1151 else if (GET_REDMASK(v
) ==0xf800
1152 && GET_GREENMASK(v
)==0x07e0
1153 && GET_BLUEMASK(v
) ==0x001f
1154 && CHECK_BYTE_ORDER(v
)
1155 && v
->BitsPerPixel
==16
1156 && sizeof(GLushort
)==2
1157 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1158 /* 5-6-5 color weight on common PC VGA boards */
1159 v
->undithered_pf
= PF_5R6G5B
;
1160 v
->dithered_pf
= PF_Dither_5R6G5B
;
1162 else if (GET_REDMASK(v
) ==0xe0
1163 && GET_GREENMASK(v
)==0x1c
1164 && GET_BLUEMASK(v
) ==0x03
1165 && CHECK_FOR_HPCR(v
)) {
1166 setup_8bit_hpcr( v
);
1173 * Setup RGB rendering for a window with a monochrome visual.
1175 static void setup_monochrome( XMesaVisual v
, XMesaBuffer b
)
1178 v
->dithered_pf
= v
->undithered_pf
= PF_1Bit
;
1179 /* if black=1 then we must flip pixel values */
1180 v
->bitFlip
= (GET_BLACK_PIXEL(v
) != 0);
1186 * When a context is "made current" for the first time, we can finally
1187 * finish initializing the context's visual and buffer information.
1188 * Input: v - the XMesaVisual to initialize
1189 * b - the XMesaBuffer to initialize (may be NULL)
1190 * rgb_flag - TRUE = RGBA mode, FALSE = color index mode
1191 * window - the window/pixmap we're rendering into
1192 * cmap - the colormap associated with the window/pixmap
1193 * Return: GL_TRUE=success, GL_FALSE=failure
1195 static GLboolean
initialize_visual_and_buffer( int client
,
1199 XMesaDrawable window
,
1200 XMesaColormap cmap
)
1202 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
1203 #ifndef XFree86Server
1208 assert(b
->xm_visual
== v
);
1212 front_xrb
= b
->frontxrb
;
1213 back_xrb
= b
->backxrb
;
1216 front_xrb
= back_xrb
= NULL
;
1219 /* Save true bits/pixel */
1220 v
->BitsPerPixel
= bits_per_pixel(v
);
1221 assert(v
->BitsPerPixel
> 0);
1224 if (rgb_flag
==GL_FALSE
) {
1225 /* COLOR-INDEXED WINDOW:
1226 * Even if the visual is TrueColor or DirectColor we treat it as
1227 * being color indexed. This is weird but might be useful to someone.
1229 v
->dithered_pf
= v
->undithered_pf
= PF_Index
;
1230 v
->mesa_visual
.indexBits
= GET_VISUAL_DEPTH(v
);
1234 * We support RGB rendering into almost any kind of visual.
1236 const int xclass
= v
->mesa_visual
.visualType
;
1237 if (xclass
==GLX_TRUE_COLOR
|| xclass
==GLX_DIRECT_COLOR
) {
1238 setup_truecolor( v
, b
, cmap
);
1240 else if (xclass
==GLX_STATIC_GRAY
&& GET_VISUAL_DEPTH(v
)==1) {
1241 setup_monochrome( v
, b
);
1243 else if (xclass
==GLX_GRAY_SCALE
|| xclass
==GLX_STATIC_GRAY
) {
1244 if (!setup_grayscale( client
, v
, b
, cmap
)) {
1248 else if ((xclass
==GLX_PSEUDO_COLOR
|| xclass
==GLX_STATIC_COLOR
)
1249 && GET_VISUAL_DEPTH(v
)>=4 && GET_VISUAL_DEPTH(v
)<=16) {
1250 if (!setup_dithered_color( client
, v
, b
, cmap
)) {
1255 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.");
1258 v
->mesa_visual
.indexBits
= 0;
1260 if (_mesa_getenv("MESA_NO_DITHER")) {
1261 v
->dithered_pf
= v
->undithered_pf
;
1267 * If MESA_INFO env var is set print out some debugging info
1268 * which can help Brian figure out what's going on when a user
1271 if (_mesa_getenv("MESA_INFO")) {
1272 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
1273 _mesa_printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
1274 _mesa_printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
1275 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
1276 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
1277 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
1281 /* Do window-specific initializations */
1283 b
->frontxrb
->drawable
= window
;
1284 b
->frontxrb
->pixmap
= (XMesaPixmap
) window
;
1286 /* Setup for single/double buffering */
1287 if (v
->mesa_visual
.doubleBufferMode
) {
1288 /* Double buffered */
1289 b
->shm
= check_for_xshm( v
->display
);
1290 xmesa_alloc_back_buffer(b
, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
1293 /* X11 graphics contexts */
1294 #ifdef XFree86Server
1295 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
1297 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
1299 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
1301 /* cleargc - for glClear() */
1302 #ifdef XFree86Server
1303 b
->cleargc
= CreateScratchGC(v
->display
, window
->depth
);
1305 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
1307 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
1310 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1311 * Patch contributed by Michael Pichler May 15, 1995.
1313 #ifdef XFree86Server
1314 b
->swapgc
= CreateScratchGC(v
->display
, window
->depth
);
1318 dixChangeGC(NullClient
, b
->swapgc
, GCGraphicsExposures
, v
, NULL
);
1321 gcvalues
.graphics_exposures
= False
;
1322 b
->swapgc
= XCreateGC( v
->display
, window
,
1323 GCGraphicsExposures
, &gcvalues
);
1325 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
1327 * Set fill style and tile pixmap once for all for HPCR stuff
1328 * (instead of doing it each time in clear_color_HPCR_pixmap())
1329 * Initialize whole stuff
1330 * Patch contributed by Jacques Leroy March 8, 1998.
1332 if (v
->hpcr_clear_flag
&& back_xrb
->pixmap
) {
1334 for (i
=0; i
<16; i
++)
1336 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 0, 0);
1337 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 1, 0);
1339 XMesaPutImage(b
->display
, (XMesaDrawable
) v
->hpcr_clear_pixmap
,
1340 b
->cleargc
, v
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1341 XMesaSetFillStyle( v
->display
, b
->cleargc
, FillTiled
);
1342 XMesaSetTile( v
->display
, b
->cleargc
, v
->hpcr_clear_pixmap
);
1345 /* Initialize the row buffer XImage for use in write_color_span() */
1346 #ifdef XFree86Server
1347 b
->rowimage
= XMesaCreateImage(GET_VISUAL_DEPTH(v
), MAX_WIDTH
, 1,
1348 (char *)MALLOC(MAX_WIDTH
*4));
1350 b
->rowimage
= XCreateImage( v
->display
,
1353 ZPixmap
, 0, /*format, offset*/
1354 (char*) MALLOC(MAX_WIDTH
*4), /*data*/
1355 MAX_WIDTH
, 1, /*width, height*/
1357 0 /*bytes_per_line*/ );
1369 * Convert an RGBA color to a pixel value.
1372 xmesa_color_to_pixel(GLcontext
*ctx
,
1373 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
1376 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1377 switch (pixelFormat
) {
1383 PACK_TRUECOLOR( p
, r
, g
, b
);
1387 return PACK_8A8B8G8R( r
, g
, b
, a
);
1389 return PACK_8A8R8G8B( r
, g
, b
, a
);
1393 return PACK_8R8G8B( r
, g
, b
);
1395 return PACK_5R6G5B( r
, g
, b
);
1399 return DITHER( 1, 0, r
, g
, b
);
1402 /* 382 = (3*255)/2 */
1403 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
1405 return DITHER_HPCR(1, 1, r
, g
, b
);
1409 return LOOKUP( r
, g
, b
);
1412 return GRAY_RGB( r
, g
, b
);
1413 case PF_Dither_True
:
1415 case PF_Dither_5R6G5B
:
1418 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
1422 _mesa_problem(ctx
, "Bad pixel format in xmesa_color_to_pixel");
1428 #define NUM_VISUAL_TYPES 6
1431 * Convert an X visual type to a GLX visual type.
1433 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
1435 * \return If \c visualType is a valid X visual type, a GLX visual type will
1436 * be returned. Otherwise \c GLX_NONE will be returned.
1439 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
1443 xmesa_convert_from_x_visual_type( int visualType
)
1445 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
1446 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
1447 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
1448 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
1451 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
1452 ? glx_visual_types
[ visualType
] : GLX_NONE
;
1456 /**********************************************************************/
1457 /***** Public Functions *****/
1458 /**********************************************************************/
1462 * Create a new X/Mesa visual.
1463 * Input: display - X11 display
1464 * visinfo - an XVisualInfo pointer
1465 * rgb_flag - GL_TRUE = RGB mode,
1466 * GL_FALSE = color index mode
1467 * alpha_flag - alpha buffer requested?
1468 * db_flag - GL_TRUE = double-buffered,
1469 * GL_FALSE = single buffered
1470 * stereo_flag - stereo visual?
1471 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1472 * GL_FALSE = use an off-screen pixmap for back buffer
1473 * depth_size - requested bits/depth values, or zero
1474 * stencil_size - requested bits/stencil values, or zero
1475 * accum_red_size - requested bits/red accum values, or zero
1476 * accum_green_size - requested bits/green accum values, or zero
1477 * accum_blue_size - requested bits/blue accum values, or zero
1478 * accum_alpha_size - requested bits/alpha accum values, or zero
1479 * num_samples - number of samples/pixel if multisampling, or zero
1480 * level - visual level, usually 0
1481 * visualCaveat - ala the GLX extension, usually GLX_NONE
1482 * Return; a new XMesaVisual or 0 if error.
1484 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
1485 XMesaVisualInfo visinfo
,
1487 GLboolean alpha_flag
,
1489 GLboolean stereo_flag
,
1490 GLboolean ximage_flag
,
1493 GLint accum_red_size
,
1494 GLint accum_green_size
,
1495 GLint accum_blue_size
,
1496 GLint accum_alpha_size
,
1499 GLint visualCaveat
)
1503 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
1505 /* For debugging only */
1506 if (_mesa_getenv("MESA_XSYNC")) {
1507 /* This makes debugging X easier.
1508 * In your debugger, set a breakpoint on _XError to stop when an
1509 * X protocol error is generated.
1511 #ifdef XFree86Server
1514 XSynchronize( display
, 1 );
1518 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
1524 * In the X server, NULL is passed in for the display. It will have
1525 * to be set before using this visual. See XMesaSetVisualDisplay()
1528 v
->display
= display
;
1530 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
1531 * the struct but we may need some of the information contained in it
1534 #ifndef XFree86Server
1535 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
1540 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
1543 /* check for MESA_GAMMA environment variable */
1544 gamma
= _mesa_getenv("MESA_GAMMA");
1546 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
1547 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
1548 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
1549 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
1550 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
1553 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
1556 v
->ximage_flag
= ximage_flag
;
1558 #ifdef XFree86Server
1559 /* We could calculate these values by ourselves. nplanes is either the sum
1560 * of the red, green, and blue bits or the number index bits.
1561 * ColormapEntries is either (1U << index_bits) or
1562 * (1U << max(redBits, greenBits, blueBits)).
1564 v
->nplanes
= visinfo
->nplanes
;
1565 v
->ColormapEntries
= visinfo
->ColormapEntries
;
1567 v
->mesa_visual
.redMask
= visinfo
->redMask
;
1568 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
1569 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
1570 v
->mesa_visual
.visualID
= visinfo
->vid
;
1571 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
1573 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
1574 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
1575 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
1576 v
->mesa_visual
.visualID
= visinfo
->visualid
;
1577 v
->mesa_visual
.screen
= visinfo
->screen
;
1580 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
1581 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
1583 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
1586 v
->mesa_visual
.visualRating
= visualCaveat
;
1588 (void) initialize_visual_and_buffer( 0, v
, NULL
, rgb_flag
, 0, 0 );
1591 const int xclass
= v
->mesa_visual
.visualType
;
1592 if (xclass
==GLX_TRUE_COLOR
|| xclass
==GLX_DIRECT_COLOR
) {
1593 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
1594 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
1595 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
1599 /* this is an approximation */
1601 depth
= GET_VISUAL_DEPTH(v
);
1602 red_bits
= depth
/ 3;
1604 green_bits
= depth
/ 2;
1605 depth
-= green_bits
;
1608 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
1612 if (alpha_flag
&& alpha_bits
== 0)
1615 _mesa_initialize_visual( &v
->mesa_visual
,
1616 rgb_flag
, db_flag
, stereo_flag
,
1617 red_bits
, green_bits
,
1618 blue_bits
, alpha_bits
,
1619 v
->mesa_visual
.indexBits
,
1622 accum_red_size
, accum_green_size
,
1623 accum_blue_size
, accum_alpha_size
,
1626 /* XXX minor hack */
1627 v
->mesa_visual
.level
= level
;
1632 void XMesaSetVisualDisplay( XMesaDisplay
*dpy
, XMesaVisual v
)
1638 void XMesaDestroyVisual( XMesaVisual v
)
1640 #ifndef XFree86Server
1641 _mesa_free(v
->visinfo
);
1649 * Create a new XMesaContext.
1650 * \param v the XMesaVisual
1651 * \param share_list another XMesaContext with which to share display
1652 * lists or NULL if no sharing is wanted.
1653 * \return an XMesaContext or NULL if error.
1655 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
1657 static GLboolean firstTime
= GL_TRUE
;
1660 struct dd_function_table functions
;
1664 _glthread_INIT_MUTEX(_xmesa_lock
);
1665 firstTime
= GL_FALSE
;
1668 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
1669 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
1673 mesaCtx
= &(c
->mesa
);
1675 /* initialize with default driver functions, then plug in XMesa funcs */
1676 _mesa_init_driver_functions(&functions
);
1677 xmesa_init_driver_functions(v
, &functions
);
1678 if (!_mesa_initialize_context(mesaCtx
, &v
->mesa_visual
,
1679 share_list
? &(share_list
->mesa
) : (GLcontext
*) NULL
,
1680 &functions
, (void *) c
)) {
1685 _mesa_enable_sw_extensions(mesaCtx
);
1686 _mesa_enable_1_3_extensions(mesaCtx
);
1687 _mesa_enable_1_4_extensions(mesaCtx
);
1688 _mesa_enable_1_5_extensions(mesaCtx
);
1689 _mesa_enable_2_0_extensions(mesaCtx
);
1690 #if ENABLE_EXT_texure_compression_s3tc
1692 _mesa_enable_extension(mesaCtx
, "GL_EXT_texture_compression_s3tc");
1693 _mesa_enable_extension(mesaCtx
, "GL_S3_s3tc");
1695 _mesa_enable_extension(mesaCtx
, "GL_3DFX_texture_compression_FXT1");
1697 #if ENABLE_EXT_timer_query
1698 _mesa_enable_extension(mesaCtx
, "GL_EXT_timer_query");
1701 /* finish up xmesa context initializations */
1702 c
->swapbytes
= CHECK_BYTE_ORDER(v
) ? GL_FALSE
: GL_TRUE
;
1704 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1705 c
->display
= v
->display
;
1706 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
1708 /* Initialize the software rasterizer and helper modules.
1710 if (!_swrast_CreateContext( mesaCtx
) ||
1711 !_ac_CreateContext( mesaCtx
) ||
1712 !_tnl_CreateContext( mesaCtx
) ||
1713 !_swsetup_CreateContext( mesaCtx
)) {
1714 _mesa_free_context_data(&c
->mesa
);
1720 tnl
= TNL_CONTEXT(mesaCtx
);
1721 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1723 xmesa_register_swrast_functions( mesaCtx
);
1724 _swsetup_Wakeup(mesaCtx
);
1731 void XMesaDestroyContext( XMesaContext c
)
1733 GLcontext
*mesaCtx
= &c
->mesa
;
1735 XMesaBuffer xmbuf
= XMESA_BUFFER(mesaCtx
->DrawBuffer
);
1737 if (xmbuf
&& xmbuf
->FXctx
)
1738 fxMesaDestroyContext(xmbuf
->FXctx
);
1740 _swsetup_DestroyContext( mesaCtx
);
1741 _swrast_DestroyContext( mesaCtx
);
1742 _tnl_DestroyContext( mesaCtx
);
1743 _ac_DestroyContext( mesaCtx
);
1744 _mesa_free_context_data( mesaCtx
);
1751 * XXX this isn't a public function! It's a hack for the 3Dfx driver.
1752 * Create a new XMesaBuffer from an X window.
1753 * Input: v - the XMesaVisual
1756 * Return: new XMesaBuffer or NULL if error
1759 XMesaCreateWindowBuffer2(XMesaVisual v
, XMesaWindow w
, XMesaContext c
)
1761 #ifndef XFree86Server
1762 XWindowAttributes attr
;
1774 /* Check that window depth matches visual depth */
1775 #ifdef XFree86Server
1776 client
= CLIENT_ID(((XMesaDrawable
)w
)->id
);
1778 if (GET_VISUAL_DEPTH(v
) != ((XMesaDrawable
)w
)->depth
) {
1779 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1780 GET_VISUAL_DEPTH(v
), ((XMesaDrawable
) w
)->depth
);
1784 XGetWindowAttributes( v
->display
, w
, &attr
);
1786 if (GET_VISUAL_DEPTH(v
) != attr
.depth
) {
1787 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1788 GET_VISUAL_DEPTH(v
), attr
.depth
);
1794 #ifdef XFree86Server
1795 cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
1797 if (attr
.colormap
) {
1798 cmap
= attr
.colormap
;
1801 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1802 /* this is weird, a window w/out a colormap!? */
1803 /* OK, let's just allocate a new one and hope for the best */
1804 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1808 b
= alloc_xmesa_buffer(v
, WINDOW
, cmap
);
1813 if (!initialize_visual_and_buffer( client
, v
, b
, v
->mesa_visual
.rgbMode
,
1814 (XMesaDrawable
) w
, cmap
)) {
1815 free_xmesa_buffer(client
, b
);
1820 fxEnvVar
= _mesa_getenv("MESA_GLX_FX");
1822 if (fxEnvVar
[0]!='d') {
1826 if (v
->mesa_visual
.depthBits
> 0) {
1827 attribs
[numAttribs
++] = FXMESA_DEPTH_SIZE
;
1828 attribs
[numAttribs
++] = v
->mesa_visual
.depthBits
;
1830 if (v
->mesa_visual
.doubleBufferMode
) {
1831 attribs
[numAttribs
++] = FXMESA_DOUBLEBUFFER
;
1833 if (v
->mesa_visual
.accumRedBits
> 0) {
1834 attribs
[numAttribs
++] = FXMESA_ACCUM_SIZE
;
1835 attribs
[numAttribs
++] = v
->mesa_visual
.accumRedBits
;
1837 if (v
->mesa_visual
.stencilBits
> 0) {
1838 attribs
[numAttribs
++] = FXMESA_STENCIL_SIZE
;
1839 attribs
[numAttribs
++] = v
->mesa_visual
.stencilBits
;
1841 if (v
->mesa_visual
.alphaBits
> 0) {
1842 attribs
[numAttribs
++] = FXMESA_ALPHA_SIZE
;
1843 attribs
[numAttribs
++] = v
->mesa_visual
.alphaBits
;
1846 attribs
[numAttribs
++] = FXMESA_SHARE_CONTEXT
;
1847 attribs
[numAttribs
++] = (int) &(c
->mesa
);
1849 attribs
[numAttribs
++] = FXMESA_NONE
;
1851 /* [dBorca] we should take an envvar for `fxMesaSelectCurrentBoard'!!! */
1852 hw
= fxMesaSelectCurrentBoard(0);
1854 /* if these fail, there's a new bug somewhere */
1855 ASSERT(b
->mesa_buffer
.Width
> 0);
1856 ASSERT(b
->mesa_buffer
.Height
> 0);
1858 if ((hw
== GR_SSTTYPE_VOODOO
) || (hw
== GR_SSTTYPE_Voodoo2
)) {
1859 b
->FXctx
= fxMesaCreateBestContext(0, b
->mesa_buffer
.Width
,
1860 b
->mesa_buffer
.Height
, attribs
);
1861 if ((v
->undithered_pf
!=PF_Index
) && (b
->backxrb
->ximage
)) {
1862 b
->FXisHackUsable
= b
->FXctx
? GL_TRUE
: GL_FALSE
;
1863 if (b
->FXctx
&& (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')) {
1864 b
->FXwindowHack
= GL_TRUE
;
1865 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
1868 b
->FXwindowHack
= GL_FALSE
;
1873 if (fxEnvVar
[0]=='w' || fxEnvVar
[0]=='W')
1874 b
->FXctx
= fxMesaCreateContext(w
, GR_RESOLUTION_NONE
,
1875 GR_REFRESH_75Hz
, attribs
);
1877 b
->FXctx
= fxMesaCreateBestContext(0, b
->mesa_buffer
.Width
,
1878 b
->mesa_buffer
.Height
, attribs
);
1879 b
->FXisHackUsable
= GL_FALSE
;
1880 b
->FXwindowHack
= GL_FALSE
;
1884 "voodoo %d, wid %d height %d hack: usable %d active %d\n",
1885 hw, b->mesa_buffer.Width, b->mesa_buffer.Height,
1886 b->FXisHackUsable, b->FXwindowHack);
1891 _mesa_warning(NULL
, "WARNING: This Mesa Library includes the Glide driver but\n");
1892 _mesa_warning(NULL
, " you have not defined the MESA_GLX_FX env. var.\n");
1893 _mesa_warning(NULL
, " (check the README.3DFX file for more information).\n\n");
1894 _mesa_warning(NULL
, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n");
1903 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
1905 return XMesaCreateWindowBuffer2( v
, w
, NULL
);
1910 * Create a new XMesaBuffer from an X pixmap.
1912 * \param v the XMesaVisual
1913 * \param p the pixmap
1914 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1915 * \c GLX_DIRECT_COLOR visual for the pixmap
1916 * \returns new XMesaBuffer or NULL if error
1919 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
1926 b
= alloc_xmesa_buffer(v
, PIXMAP
, cmap
);
1931 #ifdef XFree86Server
1932 client
= CLIENT_ID(((XMesaDrawable
)p
)->id
);
1935 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1936 (XMesaDrawable
) p
, cmap
)) {
1937 free_xmesa_buffer(client
, b
);
1947 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1948 unsigned int width
, unsigned int height
)
1950 #ifdef XFree86Server
1955 XMesaDrawable drawable
; /* X Pixmap Drawable */
1958 b
= alloc_xmesa_buffer(v
, PBUFFER
, cmap
);
1963 /* allocate pixmap for front buffer */
1964 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1965 drawable
= XCreatePixmap( v
->display
, root
, width
, height
, v
->visinfo
->depth
);
1967 if (!initialize_visual_and_buffer(client
, v
, b
, v
->mesa_visual
.rgbMode
,
1969 free_xmesa_buffer(client
, b
);
1980 * Deallocate an XMesaBuffer structure and all related info.
1982 void XMesaDestroyBuffer( XMesaBuffer b
)
1986 #ifdef XFree86Server
1987 if (b
->frontxrb
->drawable
)
1988 client
= CLIENT_ID(b
->frontxrb
->drawable
->id
);
1991 if (b
->gc
) XMesaFreeGC( b
->xm_visual
->display
, b
->gc
);
1992 if (b
->cleargc
) XMesaFreeGC( b
->xm_visual
->display
, b
->cleargc
);
1993 if (b
->swapgc
) XMesaFreeGC( b
->xm_visual
->display
, b
->swapgc
);
1995 if (b
->xm_visual
->mesa_visual
.doubleBufferMode
)
1997 if (b
->backxrb
->ximage
) {
1998 #if defined(USE_XSHM) && !defined(XFree86Server)
2000 XShmDetach( b
->xm_visual
->display
, &b
->shminfo
);
2001 XDestroyImage( b
->backxrb
->ximage
);
2002 shmdt( b
->shminfo
.shmaddr
);
2006 XMesaDestroyImage( b
->backxrb
->ximage
);
2008 if (b
->backxrb
->pixmap
) {
2009 XMesaFreePixmap( b
->xm_visual
->display
, b
->backxrb
->pixmap
);
2010 if (b
->xm_visual
->hpcr_clear_flag
) {
2011 XMesaFreePixmap( b
->xm_visual
->display
,
2012 b
->xm_visual
->hpcr_clear_pixmap
);
2013 XMesaDestroyImage( b
->xm_visual
->hpcr_clear_ximage
);
2018 _mesa_free( b
->rowimage
->data
);
2019 b
->rowimage
->data
= NULL
;
2020 XMesaDestroyImage( b
->rowimage
);
2023 free_xmesa_buffer(client
, b
);
2029 * Bind buffer b to context c and make c the current rendering context.
2031 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
2033 return XMesaMakeCurrent2( c
, b
, b
);
2038 * Bind buffer b to context c and make c the current rendering context.
2040 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
2041 XMesaBuffer readBuffer
)
2044 if (!drawBuffer
|| !readBuffer
)
2045 return GL_FALSE
; /* must specify buffers! */
2048 if (drawBuffer
->FXctx
) {
2049 fxMesaMakeCurrent(drawBuffer
->FXctx
);
2051 c
->xm_buffer
= drawBuffer
;
2056 if (&(c
->mesa
) == _mesa_get_current_context()
2057 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
2058 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
2059 && ((XMesaBuffer
) c
->mesa
.DrawBuffer
)->wasCurrent
) {
2060 /* same context and buffer, do nothing */
2064 c
->xm_buffer
= drawBuffer
;
2066 _mesa_make_current(&(c
->mesa
),
2067 &drawBuffer
->mesa_buffer
,
2068 &readBuffer
->mesa_buffer
);
2070 if (c
->xm_visual
->mesa_visual
.rgbMode
) {
2072 * Must recompute and set these pixel values because colormap
2073 * can be different for different windows.
2075 c
->clearpixel
= xmesa_color_to_pixel( &c
->mesa
,
2080 c
->xm_visual
->undithered_pf
);
2081 XMesaSetForeground(c
->display
, drawBuffer
->cleargc
, c
->clearpixel
);
2084 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
2085 drawBuffer
->wasCurrent
= GL_TRUE
;
2089 _mesa_make_current( NULL
, NULL
, NULL
);
2096 * Unbind the context c from its buffer.
2098 GLboolean
XMesaUnbindContext( XMesaContext c
)
2100 /* A no-op for XFree86 integration purposes */
2105 XMesaContext
XMesaGetCurrentContext( void )
2107 GET_CURRENT_CONTEXT(ctx
);
2109 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2118 XMesaBuffer
XMesaGetCurrentBuffer( void )
2120 GET_CURRENT_CONTEXT(ctx
);
2122 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
2131 /* New in Mesa 3.1 */
2132 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
2134 GET_CURRENT_CONTEXT(ctx
);
2136 return (XMesaBuffer
) (ctx
->ReadBuffer
);
2144 GLboolean
XMesaForceCurrent(XMesaContext c
)
2147 if (&(c
->mesa
) != _mesa_get_current_context()) {
2148 _mesa_make_current(&c
->mesa
, c
->mesa
.DrawBuffer
, c
->mesa
.ReadBuffer
);
2152 _mesa_make_current(NULL
, NULL
, NULL
);
2158 GLboolean
XMesaLoseCurrent(XMesaContext c
)
2161 _mesa_make_current(NULL
, NULL
, NULL
);
2167 * Switch 3Dfx support hack between window and full-screen mode.
2169 GLboolean
XMesaSetFXmode( GLint mode
)
2172 const char *fx
= _mesa_getenv("MESA_GLX_FX");
2173 if (fx
&& fx
[0] != 'd') {
2174 GET_CURRENT_CONTEXT(ctx
);
2175 GrHwConfiguration hw
;
2176 if (!FX_grSstQueryHardware(&hw
)) {
2177 /*fprintf(stderr, "!grSstQueryHardware\n");*/
2180 if (hw
.num_sst
< 1) {
2181 /*fprintf(stderr, "hw.num_sst < 1\n");*/
2185 /* [dBorca] Hack alert:
2186 * oh, this is sooo wrong: ctx above is
2187 * really an fxMesaContext, not an XMesaContext
2189 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
2190 if (mode
== XMESA_FX_WINDOW
) {
2191 if (xmbuf
->FXisHackUsable
) {
2192 FX_grSstControl(GR_CONTROL_DEACTIVATE
);
2193 xmbuf
->FXwindowHack
= GL_TRUE
;
2197 else if (mode
== XMESA_FX_FULLSCREEN
) {
2198 FX_grSstControl(GR_CONTROL_ACTIVATE
);
2199 xmbuf
->FXwindowHack
= GL_FALSE
;
2203 /* Error: Bad mode value */
2207 /*fprintf(stderr, "fallthrough\n");*/
2218 * Read image from VooDoo frame buffer into X/Mesa's back XImage.
2220 static void FXgetImage( XMesaBuffer b
)
2222 GET_CURRENT_CONTEXT(ctx
);
2223 static unsigned short pixbuf
[MAX_WIDTH
];
2227 unsigned int bw
, depth
, width
, height
;
2228 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
2230 #ifdef XFree86Server
2231 x
= b
->frontxrb
->pixmap
->x
;
2232 y
= b
->frontxrb
->pixmap
->y
;
2233 width
= b
->frontxrb
->pixmap
->width
;
2234 height
= b
->frontxrb
->pixmap
->height
;
2235 depth
= b
->frontxrb
->pixmap
->depth
;
2237 XGetGeometry( b
->xm_visual
->display
, b
->frontxrb
->pixmap
,
2238 &root
, &xpos
, &ypos
, &width
, &height
, &bw
, &depth
);
2240 if (b
->mesa_buffer
.Width
!= width
|| b
->mesa_buffer
.Height
!= height
) {
2241 b
->mesa_buffer
.Width
= MIN2((int)width
, b
->FXctx
->width
);
2242 b
->mesa_buffer
.Height
= MIN2((int)height
, b
->FXctx
->height
);
2243 if (b
->mesa_buffer
.Width
& 1)
2244 b
->mesa_buffer
.Width
--; /* prevent odd width */
2245 xmesa_alloc_back_buffer(b
, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
2248 /* [dBorca] we're always in the right GR_COLORFORMAT... aren't we? */
2249 /* grLfbWriteColorFormat(GR_COLORFORMAT_ARGB); */
2250 if (b
->xm_visual
->undithered_pf
==PF_5R6G5B
) {
2251 /* Special case: 16bpp RGB */
2252 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
, /* src buffer */
2253 0, b
->FXctx
->height
- b
->mesa_buffer
.Height
, /*pos*/
2254 b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
, /* size */
2255 b
->mesa_buffer
.Width
* sizeof(GLushort
), /* stride */
2256 b
->backxrb
->ximage
->data
); /* dest buffer */
2258 else if (b
->xm_visual
->dithered_pf
==PF_Dither
2259 && GET_VISUAL_DEPTH(b
->xm_visual
)==8) {
2260 /* Special case: 8bpp RGB */
2261 for (y
=0;y
<b
->mesa_buffer
.Height
;y
++) {
2262 GLubyte
*ptr
= (GLubyte
*) b
->backxrb
->ximage
->data
2263 + b
->backxrb
->ximage
->bytes_per_line
* y
;
2266 /* read row from 3Dfx frame buffer */
2267 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2268 0, b
->FXctx
->height
-(b
->mesa_buffer
.Height
-y
),
2269 b
->mesa_buffer
.Width
, 1,
2273 /* write to XImage back buffer */
2274 for (x
=0;x
<b
->mesa_buffer
.Width
;x
++) {
2275 GLubyte r
= (pixbuf
[x
] & 0xf800) >> 8;
2276 GLubyte g
= (pixbuf
[x
] & 0x07e0) >> 3;
2277 GLubyte b
= (pixbuf
[x
] & 0x001f) << 3;
2278 *ptr
++ = XDITHER( x
, r
, g
, b
);
2283 /* General case: slow! */
2284 for (y
=0;y
<b
->mesa_buffer
.Height
;y
++) {
2285 /* read row from 3Dfx frame buffer */
2286 grLfbReadRegion( GR_BUFFER_FRONTBUFFER
,
2287 0, b
->FXctx
->height
-(b
->mesa_buffer
.Height
-y
),
2288 b
->mesa_buffer
.Width
, 1,
2292 /* write to XImage back buffer */
2293 for (x
=0;x
<b
->mesa_buffer
.Width
;x
++) {
2294 XMesaPutPixel(b
->backxrb
->ximage
,x
,y
,
2295 xmesa_color_to_pixel(ctx
,
2296 (pixbuf
[x
] & 0xf800) >> 8,
2297 (pixbuf
[x
] & 0x07e0) >> 3,
2298 (pixbuf
[x
] & 0x001f) << 3,
2300 b
->xm_visual
->undithered_pf
));
2304 /* grLfbWriteColorFormat(GR_COLORFORMAT_ABGR); */
2310 * Copy the back buffer to the front buffer. If there's no back buffer
2313 void XMesaSwapBuffers( XMesaBuffer b
)
2315 GET_CURRENT_CONTEXT(ctx
);
2318 /* single buffered */
2322 /* If we're swapping the buffer associated with the current context
2323 * we have to flush any pending rendering commands first.
2325 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2326 _mesa_notifySwapBuffers(ctx
);
2331 fxMesaSwapBuffers();
2333 if (b
->FXwindowHack
)
2339 if (b
->backxrb
->ximage
) {
2340 /* Copy Ximage from host's memory to server's window */
2341 #if defined(USE_XSHM) && !defined(XFree86Server)
2343 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2344 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2346 b
->backxrb
->ximage
, 0, 0,
2347 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2349 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2354 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2355 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2357 b
->backxrb
->ximage
, 0, 0,
2358 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
2359 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2363 /* Copy pixmap to window on server */
2364 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2365 XMesaCopyArea( b
->xm_visual
->display
,
2366 b
->backxrb
->pixmap
, /* source drawable */
2367 b
->frontxrb
->drawable
, /* dest. drawable */
2369 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2370 0, 0 /* dest region */
2372 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2375 #if !defined(XFree86Server)
2376 XSync( b
->xm_visual
->display
, False
);
2383 * Copy sub-region of back buffer to front buffer
2385 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
2387 GET_CURRENT_CONTEXT(ctx
);
2389 /* If we're swapping the buffer associated with the current context
2390 * we have to flush any pending rendering commands first.
2392 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2393 _mesa_notifySwapBuffers(ctx
);
2396 /* single buffered */
2401 int yTop
= b
->mesa_buffer
.Height
- y
- height
;
2404 fxMesaSwapBuffers();
2405 if (b
->FXwindowHack
)
2411 if (b
->backxrb
->ximage
) {
2412 /* Copy Ximage from host's memory to server's window */
2413 #if defined(USE_XSHM) && !defined(XFree86Server)
2415 /* XXX assuming width and height aren't too large! */
2416 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2418 b
->backxrb
->ximage
, x
, yTop
,
2419 x
, yTop
, width
, height
, False
);
2420 /* wait for finished event??? */
2425 /* XXX assuming width and height aren't too large! */
2426 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2428 b
->backxrb
->ximage
, x
, yTop
,
2429 x
, yTop
, width
, height
);
2433 /* Copy pixmap to window on server */
2434 XMesaCopyArea( b
->xm_visual
->display
,
2435 b
->backxrb
->pixmap
, /* source drawable */
2436 b
->frontxrb
->drawable
, /* dest. drawable */
2438 x
, yTop
, width
, height
, /* source region */
2439 x
, yTop
/* dest region */
2447 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2448 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2449 * back buffer directly.
2450 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2451 * ximage - pointer to back buffer's XImage, or NULL
2452 * Return: GL_TRUE = context is double buffered
2453 * GL_FALSE = context is single buffered
2455 #ifndef XFree86Server
2456 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
2457 XMesaPixmap
*pixmap
,
2458 XMesaImage
**ximage
)
2462 *pixmap
= b
->backxrb
->pixmap
;
2464 *ximage
= b
->backxrb
->ximage
;
2473 #endif /* XFree86Server */
2477 * Return the depth buffer associated with an XMesaBuffer.
2478 * Input: b - the XMesa buffer handle
2479 * Output: width, height - size of buffer in pixels
2480 * bytesPerValue - bytes per depth value (2 or 4)
2481 * buffer - pointer to depth buffer values
2482 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2484 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
2485 GLint
*bytesPerValue
, void **buffer
)
2487 struct gl_renderbuffer
*rb
2488 = b
->mesa_buffer
.Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2489 if (!rb
|| !rb
->Data
) {
2497 *width
= b
->mesa_buffer
.Width
;
2498 *height
= b
->mesa_buffer
.Height
;
2499 *bytesPerValue
= b
->mesa_buffer
.Visual
.depthBits
<= 16
2500 ? sizeof(GLushort
) : sizeof(GLuint
);
2507 void XMesaFlush( XMesaContext c
)
2509 if (c
&& c
->xm_visual
) {
2510 #ifdef XFree86Server
2513 XSync( c
->xm_visual
->display
, False
);
2520 const char *XMesaGetString( XMesaContext c
, int name
)
2523 if (name
==XMESA_VERSION
) {
2526 else if (name
==XMESA_EXTENSIONS
) {
2536 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
2539 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
2540 if (b
->frontxrb
->drawable
== d
&& b
->display
== dpy
) {
2550 * Look for XMesaBuffers whose X window has been destroyed.
2551 * Deallocate any such XMesaBuffers.
2553 void XMesaGarbageCollect( void )
2555 XMesaBuffer b
, next
;
2556 for (b
=XMesaBufferList
; b
; b
=next
) {
2558 if (b
->display
&& b
->frontxrb
->drawable
&& b
->type
== WINDOW
) {
2559 #ifdef XFree86Server
2562 XSync(b
->display
, False
);
2563 if (!window_exists( b
->display
, b
->frontxrb
->drawable
)) {
2564 /* found a dead window, free the ancillary info */
2565 XMesaDestroyBuffer( b
);
2573 void XMesaReset( void )
2575 while (XMesaBufferList
)
2576 XMesaDestroyBuffer(XMesaBufferList
);
2578 XMesaBufferList
= NULL
;
2582 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
2583 GLfloat red
, GLfloat green
,
2584 GLfloat blue
, GLfloat alpha
)
2586 GLcontext
*ctx
= &xmesa
->mesa
;
2587 GLint r
= (GLint
) (red
* 255.0F
);
2588 GLint g
= (GLint
) (green
* 255.0F
);
2589 GLint b
= (GLint
) (blue
* 255.0F
);
2590 GLint a
= (GLint
) (alpha
* 255.0F
);
2592 switch (xmesa
->pixelformat
) {
2598 PACK_TRUECOLOR( p
, r
, g
, b
);
2602 return PACK_8A8B8G8R( r
, g
, b
, a
);
2604 return PACK_8A8R8G8B( r
, g
, b
, a
);
2606 return PACK_8R8G8B( r
, g
, b
);
2608 return PACK_5R6G5B( r
, g
, b
);
2612 return DITHER( x
, y
, r
, g
, b
);
2615 /* 382 = (3*255)/2 */
2616 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
2618 return DITHER_HPCR(x
, y
, r
, g
, b
);
2622 return LOOKUP( r
, g
, b
);
2625 return GRAY_RGB( r
, g
, b
);
2626 case PF_Dither_5R6G5B
:
2628 case PF_Dither_True
:
2631 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
2635 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
2642 * This is typically called when the window size changes and we need
2643 * to reallocate the buffer's back/depth/stencil/accum buffers.
2645 void XMesaResizeBuffers( XMesaBuffer b
)
2647 #ifdef XFree86Server
2648 GLuint winwidth
, winheight
;
2649 GET_CURRENT_CONTEXT(ctx
);
2651 winwidth
= MIN2(b
->frontxrb
->drawable
->width
, MAX_WIDTH
);
2652 winheight
= MIN2(b
->frontxrb
->drawable
->height
, MAX_HEIGHT
);
2654 xmesa_resize_buffers(ctx
, &(b
->mesa_buffer
), winwidth
, winheight
);
2658 unsigned int width
, height
, bw
, depth
;
2659 GET_CURRENT_CONTEXT(ctx
);
2660 XGetGeometry( b
->xm_visual
->display
, b
->frontxrb
->pixmap
,
2661 &root
, &xpos
, &ypos
, &width
, &height
, &bw
, &depth
);
2662 xmesa_resize_buffers(ctx
, &(b
->mesa_buffer
), width
, height
);