2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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.
28 * All the XMesa* API functions.
33 * The window coordinate system origin (0,0) is in the lower-left corner
34 * of the window. X11's window coordinate origin is in the upper-left
35 * corner of the window. Therefore, most drawing functions in this
36 * file have to flip Y coordinates.
38 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
39 * in support for the MIT Shared Memory extension. If enabled, when you
40 * use an Ximage for the back buffer in double buffered mode, the "swap"
41 * operation will be faster. You must also link with -lXext.
43 * Byte swapping: If the Mesa host and the X display use a different
44 * byte order then there's some trickiness to be aware of when using
45 * XImages. The byte ordering used for the XImage is that of the X
46 * display, not the Mesa host.
47 * The color-to-pixel encoding for True/DirectColor must be done
48 * according to the display's visual red_mask, green_mask, and blue_mask.
49 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
50 * do byte swapping if needed. If one wants to directly "poke" the pixel
51 * into the XImage's buffer then the pixel must be byte swapped first. In
52 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
53 * and use XPutPixel everywhere except in the implementation of
54 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
55 * instead of using XPutPixel we "poke" our values after byte-swapping
56 * the clear pixel value if needed.
65 #include "glxheader.h"
69 #include "extensions.h"
70 #include "framebuffer.h"
74 #include "renderbuffer.h"
76 #include "swrast/swrast.h"
77 #include "swrast_setup/swrast_setup.h"
80 #include "tnl/t_context.h"
81 #include "tnl/t_pipeline.h"
82 #include "drivers/common/driverfuncs.h"
84 #include "state_tracker/st_public.h"
85 #include "state_tracker/st_context.h"
86 #include "pipe/softpipe/sp_context.h"
87 #include "pipe/p_defines.h"
90 * Global X driver lock
92 _glthread_Mutex _xmesa_lock
;
97 * Lookup tables for HPCR pixel format:
99 static short hpcr_rgbTbl
[3][256] = {
101 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
102 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
103 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
104 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
105 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
106 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
107 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
108 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
109 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
110 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
111 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
112 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
113 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
114 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
115 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
116 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
119 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
120 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31,
121 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
122 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
123 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
124 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
125 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
126 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
127 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
128 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
129 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
130 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
131 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
132 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
133 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
134 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
137 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
138 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47,
139 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55,
140 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63,
141 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71,
142 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79,
143 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87,
144 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
145 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
146 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
147 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
148 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
149 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
150 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
151 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
152 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
158 /**********************************************************************/
159 /***** X Utility Functions *****/
160 /**********************************************************************/
164 * Return the host's byte order as LSBFirst or MSBFirst ala X.
166 #ifndef XFree86Server
167 static int host_byte_order( void )
170 char *cptr
= (char *) &i
;
171 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
177 * Check if the X Shared Memory extension is available.
178 * Return: 0 = not available
179 * 1 = shared XImage support available
180 * 2 = shared Pixmap support available also
182 static int check_for_xshm( XMesaDisplay
*display
)
184 #if defined(USE_XSHM) && !defined(XFree86Server)
185 int major
, minor
, ignore
;
188 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
189 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
190 return (pixmaps
==True
) ? 2 : 1;
200 /* No XSHM support */
207 * Apply gamma correction to an intensity value in [0..max]. Return the
208 * new intensity value.
211 gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
217 double x
= (double) value
/ (double) max
;
218 return IROUND_POS((GLfloat
) max
* _mesa_pow(x
, 1.0F
/gamma
));
225 * Return the true number of bits per pixel for XImages.
226 * For example, if we request a 24-bit deep visual we may actually need/get
227 * 32bpp XImages. This function returns the appropriate bpp.
228 * Input: dpy - the X display
229 * visinfo - desribes the visual to be used for XImages
230 * Return: true number of bits per pixel for XImages
233 bits_per_pixel( XMesaVisual xmv
)
236 const int depth
= xmv
->nplanes
;
239 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
240 if (screenInfo
.formats
[i
].depth
== depth
)
241 return screenInfo
.formats
[i
].bitsPerPixel
;
243 return depth
; /* should never get here, but this should be safe */
245 XMesaDisplay
*dpy
= xmv
->display
;
246 XMesaVisualInfo visinfo
= xmv
->visinfo
;
249 /* Create a temporary XImage */
250 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
251 ZPixmap
, 0, /*format, offset*/
252 (char*) MALLOC(8), /*data*/
253 1, 1, /*width, height*/
258 /* grab the bits/pixel value */
259 bitsPerPixel
= img
->bits_per_pixel
;
260 /* free the XImage */
261 _mesa_free( img
->data
);
263 XMesaDestroyImage( img
);
271 * Determine if a given X window ID is valid (window exists).
272 * Do this by calling XGetWindowAttributes() for the window and
273 * checking if we catch an X error.
274 * Input: dpy - the display
275 * win - the window to check for existance
276 * Return: GL_TRUE - window exists
277 * GL_FALSE - window doesn't exist
279 #ifndef XFree86Server
280 static GLboolean WindowExistsFlag
;
282 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
285 if (xerr
->error_code
== BadWindow
) {
286 WindowExistsFlag
= GL_FALSE
;
291 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
293 XWindowAttributes wa
;
294 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
295 WindowExistsFlag
= GL_TRUE
;
296 old_handler
= XSetErrorHandler(window_exists_err_handler
);
297 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
298 XSetErrorHandler(old_handler
);
299 return WindowExistsFlag
;
303 get_drawable_size( XMesaDisplay
*dpy
, Drawable d
, GLuint
*width
, GLuint
*height
)
308 unsigned int w
, h
, bw
, depth
;
309 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
318 * Return the size of the window (or pixmap) that corresponds to the
320 * \param width returns width in pixels
321 * \param height returns height in pixels
324 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
325 GLuint
*width
, GLuint
*height
)
328 *width
= MIN2(b
->frontxrb
->drawable
->width
, MAX_WIDTH
);
329 *height
= MIN2(b
->frontxrb
->drawable
->height
, MAX_HEIGHT
);
333 _glthread_LOCK_MUTEX(_xmesa_lock
);
334 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
335 stat
= get_drawable_size(dpy
, b
->frontxrb
->pixmap
, width
, height
);
336 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
339 /* probably querying a window that's recently been destroyed */
340 _mesa_warning(NULL
, "XGetGeometry failed!\n");
341 *width
= *height
= 1;
348 /**********************************************************************/
349 /***** Linked list of XMesaBuffers *****/
350 /**********************************************************************/
352 XMesaBuffer XMesaBufferList
= NULL
;
356 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
357 * Note that XMesaBuffer is derived from GLframebuffer.
358 * The new XMesaBuffer will not have any size (Width=Height=0).
360 * \param d the corresponding X drawable (window or pixmap)
361 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
362 * \param vis the buffer's visual
363 * \param cmap the window's colormap, if known.
364 * \return new XMesaBuffer or NULL if any problem
367 create_xmesa_buffer(XMesaDrawable d
, BufferType type
,
368 XMesaVisual vis
, XMesaColormap cmap
)
372 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
374 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
378 b
->display
= vis
->display
;
383 _mesa_initialize_framebuffer(&b
->mesa_buffer
, &vis
->mesa_visual
);
384 b
->mesa_buffer
.Delete
= xmesa_delete_framebuffer
;
389 b
->frontxrb
= xmesa_create_renderbuffer(NULL
, 0, &vis
->mesa_visual
, GL_FALSE
);
394 b
->frontxrb
->Parent
= b
;
395 b
->frontxrb
->drawable
= d
;
396 b
->frontxrb
->pixmap
= (XMesaPixmap
) d
;
397 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_FRONT_LEFT
,
398 &b
->frontxrb
->St
.Base
);
403 if (vis
->mesa_visual
.doubleBufferMode
) {
404 b
->backxrb
= xmesa_create_renderbuffer(NULL
, 0, &vis
->mesa_visual
, GL_TRUE
);
406 /* XXX free front xrb too */
410 b
->backxrb
->Parent
= b
;
411 /* determine back buffer implementation */
412 b
->db_mode
= vis
->ximage_flag
? BACK_XIMAGE
: BACK_PIXMAP
;
414 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_BACK_LEFT
,
415 &b
->backxrb
->St
.Base
);
419 * Software alpha planes
421 if (vis
->mesa_visual
.alphaBits
> 0
422 && vis
->undithered_pf
!= PF_8A8B8G8R
423 && vis
->undithered_pf
!= PF_8A8R8G8B
) {
424 /* Visual has alpha, but pixel format doesn't support it.
425 * We'll use an alpha renderbuffer wrapper.
427 b
->swAlpha
= GL_TRUE
;
430 b
->swAlpha
= GL_FALSE
;
433 if (vis
->mesa_visual
.depthBits
> 0 &&
434 vis
->mesa_visual
.stencilBits
> 0) {
435 /* combined depth/stencil */
436 struct gl_renderbuffer
*rb
437 = st_new_renderbuffer_fb(GL_DEPTH24_STENCIL8_EXT
);
438 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_DEPTH
, rb
);
439 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_STENCIL
, rb
);
442 if (vis
->mesa_visual
.depthBits
> 0) {
443 struct gl_renderbuffer
*rb
444 = st_new_renderbuffer_fb(GL_DEPTH_COMPONENT32
);
445 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_DEPTH
, rb
);
448 if (vis
->mesa_visual
.stencilBits
> 0) {
449 struct gl_renderbuffer
*rb
450 = st_new_renderbuffer_fb(GL_STENCIL_INDEX8_EXT
);
451 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_STENCIL
, rb
);
455 if (vis
->mesa_visual
.accumRedBits
> 0) {
456 struct gl_renderbuffer
*rb
457 = st_new_renderbuffer_fb(GL_RGBA16
);
458 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_ACCUM
, rb
);
463 * Other renderbuffer (depth, stencil, etc)
465 _mesa_add_soft_renderbuffers(&b
->mesa_buffer
,
466 GL_FALSE
, /* color */
467 GL_FALSE
, /*vis->mesa_visual.haveDepthBuffer,*/
468 GL_FALSE
, /* stencil */
469 GL_FALSE
, /* accum */
471 vis
->mesa_visual
.numAuxBuffers
> 0 );
473 /* GLX_EXT_texture_from_pixmap */
474 b
->TextureTarget
= 0;
475 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
476 b
->TextureMipmap
= 0;
478 /* insert buffer into linked list */
479 b
->Next
= XMesaBufferList
;
487 * Find an XMesaBuffer by matching X display and colormap but NOT matching
488 * the notThis buffer.
491 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
494 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
495 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
504 * Remove buffer from linked list, delete if no longer referenced.
507 xmesa_free_buffer(XMesaBuffer buffer
)
509 XMesaBuffer prev
= NULL
, b
;
511 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
513 struct gl_framebuffer
*fb
= &buffer
->mesa_buffer
;
515 /* unlink buffer from list */
517 prev
->Next
= buffer
->Next
;
519 XMesaBufferList
= buffer
->Next
;
521 /* mark as delete pending */
522 fb
->DeletePending
= GL_TRUE
;
524 /* Since the X window for the XMesaBuffer is going away, we don't
525 * want to dereference this pointer in the future.
527 b
->frontxrb
->drawable
= 0;
529 /* Unreference. If count = zero we'll really delete the buffer */
530 _mesa_unreference_framebuffer(&fb
);
534 /* continue search */
537 /* buffer not found in XMesaBufferList */
538 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
543 * Copy X color table stuff from one XMesaBuffer to another.
546 copy_colortable_info(XMesaBuffer dst
, const XMesaBuffer src
)
548 MEMCPY(dst
->color_table
, src
->color_table
, sizeof(src
->color_table
));
549 MEMCPY(dst
->pixel_to_r
, src
->pixel_to_r
, sizeof(src
->pixel_to_r
));
550 MEMCPY(dst
->pixel_to_g
, src
->pixel_to_g
, sizeof(src
->pixel_to_g
));
551 MEMCPY(dst
->pixel_to_b
, src
->pixel_to_b
, sizeof(src
->pixel_to_b
));
552 dst
->num_alloced
= src
->num_alloced
;
553 MEMCPY(dst
->alloced_colors
, src
->alloced_colors
,
554 sizeof(src
->alloced_colors
));
559 /**********************************************************************/
560 /***** Misc Private Functions *****/
561 /**********************************************************************/
565 * A replacement for XAllocColor. This function should never
566 * fail to allocate a color. When XAllocColor fails, we return
567 * the nearest matching color. If we have to allocate many colors
568 * this function isn't too efficient; the XQueryColors() could be
570 * Written by Michael Pichler, Brian Paul, Mark Kilgard
571 * Input: dpy - X display
573 * cmapSize - size of colormap
574 * In/Out: color - the XColor struct
575 * Output: exact - 1=exact color match, 0=closest match
576 * alloced - 1=XAlloc worked, 0=XAlloc failed
579 noFaultXAllocColor( int client
,
584 int *exact
, int *alloced
)
590 /* we'll try to cache ctable for better remote display performance */
591 static Display
*prevDisplay
= NULL
;
592 static XMesaColormap prevCmap
= 0;
593 static int prevCmapSize
= 0;
594 static XMesaColor
*ctable
= NULL
;
598 double mindist
; /* 3*2^16^2 exceeds long int precision. */
602 /* First try just using XAllocColor. */
605 &color
->red
, &color
->green
, &color
->blue
,
609 if (XAllocColor(dpy
, cmap
, color
))
617 /* Alloc failed, search for closest match */
619 /* Retrieve color table entries. */
620 /* XXX alloca candidate. */
622 ppixIn
= (Pixel
*) MALLOC(cmapSize
* sizeof(Pixel
));
623 ctable
= (xrgb
*) MALLOC(cmapSize
* sizeof(xrgb
));
624 for (i
= 0; i
< cmapSize
; i
++) {
627 QueryColors(cmap
, cmapSize
, ppixIn
, ctable
);
629 if (prevDisplay
!= dpy
|| prevCmap
!= cmap
630 || prevCmapSize
!= cmapSize
|| !ctable
) {
631 /* free previously cached color table */
634 /* Get the color table from X */
635 ctable
= (XMesaColor
*) MALLOC(cmapSize
* sizeof(XMesaColor
));
637 for (i
= 0; i
< cmapSize
; i
++) {
640 XQueryColors(dpy
, cmap
, ctable
, cmapSize
);
643 prevCmapSize
= cmapSize
;
647 /* Find best match. */
650 for (i
= 0; i
< cmapSize
; i
++) {
651 double dr
= 0.30 * ((double) color
->red
- (double) ctable
[i
].red
);
652 double dg
= 0.59 * ((double) color
->green
- (double) ctable
[i
].green
);
653 double db
= 0.11 * ((double) color
->blue
- (double) ctable
[i
].blue
);
654 double dist
= dr
* dr
+ dg
* dg
+ db
* db
;
655 if (bestmatch
< 0 || dist
< mindist
) {
662 subColor
.red
= ctable
[bestmatch
].red
;
663 subColor
.green
= ctable
[bestmatch
].green
;
664 subColor
.blue
= ctable
[bestmatch
].blue
;
665 /* Try to allocate the closest match color. This should only
666 * fail if the cell is read/write. Otherwise, we're incrementing
667 * the cell's reference count.
671 &subColor
.red
, &subColor
.green
, &subColor
.blue
,
673 client
) == Success
) {
675 if (XAllocColor(dpy
, cmap
, &subColor
)) {
680 /* do this to work around a problem reported by Frank Ortega */
681 subColor
.pixel
= (unsigned long) bestmatch
;
682 subColor
.red
= ctable
[bestmatch
].red
;
683 subColor
.green
= ctable
[bestmatch
].green
;
684 subColor
.blue
= ctable
[bestmatch
].blue
;
685 subColor
.flags
= DoRed
| DoGreen
| DoBlue
;
692 /* don't free table, save it for next time */
702 * Do setup for PF_GRAYSCALE pixel format.
703 * Note that buffer may be NULL.
706 setup_grayscale(int client
, XMesaVisual v
,
707 XMesaBuffer buffer
, XMesaColormap cmap
)
709 if (GET_VISUAL_DEPTH(v
)<4 || GET_VISUAL_DEPTH(v
)>16) {
714 XMesaBuffer prevBuffer
;
720 prevBuffer
= xmesa_find_buffer(v
->display
, cmap
, buffer
);
722 (buffer
->xm_visual
->mesa_visual
.rgbMode
==
723 prevBuffer
->xm_visual
->mesa_visual
.rgbMode
)) {
724 /* Copy colormap stuff from previous XMesaBuffer which uses same
725 * X colormap. Do this to avoid time spent in noFaultXAllocColor.
727 copy_colortable_info(buffer
, prevBuffer
);
730 /* Allocate 256 shades of gray */
732 int colorsfailed
= 0;
733 for (gray
=0;gray
<256;gray
++) {
734 GLint r
= gamma_adjust( v
->RedGamma
, gray
, 255 );
735 GLint g
= gamma_adjust( v
->GreenGamma
, gray
, 255 );
736 GLint b
= gamma_adjust( v
->BlueGamma
, gray
, 255 );
739 xcol
.red
= (r
<< 8) | r
;
740 xcol
.green
= (g
<< 8) | g
;
741 xcol
.blue
= (b
<< 8) | b
;
742 noFaultXAllocColor( client
, v
->display
,
743 cmap
, GET_COLORMAP_SIZE(v
),
744 &xcol
, &exact
, &alloced
);
749 assert(buffer
->num_alloced
<256);
750 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
751 buffer
->num_alloced
++;
756 buffer->color_table[gray*3+0] = xcol.pixel;
757 buffer->color_table[gray*3+1] = xcol.pixel;
758 buffer->color_table[gray*3+2] = xcol.pixel;
759 assert(xcol.pixel < 65536);
760 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100;
761 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100;
762 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100;
764 buffer
->color_table
[gray
] = xcol
.pixel
;
765 assert(xcol
.pixel
< 65536);
766 buffer
->pixel_to_r
[xcol
.pixel
] = gray
;
767 buffer
->pixel_to_g
[xcol
.pixel
] = gray
;
768 buffer
->pixel_to_b
[xcol
.pixel
] = gray
;
771 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
773 "Note: %d out of 256 needed colors do not match exactly.\n",
779 v
->dithered_pf
= PF_Grayscale
;
780 v
->undithered_pf
= PF_Grayscale
;
787 * Setup RGB rendering for a window with a PseudoColor, StaticColor,
788 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225
789 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
790 * color. While this function was originally designed just for 8-bit
791 * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
792 * Dithering code contributed by Bob Mercier.
795 setup_dithered_color(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
= xmesa_find_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, matching XMesaBuffer.
814 * Do this to avoid time spent in noFaultXAllocColor.
816 copy_colortable_info(buffer
, prevBuffer
);
819 /* Allocate X colors and initialize color_table[], red_table[], etc */
821 int colorsfailed
= 0;
822 for (r
= 0; r
< DITH_R
; r
++) {
823 for (g
= 0; g
< DITH_G
; g
++) {
824 for (b
= 0; b
< DITH_B
; b
++) {
827 xcol
.red
=gamma_adjust(v
->RedGamma
, r
*65535/(DITH_R
-1),65535);
828 xcol
.green
=gamma_adjust(v
->GreenGamma
, g
*65535/(DITH_G
-1),65535);
829 xcol
.blue
=gamma_adjust(v
->BlueGamma
, b
*65535/(DITH_B
-1),65535);
830 noFaultXAllocColor( client
, v
->display
,
831 cmap
, GET_COLORMAP_SIZE(v
),
832 &xcol
, &exact
, &alloced
);
837 assert(buffer
->num_alloced
<256);
838 buffer
->alloced_colors
[buffer
->num_alloced
] = xcol
.pixel
;
839 buffer
->num_alloced
++;
841 i
= DITH_MIX( r
, g
, b
);
843 buffer
->color_table
[i
] = xcol
.pixel
;
844 assert(xcol
.pixel
< 65536);
845 buffer
->pixel_to_r
[xcol
.pixel
] = r
* 255 / (DITH_R
-1);
846 buffer
->pixel_to_g
[xcol
.pixel
] = g
* 255 / (DITH_G
-1);
847 buffer
->pixel_to_b
[xcol
.pixel
] = b
* 255 / (DITH_B
-1);
852 if (colorsfailed
&& _mesa_getenv("MESA_DEBUG")) {
854 "Note: %d out of %d needed colors do not match exactly.\n",
855 colorsfailed
, DITH_R
* DITH_G
* DITH_B
);
860 v
->dithered_pf
= PF_Dither
;
861 v
->undithered_pf
= PF_Lookup
;
867 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode.
868 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer.
869 * Special dithering tables have to be initialized.
872 setup_8bit_hpcr(XMesaVisual v
)
874 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be)
875 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
876 * on the root window AND the colormap obtainable by XGetRGBColormaps
877 * for that atom must be set on the window. (see also tkInitWindow)
878 * If that colormap is not set, the output will look stripy.
881 /* Setup color tables with gamma correction */
885 g
= 1.0 / v
->RedGamma
;
886 for (i
=0; i
<256; i
++) {
887 GLint red
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[0][i
]/255.0, g
));
888 v
->hpcr_rgbTbl
[0][i
] = CLAMP( red
, 16, 239 );
891 g
= 1.0 / v
->GreenGamma
;
892 for (i
=0; i
<256; i
++) {
893 GLint green
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[1][i
]/255.0, g
));
894 v
->hpcr_rgbTbl
[1][i
] = CLAMP( green
, 16, 239 );
897 g
= 1.0 / v
->BlueGamma
;
898 for (i
=0; i
<256; i
++) {
899 GLint blue
= IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl
[2][i
]/255.0, g
));
900 v
->hpcr_rgbTbl
[2][i
] = CLAMP( blue
, 32, 223 );
902 v
->undithered_pf
= PF_HPCR
; /* can't really disable dithering for now */
903 v
->dithered_pf
= PF_HPCR
;
905 /* which method should I use to clear */
906 /* GL_FALSE: keep the ordinary method */
907 /* GL_TRUE : clear with dither pattern */
908 v
->hpcr_clear_flag
= _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE
: GL_FALSE
;
910 if (v
->hpcr_clear_flag
) {
911 v
->hpcr_clear_pixmap
= XMesaCreatePixmap(v
->display
,
912 DefaultRootWindow(v
->display
),
914 #ifndef XFree86Server
915 v
->hpcr_clear_ximage
= XGetImage(v
->display
, v
->hpcr_clear_pixmap
,
916 0, 0, 16, 2, AllPlanes
, ZPixmap
);
923 * Setup RGB rendering for a window with a True/DirectColor visual.
926 setup_truecolor(XMesaVisual v
, XMesaBuffer buffer
, XMesaColormap cmap
)
928 unsigned long rmask
, gmask
, bmask
;
932 /* Compute red multiplier (mask) and bit shift */
934 rmask
= GET_REDMASK(v
);
935 while ((rmask
& 1)==0) {
940 /* Compute green multiplier (mask) and bit shift */
942 gmask
= GET_GREENMASK(v
);
943 while ((gmask
& 1)==0) {
948 /* Compute blue multiplier (mask) and bit shift */
950 bmask
= GET_BLUEMASK(v
);
951 while ((bmask
& 1)==0) {
957 * Compute component-to-pixel lookup tables and dithering kernel
960 static GLubyte kernel
[16] = {
961 0*16, 8*16, 2*16, 10*16,
962 12*16, 4*16, 14*16, 6*16,
963 3*16, 11*16, 1*16, 9*16,
964 15*16, 7*16, 13*16, 5*16,
966 GLint rBits
= _mesa_bitcount(rmask
);
967 GLint gBits
= _mesa_bitcount(gmask
);
968 GLint bBits
= _mesa_bitcount(bmask
);
972 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
973 for (i
=0; i
<=rmask
; i
++)
974 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
975 for (i
=0; i
<=gmask
; i
++)
976 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
977 for (i
=0; i
<=bmask
; i
++)
978 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
980 /* convert RGB values from [0,255] to pixel components */
982 for (i
=0;i
<256;i
++) {
983 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
984 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
985 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
986 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
987 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
988 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
990 /* overflow protection */
991 for (i
=256;i
<512;i
++) {
992 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
993 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
994 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
997 /* setup dithering kernel */
999 if (gBits
> maxBits
) maxBits
= gBits
;
1000 if (bBits
> maxBits
) maxBits
= bBits
;
1001 for (i
=0;i
<16;i
++) {
1002 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
1005 v
->undithered_pf
= PF_Truecolor
;
1006 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
1010 * Now check for TrueColor visuals which we can optimize.
1012 if ( GET_REDMASK(v
) ==0x0000ff
1013 && GET_GREENMASK(v
)==0x00ff00
1014 && GET_BLUEMASK(v
) ==0xff0000
1015 && CHECK_BYTE_ORDER(v
)
1016 && v
->BitsPerPixel
==32
1017 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1018 /* common 32 bpp config used on SGI, Sun */
1019 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
; /* ABGR */
1021 else if (GET_REDMASK(v
) == 0xff0000
1022 && GET_GREENMASK(v
)== 0x00ff00
1023 && GET_BLUEMASK(v
) == 0x0000ff
1024 && CHECK_BYTE_ORDER(v
)
1025 && v
->RedGamma
== 1.0 && v
->GreenGamma
== 1.0 && v
->BlueGamma
== 1.0){
1026 if (v
->BitsPerPixel
==32) {
1027 /* if 32 bpp, and visual indicates 8 bpp alpha channel */
1028 if (GET_VISUAL_DEPTH(v
) == 32 && v
->mesa_visual
.alphaBits
== 8)
1029 v
->undithered_pf
= v
->dithered_pf
= PF_8A8R8G8B
; /* ARGB */
1031 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
; /* xRGB */
1033 else if (v
->BitsPerPixel
== 24) {
1034 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
; /* RGB */
1037 else if (GET_REDMASK(v
) ==0xf800
1038 && GET_GREENMASK(v
)==0x07e0
1039 && GET_BLUEMASK(v
) ==0x001f
1040 && CHECK_BYTE_ORDER(v
)
1041 && v
->BitsPerPixel
==16
1042 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
1044 v
->undithered_pf
= PF_5R6G5B
;
1045 v
->dithered_pf
= PF_Dither_5R6G5B
;
1047 else if (GET_REDMASK(v
) ==0xe0
1048 && GET_GREENMASK(v
)==0x1c
1049 && GET_BLUEMASK(v
) ==0x03
1050 && CHECK_FOR_HPCR(v
)) {
1051 /* 8-bit HP color recovery */
1052 setup_8bit_hpcr( v
);
1059 * Setup RGB rendering for a window with a monochrome visual.
1062 setup_monochrome( XMesaVisual v
, XMesaBuffer b
)
1065 v
->dithered_pf
= v
->undithered_pf
= PF_1Bit
;
1066 /* if black=1 then we must flip pixel values */
1067 v
->bitFlip
= (GET_BLACK_PIXEL(v
) != 0);
1073 * When a context is bound for the first time, we can finally finish
1074 * initializing the context's visual and buffer information.
1075 * \param v the XMesaVisual to initialize
1076 * \param b the XMesaBuffer to initialize (may be NULL)
1077 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
1078 * \param window the window/pixmap we're rendering into
1079 * \param cmap the colormap associated with the window/pixmap
1080 * \return GL_TRUE=success, GL_FALSE=failure
1083 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
1084 GLboolean rgb_flag
, XMesaDrawable window
,
1089 #ifdef XFree86Server
1090 client
= (window
) ? CLIENT_ID(window
->id
) : 0;
1093 ASSERT(!b
|| b
->xm_visual
== v
);
1095 /* Save true bits/pixel */
1096 v
->BitsPerPixel
= bits_per_pixel(v
);
1097 assert(v
->BitsPerPixel
> 0);
1099 if (rgb_flag
== GL_FALSE
) {
1100 /* COLOR-INDEXED WINDOW:
1101 * Even if the visual is TrueColor or DirectColor we treat it as
1102 * being color indexed. This is weird but might be useful to someone.
1104 v
->dithered_pf
= v
->undithered_pf
= PF_Index
;
1105 v
->mesa_visual
.indexBits
= GET_VISUAL_DEPTH(v
);
1109 * We support RGB rendering into almost any kind of visual.
1111 const int xclass
= v
->mesa_visual
.visualType
;
1112 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
1113 setup_truecolor( v
, b
, cmap
);
1115 else if (xclass
== GLX_STATIC_GRAY
&& GET_VISUAL_DEPTH(v
) == 1) {
1116 setup_monochrome( v
, b
);
1118 else if (xclass
== GLX_GRAY_SCALE
|| xclass
== GLX_STATIC_GRAY
) {
1119 if (!setup_grayscale( client
, v
, b
, cmap
)) {
1123 else if ((xclass
== GLX_PSEUDO_COLOR
|| xclass
== GLX_STATIC_COLOR
)
1124 && GET_VISUAL_DEPTH(v
)>=4 && GET_VISUAL_DEPTH(v
)<=16) {
1125 if (!setup_dithered_color( client
, v
, b
, cmap
)) {
1130 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.\n");
1133 v
->mesa_visual
.indexBits
= 0;
1135 if (_mesa_getenv("MESA_NO_DITHER")) {
1136 v
->dithered_pf
= v
->undithered_pf
;
1142 * If MESA_INFO env var is set print out some debugging info
1143 * which can help Brian figure out what's going on when a user
1146 if (_mesa_getenv("MESA_INFO")) {
1147 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
1148 _mesa_printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
1149 _mesa_printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
1150 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
1151 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
1152 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
1158 /* Do window-specific initializations */
1160 /* these should have been set in create_xmesa_buffer */
1161 ASSERT(b
->frontxrb
->drawable
== window
);
1162 ASSERT(b
->frontxrb
->pixmap
== (XMesaPixmap
) window
);
1164 /* Setup for single/double buffering */
1165 if (v
->mesa_visual
.doubleBufferMode
) {
1166 /* Double buffered */
1167 b
->shm
= check_for_xshm( v
->display
);
1170 /* X11 graphics contexts */
1171 #ifdef XFree86Server
1172 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
1174 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
1176 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
1178 /* cleargc - for glClear() */
1179 #ifdef XFree86Server
1180 b
->cleargc
= CreateScratchGC(v
->display
, window
->depth
);
1182 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
1184 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
1187 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
1188 * Patch contributed by Michael Pichler May 15, 1995.
1190 #ifdef XFree86Server
1191 b
->swapgc
= CreateScratchGC(v
->display
, window
->depth
);
1195 dixChangeGC(NullClient
, b
->swapgc
, GCGraphicsExposures
, v
, NULL
);
1200 gcvalues
.graphics_exposures
= False
;
1201 b
->swapgc
= XCreateGC(v
->display
, window
,
1202 GCGraphicsExposures
, &gcvalues
);
1205 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
1207 * Set fill style and tile pixmap once for all for HPCR stuff
1208 * (instead of doing it each time in clear_color_HPCR_pixmap())
1209 * Initialize whole stuff
1210 * Patch contributed by Jacques Leroy March 8, 1998.
1212 if (v
->hpcr_clear_flag
&& b
->backxrb
&& b
->backxrb
->pixmap
) {
1214 for (i
= 0; i
< 16; i
++) {
1215 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 0, 0);
1216 XMesaPutPixel(v
->hpcr_clear_ximage
, i
, 1, 0);
1218 XMesaPutImage(b
->display
, (XMesaDrawable
) v
->hpcr_clear_pixmap
,
1219 b
->cleargc
, v
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
1220 XMesaSetFillStyle( v
->display
, b
->cleargc
, FillTiled
);
1221 XMesaSetTile( v
->display
, b
->cleargc
, v
->hpcr_clear_pixmap
);
1224 /* Initialize the row buffer XImage for use in write_color_span() */
1225 data
= (char*) MALLOC(MAX_WIDTH
*4);
1226 #ifdef XFree86Server
1227 b
->rowimage
= XMesaCreateImage(GET_VISUAL_DEPTH(v
), MAX_WIDTH
, 1, data
);
1229 b
->rowimage
= XCreateImage( v
->display
,
1232 ZPixmap
, 0, /*format, offset*/
1234 MAX_WIDTH
, 1, /*width, height*/
1236 0 /*bytes_per_line*/ );
1248 * Convert an RGBA color to a pixel value.
1251 xmesa_color_to_pixel(GLcontext
*ctx
,
1252 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
1255 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1256 switch (pixelFormat
) {
1262 PACK_TRUECOLOR( p
, r
, g
, b
);
1266 return PACK_8A8B8G8R( r
, g
, b
, a
);
1268 return PACK_8A8R8G8B( r
, g
, b
, a
);
1272 return PACK_8R8G8B( r
, g
, b
);
1274 return PACK_5R6G5B( r
, g
, b
);
1278 return DITHER( 1, 0, r
, g
, b
);
1281 /* 382 = (3*255)/2 */
1282 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
1284 return DITHER_HPCR(1, 1, r
, g
, b
);
1288 return LOOKUP( r
, g
, b
);
1291 return GRAY_RGB( r
, g
, b
);
1292 case PF_Dither_True
:
1294 case PF_Dither_5R6G5B
:
1297 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
1301 _mesa_problem(ctx
, "Bad pixel format in xmesa_color_to_pixel");
1307 #define NUM_VISUAL_TYPES 6
1310 * Convert an X visual type to a GLX visual type.
1312 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
1314 * \return If \c visualType is a valid X visual type, a GLX visual type will
1315 * be returned. Otherwise \c GLX_NONE will be returned.
1318 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
1322 xmesa_convert_from_x_visual_type( int visualType
)
1324 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
1325 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
1326 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
1327 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
1330 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
1331 ? glx_visual_types
[ visualType
] : GLX_NONE
;
1335 /**********************************************************************/
1336 /***** Public Functions *****/
1337 /**********************************************************************/
1341 * Create a new X/Mesa visual.
1342 * Input: display - X11 display
1343 * visinfo - an XVisualInfo pointer
1344 * rgb_flag - GL_TRUE = RGB mode,
1345 * GL_FALSE = color index mode
1346 * alpha_flag - alpha buffer requested?
1347 * db_flag - GL_TRUE = double-buffered,
1348 * GL_FALSE = single buffered
1349 * stereo_flag - stereo visual?
1350 * ximage_flag - GL_TRUE = use an XImage for back buffer,
1351 * GL_FALSE = use an off-screen pixmap for back buffer
1352 * depth_size - requested bits/depth values, or zero
1353 * stencil_size - requested bits/stencil values, or zero
1354 * accum_red_size - requested bits/red accum values, or zero
1355 * accum_green_size - requested bits/green accum values, or zero
1356 * accum_blue_size - requested bits/blue accum values, or zero
1357 * accum_alpha_size - requested bits/alpha accum values, or zero
1358 * num_samples - number of samples/pixel if multisampling, or zero
1359 * level - visual level, usually 0
1360 * visualCaveat - ala the GLX extension, usually GLX_NONE
1361 * Return; a new XMesaVisual or 0 if error.
1364 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
1365 XMesaVisualInfo visinfo
,
1367 GLboolean alpha_flag
,
1369 GLboolean stereo_flag
,
1370 GLboolean ximage_flag
,
1373 GLint accum_red_size
,
1374 GLint accum_green_size
,
1375 GLint accum_blue_size
,
1376 GLint accum_alpha_size
,
1379 GLint visualCaveat
)
1383 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
1385 #ifndef XFree86Server
1386 /* For debugging only */
1387 if (_mesa_getenv("MESA_XSYNC")) {
1388 /* This makes debugging X easier.
1389 * In your debugger, set a breakpoint on _XError to stop when an
1390 * X protocol error is generated.
1392 XSynchronize( display
, 1 );
1396 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
1401 v
->display
= display
;
1403 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
1404 * the struct but we may need some of the information contained in it
1407 #ifndef XFree86Server
1408 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
1413 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
1416 /* check for MESA_GAMMA environment variable */
1417 gamma
= _mesa_getenv("MESA_GAMMA");
1419 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
1420 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
1421 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
1422 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
1423 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
1426 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
1429 v
->ximage_flag
= ximage_flag
;
1431 #ifdef XFree86Server
1432 /* We could calculate these values by ourselves. nplanes is either the sum
1433 * of the red, green, and blue bits or the number index bits.
1434 * ColormapEntries is either (1U << index_bits) or
1435 * (1U << max(redBits, greenBits, blueBits)).
1437 assert(visinfo
->nplanes
> 0);
1438 v
->nplanes
= visinfo
->nplanes
;
1439 v
->ColormapEntries
= visinfo
->ColormapEntries
;
1441 v
->mesa_visual
.redMask
= visinfo
->redMask
;
1442 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
1443 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
1444 v
->mesa_visual
.visualID
= visinfo
->vid
;
1445 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
1447 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
1448 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
1449 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
1450 v
->mesa_visual
.visualID
= visinfo
->visualid
;
1451 v
->mesa_visual
.screen
= visinfo
->screen
;
1454 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
1455 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
1457 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
1460 v
->mesa_visual
.visualRating
= visualCaveat
;
1463 v
->mesa_visual
.alphaBits
= 8;
1465 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
1468 const int xclass
= v
->mesa_visual
.visualType
;
1469 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
1470 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
1471 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
1472 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
1475 /* this is an approximation */
1477 depth
= GET_VISUAL_DEPTH(v
);
1478 red_bits
= depth
/ 3;
1480 green_bits
= depth
/ 2;
1481 depth
-= green_bits
;
1484 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
1486 alpha_bits
= v
->mesa_visual
.alphaBits
;
1489 _mesa_initialize_visual( &v
->mesa_visual
,
1490 rgb_flag
, db_flag
, stereo_flag
,
1491 red_bits
, green_bits
,
1492 blue_bits
, alpha_bits
,
1493 v
->mesa_visual
.indexBits
,
1496 accum_red_size
, accum_green_size
,
1497 accum_blue_size
, accum_alpha_size
,
1500 /* XXX minor hack */
1501 v
->mesa_visual
.level
= level
;
1507 void XMesaDestroyVisual( XMesaVisual v
)
1509 #ifndef XFree86Server
1510 _mesa_free(v
->visinfo
);
1518 * Create a new XMesaContext.
1519 * \param v the XMesaVisual
1520 * \param share_list another XMesaContext with which to share display
1521 * lists or NULL if no sharing is wanted.
1522 * \return an XMesaContext or NULL if error.
1525 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
1527 static GLboolean firstTime
= GL_TRUE
;
1530 struct dd_function_table functions
;
1534 _glthread_INIT_MUTEX(_xmesa_lock
);
1535 firstTime
= GL_FALSE
;
1538 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
1539 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
1543 mesaCtx
= &(c
->mesa
);
1545 /* initialize with default driver functions, then plug in XMesa funcs */
1546 _mesa_init_driver_functions(&functions
);
1547 xmesa_init_driver_functions(v
, &functions
);
1548 st_init_driver_functions(&functions
);
1551 functions.NewRenderbuffer = xmesa_new_renderbuffer;
1554 if (!_mesa_initialize_context(mesaCtx
, &v
->mesa_visual
,
1555 share_list
? &(share_list
->mesa
) : (GLcontext
*) NULL
,
1556 &functions
, (void *) c
)) {
1561 _mesa_enable_sw_extensions(mesaCtx
);
1562 _mesa_enable_1_3_extensions(mesaCtx
);
1563 _mesa_enable_1_4_extensions(mesaCtx
);
1564 _mesa_enable_1_5_extensions(mesaCtx
);
1565 _mesa_enable_2_0_extensions(mesaCtx
);
1566 #if ENABLE_EXT_texure_compression_s3tc
1568 _mesa_enable_extension(mesaCtx
, "GL_EXT_texture_compression_s3tc");
1569 _mesa_enable_extension(mesaCtx
, "GL_S3_s3tc");
1571 _mesa_enable_extension(mesaCtx
, "GL_3DFX_texture_compression_FXT1");
1573 #if ENABLE_EXT_timer_query
1574 _mesa_enable_extension(mesaCtx
, "GL_EXT_timer_query");
1577 #ifdef XFree86Server
1578 /* If we're running in the X server, do bounds checking to prevent
1579 * segfaults and server crashes!
1581 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
1584 /* finish up xmesa context initializations */
1585 c
->swapbytes
= CHECK_BYTE_ORDER(v
) ? GL_FALSE
: GL_TRUE
;
1587 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
1588 c
->display
= v
->display
;
1589 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
1591 /* Initialize the software rasterizer and helper modules.
1593 if (!_swrast_CreateContext( mesaCtx
) ||
1594 !_vbo_CreateContext( mesaCtx
) ||
1595 !_tnl_CreateContext( mesaCtx
) ||
1596 !_swsetup_CreateContext( mesaCtx
)) {
1597 _mesa_free_context_data(&c
->mesa
);
1603 tnl
= TNL_CONTEXT(mesaCtx
);
1604 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1606 xmesa_register_swrast_functions( mesaCtx
);
1607 _swsetup_Wakeup(mesaCtx
);
1610 st_create_context( mesaCtx
,
1611 xmesa_create_softpipe( c
) );
1613 /* override these functions, as if the xlib driver were derived from
1614 * the softpipe driver.
1617 mesaCtx
->st
->pipe
->surface_alloc
= xmesa_surface_alloc
;
1619 mesaCtx
->st
->pipe
->supported_formats
= xmesa_supported_formats
;
1620 mesaCtx
->st
->pipe
->get_tile_rgba
= xmesa_get_tile_rgba
;
1621 mesaCtx
->st
->pipe
->put_tile_rgba
= xmesa_put_tile_rgba
;
1623 mesaCtx
->st
->haveFramebufferRegions
= GL_FALSE
;
1625 /* special pipe->clear function */
1626 mesaCtx
->st
->pipe
->clear
= xmesa_clear
;
1634 void XMesaDestroyContext( XMesaContext c
)
1636 GLcontext
*mesaCtx
= &c
->mesa
;
1639 FXdestroyContext( XMESA_BUFFER(mesaCtx
->DrawBuffer
) );
1642 _swsetup_DestroyContext( mesaCtx
);
1643 _swrast_DestroyContext( mesaCtx
);
1644 _tnl_DestroyContext( mesaCtx
);
1645 _vbo_DestroyContext( mesaCtx
);
1646 _mesa_free_context_data( mesaCtx
);
1653 * Private function for creating an XMesaBuffer which corresponds to an
1654 * X window or pixmap.
1655 * \param v the window's XMesaVisual
1656 * \param w the window we're wrapping
1657 * \return new XMesaBuffer or NULL if error
1660 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
1662 #ifndef XFree86Server
1663 XWindowAttributes attr
;
1672 /* Check that window depth matches visual depth */
1673 #ifdef XFree86Server
1674 depth
= ((XMesaDrawable
)w
)->depth
;
1676 XGetWindowAttributes( v
->display
, w
, &attr
);
1679 if (GET_VISUAL_DEPTH(v
) != depth
) {
1680 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1681 GET_VISUAL_DEPTH(v
), depth
);
1686 #ifdef XFree86Server
1687 cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
1689 if (attr
.colormap
) {
1690 cmap
= attr
.colormap
;
1693 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1694 /* this is weird, a window w/out a colormap!? */
1695 /* OK, let's just allocate a new one and hope for the best */
1696 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1700 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
1704 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
1705 (XMesaDrawable
) w
, cmap
)) {
1706 xmesa_free_buffer(b
);
1716 * Create a new XMesaBuffer from an X pixmap.
1718 * \param v the XMesaVisual
1719 * \param p the pixmap
1720 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1721 * \c GLX_DIRECT_COLOR visual for the pixmap
1722 * \returns new XMesaBuffer or NULL if error
1725 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
1731 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1735 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1736 (XMesaDrawable
) p
, cmap
)) {
1737 xmesa_free_buffer(b
);
1746 * For GLX_EXT_texture_from_pixmap
1749 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
1751 int format
, int target
, int mipmap
)
1753 GET_CURRENT_CONTEXT(ctx
);
1755 GLuint width
, height
;
1759 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1763 /* get pixmap size, update framebuffer/renderbuffer dims */
1764 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
1765 _mesa_resize_framebuffer(NULL
, &(b
->mesa_buffer
), width
, height
);
1769 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1770 target
= GLX_TEXTURE_2D_EXT
;
1772 else if ( _mesa_bitcount(width
) == 1
1773 && _mesa_bitcount(height
) == 1) {
1774 /* power of two size */
1776 target
= GLX_TEXTURE_1D_EXT
;
1779 target
= GLX_TEXTURE_2D_EXT
;
1782 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1783 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1786 /* non power of two textures not supported */
1787 XMesaDestroyBuffer(b
);
1792 b
->TextureTarget
= target
;
1793 b
->TextureFormat
= format
;
1794 b
->TextureMipmap
= mipmap
;
1796 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1797 (XMesaDrawable
) p
, cmap
)) {
1798 xmesa_free_buffer(b
);
1808 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1809 unsigned int width
, unsigned int height
)
1811 #ifndef XFree86Server
1813 XMesaDrawable drawable
; /* X Pixmap Drawable */
1816 /* allocate pixmap for front buffer */
1817 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1818 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1823 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1827 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1829 xmesa_free_buffer(b
);
1842 * Deallocate an XMesaBuffer structure and all related info.
1845 XMesaDestroyBuffer(XMesaBuffer b
)
1847 xmesa_free_buffer(b
);
1852 * Query the current window size and update the corresponding GLframebuffer
1853 * and all attached renderbuffers.
1855 * 1. the first time a buffer is bound to a context.
1856 * 2. from glViewport to poll for window size changes
1857 * 3. from the XMesaResizeBuffers() API function.
1858 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1859 * when resizing a buffer when no rendering context is bound.
1862 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1864 GLuint width
, height
;
1865 xmesa_get_window_size(drawBuffer
->display
, drawBuffer
, &width
, &height
);
1866 if (drawBuffer
->mesa_buffer
.Width
!= width
||
1867 drawBuffer
->mesa_buffer
.Height
!= height
) {
1868 GLcontext
*ctx
= xmctx
? &xmctx
->mesa
: NULL
;
1869 _mesa_resize_framebuffer(ctx
, &(drawBuffer
->mesa_buffer
), width
, height
);
1871 drawBuffer
->mesa_buffer
.Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1876 * Bind buffer b to context c and make c the current rendering context.
1878 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1880 return XMesaMakeCurrent2( c
, b
, b
);
1885 * Bind buffer b to context c and make c the current rendering context.
1888 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1889 XMesaBuffer readBuffer
)
1892 if (!drawBuffer
|| !readBuffer
)
1893 return GL_FALSE
; /* must specify buffers! */
1895 if (&(c
->mesa
) == _mesa_get_current_context()
1896 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1897 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1898 && XMESA_BUFFER(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1899 /* same context and buffer, do nothing */
1903 c
->xm_buffer
= drawBuffer
;
1906 if (FXmakeCurrent( drawBuffer
))
1910 /* Call this periodically to detect when the user has begun using
1911 * GL rendering from multiple threads.
1913 _glapi_check_multithread();
1915 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1916 if (readBuffer
!= drawBuffer
)
1917 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1919 _mesa_make_current(&(c
->mesa
),
1920 &drawBuffer
->mesa_buffer
,
1921 &readBuffer
->mesa_buffer
);
1923 if (c
->xm_visual
->mesa_visual
.rgbMode
) {
1925 * Must recompute and set these pixel values because colormap
1926 * can be different for different windows.
1928 c
->clearpixel
= xmesa_color_to_pixel( &c
->mesa
,
1933 c
->xm_visual
->undithered_pf
);
1934 XMesaSetForeground(c
->display
, drawBuffer
->cleargc
, c
->clearpixel
);
1937 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1938 drawBuffer
->wasCurrent
= GL_TRUE
;
1942 _mesa_make_current( NULL
, NULL
, NULL
);
1949 * Unbind the context c from its buffer.
1951 GLboolean
XMesaUnbindContext( XMesaContext c
)
1953 /* A no-op for XFree86 integration purposes */
1958 XMesaContext
XMesaGetCurrentContext( void )
1960 GET_CURRENT_CONTEXT(ctx
);
1962 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1971 XMesaBuffer
XMesaGetCurrentBuffer( void )
1973 GET_CURRENT_CONTEXT(ctx
);
1975 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1984 /* New in Mesa 3.1 */
1985 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1987 GET_CURRENT_CONTEXT(ctx
);
1989 return XMESA_BUFFER(ctx
->ReadBuffer
);
1997 #ifdef XFree86Server
1999 GLboolean
XMesaForceCurrent(XMesaContext c
)
2002 _glapi_set_dispatch(c
->mesa
.CurrentDispatch
);
2004 if (&(c
->mesa
) != _mesa_get_current_context()) {
2005 _mesa_make_current(&c
->mesa
, c
->mesa
.DrawBuffer
, c
->mesa
.ReadBuffer
);
2009 _mesa_make_current(NULL
, NULL
, NULL
);
2016 GLboolean
XMesaLoseCurrent(XMesaContext c
)
2019 _mesa_make_current(NULL
, NULL
, NULL
);
2025 GLboolean
XMesaCopyContext( XMesaContext xm_src
, XMesaContext xm_dst
, GLuint mask
)
2027 _mesa_copy_context(&xm_src
->mesa
, &xm_dst
->mesa
, mask
);
2030 #endif /* XFree86Server */
2034 GLboolean
XMesaSetFXmode( GLint mode
)
2044 * Copy the back buffer to the front buffer. If there's no back buffer
2048 void XMesaSwapBuffers( XMesaBuffer b
)
2050 GET_CURRENT_CONTEXT(ctx
);
2053 /* single buffered */
2057 /* If we're swapping the buffer associated with the current context
2058 * we have to flush any pending rendering commands first.
2060 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2061 _mesa_notifySwapBuffers(ctx
);
2065 if (FXswapBuffers(b
))
2068 if (b
->backxrb
->ximage
) {
2069 /* Copy Ximage (back buf) from client memory to server window */
2070 #if defined(USE_XSHM) && !defined(XFree86Server)
2072 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2073 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2075 b
->backxrb
->ximage
, 0, 0,
2076 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2078 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2083 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2084 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2086 b
->backxrb
->ximage
, 0, 0,
2087 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
2088 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2091 else if (b
->backxrb
->pixmap
) {
2092 /* Copy pixmap (back buf) to window (front buf) on server */
2093 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
2094 XMesaCopyArea( b
->xm_visual
->display
,
2095 b
->backxrb
->pixmap
, /* source drawable */
2096 b
->frontxrb
->drawable
, /* dest. drawable */
2098 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
2099 0, 0 /* dest region */
2101 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
2105 _mesa_copy_soft_alpha_renderbuffers(ctx
, &b
->mesa_buffer
);
2107 #if !defined(XFree86Server)
2108 XSync( b
->xm_visual
->display
, False
);
2115 * Copy sub-region of back buffer to front buffer
2117 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
2119 GET_CURRENT_CONTEXT(ctx
);
2121 /* If we're swapping the buffer associated with the current context
2122 * we have to flush any pending rendering commands first.
2124 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
2125 _mesa_notifySwapBuffers(ctx
);
2128 /* single buffered */
2133 int yTop
= b
->mesa_buffer
.Height
- y
- height
;
2135 if (FXswapBuffers(b
))
2138 if (b
->backxrb
->ximage
) {
2139 /* Copy Ximage from host's memory to server's window */
2140 #if defined(USE_XSHM) && !defined(XFree86Server)
2142 /* XXX assuming width and height aren't too large! */
2143 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2145 b
->backxrb
->ximage
, x
, yTop
,
2146 x
, yTop
, width
, height
, False
);
2147 /* wait for finished event??? */
2152 /* XXX assuming width and height aren't too large! */
2153 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
2155 b
->backxrb
->ximage
, x
, yTop
,
2156 x
, yTop
, width
, height
);
2160 /* Copy pixmap to window on server */
2161 XMesaCopyArea( b
->xm_visual
->display
,
2162 b
->backxrb
->pixmap
, /* source drawable */
2163 b
->frontxrb
->drawable
, /* dest. drawable */
2165 x
, yTop
, width
, height
, /* source region */
2166 x
, yTop
/* dest region */
2174 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
2175 * is a way to get "under the hood" of X/Mesa so one can manipulate the
2176 * back buffer directly.
2177 * Output: pixmap - pointer to back buffer's Pixmap, or 0
2178 * ximage - pointer to back buffer's XImage, or NULL
2179 * Return: GL_TRUE = context is double buffered
2180 * GL_FALSE = context is single buffered
2182 #ifndef XFree86Server
2183 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
2184 XMesaPixmap
*pixmap
,
2185 XMesaImage
**ximage
)
2189 *pixmap
= b
->backxrb
->pixmap
;
2191 *ximage
= b
->backxrb
->ximage
;
2200 #endif /* XFree86Server */
2204 * Return the depth buffer associated with an XMesaBuffer.
2205 * Input: b - the XMesa buffer handle
2206 * Output: width, height - size of buffer in pixels
2207 * bytesPerValue - bytes per depth value (2 or 4)
2208 * buffer - pointer to depth buffer values
2209 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
2211 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
2212 GLint
*bytesPerValue
, void **buffer
)
2214 struct gl_renderbuffer
*rb
2215 = b
->mesa_buffer
.Attachment
[BUFFER_DEPTH
].Renderbuffer
;
2216 if (!rb
|| !rb
->Data
) {
2224 *width
= b
->mesa_buffer
.Width
;
2225 *height
= b
->mesa_buffer
.Height
;
2226 *bytesPerValue
= b
->mesa_buffer
.Visual
.depthBits
<= 16
2227 ? sizeof(GLushort
) : sizeof(GLuint
);
2234 void XMesaFlush( XMesaContext c
)
2236 if (c
&& c
->xm_visual
) {
2237 #ifdef XFree86Server
2240 XSync( c
->xm_visual
->display
, False
);
2247 const char *XMesaGetString( XMesaContext c
, int name
)
2250 if (name
==XMESA_VERSION
) {
2253 else if (name
==XMESA_EXTENSIONS
) {
2263 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
2266 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
2267 if (b
->frontxrb
->drawable
== d
&& b
->display
== dpy
) {
2276 * Free/destroy all XMesaBuffers associated with given display.
2278 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
2280 XMesaBuffer b
, next
;
2281 for (b
= XMesaBufferList
; b
; b
= next
) {
2283 if (b
->display
== dpy
) {
2284 xmesa_free_buffer(b
);
2291 * Look for XMesaBuffers whose X window has been destroyed.
2292 * Deallocate any such XMesaBuffers.
2294 void XMesaGarbageCollect( void )
2296 XMesaBuffer b
, next
;
2297 for (b
=XMesaBufferList
; b
; b
=next
) {
2299 if (b
->display
&& b
->frontxrb
->drawable
&& b
->type
== WINDOW
) {
2300 #ifdef XFree86Server
2303 XSync(b
->display
, False
);
2304 if (!window_exists( b
->display
, b
->frontxrb
->drawable
)) {
2305 /* found a dead window, free the ancillary info */
2306 XMesaDestroyBuffer( b
);
2314 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
2315 GLfloat red
, GLfloat green
,
2316 GLfloat blue
, GLfloat alpha
)
2318 GLcontext
*ctx
= &xmesa
->mesa
;
2319 GLint r
= (GLint
) (red
* 255.0F
);
2320 GLint g
= (GLint
) (green
* 255.0F
);
2321 GLint b
= (GLint
) (blue
* 255.0F
);
2322 GLint a
= (GLint
) (alpha
* 255.0F
);
2324 switch (xmesa
->pixelformat
) {
2330 PACK_TRUECOLOR( p
, r
, g
, b
);
2334 return PACK_8A8B8G8R( r
, g
, b
, a
);
2336 return PACK_8A8R8G8B( r
, g
, b
, a
);
2338 return PACK_8R8G8B( r
, g
, b
);
2340 return PACK_5R6G5B( r
, g
, b
);
2344 return DITHER( x
, y
, r
, g
, b
);
2347 /* 382 = (3*255)/2 */
2348 return ((r
+g
+b
) > 382) ^ xmesa
->xm_visual
->bitFlip
;
2350 return DITHER_HPCR(x
, y
, r
, g
, b
);
2354 return LOOKUP( r
, g
, b
);
2357 return GRAY_RGB( r
, g
, b
);
2358 case PF_Dither_5R6G5B
:
2360 case PF_Dither_True
:
2363 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
2367 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
2374 * This is typically called when the window size changes and we need
2375 * to reallocate the buffer's back/depth/stencil/accum buffers.
2378 XMesaResizeBuffers( XMesaBuffer b
)
2380 GET_CURRENT_CONTEXT(ctx
);
2381 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
2384 xmesa_check_and_update_buffer_size(xmctx
, b
);
2389 xbuffer_to_renderbuffer(int buffer
)
2391 assert(MAX_AUX_BUFFERS
<= 4);
2394 case GLX_FRONT_LEFT_EXT
:
2395 return BUFFER_FRONT_LEFT
;
2396 case GLX_FRONT_RIGHT_EXT
:
2397 return BUFFER_FRONT_RIGHT
;
2398 case GLX_BACK_LEFT_EXT
:
2399 return BUFFER_BACK_LEFT
;
2400 case GLX_BACK_RIGHT_EXT
:
2401 return BUFFER_BACK_RIGHT
;
2417 /* BadValue error */
2424 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
2425 const int *attrib_list
)
2428 GET_CURRENT_CONTEXT(ctx
);
2429 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
2430 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
2431 struct gl_texture_object
*texObj
;
2433 struct gl_renderbuffer
*rb
;
2434 struct xmesa_renderbuffer
*xrb
;
2436 XMesaImage
*img
= NULL
;
2437 GLboolean freeImg
= GL_FALSE
;
2439 b
= xbuffer_to_renderbuffer(buffer
);
2443 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_NONE_EXT
)
2444 return; /* BadMatch error */
2446 rb
= drawable
->mesa_buffer
.Attachment
[b
].Renderbuffer
;
2448 /* invalid buffer */
2451 xrb
= xmesa_renderbuffer(rb
);
2454 switch (drawable
->TextureTarget
) {
2455 case GLX_TEXTURE_1D_EXT
:
2456 texObj
= texUnit
->Current1D
;
2458 case GLX_TEXTURE_2D_EXT
:
2459 texObj
= texUnit
->Current2D
;
2461 case GLX_TEXTURE_RECTANGLE_EXT
:
2462 texObj
= texUnit
->CurrentRect
;
2465 return; /* BadMatch error */
2470 * The following is a quick and simple way to implement
2471 * BindTexImage. The better way is to write some new FetchTexel()
2472 * functions which would extract texels from XImages. We'd still
2473 * need to use GetImage when texturing from a Pixmap (front buffer)
2474 * but texturing from a back buffer (XImage) would avoid an image
2480 img
= XMesaGetImage(dpy
, xrb
->pixmap
, 0, 0, rb
->Width
, rb
->Height
, ~0L,
2484 else if (xrb
->ximage
) {
2488 /* store the XImage as a new texture image */
2490 GLenum format
, type
, intFormat
;
2491 if (img
->bits_per_pixel
== 32) {
2493 type
= GL_UNSIGNED_BYTE
;
2494 intFormat
= GL_RGBA
;
2496 else if (img
->bits_per_pixel
== 24) {
2498 type
= GL_UNSIGNED_BYTE
;
2501 else if (img
->bits_per_pixel
== 16) {
2503 type
= GL_UNSIGNED_SHORT_5_6_5
;
2507 _mesa_problem(NULL
, "Unexpected XImage format in XMesaBindTexImage");
2510 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGBA_EXT
) {
2511 intFormat
= GL_RGBA
;
2513 else if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGB_EXT
) {
2517 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, intFormat
, rb
->Width
, rb
->Height
, 0,
2518 format
, type
, img
->data
);
2521 XMesaDestroyImage(img
);
2529 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)
2531 const GLint b
= xbuffer_to_renderbuffer(buffer
);