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