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.
57 #include "main/context.h"
58 #include "main/framebuffer.h"
60 #include "state_tracker/st_public.h"
61 #include "state_tracker/st_context.h"
62 #include "pipe/p_defines.h"
63 #include "pipe/p_screen.h"
64 #include "pipe/p_context.h"
66 #include "trace/tr_screen.h"
67 #include "trace/tr_context.h"
68 #include "trace/tr_texture.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
;
90 static struct pipe_screen
*_screen
= NULL
;
91 static struct pipe_screen
*screen
= NULL
;
94 /**********************************************************************/
95 /***** X Utility Functions *****/
96 /**********************************************************************/
100 * Return the host's byte order as LSBFirst or MSBFirst ala X.
102 static int host_byte_order( void )
105 char *cptr
= (char *) &i
;
106 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
113 * Return the true number of bits per pixel for XImages.
114 * For example, if we request a 24-bit deep visual we may actually need/get
115 * 32bpp XImages. This function returns the appropriate bpp.
116 * Input: dpy - the X display
117 * visinfo - desribes the visual to be used for XImages
118 * Return: true number of bits per pixel for XImages
121 bits_per_pixel( XMesaVisual xmv
)
123 Display
*dpy
= xmv
->display
;
124 XVisualInfo
* visinfo
= xmv
->visinfo
;
127 /* Create a temporary XImage */
128 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
129 ZPixmap
, 0, /*format, offset*/
130 (char*) MALLOC(8), /*data*/
131 1, 1, /*width, height*/
136 /* grab the bits/pixel value */
137 bitsPerPixel
= img
->bits_per_pixel
;
138 /* free the XImage */
141 XDestroyImage( img
);
148 * Determine if a given X window ID is valid (window exists).
149 * Do this by calling XGetWindowAttributes() for the window and
150 * checking if we catch an X error.
151 * Input: dpy - the display
152 * win - the window to check for existance
153 * Return: GL_TRUE - window exists
154 * GL_FALSE - window doesn't exist
156 static GLboolean WindowExistsFlag
;
158 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
161 if (xerr
->error_code
== BadWindow
) {
162 WindowExistsFlag
= GL_FALSE
;
167 static GLboolean
window_exists( Display
*dpy
, Window win
)
169 XWindowAttributes wa
;
170 int (*old_handler
)( Display
*, XErrorEvent
* );
171 WindowExistsFlag
= GL_TRUE
;
172 old_handler
= XSetErrorHandler(window_exists_err_handler
);
173 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
174 XSetErrorHandler(old_handler
);
175 return WindowExistsFlag
;
179 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
184 unsigned int w
, h
, bw
, depth
;
185 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
193 * Return the size of the window (or pixmap) that corresponds to the
195 * \param width returns width in pixels
196 * \param height returns height in pixels
199 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
200 GLuint
*width
, GLuint
*height
)
204 pipe_mutex_lock(_xmesa_lock
);
205 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
206 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
207 pipe_mutex_unlock(_xmesa_lock
);
210 /* probably querying a window that's recently been destroyed */
211 _mesa_warning(NULL
, "XGetGeometry failed!\n");
212 *width
= *height
= 1;
216 #define GET_REDMASK(__v) __v->mesa_visual.redMask
217 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
218 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
222 * Choose the pixel format for the given visual.
223 * This will tell the gallium driver how to pack pixel data into
227 choose_pixel_format(XMesaVisual v
)
229 boolean native_byte_order
= (host_byte_order() ==
230 ImageByteOrder(v
->display
));
232 if ( GET_REDMASK(v
) == 0x0000ff
233 && GET_GREENMASK(v
) == 0x00ff00
234 && GET_BLUEMASK(v
) == 0xff0000
235 && v
->BitsPerPixel
== 32) {
236 if (native_byte_order
) {
237 /* no byteswapping needed */
238 return PIPE_FORMAT_R8G8B8A8_UNORM
;
241 return PIPE_FORMAT_A8B8G8R8_UNORM
;
244 else if ( GET_REDMASK(v
) == 0xff0000
245 && GET_GREENMASK(v
) == 0x00ff00
246 && GET_BLUEMASK(v
) == 0x0000ff
247 && v
->BitsPerPixel
== 32) {
248 if (native_byte_order
) {
249 /* no byteswapping needed */
250 return PIPE_FORMAT_B8G8R8A8_UNORM
;
253 return PIPE_FORMAT_A8R8G8B8_UNORM
;
256 else if ( GET_REDMASK(v
) == 0x0000ff00
257 && GET_GREENMASK(v
) == 0x00ff0000
258 && GET_BLUEMASK(v
) == 0xff000000
259 && v
->BitsPerPixel
== 32) {
260 if (native_byte_order
) {
261 /* no byteswapping needed */
262 return PIPE_FORMAT_A8R8G8B8_UNORM
;
265 return PIPE_FORMAT_B8G8R8A8_UNORM
;
268 else if ( GET_REDMASK(v
) == 0xf800
269 && GET_GREENMASK(v
) == 0x07e0
270 && GET_BLUEMASK(v
) == 0x001f
272 && v
->BitsPerPixel
== 16) {
274 return PIPE_FORMAT_B5G6R5_UNORM
;
284 * Query the default gallium screen for a Z/Stencil format that
285 * at least matches the given depthBits and stencilBits.
288 xmesa_choose_z_stencil_format(int depthBits
, int stencilBits
,
289 enum pipe_format
*depthFormat
,
290 enum pipe_format
*stencilFormat
)
292 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
293 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
294 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
295 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
296 static enum pipe_format formats
[] = {
297 PIPE_FORMAT_S8Z24_UNORM
,
298 PIPE_FORMAT_Z24S8_UNORM
,
299 PIPE_FORMAT_Z16_UNORM
,
300 PIPE_FORMAT_Z32_UNORM
306 *depthFormat
= *stencilFormat
= PIPE_FORMAT_NONE
;
308 /* search for supported format */
309 for (i
= 0; i
< Elements(formats
); i
++) {
310 if (screen
->is_format_supported(screen
, formats
[i
],
311 target
, tex_usage
, geom_flags
)) {
312 *depthFormat
= formats
[i
];
318 *stencilFormat
= *depthFormat
;
321 /* XXX we should check that he chosen format has at least as many bits
322 * as what was requested.
328 /**********************************************************************/
329 /***** Linked list of XMesaBuffers *****/
330 /**********************************************************************/
332 XMesaBuffer XMesaBufferList
= NULL
;
336 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
337 * Note that XMesaBuffer is derived from GLframebuffer.
338 * The new XMesaBuffer will not have any size (Width=Height=0).
340 * \param d the corresponding X drawable (window or pixmap)
341 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
342 * \param vis the buffer's visual
343 * \param cmap the window's colormap, if known.
344 * \return new XMesaBuffer or NULL if any problem
347 create_xmesa_buffer(Drawable d
, BufferType type
,
348 XMesaVisual vis
, Colormap cmap
)
352 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
355 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
357 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
362 b
->ws
.visual
= vis
->visinfo
->visual
;
363 b
->ws
.depth
= vis
->visinfo
->depth
;
369 /* determine PIPE_FORMATs for buffers */
370 colorFormat
= choose_pixel_format(vis
);
372 xmesa_choose_z_stencil_format(vis
->mesa_visual
.depthBits
,
373 vis
->mesa_visual
.stencilBits
,
374 &depthFormat
, &stencilFormat
);
377 get_drawable_size(vis
->display
, d
, &width
, &height
);
380 * Create framebuffer, but we'll plug in our own renderbuffers below.
382 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
383 colorFormat
, depthFormat
, stencilFormat
,
388 /* GLX_EXT_texture_from_pixmap */
389 b
->TextureTarget
= 0;
390 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
391 b
->TextureMipmap
= 0;
393 /* insert buffer into linked list */
394 b
->Next
= XMesaBufferList
;
402 * Find an XMesaBuffer by matching X display and colormap but NOT matching
403 * the notThis buffer.
406 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
409 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
410 if (b
->xm_visual
->display
== dpy
&&
421 * Remove buffer from linked list, delete if no longer referenced.
424 xmesa_free_buffer(XMesaBuffer buffer
)
426 XMesaBuffer prev
= NULL
, b
;
428 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
430 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
432 /* unlink buffer from list */
434 prev
->Next
= buffer
->Next
;
436 XMesaBufferList
= buffer
->Next
;
438 /* mark as delete pending */
439 fb
->DeletePending
= GL_TRUE
;
441 /* Since the X window for the XMesaBuffer is going away, we don't
442 * want to dereference this pointer in the future.
446 /* Unreference. If count = zero we'll really delete the buffer */
447 _mesa_reference_framebuffer(&fb
, NULL
);
449 XFreeGC(b
->xm_visual
->display
, b
->ws
.gc
);
455 /* continue search */
458 /* buffer not found in XMesaBufferList */
459 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
464 /**********************************************************************/
465 /***** Misc Private Functions *****/
466 /**********************************************************************/
470 * When a context is bound for the first time, we can finally finish
471 * initializing the context's visual and buffer information.
472 * \param v the XMesaVisual to initialize
473 * \param b the XMesaBuffer to initialize (may be NULL)
474 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
475 * \param window the window/pixmap we're rendering into
476 * \param cmap the colormap associated with the window/pixmap
477 * \return GL_TRUE=success, GL_FALSE=failure
480 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
481 GLboolean rgb_flag
, Drawable window
,
484 ASSERT(!b
|| b
->xm_visual
== v
);
486 /* Save true bits/pixel */
487 v
->BitsPerPixel
= bits_per_pixel(v
);
488 assert(v
->BitsPerPixel
> 0);
490 if (rgb_flag
== GL_FALSE
) {
491 /* COLOR-INDEXED WINDOW: not supported*/
496 * We support RGB rendering into almost any kind of visual.
498 const int xclass
= v
->mesa_visual
.visualType
;
499 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
501 "XMesa: RGB mode rendering not supported in given visual.\n");
504 v
->mesa_visual
.indexBits
= 0;
506 if (v
->BitsPerPixel
== 32) {
507 /* We use XImages for all front/back buffers. If an X Window or
508 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
509 * will be preserved. For XImages we're in luck.
511 v
->mesa_visual
.alphaBits
= 8;
516 * If MESA_INFO env var is set print out some debugging info
517 * which can help Brian figure out what's going on when a user
520 if (_mesa_getenv("MESA_INFO")) {
521 printf("X/Mesa visual = %p\n", (void *) v
);
522 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
523 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
524 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
528 /* these should have been set in create_xmesa_buffer */
529 ASSERT(b
->ws
.drawable
== window
);
532 /* X11 graphics context */
533 b
->ws
.gc
= XCreateGC( v
->display
, window
, 0, NULL
);
534 XSetFunction( v
->display
, b
->ws
.gc
, GXcopy
);
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
,
617 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
619 xmesa_init( display
);
621 /* For debugging only */
622 if (_mesa_getenv("MESA_XSYNC")) {
623 /* This makes debugging X easier.
624 * In your debugger, set a breakpoint on _XError to stop when an
625 * X protocol error is generated.
627 XSynchronize( display
, 1 );
630 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
635 v
->display
= display
;
637 /* Save a copy of the XVisualInfo struct because the user may Xfree()
638 * the struct but we may need some of the information contained in it
641 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
646 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
648 v
->ximage_flag
= ximage_flag
;
650 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
651 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
652 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
653 v
->mesa_visual
.visualID
= visinfo
->visualid
;
654 v
->mesa_visual
.screen
= visinfo
->screen
;
656 #if !(defined(__cplusplus) || defined(c_plusplus))
657 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
659 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
662 v
->mesa_visual
.visualRating
= visualCaveat
;
665 v
->mesa_visual
.alphaBits
= 8;
667 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
670 const int xclass
= v
->mesa_visual
.visualType
;
671 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
672 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
673 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
674 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
677 /* this is an approximation */
679 depth
= v
->visinfo
->depth
;
680 red_bits
= depth
/ 3;
682 green_bits
= depth
/ 2;
686 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
688 alpha_bits
= v
->mesa_visual
.alphaBits
;
691 _mesa_initialize_visual( &v
->mesa_visual
,
692 db_flag
, stereo_flag
,
693 red_bits
, green_bits
,
694 blue_bits
, alpha_bits
,
697 accum_red_size
, accum_green_size
,
698 accum_blue_size
, accum_alpha_size
,
702 v
->mesa_visual
.level
= level
;
708 void XMesaDestroyVisual( XMesaVisual v
)
716 * Do one-time initializations.
719 xmesa_init( Display
*display
)
721 static GLboolean firstTime
= GL_TRUE
;
723 pipe_mutex_init(_xmesa_lock
);
724 _screen
= driver
.create_pipe_screen( display
);
725 screen
= trace_screen_create( _screen
);
726 firstTime
= GL_FALSE
;
732 * Create a new XMesaContext.
733 * \param v the XMesaVisual
734 * \param share_list another XMesaContext with which to share display
735 * lists or NULL if no sharing is wanted.
736 * \return an XMesaContext or NULL if error.
739 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
741 struct pipe_context
*pipe
= NULL
;
746 xmesa_init( v
->display
);
748 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
749 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
753 pf
= choose_pixel_format(v
);
757 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
758 c
->xm_read_buffer
= NULL
;
763 /* Trace screen knows how to properly wrap context creation in the
764 * wrapped screen, so nothing special to do here:
766 pipe
= screen
->context_create(screen
, (void *) c
);
770 c
->st
= st_create_context(pipe
,
772 share_list
? share_list
->st
: NULL
);
776 mesaCtx
= c
->st
->ctx
;
777 c
->st
->ctx
->DriverCtx
= c
;
783 st_destroy_context(c
->st
);
794 void XMesaDestroyContext( XMesaContext c
)
796 st_destroy_context(c
->st
);
798 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
799 * outlive it, causing segfaults
800 struct pipe_screen *screen = c->st->pipe->screen;
801 screen->destroy(screen);
810 * Private function for creating an XMesaBuffer which corresponds to an
811 * X window or pixmap.
812 * \param v the window's XMesaVisual
813 * \param w the window we're wrapping
814 * \return new XMesaBuffer or NULL if error
817 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
819 XWindowAttributes attr
;
827 /* Check that window depth matches visual depth */
828 XGetWindowAttributes( v
->display
, w
, &attr
);
830 if (v
->visinfo
->depth
!= depth
) {
831 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
832 v
->visinfo
->depth
, depth
);
838 cmap
= attr
.colormap
;
841 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
842 /* this is weird, a window w/out a colormap!? */
843 /* OK, let's just allocate a new one and hope for the best */
844 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
847 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
851 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
852 (Drawable
) w
, cmap
)) {
853 xmesa_free_buffer(b
);
863 * Create a new XMesaBuffer from an X pixmap.
865 * \param v the XMesaVisual
866 * \param p the pixmap
867 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
868 * \c GLX_DIRECT_COLOR visual for the pixmap
869 * \returns new XMesaBuffer or NULL if error
872 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
878 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
882 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
883 (Drawable
) p
, cmap
)) {
884 xmesa_free_buffer(b
);
893 * For GLX_EXT_texture_from_pixmap
896 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
898 int format
, int target
, int mipmap
)
900 GET_CURRENT_CONTEXT(ctx
);
902 GLuint width
, height
;
906 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
910 /* get pixmap size, update framebuffer/renderbuffer dims */
911 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
912 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
916 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
917 target
= GLX_TEXTURE_2D_EXT
;
919 else if ( _mesa_bitcount(width
) == 1
920 && _mesa_bitcount(height
) == 1) {
921 /* power of two size */
923 target
= GLX_TEXTURE_1D_EXT
;
926 target
= GLX_TEXTURE_2D_EXT
;
929 else if (ctx
->Extensions
.NV_texture_rectangle
) {
930 target
= GLX_TEXTURE_RECTANGLE_EXT
;
933 /* non power of two textures not supported */
934 XMesaDestroyBuffer(b
);
939 b
->TextureTarget
= target
;
940 b
->TextureFormat
= format
;
941 b
->TextureMipmap
= mipmap
;
943 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
944 (Drawable
) p
, cmap
)) {
945 xmesa_free_buffer(b
);
955 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
956 unsigned int width
, unsigned int height
)
959 Drawable drawable
; /* X Pixmap Drawable */
962 /* allocate pixmap for front buffer */
963 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
964 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
969 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
973 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
975 xmesa_free_buffer(b
);
985 * Deallocate an XMesaBuffer structure and all related info.
988 XMesaDestroyBuffer(XMesaBuffer b
)
990 xmesa_free_buffer(b
);
995 * Query the current window size and update the corresponding GLframebuffer
996 * and all attached renderbuffers.
998 * 1. the first time a buffer is bound to a context.
999 * 2. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1000 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1001 * when resizing a buffer when no rendering context is bound.
1004 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1006 GLuint width
, height
;
1007 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1008 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1015 * Bind buffer b to context c and make c the current rendering context.
1018 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1019 XMesaBuffer readBuffer
)
1021 XMesaContext old_ctx
= XMesaGetCurrentContext();
1023 if (old_ctx
&& old_ctx
!= c
) {
1024 XMesaFlush(old_ctx
);
1025 old_ctx
->xm_buffer
= NULL
;
1026 old_ctx
->xm_read_buffer
= NULL
;
1030 if (!drawBuffer
|| !readBuffer
)
1031 return GL_FALSE
; /* must specify buffers! */
1034 c
->xm_buffer
== drawBuffer
&&
1035 c
->xm_read_buffer
== readBuffer
)
1038 c
->xm_buffer
= drawBuffer
;
1039 c
->xm_read_buffer
= readBuffer
;
1041 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1043 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1044 if (readBuffer
!= drawBuffer
)
1045 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1047 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1048 drawBuffer
->wasCurrent
= GL_TRUE
;
1052 st_make_current( NULL
, NULL
, NULL
);
1060 * Unbind the context c from its buffer.
1062 GLboolean
XMesaUnbindContext( XMesaContext c
)
1064 /* A no-op for XFree86 integration purposes */
1069 XMesaContext
XMesaGetCurrentContext( void )
1071 GET_CURRENT_CONTEXT(ctx
);
1073 XMesaContext xmesa
= xmesa_context(ctx
);
1084 * Swap front and back color buffers and have winsys display front buffer.
1085 * If there's no front color buffer no swap actually occurs.
1088 void XMesaSwapBuffers( XMesaBuffer b
)
1090 struct pipe_surface
*frontLeftSurf
;
1092 st_swapbuffers(b
->stfb
, &frontLeftSurf
, NULL
);
1094 if (frontLeftSurf
) {
1095 screen
->flush_frontbuffer( screen
,
1100 xmesa_check_and_update_buffer_size(NULL
, b
);
1106 * Copy sub-region of back buffer to front buffer
1108 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1110 struct pipe_surface
*surf_front
;
1111 struct pipe_surface
*surf_back
;
1112 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1114 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
, &surf_front
);
1115 st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
, &surf_back
);
1117 if (!surf_front
|| !surf_back
)
1121 pipe
->surface_copy(pipe
,
1122 surf_front
, x
, y
, /* dest */
1123 surf_back
, x
, y
, /* src */
1129 void XMesaFlush( XMesaContext c
)
1131 if (c
&& c
->xm_visual
->display
) {
1133 XSync( c
->xm_visual
->display
, False
);
1141 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1144 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1145 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1154 * Free/destroy all XMesaBuffers associated with given display.
1156 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1158 XMesaBuffer b
, next
;
1159 for (b
= XMesaBufferList
; b
; b
= next
) {
1161 if (b
->xm_visual
->display
== dpy
) {
1162 xmesa_free_buffer(b
);
1169 * Look for XMesaBuffers whose X window has been destroyed.
1170 * Deallocate any such XMesaBuffers.
1172 void XMesaGarbageCollect( void )
1174 XMesaBuffer b
, next
;
1175 for (b
=XMesaBufferList
; b
; b
=next
) {
1178 b
->xm_visual
->display
&&
1180 b
->type
== WINDOW
) {
1181 XSync(b
->xm_visual
->display
, False
);
1182 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1183 /* found a dead window, free the ancillary info */
1184 XMesaDestroyBuffer( b
);
1194 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1195 const int *attrib_list
)
1202 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)