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
;
336 * Choose a depth/stencil format that satisfies the given depth and
339 static enum pipe_format
340 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
342 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
343 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
344 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
345 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
346 enum pipe_format formats
[8], fmt
;
351 if (depth
<= 16 && stencil
== 0) {
352 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
354 if (depth
<= 24 && stencil
== 0) {
355 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
356 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
358 if (depth
<= 24 && stencil
<= 8) {
359 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
360 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
362 if (depth
<= 32 && stencil
== 0) {
363 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
366 fmt
= PIPE_FORMAT_NONE
;
367 for (i
= 0; i
< count
; i
++) {
368 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
369 target
, tex_usage
, geom_flags
)) {
380 /**********************************************************************/
381 /***** Linked list of XMesaBuffers *****/
382 /**********************************************************************/
384 static XMesaBuffer XMesaBufferList
= NULL
;
388 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
389 * Note that XMesaBuffer is derived from GLframebuffer.
390 * The new XMesaBuffer will not have any size (Width=Height=0).
392 * \param d the corresponding X drawable (window or pixmap)
393 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
394 * \param vis the buffer's visual
395 * \param cmap the window's colormap, if known.
396 * \return new XMesaBuffer or NULL if any problem
399 create_xmesa_buffer(Drawable d
, BufferType type
,
400 XMesaVisual vis
, Colormap cmap
)
402 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
406 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
411 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
416 b
->ws
.visual
= vis
->visinfo
->visual
;
417 b
->ws
.depth
= vis
->visinfo
->depth
;
423 get_drawable_size(vis
->display
, d
, &width
, &height
);
426 * Create framebuffer, but we'll plug in our own renderbuffers below.
428 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
430 /* GLX_EXT_texture_from_pixmap */
431 b
->TextureTarget
= 0;
432 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
433 b
->TextureMipmap
= 0;
435 /* insert buffer into linked list */
436 b
->Next
= XMesaBufferList
;
444 * Find an XMesaBuffer by matching X display and colormap but NOT matching
445 * the notThis buffer.
448 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
451 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
452 if (b
->xm_visual
->display
== dpy
&&
463 * Remove buffer from linked list, delete if no longer referenced.
466 xmesa_free_buffer(XMesaBuffer buffer
)
468 XMesaBuffer prev
= NULL
, b
;
470 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
472 /* unlink buffer from list */
474 prev
->Next
= buffer
->Next
;
476 XMesaBufferList
= buffer
->Next
;
478 /* Since the X window for the XMesaBuffer is going away, we don't
479 * want to dereference this pointer in the future.
483 /* XXX we should move the buffer to a delete-pending list and destroy
484 * the buffer until it is no longer current.
486 xmesa_destroy_st_framebuffer(buffer
->stfb
);
492 /* continue search */
495 /* buffer not found in XMesaBufferList */
496 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
501 /**********************************************************************/
502 /***** Misc Private Functions *****/
503 /**********************************************************************/
507 * When a context is bound for the first time, we can finally finish
508 * initializing the context's visual and buffer information.
509 * \param v the XMesaVisual to initialize
510 * \param b the XMesaBuffer to initialize (may be NULL)
511 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
512 * \param window the window/pixmap we're rendering into
513 * \param cmap the colormap associated with the window/pixmap
514 * \return GL_TRUE=success, GL_FALSE=failure
517 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
518 GLboolean rgb_flag
, Drawable window
,
521 ASSERT(!b
|| b
->xm_visual
== v
);
523 /* Save true bits/pixel */
524 v
->BitsPerPixel
= bits_per_pixel(v
);
525 assert(v
->BitsPerPixel
> 0);
527 if (rgb_flag
== GL_FALSE
) {
528 /* COLOR-INDEXED WINDOW: not supported*/
533 * We support RGB rendering into almost any kind of visual.
535 const int xclass
= v
->mesa_visual
.visualType
;
536 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
538 "XMesa: RGB mode rendering not supported in given visual.\n");
541 v
->mesa_visual
.indexBits
= 0;
543 if (v
->BitsPerPixel
== 32) {
544 /* We use XImages for all front/back buffers. If an X Window or
545 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
546 * will be preserved. For XImages we're in luck.
548 v
->mesa_visual
.alphaBits
= 8;
553 * If MESA_INFO env var is set print out some debugging info
554 * which can help Brian figure out what's going on when a user
557 if (_mesa_getenv("MESA_INFO")) {
558 printf("X/Mesa visual = %p\n", (void *) v
);
559 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
560 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
561 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
569 #define NUM_VISUAL_TYPES 6
572 * Convert an X visual type to a GLX visual type.
574 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
576 * \return If \c visualType is a valid X visual type, a GLX visual type will
577 * be returned. Otherwise \c GLX_NONE will be returned.
580 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
584 xmesa_convert_from_x_visual_type( int visualType
)
586 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
587 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
588 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
589 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
592 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
593 ? glx_visual_types
[ visualType
] : GLX_NONE
;
597 /**********************************************************************/
598 /***** Public Functions *****/
599 /**********************************************************************/
603 * Create a new X/Mesa visual.
604 * Input: display - X11 display
605 * visinfo - an XVisualInfo pointer
606 * rgb_flag - GL_TRUE = RGB mode,
607 * GL_FALSE = color index mode
608 * alpha_flag - alpha buffer requested?
609 * db_flag - GL_TRUE = double-buffered,
610 * GL_FALSE = single buffered
611 * stereo_flag - stereo visual?
612 * ximage_flag - GL_TRUE = use an XImage for back buffer,
613 * GL_FALSE = use an off-screen pixmap for back buffer
614 * depth_size - requested bits/depth values, or zero
615 * stencil_size - requested bits/stencil values, or zero
616 * accum_red_size - requested bits/red accum values, or zero
617 * accum_green_size - requested bits/green accum values, or zero
618 * accum_blue_size - requested bits/blue accum values, or zero
619 * accum_alpha_size - requested bits/alpha accum values, or zero
620 * num_samples - number of samples/pixel if multisampling, or zero
621 * level - visual level, usually 0
622 * visualCaveat - ala the GLX extension, usually GLX_NONE
623 * Return; a new XMesaVisual or 0 if error.
626 XMesaVisual
XMesaCreateVisual( Display
*display
,
627 XVisualInfo
* visinfo
,
629 GLboolean alpha_flag
,
631 GLboolean stereo_flag
,
632 GLboolean ximage_flag
,
635 GLint accum_red_size
,
636 GLint accum_green_size
,
637 GLint accum_blue_size
,
638 GLint accum_alpha_size
,
643 XMesaDisplay xmdpy
= xmesa_init_display(display
);
645 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
650 /* For debugging only */
651 if (_mesa_getenv("MESA_XSYNC")) {
652 /* This makes debugging X easier.
653 * In your debugger, set a breakpoint on _XError to stop when an
654 * X protocol error is generated.
656 XSynchronize( display
, 1 );
659 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
664 v
->display
= display
;
666 /* Save a copy of the XVisualInfo struct because the user may Xfree()
667 * the struct but we may need some of the information contained in it
670 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
675 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
677 v
->ximage_flag
= ximage_flag
;
679 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
680 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
681 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
682 v
->mesa_visual
.visualID
= visinfo
->visualid
;
683 v
->mesa_visual
.screen
= visinfo
->screen
;
685 #if !(defined(__cplusplus) || defined(c_plusplus))
686 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
688 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
691 v
->mesa_visual
.visualRating
= visualCaveat
;
694 v
->mesa_visual
.alphaBits
= 8;
696 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
699 const int xclass
= v
->mesa_visual
.visualType
;
700 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
701 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
702 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
703 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
706 /* this is an approximation */
708 depth
= v
->visinfo
->depth
;
709 red_bits
= depth
/ 3;
711 green_bits
= depth
/ 2;
715 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
717 alpha_bits
= v
->mesa_visual
.alphaBits
;
720 _mesa_initialize_visual( &v
->mesa_visual
,
721 db_flag
, stereo_flag
,
722 red_bits
, green_bits
,
723 blue_bits
, alpha_bits
,
726 accum_red_size
, accum_green_size
,
727 accum_blue_size
, accum_alpha_size
,
730 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
732 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
734 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
736 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
739 v
->stvis
.color_format
= choose_pixel_format(v
);
740 v
->stvis
.depth_stencil_format
=
741 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
743 v
->stvis
.accum_format
= (accum_red_size
+
744 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
745 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
747 v
->stvis
.samples
= num_samples
;
748 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
751 v
->mesa_visual
.level
= level
;
757 void XMesaDestroyVisual( XMesaVisual v
)
765 * Do per-display initializations.
768 xmesa_init( Display
*display
)
770 xmesa_init_display(display
);
775 * Create a new XMesaContext.
776 * \param v the XMesaVisual
777 * \param share_list another XMesaContext with which to share display
778 * lists or NULL if no sharing is wanted.
779 * \return an XMesaContext or NULL if error.
782 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
784 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
790 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
791 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
796 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
797 c
->xm_read_buffer
= NULL
;
799 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
,
800 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
804 c
->st
->st_manager_private
= (void *) c
;
810 c
->st
->destroy(c
->st
);
819 void XMesaDestroyContext( XMesaContext c
)
821 c
->st
->destroy(c
->st
);
823 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
824 * outlive it, causing segfaults
825 struct pipe_screen *screen = c->st->pipe->screen;
826 screen->destroy(screen);
835 * Private function for creating an XMesaBuffer which corresponds to an
836 * X window or pixmap.
837 * \param v the window's XMesaVisual
838 * \param w the window we're wrapping
839 * \return new XMesaBuffer or NULL if error
842 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
844 XWindowAttributes attr
;
852 /* Check that window depth matches visual depth */
853 XGetWindowAttributes( v
->display
, w
, &attr
);
855 if (v
->visinfo
->depth
!= depth
) {
856 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
857 v
->visinfo
->depth
, depth
);
863 cmap
= attr
.colormap
;
866 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
867 /* this is weird, a window w/out a colormap!? */
868 /* OK, let's just allocate a new one and hope for the best */
869 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
872 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
876 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
877 (Drawable
) w
, cmap
)) {
878 xmesa_free_buffer(b
);
888 * Create a new XMesaBuffer from an X pixmap.
890 * \param v the XMesaVisual
891 * \param p the pixmap
892 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
893 * \c GLX_DIRECT_COLOR visual for the pixmap
894 * \returns new XMesaBuffer or NULL if error
897 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
903 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
907 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
908 (Drawable
) p
, cmap
)) {
909 xmesa_free_buffer(b
);
918 * For GLX_EXT_texture_from_pixmap
921 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
923 int format
, int target
, int mipmap
)
925 GET_CURRENT_CONTEXT(ctx
);
930 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
934 /* get pixmap size */
935 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
939 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
940 target
= GLX_TEXTURE_2D_EXT
;
942 else if ( _mesa_bitcount(b
->width
) == 1
943 && _mesa_bitcount(b
->height
) == 1) {
944 /* power of two size */
945 if (b
->height
== 1) {
946 target
= GLX_TEXTURE_1D_EXT
;
949 target
= GLX_TEXTURE_2D_EXT
;
952 else if (ctx
->Extensions
.NV_texture_rectangle
) {
953 target
= GLX_TEXTURE_RECTANGLE_EXT
;
956 /* non power of two textures not supported */
957 XMesaDestroyBuffer(b
);
962 b
->TextureTarget
= target
;
963 b
->TextureFormat
= format
;
964 b
->TextureMipmap
= mipmap
;
966 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
967 (Drawable
) p
, cmap
)) {
968 xmesa_free_buffer(b
);
978 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
979 unsigned int width
, unsigned int height
)
982 Drawable drawable
; /* X Pixmap Drawable */
985 /* allocate pixmap for front buffer */
986 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
987 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
992 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
996 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
998 xmesa_free_buffer(b
);
1008 * Deallocate an XMesaBuffer structure and all related info.
1011 XMesaDestroyBuffer(XMesaBuffer b
)
1013 xmesa_free_buffer(b
);
1018 * Query the current drawable size and notify the binding context.
1021 xmesa_check_buffer_size(XMesaBuffer b
)
1023 XMesaContext xmctx
= XMesaGetCurrentContext();
1025 if (b
->type
== PBUFFER
)
1028 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1029 if (xmctx
&& xmctx
->xm_buffer
== b
)
1030 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
1035 * Bind buffer b to context c and make c the current rendering context.
1038 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1039 XMesaBuffer readBuffer
)
1041 XMesaContext old_ctx
= XMesaGetCurrentContext();
1043 if (old_ctx
&& old_ctx
!= c
) {
1044 XMesaFlush(old_ctx
);
1045 old_ctx
->xm_buffer
= NULL
;
1046 old_ctx
->xm_read_buffer
= NULL
;
1050 if (!drawBuffer
|| !readBuffer
)
1051 return GL_FALSE
; /* must specify buffers! */
1054 c
->xm_buffer
== drawBuffer
&&
1055 c
->xm_read_buffer
== readBuffer
)
1058 xmesa_check_buffer_size(drawBuffer
);
1059 if (readBuffer
!= drawBuffer
)
1060 xmesa_check_buffer_size(readBuffer
);
1062 c
->xm_buffer
= drawBuffer
;
1063 c
->xm_read_buffer
= readBuffer
;
1065 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1067 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1068 drawBuffer
->wasCurrent
= GL_TRUE
;
1072 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1080 * Unbind the context c from its buffer.
1082 GLboolean
XMesaUnbindContext( XMesaContext c
)
1084 /* A no-op for XFree86 integration purposes */
1089 XMesaContext
XMesaGetCurrentContext( void )
1091 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1092 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1098 * Swap front and back color buffers and have winsys display front buffer.
1099 * If there's no front color buffer no swap actually occurs.
1102 void XMesaSwapBuffers( XMesaBuffer b
)
1104 XMesaContext xmctx
= XMesaGetCurrentContext();
1106 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1107 xmctx
->st
->flush( xmctx
->st
,
1108 PIPE_FLUSH_RENDER_CACHE
|
1109 PIPE_FLUSH_SWAPBUFFERS
|
1114 xmesa_swap_st_framebuffer(b
->stfb
);
1120 * Copy sub-region of back buffer to front buffer
1122 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1124 xmesa_copy_st_framebuffer(b
->stfb
,
1125 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1126 x
, y
, width
, height
);
1131 void XMesaFlush( XMesaContext c
)
1133 if (c
&& c
->xm_visual
->display
) {
1134 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1135 struct pipe_fence_handle
*fence
= NULL
;
1137 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1139 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
, 0);
1140 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1142 XSync( c
->xm_visual
->display
, False
);
1150 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1153 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1154 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1163 * Free/destroy all XMesaBuffers associated with given display.
1165 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1167 XMesaBuffer b
, next
;
1168 for (b
= XMesaBufferList
; b
; b
= next
) {
1170 if (b
->xm_visual
->display
== dpy
) {
1171 xmesa_free_buffer(b
);
1178 * Look for XMesaBuffers whose X window has been destroyed.
1179 * Deallocate any such XMesaBuffers.
1181 void XMesaGarbageCollect( void )
1183 XMesaBuffer b
, next
;
1184 for (b
=XMesaBufferList
; b
; b
=next
) {
1187 b
->xm_visual
->display
&&
1189 b
->type
== WINDOW
) {
1190 XSync(b
->xm_visual
->display
, False
);
1191 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1192 /* found a dead window, free the ancillary info */
1193 XMesaDestroyBuffer( b
);
1203 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1204 const int *attrib_list
)
1211 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1217 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1220 dst
->st
->copy(dst
->st
, src
->st
, mask
);