Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2003 VMware, Inc.
  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 VMWARE 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/mtypes.h"
  30. #include "main/context.h"
  31. #include "main/enums.h"
  32. #include "main/colormac.h"
  33. #include "main/fbobject.h"
  34.  
  35. #include "brw_context.h"
  36. #include "brw_defines.h"
  37. #include "intel_blit.h"
  38. #include "intel_buffers.h"
  39. #include "intel_fbo.h"
  40. #include "intel_reg.h"
  41. #include "intel_batchbuffer.h"
  42. #include "intel_mipmap_tree.h"
  43.  
  44. #define FILE_DEBUG_FLAG DEBUG_BLIT
  45.  
  46. static void
  47. intel_miptree_set_alpha_to_one(struct brw_context *brw,
  48.                                struct intel_mipmap_tree *mt,
  49.                                int x, int y, int width, int height);
  50.  
  51. static GLuint translate_raster_op(GLenum logicop)
  52. {
  53.    switch(logicop) {
  54.    case GL_CLEAR: return 0x00;
  55.    case GL_AND: return 0x88;
  56.    case GL_AND_REVERSE: return 0x44;
  57.    case GL_COPY: return 0xCC;
  58.    case GL_AND_INVERTED: return 0x22;
  59.    case GL_NOOP: return 0xAA;
  60.    case GL_XOR: return 0x66;
  61.    case GL_OR: return 0xEE;
  62.    case GL_NOR: return 0x11;
  63.    case GL_EQUIV: return 0x99;
  64.    case GL_INVERT: return 0x55;
  65.    case GL_OR_REVERSE: return 0xDD;
  66.    case GL_COPY_INVERTED: return 0x33;
  67.    case GL_OR_INVERTED: return 0xBB;
  68.    case GL_NAND: return 0x77;
  69.    case GL_SET: return 0xFF;
  70.    default: return 0;
  71.    }
  72. }
  73.  
  74. static uint32_t
  75. br13_for_cpp(int cpp)
  76. {
  77.    switch (cpp) {
  78.    case 4:
  79.       return BR13_8888;
  80.       break;
  81.    case 2:
  82.       return BR13_565;
  83.       break;
  84.    case 1:
  85.       return BR13_8;
  86.       break;
  87.    default:
  88.       unreachable("not reached");
  89.    }
  90. }
  91.  
  92. /**
  93.  * Emits the packet for switching the blitter from X to Y tiled or back.
  94.  *
  95.  * This has to be called in a single BEGIN_BATCH_BLT_TILED() /
  96.  * ADVANCE_BATCH_TILED().  This is because BCS_SWCTRL is saved and restored as
  97.  * part of the power context, not a render context, and if the batchbuffer was
  98.  * to get flushed between setting and blitting, or blitting and restoring, our
  99.  * tiling state would leak into other unsuspecting applications (like the X
  100.  * server).
  101.  */
  102. static void
  103. set_blitter_tiling(struct brw_context *brw,
  104.                    bool dst_y_tiled, bool src_y_tiled)
  105. {
  106.    assert(brw->gen >= 6);
  107.  
  108.    /* Idle the blitter before we update how tiling is interpreted. */
  109.    OUT_BATCH(MI_FLUSH_DW);
  110.    OUT_BATCH(0);
  111.    OUT_BATCH(0);
  112.    OUT_BATCH(0);
  113.  
  114.    OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
  115.    OUT_BATCH(BCS_SWCTRL);
  116.    OUT_BATCH((BCS_SWCTRL_DST_Y | BCS_SWCTRL_SRC_Y) << 16 |
  117.              (dst_y_tiled ? BCS_SWCTRL_DST_Y : 0) |
  118.              (src_y_tiled ? BCS_SWCTRL_SRC_Y : 0));
  119. }
  120.  
  121. #define BEGIN_BATCH_BLT_TILED(n, dst_y_tiled, src_y_tiled) do {         \
  122.       BEGIN_BATCH_BLT(n + ((dst_y_tiled || src_y_tiled) ? 14 : 0));     \
  123.       if (dst_y_tiled || src_y_tiled)                                   \
  124.          set_blitter_tiling(brw, dst_y_tiled, src_y_tiled);             \
  125.    } while (0)
  126.  
  127. #define ADVANCE_BATCH_TILED(dst_y_tiled, src_y_tiled) do {              \
  128.       if (dst_y_tiled || src_y_tiled)                                   \
  129.          set_blitter_tiling(brw, false, false);                         \
  130.       ADVANCE_BATCH();                                                  \
  131.    } while (0)
  132.  
  133. /**
  134.  * Implements a rectangular block transfer (blit) of pixels between two
  135.  * miptrees.
  136.  *
  137.  * Our blitter can operate on 1, 2, or 4-byte-per-pixel data, with generous,
  138.  * but limited, pitches and sizes allowed.
  139.  *
  140.  * The src/dst coordinates are relative to the given level/slice of the
  141.  * miptree.
  142.  *
  143.  * If @src_flip or @dst_flip is set, then the rectangle within that miptree
  144.  * will be inverted (including scanline order) when copying.  This is common
  145.  * in GL when copying between window system and user-created
  146.  * renderbuffers/textures.
  147.  */
  148. bool
  149. intel_miptree_blit(struct brw_context *brw,
  150.                    struct intel_mipmap_tree *src_mt,
  151.                    int src_level, int src_slice,
  152.                    uint32_t src_x, uint32_t src_y, bool src_flip,
  153.                    struct intel_mipmap_tree *dst_mt,
  154.                    int dst_level, int dst_slice,
  155.                    uint32_t dst_x, uint32_t dst_y, bool dst_flip,
  156.                    uint32_t width, uint32_t height,
  157.                    GLenum logicop)
  158. {
  159.    /* The blitter doesn't understand multisampling at all. */
  160.    if (src_mt->num_samples > 0 || dst_mt->num_samples > 0)
  161.       return false;
  162.  
  163.    /* No sRGB decode or encode is done by the hardware blitter, which is
  164.     * consistent with what we want in the callers (glCopyTexSubImage(),
  165.     * glBlitFramebuffer(), texture validation, etc.).
  166.     */
  167.    mesa_format src_format = _mesa_get_srgb_format_linear(src_mt->format);
  168.    mesa_format dst_format = _mesa_get_srgb_format_linear(dst_mt->format);
  169.  
  170.    /* The blitter doesn't support doing any format conversions.  We do also
  171.     * support blitting ARGB8888 to XRGB8888 (trivial, the values dropped into
  172.     * the X channel don't matter), and XRGB8888 to ARGB8888 by setting the A
  173.     * channel to 1.0 at the end.
  174.     */
  175.    if (src_format != dst_format &&
  176.       ((src_format != MESA_FORMAT_B8G8R8A8_UNORM &&
  177.         src_format != MESA_FORMAT_B8G8R8X8_UNORM) ||
  178.        (dst_format != MESA_FORMAT_B8G8R8A8_UNORM &&
  179.         dst_format != MESA_FORMAT_B8G8R8X8_UNORM))) {
  180.       perf_debug("%s: Can't use hardware blitter from %s to %s, "
  181.                  "falling back.\n", __func__,
  182.                  _mesa_get_format_name(src_format),
  183.                  _mesa_get_format_name(dst_format));
  184.       return false;
  185.    }
  186.  
  187.    /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
  188.     * Data Size Limitations):
  189.     *
  190.     *    The BLT engine is capable of transferring very large quantities of
  191.     *    graphics data. Any graphics data read from and written to the
  192.     *    destination is permitted to represent a number of pixels that
  193.     *    occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
  194.     *    at the destination. The maximum number of pixels that may be
  195.     *    represented per scan line’s worth of graphics data depends on the
  196.     *    color depth.
  197.     *
  198.     * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
  199.     * 16-bit integer to represent buffer pitch, so it can only handle buffer
  200.     * pitches < 32k.
  201.     *
  202.     * As a result of these two limitations, we can only use the blitter to do
  203.     * this copy when the miptree's pitch is less than 32k.
  204.     */
  205.    if (src_mt->pitch >= 32768 ||
  206.        dst_mt->pitch >= 32768) {
  207.       perf_debug("Falling back due to >=32k pitch\n");
  208.       return false;
  209.    }
  210.  
  211.    /* The blitter has no idea about HiZ or fast color clears, so we need to
  212.     * resolve the miptrees before we do anything.
  213.     */
  214.    intel_miptree_slice_resolve_depth(brw, src_mt, src_level, src_slice);
  215.    intel_miptree_slice_resolve_depth(brw, dst_mt, dst_level, dst_slice);
  216.    intel_miptree_resolve_color(brw, src_mt);
  217.    intel_miptree_resolve_color(brw, dst_mt);
  218.  
  219.    if (src_flip)
  220.       src_y = minify(src_mt->physical_height0, src_level - src_mt->first_level) - src_y - height;
  221.  
  222.    if (dst_flip)
  223.       dst_y = minify(dst_mt->physical_height0, dst_level - dst_mt->first_level) - dst_y - height;
  224.  
  225.    int src_pitch = src_mt->pitch;
  226.    if (src_flip != dst_flip)
  227.       src_pitch = -src_pitch;
  228.  
  229.    uint32_t src_image_x, src_image_y, dst_image_x, dst_image_y;
  230.    intel_miptree_get_image_offset(src_mt, src_level, src_slice,
  231.                                   &src_image_x, &src_image_y);
  232.    intel_miptree_get_image_offset(dst_mt, dst_level, dst_slice,
  233.                                   &dst_image_x, &dst_image_y);
  234.    src_x += src_image_x;
  235.    src_y += src_image_y;
  236.    dst_x += dst_image_x;
  237.    dst_y += dst_image_y;
  238.  
  239.    /* The blitter interprets the 16-bit destination x/y as a signed 16-bit
  240.     * value. The values we're working with are unsigned, so make sure we don't
  241.     * overflow.
  242.     */
  243.    if (src_x >= 32768 || src_y >= 32768 || dst_x >= 32768 || dst_y >= 32768) {
  244.       perf_debug("Falling back due to >=32k offset [src(%d, %d) dst(%d, %d)]\n",
  245.                  src_x, src_y, dst_x, dst_y);
  246.       return false;
  247.    }
  248.  
  249.    if (!intelEmitCopyBlit(brw,
  250.                           src_mt->cpp,
  251.                           src_pitch,
  252.                           src_mt->bo, src_mt->offset,
  253.                           src_mt->tiling,
  254.                           dst_mt->pitch,
  255.                           dst_mt->bo, dst_mt->offset,
  256.                           dst_mt->tiling,
  257.                           src_x, src_y,
  258.                           dst_x, dst_y,
  259.                           width, height,
  260.                           logicop)) {
  261.       return false;
  262.    }
  263.  
  264.    if (src_mt->format == MESA_FORMAT_B8G8R8X8_UNORM &&
  265.        dst_mt->format == MESA_FORMAT_B8G8R8A8_UNORM) {
  266.       intel_miptree_set_alpha_to_one(brw, dst_mt,
  267.                                      dst_x, dst_y,
  268.                                      width, height);
  269.    }
  270.  
  271.    return true;
  272. }
  273.  
  274. static bool
  275. alignment_valid(struct brw_context *brw, unsigned offset, uint32_t tiling)
  276. {
  277.    /* Tiled buffers must be page-aligned (4K). */
  278.    if (tiling != I915_TILING_NONE)
  279.       return (offset & 4095) == 0;
  280.  
  281.    /* On Gen8+, linear buffers must be cacheline-aligned. */
  282.    if (brw->gen >= 8)
  283.       return (offset & 63) == 0;
  284.  
  285.    return true;
  286. }
  287.  
  288. /* Copy BitBlt
  289.  */
  290. bool
  291. intelEmitCopyBlit(struct brw_context *brw,
  292.                   GLuint cpp,
  293.                   GLshort src_pitch,
  294.                   drm_intel_bo *src_buffer,
  295.                   GLuint src_offset,
  296.                   uint32_t src_tiling,
  297.                   GLshort dst_pitch,
  298.                   drm_intel_bo *dst_buffer,
  299.                   GLuint dst_offset,
  300.                   uint32_t dst_tiling,
  301.                   GLshort src_x, GLshort src_y,
  302.                   GLshort dst_x, GLshort dst_y,
  303.                   GLshort w, GLshort h,
  304.                   GLenum logic_op)
  305. {
  306.    GLuint CMD, BR13, pass = 0;
  307.    int dst_y2 = dst_y + h;
  308.    int dst_x2 = dst_x + w;
  309.    drm_intel_bo *aper_array[3];
  310.    bool dst_y_tiled = dst_tiling == I915_TILING_Y;
  311.    bool src_y_tiled = src_tiling == I915_TILING_Y;
  312.  
  313.    if (!alignment_valid(brw, dst_offset, dst_tiling))
  314.       return false;
  315.    if (!alignment_valid(brw, src_offset, src_tiling))
  316.       return false;
  317.  
  318.    if ((dst_y_tiled || src_y_tiled) && brw->gen < 6)
  319.       return false;
  320.  
  321.    assert(!dst_y_tiled || (dst_pitch % 128) == 0);
  322.    assert(!src_y_tiled || (src_pitch % 128) == 0);
  323.  
  324.    /* do space check before going any further */
  325.    do {
  326.        aper_array[0] = brw->batch.bo;
  327.        aper_array[1] = dst_buffer;
  328.        aper_array[2] = src_buffer;
  329.  
  330.        if (dri_bufmgr_check_aperture_space(aper_array, 3) != 0) {
  331.            intel_batchbuffer_flush(brw);
  332.            pass++;
  333.        } else
  334.            break;
  335.    } while (pass < 2);
  336.  
  337.    if (pass >= 2)
  338.       return false;
  339.  
  340.    unsigned length = brw->gen >= 8 ? 10 : 8;
  341.  
  342.    intel_batchbuffer_require_space(brw, length * 4, BLT_RING);
  343.    DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
  344.        __func__,
  345.        src_buffer, src_pitch, src_offset, src_x, src_y,
  346.        dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h);
  347.  
  348.    /* Blit pitch must be dword-aligned.  Otherwise, the hardware appears to drop
  349.     * the low bits.  Offsets must be naturally aligned.
  350.     */
  351.    if (src_pitch % 4 != 0 || src_offset % cpp != 0 ||
  352.        dst_pitch % 4 != 0 || dst_offset % cpp != 0)
  353.       return false;
  354.  
  355.    /* For big formats (such as floating point), do the copy using 16 or 32bpp
  356.     * and multiply the coordinates.
  357.     */
  358.    if (cpp > 4) {
  359.       if (cpp % 4 == 2) {
  360.          dst_x *= cpp / 2;
  361.          dst_x2 *= cpp / 2;
  362.          src_x *= cpp / 2;
  363.          cpp = 2;
  364.       } else {
  365.          assert(cpp % 4 == 0);
  366.          dst_x *= cpp / 4;
  367.          dst_x2 *= cpp / 4;
  368.          src_x *= cpp / 4;
  369.          cpp = 4;
  370.       }
  371.    }
  372.  
  373.    BR13 = br13_for_cpp(cpp) | translate_raster_op(logic_op) << 16;
  374.  
  375.    switch (cpp) {
  376.    case 1:
  377.    case 2:
  378.       CMD = XY_SRC_COPY_BLT_CMD;
  379.       break;
  380.    case 4:
  381.       CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
  382.       break;
  383.    default:
  384.       return false;
  385.    }
  386.  
  387.    if (dst_tiling != I915_TILING_NONE) {
  388.       CMD |= XY_DST_TILED;
  389.       dst_pitch /= 4;
  390.    }
  391.    if (src_tiling != I915_TILING_NONE) {
  392.       CMD |= XY_SRC_TILED;
  393.       src_pitch /= 4;
  394.    }
  395.  
  396.    if (dst_y2 <= dst_y || dst_x2 <= dst_x) {
  397.       return true;
  398.    }
  399.  
  400.    assert(dst_x < dst_x2);
  401.    assert(dst_y < dst_y2);
  402.    assert(src_offset + (src_y + h - 1) * abs(src_pitch) +
  403.           (w * cpp) <= src_buffer->size);
  404.    assert(dst_offset + (dst_y + h - 1) * abs(dst_pitch) +
  405.           (w * cpp) <= dst_buffer->size);
  406.  
  407.    BEGIN_BATCH_BLT_TILED(length, dst_y_tiled, src_y_tiled);
  408.    OUT_BATCH(CMD | (length - 2));
  409.    OUT_BATCH(BR13 | (uint16_t)dst_pitch);
  410.    OUT_BATCH(SET_FIELD(dst_y, BLT_Y) | SET_FIELD(dst_x, BLT_X));
  411.    OUT_BATCH(SET_FIELD(dst_y2, BLT_Y) | SET_FIELD(dst_x2, BLT_X));
  412.    if (brw->gen >= 8) {
  413.       OUT_RELOC64(dst_buffer,
  414.                   I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  415.                   dst_offset);
  416.    } else {
  417.       OUT_RELOC(dst_buffer,
  418.                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  419.                 dst_offset);
  420.    }
  421.    OUT_BATCH(SET_FIELD(src_y, BLT_Y) | SET_FIELD(src_x, BLT_X));
  422.    OUT_BATCH((uint16_t)src_pitch);
  423.    if (brw->gen >= 8) {
  424.       OUT_RELOC64(src_buffer,
  425.                   I915_GEM_DOMAIN_RENDER, 0,
  426.                   src_offset);
  427.    } else {
  428.       OUT_RELOC(src_buffer,
  429.                 I915_GEM_DOMAIN_RENDER, 0,
  430.                 src_offset);
  431.    }
  432.  
  433.    ADVANCE_BATCH_TILED(dst_y_tiled, src_y_tiled);
  434.  
  435.    intel_batchbuffer_emit_mi_flush(brw);
  436.  
  437.    return true;
  438. }
  439.  
  440. bool
  441. intelEmitImmediateColorExpandBlit(struct brw_context *brw,
  442.                                   GLuint cpp,
  443.                                   GLubyte *src_bits, GLuint src_size,
  444.                                   GLuint fg_color,
  445.                                   GLshort dst_pitch,
  446.                                   drm_intel_bo *dst_buffer,
  447.                                   GLuint dst_offset,
  448.                                   uint32_t dst_tiling,
  449.                                   GLshort x, GLshort y,
  450.                                   GLshort w, GLshort h,
  451.                                   GLenum logic_op)
  452. {
  453.    int dwords = ALIGN(src_size, 8) / 4;
  454.    uint32_t opcode, br13, blit_cmd;
  455.  
  456.    if (dst_tiling != I915_TILING_NONE) {
  457.       if (dst_offset & 4095)
  458.          return false;
  459.       if (dst_tiling == I915_TILING_Y)
  460.          return false;
  461.    }
  462.  
  463.    assert((logic_op >= GL_CLEAR) && (logic_op <= (GL_CLEAR + 0x0f)));
  464.    assert(dst_pitch > 0);
  465.  
  466.    if (w < 0 || h < 0)
  467.       return true;
  468.  
  469.    DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
  470.        __func__,
  471.        dst_buffer, dst_pitch, dst_offset, x, y, w, h, src_size, dwords);
  472.  
  473.    unsigned xy_setup_blt_length = brw->gen >= 8 ? 10 : 8;
  474.    intel_batchbuffer_require_space(brw, (xy_setup_blt_length * 4) +
  475.                                         (3 * 4) + dwords * 4, BLT_RING);
  476.  
  477.    opcode = XY_SETUP_BLT_CMD;
  478.    if (cpp == 4)
  479.       opcode |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
  480.    if (dst_tiling != I915_TILING_NONE) {
  481.       opcode |= XY_DST_TILED;
  482.       dst_pitch /= 4;
  483.    }
  484.  
  485.    br13 = dst_pitch | (translate_raster_op(logic_op) << 16) | (1 << 29);
  486.    br13 |= br13_for_cpp(cpp);
  487.  
  488.    blit_cmd = XY_TEXT_IMMEDIATE_BLIT_CMD | XY_TEXT_BYTE_PACKED; /* packing? */
  489.    if (dst_tiling != I915_TILING_NONE)
  490.       blit_cmd |= XY_DST_TILED;
  491.  
  492.    BEGIN_BATCH_BLT(xy_setup_blt_length + 3);
  493.    OUT_BATCH(opcode | (xy_setup_blt_length - 2));
  494.    OUT_BATCH(br13);
  495.    OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
  496.    OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
  497.    if (brw->gen >= 8) {
  498.       OUT_RELOC64(dst_buffer,
  499.                   I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  500.                   dst_offset);
  501.    } else {
  502.       OUT_RELOC(dst_buffer,
  503.                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  504.                 dst_offset);
  505.    }
  506.    OUT_BATCH(0); /* bg */
  507.    OUT_BATCH(fg_color); /* fg */
  508.    OUT_BATCH(0); /* pattern base addr */
  509.    if (brw->gen >= 8)
  510.       OUT_BATCH(0);
  511.  
  512.    OUT_BATCH(blit_cmd | ((3 - 2) + dwords));
  513.    OUT_BATCH(SET_FIELD(y, BLT_Y) | SET_FIELD(x, BLT_X));
  514.    OUT_BATCH(SET_FIELD(y + h, BLT_Y) | SET_FIELD(x + w, BLT_X));
  515.    ADVANCE_BATCH();
  516.  
  517.    intel_batchbuffer_data(brw, src_bits, dwords * 4, BLT_RING);
  518.  
  519.    intel_batchbuffer_emit_mi_flush(brw);
  520.  
  521.    return true;
  522. }
  523.  
  524. /* We don't have a memmove-type blit like some other hardware, so we'll do a
  525.  * rectangular blit covering a large space, then emit 1-scanline blit at the
  526.  * end to cover the last if we need.
  527.  */
  528. void
  529. intel_emit_linear_blit(struct brw_context *brw,
  530.                        drm_intel_bo *dst_bo,
  531.                        unsigned int dst_offset,
  532.                        drm_intel_bo *src_bo,
  533.                        unsigned int src_offset,
  534.                        unsigned int size)
  535. {
  536.    struct gl_context *ctx = &brw->ctx;
  537.    GLuint pitch, height;
  538.    int16_t src_x, dst_x;
  539.    bool ok;
  540.  
  541.    /* The pitch given to the GPU must be DWORD aligned, and
  542.     * we want width to match pitch. Max width is (1 << 15 - 1),
  543.     * rounding that down to the nearest DWORD is 1 << 15 - 4
  544.     */
  545.    pitch = ROUND_DOWN_TO(MIN2(size, (1 << 15) - 1), 4);
  546.    height = (pitch == 0) ? 1 : size / pitch;
  547.    src_x = src_offset % 64;
  548.    dst_x = dst_offset % 64;
  549.    ok = intelEmitCopyBlit(brw, 1,
  550.                           pitch, src_bo, src_offset - src_x, I915_TILING_NONE,
  551.                           pitch, dst_bo, dst_offset - dst_x, I915_TILING_NONE,
  552.                           src_x, 0, /* src x/y */
  553.                           dst_x, 0, /* dst x/y */
  554.                           pitch, height, /* w, h */
  555.                           GL_COPY);
  556.    if (!ok)
  557.       _mesa_problem(ctx, "Failed to linear blit %dx%d\n", pitch, height);
  558.  
  559.    src_offset += pitch * height;
  560.    dst_offset += pitch * height;
  561.    src_x = src_offset % 64;
  562.    dst_x = dst_offset % 64;
  563.    size -= pitch * height;
  564.    assert (size < (1 << 15));
  565.    pitch = ALIGN(size, 4);
  566.  
  567.    if (size != 0) {
  568.       ok = intelEmitCopyBlit(brw, 1,
  569.                              pitch, src_bo, src_offset - src_x, I915_TILING_NONE,
  570.                              pitch, dst_bo, dst_offset - dst_x, I915_TILING_NONE,
  571.                              src_x, 0, /* src x/y */
  572.                              dst_x, 0, /* dst x/y */
  573.                              size, 1, /* w, h */
  574.                              GL_COPY);
  575.       if (!ok)
  576.          _mesa_problem(ctx, "Failed to linear blit %dx%d\n", size, 1);
  577.    }
  578. }
  579.  
  580. /**
  581.  * Used to initialize the alpha value of an ARGB8888 miptree after copying
  582.  * into it from an XRGB8888 source.
  583.  *
  584.  * This is very common with glCopyTexImage2D().  Note that the coordinates are
  585.  * relative to the start of the miptree, not relative to a slice within the
  586.  * miptree.
  587.  */
  588. static void
  589. intel_miptree_set_alpha_to_one(struct brw_context *brw,
  590.                               struct intel_mipmap_tree *mt,
  591.                               int x, int y, int width, int height)
  592. {
  593.    uint32_t BR13, CMD;
  594.    int pitch, cpp;
  595.    drm_intel_bo *aper_array[2];
  596.  
  597.    pitch = mt->pitch;
  598.    cpp = mt->cpp;
  599.  
  600.    DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
  601.        __func__, mt->bo, pitch, x, y, width, height);
  602.  
  603.    BR13 = br13_for_cpp(cpp) | 0xf0 << 16;
  604.    CMD = XY_COLOR_BLT_CMD;
  605.    CMD |= XY_BLT_WRITE_ALPHA;
  606.  
  607.    if (mt->tiling != I915_TILING_NONE) {
  608.       CMD |= XY_DST_TILED;
  609.       pitch /= 4;
  610.    }
  611.    BR13 |= pitch;
  612.  
  613.    /* do space check before going any further */
  614.    aper_array[0] = brw->batch.bo;
  615.    aper_array[1] = mt->bo;
  616.  
  617.    if (drm_intel_bufmgr_check_aperture_space(aper_array,
  618.                                              ARRAY_SIZE(aper_array)) != 0) {
  619.       intel_batchbuffer_flush(brw);
  620.    }
  621.  
  622.    unsigned length = brw->gen >= 8 ? 7 : 6;
  623.    bool dst_y_tiled = mt->tiling == I915_TILING_Y;
  624.  
  625.    BEGIN_BATCH_BLT_TILED(length, dst_y_tiled, false);
  626.    OUT_BATCH(CMD | (length - 2));
  627.    OUT_BATCH(BR13);
  628.    OUT_BATCH(SET_FIELD(y, BLT_Y) | SET_FIELD(x, BLT_X));
  629.    OUT_BATCH(SET_FIELD(y + height, BLT_Y) | SET_FIELD(x + width, BLT_X));
  630.    if (brw->gen >= 8) {
  631.       OUT_RELOC64(mt->bo,
  632.                   I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  633.                   0);
  634.    } else {
  635.       OUT_RELOC(mt->bo,
  636.                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  637.                 0);
  638.    }
  639.    OUT_BATCH(0xffffffff); /* white, but only alpha gets written */
  640.    ADVANCE_BATCH_TILED(dst_y_tiled, false);
  641.  
  642.    intel_batchbuffer_emit_mi_flush(brw);
  643. }
  644.