Subversion Repositories Kolibri OS

Rev

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