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 depthBits
, int stencilBits
,
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 static enum pipe_format formats
[] = {
336 PIPE_FORMAT_S8Z24_UNORM
,
337 PIPE_FORMAT_Z24S8_UNORM
,
338 PIPE_FORMAT_Z16_UNORM
,
339 PIPE_FORMAT_Z32_UNORM
345 *depthFormat
= *stencilFormat
= PIPE_FORMAT_NONE
;
347 /* search for supported format */
348 for (i
= 0; i
< Elements(formats
); i
++) {
349 if (screen
->is_format_supported(screen
, formats
[i
],
350 target
, tex_usage
, geom_flags
)) {
351 *depthFormat
= formats
[i
];
357 *stencilFormat
= *depthFormat
;
360 /* XXX we should check that he chosen format has at least as many bits
361 * as what was requested.
367 /**********************************************************************/
368 /***** Linked list of XMesaBuffers *****/
369 /**********************************************************************/
371 XMesaBuffer XMesaBufferList
= NULL
;
375 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
376 * Note that XMesaBuffer is derived from GLframebuffer.
377 * The new XMesaBuffer will not have any size (Width=Height=0).
379 * \param d the corresponding X drawable (window or pixmap)
380 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
381 * \param vis the buffer's visual
382 * \param cmap the window's colormap, if known.
383 * \return new XMesaBuffer or NULL if any problem
386 create_xmesa_buffer(Drawable d
, BufferType type
,
387 XMesaVisual vis
, Colormap cmap
)
391 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
394 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
396 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
406 /* determine PIPE_FORMATs for buffers */
407 colorFormat
= choose_pixel_format(vis
);
409 xmesa_choose_z_stencil_format(vis
->mesa_visual
.depthBits
,
410 vis
->mesa_visual
.stencilBits
,
411 &depthFormat
, &stencilFormat
);
414 get_drawable_size(vis
->display
, d
, &width
, &height
);
417 * Create framebuffer, but we'll plug in our own renderbuffers below.
419 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
420 colorFormat
, depthFormat
, stencilFormat
,
426 * Create scratch XImage for xmesa_display_surface()
428 b
->tempImage
= XCreateImage(vis
->display
,
429 vis
->visinfo
->visual
,
431 ZPixmap
, 0, /* format, offset */
435 0); /* bytes_per_line */
437 /* GLX_EXT_texture_from_pixmap */
438 b
->TextureTarget
= 0;
439 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
440 b
->TextureMipmap
= 0;
442 /* insert buffer into linked list */
443 b
->Next
= XMesaBufferList
;
451 * Find an XMesaBuffer by matching X display and colormap but NOT matching
452 * the notThis buffer.
455 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
458 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
459 if (b
->xm_visual
->display
== dpy
&&
470 * Remove buffer from linked list, delete if no longer referenced.
473 xmesa_free_buffer(XMesaBuffer buffer
)
475 XMesaBuffer prev
= NULL
, b
;
477 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
479 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
481 /* unlink buffer from list */
483 prev
->Next
= buffer
->Next
;
485 XMesaBufferList
= buffer
->Next
;
487 /* mark as delete pending */
488 fb
->DeletePending
= GL_TRUE
;
490 /* Since the X window for the XMesaBuffer is going away, we don't
491 * want to dereference this pointer in the future.
495 buffer
->tempImage
->data
= NULL
;
496 XDestroyImage(buffer
->tempImage
);
498 /* Unreference. If count = zero we'll really delete the buffer */
499 _mesa_reference_framebuffer(&fb
, NULL
);
501 XFreeGC(b
->xm_visual
->display
, b
->gc
);
507 /* continue search */
510 /* buffer not found in XMesaBufferList */
511 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
516 /**********************************************************************/
517 /***** Misc Private Functions *****/
518 /**********************************************************************/
522 * When a context is bound for the first time, we can finally finish
523 * initializing the context's visual and buffer information.
524 * \param v the XMesaVisual to initialize
525 * \param b the XMesaBuffer to initialize (may be NULL)
526 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
527 * \param window the window/pixmap we're rendering into
528 * \param cmap the colormap associated with the window/pixmap
529 * \return GL_TRUE=success, GL_FALSE=failure
532 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
533 GLboolean rgb_flag
, Drawable window
,
536 ASSERT(!b
|| b
->xm_visual
== v
);
538 /* Save true bits/pixel */
539 v
->BitsPerPixel
= bits_per_pixel(v
);
540 assert(v
->BitsPerPixel
> 0);
542 if (rgb_flag
== GL_FALSE
) {
543 /* COLOR-INDEXED WINDOW: not supported*/
548 * We support RGB rendering into almost any kind of visual.
550 const int xclass
= v
->mesa_visual
.visualType
;
551 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
553 "XMesa: RGB mode rendering not supported in given visual.\n");
556 v
->mesa_visual
.indexBits
= 0;
558 if (v
->BitsPerPixel
== 32) {
559 /* We use XImages for all front/back buffers. If an X Window or
560 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
561 * will be preserved. For XImages we're in luck.
563 v
->mesa_visual
.alphaBits
= 8;
568 * If MESA_INFO env var is set print out some debugging info
569 * which can help Brian figure out what's going on when a user
572 if (_mesa_getenv("MESA_INFO")) {
573 printf("X/Mesa visual = %p\n", (void *) v
);
574 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
575 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
576 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
580 /* these should have been set in create_xmesa_buffer */
581 ASSERT(b
->drawable
== window
);
583 /* Setup for single/double buffering */
584 if (v
->mesa_visual
.doubleBufferMode
) {
585 /* Double buffered */
586 b
->shm
= xmesa_check_for_xshm( v
->display
);
589 /* X11 graphics context */
590 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
591 XSetFunction( v
->display
, b
->gc
, GXcopy
);
599 #define NUM_VISUAL_TYPES 6
602 * Convert an X visual type to a GLX visual type.
604 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
606 * \return If \c visualType is a valid X visual type, a GLX visual type will
607 * be returned. Otherwise \c GLX_NONE will be returned.
610 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
614 xmesa_convert_from_x_visual_type( int visualType
)
616 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
617 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
618 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
619 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
622 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
623 ? glx_visual_types
[ visualType
] : GLX_NONE
;
627 /**********************************************************************/
628 /***** Public Functions *****/
629 /**********************************************************************/
633 * Create a new X/Mesa visual.
634 * Input: display - X11 display
635 * visinfo - an XVisualInfo pointer
636 * rgb_flag - GL_TRUE = RGB mode,
637 * GL_FALSE = color index mode
638 * alpha_flag - alpha buffer requested?
639 * db_flag - GL_TRUE = double-buffered,
640 * GL_FALSE = single buffered
641 * stereo_flag - stereo visual?
642 * ximage_flag - GL_TRUE = use an XImage for back buffer,
643 * GL_FALSE = use an off-screen pixmap for back buffer
644 * depth_size - requested bits/depth values, or zero
645 * stencil_size - requested bits/stencil values, or zero
646 * accum_red_size - requested bits/red accum values, or zero
647 * accum_green_size - requested bits/green accum values, or zero
648 * accum_blue_size - requested bits/blue accum values, or zero
649 * accum_alpha_size - requested bits/alpha accum values, or zero
650 * num_samples - number of samples/pixel if multisampling, or zero
651 * level - visual level, usually 0
652 * visualCaveat - ala the GLX extension, usually GLX_NONE
653 * Return; a new XMesaVisual or 0 if error.
656 XMesaVisual
XMesaCreateVisual( Display
*display
,
657 XVisualInfo
* visinfo
,
659 GLboolean alpha_flag
,
661 GLboolean stereo_flag
,
662 GLboolean ximage_flag
,
665 GLint accum_red_size
,
666 GLint accum_green_size
,
667 GLint accum_blue_size
,
668 GLint accum_alpha_size
,
674 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
678 /* For debugging only */
679 if (_mesa_getenv("MESA_XSYNC")) {
680 /* This makes debugging X easier.
681 * In your debugger, set a breakpoint on _XError to stop when an
682 * X protocol error is generated.
684 XSynchronize( display
, 1 );
687 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
692 v
->display
= display
;
694 /* Save a copy of the XVisualInfo struct because the user may Xfree()
695 * the struct but we may need some of the information contained in it
698 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
703 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
705 v
->ximage_flag
= ximage_flag
;
707 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
708 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
709 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
710 v
->mesa_visual
.visualID
= visinfo
->visualid
;
711 v
->mesa_visual
.screen
= visinfo
->screen
;
713 #if !(defined(__cplusplus) || defined(c_plusplus))
714 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
716 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
719 v
->mesa_visual
.visualRating
= visualCaveat
;
722 v
->mesa_visual
.alphaBits
= 8;
724 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
727 const int xclass
= v
->mesa_visual
.visualType
;
728 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
729 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
730 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
731 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
734 /* this is an approximation */
736 depth
= v
->visinfo
->depth
;
737 red_bits
= depth
/ 3;
739 green_bits
= depth
/ 2;
743 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
745 alpha_bits
= v
->mesa_visual
.alphaBits
;
748 _mesa_initialize_visual( &v
->mesa_visual
,
749 db_flag
, stereo_flag
,
750 red_bits
, green_bits
,
751 blue_bits
, alpha_bits
,
754 accum_red_size
, accum_green_size
,
755 accum_blue_size
, accum_alpha_size
,
759 v
->mesa_visual
.level
= level
;
765 void XMesaDestroyVisual( XMesaVisual v
)
773 * Do one-time initializations.
778 static GLboolean firstTime
= GL_TRUE
;
780 pipe_mutex_init(_xmesa_lock
);
781 _screen
= driver
.create_pipe_screen();
782 screen
= trace_screen_create( _screen
);
783 firstTime
= GL_FALSE
;
789 * Create a new XMesaContext.
790 * \param v the XMesaVisual
791 * \param share_list another XMesaContext with which to share display
792 * lists or NULL if no sharing is wanted.
793 * \return an XMesaContext or NULL if error.
796 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
798 struct pipe_context
*pipe
= NULL
;
805 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
806 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
810 pf
= choose_pixel_format(v
);
814 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
815 c
->xm_read_buffer
= NULL
;
820 /* Trace screen knows how to properly wrap context creation in the
821 * wrapped screen, so nothing special to do here:
823 pipe
= screen
->context_create(screen
, (void *) c
);
827 c
->st
= st_create_context(pipe
,
829 share_list
? share_list
->st
: NULL
);
833 mesaCtx
= c
->st
->ctx
;
834 c
->st
->ctx
->DriverCtx
= c
;
840 st_destroy_context(c
->st
);
851 void XMesaDestroyContext( XMesaContext c
)
853 st_destroy_context(c
->st
);
855 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
856 * outlive it, causing segfaults
857 struct pipe_screen *screen = c->st->pipe->screen;
858 screen->destroy(screen);
867 * Private function for creating an XMesaBuffer which corresponds to an
868 * X window or pixmap.
869 * \param v the window's XMesaVisual
870 * \param w the window we're wrapping
871 * \return new XMesaBuffer or NULL if error
874 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
876 XWindowAttributes attr
;
884 /* Check that window depth matches visual depth */
885 XGetWindowAttributes( v
->display
, w
, &attr
);
887 if (v
->visinfo
->depth
!= depth
) {
888 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
889 v
->visinfo
->depth
, depth
);
895 cmap
= attr
.colormap
;
898 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
899 /* this is weird, a window w/out a colormap!? */
900 /* OK, let's just allocate a new one and hope for the best */
901 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
904 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
908 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
909 (Drawable
) w
, cmap
)) {
910 xmesa_free_buffer(b
);
920 * Create a new XMesaBuffer from an X pixmap.
922 * \param v the XMesaVisual
923 * \param p the pixmap
924 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
925 * \c GLX_DIRECT_COLOR visual for the pixmap
926 * \returns new XMesaBuffer or NULL if error
929 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
935 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
939 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
940 (Drawable
) p
, cmap
)) {
941 xmesa_free_buffer(b
);
950 * For GLX_EXT_texture_from_pixmap
953 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
955 int format
, int target
, int mipmap
)
957 GET_CURRENT_CONTEXT(ctx
);
959 GLuint width
, height
;
963 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
967 /* get pixmap size, update framebuffer/renderbuffer dims */
968 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
969 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
973 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
974 target
= GLX_TEXTURE_2D_EXT
;
976 else if ( _mesa_bitcount(width
) == 1
977 && _mesa_bitcount(height
) == 1) {
978 /* power of two size */
980 target
= GLX_TEXTURE_1D_EXT
;
983 target
= GLX_TEXTURE_2D_EXT
;
986 else if (ctx
->Extensions
.NV_texture_rectangle
) {
987 target
= GLX_TEXTURE_RECTANGLE_EXT
;
990 /* non power of two textures not supported */
991 XMesaDestroyBuffer(b
);
996 b
->TextureTarget
= target
;
997 b
->TextureFormat
= format
;
998 b
->TextureMipmap
= mipmap
;
1000 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1001 (Drawable
) p
, cmap
)) {
1002 xmesa_free_buffer(b
);
1012 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1013 unsigned int width
, unsigned int height
)
1016 Drawable drawable
; /* X Pixmap Drawable */
1019 /* allocate pixmap for front buffer */
1020 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1021 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1026 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1030 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1032 xmesa_free_buffer(b
);
1042 * Deallocate an XMesaBuffer structure and all related info.
1045 XMesaDestroyBuffer(XMesaBuffer b
)
1047 xmesa_free_buffer(b
);
1052 * Query the current window size and update the corresponding GLframebuffer
1053 * and all attached renderbuffers.
1055 * 1. the first time a buffer is bound to a context.
1056 * 2. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1057 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1058 * when resizing a buffer when no rendering context is bound.
1061 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1063 GLuint width
, height
;
1064 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1065 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1072 * Bind buffer b to context c and make c the current rendering context.
1075 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1076 XMesaBuffer readBuffer
)
1078 XMesaContext old_ctx
= XMesaGetCurrentContext();
1080 if (old_ctx
&& old_ctx
!= c
) {
1081 XMesaFlush(old_ctx
);
1082 old_ctx
->xm_buffer
= NULL
;
1083 old_ctx
->xm_read_buffer
= NULL
;
1087 if (!drawBuffer
|| !readBuffer
)
1088 return GL_FALSE
; /* must specify buffers! */
1091 c
->xm_buffer
== drawBuffer
&&
1092 c
->xm_read_buffer
== readBuffer
)
1095 c
->xm_buffer
= drawBuffer
;
1096 c
->xm_read_buffer
= readBuffer
;
1098 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1100 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1101 if (readBuffer
!= drawBuffer
)
1102 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1104 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1105 drawBuffer
->wasCurrent
= GL_TRUE
;
1109 st_make_current( NULL
, NULL
, NULL
);
1117 * Unbind the context c from its buffer.
1119 GLboolean
XMesaUnbindContext( XMesaContext c
)
1121 /* A no-op for XFree86 integration purposes */
1126 XMesaContext
XMesaGetCurrentContext( void )
1128 GET_CURRENT_CONTEXT(ctx
);
1130 XMesaContext xmesa
= xmesa_context(ctx
);
1141 * Swap front and back color buffers and have winsys display front buffer.
1142 * If there's no front color buffer no swap actually occurs.
1145 void XMesaSwapBuffers( XMesaBuffer b
)
1147 struct pipe_surface
*frontLeftSurf
;
1149 st_swapbuffers(b
->stfb
, &frontLeftSurf
, NULL
);
1151 if (frontLeftSurf
) {
1152 if (_screen
!= screen
) {
1153 struct trace_surface
*tr_surf
= trace_surface( frontLeftSurf
);
1154 struct pipe_surface
*surf
= tr_surf
->surface
;
1155 frontLeftSurf
= surf
;
1158 driver
.display_surface(b
, frontLeftSurf
);
1161 xmesa_check_and_update_buffer_size(NULL
, b
);
1167 * Copy sub-region of back buffer to front buffer
1169 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1171 struct pipe_surface
*surf_front
;
1172 struct pipe_surface
*surf_back
;
1173 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1175 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
, &surf_front
);
1176 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
, &surf_back
);
1178 if (!surf_front
|| !surf_back
)
1182 pipe
->surface_copy(pipe
,
1183 surf_front
, x
, y
, /* dest */
1184 surf_back
, x
, y
, /* src */
1190 void XMesaFlush( XMesaContext c
)
1192 if (c
&& c
->xm_visual
->display
) {
1194 XSync( c
->xm_visual
->display
, False
);
1202 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1205 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1206 if (b
->drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1215 * Free/destroy all XMesaBuffers associated with given display.
1217 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1219 XMesaBuffer b
, next
;
1220 for (b
= XMesaBufferList
; b
; b
= next
) {
1222 if (b
->xm_visual
->display
== dpy
) {
1223 xmesa_free_buffer(b
);
1230 * Look for XMesaBuffers whose X window has been destroyed.
1231 * Deallocate any such XMesaBuffers.
1233 void XMesaGarbageCollect( void )
1235 XMesaBuffer b
, next
;
1236 for (b
=XMesaBufferList
; b
; b
=next
) {
1239 b
->xm_visual
->display
&&
1241 b
->type
== WINDOW
) {
1242 XSync(b
->xm_visual
->display
, False
);
1243 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1244 /* found a dead window, free the ancillary info */
1245 XMesaDestroyBuffer( b
);
1255 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1256 const int *attrib_list
)
1263 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)