Subversion Repositories Kolibri OS

Rev

Rev 4358 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  4.  * 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
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28. #include <errno.h>
  29. #include <time.h>
  30. #include "main/glheader.h"
  31. #include "main/context.h"
  32. #include "main/framebuffer.h"
  33. #include "main/renderbuffer.h"
  34. #include "main/texobj.h"
  35. #include "main/hash.h"
  36. #include "main/fbobject.h"
  37. #include "main/version.h"
  38. #include "swrast/s_renderbuffer.h"
  39.  
  40. #include "utils.h"
  41. #include "xmlpool.h"
  42.  
  43. PUBLIC const char __driConfigOptions[] =
  44.    DRI_CONF_BEGIN
  45.    DRI_CONF_SECTION_PERFORMANCE
  46.       DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
  47.       /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
  48.        * DRI_CONF_BO_REUSE_ALL
  49.        */
  50.       DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
  51.          DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
  52.             DRI_CONF_ENUM(0, "Disable buffer object reuse")
  53.             DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
  54.          DRI_CONF_DESC_END
  55.       DRI_CONF_OPT_END
  56.  
  57.       DRI_CONF_OPT_BEGIN_B(early_z, "false")
  58.          DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
  59.       DRI_CONF_OPT_END
  60.  
  61.    DRI_CONF_SECTION_END
  62.    DRI_CONF_SECTION_QUALITY
  63.       DRI_CONF_FORCE_S3TC_ENABLE("false")
  64.    DRI_CONF_SECTION_END
  65.    DRI_CONF_SECTION_DEBUG
  66.       DRI_CONF_NO_RAST("false")
  67.       DRI_CONF_ALWAYS_FLUSH_BATCH("false")
  68.       DRI_CONF_ALWAYS_FLUSH_CACHE("false")
  69.       DRI_CONF_DISABLE_THROTTLING("false")
  70.       DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
  71.       DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
  72.       DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
  73.  
  74.       DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
  75.          DRI_CONF_DESC(en, "Perform code generation at shader link time.")
  76.       DRI_CONF_OPT_END
  77.    DRI_CONF_SECTION_END
  78. DRI_CONF_END;
  79.  
  80. const GLuint __driNConfigOptions = 12;
  81.  
  82. #include "intel_batchbuffer.h"
  83. #include "intel_buffers.h"
  84. #include "intel_bufmgr.h"
  85. #include "intel_chipset.h"
  86. #include "intel_fbo.h"
  87. #include "intel_mipmap_tree.h"
  88. #include "intel_screen.h"
  89. #include "intel_tex.h"
  90. #include "intel_regions.h"
  91.  
  92. #include "i915_drm.h"
  93.  
  94. #ifdef USE_NEW_INTERFACE
  95. static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
  96. #endif /*USE_NEW_INTERFACE */
  97.  
  98. /**
  99.  * For debugging purposes, this returns a time in seconds.
  100.  */
  101. double
  102. get_time(void)
  103. {
  104.    struct timespec tp;
  105.  
  106. //   clock_gettime(CLOCK_MONOTONIC, &tp);
  107.  
  108.    return tp.tv_sec + tp.tv_nsec / 1000000000.0;
  109. }
  110.  
  111. void
  112. aub_dump_bmp(struct gl_context *ctx)
  113. {
  114.    struct gl_framebuffer *fb = ctx->DrawBuffer;
  115.  
  116.    for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
  117.       struct intel_renderbuffer *irb =
  118.          intel_renderbuffer(fb->_ColorDrawBuffers[i]);
  119.  
  120.       if (irb && irb->mt) {
  121.          enum aub_dump_bmp_format format;
  122.  
  123.          switch (irb->Base.Base.Format) {
  124.          case MESA_FORMAT_ARGB8888:
  125.          case MESA_FORMAT_XRGB8888:
  126.             format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
  127.             break;
  128.          default:
  129.             continue;
  130.          }
  131.  
  132.          assert(irb->mt->region->pitch % irb->mt->region->cpp == 0);
  133.          drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
  134.                                        irb->draw_x,
  135.                                        irb->draw_y,
  136.                                        irb->Base.Base.Width,
  137.                                        irb->Base.Base.Height,
  138.                                        format,
  139.                                        irb->mt->region->pitch,
  140.                                        0);
  141.       }
  142.    }
  143. }
  144.  
  145. static const __DRItexBufferExtension intelTexBufferExtension = {
  146.    .base = { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
  147.  
  148.    .setTexBuffer        = intelSetTexBuffer,
  149.    .setTexBuffer2       = intelSetTexBuffer2,
  150.    .releaseTexBuffer    = NULL,
  151. };
  152.  
  153. static void
  154. intelDRI2Flush(__DRIdrawable *drawable)
  155. {
  156.    GET_CURRENT_CONTEXT(ctx);
  157.    struct intel_context *intel = intel_context(ctx);
  158.    if (intel == NULL)
  159.       return;
  160.  
  161.    INTEL_FIREVERTICES(intel);
  162.  
  163.    intel->need_throttle = true;
  164.  
  165.    if (intel->batch.used)
  166.       intel_batchbuffer_flush(intel);
  167.  
  168.    if (INTEL_DEBUG & DEBUG_AUB) {
  169.       aub_dump_bmp(ctx);
  170.    }
  171. }
  172.  
  173. static const struct __DRI2flushExtensionRec intelFlushExtension = {
  174.     .base = { __DRI2_FLUSH, 3 },
  175.  
  176.     .flush              = intelDRI2Flush,
  177.     .invalidate         = dri2InvalidateDrawable,
  178. };
  179.  
  180. static struct intel_image_format intel_image_formats[] = {
  181.    { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
  182.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
  183.  
  184.    { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
  185.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
  186.  
  187.    { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
  188.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  189.        { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
  190.        { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
  191.  
  192.    { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
  193.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  194.        { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  195.        { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
  196.  
  197.    { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
  198.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  199.        { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
  200.        { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
  201.  
  202.    { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
  203.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  204.        { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  205.        { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
  206.  
  207.    { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
  208.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  209.        { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  210.        { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
  211.  
  212.    { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
  213.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  214.        { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
  215.  
  216.    { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
  217.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
  218.        { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
  219.  
  220.    /* For YUYV buffers, we set up two overlapping DRI images and treat
  221.     * them as planar buffers in the compositors.  Plane 0 is GR88 and
  222.     * samples YU or YV pairs and places Y into the R component, while
  223.     * plane 1 is ARGB and samples YUYV clusters and places pairs and
  224.     * places U into the G component and V into A.  This lets the
  225.     * texture sampler interpolate the Y components correctly when
  226.     * sampling from plane 0, and interpolate U and V correctly when
  227.     * sampling from plane 1. */
  228.    { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
  229.      { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
  230.        { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
  231. };
  232.  
  233. static __DRIimage *
  234. intel_allocate_image(int dri_format, void *loaderPrivate)
  235. {
  236.     __DRIimage *image;
  237.  
  238.     image = calloc(1, sizeof *image);
  239.     if (image == NULL)
  240.         return NULL;
  241.  
  242.     image->dri_format = dri_format;
  243.     image->offset = 0;
  244.  
  245.     switch (dri_format) {
  246.     case __DRI_IMAGE_FORMAT_RGB565:
  247.        image->format = MESA_FORMAT_RGB565;
  248.        break;
  249.     case __DRI_IMAGE_FORMAT_XRGB8888:
  250.        image->format = MESA_FORMAT_XRGB8888;
  251.        break;
  252.     case __DRI_IMAGE_FORMAT_ARGB8888:
  253.        image->format = MESA_FORMAT_ARGB8888;
  254.        break;
  255.     case __DRI_IMAGE_FORMAT_ABGR8888:
  256.        image->format = MESA_FORMAT_RGBA8888_REV;
  257.        break;
  258.     case __DRI_IMAGE_FORMAT_XBGR8888:
  259.        image->format = MESA_FORMAT_RGBX8888_REV;
  260.        break;
  261.     case __DRI_IMAGE_FORMAT_R8:
  262.        image->format = MESA_FORMAT_R8;
  263.        break;
  264.     case __DRI_IMAGE_FORMAT_GR88:
  265.        image->format = MESA_FORMAT_GR88;
  266.        break;
  267.     case __DRI_IMAGE_FORMAT_NONE:
  268.        image->format = MESA_FORMAT_NONE;
  269.        break;
  270.     default:
  271.        free(image);
  272.        return NULL;
  273.     }
  274.  
  275.     image->internal_format = _mesa_get_format_base_format(image->format);
  276.     image->data = loaderPrivate;
  277.  
  278.     return image;
  279. }
  280.  
  281. /**
  282.  * Sets up a DRIImage structure to point to our shared image in a region
  283.  */
  284. static void
  285. intel_setup_image_from_mipmap_tree(struct intel_context *intel, __DRIimage *image,
  286.                                    struct intel_mipmap_tree *mt, GLuint level,
  287.                                    GLuint zoffset)
  288. {
  289.    unsigned int draw_x, draw_y;
  290.    uint32_t mask_x, mask_y;
  291.  
  292.    intel_miptree_check_level_layer(mt, level, zoffset);
  293.  
  294.    intel_region_get_tile_masks(mt->region, &mask_x, &mask_y, false);
  295.    intel_miptree_get_image_offset(mt, level, zoffset, &draw_x, &draw_y);
  296.  
  297.    image->width = mt->level[level].width;
  298.    image->height = mt->level[level].height;
  299.    image->tile_x = draw_x & mask_x;
  300.    image->tile_y = draw_y & mask_y;
  301.  
  302.    image->offset = intel_region_get_aligned_offset(mt->region,
  303.                                                    draw_x & ~mask_x,
  304.                                                    draw_y & ~mask_y,
  305.                                                    false);
  306.  
  307.    intel_region_reference(&image->region, mt->region);
  308. }
  309.  
  310. static void
  311. intel_setup_image_from_dimensions(__DRIimage *image)
  312. {
  313.    image->width    = image->region->width;
  314.    image->height   = image->region->height;
  315.    image->tile_x = 0;
  316.    image->tile_y = 0;
  317. }
  318.  
  319. static inline uint32_t
  320. intel_dri_format(GLuint format)
  321. {
  322.    switch (format) {
  323.    case MESA_FORMAT_RGB565:
  324.       return __DRI_IMAGE_FORMAT_RGB565;
  325.    case MESA_FORMAT_XRGB8888:
  326.       return __DRI_IMAGE_FORMAT_XRGB8888;
  327.    case MESA_FORMAT_ARGB8888:
  328.       return __DRI_IMAGE_FORMAT_ARGB8888;
  329.    case MESA_FORMAT_RGBA8888_REV:
  330.       return __DRI_IMAGE_FORMAT_ABGR8888;
  331.    case MESA_FORMAT_R8:
  332.       return __DRI_IMAGE_FORMAT_R8;
  333.    case MESA_FORMAT_RG88:
  334.       return __DRI_IMAGE_FORMAT_GR88;
  335.    }
  336.  
  337.    return MESA_FORMAT_NONE;
  338. }
  339.  
  340. static __DRIimage *
  341. intel_create_image_from_name(__DRIscreen *screen,
  342.                              int width, int height, int format,
  343.                              int name, int pitch, void *loaderPrivate)
  344. {
  345.     struct intel_screen *intelScreen = screen->driverPrivate;
  346.     __DRIimage *image;
  347.     int cpp;
  348.  
  349.     image = intel_allocate_image(format, loaderPrivate);
  350.     if (image == NULL)
  351.        return NULL;
  352.  
  353.     if (image->format == MESA_FORMAT_NONE)
  354.        cpp = 1;
  355.     else
  356.        cpp = _mesa_get_format_bytes(image->format);
  357.     image->region = intel_region_alloc_for_handle(intelScreen,
  358.                                                   cpp, width, height,
  359.                                                   pitch * cpp, name, "image");
  360.     if (image->region == NULL) {
  361.        free(image);
  362.        return NULL;
  363.     }
  364.  
  365.     intel_setup_image_from_dimensions(image);
  366.  
  367.     return image;      
  368. }
  369.  
  370. static __DRIimage *
  371. intel_create_image_from_renderbuffer(__DRIcontext *context,
  372.                                      int renderbuffer, void *loaderPrivate)
  373. {
  374.    __DRIimage *image;
  375.    struct intel_context *intel = context->driverPrivate;
  376.    struct gl_renderbuffer *rb;
  377.    struct intel_renderbuffer *irb;
  378.  
  379.    rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
  380.    if (!rb) {
  381.       _mesa_error(&intel->ctx,
  382.                   GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
  383.       return NULL;
  384.    }
  385.  
  386.    irb = intel_renderbuffer(rb);
  387.    image = calloc(1, sizeof *image);
  388.    if (image == NULL)
  389.       return NULL;
  390.  
  391.    image->internal_format = rb->InternalFormat;
  392.    image->format = rb->Format;
  393.    image->offset = 0;
  394.    image->data = loaderPrivate;
  395.    intel_region_reference(&image->region, irb->mt->region);
  396.    intel_setup_image_from_dimensions(image);
  397.    image->dri_format = intel_dri_format(image->format);
  398.  
  399.    rb->NeedsFinishRenderTexture = true;
  400.    return image;
  401. }
  402.  
  403. static __DRIimage *
  404. intel_create_image_from_texture(__DRIcontext *context, int target,
  405.                                 unsigned texture, int zoffset,
  406.                                 int level,
  407.                                 unsigned *error,
  408.                                 void *loaderPrivate)
  409. {
  410.    __DRIimage *image;
  411.    struct intel_context *intel = context->driverPrivate;
  412.    struct gl_texture_object *obj;
  413.    struct intel_texture_object *iobj;
  414.    GLuint face = 0;
  415.  
  416.    obj = _mesa_lookup_texture(&intel->ctx, texture);
  417.    if (!obj || obj->Target != target) {
  418.       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
  419.       return NULL;
  420.    }
  421.  
  422.    if (target == GL_TEXTURE_CUBE_MAP)
  423.       face = zoffset;
  424.  
  425.    _mesa_test_texobj_completeness(&intel->ctx, obj);
  426.    iobj = intel_texture_object(obj);
  427.    if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
  428.       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
  429.       return NULL;
  430.    }
  431.  
  432.    if (level < obj->BaseLevel || level > obj->_MaxLevel) {
  433.       *error = __DRI_IMAGE_ERROR_BAD_MATCH;
  434.       return NULL;
  435.    }
  436.  
  437.    if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
  438.       *error = __DRI_IMAGE_ERROR_BAD_MATCH;
  439.       return NULL;
  440.    }
  441.    image = calloc(1, sizeof *image);
  442.    if (image == NULL) {
  443.       *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
  444.       return NULL;
  445.    }
  446.  
  447.    image->internal_format = obj->Image[face][level]->InternalFormat;
  448.    image->format = obj->Image[face][level]->TexFormat;
  449.    image->data = loaderPrivate;
  450.    intel_setup_image_from_mipmap_tree(intel, image, iobj->mt, level, zoffset);
  451.    image->dri_format = intel_dri_format(image->format);
  452.    if (image->dri_format == MESA_FORMAT_NONE) {
  453.       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
  454.       free(image);
  455.       return NULL;
  456.    }
  457.  
  458.    *error = __DRI_IMAGE_ERROR_SUCCESS;
  459.    return image;
  460. }
  461.  
  462. static void
  463. intel_destroy_image(__DRIimage *image)
  464. {
  465.     intel_region_release(&image->region);
  466.     free(image);
  467. }
  468.  
  469. static __DRIimage *
  470. intel_create_image(__DRIscreen *screen,
  471.                    int width, int height, int format,
  472.                    unsigned int use,
  473.                    void *loaderPrivate)
  474. {
  475.    __DRIimage *image;
  476.    struct intel_screen *intelScreen = screen->driverPrivate;
  477.    uint32_t tiling;
  478.    int cpp;
  479.  
  480.    tiling = I915_TILING_X;
  481.    if (use & __DRI_IMAGE_USE_CURSOR) {
  482.       if (width != 64 || height != 64)
  483.          return NULL;
  484.       tiling = I915_TILING_NONE;
  485.    }
  486.  
  487.    image = intel_allocate_image(format, loaderPrivate);
  488.    if (image == NULL)
  489.       return NULL;
  490.  
  491.    cpp = _mesa_get_format_bytes(image->format);
  492.    image->region =
  493.       intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
  494.    if (image->region == NULL) {
  495.       free(image);
  496.       return NULL;
  497.    }
  498.    
  499.    intel_setup_image_from_dimensions(image);
  500.  
  501.    return image;
  502. }
  503.  
  504. static GLboolean
  505. intel_query_image(__DRIimage *image, int attrib, int *value)
  506. {
  507.    switch (attrib) {
  508.    case __DRI_IMAGE_ATTRIB_STRIDE:
  509.       *value = image->region->pitch;
  510.       return true;
  511.    case __DRI_IMAGE_ATTRIB_HANDLE:
  512.       *value = image->region->bo->handle;
  513.       return true;
  514.    case __DRI_IMAGE_ATTRIB_NAME:
  515.       return intel_region_flink(image->region, (uint32_t *) value);
  516.    case __DRI_IMAGE_ATTRIB_FORMAT:
  517.       *value = image->dri_format;
  518.       return true;
  519.    case __DRI_IMAGE_ATTRIB_WIDTH:
  520.       *value = image->region->width;
  521.       return true;
  522.    case __DRI_IMAGE_ATTRIB_HEIGHT:
  523.       *value = image->region->height;
  524.       return true;
  525.    case __DRI_IMAGE_ATTRIB_COMPONENTS:
  526.       if (image->planar_format == NULL)
  527.          return false;
  528.       *value = image->planar_format->components;
  529.       return true;
  530.    case __DRI_IMAGE_ATTRIB_FD:
  531. //      if (drm_intel_bo_gem_export_to_prime(image->region->bo, value) == 0)
  532. //         return true;
  533.       return false;
  534.   default:
  535.       return false;
  536.    }
  537. }
  538.  
  539. static __DRIimage *
  540. intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
  541. {
  542.    __DRIimage *image;
  543.  
  544.    image = calloc(1, sizeof *image);
  545.    if (image == NULL)
  546.       return NULL;
  547.  
  548.    intel_region_reference(&image->region, orig_image->region);
  549.    if (image->region == NULL) {
  550.       free(image);
  551.       return NULL;
  552.    }
  553.  
  554.    image->internal_format = orig_image->internal_format;
  555.    image->planar_format   = orig_image->planar_format;
  556.    image->dri_format      = orig_image->dri_format;
  557.    image->format          = orig_image->format;
  558.    image->offset          = orig_image->offset;
  559.    image->width           = orig_image->width;
  560.    image->height          = orig_image->height;
  561.    image->tile_x          = orig_image->tile_x;
  562.    image->tile_y          = orig_image->tile_y;
  563.    image->data            = loaderPrivate;
  564.  
  565.    memcpy(image->strides, orig_image->strides, sizeof(image->strides));
  566.    memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
  567.  
  568.    return image;
  569. }
  570.  
  571. static GLboolean
  572. intel_validate_usage(__DRIimage *image, unsigned int use)
  573. {
  574.    if (use & __DRI_IMAGE_USE_CURSOR) {
  575.       if (image->region->width != 64 || image->region->height != 64)
  576.          return GL_FALSE;
  577.    }
  578.  
  579.    return GL_TRUE;
  580. }
  581.  
  582. static __DRIimage *
  583. intel_create_image_from_names(__DRIscreen *screen,
  584.                               int width, int height, int fourcc,
  585.                               int *names, int num_names,
  586.                               int *strides, int *offsets,
  587.                               void *loaderPrivate)
  588. {
  589.     struct intel_image_format *f = NULL;
  590.     __DRIimage *image;
  591.     int i, index;
  592.  
  593.     if (screen == NULL || names == NULL || num_names != 1)
  594.         return NULL;
  595.  
  596.     for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
  597.         if (intel_image_formats[i].fourcc == fourcc) {
  598.            f = &intel_image_formats[i];
  599.         }
  600.     }
  601.  
  602.     if (f == NULL)
  603.         return NULL;
  604.  
  605.     image = intel_create_image_from_name(screen, width, height,
  606.                                          __DRI_IMAGE_FORMAT_NONE,
  607.                                          names[0], strides[0],
  608.                                          loaderPrivate);
  609.  
  610.    if (image == NULL)
  611.       return NULL;
  612.  
  613.     image->planar_format = f;
  614.     for (i = 0; i < f->nplanes; i++) {
  615.         index = f->planes[i].buffer_index;
  616.         image->offsets[index] = offsets[index];
  617.         image->strides[index] = strides[index];
  618.     }
  619.  
  620.     return image;
  621. }
  622.  
  623. #if 0
  624. static __DRIimage *
  625. intel_create_image_from_fds(__DRIscreen *screen,
  626.                             int width, int height, int fourcc,
  627.                             int *fds, int num_fds, int *strides, int *offsets,
  628.                             void *loaderPrivate)
  629. {
  630.    struct intel_screen *intelScreen = screen->driverPrivate;
  631.    struct intel_image_format *f = NULL;
  632.    __DRIimage *image;
  633.    int i, index;
  634.  
  635.    if (fds == NULL || num_fds != 1)
  636.       return NULL;
  637.  
  638.    for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
  639.       if (intel_image_formats[i].fourcc == fourcc) {
  640.          f = &intel_image_formats[i];
  641.       }
  642.    }
  643.  
  644.    if (f == NULL)
  645.       return NULL;
  646.  
  647.    image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
  648.    if (image == NULL)
  649.       return NULL;
  650.  
  651.    image->region = intel_region_alloc_for_fd(intelScreen,
  652.                                              1, width, height,
  653.                                              strides[0], fds[0], "image");
  654.    if (image->region == NULL) {
  655.       free(image);
  656.       return NULL;
  657.    }
  658.  
  659.    image->planar_format = f;
  660.    for (i = 0; i < f->nplanes; i++) {
  661.       index = f->planes[i].buffer_index;
  662.       image->offsets[index] = offsets[index];
  663.       image->strides[index] = strides[index];
  664.    }
  665.  
  666.    return image;
  667. }
  668. #endif
  669.  
  670. static __DRIimage *
  671. intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
  672. {
  673.     int width, height, offset, stride, dri_format, index;
  674.     struct intel_image_format *f;
  675.     uint32_t mask_x, mask_y;
  676.     __DRIimage *image;
  677.  
  678.     if (parent == NULL || parent->planar_format == NULL)
  679.         return NULL;
  680.  
  681.     f = parent->planar_format;
  682.  
  683.     if (plane >= f->nplanes)
  684.         return NULL;
  685.  
  686.     width = parent->region->width >> f->planes[plane].width_shift;
  687.     height = parent->region->height >> f->planes[plane].height_shift;
  688.     dri_format = f->planes[plane].dri_format;
  689.     index = f->planes[plane].buffer_index;
  690.     offset = parent->offsets[index];
  691.     stride = parent->strides[index];
  692.  
  693.     image = intel_allocate_image(dri_format, loaderPrivate);
  694.     if (image == NULL)
  695.        return NULL;
  696.  
  697.     if (offset + height * stride > parent->region->bo->size) {
  698.        _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
  699.        free(image);
  700.        return NULL;
  701.     }
  702.  
  703.     image->region = calloc(sizeof(*image->region), 1);
  704.     if (image->region == NULL) {
  705.        free(image);
  706.        return NULL;
  707.     }
  708.  
  709.     image->region->cpp = _mesa_get_format_bytes(image->format);
  710.     image->region->width = width;
  711.     image->region->height = height;
  712.     image->region->pitch = stride;
  713.     image->region->refcount = 1;
  714.     image->region->bo = parent->region->bo;
  715.     drm_intel_bo_reference(image->region->bo);
  716.     image->region->tiling = parent->region->tiling;
  717.     image->offset = offset;
  718.     intel_setup_image_from_dimensions(image);
  719.  
  720.     intel_region_get_tile_masks(image->region, &mask_x, &mask_y, false);
  721.     if (offset & mask_x)
  722.        _mesa_warning(NULL,
  723.                      "intel_create_sub_image: offset not on tile boundary");
  724.  
  725.     return image;
  726. }
  727.  
  728. static struct __DRIimageExtensionRec intelImageExtension = {
  729.     .base = { __DRI_IMAGE, 7 },
  730.  
  731.     .createImageFromName                = intel_create_image_from_name,
  732.     .createImageFromRenderbuffer        = intel_create_image_from_renderbuffer,
  733.     .destroyImage                       = intel_destroy_image,
  734.     .createImage                        = intel_create_image,
  735.     .queryImage                         = intel_query_image,
  736.     .dupImage                           = intel_dup_image,
  737.     .validateUsage                      = intel_validate_usage,
  738.     .createImageFromNames               = intel_create_image_from_names,
  739.     .fromPlanar                         = intel_from_planar,
  740.     .createImageFromTexture             = intel_create_image_from_texture,
  741. //    .createImageFromFds                 = intel_create_image_from_fds
  742. };
  743.  
  744. static const __DRIextension *intelScreenExtensions[] = {
  745.     &intelTexBufferExtension.base,
  746.     &intelFlushExtension.base,
  747.     &intelImageExtension.base,
  748.     &dri2ConfigQueryExtension.base,
  749.     NULL
  750. };
  751.  
  752. static bool
  753. intel_get_param(__DRIscreen *psp, int param, int *value)
  754. {
  755.    int ret;
  756.    struct drm_i915_getparam gp;
  757.  
  758.    memset(&gp, 0, sizeof(gp));
  759.    gp.param = param;
  760.    gp.value = value;
  761.  
  762.    ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  763.    if (ret) {
  764.       if (ret != -EINVAL)
  765.          _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
  766.       return false;
  767.    }
  768.  
  769.    return true;
  770. }
  771.  
  772.  
  773. static bool
  774. intel_get_boolean(__DRIscreen *psp, int param)
  775. {
  776.    int value = 0;
  777.    return intel_get_param(psp, param, &value) && value;
  778. }
  779.  
  780. static void
  781. intelDestroyScreen(__DRIscreen * sPriv)
  782. {
  783.    struct intel_screen *intelScreen = sPriv->driverPrivate;
  784.  
  785.    dri_bufmgr_destroy(intelScreen->bufmgr);
  786.    driDestroyOptionInfo(&intelScreen->optionCache);
  787.  
  788.    free(intelScreen);
  789.    sPriv->driverPrivate = NULL;
  790. }
  791.  
  792.  
  793. /**
  794.  * This is called when we need to set up GL rendering to a new X window.
  795.  */
  796. static GLboolean
  797. intelCreateBuffer(__DRIscreen * driScrnPriv,
  798.                   __DRIdrawable * driDrawPriv,
  799.                   const struct gl_config * mesaVis, GLboolean isPixmap)
  800. {
  801.    struct intel_renderbuffer *rb;
  802.    gl_format rgbFormat;
  803.    struct gl_framebuffer *fb;
  804.  
  805.    if (isPixmap)
  806.       return false;
  807.  
  808.    fb = CALLOC_STRUCT(gl_framebuffer);
  809.    if (!fb)
  810.       return false;
  811.  
  812.    _mesa_initialize_window_framebuffer(fb, mesaVis);
  813.  
  814.    if (mesaVis->redBits == 5)
  815.       rgbFormat = MESA_FORMAT_RGB565;
  816.    else if (mesaVis->sRGBCapable)
  817.       rgbFormat = MESA_FORMAT_SARGB8;
  818.    else if (mesaVis->alphaBits == 0)
  819.       rgbFormat = MESA_FORMAT_XRGB8888;
  820.    else
  821.       rgbFormat = MESA_FORMAT_ARGB8888;
  822.  
  823.    /* setup the hardware-based renderbuffers */
  824.    rb = intel_create_renderbuffer(rgbFormat);
  825.    _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
  826.  
  827.    if (mesaVis->doubleBufferMode) {
  828.       rb = intel_create_renderbuffer(rgbFormat);
  829.       _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
  830.    }
  831.  
  832.    /*
  833.     * Assert here that the gl_config has an expected depth/stencil bit
  834.     * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
  835.     * which constructs the advertised configs.)
  836.     */
  837.    if (mesaVis->depthBits == 24) {
  838.       assert(mesaVis->stencilBits == 8);
  839.  
  840.       /*
  841.        * Use combined depth/stencil. Note that the renderbuffer is
  842.        * attached to two attachment points.
  843.        */
  844.       rb = intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24);
  845.       _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
  846.       _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
  847.    }
  848.    else if (mesaVis->depthBits == 16) {
  849.       assert(mesaVis->stencilBits == 0);
  850.       rb = intel_create_private_renderbuffer(MESA_FORMAT_Z16);
  851.       _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
  852.    }
  853.    else {
  854.       assert(mesaVis->depthBits == 0);
  855.       assert(mesaVis->stencilBits == 0);
  856.    }
  857.  
  858.    /* now add any/all software-based renderbuffers we may need */
  859.    _swrast_add_soft_renderbuffers(fb,
  860.                                   false, /* never sw color */
  861.                                   false, /* never sw depth */
  862.                                   false, /* never sw stencil */
  863.                                   mesaVis->accumRedBits > 0,
  864.                                   false, /* never sw alpha */
  865.                                   false  /* never sw aux */ );
  866.    driDrawPriv->driverPrivate = fb;
  867.  
  868.    return true;
  869. }
  870.  
  871. static void
  872. intelDestroyBuffer(__DRIdrawable * driDrawPriv)
  873. {
  874.     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
  875.  
  876.     _mesa_reference_framebuffer(&fb, NULL);
  877. }
  878.  
  879. /* There are probably better ways to do this, such as an
  880.  * init-designated function to register chipids and createcontext
  881.  * functions.
  882.  */
  883. extern bool
  884. i830CreateContext(int api,
  885.                   const struct gl_config *mesaVis,
  886.                   __DRIcontext *driContextPriv,
  887.                   unsigned major_version,
  888.                   unsigned minor_version,
  889.                   unsigned *error,
  890.                   void *sharedContextPrivate);
  891.  
  892. extern bool
  893. i915CreateContext(int api,
  894.                   const struct gl_config *mesaVis,
  895.                   __DRIcontext *driContextPriv,
  896.                   unsigned major_version,
  897.                   unsigned minor_version,
  898.                   unsigned *error,
  899.                   void *sharedContextPrivate);
  900.  
  901. static GLboolean
  902. intelCreateContext(gl_api api,
  903.                    const struct gl_config * mesaVis,
  904.                    __DRIcontext * driContextPriv,
  905.                    unsigned major_version,
  906.                    unsigned minor_version,
  907.                    uint32_t flags,
  908.                    unsigned *error,
  909.                    void *sharedContextPrivate)
  910. {
  911.    bool success = false;
  912.  
  913.    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
  914.    struct intel_screen *intelScreen = sPriv->driverPrivate;
  915.  
  916.    if (IS_9XX(intelScreen->deviceID)) {
  917.       success = i915CreateContext(api, mesaVis, driContextPriv,
  918.                                   major_version, minor_version, error,
  919.                                   sharedContextPrivate);
  920.    } else {
  921.       intelScreen->no_vbo = true;
  922.       success = i830CreateContext(api, mesaVis, driContextPriv,
  923.                                   major_version, minor_version, error,
  924.                                   sharedContextPrivate);
  925.    }
  926.  
  927.    if (success)
  928.       return true;
  929.  
  930.    if (driContextPriv->driverPrivate != NULL)
  931.       intelDestroyContext(driContextPriv);
  932.  
  933.    return false;
  934. }
  935.  
  936. static bool
  937. intel_init_bufmgr(struct intel_screen *intelScreen)
  938. {
  939.    __DRIscreen *spriv = intelScreen->driScrnPriv;
  940.  
  941.    intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
  942.  
  943.    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
  944.    if (intelScreen->bufmgr == NULL) {
  945.       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
  946.               __func__, __LINE__);
  947.       return false;
  948.    }
  949.  
  950.    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
  951.  
  952.    if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
  953.       fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
  954.       return false;
  955.    }
  956.  
  957.    return true;
  958. }
  959.  
  960. static bool
  961. intel_detect_swizzling(struct intel_screen *screen)
  962. {
  963.    drm_intel_bo *buffer;
  964.    unsigned long flags = 0;
  965.    unsigned long aligned_pitch;
  966.    uint32_t tiling = I915_TILING_X;
  967.    uint32_t swizzle_mode = 0;
  968.  
  969.    buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
  970.                                      64, 64, 4,
  971.                                      &tiling, &aligned_pitch, flags);
  972.    if (buffer == NULL)
  973.       return false;
  974.  
  975.    drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
  976.    drm_intel_bo_unreference(buffer);
  977.  
  978.    if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
  979.       return false;
  980.    else
  981.       return true;
  982. }
  983.  
  984. static __DRIconfig**
  985. intel_screen_make_configs(__DRIscreen *dri_screen)
  986. {
  987.    static const gl_format formats[] = {
  988.       MESA_FORMAT_RGB565,
  989.       MESA_FORMAT_ARGB8888
  990.    };
  991.  
  992.    /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
  993.    static const GLenum back_buffer_modes[] = {
  994.        GLX_SWAP_UNDEFINED_OML, GLX_NONE,
  995.    };
  996.  
  997.    static const uint8_t singlesample_samples[1] = {0};
  998.  
  999.    uint8_t depth_bits[4], stencil_bits[4];
  1000.    __DRIconfig **configs = NULL;
  1001.  
  1002.    /* Generate singlesample configs without accumulation buffer. */
  1003.    for (int i = 0; i < ARRAY_SIZE(formats); i++) {
  1004.       __DRIconfig **new_configs;
  1005.       int num_depth_stencil_bits = 2;
  1006.  
  1007.       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
  1008.        * buffer that has a different number of bits per pixel than the color
  1009.        * buffer.
  1010.        */
  1011.       depth_bits[0] = 0;
  1012.       stencil_bits[0] = 0;
  1013.  
  1014.       if (formats[i] == MESA_FORMAT_RGB565) {
  1015.          depth_bits[1] = 16;
  1016.          stencil_bits[1] = 0;
  1017.       } else {
  1018.          depth_bits[1] = 24;
  1019.          stencil_bits[1] = 8;
  1020.       }
  1021.  
  1022.       new_configs = driCreateConfigs(formats[i],
  1023.                                      depth_bits,
  1024.                                      stencil_bits,
  1025.                                      num_depth_stencil_bits,
  1026.                                      back_buffer_modes, 2,
  1027.                                      singlesample_samples, 1,
  1028.                                      false);
  1029.       configs = driConcatConfigs(configs, new_configs);
  1030.    }
  1031.  
  1032.    /* Generate the minimum possible set of configs that include an
  1033.     * accumulation buffer.
  1034.     */
  1035.    for (int i = 0; i < ARRAY_SIZE(formats); i++) {
  1036.       __DRIconfig **new_configs;
  1037.  
  1038.       if (formats[i] == MESA_FORMAT_RGB565) {
  1039.          depth_bits[0] = 16;
  1040.          stencil_bits[0] = 0;
  1041.       } else {
  1042.          depth_bits[0] = 24;
  1043.          stencil_bits[0] = 8;
  1044.       }
  1045.  
  1046.       new_configs = driCreateConfigs(formats[i],
  1047.                                      depth_bits, stencil_bits, 1,
  1048.                                      back_buffer_modes, 1,
  1049.                                      singlesample_samples, 1,
  1050.                                      true);
  1051.       configs = driConcatConfigs(configs, new_configs);
  1052.    }
  1053.  
  1054.    if (configs == NULL) {
  1055.       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
  1056.               __LINE__);
  1057.       return NULL;
  1058.    }
  1059.  
  1060.    return configs;
  1061. }
  1062.  
  1063. static void
  1064. set_max_gl_versions(struct intel_screen *screen)
  1065. {
  1066.    int gl_version_override = _mesa_get_gl_version_override();
  1067.  
  1068.    switch (screen->gen) {
  1069.    case 3:
  1070.       screen->max_gl_core_version = 0;
  1071.       screen->max_gl_es1_version = 11;
  1072.       screen->max_gl_compat_version = 21;
  1073.       screen->max_gl_es2_version = 20;
  1074.       break;
  1075.    case 2:
  1076.       screen->max_gl_core_version = 0;
  1077.       screen->max_gl_compat_version = 13;
  1078.       screen->max_gl_es1_version = 11;
  1079.       screen->max_gl_es2_version = 0;
  1080.       break;
  1081.    default:
  1082.       assert(!"unrecognized intel_screen::gen");
  1083.       break;
  1084.    }
  1085.  
  1086.    if (gl_version_override >= 31) {
  1087.       screen->max_gl_core_version = MAX2(screen->max_gl_core_version,
  1088.                                          gl_version_override);
  1089.    } else {
  1090.       screen->max_gl_compat_version = MAX2(screen->max_gl_compat_version,
  1091.                                            gl_version_override);
  1092.    }
  1093.  
  1094. #ifndef FEATURE_ES1
  1095.    screen->max_gl_es1_version = 0;
  1096. #endif
  1097.  
  1098. #ifndef FEATURE_ES2
  1099.    screen->max_gl_es2_version = 0;
  1100. #endif
  1101. }
  1102.  
  1103. /**
  1104.  * This is the driver specific part of the createNewScreen entry point.
  1105.  * Called when using DRI2.
  1106.  *
  1107.  * \return the struct gl_config supported by this driver
  1108.  */
  1109. static const
  1110. __DRIconfig **intelInitScreen2(__DRIscreen *psp)
  1111. {
  1112.    struct intel_screen *intelScreen;
  1113.  
  1114.    if (psp->dri2.loader->base.version <= 2 ||
  1115.        psp->dri2.loader->getBuffersWithFormat == NULL) {
  1116.       fprintf(stderr,
  1117.               "\nERROR!  DRI2 loader with getBuffersWithFormat() "
  1118.               "support required\n");
  1119.       return false;
  1120.    }
  1121.  
  1122.    /* Allocate the private area */
  1123.    intelScreen = calloc(1, sizeof *intelScreen);
  1124.    if (!intelScreen) {
  1125.       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
  1126.       return false;
  1127.    }
  1128.    /* parse information in __driConfigOptions */
  1129.    driParseOptionInfo(&intelScreen->optionCache,
  1130.                       __driConfigOptions, __driNConfigOptions);
  1131.  
  1132.    intelScreen->driScrnPriv = psp;
  1133.    psp->driverPrivate = (void *) intelScreen;
  1134.  
  1135.    if (!intel_init_bufmgr(intelScreen))
  1136.        return false;
  1137.  
  1138.    intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
  1139.  
  1140.    if (IS_9XX(intelScreen->deviceID)) {
  1141.       intelScreen->gen = 3;
  1142.    } else {
  1143.       intelScreen->gen = 2;
  1144.    }
  1145.  
  1146.    intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
  1147.  
  1148.    set_max_gl_versions(intelScreen);
  1149.  
  1150.    psp->api_mask = (1 << __DRI_API_OPENGL);
  1151.    if (intelScreen->max_gl_core_version > 0)
  1152.       psp->api_mask |= (1 << __DRI_API_OPENGL_CORE);
  1153.    if (intelScreen->max_gl_es1_version > 0)
  1154.       psp->api_mask |= (1 << __DRI_API_GLES);
  1155.    if (intelScreen->max_gl_es2_version > 0)
  1156.       psp->api_mask |= (1 << __DRI_API_GLES2);
  1157.    if (intelScreen->max_gl_es2_version >= 30)
  1158.       psp->api_mask |= (1 << __DRI_API_GLES3);
  1159.  
  1160.    psp->extensions = intelScreenExtensions;
  1161.  
  1162.    return (const __DRIconfig**) intel_screen_make_configs(psp);
  1163. }
  1164.  
  1165. struct intel_buffer {
  1166.    __DRIbuffer base;
  1167.    struct intel_region *region;
  1168. };
  1169.  
  1170. static __DRIbuffer *
  1171. intelAllocateBuffer(__DRIscreen *screen,
  1172.                     unsigned attachment, unsigned format,
  1173.                     int width, int height)
  1174. {
  1175.    struct intel_buffer *intelBuffer;
  1176.    struct intel_screen *intelScreen = screen->driverPrivate;
  1177.  
  1178.    assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
  1179.           attachment == __DRI_BUFFER_BACK_LEFT);
  1180.  
  1181.    intelBuffer = calloc(1, sizeof *intelBuffer);
  1182.    if (intelBuffer == NULL)
  1183.       return NULL;
  1184.  
  1185.    /* The front and back buffers are color buffers, which are X tiled. */
  1186.    intelBuffer->region = intel_region_alloc(intelScreen,
  1187.                                             I915_TILING_X,
  1188.                                             format / 8,
  1189.                                             width,
  1190.                                             height,
  1191.                                             true);
  1192.    
  1193.    if (intelBuffer->region == NULL) {
  1194.            free(intelBuffer);
  1195.            return NULL;
  1196.    }
  1197.    
  1198.    intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
  1199.  
  1200.    intelBuffer->base.attachment = attachment;
  1201.    intelBuffer->base.cpp = intelBuffer->region->cpp;
  1202.    intelBuffer->base.pitch = intelBuffer->region->pitch;
  1203.  
  1204.    return &intelBuffer->base;
  1205. }
  1206.  
  1207. static void
  1208. intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
  1209. {
  1210.    struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
  1211.  
  1212.    intel_region_release(&intelBuffer->region);
  1213.    free(intelBuffer);
  1214. }
  1215.  
  1216.  
  1217. const struct __DriverAPIRec driDriverAPI = {
  1218.    .InitScreen           = intelInitScreen2,
  1219.    .DestroyScreen        = intelDestroyScreen,
  1220.    .CreateContext        = intelCreateContext,
  1221.    .DestroyContext       = intelDestroyContext,
  1222.    .CreateBuffer         = intelCreateBuffer,
  1223.    .DestroyBuffer        = intelDestroyBuffer,
  1224.    .MakeCurrent          = intelMakeCurrent,
  1225.    .UnbindContext        = intelUnbindContext,
  1226.    .AllocateBuffer       = intelAllocateBuffer,
  1227.    .ReleaseBuffer        = intelReleaseBuffer
  1228. };
  1229.  
  1230. /* This is the table of extensions that the loader will dlsym() for. */
  1231. __declspec(dllexport) const __DRIextension *__driDriverExtensions[] = {
  1232.     &driCoreExtension.base,
  1233.     &driDRI2Extension.base,
  1234.     NULL
  1235. };
  1236.