2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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.
66 #include "glxheader.h"
68 #include "main/api_exec.h"
69 #include "main/context.h"
70 #include "main/extensions.h"
71 #include "main/framebuffer.h"
72 #include "main/imports.h"
73 #include "main/macros.h"
74 #include "main/renderbuffer.h"
75 #include "main/teximage.h"
76 #include "main/version.h"
77 #include "main/vtxfmt.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"
87 #include "util/u_math.h"
90 * Global X driver lock
96 /**********************************************************************/
97 /***** X Utility Functions *****/
98 /**********************************************************************/
102 * Return the host's byte order as LSBFirst or MSBFirst ala X.
104 static int host_byte_order( void )
107 char *cptr
= (char *) &i
;
108 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
113 * Check if the X Shared Memory extension is available.
114 * Return: 0 = not available
115 * 1 = shared XImage support available
116 * 2 = shared Pixmap support available also
118 static int check_for_xshm( XMesaDisplay
*display
)
120 #if defined(USE_XSHM)
123 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
124 /* Note: we're no longer calling XShmQueryVersion() here. It seems
125 * to be flakey (triggers a spurious X protocol error when we close
126 * one display connection and start using a new one. XShm has been
127 * around a long time and hasn't changed so if MIT_SHM is supported
128 * we assume we're good to go.
136 /* No XSHM support */
143 * Apply gamma correction to an intensity value in [0..max]. Return the
144 * new intensity value.
147 gamma_adjust( GLfloat gamma
, GLint value
, GLint max
)
153 double x
= (double) value
/ (double) max
;
154 return IROUND_POS((GLfloat
) max
* pow(x
, 1.0F
/gamma
));
161 * Return the true number of bits per pixel for XImages.
162 * For example, if we request a 24-bit deep visual we may actually need/get
163 * 32bpp XImages. This function returns the appropriate bpp.
164 * Input: dpy - the X display
165 * visinfo - desribes the visual to be used for XImages
166 * Return: true number of bits per pixel for XImages
169 bits_per_pixel( XMesaVisual xmv
)
171 XMesaDisplay
*dpy
= xmv
->display
;
172 XMesaVisualInfo visinfo
= xmv
->visinfo
;
175 /* Create a temporary XImage */
176 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
177 ZPixmap
, 0, /*format, offset*/
179 1, 1, /*width, height*/
184 /* grab the bits/pixel value */
185 bitsPerPixel
= img
->bits_per_pixel
;
186 /* free the XImage */
189 XMesaDestroyImage( img
);
196 * Determine if a given X window ID is valid (window exists).
197 * Do this by calling XGetWindowAttributes() for the window and
198 * checking if we catch an X error.
199 * Input: dpy - the display
200 * win - the window to check for existence
201 * Return: GL_TRUE - window exists
202 * GL_FALSE - window doesn't exist
204 static GLboolean WindowExistsFlag
;
206 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
209 if (xerr
->error_code
== BadWindow
) {
210 WindowExistsFlag
= GL_FALSE
;
215 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
217 XWindowAttributes wa
;
218 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
219 WindowExistsFlag
= GL_TRUE
;
220 old_handler
= XSetErrorHandler(window_exists_err_handler
);
221 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
222 XSetErrorHandler(old_handler
);
223 return WindowExistsFlag
;
227 get_drawable_size( XMesaDisplay
*dpy
, Drawable d
, GLuint
*width
, GLuint
*height
)
232 unsigned int w
, h
, bw
, depth
;
233 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
241 * Return the size of the window (or pixmap) that corresponds to the
243 * \param width returns width in pixels
244 * \param height returns height in pixels
247 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
248 GLuint
*width
, GLuint
*height
)
252 mtx_lock(&_xmesa_lock
);
253 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
254 stat
= get_drawable_size(dpy
, b
->frontxrb
->pixmap
, width
, height
);
255 mtx_unlock(&_xmesa_lock
);
258 /* probably querying a window that's recently been destroyed */
259 _mesa_warning(NULL
, "XGetGeometry failed!\n");
260 *width
= *height
= 1;
266 /**********************************************************************/
267 /***** Linked list of XMesaBuffers *****/
268 /**********************************************************************/
270 XMesaBuffer XMesaBufferList
= NULL
;
274 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
275 * Note that XMesaBuffer is derived from struct gl_framebuffer.
276 * The new XMesaBuffer will not have any size (Width=Height=0).
278 * \param d the corresponding X drawable (window or pixmap)
279 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
280 * \param vis the buffer's visual
281 * \param cmap the window's colormap, if known.
282 * \return new XMesaBuffer or NULL if any problem
285 create_xmesa_buffer(XMesaDrawable d
, BufferType type
,
286 XMesaVisual vis
, XMesaColormap cmap
)
290 assert(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
292 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
296 b
->display
= vis
->display
;
301 _mesa_initialize_window_framebuffer(&b
->mesa_buffer
, &vis
->mesa_visual
);
302 b
->mesa_buffer
.Delete
= xmesa_delete_framebuffer
;
307 b
->frontxrb
= xmesa_new_renderbuffer(NULL
, 0, vis
, GL_FALSE
);
312 b
->frontxrb
->Parent
= b
;
313 b
->frontxrb
->drawable
= d
;
314 b
->frontxrb
->pixmap
= (XMesaPixmap
) d
;
315 _mesa_attach_and_own_rb(&b
->mesa_buffer
, BUFFER_FRONT_LEFT
,
316 &b
->frontxrb
->Base
.Base
);
321 if (vis
->mesa_visual
.doubleBufferMode
) {
322 b
->backxrb
= xmesa_new_renderbuffer(NULL
, 0, vis
, GL_TRUE
);
324 /* XXX free front xrb too */
328 b
->backxrb
->Parent
= b
;
329 /* determine back buffer implementation */
330 b
->db_mode
= vis
->ximage_flag
? BACK_XIMAGE
: BACK_PIXMAP
;
332 _mesa_attach_and_own_rb(&b
->mesa_buffer
, BUFFER_BACK_LEFT
,
333 &b
->backxrb
->Base
.Base
);
337 * Other renderbuffer (depth, stencil, etc)
339 _swrast_add_soft_renderbuffers(&b
->mesa_buffer
,
340 GL_FALSE
, /* color */
341 vis
->mesa_visual
.haveDepthBuffer
,
342 vis
->mesa_visual
.haveStencilBuffer
,
343 vis
->mesa_visual
.haveAccumBuffer
,
344 GL_FALSE
, /* software alpha buffer */
345 vis
->mesa_visual
.numAuxBuffers
> 0 );
347 /* GLX_EXT_texture_from_pixmap */
348 b
->TextureTarget
= 0;
349 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
350 b
->TextureMipmap
= 0;
352 /* insert buffer into linked list */
353 b
->Next
= XMesaBufferList
;
361 * Find an XMesaBuffer by matching X display and colormap but NOT matching
362 * the notThis buffer.
365 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
368 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
369 if (b
->display
==dpy
&& b
->cmap
==cmap
&& b
!=notThis
) {
378 * Remove buffer from linked list, delete if no longer referenced.
381 xmesa_free_buffer(XMesaBuffer buffer
)
383 XMesaBuffer prev
= NULL
, b
;
385 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
387 struct gl_framebuffer
*fb
= &buffer
->mesa_buffer
;
389 /* unlink buffer from list */
391 prev
->Next
= buffer
->Next
;
393 XMesaBufferList
= buffer
->Next
;
395 /* mark as delete pending */
396 fb
->DeletePending
= GL_TRUE
;
398 /* Since the X window for the XMesaBuffer is going away, we don't
399 * want to dereference this pointer in the future.
401 b
->frontxrb
->drawable
= 0;
403 /* Unreference. If count = zero we'll really delete the buffer */
404 _mesa_reference_framebuffer(&fb
, NULL
);
408 /* continue search */
411 /* buffer not found in XMesaBufferList */
412 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
418 /**********************************************************************/
419 /***** Misc Private Functions *****/
420 /**********************************************************************/
424 * Setup RGB rendering for a window with a True/DirectColor visual.
427 setup_truecolor(XMesaVisual v
, XMesaBuffer buffer
, XMesaColormap cmap
)
429 unsigned long rmask
, gmask
, bmask
;
433 /* Compute red multiplier (mask) and bit shift */
435 rmask
= GET_REDMASK(v
);
436 while ((rmask
& 1)==0) {
441 /* Compute green multiplier (mask) and bit shift */
443 gmask
= GET_GREENMASK(v
);
444 while ((gmask
& 1)==0) {
449 /* Compute blue multiplier (mask) and bit shift */
451 bmask
= GET_BLUEMASK(v
);
452 while ((bmask
& 1)==0) {
458 * Compute component-to-pixel lookup tables and dithering kernel
461 static GLubyte kernel
[16] = {
462 0*16, 8*16, 2*16, 10*16,
463 12*16, 4*16, 14*16, 6*16,
464 3*16, 11*16, 1*16, 9*16,
465 15*16, 7*16, 13*16, 5*16,
467 GLint rBits
= util_bitcount(rmask
);
468 GLint gBits
= util_bitcount(gmask
);
469 GLint bBits
= util_bitcount(bmask
);
473 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
474 for (i
=0; i
<=rmask
; i
++)
475 v
->PixelToR
[i
] = (unsigned char) ((i
* 255) / rmask
);
476 for (i
=0; i
<=gmask
; i
++)
477 v
->PixelToG
[i
] = (unsigned char) ((i
* 255) / gmask
);
478 for (i
=0; i
<=bmask
; i
++)
479 v
->PixelToB
[i
] = (unsigned char) ((i
* 255) / bmask
);
481 /* convert RGB values from [0,255] to pixel components */
483 for (i
=0;i
<256;i
++) {
484 GLint r
= gamma_adjust(v
->RedGamma
, i
, 255);
485 GLint g
= gamma_adjust(v
->GreenGamma
, i
, 255);
486 GLint b
= gamma_adjust(v
->BlueGamma
, i
, 255);
487 v
->RtoPixel
[i
] = (r
>> (8-rBits
)) << v
->rshift
;
488 v
->GtoPixel
[i
] = (g
>> (8-gBits
)) << v
->gshift
;
489 v
->BtoPixel
[i
] = (b
>> (8-bBits
)) << v
->bshift
;
491 /* overflow protection */
492 for (i
=256;i
<512;i
++) {
493 v
->RtoPixel
[i
] = v
->RtoPixel
[255];
494 v
->GtoPixel
[i
] = v
->GtoPixel
[255];
495 v
->BtoPixel
[i
] = v
->BtoPixel
[255];
498 /* setup dithering kernel */
500 if (gBits
> maxBits
) maxBits
= gBits
;
501 if (bBits
> maxBits
) maxBits
= bBits
;
503 v
->Kernel
[i
] = kernel
[i
] >> maxBits
;
506 v
->undithered_pf
= PF_Truecolor
;
507 v
->dithered_pf
= (GET_VISUAL_DEPTH(v
)<24) ? PF_Dither_True
: PF_Truecolor
;
511 * Now check for TrueColor visuals which we can optimize.
513 if ( GET_REDMASK(v
) ==0x0000ff
514 && GET_GREENMASK(v
)==0x00ff00
515 && GET_BLUEMASK(v
) ==0xff0000
516 && CHECK_BYTE_ORDER(v
)
517 && v
->BitsPerPixel
==32
518 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
519 /* common 32 bpp config used on SGI, Sun */
520 v
->undithered_pf
= v
->dithered_pf
= PF_8A8B8G8R
; /* ABGR */
522 else if (GET_REDMASK(v
) == 0xff0000
523 && GET_GREENMASK(v
)== 0x00ff00
524 && GET_BLUEMASK(v
) == 0x0000ff
525 && CHECK_BYTE_ORDER(v
)
526 && v
->RedGamma
== 1.0 && v
->GreenGamma
== 1.0 && v
->BlueGamma
== 1.0){
527 if (v
->BitsPerPixel
==32) {
528 /* if 32 bpp, and visual indicates 8 bpp alpha channel */
529 if (GET_VISUAL_DEPTH(v
) == 32 && v
->mesa_visual
.alphaBits
== 8)
530 v
->undithered_pf
= v
->dithered_pf
= PF_8A8R8G8B
; /* ARGB */
532 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B
; /* xRGB */
534 else if (v
->BitsPerPixel
== 24) {
535 v
->undithered_pf
= v
->dithered_pf
= PF_8R8G8B24
; /* RGB */
538 else if (GET_REDMASK(v
) ==0xf800
539 && GET_GREENMASK(v
)==0x07e0
540 && GET_BLUEMASK(v
) ==0x001f
541 && CHECK_BYTE_ORDER(v
)
542 && v
->BitsPerPixel
==16
543 && v
->RedGamma
==1.0 && v
->GreenGamma
==1.0 && v
->BlueGamma
==1.0) {
545 v
->undithered_pf
= PF_5R6G5B
;
546 v
->dithered_pf
= PF_Dither_5R6G5B
;
552 * When a context is bound for the first time, we can finally finish
553 * initializing the context's visual and buffer information.
554 * \param v the XMesaVisual to initialize
555 * \param b the XMesaBuffer to initialize (may be NULL)
556 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
557 * \param window the window/pixmap we're rendering into
558 * \param cmap the colormap associated with the window/pixmap
559 * \return GL_TRUE=success, GL_FALSE=failure
562 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
563 XMesaDrawable window
,
566 const int xclass
= v
->visualType
;
569 assert(!b
|| b
->xm_visual
== v
);
571 /* Save true bits/pixel */
572 v
->BitsPerPixel
= bits_per_pixel(v
);
573 assert(v
->BitsPerPixel
> 0);
576 * We support RGB rendering into almost any kind of visual.
578 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
579 setup_truecolor( v
, b
, cmap
);
582 _mesa_warning(NULL
, "XMesa: RGB mode rendering not supported in given visual.\n");
585 v
->mesa_visual
.indexBits
= 0;
587 if (getenv("MESA_NO_DITHER")) {
588 v
->dithered_pf
= v
->undithered_pf
;
593 * If MESA_INFO env var is set print out some debugging info
594 * which can help Brian figure out what's going on when a user
597 if (getenv("MESA_INFO")) {
598 printf("X/Mesa visual = %p\n", (void *) v
);
599 printf("X/Mesa dithered pf = %u\n", v
->dithered_pf
);
600 printf("X/Mesa undithered pf = %u\n", v
->undithered_pf
);
601 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
602 printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
603 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
607 /* Do window-specific initializations */
609 /* these should have been set in create_xmesa_buffer */
610 assert(b
->frontxrb
->drawable
== window
);
611 assert(b
->frontxrb
->pixmap
== (XMesaPixmap
) window
);
613 /* Setup for single/double buffering */
614 if (v
->mesa_visual
.doubleBufferMode
) {
615 /* Double buffered */
616 b
->shm
= check_for_xshm( v
->display
);
619 /* X11 graphics contexts */
620 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
621 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
623 /* cleargc - for glClear() */
624 b
->cleargc
= XCreateGC( v
->display
, window
, 0, NULL
);
625 XMesaSetFunction( v
->display
, b
->cleargc
, GXcopy
);
628 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
629 * Patch contributed by Michael Pichler May 15, 1995.
633 gcvalues
.graphics_exposures
= False
;
634 b
->swapgc
= XCreateGC(v
->display
, window
,
635 GCGraphicsExposures
, &gcvalues
);
637 XMesaSetFunction( v
->display
, b
->swapgc
, GXcopy
);
646 * Convert an RGBA color to a pixel value.
649 xmesa_color_to_pixel(struct gl_context
*ctx
,
650 GLubyte r
, GLubyte g
, GLubyte b
, GLubyte a
,
653 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
654 switch (pixelFormat
) {
658 PACK_TRUECOLOR( p
, r
, g
, b
);
662 return PACK_8A8B8G8R( r
, g
, b
, a
);
664 return PACK_8A8R8G8B( r
, g
, b
, a
);
668 return PACK_8R8G8B( r
, g
, b
);
670 return PACK_5R6G5B( r
, g
, b
);
673 case PF_Dither_5R6G5B
:
676 PACK_TRUEDITHER(p
, 1, 0, r
, g
, b
);
680 _mesa_problem(ctx
, "Bad pixel format in xmesa_color_to_pixel");
686 #define NUM_VISUAL_TYPES 6
689 * Convert an X visual type to a GLX visual type.
691 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
693 * \return If \c visualType is a valid X visual type, a GLX visual type will
694 * be returned. Otherwise \c GLX_NONE will be returned.
697 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
701 xmesa_convert_from_x_visual_type( int visualType
)
703 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
704 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
705 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
706 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
709 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
710 ? glx_visual_types
[ visualType
] : GLX_NONE
;
714 /**********************************************************************/
715 /***** Public Functions *****/
716 /**********************************************************************/
720 * Create a new X/Mesa visual.
721 * Input: display - X11 display
722 * visinfo - an XVisualInfo pointer
723 * rgb_flag - GL_TRUE = RGB mode,
724 * GL_FALSE = color index mode
725 * alpha_flag - alpha buffer requested?
726 * db_flag - GL_TRUE = double-buffered,
727 * GL_FALSE = single buffered
728 * stereo_flag - stereo visual?
729 * ximage_flag - GL_TRUE = use an XImage for back buffer,
730 * GL_FALSE = use an off-screen pixmap for back buffer
731 * depth_size - requested bits/depth values, or zero
732 * stencil_size - requested bits/stencil values, or zero
733 * accum_red_size - requested bits/red accum values, or zero
734 * accum_green_size - requested bits/green accum values, or zero
735 * accum_blue_size - requested bits/blue accum values, or zero
736 * accum_alpha_size - requested bits/alpha accum values, or zero
737 * num_samples - number of samples/pixel if multisampling, or zero
738 * level - visual level, usually 0
739 * visualCaveat - ala the GLX extension, usually GLX_NONE
740 * Return; a new XMesaVisual or 0 if error.
743 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
744 XMesaVisualInfo visinfo
,
746 GLboolean alpha_flag
,
748 GLboolean stereo_flag
,
749 GLboolean ximage_flag
,
752 GLint accum_red_size
,
753 GLint accum_green_size
,
754 GLint accum_blue_size
,
755 GLint accum_alpha_size
,
762 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
764 /* For debugging only */
765 if (getenv("MESA_XSYNC")) {
766 /* This makes debugging X easier.
767 * In your debugger, set a breakpoint on _XError to stop when an
768 * X protocol error is generated.
770 XSynchronize( display
, 1 );
773 /* Color-index rendering not supported. */
777 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
782 v
->display
= display
;
784 /* Save a copy of the XVisualInfo struct because the user may Xfree()
785 * the struct but we may need some of the information contained in it
788 v
->visinfo
= malloc(sizeof(*visinfo
));
793 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
795 /* check for MESA_GAMMA environment variable */
796 gamma
= getenv("MESA_GAMMA");
798 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 0.0;
799 sscanf( gamma
, "%f %f %f", &v
->RedGamma
, &v
->GreenGamma
, &v
->BlueGamma
);
800 if (v
->RedGamma
<=0.0) v
->RedGamma
= 1.0;
801 if (v
->GreenGamma
<=0.0) v
->GreenGamma
= v
->RedGamma
;
802 if (v
->BlueGamma
<=0.0) v
->BlueGamma
= v
->RedGamma
;
805 v
->RedGamma
= v
->GreenGamma
= v
->BlueGamma
= 1.0;
808 v
->ximage_flag
= ximage_flag
;
810 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
811 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
812 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
813 v
->visualID
= visinfo
->visualid
;
814 v
->screen
= visinfo
->screen
;
816 #if !(defined(__cplusplus) || defined(c_plusplus))
817 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
819 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
822 v
->mesa_visual
.visualRating
= visualCaveat
;
825 v
->mesa_visual
.alphaBits
= 8;
827 (void) initialize_visual_and_buffer( v
, NULL
, 0, 0 );
830 const int xclass
= v
->visualType
;
831 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
832 red_bits
= util_bitcount(GET_REDMASK(v
));
833 green_bits
= util_bitcount(GET_GREENMASK(v
));
834 blue_bits
= util_bitcount(GET_BLUEMASK(v
));
837 /* this is an approximation */
839 depth
= GET_VISUAL_DEPTH(v
);
840 red_bits
= depth
/ 3;
842 green_bits
= depth
/ 2;
846 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
848 alpha_bits
= v
->mesa_visual
.alphaBits
;
851 if (!_mesa_initialize_visual(&v
->mesa_visual
,
852 db_flag
, stereo_flag
,
853 red_bits
, green_bits
,
854 blue_bits
, alpha_bits
,
857 accum_red_size
, accum_green_size
,
858 accum_blue_size
, accum_alpha_size
,
866 v
->mesa_visual
.level
= level
;
872 void XMesaDestroyVisual( XMesaVisual v
)
881 * Create a new XMesaContext.
882 * \param v the XMesaVisual
883 * \param share_list another XMesaContext with which to share display
884 * lists or NULL if no sharing is wanted.
885 * \return an XMesaContext or NULL if error.
888 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
890 static GLboolean firstTime
= GL_TRUE
;
892 struct gl_context
*mesaCtx
;
893 struct dd_function_table functions
;
897 mtx_init(&_xmesa_lock
, mtx_plain
);
898 firstTime
= GL_FALSE
;
901 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
902 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
906 mesaCtx
= &(c
->mesa
);
908 /* initialize with default driver functions, then plug in XMesa funcs */
909 _mesa_init_driver_functions(&functions
);
910 _tnl_init_driver_draw_function(&functions
);
911 xmesa_init_driver_functions(v
, &functions
);
912 if (!_mesa_initialize_context(mesaCtx
, API_OPENGL_COMPAT
, &v
->mesa_visual
,
913 share_list
? &(share_list
->mesa
) : (struct gl_context
*) NULL
,
919 /* Enable this to exercise fixed function -> shader translation
920 * with software rendering.
923 mesaCtx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
924 mesaCtx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
927 _mesa_enable_sw_extensions(mesaCtx
);
929 #if ENABLE_EXT_timer_query
930 mesaCtx
->Extensions
.EXT_timer_query
= GL_TRUE
;
934 /* finish up xmesa context initializations */
936 c
->swapbytes
= CHECK_BYTE_ORDER(v
) ? GL_FALSE
: GL_TRUE
;
938 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
939 c
->display
= v
->display
;
940 c
->pixelformat
= v
->dithered_pf
; /* Dithering is enabled by default */
942 /* Initialize the software rasterizer and helper modules.
944 if (!_swrast_CreateContext( mesaCtx
) ||
945 !_vbo_CreateContext( mesaCtx
) ||
946 !_tnl_CreateContext( mesaCtx
) ||
947 !_swsetup_CreateContext( mesaCtx
)) {
948 _mesa_free_context_data(&c
->mesa
);
954 tnl
= TNL_CONTEXT(mesaCtx
);
955 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
957 xmesa_register_swrast_functions( mesaCtx
);
958 _swsetup_Wakeup(mesaCtx
);
960 _mesa_meta_init(mesaCtx
);
962 _mesa_override_extensions(mesaCtx
);
963 _mesa_compute_version(mesaCtx
);
965 /* Exec table initialization requires the version to be computed */
966 _mesa_initialize_dispatch_tables(mesaCtx
);
967 _mesa_initialize_vbo_vtxfmt(mesaCtx
);
975 void XMesaDestroyContext( XMesaContext c
)
977 struct gl_context
*mesaCtx
= &c
->mesa
;
979 _mesa_meta_free( mesaCtx
);
981 _swsetup_DestroyContext( mesaCtx
);
982 _swrast_DestroyContext( mesaCtx
);
983 _tnl_DestroyContext( mesaCtx
);
984 _vbo_DestroyContext( mesaCtx
);
985 _mesa_free_context_data( mesaCtx
);
992 * Private function for creating an XMesaBuffer which corresponds to an
993 * X window or pixmap.
994 * \param v the window's XMesaVisual
995 * \param w the window we're wrapping
996 * \return new XMesaBuffer or NULL if error
999 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
1001 XWindowAttributes attr
;
1009 /* Check that window depth matches visual depth */
1010 XGetWindowAttributes( v
->display
, w
, &attr
);
1012 if (GET_VISUAL_DEPTH(v
) != depth
) {
1013 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1014 GET_VISUAL_DEPTH(v
), depth
);
1019 if (attr
.colormap
) {
1020 cmap
= attr
.colormap
;
1023 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1024 /* this is weird, a window w/out a colormap!? */
1025 /* OK, let's just allocate a new one and hope for the best */
1026 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1029 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
1033 if (!initialize_visual_and_buffer( v
, b
, (XMesaDrawable
) w
, cmap
)) {
1034 xmesa_free_buffer(b
);
1044 * Create a new XMesaBuffer from an X pixmap.
1046 * \param v the XMesaVisual
1047 * \param p the pixmap
1048 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1049 * \c GLX_DIRECT_COLOR visual for the pixmap
1050 * \returns new XMesaBuffer or NULL if error
1053 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
1059 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1063 if (!initialize_visual_and_buffer(v
, b
, (XMesaDrawable
) p
, cmap
)) {
1064 xmesa_free_buffer(b
);
1073 * For GLX_EXT_texture_from_pixmap
1076 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
1078 int format
, int target
, int mipmap
)
1080 GET_CURRENT_CONTEXT(ctx
);
1082 GLuint width
, height
;
1086 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
1090 /* get pixmap size, update framebuffer/renderbuffer dims */
1091 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
1092 _mesa_resize_framebuffer(NULL
, &(b
->mesa_buffer
), width
, height
);
1096 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1097 target
= GLX_TEXTURE_2D_EXT
;
1099 else if ( util_bitcount(width
) == 1
1100 && util_bitcount(height
) == 1) {
1101 /* power of two size */
1103 target
= GLX_TEXTURE_1D_EXT
;
1106 target
= GLX_TEXTURE_2D_EXT
;
1109 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1110 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1113 /* non power of two textures not supported */
1114 XMesaDestroyBuffer(b
);
1119 b
->TextureTarget
= target
;
1120 b
->TextureFormat
= format
;
1121 b
->TextureMipmap
= mipmap
;
1123 if (!initialize_visual_and_buffer(v
, b
, (XMesaDrawable
) p
, cmap
)) {
1124 xmesa_free_buffer(b
);
1134 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1135 unsigned int width
, unsigned int height
)
1138 XMesaDrawable drawable
; /* X Pixmap Drawable */
1141 /* allocate pixmap for front buffer */
1142 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1143 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1148 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1152 if (!initialize_visual_and_buffer(v
, b
, drawable
, cmap
)) {
1153 xmesa_free_buffer(b
);
1163 * Deallocate an XMesaBuffer structure and all related info.
1166 XMesaDestroyBuffer(XMesaBuffer b
)
1168 xmesa_free_buffer(b
);
1173 * Query the current window size and update the corresponding struct gl_framebuffer
1174 * and all attached renderbuffers.
1176 * 1. the first time a buffer is bound to a context.
1177 * 2. from glViewport to poll for window size changes
1178 * 3. from the XMesaResizeBuffers() API function.
1179 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1180 * when resizing a buffer when no rendering context is bound.
1183 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1185 GLuint width
, height
;
1186 xmesa_get_window_size(drawBuffer
->display
, drawBuffer
, &width
, &height
);
1187 if (drawBuffer
->mesa_buffer
.Width
!= width
||
1188 drawBuffer
->mesa_buffer
.Height
!= height
) {
1189 struct gl_context
*ctx
= xmctx
? &xmctx
->mesa
: NULL
;
1190 _mesa_resize_framebuffer(ctx
, &(drawBuffer
->mesa_buffer
), width
, height
);
1196 * Bind buffer b to context c and make c the current rendering context.
1198 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1200 return XMesaMakeCurrent2( c
, b
, b
);
1205 * Bind buffer b to context c and make c the current rendering context.
1208 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1209 XMesaBuffer readBuffer
)
1212 if (!drawBuffer
|| !readBuffer
)
1213 return GL_FALSE
; /* must specify buffers! */
1215 if (&(c
->mesa
) == _mesa_get_current_context()
1216 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1217 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1218 && XMESA_BUFFER(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1219 /* same context and buffer, do nothing */
1223 c
->xm_buffer
= drawBuffer
;
1225 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1226 if (readBuffer
!= drawBuffer
)
1227 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1229 _mesa_make_current(&(c
->mesa
),
1230 &drawBuffer
->mesa_buffer
,
1231 &readBuffer
->mesa_buffer
);
1234 * Must recompute and set these pixel values because colormap
1235 * can be different for different windows.
1237 c
->clearpixel
= xmesa_color_to_pixel( &c
->mesa
,
1242 c
->xm_visual
->undithered_pf
);
1243 XMesaSetForeground(c
->display
, drawBuffer
->cleargc
, c
->clearpixel
);
1245 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1246 drawBuffer
->wasCurrent
= GL_TRUE
;
1250 _mesa_make_current( NULL
, NULL
, NULL
);
1257 * Unbind the context c from its buffer.
1259 GLboolean
XMesaUnbindContext( XMesaContext c
)
1261 /* A no-op for XFree86 integration purposes */
1266 XMesaContext
XMesaGetCurrentContext( void )
1268 GET_CURRENT_CONTEXT(ctx
);
1270 XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
1279 XMesaBuffer
XMesaGetCurrentBuffer( void )
1281 GET_CURRENT_CONTEXT(ctx
);
1283 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1292 /* New in Mesa 3.1 */
1293 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1295 GET_CURRENT_CONTEXT(ctx
);
1297 return XMESA_BUFFER(ctx
->ReadBuffer
);
1305 Display
*XMesaGetCurrentDisplay(void)
1307 GET_CURRENT_CONTEXT(ctx
);
1308 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1309 return xmctx
? xmctx
->display
: NULL
;
1315 * Copy the back buffer to the front buffer. If there's no back buffer
1319 void XMesaSwapBuffers( XMesaBuffer b
)
1321 GET_CURRENT_CONTEXT(ctx
);
1324 /* single buffered */
1328 /* If we're swapping the buffer associated with the current context
1329 * we have to flush any pending rendering commands first.
1331 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
1332 _mesa_notifySwapBuffers(ctx
);
1335 if (b
->backxrb
->ximage
) {
1336 /* Copy Ximage (back buf) from client memory to server window */
1337 #if defined(USE_XSHM)
1339 /*mtx_lock(&_xmesa_lock);*/
1340 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1342 b
->backxrb
->ximage
, 0, 0,
1343 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
1345 /*mtx_unlock(&_xmesa_lock);*/
1350 /*mtx_lock(&_xmesa_lock);*/
1351 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1353 b
->backxrb
->ximage
, 0, 0,
1354 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
);
1355 /*mtx_unlock(&_xmesa_lock);*/
1358 else if (b
->backxrb
->pixmap
) {
1359 /* Copy pixmap (back buf) to window (front buf) on server */
1360 /*mtx_lock(&_xmesa_lock);*/
1361 XMesaCopyArea( b
->xm_visual
->display
,
1362 b
->backxrb
->pixmap
, /* source drawable */
1363 b
->frontxrb
->drawable
, /* dest. drawable */
1365 0, 0, b
->mesa_buffer
.Width
, b
->mesa_buffer
.Height
,
1366 0, 0 /* dest region */
1368 /*mtx_unlock(&_xmesa_lock);*/
1371 XSync( b
->xm_visual
->display
, False
);
1377 * Copy sub-region of back buffer to front buffer
1379 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1381 GET_CURRENT_CONTEXT(ctx
);
1383 /* If we're swapping the buffer associated with the current context
1384 * we have to flush any pending rendering commands first.
1386 if (ctx
&& ctx
->DrawBuffer
== &(b
->mesa_buffer
))
1387 _mesa_notifySwapBuffers(ctx
);
1390 /* single buffered */
1395 int yTop
= b
->mesa_buffer
.Height
- y
- height
;
1396 if (b
->backxrb
->ximage
) {
1397 /* Copy Ximage from host's memory to server's window */
1398 #if defined(USE_XSHM)
1400 /* XXX assuming width and height aren't too large! */
1401 XShmPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1403 b
->backxrb
->ximage
, x
, yTop
,
1404 x
, yTop
, width
, height
, False
);
1405 /* wait for finished event??? */
1410 /* XXX assuming width and height aren't too large! */
1411 XMesaPutImage( b
->xm_visual
->display
, b
->frontxrb
->drawable
,
1413 b
->backxrb
->ximage
, x
, yTop
,
1414 x
, yTop
, width
, height
);
1418 /* Copy pixmap to window on server */
1419 XMesaCopyArea( b
->xm_visual
->display
,
1420 b
->backxrb
->pixmap
, /* source drawable */
1421 b
->frontxrb
->drawable
, /* dest. drawable */
1423 x
, yTop
, width
, height
, /* source region */
1424 x
, yTop
/* dest region */
1432 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
1433 * is a way to get "under the hood" of X/Mesa so one can manipulate the
1434 * back buffer directly.
1435 * Output: pixmap - pointer to back buffer's Pixmap, or 0
1436 * ximage - pointer to back buffer's XImage, or NULL
1437 * Return: GL_TRUE = context is double buffered
1438 * GL_FALSE = context is single buffered
1440 GLboolean
XMesaGetBackBuffer( XMesaBuffer b
,
1441 XMesaPixmap
*pixmap
,
1442 XMesaImage
**ximage
)
1446 *pixmap
= b
->backxrb
->pixmap
;
1448 *ximage
= b
->backxrb
->ximage
;
1460 * Return the depth buffer associated with an XMesaBuffer.
1461 * Input: b - the XMesa buffer handle
1462 * Output: width, height - size of buffer in pixels
1463 * bytesPerValue - bytes per depth value (2 or 4)
1464 * buffer - pointer to depth buffer values
1465 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1467 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
1468 GLint
*bytesPerValue
, void **buffer
)
1470 struct gl_renderbuffer
*rb
1471 = b
->mesa_buffer
.Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1472 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
);
1474 if (!xrb
|| !xrb
->Base
.Buffer
) {
1482 *width
= b
->mesa_buffer
.Width
;
1483 *height
= b
->mesa_buffer
.Height
;
1484 *bytesPerValue
= b
->mesa_buffer
.Visual
.depthBits
<= 16
1485 ? sizeof(GLushort
) : sizeof(GLuint
);
1486 *buffer
= (void *) xrb
->Base
.Buffer
;
1492 void XMesaFlush( XMesaContext c
)
1494 if (c
&& c
->xm_visual
) {
1495 XSync( c
->xm_visual
->display
, False
);
1501 const char *XMesaGetString( XMesaContext c
, int name
)
1504 if (name
==XMESA_VERSION
) {
1507 else if (name
==XMESA_EXTENSIONS
) {
1517 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
1520 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
1521 if (b
->frontxrb
->drawable
== d
&& b
->display
== dpy
) {
1530 * Free/destroy all XMesaBuffers associated with given display.
1532 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
1534 XMesaBuffer b
, next
;
1535 for (b
= XMesaBufferList
; b
; b
= next
) {
1537 if (b
->display
== dpy
) {
1538 xmesa_free_buffer(b
);
1545 * Look for XMesaBuffers whose X window has been destroyed.
1546 * Deallocate any such XMesaBuffers.
1548 void XMesaGarbageCollect( XMesaDisplay
* dpy
)
1550 XMesaBuffer b
, next
;
1551 for (b
=XMesaBufferList
; b
; b
=next
) {
1553 if (b
->display
&& b
->display
== dpy
&& b
->frontxrb
->drawable
&& b
->type
== WINDOW
) {
1554 XSync(b
->display
, False
);
1555 if (!window_exists( b
->display
, b
->frontxrb
->drawable
)) {
1556 /* found a dead window, free the ancillary info */
1557 XMesaDestroyBuffer( b
);
1564 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
1565 GLfloat red
, GLfloat green
,
1566 GLfloat blue
, GLfloat alpha
)
1568 GLint r
= (GLint
) (red
* 255.0F
);
1569 GLint g
= (GLint
) (green
* 255.0F
);
1570 GLint b
= (GLint
) (blue
* 255.0F
);
1571 GLint a
= (GLint
) (alpha
* 255.0F
);
1573 switch (xmesa
->pixelformat
) {
1577 PACK_TRUECOLOR( p
, r
, g
, b
);
1581 return PACK_8A8B8G8R( r
, g
, b
, a
);
1583 return PACK_8A8R8G8B( r
, g
, b
, a
);
1585 return PACK_8R8G8B( r
, g
, b
);
1587 return PACK_5R6G5B( r
, g
, b
);
1588 case PF_Dither_5R6G5B
:
1590 case PF_Dither_True
:
1593 PACK_TRUEDITHER(p
, x
, y
, r
, g
, b
);
1597 _mesa_problem(NULL
, "Bad pixel format in XMesaDitherColor");
1604 * This is typically called when the window size changes and we need
1605 * to reallocate the buffer's back/depth/stencil/accum buffers.
1608 XMesaResizeBuffers( XMesaBuffer b
)
1610 GET_CURRENT_CONTEXT(ctx
);
1611 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
1614 xmesa_check_and_update_buffer_size(xmctx
, b
);
1619 xbuffer_to_renderbuffer(int buffer
)
1621 assert(MAX_AUX_BUFFERS
<= 4);
1624 case GLX_FRONT_LEFT_EXT
:
1625 return BUFFER_FRONT_LEFT
;
1626 case GLX_FRONT_RIGHT_EXT
:
1627 return BUFFER_FRONT_RIGHT
;
1628 case GLX_BACK_LEFT_EXT
:
1629 return BUFFER_BACK_LEFT
;
1630 case GLX_BACK_RIGHT_EXT
:
1631 return BUFFER_BACK_RIGHT
;
1644 /* BadValue error */
1651 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
1652 const int *attrib_list
)
1655 GET_CURRENT_CONTEXT(ctx
);
1656 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
1657 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1658 struct gl_texture_object
*texObj
;
1660 struct gl_renderbuffer
*rb
;
1661 struct xmesa_renderbuffer
*xrb
;
1663 XMesaImage
*img
= NULL
;
1664 GLboolean freeImg
= GL_FALSE
;
1666 b
= xbuffer_to_renderbuffer(buffer
);
1670 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_NONE_EXT
)
1671 return; /* BadMatch error */
1673 rb
= drawable
->mesa_buffer
.Attachment
[b
].Renderbuffer
;
1675 /* invalid buffer */
1678 xrb
= xmesa_renderbuffer(rb
);
1681 switch (drawable
->TextureTarget
) {
1682 case GLX_TEXTURE_1D_EXT
:
1683 texObj
= texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
1685 case GLX_TEXTURE_2D_EXT
:
1686 texObj
= texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
1688 case GLX_TEXTURE_RECTANGLE_EXT
:
1689 texObj
= texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
];
1692 return; /* BadMatch error */
1697 * The following is a quick and simple way to implement
1698 * BindTexImage. The better way is to write some new FetchTexel()
1699 * functions which would extract texels from XImages. We'd still
1700 * need to use GetImage when texturing from a Pixmap (front buffer)
1701 * but texturing from a back buffer (XImage) would avoid an image
1707 img
= XMesaGetImage(dpy
, xrb
->pixmap
, 0, 0, rb
->Width
, rb
->Height
, ~0L,
1711 else if (xrb
->ximage
) {
1715 /* store the XImage as a new texture image */
1717 GLenum format
, type
, intFormat
;
1718 if (img
->bits_per_pixel
== 32) {
1720 type
= GL_UNSIGNED_BYTE
;
1721 intFormat
= GL_RGBA
;
1723 else if (img
->bits_per_pixel
== 24) {
1725 type
= GL_UNSIGNED_BYTE
;
1728 else if (img
->bits_per_pixel
== 16) {
1730 type
= GL_UNSIGNED_SHORT_5_6_5
;
1734 _mesa_problem(NULL
, "Unexpected XImage format in XMesaBindTexImage");
1737 if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGBA_EXT
) {
1738 intFormat
= GL_RGBA
;
1740 else if (drawable
->TextureFormat
== GLX_TEXTURE_FORMAT_RGB_EXT
) {
1744 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, intFormat
, rb
->Width
, rb
->Height
, 0,
1745 format
, type
, img
->data
);
1748 XMesaDestroyImage(img
);
1756 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)
1758 const GLint b
= xbuffer_to_renderbuffer(buffer
);