2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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.
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "pipe/p_screen.h"
63 #include "pipe/p_state.h"
65 #include "util/u_atomic.h"
66 #include "util/u_inlines.h"
68 #include "hud/hud_context.h"
70 #include "xm_public.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
;
79 static struct st_api
*stapi
;
81 /* Default strict invalidate to false. This means we will not call
82 * XGetGeometry after every swapbuffers, which allows swapbuffers to
83 * remain asynchronous. For apps running at 100fps with synchronous
84 * swapping, a 10% boost is typical. For gears, I see closer to 20%
87 * Note that the work of copying data on swapbuffers doesn't disappear
88 * - this change just allows the X server to execute the PutImage
89 * asynchronously without us effectively blocked until its completion.
91 * This speeds up even llvmpipe's threaded rasterization as the
92 * swapbuffers operation was a large part of the serial component of
95 * The downside of this is correctness - applications which don't call
96 * glViewport on window resizes will get incorrect rendering. A
97 * better solution would be to have per-frame but asynchronous
98 * invalidation. Xcb almost looks as if it could provide this, but
99 * the API doesn't seem to quite be there.
101 boolean xmesa_strict_invalidate
= FALSE
;
103 void xmesa_set_driver( const struct xm_driver
*templ
)
106 stapi
= driver
.create_st_api();
108 xmesa_strict_invalidate
=
109 debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE
);
114 xmesa_get_param(struct st_manager
*smapi
,
115 enum st_manager_param param
)
118 case ST_MANAGER_BROKEN_INVALIDATE
:
119 return !xmesa_strict_invalidate
;
125 /* linked list of XMesaDisplay hooks per display */
126 typedef struct _XMesaExtDisplayInfo
{
127 struct _XMesaExtDisplayInfo
*next
;
129 struct xmesa_display mesaDisplay
;
130 } XMesaExtDisplayInfo
;
132 typedef struct _XMesaExtInfo
{
133 XMesaExtDisplayInfo
*head
;
137 static XMesaExtInfo MesaExtInfo
;
139 /* hook to delete XMesaDisplay on XDestroyDisplay */
141 xmesa_close_display(Display
*display
)
143 XMesaExtDisplayInfo
*info
, *prev
;
145 assert(MesaExtInfo
.ndisplays
> 0);
146 assert(MesaExtInfo
.head
);
148 _XLockMutex(_Xglobal_lock
);
149 /* first find display */
151 for (info
= MesaExtInfo
.head
; info
; info
= info
->next
) {
152 if (info
->display
== display
) {
159 /* no display found */
160 _XUnlockMutex(_Xglobal_lock
);
164 /* remove display entry from list */
165 if (prev
!= MesaExtInfo
.head
) {
166 prev
->next
= info
->next
;
168 MesaExtInfo
.head
= info
->next
;
170 MesaExtInfo
.ndisplays
--;
172 _XUnlockMutex(_Xglobal_lock
);
174 /* don't forget to clean up mesaDisplay */
175 XMesaDisplay xmdpy
= &info
->mesaDisplay
;
178 * XXX: Don't destroy the screens here, since there may still
179 * be some dangling screen pointers that are used after this point
180 * if (xmdpy->screen) {
181 * xmdpy->screen->destroy(xmdpy->screen);
186 XFree((char *) info
);
190 xmesa_init_display( Display
*display
)
192 pipe_static_mutex(init_mutex
);
194 XMesaExtDisplayInfo
*info
;
196 if (display
== NULL
) {
200 pipe_mutex_lock(init_mutex
);
202 /* Look for XMesaDisplay which corresponds to this display */
203 info
= MesaExtInfo
.head
;
205 if (info
->display
== display
) {
207 pipe_mutex_unlock(init_mutex
);
208 return &info
->mesaDisplay
;
213 /* Not found. Create new XMesaDisplay */
214 /* first allocate X-related resources and hook destroy callback */
216 /* allocate mesa display info */
217 info
= (XMesaExtDisplayInfo
*) Xmalloc(sizeof(XMesaExtDisplayInfo
));
219 pipe_mutex_unlock(init_mutex
);
222 info
->display
= display
;
223 xmdpy
= &info
->mesaDisplay
; /* to be filled out below */
225 /* chain to the list of displays */
226 _XLockMutex(_Xglobal_lock
);
227 info
->next
= MesaExtInfo
.head
;
228 MesaExtInfo
.head
= info
;
229 MesaExtInfo
.ndisplays
++;
230 _XUnlockMutex(_Xglobal_lock
);
232 /* now create the new XMesaDisplay info */
235 xmdpy
->display
= display
;
236 xmdpy
->screen
= driver
.create_pipe_screen(display
);
237 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
240 xmdpy
->smapi
->screen
= xmdpy
->screen
;
241 xmdpy
->smapi
->get_param
= xmesa_get_param
;
244 if (xmdpy
->screen
&& xmdpy
->smapi
) {
245 pipe_mutex_init(xmdpy
->mutex
);
249 xmdpy
->screen
->destroy(xmdpy
->screen
);
250 xmdpy
->screen
= NULL
;
255 xmdpy
->display
= NULL
;
258 pipe_mutex_unlock(init_mutex
);
264 /**********************************************************************/
265 /***** X Utility Functions *****/
266 /**********************************************************************/
270 * Return the host's byte order as LSBFirst or MSBFirst ala X.
272 static int host_byte_order( void )
275 char *cptr
= (char *) &i
;
276 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
283 * Return the true number of bits per pixel for XImages.
284 * For example, if we request a 24-bit deep visual we may actually need/get
285 * 32bpp XImages. This function returns the appropriate bpp.
286 * Input: dpy - the X display
287 * visinfo - desribes the visual to be used for XImages
288 * Return: true number of bits per pixel for XImages
291 bits_per_pixel( XMesaVisual xmv
)
293 Display
*dpy
= xmv
->display
;
294 XVisualInfo
* visinfo
= xmv
->visinfo
;
297 /* Create a temporary XImage */
298 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
299 ZPixmap
, 0, /*format, offset*/
301 1, 1, /*width, height*/
306 /* grab the bits/pixel value */
307 bitsPerPixel
= img
->bits_per_pixel
;
308 /* free the XImage */
311 XDestroyImage( img
);
318 * Determine if a given X window ID is valid (window exists).
319 * Do this by calling XGetWindowAttributes() for the window and
320 * checking if we catch an X error.
321 * Input: dpy - the display
322 * win - the window to check for existence
323 * Return: GL_TRUE - window exists
324 * GL_FALSE - window doesn't exist
326 static GLboolean WindowExistsFlag
;
328 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
331 if (xerr
->error_code
== BadWindow
) {
332 WindowExistsFlag
= GL_FALSE
;
337 static GLboolean
window_exists( Display
*dpy
, Window win
)
339 XWindowAttributes wa
;
340 int (*old_handler
)( Display
*, XErrorEvent
* );
341 WindowExistsFlag
= GL_TRUE
;
342 old_handler
= XSetErrorHandler(window_exists_err_handler
);
343 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
344 XSetErrorHandler(old_handler
);
345 return WindowExistsFlag
;
349 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
354 unsigned int w
, h
, bw
, depth
;
355 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
363 * Return the size of the window (or pixmap) that corresponds to the
365 * \param width returns width in pixels
366 * \param height returns height in pixels
369 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
370 GLuint
*width
, GLuint
*height
)
372 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
375 pipe_mutex_lock(xmdpy
->mutex
);
376 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
377 pipe_mutex_unlock(xmdpy
->mutex
);
380 /* probably querying a window that's recently been destroyed */
381 _mesa_warning(NULL
, "XGetGeometry failed!\n");
382 *width
= *height
= 1;
386 #define GET_REDMASK(__v) __v->mesa_visual.redMask
387 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
388 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
392 * Choose the pixel format for the given visual.
393 * This will tell the gallium driver how to pack pixel data into
397 choose_pixel_format(XMesaVisual v
)
399 boolean native_byte_order
= (host_byte_order() ==
400 ImageByteOrder(v
->display
));
402 if ( GET_REDMASK(v
) == 0x0000ff
403 && GET_GREENMASK(v
) == 0x00ff00
404 && GET_BLUEMASK(v
) == 0xff0000
405 && v
->BitsPerPixel
== 32) {
406 if (native_byte_order
) {
407 /* no byteswapping needed */
408 return PIPE_FORMAT_RGBA8888_UNORM
;
411 return PIPE_FORMAT_ABGR8888_UNORM
;
414 else if ( GET_REDMASK(v
) == 0xff0000
415 && GET_GREENMASK(v
) == 0x00ff00
416 && GET_BLUEMASK(v
) == 0x0000ff
417 && v
->BitsPerPixel
== 32) {
418 if (native_byte_order
) {
419 /* no byteswapping needed */
420 return PIPE_FORMAT_BGRA8888_UNORM
;
423 return PIPE_FORMAT_ARGB8888_UNORM
;
426 else if ( GET_REDMASK(v
) == 0x0000ff00
427 && GET_GREENMASK(v
) == 0x00ff0000
428 && GET_BLUEMASK(v
) == 0xff000000
429 && v
->BitsPerPixel
== 32) {
430 if (native_byte_order
) {
431 /* no byteswapping needed */
432 return PIPE_FORMAT_ARGB8888_UNORM
;
435 return PIPE_FORMAT_BGRA8888_UNORM
;
438 else if ( GET_REDMASK(v
) == 0xf800
439 && GET_GREENMASK(v
) == 0x07e0
440 && GET_BLUEMASK(v
) == 0x001f
442 && v
->BitsPerPixel
== 16) {
444 return PIPE_FORMAT_B5G6R5_UNORM
;
447 return PIPE_FORMAT_NONE
;
452 * Choose a depth/stencil format that satisfies the given depth and
455 static enum pipe_format
456 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
)
458 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
459 const unsigned tex_usage
= PIPE_BIND_DEPTH_STENCIL
;
460 const unsigned sample_count
= 0;
461 enum pipe_format formats
[8], fmt
;
466 if (depth
<= 16 && stencil
== 0) {
467 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
469 if (depth
<= 24 && stencil
== 0) {
470 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
471 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
473 if (depth
<= 24 && stencil
<= 8) {
474 formats
[count
++] = PIPE_FORMAT_S8_UINT_Z24_UNORM
;
475 formats
[count
++] = PIPE_FORMAT_Z24_UNORM_S8_UINT
;
477 if (depth
<= 32 && stencil
== 0) {
478 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
481 fmt
= PIPE_FORMAT_NONE
;
482 for (i
= 0; i
< count
; i
++) {
483 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
484 target
, sample_count
,
496 /**********************************************************************/
497 /***** Linked list of XMesaBuffers *****/
498 /**********************************************************************/
500 static XMesaBuffer XMesaBufferList
= NULL
;
504 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
505 * Note that XMesaBuffer is derived from struct gl_framebuffer.
506 * The new XMesaBuffer will not have any size (Width=Height=0).
508 * \param d the corresponding X drawable (window or pixmap)
509 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
510 * \param vis the buffer's visual
511 * \param cmap the window's colormap, if known.
512 * \return new XMesaBuffer or NULL if any problem
515 create_xmesa_buffer(Drawable d
, BufferType type
,
516 XMesaVisual vis
, Colormap cmap
)
518 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
521 assert(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
526 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
531 b
->ws
.visual
= vis
->visinfo
->visual
;
532 b
->ws
.depth
= vis
->visinfo
->depth
;
538 get_drawable_size(vis
->display
, d
, &b
->width
, &b
->height
);
541 * Create framebuffer, but we'll plug in our own renderbuffers below.
543 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
545 /* GLX_EXT_texture_from_pixmap */
546 b
->TextureTarget
= 0;
547 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
548 b
->TextureMipmap
= 0;
550 /* insert buffer into linked list */
551 b
->Next
= XMesaBufferList
;
559 * Find an XMesaBuffer by matching X display and colormap but NOT matching
560 * the notThis buffer.
563 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
566 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
567 if (b
->xm_visual
->display
== dpy
&&
578 * Remove buffer from linked list, delete if no longer referenced.
581 xmesa_free_buffer(XMesaBuffer buffer
)
583 XMesaBuffer prev
= NULL
, b
;
585 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
587 /* unlink buffer from list */
589 prev
->Next
= buffer
->Next
;
591 XMesaBufferList
= buffer
->Next
;
593 /* Since the X window for the XMesaBuffer is going away, we don't
594 * want to dereference this pointer in the future.
598 /* XXX we should move the buffer to a delete-pending list and destroy
599 * the buffer until it is no longer current.
601 xmesa_destroy_st_framebuffer(buffer
->stfb
);
607 /* continue search */
610 /* buffer not found in XMesaBufferList */
611 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
616 /**********************************************************************/
617 /***** Misc Private Functions *****/
618 /**********************************************************************/
622 * When a context is bound for the first time, we can finally finish
623 * initializing the context's visual and buffer information.
624 * \param v the XMesaVisual to initialize
625 * \param b the XMesaBuffer to initialize (may be NULL)
626 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
627 * \param window the window/pixmap we're rendering into
628 * \param cmap the colormap associated with the window/pixmap
629 * \return GL_TRUE=success, GL_FALSE=failure
632 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
633 GLboolean rgb_flag
, Drawable window
,
636 assert(!b
|| b
->xm_visual
== v
);
638 /* Save true bits/pixel */
639 v
->BitsPerPixel
= bits_per_pixel(v
);
640 assert(v
->BitsPerPixel
> 0);
642 if (rgb_flag
== GL_FALSE
) {
643 /* COLOR-INDEXED WINDOW: not supported*/
648 * We support RGB rendering into almost any kind of visual.
650 const int xclass
= v
->visualType
;
651 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
653 "XMesa: RGB mode rendering not supported in given visual.\n");
656 v
->mesa_visual
.indexBits
= 0;
658 if (v
->BitsPerPixel
== 32) {
659 /* We use XImages for all front/back buffers. If an X Window or
660 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
661 * will be preserved. For XImages we're in luck.
663 v
->mesa_visual
.alphaBits
= 8;
668 * If MESA_INFO env var is set print out some debugging info
669 * which can help Brian figure out what's going on when a user
672 if (getenv("MESA_INFO")) {
673 printf("X/Mesa visual = %p\n", (void *) v
);
674 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
675 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
676 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
684 #define NUM_VISUAL_TYPES 6
687 * Convert an X visual type to a GLX visual type.
689 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
691 * \return If \c visualType is a valid X visual type, a GLX visual type will
692 * be returned. Otherwise \c GLX_NONE will be returned.
695 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
699 xmesa_convert_from_x_visual_type( int visualType
)
701 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
702 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
703 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
704 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
707 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
708 ? glx_visual_types
[ visualType
] : GLX_NONE
;
712 /**********************************************************************/
713 /***** Public Functions *****/
714 /**********************************************************************/
718 * Create a new X/Mesa visual.
719 * Input: display - X11 display
720 * visinfo - an XVisualInfo pointer
721 * rgb_flag - GL_TRUE = RGB mode,
722 * GL_FALSE = color index mode
723 * alpha_flag - alpha buffer requested?
724 * db_flag - GL_TRUE = double-buffered,
725 * GL_FALSE = single buffered
726 * stereo_flag - stereo visual?
727 * ximage_flag - GL_TRUE = use an XImage for back buffer,
728 * GL_FALSE = use an off-screen pixmap for back buffer
729 * depth_size - requested bits/depth values, or zero
730 * stencil_size - requested bits/stencil values, or zero
731 * accum_red_size - requested bits/red accum values, or zero
732 * accum_green_size - requested bits/green accum values, or zero
733 * accum_blue_size - requested bits/blue accum values, or zero
734 * accum_alpha_size - requested bits/alpha accum values, or zero
735 * num_samples - number of samples/pixel if multisampling, or zero
736 * level - visual level, usually 0
737 * visualCaveat - ala the GLX extension, usually GLX_NONE
738 * Return; a new XMesaVisual or 0 if error.
741 XMesaVisual
XMesaCreateVisual( Display
*display
,
742 XVisualInfo
* visinfo
,
744 GLboolean alpha_flag
,
746 GLboolean stereo_flag
,
747 GLboolean ximage_flag
,
750 GLint accum_red_size
,
751 GLint accum_green_size
,
752 GLint accum_blue_size
,
753 GLint accum_alpha_size
,
758 XMesaDisplay xmdpy
= xmesa_init_display(display
);
760 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
765 /* For debugging only */
766 if (getenv("MESA_XSYNC")) {
767 /* This makes debugging X easier.
768 * In your debugger, set a breakpoint on _XError to stop when an
769 * X protocol error is generated.
771 XSynchronize( display
, 1 );
774 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
779 v
->display
= display
;
781 /* Save a copy of the XVisualInfo struct because the user may Xfree()
782 * the struct but we may need some of the information contained in it
785 v
->visinfo
= malloc(sizeof(*visinfo
));
790 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
792 v
->ximage_flag
= ximage_flag
;
794 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
795 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
796 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
797 v
->visualID
= visinfo
->visualid
;
798 v
->screen
= visinfo
->screen
;
800 #if !(defined(__cplusplus) || defined(c_plusplus))
801 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
803 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
806 v
->mesa_visual
.visualRating
= visualCaveat
;
809 v
->mesa_visual
.alphaBits
= 8;
811 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
814 const int xclass
= v
->visualType
;
815 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
816 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
817 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
818 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
821 /* this is an approximation */
823 depth
= v
->visinfo
->depth
;
824 red_bits
= depth
/ 3;
826 green_bits
= depth
/ 2;
830 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
832 alpha_bits
= v
->mesa_visual
.alphaBits
;
835 /* initialize visual */
837 struct gl_config
*vis
= &v
->mesa_visual
;
839 vis
->rgbMode
= GL_TRUE
;
840 vis
->doubleBufferMode
= db_flag
;
841 vis
->stereoMode
= stereo_flag
;
843 vis
->redBits
= red_bits
;
844 vis
->greenBits
= green_bits
;
845 vis
->blueBits
= blue_bits
;
846 vis
->alphaBits
= alpha_bits
;
847 vis
->rgbBits
= red_bits
+ green_bits
+ blue_bits
;
850 vis
->depthBits
= depth_size
;
851 vis
->stencilBits
= stencil_size
;
853 vis
->accumRedBits
= accum_red_size
;
854 vis
->accumGreenBits
= accum_green_size
;
855 vis
->accumBlueBits
= accum_blue_size
;
856 vis
->accumAlphaBits
= accum_alpha_size
;
858 vis
->haveAccumBuffer
= accum_red_size
> 0;
859 vis
->haveDepthBuffer
= depth_size
> 0;
860 vis
->haveStencilBuffer
= stencil_size
> 0;
862 vis
->numAuxBuffers
= 0;
864 vis
->sampleBuffers
= 0;
868 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
870 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
872 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
874 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
877 v
->stvis
.color_format
= choose_pixel_format(v
);
878 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
884 v
->stvis
.depth_stencil_format
=
885 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
);
887 v
->stvis
.accum_format
= (accum_red_size
+
888 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
889 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
891 v
->stvis
.samples
= num_samples
;
892 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
895 v
->mesa_visual
.level
= level
;
901 void XMesaDestroyVisual( XMesaVisual v
)
909 * Return the informative name.
919 * Do per-display initializations.
922 xmesa_init( Display
*display
)
924 xmesa_init_display(display
);
929 * Create a new XMesaContext.
930 * \param v the XMesaVisual
931 * \param share_list another XMesaContext with which to share display
932 * lists or NULL if no sharing is wanted.
933 * \return an XMesaContext or NULL if error.
936 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
,
937 GLuint major
, GLuint minor
,
938 GLuint profileMask
, GLuint contextFlags
)
940 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
941 struct st_context_attribs attribs
;
942 enum st_context_error ctx_err
= 0;
946 goto no_xmesa_context
;
948 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
949 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
951 goto no_xmesa_context
;
954 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
955 c
->xm_read_buffer
= NULL
;
957 memset(&attribs
, 0, sizeof(attribs
));
958 attribs
.visual
= v
->stvis
;
959 attribs
.major
= major
;
960 attribs
.minor
= minor
;
961 if (contextFlags
& GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
962 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
963 if (contextFlags
& GLX_CONTEXT_DEBUG_BIT_ARB
)
964 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
965 if (contextFlags
& GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB
)
966 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
968 switch (profileMask
) {
969 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
970 /* There are no profiles before OpenGL 3.2. The
971 * GLX_ARB_create_context_profile spec says:
973 * "If the requested OpenGL version is less than 3.2,
974 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
975 * of the context is determined solely by the requested version."
977 if (major
> 3 || (major
== 3 && minor
>= 2)) {
978 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
982 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
984 * The spec also says:
986 * "If version 3.1 is requested, the context returned may implement
987 * any of the following versions:
989 * * Version 3.1. The GL_ARB_compatibility extension may or may not
990 * be implemented, as determined by the implementation.
991 * * The core profile of version 3.2 or greater."
993 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
994 * honour a 3.1 context is through core profile.
996 if (major
== 3 && minor
== 1) {
997 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
999 attribs
.profile
= ST_PROFILE_DEFAULT
;
1002 case GLX_CONTEXT_ES_PROFILE_BIT_EXT
:
1004 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
1006 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
1014 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
, &attribs
,
1015 &ctx_err
, (share_list
) ? share_list
->st
: NULL
);
1019 c
->st
->st_manager_private
= (void *) c
;
1021 c
->hud
= hud_create(c
->st
->pipe
, c
->st
->cso_context
);
1034 void XMesaDestroyContext( XMesaContext c
)
1037 hud_destroy(c
->hud
);
1040 c
->st
->destroy(c
->st
);
1042 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
1043 * outlive it, causing segfaults
1044 struct pipe_screen *screen = c->st->pipe->screen;
1045 screen->destroy(screen);
1054 * Private function for creating an XMesaBuffer which corresponds to an
1055 * X window or pixmap.
1056 * \param v the window's XMesaVisual
1057 * \param w the window we're wrapping
1058 * \return new XMesaBuffer or NULL if error
1061 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
1063 XWindowAttributes attr
;
1071 /* Check that window depth matches visual depth */
1072 XGetWindowAttributes( v
->display
, w
, &attr
);
1074 if (v
->visinfo
->depth
!= depth
) {
1075 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1076 v
->visinfo
->depth
, depth
);
1081 if (attr
.colormap
) {
1082 cmap
= attr
.colormap
;
1085 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1086 /* this is weird, a window w/out a colormap!? */
1087 /* OK, let's just allocate a new one and hope for the best */
1088 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1091 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
1095 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
1096 (Drawable
) w
, cmap
)) {
1097 xmesa_free_buffer(b
);
1107 * Create a new XMesaBuffer from an X pixmap.
1109 * \param v the XMesaVisual
1110 * \param p the pixmap
1111 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1112 * \c GLX_DIRECT_COLOR visual for the pixmap
1113 * \returns new XMesaBuffer or NULL if error
1116 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
1122 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1126 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1127 (Drawable
) p
, cmap
)) {
1128 xmesa_free_buffer(b
);
1137 * For GLX_EXT_texture_from_pixmap
1140 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
1142 int format
, int target
, int mipmap
)
1144 GET_CURRENT_CONTEXT(ctx
);
1149 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1153 /* get pixmap size */
1154 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
1158 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1159 target
= GLX_TEXTURE_2D_EXT
;
1161 else if ( _mesa_bitcount(b
->width
) == 1
1162 && _mesa_bitcount(b
->height
) == 1) {
1163 /* power of two size */
1164 if (b
->height
== 1) {
1165 target
= GLX_TEXTURE_1D_EXT
;
1168 target
= GLX_TEXTURE_2D_EXT
;
1171 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1172 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1175 /* non power of two textures not supported */
1176 XMesaDestroyBuffer(b
);
1181 b
->TextureTarget
= target
;
1182 b
->TextureFormat
= format
;
1183 b
->TextureMipmap
= mipmap
;
1185 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1186 (Drawable
) p
, cmap
)) {
1187 xmesa_free_buffer(b
);
1197 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1198 unsigned int width
, unsigned int height
)
1201 Drawable drawable
; /* X Pixmap Drawable */
1204 /* allocate pixmap for front buffer */
1205 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1206 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1211 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1215 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1217 xmesa_free_buffer(b
);
1227 * Deallocate an XMesaBuffer structure and all related info.
1230 XMesaDestroyBuffer(XMesaBuffer b
)
1232 xmesa_free_buffer(b
);
1237 * Notify the binding context to validate the buffer.
1240 xmesa_notify_invalid_buffer(XMesaBuffer b
)
1242 p_atomic_inc(&b
->stfb
->stamp
);
1247 * Query the current drawable size and notify the binding context.
1250 xmesa_check_buffer_size(XMesaBuffer b
)
1252 GLuint old_width
, old_height
;
1254 if (b
->type
== PBUFFER
)
1257 old_width
= b
->width
;
1258 old_height
= b
->height
;
1260 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1262 if (b
->width
!= old_width
|| b
->height
!= old_height
)
1263 xmesa_notify_invalid_buffer(b
);
1268 * Bind buffer b to context c and make c the current rendering context.
1271 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1272 XMesaBuffer readBuffer
)
1274 XMesaContext old_ctx
= XMesaGetCurrentContext();
1276 if (old_ctx
&& old_ctx
!= c
) {
1277 XMesaFlush(old_ctx
);
1278 old_ctx
->xm_buffer
= NULL
;
1279 old_ctx
->xm_read_buffer
= NULL
;
1283 if (!drawBuffer
|| !readBuffer
)
1284 return GL_FALSE
; /* must specify buffers! */
1287 c
->xm_buffer
== drawBuffer
&&
1288 c
->xm_read_buffer
== readBuffer
)
1291 xmesa_check_buffer_size(drawBuffer
);
1292 if (readBuffer
!= drawBuffer
)
1293 xmesa_check_buffer_size(readBuffer
);
1295 c
->xm_buffer
= drawBuffer
;
1296 c
->xm_read_buffer
= readBuffer
;
1298 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1300 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1301 drawBuffer
->wasCurrent
= GL_TRUE
;
1305 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1313 * Unbind the context c from its buffer.
1315 GLboolean
XMesaUnbindContext( XMesaContext c
)
1317 /* A no-op for XFree86 integration purposes */
1322 XMesaContext
XMesaGetCurrentContext( void )
1324 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1325 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1331 * Swap front and back color buffers and have winsys display front buffer.
1332 * If there's no front color buffer no swap actually occurs.
1335 void XMesaSwapBuffers( XMesaBuffer b
)
1337 XMesaContext xmctx
= XMesaGetCurrentContext();
1339 /* Need to draw HUD before flushing */
1340 if (xmctx
&& xmctx
->hud
) {
1341 struct pipe_resource
*back
=
1342 xmesa_get_framebuffer_resource(b
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
1343 hud_draw(xmctx
->hud
, back
);
1346 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1347 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
);
1350 xmesa_swap_st_framebuffer(b
->stfb
);
1356 * Copy sub-region of back buffer to front buffer
1358 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1360 XMesaContext xmctx
= XMesaGetCurrentContext();
1362 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
);
1364 xmesa_copy_st_framebuffer(b
->stfb
,
1365 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1366 x
, b
->height
- y
- height
, width
, height
);
1371 void XMesaFlush( XMesaContext c
)
1373 if (c
&& c
->xm_visual
->display
) {
1374 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1375 struct pipe_fence_handle
*fence
= NULL
;
1377 c
->st
->flush(c
->st
, ST_FLUSH_FRONT
, &fence
);
1379 xmdpy
->screen
->fence_finish(xmdpy
->screen
, fence
,
1380 PIPE_TIMEOUT_INFINITE
);
1381 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1383 XFlush( c
->xm_visual
->display
);
1391 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1394 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1395 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1404 * Free/destroy all XMesaBuffers associated with given display.
1406 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1408 XMesaBuffer b
, next
;
1409 for (b
= XMesaBufferList
; b
; b
= next
) {
1411 if (b
->xm_visual
->display
== dpy
) {
1412 xmesa_free_buffer(b
);
1413 /* delete head of list? */
1414 if (XMesaBufferList
== b
) {
1415 XMesaBufferList
= next
;
1423 * Look for XMesaBuffers whose X window has been destroyed.
1424 * Deallocate any such XMesaBuffers.
1426 void XMesaGarbageCollect( void )
1428 XMesaBuffer b
, next
;
1429 for (b
=XMesaBufferList
; b
; b
=next
) {
1432 b
->xm_visual
->display
&&
1434 b
->type
== WINDOW
) {
1435 XSync(b
->xm_visual
->display
, False
);
1436 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1437 /* found a dead window, free the ancillary info */
1438 XMesaDestroyBuffer( b
);
1445 static enum st_attachment_type
xmesa_attachment_type(int glx_attachment
)
1447 switch(glx_attachment
) {
1448 case GLX_FRONT_LEFT_EXT
:
1449 return ST_ATTACHMENT_FRONT_LEFT
;
1450 case GLX_FRONT_RIGHT_EXT
:
1451 return ST_ATTACHMENT_FRONT_RIGHT
;
1452 case GLX_BACK_LEFT_EXT
:
1453 return ST_ATTACHMENT_BACK_LEFT
;
1454 case GLX_BACK_RIGHT_EXT
:
1455 return ST_ATTACHMENT_BACK_RIGHT
;
1458 return ST_ATTACHMENT_FRONT_LEFT
;
1464 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1465 const int *attrib_list
)
1467 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1468 struct st_framebuffer_iface
* stfbi
= drawable
->stfb
;
1469 struct pipe_resource
*res
;
1471 enum st_attachment_type st_attachment
= xmesa_attachment_type(buffer
);
1475 w
= drawable
->width
;
1476 h
= drawable
->height
;
1478 /* We need to validate our attachments before using them,
1479 * in case the texture doesn't exist yet. */
1480 xmesa_st_framebuffer_validate_textures(stfbi
, w
, h
, 1 << st_attachment
);
1481 res
= xmesa_get_attachment(stfbi
, st_attachment
);
1484 struct pipe_context
* pipe
= xmesa_get_context(stfbi
);
1485 enum pipe_format internal_format
= res
->format
;
1486 struct pipe_transfer
*tex_xfer
;
1488 int line
, byte_width
;
1491 internal_format
= choose_pixel_format(drawable
->xm_visual
);
1493 map
= pipe_transfer_map(pipe
, res
,
1494 0, 0, /* level, layer */
1495 PIPE_TRANSFER_WRITE
,
1501 /* Grab the XImage that we want to turn into a texture. */
1502 img
= XGetImage(dpy
,
1503 drawable
->ws
.drawable
,
1510 pipe_transfer_unmap(pipe
, tex_xfer
);
1514 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */
1515 byte_width
= w
* ((img
->bits_per_pixel
+ 7) / 8);
1517 for (line
= 0; line
< h
; line
++)
1518 memcpy(&map
[line
* tex_xfer
->stride
],
1519 &img
->data
[line
* img
->bytes_per_line
],
1522 pipe_transfer_unmap(pipe
, tex_xfer
);
1529 FALSE
/* no mipmap */);
1537 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1543 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1546 dst
->st
->copy(dst
->st
, src
->st
, mask
);