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
;
314 * Choose a depth/stencil format that satisfies the given depth and
317 static enum pipe_format
318 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
320 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
321 const unsigned tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
322 const unsigned geom_flags
= (PIPE_TEXTURE_GEOM_NON_SQUARE
|
323 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
);
324 enum pipe_format formats
[8], fmt
;
329 if (depth
<= 16 && stencil
== 0) {
330 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
332 if (depth
<= 24 && stencil
== 0) {
333 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
334 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
336 if (depth
<= 24 && stencil
<= 8) {
337 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
338 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
340 if (depth
<= 32 && stencil
== 0) {
341 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
344 fmt
= PIPE_FORMAT_NONE
;
345 for (i
= 0; i
< count
; i
++) {
346 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
347 target
, tex_usage
, geom_flags
)) {
358 /**********************************************************************/
359 /***** Linked list of XMesaBuffers *****/
360 /**********************************************************************/
362 static XMesaBuffer XMesaBufferList
= NULL
;
366 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
367 * Note that XMesaBuffer is derived from GLframebuffer.
368 * The new XMesaBuffer will not have any size (Width=Height=0).
370 * \param d the corresponding X drawable (window or pixmap)
371 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
372 * \param vis the buffer's visual
373 * \param cmap the window's colormap, if known.
374 * \return new XMesaBuffer or NULL if any problem
377 create_xmesa_buffer(Drawable d
, BufferType type
,
378 XMesaVisual vis
, Colormap cmap
)
380 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
384 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
389 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
394 b
->ws
.visual
= vis
->visinfo
->visual
;
395 b
->ws
.depth
= vis
->visinfo
->depth
;
401 get_drawable_size(vis
->display
, d
, &width
, &height
);
404 * Create framebuffer, but we'll plug in our own renderbuffers below.
406 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
408 /* GLX_EXT_texture_from_pixmap */
409 b
->TextureTarget
= 0;
410 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
411 b
->TextureMipmap
= 0;
413 /* insert buffer into linked list */
414 b
->Next
= XMesaBufferList
;
422 * Find an XMesaBuffer by matching X display and colormap but NOT matching
423 * the notThis buffer.
426 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
429 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
430 if (b
->xm_visual
->display
== dpy
&&
441 * Remove buffer from linked list, delete if no longer referenced.
444 xmesa_free_buffer(XMesaBuffer buffer
)
446 XMesaBuffer prev
= NULL
, b
;
448 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
450 /* unlink buffer from list */
452 prev
->Next
= buffer
->Next
;
454 XMesaBufferList
= buffer
->Next
;
456 /* Since the X window for the XMesaBuffer is going away, we don't
457 * want to dereference this pointer in the future.
461 /* XXX we should move the buffer to a delete-pending list and destroy
462 * the buffer until it is no longer current.
464 xmesa_destroy_st_framebuffer(buffer
->stfb
);
470 /* continue search */
473 /* buffer not found in XMesaBufferList */
474 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
479 /**********************************************************************/
480 /***** Misc Private Functions *****/
481 /**********************************************************************/
485 * When a context is bound for the first time, we can finally finish
486 * initializing the context's visual and buffer information.
487 * \param v the XMesaVisual to initialize
488 * \param b the XMesaBuffer to initialize (may be NULL)
489 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
490 * \param window the window/pixmap we're rendering into
491 * \param cmap the colormap associated with the window/pixmap
492 * \return GL_TRUE=success, GL_FALSE=failure
495 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
496 GLboolean rgb_flag
, Drawable window
,
499 ASSERT(!b
|| b
->xm_visual
== v
);
501 /* Save true bits/pixel */
502 v
->BitsPerPixel
= bits_per_pixel(v
);
503 assert(v
->BitsPerPixel
> 0);
505 if (rgb_flag
== GL_FALSE
) {
506 /* COLOR-INDEXED WINDOW: not supported*/
511 * We support RGB rendering into almost any kind of visual.
513 const int xclass
= v
->mesa_visual
.visualType
;
514 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
516 "XMesa: RGB mode rendering not supported in given visual.\n");
519 v
->mesa_visual
.indexBits
= 0;
521 if (v
->BitsPerPixel
== 32) {
522 /* We use XImages for all front/back buffers. If an X Window or
523 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
524 * will be preserved. For XImages we're in luck.
526 v
->mesa_visual
.alphaBits
= 8;
531 * If MESA_INFO env var is set print out some debugging info
532 * which can help Brian figure out what's going on when a user
535 if (_mesa_getenv("MESA_INFO")) {
536 printf("X/Mesa visual = %p\n", (void *) v
);
537 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
538 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
539 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
547 #define NUM_VISUAL_TYPES 6
550 * Convert an X visual type to a GLX visual type.
552 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
554 * \return If \c visualType is a valid X visual type, a GLX visual type will
555 * be returned. Otherwise \c GLX_NONE will be returned.
558 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
562 xmesa_convert_from_x_visual_type( int visualType
)
564 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
565 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
566 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
567 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
570 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
571 ? glx_visual_types
[ visualType
] : GLX_NONE
;
575 /**********************************************************************/
576 /***** Public Functions *****/
577 /**********************************************************************/
581 * Create a new X/Mesa visual.
582 * Input: display - X11 display
583 * visinfo - an XVisualInfo pointer
584 * rgb_flag - GL_TRUE = RGB mode,
585 * GL_FALSE = color index mode
586 * alpha_flag - alpha buffer requested?
587 * db_flag - GL_TRUE = double-buffered,
588 * GL_FALSE = single buffered
589 * stereo_flag - stereo visual?
590 * ximage_flag - GL_TRUE = use an XImage for back buffer,
591 * GL_FALSE = use an off-screen pixmap for back buffer
592 * depth_size - requested bits/depth values, or zero
593 * stencil_size - requested bits/stencil values, or zero
594 * accum_red_size - requested bits/red accum values, or zero
595 * accum_green_size - requested bits/green accum values, or zero
596 * accum_blue_size - requested bits/blue accum values, or zero
597 * accum_alpha_size - requested bits/alpha accum values, or zero
598 * num_samples - number of samples/pixel if multisampling, or zero
599 * level - visual level, usually 0
600 * visualCaveat - ala the GLX extension, usually GLX_NONE
601 * Return; a new XMesaVisual or 0 if error.
604 XMesaVisual
XMesaCreateVisual( Display
*display
,
605 XVisualInfo
* visinfo
,
607 GLboolean alpha_flag
,
609 GLboolean stereo_flag
,
610 GLboolean ximage_flag
,
613 GLint accum_red_size
,
614 GLint accum_green_size
,
615 GLint accum_blue_size
,
616 GLint accum_alpha_size
,
621 XMesaDisplay xmdpy
= xmesa_init_display(display
);
623 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
628 /* For debugging only */
629 if (_mesa_getenv("MESA_XSYNC")) {
630 /* This makes debugging X easier.
631 * In your debugger, set a breakpoint on _XError to stop when an
632 * X protocol error is generated.
634 XSynchronize( display
, 1 );
637 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
642 v
->display
= display
;
644 /* Save a copy of the XVisualInfo struct because the user may Xfree()
645 * the struct but we may need some of the information contained in it
648 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
653 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
655 v
->ximage_flag
= ximage_flag
;
657 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
658 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
659 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
660 v
->mesa_visual
.visualID
= visinfo
->visualid
;
661 v
->mesa_visual
.screen
= visinfo
->screen
;
663 #if !(defined(__cplusplus) || defined(c_plusplus))
664 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
666 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
669 v
->mesa_visual
.visualRating
= visualCaveat
;
672 v
->mesa_visual
.alphaBits
= 8;
674 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
677 const int xclass
= v
->mesa_visual
.visualType
;
678 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
679 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
680 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
681 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
684 /* this is an approximation */
686 depth
= v
->visinfo
->depth
;
687 red_bits
= depth
/ 3;
689 green_bits
= depth
/ 2;
693 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
695 alpha_bits
= v
->mesa_visual
.alphaBits
;
698 _mesa_initialize_visual( &v
->mesa_visual
,
699 db_flag
, stereo_flag
,
700 red_bits
, green_bits
,
701 blue_bits
, alpha_bits
,
704 accum_red_size
, accum_green_size
,
705 accum_blue_size
, accum_alpha_size
,
708 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
710 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
712 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
714 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
717 v
->stvis
.color_format
= choose_pixel_format(v
);
718 v
->stvis
.depth_stencil_format
=
719 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
721 v
->stvis
.accum_format
= (accum_red_size
+
722 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
723 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
725 v
->stvis
.samples
= num_samples
;
726 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
729 v
->mesa_visual
.level
= level
;
735 void XMesaDestroyVisual( XMesaVisual v
)
743 * Do per-display initializations.
746 xmesa_init( Display
*display
)
748 xmesa_init_display(display
);
753 * Create a new XMesaContext.
754 * \param v the XMesaVisual
755 * \param share_list another XMesaContext with which to share display
756 * lists or NULL if no sharing is wanted.
757 * \return an XMesaContext or NULL if error.
760 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
762 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
768 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
769 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
774 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
775 c
->xm_read_buffer
= NULL
;
777 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
,
778 &v
->stvis
, (share_list
) ? share_list
->st
: NULL
);
782 c
->st
->st_manager_private
= (void *) c
;
788 c
->st
->destroy(c
->st
);
797 void XMesaDestroyContext( XMesaContext c
)
799 c
->st
->destroy(c
->st
);
801 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
802 * outlive it, causing segfaults
803 struct pipe_screen *screen = c->st->pipe->screen;
804 screen->destroy(screen);
813 * Private function for creating an XMesaBuffer which corresponds to an
814 * X window or pixmap.
815 * \param v the window's XMesaVisual
816 * \param w the window we're wrapping
817 * \return new XMesaBuffer or NULL if error
820 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
822 XWindowAttributes attr
;
830 /* Check that window depth matches visual depth */
831 XGetWindowAttributes( v
->display
, w
, &attr
);
833 if (v
->visinfo
->depth
!= depth
) {
834 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
835 v
->visinfo
->depth
, depth
);
841 cmap
= attr
.colormap
;
844 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
845 /* this is weird, a window w/out a colormap!? */
846 /* OK, let's just allocate a new one and hope for the best */
847 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
850 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
854 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
855 (Drawable
) w
, cmap
)) {
856 xmesa_free_buffer(b
);
866 * Create a new XMesaBuffer from an X pixmap.
868 * \param v the XMesaVisual
869 * \param p the pixmap
870 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
871 * \c GLX_DIRECT_COLOR visual for the pixmap
872 * \returns new XMesaBuffer or NULL if error
875 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
881 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
885 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
886 (Drawable
) p
, cmap
)) {
887 xmesa_free_buffer(b
);
896 * For GLX_EXT_texture_from_pixmap
899 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
901 int format
, int target
, int mipmap
)
903 GET_CURRENT_CONTEXT(ctx
);
908 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
912 /* get pixmap size */
913 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
917 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
918 target
= GLX_TEXTURE_2D_EXT
;
920 else if ( _mesa_bitcount(b
->width
) == 1
921 && _mesa_bitcount(b
->height
) == 1) {
922 /* power of two size */
923 if (b
->height
== 1) {
924 target
= GLX_TEXTURE_1D_EXT
;
927 target
= GLX_TEXTURE_2D_EXT
;
930 else if (ctx
->Extensions
.NV_texture_rectangle
) {
931 target
= GLX_TEXTURE_RECTANGLE_EXT
;
934 /* non power of two textures not supported */
935 XMesaDestroyBuffer(b
);
940 b
->TextureTarget
= target
;
941 b
->TextureFormat
= format
;
942 b
->TextureMipmap
= mipmap
;
944 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
945 (Drawable
) p
, cmap
)) {
946 xmesa_free_buffer(b
);
956 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
957 unsigned int width
, unsigned int height
)
960 Drawable drawable
; /* X Pixmap Drawable */
963 /* allocate pixmap for front buffer */
964 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
965 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
970 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
974 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
976 xmesa_free_buffer(b
);
986 * Deallocate an XMesaBuffer structure and all related info.
989 XMesaDestroyBuffer(XMesaBuffer b
)
991 xmesa_free_buffer(b
);
996 * Query the current drawable size and notify the binding context.
999 xmesa_check_buffer_size(XMesaBuffer b
)
1001 XMesaContext xmctx
= XMesaGetCurrentContext();
1003 if (b
->type
== PBUFFER
)
1006 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1007 if (xmctx
&& xmctx
->xm_buffer
== b
)
1008 xmctx
->st
->notify_invalid_framebuffer(xmctx
->st
, b
->stfb
);
1013 * Bind buffer b to context c and make c the current rendering context.
1016 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1017 XMesaBuffer readBuffer
)
1019 XMesaContext old_ctx
= XMesaGetCurrentContext();
1021 if (old_ctx
&& old_ctx
!= c
) {
1022 XMesaFlush(old_ctx
);
1023 old_ctx
->xm_buffer
= NULL
;
1024 old_ctx
->xm_read_buffer
= NULL
;
1028 if (!drawBuffer
|| !readBuffer
)
1029 return GL_FALSE
; /* must specify buffers! */
1032 c
->xm_buffer
== drawBuffer
&&
1033 c
->xm_read_buffer
== readBuffer
)
1036 xmesa_check_buffer_size(drawBuffer
);
1037 if (readBuffer
!= drawBuffer
)
1038 xmesa_check_buffer_size(readBuffer
);
1040 c
->xm_buffer
= drawBuffer
;
1041 c
->xm_read_buffer
= readBuffer
;
1043 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1045 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1046 drawBuffer
->wasCurrent
= GL_TRUE
;
1050 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1058 * Unbind the context c from its buffer.
1060 GLboolean
XMesaUnbindContext( XMesaContext c
)
1062 /* A no-op for XFree86 integration purposes */
1067 XMesaContext
XMesaGetCurrentContext( void )
1069 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1070 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1076 * Swap front and back color buffers and have winsys display front buffer.
1077 * If there's no front color buffer no swap actually occurs.
1080 void XMesaSwapBuffers( XMesaBuffer b
)
1082 XMesaContext xmctx
= XMesaGetCurrentContext();
1084 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1085 xmctx
->st
->flush( xmctx
->st
,
1086 PIPE_FLUSH_RENDER_CACHE
|
1087 PIPE_FLUSH_SWAPBUFFERS
|
1092 xmesa_swap_st_framebuffer(b
->stfb
);
1098 * Copy sub-region of back buffer to front buffer
1100 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1102 xmesa_copy_st_framebuffer(b
->stfb
,
1103 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1104 x
, y
, width
, height
);
1109 void XMesaFlush( XMesaContext c
)
1111 if (c
&& c
->xm_visual
->display
) {
1112 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1113 struct pipe_fence_handle
*fence
= NULL
;
1115 c
->st
->flush(c
->st
, PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
1117 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
, 0);
1118 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1120 XSync( c
->xm_visual
->display
, False
);
1128 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1131 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1132 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1141 * Free/destroy all XMesaBuffers associated with given display.
1143 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1145 XMesaBuffer b
, next
;
1146 for (b
= XMesaBufferList
; b
; b
= next
) {
1148 if (b
->xm_visual
->display
== dpy
) {
1149 xmesa_free_buffer(b
);
1156 * Look for XMesaBuffers whose X window has been destroyed.
1157 * Deallocate any such XMesaBuffers.
1159 void XMesaGarbageCollect( void )
1161 XMesaBuffer b
, next
;
1162 for (b
=XMesaBufferList
; b
; b
=next
) {
1165 b
->xm_visual
->display
&&
1167 b
->type
== WINDOW
) {
1168 XSync(b
->xm_visual
->display
, False
);
1169 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1170 /* found a dead window, free the ancillary info */
1171 XMesaDestroyBuffer( b
);
1181 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1182 const int *attrib_list
)
1189 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1195 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1198 dst
->st
->copy(dst
->st
, src
->st
, mask
);