xm_api.c revision 40fd4323b4be0eee6d4204463737a37011739333
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) * 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (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: 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * The above copyright notice and this permission notice shall be included 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (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 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22868fa2fe829687343ffae624259930155e16dbd8Torne (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 ) 971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci{ 981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 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; 214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) XImage *img; 215 int bitsPerPixel; 216 /* Create a temporary XImage */ 217 img = XCreateImage( dpy, visinfo->visual, visinfo->depth, 218 ZPixmap, 0, /*format, offset*/ 219 (char*) MALLOC(8), /*data*/ 220 1, 1, /*width, height*/ 221 32, /*bitmap_pad*/ 222 0 /*bytes_per_line*/ 223 ); 224 assert(img); 225 /* grab the bits/pixel value */ 226 bitsPerPixel = img->bits_per_pixel; 227 /* free the XImage */ 228 free( img->data ); 229 img->data = NULL; 230 XDestroyImage( img ); 231 return bitsPerPixel; 232} 233 234 235 236/* 237 * Determine if a given X window ID is valid (window exists). 238 * Do this by calling XGetWindowAttributes() for the window and 239 * checking if we catch an X error. 240 * Input: dpy - the display 241 * win - the window to check for existance 242 * Return: GL_TRUE - window exists 243 * GL_FALSE - window doesn't exist 244 */ 245static GLboolean WindowExistsFlag; 246 247static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr ) 248{ 249 (void) dpy; 250 if (xerr->error_code == BadWindow) { 251 WindowExistsFlag = GL_FALSE; 252 } 253 return 0; 254} 255 256static GLboolean window_exists( Display *dpy, Window win ) 257{ 258 XWindowAttributes wa; 259 int (*old_handler)( Display*, XErrorEvent* ); 260 WindowExistsFlag = GL_TRUE; 261 old_handler = XSetErrorHandler(window_exists_err_handler); 262 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */ 263 XSetErrorHandler(old_handler); 264 return WindowExistsFlag; 265} 266 267static Status 268get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height ) 269{ 270 Window root; 271 Status stat; 272 int xpos, ypos; 273 unsigned int w, h, bw, depth; 274 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth); 275 *width = w; 276 *height = h; 277 return stat; 278} 279 280 281/** 282 * Return the size of the window (or pixmap) that corresponds to the 283 * given XMesaBuffer. 284 * \param width returns width in pixels 285 * \param height returns height in pixels 286 */ 287void 288xmesa_get_window_size(Display *dpy, XMesaBuffer b, 289 GLuint *width, GLuint *height) 290{ 291 XMesaDisplay xmdpy = xmesa_init_display(dpy); 292 Status stat; 293 294 pipe_mutex_lock(xmdpy->mutex); 295 stat = get_drawable_size(dpy, b->ws.drawable, width, height); 296 pipe_mutex_unlock(xmdpy->mutex); 297 298 if (!stat) { 299 /* probably querying a window that's recently been destroyed */ 300 _mesa_warning(NULL, "XGetGeometry failed!\n"); 301 *width = *height = 1; 302 } 303} 304 305#define GET_REDMASK(__v) __v->mesa_visual.redMask 306#define GET_GREENMASK(__v) __v->mesa_visual.greenMask 307#define GET_BLUEMASK(__v) __v->mesa_visual.blueMask 308 309 310/** 311 * Choose the pixel format for the given visual. 312 * This will tell the gallium driver how to pack pixel data into 313 * drawing surfaces. 314 */ 315static GLuint 316choose_pixel_format(XMesaVisual v) 317{ 318 boolean native_byte_order = (host_byte_order() == 319 ImageByteOrder(v->display)); 320 321 if ( GET_REDMASK(v) == 0x0000ff 322 && GET_GREENMASK(v) == 0x00ff00 323 && GET_BLUEMASK(v) == 0xff0000 324 && v->BitsPerPixel == 32) { 325 if (native_byte_order) { 326 /* no byteswapping needed */ 327 return PIPE_FORMAT_R8G8B8A8_UNORM; 328 } 329 else { 330 return PIPE_FORMAT_A8B8G8R8_UNORM; 331 } 332 } 333 else if ( GET_REDMASK(v) == 0xff0000 334 && GET_GREENMASK(v) == 0x00ff00 335 && GET_BLUEMASK(v) == 0x0000ff 336 && v->BitsPerPixel == 32) { 337 if (native_byte_order) { 338 /* no byteswapping needed */ 339 return PIPE_FORMAT_B8G8R8A8_UNORM; 340 } 341 else { 342 return PIPE_FORMAT_A8R8G8B8_UNORM; 343 } 344 } 345 else if ( GET_REDMASK(v) == 0x0000ff00 346 && GET_GREENMASK(v) == 0x00ff0000 347 && GET_BLUEMASK(v) == 0xff000000 348 && v->BitsPerPixel == 32) { 349 if (native_byte_order) { 350 /* no byteswapping needed */ 351 return PIPE_FORMAT_A8R8G8B8_UNORM; 352 } 353 else { 354 return PIPE_FORMAT_B8G8R8A8_UNORM; 355 } 356 } 357 else if ( GET_REDMASK(v) == 0xf800 358 && GET_GREENMASK(v) == 0x07e0 359 && GET_BLUEMASK(v) == 0x001f 360 && native_byte_order 361 && v->BitsPerPixel == 16) { 362 /* 5-6-5 RGB */ 363 return PIPE_FORMAT_B5G6R5_UNORM; 364 } 365 366 return PIPE_FORMAT_NONE; 367} 368 369 370/** 371 * Choose a depth/stencil format that satisfies the given depth and 372 * stencil sizes. 373 */ 374static enum pipe_format 375choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil) 376{ 377 const enum pipe_texture_target target = PIPE_TEXTURE_2D; 378 const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL; 379 const unsigned geom_flags = (PIPE_TEXTURE_GEOM_NON_SQUARE | 380 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO); 381 const unsigned sample_count = 0; 382 enum pipe_format formats[8], fmt; 383 int count, i; 384 385 count = 0; 386 387 if (depth <= 16 && stencil == 0) { 388 formats[count++] = PIPE_FORMAT_Z16_UNORM; 389 } 390 if (depth <= 24 && stencil == 0) { 391 formats[count++] = PIPE_FORMAT_X8Z24_UNORM; 392 formats[count++] = PIPE_FORMAT_Z24X8_UNORM; 393 } 394 if (depth <= 24 && stencil <= 8) { 395 formats[count++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM; 396 formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED; 397 } 398 if (depth <= 32 && stencil == 0) { 399 formats[count++] = PIPE_FORMAT_Z32_UNORM; 400 } 401 402 fmt = PIPE_FORMAT_NONE; 403 for (i = 0; i < count; i++) { 404 if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i], 405 target, sample_count, 406 tex_usage, geom_flags)) { 407 fmt = formats[i]; 408 break; 409 } 410 } 411 412 return fmt; 413} 414 415 416 417/**********************************************************************/ 418/***** Linked list of XMesaBuffers *****/ 419/**********************************************************************/ 420 421static XMesaBuffer XMesaBufferList = NULL; 422 423 424/** 425 * Allocate a new XMesaBuffer object which corresponds to the given drawable. 426 * Note that XMesaBuffer is derived from GLframebuffer. 427 * The new XMesaBuffer will not have any size (Width=Height=0). 428 * 429 * \param d the corresponding X drawable (window or pixmap) 430 * \param type either WINDOW, PIXMAP or PBUFFER, describing d 431 * \param vis the buffer's visual 432 * \param cmap the window's colormap, if known. 433 * \return new XMesaBuffer or NULL if any problem 434 */ 435static XMesaBuffer 436create_xmesa_buffer(Drawable d, BufferType type, 437 XMesaVisual vis, Colormap cmap) 438{ 439 XMesaDisplay xmdpy = xmesa_init_display(vis->display); 440 XMesaBuffer b; 441 uint width, height; 442 443 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER); 444 445 if (!xmdpy) 446 return NULL; 447 448 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer); 449 if (!b) 450 return NULL; 451 452 b->ws.drawable = d; 453 b->ws.visual = vis->visinfo->visual; 454 b->ws.depth = vis->visinfo->depth; 455 456 b->xm_visual = vis; 457 b->type = type; 458 b->cmap = cmap; 459 460 get_drawable_size(vis->display, d, &width, &height); 461 462 /* 463 * Create framebuffer, but we'll plug in our own renderbuffers below. 464 */ 465 b->stfb = xmesa_create_st_framebuffer(xmdpy, b); 466 467 /* GLX_EXT_texture_from_pixmap */ 468 b->TextureTarget = 0; 469 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT; 470 b->TextureMipmap = 0; 471 472 /* insert buffer into linked list */ 473 b->Next = XMesaBufferList; 474 XMesaBufferList = b; 475 476 return b; 477} 478 479 480/** 481 * Find an XMesaBuffer by matching X display and colormap but NOT matching 482 * the notThis buffer. 483 */ 484XMesaBuffer 485xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis) 486{ 487 XMesaBuffer b; 488 for (b = XMesaBufferList; b; b = b->Next) { 489 if (b->xm_visual->display == dpy && 490 b->cmap == cmap && 491 b != notThis) { 492 return b; 493 } 494 } 495 return NULL; 496} 497 498 499/** 500 * Remove buffer from linked list, delete if no longer referenced. 501 */ 502static void 503xmesa_free_buffer(XMesaBuffer buffer) 504{ 505 XMesaBuffer prev = NULL, b; 506 507 for (b = XMesaBufferList; b; b = b->Next) { 508 if (b == buffer) { 509 /* unlink buffer from list */ 510 if (prev) 511 prev->Next = buffer->Next; 512 else 513 XMesaBufferList = buffer->Next; 514 515 /* Since the X window for the XMesaBuffer is going away, we don't 516 * want to dereference this pointer in the future. 517 */ 518 b->ws.drawable = 0; 519 520 /* XXX we should move the buffer to a delete-pending list and destroy 521 * the buffer until it is no longer current. 522 */ 523 xmesa_destroy_st_framebuffer(buffer->stfb); 524 525 free(buffer); 526 527 return; 528 } 529 /* continue search */ 530 prev = b; 531 } 532 /* buffer not found in XMesaBufferList */ 533 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n"); 534} 535 536 537 538/**********************************************************************/ 539/***** Misc Private Functions *****/ 540/**********************************************************************/ 541 542 543/** 544 * When a context is bound for the first time, we can finally finish 545 * initializing the context's visual and buffer information. 546 * \param v the XMesaVisual to initialize 547 * \param b the XMesaBuffer to initialize (may be NULL) 548 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode 549 * \param window the window/pixmap we're rendering into 550 * \param cmap the colormap associated with the window/pixmap 551 * \return GL_TRUE=success, GL_FALSE=failure 552 */ 553static GLboolean 554initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b, 555 GLboolean rgb_flag, Drawable window, 556 Colormap cmap) 557{ 558 ASSERT(!b || b->xm_visual == v); 559 560 /* Save true bits/pixel */ 561 v->BitsPerPixel = bits_per_pixel(v); 562 assert(v->BitsPerPixel > 0); 563 564 if (rgb_flag == GL_FALSE) { 565 /* COLOR-INDEXED WINDOW: not supported*/ 566 return GL_FALSE; 567 } 568 else { 569 /* RGB WINDOW: 570 * We support RGB rendering into almost any kind of visual. 571 */ 572 const int xclass = v->mesa_visual.visualType; 573 if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) { 574 _mesa_warning(NULL, 575 "XMesa: RGB mode rendering not supported in given visual.\n"); 576 return GL_FALSE; 577 } 578 v->mesa_visual.indexBits = 0; 579 580 if (v->BitsPerPixel == 32) { 581 /* We use XImages for all front/back buffers. If an X Window or 582 * X Pixmap is 32bpp, there's no guarantee that the alpha channel 583 * will be preserved. For XImages we're in luck. 584 */ 585 v->mesa_visual.alphaBits = 8; 586 } 587 } 588 589 /* 590 * If MESA_INFO env var is set print out some debugging info 591 * which can help Brian figure out what's going on when a user 592 * reports bugs. 593 */ 594 if (_mesa_getenv("MESA_INFO")) { 595 printf("X/Mesa visual = %p\n", (void *) v); 596 printf("X/Mesa level = %d\n", v->mesa_visual.level); 597 printf("X/Mesa depth = %d\n", v->visinfo->depth); 598 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel); 599 } 600 601 return GL_TRUE; 602} 603 604 605 606#define NUM_VISUAL_TYPES 6 607 608/** 609 * Convert an X visual type to a GLX visual type. 610 * 611 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.) 612 * to be converted. 613 * \return If \c visualType is a valid X visual type, a GLX visual type will 614 * be returned. Otherwise \c GLX_NONE will be returned. 615 * 616 * \note 617 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the 618 * DRI CVS tree. 619 */ 620static GLint 621xmesa_convert_from_x_visual_type( int visualType ) 622{ 623 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = { 624 GLX_STATIC_GRAY, GLX_GRAY_SCALE, 625 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR, 626 GLX_TRUE_COLOR, GLX_DIRECT_COLOR 627 }; 628 629 return ( (unsigned) visualType < NUM_VISUAL_TYPES ) 630 ? glx_visual_types[ visualType ] : GLX_NONE; 631} 632 633 634/**********************************************************************/ 635/***** Public Functions *****/ 636/**********************************************************************/ 637 638 639/* 640 * Create a new X/Mesa visual. 641 * Input: display - X11 display 642 * visinfo - an XVisualInfo pointer 643 * rgb_flag - GL_TRUE = RGB mode, 644 * GL_FALSE = color index mode 645 * alpha_flag - alpha buffer requested? 646 * db_flag - GL_TRUE = double-buffered, 647 * GL_FALSE = single buffered 648 * stereo_flag - stereo visual? 649 * ximage_flag - GL_TRUE = use an XImage for back buffer, 650 * GL_FALSE = use an off-screen pixmap for back buffer 651 * depth_size - requested bits/depth values, or zero 652 * stencil_size - requested bits/stencil values, or zero 653 * accum_red_size - requested bits/red accum values, or zero 654 * accum_green_size - requested bits/green accum values, or zero 655 * accum_blue_size - requested bits/blue accum values, or zero 656 * accum_alpha_size - requested bits/alpha accum values, or zero 657 * num_samples - number of samples/pixel if multisampling, or zero 658 * level - visual level, usually 0 659 * visualCaveat - ala the GLX extension, usually GLX_NONE 660 * Return; a new XMesaVisual or 0 if error. 661 */ 662PUBLIC 663XMesaVisual XMesaCreateVisual( Display *display, 664 XVisualInfo * visinfo, 665 GLboolean rgb_flag, 666 GLboolean alpha_flag, 667 GLboolean db_flag, 668 GLboolean stereo_flag, 669 GLboolean ximage_flag, 670 GLint depth_size, 671 GLint stencil_size, 672 GLint accum_red_size, 673 GLint accum_green_size, 674 GLint accum_blue_size, 675 GLint accum_alpha_size, 676 GLint num_samples, 677 GLint level, 678 GLint visualCaveat ) 679{ 680 XMesaDisplay xmdpy = xmesa_init_display(display); 681 XMesaVisual v; 682 GLint red_bits, green_bits, blue_bits, alpha_bits; 683 684 if (!xmdpy) 685 return NULL; 686 687 /* For debugging only */ 688 if (_mesa_getenv("MESA_XSYNC")) { 689 /* This makes debugging X easier. 690 * In your debugger, set a breakpoint on _XError to stop when an 691 * X protocol error is generated. 692 */ 693 XSynchronize( display, 1 ); 694 } 695 696 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual); 697 if (!v) { 698 return NULL; 699 } 700 701 v->display = display; 702 703 /* Save a copy of the XVisualInfo struct because the user may Xfree() 704 * the struct but we may need some of the information contained in it 705 * at a later time. 706 */ 707 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo)); 708 if (!v->visinfo) { 709 free(v); 710 return NULL; 711 } 712 memcpy(v->visinfo, visinfo, sizeof(*visinfo)); 713 714 v->ximage_flag = ximage_flag; 715 716 v->mesa_visual.redMask = visinfo->red_mask; 717 v->mesa_visual.greenMask = visinfo->green_mask; 718 v->mesa_visual.blueMask = visinfo->blue_mask; 719 v->mesa_visual.visualID = visinfo->visualid; 720 v->mesa_visual.screen = visinfo->screen; 721 722#if !(defined(__cplusplus) || defined(c_plusplus)) 723 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->class); 724#else 725 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->c_class); 726#endif 727 728 v->mesa_visual.visualRating = visualCaveat; 729 730 if (alpha_flag) 731 v->mesa_visual.alphaBits = 8; 732 733 (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 ); 734 735 { 736 const int xclass = v->mesa_visual.visualType; 737 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 738 red_bits = _mesa_bitcount(GET_REDMASK(v)); 739 green_bits = _mesa_bitcount(GET_GREENMASK(v)); 740 blue_bits = _mesa_bitcount(GET_BLUEMASK(v)); 741 } 742 else { 743 /* this is an approximation */ 744 int depth; 745 depth = v->visinfo->depth; 746 red_bits = depth / 3; 747 depth -= red_bits; 748 green_bits = depth / 2; 749 depth -= green_bits; 750 blue_bits = depth; 751 alpha_bits = 0; 752 assert( red_bits + green_bits + blue_bits == v->visinfo->depth ); 753 } 754 alpha_bits = v->mesa_visual.alphaBits; 755 } 756 757 /* initialize visual */ 758 { 759 __GLcontextModes *vis = &v->mesa_visual; 760 761 vis->rgbMode = GL_TRUE; 762 vis->doubleBufferMode = db_flag; 763 vis->stereoMode = stereo_flag; 764 765 vis->redBits = red_bits; 766 vis->greenBits = green_bits; 767 vis->blueBits = blue_bits; 768 vis->alphaBits = alpha_bits; 769 vis->rgbBits = red_bits + green_bits + blue_bits; 770 771 vis->indexBits = 0; 772 vis->depthBits = depth_size; 773 vis->stencilBits = stencil_size; 774 775 vis->accumRedBits = accum_red_size; 776 vis->accumGreenBits = accum_green_size; 777 vis->accumBlueBits = accum_blue_size; 778 vis->accumAlphaBits = accum_alpha_size; 779 780 vis->haveAccumBuffer = accum_red_size > 0; 781 vis->haveDepthBuffer = depth_size > 0; 782 vis->haveStencilBuffer = stencil_size > 0; 783 784 vis->numAuxBuffers = 0; 785 vis->level = 0; 786 vis->pixmapMode = 0; 787 vis->sampleBuffers = 0; 788 vis->samples = 0; 789 } 790 791 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK; 792 if (db_flag) 793 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK; 794 if (stereo_flag) { 795 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK; 796 if (db_flag) 797 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK; 798 } 799 800 v->stvis.color_format = choose_pixel_format(v); 801 if (v->stvis.color_format == PIPE_FORMAT_NONE) { 802 FREE(v->visinfo); 803 FREE(v); 804 return NULL; 805 } 806 807 v->stvis.depth_stencil_format = 808 choose_depth_stencil_format(xmdpy, depth_size, stencil_size); 809 810 v->stvis.accum_format = (accum_red_size + 811 accum_green_size + accum_blue_size + accum_alpha_size) ? 812 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE; 813 814 v->stvis.samples = num_samples; 815 v->stvis.render_buffer = ST_ATTACHMENT_INVALID; 816 817 /* XXX minor hack */ 818 v->mesa_visual.level = level; 819 return v; 820} 821 822 823PUBLIC 824void XMesaDestroyVisual( XMesaVisual v ) 825{ 826 free(v->visinfo); 827 free(v); 828} 829 830 831/** 832 * Do per-display initializations. 833 */ 834void 835xmesa_init( Display *display ) 836{ 837 xmesa_init_display(display); 838} 839 840 841/** 842 * Create a new XMesaContext. 843 * \param v the XMesaVisual 844 * \param share_list another XMesaContext with which to share display 845 * lists or NULL if no sharing is wanted. 846 * \return an XMesaContext or NULL if error. 847 */ 848PUBLIC 849XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list ) 850{ 851 XMesaDisplay xmdpy = xmesa_init_display(v->display); 852 XMesaContext c; 853 854 if (!xmdpy) 855 return NULL; 856 857 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */ 858 c = (XMesaContext) CALLOC_STRUCT(xmesa_context); 859 if (!c) 860 return NULL; 861 862 c->xm_visual = v; 863 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */ 864 c->xm_read_buffer = NULL; 865 866 c->st = stapi->create_context(stapi, xmdpy->smapi, 867 &v->stvis, (share_list) ? share_list->st : NULL); 868 if (c->st == NULL) 869 goto fail; 870 871 c->st->st_manager_private = (void *) c; 872 873 return c; 874 875fail: 876 if (c->st) 877 c->st->destroy(c->st); 878 879 free(c); 880 return NULL; 881} 882 883 884 885PUBLIC 886void XMesaDestroyContext( XMesaContext c ) 887{ 888 c->st->destroy(c->st); 889 890 /* FIXME: We should destroy the screen here, but if we do so, surfaces may 891 * outlive it, causing segfaults 892 struct pipe_screen *screen = c->st->pipe->screen; 893 screen->destroy(screen); 894 */ 895 896 free(c); 897} 898 899 900 901/** 902 * Private function for creating an XMesaBuffer which corresponds to an 903 * X window or pixmap. 904 * \param v the window's XMesaVisual 905 * \param w the window we're wrapping 906 * \return new XMesaBuffer or NULL if error 907 */ 908PUBLIC XMesaBuffer 909XMesaCreateWindowBuffer(XMesaVisual v, Window w) 910{ 911 XWindowAttributes attr; 912 XMesaBuffer b; 913 Colormap cmap; 914 int depth; 915 916 assert(v); 917 assert(w); 918 919 /* Check that window depth matches visual depth */ 920 XGetWindowAttributes( v->display, w, &attr ); 921 depth = attr.depth; 922 if (v->visinfo->depth != depth) { 923 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n", 924 v->visinfo->depth, depth); 925 return NULL; 926 } 927 928 /* Find colormap */ 929 if (attr.colormap) { 930 cmap = attr.colormap; 931 } 932 else { 933 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w); 934 /* this is weird, a window w/out a colormap!? */ 935 /* OK, let's just allocate a new one and hope for the best */ 936 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone); 937 } 938 939 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap); 940 if (!b) 941 return NULL; 942 943 if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode, 944 (Drawable) w, cmap )) { 945 xmesa_free_buffer(b); 946 return NULL; 947 } 948 949 return b; 950} 951 952 953 954/** 955 * Create a new XMesaBuffer from an X pixmap. 956 * 957 * \param v the XMesaVisual 958 * \param p the pixmap 959 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or 960 * \c GLX_DIRECT_COLOR visual for the pixmap 961 * \returns new XMesaBuffer or NULL if error 962 */ 963PUBLIC XMesaBuffer 964XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap) 965{ 966 XMesaBuffer b; 967 968 assert(v); 969 970 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 971 if (!b) 972 return NULL; 973 974 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 975 (Drawable) p, cmap)) { 976 xmesa_free_buffer(b); 977 return NULL; 978 } 979 980 return b; 981} 982 983 984/** 985 * For GLX_EXT_texture_from_pixmap 986 */ 987XMesaBuffer 988XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p, 989 Colormap cmap, 990 int format, int target, int mipmap) 991{ 992 GET_CURRENT_CONTEXT(ctx); 993 XMesaBuffer b; 994 995 assert(v); 996 997 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 998 if (!b) 999 return NULL; 1000 1001 /* get pixmap size */ 1002 xmesa_get_window_size(v->display, b, &b->width, &b->height); 1003 1004 if (target == 0) { 1005 /* examine dims */ 1006 if (ctx->Extensions.ARB_texture_non_power_of_two) { 1007 target = GLX_TEXTURE_2D_EXT; 1008 } 1009 else if ( _mesa_bitcount(b->width) == 1 1010 && _mesa_bitcount(b->height) == 1) { 1011 /* power of two size */ 1012 if (b->height == 1) { 1013 target = GLX_TEXTURE_1D_EXT; 1014 } 1015 else { 1016 target = GLX_TEXTURE_2D_EXT; 1017 } 1018 } 1019 else if (ctx->Extensions.NV_texture_rectangle) { 1020 target = GLX_TEXTURE_RECTANGLE_EXT; 1021 } 1022 else { 1023 /* non power of two textures not supported */ 1024 XMesaDestroyBuffer(b); 1025 return 0; 1026 } 1027 } 1028 1029 b->TextureTarget = target; 1030 b->TextureFormat = format; 1031 b->TextureMipmap = mipmap; 1032 1033 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 1034 (Drawable) p, cmap)) { 1035 xmesa_free_buffer(b); 1036 return NULL; 1037 } 1038 1039 return b; 1040} 1041 1042 1043 1044XMesaBuffer 1045XMesaCreatePBuffer(XMesaVisual v, Colormap cmap, 1046 unsigned int width, unsigned int height) 1047{ 1048 Window root; 1049 Drawable drawable; /* X Pixmap Drawable */ 1050 XMesaBuffer b; 1051 1052 /* allocate pixmap for front buffer */ 1053 root = RootWindow( v->display, v->visinfo->screen ); 1054 drawable = XCreatePixmap(v->display, root, width, height, 1055 v->visinfo->depth); 1056 if (!drawable) 1057 return NULL; 1058 1059 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap); 1060 if (!b) 1061 return NULL; 1062 1063 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 1064 drawable, cmap)) { 1065 xmesa_free_buffer(b); 1066 return NULL; 1067 } 1068 1069 return b; 1070} 1071 1072 1073 1074/* 1075 * Deallocate an XMesaBuffer structure and all related info. 1076 */ 1077PUBLIC void 1078XMesaDestroyBuffer(XMesaBuffer b) 1079{ 1080 xmesa_free_buffer(b); 1081} 1082 1083 1084/** 1085 * Query the current drawable size and notify the binding context. 1086 */ 1087void 1088xmesa_check_buffer_size(XMesaBuffer b) 1089{ 1090 XMesaContext xmctx = XMesaGetCurrentContext(); 1091 1092 if (b->type == PBUFFER) 1093 return; 1094 1095 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height); 1096 if (xmctx && xmctx->xm_buffer == b) 1097 xmctx->st->notify_invalid_framebuffer(xmctx->st, b->stfb); 1098} 1099 1100 1101/* 1102 * Bind buffer b to context c and make c the current rendering context. 1103 */ 1104PUBLIC 1105GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer, 1106 XMesaBuffer readBuffer ) 1107{ 1108 XMesaContext old_ctx = XMesaGetCurrentContext(); 1109 1110 if (old_ctx && old_ctx != c) { 1111 XMesaFlush(old_ctx); 1112 old_ctx->xm_buffer = NULL; 1113 old_ctx->xm_read_buffer = NULL; 1114 } 1115 1116 if (c) { 1117 if (!drawBuffer || !readBuffer) 1118 return GL_FALSE; /* must specify buffers! */ 1119 1120 if (c == old_ctx && 1121 c->xm_buffer == drawBuffer && 1122 c->xm_read_buffer == readBuffer) 1123 return GL_TRUE; 1124 1125 xmesa_check_buffer_size(drawBuffer); 1126 if (readBuffer != drawBuffer) 1127 xmesa_check_buffer_size(readBuffer); 1128 1129 c->xm_buffer = drawBuffer; 1130 c->xm_read_buffer = readBuffer; 1131 1132 stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb); 1133 1134 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */ 1135 drawBuffer->wasCurrent = GL_TRUE; 1136 } 1137 else { 1138 /* Detach */ 1139 stapi->make_current(stapi, NULL, NULL, NULL); 1140 1141 } 1142 return GL_TRUE; 1143} 1144 1145 1146/* 1147 * Unbind the context c from its buffer. 1148 */ 1149GLboolean XMesaUnbindContext( XMesaContext c ) 1150{ 1151 /* A no-op for XFree86 integration purposes */ 1152 return GL_TRUE; 1153} 1154 1155 1156XMesaContext XMesaGetCurrentContext( void ) 1157{ 1158 struct st_context_iface *st = stapi->get_current(stapi); 1159 return (XMesaContext) (st) ? st->st_manager_private : NULL; 1160} 1161 1162 1163 1164/** 1165 * Swap front and back color buffers and have winsys display front buffer. 1166 * If there's no front color buffer no swap actually occurs. 1167 */ 1168PUBLIC 1169void XMesaSwapBuffers( XMesaBuffer b ) 1170{ 1171 XMesaContext xmctx = XMesaGetCurrentContext(); 1172 1173 if (xmctx && xmctx->xm_buffer == b) { 1174 xmctx->st->flush( xmctx->st, 1175 PIPE_FLUSH_RENDER_CACHE | 1176 PIPE_FLUSH_SWAPBUFFERS | 1177 PIPE_FLUSH_FRAME, 1178 NULL); 1179 } 1180 1181 xmesa_swap_st_framebuffer(b->stfb); 1182} 1183 1184 1185 1186/* 1187 * Copy sub-region of back buffer to front buffer 1188 */ 1189void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height ) 1190{ 1191 xmesa_copy_st_framebuffer(b->stfb, 1192 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT, 1193 x, y, width, height); 1194} 1195 1196 1197 1198void XMesaFlush( XMesaContext c ) 1199{ 1200 if (c && c->xm_visual->display) { 1201 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display); 1202 struct pipe_fence_handle *fence = NULL; 1203 1204 c->st->flush(c->st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence); 1205 if (fence) { 1206 xmdpy->screen->fence_finish(xmdpy->screen, fence, 0); 1207 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL); 1208 } 1209 XFlush( c->xm_visual->display ); 1210 } 1211} 1212 1213 1214 1215 1216 1217XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d ) 1218{ 1219 XMesaBuffer b; 1220 for (b = XMesaBufferList; b; b = b->Next) { 1221 if (b->ws.drawable == d && b->xm_visual->display == dpy) { 1222 return b; 1223 } 1224 } 1225 return NULL; 1226} 1227 1228 1229/** 1230 * Free/destroy all XMesaBuffers associated with given display. 1231 */ 1232void xmesa_destroy_buffers_on_display(Display *dpy) 1233{ 1234 XMesaBuffer b, next; 1235 for (b = XMesaBufferList; b; b = next) { 1236 next = b->Next; 1237 if (b->xm_visual->display == dpy) { 1238 xmesa_free_buffer(b); 1239 /* delete head of list? */ 1240 if (XMesaBufferList == b) { 1241 XMesaBufferList = next; 1242 } 1243 } 1244 } 1245} 1246 1247 1248/* 1249 * Look for XMesaBuffers whose X window has been destroyed. 1250 * Deallocate any such XMesaBuffers. 1251 */ 1252void XMesaGarbageCollect( void ) 1253{ 1254 XMesaBuffer b, next; 1255 for (b=XMesaBufferList; b; b=next) { 1256 next = b->Next; 1257 if (b->xm_visual && 1258 b->xm_visual->display && 1259 b->ws.drawable && 1260 b->type == WINDOW) { 1261 XSync(b->xm_visual->display, False); 1262 if (!window_exists( b->xm_visual->display, b->ws.drawable )) { 1263 /* found a dead window, free the ancillary info */ 1264 XMesaDestroyBuffer( b ); 1265 } 1266 } 1267 } 1268} 1269 1270 1271 1272 1273PUBLIC void 1274XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer, 1275 const int *attrib_list) 1276{ 1277} 1278 1279 1280 1281PUBLIC void 1282XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer) 1283{ 1284} 1285 1286 1287void 1288XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask) 1289{ 1290 if (dst->st->copy) 1291 dst->st->copy(dst->st, src->st, mask); 1292} 1293