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 int xmesa_check_for_xshm( XMesaDisplay
*display
)
109 #if defined(USE_XSHM) && !defined(XFree86Server)
110 int major
, minor
, ignore
;
113 if (getenv("SP_NO_RAST"))
116 if (getenv("MESA_NOSHM")) {
120 if (XQueryExtension( display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
121 if (XShmQueryVersion( display
, &major
, &minor
, &pixmaps
)==True
) {
122 return (pixmaps
==True
) ? 2 : 1;
132 /* No XSHM support */
139 * Return the true number of bits per pixel for XImages.
140 * For example, if we request a 24-bit deep visual we may actually need/get
141 * 32bpp XImages. This function returns the appropriate bpp.
142 * Input: dpy - the X display
143 * visinfo - desribes the visual to be used for XImages
144 * Return: true number of bits per pixel for XImages
147 bits_per_pixel( XMesaVisual xmv
)
150 const int depth
= xmv
->nplanes
;
153 for (i
= 0; i
< screenInfo
.numPixmapFormats
; i
++) {
154 if (screenInfo
.formats
[i
].depth
== depth
)
155 return screenInfo
.formats
[i
].bitsPerPixel
;
157 return depth
; /* should never get here, but this should be safe */
159 XMesaDisplay
*dpy
= xmv
->display
;
160 XMesaVisualInfo visinfo
= xmv
->visinfo
;
163 /* Create a temporary XImage */
164 img
= XCreateImage( dpy
, visinfo
->visual
, visinfo
->depth
,
165 ZPixmap
, 0, /*format, offset*/
166 (char*) MALLOC(8), /*data*/
167 1, 1, /*width, height*/
172 /* grab the bits/pixel value */
173 bitsPerPixel
= img
->bits_per_pixel
;
174 /* free the XImage */
175 _mesa_free( img
->data
);
177 XMesaDestroyImage( img
);
185 * Determine if a given X window ID is valid (window exists).
186 * Do this by calling XGetWindowAttributes() for the window and
187 * checking if we catch an X error.
188 * Input: dpy - the display
189 * win - the window to check for existance
190 * Return: GL_TRUE - window exists
191 * GL_FALSE - window doesn't exist
193 #ifndef XFree86Server
194 static GLboolean WindowExistsFlag
;
196 static int window_exists_err_handler( XMesaDisplay
* dpy
, XErrorEvent
* xerr
)
199 if (xerr
->error_code
== BadWindow
) {
200 WindowExistsFlag
= GL_FALSE
;
205 static GLboolean
window_exists( XMesaDisplay
*dpy
, Window win
)
207 XWindowAttributes wa
;
208 int (*old_handler
)( XMesaDisplay
*, XErrorEvent
* );
209 WindowExistsFlag
= GL_TRUE
;
210 old_handler
= XSetErrorHandler(window_exists_err_handler
);
211 XGetWindowAttributes( dpy
, win
, &wa
); /* dummy request */
212 XSetErrorHandler(old_handler
);
213 return WindowExistsFlag
;
217 get_drawable_size( XMesaDisplay
*dpy
, Drawable d
, uint
*width
, uint
*height
)
222 unsigned int w
, h
, bw
, depth
;
223 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
232 * Return the size of the window (or pixmap) that corresponds to the
234 * \param width returns width in pixels
235 * \param height returns height in pixels
238 xmesa_get_window_size(XMesaDisplay
*dpy
, XMesaBuffer b
,
239 GLuint
*width
, GLuint
*height
)
242 *width
= MIN2(b
->drawable
->width
, MAX_WIDTH
);
243 *height
= MIN2(b
->drawable
->height
, MAX_HEIGHT
);
247 _glthread_LOCK_MUTEX(_xmesa_lock
);
248 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
249 stat
= get_drawable_size(dpy
, b
->drawable
, width
, height
);
250 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
253 /* probably querying a window that's recently been destroyed */
254 _mesa_warning(NULL
, "XGetGeometry failed!\n");
255 *width
= *height
= 1;
262 * Choose the pixel format for the given visual.
263 * This will tell the gallium driver how to pack pixel data into
267 choose_pixel_format(XMesaVisual v
)
269 if ( GET_REDMASK(v
) == 0x0000ff
270 && GET_GREENMASK(v
) == 0x00ff00
271 && GET_BLUEMASK(v
) == 0xff0000
272 && v
->BitsPerPixel
== 32) {
273 if (CHECK_BYTE_ORDER(v
)) {
274 /* no byteswapping needed */
275 return 0 /* PIXEL_FORMAT_U_A8_B8_G8_R8 */;
278 return PIPE_FORMAT_R8G8B8A8_UNORM
;
281 else if ( GET_REDMASK(v
) == 0xff0000
282 && GET_GREENMASK(v
) == 0x00ff00
283 && GET_BLUEMASK(v
) == 0x0000ff
284 && v
->BitsPerPixel
== 32) {
285 if (CHECK_BYTE_ORDER(v
)) {
286 /* no byteswapping needed */
287 return PIPE_FORMAT_A8R8G8B8_UNORM
;
290 return PIPE_FORMAT_B8G8R8A8_UNORM
;
293 else if ( GET_REDMASK(v
) == 0xf800
294 && GET_GREENMASK(v
) == 0x07e0
295 && GET_BLUEMASK(v
) == 0x001f
296 && CHECK_BYTE_ORDER(v
)
297 && v
->BitsPerPixel
== 16) {
299 return PIPE_FORMAT_R5G6B5_UNORM
;
308 /**********************************************************************/
309 /***** Linked list of XMesaBuffers *****/
310 /**********************************************************************/
312 XMesaBuffer XMesaBufferList
= NULL
;
316 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
317 * Note that XMesaBuffer is derived from GLframebuffer.
318 * The new XMesaBuffer will not have any size (Width=Height=0).
320 * \param d the corresponding X drawable (window or pixmap)
321 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
322 * \param vis the buffer's visual
323 * \param cmap the window's colormap, if known.
324 * \return new XMesaBuffer or NULL if any problem
327 create_xmesa_buffer(XMesaDrawable d
, BufferType type
,
328 XMesaVisual vis
, XMesaColormap cmap
)
332 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
335 ASSERT(type
== WINDOW
|| type
== PIXMAP
|| type
== PBUFFER
);
337 b
= (XMesaBuffer
) CALLOC_STRUCT(xmesa_buffer
);
347 /* determine PIPE_FORMATs for buffers */
348 colorFormat
= choose_pixel_format(vis
);
350 if (vis
->mesa_visual
.depthBits
== 0)
351 depthFormat
= PIPE_FORMAT_NONE
;
352 else if (vis
->mesa_visual
.depthBits
<= 16)
353 depthFormat
= PIPE_FORMAT_Z16_UNORM
;
354 else if (vis
->mesa_visual
.depthBits
<= 24)
355 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
357 depthFormat
= PIPE_FORMAT_Z32_UNORM
;
359 if (vis
->mesa_visual
.stencilBits
== 8) {
360 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
)
361 stencilFormat
= depthFormat
;
363 stencilFormat
= PIPE_FORMAT_S8_UNORM
;
366 stencilFormat
= PIPE_FORMAT_NONE
;
370 get_drawable_size(vis
->display
, d
, &width
, &height
);
373 * Create framebuffer, but we'll plug in our own renderbuffers below.
375 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
376 colorFormat
, depthFormat
, stencilFormat
,
382 * Create scratch XImage for xmesa_display_surface()
384 b
->tempImage
= XCreateImage(vis
->display
,
385 vis
->visinfo
->visual
,
387 ZPixmap
, 0, /* format, offset */
391 0); /* bytes_per_line */
393 /* GLX_EXT_texture_from_pixmap */
394 b
->TextureTarget
= 0;
395 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
396 b
->TextureMipmap
= 0;
398 /* insert buffer into linked list */
399 b
->Next
= XMesaBufferList
;
407 * Find an XMesaBuffer by matching X display and colormap but NOT matching
408 * the notThis buffer.
411 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
414 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
415 if (b
->xm_visual
->display
== dpy
&&
426 * Remove buffer from linked list, delete if no longer referenced.
429 xmesa_free_buffer(XMesaBuffer buffer
)
431 XMesaBuffer prev
= NULL
, b
;
433 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
435 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
437 /* unlink buffer from list */
439 prev
->Next
= buffer
->Next
;
441 XMesaBufferList
= buffer
->Next
;
443 /* mark as delete pending */
444 fb
->DeletePending
= GL_TRUE
;
446 /* Since the X window for the XMesaBuffer is going away, we don't
447 * want to dereference this pointer in the future.
451 buffer
->tempImage
->data
= NULL
;
452 XDestroyImage(buffer
->tempImage
);
454 /* Unreference. If count = zero we'll really delete the buffer */
455 _mesa_unreference_framebuffer(&fb
);
457 XFreeGC(b
->xm_visual
->display
, b
->gc
);
463 /* continue search */
466 /* buffer not found in XMesaBufferList */
467 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
472 /**********************************************************************/
473 /***** Misc Private Functions *****/
474 /**********************************************************************/
478 * When a context is bound for the first time, we can finally finish
479 * initializing the context's visual and buffer information.
480 * \param v the XMesaVisual to initialize
481 * \param b the XMesaBuffer to initialize (may be NULL)
482 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
483 * \param window the window/pixmap we're rendering into
484 * \param cmap the colormap associated with the window/pixmap
485 * \return GL_TRUE=success, GL_FALSE=failure
488 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
489 GLboolean rgb_flag
, XMesaDrawable window
,
493 int client
= (window
) ? CLIENT_ID(window
->id
) : 0;
496 ASSERT(!b
|| b
->xm_visual
== v
);
498 /* Save true bits/pixel */
499 v
->BitsPerPixel
= bits_per_pixel(v
);
500 assert(v
->BitsPerPixel
> 0);
502 if (rgb_flag
== GL_FALSE
) {
503 /* COLOR-INDEXED WINDOW: not supported*/
508 * We support RGB rendering into almost any kind of visual.
510 const int xclass
= v
->mesa_visual
.visualType
;
511 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
513 "XMesa: RGB mode rendering not supported in given visual.\n");
516 v
->mesa_visual
.indexBits
= 0;
518 if (v
->BitsPerPixel
== 32) {
519 /* We use XImages for all front/back buffers. If an X Window or
520 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
521 * will be preserved. For XImages we're in luck.
523 v
->mesa_visual
.alphaBits
= 8;
528 * If MESA_INFO env var is set print out some debugging info
529 * which can help Brian figure out what's going on when a user
532 if (_mesa_getenv("MESA_INFO")) {
533 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
534 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
535 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
536 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
540 /* these should have been set in create_xmesa_buffer */
541 ASSERT(b
->drawable
== window
);
543 /* Setup for single/double buffering */
544 if (v
->mesa_visual
.doubleBufferMode
) {
545 /* Double buffered */
546 b
->shm
= xmesa_check_for_xshm( v
->display
);
549 /* X11 graphics context */
551 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
553 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
555 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
563 #define NUM_VISUAL_TYPES 6
566 * Convert an X visual type to a GLX visual type.
568 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
570 * \return If \c visualType is a valid X visual type, a GLX visual type will
571 * be returned. Otherwise \c GLX_NONE will be returned.
574 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
578 xmesa_convert_from_x_visual_type( int visualType
)
580 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
581 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
582 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
583 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
586 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
587 ? glx_visual_types
[ visualType
] : GLX_NONE
;
591 /**********************************************************************/
592 /***** Public Functions *****/
593 /**********************************************************************/
597 * Create a new X/Mesa visual.
598 * Input: display - X11 display
599 * visinfo - an XVisualInfo pointer
600 * rgb_flag - GL_TRUE = RGB mode,
601 * GL_FALSE = color index mode
602 * alpha_flag - alpha buffer requested?
603 * db_flag - GL_TRUE = double-buffered,
604 * GL_FALSE = single buffered
605 * stereo_flag - stereo visual?
606 * ximage_flag - GL_TRUE = use an XImage for back buffer,
607 * GL_FALSE = use an off-screen pixmap for back buffer
608 * depth_size - requested bits/depth values, or zero
609 * stencil_size - requested bits/stencil values, or zero
610 * accum_red_size - requested bits/red accum values, or zero
611 * accum_green_size - requested bits/green accum values, or zero
612 * accum_blue_size - requested bits/blue accum values, or zero
613 * accum_alpha_size - requested bits/alpha accum values, or zero
614 * num_samples - number of samples/pixel if multisampling, or zero
615 * level - visual level, usually 0
616 * visualCaveat - ala the GLX extension, usually GLX_NONE
617 * Return; a new XMesaVisual or 0 if error.
620 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
621 XMesaVisualInfo visinfo
,
623 GLboolean alpha_flag
,
625 GLboolean stereo_flag
,
626 GLboolean ximage_flag
,
629 GLint accum_red_size
,
630 GLint accum_green_size
,
631 GLint accum_blue_size
,
632 GLint accum_alpha_size
,
638 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
640 #ifndef XFree86Server
641 /* For debugging only */
642 if (_mesa_getenv("MESA_XSYNC")) {
643 /* This makes debugging X easier.
644 * In your debugger, set a breakpoint on _XError to stop when an
645 * X protocol error is generated.
647 XSynchronize( display
, 1 );
651 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
656 v
->display
= display
;
658 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
659 * the struct but we may need some of the information contained in it
662 #ifndef XFree86Server
663 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
668 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
671 v
->ximage_flag
= ximage_flag
;
674 /* We could calculate these values by ourselves. nplanes is either the sum
675 * of the red, green, and blue bits or the number index bits.
676 * ColormapEntries is either (1U << index_bits) or
677 * (1U << max(redBits, greenBits, blueBits)).
679 assert(visinfo
->nplanes
> 0);
680 v
->nplanes
= visinfo
->nplanes
;
681 v
->ColormapEntries
= visinfo
->ColormapEntries
;
683 v
->mesa_visual
.redMask
= visinfo
->redMask
;
684 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
685 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
686 v
->mesa_visual
.visualID
= visinfo
->vid
;
687 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
689 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
690 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
691 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
692 v
->mesa_visual
.visualID
= visinfo
->visualid
;
693 v
->mesa_visual
.screen
= visinfo
->screen
;
696 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
697 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
699 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
702 v
->mesa_visual
.visualRating
= visualCaveat
;
705 v
->mesa_visual
.alphaBits
= 8;
707 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
710 const int xclass
= v
->mesa_visual
.visualType
;
711 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
712 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
713 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
714 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
717 /* this is an approximation */
719 depth
= GET_VISUAL_DEPTH(v
);
720 red_bits
= depth
/ 3;
722 green_bits
= depth
/ 2;
726 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
728 alpha_bits
= v
->mesa_visual
.alphaBits
;
731 _mesa_initialize_visual( &v
->mesa_visual
,
732 rgb_flag
, db_flag
, stereo_flag
,
733 red_bits
, green_bits
,
734 blue_bits
, alpha_bits
,
735 v
->mesa_visual
.indexBits
,
738 accum_red_size
, accum_green_size
,
739 accum_blue_size
, accum_alpha_size
,
743 v
->mesa_visual
.level
= level
;
749 void XMesaDestroyVisual( XMesaVisual v
)
751 #ifndef XFree86Server
752 _mesa_free(v
->visinfo
);
760 * Create a new XMesaContext.
761 * \param v the XMesaVisual
762 * \param share_list another XMesaContext with which to share display
763 * lists or NULL if no sharing is wanted.
764 * \return an XMesaContext or NULL if error.
767 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
769 static GLboolean firstTime
= GL_TRUE
;
770 struct pipe_context
*pipe
;
776 _glthread_INIT_MUTEX(_xmesa_lock
);
777 firstTime
= GL_FALSE
;
780 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
781 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
785 pf
= choose_pixel_format(v
);
789 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
791 if (!getenv("XM_AUB")) {
792 xmesa_mode
= XMESA_SOFTPIPE
;
793 pipe
= xmesa_create_pipe_context( c
, pf
);
796 xmesa_mode
= XMESA_AUB
;
797 pipe
= xmesa_create_i965simple(xmesa_get_pipe_winsys_aub(v
));
803 c
->st
= st_create_context(pipe
, &v
->mesa_visual
,
804 share_list
? share_list
->st
: NULL
);
808 mesaCtx
= c
->st
->ctx
;
809 c
->st
->ctx
->DriverCtx
= c
;
812 _mesa_enable_sw_extensions(mesaCtx
);
813 _mesa_enable_1_3_extensions(mesaCtx
);
814 _mesa_enable_1_4_extensions(mesaCtx
);
815 _mesa_enable_1_5_extensions(mesaCtx
);
816 _mesa_enable_2_0_extensions(mesaCtx
);
820 /* If we're running in the X server, do bounds checking to prevent
821 * segfaults and server crashes!
823 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
830 st_destroy_context(c
->st
);
840 void XMesaDestroyContext( XMesaContext c
)
842 st_destroy_context(c
->st
);
849 * Private function for creating an XMesaBuffer which corresponds to an
850 * X window or pixmap.
851 * \param v the window's XMesaVisual
852 * \param w the window we're wrapping
853 * \return new XMesaBuffer or NULL if error
856 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
858 #ifndef XFree86Server
859 XWindowAttributes attr
;
868 /* Check that window depth matches visual depth */
870 depth
= ((XMesaDrawable
)w
)->depth
;
872 XGetWindowAttributes( v
->display
, w
, &attr
);
875 if (GET_VISUAL_DEPTH(v
) != depth
) {
876 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
877 GET_VISUAL_DEPTH(v
), depth
);
883 cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
886 cmap
= attr
.colormap
;
889 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
890 /* this is weird, a window w/out a colormap!? */
891 /* OK, let's just allocate a new one and hope for the best */
892 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
896 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
900 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
901 (XMesaDrawable
) w
, cmap
)) {
902 xmesa_free_buffer(b
);
912 * Create a new XMesaBuffer from an X pixmap.
914 * \param v the XMesaVisual
915 * \param p the pixmap
916 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
917 * \c GLX_DIRECT_COLOR visual for the pixmap
918 * \returns new XMesaBuffer or NULL if error
921 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
927 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
931 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
932 (XMesaDrawable
) p
, cmap
)) {
933 xmesa_free_buffer(b
);
942 * For GLX_EXT_texture_from_pixmap
945 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
947 int format
, int target
, int mipmap
)
949 GET_CURRENT_CONTEXT(ctx
);
951 GLuint width
, height
;
955 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
959 /* get pixmap size, update framebuffer/renderbuffer dims */
960 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
961 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
965 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
966 target
= GLX_TEXTURE_2D_EXT
;
968 else if ( _mesa_bitcount(width
) == 1
969 && _mesa_bitcount(height
) == 1) {
970 /* power of two size */
972 target
= GLX_TEXTURE_1D_EXT
;
975 target
= GLX_TEXTURE_2D_EXT
;
978 else if (ctx
->Extensions
.NV_texture_rectangle
) {
979 target
= GLX_TEXTURE_RECTANGLE_EXT
;
982 /* non power of two textures not supported */
983 XMesaDestroyBuffer(b
);
988 b
->TextureTarget
= target
;
989 b
->TextureFormat
= format
;
990 b
->TextureMipmap
= mipmap
;
992 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
993 (XMesaDrawable
) p
, cmap
)) {
994 xmesa_free_buffer(b
);
1004 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1005 unsigned int width
, unsigned int height
)
1007 #ifndef XFree86Server
1009 XMesaDrawable drawable
; /* X Pixmap Drawable */
1012 /* allocate pixmap for front buffer */
1013 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1014 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1019 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1023 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1025 xmesa_free_buffer(b
);
1038 * Deallocate an XMesaBuffer structure and all related info.
1041 XMesaDestroyBuffer(XMesaBuffer b
)
1043 xmesa_free_buffer(b
);
1048 * Query the current window size and update the corresponding GLframebuffer
1049 * and all attached renderbuffers.
1051 * 1. the first time a buffer is bound to a context.
1052 * 2. from the XMesaResizeBuffers() API function.
1053 * 3. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1054 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1055 * when resizing a buffer when no rendering context is bound.
1058 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1060 GLuint width
, height
;
1061 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1062 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1067 * Bind buffer b to context c and make c the current rendering context.
1069 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1071 return XMesaMakeCurrent2( c
, b
, b
);
1076 * Bind buffer b to context c and make c the current rendering context.
1079 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1080 XMesaBuffer readBuffer
)
1083 if (!drawBuffer
|| !readBuffer
)
1084 return GL_FALSE
; /* must specify buffers! */
1087 /* XXX restore this optimization */
1088 if (&(c
->mesa
) == _mesa_get_current_context()
1089 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1090 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1091 && xmesa_buffer(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1092 /* same context and buffer, do nothing */
1097 c
->xm_buffer
= drawBuffer
;
1099 /* Call this periodically to detect when the user has begun using
1100 * GL rendering from multiple threads.
1102 _glapi_check_multithread();
1104 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1106 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1107 if (readBuffer
!= drawBuffer
)
1108 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1110 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1111 drawBuffer
->wasCurrent
= GL_TRUE
;
1115 st_make_current( NULL
, NULL
, NULL
);
1122 * Unbind the context c from its buffer.
1124 GLboolean
XMesaUnbindContext( XMesaContext c
)
1126 /* A no-op for XFree86 integration purposes */
1131 XMesaContext
XMesaGetCurrentContext( void )
1133 GET_CURRENT_CONTEXT(ctx
);
1135 XMesaContext xmesa
= xmesa_context(ctx
);
1144 XMesaBuffer
XMesaGetCurrentBuffer( void )
1146 GET_CURRENT_CONTEXT(ctx
);
1148 XMesaBuffer xmbuf
= xmesa_buffer(ctx
->DrawBuffer
);
1157 /* New in Mesa 3.1 */
1158 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1160 GET_CURRENT_CONTEXT(ctx
);
1162 return xmesa_buffer(ctx
->ReadBuffer
);
1170 #ifdef XFree86Server
1172 GLboolean
XMesaForceCurrent(XMesaContext c
)
1175 _glapi_set_dispatch(c
->mesa
.CurrentDispatch
);
1177 if (&(c
->mesa
) != _mesa_get_current_context()) {
1178 _mesa_make_current(&c
->mesa
, c
->mesa
.DrawBuffer
, c
->mesa
.ReadBuffer
);
1182 _mesa_make_current(NULL
, NULL
, NULL
);
1189 GLboolean
XMesaLoseCurrent(XMesaContext c
)
1192 _mesa_make_current(NULL
, NULL
, NULL
);
1198 GLboolean
XMesaCopyContext( XMesaContext xm_src
, XMesaContext xm_dst
, GLuint mask
)
1200 _mesa_copy_context(&xm_src
->mesa
, &xm_dst
->mesa
, mask
);
1203 #endif /* XFree86Server */
1207 GLboolean
XMesaSetFXmode( GLint mode
)
1217 * Copy the back buffer to the front buffer. If there's no back buffer
1221 void XMesaSwapBuffers( XMesaBuffer b
)
1223 struct pipe_surface
*surf
;
1225 /* If we're swapping the buffer associated with the current context
1226 * we have to flush any pending rendering commands first.
1228 st_notify_swapbuffers(b
->stfb
);
1230 surf
= st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1232 if (xmesa_mode
== XMESA_AUB
)
1233 xmesa_display_aub( surf
);
1235 xmesa_display_surface(b
, surf
);
1238 xmesa_check_and_update_buffer_size(NULL
, b
);
1244 * Copy sub-region of back buffer to front buffer
1246 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1248 struct pipe_surface
*surf_front
1249 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
);
1250 struct pipe_surface
*surf_back
1251 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1252 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1254 if (!surf_front
|| !surf_back
)
1257 pipe
->surface_copy(pipe
,
1259 surf_front
, x
, y
, /* dest */
1260 surf_back
, x
, y
, /* src */
1267 * Return the depth buffer associated with an XMesaBuffer.
1268 * Input: b - the XMesa buffer handle
1269 * Output: width, height - size of buffer in pixels
1270 * bytesPerValue - bytes per depth value (2 or 4)
1271 * buffer - pointer to depth buffer values
1272 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1274 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
1275 GLint
*bytesPerValue
, void **buffer
)
1285 void XMesaFlush( XMesaContext c
)
1287 if (c
&& c
->xm_visual
->display
) {
1288 #ifdef XFree86Server
1291 XSync( c
->xm_visual
->display
, False
);
1298 const char *XMesaGetString( XMesaContext c
, int name
)
1301 if (name
==XMESA_VERSION
) {
1304 else if (name
==XMESA_EXTENSIONS
) {
1314 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
1317 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
1318 if (b
->drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1327 * Free/destroy all XMesaBuffers associated with given display.
1329 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
1331 XMesaBuffer b
, next
;
1332 for (b
= XMesaBufferList
; b
; b
= next
) {
1334 if (b
->xm_visual
->display
== dpy
) {
1335 xmesa_free_buffer(b
);
1342 * Look for XMesaBuffers whose X window has been destroyed.
1343 * Deallocate any such XMesaBuffers.
1345 void XMesaGarbageCollect( void )
1347 XMesaBuffer b
, next
;
1348 for (b
=XMesaBufferList
; b
; b
=next
) {
1351 b
->xm_visual
->display
&&
1353 b
->type
== WINDOW
) {
1354 #ifdef XFree86Server
1357 XSync(b
->xm_visual
->display
, False
);
1358 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1359 /* found a dead window, free the ancillary info */
1360 XMesaDestroyBuffer( b
);
1368 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
1369 GLfloat red
, GLfloat green
,
1370 GLfloat blue
, GLfloat alpha
)
1372 /* no longer supported */
1378 * This is typically called when the window size changes and we need
1379 * to reallocate the buffer's back/depth/stencil/accum buffers.
1382 XMesaResizeBuffers( XMesaBuffer b
)
1384 GET_CURRENT_CONTEXT(ctx
);
1385 XMesaContext xmctx
= xmesa_context(ctx
);
1388 xmesa_check_and_update_buffer_size(xmctx
, b
);
1395 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
1396 const int *attrib_list
)
1403 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)