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();
82 xmesa_init_display( Display
*display
)
84 pipe_static_mutex(init_mutex
);
85 static struct xmesa_display xm_display
;
88 pipe_mutex_lock(init_mutex
);
90 /* TODO support for multiple displays */
93 if (!xmdpy
->display
&& display
) {
94 xmdpy
->display
= display
;
95 xmdpy
->screen
= driver
.create_pipe_screen(display
);
96 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
98 xmdpy
->smapi
->screen
= xmdpy
->screen
;
100 if (xmdpy
->screen
&& xmdpy
->smapi
) {
101 pipe_mutex_init(xmdpy
->mutex
);
105 xmdpy
->screen
->destroy(xmdpy
->screen
);
106 xmdpy
->screen
= NULL
;
113 xmdpy
->display
= NULL
;
116 if (!xmdpy
->display
|| xmdpy
->display
!= display
)
119 pipe_mutex_unlock(init_mutex
);
124 /**********************************************************************/
125 /***** X Utility Functions *****/
126 /**********************************************************************/
130 * Return the host's byte order as LSBFirst or MSBFirst ala X.
132 static int host_byte_order( void )
135 char *cptr
= (char *) &i
;
136 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
143 * Return the true number of bits per pixel for XImages.
144 * For example, if we request a 24-bit deep visual we may actually need/get
145 * 32bpp XImages. This function returns the appropriate bpp.
146 * Input: dpy - the X display
147 * visinfo - desribes the visual to be used for XImages
148 * Return: true number of bits per pixel for XImages
151 bits_per_pixel( XMesaVisual xmv
)
153 Display
*dpy
= xmv
->display
;
154 XVisualInfo
* visinfo
= xmv
->visinfo
;
157 /* Create a temporary XImage */
158 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
159 ZPixmap
, 0, /*format, offset*/
160 (char*) MALLOC(8), /*data*/
161 1, 1, /*width, height*/
166 /* grab the bits/pixel value */
167 bitsPerPixel
= img
->bits_per_pixel
;
168 /* free the XImage */
171 XDestroyImage( img
);
178 * Determine if a given X window ID is valid (window exists).
179 * Do this by calling XGetWindowAttributes() for the window and
180 * checking if we catch an X error.
181 * Input: dpy - the display
182 * win - the window to check for existance
183 * Return: GL_TRUE - window exists
184 * GL_FALSE - window doesn't exist
186 static GLboolean WindowExistsFlag
;
188 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
191 if (xerr
->error_code
== BadWindow
) {
192 WindowExistsFlag
= GL_FALSE
;
197 static GLboolean
window_exists( Display
*dpy
, Window win
)
199 XWindowAttributes wa
;
200 int (*old_handler
)( Display
*, XErrorEvent
* );
201 WindowExistsFlag
= GL_TRUE
;
202 old_handler
= XSetErrorHandler(window_exists_err_handler
);
203 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
204 XSetErrorHandler(old_handler
);
205 return WindowExistsFlag
;
209 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
214 unsigned int w
, h
, bw
, depth
;
215 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
223 * Return the size of the window (or pixmap) that corresponds to the
225 * \param width returns width in pixels
226 * \param height returns height in pixels
229 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
230 GLuint
*width
, GLuint
*height
)
232 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
235 pipe_mutex_lock(xmdpy
->mutex
);
236 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
237 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
238 pipe_mutex_unlock(xmdpy
->mutex
);
241 /* probably querying a window that's recently been destroyed */
242 _mesa_warning(NULL
, "XGetGeometry failed!\n");
243 *width
= *height
= 1;
247 #define GET_REDMASK(__v) __v->mesa_visual.redMask
248 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
249 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
253 * Choose the pixel format for the given visual.
254 * This will tell the gallium driver how to pack pixel data into
258 choose_pixel_format(XMesaVisual v
)
260 boolean native_byte_order
= (host_byte_order() ==
261 ImageByteOrder(v
->display
));
263 if ( GET_REDMASK(v
) == 0x0000ff
264 && GET_GREENMASK(v
) == 0x00ff00
265 && GET_BLUEMASK(v
) == 0xff0000
266 && v
->BitsPerPixel
== 32) {
267 if (native_byte_order
) {
268 /* no byteswapping needed */
269 return PIPE_FORMAT_R8G8B8A8_UNORM
;
272 return PIPE_FORMAT_A8B8G8R8_UNORM
;
275 else if ( GET_REDMASK(v
) == 0xff0000
276 && GET_GREENMASK(v
) == 0x00ff00
277 && GET_BLUEMASK(v
) == 0x0000ff
278 && v
->BitsPerPixel
== 32) {
279 if (native_byte_order
) {
280 /* no byteswapping needed */
281 return PIPE_FORMAT_B8G8R8A8_UNORM
;
284 return PIPE_FORMAT_A8R8G8B8_UNORM
;
287 else if ( GET_REDMASK(v
) == 0x0000ff00
288 && GET_GREENMASK(v
) == 0x00ff0000
289 && GET_BLUEMASK(v
) == 0xff000000
290 && v
->BitsPerPixel
== 32) {
291 if (native_byte_order
) {
292 /* no byteswapping needed */
293 return PIPE_FORMAT_A8R8G8B8_UNORM
;
296 return PIPE_FORMAT_B8G8R8A8_UNORM
;
299 else if ( GET_REDMASK(v
) == 0xf800
300 && GET_GREENMASK(v
) == 0x07e0
301 && GET_BLUEMASK(v
) == 0x001f
303 && v
->BitsPerPixel
== 16) {
305 return PIPE_FORMAT_B5G6R5_UNORM
;
313 * Choose a depth/stencil format that is "better" than the given depth and
316 static enum pipe_format
317 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
319 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
320 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
321 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
322 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
323 enum pipe_format formats
[8], fmt
;
327 if (depth
<= 24 && stencil
<= 8) {
328 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
329 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
334 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
336 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
339 fmt
= PIPE_FORMAT_NONE
;
340 for (i
= 0; i
< count
; i
++) {
341 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
342 target
, tex_usage
, geom_flags
)) {
353 /**********************************************************************/
354 /***** Linked list of XMesaBuffers *****/
355 /**********************************************************************/
357 static XMesaBuffer XMesaBufferList
= NULL
;
361 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
362 * Note that XMesaBuffer is derived from GLframebuffer.
363 * The new XMesaBuffer will not have any size (Width=Height=0).
365 * \param d the corresponding X drawable (window or pixmap)
366 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
367 * \param vis the buffer's visual
368 * \param cmap the window's colormap, if known.
369 * \return new XMesaBuffer or NULL if any problem
372 create_xmesa_buffer(Drawable d
, BufferType type
,
373 XMesaVisual vis
, Colormap cmap
)
375 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
379 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
384 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
389 b
->ws
.visual
= vis
->visinfo
->visual
;
390 b
->ws
.depth
= vis
->visinfo
->depth
;
396 get_drawable_size(vis
->display
, d
, &width
, &height
);
399 * Create framebuffer, but we'll plug in our own renderbuffers below.
401 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
403 /* GLX_EXT_texture_from_pixmap */
404 b
->TextureTarget
= 0;
405 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
406 b
->TextureMipmap
= 0;
408 /* insert buffer into linked list */
409 b
->Next
= XMesaBufferList
;
417 * Find an XMesaBuffer by matching X display and colormap but NOT matching
418 * the notThis buffer.
421 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
424 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
425 if (b
->xm_visual
->display
== dpy
&&
436 * Remove buffer from linked list, delete if no longer referenced.
439 xmesa_free_buffer(XMesaBuffer buffer
)
441 XMesaBuffer prev
= NULL
, b
;
443 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
445 /* unlink buffer from list */
447 prev
->Next
= buffer
->Next
;
449 XMesaBufferList
= buffer
->Next
;
451 /* Since the X window for the XMesaBuffer is going away, we don't
452 * want to dereference this pointer in the future.
456 /* XXX we should move the buffer to a delete-pending list and destroy
457 * the buffer until it is no longer current.
459 xmesa_destroy_st_framebuffer(buffer
->stfb
);
465 /* continue search */
468 /* buffer not found in XMesaBufferList */
469 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
474 /**********************************************************************/
475 /***** Misc Private Functions *****/
476 /**********************************************************************/
480 * When a context is bound for the first time, we can finally finish
481 * initializing the context's visual and buffer information.
482 * \param v the XMesaVisual to initialize
483 * \param b the XMesaBuffer to initialize (may be NULL)
484 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
485 * \param window the window/pixmap we're rendering into
486 * \param cmap the colormap associated with the window/pixmap
487 * \return GL_TRUE=success, GL_FALSE=failure
490 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
491 GLboolean rgb_flag
, Drawable window
,
494 ASSERT(!b
|| b
->xm_visual
== v
);
496 /* Save true bits/pixel */
497 v
->BitsPerPixel
= bits_per_pixel(v
);
498 assert(v
->BitsPerPixel
> 0);
500 if (rgb_flag
== GL_FALSE
) {
501 /* COLOR-INDEXED WINDOW: not supported*/
506 * We support RGB rendering into almost any kind of visual.
508 const int xclass
= v
->mesa_visual
.visualType
;
509 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
511 "XMesa: RGB mode rendering not supported in given visual.\n");
514 v
->mesa_visual
.indexBits
= 0;
516 if (v
->BitsPerPixel
== 32) {
517 /* We use XImages for all front/back buffers. If an X Window or
518 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
519 * will be preserved. For XImages we're in luck.
521 v
->mesa_visual
.alphaBits
= 8;
526 * If MESA_INFO env var is set print out some debugging info
527 * which can help Brian figure out what's going on when a user
530 if (_mesa_getenv("MESA_INFO")) {
531 printf("X/Mesa visual = %p\n", (void *) v
);
532 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
533 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
534 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
542 #define NUM_VISUAL_TYPES 6
545 * Convert an X visual type to a GLX visual type.
547 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
549 * \return If \c visualType is a valid X visual type, a GLX visual type will
550 * be returned. Otherwise \c GLX_NONE will be returned.
553 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
557 xmesa_convert_from_x_visual_type( int visualType
)
559 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
560 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
561 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
562 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
565 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
566 ? glx_visual_types
[ visualType
] : GLX_NONE
;
570 /**********************************************************************/
571 /***** Public Functions *****/
572 /**********************************************************************/
576 * Create a new X/Mesa visual.
577 * Input: display - X11 display
578 * visinfo - an XVisualInfo pointer
579 * rgb_flag - GL_TRUE = RGB mode,
580 * GL_FALSE = color index mode
581 * alpha_flag - alpha buffer requested?
582 * db_flag - GL_TRUE = double-buffered,
583 * GL_FALSE = single buffered
584 * stereo_flag - stereo visual?
585 * ximage_flag - GL_TRUE = use an XImage for back buffer,
586 * GL_FALSE = use an off-screen pixmap for back buffer
587 * depth_size - requested bits/depth values, or zero
588 * stencil_size - requested bits/stencil values, or zero
589 * accum_red_size - requested bits/red accum values, or zero
590 * accum_green_size - requested bits/green accum values, or zero
591 * accum_blue_size - requested bits/blue accum values, or zero
592 * accum_alpha_size - requested bits/alpha accum values, or zero
593 * num_samples - number of samples/pixel if multisampling, or zero
594 * level - visual level, usually 0
595 * visualCaveat - ala the GLX extension, usually GLX_NONE
596 * Return; a new XMesaVisual or 0 if error.
599 XMesaVisual
XMesaCreateVisual( Display
*display
,
600 XVisualInfo
* visinfo
,
602 GLboolean alpha_flag
,
604 GLboolean stereo_flag
,
605 GLboolean ximage_flag
,
608 GLint accum_red_size
,
609 GLint accum_green_size
,
610 GLint accum_blue_size
,
611 GLint accum_alpha_size
,
616 XMesaDisplay xmdpy
= xmesa_init_display(display
);
618 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
623 /* For debugging only */
624 if (_mesa_getenv("MESA_XSYNC")) {
625 /* This makes debugging X easier.
626 * In your debugger, set a breakpoint on _XError to stop when an
627 * X protocol error is generated.
629 XSynchronize( display
, 1 );
632 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
637 v
->display
= display
;
639 /* Save a copy of the XVisualInfo struct because the user may Xfree()
640 * the struct but we may need some of the information contained in it
643 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
648 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
650 v
->ximage_flag
= ximage_flag
;
652 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
653 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
654 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
655 v
->mesa_visual
.visualID
= visinfo
->visualid
;
656 v
->mesa_visual
.screen
= visinfo
->screen
;
658 #if !(defined(__cplusplus) || defined(c_plusplus))
659 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
661 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
664 v
->mesa_visual
.visualRating
= visualCaveat
;
667 v
->mesa_visual
.alphaBits
= 8;
669 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
672 const int xclass
= v
->mesa_visual
.visualType
;
673 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
674 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
675 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
676 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
679 /* this is an approximation */
681 depth
= v
->visinfo
->depth
;
682 red_bits
= depth
/ 3;
684 green_bits
= depth
/ 2;
688 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
690 alpha_bits
= v
->mesa_visual
.alphaBits
;
693 _mesa_initialize_visual( &v
->mesa_visual
,
694 db_flag
, stereo_flag
,
695 red_bits
, green_bits
,
696 blue_bits
, alpha_bits
,
699 accum_red_size
, accum_green_size
,
700 accum_blue_size
, accum_alpha_size
,
703 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
705 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
707 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
709 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
712 v
->stvis
.color_format
= choose_pixel_format(v
);
713 v
->stvis
.depth_stencil_format
=
714 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
716 v
->stvis
.accum_format
= (accum_red_size
+
717 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
718 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
720 v
->stvis
.samples
= num_samples
;
721 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
724 v
->mesa_visual
.level
= level
;
730 void XMesaDestroyVisual( XMesaVisual v
)
738 * Do per-display initializations.
741 xmesa_init( Display
*display
)
743 xmesa_init_display(display
);
748 * Create a new XMesaContext.
749 * \param v the XMesaVisual
750 * \param share_list another XMesaContext with which to share display
751 * lists or NULL if no sharing is wanted.
752 * \return an XMesaContext or NULL if error.
755 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
757 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
763 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
764 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
769 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
770 c
->xm_read_buffer
= NULL
;
772 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
,
773 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
777 c
->st
->st_manager_private
= (void *) c
;
783 c
->st
->destroy(c
->st
);
792 void XMesaDestroyContext( XMesaContext c
)
794 c
->st
->destroy(c
->st
);
796 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
797 * outlive it, causing segfaults
798 struct pipe_screen *screen = c->st->pipe->screen;
799 screen->destroy(screen);
808 * Private function for creating an XMesaBuffer which corresponds to an
809 * X window or pixmap.
810 * \param v the window's XMesaVisual
811 * \param w the window we're wrapping
812 * \return new XMesaBuffer or NULL if error
815 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
817 XWindowAttributes attr
;
825 /* Check that window depth matches visual depth */
826 XGetWindowAttributes( v
->display
, w
, &attr
);
828 if (v
->visinfo
->depth
!= depth
) {
829 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
830 v
->visinfo
->depth
, depth
);
836 cmap
= attr
.colormap
;
839 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
840 /* this is weird, a window w/out a colormap!? */
841 /* OK, let's just allocate a new one and hope for the best */
842 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
845 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
849 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
850 (Drawable
) w
, cmap
)) {
851 xmesa_free_buffer(b
);
861 * Create a new XMesaBuffer from an X pixmap.
863 * \param v the XMesaVisual
864 * \param p the pixmap
865 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
866 * \c GLX_DIRECT_COLOR visual for the pixmap
867 * \returns new XMesaBuffer or NULL if error
870 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
876 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
880 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
881 (Drawable
) p
, cmap
)) {
882 xmesa_free_buffer(b
);
891 * For GLX_EXT_texture_from_pixmap
894 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
896 int format
, int target
, int mipmap
)
898 GET_CURRENT_CONTEXT(ctx
);
903 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
907 /* get pixmap size */
908 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
912 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
913 target
= GLX_TEXTURE_2D_EXT
;
915 else if ( _mesa_bitcount(b
->width
) == 1
916 && _mesa_bitcount(b
->height
) == 1) {
917 /* power of two size */
918 if (b
->height
== 1) {
919 target
= GLX_TEXTURE_1D_EXT
;
922 target
= GLX_TEXTURE_2D_EXT
;
925 else if (ctx
->Extensions
.NV_texture_rectangle
) {
926 target
= GLX_TEXTURE_RECTANGLE_EXT
;
929 /* non power of two textures not supported */
930 XMesaDestroyBuffer(b
);
935 b
->TextureTarget
= target
;
936 b
->TextureFormat
= format
;
937 b
->TextureMipmap
= mipmap
;
939 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
940 (Drawable
) p
, cmap
)) {
941 xmesa_free_buffer(b
);
951 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
952 unsigned int width
, unsigned int height
)
955 Drawable drawable
; /* X Pixmap Drawable */
958 /* allocate pixmap for front buffer */
959 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
960 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
965 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
969 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
971 xmesa_free_buffer(b
);
981 * Deallocate an XMesaBuffer structure and all related info.
984 XMesaDestroyBuffer(XMesaBuffer b
)
986 xmesa_free_buffer(b
);
991 * Query the current drawable size and notify the binding context.
994 xmesa_check_buffer_size(XMesaBuffer b
)
996 XMesaContext xmctx
= XMesaGetCurrentContext();
998 if (b
->type
== PBUFFER
)
1001 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1002 if (xmctx
&& xmctx
->xm_buffer
== b
)
1003 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
1008 * Bind buffer b to context c and make c the current rendering context.
1011 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1012 XMesaBuffer readBuffer
)
1014 XMesaContext old_ctx
= XMesaGetCurrentContext();
1016 if (old_ctx
&& old_ctx
!= c
) {
1017 XMesaFlush(old_ctx
);
1018 old_ctx
->xm_buffer
= NULL
;
1019 old_ctx
->xm_read_buffer
= NULL
;
1023 if (!drawBuffer
|| !readBuffer
)
1024 return GL_FALSE
; /* must specify buffers! */
1027 c
->xm_buffer
== drawBuffer
&&
1028 c
->xm_read_buffer
== readBuffer
)
1031 xmesa_check_buffer_size(drawBuffer
);
1032 if (readBuffer
!= drawBuffer
)
1033 xmesa_check_buffer_size(readBuffer
);
1035 c
->xm_buffer
= drawBuffer
;
1036 c
->xm_read_buffer
= readBuffer
;
1038 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1040 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1041 drawBuffer
->wasCurrent
= GL_TRUE
;
1045 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1053 * Unbind the context c from its buffer.
1055 GLboolean
XMesaUnbindContext( XMesaContext c
)
1057 /* A no-op for XFree86 integration purposes */
1062 XMesaContext
XMesaGetCurrentContext( void )
1064 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1065 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1071 * Swap front and back color buffers and have winsys display front buffer.
1072 * If there's no front color buffer no swap actually occurs.
1075 void XMesaSwapBuffers( XMesaBuffer b
)
1077 XMesaContext xmctx
= XMesaGetCurrentContext();
1079 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1080 xmctx
->st
->flush( xmctx
->st
,
1081 PIPE_FLUSH_RENDER_CACHE
|
1082 PIPE_FLUSH_SWAPBUFFERS
|
1087 xmesa_swap_st_framebuffer(b
->stfb
);
1093 * Copy sub-region of back buffer to front buffer
1095 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1097 xmesa_copy_st_framebuffer(b
->stfb
,
1098 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1099 x
, y
, width
, height
);
1104 void XMesaFlush( XMesaContext c
)
1106 if (c
&& c
->xm_visual
->display
) {
1107 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1108 struct pipe_fence_handle
*fence
= NULL
;
1110 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1112 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
, 0);
1113 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1115 XSync( c
->xm_visual
->display
, False
);
1123 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1126 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1127 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1136 * Free/destroy all XMesaBuffers associated with given display.
1138 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1140 XMesaBuffer b
, next
;
1141 for (b
= XMesaBufferList
; b
; b
= next
) {
1143 if (b
->xm_visual
->display
== dpy
) {
1144 xmesa_free_buffer(b
);
1151 * Look for XMesaBuffers whose X window has been destroyed.
1152 * Deallocate any such XMesaBuffers.
1154 void XMesaGarbageCollect( void )
1156 XMesaBuffer b
, next
;
1157 for (b
=XMesaBufferList
; b
; b
=next
) {
1160 b
->xm_visual
->display
&&
1162 b
->type
== WINDOW
) {
1163 XSync(b
->xm_visual
->display
, False
);
1164 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1165 /* found a dead window, free the ancillary info */
1166 XMesaDestroyBuffer( b
);
1176 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1177 const int *attrib_list
)
1184 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1190 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1193 dst
->st
->copy(dst
->st
, src
->st
, mask
);