intel_pixel_bitmap.c revision 9b22427911ad27efc1f36faee9462c6082d0417c
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/polygon.h" 36#include "main/pixelstore.h" 37#include "main/polygon.h" 38#include "main/state.h" 39#include "main/teximage.h" 40#include "main/texobj.h" 41#include "main/texstate.h" 42#include "main/texparam.h" 43#include "main/varray.h" 44#include "main/attrib.h" 45#include "main/enable.h" 46#include "main/viewport.h" 47#include "shader/arbprogram.h" 48#include "swrast/swrast.h" 49 50#include "intel_screen.h" 51#include "intel_context.h" 52#include "intel_batchbuffer.h" 53#include "intel_blit.h" 54#include "intel_regions.h" 55#include "intel_buffers.h" 56#include "intel_pixel.h" 57#include "intel_reg.h" 58 59 60#define FILE_DEBUG_FLAG DEBUG_PIXEL 61 62 63/* Unlike the other intel_pixel_* functions, the expectation here is 64 * that the incoming data is not in a PBO. With the XY_TEXT blit 65 * method, there's no benefit haveing it in a PBO, but we could 66 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit 67 * PBO bitmaps. I think they are probably pretty rare though - I 68 * wonder if Xgl uses them? 69 */ 70static const GLubyte *map_pbo( GLcontext *ctx, 71 GLsizei width, GLsizei height, 72 const struct gl_pixelstore_attrib *unpack, 73 const GLubyte *bitmap ) 74{ 75 GLubyte *buf; 76 77 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1, 78 GL_COLOR_INDEX, GL_BITMAP, 79 (GLvoid *) bitmap)) { 80 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)"); 81 return NULL; 82 } 83 84 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 85 GL_READ_ONLY_ARB, 86 unpack->BufferObj); 87 if (!buf) { 88 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)"); 89 return NULL; 90 } 91 92 return ADD_POINTERS(buf, bitmap); 93} 94 95static GLboolean test_bit( const GLubyte *src, GLuint bit ) 96{ 97 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0; 98} 99 100static void set_bit( GLubyte *dest, GLuint bit ) 101{ 102 dest[bit/8] |= 1 << (bit % 8); 103} 104 105/* Extract a rectangle's worth of data from the bitmap. Called 106 * per-cliprect. 107 */ 108static GLuint get_bitmap_rect(GLsizei width, GLsizei height, 109 const struct gl_pixelstore_attrib *unpack, 110 const GLubyte *bitmap, 111 GLuint x, GLuint y, 112 GLuint w, GLuint h, 113 GLubyte *dest, 114 GLuint row_align, 115 GLboolean invert) 116{ 117 GLuint src_offset = (x + unpack->SkipPixels) & 0x7; 118 GLuint mask = unpack->LsbFirst ? 0 : 7; 119 GLuint bit = 0; 120 GLint row, col; 121 GLint first, last; 122 GLint incr; 123 GLuint count = 0; 124 125 if (INTEL_DEBUG & DEBUG_PIXEL) 126 _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n", 127 __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask); 128 129 if (invert) { 130 first = h-1; 131 last = 0; 132 incr = -1; 133 } 134 else { 135 first = 0; 136 last = h-1; 137 incr = 1; 138 } 139 140 /* Require that dest be pre-zero'd. 141 */ 142 for (row = first; row != (last+incr); row += incr) { 143 const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap, 144 width, height, 145 GL_COLOR_INDEX, GL_BITMAP, 146 y + row, x); 147 148 for (col = 0; col < w; col++, bit++) { 149 if (test_bit(rowsrc, (col + src_offset) ^ mask)) { 150 set_bit(dest, bit ^ 7); 151 count++; 152 } 153 } 154 155 if (row_align) 156 bit = ALIGN(bit, row_align); 157 } 158 159 return count; 160} 161 162/** 163 * Returns the low Y value of the vertical range given, flipped according to 164 * whether the framebuffer is or not. 165 */ 166static INLINE int 167y_flip(struct gl_framebuffer *fb, int y, int height) 168{ 169 if (fb->Name != 0) 170 return y; 171 else 172 return fb->Height - y - height; 173} 174 175/* 176 * Render a bitmap. 177 */ 178static GLboolean 179do_blit_bitmap( GLcontext *ctx, 180 GLint dstx, GLint dsty, 181 GLsizei width, GLsizei height, 182 const struct gl_pixelstore_attrib *unpack, 183 const GLubyte *bitmap ) 184{ 185 struct intel_context *intel = intel_context(ctx); 186 struct intel_region *dst = intel_drawbuf_region(intel); 187 struct gl_framebuffer *fb = ctx->DrawBuffer; 188 GLfloat tmpColor[4]; 189 GLubyte ubcolor[4]; 190 GLuint color; 191 unsigned int num_cliprects; 192 drm_clip_rect_t *cliprects; 193 int x_off, y_off; 194 GLsizei bitmap_width = width; 195 GLsizei bitmap_height = height; 196 197 /* Update draw buffer bounds */ 198 _mesa_update_state(ctx); 199 200 if (ctx->Depth.Test) { 201 /* The blit path produces incorrect results when depth testing is on. 202 * It seems the blit Z coord is always 1.0 (the far plane) so fragments 203 * will likely be obscured by other, closer geometry. 204 */ 205 return GL_FALSE; 206 } 207 208 if (!dst) 209 return GL_FALSE; 210 211 if (_mesa_is_bufferobj(unpack->BufferObj)) { 212 bitmap = map_pbo(ctx, width, height, unpack, bitmap); 213 if (bitmap == NULL) 214 return GL_TRUE; /* even though this is an error, we're done */ 215 } 216 217 COPY_4V(tmpColor, ctx->Current.RasterColor); 218 219 if (NEED_SECONDARY_COLOR(ctx)) { 220 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor); 221 } 222 223 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]); 224 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]); 225 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]); 226 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]); 227 228 if (dst->cpp == 2) 229 color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]); 230 else 231 color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]); 232 233 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F)) 234 return GL_FALSE; 235 236 intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off); 237 if (num_cliprects != 0) { 238 GLuint i; 239 GLint orig_dstx = dstx; 240 GLint orig_dsty = dsty; 241 242 /* Clip to buffer bounds and scissor. */ 243 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, 244 fb->_Xmax, fb->_Ymax, 245 &dstx, &dsty, &width, &height)) 246 goto out; 247 248 dstx = x_off + dstx; 249 dsty = y_off + y_flip(fb, dsty, height); 250 251 for (i = 0; i < num_cliprects; i++) { 252 int box_x, box_y, box_w, box_h; 253 GLint px, py; 254 GLuint stipple[32]; 255 256 box_x = dstx; 257 box_y = dsty; 258 box_w = width; 259 box_h = height; 260 261 /* Clip to drawable cliprect */ 262 if (!_mesa_clip_to_region(cliprects[i].x1, 263 cliprects[i].y1, 264 cliprects[i].x2, 265 cliprects[i].y2, 266 &box_x, &box_y, &box_w, &box_h)) 267 continue; 268 269#define DY 32 270#define DX 32 271 272 /* Then, finally, chop it all into chunks that can be 273 * digested by hardware: 274 */ 275 for (py = 0; py < box_h; py += DY) { 276 for (px = 0; px < box_w; px += DX) { 277 int h = MIN2(DY, box_h - py); 278 int w = MIN2(DX, box_w - px); 279 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8; 280 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ? 281 ctx->Color.LogicOp : GL_COPY; 282 283 assert(sz <= sizeof(stipple)); 284 memset(stipple, 0, sz); 285 286 /* May need to adjust this when padding has been introduced in 287 * sz above: 288 * 289 * Have to translate destination coordinates back into source 290 * coordinates. 291 */ 292 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack, 293 bitmap, 294 -orig_dstx + (box_x + px - x_off), 295 -orig_dsty + y_flip(fb, 296 box_y + py - y_off, h), 297 w, h, 298 (GLubyte *)stipple, 299 8, 300 fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0) 301 continue; 302 303 if (!intelEmitImmediateColorExpandBlit(intel, 304 dst->cpp, 305 (GLubyte *)stipple, 306 sz, 307 color, 308 dst->pitch, 309 dst->buffer, 310 0, 311 dst->tiling, 312 box_x + px, 313 box_y + py, 314 w, h, 315 logic_op)) { 316 return GL_FALSE; 317 } 318 } 319 } 320 } 321 } 322out: 323 324 if (INTEL_DEBUG & DEBUG_SYNC) 325 intel_batchbuffer_flush(intel->batch); 326 327 if (_mesa_is_bufferobj(unpack->BufferObj)) { 328 /* done with PBO so unmap it now */ 329 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 330 unpack->BufferObj); 331 } 332 333 intel_check_front_buffer_rendering(intel); 334 335 return GL_TRUE; 336} 337 338static GLboolean 339intel_texture_bitmap(GLcontext * ctx, 340 GLint dst_x, GLint dst_y, 341 GLsizei width, GLsizei height, 342 const struct gl_pixelstore_attrib *unpack, 343 const GLubyte *bitmap) 344{ 345 struct intel_context *intel = intel_context(ctx); 346 static const char *fp = 347 "!!ARBfp1.0\n" 348 "TEMP val;\n" 349 "PARAM color=program.local[0];\n" 350 "TEX val, fragment.texcoord[0], texture[0], 2D;\n" 351 "ADD val, val.wwww, {-.5, -.5, -.5, -.5};\n" 352 "KIL val;\n" 353 "MOV result.color, color;\n" 354 "END\n"; 355 GLuint texname; 356 GLfloat vertices[4][4]; 357 GLint old_active_texture; 358 GLubyte *a8_bitmap; 359 GLfloat dst_z; 360 361 /* We need a fragment program for the KIL effect */ 362 if (!ctx->Extensions.ARB_fragment_program || 363 !ctx->Extensions.ARB_vertex_program) { 364 if (INTEL_DEBUG & DEBUG_FALLBACKS) 365 fprintf(stderr, 366 "glBitmap fallback: No fragment/vertex program support\n"); 367 return GL_FALSE; 368 } 369 370 /* We're going to mess with texturing with no regard to existing texture 371 * state, so if there is some set up we have to bail. 372 */ 373 if (ctx->Texture._EnabledUnits != 0) { 374 if (INTEL_DEBUG & DEBUG_FALLBACKS) 375 fprintf(stderr, "glBitmap fallback: texturing enabled\n"); 376 return GL_FALSE; 377 } 378 379 /* Can't do textured DrawPixels with a fragment program, unless we were 380 * to generate a new program that sampled our texture and put the results 381 * in the fragment color before the user's program started. 382 */ 383 if (ctx->FragmentProgram.Enabled) { 384 if (INTEL_DEBUG & DEBUG_FALLBACKS) 385 fprintf(stderr, "glBitmap fallback: fragment program enabled\n"); 386 return GL_FALSE; 387 } 388 389 if (ctx->VertexProgram.Enabled) { 390 if (INTEL_DEBUG & DEBUG_FALLBACKS) 391 fprintf(stderr, "glBitmap fallback: vertex program enabled\n"); 392 return GL_FALSE; 393 } 394 395 if (!ctx->Extensions.ARB_texture_non_power_of_two && 396 (!is_power_of_two(width) || !is_power_of_two(height))) { 397 if (INTEL_DEBUG & DEBUG_FALLBACKS) 398 fprintf(stderr, 399 "glBitmap() fallback: NPOT texture\n"); 400 return GL_FALSE; 401 } 402 403 if (ctx->Fog.Enabled) { 404 if (INTEL_DEBUG & DEBUG_FALLBACKS) 405 fprintf(stderr, "glBitmap() fallback: fog\n"); 406 return GL_FALSE; 407 } 408 409 /* Check that we can load in a texture this big. */ 410 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) || 411 height > (1 << (ctx->Const.MaxTextureLevels - 1))) { 412 if (INTEL_DEBUG & DEBUG_FALLBACKS) 413 fprintf(stderr, "glBitmap fallback: bitmap too large (%dx%d)\n", 414 width, height); 415 return GL_FALSE; 416 } 417 418 if (_mesa_is_bufferobj(unpack->BufferObj)) { 419 bitmap = map_pbo(ctx, width, height, unpack, bitmap); 420 if (bitmap == NULL) 421 return GL_TRUE; /* even though this is an error, we're done */ 422 } 423 424 /* Convert the A1 bitmap to an A8 format suitable for glTexImage */ 425 a8_bitmap = _mesa_calloc(width * height); 426 _mesa_expand_bitmap(width, height, unpack, bitmap, a8_bitmap, width, 0xff); 427 428 if (_mesa_is_bufferobj(unpack->BufferObj)) { 429 /* done with PBO so unmap it now */ 430 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 431 unpack->BufferObj); 432 } 433 434 /* Save GL state before we start setting up our drawing */ 435 _mesa_PushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POLYGON_BIT | 436 GL_TEXTURE_BIT | GL_VIEWPORT_BIT); 437 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT | 438 GL_CLIENT_PIXEL_STORE_BIT); 439 old_active_texture = ctx->Texture.CurrentUnit; 440 441 _mesa_Disable(GL_POLYGON_STIPPLE); 442 _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL); 443 444 /* Upload our bitmap data to an alpha texture */ 445 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB); 446 _mesa_Enable(GL_TEXTURE_2D); 447 _mesa_GenTextures(1, &texname); 448 _mesa_BindTexture(GL_TEXTURE_2D, texname); 449 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 450 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 451 452 _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE); 453 _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); 454 _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0); 455 _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0); 456 _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0); 457 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 1); 458 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, 459 GL_ALPHA, GL_UNSIGNED_BYTE, a8_bitmap); 460 _mesa_free(a8_bitmap); 461 462 meta_set_fragment_program(&intel->meta, &intel->meta.bitmap_fp, fp); 463 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, 464 ctx->Current.RasterColor); 465 meta_set_passthrough_vertex_program(&intel->meta); 466 meta_set_passthrough_transform(&intel->meta); 467 468 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */ 469 dst_z = -1.0 + 2.0 * ctx->Current.RasterPos[2]; 470 471 /* RasterPos[2] already takes into account the DepthRange mapping. */ 472 _mesa_DepthRange(0.0, 1.0); 473 474 vertices[0][0] = dst_x; 475 vertices[0][1] = dst_y; 476 vertices[0][2] = dst_z; 477 vertices[0][3] = 1.0; 478 vertices[1][0] = dst_x + width; 479 vertices[1][1] = dst_y; 480 vertices[1][2] = dst_z; 481 vertices[1][3] = 1.0; 482 vertices[2][0] = dst_x + width; 483 vertices[2][1] = dst_y + height; 484 vertices[2][2] = dst_z; 485 vertices[2][3] = 1.0; 486 vertices[3][0] = dst_x; 487 vertices[3][1] = dst_y + height; 488 vertices[3][2] = dst_z; 489 vertices[3][3] = 1.0; 490 491 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices); 492 _mesa_Enable(GL_VERTEX_ARRAY); 493 meta_set_default_texrect(&intel->meta); 494 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); 495 496 meta_restore_texcoords(&intel->meta); 497 meta_restore_transform(&intel->meta); 498 meta_restore_fragment_program(&intel->meta); 499 meta_restore_vertex_program(&intel->meta); 500 501 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture); 502 _mesa_PopClientAttrib(); 503 _mesa_PopAttrib(); 504 505 _mesa_DeleteTextures(1, &texname); 506 507 return GL_TRUE; 508} 509 510/* There are a large number of possible ways to implement bitmap on 511 * this hardware, most of them have some sort of drawback. Here are a 512 * few that spring to mind: 513 * 514 * Blit: 515 * - XY_MONO_SRC_BLT_CMD 516 * - use XY_SETUP_CLIP_BLT for cliprect clipping. 517 * - XY_TEXT_BLT 518 * - XY_TEXT_IMMEDIATE_BLT 519 * - blit per cliprect, subject to maximum immediate data size. 520 * - XY_COLOR_BLT 521 * - per pixel or run of pixels 522 * - XY_PIXEL_BLT 523 * - good for sparse bitmaps 524 * 525 * 3D engine: 526 * - Point per pixel 527 * - Translate bitmap to an alpha texture and render as a quad 528 * - Chop bitmap up into 32x32 squares and render w/polygon stipple. 529 */ 530void 531intelBitmap(GLcontext * ctx, 532 GLint x, GLint y, 533 GLsizei width, GLsizei height, 534 const struct gl_pixelstore_attrib *unpack, 535 const GLubyte * pixels) 536{ 537 if (do_blit_bitmap(ctx, x, y, width, height, 538 unpack, pixels)) 539 return; 540 541 if (intel_texture_bitmap(ctx, x, y, width, height, 542 unpack, pixels)) 543 return; 544 545 if (INTEL_DEBUG & DEBUG_PIXEL) 546 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__); 547 548 _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels); 549} 550