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"
67 #include "main/api_exec.h"
68 #include "main/context.h"
69 #include "main/extensions.h"
70 #include "main/framebuffer.h"
71 #include "main/imports.h"
72 #include "main/macros.h"
73 #include "main/renderbuffer.h"
74 #include "main/teximage.h"
75 #include "main/version.h"
76 #include "main/vtxfmt.h"
77 #include "glapi/glthread.h"
78 #include "swrast/swrast.h"
79 #include "swrast/s_renderbuffer.h"
80 #include "swrast_setup/swrast_setup.h"
83 #include "tnl/t_context.h"
84 #include "tnl/t_pipeline.h"
85 #include "drivers/common/driverfuncs.h"
86 #include "drivers/common/meta.h"
89 * Global X driver lock
91 _glthread_Mutex _xmesa_lock
;
95 /**********************************************************************/
96 /***** X Utility Functions *****/
97 /**********************************************************************/
101 * Return the host's byte order as LSBFirst or MSBFirst ala X.
103 static int host_byte_order( void )
106 char *cptr
= (char *) &i
;
107 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
112 * Check if the X Shared Memory extension is available.
113 * Return: 0 = not available
114 * 1 = shared XImage support available
115 * 2 = shared Pixmap support available also
117 static int check_for_xshm( XMesaDisplay
*display
)
119 #if defined(USE_XSHM)
122 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
123 /* Note: we're no longer calling XShmQueryVersion() here. It seems
124 * to be flakey (triggers a spurious X protocol error when we close
125 * one display connection and start using a new one. XShm has been
126 * around a long time and hasn't changed so if MIT_SHM is supported
127 * we assume we're good to go.
135 /* No XSHM support */
142 * Apply gamma correction to an intensity value in [0..max]. Return the
143 * new intensity value.
146 gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
152 double x
= (double) value
/ (double) max
;
153 return IROUND_POS((GLfloat
) max
* pow(x
, 1.0F
/gamma
));
160 * Return the true number of bits per pixel for XImages.
161 * For example, if we request a 24-bit deep visual we may actually need/get
162 * 32bpp XImages. This function returns the appropriate bpp.
163 * Input: dpy - the X display
164 * visinfo - desribes the visual to be used for XImages
165 * Return: true number of bits per pixel for XImages
168 bits_per_pixel( XMesaVisual xmv
)
170 XMesaDisplay
*dpy
= xmv
->display
;
171 XMesaVisualInfo visinfo
= xmv
->visinfo
;
174 /* Create a temporary XImage */
175 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
176 ZPixmap
, 0, /*format, offset*/
178 1, 1, /*width, height*/
183 /* grab the bits/pixel value */
184 bitsPerPixel
= img
->bits_per_pixel
;
185 /* free the XImage */
188 XMesaDestroyImage( img
);
195 * Determine if a given X window ID is valid (window exists).
196 * Do this by calling XGetWindowAttributes() for the window and
197 * checking if we catch an X error.
198 * Input: dpy - the display
199 * win - the window to check for existance
200 * Return: GL_TRUE - window exists
201 * GL_FALSE - window doesn't exist
203 static GLboolean WindowExistsFlag
;
205 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
208 if (xerr
->error_code
== BadWindow
) {
209 WindowExistsFlag
= GL_FALSE
;
214 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
216 XWindowAttributes wa
;
217 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
218 WindowExistsFlag
= GL_TRUE
;
219 old_handler
= XSetErrorHandler(window_exists_err_handler
);
220 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
221 XSetErrorHandler(old_handler
);
222 return WindowExistsFlag
;
226 get_drawable_size( XMesaDisplay
*dpy
, Drawable d
, GLuint
*width
, GLuint
*height
)
231 unsigned int w
, h
, bw
, depth
;
232 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
240 * Return the size of the window (or pixmap) that corresponds to the
242 * \param width returns width in pixels
243 * \param height returns height in pixels
246 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
247 GLuint
*width
, GLuint
*height
)
251 _glthread_LOCK_MUTEX(_xmesa_lock
);
252 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
253 stat
= get_drawable_size(dpy
, b
->frontxrb
->pixmap
, width
, height
);
254 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
257 /* probably querying a window that's recently been destroyed */
258 _mesa_warning(NULL
, "XGetGeometry failed!\n");
259 *width
= *height
= 1;
265 /**********************************************************************/
266 /***** Linked list of XMesaBuffers *****/
267 /**********************************************************************/
269 XMesaBuffer XMesaBufferList
= NULL
;
273 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
274 * Note that XMesaBuffer is derived from struct gl_framebuffer.
275 * The new XMesaBuffer will not have any size (Width=Height=0).
277 * \param d the corresponding X drawable (window or pixmap)
278 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
279 * \param vis the buffer's visual
280 * \param cmap the window's colormap, if known.
281 * \return new XMesaBuffer or NULL if any problem
284 create_xmesa_buffer(XMesaDrawable d
, BufferType type
,
285 XMesaVisual vis
, XMesaColormap cmap
)
289 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
291 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
295 b
->display
= vis
->display
;
300 _mesa_initialize_window_framebuffer(&b
->mesa_buffer
, &vis
->mesa_visual
);
301 b
->mesa_buffer
.Delete
= xmesa_delete_framebuffer
;
306 b
->frontxrb
= xmesa_new_renderbuffer(NULL
, 0, vis
, GL_FALSE
);
311 b
->frontxrb
->Parent
= b
;
312 b
->frontxrb
->drawable
= d
;
313 b
->frontxrb
->pixmap
= (XMesaPixmap
) d
;
314 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_FRONT_LEFT
,
315 &b
->frontxrb
->Base
.Base
);
320 if (vis
->mesa_visual
.doubleBufferMode
) {
321 b
->backxrb
= xmesa_new_renderbuffer(NULL
, 0, vis
, GL_TRUE
);
323 /* XXX free front xrb too */
327 b
->backxrb
->Parent
= b
;
328 /* determine back buffer implementation */
329 b
->db_mode
= vis
->ximage_flag
? BACK_XIMAGE
: BACK_PIXMAP
;
331 _mesa_add_renderbuffer(&b
->mesa_buffer
, BUFFER_BACK_LEFT
,
332 &b
->backxrb
->Base
.Base
);
336 * Other renderbuffer (depth, stencil, etc)
338 _swrast_add_soft_renderbuffers(&b
->mesa_buffer
,
339 GL_FALSE
, /* color */
340 vis
->mesa_visual
.haveDepthBuffer
,
341 vis
->mesa_visual
.haveStencilBuffer
,
342 vis
->mesa_visual
.haveAccumBuffer
,
343 GL_FALSE
, /* software alpha buffer */
344 vis
->mesa_visual
.numAuxBuffers
> 0 );
346 /* GLX_EXT_texture_from_pixmap */
347 b
->TextureTarget
= 0;
348 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
349 b
->TextureMipmap
= 0;
351 /* insert buffer into linked list */
352 b
->Next
= XMesaBufferList
;
360 * Find an XMesaBuffer by matching X display and colormap but NOT matching
361 * the notThis buffer.
364 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
367 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
368 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
377 * Remove buffer from linked list, delete if no longer referenced.
380 xmesa_free_buffer(XMesaBuffer buffer
)
382 XMesaBuffer prev
= NULL
, b
;
384 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
386 struct gl_framebuffer
*fb
= &buffer
->mesa_buffer
;
388 /* unlink buffer from list */
390 prev
->Next
= buffer
->Next
;
392 XMesaBufferList
= buffer
->Next
;
394 /* mark as delete pending */
395 fb
->DeletePending
= GL_TRUE
;
397 /* Since the X window for the XMesaBuffer is going away, we don't
398 * want to dereference this pointer in the future.
400 b
->frontxrb
->drawable
= 0;
402 /* Unreference. If count = zero we'll really delete the buffer */
403 _mesa_reference_framebuffer(&fb
, NULL
);
407 /* continue search */
410 /* buffer not found in XMesaBufferList */
411 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
417 /**********************************************************************/
418 /***** Misc Private Functions *****/
419 /**********************************************************************/
423 * Setup RGB rendering for a window with a True/DirectColor visual.
426 setup_truecolor(XMesaVisual v
, XMesaBuffer buffer
, XMesaColormap cmap
)
428 unsigned long rmask
, gmask
, bmask
;
432 /* Compute red multiplier (mask) and bit shift */
434 rmask
= GET_REDMASK(v
);
435 while ((rmask
& 1)==0) {
440 /* Compute green multiplier (mask) and bit shift */
442 gmask
= GET_GREENMASK(v
);
443 while ((gmask
& 1)==0) {
448 /* Compute blue multiplier (mask) and bit shift */
450 bmask
= GET_BLUEMASK(v
);
451 while ((bmask
& 1)==0) {
457 * Compute component-to-pixel lookup tables and dithering kernel
460 static GLubyte kernel
[16] = {
461 0*16, 8*16, 2*16, 10*16,
462 12*16, 4*16, 14*16, 6*16,
463 3*16, 11*16, 1*16, 9*16,
464 15*16, 7*16, 13*16, 5*16,
466 GLint rBits
= _mesa_bitcount(rmask
);
467 GLint gBits
= _mesa_bitcount(gmask
);
468 GLint bBits
= _mesa_bitcount(bmask
);
472 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
473 for (i
=0; i
<=rmask
; i
++)
474 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
475 for (i
=0; i
<=gmask
; i
++)
476 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
477 for (i
=0; i
<=bmask
; i
++)
478 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
480 /* convert RGB values from [0,255] to pixel components */
482 for (i
=0;i
<256;i
++) {
483 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
484 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
485 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
486 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
487 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
488 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
490 /* overflow protection */
491 for (i
=256;i
<512;i
++) {
492 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
493 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
494 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
497 /* setup dithering kernel */
499 if (gBits
> maxBits
) maxBits
= gBits
;
500 if (bBits
> maxBits
) maxBits
= bBits
;
502 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
505 v
->undithered_pf
= PF_Truecolor
;
506 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
510 * Now check for TrueColor visuals which we can optimize.
512 if ( GET_REDMASK(v
) ==0x0000ff
513 && GET_GREENMASK(v
)==0x00ff00
514 && GET_BLUEMASK(v
) ==0xff0000
515 && CHECK_BYTE_ORDER(v
)
516 && v
->BitsPerPixel
==32
517 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
518 /* common 32 bpp config used on SGI, Sun */
519 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
; /* ABGR */
521 else if (GET_REDMASK(v
) == 0xff0000
522 && GET_GREENMASK(v
)== 0x00ff00
523 && GET_BLUEMASK(v
) == 0x0000ff
524 && CHECK_BYTE_ORDER(v
)
525 && v
->RedGamma
== 1.0 && v
->GreenGamma
== 1.0 && v
->BlueGamma
== 1.0){
526 if (v
->BitsPerPixel
==32) {
527 /* if 32 bpp, and visual indicates 8 bpp alpha channel */
528 if (GET_VISUAL_DEPTH(v
) == 32 && v
->mesa_visual
.alphaBits
== 8)
529 v
->undithered_pf
= v
->dithered_pf
= PF_8A8R8G8B
; /* ARGB */
531 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
; /* xRGB */
533 else if (v
->BitsPerPixel
== 24) {
534 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
; /* RGB */
537 else if (GET_REDMASK(v
) ==0xf800
538 && GET_GREENMASK(v
)==0x07e0
539 && GET_BLUEMASK(v
) ==0x001f
540 && CHECK_BYTE_ORDER(v
)
541 && v
->BitsPerPixel
==16
542 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
544 v
->undithered_pf
= PF_5R6G5B
;
545 v
->dithered_pf
= PF_Dither_5R6G5B
;
551 * When a context is bound for the first time, we can finally finish
552 * initializing the context's visual and buffer information.
553 * \param v the XMesaVisual to initialize
554 * \param b the XMesaBuffer to initialize (may be NULL)
555 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
556 * \param window the window/pixmap we're rendering into
557 * \param cmap the colormap associated with the window/pixmap
558 * \return GL_TRUE=success, GL_FALSE=failure
561 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
562 XMesaDrawable window
,
565 const int xclass
= v
->visualType
;
568 ASSERT(!b
|| b
->xm_visual
== v
);
570 /* Save true bits/pixel */
571 v
->BitsPerPixel
= bits_per_pixel(v
);
572 assert(v
->BitsPerPixel
> 0);
575 * We support RGB rendering into almost any kind of visual.
577 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
578 setup_truecolor( v
, b
, cmap
);
581 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.\n");
584 v
->mesa_visual
.indexBits
= 0;
586 if (_mesa_getenv("MESA_NO_DITHER")) {
587 v
->dithered_pf
= v
->undithered_pf
;
592 * If MESA_INFO env var is set print out some debugging info
593 * which can help Brian figure out what's going on when a user
596 if (_mesa_getenv("MESA_INFO")) {
597 printf("X/Mesa visual = %p\n", (void *) v
);
598 printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
599 printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
600 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
601 printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
602 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
606 /* Do window-specific initializations */
608 /* these should have been set in create_xmesa_buffer */
609 ASSERT(b
->frontxrb
->drawable
== window
);
610 ASSERT(b
->frontxrb
->pixmap
== (XMesaPixmap
) window
);
612 /* Setup for single/double buffering */
613 if (v
->mesa_visual
.doubleBufferMode
) {
614 /* Double buffered */
615 b
->shm
= check_for_xshm( v
->display
);
618 /* X11 graphics contexts */
619 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
620 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
622 /* cleargc - for glClear() */
623 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
624 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
627 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
628 * Patch contributed by Michael Pichler May 15, 1995.
632 gcvalues
.graphics_exposures
= False
;
633 b
->swapgc
= XCreateGC(v
->display
, window
,
634 GCGraphicsExposures
, &gcvalues
);
636 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
645 * Convert an RGBA color to a pixel value.
648 xmesa_color_to_pixel(struct gl_context
*ctx
,
649 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
652 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
653 switch (pixelFormat
) {
657 PACK_TRUECOLOR( p
, r
, g
, b
);
661 return PACK_8A8B8G8R( r
, g
, b
, a
);
663 return PACK_8A8R8G8B( r
, g
, b
, a
);
667 return PACK_8R8G8B( r
, g
, b
);
669 return PACK_5R6G5B( r
, g
, b
);
672 case PF_Dither_5R6G5B
:
675 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
679 _mesa_problem(ctx
, "Bad pixel format in xmesa_color_to_pixel");
685 #define NUM_VISUAL_TYPES 6
688 * Convert an X visual type to a GLX visual type.
690 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
692 * \return If \c visualType is a valid X visual type, a GLX visual type will
693 * be returned. Otherwise \c GLX_NONE will be returned.
696 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
700 xmesa_convert_from_x_visual_type( int visualType
)
702 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
703 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
704 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
705 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
708 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
709 ? glx_visual_types
[ visualType
] : GLX_NONE
;
713 /**********************************************************************/
714 /***** Public Functions *****/
715 /**********************************************************************/
719 * Create a new X/Mesa visual.
720 * Input: display - X11 display
721 * visinfo - an XVisualInfo pointer
722 * rgb_flag - GL_TRUE = RGB mode,
723 * GL_FALSE = color index mode
724 * alpha_flag - alpha buffer requested?
725 * db_flag - GL_TRUE = double-buffered,
726 * GL_FALSE = single buffered
727 * stereo_flag - stereo visual?
728 * ximage_flag - GL_TRUE = use an XImage for back buffer,
729 * GL_FALSE = use an off-screen pixmap for back buffer
730 * depth_size - requested bits/depth values, or zero
731 * stencil_size - requested bits/stencil values, or zero
732 * accum_red_size - requested bits/red accum values, or zero
733 * accum_green_size - requested bits/green accum values, or zero
734 * accum_blue_size - requested bits/blue accum values, or zero
735 * accum_alpha_size - requested bits/alpha accum values, or zero
736 * num_samples - number of samples/pixel if multisampling, or zero
737 * level - visual level, usually 0
738 * visualCaveat - ala the GLX extension, usually GLX_NONE
739 * Return; a new XMesaVisual or 0 if error.
742 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
743 XMesaVisualInfo visinfo
,
745 GLboolean alpha_flag
,
747 GLboolean stereo_flag
,
748 GLboolean ximage_flag
,
751 GLint accum_red_size
,
752 GLint accum_green_size
,
753 GLint accum_blue_size
,
754 GLint accum_alpha_size
,
761 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
763 /* For debugging only */
764 if (_mesa_getenv("MESA_XSYNC")) {
765 /* This makes debugging X easier.
766 * In your debugger, set a breakpoint on _XError to stop when an
767 * X protocol error is generated.
769 XSynchronize( display
, 1 );
772 /* Color-index rendering not supported. */
776 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
781 v
->display
= display
;
783 /* Save a copy of the XVisualInfo struct because the user may Xfree()
784 * the struct but we may need some of the information contained in it
787 v
->visinfo
= (XVisualInfo
*) malloc(sizeof(*visinfo
));
792 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
794 /* check for MESA_GAMMA environment variable */
795 gamma
= _mesa_getenv("MESA_GAMMA");
797 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
798 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
799 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
800 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
801 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
804 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
807 v
->ximage_flag
= ximage_flag
;
809 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
810 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
811 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
812 v
->visualID
= visinfo
->visualid
;
813 v
->screen
= visinfo
->screen
;
815 #if !(defined(__cplusplus) || defined(c_plusplus))
816 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
818 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
821 v
->mesa_visual
.visualRating
= visualCaveat
;
824 v
->mesa_visual
.alphaBits
= 8;
826 (void) initialize_visual_and_buffer( v
, NULL
, 0, 0 );
829 const int xclass
= v
->visualType
;
830 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
831 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
832 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
833 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
836 /* this is an approximation */
838 depth
= GET_VISUAL_DEPTH(v
);
839 red_bits
= depth
/ 3;
841 green_bits
= depth
/ 2;
845 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
847 alpha_bits
= v
->mesa_visual
.alphaBits
;
850 if (!_mesa_initialize_visual(&v
->mesa_visual
,
851 db_flag
, stereo_flag
,
852 red_bits
, green_bits
,
853 blue_bits
, alpha_bits
,
856 accum_red_size
, accum_green_size
,
857 accum_blue_size
, accum_alpha_size
,
864 v
->mesa_visual
.level
= level
;
870 void XMesaDestroyVisual( XMesaVisual v
)
879 * Create a new XMesaContext.
880 * \param v the XMesaVisual
881 * \param share_list another XMesaContext with which to share display
882 * lists or NULL if no sharing is wanted.
883 * \return an XMesaContext or NULL if error.
886 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
888 static GLboolean firstTime
= GL_TRUE
;
890 struct gl_context
*mesaCtx
;
891 struct dd_function_table functions
;
895 _glthread_INIT_MUTEX(_xmesa_lock
);
896 firstTime
= GL_FALSE
;
899 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
900 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
904 mesaCtx
= &(c
->mesa
);
906 /* initialize with default driver functions, then plug in XMesa funcs */
907 _mesa_init_driver_functions(&functions
);
908 xmesa_init_driver_functions(v
, &functions
);
909 if (!_mesa_initialize_context(mesaCtx
, API_OPENGL_COMPAT
, &v
->mesa_visual
,
910 share_list
? &(share_list
->mesa
) : (struct gl_context
*) NULL
,
916 /* Enable this to exercise fixed function -> shader translation
917 * with software rendering.
920 mesaCtx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
921 mesaCtx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
924 _mesa_enable_sw_extensions(mesaCtx
);
925 _mesa_enable_1_3_extensions(mesaCtx
);
926 _mesa_enable_1_4_extensions(mesaCtx
);
927 _mesa_enable_1_5_extensions(mesaCtx
);
928 _mesa_enable_2_0_extensions(mesaCtx
);
929 _mesa_enable_2_1_extensions(mesaCtx
);
930 if (mesaCtx
->Mesa_DXTn
) {
931 _mesa_enable_extension(mesaCtx
, "GL_EXT_texture_compression_s3tc");
932 _mesa_enable_extension(mesaCtx
, "GL_S3_s3tc");
934 _mesa_enable_extension(mesaCtx
, "GL_3DFX_texture_compression_FXT1");
935 #if ENABLE_EXT_timer_query
936 _mesa_enable_extension(mesaCtx
, "GL_EXT_timer_query");
940 /* finish up xmesa context initializations */
941 c
->swapbytes
= CHECK_BYTE_ORDER(v
) ? GL_FALSE
: GL_TRUE
;
943 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
944 c
->display
= v
->display
;
945 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
947 /* Initialize the software rasterizer and helper modules.
949 if (!_swrast_CreateContext( mesaCtx
) ||
950 !_vbo_CreateContext( mesaCtx
) ||
951 !_tnl_CreateContext( mesaCtx
) ||
952 !_swsetup_CreateContext( mesaCtx
)) {
953 _mesa_free_context_data(&c
->mesa
);
959 tnl
= TNL_CONTEXT(mesaCtx
);
960 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
962 xmesa_register_swrast_functions( mesaCtx
);
963 _swsetup_Wakeup(mesaCtx
);
965 _mesa_meta_init(mesaCtx
);
967 _mesa_compute_version(mesaCtx
);
969 /* Exec table initialization requires the version to be computed */
970 _mesa_initialize_exec_table(mesaCtx
);
971 _mesa_initialize_vbo_vtxfmt(mesaCtx
);
979 void XMesaDestroyContext( XMesaContext c
)
981 struct gl_context
*mesaCtx
= &c
->mesa
;
983 _mesa_meta_free( mesaCtx
);
985 _swsetup_DestroyContext( mesaCtx
);
986 _swrast_DestroyContext( mesaCtx
);
987 _tnl_DestroyContext( mesaCtx
);
988 _vbo_DestroyContext( mesaCtx
);
989 _mesa_free_context_data( mesaCtx
);
996 * Private function for creating an XMesaBuffer which corresponds to an
997 * X window or pixmap.
998 * \param v the window's XMesaVisual
999 * \param w the window we're wrapping
1000 * \return new XMesaBuffer or NULL if error
1003 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
1005 XWindowAttributes attr
;
1013 /* Check that window depth matches visual depth */
1014 XGetWindowAttributes( v
->display
, w
, &attr
);
1016 if (GET_VISUAL_DEPTH(v
) != depth
) {
1017 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1018 GET_VISUAL_DEPTH(v
), depth
);
1023 if (attr
.colormap
) {
1024 cmap
= attr
.colormap
;
1027 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1028 /* this is weird, a window w/out a colormap!? */
1029 /* OK, let's just allocate a new one and hope for the best */
1030 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1033 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
1037 if (!initialize_visual_and_buffer( v
, b
, (XMesaDrawable
) w
, cmap
)) {
1038 xmesa_free_buffer(b
);
1048 * Create a new XMesaBuffer from an X pixmap.
1050 * \param v the XMesaVisual
1051 * \param p the pixmap
1052 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1053 * \c GLX_DIRECT_COLOR visual for the pixmap
1054 * \returns new XMesaBuffer or NULL if error
1057 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
1063 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1067 if (!initialize_visual_and_buffer(v
, b
, (XMesaDrawable
) p
, cmap
)) {
1068 xmesa_free_buffer(b
);
1077 * For GLX_EXT_texture_from_pixmap
1080 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
1082 int format
, int target
, int mipmap
)
1084 GET_CURRENT_CONTEXT(ctx
);
1086 GLuint width
, height
;
1090 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1094 /* get pixmap size, update framebuffer/renderbuffer dims */
1095 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
1096 _mesa_resize_framebuffer(NULL
, &(b
->mesa_buffer
), width
, height
);
1100 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1101 target
= GLX_TEXTURE_2D_EXT
;
1103 else if ( _mesa_bitcount(width
) == 1
1104 && _mesa_bitcount(height
) == 1) {
1105 /* power of two size */
1107 target
= GLX_TEXTURE_1D_EXT
;
1110 target
= GLX_TEXTURE_2D_EXT
;
1113 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1114 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1117 /* non power of two textures not supported */
1118 XMesaDestroyBuffer(b
);
1123 b
->TextureTarget
= target
;
1124 b
->TextureFormat
= format
;
1125 b
->TextureMipmap
= mipmap
;
1127 if (!initialize_visual_and_buffer(v
, b
, (XMesaDrawable
) p
, cmap
)) {
1128 xmesa_free_buffer(b
);
1138 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1139 unsigned int width
, unsigned int height
)
1142 XMesaDrawable drawable
; /* X Pixmap Drawable */
1145 /* allocate pixmap for front buffer */
1146 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1147 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1152 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1156 if (!initialize_visual_and_buffer(v
, b
, drawable
, cmap
)) {
1157 xmesa_free_buffer(b
);
1167 * Deallocate an XMesaBuffer structure and all related info.
1170 XMesaDestroyBuffer(XMesaBuffer b
)
1172 xmesa_free_buffer(b
);
1177 * Query the current window size and update the corresponding struct gl_framebuffer
1178 * and all attached renderbuffers.
1180 * 1. the first time a buffer is bound to a context.
1181 * 2. from glViewport to poll for window size changes
1182 * 3. from the XMesaResizeBuffers() API function.
1183 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1184 * when resizing a buffer when no rendering context is bound.
1187 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1189 GLuint width
, height
;
1190 xmesa_get_window_size(drawBuffer
->display
, drawBuffer
, &width
, &height
);
1191 if (drawBuffer
->mesa_buffer
.Width
!= width
||
1192 drawBuffer
->mesa_buffer
.Height
!= height
) {
1193 struct gl_context
*ctx
= xmctx
? &xmctx
->mesa
: NULL
;
1194 _mesa_resize_framebuffer(ctx
, &(drawBuffer
->mesa_buffer
), width
, height
);
1196 drawBuffer
->mesa_buffer
.Initialized
= GL_TRUE
; /* XXX TEMPORARY? */
1201 * Bind buffer b to context c and make c the current rendering context.
1203 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1205 return XMesaMakeCurrent2( c
, b
, b
);
1210 * Bind buffer b to context c and make c the current rendering context.
1213 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1214 XMesaBuffer readBuffer
)
1217 if (!drawBuffer
|| !readBuffer
)
1218 return GL_FALSE
; /* must specify buffers! */
1220 if (&(c
->mesa
) == _mesa_get_current_context()
1221 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1222 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1223 && XMESA_BUFFER(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1224 /* same context and buffer, do nothing */
1228 c
->xm_buffer
= drawBuffer
;
1230 /* Call this periodically to detect when the user has begun using
1231 * GL rendering from multiple threads.
1233 _glapi_check_multithread();
1235 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1236 if (readBuffer
!= drawBuffer
)
1237 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1239 _mesa_make_current(&(c
->mesa
),
1240 &drawBuffer
->mesa_buffer
,
1241 &readBuffer
->mesa_buffer
);
1244 * Must recompute and set these pixel values because colormap
1245 * can be different for different windows.
1247 c
->clearpixel
= xmesa_color_to_pixel( &c
->mesa
,
1252 c
->xm_visual
->undithered_pf
);
1253 XMesaSetForeground(c
->display
, drawBuffer
->cleargc
, c
->clearpixel
);
1255 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1256 drawBuffer
->wasCurrent
= GL_TRUE
;
1260 _mesa_make_current( NULL
, NULL
, NULL
);
1267 * Unbind the context c from its buffer.
1269 GLboolean
XMesaUnbindContext( XMesaContext c
)
1271 /* A no-op for XFree86 integration purposes */
1276 XMesaContext
XMesaGetCurrentContext( void )
1278 GET_CURRENT_CONTEXT(ctx
);
1280 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1289 XMesaBuffer
XMesaGetCurrentBuffer( void )
1291 GET_CURRENT_CONTEXT(ctx
);
1293 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1302 /* New in Mesa 3.1 */
1303 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1305 GET_CURRENT_CONTEXT(ctx
);
1307 return XMESA_BUFFER(ctx
->ReadBuffer
);
1316 GLboolean
XMesaSetFXmode( GLint mode
)
1325 * Copy the back buffer to the front buffer. If there's no back buffer
1329 void XMesaSwapBuffers( XMesaBuffer b
)
1331 GET_CURRENT_CONTEXT(ctx
);
1334 /* single buffered */
1338 /* If we're swapping the buffer associated with the current context
1339 * we have to flush any pending rendering commands first.
1341 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
1342 _mesa_notifySwapBuffers(ctx
);
1345 if (b
->backxrb
->ximage
) {
1346 /* Copy Ximage (back buf) from client memory to server window */
1347 #if defined(USE_XSHM)
1349 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1350 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1352 b
->backxrb
->ximage
, 0, 0,
1353 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
1355 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1360 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1361 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1363 b
->backxrb
->ximage
, 0, 0,
1364 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
1365 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1368 else if (b
->backxrb
->pixmap
) {
1369 /* Copy pixmap (back buf) to window (front buf) on server */
1370 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1371 XMesaCopyArea( b
->xm_visual
->display
,
1372 b
->backxrb
->pixmap
, /* source drawable */
1373 b
->frontxrb
->drawable
, /* dest. drawable */
1375 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
1376 0, 0 /* dest region */
1378 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1381 XSync( b
->xm_visual
->display
, False
);
1387 * Copy sub-region of back buffer to front buffer
1389 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1391 GET_CURRENT_CONTEXT(ctx
);
1393 /* If we're swapping the buffer associated with the current context
1394 * we have to flush any pending rendering commands first.
1396 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
1397 _mesa_notifySwapBuffers(ctx
);
1400 /* single buffered */
1405 int yTop
= b
->mesa_buffer
.Height
- y
- height
;
1406 if (b
->backxrb
->ximage
) {
1407 /* Copy Ximage from host's memory to server's window */
1408 #if defined(USE_XSHM)
1410 /* XXX assuming width and height aren't too large! */
1411 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1413 b
->backxrb
->ximage
, x
, yTop
,
1414 x
, yTop
, width
, height
, False
);
1415 /* wait for finished event??? */
1420 /* XXX assuming width and height aren't too large! */
1421 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1423 b
->backxrb
->ximage
, x
, yTop
,
1424 x
, yTop
, width
, height
);
1428 /* Copy pixmap to window on server */
1429 XMesaCopyArea( b
->xm_visual
->display
,
1430 b
->backxrb
->pixmap
, /* source drawable */
1431 b
->frontxrb
->drawable
, /* dest. drawable */
1433 x
, yTop
, width
, height
, /* source region */
1434 x
, yTop
/* dest region */
1442 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
1443 * is a way to get "under the hood" of X/Mesa so one can manipulate the
1444 * back buffer directly.
1445 * Output: pixmap - pointer to back buffer's Pixmap, or 0
1446 * ximage - pointer to back buffer's XImage, or NULL
1447 * Return: GL_TRUE = context is double buffered
1448 * GL_FALSE = context is single buffered
1450 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
1451 XMesaPixmap
*pixmap
,
1452 XMesaImage
**ximage
)
1456 *pixmap
= b
->backxrb
->pixmap
;
1458 *ximage
= b
->backxrb
->ximage
;
1470 * Return the depth buffer associated with an XMesaBuffer.
1471 * Input: b - the XMesa buffer handle
1472 * Output: width, height - size of buffer in pixels
1473 * bytesPerValue - bytes per depth value (2 or 4)
1474 * buffer - pointer to depth buffer values
1475 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1477 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
1478 GLint
*bytesPerValue
, void **buffer
)
1480 struct gl_renderbuffer
*rb
1481 = b
->mesa_buffer
.Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1482 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
);
1484 if (!xrb
|| !xrb
->Base
.Buffer
) {
1492 *width
= b
->mesa_buffer
.Width
;
1493 *height
= b
->mesa_buffer
.Height
;
1494 *bytesPerValue
= b
->mesa_buffer
.Visual
.depthBits
<= 16
1495 ? sizeof(GLushort
) : sizeof(GLuint
);
1496 *buffer
= (void *) xrb
->Base
.Buffer
;
1502 void XMesaFlush( XMesaContext c
)
1504 if (c
&& c
->xm_visual
) {
1505 XSync( c
->xm_visual
->display
, False
);
1511 const char *XMesaGetString( XMesaContext c
, int name
)
1514 if (name
==XMESA_VERSION
) {
1517 else if (name
==XMESA_EXTENSIONS
) {
1527 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
1530 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
1531 if (b
->frontxrb
->drawable
== d
&& b
->display
== dpy
) {
1540 * Free/destroy all XMesaBuffers associated with given display.
1542 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
1544 XMesaBuffer b
, next
;
1545 for (b
= XMesaBufferList
; b
; b
= next
) {
1547 if (b
->display
== dpy
) {
1548 xmesa_free_buffer(b
);
1555 * Look for XMesaBuffers whose X window has been destroyed.
1556 * Deallocate any such XMesaBuffers.
1558 void XMesaGarbageCollect( XMesaDisplay
* dpy
)
1560 XMesaBuffer b
, next
;
1561 for (b
=XMesaBufferList
; b
; b
=next
) {
1563 if (b
->display
&& b
->display
== dpy
&& b
->frontxrb
->drawable
&& b
->type
== WINDOW
) {
1564 XSync(b
->display
, False
);
1565 if (!window_exists( b
->display
, b
->frontxrb
->drawable
)) {
1566 /* found a dead window, free the ancillary info */
1567 XMesaDestroyBuffer( b
);
1574 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
1575 GLfloat red
, GLfloat green
,
1576 GLfloat blue
, GLfloat alpha
)
1578 GLint r
= (GLint
) (red
* 255.0F
);
1579 GLint g
= (GLint
) (green
* 255.0F
);
1580 GLint b
= (GLint
) (blue
* 255.0F
);
1581 GLint a
= (GLint
) (alpha
* 255.0F
);
1583 switch (xmesa
->pixelformat
) {
1587 PACK_TRUECOLOR( p
, r
, g
, b
);
1591 return PACK_8A8B8G8R( r
, g
, b
, a
);
1593 return PACK_8A8R8G8B( r
, g
, b
, a
);
1595 return PACK_8R8G8B( r
, g
, b
);
1597 return PACK_5R6G5B( r
, g
, b
);
1598 case PF_Dither_5R6G5B
:
1600 case PF_Dither_True
:
1603 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
1607 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
1614 * This is typically called when the window size changes and we need
1615 * to reallocate the buffer's back/depth/stencil/accum buffers.
1618 XMesaResizeBuffers( XMesaBuffer b
)
1620 GET_CURRENT_CONTEXT(ctx
);
1621 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1624 xmesa_check_and_update_buffer_size(xmctx
, b
);
1629 xbuffer_to_renderbuffer(int buffer
)
1631 assert(MAX_AUX_BUFFERS
<= 4);
1634 case GLX_FRONT_LEFT_EXT
:
1635 return BUFFER_FRONT_LEFT
;
1636 case GLX_FRONT_RIGHT_EXT
:
1637 return BUFFER_FRONT_RIGHT
;
1638 case GLX_BACK_LEFT_EXT
:
1639 return BUFFER_BACK_LEFT
;
1640 case GLX_BACK_RIGHT_EXT
:
1641 return BUFFER_BACK_RIGHT
;
1654 /* BadValue error */
1661 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
1662 const int *attrib_list
)
1665 GET_CURRENT_CONTEXT(ctx
);
1666 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
1667 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1668 struct gl_texture_object
*texObj
;
1670 struct gl_renderbuffer
*rb
;
1671 struct xmesa_renderbuffer
*xrb
;
1673 XMesaImage
*img
= NULL
;
1674 GLboolean freeImg
= GL_FALSE
;
1676 b
= xbuffer_to_renderbuffer(buffer
);
1680 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_NONE_EXT
)
1681 return; /* BadMatch error */
1683 rb
= drawable
->mesa_buffer
.Attachment
[b
].Renderbuffer
;
1685 /* invalid buffer */
1688 xrb
= xmesa_renderbuffer(rb
);
1691 switch (drawable
->TextureTarget
) {
1692 case GLX_TEXTURE_1D_EXT
:
1693 texObj
= texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
1695 case GLX_TEXTURE_2D_EXT
:
1696 texObj
= texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
1698 case GLX_TEXTURE_RECTANGLE_EXT
:
1699 texObj
= texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
];
1702 return; /* BadMatch error */
1707 * The following is a quick and simple way to implement
1708 * BindTexImage. The better way is to write some new FetchTexel()
1709 * functions which would extract texels from XImages. We'd still
1710 * need to use GetImage when texturing from a Pixmap (front buffer)
1711 * but texturing from a back buffer (XImage) would avoid an image
1717 img
= XMesaGetImage(dpy
, xrb
->pixmap
, 0, 0, rb
->Width
, rb
->Height
, ~0L,
1721 else if (xrb
->ximage
) {
1725 /* store the XImage as a new texture image */
1727 GLenum format
, type
, intFormat
;
1728 if (img
->bits_per_pixel
== 32) {
1730 type
= GL_UNSIGNED_BYTE
;
1731 intFormat
= GL_RGBA
;
1733 else if (img
->bits_per_pixel
== 24) {
1735 type
= GL_UNSIGNED_BYTE
;
1738 else if (img
->bits_per_pixel
== 16) {
1740 type
= GL_UNSIGNED_SHORT_5_6_5
;
1744 _mesa_problem(NULL
, "Unexpected XImage format in XMesaBindTexImage");
1747 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGBA_EXT
) {
1748 intFormat
= GL_RGBA
;
1750 else if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGB_EXT
) {
1754 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, intFormat
, rb
->Width
, rb
->Height
, 0,
1755 format
, type
, img
->data
);
1758 XMesaDestroyImage(img
);
1766 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)
1768 const GLint b
= xbuffer_to_renderbuffer(buffer
);