Home | History | Annotate | Download | only in state_tracker
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 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   * Authors:
     30   *   Brian Paul
     31   */
     32 
     33 #include "main/imports.h"
     34 #include "main/image.h"
     35 #include "main/macros.h"
     36 
     37 #include "st_context.h"
     38 #include "st_texture.h"
     39 #include "st_cb_bitmap.h"
     40 #include "st_cb_blit.h"
     41 #include "st_cb_fbo.h"
     42 #include "st_manager.h"
     43 #include "st_scissor.h"
     44 
     45 #include "util/u_format.h"
     46 
     47 static void
     48 st_BlitFramebuffer(struct gl_context *ctx,
     49                    struct gl_framebuffer *readFB,
     50                    struct gl_framebuffer *drawFB,
     51                    GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
     52                    GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
     53                    GLbitfield mask, GLenum filter)
     54 {
     55    const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
     56                                     GL_STENCIL_BUFFER_BIT);
     57    struct st_context *st = st_context(ctx);
     58    const uint pFilter = ((filter == GL_NEAREST)
     59                          ? PIPE_TEX_FILTER_NEAREST
     60                          : PIPE_TEX_FILTER_LINEAR);
     61    struct {
     62       GLint srcX0, srcY0, srcX1, srcY1;
     63       GLint dstX0, dstY0, dstX1, dstY1;
     64    } clip;
     65    struct pipe_blit_info blit;
     66 
     67    st_manager_validate_framebuffers(st);
     68 
     69    /* Make sure bitmap rendering has landed in the framebuffers */
     70    st_flush_bitmap_cache(st);
     71    st_invalidate_readpix_cache(st);
     72 
     73    clip.srcX0 = srcX0;
     74    clip.srcY0 = srcY0;
     75    clip.srcX1 = srcX1;
     76    clip.srcY1 = srcY1;
     77    clip.dstX0 = dstX0;
     78    clip.dstY0 = dstY0;
     79    clip.dstX1 = dstX1;
     80    clip.dstY1 = dstY1;
     81 
     82    /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
     83     * the integer coordinates to account for clipping (or scissors) because that
     84     * would make us cut off fractional parts, affecting the result of the blit.
     85     *
     86     * XXX: This should depend on mask !
     87     */
     88    if (!_mesa_clip_blit(ctx, readFB, drawFB,
     89                         &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
     90                         &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
     91       return; /* nothing to draw/blit */
     92    }
     93    memset(&blit, 0, sizeof(struct pipe_blit_info));
     94    blit.scissor_enable =
     95       (dstX0 != clip.dstX0) ||
     96       (dstY0 != clip.dstY0) ||
     97       (dstX1 != clip.dstX1) ||
     98       (dstY1 != clip.dstY1);
     99 
    100    if (st_fb_orientation(drawFB) == Y_0_TOP) {
    101       /* invert Y for dest */
    102       dstY0 = drawFB->Height - dstY0;
    103       dstY1 = drawFB->Height - dstY1;
    104       /* invert Y for clip */
    105       clip.dstY0 = drawFB->Height - clip.dstY0;
    106       clip.dstY1 = drawFB->Height - clip.dstY1;
    107    }
    108    if (blit.scissor_enable) {
    109       blit.scissor.minx = MIN2(clip.dstX0, clip.dstX1);
    110       blit.scissor.miny = MIN2(clip.dstY0, clip.dstY1);
    111       blit.scissor.maxx = MAX2(clip.dstX0, clip.dstX1);
    112       blit.scissor.maxy = MAX2(clip.dstY0, clip.dstY1);
    113 #if 0
    114       debug_printf("scissor = (%i,%i)-(%i,%i)\n",
    115                    blit.scissor.minx,blit.scissor.miny,
    116                    blit.scissor.maxx,blit.scissor.maxy);
    117 #endif
    118    }
    119 
    120    if (st_fb_orientation(readFB) == Y_0_TOP) {
    121       /* invert Y for src */
    122       srcY0 = readFB->Height - srcY0;
    123       srcY1 = readFB->Height - srcY1;
    124    }
    125 
    126    if (srcY0 > srcY1 && dstY0 > dstY1) {
    127       /* Both src and dst are upside down.  Swap Y to make it
    128        * right-side up to increase odds of using a fast path.
    129        * Recall that all Gallium raster coords have Y=0=top.
    130        */
    131       GLint tmp;
    132       tmp = srcY0;
    133       srcY0 = srcY1;
    134       srcY1 = tmp;
    135       tmp = dstY0;
    136       dstY0 = dstY1;
    137       dstY1 = tmp;
    138    }
    139 
    140    blit.src.box.depth = 1;
    141    blit.dst.box.depth = 1;
    142 
    143    /* Destination dimensions have to be positive: */
    144    if (dstX0 < dstX1) {
    145       blit.dst.box.x = dstX0;
    146       blit.src.box.x = srcX0;
    147       blit.dst.box.width = dstX1 - dstX0;
    148       blit.src.box.width = srcX1 - srcX0;
    149    } else {
    150       blit.dst.box.x = dstX1;
    151       blit.src.box.x = srcX1;
    152       blit.dst.box.width = dstX0 - dstX1;
    153       blit.src.box.width = srcX0 - srcX1;
    154    }
    155    if (dstY0 < dstY1) {
    156       blit.dst.box.y = dstY0;
    157       blit.src.box.y = srcY0;
    158       blit.dst.box.height = dstY1 - dstY0;
    159       blit.src.box.height = srcY1 - srcY0;
    160    } else {
    161       blit.dst.box.y = dstY1;
    162       blit.src.box.y = srcY1;
    163       blit.dst.box.height = dstY0 - dstY1;
    164       blit.src.box.height = srcY0 - srcY1;
    165    }
    166 
    167    if (drawFB != ctx->WinSysDrawBuffer)
    168       st_window_rectangles_to_blit(ctx, &blit);
    169 
    170    blit.filter = pFilter;
    171    blit.render_condition_enable = TRUE;
    172    blit.alpha_blend = FALSE;
    173 
    174    if (mask & GL_COLOR_BUFFER_BIT) {
    175       struct gl_renderbuffer_attachment *srcAtt =
    176          &readFB->Attachment[readFB->_ColorReadBufferIndex];
    177 
    178       blit.mask = PIPE_MASK_RGBA;
    179 
    180       if (srcAtt->Type == GL_TEXTURE) {
    181          struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
    182          GLuint i;
    183 
    184          if (!srcObj || !srcObj->pt) {
    185             return;
    186          }
    187 
    188          for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
    189             struct st_renderbuffer *dstRb =
    190                st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
    191 
    192             if (dstRb) {
    193                struct pipe_surface *dstSurf;
    194 
    195                st_update_renderbuffer_surface(st, dstRb);
    196 
    197                dstSurf = dstRb->surface;
    198 
    199                if (dstSurf) {
    200                   blit.dst.resource = dstSurf->texture;
    201                   blit.dst.level = dstSurf->u.tex.level;
    202                   blit.dst.box.z = dstSurf->u.tex.first_layer;
    203                   blit.dst.format = dstSurf->format;
    204 
    205                   blit.src.resource = srcObj->pt;
    206                   blit.src.level = srcAtt->TextureLevel;
    207                   blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
    208                   blit.src.format = srcObj->pt->format;
    209 
    210                   if (!ctx->Color.sRGBEnabled)
    211                      blit.src.format = util_format_linear(blit.src.format);
    212 
    213                   st->pipe->blit(st->pipe, &blit);
    214                   dstRb->defined = true; /* front buffer tracking */
    215                }
    216             }
    217          }
    218       }
    219       else {
    220          struct st_renderbuffer *srcRb =
    221             st_renderbuffer(readFB->_ColorReadBuffer);
    222          struct pipe_surface *srcSurf;
    223          GLuint i;
    224 
    225          if (!srcRb)
    226             return;
    227 
    228          st_update_renderbuffer_surface(st, srcRb);
    229 
    230          if (!srcRb->surface)
    231             return;
    232 
    233          srcSurf = srcRb->surface;
    234 
    235          for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
    236             struct st_renderbuffer *dstRb =
    237                st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
    238 
    239             if (dstRb) {
    240                struct pipe_surface *dstSurf;
    241 
    242                st_update_renderbuffer_surface(st, dstRb);
    243 
    244                dstSurf = dstRb->surface;
    245 
    246                if (dstSurf) {
    247                   blit.dst.resource = dstSurf->texture;
    248                   blit.dst.level = dstSurf->u.tex.level;
    249                   blit.dst.box.z = dstSurf->u.tex.first_layer;
    250                   blit.dst.format = dstSurf->format;
    251 
    252                   blit.src.resource = srcSurf->texture;
    253                   blit.src.level = srcSurf->u.tex.level;
    254                   blit.src.box.z = srcSurf->u.tex.first_layer;
    255                   blit.src.format = srcSurf->format;
    256 
    257                   st->pipe->blit(st->pipe, &blit);
    258                   dstRb->defined = true; /* front buffer tracking */
    259                }
    260             }
    261          }
    262       }
    263    }
    264 
    265    if (mask & depthStencil) {
    266       /* depth and/or stencil blit */
    267 
    268       /* get src/dst depth surfaces */
    269       struct st_renderbuffer *srcDepthRb =
    270          st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
    271       struct st_renderbuffer *dstDepthRb =
    272          st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
    273       struct pipe_surface *dstDepthSurf =
    274          dstDepthRb ? dstDepthRb->surface : NULL;
    275 
    276       struct st_renderbuffer *srcStencilRb =
    277          st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
    278       struct st_renderbuffer *dstStencilRb =
    279          st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
    280       struct pipe_surface *dstStencilSurf =
    281          dstStencilRb ? dstStencilRb->surface : NULL;
    282 
    283       if (_mesa_has_depthstencil_combined(readFB) &&
    284           _mesa_has_depthstencil_combined(drawFB)) {
    285          blit.mask = 0;
    286          if (mask & GL_DEPTH_BUFFER_BIT)
    287             blit.mask |= PIPE_MASK_Z;
    288          if (mask & GL_STENCIL_BUFFER_BIT)
    289             blit.mask |= PIPE_MASK_S;
    290 
    291          blit.dst.resource = dstDepthSurf->texture;
    292          blit.dst.level = dstDepthSurf->u.tex.level;
    293          blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
    294          blit.dst.format = dstDepthSurf->format;
    295 
    296          blit.src.resource = srcDepthRb->texture;
    297          blit.src.level = srcDepthRb->surface->u.tex.level;
    298          blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
    299          blit.src.format = srcDepthRb->surface->format;
    300 
    301          st->pipe->blit(st->pipe, &blit);
    302       }
    303       else {
    304          /* blitting depth and stencil separately */
    305 
    306          if (mask & GL_DEPTH_BUFFER_BIT) {
    307             blit.mask = PIPE_MASK_Z;
    308 
    309             blit.dst.resource = dstDepthSurf->texture;
    310             blit.dst.level = dstDepthSurf->u.tex.level;
    311             blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
    312             blit.dst.format = dstDepthSurf->format;
    313 
    314             blit.src.resource = srcDepthRb->texture;
    315             blit.src.level = srcDepthRb->surface->u.tex.level;
    316             blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
    317             blit.src.format = srcDepthRb->surface->format;
    318 
    319             st->pipe->blit(st->pipe, &blit);
    320          }
    321 
    322          if (mask & GL_STENCIL_BUFFER_BIT) {
    323             blit.mask = PIPE_MASK_S;
    324 
    325             blit.dst.resource = dstStencilSurf->texture;
    326             blit.dst.level = dstStencilSurf->u.tex.level;
    327             blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
    328             blit.dst.format = dstStencilSurf->format;
    329 
    330             blit.src.resource = srcStencilRb->texture;
    331             blit.src.level = srcStencilRb->surface->u.tex.level;
    332             blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
    333             blit.src.format = srcStencilRb->surface->format;
    334 
    335             st->pipe->blit(st->pipe, &blit);
    336          }
    337       }
    338    }
    339 }
    340 
    341 
    342 void
    343 st_init_blit_functions(struct dd_function_table *functions)
    344 {
    345    functions->BlitFramebuffer = st_BlitFramebuffer;
    346 }
    347