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.
61 #include "main/context.h"
62 #include "main/framebuffer.h"
64 #include "state_tracker/st_public.h"
65 #include "state_tracker/st_context.h"
66 #include "pipe/p_defines.h"
67 #include "pipe/p_screen.h"
68 #include "pipe/p_context.h"
70 #include "trace/tr_screen.h"
71 #include "trace/tr_context.h"
72 #include "trace/tr_texture.h"
74 #include "xm_winsys.h"
78 /* Driver interface routines, set up by xlib backend on library
79 * _init(). These are global in the same way that function names are
82 static struct xm_driver driver
;
84 void xmesa_set_driver( const struct xm_driver
*templ
)
90 * Global X driver lock
92 pipe_mutex _xmesa_lock
;
94 static struct pipe_screen
*_screen
= NULL
;
95 static struct pipe_screen
*screen
= NULL
;
98 /**********************************************************************/
99 /***** X Utility Functions *****/
100 /**********************************************************************/
104 * Return the host's byte order as LSBFirst or MSBFirst ala X.
106 static int host_byte_order( void )
109 char *cptr
= (char *) &i
;
110 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
115 * Check if the X Shared Memory extension is available.
116 * Return: 0 = not available
117 * 1 = shared XImage support available
118 * 2 = shared Pixmap support available also
120 int xmesa_check_for_xshm( Display
*display
)
122 #if defined(USE_XSHM)
123 int major
, minor
, ignore
;
126 if (getenv("SP_NO_RAST"))
129 if (getenv("MESA_NOSHM")) {
133 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
134 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
135 return (pixmaps
==True
) ? 2 : 1;
145 /* No XSHM support */
152 * Return the true number of bits per pixel for XImages.
153 * For example, if we request a 24-bit deep visual we may actually need/get
154 * 32bpp XImages. This function returns the appropriate bpp.
155 * Input: dpy - the X display
156 * visinfo - desribes the visual to be used for XImages
157 * Return: true number of bits per pixel for XImages
160 bits_per_pixel( XMesaVisual xmv
)
162 Display
*dpy
= xmv
->display
;
163 XVisualInfo
* visinfo
= xmv
->visinfo
;
166 /* Create a temporary XImage */
167 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
168 ZPixmap
, 0, /*format, offset*/
169 (char*) MALLOC(8), /*data*/
170 1, 1, /*width, height*/
175 /* grab the bits/pixel value */
176 bitsPerPixel
= img
->bits_per_pixel
;
177 /* free the XImage */
180 XDestroyImage( img
);
187 * Determine if a given X window ID is valid (window exists).
188 * Do this by calling XGetWindowAttributes() for the window and
189 * checking if we catch an X error.
190 * Input: dpy - the display
191 * win - the window to check for existance
192 * Return: GL_TRUE - window exists
193 * GL_FALSE - window doesn't exist
195 static GLboolean WindowExistsFlag
;
197 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
200 if (xerr
->error_code
== BadWindow
) {
201 WindowExistsFlag
= GL_FALSE
;
206 static GLboolean
window_exists( Display
*dpy
, Window win
)
208 XWindowAttributes wa
;
209 int (*old_handler
)( Display
*, XErrorEvent
* );
210 WindowExistsFlag
= GL_TRUE
;
211 old_handler
= XSetErrorHandler(window_exists_err_handler
);
212 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
213 XSetErrorHandler(old_handler
);
214 return WindowExistsFlag
;
218 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
223 unsigned int w
, h
, bw
, depth
;
224 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
232 * Return the size of the window (or pixmap) that corresponds to the
234 * \param width returns width in pixels
235 * \param height returns height in pixels
238 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
239 GLuint
*width
, GLuint
*height
)
243 pipe_mutex_lock(_xmesa_lock
);
244 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
245 stat
= get_drawable_size(dpy
, b
->drawable
, width
, height
);
246 pipe_mutex_unlock(_xmesa_lock
);
249 /* probably querying a window that's recently been destroyed */
250 _mesa_warning(NULL
, "XGetGeometry failed!\n");
251 *width
= *height
= 1;
255 #define GET_REDMASK(__v) __v->mesa_visual.redMask
256 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
257 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
261 * Choose the pixel format for the given visual.
262 * This will tell the gallium driver how to pack pixel data into
266 choose_pixel_format(XMesaVisual v
)
268 boolean native_byte_order
= (host_byte_order() ==
269 ImageByteOrder(v
->display
));
271 if ( GET_REDMASK(v
) == 0x0000ff
272 && GET_GREENMASK(v
) == 0x00ff00
273 && GET_BLUEMASK(v
) == 0xff0000
274 && v
->BitsPerPixel
== 32) {
275 if (native_byte_order
) {
276 /* no byteswapping needed */
277 return PIPE_FORMAT_R8G8B8A8_UNORM
;
280 return PIPE_FORMAT_A8B8G8R8_UNORM
;
283 else if ( GET_REDMASK(v
) == 0xff0000
284 && GET_GREENMASK(v
) == 0x00ff00
285 && GET_BLUEMASK(v
) == 0x0000ff
286 && v
->BitsPerPixel
== 32) {
287 if (native_byte_order
) {
288 /* no byteswapping needed */
289 return PIPE_FORMAT_B8G8R8A8_UNORM
;
292 return PIPE_FORMAT_A8R8G8B8_UNORM
;
295 else if ( GET_REDMASK(v
) == 0x0000ff00
296 && GET_GREENMASK(v
) == 0x00ff0000
297 && GET_BLUEMASK(v
) == 0xff000000
298 && v
->BitsPerPixel
== 32) {
299 if (native_byte_order
) {
300 /* no byteswapping needed */
301 return PIPE_FORMAT_A8R8G8B8_UNORM
;
304 return PIPE_FORMAT_B8G8R8A8_UNORM
;
307 else if ( GET_REDMASK(v
) == 0xf800
308 && GET_GREENMASK(v
) == 0x07e0
309 && GET_BLUEMASK(v
) == 0x001f
311 && v
->BitsPerPixel
== 16) {
313 return PIPE_FORMAT_B5G6R5_UNORM
;
323 * Query the default gallium screen for a Z/Stencil format that
324 * at least matches the given depthBits and stencilBits.
327 xmesa_choose_z_stencil_format(int depth
, int stencil
,
328 enum pipe_format
*depthFormat
,
329 enum pipe_format
*stencilFormat
)
331 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
332 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
333 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
334 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
335 enum pipe_format formats
[8];
340 if (depth
<= 16 && stencil
== 0) {
341 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
343 if (depth
<= 24 && stencil
== 0) {
344 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
345 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
347 if (depth
<= 24 && stencil
<= 8) {
348 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
349 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
351 if (depth
<= 32 && stencil
== 0) {
352 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
355 *depthFormat
= PIPE_FORMAT_NONE
;
356 for (i
= 0; i
< count
; i
++) {
357 if (screen
->is_format_supported(screen
, formats
[i
],
358 target
, tex_usage
, geom_flags
)) {
359 *depthFormat
= formats
[i
];
365 *stencilFormat
= *depthFormat
;
368 *stencilFormat
= PIPE_FORMAT_NONE
;
374 /**********************************************************************/
375 /***** Linked list of XMesaBuffers *****/
376 /**********************************************************************/
378 XMesaBuffer XMesaBufferList
= NULL
;
382 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
383 * Note that XMesaBuffer is derived from GLframebuffer.
384 * The new XMesaBuffer will not have any size (Width=Height=0).
386 * \param d the corresponding X drawable (window or pixmap)
387 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
388 * \param vis the buffer's visual
389 * \param cmap the window's colormap, if known.
390 * \return new XMesaBuffer or NULL if any problem
393 create_xmesa_buffer(Drawable d
, BufferType type
,
394 XMesaVisual vis
, Colormap cmap
)
398 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
401 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
403 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
413 /* determine PIPE_FORMATs for buffers */
414 colorFormat
= choose_pixel_format(vis
);
416 xmesa_choose_z_stencil_format(vis
->mesa_visual
.depthBits
,
417 vis
->mesa_visual
.stencilBits
,
418 &depthFormat
, &stencilFormat
);
421 get_drawable_size(vis
->display
, d
, &width
, &height
);
424 * Create framebuffer, but we'll plug in our own renderbuffers below.
426 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
427 colorFormat
, depthFormat
, stencilFormat
,
433 * Create scratch XImage for xmesa_display_surface()
435 b
->tempImage
= XCreateImage(vis
->display
,
436 vis
->visinfo
->visual
,
438 ZPixmap
, 0, /* format, offset */
442 0); /* bytes_per_line */
444 /* GLX_EXT_texture_from_pixmap */
445 b
->TextureTarget
= 0;
446 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
447 b
->TextureMipmap
= 0;
449 /* insert buffer into linked list */
450 b
->Next
= XMesaBufferList
;
458 * Find an XMesaBuffer by matching X display and colormap but NOT matching
459 * the notThis buffer.
462 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
465 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
466 if (b
->xm_visual
->display
== dpy
&&
477 * Remove buffer from linked list, delete if no longer referenced.
480 xmesa_free_buffer(XMesaBuffer buffer
)
482 XMesaBuffer prev
= NULL
, b
;
484 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
486 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
488 /* unlink buffer from list */
490 prev
->Next
= buffer
->Next
;
492 XMesaBufferList
= buffer
->Next
;
494 /* mark as delete pending */
495 fb
->DeletePending
= GL_TRUE
;
497 /* Since the X window for the XMesaBuffer is going away, we don't
498 * want to dereference this pointer in the future.
502 buffer
->tempImage
->data
= NULL
;
503 XDestroyImage(buffer
->tempImage
);
505 /* Unreference. If count = zero we'll really delete the buffer */
506 _mesa_reference_framebuffer(&fb
, NULL
);
508 XFreeGC(b
->xm_visual
->display
, b
->gc
);
514 /* continue search */
517 /* buffer not found in XMesaBufferList */
518 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
523 /**********************************************************************/
524 /***** Misc Private Functions *****/
525 /**********************************************************************/
529 * When a context is bound for the first time, we can finally finish
530 * initializing the context's visual and buffer information.
531 * \param v the XMesaVisual to initialize
532 * \param b the XMesaBuffer to initialize (may be NULL)
533 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
534 * \param window the window/pixmap we're rendering into
535 * \param cmap the colormap associated with the window/pixmap
536 * \return GL_TRUE=success, GL_FALSE=failure
539 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
540 GLboolean rgb_flag
, Drawable window
,
543 ASSERT(!b
|| b
->xm_visual
== v
);
545 /* Save true bits/pixel */
546 v
->BitsPerPixel
= bits_per_pixel(v
);
547 assert(v
->BitsPerPixel
> 0);
549 if (rgb_flag
== GL_FALSE
) {
550 /* COLOR-INDEXED WINDOW: not supported*/
555 * We support RGB rendering into almost any kind of visual.
557 const int xclass
= v
->mesa_visual
.visualType
;
558 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
560 "XMesa: RGB mode rendering not supported in given visual.\n");
563 v
->mesa_visual
.indexBits
= 0;
565 if (v
->BitsPerPixel
== 32) {
566 /* We use XImages for all front/back buffers. If an X Window or
567 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
568 * will be preserved. For XImages we're in luck.
570 v
->mesa_visual
.alphaBits
= 8;
575 * If MESA_INFO env var is set print out some debugging info
576 * which can help Brian figure out what's going on when a user
579 if (_mesa_getenv("MESA_INFO")) {
580 printf("X/Mesa visual = %p\n", (void *) v
);
581 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
582 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
583 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
587 /* these should have been set in create_xmesa_buffer */
588 ASSERT(b
->drawable
== window
);
590 /* Setup for single/double buffering */
591 if (v
->mesa_visual
.doubleBufferMode
) {
592 /* Double buffered */
593 b
->shm
= xmesa_check_for_xshm( v
->display
);
596 /* X11 graphics context */
597 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
598 XSetFunction( v
->display
, b
->gc
, GXcopy
);
606 #define NUM_VISUAL_TYPES 6
609 * Convert an X visual type to a GLX visual type.
611 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
613 * \return If \c visualType is a valid X visual type, a GLX visual type will
614 * be returned. Otherwise \c GLX_NONE will be returned.
617 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
621 xmesa_convert_from_x_visual_type( int visualType
)
623 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
624 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
625 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
626 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
629 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
630 ? glx_visual_types
[ visualType
] : GLX_NONE
;
634 /**********************************************************************/
635 /***** Public Functions *****/
636 /**********************************************************************/
640 * Create a new X/Mesa visual.
641 * Input: display - X11 display
642 * visinfo - an XVisualInfo pointer
643 * rgb_flag - GL_TRUE = RGB mode,
644 * GL_FALSE = color index mode
645 * alpha_flag - alpha buffer requested?
646 * db_flag - GL_TRUE = double-buffered,
647 * GL_FALSE = single buffered
648 * stereo_flag - stereo visual?
649 * ximage_flag - GL_TRUE = use an XImage for back buffer,
650 * GL_FALSE = use an off-screen pixmap for back buffer
651 * depth_size - requested bits/depth values, or zero
652 * stencil_size - requested bits/stencil values, or zero
653 * accum_red_size - requested bits/red accum values, or zero
654 * accum_green_size - requested bits/green accum values, or zero
655 * accum_blue_size - requested bits/blue accum values, or zero
656 * accum_alpha_size - requested bits/alpha accum values, or zero
657 * num_samples - number of samples/pixel if multisampling, or zero
658 * level - visual level, usually 0
659 * visualCaveat - ala the GLX extension, usually GLX_NONE
660 * Return; a new XMesaVisual or 0 if error.
663 XMesaVisual
XMesaCreateVisual( Display
*display
,
664 XVisualInfo
* visinfo
,
666 GLboolean alpha_flag
,
668 GLboolean stereo_flag
,
669 GLboolean ximage_flag
,
672 GLint accum_red_size
,
673 GLint accum_green_size
,
674 GLint accum_blue_size
,
675 GLint accum_alpha_size
,
681 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
685 /* For debugging only */
686 if (_mesa_getenv("MESA_XSYNC")) {
687 /* This makes debugging X easier.
688 * In your debugger, set a breakpoint on _XError to stop when an
689 * X protocol error is generated.
691 XSynchronize( display
, 1 );
694 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
699 v
->display
= display
;
701 /* Save a copy of the XVisualInfo struct because the user may Xfree()
702 * the struct but we may need some of the information contained in it
705 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
710 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
712 v
->ximage_flag
= ximage_flag
;
714 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
715 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
716 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
717 v
->mesa_visual
.visualID
= visinfo
->visualid
;
718 v
->mesa_visual
.screen
= visinfo
->screen
;
720 #if !(defined(__cplusplus) || defined(c_plusplus))
721 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
723 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
726 v
->mesa_visual
.visualRating
= visualCaveat
;
729 v
->mesa_visual
.alphaBits
= 8;
731 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
734 const int xclass
= v
->mesa_visual
.visualType
;
735 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
736 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
737 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
738 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
741 /* this is an approximation */
743 depth
= v
->visinfo
->depth
;
744 red_bits
= depth
/ 3;
746 green_bits
= depth
/ 2;
750 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
752 alpha_bits
= v
->mesa_visual
.alphaBits
;
755 _mesa_initialize_visual( &v
->mesa_visual
,
756 db_flag
, stereo_flag
,
757 red_bits
, green_bits
,
758 blue_bits
, alpha_bits
,
761 accum_red_size
, accum_green_size
,
762 accum_blue_size
, accum_alpha_size
,
766 v
->mesa_visual
.level
= level
;
772 void XMesaDestroyVisual( XMesaVisual v
)
780 * Do one-time initializations.
785 static GLboolean firstTime
= GL_TRUE
;
787 pipe_mutex_init(_xmesa_lock
);
788 _screen
= driver
.create_pipe_screen();
789 screen
= trace_screen_create( _screen
);
790 firstTime
= GL_FALSE
;
796 * Create a new XMesaContext.
797 * \param v the XMesaVisual
798 * \param share_list another XMesaContext with which to share display
799 * lists or NULL if no sharing is wanted.
800 * \return an XMesaContext or NULL if error.
803 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
805 struct pipe_context
*pipe
= NULL
;
812 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
813 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
817 pf
= choose_pixel_format(v
);
821 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
822 c
->xm_read_buffer
= NULL
;
827 /* Trace screen knows how to properly wrap context creation in the
828 * wrapped screen, so nothing special to do here:
830 pipe
= screen
->context_create(screen
, (void *) c
);
834 c
->st
= st_create_context(pipe
,
836 share_list
? share_list
->st
: NULL
);
840 mesaCtx
= c
->st
->ctx
;
841 c
->st
->ctx
->DriverCtx
= c
;
847 st_destroy_context(c
->st
);
858 void XMesaDestroyContext( XMesaContext c
)
860 st_destroy_context(c
->st
);
862 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
863 * outlive it, causing segfaults
864 struct pipe_screen *screen = c->st->pipe->screen;
865 screen->destroy(screen);
874 * Private function for creating an XMesaBuffer which corresponds to an
875 * X window or pixmap.
876 * \param v the window's XMesaVisual
877 * \param w the window we're wrapping
878 * \return new XMesaBuffer or NULL if error
881 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
883 XWindowAttributes attr
;
891 /* Check that window depth matches visual depth */
892 XGetWindowAttributes( v
->display
, w
, &attr
);
894 if (v
->visinfo
->depth
!= depth
) {
895 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
896 v
->visinfo
->depth
, depth
);
902 cmap
= attr
.colormap
;
905 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
906 /* this is weird, a window w/out a colormap!? */
907 /* OK, let's just allocate a new one and hope for the best */
908 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
911 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
915 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
916 (Drawable
) w
, cmap
)) {
917 xmesa_free_buffer(b
);
927 * Create a new XMesaBuffer from an X pixmap.
929 * \param v the XMesaVisual
930 * \param p the pixmap
931 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
932 * \c GLX_DIRECT_COLOR visual for the pixmap
933 * \returns new XMesaBuffer or NULL if error
936 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
942 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
946 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
947 (Drawable
) p
, cmap
)) {
948 xmesa_free_buffer(b
);
957 * For GLX_EXT_texture_from_pixmap
960 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
962 int format
, int target
, int mipmap
)
964 GET_CURRENT_CONTEXT(ctx
);
966 GLuint width
, height
;
970 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
974 /* get pixmap size, update framebuffer/renderbuffer dims */
975 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
976 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
980 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
981 target
= GLX_TEXTURE_2D_EXT
;
983 else if ( _mesa_bitcount(width
) == 1
984 && _mesa_bitcount(height
) == 1) {
985 /* power of two size */
987 target
= GLX_TEXTURE_1D_EXT
;
990 target
= GLX_TEXTURE_2D_EXT
;
993 else if (ctx
->Extensions
.NV_texture_rectangle
) {
994 target
= GLX_TEXTURE_RECTANGLE_EXT
;
997 /* non power of two textures not supported */
998 XMesaDestroyBuffer(b
);
1003 b
->TextureTarget
= target
;
1004 b
->TextureFormat
= format
;
1005 b
->TextureMipmap
= mipmap
;
1007 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1008 (Drawable
) p
, cmap
)) {
1009 xmesa_free_buffer(b
);
1019 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1020 unsigned int width
, unsigned int height
)
1023 Drawable drawable
; /* X Pixmap Drawable */
1026 /* allocate pixmap for front buffer */
1027 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1028 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1033 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1037 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1039 xmesa_free_buffer(b
);
1049 * Deallocate an XMesaBuffer structure and all related info.
1052 XMesaDestroyBuffer(XMesaBuffer b
)
1054 xmesa_free_buffer(b
);
1059 * Query the current window size and update the corresponding GLframebuffer
1060 * and all attached renderbuffers.
1062 * 1. the first time a buffer is bound to a context.
1063 * 2. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1064 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1065 * when resizing a buffer when no rendering context is bound.
1068 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1070 GLuint width
, height
;
1071 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1072 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1079 * Bind buffer b to context c and make c the current rendering context.
1082 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1083 XMesaBuffer readBuffer
)
1085 XMesaContext old_ctx
= XMesaGetCurrentContext();
1087 if (old_ctx
&& old_ctx
!= c
) {
1088 XMesaFlush(old_ctx
);
1089 old_ctx
->xm_buffer
= NULL
;
1090 old_ctx
->xm_read_buffer
= NULL
;
1094 if (!drawBuffer
|| !readBuffer
)
1095 return GL_FALSE
; /* must specify buffers! */
1098 c
->xm_buffer
== drawBuffer
&&
1099 c
->xm_read_buffer
== readBuffer
)
1102 c
->xm_buffer
= drawBuffer
;
1103 c
->xm_read_buffer
= readBuffer
;
1105 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1107 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1108 if (readBuffer
!= drawBuffer
)
1109 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1111 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1112 drawBuffer
->wasCurrent
= GL_TRUE
;
1116 st_make_current( NULL
, NULL
, NULL
);
1124 * Unbind the context c from its buffer.
1126 GLboolean
XMesaUnbindContext( XMesaContext c
)
1128 /* A no-op for XFree86 integration purposes */
1133 XMesaContext
XMesaGetCurrentContext( void )
1135 GET_CURRENT_CONTEXT(ctx
);
1137 XMesaContext xmesa
= xmesa_context(ctx
);
1148 * Swap front and back color buffers and have winsys display front buffer.
1149 * If there's no front color buffer no swap actually occurs.
1152 void XMesaSwapBuffers( XMesaBuffer b
)
1154 struct pipe_surface
*frontLeftSurf
;
1156 st_swapbuffers(b
->stfb
, &frontLeftSurf
, NULL
);
1158 if (frontLeftSurf
) {
1159 if (_screen
!= screen
) {
1160 struct trace_surface
*tr_surf
= trace_surface( frontLeftSurf
);
1161 struct pipe_surface
*surf
= tr_surf
->surface
;
1162 frontLeftSurf
= surf
;
1165 driver
.display_surface(b
, frontLeftSurf
);
1168 xmesa_check_and_update_buffer_size(NULL
, b
);
1174 * Copy sub-region of back buffer to front buffer
1176 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1178 struct pipe_surface
*surf_front
;
1179 struct pipe_surface
*surf_back
;
1180 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1182 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
, &surf_front
);
1183 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
, &surf_back
);
1185 if (!surf_front
|| !surf_back
)
1189 pipe
->surface_copy(pipe
,
1190 surf_front
, x
, y
, /* dest */
1191 surf_back
, x
, y
, /* src */
1197 void XMesaFlush( XMesaContext c
)
1199 if (c
&& c
->xm_visual
->display
) {
1201 XSync( c
->xm_visual
->display
, False
);
1209 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1212 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1213 if (b
->drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1222 * Free/destroy all XMesaBuffers associated with given display.
1224 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1226 XMesaBuffer b
, next
;
1227 for (b
= XMesaBufferList
; b
; b
= next
) {
1229 if (b
->xm_visual
->display
== dpy
) {
1230 xmesa_free_buffer(b
);
1237 * Look for XMesaBuffers whose X window has been destroyed.
1238 * Deallocate any such XMesaBuffers.
1240 void XMesaGarbageCollect( void )
1242 XMesaBuffer b
, next
;
1243 for (b
=XMesaBufferList
; b
; b
=next
) {
1246 b
->xm_visual
->display
&&
1248 b
->type
== WINDOW
) {
1249 XSync(b
->xm_visual
->display
, False
);
1250 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1251 /* found a dead window, free the ancillary info */
1252 XMesaDestroyBuffer( b
);
1262 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1263 const int *attrib_list
)
1270 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)