Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. /**
  26.  * \file xm_api.c
  27.  *
  28.  * All the XMesa* API functions.
  29.  *
  30.  *
  31.  * NOTES:
  32.  *
  33.  * The window coordinate system origin (0,0) is in the lower-left corner
  34.  * of the window.  X11's window coordinate origin is in the upper-left
  35.  * corner of the window.  Therefore, most drawing functions in this
  36.  * file have to flip Y coordinates.
  37.  *
  38.  *
  39.  * Byte swapping:  If the Mesa host and the X display use a different
  40.  * byte order then there's some trickiness to be aware of when using
  41.  * XImages.  The byte ordering used for the XImage is that of the X
  42.  * display, not the Mesa host.
  43.  * The color-to-pixel encoding for True/DirectColor must be done
  44.  * according to the display's visual red_mask, green_mask, and blue_mask.
  45.  * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
  46.  * do byte swapping if needed.  If one wants to directly "poke" the pixel
  47.  * into the XImage's buffer then the pixel must be byte swapped first.
  48.  *
  49.  */
  50.  
  51. #ifdef __CYGWIN__
  52. #undef WIN32
  53. #undef __WIN32__
  54. #endif
  55.  
  56. #include <stdio.h>
  57. #include "xm_api.h"
  58. #include "xm_st.h"
  59.  
  60. #include "pipe/p_context.h"
  61. #include "pipe/p_defines.h"
  62. #include "pipe/p_screen.h"
  63. #include "pipe/p_state.h"
  64.  
  65. #include "util/u_atomic.h"
  66. #include "util/u_inlines.h"
  67.  
  68. #include "hud/hud_context.h"
  69.  
  70. #include "xm_public.h"
  71. #include <GL/glx.h>
  72.  
  73.  
  74. /* Driver interface routines, set up by xlib backend on library
  75.  * _init().  These are global in the same way that function names are
  76.  * global.
  77.  */
  78. static struct xm_driver driver;
  79. static struct st_api *stapi;
  80.  
  81. /* Default strict invalidate to false.  This means we will not call
  82.  * XGetGeometry after every swapbuffers, which allows swapbuffers to
  83.  * remain asynchronous.  For apps running at 100fps with synchronous
  84.  * swapping, a 10% boost is typical.  For gears, I see closer to 20%
  85.  * speedup.
  86.  *
  87.  * Note that the work of copying data on swapbuffers doesn't disappear
  88.  * - this change just allows the X server to execute the PutImage
  89.  * asynchronously without us effectively blocked until its completion.
  90.  *
  91.  * This speeds up even llvmpipe's threaded rasterization as the
  92.  * swapbuffers operation was a large part of the serial component of
  93.  * an llvmpipe frame.
  94.  *
  95.  * The downside of this is correctness - applications which don't call
  96.  * glViewport on window resizes will get incorrect rendering.  A
  97.  * better solution would be to have per-frame but asynchronous
  98.  * invalidation.  Xcb almost looks as if it could provide this, but
  99.  * the API doesn't seem to quite be there.
  100.  */
  101. boolean xmesa_strict_invalidate = FALSE;
  102.  
  103. void xmesa_set_driver( const struct xm_driver *templ )
  104. {
  105.    driver = *templ;
  106.    stapi = driver.create_st_api();
  107.  
  108.    xmesa_strict_invalidate =
  109.       debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE);
  110. }
  111.  
  112.  
  113. /*
  114.  * XXX replace this with a linked list, or better yet, try to attach the
  115.  * gallium/mesa extra bits to the X Display object with XAddExtension().
  116.  */
  117. #define MAX_DISPLAYS 10
  118. static struct xmesa_display Displays[MAX_DISPLAYS];
  119. static int NumDisplays = 0;
  120.  
  121. static int
  122. xmesa_get_param(struct st_manager *smapi,
  123.                 enum st_manager_param param)
  124. {
  125.    switch(param) {
  126.    case ST_MANAGER_BROKEN_INVALIDATE:
  127.       return !xmesa_strict_invalidate;
  128.    default:
  129.       return 0;
  130.    }
  131. }
  132.  
  133. static XMesaDisplay
  134. xmesa_init_display( Display *display )
  135. {
  136.    pipe_static_mutex(init_mutex);
  137.    XMesaDisplay xmdpy;
  138.    int i;
  139.  
  140.    pipe_mutex_lock(init_mutex);
  141.  
  142.    /* Look for XMesaDisplay which corresponds to 'display' */
  143.    for (i = 0; i < NumDisplays; i++) {
  144.       if (Displays[i].display == display) {
  145.          /* Found it */
  146.          pipe_mutex_unlock(init_mutex);
  147.          return &Displays[i];
  148.       }
  149.    }
  150.  
  151.    /* Create new XMesaDisplay */
  152.  
  153.    assert(NumDisplays < MAX_DISPLAYS);
  154.    xmdpy = &Displays[NumDisplays];
  155.    NumDisplays++;
  156.  
  157.    if (!xmdpy->display && display) {
  158.       xmdpy->display = display;
  159.       xmdpy->screen = driver.create_pipe_screen(display);
  160.       xmdpy->smapi = CALLOC_STRUCT(st_manager);
  161.       if (xmdpy->smapi) {
  162.          xmdpy->smapi->screen = xmdpy->screen;
  163.          xmdpy->smapi->get_param = xmesa_get_param;
  164.       }
  165.  
  166.       if (xmdpy->screen && xmdpy->smapi) {
  167.          pipe_mutex_init(xmdpy->mutex);
  168.       }
  169.       else {
  170.          if (xmdpy->screen) {
  171.             xmdpy->screen->destroy(xmdpy->screen);
  172.             xmdpy->screen = NULL;
  173.          }
  174.          free(xmdpy->smapi);
  175.          xmdpy->smapi = NULL;
  176.  
  177.          xmdpy->display = NULL;
  178.       }
  179.    }
  180.    if (!xmdpy->display || xmdpy->display != display)
  181.       xmdpy = NULL;
  182.  
  183.    pipe_mutex_unlock(init_mutex);
  184.  
  185.    return xmdpy;
  186. }
  187.  
  188. /**********************************************************************/
  189. /*****                     X Utility Functions                    *****/
  190. /**********************************************************************/
  191.  
  192.  
  193. /**
  194.  * Return the host's byte order as LSBFirst or MSBFirst ala X.
  195.  */
  196. static int host_byte_order( void )
  197. {
  198.    int i = 1;
  199.    char *cptr = (char *) &i;
  200.    return (*cptr==1) ? LSBFirst : MSBFirst;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. /**
  207.  * Return the true number of bits per pixel for XImages.
  208.  * For example, if we request a 24-bit deep visual we may actually need/get
  209.  * 32bpp XImages.  This function returns the appropriate bpp.
  210.  * Input:  dpy - the X display
  211.  *         visinfo - desribes the visual to be used for XImages
  212.  * Return:  true number of bits per pixel for XImages
  213.  */
  214. static int
  215. bits_per_pixel( XMesaVisual xmv )
  216. {
  217.    Display *dpy = xmv->display;
  218.    XVisualInfo * visinfo = xmv->visinfo;
  219.    XImage *img;
  220.    int bitsPerPixel;
  221.    /* Create a temporary XImage */
  222.    img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
  223.                        ZPixmap, 0,           /*format, offset*/
  224.                        malloc(8),    /*data*/
  225.                        1, 1,                 /*width, height*/
  226.                        32,                   /*bitmap_pad*/
  227.                        0                     /*bytes_per_line*/
  228.                      );
  229.    assert(img);
  230.    /* grab the bits/pixel value */
  231.    bitsPerPixel = img->bits_per_pixel;
  232.    /* free the XImage */
  233.    free( img->data );
  234.    img->data = NULL;
  235.    XDestroyImage( img );
  236.    return bitsPerPixel;
  237. }
  238.  
  239.  
  240.  
  241. /*
  242.  * Determine if a given X window ID is valid (window exists).
  243.  * Do this by calling XGetWindowAttributes() for the window and
  244.  * checking if we catch an X error.
  245.  * Input:  dpy - the display
  246.  *         win - the window to check for existence
  247.  * Return:  GL_TRUE - window exists
  248.  *          GL_FALSE - window doesn't exist
  249.  */
  250. static GLboolean WindowExistsFlag;
  251.  
  252. static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr )
  253. {
  254.    (void) dpy;
  255.    if (xerr->error_code == BadWindow) {
  256.       WindowExistsFlag = GL_FALSE;
  257.    }
  258.    return 0;
  259. }
  260.  
  261. static GLboolean window_exists( Display *dpy, Window win )
  262. {
  263.    XWindowAttributes wa;
  264.    int (*old_handler)( Display*, XErrorEvent* );
  265.    WindowExistsFlag = GL_TRUE;
  266.    old_handler = XSetErrorHandler(window_exists_err_handler);
  267.    XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
  268.    XSetErrorHandler(old_handler);
  269.    return WindowExistsFlag;
  270. }
  271.  
  272. static Status
  273. get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height )
  274. {
  275.    Window root;
  276.    Status stat;
  277.    int xpos, ypos;
  278.    unsigned int w, h, bw, depth;
  279.    stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
  280.    *width = w;
  281.    *height = h;
  282.    return stat;
  283. }
  284.  
  285.  
  286. /**
  287.  * Return the size of the window (or pixmap) that corresponds to the
  288.  * given XMesaBuffer.
  289.  * \param width  returns width in pixels
  290.  * \param height  returns height in pixels
  291.  */
  292. void
  293. xmesa_get_window_size(Display *dpy, XMesaBuffer b,
  294.                       GLuint *width, GLuint *height)
  295. {
  296.    XMesaDisplay xmdpy = xmesa_init_display(dpy);
  297.    Status stat;
  298.  
  299.    pipe_mutex_lock(xmdpy->mutex);
  300.    stat = get_drawable_size(dpy, b->ws.drawable, width, height);
  301.    pipe_mutex_unlock(xmdpy->mutex);
  302.  
  303.    if (!stat) {
  304.       /* probably querying a window that's recently been destroyed */
  305.       _mesa_warning(NULL, "XGetGeometry failed!\n");
  306.       *width = *height = 1;
  307.    }
  308. }
  309.  
  310. #define GET_REDMASK(__v)        __v->mesa_visual.redMask
  311. #define GET_GREENMASK(__v)      __v->mesa_visual.greenMask
  312. #define GET_BLUEMASK(__v)       __v->mesa_visual.blueMask
  313.  
  314.  
  315. /**
  316.  * Choose the pixel format for the given visual.
  317.  * This will tell the gallium driver how to pack pixel data into
  318.  * drawing surfaces.
  319.  */
  320. static GLuint
  321. choose_pixel_format(XMesaVisual v)
  322. {
  323.    boolean native_byte_order = (host_byte_order() ==
  324.                                 ImageByteOrder(v->display));
  325.  
  326.    if (   GET_REDMASK(v)   == 0x0000ff
  327.        && GET_GREENMASK(v) == 0x00ff00
  328.        && GET_BLUEMASK(v)  == 0xff0000
  329.        && v->BitsPerPixel == 32) {
  330.       if (native_byte_order) {
  331.          /* no byteswapping needed */
  332.          return PIPE_FORMAT_RGBA8888_UNORM;
  333.       }
  334.       else {
  335.          return PIPE_FORMAT_ABGR8888_UNORM;
  336.       }
  337.    }
  338.    else if (   GET_REDMASK(v)   == 0xff0000
  339.             && GET_GREENMASK(v) == 0x00ff00
  340.             && GET_BLUEMASK(v)  == 0x0000ff
  341.             && v->BitsPerPixel == 32) {
  342.       if (native_byte_order) {
  343.          /* no byteswapping needed */
  344.          return PIPE_FORMAT_BGRA8888_UNORM;
  345.       }
  346.       else {
  347.          return PIPE_FORMAT_ARGB8888_UNORM;
  348.       }
  349.    }
  350.    else if (   GET_REDMASK(v)   == 0x0000ff00
  351.             && GET_GREENMASK(v) == 0x00ff0000
  352.             && GET_BLUEMASK(v)  == 0xff000000
  353.             && v->BitsPerPixel == 32) {
  354.       if (native_byte_order) {
  355.          /* no byteswapping needed */
  356.          return PIPE_FORMAT_ARGB8888_UNORM;
  357.       }
  358.       else {
  359.          return PIPE_FORMAT_BGRA8888_UNORM;
  360.       }
  361.    }
  362.    else if (   GET_REDMASK(v)   == 0xf800
  363.             && GET_GREENMASK(v) == 0x07e0
  364.             && GET_BLUEMASK(v)  == 0x001f
  365.             && native_byte_order
  366.             && v->BitsPerPixel == 16) {
  367.       /* 5-6-5 RGB */
  368.       return PIPE_FORMAT_B5G6R5_UNORM;
  369.    }
  370.  
  371.    return PIPE_FORMAT_NONE;
  372. }
  373.  
  374.  
  375. /**
  376.  * Choose a depth/stencil format that satisfies the given depth and
  377.  * stencil sizes.
  378.  */
  379. static enum pipe_format
  380. choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil)
  381. {
  382.    const enum pipe_texture_target target = PIPE_TEXTURE_2D;
  383.    const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL;
  384.    const unsigned sample_count = 0;
  385.    enum pipe_format formats[8], fmt;
  386.    int count, i;
  387.  
  388.    count = 0;
  389.  
  390.    if (depth <= 16 && stencil == 0) {
  391.       formats[count++] = PIPE_FORMAT_Z16_UNORM;
  392.    }
  393.    if (depth <= 24 && stencil == 0) {
  394.       formats[count++] = PIPE_FORMAT_X8Z24_UNORM;
  395.       formats[count++] = PIPE_FORMAT_Z24X8_UNORM;
  396.    }
  397.    if (depth <= 24 && stencil <= 8) {
  398.       formats[count++] = PIPE_FORMAT_S8_UINT_Z24_UNORM;
  399.       formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_UINT;
  400.    }
  401.    if (depth <= 32 && stencil == 0) {
  402.       formats[count++] = PIPE_FORMAT_Z32_UNORM;
  403.    }
  404.  
  405.    fmt = PIPE_FORMAT_NONE;
  406.    for (i = 0; i < count; i++) {
  407.       if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i],
  408.                                              target, sample_count,
  409.                                              tex_usage)) {
  410.          fmt = formats[i];
  411.          break;
  412.       }
  413.    }
  414.  
  415.    return fmt;
  416. }
  417.  
  418.  
  419.  
  420. /**********************************************************************/
  421. /*****                Linked list of XMesaBuffers                 *****/
  422. /**********************************************************************/
  423.  
  424. static XMesaBuffer XMesaBufferList = NULL;
  425.  
  426.  
  427. /**
  428.  * Allocate a new XMesaBuffer object which corresponds to the given drawable.
  429.  * Note that XMesaBuffer is derived from struct gl_framebuffer.
  430.  * The new XMesaBuffer will not have any size (Width=Height=0).
  431.  *
  432.  * \param d  the corresponding X drawable (window or pixmap)
  433.  * \param type  either WINDOW, PIXMAP or PBUFFER, describing d
  434.  * \param vis  the buffer's visual
  435.  * \param cmap  the window's colormap, if known.
  436.  * \return new XMesaBuffer or NULL if any problem
  437.  */
  438. static XMesaBuffer
  439. create_xmesa_buffer(Drawable d, BufferType type,
  440.                     XMesaVisual vis, Colormap cmap)
  441. {
  442.    XMesaDisplay xmdpy = xmesa_init_display(vis->display);
  443.    XMesaBuffer b;
  444.  
  445.    assert(type == WINDOW || type == PIXMAP || type == PBUFFER);
  446.  
  447.    if (!xmdpy)
  448.       return NULL;
  449.  
  450.    b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
  451.    if (!b)
  452.       return NULL;
  453.  
  454.    b->ws.drawable = d;
  455.    b->ws.visual = vis->visinfo->visual;
  456.    b->ws.depth = vis->visinfo->depth;
  457.  
  458.    b->xm_visual = vis;
  459.    b->type = type;
  460.    b->cmap = cmap;
  461.  
  462.    get_drawable_size(vis->display, d, &b->width, &b->height);
  463.  
  464.    /*
  465.     * Create framebuffer, but we'll plug in our own renderbuffers below.
  466.     */
  467.    b->stfb = xmesa_create_st_framebuffer(xmdpy, b);
  468.  
  469.    /* GLX_EXT_texture_from_pixmap */
  470.    b->TextureTarget = 0;
  471.    b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT;
  472.    b->TextureMipmap = 0;
  473.  
  474.    /* insert buffer into linked list */
  475.    b->Next = XMesaBufferList;
  476.    XMesaBufferList = b;
  477.  
  478.    return b;
  479. }
  480.  
  481.  
  482. /**
  483.  * Find an XMesaBuffer by matching X display and colormap but NOT matching
  484.  * the notThis buffer.
  485.  */
  486. XMesaBuffer
  487. xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis)
  488. {
  489.    XMesaBuffer b;
  490.    for (b = XMesaBufferList; b; b = b->Next) {
  491.       if (b->xm_visual->display == dpy &&
  492.           b->cmap == cmap &&
  493.           b != notThis) {
  494.          return b;
  495.       }
  496.    }
  497.    return NULL;
  498. }
  499.  
  500.  
  501. /**
  502.  * Remove buffer from linked list, delete if no longer referenced.
  503.  */
  504. static void
  505. xmesa_free_buffer(XMesaBuffer buffer)
  506. {
  507.    XMesaBuffer prev = NULL, b;
  508.  
  509.    for (b = XMesaBufferList; b; b = b->Next) {
  510.       if (b == buffer) {
  511.          /* unlink buffer from list */
  512.          if (prev)
  513.             prev->Next = buffer->Next;
  514.          else
  515.             XMesaBufferList = buffer->Next;
  516.  
  517.          /* Since the X window for the XMesaBuffer is going away, we don't
  518.           * want to dereference this pointer in the future.
  519.           */
  520.          b->ws.drawable = 0;
  521.  
  522.          /* XXX we should move the buffer to a delete-pending list and destroy
  523.           * the buffer until it is no longer current.
  524.           */
  525.          xmesa_destroy_st_framebuffer(buffer->stfb);
  526.  
  527.          free(buffer);
  528.  
  529.          return;
  530.       }
  531.       /* continue search */
  532.       prev = b;
  533.    }
  534.    /* buffer not found in XMesaBufferList */
  535.    _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
  536. }
  537.  
  538.  
  539.  
  540. /**********************************************************************/
  541. /*****                   Misc Private Functions                   *****/
  542. /**********************************************************************/
  543.  
  544.  
  545. /**
  546.  * When a context is bound for the first time, we can finally finish
  547.  * initializing the context's visual and buffer information.
  548.  * \param v  the XMesaVisual to initialize
  549.  * \param b  the XMesaBuffer to initialize (may be NULL)
  550.  * \param rgb_flag  TRUE = RGBA mode, FALSE = color index mode
  551.  * \param window  the window/pixmap we're rendering into
  552.  * \param cmap  the colormap associated with the window/pixmap
  553.  * \return GL_TRUE=success, GL_FALSE=failure
  554.  */
  555. static GLboolean
  556. initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
  557.                              GLboolean rgb_flag, Drawable window,
  558.                              Colormap cmap)
  559. {
  560.    assert(!b || b->xm_visual == v);
  561.  
  562.    /* Save true bits/pixel */
  563.    v->BitsPerPixel = bits_per_pixel(v);
  564.    assert(v->BitsPerPixel > 0);
  565.  
  566.    if (rgb_flag == GL_FALSE) {
  567.       /* COLOR-INDEXED WINDOW: not supported*/
  568.       return GL_FALSE;
  569.    }
  570.    else {
  571.       /* RGB WINDOW:
  572.        * We support RGB rendering into almost any kind of visual.
  573.        */
  574.       const int xclass = v->visualType;
  575.       if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) {
  576.          _mesa_warning(NULL,
  577.             "XMesa: RGB mode rendering not supported in given visual.\n");
  578.          return GL_FALSE;
  579.       }
  580.       v->mesa_visual.indexBits = 0;
  581.  
  582.       if (v->BitsPerPixel == 32) {
  583.          /* We use XImages for all front/back buffers.  If an X Window or
  584.           * X Pixmap is 32bpp, there's no guarantee that the alpha channel
  585.           * will be preserved.  For XImages we're in luck.
  586.           */
  587.          v->mesa_visual.alphaBits = 8;
  588.       }
  589.    }
  590.  
  591.    /*
  592.     * If MESA_INFO env var is set print out some debugging info
  593.     * which can help Brian figure out what's going on when a user
  594.     * reports bugs.
  595.     */
  596.    if (getenv("MESA_INFO")) {
  597.       printf("X/Mesa visual = %p\n", (void *) v);
  598.       printf("X/Mesa level = %d\n", v->mesa_visual.level);
  599.       printf("X/Mesa depth = %d\n", v->visinfo->depth);
  600.       printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
  601.    }
  602.  
  603.    return GL_TRUE;
  604. }
  605.  
  606.  
  607.  
  608. #define NUM_VISUAL_TYPES   6
  609.  
  610. /**
  611.  * Convert an X visual type to a GLX visual type.
  612.  *
  613.  * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
  614.  *        to be converted.
  615.  * \return If \c visualType is a valid X visual type, a GLX visual type will
  616.  *         be returned.  Otherwise \c GLX_NONE will be returned.
  617.  *
  618.  * \note
  619.  * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
  620.  * DRI CVS tree.
  621.  */
  622. static GLint
  623. xmesa_convert_from_x_visual_type( int visualType )
  624. {
  625.     static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
  626.         GLX_STATIC_GRAY,  GLX_GRAY_SCALE,
  627.         GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
  628.         GLX_TRUE_COLOR,   GLX_DIRECT_COLOR
  629.     };
  630.  
  631.     return ( (unsigned) visualType < NUM_VISUAL_TYPES )
  632.         ? glx_visual_types[ visualType ] : GLX_NONE;
  633. }
  634.  
  635.  
  636. /**********************************************************************/
  637. /*****                       Public Functions                     *****/
  638. /**********************************************************************/
  639.  
  640.  
  641. /*
  642.  * Create a new X/Mesa visual.
  643.  * Input:  display - X11 display
  644.  *         visinfo - an XVisualInfo pointer
  645.  *         rgb_flag - GL_TRUE = RGB mode,
  646.  *                    GL_FALSE = color index mode
  647.  *         alpha_flag - alpha buffer requested?
  648.  *         db_flag - GL_TRUE = double-buffered,
  649.  *                   GL_FALSE = single buffered
  650.  *         stereo_flag - stereo visual?
  651.  *         ximage_flag - GL_TRUE = use an XImage for back buffer,
  652.  *                       GL_FALSE = use an off-screen pixmap for back buffer
  653.  *         depth_size - requested bits/depth values, or zero
  654.  *         stencil_size - requested bits/stencil values, or zero
  655.  *         accum_red_size - requested bits/red accum values, or zero
  656.  *         accum_green_size - requested bits/green accum values, or zero
  657.  *         accum_blue_size - requested bits/blue accum values, or zero
  658.  *         accum_alpha_size - requested bits/alpha accum values, or zero
  659.  *         num_samples - number of samples/pixel if multisampling, or zero
  660.  *         level - visual level, usually 0
  661.  *         visualCaveat - ala the GLX extension, usually GLX_NONE
  662.  * Return;  a new XMesaVisual or 0 if error.
  663.  */
  664. PUBLIC
  665. XMesaVisual XMesaCreateVisual( Display *display,
  666.                                XVisualInfo * visinfo,
  667.                                GLboolean rgb_flag,
  668.                                GLboolean alpha_flag,
  669.                                GLboolean db_flag,
  670.                                GLboolean stereo_flag,
  671.                                GLboolean ximage_flag,
  672.                                GLint depth_size,
  673.                                GLint stencil_size,
  674.                                GLint accum_red_size,
  675.                                GLint accum_green_size,
  676.                                GLint accum_blue_size,
  677.                                GLint accum_alpha_size,
  678.                                GLint num_samples,
  679.                                GLint level,
  680.                                GLint visualCaveat )
  681. {
  682.    XMesaDisplay xmdpy = xmesa_init_display(display);
  683.    XMesaVisual v;
  684.    GLint red_bits, green_bits, blue_bits, alpha_bits;
  685.  
  686.    if (!xmdpy)
  687.       return NULL;
  688.  
  689.    /* For debugging only */
  690.    if (getenv("MESA_XSYNC")) {
  691.       /* This makes debugging X easier.
  692.        * In your debugger, set a breakpoint on _XError to stop when an
  693.        * X protocol error is generated.
  694.        */
  695.       XSynchronize( display, 1 );
  696.    }
  697.  
  698.    v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
  699.    if (!v) {
  700.       return NULL;
  701.    }
  702.  
  703.    v->display = display;
  704.  
  705.    /* Save a copy of the XVisualInfo struct because the user may Xfree()
  706.     * the struct but we may need some of the information contained in it
  707.     * at a later time.
  708.     */
  709.    v->visinfo = malloc(sizeof(*visinfo));
  710.    if (!v->visinfo) {
  711.       free(v);
  712.       return NULL;
  713.    }
  714.    memcpy(v->visinfo, visinfo, sizeof(*visinfo));
  715.  
  716.    v->ximage_flag = ximage_flag;
  717.  
  718.    v->mesa_visual.redMask = visinfo->red_mask;
  719.    v->mesa_visual.greenMask = visinfo->green_mask;
  720.    v->mesa_visual.blueMask = visinfo->blue_mask;
  721.    v->visualID = visinfo->visualid;
  722.    v->screen = visinfo->screen;
  723.  
  724. #if !(defined(__cplusplus) || defined(c_plusplus))
  725.    v->visualType = xmesa_convert_from_x_visual_type(visinfo->class);
  726. #else
  727.    v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
  728. #endif
  729.  
  730.    v->mesa_visual.visualRating = visualCaveat;
  731.  
  732.    if (alpha_flag)
  733.       v->mesa_visual.alphaBits = 8;
  734.  
  735.    (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 );
  736.  
  737.    {
  738.       const int xclass = v->visualType;
  739.       if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
  740.          red_bits   = _mesa_bitcount(GET_REDMASK(v));
  741.          green_bits = _mesa_bitcount(GET_GREENMASK(v));
  742.          blue_bits  = _mesa_bitcount(GET_BLUEMASK(v));
  743.       }
  744.       else {
  745.          /* this is an approximation */
  746.          int depth;
  747.          depth = v->visinfo->depth;
  748.          red_bits = depth / 3;
  749.          depth -= red_bits;
  750.          green_bits = depth / 2;
  751.          depth -= green_bits;
  752.          blue_bits = depth;
  753.          alpha_bits = 0;
  754.          assert( red_bits + green_bits + blue_bits == v->visinfo->depth );
  755.       }
  756.       alpha_bits = v->mesa_visual.alphaBits;
  757.    }
  758.  
  759.    /* initialize visual */
  760.    {
  761.       struct gl_config *vis = &v->mesa_visual;
  762.  
  763.       vis->rgbMode          = GL_TRUE;
  764.       vis->doubleBufferMode = db_flag;
  765.       vis->stereoMode       = stereo_flag;
  766.  
  767.       vis->redBits          = red_bits;
  768.       vis->greenBits        = green_bits;
  769.       vis->blueBits         = blue_bits;
  770.       vis->alphaBits        = alpha_bits;
  771.       vis->rgbBits          = red_bits + green_bits + blue_bits;
  772.  
  773.       vis->indexBits      = 0;
  774.       vis->depthBits      = depth_size;
  775.       vis->stencilBits    = stencil_size;
  776.  
  777.       vis->accumRedBits   = accum_red_size;
  778.       vis->accumGreenBits = accum_green_size;
  779.       vis->accumBlueBits  = accum_blue_size;
  780.       vis->accumAlphaBits = accum_alpha_size;
  781.  
  782.       vis->haveAccumBuffer   = accum_red_size > 0;
  783.       vis->haveDepthBuffer   = depth_size > 0;
  784.       vis->haveStencilBuffer = stencil_size > 0;
  785.  
  786.       vis->numAuxBuffers = 0;
  787.       vis->level = 0;
  788.       vis->sampleBuffers = 0;
  789.       vis->samples = 0;
  790.    }
  791.  
  792.    v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
  793.    if (db_flag)
  794.       v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
  795.    if (stereo_flag) {
  796.       v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
  797.       if (db_flag)
  798.          v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
  799.    }
  800.  
  801.    v->stvis.color_format = choose_pixel_format(v);
  802.    if (v->stvis.color_format == PIPE_FORMAT_NONE) {
  803.       free(v->visinfo);
  804.       free(v);
  805.       return NULL;
  806.    }
  807.  
  808.    v->stvis.depth_stencil_format =
  809.       choose_depth_stencil_format(xmdpy, depth_size, stencil_size);
  810.  
  811.    v->stvis.accum_format = (accum_red_size +
  812.          accum_green_size + accum_blue_size + accum_alpha_size) ?
  813.       PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
  814.  
  815.    v->stvis.samples = num_samples;
  816.    v->stvis.render_buffer = ST_ATTACHMENT_INVALID;
  817.  
  818.    /* XXX minor hack */
  819.    v->mesa_visual.level = level;
  820.    return v;
  821. }
  822.  
  823.  
  824. PUBLIC
  825. void XMesaDestroyVisual( XMesaVisual v )
  826. {
  827.    free(v->visinfo);
  828.    free(v);
  829. }
  830.  
  831.  
  832. /**
  833.  * Return the informative name.
  834.  */
  835. const char *
  836. xmesa_get_name(void)
  837. {
  838.    return stapi->name;
  839. }
  840.  
  841.  
  842. /**
  843.  * Do per-display initializations.
  844.  */
  845. void
  846. xmesa_init( Display *display )
  847. {
  848.    xmesa_init_display(display);
  849. }
  850.  
  851.  
  852. /**
  853.  * Create a new XMesaContext.
  854.  * \param v  the XMesaVisual
  855.  * \param share_list  another XMesaContext with which to share display
  856.  *                    lists or NULL if no sharing is wanted.
  857.  * \return an XMesaContext or NULL if error.
  858.  */
  859. PUBLIC
  860. XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list,
  861.                                  GLuint major, GLuint minor,
  862.                                  GLuint profileMask, GLuint contextFlags)
  863. {
  864.    XMesaDisplay xmdpy = xmesa_init_display(v->display);
  865.    struct st_context_attribs attribs;
  866.    enum st_context_error ctx_err = 0;
  867.    XMesaContext c;
  868.  
  869.    if (!xmdpy)
  870.       goto no_xmesa_context;
  871.  
  872.    /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
  873.    c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
  874.    if (!c)
  875.       goto no_xmesa_context;
  876.  
  877.    c->xm_visual = v;
  878.    c->xm_buffer = NULL;   /* set later by XMesaMakeCurrent */
  879.    c->xm_read_buffer = NULL;
  880.  
  881.    memset(&attribs, 0, sizeof(attribs));
  882.    attribs.visual = v->stvis;
  883.    attribs.major = major;
  884.    attribs.minor = minor;
  885.    if (contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
  886.       attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
  887.    if (contextFlags & GLX_CONTEXT_DEBUG_BIT_ARB)
  888.       attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
  889.    if (contextFlags & GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB)
  890.       attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS;
  891.  
  892.    switch (profileMask) {
  893.    case GLX_CONTEXT_CORE_PROFILE_BIT_ARB:
  894.       /* There are no profiles before OpenGL 3.2.  The
  895.        * GLX_ARB_create_context_profile spec says:
  896.        *
  897.        *     "If the requested OpenGL version is less than 3.2,
  898.        *     GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
  899.        *     of the context is determined solely by the requested version."
  900.        */
  901.       if (major > 3 || (major == 3 && minor >= 2)) {
  902.          attribs.profile = ST_PROFILE_OPENGL_CORE;
  903.          break;
  904.       }
  905.       /* fall-through */
  906.    case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB:
  907.       /*
  908.        * The spec also says:
  909.        *
  910.        *     "If version 3.1 is requested, the context returned may implement
  911.        *     any of the following versions:
  912.        *
  913.        *       * Version 3.1. The GL_ARB_compatibility extension may or may not
  914.        *         be implemented, as determined by the implementation.
  915.        *       * The core profile of version 3.2 or greater."
  916.        *
  917.        * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
  918.        * honour a 3.1 context is through core profile.
  919.        */
  920.       if (major == 3 && minor == 1) {
  921.          attribs.profile = ST_PROFILE_OPENGL_CORE;
  922.       } else {
  923.          attribs.profile = ST_PROFILE_DEFAULT;
  924.       }
  925.       break;
  926.    case GLX_CONTEXT_ES_PROFILE_BIT_EXT:
  927.       if (major >= 2) {
  928.          attribs.profile = ST_PROFILE_OPENGL_ES2;
  929.       } else {
  930.          attribs.profile = ST_PROFILE_OPENGL_ES1;
  931.       }
  932.       break;
  933.    default:
  934.       assert(0);
  935.       goto no_st;
  936.    }
  937.  
  938.    c->st = stapi->create_context(stapi, xmdpy->smapi, &attribs,
  939.          &ctx_err, (share_list) ? share_list->st : NULL);
  940.    if (c->st == NULL)
  941.       goto no_st;
  942.  
  943.    c->st->st_manager_private = (void *) c;
  944.  
  945.    c->hud = hud_create(c->st->pipe, c->st->cso_context);
  946.  
  947.    return c;
  948.  
  949. no_st:
  950.    free(c);
  951. no_xmesa_context:
  952.    return NULL;
  953. }
  954.  
  955.  
  956.  
  957. PUBLIC
  958. void XMesaDestroyContext( XMesaContext c )
  959. {
  960.    if (c->hud) {
  961.       hud_destroy(c->hud);
  962.    }
  963.  
  964.    c->st->destroy(c->st);
  965.  
  966.    /* FIXME: We should destroy the screen here, but if we do so, surfaces may
  967.     * outlive it, causing segfaults
  968.    struct pipe_screen *screen = c->st->pipe->screen;
  969.    screen->destroy(screen);
  970.    */
  971.  
  972.    free(c);
  973. }
  974.  
  975.  
  976.  
  977. /**
  978.  * Private function for creating an XMesaBuffer which corresponds to an
  979.  * X window or pixmap.
  980.  * \param v  the window's XMesaVisual
  981.  * \param w  the window we're wrapping
  982.  * \return  new XMesaBuffer or NULL if error
  983.  */
  984. PUBLIC XMesaBuffer
  985. XMesaCreateWindowBuffer(XMesaVisual v, Window w)
  986. {
  987.    XWindowAttributes attr;
  988.    XMesaBuffer b;
  989.    Colormap cmap;
  990.    int depth;
  991.  
  992.    assert(v);
  993.    assert(w);
  994.  
  995.    /* Check that window depth matches visual depth */
  996.    XGetWindowAttributes( v->display, w, &attr );
  997.    depth = attr.depth;
  998.    if (v->visinfo->depth != depth) {
  999.       _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
  1000.                     v->visinfo->depth, depth);
  1001.       return NULL;
  1002.    }
  1003.  
  1004.    /* Find colormap */
  1005.    if (attr.colormap) {
  1006.       cmap = attr.colormap;
  1007.    }
  1008.    else {
  1009.       _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
  1010.       /* this is weird, a window w/out a colormap!? */
  1011.       /* OK, let's just allocate a new one and hope for the best */
  1012.       cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
  1013.    }
  1014.  
  1015.    b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap);
  1016.    if (!b)
  1017.       return NULL;
  1018.  
  1019.    if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode,
  1020.                                       (Drawable) w, cmap )) {
  1021.       xmesa_free_buffer(b);
  1022.       return NULL;
  1023.    }
  1024.  
  1025.    return b;
  1026. }
  1027.  
  1028.  
  1029.  
  1030. /**
  1031.  * Create a new XMesaBuffer from an X pixmap.
  1032.  *
  1033.  * \param v    the XMesaVisual
  1034.  * \param p    the pixmap
  1035.  * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
  1036.  *             \c GLX_DIRECT_COLOR visual for the pixmap
  1037.  * \returns new XMesaBuffer or NULL if error
  1038.  */
  1039. PUBLIC XMesaBuffer
  1040. XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap)
  1041. {
  1042.    XMesaBuffer b;
  1043.  
  1044.    assert(v);
  1045.  
  1046.    b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
  1047.    if (!b)
  1048.       return NULL;
  1049.  
  1050.    if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
  1051.                                      (Drawable) p, cmap)) {
  1052.       xmesa_free_buffer(b);
  1053.       return NULL;
  1054.    }
  1055.  
  1056.    return b;
  1057. }
  1058.  
  1059.  
  1060. /**
  1061.  * For GLX_EXT_texture_from_pixmap
  1062.  */
  1063. XMesaBuffer
  1064. XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
  1065.                                Colormap cmap,
  1066.                                int format, int target, int mipmap)
  1067. {
  1068.    GET_CURRENT_CONTEXT(ctx);
  1069.    XMesaBuffer b;
  1070.  
  1071.    assert(v);
  1072.  
  1073.    b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
  1074.    if (!b)
  1075.       return NULL;
  1076.  
  1077.    /* get pixmap size */
  1078.    xmesa_get_window_size(v->display, b, &b->width, &b->height);
  1079.  
  1080.    if (target == 0) {
  1081.       /* examine dims */
  1082.       if (ctx->Extensions.ARB_texture_non_power_of_two) {
  1083.          target = GLX_TEXTURE_2D_EXT;
  1084.       }
  1085.       else if (   _mesa_bitcount(b->width)  == 1
  1086.                && _mesa_bitcount(b->height) == 1) {
  1087.          /* power of two size */
  1088.          if (b->height == 1) {
  1089.             target = GLX_TEXTURE_1D_EXT;
  1090.          }
  1091.          else {
  1092.             target = GLX_TEXTURE_2D_EXT;
  1093.          }
  1094.       }
  1095.       else if (ctx->Extensions.NV_texture_rectangle) {
  1096.          target = GLX_TEXTURE_RECTANGLE_EXT;
  1097.       }
  1098.       else {
  1099.          /* non power of two textures not supported */
  1100.          XMesaDestroyBuffer(b);
  1101.          return 0;
  1102.       }
  1103.    }
  1104.  
  1105.    b->TextureTarget = target;
  1106.    b->TextureFormat = format;
  1107.    b->TextureMipmap = mipmap;
  1108.  
  1109.    if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
  1110.                                      (Drawable) p, cmap)) {
  1111.       xmesa_free_buffer(b);
  1112.       return NULL;
  1113.    }
  1114.  
  1115.    return b;
  1116. }
  1117.  
  1118.  
  1119.  
  1120. XMesaBuffer
  1121. XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
  1122.                    unsigned int width, unsigned int height)
  1123. {
  1124.    Window root;
  1125.    Drawable drawable;  /* X Pixmap Drawable */
  1126.    XMesaBuffer b;
  1127.  
  1128.    /* allocate pixmap for front buffer */
  1129.    root = RootWindow( v->display, v->visinfo->screen );
  1130.    drawable = XCreatePixmap(v->display, root, width, height,
  1131.                             v->visinfo->depth);
  1132.    if (!drawable)
  1133.       return NULL;
  1134.  
  1135.    b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
  1136.    if (!b)
  1137.       return NULL;
  1138.  
  1139.    if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
  1140.                                      drawable, cmap)) {
  1141.       xmesa_free_buffer(b);
  1142.       return NULL;
  1143.    }
  1144.  
  1145.    return b;
  1146. }
  1147.  
  1148.  
  1149.  
  1150. /*
  1151.  * Deallocate an XMesaBuffer structure and all related info.
  1152.  */
  1153. PUBLIC void
  1154. XMesaDestroyBuffer(XMesaBuffer b)
  1155. {
  1156.    xmesa_free_buffer(b);
  1157. }
  1158.  
  1159.  
  1160. /**
  1161.  * Notify the binding context to validate the buffer.
  1162.  */
  1163. void
  1164. xmesa_notify_invalid_buffer(XMesaBuffer b)
  1165. {
  1166.    p_atomic_inc(&b->stfb->stamp);
  1167. }
  1168.  
  1169.  
  1170. /**
  1171.  * Query the current drawable size and notify the binding context.
  1172.  */
  1173. void
  1174. xmesa_check_buffer_size(XMesaBuffer b)
  1175. {
  1176.    GLuint old_width, old_height;
  1177.  
  1178.    if (b->type == PBUFFER)
  1179.       return;
  1180.  
  1181.    old_width = b->width;
  1182.    old_height = b->height;
  1183.  
  1184.    xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height);
  1185.  
  1186.    if (b->width != old_width || b->height != old_height)
  1187.       xmesa_notify_invalid_buffer(b);
  1188. }
  1189.  
  1190.  
  1191. /*
  1192.  * Bind buffer b to context c and make c the current rendering context.
  1193.  */
  1194. PUBLIC
  1195. GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
  1196.                              XMesaBuffer readBuffer )
  1197. {
  1198.    XMesaContext old_ctx = XMesaGetCurrentContext();
  1199.  
  1200.    if (old_ctx && old_ctx != c) {
  1201.       XMesaFlush(old_ctx);
  1202.       old_ctx->xm_buffer = NULL;
  1203.       old_ctx->xm_read_buffer = NULL;
  1204.    }
  1205.  
  1206.    if (c) {
  1207.       if (!drawBuffer || !readBuffer)
  1208.          return GL_FALSE;  /* must specify buffers! */
  1209.  
  1210.       if (c == old_ctx &&
  1211.           c->xm_buffer == drawBuffer &&
  1212.           c->xm_read_buffer == readBuffer)
  1213.          return GL_TRUE;
  1214.  
  1215.       xmesa_check_buffer_size(drawBuffer);
  1216.       if (readBuffer != drawBuffer)
  1217.          xmesa_check_buffer_size(readBuffer);
  1218.  
  1219.       c->xm_buffer = drawBuffer;
  1220.       c->xm_read_buffer = readBuffer;
  1221.  
  1222.       stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb);
  1223.  
  1224.       /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
  1225.       drawBuffer->wasCurrent = GL_TRUE;
  1226.    }
  1227.    else {
  1228.       /* Detach */
  1229.       stapi->make_current(stapi, NULL, NULL, NULL);
  1230.  
  1231.    }
  1232.    return GL_TRUE;
  1233. }
  1234.  
  1235.  
  1236. /*
  1237.  * Unbind the context c from its buffer.
  1238.  */
  1239. GLboolean XMesaUnbindContext( XMesaContext c )
  1240. {
  1241.    /* A no-op for XFree86 integration purposes */
  1242.    return GL_TRUE;
  1243. }
  1244.  
  1245.  
  1246. XMesaContext XMesaGetCurrentContext( void )
  1247. {
  1248.    struct st_context_iface *st = stapi->get_current(stapi);
  1249.    return (XMesaContext) (st) ? st->st_manager_private : NULL;
  1250. }
  1251.  
  1252.  
  1253.  
  1254. /**
  1255.  * Swap front and back color buffers and have winsys display front buffer.
  1256.  * If there's no front color buffer no swap actually occurs.
  1257.  */
  1258. PUBLIC
  1259. void XMesaSwapBuffers( XMesaBuffer b )
  1260. {
  1261.    XMesaContext xmctx = XMesaGetCurrentContext();
  1262.  
  1263.    /* Need to draw HUD before flushing */
  1264.    if (xmctx && xmctx->hud) {
  1265.       struct pipe_resource *back =
  1266.          xmesa_get_framebuffer_resource(b->stfb, ST_ATTACHMENT_BACK_LEFT);
  1267.       hud_draw(xmctx->hud, back);
  1268.    }
  1269.  
  1270.    if (xmctx && xmctx->xm_buffer == b) {
  1271.       xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL);
  1272.    }
  1273.  
  1274.    xmesa_swap_st_framebuffer(b->stfb);
  1275. }
  1276.  
  1277.  
  1278.  
  1279. /*
  1280.  * Copy sub-region of back buffer to front buffer
  1281.  */
  1282. void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
  1283. {
  1284.    XMesaContext xmctx = XMesaGetCurrentContext();
  1285.  
  1286.    xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL);
  1287.  
  1288.    xmesa_copy_st_framebuffer(b->stfb,
  1289.          ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT,
  1290.          x, b->height - y - height, width, height);
  1291. }
  1292.  
  1293.  
  1294.  
  1295. void XMesaFlush( XMesaContext c )
  1296. {
  1297.    if (c && c->xm_visual->display) {
  1298.       XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display);
  1299.       struct pipe_fence_handle *fence = NULL;
  1300.  
  1301.       c->st->flush(c->st, ST_FLUSH_FRONT, &fence);
  1302.       if (fence) {
  1303.          xmdpy->screen->fence_finish(xmdpy->screen, fence,
  1304.                                      PIPE_TIMEOUT_INFINITE);
  1305.          xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL);
  1306.       }
  1307.       XFlush( c->xm_visual->display );
  1308.    }
  1309. }
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315. XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d )
  1316. {
  1317.    XMesaBuffer b;
  1318.    for (b = XMesaBufferList; b; b = b->Next) {
  1319.       if (b->ws.drawable == d && b->xm_visual->display == dpy) {
  1320.          return b;
  1321.       }
  1322.    }
  1323.    return NULL;
  1324. }
  1325.  
  1326.  
  1327. /**
  1328.  * Free/destroy all XMesaBuffers associated with given display.
  1329.  */
  1330. void xmesa_destroy_buffers_on_display(Display *dpy)
  1331. {
  1332.    XMesaBuffer b, next;
  1333.    for (b = XMesaBufferList; b; b = next) {
  1334.       next = b->Next;
  1335.       if (b->xm_visual->display == dpy) {
  1336.          xmesa_free_buffer(b);
  1337.          /* delete head of list? */
  1338.          if (XMesaBufferList == b) {
  1339.             XMesaBufferList = next;
  1340.          }
  1341.       }
  1342.    }
  1343. }
  1344.  
  1345.  
  1346. /*
  1347.  * Look for XMesaBuffers whose X window has been destroyed.
  1348.  * Deallocate any such XMesaBuffers.
  1349.  */
  1350. void XMesaGarbageCollect( void )
  1351. {
  1352.    XMesaBuffer b, next;
  1353.    for (b=XMesaBufferList; b; b=next) {
  1354.       next = b->Next;
  1355.       if (b->xm_visual &&
  1356.           b->xm_visual->display &&
  1357.           b->ws.drawable &&
  1358.           b->type == WINDOW) {
  1359.          XSync(b->xm_visual->display, False);
  1360.          if (!window_exists( b->xm_visual->display, b->ws.drawable )) {
  1361.             /* found a dead window, free the ancillary info */
  1362.             XMesaDestroyBuffer( b );
  1363.          }
  1364.       }
  1365.    }
  1366. }
  1367.  
  1368.  
  1369. static enum st_attachment_type xmesa_attachment_type(int glx_attachment)
  1370. {
  1371.    switch(glx_attachment) {
  1372.       case GLX_FRONT_LEFT_EXT:
  1373.          return ST_ATTACHMENT_FRONT_LEFT;
  1374.       case GLX_FRONT_RIGHT_EXT:
  1375.          return ST_ATTACHMENT_FRONT_RIGHT;
  1376.       case GLX_BACK_LEFT_EXT:
  1377.          return ST_ATTACHMENT_BACK_LEFT;
  1378.       case GLX_BACK_RIGHT_EXT:
  1379.          return ST_ATTACHMENT_BACK_RIGHT;
  1380.       default:
  1381.          assert(0);
  1382.          return ST_ATTACHMENT_FRONT_LEFT;
  1383.    }
  1384. }
  1385.  
  1386.  
  1387. PUBLIC void
  1388. XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
  1389.                   const int *attrib_list)
  1390. {
  1391.    struct st_context_iface *st = stapi->get_current(stapi);
  1392.    struct st_framebuffer_iface* stfbi = drawable->stfb;
  1393.    struct pipe_resource *res;
  1394.    int x, y, w, h;
  1395.    enum st_attachment_type st_attachment = xmesa_attachment_type(buffer);
  1396.  
  1397.    x = 0;
  1398.    y = 0;
  1399.    w = drawable->width;
  1400.    h = drawable->height;
  1401.  
  1402.    /* We need to validate our attachments before using them,
  1403.     * in case the texture doesn't exist yet. */
  1404.    xmesa_st_framebuffer_validate_textures(stfbi, w, h, 1 << st_attachment);
  1405.    res = xmesa_get_attachment(stfbi, st_attachment);
  1406.  
  1407.    if (res) {
  1408.       struct pipe_context* pipe = xmesa_get_context(stfbi);
  1409.       enum pipe_format internal_format = res->format;
  1410.       struct pipe_transfer *tex_xfer;
  1411.       char *map;
  1412.       int line, byte_width;
  1413.       XImage *img;
  1414.  
  1415.       internal_format = choose_pixel_format(drawable->xm_visual);
  1416.  
  1417.       map = pipe_transfer_map(pipe, res,
  1418.                               0, 0,    /* level, layer */
  1419.                               PIPE_TRANSFER_WRITE,
  1420.                               x, y,
  1421.                               w, h, &tex_xfer);
  1422.       if (!map)
  1423.          return;
  1424.  
  1425.       /* Grab the XImage that we want to turn into a texture. */
  1426.       img = XGetImage(dpy,
  1427.                       drawable->ws.drawable,
  1428.                       x, y,
  1429.                       w, h,
  1430.                       AllPlanes,
  1431.                       ZPixmap);
  1432.  
  1433.       if (!img) {
  1434.          pipe_transfer_unmap(pipe, tex_xfer);
  1435.          return;
  1436.       }
  1437.  
  1438.       /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */
  1439.       byte_width = w * ((img->bits_per_pixel + 7) / 8);
  1440.  
  1441.       for (line = 0; line < h; line++)
  1442.          memcpy(&map[line * tex_xfer->stride],
  1443.                 &img->data[line * img->bytes_per_line],
  1444.                 byte_width);
  1445.  
  1446.       pipe_transfer_unmap(pipe, tex_xfer);
  1447.  
  1448.       st->teximage(st,
  1449.                    ST_TEXTURE_2D,
  1450.                    0,    /* level */
  1451.                    internal_format,
  1452.                    res,
  1453.                    FALSE /* no mipmap */);
  1454.  
  1455.    }
  1456. }
  1457.  
  1458.  
  1459.  
  1460. PUBLIC void
  1461. XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer)
  1462. {
  1463. }
  1464.  
  1465.  
  1466. void
  1467. XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask)
  1468. {
  1469.    if (dst->st->copy)
  1470.       dst->st->copy(dst->st, src->st, mask);
  1471. }
  1472.