intel_pixel_bitmap.c revision e67350da34c6009edff50c160df27493cb363e1b
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/mtypes.h" 33#include "main/macros.h" 34#include "main/bufferobj.h" 35#include "main/state.h" 36#include "swrast/swrast.h" 37 38#include "intel_screen.h" 39#include "intel_context.h" 40#include "intel_batchbuffer.h" 41#include "intel_blit.h" 42#include "intel_regions.h" 43#include "intel_buffer_objects.h" 44#include "intel_buffers.h" 45#include "intel_pixel.h" 46#include "intel_reg.h" 47 48 49#define FILE_DEBUG_FLAG DEBUG_PIXEL 50 51 52/* Unlike the other intel_pixel_* functions, the expectation here is 53 * that the incoming data is not in a PBO. With the XY_TEXT blit 54 * method, there's no benefit haveing it in a PBO, but we could 55 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit 56 * PBO bitmaps. I think they are probably pretty rare though - I 57 * wonder if Xgl uses them? 58 */ 59static const GLubyte *map_pbo( GLcontext *ctx, 60 GLsizei width, GLsizei height, 61 const struct gl_pixelstore_attrib *unpack, 62 const GLubyte *bitmap ) 63{ 64 GLubyte *buf; 65 66 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1, 67 GL_COLOR_INDEX, GL_BITMAP, 68 (GLvoid *) bitmap)) { 69 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)"); 70 return NULL; 71 } 72 73 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 74 GL_READ_ONLY_ARB, 75 unpack->BufferObj); 76 if (!buf) { 77 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)"); 78 return NULL; 79 } 80 81 return ADD_POINTERS(buf, bitmap); 82} 83 84static GLboolean test_bit( const GLubyte *src, 85 GLuint bit ) 86{ 87 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0; 88} 89 90static void set_bit( GLubyte *dest, 91 GLuint bit ) 92{ 93 dest[bit/8] |= 1 << (bit % 8); 94} 95 96/* Extract a rectangle's worth of data from the bitmap. Called 97 * per-cliprect. 98 */ 99static GLuint get_bitmap_rect(GLsizei width, GLsizei height, 100 const struct gl_pixelstore_attrib *unpack, 101 const GLubyte *bitmap, 102 GLuint x, GLuint y, 103 GLuint w, GLuint h, 104 GLubyte *dest, 105 GLuint row_align, 106 GLboolean invert) 107{ 108 GLuint src_offset = (x + unpack->SkipPixels) & 0x7; 109 GLuint mask = unpack->LsbFirst ? 0 : 7; 110 GLuint bit = 0; 111 GLint row, col; 112 GLint first, last; 113 GLint incr; 114 GLuint count = 0; 115 116 if (INTEL_DEBUG & DEBUG_PIXEL) 117 _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n", 118 __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask); 119 120 if (invert) { 121 first = h-1; 122 last = 0; 123 incr = -1; 124 } 125 else { 126 first = 0; 127 last = h-1; 128 incr = 1; 129 } 130 131 /* Require that dest be pre-zero'd. 132 */ 133 for (row = first; row != (last+incr); row += incr) { 134 const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap, 135 width, height, 136 GL_COLOR_INDEX, GL_BITMAP, 137 y + row, x); 138 139 for (col = 0; col < w; col++, bit++) { 140 if (test_bit(rowsrc, (col + src_offset) ^ mask)) { 141 set_bit(dest, bit ^ 7); 142 count++; 143 } 144 } 145 146 if (row_align) 147 bit = ALIGN(bit, row_align); 148 } 149 150 return count; 151} 152 153 154 155 156/* 157 * Render a bitmap. 158 */ 159static GLboolean 160do_blit_bitmap( GLcontext *ctx, 161 GLint dstx, GLint dsty, 162 GLsizei width, GLsizei height, 163 const struct gl_pixelstore_attrib *unpack, 164 const GLubyte *bitmap ) 165{ 166 struct intel_context *intel = intel_context(ctx); 167 struct intel_region *dst = intel_drawbuf_region(intel); 168 struct gl_framebuffer *fb = ctx->DrawBuffer; 169 GLfloat tmpColor[4]; 170 GLubyte ubcolor[4]; 171 GLuint color8888, color565; 172 unsigned int num_cliprects; 173 drm_clip_rect_t *cliprects; 174 int x_off, y_off; 175 GLsizei bitmap_width = width; 176 GLsizei bitmap_height = height; 177 178 /* Update draw buffer bounds */ 179 _mesa_update_state(ctx); 180 181 if (!dst) 182 return GL_FALSE; 183 184 if (unpack->BufferObj->Name) { 185 bitmap = map_pbo(ctx, width, height, unpack, bitmap); 186 if (bitmap == NULL) 187 return GL_TRUE; /* even though this is an error, we're done */ 188 } 189 190 COPY_4V(tmpColor, ctx->Current.RasterColor); 191 192 if (NEED_SECONDARY_COLOR(ctx)) { 193 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor); 194 } 195 196 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]); 197 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]); 198 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]); 199 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]); 200 201 color8888 = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1], ubcolor[2], ubcolor[3]); 202 color565 = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]); 203 204 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F)) 205 return GL_FALSE; 206 207 LOCK_HARDWARE(intel); 208 209 intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off); 210 if (num_cliprects != 0) { 211 drm_clip_rect_t dest_rect; 212 GLint srcx = 0, srcy = 0; 213 GLuint i; 214 GLint orig_dstx = dstx; 215 GLint orig_dsty = dsty; 216 217 /* Clip to buffer bounds and scissor. */ 218 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, 219 fb->_Xmax, fb->_Ymax, 220 &dstx, &dsty, &width, &height)) 221 goto out; 222 223 /* Convert from GL to hardware coordinates. Transform original points 224 * along with it so that we can look at cliprects in hw coordinates and 225 * map back to points in the source space. 226 */ 227 if (fb->Name == 0) { 228 /* bitmap to a system framebuffer */ 229 dstx = x_off + dstx; 230 dsty = y_off + (fb->Height - dsty - height); 231 orig_dstx = x_off + orig_dstx; 232 orig_dsty = y_off + (fb->Height - orig_dsty - height); 233 } else { 234 /* bitmap to a user framebuffer object */ 235 dstx = x_off + dstx; 236 dsty = y_off + dsty; 237 orig_dstx = x_off + orig_dstx; 238 orig_dsty = y_off + orig_dsty; 239 } 240 241 dest_rect.x1 = dstx; 242 dest_rect.y1 = dsty; 243 dest_rect.x2 = dstx + width; 244 dest_rect.y2 = dsty + height; 245 246 for (i = 0; i < num_cliprects; i++) { 247 drm_clip_rect_t rect; 248 int box_w, box_h; 249 GLint px, py; 250 GLuint stipple[32]; 251 252 if (!intel_intersect_cliprects(&rect, &dest_rect, &cliprects[i])) 253 continue; 254 255 /* Now go back to GL coordinates to figure out what subset of 256 * the bitmap we are uploading for this cliprect: 257 */ 258 box_w = rect.x2 - rect.x1; 259 box_h = rect.y2 - rect.y1; 260 srcx = rect.x1 - orig_dstx; 261 srcy = rect.y1 - orig_dsty; 262 263#define DY 32 264#define DX 32 265 266 /* Then, finally, chop it all into chunks that can be 267 * digested by hardware: 268 */ 269 for (py = 0; py < box_h; py += DY) { 270 for (px = 0; px < box_w; px += DX) { 271 int h = MIN2(DY, box_h - py); 272 int w = MIN2(DX, box_w - px); 273 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8; 274 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ? 275 ctx->Color.LogicOp : GL_COPY; 276 277 assert(sz <= sizeof(stipple)); 278 memset(stipple, 0, sz); 279 280 /* May need to adjust this when padding has been introduced in 281 * sz above: 282 */ 283 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack, 284 bitmap, 285 srcx + px, srcy + py, w, h, 286 (GLubyte *)stipple, 287 8, 288 fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0) 289 continue; 290 291 /* 292 */ 293 intelEmitImmediateColorExpandBlit( intel, 294 dst->cpp, 295 (GLubyte *)stipple, 296 sz, 297 (dst->cpp == 2) ? color565 : color8888, 298 dst->pitch, 299 dst->buffer, 300 0, 301 dst->tiling, 302 rect.x1 + px, 303 rect.y2 - (py + h), 304 w, h, 305 logic_op); 306 } 307 } 308 } 309 } 310out: 311 UNLOCK_HARDWARE(intel); 312 313 if (INTEL_DEBUG & DEBUG_SYNC) 314 intel_batchbuffer_flush(intel->batch); 315 316 if (unpack->BufferObj->Name) { 317 /* done with PBO so unmap it now */ 318 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 319 unpack->BufferObj); 320 } 321 322 return GL_TRUE; 323} 324 325 326 327 328 329/* There are a large number of possible ways to implement bitmap on 330 * this hardware, most of them have some sort of drawback. Here are a 331 * few that spring to mind: 332 * 333 * Blit: 334 * - XY_MONO_SRC_BLT_CMD 335 * - use XY_SETUP_CLIP_BLT for cliprect clipping. 336 * - XY_TEXT_BLT 337 * - XY_TEXT_IMMEDIATE_BLT 338 * - blit per cliprect, subject to maximum immediate data size. 339 * - XY_COLOR_BLT 340 * - per pixel or run of pixels 341 * - XY_PIXEL_BLT 342 * - good for sparse bitmaps 343 * 344 * 3D engine: 345 * - Point per pixel 346 * - Translate bitmap to an alpha texture and render as a quad 347 * - Chop bitmap up into 32x32 squares and render w/polygon stipple. 348 */ 349void 350intelBitmap(GLcontext * ctx, 351 GLint x, GLint y, 352 GLsizei width, GLsizei height, 353 const struct gl_pixelstore_attrib *unpack, 354 const GLubyte * pixels) 355{ 356 if (do_blit_bitmap(ctx, x, y, width, height, 357 unpack, pixels)) 358 return; 359 360 if (INTEL_DEBUG & DEBUG_PIXEL) 361 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__); 362 363 _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels); 364} 365