2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * All the XMesa* API functions.
33 * The window coordinate system origin (0,0) is in the lower-left corner
34 * of the window. X11's window coordinate origin is in the upper-left
35 * corner of the window. Therefore, most drawing functions in this
36 * file have to flip Y coordinates.
38 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
39 * in support for the MIT Shared Memory extension. If enabled, when you
40 * use an Ximage for the back buffer in double buffered mode, the "swap"
41 * operation will be faster. You must also link with -lXext.
43 * Byte swapping: If the Mesa host and the X display use a different
44 * byte order then there's some trickiness to be aware of when using
45 * XImages. The byte ordering used for the XImage is that of the X
46 * display, not the Mesa host.
47 * The color-to-pixel encoding for True/DirectColor must be done
48 * according to the display's visual red_mask, green_mask, and blue_mask.
49 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
50 * do byte swapping if needed. If one wants to directly "poke" the pixel
51 * into the XImage's buffer then the pixel must be byte swapped first.
61 #include "main/context.h"
62 #include "main/framebuffer.h"
64 #include "state_tracker/st_public.h"
65 #include "state_tracker/st_context.h"
66 #include "pipe/p_defines.h"
67 #include "pipe/p_screen.h"
68 #include "pipe/p_context.h"
70 #include "xm_winsys.h"
74 /* Driver interface routines, set up by xlib backend on library
75 * _init(). These are global in the same way that function names are
78 static struct xm_driver driver
;
80 void xmesa_set_driver( const struct xm_driver
*templ
)
86 * Global X driver lock
88 pipe_mutex _xmesa_lock
;
92 /**********************************************************************/
93 /***** X Utility Functions *****/
94 /**********************************************************************/
98 * Return the host's byte order as LSBFirst or MSBFirst ala X.
100 static int host_byte_order( void )
103 char *cptr
= (char *) &i
;
104 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
109 * Check if the X Shared Memory extension is available.
110 * Return: 0 = not available
111 * 1 = shared XImage support available
112 * 2 = shared Pixmap support available also
114 int xmesa_check_for_xshm( Display
*display
)
116 #if defined(USE_XSHM)
117 int major
, minor
, ignore
;
120 if (getenv("SP_NO_RAST"))
123 if (getenv("MESA_NOSHM")) {
127 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
128 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
129 return (pixmaps
==True
) ? 2 : 1;
139 /* No XSHM support */
146 * Return the true number of bits per pixel for XImages.
147 * For example, if we request a 24-bit deep visual we may actually need/get
148 * 32bpp XImages. This function returns the appropriate bpp.
149 * Input: dpy - the X display
150 * visinfo - desribes the visual to be used for XImages
151 * Return: true number of bits per pixel for XImages
154 bits_per_pixel( XMesaVisual xmv
)
156 Display
*dpy
= xmv
->display
;
157 XVisualInfo
* visinfo
= xmv
->visinfo
;
160 /* Create a temporary XImage */
161 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
162 ZPixmap
, 0, /*format, offset*/
163 (char*) MALLOC(8), /*data*/
164 1, 1, /*width, height*/
169 /* grab the bits/pixel value */
170 bitsPerPixel
= img
->bits_per_pixel
;
171 /* free the XImage */
172 _mesa_free( img
->data
);
174 XDestroyImage( img
);
181 * Determine if a given X window ID is valid (window exists).
182 * Do this by calling XGetWindowAttributes() for the window and
183 * checking if we catch an X error.
184 * Input: dpy - the display
185 * win - the window to check for existance
186 * Return: GL_TRUE - window exists
187 * GL_FALSE - window doesn't exist
189 static GLboolean WindowExistsFlag
;
191 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
194 if (xerr
->error_code
== BadWindow
) {
195 WindowExistsFlag
= GL_FALSE
;
200 static GLboolean
window_exists( Display
*dpy
, Window win
)
202 XWindowAttributes wa
;
203 int (*old_handler
)( Display
*, XErrorEvent
* );
204 WindowExistsFlag
= GL_TRUE
;
205 old_handler
= XSetErrorHandler(window_exists_err_handler
);
206 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
207 XSetErrorHandler(old_handler
);
208 return WindowExistsFlag
;
212 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
217 unsigned int w
, h
, bw
, depth
;
218 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
226 * Return the size of the window (or pixmap) that corresponds to the
228 * \param width returns width in pixels
229 * \param height returns height in pixels
232 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
233 GLuint
*width
, GLuint
*height
)
237 pipe_mutex_lock(_xmesa_lock
);
238 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
239 stat
= get_drawable_size(dpy
, b
->drawable
, width
, height
);
240 pipe_mutex_unlock(_xmesa_lock
);
243 /* probably querying a window that's recently been destroyed */
244 _mesa_warning(NULL
, "XGetGeometry failed!\n");
245 *width
= *height
= 1;
249 #define GET_REDMASK(__v) __v->mesa_visual.redMask
250 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
251 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
255 * Choose the pixel format for the given visual.
256 * This will tell the gallium driver how to pack pixel data into
260 choose_pixel_format(XMesaVisual v
)
262 boolean native_byte_order
= (host_byte_order() ==
263 ImageByteOrder(v
->display
));
265 if ( GET_REDMASK(v
) == 0x0000ff
266 && GET_GREENMASK(v
) == 0x00ff00
267 && GET_BLUEMASK(v
) == 0xff0000
268 && v
->BitsPerPixel
== 32) {
269 if (native_byte_order
) {
270 /* no byteswapping needed */
271 return 0 /* PIXEL_FORMAT_U_A8_B8_G8_R8 */;
274 return PIPE_FORMAT_R8G8B8A8_UNORM
;
277 else if ( GET_REDMASK(v
) == 0xff0000
278 && GET_GREENMASK(v
) == 0x00ff00
279 && GET_BLUEMASK(v
) == 0x0000ff
280 && v
->BitsPerPixel
== 32) {
281 if (native_byte_order
) {
282 /* no byteswapping needed */
283 return PIPE_FORMAT_A8R8G8B8_UNORM
;
286 return PIPE_FORMAT_B8G8R8A8_UNORM
;
289 else if ( GET_REDMASK(v
) == 0xf800
290 && GET_GREENMASK(v
) == 0x07e0
291 && GET_BLUEMASK(v
) == 0x001f
293 && v
->BitsPerPixel
== 16) {
295 return PIPE_FORMAT_R5G6B5_UNORM
;
304 /**********************************************************************/
305 /***** Linked list of XMesaBuffers *****/
306 /**********************************************************************/
308 XMesaBuffer XMesaBufferList
= NULL
;
312 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
313 * Note that XMesaBuffer is derived from GLframebuffer.
314 * The new XMesaBuffer will not have any size (Width=Height=0).
316 * \param d the corresponding X drawable (window or pixmap)
317 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
318 * \param vis the buffer's visual
319 * \param cmap the window's colormap, if known.
320 * \return new XMesaBuffer or NULL if any problem
323 create_xmesa_buffer(Drawable d
, BufferType type
,
324 XMesaVisual vis
, Colormap cmap
)
328 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
331 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
333 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
343 /* determine PIPE_FORMATs for buffers */
344 colorFormat
= choose_pixel_format(vis
);
346 if (vis
->mesa_visual
.depthBits
== 0)
347 depthFormat
= PIPE_FORMAT_NONE
;
348 #ifdef GALLIUM_CELL /* XXX temporary for Cell! */
350 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
352 else if (vis
->mesa_visual
.depthBits
<= 16)
353 depthFormat
= PIPE_FORMAT_Z16_UNORM
;
354 else if (vis
->mesa_visual
.depthBits
<= 24)
355 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
357 depthFormat
= PIPE_FORMAT_Z32_UNORM
;
360 if (vis
->mesa_visual
.stencilBits
== 8) {
361 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
)
362 stencilFormat
= depthFormat
;
364 stencilFormat
= PIPE_FORMAT_S8_UNORM
;
368 stencilFormat
= PIPE_FORMAT_NONE
;
369 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
) {
370 /* use 24-bit Z, undefined stencil channel */
371 depthFormat
= PIPE_FORMAT_X8Z24_UNORM
;
376 get_drawable_size(vis
->display
, d
, &width
, &height
);
379 * Create framebuffer, but we'll plug in our own renderbuffers below.
381 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
382 colorFormat
, depthFormat
, stencilFormat
,
388 * Create scratch XImage for xmesa_display_surface()
390 b
->tempImage
= XCreateImage(vis
->display
,
391 vis
->visinfo
->visual
,
393 ZPixmap
, 0, /* format, offset */
397 0); /* bytes_per_line */
399 /* GLX_EXT_texture_from_pixmap */
400 b
->TextureTarget
= 0;
401 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
402 b
->TextureMipmap
= 0;
404 /* insert buffer into linked list */
405 b
->Next
= XMesaBufferList
;
413 * Find an XMesaBuffer by matching X display and colormap but NOT matching
414 * the notThis buffer.
417 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
420 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
421 if (b
->xm_visual
->display
== dpy
&&
432 * Remove buffer from linked list, delete if no longer referenced.
435 xmesa_free_buffer(XMesaBuffer buffer
)
437 XMesaBuffer prev
= NULL
, b
;
439 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
441 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
443 /* unlink buffer from list */
445 prev
->Next
= buffer
->Next
;
447 XMesaBufferList
= buffer
->Next
;
449 /* mark as delete pending */
450 fb
->DeletePending
= GL_TRUE
;
452 /* Since the X window for the XMesaBuffer is going away, we don't
453 * want to dereference this pointer in the future.
457 buffer
->tempImage
->data
= NULL
;
458 XDestroyImage(buffer
->tempImage
);
460 /* Unreference. If count = zero we'll really delete the buffer */
461 _mesa_reference_framebuffer(&fb
, NULL
);
463 XFreeGC(b
->xm_visual
->display
, b
->gc
);
469 /* continue search */
472 /* buffer not found in XMesaBufferList */
473 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
478 /**********************************************************************/
479 /***** Misc Private Functions *****/
480 /**********************************************************************/
484 * When a context is bound for the first time, we can finally finish
485 * initializing the context's visual and buffer information.
486 * \param v the XMesaVisual to initialize
487 * \param b the XMesaBuffer to initialize (may be NULL)
488 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
489 * \param window the window/pixmap we're rendering into
490 * \param cmap the colormap associated with the window/pixmap
491 * \return GL_TRUE=success, GL_FALSE=failure
494 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
495 GLboolean rgb_flag
, Drawable window
,
498 ASSERT(!b
|| b
->xm_visual
== v
);
500 /* Save true bits/pixel */
501 v
->BitsPerPixel
= bits_per_pixel(v
);
502 assert(v
->BitsPerPixel
> 0);
504 if (rgb_flag
== GL_FALSE
) {
505 /* COLOR-INDEXED WINDOW: not supported*/
510 * We support RGB rendering into almost any kind of visual.
512 const int xclass
= v
->mesa_visual
.visualType
;
513 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
515 "XMesa: RGB mode rendering not supported in given visual.\n");
518 v
->mesa_visual
.indexBits
= 0;
520 if (v
->BitsPerPixel
== 32) {
521 /* We use XImages for all front/back buffers. If an X Window or
522 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
523 * will be preserved. For XImages we're in luck.
525 v
->mesa_visual
.alphaBits
= 8;
530 * If MESA_INFO env var is set print out some debugging info
531 * which can help Brian figure out what's going on when a user
534 if (_mesa_getenv("MESA_INFO")) {
535 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
536 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
537 _mesa_printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
538 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
542 /* these should have been set in create_xmesa_buffer */
543 ASSERT(b
->drawable
== window
);
545 /* Setup for single/double buffering */
546 if (v
->mesa_visual
.doubleBufferMode
) {
547 /* Double buffered */
548 b
->shm
= xmesa_check_for_xshm( v
->display
);
551 /* X11 graphics context */
552 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
553 XSetFunction( v
->display
, b
->gc
, GXcopy
);
561 #define NUM_VISUAL_TYPES 6
564 * Convert an X visual type to a GLX visual type.
566 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
568 * \return If \c visualType is a valid X visual type, a GLX visual type will
569 * be returned. Otherwise \c GLX_NONE will be returned.
572 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
576 xmesa_convert_from_x_visual_type( int visualType
)
578 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
579 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
580 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
581 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
584 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
585 ? glx_visual_types
[ visualType
] : GLX_NONE
;
589 /**********************************************************************/
590 /***** Public Functions *****/
591 /**********************************************************************/
595 * Create a new X/Mesa visual.
596 * Input: display - X11 display
597 * visinfo - an XVisualInfo pointer
598 * rgb_flag - GL_TRUE = RGB mode,
599 * GL_FALSE = color index mode
600 * alpha_flag - alpha buffer requested?
601 * db_flag - GL_TRUE = double-buffered,
602 * GL_FALSE = single buffered
603 * stereo_flag - stereo visual?
604 * ximage_flag - GL_TRUE = use an XImage for back buffer,
605 * GL_FALSE = use an off-screen pixmap for back buffer
606 * depth_size - requested bits/depth values, or zero
607 * stencil_size - requested bits/stencil values, or zero
608 * accum_red_size - requested bits/red accum values, or zero
609 * accum_green_size - requested bits/green accum values, or zero
610 * accum_blue_size - requested bits/blue accum values, or zero
611 * accum_alpha_size - requested bits/alpha accum values, or zero
612 * num_samples - number of samples/pixel if multisampling, or zero
613 * level - visual level, usually 0
614 * visualCaveat - ala the GLX extension, usually GLX_NONE
615 * Return; a new XMesaVisual or 0 if error.
618 XMesaVisual
XMesaCreateVisual( Display
*display
,
619 XVisualInfo
* visinfo
,
621 GLboolean alpha_flag
,
623 GLboolean stereo_flag
,
624 GLboolean ximage_flag
,
627 GLint accum_red_size
,
628 GLint accum_green_size
,
629 GLint accum_blue_size
,
630 GLint accum_alpha_size
,
636 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
638 /* For debugging only */
639 if (_mesa_getenv("MESA_XSYNC")) {
640 /* This makes debugging X easier.
641 * In your debugger, set a breakpoint on _XError to stop when an
642 * X protocol error is generated.
644 XSynchronize( display
, 1 );
647 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
652 v
->display
= display
;
654 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
655 * the struct but we may need some of the information contained in it
658 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
663 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
665 v
->ximage_flag
= ximage_flag
;
667 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
668 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
669 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
670 v
->mesa_visual
.visualID
= visinfo
->visualid
;
671 v
->mesa_visual
.screen
= visinfo
->screen
;
673 #if !(defined(__cplusplus) || defined(c_plusplus))
674 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
676 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
679 v
->mesa_visual
.visualRating
= visualCaveat
;
682 v
->mesa_visual
.alphaBits
= 8;
684 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
687 const int xclass
= v
->mesa_visual
.visualType
;
688 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
689 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
690 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
691 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
694 /* this is an approximation */
696 depth
= v
->visinfo
->depth
;
697 red_bits
= depth
/ 3;
699 green_bits
= depth
/ 2;
703 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
705 alpha_bits
= v
->mesa_visual
.alphaBits
;
708 _mesa_initialize_visual( &v
->mesa_visual
,
709 rgb_flag
, db_flag
, stereo_flag
,
710 red_bits
, green_bits
,
711 blue_bits
, alpha_bits
,
712 v
->mesa_visual
.indexBits
,
715 accum_red_size
, accum_green_size
,
716 accum_blue_size
, accum_alpha_size
,
720 v
->mesa_visual
.level
= level
;
726 void XMesaDestroyVisual( XMesaVisual v
)
728 _mesa_free(v
->visinfo
);
735 * Create a new XMesaContext.
736 * \param v the XMesaVisual
737 * \param share_list another XMesaContext with which to share display
738 * lists or NULL if no sharing is wanted.
739 * \return an XMesaContext or NULL if error.
742 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
744 static GLboolean firstTime
= GL_TRUE
;
745 struct pipe_screen
*screen
;
746 struct pipe_context
*pipe
;
752 pipe_mutex_init(_xmesa_lock
);
753 firstTime
= GL_FALSE
;
756 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
757 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
761 pf
= choose_pixel_format(v
);
765 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
767 /* XXX: create once per Xlib Display.
769 screen
= driver
.create_pipe_screen();
773 pipe
= driver
.create_pipe_context( screen
,
778 c
->st
= st_create_context(pipe
,
780 share_list
? share_list
->st
: NULL
);
784 mesaCtx
= c
->st
->ctx
;
785 c
->st
->ctx
->DriverCtx
= c
;
788 _mesa_enable_sw_extensions(mesaCtx
);
789 _mesa_enable_1_3_extensions(mesaCtx
);
790 _mesa_enable_1_4_extensions(mesaCtx
);
791 _mesa_enable_1_5_extensions(mesaCtx
);
792 _mesa_enable_2_0_extensions(mesaCtx
);
799 st_destroy_context(c
->st
);
804 screen
->destroy( screen
);
813 void XMesaDestroyContext( XMesaContext c
)
815 st_destroy_context(c
->st
);
817 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
818 * outlive it, causing segfaults
819 struct pipe_screen *screen = c->st->pipe->screen;
820 screen->destroy(screen);
829 * Private function for creating an XMesaBuffer which corresponds to an
830 * X window or pixmap.
831 * \param v the window's XMesaVisual
832 * \param w the window we're wrapping
833 * \return new XMesaBuffer or NULL if error
836 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
838 XWindowAttributes attr
;
846 /* Check that window depth matches visual depth */
847 XGetWindowAttributes( v
->display
, w
, &attr
);
849 if (v
->visinfo
->depth
!= depth
) {
850 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
851 v
->visinfo
->depth
, depth
);
857 cmap
= attr
.colormap
;
860 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
861 /* this is weird, a window w/out a colormap!? */
862 /* OK, let's just allocate a new one and hope for the best */
863 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
866 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
870 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
871 (Drawable
) w
, cmap
)) {
872 xmesa_free_buffer(b
);
882 * Create a new XMesaBuffer from an X pixmap.
884 * \param v the XMesaVisual
885 * \param p the pixmap
886 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
887 * \c GLX_DIRECT_COLOR visual for the pixmap
888 * \returns new XMesaBuffer or NULL if error
891 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
897 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
901 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
902 (Drawable
) p
, cmap
)) {
903 xmesa_free_buffer(b
);
912 * For GLX_EXT_texture_from_pixmap
915 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
917 int format
, int target
, int mipmap
)
919 GET_CURRENT_CONTEXT(ctx
);
921 GLuint width
, height
;
925 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
929 /* get pixmap size, update framebuffer/renderbuffer dims */
930 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
931 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
935 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
936 target
= GLX_TEXTURE_2D_EXT
;
938 else if ( _mesa_bitcount(width
) == 1
939 && _mesa_bitcount(height
) == 1) {
940 /* power of two size */
942 target
= GLX_TEXTURE_1D_EXT
;
945 target
= GLX_TEXTURE_2D_EXT
;
948 else if (ctx
->Extensions
.NV_texture_rectangle
) {
949 target
= GLX_TEXTURE_RECTANGLE_EXT
;
952 /* non power of two textures not supported */
953 XMesaDestroyBuffer(b
);
958 b
->TextureTarget
= target
;
959 b
->TextureFormat
= format
;
960 b
->TextureMipmap
= mipmap
;
962 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
963 (Drawable
) p
, cmap
)) {
964 xmesa_free_buffer(b
);
974 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
975 unsigned int width
, unsigned int height
)
978 Drawable drawable
; /* X Pixmap Drawable */
981 /* allocate pixmap for front buffer */
982 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
983 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
988 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
992 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
994 xmesa_free_buffer(b
);
1004 * Deallocate an XMesaBuffer structure and all related info.
1007 XMesaDestroyBuffer(XMesaBuffer b
)
1009 xmesa_free_buffer(b
);
1014 * Query the current window size and update the corresponding GLframebuffer
1015 * and all attached renderbuffers.
1017 * 1. the first time a buffer is bound to a context.
1018 * 2. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1019 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1020 * when resizing a buffer when no rendering context is bound.
1023 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1025 GLuint width
, height
;
1026 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1027 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1034 * Bind buffer b to context c and make c the current rendering context.
1037 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1038 XMesaBuffer readBuffer
)
1041 if (!drawBuffer
|| !readBuffer
)
1042 return GL_FALSE
; /* must specify buffers! */
1045 /* XXX restore this optimization */
1046 if (&(c
->mesa
) == _mesa_get_current_context()
1047 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1048 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1049 && xmesa_buffer(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1050 /* same context and buffer, do nothing */
1055 c
->xm_buffer
= drawBuffer
;
1057 /* Call this periodically to detect when the user has begun using
1058 * GL rendering from multiple threads.
1060 _glapi_check_multithread();
1062 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1064 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1065 if (readBuffer
!= drawBuffer
)
1066 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1068 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1069 drawBuffer
->wasCurrent
= GL_TRUE
;
1073 st_make_current( 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 GET_CURRENT_CONTEXT(ctx
);
1093 XMesaContext xmesa
= xmesa_context(ctx
);
1107 * Copy the back buffer to the front buffer. If there's no back buffer
1111 void XMesaSwapBuffers( XMesaBuffer b
)
1113 struct pipe_surface
*surf
;
1115 /* If we're swapping the buffer associated with the current context
1116 * we have to flush any pending rendering commands first.
1118 st_notify_swapbuffers(b
->stfb
);
1120 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
, &surf
);
1122 driver
.display_surface(b
, surf
);
1125 xmesa_check_and_update_buffer_size(NULL
, b
);
1131 * Copy sub-region of back buffer to front buffer
1133 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1135 struct pipe_surface
*surf_front
;
1136 struct pipe_surface
*surf_back
;
1137 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1139 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
, &surf_front
);
1140 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
, &surf_back
);
1142 if (!surf_front
|| !surf_back
)
1145 pipe
->surface_copy(pipe
,
1146 surf_front
, x
, y
, /* dest */
1147 surf_back
, x
, y
, /* src */
1153 void XMesaFlush( XMesaContext c
)
1155 if (c
&& c
->xm_visual
->display
) {
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
->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
);
1193 * Look for XMesaBuffers whose X window has been destroyed.
1194 * Deallocate any such XMesaBuffers.
1196 void XMesaGarbageCollect( void )
1198 XMesaBuffer b
, next
;
1199 for (b
=XMesaBufferList
; b
; b
=next
) {
1202 b
->xm_visual
->display
&&
1204 b
->type
== WINDOW
) {
1205 XSync(b
->xm_visual
->display
, False
);
1206 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1207 /* found a dead window, free the ancillary info */
1208 XMesaDestroyBuffer( b
);
1218 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1219 const int *attrib_list
)
1226 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)