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;
91 xmesa_get_param(struct st_manager
*smapi
,
92 enum st_manager_param param
)
98 xmesa_init_display( Display
*display
)
100 pipe_static_mutex(init_mutex
);
104 pipe_mutex_lock(init_mutex
);
106 /* Look for XMesaDisplay which corresponds to 'display' */
107 for (i
= 0; i
< NumDisplays
; i
++) {
108 if (Displays
[i
].display
== display
) {
110 pipe_mutex_unlock(init_mutex
);
115 /* Create new XMesaDisplay */
117 assert(NumDisplays
< MAX_DISPLAYS
);
118 xmdpy
= &Displays
[NumDisplays
];
121 if (!xmdpy
->display
&& display
) {
122 xmdpy
->display
= display
;
123 xmdpy
->screen
= driver
.create_pipe_screen(display
);
124 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
126 xmdpy
->smapi
->screen
= xmdpy
->screen
;
127 xmdpy
->smapi
->get_param
= xmesa_get_param
;
130 if (xmdpy
->screen
&& xmdpy
->smapi
) {
131 pipe_mutex_init(xmdpy
->mutex
);
135 xmdpy
->screen
->destroy(xmdpy
->screen
);
136 xmdpy
->screen
= NULL
;
143 xmdpy
->display
= NULL
;
146 if (!xmdpy
->display
|| xmdpy
->display
!= display
)
149 pipe_mutex_unlock(init_mutex
);
154 /**********************************************************************/
155 /***** X Utility Functions *****/
156 /**********************************************************************/
160 * Return the host's byte order as LSBFirst or MSBFirst ala X.
162 static int host_byte_order( void )
165 char *cptr
= (char *) &i
;
166 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
173 * Return the true number of bits per pixel for XImages.
174 * For example, if we request a 24-bit deep visual we may actually need/get
175 * 32bpp XImages. This function returns the appropriate bpp.
176 * Input: dpy - the X display
177 * visinfo - desribes the visual to be used for XImages
178 * Return: true number of bits per pixel for XImages
181 bits_per_pixel( XMesaVisual xmv
)
183 Display
*dpy
= xmv
->display
;
184 XVisualInfo
* visinfo
= xmv
->visinfo
;
187 /* Create a temporary XImage */
188 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
189 ZPixmap
, 0, /*format, offset*/
190 (char*) MALLOC(8), /*data*/
191 1, 1, /*width, height*/
196 /* grab the bits/pixel value */
197 bitsPerPixel
= img
->bits_per_pixel
;
198 /* free the XImage */
201 XDestroyImage( img
);
208 * Determine if a given X window ID is valid (window exists).
209 * Do this by calling XGetWindowAttributes() for the window and
210 * checking if we catch an X error.
211 * Input: dpy - the display
212 * win - the window to check for existance
213 * Return: GL_TRUE - window exists
214 * GL_FALSE - window doesn't exist
216 static GLboolean WindowExistsFlag
;
218 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
221 if (xerr
->error_code
== BadWindow
) {
222 WindowExistsFlag
= GL_FALSE
;
227 static GLboolean
window_exists( Display
*dpy
, Window win
)
229 XWindowAttributes wa
;
230 int (*old_handler
)( Display
*, XErrorEvent
* );
231 WindowExistsFlag
= GL_TRUE
;
232 old_handler
= XSetErrorHandler(window_exists_err_handler
);
233 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
234 XSetErrorHandler(old_handler
);
235 return WindowExistsFlag
;
239 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
244 unsigned int w
, h
, bw
, depth
;
245 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
253 * Return the size of the window (or pixmap) that corresponds to the
255 * \param width returns width in pixels
256 * \param height returns height in pixels
259 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
260 GLuint
*width
, GLuint
*height
)
262 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
265 pipe_mutex_lock(xmdpy
->mutex
);
266 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
267 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
268 pipe_mutex_unlock(xmdpy
->mutex
);
271 /* probably querying a window that's recently been destroyed */
272 _mesa_warning(NULL
, "XGetGeometry failed!\n");
273 *width
= *height
= 1;
277 #define GET_REDMASK(__v) __v->mesa_visual.redMask
278 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
279 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
283 * Choose the pixel format for the given visual.
284 * This will tell the gallium driver how to pack pixel data into
288 choose_pixel_format(XMesaVisual v
)
290 boolean native_byte_order
= (host_byte_order() ==
291 ImageByteOrder(v
->display
));
293 if ( GET_REDMASK(v
) == 0x0000ff
294 && GET_GREENMASK(v
) == 0x00ff00
295 && GET_BLUEMASK(v
) == 0xff0000
296 && v
->BitsPerPixel
== 32) {
297 if (native_byte_order
) {
298 /* no byteswapping needed */
299 return PIPE_FORMAT_R8G8B8A8_UNORM
;
302 return PIPE_FORMAT_A8B8G8R8_UNORM
;
305 else if ( GET_REDMASK(v
) == 0xff0000
306 && GET_GREENMASK(v
) == 0x00ff00
307 && GET_BLUEMASK(v
) == 0x0000ff
308 && v
->BitsPerPixel
== 32) {
309 if (native_byte_order
) {
310 /* no byteswapping needed */
311 return PIPE_FORMAT_B8G8R8A8_UNORM
;
314 return PIPE_FORMAT_A8R8G8B8_UNORM
;
317 else if ( GET_REDMASK(v
) == 0x0000ff00
318 && GET_GREENMASK(v
) == 0x00ff0000
319 && GET_BLUEMASK(v
) == 0xff000000
320 && v
->BitsPerPixel
== 32) {
321 if (native_byte_order
) {
322 /* no byteswapping needed */
323 return PIPE_FORMAT_A8R8G8B8_UNORM
;
326 return PIPE_FORMAT_B8G8R8A8_UNORM
;
329 else if ( GET_REDMASK(v
) == 0xf800
330 && GET_GREENMASK(v
) == 0x07e0
331 && GET_BLUEMASK(v
) == 0x001f
333 && v
->BitsPerPixel
== 16) {
335 return PIPE_FORMAT_B5G6R5_UNORM
;
338 return PIPE_FORMAT_NONE
;
343 * Choose a depth/stencil format that satisfies the given depth and
346 static enum pipe_format
347 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
349 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
350 const unsigned tex_usage
= PIPE_BIND_DEPTH_STENCIL
;
351 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
352 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
353 const unsigned sample_count
= 0;
354 enum pipe_format formats
[8], fmt
;
359 if (depth
<= 16 && stencil
== 0) {
360 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
362 if (depth
<= 24 && stencil
== 0) {
363 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
364 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
366 if (depth
<= 24 && stencil
<= 8) {
367 formats
[count
++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM
;
368 formats
[count
++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED
;
370 if (depth
<= 32 && stencil
== 0) {
371 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
374 fmt
= PIPE_FORMAT_NONE
;
375 for (i
= 0; i
< count
; i
++) {
376 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
377 target
, sample_count
,
378 tex_usage
, geom_flags
)) {
389 /**********************************************************************/
390 /***** Linked list of XMesaBuffers *****/
391 /**********************************************************************/
393 static XMesaBuffer XMesaBufferList
= NULL
;
397 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
398 * Note that XMesaBuffer is derived from GLframebuffer.
399 * The new XMesaBuffer will not have any size (Width=Height=0).
401 * \param d the corresponding X drawable (window or pixmap)
402 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
403 * \param vis the buffer's visual
404 * \param cmap the window's colormap, if known.
405 * \return new XMesaBuffer or NULL if any problem
408 create_xmesa_buffer(Drawable d
, BufferType type
,
409 XMesaVisual vis
, Colormap cmap
)
411 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
415 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
420 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
425 b
->ws
.visual
= vis
->visinfo
->visual
;
426 b
->ws
.depth
= vis
->visinfo
->depth
;
432 get_drawable_size(vis
->display
, d
, &width
, &height
);
435 * Create framebuffer, but we'll plug in our own renderbuffers below.
437 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
439 /* GLX_EXT_texture_from_pixmap */
440 b
->TextureTarget
= 0;
441 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
442 b
->TextureMipmap
= 0;
444 /* insert buffer into linked list */
445 b
->Next
= XMesaBufferList
;
453 * Find an XMesaBuffer by matching X display and colormap but NOT matching
454 * the notThis buffer.
457 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
460 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
461 if (b
->xm_visual
->display
== dpy
&&
472 * Remove buffer from linked list, delete if no longer referenced.
475 xmesa_free_buffer(XMesaBuffer buffer
)
477 XMesaBuffer prev
= NULL
, b
;
479 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
481 /* unlink buffer from list */
483 prev
->Next
= buffer
->Next
;
485 XMesaBufferList
= buffer
->Next
;
487 /* Since the X window for the XMesaBuffer is going away, we don't
488 * want to dereference this pointer in the future.
492 /* XXX we should move the buffer to a delete-pending list and destroy
493 * the buffer until it is no longer current.
495 xmesa_destroy_st_framebuffer(buffer
->stfb
);
501 /* continue search */
504 /* buffer not found in XMesaBufferList */
505 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
510 /**********************************************************************/
511 /***** Misc Private Functions *****/
512 /**********************************************************************/
516 * When a context is bound for the first time, we can finally finish
517 * initializing the context's visual and buffer information.
518 * \param v the XMesaVisual to initialize
519 * \param b the XMesaBuffer to initialize (may be NULL)
520 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
521 * \param window the window/pixmap we're rendering into
522 * \param cmap the colormap associated with the window/pixmap
523 * \return GL_TRUE=success, GL_FALSE=failure
526 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
527 GLboolean rgb_flag
, Drawable window
,
530 ASSERT(!b
|| b
->xm_visual
== v
);
532 /* Save true bits/pixel */
533 v
->BitsPerPixel
= bits_per_pixel(v
);
534 assert(v
->BitsPerPixel
> 0);
536 if (rgb_flag
== GL_FALSE
) {
537 /* COLOR-INDEXED WINDOW: not supported*/
542 * We support RGB rendering into almost any kind of visual.
544 const int xclass
= v
->mesa_visual
.visualType
;
545 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
547 "XMesa: RGB mode rendering not supported in given visual.\n");
550 v
->mesa_visual
.indexBits
= 0;
552 if (v
->BitsPerPixel
== 32) {
553 /* We use XImages for all front/back buffers. If an X Window or
554 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
555 * will be preserved. For XImages we're in luck.
557 v
->mesa_visual
.alphaBits
= 8;
562 * If MESA_INFO env var is set print out some debugging info
563 * which can help Brian figure out what's going on when a user
566 if (_mesa_getenv("MESA_INFO")) {
567 printf("X/Mesa visual = %p\n", (void *) v
);
568 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
569 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
570 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
578 #define NUM_VISUAL_TYPES 6
581 * Convert an X visual type to a GLX visual type.
583 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
585 * \return If \c visualType is a valid X visual type, a GLX visual type will
586 * be returned. Otherwise \c GLX_NONE will be returned.
589 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
593 xmesa_convert_from_x_visual_type( int visualType
)
595 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
596 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
597 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
598 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
601 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
602 ? glx_visual_types
[ visualType
] : GLX_NONE
;
606 /**********************************************************************/
607 /***** Public Functions *****/
608 /**********************************************************************/
612 * Create a new X/Mesa visual.
613 * Input: display - X11 display
614 * visinfo - an XVisualInfo pointer
615 * rgb_flag - GL_TRUE = RGB mode,
616 * GL_FALSE = color index mode
617 * alpha_flag - alpha buffer requested?
618 * db_flag - GL_TRUE = double-buffered,
619 * GL_FALSE = single buffered
620 * stereo_flag - stereo visual?
621 * ximage_flag - GL_TRUE = use an XImage for back buffer,
622 * GL_FALSE = use an off-screen pixmap for back buffer
623 * depth_size - requested bits/depth values, or zero
624 * stencil_size - requested bits/stencil values, or zero
625 * accum_red_size - requested bits/red accum values, or zero
626 * accum_green_size - requested bits/green accum values, or zero
627 * accum_blue_size - requested bits/blue accum values, or zero
628 * accum_alpha_size - requested bits/alpha accum values, or zero
629 * num_samples - number of samples/pixel if multisampling, or zero
630 * level - visual level, usually 0
631 * visualCaveat - ala the GLX extension, usually GLX_NONE
632 * Return; a new XMesaVisual or 0 if error.
635 XMesaVisual
XMesaCreateVisual( Display
*display
,
636 XVisualInfo
* visinfo
,
638 GLboolean alpha_flag
,
640 GLboolean stereo_flag
,
641 GLboolean ximage_flag
,
644 GLint accum_red_size
,
645 GLint accum_green_size
,
646 GLint accum_blue_size
,
647 GLint accum_alpha_size
,
652 XMesaDisplay xmdpy
= xmesa_init_display(display
);
654 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
659 /* For debugging only */
660 if (_mesa_getenv("MESA_XSYNC")) {
661 /* This makes debugging X easier.
662 * In your debugger, set a breakpoint on _XError to stop when an
663 * X protocol error is generated.
665 XSynchronize( display
, 1 );
668 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
673 v
->display
= display
;
675 /* Save a copy of the XVisualInfo struct because the user may Xfree()
676 * the struct but we may need some of the information contained in it
679 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
684 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
686 v
->ximage_flag
= ximage_flag
;
688 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
689 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
690 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
691 v
->mesa_visual
.visualID
= visinfo
->visualid
;
692 v
->mesa_visual
.screen
= visinfo
->screen
;
694 #if !(defined(__cplusplus) || defined(c_plusplus))
695 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
697 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
700 v
->mesa_visual
.visualRating
= visualCaveat
;
703 v
->mesa_visual
.alphaBits
= 8;
705 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
708 const int xclass
= v
->mesa_visual
.visualType
;
709 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
710 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
711 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
712 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
715 /* this is an approximation */
717 depth
= v
->visinfo
->depth
;
718 red_bits
= depth
/ 3;
720 green_bits
= depth
/ 2;
724 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
726 alpha_bits
= v
->mesa_visual
.alphaBits
;
729 _mesa_initialize_visual( &v
->mesa_visual
,
730 db_flag
, stereo_flag
,
731 red_bits
, green_bits
,
732 blue_bits
, alpha_bits
,
735 accum_red_size
, accum_green_size
,
736 accum_blue_size
, accum_alpha_size
,
739 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
741 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
743 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
745 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
748 v
->stvis
.color_format
= choose_pixel_format(v
);
749 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
755 v
->stvis
.depth_stencil_format
=
756 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
758 v
->stvis
.accum_format
= (accum_red_size
+
759 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
760 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
762 v
->stvis
.samples
= num_samples
;
763 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
766 v
->mesa_visual
.level
= level
;
772 void XMesaDestroyVisual( XMesaVisual v
)
780 * Do per-display initializations.
783 xmesa_init( Display
*display
)
785 xmesa_init_display(display
);
790 * Create a new XMesaContext.
791 * \param v the XMesaVisual
792 * \param share_list another XMesaContext with which to share display
793 * lists or NULL if no sharing is wanted.
794 * \return an XMesaContext or NULL if error.
797 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
799 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
805 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
806 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
811 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
812 c
->xm_read_buffer
= NULL
;
814 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
,
815 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
819 c
->st
->st_manager_private
= (void *) c
;
825 c
->st
->destroy(c
->st
);
834 void XMesaDestroyContext( XMesaContext c
)
836 c
->st
->destroy(c
->st
);
838 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
839 * outlive it, causing segfaults
840 struct pipe_screen *screen = c->st->pipe->screen;
841 screen->destroy(screen);
850 * Private function for creating an XMesaBuffer which corresponds to an
851 * X window or pixmap.
852 * \param v the window's XMesaVisual
853 * \param w the window we're wrapping
854 * \return new XMesaBuffer or NULL if error
857 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
859 XWindowAttributes attr
;
867 /* Check that window depth matches visual depth */
868 XGetWindowAttributes( v
->display
, w
, &attr
);
870 if (v
->visinfo
->depth
!= depth
) {
871 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
872 v
->visinfo
->depth
, depth
);
878 cmap
= attr
.colormap
;
881 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
882 /* this is weird, a window w/out a colormap!? */
883 /* OK, let's just allocate a new one and hope for the best */
884 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
887 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
891 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
892 (Drawable
) w
, cmap
)) {
893 xmesa_free_buffer(b
);
903 * Create a new XMesaBuffer from an X pixmap.
905 * \param v the XMesaVisual
906 * \param p the pixmap
907 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
908 * \c GLX_DIRECT_COLOR visual for the pixmap
909 * \returns new XMesaBuffer or NULL if error
912 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
918 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
922 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
923 (Drawable
) p
, cmap
)) {
924 xmesa_free_buffer(b
);
933 * For GLX_EXT_texture_from_pixmap
936 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
938 int format
, int target
, int mipmap
)
940 GET_CURRENT_CONTEXT(ctx
);
945 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
949 /* get pixmap size */
950 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
954 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
955 target
= GLX_TEXTURE_2D_EXT
;
957 else if ( _mesa_bitcount(b
->width
) == 1
958 && _mesa_bitcount(b
->height
) == 1) {
959 /* power of two size */
960 if (b
->height
== 1) {
961 target
= GLX_TEXTURE_1D_EXT
;
964 target
= GLX_TEXTURE_2D_EXT
;
967 else if (ctx
->Extensions
.NV_texture_rectangle
) {
968 target
= GLX_TEXTURE_RECTANGLE_EXT
;
971 /* non power of two textures not supported */
972 XMesaDestroyBuffer(b
);
977 b
->TextureTarget
= target
;
978 b
->TextureFormat
= format
;
979 b
->TextureMipmap
= mipmap
;
981 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
982 (Drawable
) p
, cmap
)) {
983 xmesa_free_buffer(b
);
993 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
994 unsigned int width
, unsigned int height
)
997 Drawable drawable
; /* X Pixmap Drawable */
1000 /* allocate pixmap for front buffer */
1001 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1002 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1007 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1011 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1013 xmesa_free_buffer(b
);
1023 * Deallocate an XMesaBuffer structure and all related info.
1026 XMesaDestroyBuffer(XMesaBuffer b
)
1028 xmesa_free_buffer(b
);
1033 * Query the current drawable size and notify the binding context.
1036 xmesa_check_buffer_size(XMesaBuffer b
)
1038 XMesaContext xmctx
= XMesaGetCurrentContext();
1040 if (b
->type
== PBUFFER
)
1043 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1044 if (xmctx
&& xmctx
->xm_buffer
== b
)
1045 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
1050 * Bind buffer b to context c and make c the current rendering context.
1053 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1054 XMesaBuffer readBuffer
)
1056 XMesaContext old_ctx
= XMesaGetCurrentContext();
1058 if (old_ctx
&& old_ctx
!= c
) {
1059 XMesaFlush(old_ctx
);
1060 old_ctx
->xm_buffer
= NULL
;
1061 old_ctx
->xm_read_buffer
= NULL
;
1065 if (!drawBuffer
|| !readBuffer
)
1066 return GL_FALSE
; /* must specify buffers! */
1069 c
->xm_buffer
== drawBuffer
&&
1070 c
->xm_read_buffer
== readBuffer
)
1073 xmesa_check_buffer_size(drawBuffer
);
1074 if (readBuffer
!= drawBuffer
)
1075 xmesa_check_buffer_size(readBuffer
);
1077 c
->xm_buffer
= drawBuffer
;
1078 c
->xm_read_buffer
= readBuffer
;
1080 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1082 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1083 drawBuffer
->wasCurrent
= GL_TRUE
;
1087 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1095 * Unbind the context c from its buffer.
1097 GLboolean
XMesaUnbindContext( XMesaContext c
)
1099 /* A no-op for XFree86 integration purposes */
1104 XMesaContext
XMesaGetCurrentContext( void )
1106 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1107 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1113 * Swap front and back color buffers and have winsys display front buffer.
1114 * If there's no front color buffer no swap actually occurs.
1117 void XMesaSwapBuffers( XMesaBuffer b
)
1119 XMesaContext xmctx
= XMesaGetCurrentContext();
1121 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1122 xmctx
->st
->flush( xmctx
->st
,
1123 PIPE_FLUSH_RENDER_CACHE
|
1124 PIPE_FLUSH_SWAPBUFFERS
|
1129 xmesa_swap_st_framebuffer(b
->stfb
);
1135 * Copy sub-region of back buffer to front buffer
1137 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1139 xmesa_copy_st_framebuffer(b
->stfb
,
1140 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1141 x
, y
, width
, height
);
1146 void XMesaFlush( XMesaContext c
)
1148 if (c
&& c
->xm_visual
->display
) {
1149 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1150 struct pipe_fence_handle
*fence
= NULL
;
1152 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1154 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
, 0);
1155 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1157 XSync( c
->xm_visual
->display
, False
);
1165 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1168 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1169 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1178 * Free/destroy all XMesaBuffers associated with given display.
1180 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1182 XMesaBuffer b
, next
;
1183 for (b
= XMesaBufferList
; b
; b
= next
) {
1185 if (b
->xm_visual
->display
== dpy
) {
1186 xmesa_free_buffer(b
);
1187 /* delete head of list? */
1188 if (XMesaBufferList
== b
) {
1189 XMesaBufferList
= next
;
1197 * Look for XMesaBuffers whose X window has been destroyed.
1198 * Deallocate any such XMesaBuffers.
1200 void XMesaGarbageCollect( void )
1202 XMesaBuffer b
, next
;
1203 for (b
=XMesaBufferList
; b
; b
=next
) {
1206 b
->xm_visual
->display
&&
1208 b
->type
== WINDOW
) {
1209 XSync(b
->xm_visual
->display
, False
);
1210 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1211 /* found a dead window, free the ancillary info */
1212 XMesaDestroyBuffer( b
);
1222 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1223 const int *attrib_list
)
1230 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1236 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1239 dst
->st
->copy(dst
->st
, src
->st
, mask
);