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"
64 #include "frontend/api.h"
66 #include "util/u_atomic.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
69 #include "util/u_memory.h"
71 #include "hud/hud_context.h"
73 #include "main/errors.h"
75 #include "xm_public.h"
79 /* Driver interface routines, set up by xlib backend on library
80 * _init(). These are global in the same way that function names are
83 static struct xm_driver driver
;
84 static struct st_api
*stapi
;
86 /* Default strict invalidate to false. This means we will not call
87 * XGetGeometry after every swapbuffers, which allows swapbuffers to
88 * remain asynchronous. For apps running at 100fps with synchronous
89 * swapping, a 10% boost is typical. For gears, I see closer to 20%
92 * Note that the work of copying data on swapbuffers doesn't disappear
93 * - this change just allows the X server to execute the PutImage
94 * asynchronously without us effectively blocked until its completion.
96 * This speeds up even llvmpipe's threaded rasterization as the
97 * swapbuffers operation was a large part of the serial component of
100 * The downside of this is correctness - applications which don't call
101 * glViewport on window resizes will get incorrect rendering. A
102 * better solution would be to have per-frame but asynchronous
103 * invalidation. Xcb almost looks as if it could provide this, but
104 * the API doesn't seem to quite be there.
106 boolean xmesa_strict_invalidate
= FALSE
;
108 void xmesa_set_driver( const struct xm_driver
*templ
)
111 stapi
= driver
.create_st_api();
113 xmesa_strict_invalidate
=
114 debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE
);
119 xmesa_get_param(struct st_manager
*smapi
,
120 enum st_manager_param param
)
123 case ST_MANAGER_BROKEN_INVALIDATE
:
124 return !xmesa_strict_invalidate
;
130 /* linked list of XMesaDisplay hooks per display */
131 typedef struct _XMesaExtDisplayInfo
{
132 struct _XMesaExtDisplayInfo
*next
;
134 struct xmesa_display mesaDisplay
;
135 } XMesaExtDisplayInfo
;
137 typedef struct _XMesaExtInfo
{
138 XMesaExtDisplayInfo
*head
;
142 static XMesaExtInfo MesaExtInfo
;
144 /* hook to delete XMesaDisplay on XDestroyDisplay */
146 xmesa_close_display(Display
*display
)
148 XMesaExtDisplayInfo
*info
, *prev
;
150 /* These assertions are not valid since screen creation can fail and result
152 assert(MesaExtInfo.ndisplays > 0);
153 assert(MesaExtInfo.head);
156 _XLockMutex(_Xglobal_lock
);
157 /* first find display */
159 for (info
= MesaExtInfo
.head
; info
; info
= info
->next
) {
160 if (info
->display
== display
) {
167 /* no display found */
168 _XUnlockMutex(_Xglobal_lock
);
172 /* remove display entry from list */
173 if (prev
!= MesaExtInfo
.head
) {
174 prev
->next
= info
->next
;
176 MesaExtInfo
.head
= info
->next
;
178 MesaExtInfo
.ndisplays
--;
180 _XUnlockMutex(_Xglobal_lock
);
182 /* don't forget to clean up mesaDisplay */
183 XMesaDisplay xmdpy
= &info
->mesaDisplay
;
186 * XXX: Don't destroy the screens here, since there may still
187 * be some dangling screen pointers that are used after this point
188 * if (xmdpy->screen) {
189 * xmdpy->screen->destroy(xmdpy->screen);
193 if (xmdpy
->smapi
->destroy
)
194 xmdpy
->smapi
->destroy(xmdpy
->smapi
);
197 XFree((char *) info
);
201 xmesa_init_display( Display
*display
)
203 static mtx_t init_mutex
= _MTX_INITIALIZER_NP
;
205 XMesaExtDisplayInfo
*info
;
207 if (display
== NULL
) {
211 mtx_lock(&init_mutex
);
213 /* Look for XMesaDisplay which corresponds to this display */
214 info
= MesaExtInfo
.head
;
216 if (info
->display
== display
) {
218 mtx_unlock(&init_mutex
);
219 return &info
->mesaDisplay
;
224 /* Not found. Create new XMesaDisplay */
225 /* first allocate X-related resources and hook destroy callback */
227 /* allocate mesa display info */
228 info
= (XMesaExtDisplayInfo
*) Xmalloc(sizeof(XMesaExtDisplayInfo
));
230 mtx_unlock(&init_mutex
);
233 info
->display
= display
;
235 xmdpy
= &info
->mesaDisplay
; /* to be filled out below */
236 xmdpy
->display
= display
;
239 xmdpy
->smapi
= CALLOC_STRUCT(st_manager
);
242 mtx_unlock(&init_mutex
);
246 xmdpy
->screen
= driver
.create_pipe_screen(display
);
247 if (!xmdpy
->screen
) {
250 mtx_unlock(&init_mutex
);
254 /* At this point, both smapi and screen are known to be valid */
255 xmdpy
->smapi
->screen
= xmdpy
->screen
;
256 xmdpy
->smapi
->get_param
= xmesa_get_param
;
257 (void) mtx_init(&xmdpy
->mutex
, mtx_plain
);
259 /* chain to the list of displays */
260 _XLockMutex(_Xglobal_lock
);
261 info
->next
= MesaExtInfo
.head
;
262 MesaExtInfo
.head
= info
;
263 MesaExtInfo
.ndisplays
++;
264 _XUnlockMutex(_Xglobal_lock
);
266 mtx_unlock(&init_mutex
);
272 /**********************************************************************/
273 /***** X Utility Functions *****/
274 /**********************************************************************/
278 * Return the host's byte order as LSBFirst or MSBFirst ala X.
280 static int host_byte_order( void )
283 char *cptr
= (char *) &i
;
284 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
291 * Return the true number of bits per pixel for XImages.
292 * For example, if we request a 24-bit deep visual we may actually need/get
293 * 32bpp XImages. This function returns the appropriate bpp.
294 * Input: dpy - the X display
295 * visinfo - desribes the visual to be used for XImages
296 * Return: true number of bits per pixel for XImages
299 bits_per_pixel( XMesaVisual xmv
)
301 Display
*dpy
= xmv
->display
;
302 XVisualInfo
* visinfo
= xmv
->visinfo
;
305 /* Create a temporary XImage */
306 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
307 ZPixmap
, 0, /*format, offset*/
309 1, 1, /*width, height*/
314 /* grab the bits/pixel value */
315 bitsPerPixel
= img
->bits_per_pixel
;
316 /* free the XImage */
319 XDestroyImage( img
);
326 * Determine if a given X window ID is valid (window exists).
327 * Do this by calling XGetWindowAttributes() for the window and
328 * checking if we catch an X error.
329 * Input: dpy - the display
330 * win - the window to check for existence
331 * Return: GL_TRUE - window exists
332 * GL_FALSE - window doesn't exist
334 static GLboolean WindowExistsFlag
;
336 static int window_exists_err_handler( Display
* dpy
, XErrorEvent
* xerr
)
339 if (xerr
->error_code
== BadWindow
) {
340 WindowExistsFlag
= GL_FALSE
;
345 static GLboolean
window_exists( Display
*dpy
, Window win
)
347 XWindowAttributes wa
;
348 int (*old_handler
)( Display
*, XErrorEvent
* );
349 WindowExistsFlag
= GL_TRUE
;
350 old_handler
= XSetErrorHandler(window_exists_err_handler
);
351 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
352 XSetErrorHandler(old_handler
);
353 return WindowExistsFlag
;
357 get_drawable_size( Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
362 unsigned int w
, h
, bw
, depth
;
363 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
371 * Return the size of the window (or pixmap) that corresponds to the
373 * \param width returns width in pixels
374 * \param height returns height in pixels
377 xmesa_get_window_size(Display
*dpy
, XMesaBuffer b
,
378 GLuint
*width
, GLuint
*height
)
380 XMesaDisplay xmdpy
= xmesa_init_display(dpy
);
383 mtx_lock(&xmdpy
->mutex
);
384 stat
= get_drawable_size(dpy
, b
->ws
.drawable
, width
, height
);
385 mtx_unlock(&xmdpy
->mutex
);
388 /* probably querying a window that's recently been destroyed */
389 _mesa_warning(NULL
, "XGetGeometry failed!\n");
390 *width
= *height
= 1;
394 #define GET_REDMASK(__v) __v->mesa_visual.redMask
395 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
396 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
400 * Choose the pixel format for the given visual.
401 * This will tell the gallium driver how to pack pixel data into
405 choose_pixel_format(XMesaVisual v
)
407 boolean native_byte_order
= (host_byte_order() ==
408 ImageByteOrder(v
->display
));
410 if ( GET_REDMASK(v
) == 0x0000ff
411 && GET_GREENMASK(v
) == 0x00ff00
412 && GET_BLUEMASK(v
) == 0xff0000
413 && v
->BitsPerPixel
== 32) {
414 if (native_byte_order
) {
415 /* no byteswapping needed */
416 return PIPE_FORMAT_RGBA8888_UNORM
;
419 return PIPE_FORMAT_ABGR8888_UNORM
;
422 else if ( GET_REDMASK(v
) == 0xff0000
423 && GET_GREENMASK(v
) == 0x00ff00
424 && GET_BLUEMASK(v
) == 0x0000ff
425 && v
->BitsPerPixel
== 32) {
426 if (native_byte_order
) {
427 /* no byteswapping needed */
428 return PIPE_FORMAT_BGRA8888_UNORM
;
431 return PIPE_FORMAT_ARGB8888_UNORM
;
434 else if ( GET_REDMASK(v
) == 0x0000ff00
435 && GET_GREENMASK(v
) == 0x00ff0000
436 && GET_BLUEMASK(v
) == 0xff000000
437 && v
->BitsPerPixel
== 32) {
438 if (native_byte_order
) {
439 /* no byteswapping needed */
440 return PIPE_FORMAT_ARGB8888_UNORM
;
443 return PIPE_FORMAT_BGRA8888_UNORM
;
446 else if ( GET_REDMASK(v
) == 0xf800
447 && GET_GREENMASK(v
) == 0x07e0
448 && GET_BLUEMASK(v
) == 0x001f
450 && v
->BitsPerPixel
== 16) {
452 return PIPE_FORMAT_B5G6R5_UNORM
;
455 return PIPE_FORMAT_NONE
;
460 * Choose a depth/stencil format that satisfies the given depth and
463 static enum pipe_format
464 choose_depth_stencil_format(XMesaDisplay xmdpy
, int depth
, int stencil
,
467 const enum pipe_texture_target target
= PIPE_TEXTURE_2D
;
468 const unsigned tex_usage
= PIPE_BIND_DEPTH_STENCIL
;
469 enum pipe_format formats
[8], fmt
;
474 if (depth
<= 16 && stencil
== 0) {
475 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
477 if (depth
<= 24 && stencil
== 0) {
478 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
479 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
481 if (depth
<= 24 && stencil
<= 8) {
482 formats
[count
++] = PIPE_FORMAT_S8_UINT_Z24_UNORM
;
483 formats
[count
++] = PIPE_FORMAT_Z24_UNORM_S8_UINT
;
485 if (depth
<= 32 && stencil
== 0) {
486 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
489 fmt
= PIPE_FORMAT_NONE
;
490 for (i
= 0; i
< count
; i
++) {
491 if (xmdpy
->screen
->is_format_supported(xmdpy
->screen
, formats
[i
],
492 target
, sample_count
,
493 sample_count
, tex_usage
)) {
504 /**********************************************************************/
505 /***** Linked list of XMesaBuffers *****/
506 /**********************************************************************/
508 static XMesaBuffer XMesaBufferList
= NULL
;
512 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
513 * Note that XMesaBuffer is derived from struct gl_framebuffer.
514 * The new XMesaBuffer will not have any size (Width=Height=0).
516 * \param d the corresponding X drawable (window or pixmap)
517 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
518 * \param vis the buffer's visual
519 * \param cmap the window's colormap, if known.
520 * \return new XMesaBuffer or NULL if any problem
523 create_xmesa_buffer(Drawable d
, BufferType type
,
524 XMesaVisual vis
, Colormap cmap
)
526 XMesaDisplay xmdpy
= xmesa_init_display(vis
->display
);
529 assert(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
534 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
539 b
->ws
.visual
= vis
->visinfo
->visual
;
540 b
->ws
.depth
= vis
->visinfo
->depth
;
546 get_drawable_size(vis
->display
, d
, &b
->width
, &b
->height
);
549 * Create framebuffer, but we'll plug in our own renderbuffers below.
551 b
->stfb
= xmesa_create_st_framebuffer(xmdpy
, b
);
553 /* GLX_EXT_texture_from_pixmap */
554 b
->TextureTarget
= 0;
555 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
556 b
->TextureMipmap
= 0;
558 /* insert buffer into linked list */
559 b
->Next
= XMesaBufferList
;
567 * Find an XMesaBuffer by matching X display and colormap but NOT matching
568 * the notThis buffer.
571 xmesa_find_buffer(Display
*dpy
, Colormap cmap
, XMesaBuffer notThis
)
574 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
575 if (b
->xm_visual
->display
== dpy
&&
586 * Remove buffer from linked list, delete if no longer referenced.
589 xmesa_free_buffer(XMesaBuffer buffer
)
591 XMesaBuffer prev
= NULL
, b
;
593 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
595 /* unlink buffer from list */
597 prev
->Next
= buffer
->Next
;
599 XMesaBufferList
= buffer
->Next
;
601 /* Since the X window for the XMesaBuffer is going away, we don't
602 * want to dereference this pointer in the future.
606 /* Notify the st manager that the associated framebuffer interface
607 * object is no longer valid.
609 stapi
->destroy_drawable(stapi
, buffer
->stfb
);
611 /* XXX we should move the buffer to a delete-pending list and destroy
612 * the buffer until it is no longer current.
614 xmesa_destroy_st_framebuffer(buffer
->stfb
);
620 /* continue search */
623 /* buffer not found in XMesaBufferList */
624 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
629 /**********************************************************************/
630 /***** Misc Private Functions *****/
631 /**********************************************************************/
635 * When a context is bound for the first time, we can finally finish
636 * initializing the context's visual and buffer information.
637 * \param v the XMesaVisual to initialize
638 * \param b the XMesaBuffer to initialize (may be NULL)
639 * \param window the window/pixmap we're rendering into
640 * \param cmap the colormap associated with the window/pixmap
641 * \return GL_TRUE=success, GL_FALSE=failure
644 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
645 Drawable window
, Colormap cmap
)
647 assert(!b
|| b
->xm_visual
== v
);
649 /* Save true bits/pixel */
650 v
->BitsPerPixel
= bits_per_pixel(v
);
651 assert(v
->BitsPerPixel
> 0);
654 * We support RGB rendering into almost any kind of visual.
656 const int xclass
= v
->visualType
;
657 if (xclass
!= GLX_TRUE_COLOR
&& xclass
!= GLX_DIRECT_COLOR
) {
659 "XMesa: RGB mode rendering not supported in given visual.\n");
663 if (v
->BitsPerPixel
== 32) {
664 /* We use XImages for all front/back buffers. If an X Window or
665 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
666 * will be preserved. For XImages we're in luck.
668 v
->mesa_visual
.alphaBits
= 8;
672 * If MESA_INFO env var is set print out some debugging info
673 * which can help Brian figure out what's going on when a user
676 if (getenv("MESA_INFO")) {
677 printf("X/Mesa visual = %p\n", (void *) v
);
678 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
679 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
680 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
688 #define NUM_VISUAL_TYPES 6
691 * Convert an X visual type to a GLX visual type.
693 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
695 * \return If \c visualType is a valid X visual type, a GLX visual type will
696 * be returned. Otherwise \c GLX_NONE will be returned.
699 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
703 xmesa_convert_from_x_visual_type( int visualType
)
705 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
706 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
707 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
708 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
711 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
712 ? glx_visual_types
[ visualType
] : GLX_NONE
;
716 /**********************************************************************/
717 /***** Public Functions *****/
718 /**********************************************************************/
722 * Create a new X/Mesa visual.
723 * Input: display - X11 display
724 * visinfo - an XVisualInfo pointer
725 * rgb_flag - GL_TRUE = RGB mode,
726 * GL_FALSE = color index mode
727 * alpha_flag - alpha buffer requested?
728 * db_flag - GL_TRUE = double-buffered,
729 * GL_FALSE = single buffered
730 * stereo_flag - stereo visual?
731 * ximage_flag - GL_TRUE = use an XImage for back buffer,
732 * GL_FALSE = use an off-screen pixmap for back buffer
733 * depth_size - requested bits/depth values, or zero
734 * stencil_size - requested bits/stencil values, or zero
735 * accum_red_size - requested bits/red accum values, or zero
736 * accum_green_size - requested bits/green accum values, or zero
737 * accum_blue_size - requested bits/blue accum values, or zero
738 * accum_alpha_size - requested bits/alpha accum values, or zero
739 * num_samples - number of samples/pixel if multisampling, or zero
740 * level - visual level, usually 0
741 * visualCaveat - ala the GLX extension, usually GLX_NONE
742 * Return; a new XMesaVisual or 0 if error.
745 XMesaVisual
XMesaCreateVisual( Display
*display
,
746 XVisualInfo
* visinfo
,
748 GLboolean alpha_flag
,
750 GLboolean stereo_flag
,
751 GLboolean ximage_flag
,
754 GLint accum_red_size
,
755 GLint accum_green_size
,
756 GLint accum_blue_size
,
757 GLint accum_alpha_size
,
762 XMesaDisplay xmdpy
= xmesa_init_display(display
);
764 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
772 /* For debugging only */
773 if (getenv("MESA_XSYNC")) {
774 /* This makes debugging X easier.
775 * In your debugger, set a breakpoint on _XError to stop when an
776 * X protocol error is generated.
778 XSynchronize( display
, 1 );
781 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
786 v
->display
= display
;
788 /* Save a copy of the XVisualInfo struct because the user may Xfree()
789 * the struct but we may need some of the information contained in it
792 v
->visinfo
= malloc(sizeof(*visinfo
));
797 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
799 v
->ximage_flag
= ximage_flag
;
801 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
802 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
803 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
804 v
->visualID
= visinfo
->visualid
;
805 v
->screen
= visinfo
->screen
;
807 #if !(defined(__cplusplus) || defined(c_plusplus))
808 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
810 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
813 v
->mesa_visual
.visualRating
= visualCaveat
;
816 v
->mesa_visual
.alphaBits
= 8;
818 (void) initialize_visual_and_buffer( v
, NULL
, 0, 0 );
821 const int xclass
= v
->visualType
;
822 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
823 red_bits
= util_bitcount(GET_REDMASK(v
));
824 green_bits
= util_bitcount(GET_GREENMASK(v
));
825 blue_bits
= util_bitcount(GET_BLUEMASK(v
));
828 /* this is an approximation */
830 depth
= v
->visinfo
->depth
;
831 red_bits
= depth
/ 3;
833 green_bits
= depth
/ 2;
837 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
839 alpha_bits
= v
->mesa_visual
.alphaBits
;
842 /* initialize visual */
844 struct gl_config
*vis
= &v
->mesa_visual
;
846 vis
->doubleBufferMode
= db_flag
;
847 vis
->stereoMode
= stereo_flag
;
849 vis
->redBits
= red_bits
;
850 vis
->greenBits
= green_bits
;
851 vis
->blueBits
= blue_bits
;
852 vis
->alphaBits
= alpha_bits
;
853 vis
->rgbBits
= red_bits
+ green_bits
+ blue_bits
;
855 vis
->depthBits
= depth_size
;
856 vis
->stencilBits
= stencil_size
;
858 vis
->accumRedBits
= accum_red_size
;
859 vis
->accumGreenBits
= accum_green_size
;
860 vis
->accumBlueBits
= accum_blue_size
;
861 vis
->accumAlphaBits
= accum_alpha_size
;
863 vis
->numAuxBuffers
= 0;
865 vis
->sampleBuffers
= num_samples
> 1;
866 vis
->samples
= num_samples
;
869 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
871 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
873 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
875 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
878 v
->stvis
.color_format
= choose_pixel_format(v
);
880 /* Check format support at requested num_samples (for multisample) */
881 if (!xmdpy
->screen
->is_format_supported(xmdpy
->screen
,
882 v
->stvis
.color_format
,
883 PIPE_TEXTURE_2D
, num_samples
,
885 PIPE_BIND_RENDER_TARGET
))
886 v
->stvis
.color_format
= PIPE_FORMAT_NONE
;
888 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
894 v
->stvis
.depth_stencil_format
=
895 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
,
898 v
->stvis
.accum_format
= (accum_red_size
+
899 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
900 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
902 v
->stvis
.samples
= num_samples
;
903 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
906 v
->mesa_visual
.level
= level
;
912 void XMesaDestroyVisual( XMesaVisual v
)
920 * Return the informative name.
930 * Do per-display initializations.
933 xmesa_init( Display
*display
)
935 return xmesa_init_display(display
) ? 0 : 1;
940 * Create a new XMesaContext.
941 * \param v the XMesaVisual
942 * \param share_list another XMesaContext with which to share display
943 * lists or NULL if no sharing is wanted.
944 * \return an XMesaContext or NULL if error.
947 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
,
948 GLuint major
, GLuint minor
,
949 GLuint profileMask
, GLuint contextFlags
)
951 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
952 struct st_context_attribs attribs
;
953 enum st_context_error ctx_err
= 0;
957 goto no_xmesa_context
;
959 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
960 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
962 goto no_xmesa_context
;
965 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
966 c
->xm_read_buffer
= NULL
;
968 memset(&attribs
, 0, sizeof(attribs
));
969 attribs
.visual
= v
->stvis
;
970 attribs
.major
= major
;
971 attribs
.minor
= minor
;
972 if (contextFlags
& GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
973 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
974 if (contextFlags
& GLX_CONTEXT_DEBUG_BIT_ARB
)
975 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
976 if (contextFlags
& GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB
)
977 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
979 switch (profileMask
) {
980 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
981 /* There are no profiles before OpenGL 3.2. The
982 * GLX_ARB_create_context_profile spec says:
984 * "If the requested OpenGL version is less than 3.2,
985 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
986 * of the context is determined solely by the requested version."
988 if (major
> 3 || (major
== 3 && minor
>= 2)) {
989 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
993 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
995 * The spec also says:
997 * "If version 3.1 is requested, the context returned may implement
998 * any of the following versions:
1000 * * Version 3.1. The GL_ARB_compatibility extension may or may not
1001 * be implemented, as determined by the implementation.
1002 * * The core profile of version 3.2 or greater."
1004 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
1005 * honour a 3.1 context is through core profile.
1007 if (major
== 3 && minor
== 1) {
1008 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
1010 attribs
.profile
= ST_PROFILE_DEFAULT
;
1013 case GLX_CONTEXT_ES_PROFILE_BIT_EXT
:
1015 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
1017 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
1025 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
, &attribs
,
1026 &ctx_err
, (share_list
) ? share_list
->st
: NULL
);
1030 c
->st
->st_manager_private
= (void *) c
;
1032 c
->hud
= hud_create(c
->st
->cso_context
, NULL
);
1045 void XMesaDestroyContext( XMesaContext c
)
1048 hud_destroy(c
->hud
, NULL
);
1051 c
->st
->destroy(c
->st
);
1053 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
1054 * outlive it, causing segfaults
1055 struct pipe_screen *screen = c->st->pipe->screen;
1056 screen->destroy(screen);
1065 * Private function for creating an XMesaBuffer which corresponds to an
1066 * X window or pixmap.
1067 * \param v the window's XMesaVisual
1068 * \param w the window we're wrapping
1069 * \return new XMesaBuffer or NULL if error
1072 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
1074 XWindowAttributes attr
;
1082 /* Check that window depth matches visual depth */
1083 XGetWindowAttributes( v
->display
, w
, &attr
);
1085 if (v
->visinfo
->depth
!= depth
) {
1086 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1087 v
->visinfo
->depth
, depth
);
1092 if (attr
.colormap
) {
1093 cmap
= attr
.colormap
;
1096 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1097 /* this is weird, a window w/out a colormap!? */
1098 /* OK, let's just allocate a new one and hope for the best */
1099 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1102 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
1106 if (!initialize_visual_and_buffer( v
, b
, (Drawable
) w
, cmap
)) {
1107 xmesa_free_buffer(b
);
1117 * Create a new XMesaBuffer from an X pixmap.
1119 * \param v the XMesaVisual
1120 * \param p the pixmap
1121 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1122 * \c GLX_DIRECT_COLOR visual for the pixmap
1123 * \returns new XMesaBuffer or NULL if error
1126 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
1132 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1136 if (!initialize_visual_and_buffer(v
, b
, (Drawable
) p
, cmap
)) {
1137 xmesa_free_buffer(b
);
1146 * For GLX_EXT_texture_from_pixmap
1149 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
1151 int format
, int target
, int mipmap
)
1153 GET_CURRENT_CONTEXT(ctx
);
1158 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1162 /* get pixmap size */
1163 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
1167 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1168 target
= GLX_TEXTURE_2D_EXT
;
1170 else if ( util_bitcount(b
->width
) == 1
1171 && util_bitcount(b
->height
) == 1) {
1172 /* power of two size */
1173 if (b
->height
== 1) {
1174 target
= GLX_TEXTURE_1D_EXT
;
1177 target
= GLX_TEXTURE_2D_EXT
;
1180 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1181 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1184 /* non power of two textures not supported */
1185 XMesaDestroyBuffer(b
);
1190 b
->TextureTarget
= target
;
1191 b
->TextureFormat
= format
;
1192 b
->TextureMipmap
= mipmap
;
1194 if (!initialize_visual_and_buffer(v
, b
, (Drawable
) p
, cmap
)) {
1195 xmesa_free_buffer(b
);
1205 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1206 unsigned int width
, unsigned int height
)
1209 Drawable drawable
; /* X Pixmap Drawable */
1212 /* allocate pixmap for front buffer */
1213 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1214 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1219 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1223 if (!initialize_visual_and_buffer(v
, b
, drawable
, cmap
)) {
1224 xmesa_free_buffer(b
);
1234 * Deallocate an XMesaBuffer structure and all related info.
1237 XMesaDestroyBuffer(XMesaBuffer b
)
1239 xmesa_free_buffer(b
);
1244 * Notify the binding context to validate the buffer.
1247 xmesa_notify_invalid_buffer(XMesaBuffer b
)
1249 p_atomic_inc(&b
->stfb
->stamp
);
1254 * Query the current drawable size and notify the binding context.
1257 xmesa_check_buffer_size(XMesaBuffer b
)
1259 GLuint old_width
, old_height
;
1264 if (b
->type
== PBUFFER
)
1267 old_width
= b
->width
;
1268 old_height
= b
->height
;
1270 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1272 if (b
->width
!= old_width
|| b
->height
!= old_height
)
1273 xmesa_notify_invalid_buffer(b
);
1278 * Bind buffer b to context c and make c the current rendering context.
1281 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1282 XMesaBuffer readBuffer
)
1284 XMesaContext old_ctx
= XMesaGetCurrentContext();
1286 if (old_ctx
&& old_ctx
!= c
) {
1287 XMesaFlush(old_ctx
);
1288 old_ctx
->xm_buffer
= NULL
;
1289 old_ctx
->xm_read_buffer
= NULL
;
1293 if (!drawBuffer
!= !readBuffer
) {
1294 return GL_FALSE
; /* must specify zero or two buffers! */
1298 c
->xm_buffer
== drawBuffer
&&
1299 c
->xm_read_buffer
== readBuffer
)
1302 xmesa_check_buffer_size(drawBuffer
);
1303 if (readBuffer
!= drawBuffer
)
1304 xmesa_check_buffer_size(readBuffer
);
1306 c
->xm_buffer
= drawBuffer
;
1307 c
->xm_read_buffer
= readBuffer
;
1309 stapi
->make_current(stapi
, c
->st
,
1310 drawBuffer
? drawBuffer
->stfb
: NULL
,
1311 readBuffer
? readBuffer
->stfb
: NULL
);
1313 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1315 drawBuffer
->wasCurrent
= GL_TRUE
;
1319 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1327 * Unbind the context c from its buffer.
1329 GLboolean
XMesaUnbindContext( XMesaContext c
)
1331 /* A no-op for XFree86 integration purposes */
1336 XMesaContext
XMesaGetCurrentContext( void )
1338 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1339 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1345 * Swap front and back color buffers and have winsys display front buffer.
1346 * If there's no front color buffer no swap actually occurs.
1349 void XMesaSwapBuffers( XMesaBuffer b
)
1351 XMesaContext xmctx
= XMesaGetCurrentContext();
1353 /* Need to draw HUD before flushing */
1354 if (xmctx
&& xmctx
->hud
) {
1355 struct pipe_resource
*back
=
1356 xmesa_get_framebuffer_resource(b
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
1357 hud_run(xmctx
->hud
, NULL
, back
);
1360 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1361 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
, NULL
, NULL
);
1364 xmesa_swap_st_framebuffer(b
->stfb
);
1370 * Copy sub-region of back buffer to front buffer
1372 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1374 XMesaContext xmctx
= XMesaGetCurrentContext();
1376 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
, NULL
, NULL
);
1378 xmesa_copy_st_framebuffer(b
->stfb
,
1379 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1380 x
, b
->height
- y
- height
, width
, height
);
1385 void XMesaFlush( XMesaContext c
)
1387 if (c
&& c
->xm_visual
->display
) {
1388 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1389 struct pipe_fence_handle
*fence
= NULL
;
1391 c
->st
->flush(c
->st
, ST_FLUSH_FRONT
, &fence
, NULL
, NULL
);
1393 xmdpy
->screen
->fence_finish(xmdpy
->screen
, NULL
, fence
,
1394 PIPE_TIMEOUT_INFINITE
);
1395 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1397 XFlush( c
->xm_visual
->display
);
1405 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1408 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1409 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1418 * Free/destroy all XMesaBuffers associated with given display.
1420 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1422 XMesaBuffer b
, next
;
1423 for (b
= XMesaBufferList
; b
; b
= next
) {
1425 if (b
->xm_visual
->display
== dpy
) {
1426 xmesa_free_buffer(b
);
1427 /* delete head of list? */
1428 if (XMesaBufferList
== b
) {
1429 XMesaBufferList
= next
;
1437 * Look for XMesaBuffers whose X window has been destroyed.
1438 * Deallocate any such XMesaBuffers.
1440 void XMesaGarbageCollect( void )
1442 XMesaBuffer b
, next
;
1443 for (b
=XMesaBufferList
; b
; b
=next
) {
1446 b
->xm_visual
->display
&&
1448 b
->type
== WINDOW
) {
1449 XSync(b
->xm_visual
->display
, False
);
1450 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1451 /* found a dead window, free the ancillary info */
1452 XMesaDestroyBuffer( b
);
1459 static enum st_attachment_type
xmesa_attachment_type(int glx_attachment
)
1461 switch(glx_attachment
) {
1462 case GLX_FRONT_LEFT_EXT
:
1463 return ST_ATTACHMENT_FRONT_LEFT
;
1464 case GLX_FRONT_RIGHT_EXT
:
1465 return ST_ATTACHMENT_FRONT_RIGHT
;
1466 case GLX_BACK_LEFT_EXT
:
1467 return ST_ATTACHMENT_BACK_LEFT
;
1468 case GLX_BACK_RIGHT_EXT
:
1469 return ST_ATTACHMENT_BACK_RIGHT
;
1472 return ST_ATTACHMENT_FRONT_LEFT
;
1478 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1479 const int *attrib_list
)
1481 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1482 struct st_framebuffer_iface
* stfbi
= drawable
->stfb
;
1483 struct pipe_resource
*res
;
1485 enum st_attachment_type st_attachment
= xmesa_attachment_type(buffer
);
1489 w
= drawable
->width
;
1490 h
= drawable
->height
;
1492 /* We need to validate our attachments before using them,
1493 * in case the texture doesn't exist yet. */
1494 xmesa_st_framebuffer_validate_textures(stfbi
, w
, h
, 1 << st_attachment
);
1495 res
= xmesa_get_attachment(stfbi
, st_attachment
);
1498 struct pipe_context
* pipe
= xmesa_get_context(stfbi
);
1499 enum pipe_format internal_format
= res
->format
;
1500 struct pipe_transfer
*tex_xfer
;
1502 int line
, byte_width
;
1505 internal_format
= choose_pixel_format(drawable
->xm_visual
);
1507 map
= pipe_transfer_map(pipe
, res
,
1508 0, 0, /* level, layer */
1509 PIPE_TRANSFER_WRITE
,
1515 /* Grab the XImage that we want to turn into a texture. */
1516 img
= XGetImage(dpy
,
1517 drawable
->ws
.drawable
,
1524 pipe_transfer_unmap(pipe
, tex_xfer
);
1528 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */
1529 byte_width
= w
* ((img
->bits_per_pixel
+ 7) / 8);
1531 for (line
= 0; line
< h
; line
++)
1532 memcpy(&map
[line
* tex_xfer
->stride
],
1533 &img
->data
[line
* img
->bytes_per_line
],
1536 pipe_transfer_unmap(pipe
, tex_xfer
);
1543 FALSE
/* no mipmap */);
1551 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1557 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1560 dst
->st
->copy(dst
->st
, src
->st
, mask
);