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
;
74 void xmesa_set_driver( const struct xm_driver
*templ
)
80 * Global X driver lock
82 pipe_mutex _xmesa_lock
;
84 static struct pipe_screen
*screen
= NULL
;
85 static struct st_api
*stapi
= NULL
;
86 static struct st_manager
*smapi
= NULL
;
89 /**********************************************************************/
90 /***** X Utility Functions *****/
91 /**********************************************************************/
95 * Return the host's byte order as LSBFirst or MSBFirst ala X.
97 static int host_byte_order( void )
100 char *cptr
= (char *) &i
;
101 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
108 * Return the true number of bits per pixel for XImages.
109 * For example, if we request a 24-bit deep visual we may actually need/get
110 * 32bpp XImages. This function returns the appropriate bpp.
111 * Input: dpy - the X display
112 * visinfo - desribes the visual to be used for XImages
113 * Return: true number of bits per pixel for XImages
116 bits_per_pixel( XMesaVisual xmv
)
118 Display
*dpy
= xmv
->display
;
119 XVisualInfo
* visinfo
= xmv
->visinfo
;
122 /* Create a temporary XImage */
123 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
124 ZPixmap
, 0, /*format, offset*/
125 (char*) MALLOC(8), /*data*/
126 1, 1, /*width, height*/
131 /* grab the bits/pixel value */
132 bitsPerPixel
= img
->bits_per_pixel
;
133 /* free the XImage */
136 XDestroyImage( img
);
143 * Determine if a given X window ID is valid (window exists).
144 * Do this by calling XGetWindowAttributes() for the window and
145 * checking if we catch an X error.
146 * Input: dpy - the display
147 * win - the window to check for existance
148 * Return: GL_TRUE - window exists
149 * GL_FALSE - window doesn't exist
151 static GLboolean WindowExistsFlag
;
153 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
156 if (xerr
->error_code
== BadWindow
) {
157 WindowExistsFlag
= GL_FALSE
;
162 static GLboolean
window_exists( Display
*dpy
, Window win
)
164 XWindowAttributes wa
;
165 int (*old_handler
)( Display
*, XErrorEvent
* );
166 WindowExistsFlag
= GL_TRUE
;
167 old_handler
= XSetErrorHandler(window_exists_err_handler
);
168 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
169 XSetErrorHandler(old_handler
);
170 return WindowExistsFlag
;
174 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
179 unsigned int w
, h
, bw
, depth
;
180 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
188 * Return the size of the window (or pixmap) that corresponds to the
190 * \param width returns width in pixels
191 * \param height returns height in pixels
194 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
195 GLuint
*width
, GLuint
*height
)
199 pipe_mutex_lock(_xmesa_lock
);
200 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
201 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
202 pipe_mutex_unlock(_xmesa_lock
);
205 /* probably querying a window that's recently been destroyed */
206 _mesa_warning(NULL
, "XGetGeometry failed!\n");
207 *width
= *height
= 1;
211 #define GET_REDMASK(__v) __v->mesa_visual.redMask
212 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
213 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
217 * Choose the pixel format for the given visual.
218 * This will tell the gallium driver how to pack pixel data into
222 choose_pixel_format(XMesaVisual v
)
224 boolean native_byte_order
= (host_byte_order() ==
225 ImageByteOrder(v
->display
));
227 if ( GET_REDMASK(v
) == 0x0000ff
228 && GET_GREENMASK(v
) == 0x00ff00
229 && GET_BLUEMASK(v
) == 0xff0000
230 && v
->BitsPerPixel
== 32) {
231 if (native_byte_order
) {
232 /* no byteswapping needed */
233 return PIPE_FORMAT_R8G8B8A8_UNORM
;
236 return PIPE_FORMAT_A8B8G8R8_UNORM
;
239 else if ( GET_REDMASK(v
) == 0xff0000
240 && GET_GREENMASK(v
) == 0x00ff00
241 && GET_BLUEMASK(v
) == 0x0000ff
242 && v
->BitsPerPixel
== 32) {
243 if (native_byte_order
) {
244 /* no byteswapping needed */
245 return PIPE_FORMAT_B8G8R8A8_UNORM
;
248 return PIPE_FORMAT_A8R8G8B8_UNORM
;
251 else if ( GET_REDMASK(v
) == 0x0000ff00
252 && GET_GREENMASK(v
) == 0x00ff0000
253 && GET_BLUEMASK(v
) == 0xff000000
254 && v
->BitsPerPixel
== 32) {
255 if (native_byte_order
) {
256 /* no byteswapping needed */
257 return PIPE_FORMAT_A8R8G8B8_UNORM
;
260 return PIPE_FORMAT_B8G8R8A8_UNORM
;
263 else if ( GET_REDMASK(v
) == 0xf800
264 && GET_GREENMASK(v
) == 0x07e0
265 && GET_BLUEMASK(v
) == 0x001f
267 && v
->BitsPerPixel
== 16) {
269 return PIPE_FORMAT_B5G6R5_UNORM
;
277 * Choose a depth/stencil format that is "better" than the given depth and
280 static enum pipe_format
281 choose_depth_stencil_format(int depth
, int stencil
)
283 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
284 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
285 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
286 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
287 enum pipe_format formats
[8], fmt
;
293 if (depth
<= 24 && stencil
<= 8) {
294 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
295 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
300 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
302 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
305 fmt
= PIPE_FORMAT_NONE
;
306 for (i
= 0; i
< count
; i
++) {
307 if (screen
->is_format_supported(screen
, formats
[i
],
308 target
, tex_usage
, geom_flags
)) {
319 /**********************************************************************/
320 /***** Linked list of XMesaBuffers *****/
321 /**********************************************************************/
323 XMesaBuffer XMesaBufferList
= NULL
;
327 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
328 * Note that XMesaBuffer is derived from GLframebuffer.
329 * The new XMesaBuffer will not have any size (Width=Height=0).
331 * \param d the corresponding X drawable (window or pixmap)
332 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
333 * \param vis the buffer's visual
334 * \param cmap the window's colormap, if known.
335 * \return new XMesaBuffer or NULL if any problem
338 create_xmesa_buffer(Drawable d
, BufferType type
,
339 XMesaVisual vis
, Colormap cmap
)
344 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
346 xmesa_init(vis
->display
);
350 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
355 b
->ws
.visual
= vis
->visinfo
->visual
;
356 b
->ws
.depth
= vis
->visinfo
->depth
;
362 get_drawable_size(vis
->display
, d
, &width
, &height
);
365 * Create framebuffer, but we'll plug in our own renderbuffers below.
367 b
->stfb
= xmesa_create_st_framebuffer(screen
, b
);
369 /* GLX_EXT_texture_from_pixmap */
370 b
->TextureTarget
= 0;
371 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
372 b
->TextureMipmap
= 0;
374 /* insert buffer into linked list */
375 b
->Next
= XMesaBufferList
;
383 * Find an XMesaBuffer by matching X display and colormap but NOT matching
384 * the notThis buffer.
387 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
390 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
391 if (b
->xm_visual
->display
== dpy
&&
402 * Remove buffer from linked list, delete if no longer referenced.
405 xmesa_free_buffer(XMesaBuffer buffer
)
407 XMesaBuffer prev
= NULL
, b
;
409 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
411 /* unlink buffer from list */
413 prev
->Next
= buffer
->Next
;
415 XMesaBufferList
= buffer
->Next
;
417 /* Since the X window for the XMesaBuffer is going away, we don't
418 * want to dereference this pointer in the future.
422 /* XXX we should move the buffer to a delete-pending list and destroy
423 * the buffer until it is no longer current.
425 xmesa_destroy_st_framebuffer(buffer
->stfb
);
431 /* continue search */
434 /* buffer not found in XMesaBufferList */
435 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
440 /**********************************************************************/
441 /***** Misc Private Functions *****/
442 /**********************************************************************/
446 * When a context is bound for the first time, we can finally finish
447 * initializing the context's visual and buffer information.
448 * \param v the XMesaVisual to initialize
449 * \param b the XMesaBuffer to initialize (may be NULL)
450 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
451 * \param window the window/pixmap we're rendering into
452 * \param cmap the colormap associated with the window/pixmap
453 * \return GL_TRUE=success, GL_FALSE=failure
456 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
457 GLboolean rgb_flag
, Drawable window
,
460 ASSERT(!b
|| b
->xm_visual
== v
);
462 /* Save true bits/pixel */
463 v
->BitsPerPixel
= bits_per_pixel(v
);
464 assert(v
->BitsPerPixel
> 0);
466 if (rgb_flag
== GL_FALSE
) {
467 /* COLOR-INDEXED WINDOW: not supported*/
472 * We support RGB rendering into almost any kind of visual.
474 const int xclass
= v
->mesa_visual
.visualType
;
475 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
477 "XMesa: RGB mode rendering not supported in given visual.\n");
480 v
->mesa_visual
.indexBits
= 0;
482 if (v
->BitsPerPixel
== 32) {
483 /* We use XImages for all front/back buffers. If an X Window or
484 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
485 * will be preserved. For XImages we're in luck.
487 v
->mesa_visual
.alphaBits
= 8;
492 * If MESA_INFO env var is set print out some debugging info
493 * which can help Brian figure out what's going on when a user
496 if (_mesa_getenv("MESA_INFO")) {
497 printf("X/Mesa visual = %p\n", (void *) v
);
498 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
499 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
500 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
508 #define NUM_VISUAL_TYPES 6
511 * Convert an X visual type to a GLX visual type.
513 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
515 * \return If \c visualType is a valid X visual type, a GLX visual type will
516 * be returned. Otherwise \c GLX_NONE will be returned.
519 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
523 xmesa_convert_from_x_visual_type( int visualType
)
525 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
526 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
527 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
528 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
531 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
532 ? glx_visual_types
[ visualType
] : GLX_NONE
;
536 /**********************************************************************/
537 /***** Public Functions *****/
538 /**********************************************************************/
542 * Create a new X/Mesa visual.
543 * Input: display - X11 display
544 * visinfo - an XVisualInfo pointer
545 * rgb_flag - GL_TRUE = RGB mode,
546 * GL_FALSE = color index mode
547 * alpha_flag - alpha buffer requested?
548 * db_flag - GL_TRUE = double-buffered,
549 * GL_FALSE = single buffered
550 * stereo_flag - stereo visual?
551 * ximage_flag - GL_TRUE = use an XImage for back buffer,
552 * GL_FALSE = use an off-screen pixmap for back buffer
553 * depth_size - requested bits/depth values, or zero
554 * stencil_size - requested bits/stencil values, or zero
555 * accum_red_size - requested bits/red accum values, or zero
556 * accum_green_size - requested bits/green accum values, or zero
557 * accum_blue_size - requested bits/blue accum values, or zero
558 * accum_alpha_size - requested bits/alpha accum values, or zero
559 * num_samples - number of samples/pixel if multisampling, or zero
560 * level - visual level, usually 0
561 * visualCaveat - ala the GLX extension, usually GLX_NONE
562 * Return; a new XMesaVisual or 0 if error.
565 XMesaVisual
XMesaCreateVisual( Display
*display
,
566 XVisualInfo
* visinfo
,
568 GLboolean alpha_flag
,
570 GLboolean stereo_flag
,
571 GLboolean ximage_flag
,
574 GLint accum_red_size
,
575 GLint accum_green_size
,
576 GLint accum_blue_size
,
577 GLint accum_alpha_size
,
583 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
585 xmesa_init( display
);
587 /* For debugging only */
588 if (_mesa_getenv("MESA_XSYNC")) {
589 /* This makes debugging X easier.
590 * In your debugger, set a breakpoint on _XError to stop when an
591 * X protocol error is generated.
593 XSynchronize( display
, 1 );
596 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
601 v
->display
= display
;
603 /* Save a copy of the XVisualInfo struct because the user may Xfree()
604 * the struct but we may need some of the information contained in it
607 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
612 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
614 v
->ximage_flag
= ximage_flag
;
616 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
617 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
618 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
619 v
->mesa_visual
.visualID
= visinfo
->visualid
;
620 v
->mesa_visual
.screen
= visinfo
->screen
;
622 #if !(defined(__cplusplus) || defined(c_plusplus))
623 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
625 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
628 v
->mesa_visual
.visualRating
= visualCaveat
;
631 v
->mesa_visual
.alphaBits
= 8;
633 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
636 const int xclass
= v
->mesa_visual
.visualType
;
637 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
638 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
639 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
640 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
643 /* this is an approximation */
645 depth
= v
->visinfo
->depth
;
646 red_bits
= depth
/ 3;
648 green_bits
= depth
/ 2;
652 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
654 alpha_bits
= v
->mesa_visual
.alphaBits
;
657 _mesa_initialize_visual( &v
->mesa_visual
,
658 db_flag
, stereo_flag
,
659 red_bits
, green_bits
,
660 blue_bits
, alpha_bits
,
663 accum_red_size
, accum_green_size
,
664 accum_blue_size
, accum_alpha_size
,
667 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
669 v
->stvis
.buffer_mask
= ST_ATTACHMENT_BACK_LEFT_MASK
;
671 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
673 v
->stvis
.buffer_mask
= ST_ATTACHMENT_BACK_RIGHT_MASK
;
676 v
->stvis
.color_format
= choose_pixel_format(v
);
677 v
->stvis
.depth_stencil_format
=
678 choose_depth_stencil_format(depth_size
, stencil_size
);
680 v
->stvis
.accum_format
= (accum_red_size
+
681 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
682 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
684 v
->stvis
.samples
= num_samples
;
685 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
688 v
->mesa_visual
.level
= level
;
694 void XMesaDestroyVisual( XMesaVisual v
)
702 * Do one-time initializations.
705 xmesa_init( Display
*display
)
707 static GLboolean firstTime
= GL_TRUE
;
709 pipe_mutex_init(_xmesa_lock
);
710 screen
= driver
.create_pipe_screen( display
);
711 stapi
= driver
.create_st_api();
712 smapi
= CALLOC_STRUCT(st_manager
);
714 smapi
->screen
= screen
;
716 if (!screen
|| !stapi
|| !smapi
) {
718 screen
->destroy(screen
);
722 stapi
->destroy(stapi
);
731 firstTime
= GL_FALSE
;
737 * Create a new XMesaContext.
738 * \param v the XMesaVisual
739 * \param share_list another XMesaContext with which to share display
740 * lists or NULL if no sharing is wanted.
741 * \return an XMesaContext or NULL if error.
744 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
748 xmesa_init( v
->display
);
750 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
751 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
756 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
757 c
->xm_read_buffer
= NULL
;
762 c
->st
= stapi
->create_context(stapi
, smapi
,
763 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
767 c
->st
->st_manager_private
= (void *) c
;
773 c
->st
->destroy(c
->st
);
782 void XMesaDestroyContext( XMesaContext c
)
784 c
->st
->destroy(c
->st
);
786 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
787 * outlive it, causing segfaults
788 struct pipe_screen *screen = c->st->pipe->screen;
789 screen->destroy(screen);
798 * Private function for creating an XMesaBuffer which corresponds to an
799 * X window or pixmap.
800 * \param v the window's XMesaVisual
801 * \param w the window we're wrapping
802 * \return new XMesaBuffer or NULL if error
805 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
807 XWindowAttributes attr
;
815 /* Check that window depth matches visual depth */
816 XGetWindowAttributes( v
->display
, w
, &attr
);
818 if (v
->visinfo
->depth
!= depth
) {
819 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
820 v
->visinfo
->depth
, depth
);
826 cmap
= attr
.colormap
;
829 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
830 /* this is weird, a window w/out a colormap!? */
831 /* OK, let's just allocate a new one and hope for the best */
832 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
835 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
839 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
840 (Drawable
) w
, cmap
)) {
841 xmesa_free_buffer(b
);
851 * Create a new XMesaBuffer from an X pixmap.
853 * \param v the XMesaVisual
854 * \param p the pixmap
855 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
856 * \c GLX_DIRECT_COLOR visual for the pixmap
857 * \returns new XMesaBuffer or NULL if error
860 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
866 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
870 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
871 (Drawable
) p
, cmap
)) {
872 xmesa_free_buffer(b
);
881 * For GLX_EXT_texture_from_pixmap
884 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
886 int format
, int target
, int mipmap
)
888 GET_CURRENT_CONTEXT(ctx
);
893 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
897 /* get pixmap size */
898 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
902 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
903 target
= GLX_TEXTURE_2D_EXT
;
905 else if ( _mesa_bitcount(b
->width
) == 1
906 && _mesa_bitcount(b
->height
) == 1) {
907 /* power of two size */
908 if (b
->height
== 1) {
909 target
= GLX_TEXTURE_1D_EXT
;
912 target
= GLX_TEXTURE_2D_EXT
;
915 else if (ctx
->Extensions
.NV_texture_rectangle
) {
916 target
= GLX_TEXTURE_RECTANGLE_EXT
;
919 /* non power of two textures not supported */
920 XMesaDestroyBuffer(b
);
925 b
->TextureTarget
= target
;
926 b
->TextureFormat
= format
;
927 b
->TextureMipmap
= mipmap
;
929 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
930 (Drawable
) p
, cmap
)) {
931 xmesa_free_buffer(b
);
941 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
942 unsigned int width
, unsigned int height
)
945 Drawable drawable
; /* X Pixmap Drawable */
948 /* allocate pixmap for front buffer */
949 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
950 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
955 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
959 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
961 xmesa_free_buffer(b
);
971 * Deallocate an XMesaBuffer structure and all related info.
974 XMesaDestroyBuffer(XMesaBuffer b
)
976 xmesa_free_buffer(b
);
981 * Query the current drawable size and notify the binding context.
984 xmesa_check_buffer_size(XMesaBuffer b
)
986 XMesaContext xmctx
= XMesaGetCurrentContext();
988 if (b
->type
== PBUFFER
)
991 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
992 if (xmctx
&& xmctx
->xm_buffer
== b
)
993 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
998 * Bind buffer b to context c and make c the current rendering context.
1001 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1002 XMesaBuffer readBuffer
)
1004 XMesaContext old_ctx
= XMesaGetCurrentContext();
1006 if (old_ctx
&& old_ctx
!= c
) {
1007 XMesaFlush(old_ctx
);
1008 old_ctx
->xm_buffer
= NULL
;
1009 old_ctx
->xm_read_buffer
= NULL
;
1013 if (!drawBuffer
|| !readBuffer
)
1014 return GL_FALSE
; /* must specify buffers! */
1017 c
->xm_buffer
== drawBuffer
&&
1018 c
->xm_read_buffer
== readBuffer
)
1021 xmesa_check_buffer_size(drawBuffer
);
1022 if (readBuffer
!= drawBuffer
)
1023 xmesa_check_buffer_size(readBuffer
);
1025 c
->xm_buffer
= drawBuffer
;
1026 c
->xm_read_buffer
= readBuffer
;
1028 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1030 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1031 drawBuffer
->wasCurrent
= GL_TRUE
;
1035 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1043 * Unbind the context c from its buffer.
1045 GLboolean
XMesaUnbindContext( XMesaContext c
)
1047 /* A no-op for XFree86 integration purposes */
1052 XMesaContext
XMesaGetCurrentContext( void )
1054 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1055 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1061 * Swap front and back color buffers and have winsys display front buffer.
1062 * If there's no front color buffer no swap actually occurs.
1065 void XMesaSwapBuffers( XMesaBuffer b
)
1067 XMesaContext xmctx
= XMesaGetCurrentContext();
1069 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1070 xmctx
->st
->flush( xmctx
->st
,
1071 PIPE_FLUSH_RENDER_CACHE
|
1072 PIPE_FLUSH_SWAPBUFFERS
|
1077 xmesa_swap_st_framebuffer(b
->stfb
);
1083 * Copy sub-region of back buffer to front buffer
1085 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1087 xmesa_copy_st_framebuffer(b
->stfb
,
1088 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1089 x
, y
, width
, height
);
1094 void XMesaFlush( XMesaContext c
)
1096 if (c
&& c
->xm_visual
->display
) {
1097 struct pipe_fence_handle
*fence
= NULL
;
1099 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1101 screen
->fence_finish(screen
, fence
, 0);
1102 screen
->fence_reference(screen
, &fence
, NULL
);
1104 XSync( c
->xm_visual
->display
, False
);
1112 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1115 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1116 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1125 * Free/destroy all XMesaBuffers associated with given display.
1127 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1129 XMesaBuffer b
, next
;
1130 for (b
= XMesaBufferList
; b
; b
= next
) {
1132 if (b
->xm_visual
->display
== dpy
) {
1133 xmesa_free_buffer(b
);
1140 * Look for XMesaBuffers whose X window has been destroyed.
1141 * Deallocate any such XMesaBuffers.
1143 void XMesaGarbageCollect( void )
1145 XMesaBuffer b
, next
;
1146 for (b
=XMesaBufferList
; b
; b
=next
) {
1149 b
->xm_visual
->display
&&
1151 b
->type
== WINDOW
) {
1152 XSync(b
->xm_visual
->display
, False
);
1153 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1154 /* found a dead window, free the ancillary info */
1155 XMesaDestroyBuffer( b
);
1165 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1166 const int *attrib_list
)
1173 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1179 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1182 dst
->st
->copy(dst
->st
, src
->st
, mask
);