xm_api.c revision e968975cb57eb854769292f7c6ff773c64a386c3
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Mesa 3-D graphics library
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Version:  7.1
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the "Software"),
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to deal in the Software without restriction, including without limitation
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * and/or sell copies of the Software, and to permit persons to whom the
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Software is furnished to do so, subject to the following conditions:
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The above copyright notice and this permission notice shall be included
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in all copies or substantial portions of the Software.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \file xm_api.c
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All the XMesa* API functions.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * NOTES:
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The window coordinate system origin (0,0) is in the lower-left corner
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * of the window.  X11's window coordinate origin is in the upper-left
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * corner of the window.  Therefore, most drawing functions in this
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * file have to flip Y coordinates.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Byte swapping:  If the Mesa host and the X display use a different
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * byte order then there's some trickiness to be aware of when using
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * XImages.  The byte ordering used for the XImage is that of the X
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * display, not the Mesa host.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The color-to-pixel encoding for True/DirectColor must be done
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * according to the display's visual red_mask, green_mask, and blue_mask.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * do byte swapping if needed.  If one wants to directly "poke" the pixel
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * into the XImage's buffer then the pixel must be byte swapped first.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef __CYGWIN__
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef WIN32
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef __WIN32__
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "xm_api.h"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "xm_st.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_defines.h"
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_screen.h"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_context.h"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "xm_public.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <GL/glx.h>
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Driver interface routines, set up by xlib backend on library
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * _init().  These are global in the same way that function names are
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * global.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct xm_driver driver;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct st_api *stapi;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Default strict invalidate to false.  This means we will not call
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * XGetGeometry after every swapbuffers, which allows swapbuffers to
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * remain asynchronous.  For apps running at 100fps with synchronous
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * swapping, a 10% boost is typical.  For gears, I see closer to 20%
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * speedup.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note that the work of copying data on swapbuffers doesn't disappear
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - this change just allows the X server to execute the PutImage
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * asynchronously without us effectively blocked until its completion.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This speeds up even llvmpipe's threaded rasterization as the
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * swapbuffers operation was a large part of the serial component of
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * an llvmpipe frame.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The downside of this is correctness - applications which don't call
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * glViewport on window resizes will get incorrect rendering.  A
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * better solution would be to have per-frame but asynchronous
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * invalidation.  Xcb almost looks as if it could provide this, but
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the API doesn't seem to quite be there.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)boolean xmesa_strict_invalidate = FALSE;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void xmesa_set_driver( const struct xm_driver *templ )
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   driver = *templ;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   stapi = driver.create_st_api();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   xmesa_strict_invalidate =
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * XXX replace this with a linked list, or better yet, try to attach the
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * gallium/mesa extra bits to the X Display object with XAddExtension().
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MAX_DISPLAYS 10
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct xmesa_display Displays[MAX_DISPLAYS];
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int NumDisplays = 0;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_get_param(struct st_manager *smapi,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                enum st_manager_param param)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   switch(param) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case ST_MANAGER_BROKEN_INVALIDATE:
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return !xmesa_strict_invalidate;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   default:
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static XMesaDisplay
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_init_display( Display *display )
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   pipe_static_mutex(init_mutex);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaDisplay xmdpy;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int i;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   pipe_mutex_lock(init_mutex);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Look for XMesaDisplay which corresponds to 'display' */
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (i = 0; i < NumDisplays; i++) {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (Displays[i].display == display) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* Found it */
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         pipe_mutex_unlock(init_mutex);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return &Displays[i];
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Create new XMesaDisplay */
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   assert(NumDisplays < MAX_DISPLAYS);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   xmdpy = &Displays[NumDisplays];
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   NumDisplays++;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!xmdpy->display && display) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      xmdpy->display = display;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      xmdpy->screen = driver.create_pipe_screen(display);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      xmdpy->smapi = CALLOC_STRUCT(st_manager);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (xmdpy->smapi) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         xmdpy->smapi->screen = xmdpy->screen;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         xmdpy->smapi->get_param = xmesa_get_param;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (xmdpy->screen && xmdpy->smapi) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         pipe_mutex_init(xmdpy->mutex);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         if (xmdpy->screen) {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            xmdpy->screen->destroy(xmdpy->screen);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            xmdpy->screen = NULL;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         if (xmdpy->smapi) {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            FREE(xmdpy->smapi);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            xmdpy->smapi = NULL;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         xmdpy->display = NULL;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!xmdpy->display || xmdpy->display != display)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      xmdpy = NULL;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   pipe_mutex_unlock(init_mutex);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return xmdpy;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*****                     X Utility Functions                    *****/
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return the host's byte order as LSBFirst or MSBFirst ala X.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int host_byte_order( void )
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int i = 1;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   char *cptr = (char *) &i;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return (*cptr==1) ? LSBFirst : MSBFirst;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return the true number of bits per pixel for XImages.
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * For example, if we request a 24-bit deep visual we may actually need/get
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 32bpp XImages.  This function returns the appropriate bpp.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Input:  dpy - the X display
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         visinfo - desribes the visual to be used for XImages
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return:  true number of bits per pixel for XImages
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bits_per_pixel( XMesaVisual xmv )
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   Display *dpy = xmv->display;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XVisualInfo * visinfo = xmv->visinfo;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XImage *img;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int bitsPerPixel;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Create a temporary XImage */
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       ZPixmap, 0,           /*format, offset*/
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       (char*) MALLOC(8),    /*data*/
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       1, 1,                 /*width, height*/
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       32,                   /*bitmap_pad*/
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       0                     /*bytes_per_line*/
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     );
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   assert(img);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* grab the bits/pixel value */
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   bitsPerPixel = img->bits_per_pixel;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* free the XImage */
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   free( img->data );
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   img->data = NULL;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XDestroyImage( img );
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return bitsPerPixel;
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Determine if a given X window ID is valid (window exists).
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Do this by calling XGetWindowAttributes() for the window and
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * checking if we catch an X error.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Input:  dpy - the display
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         win - the window to check for existance
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return:  GL_TRUE - window exists
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *          GL_FALSE - window doesn't exist
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLboolean WindowExistsFlag;
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr )
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   (void) dpy;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (xerr->error_code == BadWindow) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WindowExistsFlag = GL_FALSE;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return 0;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLboolean window_exists( Display *dpy, Window win )
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XWindowAttributes wa;
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int (*old_handler)( Display*, XErrorEvent* );
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   WindowExistsFlag = GL_TRUE;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   old_handler = XSetErrorHandler(window_exists_err_handler);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XSetErrorHandler(old_handler);
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return WindowExistsFlag;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static Status
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height )
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   Window root;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   Status stat;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int xpos, ypos;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   unsigned int w, h, bw, depth;
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   *width = w;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   *height = h;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return stat;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return the size of the window (or pixmap) that corresponds to the
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * given XMesaBuffer.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param width  returns width in pixels
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param height  returns height in pixels
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_get_window_size(Display *dpy, XMesaBuffer b,
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      GLuint *width, GLuint *height)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaDisplay xmdpy = xmesa_init_display(dpy);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   Status stat;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   pipe_mutex_lock(xmdpy->mutex);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   stat = get_drawable_size(dpy, b->ws.drawable, width, height);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   pipe_mutex_unlock(xmdpy->mutex);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!stat) {
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* probably querying a window that's recently been destroyed */
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      _mesa_warning(NULL, "XGetGeometry failed!\n");
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *width = *height = 1;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GET_REDMASK(__v)        __v->mesa_visual.redMask
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GET_GREENMASK(__v)      __v->mesa_visual.greenMask
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GET_BLUEMASK(__v)       __v->mesa_visual.blueMask
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Choose the pixel format for the given visual.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This will tell the gallium driver how to pack pixel data into
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * drawing surfaces.
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLuint
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)choose_pixel_format(XMesaVisual v)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   boolean native_byte_order = (host_byte_order() ==
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                ImageByteOrder(v->display));
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (   GET_REDMASK(v)   == 0x0000ff
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       && GET_GREENMASK(v) == 0x00ff00
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       && GET_BLUEMASK(v)  == 0xff0000
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       && v->BitsPerPixel == 32) {
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (native_byte_order) {
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* no byteswapping needed */
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_R8G8B8A8_UNORM;
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_A8B8G8R8_UNORM;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else if (   GET_REDMASK(v)   == 0xff0000
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_GREENMASK(v) == 0x00ff00
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_BLUEMASK(v)  == 0x0000ff
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && v->BitsPerPixel == 32) {
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (native_byte_order) {
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* no byteswapping needed */
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_B8G8R8A8_UNORM;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_A8R8G8B8_UNORM;
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else if (   GET_REDMASK(v)   == 0x0000ff00
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_GREENMASK(v) == 0x00ff0000
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_BLUEMASK(v)  == 0xff000000
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && v->BitsPerPixel == 32) {
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (native_byte_order) {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* no byteswapping needed */
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_A8R8G8B8_UNORM;
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return PIPE_FORMAT_B8G8R8A8_UNORM;
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else if (   GET_REDMASK(v)   == 0xf800
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_GREENMASK(v) == 0x07e0
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && GET_BLUEMASK(v)  == 0x001f
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && native_byte_order
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            && v->BitsPerPixel == 16) {
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* 5-6-5 RGB */
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PIPE_FORMAT_B5G6R5_UNORM;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return PIPE_FORMAT_NONE;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Choose a depth/stencil format that satisfies the given depth and
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * stencil sizes.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static enum pipe_format
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   const enum pipe_texture_target target = PIPE_TEXTURE_2D;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   const unsigned sample_count = 0;
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   enum pipe_format formats[8], fmt;
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int count, i;
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   count = 0;
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (depth <= 16 && stencil == 0) {
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_Z16_UNORM;
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (depth <= 24 && stencil == 0) {
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_X8Z24_UNORM;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_Z24X8_UNORM;
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (depth <= 24 && stencil <= 8) {
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM;
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (depth <= 32 && stencil == 0) {
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      formats[count++] = PIPE_FORMAT_Z32_UNORM;
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   fmt = PIPE_FORMAT_NONE;
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (i = 0; i < count; i++) {
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i],
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             target, sample_count,
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             tex_usage)) {
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         fmt = formats[i];
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         break;
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return fmt;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*****                Linked list of XMesaBuffers                 *****/
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static XMesaBuffer XMesaBufferList = NULL;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Allocate a new XMesaBuffer object which corresponds to the given drawable.
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note that XMesaBuffer is derived from struct gl_framebuffer.
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The new XMesaBuffer will not have any size (Width=Height=0).
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param d  the corresponding X drawable (window or pixmap)
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param type  either WINDOW, PIXMAP or PBUFFER, describing d
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param vis  the buffer's visual
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param cmap  the window's colormap, if known.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \return new XMesaBuffer or NULL if any problem
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static XMesaBuffer
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)create_xmesa_buffer(Drawable d, BufferType type,
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    XMesaVisual vis, Colormap cmap)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaDisplay xmdpy = xmesa_init_display(vis->display);
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaBuffer b;
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   uint width, height;
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER);
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!xmdpy)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!b)
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->ws.drawable = d;
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->ws.visual = vis->visinfo->visual;
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->ws.depth = vis->visinfo->depth;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->xm_visual = vis;
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->type = type;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->cmap = cmap;
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   get_drawable_size(vis->display, d, &width, &height);
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /*
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * Create framebuffer, but we'll plug in our own renderbuffers below.
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    */
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->stfb = xmesa_create_st_framebuffer(xmdpy, b);
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* GLX_EXT_texture_from_pixmap */
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->TextureTarget = 0;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT;
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->TextureMipmap = 0;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* insert buffer into linked list */
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   b->Next = XMesaBufferList;
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaBufferList = b;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return b;
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Find an XMesaBuffer by matching X display and colormap but NOT matching
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the notThis buffer.
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)XMesaBuffer
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis)
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaBuffer b;
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (b = XMesaBufferList; b; b = b->Next) {
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (b->xm_visual->display == dpy &&
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          b->cmap == cmap &&
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          b != notThis) {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return b;
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return NULL;
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Remove buffer from linked list, delete if no longer referenced.
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_free_buffer(XMesaBuffer buffer)
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaBuffer prev = NULL, b;
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (b = XMesaBufferList; b; b = b->Next) {
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (b == buffer) {
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* unlink buffer from list */
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         if (prev)
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            prev->Next = buffer->Next;
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         else
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            XMesaBufferList = buffer->Next;
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* Since the X window for the XMesaBuffer is going away, we don't
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          * want to dereference this pointer in the future.
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          */
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         b->ws.drawable = 0;
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* XXX we should move the buffer to a delete-pending list and destroy
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          * the buffer until it is no longer current.
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          */
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         xmesa_destroy_st_framebuffer(buffer->stfb);
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         free(buffer);
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return;
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* continue search */
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prev = b;
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* buffer not found in XMesaBufferList */
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*****                   Misc Private Functions                   *****/
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * When a context is bound for the first time, we can finally finish
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * initializing the context's visual and buffer information.
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param v  the XMesaVisual to initialize
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param b  the XMesaBuffer to initialize (may be NULL)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param rgb_flag  TRUE = RGBA mode, FALSE = color index mode
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param window  the window/pixmap we're rendering into
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param cmap  the colormap associated with the window/pixmap
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \return GL_TRUE=success, GL_FALSE=failure
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLboolean
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             GLboolean rgb_flag, Drawable window,
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             Colormap cmap)
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   ASSERT(!b || b->xm_visual == v);
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Save true bits/pixel */
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->BitsPerPixel = bits_per_pixel(v);
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   assert(v->BitsPerPixel > 0);
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (rgb_flag == GL_FALSE) {
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* COLOR-INDEXED WINDOW: not supported*/
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return GL_FALSE;
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else {
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* RGB WINDOW:
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       * We support RGB rendering into almost any kind of visual.
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       */
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const int xclass = v->visualType;
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) {
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 _mesa_warning(NULL,
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "XMesa: RGB mode rendering not supported in given visual.\n");
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 return GL_FALSE;
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      v->mesa_visual.indexBits = 0;
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (v->BitsPerPixel == 32) {
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* We use XImages for all front/back buffers.  If an X Window or
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          * X Pixmap is 32bpp, there's no guarantee that the alpha channel
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          * will be preserved.  For XImages we're in luck.
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          */
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         v->mesa_visual.alphaBits = 8;
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /*
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * If MESA_INFO env var is set print out some debugging info
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * which can help Brian figure out what's going on when a user
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * reports bugs.
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    */
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (_mesa_getenv("MESA_INFO")) {
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      printf("X/Mesa visual = %p\n", (void *) v);
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      printf("X/Mesa level = %d\n", v->mesa_visual.level);
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      printf("X/Mesa depth = %d\n", v->visinfo->depth);
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return GL_TRUE;
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NUM_VISUAL_TYPES   6
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Convert an X visual type to a GLX visual type.
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *        to be converted.
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \return If \c visualType is a valid X visual type, a GLX visual type will
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         be returned.  Otherwise \c GLX_NONE will be returned.
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \note
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DRI CVS tree.
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLint
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)xmesa_convert_from_x_visual_type( int visualType )
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	GLX_STATIC_GRAY,  GLX_GRAY_SCALE,
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	GLX_TRUE_COLOR,   GLX_DIRECT_COLOR
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ( (unsigned) visualType < NUM_VISUAL_TYPES )
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	? glx_visual_types[ visualType ] : GLX_NONE;
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*****                       Public Functions                     *****/
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**********************************************************************/
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Create a new X/Mesa visual.
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Input:  display - X11 display
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         visinfo - an XVisualInfo pointer
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         rgb_flag - GL_TRUE = RGB mode,
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *                    GL_FALSE = color index mode
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         alpha_flag - alpha buffer requested?
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         db_flag - GL_TRUE = double-buffered,
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *                   GL_FALSE = single buffered
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         stereo_flag - stereo visual?
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         ximage_flag - GL_TRUE = use an XImage for back buffer,
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *                       GL_FALSE = use an off-screen pixmap for back buffer
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         depth_size - requested bits/depth values, or zero
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         stencil_size - requested bits/stencil values, or zero
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         accum_red_size - requested bits/red accum values, or zero
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         accum_green_size - requested bits/green accum values, or zero
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         accum_blue_size - requested bits/blue accum values, or zero
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         accum_alpha_size - requested bits/alpha accum values, or zero
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         num_samples - number of samples/pixel if multisampling, or zero
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         level - visual level, usually 0
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *         visualCaveat - ala the GLX extension, usually GLX_NONE
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Return;  a new XMesaVisual or 0 if error.
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PUBLIC
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)XMesaVisual XMesaCreateVisual( Display *display,
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               XVisualInfo * visinfo,
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLboolean rgb_flag,
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLboolean alpha_flag,
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLboolean db_flag,
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLboolean stereo_flag,
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLboolean ximage_flag,
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint depth_size,
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint stencil_size,
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint accum_red_size,
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint accum_green_size,
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint accum_blue_size,
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint accum_alpha_size,
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint num_samples,
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint level,
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLint visualCaveat )
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaDisplay xmdpy = xmesa_init_display(display);
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   XMesaVisual v;
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   GLint red_bits, green_bits, blue_bits, alpha_bits;
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!xmdpy)
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* For debugging only */
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (_mesa_getenv("MESA_XSYNC")) {
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* This makes debugging X easier.
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       * In your debugger, set a breakpoint on _XError to stop when an
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       * X protocol error is generated.
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       */
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      XSynchronize( display, 1 );
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!v) {
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->display = display;
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Save a copy of the XVisualInfo struct because the user may Xfree()
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * the struct but we may need some of the information contained in it
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * at a later time.
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    */
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!v->visinfo) {
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      free(v);
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   memcpy(v->visinfo, visinfo, sizeof(*visinfo));
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->ximage_flag = ximage_flag;
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->mesa_visual.redMask = visinfo->red_mask;
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->mesa_visual.greenMask = visinfo->green_mask;
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->mesa_visual.blueMask = visinfo->blue_mask;
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->visualID = visinfo->visualid;
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->screen = visinfo->screen;
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !(defined(__cplusplus) || defined(c_plusplus))
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->visualType = xmesa_convert_from_x_visual_type(visinfo->class);
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->mesa_visual.visualRating = visualCaveat;
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (alpha_flag)
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      v->mesa_visual.alphaBits = 8;
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 );
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   {
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const int xclass = v->visualType;
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         red_bits   = _mesa_bitcount(GET_REDMASK(v));
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         green_bits = _mesa_bitcount(GET_GREENMASK(v));
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         blue_bits  = _mesa_bitcount(GET_BLUEMASK(v));
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         /* this is an approximation */
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         int depth;
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         depth = v->visinfo->depth;
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         red_bits = depth / 3;
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         depth -= red_bits;
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         green_bits = depth / 2;
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         depth -= green_bits;
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         blue_bits = depth;
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         alpha_bits = 0;
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         assert( red_bits + green_bits + blue_bits == v->visinfo->depth );
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      alpha_bits = v->mesa_visual.alphaBits;
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* initialize visual */
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   {
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      struct gl_config *vis = &v->mesa_visual;
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->rgbMode          = GL_TRUE;
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->doubleBufferMode = db_flag;
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->stereoMode       = stereo_flag;
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->redBits          = red_bits;
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->greenBits        = green_bits;
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->blueBits         = blue_bits;
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->alphaBits        = alpha_bits;
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->rgbBits          = red_bits + green_bits + blue_bits;
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->indexBits      = 0;
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->depthBits      = depth_size;
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->stencilBits    = stencil_size;
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->accumRedBits   = accum_red_size;
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->accumGreenBits = accum_green_size;
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->accumBlueBits  = accum_blue_size;
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->accumAlphaBits = accum_alpha_size;
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->haveAccumBuffer   = accum_red_size > 0;
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->haveDepthBuffer   = depth_size > 0;
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->haveStencilBuffer = stencil_size > 0;
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->numAuxBuffers = 0;
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->level = 0;
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->sampleBuffers = 0;
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      vis->samples = 0;
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (db_flag)
790      v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
791   if (stereo_flag) {
792      v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
793      if (db_flag)
794         v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
795   }
796
797   v->stvis.color_format = choose_pixel_format(v);
798   if (v->stvis.color_format == PIPE_FORMAT_NONE) {
799      FREE(v->visinfo);
800      FREE(v);
801      return NULL;
802   }
803
804   v->stvis.depth_stencil_format =
805      choose_depth_stencil_format(xmdpy, depth_size, stencil_size);
806
807   v->stvis.accum_format = (accum_red_size +
808         accum_green_size + accum_blue_size + accum_alpha_size) ?
809      PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
810
811   v->stvis.samples = num_samples;
812   v->stvis.render_buffer = ST_ATTACHMENT_INVALID;
813
814   /* XXX minor hack */
815   v->mesa_visual.level = level;
816   return v;
817}
818
819
820PUBLIC
821void XMesaDestroyVisual( XMesaVisual v )
822{
823   free(v->visinfo);
824   free(v);
825}
826
827
828/**
829 * Return the informative name.
830 */
831const char *
832xmesa_get_name(void)
833{
834   return stapi->name;
835}
836
837
838/**
839 * Do per-display initializations.
840 */
841void
842xmesa_init( Display *display )
843{
844   xmesa_init_display(display);
845}
846
847
848/**
849 * Create a new XMesaContext.
850 * \param v  the XMesaVisual
851 * \param share_list  another XMesaContext with which to share display
852 *                    lists or NULL if no sharing is wanted.
853 * \return an XMesaContext or NULL if error.
854 */
855PUBLIC
856XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
857{
858   XMesaDisplay xmdpy = xmesa_init_display(v->display);
859   struct st_context_attribs attribs;
860   XMesaContext c;
861
862   if (!xmdpy)
863      return NULL;
864
865   /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
866   c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
867   if (!c)
868      return NULL;
869
870   c->xm_visual = v;
871   c->xm_buffer = NULL;   /* set later by XMesaMakeCurrent */
872   c->xm_read_buffer = NULL;
873
874   memset(&attribs, 0, sizeof(attribs));
875   attribs.profile = ST_PROFILE_DEFAULT;
876   attribs.visual = v->stvis;
877
878   c->st = stapi->create_context(stapi, xmdpy->smapi,
879         &attribs, (share_list) ? share_list->st : NULL);
880   if (c->st == NULL)
881      goto fail;
882
883   c->st->st_manager_private = (void *) c;
884
885   return c;
886
887fail:
888   if (c->st)
889      c->st->destroy(c->st);
890
891   free(c);
892   return NULL;
893}
894
895
896
897PUBLIC
898void XMesaDestroyContext( XMesaContext c )
899{
900   c->st->destroy(c->st);
901
902   /* FIXME: We should destroy the screen here, but if we do so, surfaces may
903    * outlive it, causing segfaults
904   struct pipe_screen *screen = c->st->pipe->screen;
905   screen->destroy(screen);
906   */
907
908   free(c);
909}
910
911
912
913/**
914 * Private function for creating an XMesaBuffer which corresponds to an
915 * X window or pixmap.
916 * \param v  the window's XMesaVisual
917 * \param w  the window we're wrapping
918 * \return  new XMesaBuffer or NULL if error
919 */
920PUBLIC XMesaBuffer
921XMesaCreateWindowBuffer(XMesaVisual v, Window w)
922{
923   XWindowAttributes attr;
924   XMesaBuffer b;
925   Colormap cmap;
926   int depth;
927
928   assert(v);
929   assert(w);
930
931   /* Check that window depth matches visual depth */
932   XGetWindowAttributes( v->display, w, &attr );
933   depth = attr.depth;
934   if (v->visinfo->depth != depth) {
935      _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
936                    v->visinfo->depth, depth);
937      return NULL;
938   }
939
940   /* Find colormap */
941   if (attr.colormap) {
942      cmap = attr.colormap;
943   }
944   else {
945      _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
946      /* this is weird, a window w/out a colormap!? */
947      /* OK, let's just allocate a new one and hope for the best */
948      cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
949   }
950
951   b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap);
952   if (!b)
953      return NULL;
954
955   if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode,
956                                      (Drawable) w, cmap )) {
957      xmesa_free_buffer(b);
958      return NULL;
959   }
960
961   return b;
962}
963
964
965
966/**
967 * Create a new XMesaBuffer from an X pixmap.
968 *
969 * \param v    the XMesaVisual
970 * \param p    the pixmap
971 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
972 *             \c GLX_DIRECT_COLOR visual for the pixmap
973 * \returns new XMesaBuffer or NULL if error
974 */
975PUBLIC XMesaBuffer
976XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap)
977{
978   XMesaBuffer b;
979
980   assert(v);
981
982   b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
983   if (!b)
984      return NULL;
985
986   if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
987				     (Drawable) p, cmap)) {
988      xmesa_free_buffer(b);
989      return NULL;
990   }
991
992   return b;
993}
994
995
996/**
997 * For GLX_EXT_texture_from_pixmap
998 */
999XMesaBuffer
1000XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
1001                               Colormap cmap,
1002                               int format, int target, int mipmap)
1003{
1004   GET_CURRENT_CONTEXT(ctx);
1005   XMesaBuffer b;
1006
1007   assert(v);
1008
1009   b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
1010   if (!b)
1011      return NULL;
1012
1013   /* get pixmap size */
1014   xmesa_get_window_size(v->display, b, &b->width, &b->height);
1015
1016   if (target == 0) {
1017      /* examine dims */
1018      if (ctx->Extensions.ARB_texture_non_power_of_two) {
1019         target = GLX_TEXTURE_2D_EXT;
1020      }
1021      else if (   _mesa_bitcount(b->width)  == 1
1022               && _mesa_bitcount(b->height) == 1) {
1023         /* power of two size */
1024         if (b->height == 1) {
1025            target = GLX_TEXTURE_1D_EXT;
1026         }
1027         else {
1028            target = GLX_TEXTURE_2D_EXT;
1029         }
1030      }
1031      else if (ctx->Extensions.NV_texture_rectangle) {
1032         target = GLX_TEXTURE_RECTANGLE_EXT;
1033      }
1034      else {
1035         /* non power of two textures not supported */
1036         XMesaDestroyBuffer(b);
1037         return 0;
1038      }
1039   }
1040
1041   b->TextureTarget = target;
1042   b->TextureFormat = format;
1043   b->TextureMipmap = mipmap;
1044
1045   if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1046				     (Drawable) p, cmap)) {
1047      xmesa_free_buffer(b);
1048      return NULL;
1049   }
1050
1051   return b;
1052}
1053
1054
1055
1056XMesaBuffer
1057XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
1058                   unsigned int width, unsigned int height)
1059{
1060   Window root;
1061   Drawable drawable;  /* X Pixmap Drawable */
1062   XMesaBuffer b;
1063
1064   /* allocate pixmap for front buffer */
1065   root = RootWindow( v->display, v->visinfo->screen );
1066   drawable = XCreatePixmap(v->display, root, width, height,
1067                            v->visinfo->depth);
1068   if (!drawable)
1069      return NULL;
1070
1071   b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
1072   if (!b)
1073      return NULL;
1074
1075   if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1076				     drawable, cmap)) {
1077      xmesa_free_buffer(b);
1078      return NULL;
1079   }
1080
1081   return b;
1082}
1083
1084
1085
1086/*
1087 * Deallocate an XMesaBuffer structure and all related info.
1088 */
1089PUBLIC void
1090XMesaDestroyBuffer(XMesaBuffer b)
1091{
1092   xmesa_free_buffer(b);
1093}
1094
1095
1096/**
1097 * Notify the binding context to validate the buffer.
1098 */
1099void
1100xmesa_notify_invalid_buffer(XMesaBuffer b)
1101{
1102   XMesaContext xmctx = XMesaGetCurrentContext();
1103
1104   if (xmctx && xmctx->xm_buffer == b)
1105      xmctx->st->notify_invalid_framebuffer(xmctx->st, b->stfb);
1106}
1107
1108
1109/**
1110 * Query the current drawable size and notify the binding context.
1111 */
1112void
1113xmesa_check_buffer_size(XMesaBuffer b)
1114{
1115   if (b->type == PBUFFER)
1116      return;
1117
1118   xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height);
1119   xmesa_notify_invalid_buffer(b);
1120}
1121
1122
1123/*
1124 * Bind buffer b to context c and make c the current rendering context.
1125 */
1126PUBLIC
1127GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1128                             XMesaBuffer readBuffer )
1129{
1130   XMesaContext old_ctx = XMesaGetCurrentContext();
1131
1132   if (old_ctx && old_ctx != c) {
1133      XMesaFlush(old_ctx);
1134      old_ctx->xm_buffer = NULL;
1135      old_ctx->xm_read_buffer = NULL;
1136   }
1137
1138   if (c) {
1139      if (!drawBuffer || !readBuffer)
1140         return GL_FALSE;  /* must specify buffers! */
1141
1142      if (c == old_ctx &&
1143	  c->xm_buffer == drawBuffer &&
1144	  c->xm_read_buffer == readBuffer)
1145	 return GL_TRUE;
1146
1147      xmesa_check_buffer_size(drawBuffer);
1148      if (readBuffer != drawBuffer)
1149         xmesa_check_buffer_size(readBuffer);
1150
1151      c->xm_buffer = drawBuffer;
1152      c->xm_read_buffer = readBuffer;
1153
1154      stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb);
1155
1156      /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1157      drawBuffer->wasCurrent = GL_TRUE;
1158   }
1159   else {
1160      /* Detach */
1161      stapi->make_current(stapi, NULL, NULL, NULL);
1162
1163   }
1164   return GL_TRUE;
1165}
1166
1167
1168/*
1169 * Unbind the context c from its buffer.
1170 */
1171GLboolean XMesaUnbindContext( XMesaContext c )
1172{
1173   /* A no-op for XFree86 integration purposes */
1174   return GL_TRUE;
1175}
1176
1177
1178XMesaContext XMesaGetCurrentContext( void )
1179{
1180   struct st_context_iface *st = stapi->get_current(stapi);
1181   return (XMesaContext) (st) ? st->st_manager_private : NULL;
1182}
1183
1184
1185
1186/**
1187 * Swap front and back color buffers and have winsys display front buffer.
1188 * If there's no front color buffer no swap actually occurs.
1189 */
1190PUBLIC
1191void XMesaSwapBuffers( XMesaBuffer b )
1192{
1193   XMesaContext xmctx = XMesaGetCurrentContext();
1194
1195   if (xmctx && xmctx->xm_buffer == b) {
1196      xmctx->st->flush( xmctx->st,
1197            PIPE_FLUSH_RENDER_CACHE |
1198            PIPE_FLUSH_SWAPBUFFERS |
1199            PIPE_FLUSH_FRAME,
1200            NULL);
1201   }
1202
1203   xmesa_swap_st_framebuffer(b->stfb);
1204}
1205
1206
1207
1208/*
1209 * Copy sub-region of back buffer to front buffer
1210 */
1211void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1212{
1213   xmesa_copy_st_framebuffer(b->stfb,
1214         ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT,
1215         x, y, width, height);
1216}
1217
1218
1219
1220void XMesaFlush( XMesaContext c )
1221{
1222   if (c && c->xm_visual->display) {
1223      XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display);
1224      struct pipe_fence_handle *fence = NULL;
1225
1226      c->st->flush(c->st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
1227      if (fence) {
1228         xmdpy->screen->fence_finish(xmdpy->screen, fence,
1229                                     PIPE_TIMEOUT_INFINITE);
1230         xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL);
1231      }
1232      XFlush( c->xm_visual->display );
1233   }
1234}
1235
1236
1237
1238
1239
1240XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d )
1241{
1242   XMesaBuffer b;
1243   for (b = XMesaBufferList; b; b = b->Next) {
1244      if (b->ws.drawable == d && b->xm_visual->display == dpy) {
1245         return b;
1246      }
1247   }
1248   return NULL;
1249}
1250
1251
1252/**
1253 * Free/destroy all XMesaBuffers associated with given display.
1254 */
1255void xmesa_destroy_buffers_on_display(Display *dpy)
1256{
1257   XMesaBuffer b, next;
1258   for (b = XMesaBufferList; b; b = next) {
1259      next = b->Next;
1260      if (b->xm_visual->display == dpy) {
1261         xmesa_free_buffer(b);
1262         /* delete head of list? */
1263         if (XMesaBufferList == b) {
1264            XMesaBufferList = next;
1265         }
1266      }
1267   }
1268}
1269
1270
1271/*
1272 * Look for XMesaBuffers whose X window has been destroyed.
1273 * Deallocate any such XMesaBuffers.
1274 */
1275void XMesaGarbageCollect( void )
1276{
1277   XMesaBuffer b, next;
1278   for (b=XMesaBufferList; b; b=next) {
1279      next = b->Next;
1280      if (b->xm_visual &&
1281          b->xm_visual->display &&
1282          b->ws.drawable &&
1283          b->type == WINDOW) {
1284         XSync(b->xm_visual->display, False);
1285         if (!window_exists( b->xm_visual->display, b->ws.drawable )) {
1286            /* found a dead window, free the ancillary info */
1287            XMesaDestroyBuffer( b );
1288         }
1289      }
1290   }
1291}
1292
1293
1294
1295
1296PUBLIC void
1297XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
1298                  const int *attrib_list)
1299{
1300}
1301
1302
1303
1304PUBLIC void
1305XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer)
1306{
1307}
1308
1309
1310void
1311XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask)
1312{
1313   if (dst->st->copy)
1314      dst->st->copy(dst->st, src->st, mask);
1315}
1316