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 * THE AUTHORS 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 "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_screen.h"
62 #include "pipe/p_state.h"
64 #include "util/u_atomic.h"
65 #include "util/u_inlines.h"
67 #include "hud/hud_context.h"
69 #include "xm_public.h"
73 /* Driver interface routines, set up by xlib backend on library
74 * _init(). These are global in the same way that function names are
77 static struct xm_driver driver
;
78 static struct st_api
*stapi
;
80 /* Default strict invalidate to false. This means we will not call
81 * XGetGeometry after every swapbuffers, which allows swapbuffers to
82 * remain asynchronous. For apps running at 100fps with synchronous
83 * swapping, a 10% boost is typical. For gears, I see closer to 20%
86 * Note that the work of copying data on swapbuffers doesn't disappear
87 * - this change just allows the X server to execute the PutImage
88 * asynchronously without us effectively blocked until its completion.
90 * This speeds up even llvmpipe's threaded rasterization as the
91 * swapbuffers operation was a large part of the serial component of
94 * The downside of this is correctness - applications which don't call
95 * glViewport on window resizes will get incorrect rendering. A
96 * better solution would be to have per-frame but asynchronous
97 * invalidation. Xcb almost looks as if it could provide this, but
98 * the API doesn't seem to quite be there.
100 boolean xmesa_strict_invalidate
= FALSE
;
102 void xmesa_set_driver( const struct xm_driver
*templ
)
105 stapi
= driver
.create_st_api();
107 xmesa_strict_invalidate
=
108 debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE
);
113 * XXX replace this with a linked list, or better yet, try to attach the
114 * gallium/mesa extra bits to the X Display object with XAddExtension().
116 #define MAX_DISPLAYS 10
117 static struct xmesa_display Displays
[MAX_DISPLAYS
];
118 static int NumDisplays
= 0;
121 xmesa_get_param(struct st_manager
*smapi
,
122 enum st_manager_param param
)
125 case ST_MANAGER_BROKEN_INVALIDATE
:
126 return !xmesa_strict_invalidate
;
133 xmesa_init_display( Display
*display
)
135 pipe_static_mutex(init_mutex
);
139 pipe_mutex_lock(init_mutex
);
141 /* Look for XMesaDisplay which corresponds to 'display' */
142 for (i
= 0; i
< NumDisplays
; i
++) {
143 if (Displays
[i
].display
== display
) {
145 pipe_mutex_unlock(init_mutex
);
150 /* Create new XMesaDisplay */
152 assert(NumDisplays
< MAX_DISPLAYS
);
153 xmdpy
= &Displays
[NumDisplays
];
156 if (!xmdpy
->display
&& display
) {
157 xmdpy
->display
= display
;
158 xmdpy
->screen
= driver
.create_pipe_screen(display
);
159 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
161 xmdpy
->smapi
->screen
= xmdpy
->screen
;
162 xmdpy
->smapi
->get_param
= xmesa_get_param
;
165 if (xmdpy
->screen
&& xmdpy
->smapi
) {
166 pipe_mutex_init(xmdpy
->mutex
);
170 xmdpy
->screen
->destroy(xmdpy
->screen
);
171 xmdpy
->screen
= NULL
;
176 xmdpy
->display
= NULL
;
179 if (!xmdpy
->display
|| xmdpy
->display
!= display
)
182 pipe_mutex_unlock(init_mutex
);
187 /**********************************************************************/
188 /***** X Utility Functions *****/
189 /**********************************************************************/
193 * Return the host's byte order as LSBFirst or MSBFirst ala X.
195 static int host_byte_order( void )
198 char *cptr
= (char *) &i
;
199 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
206 * Return the true number of bits per pixel for XImages.
207 * For example, if we request a 24-bit deep visual we may actually need/get
208 * 32bpp XImages. This function returns the appropriate bpp.
209 * Input: dpy - the X display
210 * visinfo - desribes the visual to be used for XImages
211 * Return: true number of bits per pixel for XImages
214 bits_per_pixel( XMesaVisual xmv
)
216 Display
*dpy
= xmv
->display
;
217 XVisualInfo
* visinfo
= xmv
->visinfo
;
220 /* Create a temporary XImage */
221 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
222 ZPixmap
, 0, /*format, offset*/
224 1, 1, /*width, height*/
229 /* grab the bits/pixel value */
230 bitsPerPixel
= img
->bits_per_pixel
;
231 /* free the XImage */
234 XDestroyImage( img
);
241 * Determine if a given X window ID is valid (window exists).
242 * Do this by calling XGetWindowAttributes() for the window and
243 * checking if we catch an X error.
244 * Input: dpy - the display
245 * win - the window to check for existance
246 * Return: GL_TRUE - window exists
247 * GL_FALSE - window doesn't exist
249 static GLboolean WindowExistsFlag
;
251 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
254 if (xerr
->error_code
== BadWindow
) {
255 WindowExistsFlag
= GL_FALSE
;
260 static GLboolean
window_exists( Display
*dpy
, Window win
)
262 XWindowAttributes wa
;
263 int (*old_handler
)( Display
*, XErrorEvent
* );
264 WindowExistsFlag
= GL_TRUE
;
265 old_handler
= XSetErrorHandler(window_exists_err_handler
);
266 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
267 XSetErrorHandler(old_handler
);
268 return WindowExistsFlag
;
272 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
277 unsigned int w
, h
, bw
, depth
;
278 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
286 * Return the size of the window (or pixmap) that corresponds to the
288 * \param width returns width in pixels
289 * \param height returns height in pixels
292 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
293 GLuint
*width
, GLuint
*height
)
295 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
298 pipe_mutex_lock(xmdpy
->mutex
);
299 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
300 pipe_mutex_unlock(xmdpy
->mutex
);
303 /* probably querying a window that's recently been destroyed */
304 _mesa_warning(NULL
, "XGetGeometry failed!\n");
305 *width
= *height
= 1;
309 #define GET_REDMASK(__v) __v->mesa_visual.redMask
310 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
311 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
315 * Choose the pixel format for the given visual.
316 * This will tell the gallium driver how to pack pixel data into
320 choose_pixel_format(XMesaVisual v
)
322 boolean native_byte_order
= (host_byte_order() ==
323 ImageByteOrder(v
->display
));
325 if ( GET_REDMASK(v
) == 0x0000ff
326 && GET_GREENMASK(v
) == 0x00ff00
327 && GET_BLUEMASK(v
) == 0xff0000
328 && v
->BitsPerPixel
== 32) {
329 if (native_byte_order
) {
330 /* no byteswapping needed */
331 return PIPE_FORMAT_R8G8B8A8_UNORM
;
334 return PIPE_FORMAT_A8B8G8R8_UNORM
;
337 else if ( GET_REDMASK(v
) == 0xff0000
338 && GET_GREENMASK(v
) == 0x00ff00
339 && GET_BLUEMASK(v
) == 0x0000ff
340 && v
->BitsPerPixel
== 32) {
341 if (native_byte_order
) {
342 /* no byteswapping needed */
343 return PIPE_FORMAT_B8G8R8A8_UNORM
;
346 return PIPE_FORMAT_A8R8G8B8_UNORM
;
349 else if ( GET_REDMASK(v
) == 0x0000ff00
350 && GET_GREENMASK(v
) == 0x00ff0000
351 && GET_BLUEMASK(v
) == 0xff000000
352 && v
->BitsPerPixel
== 32) {
353 if (native_byte_order
) {
354 /* no byteswapping needed */
355 return PIPE_FORMAT_A8R8G8B8_UNORM
;
358 return PIPE_FORMAT_B8G8R8A8_UNORM
;
361 else if ( GET_REDMASK(v
) == 0xf800
362 && GET_GREENMASK(v
) == 0x07e0
363 && GET_BLUEMASK(v
) == 0x001f
365 && v
->BitsPerPixel
== 16) {
367 return PIPE_FORMAT_B5G6R5_UNORM
;
370 return PIPE_FORMAT_NONE
;
375 * Choose a depth/stencil format that satisfies the given depth and
378 static enum pipe_format
379 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
381 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
382 const unsigned tex_usage
= PIPE_BIND_DEPTH_STENCIL
;
383 const unsigned sample_count
= 0;
384 enum pipe_format formats
[8], fmt
;
389 if (depth
<= 16 && stencil
== 0) {
390 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
392 if (depth
<= 24 && stencil
== 0) {
393 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
394 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
396 if (depth
<= 24 && stencil
<= 8) {
397 formats
[count
++] = PIPE_FORMAT_S8_UINT_Z24_UNORM
;
398 formats
[count
++] = PIPE_FORMAT_Z24_UNORM_S8_UINT
;
400 if (depth
<= 32 && stencil
== 0) {
401 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
404 fmt
= PIPE_FORMAT_NONE
;
405 for (i
= 0; i
< count
; i
++) {
406 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
407 target
, sample_count
,
419 /**********************************************************************/
420 /***** Linked list of XMesaBuffers *****/
421 /**********************************************************************/
423 static XMesaBuffer XMesaBufferList
= NULL
;
427 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
428 * Note that XMesaBuffer is derived from struct gl_framebuffer.
429 * The new XMesaBuffer will not have any size (Width=Height=0).
431 * \param d the corresponding X drawable (window or pixmap)
432 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
433 * \param vis the buffer's visual
434 * \param cmap the window's colormap, if known.
435 * \return new XMesaBuffer or NULL if any problem
438 create_xmesa_buffer(Drawable d
, BufferType type
,
439 XMesaVisual vis
, Colormap cmap
)
441 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
444 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
449 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
454 b
->ws
.visual
= vis
->visinfo
->visual
;
455 b
->ws
.depth
= vis
->visinfo
->depth
;
461 get_drawable_size(vis
->display
, d
, &b
->width
, &b
->height
);
464 * Create framebuffer, but we'll plug in our own renderbuffers below.
466 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
468 /* GLX_EXT_texture_from_pixmap */
469 b
->TextureTarget
= 0;
470 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
471 b
->TextureMipmap
= 0;
473 /* insert buffer into linked list */
474 b
->Next
= XMesaBufferList
;
482 * Find an XMesaBuffer by matching X display and colormap but NOT matching
483 * the notThis buffer.
486 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
489 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
490 if (b
->xm_visual
->display
== dpy
&&
501 * Remove buffer from linked list, delete if no longer referenced.
504 xmesa_free_buffer(XMesaBuffer buffer
)
506 XMesaBuffer prev
= NULL
, b
;
508 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
510 /* unlink buffer from list */
512 prev
->Next
= buffer
->Next
;
514 XMesaBufferList
= buffer
->Next
;
516 /* Since the X window for the XMesaBuffer is going away, we don't
517 * want to dereference this pointer in the future.
521 /* XXX we should move the buffer to a delete-pending list and destroy
522 * the buffer until it is no longer current.
524 xmesa_destroy_st_framebuffer(buffer
->stfb
);
530 /* continue search */
533 /* buffer not found in XMesaBufferList */
534 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
539 /**********************************************************************/
540 /***** Misc Private Functions *****/
541 /**********************************************************************/
545 * When a context is bound for the first time, we can finally finish
546 * initializing the context's visual and buffer information.
547 * \param v the XMesaVisual to initialize
548 * \param b the XMesaBuffer to initialize (may be NULL)
549 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
550 * \param window the window/pixmap we're rendering into
551 * \param cmap the colormap associated with the window/pixmap
552 * \return GL_TRUE=success, GL_FALSE=failure
555 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
556 GLboolean rgb_flag
, Drawable window
,
559 ASSERT(!b
|| b
->xm_visual
== v
);
561 /* Save true bits/pixel */
562 v
->BitsPerPixel
= bits_per_pixel(v
);
563 assert(v
->BitsPerPixel
> 0);
565 if (rgb_flag
== GL_FALSE
) {
566 /* COLOR-INDEXED WINDOW: not supported*/
571 * We support RGB rendering into almost any kind of visual.
573 const int xclass
= v
->visualType
;
574 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
576 "XMesa: RGB mode rendering not supported in given visual.\n");
579 v
->mesa_visual
.indexBits
= 0;
581 if (v
->BitsPerPixel
== 32) {
582 /* We use XImages for all front/back buffers. If an X Window or
583 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
584 * will be preserved. For XImages we're in luck.
586 v
->mesa_visual
.alphaBits
= 8;
591 * If MESA_INFO env var is set print out some debugging info
592 * which can help Brian figure out what's going on when a user
595 if (_mesa_getenv("MESA_INFO")) {
596 printf("X/Mesa visual = %p\n", (void *) v
);
597 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
598 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
599 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
607 #define NUM_VISUAL_TYPES 6
610 * Convert an X visual type to a GLX visual type.
612 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
614 * \return If \c visualType is a valid X visual type, a GLX visual type will
615 * be returned. Otherwise \c GLX_NONE will be returned.
618 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
622 xmesa_convert_from_x_visual_type( int visualType
)
624 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
625 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
626 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
627 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
630 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
631 ? glx_visual_types
[ visualType
] : GLX_NONE
;
635 /**********************************************************************/
636 /***** Public Functions *****/
637 /**********************************************************************/
641 * Create a new X/Mesa visual.
642 * Input: display - X11 display
643 * visinfo - an XVisualInfo pointer
644 * rgb_flag - GL_TRUE = RGB mode,
645 * GL_FALSE = color index mode
646 * alpha_flag - alpha buffer requested?
647 * db_flag - GL_TRUE = double-buffered,
648 * GL_FALSE = single buffered
649 * stereo_flag - stereo visual?
650 * ximage_flag - GL_TRUE = use an XImage for back buffer,
651 * GL_FALSE = use an off-screen pixmap for back buffer
652 * depth_size - requested bits/depth values, or zero
653 * stencil_size - requested bits/stencil values, or zero
654 * accum_red_size - requested bits/red accum values, or zero
655 * accum_green_size - requested bits/green accum values, or zero
656 * accum_blue_size - requested bits/blue accum values, or zero
657 * accum_alpha_size - requested bits/alpha accum values, or zero
658 * num_samples - number of samples/pixel if multisampling, or zero
659 * level - visual level, usually 0
660 * visualCaveat - ala the GLX extension, usually GLX_NONE
661 * Return; a new XMesaVisual or 0 if error.
664 XMesaVisual
XMesaCreateVisual( Display
*display
,
665 XVisualInfo
* visinfo
,
667 GLboolean alpha_flag
,
669 GLboolean stereo_flag
,
670 GLboolean ximage_flag
,
673 GLint accum_red_size
,
674 GLint accum_green_size
,
675 GLint accum_blue_size
,
676 GLint accum_alpha_size
,
681 XMesaDisplay xmdpy
= xmesa_init_display(display
);
683 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
688 /* For debugging only */
689 if (_mesa_getenv("MESA_XSYNC")) {
690 /* This makes debugging X easier.
691 * In your debugger, set a breakpoint on _XError to stop when an
692 * X protocol error is generated.
694 XSynchronize( display
, 1 );
697 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
702 v
->display
= display
;
704 /* Save a copy of the XVisualInfo struct because the user may Xfree()
705 * the struct but we may need some of the information contained in it
708 v
->visinfo
= (XVisualInfo
*) malloc(sizeof(*visinfo
));
713 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
715 v
->ximage_flag
= ximage_flag
;
717 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
718 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
719 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
720 v
->visualID
= visinfo
->visualid
;
721 v
->screen
= visinfo
->screen
;
723 #if !(defined(__cplusplus) || defined(c_plusplus))
724 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
726 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
729 v
->mesa_visual
.visualRating
= visualCaveat
;
732 v
->mesa_visual
.alphaBits
= 8;
734 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
737 const int xclass
= v
->visualType
;
738 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
739 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
740 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
741 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
744 /* this is an approximation */
746 depth
= v
->visinfo
->depth
;
747 red_bits
= depth
/ 3;
749 green_bits
= depth
/ 2;
753 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
755 alpha_bits
= v
->mesa_visual
.alphaBits
;
758 /* initialize visual */
760 struct gl_config
*vis
= &v
->mesa_visual
;
762 vis
->rgbMode
= GL_TRUE
;
763 vis
->doubleBufferMode
= db_flag
;
764 vis
->stereoMode
= stereo_flag
;
766 vis
->redBits
= red_bits
;
767 vis
->greenBits
= green_bits
;
768 vis
->blueBits
= blue_bits
;
769 vis
->alphaBits
= alpha_bits
;
770 vis
->rgbBits
= red_bits
+ green_bits
+ blue_bits
;
773 vis
->depthBits
= depth_size
;
774 vis
->stencilBits
= stencil_size
;
776 vis
->accumRedBits
= accum_red_size
;
777 vis
->accumGreenBits
= accum_green_size
;
778 vis
->accumBlueBits
= accum_blue_size
;
779 vis
->accumAlphaBits
= accum_alpha_size
;
781 vis
->haveAccumBuffer
= accum_red_size
> 0;
782 vis
->haveDepthBuffer
= depth_size
> 0;
783 vis
->haveStencilBuffer
= stencil_size
> 0;
785 vis
->numAuxBuffers
= 0;
787 vis
->sampleBuffers
= 0;
791 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
793 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
795 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
797 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
800 v
->stvis
.color_format
= choose_pixel_format(v
);
801 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
807 v
->stvis
.depth_stencil_format
=
808 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
810 v
->stvis
.accum_format
= (accum_red_size
+
811 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
812 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
814 v
->stvis
.samples
= num_samples
;
815 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
818 v
->mesa_visual
.level
= level
;
824 void XMesaDestroyVisual( XMesaVisual v
)
832 * Return the informative name.
842 * Do per-display initializations.
845 xmesa_init( Display
*display
)
847 xmesa_init_display(display
);
852 * Create a new XMesaContext.
853 * \param v the XMesaVisual
854 * \param share_list another XMesaContext with which to share display
855 * lists or NULL if no sharing is wanted.
856 * \return an XMesaContext or NULL if error.
859 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
,
860 GLuint major
, GLuint minor
,
861 GLuint profileMask
, GLuint contextFlags
)
863 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
864 struct st_context_attribs attribs
;
865 enum st_context_error ctx_err
= 0;
871 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
872 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
877 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
878 c
->xm_read_buffer
= NULL
;
880 memset(&attribs
, 0, sizeof(attribs
));
881 attribs
.visual
= v
->stvis
;
882 attribs
.major
= major
;
883 attribs
.minor
= minor
;
884 if (contextFlags
& GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
885 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
886 if (contextFlags
& GLX_CONTEXT_DEBUG_BIT_ARB
)
887 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
888 if (contextFlags
& GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB
)
889 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
891 /* There are no profiles before OpenGL 3.2. The
892 * GLX_ARB_create_context_profile spec says:
894 * "If the requested OpenGL version is less than 3.2,
895 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality of the
896 * context is determined solely by the requested version."
898 * The spec also says:
900 * "The default value for GLX_CONTEXT_PROFILE_MASK_ARB is
901 * GLX_CONTEXT_CORE_PROFILE_BIT_ARB."
903 attribs
.profile
= ST_PROFILE_DEFAULT
;
904 if ((major
> 3 || (major
== 3 && minor
>= 2))
905 && ((profileMask
& GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
) == 0))
906 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
908 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
, &attribs
,
909 &ctx_err
, (share_list
) ? share_list
->st
: NULL
);
913 c
->st
->st_manager_private
= (void *) c
;
915 c
->hud
= hud_create(c
->st
->pipe
, c
->st
->cso_context
);
921 c
->st
->destroy(c
->st
);
930 void XMesaDestroyContext( XMesaContext c
)
936 c
->st
->destroy(c
->st
);
938 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
939 * outlive it, causing segfaults
940 struct pipe_screen *screen = c->st->pipe->screen;
941 screen->destroy(screen);
950 * Private function for creating an XMesaBuffer which corresponds to an
951 * X window or pixmap.
952 * \param v the window's XMesaVisual
953 * \param w the window we're wrapping
954 * \return new XMesaBuffer or NULL if error
957 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
959 XWindowAttributes attr
;
967 /* Check that window depth matches visual depth */
968 XGetWindowAttributes( v
->display
, w
, &attr
);
970 if (v
->visinfo
->depth
!= depth
) {
971 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
972 v
->visinfo
->depth
, depth
);
978 cmap
= attr
.colormap
;
981 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
982 /* this is weird, a window w/out a colormap!? */
983 /* OK, let's just allocate a new one and hope for the best */
984 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
987 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
991 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
992 (Drawable
) w
, cmap
)) {
993 xmesa_free_buffer(b
);
1003 * Create a new XMesaBuffer from an X pixmap.
1005 * \param v the XMesaVisual
1006 * \param p the pixmap
1007 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1008 * \c GLX_DIRECT_COLOR visual for the pixmap
1009 * \returns new XMesaBuffer or NULL if error
1012 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
1018 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1022 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1023 (Drawable
) p
, cmap
)) {
1024 xmesa_free_buffer(b
);
1033 * For GLX_EXT_texture_from_pixmap
1036 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
1038 int format
, int target
, int mipmap
)
1040 GET_CURRENT_CONTEXT(ctx
);
1045 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1049 /* get pixmap size */
1050 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
1054 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1055 target
= GLX_TEXTURE_2D_EXT
;
1057 else if ( _mesa_bitcount(b
->width
) == 1
1058 && _mesa_bitcount(b
->height
) == 1) {
1059 /* power of two size */
1060 if (b
->height
== 1) {
1061 target
= GLX_TEXTURE_1D_EXT
;
1064 target
= GLX_TEXTURE_2D_EXT
;
1067 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1068 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1071 /* non power of two textures not supported */
1072 XMesaDestroyBuffer(b
);
1077 b
->TextureTarget
= target
;
1078 b
->TextureFormat
= format
;
1079 b
->TextureMipmap
= mipmap
;
1081 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1082 (Drawable
) p
, cmap
)) {
1083 xmesa_free_buffer(b
);
1093 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1094 unsigned int width
, unsigned int height
)
1097 Drawable drawable
; /* X Pixmap Drawable */
1100 /* allocate pixmap for front buffer */
1101 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1102 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1107 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1111 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1113 xmesa_free_buffer(b
);
1123 * Deallocate an XMesaBuffer structure and all related info.
1126 XMesaDestroyBuffer(XMesaBuffer b
)
1128 xmesa_free_buffer(b
);
1133 * Notify the binding context to validate the buffer.
1136 xmesa_notify_invalid_buffer(XMesaBuffer b
)
1138 p_atomic_inc(&b
->stfb
->stamp
);
1143 * Query the current drawable size and notify the binding context.
1146 xmesa_check_buffer_size(XMesaBuffer b
)
1148 GLuint old_width
, old_height
;
1150 if (b
->type
== PBUFFER
)
1153 old_width
= b
->width
;
1154 old_height
= b
->height
;
1156 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1158 if (b
->width
!= old_width
|| b
->height
!= old_height
)
1159 xmesa_notify_invalid_buffer(b
);
1164 * Bind buffer b to context c and make c the current rendering context.
1167 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1168 XMesaBuffer readBuffer
)
1170 XMesaContext old_ctx
= XMesaGetCurrentContext();
1172 if (old_ctx
&& old_ctx
!= c
) {
1173 XMesaFlush(old_ctx
);
1174 old_ctx
->xm_buffer
= NULL
;
1175 old_ctx
->xm_read_buffer
= NULL
;
1179 if (!drawBuffer
|| !readBuffer
)
1180 return GL_FALSE
; /* must specify buffers! */
1183 c
->xm_buffer
== drawBuffer
&&
1184 c
->xm_read_buffer
== readBuffer
)
1187 xmesa_check_buffer_size(drawBuffer
);
1188 if (readBuffer
!= drawBuffer
)
1189 xmesa_check_buffer_size(readBuffer
);
1191 c
->xm_buffer
= drawBuffer
;
1192 c
->xm_read_buffer
= readBuffer
;
1194 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1196 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1197 drawBuffer
->wasCurrent
= GL_TRUE
;
1201 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1209 * Unbind the context c from its buffer.
1211 GLboolean
XMesaUnbindContext( XMesaContext c
)
1213 /* A no-op for XFree86 integration purposes */
1218 XMesaContext
XMesaGetCurrentContext( void )
1220 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1221 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1227 * Swap front and back color buffers and have winsys display front buffer.
1228 * If there's no front color buffer no swap actually occurs.
1231 void XMesaSwapBuffers( XMesaBuffer b
)
1233 XMesaContext xmctx
= XMesaGetCurrentContext();
1235 /* Need to draw HUD before flushing */
1236 if (xmctx
&& xmctx
->hud
) {
1237 struct pipe_resource
*back
=
1238 xmesa_get_framebuffer_resource(b
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
1239 hud_draw(xmctx
->hud
, back
);
1242 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1243 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
);
1246 xmesa_swap_st_framebuffer(b
->stfb
);
1252 * Copy sub-region of back buffer to front buffer
1254 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1256 xmesa_copy_st_framebuffer(b
->stfb
,
1257 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1258 x
, y
, width
, height
);
1263 void XMesaFlush( XMesaContext c
)
1265 if (c
&& c
->xm_visual
->display
) {
1266 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1267 struct pipe_fence_handle
*fence
= NULL
;
1269 c
->st
->flush(c
->st
, ST_FLUSH_FRONT
, &fence
);
1271 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
,
1272 PIPE_TIMEOUT_INFINITE
);
1273 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1275 XFlush( c
->xm_visual
->display
);
1283 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1286 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1287 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1296 * Free/destroy all XMesaBuffers associated with given display.
1298 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1300 XMesaBuffer b
, next
;
1301 for (b
= XMesaBufferList
; b
; b
= next
) {
1303 if (b
->xm_visual
->display
== dpy
) {
1304 xmesa_free_buffer(b
);
1305 /* delete head of list? */
1306 if (XMesaBufferList
== b
) {
1307 XMesaBufferList
= next
;
1315 * Look for XMesaBuffers whose X window has been destroyed.
1316 * Deallocate any such XMesaBuffers.
1318 void XMesaGarbageCollect( void )
1320 XMesaBuffer b
, next
;
1321 for (b
=XMesaBufferList
; b
; b
=next
) {
1324 b
->xm_visual
->display
&&
1326 b
->type
== WINDOW
) {
1327 XSync(b
->xm_visual
->display
, False
);
1328 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1329 /* found a dead window, free the ancillary info */
1330 XMesaDestroyBuffer( b
);
1337 static enum st_attachment_type
xmesa_attachment_type(int glx_attachment
)
1339 switch(glx_attachment
) {
1340 case GLX_FRONT_LEFT_EXT
:
1341 return ST_ATTACHMENT_FRONT_LEFT
;
1342 case GLX_FRONT_RIGHT_EXT
:
1343 return ST_ATTACHMENT_FRONT_RIGHT
;
1344 case GLX_BACK_LEFT_EXT
:
1345 return ST_ATTACHMENT_BACK_LEFT
;
1346 case GLX_BACK_RIGHT_EXT
:
1347 return ST_ATTACHMENT_BACK_RIGHT
;
1350 return ST_ATTACHMENT_FRONT_LEFT
;
1356 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1357 const int *attrib_list
)
1359 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1360 struct st_framebuffer_iface
* stfbi
= drawable
->stfb
;
1361 struct pipe_resource
*res
;
1363 enum st_attachment_type st_attachment
= xmesa_attachment_type(buffer
);
1367 w
= drawable
->width
;
1368 h
= drawable
->height
;
1370 /* We need to validate our attachments before using them,
1371 * in case the texture doesn't exist yet. */
1372 xmesa_st_framebuffer_validate_textures(stfbi
, w
, h
, 1 << st_attachment
);
1373 res
= xmesa_get_attachment(stfbi
, st_attachment
);
1376 struct pipe_context
* pipe
= xmesa_get_context(stfbi
);
1377 enum pipe_format internal_format
= res
->format
;
1378 struct pipe_transfer
*tex_xfer
;
1380 int line
, ximage_stride
;
1383 internal_format
= choose_pixel_format(drawable
->xm_visual
);
1385 map
= pipe_transfer_map(pipe
, res
,
1386 0, 0, /* level, layer */
1387 PIPE_TRANSFER_WRITE
,
1393 /* Grab the XImage that we want to turn into a texture. */
1394 img
= XGetImage(dpy
,
1395 drawable
->ws
.drawable
,
1402 pipe_transfer_unmap(pipe
, tex_xfer
);
1406 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
1407 We assume 32 bit pixels. */
1408 ximage_stride
= w
* 4;
1410 for (line
= 0; line
< h
; line
++)
1411 memcpy(&map
[line
* tex_xfer
->stride
],
1412 &img
->data
[line
* ximage_stride
],
1415 pipe_transfer_unmap(pipe
, tex_xfer
);
1422 FALSE
/* no mipmap */);
1430 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1436 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1439 dst
->st
->copy(dst
->st
, src
->st
, mask
);