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.
39 * Byte swapping: If the Mesa host and the X display use a different
40 * byte order then there's some trickiness to be aware of when using
41 * XImages. The byte ordering used for the XImage is that of the X
42 * display, not the Mesa host.
43 * The color-to-pixel encoding for True/DirectColor must be done
44 * according to the display's visual red_mask, green_mask, and blue_mask.
45 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
46 * do byte swapping if needed. If one wants to directly "poke" the pixel
47 * into the XImage's buffer then the pixel must be byte swapped first.
59 #include "main/context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_screen.h"
62 #include "pipe/p_context.h"
64 #include "xm_public.h"
68 /* Driver interface routines, set up by xlib backend on library
69 * _init(). These are global in the same way that function names are
72 static struct xm_driver driver
;
73 static struct st_api
*stapi
;
75 void xmesa_set_driver( const struct xm_driver
*templ
)
78 stapi
= driver
.create_st_api();
83 * XXX replace this with a linked list, or better yet, try to attach the
84 * gallium/mesa extra bits to the X Display object with XAddExtension().
86 #define MAX_DISPLAYS 10
87 static struct xmesa_display Displays
[MAX_DISPLAYS
];
88 static int NumDisplays
= 0;
92 xmesa_init_display( Display
*display
)
94 pipe_static_mutex(init_mutex
);
98 pipe_mutex_lock(init_mutex
);
100 /* Look for XMesaDisplay which corresponds to 'display' */
101 for (i
= 0; i
< NumDisplays
; i
++) {
102 if (Displays
[i
].display
== display
) {
104 pipe_mutex_unlock(init_mutex
);
109 /* Create new XMesaDisplay */
111 assert(NumDisplays
< MAX_DISPLAYS
);
112 xmdpy
= &Displays
[NumDisplays
];
115 if (!xmdpy
->display
&& display
) {
116 xmdpy
->display
= display
;
117 xmdpy
->screen
= driver
.create_pipe_screen(display
);
118 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
120 xmdpy
->smapi
->screen
= xmdpy
->screen
;
122 if (xmdpy
->screen
&& xmdpy
->smapi
) {
123 pipe_mutex_init(xmdpy
->mutex
);
127 xmdpy
->screen
->destroy(xmdpy
->screen
);
128 xmdpy
->screen
= NULL
;
135 xmdpy
->display
= NULL
;
138 if (!xmdpy
->display
|| xmdpy
->display
!= display
)
141 pipe_mutex_unlock(init_mutex
);
146 /**********************************************************************/
147 /***** X Utility Functions *****/
148 /**********************************************************************/
152 * Return the host's byte order as LSBFirst or MSBFirst ala X.
154 static int host_byte_order( void )
157 char *cptr
= (char *) &i
;
158 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
165 * Return the true number of bits per pixel for XImages.
166 * For example, if we request a 24-bit deep visual we may actually need/get
167 * 32bpp XImages. This function returns the appropriate bpp.
168 * Input: dpy - the X display
169 * visinfo - desribes the visual to be used for XImages
170 * Return: true number of bits per pixel for XImages
173 bits_per_pixel( XMesaVisual xmv
)
175 Display
*dpy
= xmv
->display
;
176 XVisualInfo
* visinfo
= xmv
->visinfo
;
179 /* Create a temporary XImage */
180 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
181 ZPixmap
, 0, /*format, offset*/
182 (char*) MALLOC(8), /*data*/
183 1, 1, /*width, height*/
188 /* grab the bits/pixel value */
189 bitsPerPixel
= img
->bits_per_pixel
;
190 /* free the XImage */
193 XDestroyImage( img
);
200 * Determine if a given X window ID is valid (window exists).
201 * Do this by calling XGetWindowAttributes() for the window and
202 * checking if we catch an X error.
203 * Input: dpy - the display
204 * win - the window to check for existance
205 * Return: GL_TRUE - window exists
206 * GL_FALSE - window doesn't exist
208 static GLboolean WindowExistsFlag
;
210 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
213 if (xerr
->error_code
== BadWindow
) {
214 WindowExistsFlag
= GL_FALSE
;
219 static GLboolean
window_exists( Display
*dpy
, Window win
)
221 XWindowAttributes wa
;
222 int (*old_handler
)( Display
*, XErrorEvent
* );
223 WindowExistsFlag
= GL_TRUE
;
224 old_handler
= XSetErrorHandler(window_exists_err_handler
);
225 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
226 XSetErrorHandler(old_handler
);
227 return WindowExistsFlag
;
231 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
236 unsigned int w
, h
, bw
, depth
;
237 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
245 * Return the size of the window (or pixmap) that corresponds to the
247 * \param width returns width in pixels
248 * \param height returns height in pixels
251 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
252 GLuint
*width
, GLuint
*height
)
254 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
257 pipe_mutex_lock(xmdpy
->mutex
);
258 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
259 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
260 pipe_mutex_unlock(xmdpy
->mutex
);
263 /* probably querying a window that's recently been destroyed */
264 _mesa_warning(NULL
, "XGetGeometry failed!\n");
265 *width
= *height
= 1;
269 #define GET_REDMASK(__v) __v->mesa_visual.redMask
270 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
271 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
275 * Choose the pixel format for the given visual.
276 * This will tell the gallium driver how to pack pixel data into
280 choose_pixel_format(XMesaVisual v
)
282 boolean native_byte_order
= (host_byte_order() ==
283 ImageByteOrder(v
->display
));
285 if ( GET_REDMASK(v
) == 0x0000ff
286 && GET_GREENMASK(v
) == 0x00ff00
287 && GET_BLUEMASK(v
) == 0xff0000
288 && v
->BitsPerPixel
== 32) {
289 if (native_byte_order
) {
290 /* no byteswapping needed */
291 return PIPE_FORMAT_R8G8B8A8_UNORM
;
294 return PIPE_FORMAT_A8B8G8R8_UNORM
;
297 else if ( GET_REDMASK(v
) == 0xff0000
298 && GET_GREENMASK(v
) == 0x00ff00
299 && GET_BLUEMASK(v
) == 0x0000ff
300 && v
->BitsPerPixel
== 32) {
301 if (native_byte_order
) {
302 /* no byteswapping needed */
303 return PIPE_FORMAT_B8G8R8A8_UNORM
;
306 return PIPE_FORMAT_A8R8G8B8_UNORM
;
309 else if ( GET_REDMASK(v
) == 0x0000ff00
310 && GET_GREENMASK(v
) == 0x00ff0000
311 && GET_BLUEMASK(v
) == 0xff000000
312 && v
->BitsPerPixel
== 32) {
313 if (native_byte_order
) {
314 /* no byteswapping needed */
315 return PIPE_FORMAT_A8R8G8B8_UNORM
;
318 return PIPE_FORMAT_B8G8R8A8_UNORM
;
321 else if ( GET_REDMASK(v
) == 0xf800
322 && GET_GREENMASK(v
) == 0x07e0
323 && GET_BLUEMASK(v
) == 0x001f
325 && v
->BitsPerPixel
== 16) {
327 return PIPE_FORMAT_B5G6R5_UNORM
;
330 return PIPE_FORMAT_NONE
;
335 * Choose a depth/stencil format that satisfies the given depth and
338 static enum pipe_format
339 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
341 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
342 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
343 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
344 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
345 enum pipe_format formats
[8], fmt
;
350 if (depth
<= 16 && stencil
== 0) {
351 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
353 if (depth
<= 24 && stencil
== 0) {
354 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
355 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
357 if (depth
<= 24 && stencil
<= 8) {
358 formats
[count
++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM
;
359 formats
[count
++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED
;
361 if (depth
<= 32 && stencil
== 0) {
362 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
365 fmt
= PIPE_FORMAT_NONE
;
366 for (i
= 0; i
< count
; i
++) {
367 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
368 target
, tex_usage
, geom_flags
)) {
379 /**********************************************************************/
380 /***** Linked list of XMesaBuffers *****/
381 /**********************************************************************/
383 static XMesaBuffer XMesaBufferList
= NULL
;
387 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
388 * Note that XMesaBuffer is derived from GLframebuffer.
389 * The new XMesaBuffer will not have any size (Width=Height=0).
391 * \param d the corresponding X drawable (window or pixmap)
392 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
393 * \param vis the buffer's visual
394 * \param cmap the window's colormap, if known.
395 * \return new XMesaBuffer or NULL if any problem
398 create_xmesa_buffer(Drawable d
, BufferType type
,
399 XMesaVisual vis
, Colormap cmap
)
401 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
405 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
410 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
415 b
->ws
.visual
= vis
->visinfo
->visual
;
416 b
->ws
.depth
= vis
->visinfo
->depth
;
422 get_drawable_size(vis
->display
, d
, &width
, &height
);
425 * Create framebuffer, but we'll plug in our own renderbuffers below.
427 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
429 /* GLX_EXT_texture_from_pixmap */
430 b
->TextureTarget
= 0;
431 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
432 b
->TextureMipmap
= 0;
434 /* insert buffer into linked list */
435 b
->Next
= XMesaBufferList
;
443 * Find an XMesaBuffer by matching X display and colormap but NOT matching
444 * the notThis buffer.
447 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
450 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
451 if (b
->xm_visual
->display
== dpy
&&
462 * Remove buffer from linked list, delete if no longer referenced.
465 xmesa_free_buffer(XMesaBuffer buffer
)
467 XMesaBuffer prev
= NULL
, b
;
469 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
471 /* unlink buffer from list */
473 prev
->Next
= buffer
->Next
;
475 XMesaBufferList
= buffer
->Next
;
477 /* Since the X window for the XMesaBuffer is going away, we don't
478 * want to dereference this pointer in the future.
482 /* XXX we should move the buffer to a delete-pending list and destroy
483 * the buffer until it is no longer current.
485 xmesa_destroy_st_framebuffer(buffer
->stfb
);
491 /* continue search */
494 /* buffer not found in XMesaBufferList */
495 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
500 /**********************************************************************/
501 /***** Misc Private Functions *****/
502 /**********************************************************************/
506 * When a context is bound for the first time, we can finally finish
507 * initializing the context's visual and buffer information.
508 * \param v the XMesaVisual to initialize
509 * \param b the XMesaBuffer to initialize (may be NULL)
510 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
511 * \param window the window/pixmap we're rendering into
512 * \param cmap the colormap associated with the window/pixmap
513 * \return GL_TRUE=success, GL_FALSE=failure
516 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
517 GLboolean rgb_flag
, Drawable window
,
520 ASSERT(!b
|| b
->xm_visual
== v
);
522 /* Save true bits/pixel */
523 v
->BitsPerPixel
= bits_per_pixel(v
);
524 assert(v
->BitsPerPixel
> 0);
526 if (rgb_flag
== GL_FALSE
) {
527 /* COLOR-INDEXED WINDOW: not supported*/
532 * We support RGB rendering into almost any kind of visual.
534 const int xclass
= v
->mesa_visual
.visualType
;
535 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
537 "XMesa: RGB mode rendering not supported in given visual.\n");
540 v
->mesa_visual
.indexBits
= 0;
542 if (v
->BitsPerPixel
== 32) {
543 /* We use XImages for all front/back buffers. If an X Window or
544 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
545 * will be preserved. For XImages we're in luck.
547 v
->mesa_visual
.alphaBits
= 8;
552 * If MESA_INFO env var is set print out some debugging info
553 * which can help Brian figure out what's going on when a user
556 if (_mesa_getenv("MESA_INFO")) {
557 printf("X/Mesa visual = %p\n", (void *) v
);
558 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
559 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
560 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
568 #define NUM_VISUAL_TYPES 6
571 * Convert an X visual type to a GLX visual type.
573 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
575 * \return If \c visualType is a valid X visual type, a GLX visual type will
576 * be returned. Otherwise \c GLX_NONE will be returned.
579 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
583 xmesa_convert_from_x_visual_type( int visualType
)
585 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
586 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
587 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
588 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
591 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
592 ? glx_visual_types
[ visualType
] : GLX_NONE
;
596 /**********************************************************************/
597 /***** Public Functions *****/
598 /**********************************************************************/
602 * Create a new X/Mesa visual.
603 * Input: display - X11 display
604 * visinfo - an XVisualInfo pointer
605 * rgb_flag - GL_TRUE = RGB mode,
606 * GL_FALSE = color index mode
607 * alpha_flag - alpha buffer requested?
608 * db_flag - GL_TRUE = double-buffered,
609 * GL_FALSE = single buffered
610 * stereo_flag - stereo visual?
611 * ximage_flag - GL_TRUE = use an XImage for back buffer,
612 * GL_FALSE = use an off-screen pixmap for back buffer
613 * depth_size - requested bits/depth values, or zero
614 * stencil_size - requested bits/stencil values, or zero
615 * accum_red_size - requested bits/red accum values, or zero
616 * accum_green_size - requested bits/green accum values, or zero
617 * accum_blue_size - requested bits/blue accum values, or zero
618 * accum_alpha_size - requested bits/alpha accum values, or zero
619 * num_samples - number of samples/pixel if multisampling, or zero
620 * level - visual level, usually 0
621 * visualCaveat - ala the GLX extension, usually GLX_NONE
622 * Return; a new XMesaVisual or 0 if error.
625 XMesaVisual
XMesaCreateVisual( Display
*display
,
626 XVisualInfo
* visinfo
,
628 GLboolean alpha_flag
,
630 GLboolean stereo_flag
,
631 GLboolean ximage_flag
,
634 GLint accum_red_size
,
635 GLint accum_green_size
,
636 GLint accum_blue_size
,
637 GLint accum_alpha_size
,
642 XMesaDisplay xmdpy
= xmesa_init_display(display
);
644 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
649 /* For debugging only */
650 if (_mesa_getenv("MESA_XSYNC")) {
651 /* This makes debugging X easier.
652 * In your debugger, set a breakpoint on _XError to stop when an
653 * X protocol error is generated.
655 XSynchronize( display
, 1 );
658 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
663 v
->display
= display
;
665 /* Save a copy of the XVisualInfo struct because the user may Xfree()
666 * the struct but we may need some of the information contained in it
669 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
674 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
676 v
->ximage_flag
= ximage_flag
;
678 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
679 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
680 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
681 v
->mesa_visual
.visualID
= visinfo
->visualid
;
682 v
->mesa_visual
.screen
= visinfo
->screen
;
684 #if !(defined(__cplusplus) || defined(c_plusplus))
685 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
687 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
690 v
->mesa_visual
.visualRating
= visualCaveat
;
693 v
->mesa_visual
.alphaBits
= 8;
695 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
698 const int xclass
= v
->mesa_visual
.visualType
;
699 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
700 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
701 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
702 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
705 /* this is an approximation */
707 depth
= v
->visinfo
->depth
;
708 red_bits
= depth
/ 3;
710 green_bits
= depth
/ 2;
714 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
716 alpha_bits
= v
->mesa_visual
.alphaBits
;
719 _mesa_initialize_visual( &v
->mesa_visual
,
720 db_flag
, stereo_flag
,
721 red_bits
, green_bits
,
722 blue_bits
, alpha_bits
,
725 accum_red_size
, accum_green_size
,
726 accum_blue_size
, accum_alpha_size
,
729 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
731 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
733 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
735 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
738 v
->stvis
.color_format
= choose_pixel_format(v
);
739 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
745 v
->stvis
.depth_stencil_format
=
746 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
748 v
->stvis
.accum_format
= (accum_red_size
+
749 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
750 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
752 v
->stvis
.samples
= num_samples
;
753 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
756 v
->mesa_visual
.level
= level
;
762 void XMesaDestroyVisual( XMesaVisual v
)
770 * Do per-display initializations.
773 xmesa_init( Display
*display
)
775 xmesa_init_display(display
);
780 * Create a new XMesaContext.
781 * \param v the XMesaVisual
782 * \param share_list another XMesaContext with which to share display
783 * lists or NULL if no sharing is wanted.
784 * \return an XMesaContext or NULL if error.
787 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
789 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
795 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
796 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
801 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
802 c
->xm_read_buffer
= NULL
;
804 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
,
805 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
809 c
->st
->st_manager_private
= (void *) c
;
815 c
->st
->destroy(c
->st
);
824 void XMesaDestroyContext( XMesaContext c
)
826 c
->st
->destroy(c
->st
);
828 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
829 * outlive it, causing segfaults
830 struct pipe_screen *screen = c->st->pipe->screen;
831 screen->destroy(screen);
840 * Private function for creating an XMesaBuffer which corresponds to an
841 * X window or pixmap.
842 * \param v the window's XMesaVisual
843 * \param w the window we're wrapping
844 * \return new XMesaBuffer or NULL if error
847 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
849 XWindowAttributes attr
;
857 /* Check that window depth matches visual depth */
858 XGetWindowAttributes( v
->display
, w
, &attr
);
860 if (v
->visinfo
->depth
!= depth
) {
861 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
862 v
->visinfo
->depth
, depth
);
868 cmap
= attr
.colormap
;
871 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
872 /* this is weird, a window w/out a colormap!? */
873 /* OK, let's just allocate a new one and hope for the best */
874 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
877 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
881 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
882 (Drawable
) w
, cmap
)) {
883 xmesa_free_buffer(b
);
893 * Create a new XMesaBuffer from an X pixmap.
895 * \param v the XMesaVisual
896 * \param p the pixmap
897 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
898 * \c GLX_DIRECT_COLOR visual for the pixmap
899 * \returns new XMesaBuffer or NULL if error
902 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
908 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
912 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
913 (Drawable
) p
, cmap
)) {
914 xmesa_free_buffer(b
);
923 * For GLX_EXT_texture_from_pixmap
926 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
928 int format
, int target
, int mipmap
)
930 GET_CURRENT_CONTEXT(ctx
);
935 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
939 /* get pixmap size */
940 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
944 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
945 target
= GLX_TEXTURE_2D_EXT
;
947 else if ( _mesa_bitcount(b
->width
) == 1
948 && _mesa_bitcount(b
->height
) == 1) {
949 /* power of two size */
950 if (b
->height
== 1) {
951 target
= GLX_TEXTURE_1D_EXT
;
954 target
= GLX_TEXTURE_2D_EXT
;
957 else if (ctx
->Extensions
.NV_texture_rectangle
) {
958 target
= GLX_TEXTURE_RECTANGLE_EXT
;
961 /* non power of two textures not supported */
962 XMesaDestroyBuffer(b
);
967 b
->TextureTarget
= target
;
968 b
->TextureFormat
= format
;
969 b
->TextureMipmap
= mipmap
;
971 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
972 (Drawable
) p
, cmap
)) {
973 xmesa_free_buffer(b
);
983 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
984 unsigned int width
, unsigned int height
)
987 Drawable drawable
; /* X Pixmap Drawable */
990 /* allocate pixmap for front buffer */
991 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
992 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
997 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1001 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1003 xmesa_free_buffer(b
);
1013 * Deallocate an XMesaBuffer structure and all related info.
1016 XMesaDestroyBuffer(XMesaBuffer b
)
1018 xmesa_free_buffer(b
);
1023 * Query the current drawable size and notify the binding context.
1026 xmesa_check_buffer_size(XMesaBuffer b
)
1028 XMesaContext xmctx
= XMesaGetCurrentContext();
1030 if (b
->type
== PBUFFER
)
1033 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1034 if (xmctx
&& xmctx
->xm_buffer
== b
)
1035 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
1040 * Bind buffer b to context c and make c the current rendering context.
1043 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1044 XMesaBuffer readBuffer
)
1046 XMesaContext old_ctx
= XMesaGetCurrentContext();
1048 if (old_ctx
&& old_ctx
!= c
) {
1049 XMesaFlush(old_ctx
);
1050 old_ctx
->xm_buffer
= NULL
;
1051 old_ctx
->xm_read_buffer
= NULL
;
1055 if (!drawBuffer
|| !readBuffer
)
1056 return GL_FALSE
; /* must specify buffers! */
1059 c
->xm_buffer
== drawBuffer
&&
1060 c
->xm_read_buffer
== readBuffer
)
1063 xmesa_check_buffer_size(drawBuffer
);
1064 if (readBuffer
!= drawBuffer
)
1065 xmesa_check_buffer_size(readBuffer
);
1067 c
->xm_buffer
= drawBuffer
;
1068 c
->xm_read_buffer
= readBuffer
;
1070 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1072 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1073 drawBuffer
->wasCurrent
= GL_TRUE
;
1077 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1085 * Unbind the context c from its buffer.
1087 GLboolean
XMesaUnbindContext( XMesaContext c
)
1089 /* A no-op for XFree86 integration purposes */
1094 XMesaContext
XMesaGetCurrentContext( void )
1096 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1097 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1103 * Swap front and back color buffers and have winsys display front buffer.
1104 * If there's no front color buffer no swap actually occurs.
1107 void XMesaSwapBuffers( XMesaBuffer b
)
1109 XMesaContext xmctx
= XMesaGetCurrentContext();
1111 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1112 xmctx
->st
->flush( xmctx
->st
,
1113 PIPE_FLUSH_RENDER_CACHE
|
1114 PIPE_FLUSH_SWAPBUFFERS
|
1119 xmesa_swap_st_framebuffer(b
->stfb
);
1125 * Copy sub-region of back buffer to front buffer
1127 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1129 xmesa_copy_st_framebuffer(b
->stfb
,
1130 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1131 x
, y
, width
, height
);
1136 void XMesaFlush( XMesaContext c
)
1138 if (c
&& c
->xm_visual
->display
) {
1139 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1140 struct pipe_fence_handle
*fence
= NULL
;
1142 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1144 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
, 0);
1145 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1147 XSync( c
->xm_visual
->display
, False
);
1155 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1158 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1159 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1168 * Free/destroy all XMesaBuffers associated with given display.
1170 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1172 XMesaBuffer b
, next
;
1173 for (b
= XMesaBufferList
; b
; b
= next
) {
1175 if (b
->xm_visual
->display
== dpy
) {
1176 xmesa_free_buffer(b
);
1183 * Look for XMesaBuffers whose X window has been destroyed.
1184 * Deallocate any such XMesaBuffers.
1186 void XMesaGarbageCollect( void )
1188 XMesaBuffer b
, next
;
1189 for (b
=XMesaBufferList
; b
; b
=next
) {
1192 b
->xm_visual
->display
&&
1194 b
->type
== WINDOW
) {
1195 XSync(b
->xm_visual
->display
, False
);
1196 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1197 /* found a dead window, free the ancillary info */
1198 XMesaDestroyBuffer( b
);
1208 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1209 const int *attrib_list
)
1216 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1222 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1225 dst
->st
->copy(dst
->st
, src
->st
, mask
);