Home | History | Annotate | Download | only in i965
      1 /**************************************************************************
      2  *
      3  * Copyright 2006 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 portionsalloc
     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/image.h"
     31 #include "main/colormac.h"
     32 #include "main/condrender.h"
     33 #include "main/mtypes.h"
     34 #include "main/macros.h"
     35 #include "main/pbo.h"
     36 #include "main/bufferobj.h"
     37 #include "main/state.h"
     38 #include "main/texobj.h"
     39 #include "main/context.h"
     40 #include "main/fbobject.h"
     41 #include "swrast/swrast.h"
     42 #include "drivers/common/meta.h"
     43 
     44 #include "intel_screen.h"
     45 #include "intel_context.h"
     46 #include "intel_batchbuffer.h"
     47 #include "intel_blit.h"
     48 #include "intel_regions.h"
     49 #include "intel_buffers.h"
     50 #include "intel_pixel.h"
     51 #include "intel_reg.h"
     52 
     53 
     54 #define FILE_DEBUG_FLAG DEBUG_PIXEL
     55 
     56 
     57 /* Unlike the other intel_pixel_* functions, the expectation here is
     58  * that the incoming data is not in a PBO.  With the XY_TEXT blit
     59  * method, there's no benefit haveing it in a PBO, but we could
     60  * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
     61  * PBO bitmaps.  I think they are probably pretty rare though - I
     62  * wonder if Xgl uses them?
     63  */
     64 static const GLubyte *map_pbo( struct gl_context *ctx,
     65 			       GLsizei width, GLsizei height,
     66 			       const struct gl_pixelstore_attrib *unpack,
     67 			       const GLubyte *bitmap )
     68 {
     69    GLubyte *buf;
     70 
     71    if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
     72 				  GL_COLOR_INDEX, GL_BITMAP,
     73 				  INT_MAX, (const GLvoid *) bitmap)) {
     74       _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
     75       return NULL;
     76    }
     77 
     78    buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0, unpack->BufferObj->Size,
     79 						GL_MAP_READ_BIT,
     80 						unpack->BufferObj);
     81    if (!buf) {
     82       _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
     83       return NULL;
     84    }
     85 
     86    return ADD_POINTERS(buf, bitmap);
     87 }
     88 
     89 static bool test_bit( const GLubyte *src, GLuint bit )
     90 {
     91    return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
     92 }
     93 
     94 static void set_bit( GLubyte *dest, GLuint bit )
     95 {
     96    dest[bit/8] |= 1 << (bit % 8);
     97 }
     98 
     99 /* Extract a rectangle's worth of data from the bitmap.  Called
    100  * per chunk of HW-sized bitmap.
    101  */
    102 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
    103 			      const struct gl_pixelstore_attrib *unpack,
    104 			      const GLubyte *bitmap,
    105 			      GLuint x, GLuint y,
    106 			      GLuint w, GLuint h,
    107 			      GLubyte *dest,
    108 			      GLuint row_align,
    109 			      bool invert)
    110 {
    111    GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
    112    GLuint mask = unpack->LsbFirst ? 0 : 7;
    113    GLuint bit = 0;
    114    GLint row, col;
    115    GLint first, last;
    116    GLint incr;
    117    GLuint count = 0;
    118 
    119    DBG("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
    120        __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
    121 
    122    if (invert) {
    123       first = h-1;
    124       last = 0;
    125       incr = -1;
    126    }
    127    else {
    128       first = 0;
    129       last = h-1;
    130       incr = 1;
    131    }
    132 
    133    /* Require that dest be pre-zero'd.
    134     */
    135    for (row = first; row != (last+incr); row += incr) {
    136       const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
    137 						    width, height,
    138 						    GL_COLOR_INDEX, GL_BITMAP,
    139 						    y + row, x);
    140 
    141       for (col = 0; col < w; col++, bit++) {
    142 	 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
    143 	    set_bit(dest, bit ^ 7);
    144 	    count++;
    145 	 }
    146       }
    147 
    148       if (row_align)
    149 	 bit = ALIGN(bit, row_align);
    150    }
    151 
    152    return count;
    153 }
    154 
    155 /**
    156  * Returns the low Y value of the vertical range given, flipped according to
    157  * whether the framebuffer is or not.
    158  */
    159 static INLINE int
    160 y_flip(struct gl_framebuffer *fb, int y, int height)
    161 {
    162    if (_mesa_is_user_fbo(fb))
    163       return y;
    164    else
    165       return fb->Height - y - height;
    166 }
    167 
    168 /*
    169  * Render a bitmap.
    170  */
    171 static bool
    172 do_blit_bitmap( struct gl_context *ctx,
    173 		GLint dstx, GLint dsty,
    174 		GLsizei width, GLsizei height,
    175 		const struct gl_pixelstore_attrib *unpack,
    176 		const GLubyte *bitmap )
    177 {
    178    struct intel_context *intel = intel_context(ctx);
    179    struct intel_region *dst;
    180    struct gl_framebuffer *fb = ctx->DrawBuffer;
    181    GLfloat tmpColor[4];
    182    GLubyte ubcolor[4];
    183    GLuint color;
    184    GLsizei bitmap_width = width;
    185    GLsizei bitmap_height = height;
    186    GLint px, py;
    187    GLuint stipple[32];
    188    GLint orig_dstx = dstx;
    189    GLint orig_dsty = dsty;
    190 
    191    /* Update draw buffer bounds */
    192    _mesa_update_state(ctx);
    193 
    194    if (ctx->Depth.Test) {
    195       /* The blit path produces incorrect results when depth testing is on.
    196        * It seems the blit Z coord is always 1.0 (the far plane) so fragments
    197        * will likely be obscured by other, closer geometry.
    198        */
    199       return false;
    200    }
    201 
    202    intel_prepare_render(intel);
    203    dst = intel_drawbuf_region(intel);
    204 
    205    if (!dst)
    206        return false;
    207 
    208    if (_mesa_is_bufferobj(unpack->BufferObj)) {
    209       bitmap = map_pbo(ctx, width, height, unpack, bitmap);
    210       if (bitmap == NULL)
    211 	 return true;	/* even though this is an error, we're done */
    212    }
    213 
    214    COPY_4V(tmpColor, ctx->Current.RasterColor);
    215 
    216    if (_mesa_need_secondary_color(ctx)) {
    217        ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
    218    }
    219 
    220    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
    221    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
    222    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
    223    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
    224 
    225    if (dst->cpp == 2)
    226       color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
    227    else
    228       color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
    229 
    230    if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
    231       return false;
    232 
    233    /* Clip to buffer bounds and scissor. */
    234    if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
    235 			     fb->_Xmax, fb->_Ymax,
    236 			     &dstx, &dsty, &width, &height))
    237       goto out;
    238 
    239    dsty = y_flip(fb, dsty, height);
    240 
    241 #define DY 32
    242 #define DX 32
    243 
    244    /* Chop it all into chunks that can be digested by hardware: */
    245    for (py = 0; py < height; py += DY) {
    246       for (px = 0; px < width; px += DX) {
    247 	 int h = MIN2(DY, height - py);
    248 	 int w = MIN2(DX, width - px);
    249 	 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
    250 	 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
    251 	    ctx->Color.LogicOp : GL_COPY;
    252 
    253 	 assert(sz <= sizeof(stipple));
    254 	 memset(stipple, 0, sz);
    255 
    256 	 /* May need to adjust this when padding has been introduced in
    257 	  * sz above:
    258 	  *
    259 	  * Have to translate destination coordinates back into source
    260 	  * coordinates.
    261 	  */
    262 	 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
    263 			     bitmap,
    264 			     -orig_dstx + (dstx + px),
    265 			     -orig_dsty + y_flip(fb, dsty + py, h),
    266 			     w, h,
    267 			     (GLubyte *)stipple,
    268 			     8,
    269 			     _mesa_is_winsys_fbo(fb)) == 0)
    270 	    continue;
    271 
    272 	 if (!intelEmitImmediateColorExpandBlit(intel,
    273 						dst->cpp,
    274 						(GLubyte *)stipple,
    275 						sz,
    276 						color,
    277 						dst->pitch,
    278 						dst->bo,
    279 						0,
    280 						dst->tiling,
    281 						dstx + px,
    282 						dsty + py,
    283 						w, h,
    284 						logic_op)) {
    285 	    return false;
    286 	 }
    287       }
    288    }
    289 out:
    290 
    291    if (unlikely(INTEL_DEBUG & DEBUG_SYNC))
    292       intel_batchbuffer_flush(intel);
    293 
    294    if (_mesa_is_bufferobj(unpack->BufferObj)) {
    295       /* done with PBO so unmap it now */
    296       ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
    297    }
    298 
    299    intel_check_front_buffer_rendering(intel);
    300 
    301    return true;
    302 }
    303 
    304 
    305 /* There are a large number of possible ways to implement bitmap on
    306  * this hardware, most of them have some sort of drawback.  Here are a
    307  * few that spring to mind:
    308  *
    309  * Blit:
    310  *    - XY_MONO_SRC_BLT_CMD
    311  *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
    312  *    - XY_TEXT_BLT
    313  *    - XY_TEXT_IMMEDIATE_BLT
    314  *         - blit per cliprect, subject to maximum immediate data size.
    315  *    - XY_COLOR_BLT
    316  *         - per pixel or run of pixels
    317  *    - XY_PIXEL_BLT
    318  *         - good for sparse bitmaps
    319  *
    320  * 3D engine:
    321  *    - Point per pixel
    322  *    - Translate bitmap to an alpha texture and render as a quad
    323  *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
    324  */
    325 void
    326 intelBitmap(struct gl_context * ctx,
    327 	    GLint x, GLint y,
    328 	    GLsizei width, GLsizei height,
    329 	    const struct gl_pixelstore_attrib *unpack,
    330 	    const GLubyte * pixels)
    331 {
    332    if (!_mesa_check_conditional_render(ctx))
    333       return;
    334 
    335    if (do_blit_bitmap(ctx, x, y, width, height,
    336                           unpack, pixels))
    337       return;
    338 
    339    _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
    340 }
    341