Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2008 Red Hat, Inc.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Soft-
  6.  * ware"), to deal in the Software without restriction, including without
  7.  * limitation the rights to use, copy, modify, merge, publish, distribute,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, provided that the above copyright
  10.  * notice(s) and this permission notice appear in all copies of the Soft-
  11.  * ware and that both the above copyright notice(s) and this permission
  12.  * notice appear in supporting documentation.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16.  * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
  17.  * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
  18.  * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
  19.  * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  20.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  21.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
  22.  * MANCE OF THIS SOFTWARE.
  23.  *
  24.  * Except as contained in this notice, the name of a copyright holder shall
  25.  * not be used in advertising or otherwise to promote the sale, use or
  26.  * other dealings in this Software without prior written authorization of
  27.  * the copyright holder.
  28.  *
  29.  * Authors:
  30.  *   Kristian Høgsberg (krh@redhat.com)
  31.  */
  32.  
  33. #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
  34.  
  35. #include <X11/Xlib.h>
  36. #include <X11/extensions/Xfixes.h>
  37. #include <X11/Xlib-xcb.h>
  38. #include <xcb/xcb.h>
  39. #include <xcb/dri2.h>
  40. #include "glapi.h"
  41. #include "glxclient.h"
  42. #include <X11/extensions/dri2proto.h>
  43. #include "xf86dri.h"
  44. #include <dlfcn.h>
  45. #include <fcntl.h>
  46. #include <unistd.h>
  47. #include <sys/types.h>
  48. #include <sys/mman.h>
  49. #include <sys/time.h>
  50. #include "xf86drm.h"
  51. #include "dri2.h"
  52. #include "dri_common.h"
  53.  
  54. /* From xmlpool/options.h, user exposed so should be stable */
  55. #define DRI_CONF_VBLANK_NEVER 0
  56. #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
  57. #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
  58. #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
  59.  
  60. #undef DRI2_MINOR
  61. #define DRI2_MINOR 1
  62.  
  63. struct dri2_display
  64. {
  65.    __GLXDRIdisplay base;
  66.  
  67.    /*
  68.     ** XFree86-DRI version information
  69.     */
  70.    int driMajor;
  71.    int driMinor;
  72.    int driPatch;
  73.    int swapAvailable;
  74.    int invalidateAvailable;
  75.  
  76.    __glxHashTable *dri2Hash;
  77.  
  78.    const __DRIextension *loader_extensions[4];
  79. };
  80.  
  81. struct dri2_screen {
  82.    struct glx_screen base;
  83.  
  84.    __DRIscreen *driScreen;
  85.    __GLXDRIscreen vtable;
  86.    const __DRIdri2Extension *dri2;
  87.    const __DRIcoreExtension *core;
  88.  
  89.    const __DRI2flushExtension *f;
  90.    const __DRI2configQueryExtension *config;
  91.    const __DRItexBufferExtension *texBuffer;
  92.    const __DRI2throttleExtension *throttle;
  93.    const __DRIconfig **driver_configs;
  94.  
  95.    void *driver;
  96.    int fd;
  97.  
  98.    Bool show_fps;
  99. };
  100.  
  101. struct dri2_context
  102. {
  103.    struct glx_context base;
  104.    __DRIcontext *driContext;
  105. };
  106.  
  107. struct dri2_drawable
  108. {
  109.    __GLXDRIdrawable base;
  110.    __DRIdrawable *driDrawable;
  111.    __DRIbuffer buffers[5];
  112.    int bufferCount;
  113.    int width, height;
  114.    int have_back;
  115.    int have_fake_front;
  116.    int swap_interval;
  117.  
  118.    uint64_t previous_time;
  119.    unsigned frames;
  120. };
  121.  
  122. static const struct glx_context_vtable dri2_context_vtable;
  123.  
  124. /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
  125.  * low halves separately.  This helps you split them.
  126.  */
  127. static void
  128. split_counter(uint64_t counter, uint32_t *hi, uint32_t *lo)
  129. {
  130.    *hi = (counter >> 32);
  131.    *lo = counter & 0xffffffff;
  132. }
  133.  
  134. static uint64_t
  135. merge_counter(uint32_t hi, uint32_t lo)
  136. {
  137.    return ((uint64_t)hi << 32) | lo;
  138. }
  139.  
  140. static void
  141. dri2_destroy_context(struct glx_context *context)
  142. {
  143.    struct dri2_context *pcp = (struct dri2_context *) context;
  144.    struct dri2_screen *psc = (struct dri2_screen *) context->psc;
  145.  
  146.    driReleaseDrawables(&pcp->base);
  147.  
  148.    free((char *) context->extensions);
  149.  
  150.    (*psc->core->destroyContext) (pcp->driContext);
  151.  
  152.    free(pcp);
  153. }
  154.  
  155. static Bool
  156. dri2_bind_context(struct glx_context *context, struct glx_context *old,
  157.                   GLXDrawable draw, GLXDrawable read)
  158. {
  159.    struct dri2_context *pcp = (struct dri2_context *) context;
  160.    struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
  161.    struct dri2_drawable *pdraw, *pread;
  162.    struct dri2_display *pdp;
  163.  
  164.    pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
  165.    pread = (struct dri2_drawable *) driFetchDrawable(context, read);
  166.  
  167.    driReleaseDrawables(&pcp->base);
  168.  
  169.    if (pdraw == NULL || pread == NULL)
  170.       return GLXBadDrawable;
  171.  
  172.    if (!(*psc->core->bindContext) (pcp->driContext,
  173.                                    pdraw->driDrawable, pread->driDrawable))
  174.       return GLXBadContext;
  175.  
  176.    /* If the server doesn't send invalidate events, we may miss a
  177.     * resize before the rendering starts.  Invalidate the buffers now
  178.     * so the driver will recheck before rendering starts. */
  179.    pdp = (struct dri2_display *) psc->base.display;
  180.    if (!pdp->invalidateAvailable) {
  181.       dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
  182.       if (pread != pdraw)
  183.          dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
  184.    }
  185.  
  186.    return Success;
  187. }
  188.  
  189. static void
  190. dri2_unbind_context(struct glx_context *context, struct glx_context *new)
  191. {
  192.    struct dri2_context *pcp = (struct dri2_context *) context;
  193.    struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
  194.  
  195.    (*psc->core->unbindContext) (pcp->driContext);
  196. }
  197.  
  198. static struct glx_context *
  199. dri2_create_context(struct glx_screen *base,
  200.                     struct glx_config *config_base,
  201.                     struct glx_context *shareList, int renderType)
  202. {
  203.    struct dri2_context *pcp, *pcp_shared;
  204.    struct dri2_screen *psc = (struct dri2_screen *) base;
  205.    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
  206.    __DRIcontext *shared = NULL;
  207.  
  208.    /* Check the renderType value */
  209.    if (!validate_renderType_against_config(config_base, renderType))
  210.        return NULL;
  211.  
  212.    if (shareList) {
  213.       /* If the shareList context is not a DRI2 context, we cannot possibly
  214.        * create a DRI2 context that shares it.
  215.        */
  216.       if (shareList->vtable->destroy != dri2_destroy_context) {
  217.          return NULL;
  218.       }
  219.  
  220.       pcp_shared = (struct dri2_context *) shareList;
  221.       shared = pcp_shared->driContext;
  222.    }
  223.  
  224.    pcp = calloc(1, sizeof *pcp);
  225.    if (pcp == NULL)
  226.       return NULL;
  227.  
  228.    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
  229.       free(pcp);
  230.       return NULL;
  231.    }
  232.  
  233.    pcp->base.renderType = renderType;
  234.  
  235.    pcp->driContext =
  236.       (*psc->dri2->createNewContext) (psc->driScreen,
  237.                                       config->driConfig, shared, pcp);
  238.  
  239.    if (pcp->driContext == NULL) {
  240.       free(pcp);
  241.       return NULL;
  242.    }
  243.  
  244.    pcp->base.vtable = &dri2_context_vtable;
  245.  
  246.    return &pcp->base;
  247. }
  248.  
  249. static struct glx_context *
  250. dri2_create_context_attribs(struct glx_screen *base,
  251.                             struct glx_config *config_base,
  252.                             struct glx_context *shareList,
  253.                             unsigned num_attribs,
  254.                             const uint32_t *attribs,
  255.                             unsigned *error)
  256. {
  257.    struct dri2_context *pcp = NULL;
  258.    struct dri2_context *pcp_shared = NULL;
  259.    struct dri2_screen *psc = (struct dri2_screen *) base;
  260.    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
  261.    __DRIcontext *shared = NULL;
  262.  
  263.    uint32_t minor_ver;
  264.    uint32_t major_ver;
  265.    uint32_t renderType;
  266.    uint32_t flags;
  267.    unsigned api;
  268.    int reset;
  269.    uint32_t ctx_attribs[2 * 5];
  270.    unsigned num_ctx_attribs = 0;
  271.  
  272.    if (psc->dri2->base.version < 3) {
  273.       *error = __DRI_CTX_ERROR_NO_MEMORY;
  274.       goto error_exit;
  275.    }
  276.  
  277.    /* Remap the GLX tokens to DRI2 tokens.
  278.     */
  279.    if (!dri2_convert_glx_attribs(num_attribs, attribs,
  280.                                  &major_ver, &minor_ver, &renderType, &flags,
  281.                                  &api, &reset, error))
  282.       goto error_exit;
  283.  
  284.    /* Check the renderType value */
  285.    if (!validate_renderType_against_config(config_base, renderType))
  286.        goto error_exit;
  287.  
  288.    if (shareList) {
  289.       pcp_shared = (struct dri2_context *) shareList;
  290.       shared = pcp_shared->driContext;
  291.    }
  292.  
  293.    pcp = calloc(1, sizeof *pcp);
  294.    if (pcp == NULL) {
  295.       *error = __DRI_CTX_ERROR_NO_MEMORY;
  296.       goto error_exit;
  297.    }
  298.  
  299.    if (!glx_context_init(&pcp->base, &psc->base, &config->base))
  300.       goto error_exit;
  301.  
  302.    ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
  303.    ctx_attribs[num_ctx_attribs++] = major_ver;
  304.    ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
  305.    ctx_attribs[num_ctx_attribs++] = minor_ver;
  306.  
  307.    /* Only send a value when the non-default value is requested.  By doing
  308.     * this we don't have to check the driver's DRI2 version before sending the
  309.     * default value.
  310.     */
  311.    if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
  312.       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
  313.       ctx_attribs[num_ctx_attribs++] = reset;
  314.    }
  315.  
  316.    if (flags != 0) {
  317.       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
  318.  
  319.       /* The current __DRI_CTX_FLAG_* values are identical to the
  320.        * GLX_CONTEXT_*_BIT values.
  321.        */
  322.       ctx_attribs[num_ctx_attribs++] = flags;
  323.    }
  324.  
  325.    /* The renderType is retrieved from attribs, or set to default
  326.     *  of GLX_RGBA_TYPE.
  327.     */
  328.    pcp->base.renderType = renderType;
  329.  
  330.    pcp->driContext =
  331.       (*psc->dri2->createContextAttribs) (psc->driScreen,
  332.                                           api,
  333.                                           config->driConfig,
  334.                                           shared,
  335.                                           num_ctx_attribs / 2,
  336.                                           ctx_attribs,
  337.                                           error,
  338.                                           pcp);
  339.  
  340.    if (pcp->driContext == NULL)
  341.       goto error_exit;
  342.  
  343.    pcp->base.vtable = &dri2_context_vtable;
  344.  
  345.    return &pcp->base;
  346.  
  347. error_exit:
  348.    free(pcp);
  349.  
  350.    return NULL;
  351. }
  352.  
  353. static void
  354. dri2DestroyDrawable(__GLXDRIdrawable *base)
  355. {
  356.    struct dri2_screen *psc = (struct dri2_screen *) base->psc;
  357.    struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
  358.    struct glx_display *dpyPriv = psc->base.display;
  359.    struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
  360.  
  361.    __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
  362.    (*psc->core->destroyDrawable) (pdraw->driDrawable);
  363.  
  364.    /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
  365.     * now, as the application explicitly asked to destroy the GLX
  366.     * drawable.  Otherwise, for legacy drawables, we let the DRI2
  367.     * drawable linger on the server, since there's no good way of
  368.     * knowing when the application is done with it.  The server will
  369.     * destroy the DRI2 drawable when it destroys the X drawable or the
  370.     * client exits anyway. */
  371.    if (pdraw->base.xDrawable != pdraw->base.drawable)
  372.       DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
  373.  
  374.    free(pdraw);
  375. }
  376.  
  377. static __GLXDRIdrawable *
  378. dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
  379.                    GLXDrawable drawable, struct glx_config *config_base)
  380. {
  381.    struct dri2_drawable *pdraw;
  382.    struct dri2_screen *psc = (struct dri2_screen *) base;
  383.    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
  384.    struct glx_display *dpyPriv;
  385.    struct dri2_display *pdp;
  386.    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
  387.  
  388.    pdraw = calloc(1, sizeof(*pdraw));
  389.    if (!pdraw)
  390.       return NULL;
  391.  
  392.    pdraw->base.destroyDrawable = dri2DestroyDrawable;
  393.    pdraw->base.xDrawable = xDrawable;
  394.    pdraw->base.drawable = drawable;
  395.    pdraw->base.psc = &psc->base;
  396.    pdraw->bufferCount = 0;
  397.    pdraw->swap_interval = 1; /* default may be overridden below */
  398.    pdraw->have_back = 0;
  399.  
  400.    if (psc->config)
  401.       psc->config->configQueryi(psc->driScreen,
  402.                                 "vblank_mode", &vblank_mode);
  403.  
  404.    switch (vblank_mode) {
  405.    case DRI_CONF_VBLANK_NEVER:
  406.    case DRI_CONF_VBLANK_DEF_INTERVAL_0:
  407.       pdraw->swap_interval = 0;
  408.       break;
  409.    case DRI_CONF_VBLANK_DEF_INTERVAL_1:
  410.    case DRI_CONF_VBLANK_ALWAYS_SYNC:
  411.    default:
  412.       pdraw->swap_interval = 1;
  413.       break;
  414.    }
  415.  
  416.    DRI2CreateDrawable(psc->base.dpy, xDrawable);
  417.  
  418.    dpyPriv = __glXInitialize(psc->base.dpy);
  419.    pdp = (struct dri2_display *)dpyPriv->dri2Display;;
  420.    /* Create a new drawable */
  421.    pdraw->driDrawable =
  422.       (*psc->dri2->createNewDrawable) (psc->driScreen,
  423.                                        config->driConfig, pdraw);
  424.  
  425.    if (!pdraw->driDrawable) {
  426.       DRI2DestroyDrawable(psc->base.dpy, xDrawable);
  427.       free(pdraw);
  428.       return NULL;
  429.    }
  430.  
  431.    if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
  432.       (*psc->core->destroyDrawable) (pdraw->driDrawable);
  433.       DRI2DestroyDrawable(psc->base.dpy, xDrawable);
  434.       free(pdraw);
  435.       return None;
  436.    }
  437.  
  438.    /*
  439.     * Make sure server has the same swap interval we do for the new
  440.     * drawable.
  441.     */
  442.    if (psc->vtable.setSwapInterval)
  443.       psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
  444.  
  445.    return &pdraw->base;
  446. }
  447.  
  448. static int
  449. dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
  450.                    int64_t *ust, int64_t *msc, int64_t *sbc)
  451. {
  452.    xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
  453.    xcb_dri2_get_msc_cookie_t get_msc_cookie;
  454.    xcb_dri2_get_msc_reply_t *get_msc_reply;
  455.  
  456.    get_msc_cookie = xcb_dri2_get_msc_unchecked(c, pdraw->xDrawable);
  457.    get_msc_reply = xcb_dri2_get_msc_reply(c, get_msc_cookie, NULL);
  458.  
  459.    if (!get_msc_reply)
  460.       return 0;
  461.  
  462.    *ust = merge_counter(get_msc_reply->ust_hi, get_msc_reply->ust_lo);
  463.    *msc = merge_counter(get_msc_reply->msc_hi, get_msc_reply->msc_lo);
  464.    *sbc = merge_counter(get_msc_reply->sbc_hi, get_msc_reply->sbc_lo);
  465.    free(get_msc_reply);
  466.  
  467.    return 1;
  468. }
  469.  
  470. static int
  471. dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
  472.                int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
  473. {
  474.    xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
  475.    xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
  476.    xcb_dri2_wait_msc_reply_t *wait_msc_reply;
  477.    uint32_t target_msc_hi, target_msc_lo;
  478.    uint32_t divisor_hi, divisor_lo;
  479.    uint32_t remainder_hi, remainder_lo;
  480.  
  481.    split_counter(target_msc, &target_msc_hi, &target_msc_lo);
  482.    split_counter(divisor, &divisor_hi, &divisor_lo);
  483.    split_counter(remainder, &remainder_hi, &remainder_lo);
  484.  
  485.    wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
  486.                                                  target_msc_hi, target_msc_lo,
  487.                                                  divisor_hi, divisor_lo,
  488.                                                  remainder_hi, remainder_lo);
  489.    wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
  490.  
  491.    if (!wait_msc_reply)
  492.       return 0;
  493.  
  494.    *ust = merge_counter(wait_msc_reply->ust_hi, wait_msc_reply->ust_lo);
  495.    *msc = merge_counter(wait_msc_reply->msc_hi, wait_msc_reply->msc_lo);
  496.    *sbc = merge_counter(wait_msc_reply->sbc_hi, wait_msc_reply->sbc_lo);
  497.    free(wait_msc_reply);
  498.  
  499.    return 1;
  500. }
  501.  
  502. static int
  503. dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
  504.                int64_t *msc, int64_t *sbc)
  505. {
  506.    xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
  507.    xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie;
  508.    xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
  509.    uint32_t target_sbc_hi, target_sbc_lo;
  510.  
  511.    split_counter(target_sbc, &target_sbc_hi, &target_sbc_lo);
  512.  
  513.    wait_sbc_cookie = xcb_dri2_wait_sbc_unchecked(c, pdraw->xDrawable,
  514.                                                  target_sbc_hi, target_sbc_lo);
  515.    wait_sbc_reply = xcb_dri2_wait_sbc_reply(c, wait_sbc_cookie, NULL);
  516.  
  517.    if (!wait_sbc_reply)
  518.       return 0;
  519.  
  520.    *ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
  521.    *msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
  522.    *sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
  523.    free(wait_sbc_reply);
  524.  
  525.    return 1;
  526. }
  527.  
  528. static __DRIcontext *
  529. dri2GetCurrentContext()
  530. {
  531.    struct glx_context *gc = __glXGetCurrentContext();
  532.    struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
  533.  
  534.    return dri2Ctx ? dri2Ctx->driContext : NULL;
  535. }
  536.  
  537. /**
  538.  * dri2Throttle - Request driver throttling
  539.  *
  540.  * This function uses the DRI2 throttle extension to give the
  541.  * driver the opportunity to throttle on flush front, copysubbuffer
  542.  * and swapbuffers.
  543.  */
  544. static void
  545. dri2Throttle(struct dri2_screen *psc,
  546.              struct dri2_drawable *draw,
  547.              enum __DRI2throttleReason reason)
  548. {
  549.    if (psc->throttle) {
  550.       __DRIcontext *ctx = dri2GetCurrentContext();
  551.  
  552.       psc->throttle->throttle(ctx, draw->driDrawable, reason);
  553.    }
  554. }
  555.  
  556. /**
  557.  * Asks the driver to flush any queued work necessary for serializing with the
  558.  * X command stream, and optionally the slightly more strict requirement of
  559.  * glFlush() equivalence (which would require flushing even if nothing had
  560.  * been drawn to a window system framebuffer, for example).
  561.  */
  562. static void
  563. dri2Flush(struct dri2_screen *psc,
  564.           __DRIcontext *ctx,
  565.           struct dri2_drawable *draw,
  566.           unsigned flags,
  567.           enum __DRI2throttleReason throttle_reason)
  568. {
  569.    if (ctx && psc->f && psc->f->base.version >= 4) {
  570.       psc->f->flush_with_flags(ctx, draw->driDrawable, flags, throttle_reason);
  571.    } else {
  572.       if (flags & __DRI2_FLUSH_CONTEXT)
  573.          glFlush();
  574.  
  575.       if (psc->f)
  576.          psc->f->flush(draw->driDrawable);
  577.  
  578.       dri2Throttle(psc, draw, throttle_reason);
  579.    }
  580. }
  581.  
  582. static void
  583. __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
  584.                     int width, int height,
  585.                     enum __DRI2throttleReason reason, Bool flush)
  586. {
  587.    struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
  588.    struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
  589.    XRectangle xrect;
  590.    XserverRegion region;
  591.    __DRIcontext *ctx = dri2GetCurrentContext();
  592.    unsigned flags;
  593.  
  594.    /* Check we have the right attachments */
  595.    if (!priv->have_back)
  596.       return;
  597.  
  598.    xrect.x = x;
  599.    xrect.y = priv->height - y - height;
  600.    xrect.width = width;
  601.    xrect.height = height;
  602.  
  603.    flags = __DRI2_FLUSH_DRAWABLE;
  604.    if (flush)
  605.       flags |= __DRI2_FLUSH_CONTEXT;
  606.    dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
  607.  
  608.    region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
  609.    DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
  610.                   DRI2BufferFrontLeft, DRI2BufferBackLeft);
  611.  
  612.    /* Refresh the fake front (if present) after we just damaged the real
  613.     * front.
  614.     */
  615.    if (priv->have_fake_front)
  616.       DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
  617.                      DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
  618.  
  619.    XFixesDestroyRegion(psc->base.dpy, region);
  620. }
  621.  
  622. static void
  623. dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
  624.                   int width, int height, Bool flush)
  625. {
  626.    __dri2CopySubBuffer(pdraw, x, y, width, height,
  627.                        __DRI2_THROTTLE_COPYSUBBUFFER, flush);
  628. }
  629.  
  630.  
  631. static void
  632. dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
  633. {
  634.    XRectangle xrect;
  635.    XserverRegion region;
  636.    struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
  637.  
  638.    xrect.x = 0;
  639.    xrect.y = 0;
  640.    xrect.width = priv->width;
  641.    xrect.height = priv->height;
  642.  
  643.    if (psc->f)
  644.       (*psc->f->flush) (priv->driDrawable);
  645.  
  646.    region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
  647.    DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
  648.    XFixesDestroyRegion(psc->base.dpy, region);
  649.  
  650. }
  651.  
  652. static void
  653. dri2_wait_x(struct glx_context *gc)
  654. {
  655.    struct dri2_drawable *priv = (struct dri2_drawable *)
  656.       GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
  657.  
  658.    if (priv == NULL || !priv->have_fake_front)
  659.       return;
  660.  
  661.    dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
  662. }
  663.  
  664. static void
  665. dri2_wait_gl(struct glx_context *gc)
  666. {
  667.    struct dri2_drawable *priv = (struct dri2_drawable *)
  668.       GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
  669.  
  670.    if (priv == NULL || !priv->have_fake_front)
  671.       return;
  672.  
  673.    dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
  674. }
  675.  
  676. /**
  677.  * Called by the driver when it needs to update the real front buffer with the
  678.  * contents of its fake front buffer.
  679.  */
  680. static void
  681. dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
  682. {
  683.    struct glx_display *priv;
  684.    struct dri2_display *pdp;
  685.    struct glx_context *gc;
  686.    struct dri2_drawable *pdraw = loaderPrivate;
  687.    struct dri2_screen *psc;
  688.  
  689.    if (!pdraw)
  690.       return;
  691.  
  692.    if (!pdraw->base.psc)
  693.       return;
  694.  
  695.    psc = (struct dri2_screen *) pdraw->base.psc;
  696.  
  697.    priv = __glXInitialize(psc->base.dpy);
  698.    pdp = (struct dri2_display *) priv->dri2Display;
  699.    gc = __glXGetCurrentContext();
  700.  
  701.    dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
  702.  
  703.    /* Old servers don't send invalidate events */
  704.    if (!pdp->invalidateAvailable)
  705.        dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
  706.  
  707.    dri2_wait_gl(gc);
  708. }
  709.  
  710.  
  711. static void
  712. dri2DestroyScreen(struct glx_screen *base)
  713. {
  714.    struct dri2_screen *psc = (struct dri2_screen *) base;
  715.  
  716.    /* Free the direct rendering per screen data */
  717.    (*psc->core->destroyScreen) (psc->driScreen);
  718.    driDestroyConfigs(psc->driver_configs);
  719.    close(psc->fd);
  720.    free(psc);
  721. }
  722.  
  723. /**
  724.  * Process list of buffer received from the server
  725.  *
  726.  * Processes the list of buffers received in a reply from the server to either
  727.  * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
  728.  */
  729. static void
  730. process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
  731.                 unsigned count)
  732. {
  733.    int i;
  734.  
  735.    pdraw->bufferCount = count;
  736.    pdraw->have_fake_front = 0;
  737.    pdraw->have_back = 0;
  738.  
  739.    /* This assumes the DRI2 buffer attachment tokens matches the
  740.     * __DRIbuffer tokens. */
  741.    for (i = 0; i < count; i++) {
  742.       pdraw->buffers[i].attachment = buffers[i].attachment;
  743.       pdraw->buffers[i].name = buffers[i].name;
  744.       pdraw->buffers[i].pitch = buffers[i].pitch;
  745.       pdraw->buffers[i].cpp = buffers[i].cpp;
  746.       pdraw->buffers[i].flags = buffers[i].flags;
  747.       if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
  748.          pdraw->have_fake_front = 1;
  749.       if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
  750.          pdraw->have_back = 1;
  751.    }
  752.  
  753. }
  754.  
  755. unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
  756. {
  757.       struct glx_display *glx_dpy = __glXInitialize(dpy);
  758.       __GLXDRIdrawable *pdraw;
  759.       pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
  760.       if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
  761.          return 0;
  762.       return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
  763. }
  764.  
  765. static void show_fps(struct dri2_drawable *draw)
  766. {
  767.    struct timeval tv;
  768.    uint64_t current_time;
  769.  
  770.    gettimeofday(&tv, 0);
  771.    current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
  772.  
  773.    draw->frames++;
  774.  
  775.    if (draw->previous_time + 1000000 <= current_time) {
  776.       if (draw->previous_time) {
  777.          fprintf(stderr, "libGL: FPS = %.1f\n",
  778.                  ((uint64_t)draw->frames * 1000000) /
  779.                  (double)(current_time - draw->previous_time));
  780.       }
  781.       draw->frames = 0;
  782.       draw->previous_time = current_time;
  783.    }
  784. }
  785.  
  786. static int64_t
  787. dri2XcbSwapBuffers(Display *dpy,
  788.                   __GLXDRIdrawable *pdraw,
  789.                   int64_t target_msc,
  790.                   int64_t divisor,
  791.                   int64_t remainder)
  792. {
  793.    xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie;
  794.    xcb_dri2_swap_buffers_reply_t *swap_buffers_reply;
  795.    uint32_t target_msc_hi, target_msc_lo;
  796.    uint32_t divisor_hi, divisor_lo;
  797.    uint32_t remainder_hi, remainder_lo;
  798.    int64_t ret = 0;
  799.    xcb_connection_t *c = XGetXCBConnection(dpy);
  800.  
  801.    split_counter(target_msc, &target_msc_hi, &target_msc_lo);
  802.    split_counter(divisor, &divisor_hi, &divisor_lo);
  803.    split_counter(remainder, &remainder_hi, &remainder_lo);
  804.  
  805.    swap_buffers_cookie =
  806.       xcb_dri2_swap_buffers_unchecked(c, pdraw->xDrawable,
  807.                                       target_msc_hi, target_msc_lo,
  808.                                       divisor_hi, divisor_lo,
  809.                                       remainder_hi, remainder_lo);
  810.  
  811.    /* Immediately wait on the swapbuffers reply.  If we didn't, we'd have
  812.     * to do so some time before reusing a (non-pageflipped) backbuffer.
  813.     * Otherwise, the new rendering could get ahead of the X Server's
  814.     * dispatch of the swapbuffer and you'd display garbage.
  815.     *
  816.     * We use XSync() first to reap the invalidate events through the event
  817.     * filter, to ensure that the next drawing doesn't use an invalidated
  818.     * buffer.
  819.     */
  820.    XSync(dpy, False);
  821.  
  822.    swap_buffers_reply =
  823.       xcb_dri2_swap_buffers_reply(c, swap_buffers_cookie, NULL);
  824.    if (swap_buffers_reply) {
  825.       ret = merge_counter(swap_buffers_reply->swap_hi,
  826.                           swap_buffers_reply->swap_lo);
  827.       free(swap_buffers_reply);
  828.    }
  829.    return ret;
  830. }
  831.  
  832. static int64_t
  833. dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
  834.                 int64_t remainder, Bool flush)
  835. {
  836.     struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
  837.     struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
  838.     struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
  839.     struct dri2_display *pdp =
  840.         (struct dri2_display *)dpyPriv->dri2Display;
  841.     int64_t ret = 0;
  842.  
  843.     /* Check we have the right attachments */
  844.     if (!priv->have_back)
  845.         return ret;
  846.  
  847.     /* Old servers can't handle swapbuffers */
  848.     if (!pdp->swapAvailable) {
  849.        __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
  850.                            __DRI2_THROTTLE_SWAPBUFFER, flush);
  851.     } else {
  852.        __DRIcontext *ctx = dri2GetCurrentContext();
  853.        unsigned flags = __DRI2_FLUSH_DRAWABLE;
  854.        if (flush)
  855.           flags |= __DRI2_FLUSH_CONTEXT;
  856.        dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
  857.  
  858.        ret = dri2XcbSwapBuffers(pdraw->psc->dpy, pdraw,
  859.                                 target_msc, divisor, remainder);
  860.     }
  861.  
  862.     if (psc->show_fps) {
  863.        show_fps(priv);
  864.     }
  865.  
  866.     /* Old servers don't send invalidate events */
  867.     if (!pdp->invalidateAvailable)
  868.        dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
  869.  
  870.     return ret;
  871. }
  872.  
  873. static __DRIbuffer *
  874. dri2GetBuffers(__DRIdrawable * driDrawable,
  875.                int *width, int *height,
  876.                unsigned int *attachments, int count,
  877.                int *out_count, void *loaderPrivate)
  878. {
  879.    struct dri2_drawable *pdraw = loaderPrivate;
  880.    DRI2Buffer *buffers;
  881.  
  882.    buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
  883.                             width, height, attachments, count, out_count);
  884.    if (buffers == NULL)
  885.       return NULL;
  886.  
  887.    pdraw->width = *width;
  888.    pdraw->height = *height;
  889.    process_buffers(pdraw, buffers, *out_count);
  890.  
  891.    free(buffers);
  892.  
  893.    return pdraw->buffers;
  894. }
  895.  
  896. static __DRIbuffer *
  897. dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
  898.                          int *width, int *height,
  899.                          unsigned int *attachments, int count,
  900.                          int *out_count, void *loaderPrivate)
  901. {
  902.    struct dri2_drawable *pdraw = loaderPrivate;
  903.    DRI2Buffer *buffers;
  904.  
  905.    buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
  906.                                       pdraw->base.xDrawable,
  907.                                       width, height, attachments,
  908.                                       count, out_count);
  909.    if (buffers == NULL)
  910.       return NULL;
  911.  
  912.    pdraw->width = *width;
  913.    pdraw->height = *height;
  914.    process_buffers(pdraw, buffers, *out_count);
  915.  
  916.    free(buffers);
  917.  
  918.    return pdraw->buffers;
  919. }
  920.  
  921. static int
  922. dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
  923. {
  924.    xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
  925.    struct dri2_drawable *priv =  (struct dri2_drawable *) pdraw;
  926.    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
  927.    struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
  928.  
  929.    if (psc->config)
  930.       psc->config->configQueryi(psc->driScreen,
  931.                                 "vblank_mode", &vblank_mode);
  932.  
  933.    switch (vblank_mode) {
  934.    case DRI_CONF_VBLANK_NEVER:
  935.       if (interval != 0)
  936.          return GLX_BAD_VALUE;
  937.       break;
  938.    case DRI_CONF_VBLANK_ALWAYS_SYNC:
  939.       if (interval <= 0)
  940.          return GLX_BAD_VALUE;
  941.       break;
  942.    default:
  943.       break;
  944.    }
  945.  
  946.    xcb_dri2_swap_interval(c, priv->base.xDrawable, interval);
  947.    priv->swap_interval = interval;
  948.  
  949.    return 0;
  950. }
  951.  
  952. static int
  953. dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
  954. {
  955.    struct dri2_drawable *priv =  (struct dri2_drawable *) pdraw;
  956.  
  957.   return priv->swap_interval;
  958. }
  959.  
  960. static const __DRIdri2LoaderExtension dri2LoaderExtension = {
  961.    {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
  962.    dri2GetBuffers,
  963.    dri2FlushFrontBuffer,
  964.    dri2GetBuffersWithFormat,
  965. };
  966.  
  967. static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
  968.    {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
  969.    dri2GetBuffers,
  970.    dri2FlushFrontBuffer,
  971.    NULL,
  972. };
  973.  
  974. static const __DRIuseInvalidateExtension dri2UseInvalidate = {
  975.    { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
  976. };
  977.  
  978. _X_HIDDEN void
  979. dri2InvalidateBuffers(Display *dpy, XID drawable)
  980. {
  981.    __GLXDRIdrawable *pdraw =
  982.       dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
  983.    struct dri2_screen *psc;
  984.    struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
  985.  
  986.    if (!pdraw)
  987.       return;
  988.  
  989.    psc = (struct dri2_screen *) pdraw->psc;
  990.  
  991.    if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
  992.        psc->f->invalidate(pdp->driDrawable);
  993. }
  994.  
  995. static void
  996. dri2_bind_tex_image(Display * dpy,
  997.                     GLXDrawable drawable,
  998.                     int buffer, const int *attrib_list)
  999. {
  1000.    struct glx_context *gc = __glXGetCurrentContext();
  1001.    struct dri2_context *pcp = (struct dri2_context *) gc;
  1002.    __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
  1003.    struct glx_display *dpyPriv = __glXInitialize(dpy);
  1004.    struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
  1005.    struct dri2_display *pdp =
  1006.       (struct dri2_display *) dpyPriv->dri2Display;
  1007.    struct dri2_screen *psc;
  1008.  
  1009.    if (pdraw != NULL) {
  1010.       psc = (struct dri2_screen *) base->psc;
  1011.  
  1012.       if (!pdp->invalidateAvailable && psc->f &&
  1013.            psc->f->base.version >= 3 && psc->f->invalidate)
  1014.          psc->f->invalidate(pdraw->driDrawable);
  1015.  
  1016.       if (psc->texBuffer->base.version >= 2 &&
  1017.           psc->texBuffer->setTexBuffer2 != NULL) {
  1018.          (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
  1019.                                            pdraw->base.textureTarget,
  1020.                                            pdraw->base.textureFormat,
  1021.                                            pdraw->driDrawable);
  1022.       }
  1023.       else {
  1024.          (*psc->texBuffer->setTexBuffer) (pcp->driContext,
  1025.                                           pdraw->base.textureTarget,
  1026.                                           pdraw->driDrawable);
  1027.       }
  1028.    }
  1029. }
  1030.  
  1031. static void
  1032. dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
  1033. {
  1034. #if __DRI_TEX_BUFFER_VERSION >= 3
  1035.    struct glx_context *gc = __glXGetCurrentContext();
  1036.    struct dri2_context *pcp = (struct dri2_context *) gc;
  1037.    __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
  1038.    struct glx_display *dpyPriv = __glXInitialize(dpy);
  1039.    struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
  1040.    struct dri2_display *pdp =
  1041.       (struct dri2_display *) dpyPriv->dri2Display;
  1042.    struct dri2_screen *psc;
  1043.  
  1044.    if (pdraw != NULL) {
  1045.       psc = (struct dri2_screen *) base->psc;
  1046.  
  1047.       if (psc->texBuffer->base.version >= 3 &&
  1048.           psc->texBuffer->releaseTexBuffer != NULL) {
  1049.          (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
  1050.                                            pdraw->base.textureTarget,
  1051.                                            pdraw->driDrawable);
  1052.       }
  1053.    }
  1054. #endif
  1055. }
  1056.  
  1057. static const struct glx_context_vtable dri2_context_vtable = {
  1058.    dri2_destroy_context,
  1059.    dri2_bind_context,
  1060.    dri2_unbind_context,
  1061.    dri2_wait_gl,
  1062.    dri2_wait_x,
  1063.    DRI_glXUseXFont,
  1064.    dri2_bind_tex_image,
  1065.    dri2_release_tex_image,
  1066.    NULL, /* get_proc_address */
  1067. };
  1068.  
  1069. static void
  1070. dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
  1071.                    const char *driverName)
  1072. {
  1073.    const struct dri2_display *const pdp = (struct dri2_display *)
  1074.       priv->dri2Display;
  1075.    const __DRIextension **extensions;
  1076.    int i;
  1077.  
  1078.    extensions = psc->core->getExtensions(psc->driScreen);
  1079.  
  1080.    __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
  1081.    __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
  1082.    __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
  1083.    __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
  1084.  
  1085.    /*
  1086.     * GLX_INTEL_swap_event is broken on the server side, where it's
  1087.     * currently unconditionally enabled. This completely breaks
  1088.     * systems running on drivers which don't support that extension.
  1089.     * There's no way to test for its presence on this side, so instead
  1090.     * of disabling it unconditionally, just disable it for drivers
  1091.     * which are known to not support it, or for DDX drivers supporting
  1092.     * only an older (pre-ScheduleSwap) version of DRI2.
  1093.     *
  1094.     * This is a hack which is required until:
  1095.     * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
  1096.     * is merged and updated xserver makes it's way into distros:
  1097.     */
  1098.    if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
  1099.       __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
  1100.    }
  1101.  
  1102.    if (psc->dri2->base.version >= 3) {
  1103.       const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
  1104.  
  1105.       __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
  1106.       __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
  1107.  
  1108.       if ((mask & (1 << __DRI_API_GLES2)) != 0)
  1109.          __glXEnableDirectExtension(&psc->base,
  1110.                                     "GLX_EXT_create_context_es2_profile");
  1111.    }
  1112.  
  1113.    for (i = 0; extensions[i]; i++) {
  1114.       if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
  1115.          psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
  1116.          __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
  1117.       }
  1118.  
  1119.       if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
  1120.          psc->f = (__DRI2flushExtension *) extensions[i];
  1121.          /* internal driver extension, no GL extension exposed */
  1122.       }
  1123.  
  1124.       if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
  1125.          psc->config = (__DRI2configQueryExtension *) extensions[i];
  1126.  
  1127.       if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
  1128.          psc->throttle = (__DRI2throttleExtension *) extensions[i];
  1129.  
  1130.       /* DRI2 version 3 is also required because
  1131.        * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
  1132.        */
  1133.       if (psc->dri2->base.version >= 3
  1134.           && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
  1135.          __glXEnableDirectExtension(&psc->base,
  1136.                                     "GLX_ARB_create_context_robustness");
  1137.    }
  1138. }
  1139.  
  1140. static const struct glx_screen_vtable dri2_screen_vtable = {
  1141.    dri2_create_context,
  1142.    dri2_create_context_attribs
  1143. };
  1144.  
  1145. static struct glx_screen *
  1146. dri2CreateScreen(int screen, struct glx_display * priv)
  1147. {
  1148.    const __DRIconfig **driver_configs;
  1149.    const __DRIextension **extensions;
  1150.    const struct dri2_display *const pdp = (struct dri2_display *)
  1151.       priv->dri2Display;
  1152.    struct dri2_screen *psc;
  1153.    __GLXDRIscreen *psp;
  1154.    struct glx_config *configs = NULL, *visuals = NULL;
  1155.    char *driverName, *deviceName, *tmp;
  1156.    drm_magic_t magic;
  1157.    int i;
  1158.  
  1159.    psc = calloc(1, sizeof *psc);
  1160.    if (psc == NULL)
  1161.       return NULL;
  1162.  
  1163.    psc->fd = -1;
  1164.  
  1165.    if (!glx_screen_init(&psc->base, screen, priv)) {
  1166.       free(psc);
  1167.       return NULL;
  1168.    }
  1169.  
  1170.    if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
  1171.                     &driverName, &deviceName)) {
  1172.       glx_screen_cleanup(&psc->base);
  1173.       free(psc);
  1174.       InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
  1175.       return NULL;
  1176.    }
  1177.  
  1178.    psc->driver = driOpenDriver(driverName);
  1179.    if (psc->driver == NULL) {
  1180.       ErrorMessageF("driver pointer missing\n");
  1181.       goto handle_error;
  1182.    }
  1183.  
  1184.    extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
  1185.    if (extensions == NULL) {
  1186.       ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
  1187.       goto handle_error;
  1188.    }
  1189.  
  1190.    for (i = 0; extensions[i]; i++) {
  1191.       if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
  1192.          psc->core = (__DRIcoreExtension *) extensions[i];
  1193.       if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
  1194.          psc->dri2 = (__DRIdri2Extension *) extensions[i];
  1195.    }
  1196.  
  1197.    if (psc->core == NULL || psc->dri2 == NULL) {
  1198.       ErrorMessageF("core dri or dri2 extension not found\n");
  1199.       goto handle_error;
  1200.    }
  1201.  
  1202. #ifdef O_CLOEXEC
  1203.    psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
  1204.    if (psc->fd == -1 && errno == EINVAL)
  1205. #endif
  1206.    {
  1207.       psc->fd = open(deviceName, O_RDWR);
  1208.       if (psc->fd != -1)
  1209.          fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
  1210.    }
  1211.    if (psc->fd < 0) {
  1212.       ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
  1213.       goto handle_error;
  1214.    }
  1215.  
  1216.    if (drmGetMagic(psc->fd, &magic)) {
  1217.       ErrorMessageF("failed to get magic\n");
  1218.       goto handle_error;
  1219.    }
  1220.  
  1221.    if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
  1222.       ErrorMessageF("failed to authenticate magic %d\n", magic);
  1223.       goto handle_error;
  1224.    }
  1225.  
  1226.    
  1227.    /* If the server does not support the protocol for
  1228.     * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
  1229.     */
  1230.    psc->driScreen =
  1231.       psc->dri2->createNewScreen(screen, psc->fd,
  1232.                                  (const __DRIextension **)
  1233.                                  &pdp->loader_extensions[0],
  1234.                                  &driver_configs, psc);
  1235.  
  1236.    if (psc->driScreen == NULL) {
  1237.       ErrorMessageF("failed to create dri screen\n");
  1238.       goto handle_error;
  1239.    }
  1240.  
  1241.    dri2BindExtensions(psc, priv, driverName);
  1242.  
  1243.    configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
  1244.    visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
  1245.  
  1246.    if (!configs || !visuals)
  1247.        goto handle_error;
  1248.  
  1249.    glx_config_destroy_list(psc->base.configs);
  1250.    psc->base.configs = configs;
  1251.    glx_config_destroy_list(psc->base.visuals);
  1252.    psc->base.visuals = visuals;
  1253.  
  1254.    psc->driver_configs = driver_configs;
  1255.  
  1256.    psc->base.vtable = &dri2_screen_vtable;
  1257.    psp = &psc->vtable;
  1258.    psc->base.driScreen = psp;
  1259.    psp->destroyScreen = dri2DestroyScreen;
  1260.    psp->createDrawable = dri2CreateDrawable;
  1261.    psp->swapBuffers = dri2SwapBuffers;
  1262.    psp->getDrawableMSC = NULL;
  1263.    psp->waitForMSC = NULL;
  1264.    psp->waitForSBC = NULL;
  1265.    psp->setSwapInterval = NULL;
  1266.    psp->getSwapInterval = NULL;
  1267.  
  1268.    if (pdp->driMinor >= 2) {
  1269.       psp->getDrawableMSC = dri2DrawableGetMSC;
  1270.       psp->waitForMSC = dri2WaitForMSC;
  1271.       psp->waitForSBC = dri2WaitForSBC;
  1272.       psp->setSwapInterval = dri2SetSwapInterval;
  1273.       psp->getSwapInterval = dri2GetSwapInterval;
  1274.       __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
  1275.    }
  1276.  
  1277.    /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
  1278.     * available.*/
  1279.    psp->copySubBuffer = dri2CopySubBuffer;
  1280.    __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
  1281.  
  1282.    free(driverName);
  1283.    free(deviceName);
  1284.  
  1285.    tmp = getenv("LIBGL_SHOW_FPS");
  1286.    psc->show_fps = tmp && strcmp(tmp, "1") == 0;
  1287.  
  1288.    return &psc->base;
  1289.  
  1290. handle_error:
  1291.    CriticalErrorMessageF("failed to load driver: %s\n", driverName);
  1292.  
  1293.    if (configs)
  1294.        glx_config_destroy_list(configs);
  1295.    if (visuals)
  1296.        glx_config_destroy_list(visuals);
  1297.    if (psc->driScreen)
  1298.        psc->core->destroyScreen(psc->driScreen);
  1299.    psc->driScreen = NULL;
  1300.    if (psc->fd >= 0)
  1301.       close(psc->fd);
  1302.    if (psc->driver)
  1303.       dlclose(psc->driver);
  1304.  
  1305.    free(driverName);
  1306.    free(deviceName);
  1307.    glx_screen_cleanup(&psc->base);
  1308.    free(psc);
  1309.  
  1310.    return NULL;
  1311. }
  1312.  
  1313. /* Called from __glXFreeDisplayPrivate.
  1314.  */
  1315. static void
  1316. dri2DestroyDisplay(__GLXDRIdisplay * dpy)
  1317. {
  1318.    struct dri2_display *pdp = (struct dri2_display *) dpy;
  1319.  
  1320.    __glxHashDestroy(pdp->dri2Hash);
  1321.    free(dpy);
  1322. }
  1323.  
  1324. _X_HIDDEN __GLXDRIdrawable *
  1325. dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
  1326. {
  1327.    struct glx_display *d = __glXInitialize(dpy);
  1328.    struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
  1329.    __GLXDRIdrawable *pdraw;
  1330.  
  1331.    if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
  1332.       return pdraw;
  1333.  
  1334.    return NULL;
  1335. }
  1336.  
  1337. /*
  1338.  * Allocate, initialize and return a __DRIdisplayPrivate object.
  1339.  * This is called from __glXInitialize() when we are given a new
  1340.  * display pointer.
  1341.  */
  1342. _X_HIDDEN __GLXDRIdisplay *
  1343. dri2CreateDisplay(Display * dpy)
  1344. {
  1345.    struct dri2_display *pdp;
  1346.    int eventBase, errorBase, i;
  1347.  
  1348.    if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
  1349.       return NULL;
  1350.  
  1351.    pdp = malloc(sizeof *pdp);
  1352.    if (pdp == NULL)
  1353.       return NULL;
  1354.  
  1355.    if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
  1356.       free(pdp);
  1357.       return NULL;
  1358.    }
  1359.  
  1360.    pdp->driPatch = 0;
  1361.    pdp->swapAvailable = (pdp->driMinor >= 2);
  1362.    pdp->invalidateAvailable = (pdp->driMinor >= 3);
  1363.  
  1364.    pdp->base.destroyDisplay = dri2DestroyDisplay;
  1365.    pdp->base.createScreen = dri2CreateScreen;
  1366.  
  1367.    i = 0;
  1368.    if (pdp->driMinor < 1)
  1369.       pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
  1370.    else
  1371.       pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
  1372.    
  1373.    pdp->loader_extensions[i++] = &systemTimeExtension.base;
  1374.  
  1375.    pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
  1376.  
  1377.    pdp->loader_extensions[i++] = NULL;
  1378.  
  1379.    pdp->dri2Hash = __glxHashCreate();
  1380.    if (pdp->dri2Hash == NULL) {
  1381.       free(pdp);
  1382.       return NULL;
  1383.    }
  1384.  
  1385.    return &pdp->base;
  1386. }
  1387.  
  1388. #endif /* GLX_DIRECT_RENDERING */
  1389.