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 "state_tracker/st_api.h"
66 #include "util/u_atomic.h"
67 #include "util/u_inlines.h"
68 #include "util/u_math.h"
70 #include "hud/hud_context.h"
72 #include "main/errors.h"
73 #include "main/imports.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 rgb_flag TRUE = RGBA mode, FALSE = color index mode
640 * \param window the window/pixmap we're rendering into
641 * \param cmap the colormap associated with the window/pixmap
642 * \return GL_TRUE=success, GL_FALSE=failure
645 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
646 GLboolean rgb_flag
, Drawable window
,
649 assert(!b
|| b
->xm_visual
== v
);
651 /* Save true bits/pixel */
652 v
->BitsPerPixel
= bits_per_pixel(v
);
653 assert(v
->BitsPerPixel
> 0);
655 if (rgb_flag
== GL_FALSE
) {
656 /* COLOR-INDEXED WINDOW: not supported*/
661 * We support RGB rendering into almost any kind of visual.
663 const int xclass
= v
->visualType
;
664 if (xclass
!= GLX_TRUE_COLOR
&& xclass
!= GLX_DIRECT_COLOR
) {
666 "XMesa: RGB mode rendering not supported in given visual.\n");
670 if (v
->BitsPerPixel
== 32) {
671 /* We use XImages for all front/back buffers. If an X Window or
672 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
673 * will be preserved. For XImages we're in luck.
675 v
->mesa_visual
.alphaBits
= 8;
680 * If MESA_INFO env var is set print out some debugging info
681 * which can help Brian figure out what's going on when a user
684 if (getenv("MESA_INFO")) {
685 printf("X/Mesa visual = %p\n", (void *) v
);
686 printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
687 printf("X/Mesa depth = %d\n", v
->visinfo
->depth
);
688 printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
696 #define NUM_VISUAL_TYPES 6
699 * Convert an X visual type to a GLX visual type.
701 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
703 * \return If \c visualType is a valid X visual type, a GLX visual type will
704 * be returned. Otherwise \c GLX_NONE will be returned.
707 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
711 xmesa_convert_from_x_visual_type( int visualType
)
713 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
714 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
715 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
716 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
719 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
720 ? glx_visual_types
[ visualType
] : GLX_NONE
;
724 /**********************************************************************/
725 /***** Public Functions *****/
726 /**********************************************************************/
730 * Create a new X/Mesa visual.
731 * Input: display - X11 display
732 * visinfo - an XVisualInfo pointer
733 * rgb_flag - GL_TRUE = RGB mode,
734 * GL_FALSE = color index mode
735 * alpha_flag - alpha buffer requested?
736 * db_flag - GL_TRUE = double-buffered,
737 * GL_FALSE = single buffered
738 * stereo_flag - stereo visual?
739 * ximage_flag - GL_TRUE = use an XImage for back buffer,
740 * GL_FALSE = use an off-screen pixmap for back buffer
741 * depth_size - requested bits/depth values, or zero
742 * stencil_size - requested bits/stencil values, or zero
743 * accum_red_size - requested bits/red accum values, or zero
744 * accum_green_size - requested bits/green accum values, or zero
745 * accum_blue_size - requested bits/blue accum values, or zero
746 * accum_alpha_size - requested bits/alpha accum values, or zero
747 * num_samples - number of samples/pixel if multisampling, or zero
748 * level - visual level, usually 0
749 * visualCaveat - ala the GLX extension, usually GLX_NONE
750 * Return; a new XMesaVisual or 0 if error.
753 XMesaVisual
XMesaCreateVisual( Display
*display
,
754 XVisualInfo
* visinfo
,
756 GLboolean alpha_flag
,
758 GLboolean stereo_flag
,
759 GLboolean ximage_flag
,
762 GLint accum_red_size
,
763 GLint accum_green_size
,
764 GLint accum_blue_size
,
765 GLint accum_alpha_size
,
770 XMesaDisplay xmdpy
= xmesa_init_display(display
);
772 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
777 /* For debugging only */
778 if (getenv("MESA_XSYNC")) {
779 /* This makes debugging X easier.
780 * In your debugger, set a breakpoint on _XError to stop when an
781 * X protocol error is generated.
783 XSynchronize( display
, 1 );
786 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
791 v
->display
= display
;
793 /* Save a copy of the XVisualInfo struct because the user may Xfree()
794 * the struct but we may need some of the information contained in it
797 v
->visinfo
= malloc(sizeof(*visinfo
));
802 memcpy(v
->visinfo
, visinfo
, sizeof(*visinfo
));
804 v
->ximage_flag
= ximage_flag
;
806 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
807 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
808 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
809 v
->visualID
= visinfo
->visualid
;
810 v
->screen
= visinfo
->screen
;
812 #if !(defined(__cplusplus) || defined(c_plusplus))
813 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
815 v
->visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
818 v
->mesa_visual
.visualRating
= visualCaveat
;
821 v
->mesa_visual
.alphaBits
= 8;
823 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
826 const int xclass
= v
->visualType
;
827 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
828 red_bits
= util_bitcount(GET_REDMASK(v
));
829 green_bits
= util_bitcount(GET_GREENMASK(v
));
830 blue_bits
= util_bitcount(GET_BLUEMASK(v
));
833 /* this is an approximation */
835 depth
= v
->visinfo
->depth
;
836 red_bits
= depth
/ 3;
838 green_bits
= depth
/ 2;
842 assert( red_bits
+ green_bits
+ blue_bits
== v
->visinfo
->depth
);
844 alpha_bits
= v
->mesa_visual
.alphaBits
;
847 /* initialize visual */
849 struct gl_config
*vis
= &v
->mesa_visual
;
851 vis
->rgbMode
= GL_TRUE
;
852 vis
->doubleBufferMode
= db_flag
;
853 vis
->stereoMode
= stereo_flag
;
855 vis
->redBits
= red_bits
;
856 vis
->greenBits
= green_bits
;
857 vis
->blueBits
= blue_bits
;
858 vis
->alphaBits
= alpha_bits
;
859 vis
->rgbBits
= red_bits
+ green_bits
+ blue_bits
;
861 vis
->depthBits
= depth_size
;
862 vis
->stencilBits
= stencil_size
;
864 vis
->accumRedBits
= accum_red_size
;
865 vis
->accumGreenBits
= accum_green_size
;
866 vis
->accumBlueBits
= accum_blue_size
;
867 vis
->accumAlphaBits
= accum_alpha_size
;
869 vis
->haveAccumBuffer
= accum_red_size
> 0;
870 vis
->haveDepthBuffer
= depth_size
> 0;
871 vis
->haveStencilBuffer
= stencil_size
> 0;
873 vis
->numAuxBuffers
= 0;
875 vis
->sampleBuffers
= num_samples
> 1;
876 vis
->samples
= num_samples
;
879 v
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
881 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
883 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
885 v
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
888 v
->stvis
.color_format
= choose_pixel_format(v
);
890 /* Check format support at requested num_samples (for multisample) */
891 if (!xmdpy
->screen
->is_format_supported(xmdpy
->screen
,
892 v
->stvis
.color_format
,
893 PIPE_TEXTURE_2D
, num_samples
,
895 PIPE_BIND_RENDER_TARGET
))
896 v
->stvis
.color_format
= PIPE_FORMAT_NONE
;
898 if (v
->stvis
.color_format
== PIPE_FORMAT_NONE
) {
904 v
->stvis
.depth_stencil_format
=
905 choose_depth_stencil_format(xmdpy
, depth_size
, stencil_size
,
908 v
->stvis
.accum_format
= (accum_red_size
+
909 accum_green_size
+ accum_blue_size
+ accum_alpha_size
) ?
910 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
912 v
->stvis
.samples
= num_samples
;
913 v
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
916 v
->mesa_visual
.level
= level
;
922 void XMesaDestroyVisual( XMesaVisual v
)
930 * Return the informative name.
940 * Do per-display initializations.
943 xmesa_init( Display
*display
)
945 return xmesa_init_display(display
) ? 0 : 1;
950 * Create a new XMesaContext.
951 * \param v the XMesaVisual
952 * \param share_list another XMesaContext with which to share display
953 * lists or NULL if no sharing is wanted.
954 * \return an XMesaContext or NULL if error.
957 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
,
958 GLuint major
, GLuint minor
,
959 GLuint profileMask
, GLuint contextFlags
)
961 XMesaDisplay xmdpy
= xmesa_init_display(v
->display
);
962 struct st_context_attribs attribs
;
963 enum st_context_error ctx_err
= 0;
967 goto no_xmesa_context
;
969 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
970 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
972 goto no_xmesa_context
;
975 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
976 c
->xm_read_buffer
= NULL
;
978 memset(&attribs
, 0, sizeof(attribs
));
979 attribs
.visual
= v
->stvis
;
980 attribs
.major
= major
;
981 attribs
.minor
= minor
;
982 if (contextFlags
& GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
983 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
984 if (contextFlags
& GLX_CONTEXT_DEBUG_BIT_ARB
)
985 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
986 if (contextFlags
& GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB
)
987 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
989 switch (profileMask
) {
990 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB
:
991 /* There are no profiles before OpenGL 3.2. The
992 * GLX_ARB_create_context_profile spec says:
994 * "If the requested OpenGL version is less than 3.2,
995 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
996 * of the context is determined solely by the requested version."
998 if (major
> 3 || (major
== 3 && minor
>= 2)) {
999 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
1003 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
1005 * The spec also says:
1007 * "If version 3.1 is requested, the context returned may implement
1008 * any of the following versions:
1010 * * Version 3.1. The GL_ARB_compatibility extension may or may not
1011 * be implemented, as determined by the implementation.
1012 * * The core profile of version 3.2 or greater."
1014 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
1015 * honour a 3.1 context is through core profile.
1017 if (major
== 3 && minor
== 1) {
1018 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
1020 attribs
.profile
= ST_PROFILE_DEFAULT
;
1023 case GLX_CONTEXT_ES_PROFILE_BIT_EXT
:
1025 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
1027 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
1035 c
->st
= stapi
->create_context(stapi
, xmdpy
->smapi
, &attribs
,
1036 &ctx_err
, (share_list
) ? share_list
->st
: NULL
);
1040 c
->st
->st_manager_private
= (void *) c
;
1042 c
->hud
= hud_create(c
->st
->cso_context
, NULL
);
1055 void XMesaDestroyContext( XMesaContext c
)
1058 hud_destroy(c
->hud
, NULL
);
1061 c
->st
->destroy(c
->st
);
1063 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
1064 * outlive it, causing segfaults
1065 struct pipe_screen *screen = c->st->pipe->screen;
1066 screen->destroy(screen);
1075 * Private function for creating an XMesaBuffer which corresponds to an
1076 * X window or pixmap.
1077 * \param v the window's XMesaVisual
1078 * \param w the window we're wrapping
1079 * \return new XMesaBuffer or NULL if error
1082 XMesaCreateWindowBuffer(XMesaVisual v
, Window w
)
1084 XWindowAttributes attr
;
1092 /* Check that window depth matches visual depth */
1093 XGetWindowAttributes( v
->display
, w
, &attr
);
1095 if (v
->visinfo
->depth
!= depth
) {
1096 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1097 v
->visinfo
->depth
, depth
);
1102 if (attr
.colormap
) {
1103 cmap
= attr
.colormap
;
1106 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
1107 /* this is weird, a window w/out a colormap!? */
1108 /* OK, let's just allocate a new one and hope for the best */
1109 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
1112 b
= create_xmesa_buffer((Drawable
) w
, WINDOW
, v
, cmap
);
1116 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
1117 (Drawable
) w
, cmap
)) {
1118 xmesa_free_buffer(b
);
1128 * Create a new XMesaBuffer from an X pixmap.
1130 * \param v the XMesaVisual
1131 * \param p the pixmap
1132 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1133 * \c GLX_DIRECT_COLOR visual for the pixmap
1134 * \returns new XMesaBuffer or NULL if error
1137 XMesaCreatePixmapBuffer(XMesaVisual v
, Pixmap p
, Colormap cmap
)
1143 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1147 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1148 (Drawable
) p
, cmap
)) {
1149 xmesa_free_buffer(b
);
1158 * For GLX_EXT_texture_from_pixmap
1161 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, Pixmap p
,
1163 int format
, int target
, int mipmap
)
1165 GET_CURRENT_CONTEXT(ctx
);
1170 b
= create_xmesa_buffer((Drawable
) p
, PIXMAP
, v
, cmap
);
1174 /* get pixmap size */
1175 xmesa_get_window_size(v
->display
, b
, &b
->width
, &b
->height
);
1179 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1180 target
= GLX_TEXTURE_2D_EXT
;
1182 else if ( util_bitcount(b
->width
) == 1
1183 && util_bitcount(b
->height
) == 1) {
1184 /* power of two size */
1185 if (b
->height
== 1) {
1186 target
= GLX_TEXTURE_1D_EXT
;
1189 target
= GLX_TEXTURE_2D_EXT
;
1192 else if (ctx
->Extensions
.NV_texture_rectangle
) {
1193 target
= GLX_TEXTURE_RECTANGLE_EXT
;
1196 /* non power of two textures not supported */
1197 XMesaDestroyBuffer(b
);
1202 b
->TextureTarget
= target
;
1203 b
->TextureFormat
= format
;
1204 b
->TextureMipmap
= mipmap
;
1206 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1207 (Drawable
) p
, cmap
)) {
1208 xmesa_free_buffer(b
);
1218 XMesaCreatePBuffer(XMesaVisual v
, Colormap cmap
,
1219 unsigned int width
, unsigned int height
)
1222 Drawable drawable
; /* X Pixmap Drawable */
1225 /* allocate pixmap for front buffer */
1226 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1227 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1232 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1236 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1238 xmesa_free_buffer(b
);
1248 * Deallocate an XMesaBuffer structure and all related info.
1251 XMesaDestroyBuffer(XMesaBuffer b
)
1253 xmesa_free_buffer(b
);
1258 * Notify the binding context to validate the buffer.
1261 xmesa_notify_invalid_buffer(XMesaBuffer b
)
1263 p_atomic_inc(&b
->stfb
->stamp
);
1268 * Query the current drawable size and notify the binding context.
1271 xmesa_check_buffer_size(XMesaBuffer b
)
1273 GLuint old_width
, old_height
;
1275 if (b
->type
== PBUFFER
)
1278 old_width
= b
->width
;
1279 old_height
= b
->height
;
1281 xmesa_get_window_size(b
->xm_visual
->display
, b
, &b
->width
, &b
->height
);
1283 if (b
->width
!= old_width
|| b
->height
!= old_height
)
1284 xmesa_notify_invalid_buffer(b
);
1289 * Bind buffer b to context c and make c the current rendering context.
1292 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1293 XMesaBuffer readBuffer
)
1295 XMesaContext old_ctx
= XMesaGetCurrentContext();
1297 if (old_ctx
&& old_ctx
!= c
) {
1298 XMesaFlush(old_ctx
);
1299 old_ctx
->xm_buffer
= NULL
;
1300 old_ctx
->xm_read_buffer
= NULL
;
1304 if (!drawBuffer
|| !readBuffer
)
1305 return GL_FALSE
; /* must specify buffers! */
1308 c
->xm_buffer
== drawBuffer
&&
1309 c
->xm_read_buffer
== readBuffer
)
1312 xmesa_check_buffer_size(drawBuffer
);
1313 if (readBuffer
!= drawBuffer
)
1314 xmesa_check_buffer_size(readBuffer
);
1316 c
->xm_buffer
= drawBuffer
;
1317 c
->xm_read_buffer
= readBuffer
;
1319 stapi
->make_current(stapi
, c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1321 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1322 drawBuffer
->wasCurrent
= GL_TRUE
;
1326 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
1334 * Unbind the context c from its buffer.
1336 GLboolean
XMesaUnbindContext( XMesaContext c
)
1338 /* A no-op for XFree86 integration purposes */
1343 XMesaContext
XMesaGetCurrentContext( void )
1345 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1346 return (XMesaContext
) (st
) ? st
->st_manager_private
: NULL
;
1352 * Swap front and back color buffers and have winsys display front buffer.
1353 * If there's no front color buffer no swap actually occurs.
1356 void XMesaSwapBuffers( XMesaBuffer b
)
1358 XMesaContext xmctx
= XMesaGetCurrentContext();
1360 /* Need to draw HUD before flushing */
1361 if (xmctx
&& xmctx
->hud
) {
1362 struct pipe_resource
*back
=
1363 xmesa_get_framebuffer_resource(b
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
1364 hud_run(xmctx
->hud
, NULL
, back
);
1367 if (xmctx
&& xmctx
->xm_buffer
== b
) {
1368 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
);
1371 xmesa_swap_st_framebuffer(b
->stfb
);
1377 * Copy sub-region of back buffer to front buffer
1379 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1381 XMesaContext xmctx
= XMesaGetCurrentContext();
1383 xmctx
->st
->flush( xmctx
->st
, ST_FLUSH_FRONT
, NULL
);
1385 xmesa_copy_st_framebuffer(b
->stfb
,
1386 ST_ATTACHMENT_BACK_LEFT
, ST_ATTACHMENT_FRONT_LEFT
,
1387 x
, b
->height
- y
- height
, width
, height
);
1392 void XMesaFlush( XMesaContext c
)
1394 if (c
&& c
->xm_visual
->display
) {
1395 XMesaDisplay xmdpy
= xmesa_init_display(c
->xm_visual
->display
);
1396 struct pipe_fence_handle
*fence
= NULL
;
1398 c
->st
->flush(c
->st
, ST_FLUSH_FRONT
, &fence
);
1400 xmdpy
->screen
->fence_finish(xmdpy
->screen
, NULL
, fence
,
1401 PIPE_TIMEOUT_INFINITE
);
1402 xmdpy
->screen
->fence_reference(xmdpy
->screen
, &fence
, NULL
);
1404 XFlush( c
->xm_visual
->display
);
1412 XMesaBuffer
XMesaFindBuffer( Display
*dpy
, Drawable d
)
1415 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
1416 if (b
->ws
.drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1425 * Free/destroy all XMesaBuffers associated with given display.
1427 void xmesa_destroy_buffers_on_display(Display
*dpy
)
1429 XMesaBuffer b
, next
;
1430 for (b
= XMesaBufferList
; b
; b
= next
) {
1432 if (b
->xm_visual
->display
== dpy
) {
1433 xmesa_free_buffer(b
);
1434 /* delete head of list? */
1435 if (XMesaBufferList
== b
) {
1436 XMesaBufferList
= next
;
1444 * Look for XMesaBuffers whose X window has been destroyed.
1445 * Deallocate any such XMesaBuffers.
1447 void XMesaGarbageCollect( void )
1449 XMesaBuffer b
, next
;
1450 for (b
=XMesaBufferList
; b
; b
=next
) {
1453 b
->xm_visual
->display
&&
1455 b
->type
== WINDOW
) {
1456 XSync(b
->xm_visual
->display
, False
);
1457 if (!window_exists( b
->xm_visual
->display
, b
->ws
.drawable
)) {
1458 /* found a dead window, free the ancillary info */
1459 XMesaDestroyBuffer( b
);
1466 static enum st_attachment_type
xmesa_attachment_type(int glx_attachment
)
1468 switch(glx_attachment
) {
1469 case GLX_FRONT_LEFT_EXT
:
1470 return ST_ATTACHMENT_FRONT_LEFT
;
1471 case GLX_FRONT_RIGHT_EXT
:
1472 return ST_ATTACHMENT_FRONT_RIGHT
;
1473 case GLX_BACK_LEFT_EXT
:
1474 return ST_ATTACHMENT_BACK_LEFT
;
1475 case GLX_BACK_RIGHT_EXT
:
1476 return ST_ATTACHMENT_BACK_RIGHT
;
1479 return ST_ATTACHMENT_FRONT_LEFT
;
1485 XMesaBindTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
,
1486 const int *attrib_list
)
1488 struct st_context_iface
*st
= stapi
->get_current(stapi
);
1489 struct st_framebuffer_iface
* stfbi
= drawable
->stfb
;
1490 struct pipe_resource
*res
;
1492 enum st_attachment_type st_attachment
= xmesa_attachment_type(buffer
);
1496 w
= drawable
->width
;
1497 h
= drawable
->height
;
1499 /* We need to validate our attachments before using them,
1500 * in case the texture doesn't exist yet. */
1501 xmesa_st_framebuffer_validate_textures(stfbi
, w
, h
, 1 << st_attachment
);
1502 res
= xmesa_get_attachment(stfbi
, st_attachment
);
1505 struct pipe_context
* pipe
= xmesa_get_context(stfbi
);
1506 enum pipe_format internal_format
= res
->format
;
1507 struct pipe_transfer
*tex_xfer
;
1509 int line
, byte_width
;
1512 internal_format
= choose_pixel_format(drawable
->xm_visual
);
1514 map
= pipe_transfer_map(pipe
, res
,
1515 0, 0, /* level, layer */
1516 PIPE_TRANSFER_WRITE
,
1522 /* Grab the XImage that we want to turn into a texture. */
1523 img
= XGetImage(dpy
,
1524 drawable
->ws
.drawable
,
1531 pipe_transfer_unmap(pipe
, tex_xfer
);
1535 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */
1536 byte_width
= w
* ((img
->bits_per_pixel
+ 7) / 8);
1538 for (line
= 0; line
< h
; line
++)
1539 memcpy(&map
[line
* tex_xfer
->stride
],
1540 &img
->data
[line
* img
->bytes_per_line
],
1543 pipe_transfer_unmap(pipe
, tex_xfer
);
1550 FALSE
/* no mipmap */);
1558 XMesaReleaseTexImage(Display
*dpy
, XMesaBuffer drawable
, int buffer
)
1564 XMesaCopyContext(XMesaContext src
, XMesaContext dst
, unsigned long mask
)
1567 dst
->st
->copy(dst
->st
, src
->st
, mask
);