2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * All the XMesa* API functions.
33 * The window coordinate system origin (0,0) is in the lower-left corner
34 * of the window. X11's window coordinate origin is in the upper-left
35 * corner of the window. Therefore, most drawing functions in this
36 * file have to flip Y coordinates.
38 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
39 * in support for the MIT Shared Memory extension. If enabled, when you
40 * use an Ximage for the back buffer in double buffered mode, the "swap"
41 * operation will be faster. You must also link with -lXext.
43 * Byte swapping: If the Mesa host and the X display use a different
44 * byte order then there's some trickiness to be aware of when using
45 * XImages. The byte ordering used for the XImage is that of the X
46 * display, not the Mesa host.
47 * The color-to-pixel encoding for True/DirectColor must be done
48 * according to the display's visual red_mask, green_mask, and blue_mask.
49 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
50 * do byte swapping if needed. If one wants to directly "poke" the pixel
51 * into the XImage's buffer then the pixel must be byte swapped first.
60 #include "glxheader.h"
63 #include "main/context.h"
64 #include "main/framebuffer.h"
65 #include "glapi/glthread.h"
67 #include "state_tracker/st_public.h"
68 #include "state_tracker/st_context.h"
69 #include "pipe/p_defines.h"
70 #include "pipe/p_context.h"
72 #include "xm_winsys_aub.h"
75 * Global X driver lock
77 _glthread_Mutex _xmesa_lock
;
83 /**********************************************************************/
84 /***** X Utility Functions *****/
85 /**********************************************************************/
89 * Return the host's byte order as LSBFirst or MSBFirst ala X.
92 static int host_byte_order( void )
95 char *cptr
= (char *) &i
;
96 return (*cptr
==1) ? LSBFirst
: MSBFirst
;
102 * Check if the X Shared Memory extension is available.
103 * Return: 0 = not available
104 * 1 = shared XImage support available
105 * 2 = shared Pixmap support available also
107 static int check_for_xshm( XMesaDisplay
*display
)
109 #if defined(USE_XSHM) && !defined(XFree86Server)
110 int major
, minor
, ignore
;
113 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
114 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
115 return (pixmaps
==True
) ? 2 : 1;
125 /* No XSHM support */
132 * Return the true number of bits per pixel for XImages.
133 * For example, if we request a 24-bit deep visual we may actually need/get
134 * 32bpp XImages. This function returns the appropriate bpp.
135 * Input: dpy - the X display
136 * visinfo - desribes the visual to be used for XImages
137 * Return: true number of bits per pixel for XImages
140 bits_per_pixel( XMesaVisual xmv
)
143 const int depth
= xmv
->nplanes
;
146 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
147 if (screenInfo
.formats
[i
].depth
== depth
)
148 return screenInfo
.formats
[i
].bitsPerPixel
;
150 return depth
; /* should never get here, but this should be safe */
152 XMesaDisplay
*dpy
= xmv
->display
;
153 XMesaVisualInfo visinfo
= xmv
->visinfo
;
156 /* Create a temporary XImage */
157 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
158 ZPixmap
, 0, /*format, offset*/
159 (char*) MALLOC(8), /*data*/
160 1, 1, /*width, height*/
165 /* grab the bits/pixel value */
166 bitsPerPixel
= img
->bits_per_pixel
;
167 /* free the XImage */
168 _mesa_free( img
->data
);
170 XMesaDestroyImage( img
);
178 * Determine if a given X window ID is valid (window exists).
179 * Do this by calling XGetWindowAttributes() for the window and
180 * checking if we catch an X error.
181 * Input: dpy - the display
182 * win - the window to check for existance
183 * Return: GL_TRUE - window exists
184 * GL_FALSE - window doesn't exist
186 #ifndef XFree86Server
187 static GLboolean WindowExistsFlag
;
189 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
192 if (xerr
->error_code
== BadWindow
) {
193 WindowExistsFlag
= GL_FALSE
;
198 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
200 XWindowAttributes wa
;
201 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
202 WindowExistsFlag
= GL_TRUE
;
203 old_handler
= XSetErrorHandler(window_exists_err_handler
);
204 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
205 XSetErrorHandler(old_handler
);
206 return WindowExistsFlag
;
210 get_drawable_size( XMesaDisplay
*dpy
, Drawable d
, uint
*width
, uint
*height
)
215 unsigned int w
, h
, bw
, depth
;
216 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
225 * Return the size of the window (or pixmap) that corresponds to the
227 * \param width returns width in pixels
228 * \param height returns height in pixels
231 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
232 GLuint
*width
, GLuint
*height
)
235 *width
= MIN2(b
->drawable
->width
, MAX_WIDTH
);
236 *height
= MIN2(b
->drawable
->height
, MAX_HEIGHT
);
240 _glthread_LOCK_MUTEX(_xmesa_lock
);
241 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
242 stat
= get_drawable_size(dpy
, b
->drawable
, width
, height
);
243 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
246 /* probably querying a window that's recently been destroyed */
247 _mesa_warning(NULL
, "XGetGeometry failed!\n");
248 *width
= *height
= 1;
255 * Choose the pixel format for the given visual.
256 * This will tell the gallium driver how to pack pixel data into
260 choose_pixel_format(XMesaVisual v
)
262 if ( GET_REDMASK(v
) == 0x0000ff
263 && GET_GREENMASK(v
) == 0x00ff00
264 && GET_BLUEMASK(v
) == 0xff0000
265 && v
->BitsPerPixel
== 32) {
266 if (CHECK_BYTE_ORDER(v
)) {
267 /* no byteswapping needed */
268 return 0 /* PIXEL_FORMAT_U_A8_B8_G8_R8 */;
271 return PIPE_FORMAT_R8G8B8A8_UNORM
;
274 else if ( GET_REDMASK(v
) == 0xff0000
275 && GET_GREENMASK(v
) == 0x00ff00
276 && GET_BLUEMASK(v
) == 0x0000ff
277 && v
->BitsPerPixel
== 32) {
278 if (CHECK_BYTE_ORDER(v
)) {
279 /* no byteswapping needed */
280 return PIPE_FORMAT_A8R8G8B8_UNORM
;
283 return PIPE_FORMAT_B8G8R8A8_UNORM
;
286 else if ( GET_REDMASK(v
) == 0xf800
287 && GET_GREENMASK(v
) == 0x07e0
288 && GET_BLUEMASK(v
) == 0x001f
289 && CHECK_BYTE_ORDER(v
)
290 && v
->BitsPerPixel
== 16) {
292 return PIPE_FORMAT_R5G6B5_UNORM
;
301 /**********************************************************************/
302 /***** Linked list of XMesaBuffers *****/
303 /**********************************************************************/
305 XMesaBuffer XMesaBufferList
= NULL
;
309 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
310 * Note that XMesaBuffer is derived from GLframebuffer.
311 * The new XMesaBuffer will not have any size (Width=Height=0).
313 * \param d the corresponding X drawable (window or pixmap)
314 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
315 * \param vis the buffer's visual
316 * \param cmap the window's colormap, if known.
317 * \return new XMesaBuffer or NULL if any problem
320 create_xmesa_buffer(XMesaDrawable d
, BufferType type
,
321 XMesaVisual vis
, XMesaColormap cmap
)
325 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
328 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
330 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
340 /* determine PIPE_FORMATs for buffers */
341 colorFormat
= choose_pixel_format(vis
);
343 if (vis
->mesa_visual
.depthBits
== 0)
344 depthFormat
= PIPE_FORMAT_NONE
;
345 else if (vis
->mesa_visual
.depthBits
<= 16)
346 depthFormat
= PIPE_FORMAT_Z16_UNORM
;
347 else if (vis
->mesa_visual
.depthBits
<= 24)
348 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
350 depthFormat
= PIPE_FORMAT_Z32_UNORM
;
352 if (vis
->mesa_visual
.stencilBits
== 8) {
353 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
)
354 stencilFormat
= depthFormat
;
356 stencilFormat
= PIPE_FORMAT_S8_UNORM
;
359 stencilFormat
= PIPE_FORMAT_NONE
;
363 get_drawable_size(vis
->display
, d
, &width
, &height
);
366 * Create framebuffer, but we'll plug in our own renderbuffers below.
368 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
369 colorFormat
, depthFormat
, stencilFormat
,
375 * Create scratch XImage for xmesa_display_surface()
377 b
->tempImage
= XCreateImage(vis
->display
,
378 vis
->visinfo
->visual
,
380 ZPixmap
, 0, /* format, offset */
384 0); /* bytes_per_line */
386 /* GLX_EXT_texture_from_pixmap */
387 b
->TextureTarget
= 0;
388 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
389 b
->TextureMipmap
= 0;
391 /* insert buffer into linked list */
392 b
->Next
= XMesaBufferList
;
400 * Find an XMesaBuffer by matching X display and colormap but NOT matching
401 * the notThis buffer.
404 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
407 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
408 if (b
->xm_visual
->display
== dpy
&&
419 * Remove buffer from linked list, delete if no longer referenced.
422 xmesa_free_buffer(XMesaBuffer buffer
)
424 XMesaBuffer prev
= NULL
, b
;
426 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
428 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
430 /* unlink buffer from list */
432 prev
->Next
= buffer
->Next
;
434 XMesaBufferList
= buffer
->Next
;
436 /* mark as delete pending */
437 fb
->DeletePending
= GL_TRUE
;
439 /* Since the X window for the XMesaBuffer is going away, we don't
440 * want to dereference this pointer in the future.
444 buffer
->tempImage
->data
= NULL
;
445 XDestroyImage(buffer
->tempImage
);
447 /* Unreference. If count = zero we'll really delete the buffer */
448 _mesa_unreference_framebuffer(&fb
);
450 XFreeGC(b
->xm_visual
->display
, b
->gc
);
456 /* continue search */
459 /* buffer not found in XMesaBufferList */
460 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
465 /**********************************************************************/
466 /***** Misc Private Functions *****/
467 /**********************************************************************/
471 * When a context is bound for the first time, we can finally finish
472 * initializing the context's visual and buffer information.
473 * \param v the XMesaVisual to initialize
474 * \param b the XMesaBuffer to initialize (may be NULL)
475 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
476 * \param window the window/pixmap we're rendering into
477 * \param cmap the colormap associated with the window/pixmap
478 * \return GL_TRUE=success, GL_FALSE=failure
481 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
482 GLboolean rgb_flag
, XMesaDrawable window
,
486 int client
= (window
) ? CLIENT_ID(window
->id
) : 0;
489 ASSERT(!b
|| b
->xm_visual
== v
);
491 /* Save true bits/pixel */
492 v
->BitsPerPixel
= bits_per_pixel(v
);
493 assert(v
->BitsPerPixel
> 0);
495 if (rgb_flag
== GL_FALSE
) {
496 /* COLOR-INDEXED WINDOW: not supported*/
501 * We support RGB rendering into almost any kind of visual.
503 const int xclass
= v
->mesa_visual
.visualType
;
504 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
506 "XMesa: RGB mode rendering not supported in given visual.\n");
509 v
->mesa_visual
.indexBits
= 0;
513 * If MESA_INFO env var is set print out some debugging info
514 * which can help Brian figure out what's going on when a user
517 if (_mesa_getenv("MESA_INFO")) {
518 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
519 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
520 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
521 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
525 /* these should have been set in create_xmesa_buffer */
526 ASSERT(b
->drawable
== window
);
528 /* Setup for single/double buffering */
529 if (v
->mesa_visual
.doubleBufferMode
) {
530 /* Double buffered */
531 b
->shm
= check_for_xshm( v
->display
);
534 /* X11 graphics context */
536 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
538 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
540 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
548 #define NUM_VISUAL_TYPES 6
551 * Convert an X visual type to a GLX visual type.
553 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
555 * \return If \c visualType is a valid X visual type, a GLX visual type will
556 * be returned. Otherwise \c GLX_NONE will be returned.
559 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
563 xmesa_convert_from_x_visual_type( int visualType
)
565 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
566 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
567 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
568 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
571 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
572 ? glx_visual_types
[ visualType
] : GLX_NONE
;
576 /**********************************************************************/
577 /***** Public Functions *****/
578 /**********************************************************************/
582 * Create a new X/Mesa visual.
583 * Input: display - X11 display
584 * visinfo - an XVisualInfo pointer
585 * rgb_flag - GL_TRUE = RGB mode,
586 * GL_FALSE = color index mode
587 * alpha_flag - alpha buffer requested?
588 * db_flag - GL_TRUE = double-buffered,
589 * GL_FALSE = single buffered
590 * stereo_flag - stereo visual?
591 * ximage_flag - GL_TRUE = use an XImage for back buffer,
592 * GL_FALSE = use an off-screen pixmap for back buffer
593 * depth_size - requested bits/depth values, or zero
594 * stencil_size - requested bits/stencil values, or zero
595 * accum_red_size - requested bits/red accum values, or zero
596 * accum_green_size - requested bits/green accum values, or zero
597 * accum_blue_size - requested bits/blue accum values, or zero
598 * accum_alpha_size - requested bits/alpha accum values, or zero
599 * num_samples - number of samples/pixel if multisampling, or zero
600 * level - visual level, usually 0
601 * visualCaveat - ala the GLX extension, usually GLX_NONE
602 * Return; a new XMesaVisual or 0 if error.
605 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
606 XMesaVisualInfo visinfo
,
608 GLboolean alpha_flag
,
610 GLboolean stereo_flag
,
611 GLboolean ximage_flag
,
614 GLint accum_red_size
,
615 GLint accum_green_size
,
616 GLint accum_blue_size
,
617 GLint accum_alpha_size
,
623 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
625 #ifndef XFree86Server
626 /* For debugging only */
627 if (_mesa_getenv("MESA_XSYNC")) {
628 /* This makes debugging X easier.
629 * In your debugger, set a breakpoint on _XError to stop when an
630 * X protocol error is generated.
632 XSynchronize( display
, 1 );
636 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
641 v
->display
= display
;
643 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
644 * the struct but we may need some of the information contained in it
647 #ifndef XFree86Server
648 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
653 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
656 v
->ximage_flag
= ximage_flag
;
659 /* We could calculate these values by ourselves. nplanes is either the sum
660 * of the red, green, and blue bits or the number index bits.
661 * ColormapEntries is either (1U << index_bits) or
662 * (1U << max(redBits, greenBits, blueBits)).
664 assert(visinfo
->nplanes
> 0);
665 v
->nplanes
= visinfo
->nplanes
;
666 v
->ColormapEntries
= visinfo
->ColormapEntries
;
668 v
->mesa_visual
.redMask
= visinfo
->redMask
;
669 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
670 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
671 v
->mesa_visual
.visualID
= visinfo
->vid
;
672 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
674 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
675 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
676 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
677 v
->mesa_visual
.visualID
= visinfo
->visualid
;
678 v
->mesa_visual
.screen
= visinfo
->screen
;
681 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
682 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
684 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
687 v
->mesa_visual
.visualRating
= visualCaveat
;
690 v
->mesa_visual
.alphaBits
= 8;
692 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
695 const int xclass
= v
->mesa_visual
.visualType
;
696 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
697 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
698 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
699 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
702 /* this is an approximation */
704 depth
= GET_VISUAL_DEPTH(v
);
705 red_bits
= depth
/ 3;
707 green_bits
= depth
/ 2;
711 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
713 alpha_bits
= v
->mesa_visual
.alphaBits
;
716 _mesa_initialize_visual( &v
->mesa_visual
,
717 rgb_flag
, db_flag
, stereo_flag
,
718 red_bits
, green_bits
,
719 blue_bits
, alpha_bits
,
720 v
->mesa_visual
.indexBits
,
723 accum_red_size
, accum_green_size
,
724 accum_blue_size
, accum_alpha_size
,
728 v
->mesa_visual
.level
= level
;
734 void XMesaDestroyVisual( XMesaVisual v
)
736 #ifndef XFree86Server
737 _mesa_free(v
->visinfo
);
745 * Create a new XMesaContext.
746 * \param v the XMesaVisual
747 * \param share_list another XMesaContext with which to share display
748 * lists or NULL if no sharing is wanted.
749 * \return an XMesaContext or NULL if error.
752 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
754 static GLboolean firstTime
= GL_TRUE
;
755 struct pipe_context
*pipe
;
761 _glthread_INIT_MUTEX(_xmesa_lock
);
762 firstTime
= GL_FALSE
;
765 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
766 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
770 pf
= choose_pixel_format(v
);
773 if (!getenv("XM_AUB")) {
774 xmesa_mode
= XMESA_SOFTPIPE
;
775 pipe
= xmesa_create_pipe_context( c
, pf
);
778 xmesa_mode
= XMESA_AUB
;
779 pipe
= xmesa_create_i965simple( xmesa_get_pipe_winsys_aub() );
782 c
->st
= st_create_context(pipe
, &v
->mesa_visual
,
783 share_list
? share_list
->st
: NULL
);
784 mesaCtx
= c
->st
->ctx
;
785 c
->st
->ctx
->DriverCtx
= c
;
788 _mesa_enable_sw_extensions(mesaCtx
);
789 _mesa_enable_1_3_extensions(mesaCtx
);
790 _mesa_enable_1_4_extensions(mesaCtx
);
791 _mesa_enable_1_5_extensions(mesaCtx
);
792 _mesa_enable_2_0_extensions(mesaCtx
);
796 /* If we're running in the X server, do bounds checking to prevent
797 * segfaults and server crashes!
799 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
802 /* finish up xmesa context initializations */
804 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
806 c
->st
->haveFramebufferSurfaces
= GL_TRUE
;
814 void XMesaDestroyContext( XMesaContext c
)
816 st_destroy_context(c
->st
);
823 * Private function for creating an XMesaBuffer which corresponds to an
824 * X window or pixmap.
825 * \param v the window's XMesaVisual
826 * \param w the window we're wrapping
827 * \return new XMesaBuffer or NULL if error
830 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
832 #ifndef XFree86Server
833 XWindowAttributes attr
;
842 /* Check that window depth matches visual depth */
844 depth
= ((XMesaDrawable
)w
)->depth
;
846 XGetWindowAttributes( v
->display
, w
, &attr
);
849 if (GET_VISUAL_DEPTH(v
) != depth
) {
850 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
851 GET_VISUAL_DEPTH(v
), depth
);
857 cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
860 cmap
= attr
.colormap
;
863 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
864 /* this is weird, a window w/out a colormap!? */
865 /* OK, let's just allocate a new one and hope for the best */
866 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
870 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
874 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
875 (XMesaDrawable
) w
, cmap
)) {
876 xmesa_free_buffer(b
);
886 * Create a new XMesaBuffer from an X pixmap.
888 * \param v the XMesaVisual
889 * \param p the pixmap
890 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
891 * \c GLX_DIRECT_COLOR visual for the pixmap
892 * \returns new XMesaBuffer or NULL if error
895 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
901 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
905 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
906 (XMesaDrawable
) p
, cmap
)) {
907 xmesa_free_buffer(b
);
916 * For GLX_EXT_texture_from_pixmap
919 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
921 int format
, int target
, int mipmap
)
923 GET_CURRENT_CONTEXT(ctx
);
925 GLuint width
, height
;
929 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
933 /* get pixmap size, update framebuffer/renderbuffer dims */
934 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
935 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
939 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
940 target
= GLX_TEXTURE_2D_EXT
;
942 else if ( _mesa_bitcount(width
) == 1
943 && _mesa_bitcount(height
) == 1) {
944 /* power of two size */
946 target
= GLX_TEXTURE_1D_EXT
;
949 target
= GLX_TEXTURE_2D_EXT
;
952 else if (ctx
->Extensions
.NV_texture_rectangle
) {
953 target
= GLX_TEXTURE_RECTANGLE_EXT
;
956 /* non power of two textures not supported */
957 XMesaDestroyBuffer(b
);
962 b
->TextureTarget
= target
;
963 b
->TextureFormat
= format
;
964 b
->TextureMipmap
= mipmap
;
966 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
967 (XMesaDrawable
) p
, cmap
)) {
968 xmesa_free_buffer(b
);
978 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
979 unsigned int width
, unsigned int height
)
981 #ifndef XFree86Server
983 XMesaDrawable drawable
; /* X Pixmap Drawable */
986 /* allocate pixmap for front buffer */
987 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
988 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
993 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
997 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
999 xmesa_free_buffer(b
);
1012 * Deallocate an XMesaBuffer structure and all related info.
1015 XMesaDestroyBuffer(XMesaBuffer b
)
1017 xmesa_free_buffer(b
);
1022 * Query the current window size and update the corresponding GLframebuffer
1023 * and all attached renderbuffers.
1025 * 1. the first time a buffer is bound to a context.
1026 * 2. from the XMesaResizeBuffers() API function.
1027 * 3. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1028 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1029 * when resizing a buffer when no rendering context is bound.
1032 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1034 GLuint width
, height
;
1035 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1036 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1041 * Bind buffer b to context c and make c the current rendering context.
1043 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1045 return XMesaMakeCurrent2( c
, b
, b
);
1050 * Bind buffer b to context c and make c the current rendering context.
1053 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1054 XMesaBuffer readBuffer
)
1057 if (!drawBuffer
|| !readBuffer
)
1058 return GL_FALSE
; /* must specify buffers! */
1061 /* XXX restore this optimization */
1062 if (&(c
->mesa
) == _mesa_get_current_context()
1063 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1064 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1065 && xmesa_buffer(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1066 /* same context and buffer, do nothing */
1071 c
->xm_buffer
= drawBuffer
;
1073 /* Call this periodically to detect when the user has begun using
1074 * GL rendering from multiple threads.
1076 _glapi_check_multithread();
1078 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1080 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1081 if (readBuffer
!= drawBuffer
)
1082 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1084 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1085 drawBuffer
->wasCurrent
= GL_TRUE
;
1089 st_make_current( NULL
, NULL
, NULL
);
1096 * Unbind the context c from its buffer.
1098 GLboolean
XMesaUnbindContext( XMesaContext c
)
1100 /* A no-op for XFree86 integration purposes */
1105 XMesaContext
XMesaGetCurrentContext( void )
1107 GET_CURRENT_CONTEXT(ctx
);
1109 XMesaContext xmesa
= xmesa_context(ctx
);
1118 XMesaBuffer
XMesaGetCurrentBuffer( void )
1120 GET_CURRENT_CONTEXT(ctx
);
1122 XMesaBuffer xmbuf
= xmesa_buffer(ctx
->DrawBuffer
);
1131 /* New in Mesa 3.1 */
1132 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1134 GET_CURRENT_CONTEXT(ctx
);
1136 return xmesa_buffer(ctx
->ReadBuffer
);
1144 #ifdef XFree86Server
1146 GLboolean
XMesaForceCurrent(XMesaContext c
)
1149 _glapi_set_dispatch(c
->mesa
.CurrentDispatch
);
1151 if (&(c
->mesa
) != _mesa_get_current_context()) {
1152 _mesa_make_current(&c
->mesa
, c
->mesa
.DrawBuffer
, c
->mesa
.ReadBuffer
);
1156 _mesa_make_current(NULL
, NULL
, NULL
);
1163 GLboolean
XMesaLoseCurrent(XMesaContext c
)
1166 _mesa_make_current(NULL
, NULL
, NULL
);
1172 GLboolean
XMesaCopyContext( XMesaContext xm_src
, XMesaContext xm_dst
, GLuint mask
)
1174 _mesa_copy_context(&xm_src
->mesa
, &xm_dst
->mesa
, mask
);
1177 #endif /* XFree86Server */
1181 GLboolean
XMesaSetFXmode( GLint mode
)
1191 * Copy the back buffer to the front buffer. If there's no back buffer
1195 void XMesaSwapBuffers( XMesaBuffer b
)
1197 struct pipe_surface
*surf
;
1199 /* If we're swapping the buffer associated with the current context
1200 * we have to flush any pending rendering commands first.
1202 st_notify_swapbuffers(b
->stfb
);
1204 surf
= st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1206 if (xmesa_mode
== XMESA_AUB
)
1207 xmesa_display_aub( surf
);
1209 xmesa_display_surface(b
, surf
);
1212 xmesa_check_and_update_buffer_size(NULL
, b
);
1218 * Copy sub-region of back buffer to front buffer
1220 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1222 struct pipe_surface
*surf_front
1223 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
);
1224 struct pipe_surface
*surf_back
1225 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1226 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1228 if (!surf_front
|| !surf_back
)
1231 pipe
->surface_copy(pipe
,
1232 surf_front
, x
, y
, /* dest */
1233 surf_back
, x
, y
, /* src */
1240 * Return the depth buffer associated with an XMesaBuffer.
1241 * Input: b - the XMesa buffer handle
1242 * Output: width, height - size of buffer in pixels
1243 * bytesPerValue - bytes per depth value (2 or 4)
1244 * buffer - pointer to depth buffer values
1245 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1247 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
1248 GLint
*bytesPerValue
, void **buffer
)
1258 void XMesaFlush( XMesaContext c
)
1260 if (c
&& c
->xm_visual
->display
) {
1261 #ifdef XFree86Server
1264 XSync( c
->xm_visual
->display
, False
);
1271 const char *XMesaGetString( XMesaContext c
, int name
)
1274 if (name
==XMESA_VERSION
) {
1277 else if (name
==XMESA_EXTENSIONS
) {
1287 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
1290 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
1291 if (b
->drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1300 * Free/destroy all XMesaBuffers associated with given display.
1302 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
1304 XMesaBuffer b
, next
;
1305 for (b
= XMesaBufferList
; b
; b
= next
) {
1307 if (b
->xm_visual
->display
== dpy
) {
1308 xmesa_free_buffer(b
);
1315 * Look for XMesaBuffers whose X window has been destroyed.
1316 * Deallocate any such XMesaBuffers.
1318 void XMesaGarbageCollect( void )
1320 XMesaBuffer b
, next
;
1321 for (b
=XMesaBufferList
; b
; b
=next
) {
1324 b
->xm_visual
->display
&&
1326 b
->type
== WINDOW
) {
1327 #ifdef XFree86Server
1330 XSync(b
->xm_visual
->display
, False
);
1331 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1332 /* found a dead window, free the ancillary info */
1333 XMesaDestroyBuffer( b
);
1341 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
1342 GLfloat red
, GLfloat green
,
1343 GLfloat blue
, GLfloat alpha
)
1345 /* no longer supported */
1351 * This is typically called when the window size changes and we need
1352 * to reallocate the buffer's back/depth/stencil/accum buffers.
1355 XMesaResizeBuffers( XMesaBuffer b
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 XMesaContext xmctx
= xmesa_context(ctx
);
1361 xmesa_check_and_update_buffer_size(xmctx
, b
);
1368 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
1369 const int *attrib_list
)
1376 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)