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.  
  29. #include "main/glheader.h"
  30. #include "main/context.h"
  31. #include "main/extensions.h"
  32. #include "main/fbobject.h"
  33. #include "main/framebuffer.h"
  34. #include "main/imports.h"
  35. #include "main/renderbuffer.h"
  36.  
  37. #include "swrast/swrast.h"
  38. #include "swrast_setup/swrast_setup.h"
  39. #include "tnl/tnl.h"
  40. #include "drivers/common/driverfuncs.h"
  41. #include "drivers/common/meta.h"
  42.  
  43. #include "intel_chipset.h"
  44. #include "intel_buffers.h"
  45. #include "intel_tex.h"
  46. #include "intel_batchbuffer.h"
  47. #include "intel_pixel.h"
  48. #include "intel_regions.h"
  49. #include "intel_buffer_objects.h"
  50. #include "intel_fbo.h"
  51. #include "intel_bufmgr.h"
  52. #include "intel_screen.h"
  53. #include "intel_mipmap_tree.h"
  54.  
  55. #include "utils.h"
  56. #include "../glsl/ralloc.h"
  57.  
  58. #ifndef INTEL_DEBUG
  59. int INTEL_DEBUG = (0);
  60. #endif
  61.  
  62.  
  63. static const GLubyte *
  64. intelGetString(struct gl_context * ctx, GLenum name)
  65. {
  66.    const struct brw_context *const brw = brw_context(ctx);
  67.    const char *chipset;
  68.    static char buffer[128];
  69.  
  70.    switch (name) {
  71.    case GL_VENDOR:
  72.       return (GLubyte *) "Intel Open Source Technology Center";
  73.       break;
  74.  
  75.    case GL_RENDERER:
  76.       switch (brw->intelScreen->deviceID) {
  77. #undef CHIPSET
  78. #define CHIPSET(id, symbol, str) case id: chipset = str; break;
  79. #include "pci_ids/i965_pci_ids.h"
  80.       default:
  81.          chipset = "Unknown Intel Chipset";
  82.          break;
  83.       }
  84.  
  85.       (void) driGetRendererString(buffer, chipset, 0);
  86.       return (GLubyte *) buffer;
  87.  
  88.    default:
  89.       return NULL;
  90.    }
  91. }
  92.  
  93. void
  94. intel_resolve_for_dri2_flush(struct brw_context *brw,
  95.                              __DRIdrawable *drawable)
  96. {
  97.    if (brw->gen < 6) {
  98.       /* MSAA and fast color clear are not supported, so don't waste time
  99.        * checking whether a resolve is needed.
  100.        */
  101.       return;
  102.    }
  103.  
  104.    struct gl_framebuffer *fb = drawable->driverPrivate;
  105.    struct intel_renderbuffer *rb;
  106.  
  107.    /* Usually, only the back buffer will need to be downsampled. However,
  108.     * the front buffer will also need it if the user has rendered into it.
  109.     */
  110.    static const gl_buffer_index buffers[2] = {
  111.          BUFFER_BACK_LEFT,
  112.          BUFFER_FRONT_LEFT,
  113.    };
  114.  
  115.    for (int i = 0; i < 2; ++i) {
  116.       rb = intel_get_renderbuffer(fb, buffers[i]);
  117.       if (rb == NULL || rb->mt == NULL)
  118.          continue;
  119.       if (rb->mt->num_samples <= 1)
  120.          intel_miptree_resolve_color(brw, rb->mt);
  121.       else
  122.          intel_miptree_downsample(brw, rb->mt);
  123.    }
  124. }
  125.  
  126. static void
  127. intel_flush_front(struct gl_context *ctx)
  128. {
  129.    struct brw_context *brw = brw_context(ctx);
  130.     __DRIcontext *driContext = brw->driContext;
  131.     __DRIdrawable *driDrawable = driContext->driDrawablePriv;
  132.     __DRIscreen *const screen = brw->intelScreen->driScrnPriv;
  133.  
  134.     if (brw->front_buffer_dirty && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
  135.       if (screen->dri2.loader->flushFrontBuffer != NULL &&
  136.           driDrawable &&
  137.           driDrawable->loaderPrivate) {
  138.  
  139.          /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
  140.           *
  141.           * This potentially resolves both front and back buffer. It
  142.           * is unnecessary to resolve the back, but harms nothing except
  143.           * performance. And no one cares about front-buffer render
  144.           * performance.
  145.           */
  146.          intel_resolve_for_dri2_flush(brw, driDrawable);
  147.  
  148.          screen->dri2.loader->flushFrontBuffer(driDrawable,
  149.                                                driDrawable->loaderPrivate);
  150.  
  151.          /* We set the dirty bit in intel_prepare_render() if we're
  152.           * front buffer rendering once we get there.
  153.           */
  154.          brw->front_buffer_dirty = false;
  155.       }
  156.    }
  157. }
  158.  
  159. static unsigned
  160. intel_bits_per_pixel(const struct intel_renderbuffer *rb)
  161. {
  162.    return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
  163. }
  164.  
  165. static void
  166. intel_query_dri2_buffers(struct brw_context *brw,
  167.                          __DRIdrawable *drawable,
  168.                          __DRIbuffer **buffers,
  169.                          int *count);
  170.  
  171. static void
  172. intel_process_dri2_buffer(struct brw_context *brw,
  173.                           __DRIdrawable *drawable,
  174.                           __DRIbuffer *buffer,
  175.                           struct intel_renderbuffer *rb,
  176.                           const char *buffer_name);
  177.  
  178. void
  179. intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
  180. {
  181.    struct gl_framebuffer *fb = drawable->driverPrivate;
  182.    struct intel_renderbuffer *rb;
  183.    struct brw_context *brw = context->driverPrivate;
  184.    __DRIbuffer *buffers = NULL;
  185.    int i, count;
  186.    const char *region_name;
  187.  
  188.    /* Set this up front, so that in case our buffers get invalidated
  189.     * while we're getting new buffers, we don't clobber the stamp and
  190.     * thus ignore the invalidate. */
  191.    drawable->lastStamp = drawable->dri2.stamp;
  192.  
  193.    if (unlikely(INTEL_DEBUG & DEBUG_DRI))
  194.       fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
  195.  
  196.    intel_query_dri2_buffers(brw, drawable, &buffers, &count);
  197.  
  198.    if (buffers == NULL)
  199.       return;
  200.  
  201.    for (i = 0; i < count; i++) {
  202.        switch (buffers[i].attachment) {
  203.        case __DRI_BUFFER_FRONT_LEFT:
  204.            rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
  205.            region_name = "dri2 front buffer";
  206.            break;
  207.  
  208.        case __DRI_BUFFER_FAKE_FRONT_LEFT:
  209.            rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
  210.            region_name = "dri2 fake front buffer";
  211.            break;
  212.  
  213.        case __DRI_BUFFER_BACK_LEFT:
  214.            rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
  215.            region_name = "dri2 back buffer";
  216.            break;
  217.  
  218.        case __DRI_BUFFER_DEPTH:
  219.        case __DRI_BUFFER_HIZ:
  220.        case __DRI_BUFFER_DEPTH_STENCIL:
  221.        case __DRI_BUFFER_STENCIL:
  222.        case __DRI_BUFFER_ACCUM:
  223.        default:
  224.            fprintf(stderr,
  225.                    "unhandled buffer attach event, attachment type %d\n",
  226.                    buffers[i].attachment);
  227.            return;
  228.        }
  229.  
  230.        intel_process_dri2_buffer(brw, drawable, &buffers[i], rb, region_name);
  231.    }
  232.  
  233.    driUpdateFramebufferSize(&brw->ctx, drawable);
  234. }
  235.  
  236. /**
  237.  * intel_prepare_render should be called anywhere that curent read/drawbuffer
  238.  * state is required.
  239.  */
  240. void
  241. intel_prepare_render(struct brw_context *brw)
  242. {
  243.    __DRIcontext *driContext = brw->driContext;
  244.    __DRIdrawable *drawable;
  245.  
  246.    drawable = driContext->driDrawablePriv;
  247.    if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
  248.       if (drawable->lastStamp != drawable->dri2.stamp)
  249.          intel_update_renderbuffers(driContext, drawable);
  250.       driContext->dri2.draw_stamp = drawable->dri2.stamp;
  251.    }
  252.  
  253.    drawable = driContext->driReadablePriv;
  254.    if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
  255.       if (drawable->lastStamp != drawable->dri2.stamp)
  256.          intel_update_renderbuffers(driContext, drawable);
  257.       driContext->dri2.read_stamp = drawable->dri2.stamp;
  258.    }
  259.  
  260.    /* If we're currently rendering to the front buffer, the rendering
  261.     * that will happen next will probably dirty the front buffer.  So
  262.     * mark it as dirty here.
  263.     */
  264.    if (brw->is_front_buffer_rendering)
  265.       brw->front_buffer_dirty = true;
  266.  
  267.    /* Wait for the swapbuffers before the one we just emitted, so we
  268.     * don't get too many swaps outstanding for apps that are GPU-heavy
  269.     * but not CPU-heavy.
  270.     *
  271.     * We're using intelDRI2Flush (called from the loader before
  272.     * swapbuffer) and glFlush (for front buffer rendering) as the
  273.     * indicator that a frame is done and then throttle when we get
  274.     * here as we prepare to render the next frame.  At this point for
  275.     * round trips for swap/copy and getting new buffers are done and
  276.     * we'll spend less time waiting on the GPU.
  277.     *
  278.     * Unfortunately, we don't have a handle to the batch containing
  279.     * the swap, and getting our hands on that doesn't seem worth it,
  280.     * so we just us the first batch we emitted after the last swap.
  281.     */
  282.    if (brw->need_throttle && brw->first_post_swapbuffers_batch) {
  283.       if (!brw->disable_throttling)
  284.          drm_intel_bo_wait_rendering(brw->first_post_swapbuffers_batch);
  285.       drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
  286.       brw->first_post_swapbuffers_batch = NULL;
  287.       brw->need_throttle = false;
  288.    }
  289. }
  290.  
  291. static void
  292. intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
  293. {
  294.     struct brw_context *brw = brw_context(ctx);
  295.     __DRIcontext *driContext = brw->driContext;
  296.  
  297.     if (brw->saved_viewport)
  298.         brw->saved_viewport(ctx, x, y, w, h);
  299.  
  300.     if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
  301.        dri2InvalidateDrawable(driContext->driDrawablePriv);
  302.        dri2InvalidateDrawable(driContext->driReadablePriv);
  303.     }
  304. }
  305.  
  306. static const struct dri_debug_control debug_control[] = {
  307.    { "tex",   DEBUG_TEXTURE},
  308.    { "state", DEBUG_STATE},
  309.    { "ioctl", DEBUG_IOCTL},
  310.    { "blit",  DEBUG_BLIT},
  311.    { "mip",   DEBUG_MIPTREE},
  312.    { "fall",  DEBUG_PERF},
  313.    { "perf",  DEBUG_PERF},
  314.    { "bat",   DEBUG_BATCH},
  315.    { "pix",   DEBUG_PIXEL},
  316.    { "buf",   DEBUG_BUFMGR},
  317.    { "reg",   DEBUG_REGION},
  318.    { "fbo",   DEBUG_FBO},
  319.    { "fs",    DEBUG_WM },
  320.    { "gs",    DEBUG_GS},
  321.    { "sync",  DEBUG_SYNC},
  322.    { "prim",  DEBUG_PRIMS },
  323.    { "vert",  DEBUG_VERTS },
  324.    { "dri",   DEBUG_DRI },
  325.    { "sf",    DEBUG_SF },
  326.    { "stats", DEBUG_STATS },
  327.    { "wm",    DEBUG_WM },
  328.    { "urb",   DEBUG_URB },
  329.    { "vs",    DEBUG_VS },
  330.    { "clip",  DEBUG_CLIP },
  331.    { "aub",   DEBUG_AUB },
  332.    { "shader_time", DEBUG_SHADER_TIME },
  333.    { "no16",  DEBUG_NO16 },
  334.    { "blorp", DEBUG_BLORP },
  335.    { NULL,    0 }
  336. };
  337.  
  338.  
  339. static void
  340. intelInvalidateState(struct gl_context * ctx, GLuint new_state)
  341. {
  342.    struct brw_context *brw = brw_context(ctx);
  343.  
  344.     if (ctx->swrast_context)
  345.        _swrast_InvalidateState(ctx, new_state);
  346.    _vbo_InvalidateState(ctx, new_state);
  347.  
  348.    brw->NewGLState |= new_state;
  349. }
  350.  
  351. void
  352. _intel_flush(struct gl_context *ctx, const char *file, int line)
  353. {
  354.    struct brw_context *brw = brw_context(ctx);
  355.  
  356.    if (brw->batch.used)
  357.       _intel_batchbuffer_flush(brw, file, line);
  358. }
  359.  
  360. static void
  361. intel_glFlush(struct gl_context *ctx)
  362. {
  363.    struct brw_context *brw = brw_context(ctx);
  364.  
  365.    intel_flush(ctx);
  366.    intel_flush_front(ctx);
  367.    if (brw->is_front_buffer_rendering)
  368.       brw->need_throttle = true;
  369. }
  370.  
  371. void
  372. intelFinish(struct gl_context * ctx)
  373. {
  374.    struct brw_context *brw = brw_context(ctx);
  375.  
  376.    intel_flush(ctx);
  377.    intel_flush_front(ctx);
  378.  
  379.    if (brw->batch.last_bo)
  380.       drm_intel_bo_wait_rendering(brw->batch.last_bo);
  381. }
  382.  
  383. void
  384. intelInitDriverFunctions(struct dd_function_table *functions)
  385. {
  386.    _mesa_init_driver_functions(functions);
  387.  
  388.    functions->Flush = intel_glFlush;
  389.    functions->Finish = intelFinish;
  390.    functions->GetString = intelGetString;
  391.    functions->UpdateState = intelInvalidateState;
  392.  
  393.    intelInitTextureFuncs(functions);
  394.    intelInitTextureImageFuncs(functions);
  395.    intelInitTextureSubImageFuncs(functions);
  396.    intelInitTextureCopyImageFuncs(functions);
  397.    intelInitClearFuncs(functions);
  398.    intelInitBufferFuncs(functions);
  399.    intelInitPixelFuncs(functions);
  400.    intelInitBufferObjectFuncs(functions);
  401.    intel_init_syncobj_functions(functions);
  402. }
  403.  
  404. static bool
  405. validate_context_version(struct intel_screen *screen,
  406.                          int mesa_api,
  407.                          unsigned major_version,
  408.                          unsigned minor_version,
  409.                          unsigned *dri_ctx_error)
  410. {
  411.    unsigned req_version = 10 * major_version + minor_version;
  412.    unsigned max_version = 0;
  413.  
  414.    switch (mesa_api) {
  415.    case API_OPENGL_COMPAT:
  416.       max_version = screen->max_gl_compat_version;
  417.       break;
  418.    case API_OPENGL_CORE:
  419.       max_version = screen->max_gl_core_version;
  420.       break;
  421.    case API_OPENGLES:
  422.       max_version = screen->max_gl_es1_version;
  423.       break;
  424.    case API_OPENGLES2:
  425.       max_version = screen->max_gl_es2_version;
  426.       break;
  427.    default:
  428.       max_version = 0;
  429.       break;
  430.    }
  431.  
  432.    if (max_version == 0) {
  433.       *dri_ctx_error = __DRI_CTX_ERROR_BAD_API;
  434.       return false;
  435.    } else if (req_version > max_version) {
  436.       *dri_ctx_error = __DRI_CTX_ERROR_BAD_VERSION;
  437.       return false;
  438.    }
  439.  
  440.    return true;
  441. }
  442.  
  443. bool
  444. intelInitContext(struct brw_context *brw,
  445.                  int api,
  446.                  unsigned major_version,
  447.                  unsigned minor_version,
  448.                  const struct gl_config * mesaVis,
  449.                  __DRIcontext * driContextPriv,
  450.                  void *sharedContextPrivate,
  451.                  struct dd_function_table *functions,
  452.                  unsigned *dri_ctx_error)
  453. {
  454.    struct gl_context *ctx = &brw->ctx;
  455.    struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
  456.    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
  457.    struct intel_screen *intelScreen = sPriv->driverPrivate;
  458.    int bo_reuse_mode;
  459.    struct gl_config visual;
  460.  
  461.    /* we can't do anything without a connection to the device */
  462.    if (intelScreen->bufmgr == NULL) {
  463.       *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
  464.       return false;
  465.    }
  466.  
  467.    if (!validate_context_version(intelScreen,
  468.                                  api, major_version, minor_version,
  469.                                  dri_ctx_error))
  470.       return false;
  471.  
  472.    /* Can't rely on invalidate events, fall back to glViewport hack */
  473.    if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
  474.       brw->saved_viewport = functions->Viewport;
  475.       functions->Viewport = intel_viewport;
  476.    }
  477.  
  478.    if (mesaVis == NULL) {
  479.       memset(&visual, 0, sizeof visual);
  480.       mesaVis = &visual;
  481.    }
  482.  
  483.    brw->intelScreen = intelScreen;
  484.  
  485.    if (!_mesa_initialize_context(&brw->ctx, api, mesaVis, shareCtx,
  486.                                  functions)) {
  487.       *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
  488.       printf("%s: failed to init mesa context\n", __FUNCTION__);
  489.       return false;
  490.    }
  491.  
  492.    driContextPriv->driverPrivate = brw;
  493.    brw->driContext = driContextPriv;
  494.  
  495.    brw->gen = intelScreen->gen;
  496.  
  497.    const int devID = intelScreen->deviceID;
  498.    if (IS_SNB_GT1(devID) || IS_IVB_GT1(devID) || IS_HSW_GT1(devID))
  499.       brw->gt = 1;
  500.    else if (IS_SNB_GT2(devID) || IS_IVB_GT2(devID) || IS_HSW_GT2(devID))
  501.       brw->gt = 2;
  502.    else if (IS_HSW_GT3(devID))
  503.       brw->gt = 3;
  504.    else
  505.       brw->gt = 0;
  506.  
  507.    if (IS_HASWELL(devID)) {
  508.       brw->is_haswell = true;
  509.    } else if (IS_BAYTRAIL(devID)) {
  510.       brw->is_baytrail = true;
  511.       brw->gt = 1;
  512.    } else if (IS_G4X(devID)) {
  513.       brw->is_g4x = true;
  514.    }
  515.  
  516.    brw->has_separate_stencil = brw->intelScreen->hw_has_separate_stencil;
  517.    brw->must_use_separate_stencil = brw->intelScreen->hw_must_use_separate_stencil;
  518.    brw->has_hiz = brw->gen >= 6;
  519.    brw->has_llc = brw->intelScreen->hw_has_llc;
  520.    brw->has_swizzling = brw->intelScreen->hw_has_swizzling;
  521.  
  522.    memset(&ctx->TextureFormatSupported,
  523.           0, sizeof(ctx->TextureFormatSupported));
  524.  
  525.    driParseConfigFiles(&brw->optionCache, &intelScreen->optionCache,
  526.                        sPriv->myNum, "i965");
  527.  
  528.    /* Estimate the size of the mappable aperture into the GTT.  There's an
  529.     * ioctl to get the whole GTT size, but not one to get the mappable subset.
  530.     * It turns out it's basically always 256MB, though some ancient hardware
  531.     * was smaller.
  532.     */
  533.    uint32_t gtt_size = 256 * 1024 * 1024;
  534.  
  535.    /* We don't want to map two objects such that a memcpy between them would
  536.     * just fault one mapping in and then the other over and over forever.  So
  537.     * we would need to divide the GTT size by 2.  Additionally, some GTT is
  538.     * taken up by things like the framebuffer and the ringbuffer and such, so
  539.     * be more conservative.
  540.     */
  541.    brw->max_gtt_map_object_size = gtt_size / 4;
  542.  
  543.    brw->bufmgr = intelScreen->bufmgr;
  544.  
  545.    bo_reuse_mode = driQueryOptioni(&brw->optionCache, "bo_reuse");
  546.    switch (bo_reuse_mode) {
  547.    case DRI_CONF_BO_REUSE_DISABLED:
  548.       break;
  549.    case DRI_CONF_BO_REUSE_ALL:
  550.       intel_bufmgr_gem_enable_reuse(brw->bufmgr);
  551.       break;
  552.    }
  553.  
  554.    /* Initialize the software rasterizer and helper modules.
  555.     *
  556.     * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
  557.     * software fallbacks (which we have to support on legacy GL to do weird
  558.     * glDrawPixels(), glBitmap(), and other functions).
  559.     */
  560.    if (api != API_OPENGL_CORE) {
  561.       _swrast_CreateContext(ctx);
  562.    }
  563.  
  564.    _vbo_CreateContext(ctx);
  565.    if (ctx->swrast_context) {
  566.       _tnl_CreateContext(ctx);
  567.       _swsetup_CreateContext(ctx);
  568.  
  569.       /* Configure swrast to match hardware characteristics: */
  570.       _swrast_allow_pixel_fog(ctx, false);
  571.       _swrast_allow_vertex_fog(ctx, true);
  572.    }
  573.  
  574.    _mesa_meta_init(ctx);
  575.  
  576.    intelInitExtensions(ctx);
  577.  
  578.    INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
  579.    if (INTEL_DEBUG & DEBUG_BUFMGR)
  580.       dri_bufmgr_set_debug(brw->bufmgr, true);
  581.    if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && brw->gen < 7) {
  582.       fprintf(stderr,
  583.               "shader_time debugging requires gen7 (Ivybridge) or better.\n");
  584.       INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
  585.    }
  586.    if (INTEL_DEBUG & DEBUG_PERF)
  587.       brw->perf_debug = true;
  588.  
  589.    if (INTEL_DEBUG & DEBUG_AUB)
  590.       drm_intel_bufmgr_gem_set_aub_dump(brw->bufmgr, true);
  591.  
  592.    intel_batchbuffer_init(brw);
  593.  
  594.    intel_fbo_init(brw);
  595.  
  596.    if (!driQueryOptionb(&brw->optionCache, "hiz")) {
  597.        brw->has_hiz = false;
  598.        /* On gen6, you can only do separate stencil with HIZ. */
  599.        if (brw->gen == 6)
  600.           brw->has_separate_stencil = false;
  601.    }
  602.  
  603.    if (driQueryOptionb(&brw->optionCache, "always_flush_batch")) {
  604.       fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
  605.       brw->always_flush_batch = 1;
  606.    }
  607.  
  608.    if (driQueryOptionb(&brw->optionCache, "always_flush_cache")) {
  609.       fprintf(stderr, "flushing GPU caches before/after each draw call\n");
  610.       brw->always_flush_cache = 1;
  611.    }
  612.  
  613.    if (driQueryOptionb(&brw->optionCache, "disable_throttling")) {
  614.       fprintf(stderr, "disabling flush throttling\n");
  615.       brw->disable_throttling = 1;
  616.    }
  617.  
  618.    return true;
  619. }
  620.  
  621. void
  622. intelDestroyContext(__DRIcontext * driContextPriv)
  623. {
  624.    struct brw_context *brw =
  625.       (struct brw_context *) driContextPriv->driverPrivate;
  626.    struct gl_context *ctx = &brw->ctx;
  627.  
  628.    assert(brw); /* should never be null */
  629.    if (brw) {
  630.       /* Dump a final BMP in case the application doesn't call SwapBuffers */
  631.       if (INTEL_DEBUG & DEBUG_AUB) {
  632.          intel_batchbuffer_flush(brw);
  633.          aub_dump_bmp(&brw->ctx);
  634.       }
  635.  
  636.       _mesa_meta_free(&brw->ctx);
  637.  
  638.       brw->vtbl.destroy(brw);
  639.  
  640.       if (ctx->swrast_context) {
  641.          _swsetup_DestroyContext(&brw->ctx);
  642.          _tnl_DestroyContext(&brw->ctx);
  643.       }
  644.       _vbo_DestroyContext(&brw->ctx);
  645.  
  646.       if (ctx->swrast_context)
  647.          _swrast_DestroyContext(&brw->ctx);
  648.  
  649.       intel_batchbuffer_free(brw);
  650.  
  651.       drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
  652.       brw->first_post_swapbuffers_batch = NULL;
  653.  
  654.       driDestroyOptionCache(&brw->optionCache);
  655.  
  656.       /* free the Mesa context */
  657.       _mesa_free_context_data(&brw->ctx);
  658.  
  659.       ralloc_free(brw);
  660.       driContextPriv->driverPrivate = NULL;
  661.    }
  662. }
  663.  
  664. GLboolean
  665. intelUnbindContext(__DRIcontext * driContextPriv)
  666. {
  667.    /* Unset current context and dispath table */
  668.    _mesa_make_current(NULL, NULL, NULL);
  669.  
  670.    return true;
  671. }
  672.  
  673. /**
  674.  * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
  675.  * on window system framebuffers.
  676.  *
  677.  * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
  678.  * your renderbuffer can do sRGB encode, and you can flip a switch that does
  679.  * sRGB encode if the renderbuffer can handle it.  You can ask specifically
  680.  * for a visual where you're guaranteed to be capable, but it turns out that
  681.  * everyone just makes all their ARGB8888 visuals capable and doesn't offer
  682.  * incapable ones, becuase there's no difference between the two in resources
  683.  * used.  Applications thus get built that accidentally rely on the default
  684.  * visual choice being sRGB, so we make ours sRGB capable.  Everything sounds
  685.  * great...
  686.  *
  687.  * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
  688.  * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
  689.  * So they removed the enable knob and made it "if the renderbuffer is sRGB
  690.  * capable, do sRGB encode".  Then, for your window system renderbuffers, you
  691.  * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
  692.  * and get no sRGB encode (assuming that both kinds of visual are available).
  693.  * Thus our choice to support sRGB by default on our visuals for desktop would
  694.  * result in broken rendering of GLES apps that aren't expecting sRGB encode.
  695.  *
  696.  * Unfortunately, renderbuffer setup happens before a context is created.  So
  697.  * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
  698.  * context (without an sRGB visual, though we don't have sRGB visuals exposed
  699.  * yet), we go turn that back off before anyone finds out.
  700.  */
  701. static void
  702. intel_gles3_srgb_workaround(struct brw_context *brw,
  703.                             struct gl_framebuffer *fb)
  704. {
  705.    struct gl_context *ctx = &brw->ctx;
  706.  
  707.    if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
  708.       return;
  709.  
  710.    /* Some day when we support the sRGB capable bit on visuals available for
  711.     * GLES, we'll need to respect that and not disable things here.
  712.     */
  713.    fb->Visual.sRGBCapable = false;
  714.    for (int i = 0; i < BUFFER_COUNT; i++) {
  715.       if (fb->Attachment[i].Renderbuffer &&
  716.           fb->Attachment[i].Renderbuffer->Format == MESA_FORMAT_SARGB8) {
  717.          fb->Attachment[i].Renderbuffer->Format = MESA_FORMAT_ARGB8888;
  718.       }
  719.    }
  720. }
  721.  
  722. GLboolean
  723. intelMakeCurrent(__DRIcontext * driContextPriv,
  724.                  __DRIdrawable * driDrawPriv,
  725.                  __DRIdrawable * driReadPriv)
  726. {
  727.    struct brw_context *brw;
  728.    GET_CURRENT_CONTEXT(curCtx);
  729.  
  730.    if (driContextPriv)
  731.       brw = (struct brw_context *) driContextPriv->driverPrivate;
  732.    else
  733.       brw = NULL;
  734.  
  735.    /* According to the glXMakeCurrent() man page: "Pending commands to
  736.     * the previous context, if any, are flushed before it is released."
  737.     * But only flush if we're actually changing contexts.
  738.     */
  739.    if (brw_context(curCtx) && brw_context(curCtx) != brw) {
  740.       _mesa_flush(curCtx);
  741.    }
  742.  
  743.    if (driContextPriv) {
  744.       struct gl_context *ctx = &brw->ctx;
  745.       struct gl_framebuffer *fb, *readFb;
  746.  
  747.       if (driDrawPriv == NULL && driReadPriv == NULL) {
  748.          fb = _mesa_get_incomplete_framebuffer();
  749.          readFb = _mesa_get_incomplete_framebuffer();
  750.       } else {
  751.          fb = driDrawPriv->driverPrivate;
  752.          readFb = driReadPriv->driverPrivate;
  753.          driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
  754.          driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
  755.       }
  756.  
  757.       /* The sRGB workaround changes the renderbuffer's format. We must change
  758.        * the format before the renderbuffer's miptree get's allocated, otherwise
  759.        * the formats of the renderbuffer and its miptree will differ.
  760.        */
  761.       intel_gles3_srgb_workaround(brw, fb);
  762.       intel_gles3_srgb_workaround(brw, readFb);
  763.  
  764.       intel_prepare_render(brw);
  765.       _mesa_make_current(ctx, fb, readFb);
  766.    }
  767.    else {
  768.       _mesa_make_current(NULL, NULL, NULL);
  769.    }
  770.  
  771.    return true;
  772. }
  773.  
  774. /**
  775.  * \brief Query DRI2 to obtain a DRIdrawable's buffers.
  776.  *
  777.  * To determine which DRI buffers to request, examine the renderbuffers
  778.  * attached to the drawable's framebuffer. Then request the buffers with
  779.  * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
  780.  *
  781.  * This is called from intel_update_renderbuffers().
  782.  *
  783.  * \param drawable      Drawable whose buffers are queried.
  784.  * \param buffers       [out] List of buffers returned by DRI2 query.
  785.  * \param buffer_count  [out] Number of buffers returned.
  786.  *
  787.  * \see intel_update_renderbuffers()
  788.  * \see DRI2GetBuffers()
  789.  * \see DRI2GetBuffersWithFormat()
  790.  */
  791. static void
  792. intel_query_dri2_buffers(struct brw_context *brw,
  793.                          __DRIdrawable *drawable,
  794.                          __DRIbuffer **buffers,
  795.                          int *buffer_count)
  796. {
  797.    __DRIscreen *screen = brw->intelScreen->driScrnPriv;
  798.    struct gl_framebuffer *fb = drawable->driverPrivate;
  799.    int i = 0;
  800.    unsigned attachments[8];
  801.  
  802.    struct intel_renderbuffer *front_rb;
  803.    struct intel_renderbuffer *back_rb;
  804.  
  805.    front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
  806.    back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
  807.  
  808.    memset(attachments, 0, sizeof(attachments));
  809.    if ((brw->is_front_buffer_rendering ||
  810.         brw->is_front_buffer_reading ||
  811.         !back_rb) && front_rb) {
  812.       /* If a fake front buffer is in use, then querying for
  813.        * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
  814.        * the real front buffer to the fake front buffer.  So before doing the
  815.        * query, we need to make sure all the pending drawing has landed in the
  816.        * real front buffer.
  817.        */
  818.       intel_flush(&brw->ctx);
  819.       intel_flush_front(&brw->ctx);
  820.  
  821.       attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
  822.       attachments[i++] = intel_bits_per_pixel(front_rb);
  823.    } else if (front_rb && brw->front_buffer_dirty) {
  824.       /* We have pending front buffer rendering, but we aren't querying for a
  825.        * front buffer.  If the front buffer we have is a fake front buffer,
  826.        * the X server is going to throw it away when it processes the query.
  827.        * So before doing the query, make sure all the pending drawing has
  828.        * landed in the real front buffer.
  829.        */
  830.       intel_flush(&brw->ctx);
  831.       intel_flush_front(&brw->ctx);
  832.    }
  833.  
  834.    if (back_rb) {
  835.       attachments[i++] = __DRI_BUFFER_BACK_LEFT;
  836.       attachments[i++] = intel_bits_per_pixel(back_rb);
  837.    }
  838.  
  839.    assert(i <= ARRAY_SIZE(attachments));
  840.  
  841.    *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
  842.                                                         &drawable->w,
  843.                                                         &drawable->h,
  844.                                                         attachments, i / 2,
  845.                                                         buffer_count,
  846.                                                         drawable->loaderPrivate);
  847.     LEAVE();
  848. }
  849.  
  850. /**
  851.  * \brief Assign a DRI buffer's DRM region to a renderbuffer.
  852.  *
  853.  * This is called from intel_update_renderbuffers().
  854.  *
  855.  * \par Note:
  856.  *    DRI buffers whose attachment point is DRI2BufferStencil or
  857.  *    DRI2BufferDepthStencil are handled as special cases.
  858.  *
  859.  * \param buffer_name is a human readable name, such as "dri2 front buffer",
  860.  *        that is passed to intel_region_alloc_for_handle().
  861.  *
  862.  * \see intel_update_renderbuffers()
  863.  * \see intel_region_alloc_for_handle()
  864.  */
  865. static void
  866. intel_process_dri2_buffer(struct brw_context *brw,
  867.                           __DRIdrawable *drawable,
  868.                           __DRIbuffer *buffer,
  869.                           struct intel_renderbuffer *rb,
  870.                           const char *buffer_name)
  871. {
  872.    struct intel_region *region = NULL;
  873.  
  874.    if (!rb)
  875.       return;
  876.  
  877.    unsigned num_samples = rb->Base.Base.NumSamples;
  878.  
  879.    /* We try to avoid closing and reopening the same BO name, because the first
  880.     * use of a mapping of the buffer involves a bunch of page faulting which is
  881.     * moderately expensive.
  882.     */
  883.    if (num_samples == 0) {
  884.        if (rb->mt &&
  885.            rb->mt->region &&
  886.            rb->mt->region->name == buffer->name)
  887.           return;
  888.    } else {
  889.        if (rb->mt &&
  890.            rb->mt->singlesample_mt &&
  891.            rb->mt->singlesample_mt->region &&
  892.            rb->mt->singlesample_mt->region->name == buffer->name)
  893.           return;
  894.    }
  895.  
  896.    if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
  897.       fprintf(stderr,
  898.               "attaching buffer %d, at %d, cpp %d, pitch %d\n",
  899.               buffer->name, buffer->attachment,
  900.               buffer->cpp, buffer->pitch);
  901.    }
  902.  
  903.    intel_miptree_release(&rb->mt);
  904.    region = intel_region_alloc_for_handle(brw->intelScreen,
  905.                                           buffer->cpp,
  906.                                           drawable->w,
  907.                                           drawable->h,
  908.                                           buffer->pitch,
  909.                                           buffer->name,
  910.                                           buffer_name);
  911.    if (!region)
  912.       return;
  913.  
  914.    rb->mt = intel_miptree_create_for_dri2_buffer(brw,
  915.                                                  buffer->attachment,
  916.                                                  intel_rb_format(rb),
  917.                                                  num_samples,
  918.                                                  region);
  919.    intel_region_release(&region);
  920. }
  921.