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"
66 #include "state_tracker/st_public.h"
67 #include "state_tracker/st_context.h"
68 #include "pipe/p_defines.h"
69 #include "pipe/p_screen.h"
70 #include "pipe/p_context.h"
72 #include "xm_winsys_aub.h"
75 * Global X driver lock
77 pipe_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 pipe_mutex_lock(_xmesa_lock
);
248 XSync(b
->xm_visual
->display
, 0); /* added for Chromium */
249 stat
= get_drawable_size(dpy
, b
->drawable
, width
, height
);
250 pipe_mutex_unlock(_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 #ifdef GALLIUM_CELL /* XXX temporary for Cell! */
354 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
356 else if (vis
->mesa_visual
.depthBits
<= 16)
357 depthFormat
= PIPE_FORMAT_Z16_UNORM
;
358 else if (vis
->mesa_visual
.depthBits
<= 24)
359 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
361 depthFormat
= PIPE_FORMAT_Z32_UNORM
;
364 if (vis
->mesa_visual
.stencilBits
== 8) {
365 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
)
366 stencilFormat
= depthFormat
;
368 stencilFormat
= PIPE_FORMAT_S8_UNORM
;
372 stencilFormat
= PIPE_FORMAT_NONE
;
373 if (depthFormat
== PIPE_FORMAT_S8Z24_UNORM
) {
374 /* use 24-bit Z, undefined stencil channel */
375 depthFormat
= PIPE_FORMAT_X8Z24_UNORM
;
380 get_drawable_size(vis
->display
, d
, &width
, &height
);
383 * Create framebuffer, but we'll plug in our own renderbuffers below.
385 b
->stfb
= st_create_framebuffer(&vis
->mesa_visual
,
386 colorFormat
, depthFormat
, stencilFormat
,
392 * Create scratch XImage for xmesa_display_surface()
394 b
->tempImage
= XCreateImage(vis
->display
,
395 vis
->visinfo
->visual
,
397 ZPixmap
, 0, /* format, offset */
401 0); /* bytes_per_line */
403 /* GLX_EXT_texture_from_pixmap */
404 b
->TextureTarget
= 0;
405 b
->TextureFormat
= GLX_TEXTURE_FORMAT_NONE_EXT
;
406 b
->TextureMipmap
= 0;
408 /* insert buffer into linked list */
409 b
->Next
= XMesaBufferList
;
417 * Find an XMesaBuffer by matching X display and colormap but NOT matching
418 * the notThis buffer.
421 xmesa_find_buffer(XMesaDisplay
*dpy
, XMesaColormap cmap
, XMesaBuffer notThis
)
424 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
425 if (b
->xm_visual
->display
== dpy
&&
436 * Remove buffer from linked list, delete if no longer referenced.
439 xmesa_free_buffer(XMesaBuffer buffer
)
441 XMesaBuffer prev
= NULL
, b
;
443 for (b
= XMesaBufferList
; b
; b
= b
->Next
) {
445 struct gl_framebuffer
*fb
= &buffer
->stfb
->Base
;
447 /* unlink buffer from list */
449 prev
->Next
= buffer
->Next
;
451 XMesaBufferList
= buffer
->Next
;
453 /* mark as delete pending */
454 fb
->DeletePending
= GL_TRUE
;
456 /* Since the X window for the XMesaBuffer is going away, we don't
457 * want to dereference this pointer in the future.
461 buffer
->tempImage
->data
= NULL
;
462 XDestroyImage(buffer
->tempImage
);
464 /* Unreference. If count = zero we'll really delete the buffer */
465 _mesa_unreference_framebuffer(&fb
);
467 XFreeGC(b
->xm_visual
->display
, b
->gc
);
473 /* continue search */
476 /* buffer not found in XMesaBufferList */
477 _mesa_problem(NULL
,"xmesa_free_buffer() - buffer not found\n");
482 /**********************************************************************/
483 /***** Misc Private Functions *****/
484 /**********************************************************************/
488 * When a context is bound for the first time, we can finally finish
489 * initializing the context's visual and buffer information.
490 * \param v the XMesaVisual to initialize
491 * \param b the XMesaBuffer to initialize (may be NULL)
492 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
493 * \param window the window/pixmap we're rendering into
494 * \param cmap the colormap associated with the window/pixmap
495 * \return GL_TRUE=success, GL_FALSE=failure
498 initialize_visual_and_buffer(XMesaVisual v
, XMesaBuffer b
,
499 GLboolean rgb_flag
, XMesaDrawable window
,
503 int client
= (window
) ? CLIENT_ID(window
->id
) : 0;
506 ASSERT(!b
|| b
->xm_visual
== v
);
508 /* Save true bits/pixel */
509 v
->BitsPerPixel
= bits_per_pixel(v
);
510 assert(v
->BitsPerPixel
> 0);
512 if (rgb_flag
== GL_FALSE
) {
513 /* COLOR-INDEXED WINDOW: not supported*/
518 * We support RGB rendering into almost any kind of visual.
520 const int xclass
= v
->mesa_visual
.visualType
;
521 if (xclass
!= GLX_TRUE_COLOR
&& xclass
== !GLX_DIRECT_COLOR
) {
523 "XMesa: RGB mode rendering not supported in given visual.\n");
526 v
->mesa_visual
.indexBits
= 0;
528 if (v
->BitsPerPixel
== 32) {
529 /* We use XImages for all front/back buffers. If an X Window or
530 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
531 * will be preserved. For XImages we're in luck.
533 v
->mesa_visual
.alphaBits
= 8;
538 * If MESA_INFO env var is set print out some debugging info
539 * which can help Brian figure out what's going on when a user
542 if (_mesa_getenv("MESA_INFO")) {
543 _mesa_printf("X/Mesa visual = %p\n", (void *) v
);
544 _mesa_printf("X/Mesa level = %d\n", v
->mesa_visual
.level
);
545 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v
));
546 _mesa_printf("X/Mesa bits per pixel = %d\n", v
->BitsPerPixel
);
550 /* these should have been set in create_xmesa_buffer */
551 ASSERT(b
->drawable
== window
);
553 /* Setup for single/double buffering */
554 if (v
->mesa_visual
.doubleBufferMode
) {
555 /* Double buffered */
556 b
->shm
= xmesa_check_for_xshm( v
->display
);
559 /* X11 graphics context */
561 b
->gc
= CreateScratchGC(v
->display
, window
->depth
);
563 b
->gc
= XCreateGC( v
->display
, window
, 0, NULL
);
565 XMesaSetFunction( v
->display
, b
->gc
, GXcopy
);
573 #define NUM_VISUAL_TYPES 6
576 * Convert an X visual type to a GLX visual type.
578 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
580 * \return If \c visualType is a valid X visual type, a GLX visual type will
581 * be returned. Otherwise \c GLX_NONE will be returned.
584 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
588 xmesa_convert_from_x_visual_type( int visualType
)
590 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
591 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
592 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
593 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
596 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
597 ? glx_visual_types
[ visualType
] : GLX_NONE
;
601 /**********************************************************************/
602 /***** Public Functions *****/
603 /**********************************************************************/
607 * Create a new X/Mesa visual.
608 * Input: display - X11 display
609 * visinfo - an XVisualInfo pointer
610 * rgb_flag - GL_TRUE = RGB mode,
611 * GL_FALSE = color index mode
612 * alpha_flag - alpha buffer requested?
613 * db_flag - GL_TRUE = double-buffered,
614 * GL_FALSE = single buffered
615 * stereo_flag - stereo visual?
616 * ximage_flag - GL_TRUE = use an XImage for back buffer,
617 * GL_FALSE = use an off-screen pixmap for back buffer
618 * depth_size - requested bits/depth values, or zero
619 * stencil_size - requested bits/stencil values, or zero
620 * accum_red_size - requested bits/red accum values, or zero
621 * accum_green_size - requested bits/green accum values, or zero
622 * accum_blue_size - requested bits/blue accum values, or zero
623 * accum_alpha_size - requested bits/alpha accum values, or zero
624 * num_samples - number of samples/pixel if multisampling, or zero
625 * level - visual level, usually 0
626 * visualCaveat - ala the GLX extension, usually GLX_NONE
627 * Return; a new XMesaVisual or 0 if error.
630 XMesaVisual
XMesaCreateVisual( XMesaDisplay
*display
,
631 XMesaVisualInfo visinfo
,
633 GLboolean alpha_flag
,
635 GLboolean stereo_flag
,
636 GLboolean ximage_flag
,
639 GLint accum_red_size
,
640 GLint accum_green_size
,
641 GLint accum_blue_size
,
642 GLint accum_alpha_size
,
648 GLint red_bits
, green_bits
, blue_bits
, alpha_bits
;
650 #ifndef XFree86Server
651 /* For debugging only */
652 if (_mesa_getenv("MESA_XSYNC")) {
653 /* This makes debugging X easier.
654 * In your debugger, set a breakpoint on _XError to stop when an
655 * X protocol error is generated.
657 XSynchronize( display
, 1 );
661 v
= (XMesaVisual
) CALLOC_STRUCT(xmesa_visual
);
666 v
->display
= display
;
668 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
669 * the struct but we may need some of the information contained in it
672 #ifndef XFree86Server
673 v
->visinfo
= (XVisualInfo
*) MALLOC(sizeof(*visinfo
));
678 MEMCPY(v
->visinfo
, visinfo
, sizeof(*visinfo
));
681 v
->ximage_flag
= ximage_flag
;
684 /* We could calculate these values by ourselves. nplanes is either the sum
685 * of the red, green, and blue bits or the number index bits.
686 * ColormapEntries is either (1U << index_bits) or
687 * (1U << max(redBits, greenBits, blueBits)).
689 assert(visinfo
->nplanes
> 0);
690 v
->nplanes
= visinfo
->nplanes
;
691 v
->ColormapEntries
= visinfo
->ColormapEntries
;
693 v
->mesa_visual
.redMask
= visinfo
->redMask
;
694 v
->mesa_visual
.greenMask
= visinfo
->greenMask
;
695 v
->mesa_visual
.blueMask
= visinfo
->blueMask
;
696 v
->mesa_visual
.visualID
= visinfo
->vid
;
697 v
->mesa_visual
.screen
= 0; /* FIXME: What should be done here? */
699 v
->mesa_visual
.redMask
= visinfo
->red_mask
;
700 v
->mesa_visual
.greenMask
= visinfo
->green_mask
;
701 v
->mesa_visual
.blueMask
= visinfo
->blue_mask
;
702 v
->mesa_visual
.visualID
= visinfo
->visualid
;
703 v
->mesa_visual
.screen
= visinfo
->screen
;
706 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
707 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->class);
709 v
->mesa_visual
.visualType
= xmesa_convert_from_x_visual_type(visinfo
->c_class
);
712 v
->mesa_visual
.visualRating
= visualCaveat
;
715 v
->mesa_visual
.alphaBits
= 8;
717 (void) initialize_visual_and_buffer( v
, NULL
, rgb_flag
, 0, 0 );
720 const int xclass
= v
->mesa_visual
.visualType
;
721 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
722 red_bits
= _mesa_bitcount(GET_REDMASK(v
));
723 green_bits
= _mesa_bitcount(GET_GREENMASK(v
));
724 blue_bits
= _mesa_bitcount(GET_BLUEMASK(v
));
727 /* this is an approximation */
729 depth
= GET_VISUAL_DEPTH(v
);
730 red_bits
= depth
/ 3;
732 green_bits
= depth
/ 2;
736 assert( red_bits
+ green_bits
+ blue_bits
== GET_VISUAL_DEPTH(v
) );
738 alpha_bits
= v
->mesa_visual
.alphaBits
;
741 _mesa_initialize_visual( &v
->mesa_visual
,
742 rgb_flag
, db_flag
, stereo_flag
,
743 red_bits
, green_bits
,
744 blue_bits
, alpha_bits
,
745 v
->mesa_visual
.indexBits
,
748 accum_red_size
, accum_green_size
,
749 accum_blue_size
, accum_alpha_size
,
753 v
->mesa_visual
.level
= level
;
759 void XMesaDestroyVisual( XMesaVisual v
)
761 #ifndef XFree86Server
762 _mesa_free(v
->visinfo
);
770 * Create a new XMesaContext.
771 * \param v the XMesaVisual
772 * \param share_list another XMesaContext with which to share display
773 * lists or NULL if no sharing is wanted.
774 * \return an XMesaContext or NULL if error.
777 XMesaContext
XMesaCreateContext( XMesaVisual v
, XMesaContext share_list
)
779 static GLboolean firstTime
= GL_TRUE
;
780 struct pipe_context
*pipe
;
786 pipe_mutex_init(_xmesa_lock
);
787 firstTime
= GL_FALSE
;
790 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
791 c
= (XMesaContext
) CALLOC_STRUCT(xmesa_context
);
795 pf
= choose_pixel_format(v
);
799 c
->xm_buffer
= NULL
; /* set later by XMesaMakeCurrent */
801 if (!getenv("XM_AUB")) {
802 xmesa_mode
= XMESA_SOFTPIPE
;
803 pipe
= xmesa_create_pipe_context( c
, pf
);
806 xmesa_mode
= XMESA_AUB
;
807 pipe
= xmesa_create_i965simple(xmesa_get_pipe_winsys_aub(v
));
813 c
->st
= st_create_context(pipe
, &v
->mesa_visual
,
814 share_list
? share_list
->st
: NULL
);
818 mesaCtx
= c
->st
->ctx
;
819 c
->st
->ctx
->DriverCtx
= c
;
822 _mesa_enable_sw_extensions(mesaCtx
);
823 _mesa_enable_1_3_extensions(mesaCtx
);
824 _mesa_enable_1_4_extensions(mesaCtx
);
825 _mesa_enable_1_5_extensions(mesaCtx
);
826 _mesa_enable_2_0_extensions(mesaCtx
);
830 /* If we're running in the X server, do bounds checking to prevent
831 * segfaults and server crashes!
833 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
840 st_destroy_context(c
->st
);
850 void XMesaDestroyContext( XMesaContext c
)
852 struct pipe_screen
*screen
= c
->st
->pipe
->screen
;
853 st_destroy_context(c
->st
);
854 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
855 * outlive it, causing segfaults
856 screen->destroy(screen);
864 * Private function for creating an XMesaBuffer which corresponds to an
865 * X window or pixmap.
866 * \param v the window's XMesaVisual
867 * \param w the window we're wrapping
868 * \return new XMesaBuffer or NULL if error
871 XMesaCreateWindowBuffer(XMesaVisual v
, XMesaWindow w
)
873 #ifndef XFree86Server
874 XWindowAttributes attr
;
883 /* Check that window depth matches visual depth */
885 depth
= ((XMesaDrawable
)w
)->depth
;
887 XGetWindowAttributes( v
->display
, w
, &attr
);
890 if (GET_VISUAL_DEPTH(v
) != depth
) {
891 _mesa_warning(NULL
, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
892 GET_VISUAL_DEPTH(v
), depth
);
898 cmap
= (ColormapPtr
)LookupIDByType(wColormap(w
), RT_COLORMAP
);
901 cmap
= attr
.colormap
;
904 _mesa_warning(NULL
, "Window %u has no colormap!\n", (unsigned int) w
);
905 /* this is weird, a window w/out a colormap!? */
906 /* OK, let's just allocate a new one and hope for the best */
907 cmap
= XCreateColormap(v
->display
, w
, attr
.visual
, AllocNone
);
911 b
= create_xmesa_buffer((XMesaDrawable
) w
, WINDOW
, v
, cmap
);
915 if (!initialize_visual_and_buffer( v
, b
, v
->mesa_visual
.rgbMode
,
916 (XMesaDrawable
) w
, cmap
)) {
917 xmesa_free_buffer(b
);
927 * Create a new XMesaBuffer from an X pixmap.
929 * \param v the XMesaVisual
930 * \param p the pixmap
931 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
932 * \c GLX_DIRECT_COLOR visual for the pixmap
933 * \returns new XMesaBuffer or NULL if error
936 XMesaCreatePixmapBuffer(XMesaVisual v
, XMesaPixmap p
, XMesaColormap cmap
)
942 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
946 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
947 (XMesaDrawable
) p
, cmap
)) {
948 xmesa_free_buffer(b
);
957 * For GLX_EXT_texture_from_pixmap
960 XMesaCreatePixmapTextureBuffer(XMesaVisual v
, XMesaPixmap p
,
962 int format
, int target
, int mipmap
)
964 GET_CURRENT_CONTEXT(ctx
);
966 GLuint width
, height
;
970 b
= create_xmesa_buffer((XMesaDrawable
) p
, PIXMAP
, v
, cmap
);
974 /* get pixmap size, update framebuffer/renderbuffer dims */
975 xmesa_get_window_size(v
->display
, b
, &width
, &height
);
976 _mesa_resize_framebuffer(NULL
, &(b
->stfb
->Base
), width
, height
);
980 if (ctx
->Extensions
.ARB_texture_non_power_of_two
) {
981 target
= GLX_TEXTURE_2D_EXT
;
983 else if ( _mesa_bitcount(width
) == 1
984 && _mesa_bitcount(height
) == 1) {
985 /* power of two size */
987 target
= GLX_TEXTURE_1D_EXT
;
990 target
= GLX_TEXTURE_2D_EXT
;
993 else if (ctx
->Extensions
.NV_texture_rectangle
) {
994 target
= GLX_TEXTURE_RECTANGLE_EXT
;
997 /* non power of two textures not supported */
998 XMesaDestroyBuffer(b
);
1003 b
->TextureTarget
= target
;
1004 b
->TextureFormat
= format
;
1005 b
->TextureMipmap
= mipmap
;
1007 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1008 (XMesaDrawable
) p
, cmap
)) {
1009 xmesa_free_buffer(b
);
1019 XMesaCreatePBuffer(XMesaVisual v
, XMesaColormap cmap
,
1020 unsigned int width
, unsigned int height
)
1022 #ifndef XFree86Server
1024 XMesaDrawable drawable
; /* X Pixmap Drawable */
1027 /* allocate pixmap for front buffer */
1028 root
= RootWindow( v
->display
, v
->visinfo
->screen
);
1029 drawable
= XCreatePixmap(v
->display
, root
, width
, height
,
1034 b
= create_xmesa_buffer(drawable
, PBUFFER
, v
, cmap
);
1038 if (!initialize_visual_and_buffer(v
, b
, v
->mesa_visual
.rgbMode
,
1040 xmesa_free_buffer(b
);
1053 * Deallocate an XMesaBuffer structure and all related info.
1056 XMesaDestroyBuffer(XMesaBuffer b
)
1058 xmesa_free_buffer(b
);
1063 * Query the current window size and update the corresponding GLframebuffer
1064 * and all attached renderbuffers.
1066 * 1. the first time a buffer is bound to a context.
1067 * 2. from the XMesaResizeBuffers() API function.
1068 * 3. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1069 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1070 * when resizing a buffer when no rendering context is bound.
1073 xmesa_check_and_update_buffer_size(XMesaContext xmctx
, XMesaBuffer drawBuffer
)
1075 GLuint width
, height
;
1076 xmesa_get_window_size(drawBuffer
->xm_visual
->display
, drawBuffer
, &width
, &height
);
1077 st_resize_framebuffer(drawBuffer
->stfb
, width
, height
);
1082 * Bind buffer b to context c and make c the current rendering context.
1084 GLboolean
XMesaMakeCurrent( XMesaContext c
, XMesaBuffer b
)
1086 return XMesaMakeCurrent2( c
, b
, b
);
1091 * Bind buffer b to context c and make c the current rendering context.
1094 GLboolean
XMesaMakeCurrent2( XMesaContext c
, XMesaBuffer drawBuffer
,
1095 XMesaBuffer readBuffer
)
1098 if (!drawBuffer
|| !readBuffer
)
1099 return GL_FALSE
; /* must specify buffers! */
1102 /* XXX restore this optimization */
1103 if (&(c
->mesa
) == _mesa_get_current_context()
1104 && c
->mesa
.DrawBuffer
== &drawBuffer
->mesa_buffer
1105 && c
->mesa
.ReadBuffer
== &readBuffer
->mesa_buffer
1106 && xmesa_buffer(c
->mesa
.DrawBuffer
)->wasCurrent
) {
1107 /* same context and buffer, do nothing */
1112 c
->xm_buffer
= drawBuffer
;
1114 /* Call this periodically to detect when the user has begun using
1115 * GL rendering from multiple threads.
1117 _glapi_check_multithread();
1119 st_make_current(c
->st
, drawBuffer
->stfb
, readBuffer
->stfb
);
1121 xmesa_check_and_update_buffer_size(c
, drawBuffer
);
1122 if (readBuffer
!= drawBuffer
)
1123 xmesa_check_and_update_buffer_size(c
, readBuffer
);
1125 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1126 drawBuffer
->wasCurrent
= GL_TRUE
;
1130 st_make_current( NULL
, NULL
, NULL
);
1137 * Unbind the context c from its buffer.
1139 GLboolean
XMesaUnbindContext( XMesaContext c
)
1141 /* A no-op for XFree86 integration purposes */
1146 XMesaContext
XMesaGetCurrentContext( void )
1148 GET_CURRENT_CONTEXT(ctx
);
1150 XMesaContext xmesa
= xmesa_context(ctx
);
1159 XMesaBuffer
XMesaGetCurrentBuffer( void )
1161 GET_CURRENT_CONTEXT(ctx
);
1163 XMesaBuffer xmbuf
= xmesa_buffer(ctx
->DrawBuffer
);
1172 /* New in Mesa 3.1 */
1173 XMesaBuffer
XMesaGetCurrentReadBuffer( void )
1175 GET_CURRENT_CONTEXT(ctx
);
1177 return xmesa_buffer(ctx
->ReadBuffer
);
1185 #ifdef XFree86Server
1187 GLboolean
XMesaForceCurrent(XMesaContext c
)
1190 _glapi_set_dispatch(c
->mesa
.CurrentDispatch
);
1192 if (&(c
->mesa
) != _mesa_get_current_context()) {
1193 _mesa_make_current(&c
->mesa
, c
->mesa
.DrawBuffer
, c
->mesa
.ReadBuffer
);
1197 _mesa_make_current(NULL
, NULL
, NULL
);
1204 GLboolean
XMesaLoseCurrent(XMesaContext c
)
1207 _mesa_make_current(NULL
, NULL
, NULL
);
1213 GLboolean
XMesaCopyContext( XMesaContext xm_src
, XMesaContext xm_dst
, GLuint mask
)
1215 _mesa_copy_context(&xm_src
->mesa
, &xm_dst
->mesa
, mask
);
1218 #endif /* XFree86Server */
1222 GLboolean
XMesaSetFXmode( GLint mode
)
1232 * Copy the back buffer to the front buffer. If there's no back buffer
1236 void XMesaSwapBuffers( XMesaBuffer b
)
1238 struct pipe_surface
*surf
;
1240 /* If we're swapping the buffer associated with the current context
1241 * we have to flush any pending rendering commands first.
1243 st_notify_swapbuffers(b
->stfb
);
1245 surf
= st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1247 if (xmesa_mode
== XMESA_AUB
)
1248 xmesa_display_aub( surf
);
1250 xmesa_display_surface(b
, surf
);
1253 xmesa_check_and_update_buffer_size(NULL
, b
);
1259 * Copy sub-region of back buffer to front buffer
1261 void XMesaCopySubBuffer( XMesaBuffer b
, int x
, int y
, int width
, int height
)
1263 struct pipe_surface
*surf_front
1264 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_FRONT_LEFT
);
1265 struct pipe_surface
*surf_back
1266 = st_get_framebuffer_surface(b
->stfb
, ST_SURFACE_BACK_LEFT
);
1267 struct pipe_context
*pipe
= NULL
; /* XXX fix */
1269 if (!surf_front
|| !surf_back
)
1272 pipe
->surface_copy(pipe
,
1274 surf_front
, x
, y
, /* dest */
1275 surf_back
, x
, y
, /* src */
1282 * Return the depth buffer associated with an XMesaBuffer.
1283 * Input: b - the XMesa buffer handle
1284 * Output: width, height - size of buffer in pixels
1285 * bytesPerValue - bytes per depth value (2 or 4)
1286 * buffer - pointer to depth buffer values
1287 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1289 GLboolean
XMesaGetDepthBuffer( XMesaBuffer b
, GLint
*width
, GLint
*height
,
1290 GLint
*bytesPerValue
, void **buffer
)
1300 void XMesaFlush( XMesaContext c
)
1302 if (c
&& c
->xm_visual
->display
) {
1303 #ifdef XFree86Server
1307 XSync( c
->xm_visual
->display
, False
);
1314 const char *XMesaGetString( XMesaContext c
, int name
)
1317 if (name
==XMESA_VERSION
) {
1320 else if (name
==XMESA_EXTENSIONS
) {
1330 XMesaBuffer
XMesaFindBuffer( XMesaDisplay
*dpy
, XMesaDrawable d
)
1333 for (b
=XMesaBufferList
; b
; b
=b
->Next
) {
1334 if (b
->drawable
== d
&& b
->xm_visual
->display
== dpy
) {
1343 * Free/destroy all XMesaBuffers associated with given display.
1345 void xmesa_destroy_buffers_on_display(XMesaDisplay
*dpy
)
1347 XMesaBuffer b
, next
;
1348 for (b
= XMesaBufferList
; b
; b
= next
) {
1350 if (b
->xm_visual
->display
== dpy
) {
1351 xmesa_free_buffer(b
);
1358 * Look for XMesaBuffers whose X window has been destroyed.
1359 * Deallocate any such XMesaBuffers.
1361 void XMesaGarbageCollect( void )
1363 XMesaBuffer b
, next
;
1364 for (b
=XMesaBufferList
; b
; b
=next
) {
1367 b
->xm_visual
->display
&&
1369 b
->type
== WINDOW
) {
1370 #ifdef XFree86Server
1373 XSync(b
->xm_visual
->display
, False
);
1374 if (!window_exists( b
->xm_visual
->display
, b
->drawable
)) {
1375 /* found a dead window, free the ancillary info */
1376 XMesaDestroyBuffer( b
);
1384 unsigned long XMesaDitherColor( XMesaContext xmesa
, GLint x
, GLint y
,
1385 GLfloat red
, GLfloat green
,
1386 GLfloat blue
, GLfloat alpha
)
1388 /* no longer supported */
1394 * This is typically called when the window size changes and we need
1395 * to reallocate the buffer's back/depth/stencil/accum buffers.
1398 XMesaResizeBuffers( XMesaBuffer b
)
1400 GET_CURRENT_CONTEXT(ctx
);
1401 XMesaContext xmctx
= xmesa_context(ctx
);
1404 xmesa_check_and_update_buffer_size(xmctx
, b
);
1411 XMesaBindTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
,
1412 const int *attrib_list
)
1419 XMesaReleaseTexImage(XMesaDisplay
*dpy
, XMesaBuffer drawable
, int buffer
)