Home | History | Annotate | Download | only in intel
      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 "main/glheader.h"
     29 #include "main/enums.h"
     30 #include "main/mtypes.h"
     31 #include "main/macros.h"
     32 #include "main/fbobject.h"
     33 #include "main/image.h"
     34 #include "main/bufferobj.h"
     35 #include "main/readpix.h"
     36 #include "main/state.h"
     37 
     38 #include "intel_screen.h"
     39 #include "intel_context.h"
     40 #include "intel_blit.h"
     41 #include "intel_buffers.h"
     42 #include "intel_regions.h"
     43 #include "intel_pixel.h"
     44 #include "intel_buffer_objects.h"
     45 
     46 #define FILE_DEBUG_FLAG DEBUG_PIXEL
     47 
     48 /* For many applications, the new ability to pull the source buffers
     49  * back out of the GTT and then do the packing/conversion operations
     50  * in software will be as much of an improvement as trying to get the
     51  * blitter and/or texture engine to do the work.
     52  *
     53  * This step is gated on private backbuffers.
     54  *
     55  * Obviously the frontbuffer can't be pulled back, so that is either
     56  * an argument for blit/texture readpixels, or for blitting to a
     57  * temporary and then pulling that back.
     58  *
     59  * When the destination is a pbo, however, it's not clear if it is
     60  * ever going to be pulled to main memory (though the access param
     61  * will be a good hint).  So it sounds like we do want to be able to
     62  * choose between blit/texture implementation on the gpu and pullback
     63  * and cpu-based copying.
     64  *
     65  * Unless you can magically turn client memory into a PBO for the
     66  * duration of this call, there will be a cpu-based copying step in
     67  * any case.
     68  */
     69 
     70 static bool
     71 do_blit_readpixels(struct gl_context * ctx,
     72                    GLint x, GLint y, GLsizei width, GLsizei height,
     73                    GLenum format, GLenum type,
     74                    const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
     75 {
     76    struct intel_context *intel = intel_context(ctx);
     77    struct intel_region *src = intel_readbuf_region(intel);
     78    struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
     79    GLuint dst_offset;
     80    GLuint rowLength;
     81    drm_intel_bo *dst_buffer;
     82    bool all;
     83    GLint dst_x, dst_y;
     84    GLuint dirty;
     85 
     86    DBG("%s\n", __FUNCTION__);
     87 
     88    if (!src)
     89       return false;
     90 
     91    if (!_mesa_is_bufferobj(pack->BufferObj)) {
     92       /* PBO only for now:
     93        */
     94       DBG("%s - not PBO\n", __FUNCTION__);
     95       return false;
     96    }
     97 
     98 
     99    if (ctx->_ImageTransferState ||
    100        !intel_check_blit_format(src, format, type)) {
    101       DBG("%s - bad format for blit\n", __FUNCTION__);
    102       return false;
    103    }
    104 
    105    if (pack->Alignment != 1 || pack->SwapBytes || pack->LsbFirst) {
    106       DBG("%s: bad packing params\n", __FUNCTION__);
    107       return false;
    108    }
    109 
    110    if (pack->RowLength > 0)
    111       rowLength = pack->RowLength;
    112    else
    113       rowLength = width;
    114 
    115    if (pack->Invert) {
    116       DBG("%s: MESA_PACK_INVERT not done yet\n", __FUNCTION__);
    117       return false;
    118    }
    119    else {
    120       if (_mesa_is_winsys_fbo(ctx->ReadBuffer))
    121 	 rowLength = -rowLength;
    122    }
    123 
    124    dst_offset = (GLintptr)pixels;
    125    dst_offset += _mesa_image_offset(2, pack, width, height,
    126 				    format, type, 0, 0, 0);
    127 
    128    if (!_mesa_clip_copytexsubimage(ctx,
    129 				   &dst_x, &dst_y,
    130 				   &x, &y,
    131 				   &width, &height)) {
    132       return true;
    133    }
    134 
    135    dirty = intel->front_buffer_dirty;
    136    intel_prepare_render(intel);
    137    intel->front_buffer_dirty = dirty;
    138 
    139    all = (width * height * src->cpp == dst->Base.Size &&
    140 	  x == 0 && dst_offset == 0);
    141 
    142    dst_x = 0;
    143    dst_y = 0;
    144 
    145    dst_buffer = intel_bufferobj_buffer(intel, dst,
    146 				       all ? INTEL_WRITE_FULL :
    147 				       INTEL_WRITE_PART);
    148 
    149    if (_mesa_is_winsys_fbo(ctx->ReadBuffer))
    150       y = ctx->ReadBuffer->Height - (y + height);
    151 
    152    if (!intelEmitCopyBlit(intel,
    153 			  src->cpp,
    154 			  src->pitch, src->bo, 0, src->tiling,
    155 			  rowLength, dst_buffer, dst_offset, false,
    156 			  x, y,
    157 			  dst_x, dst_y,
    158 			  width, height,
    159 			  GL_COPY)) {
    160       return false;
    161    }
    162 
    163    DBG("%s - DONE\n", __FUNCTION__);
    164 
    165    return true;
    166 }
    167 
    168 void
    169 intelReadPixels(struct gl_context * ctx,
    170                 GLint x, GLint y, GLsizei width, GLsizei height,
    171                 GLenum format, GLenum type,
    172                 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
    173 {
    174    struct intel_context *intel = intel_context(ctx);
    175    bool dirty;
    176 
    177    intel_flush_rendering_to_batch(ctx);
    178 
    179    DBG("%s\n", __FUNCTION__);
    180 
    181    if (do_blit_readpixels
    182        (ctx, x, y, width, height, format, type, pack, pixels))
    183       return;
    184 
    185    /* glReadPixels() wont dirty the front buffer, so reset the dirty
    186     * flag after calling intel_prepare_render(). */
    187    dirty = intel->front_buffer_dirty;
    188    intel_prepare_render(intel);
    189    intel->front_buffer_dirty = dirty;
    190 
    191    fallback_debug("%s: fallback to swrast\n", __FUNCTION__);
    192 
    193    /* Update Mesa state before calling _mesa_readpixels().
    194     * XXX this may not be needed since ReadPixels no longer uses the
    195     * span code.
    196     */
    197 
    198    if (ctx->NewState)
    199       _mesa_update_state(ctx);
    200 
    201    _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
    202 
    203    /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
    204    intel->front_buffer_dirty = dirty;
    205 }
    206