intel_blit.c revision a74bf4ef345d880d7d296313fed0240781d2ebd8
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 29#include <stdio.h> 30#include <errno.h> 31 32#include "mtypes.h" 33#include "context.h" 34#include "enums.h" 35 36#include "intel_batchbuffer.h" 37#include "intel_blit.h" 38#include "intel_buffers.h" 39#include "intel_context.h" 40#include "intel_fbo.h" 41#include "intel_reg.h" 42#include "intel_regions.h" 43 44#define FILE_DEBUG_FLAG DEBUG_BLIT 45 46/** 47 * Copy the back color buffer to the front color buffer. 48 * Used for SwapBuffers(). 49 */ 50void 51intelCopyBuffer(const __DRIdrawablePrivate * dPriv, 52 const drm_clip_rect_t * rect) 53{ 54 55 struct intel_context *intel; 56 const intelScreenPrivate *intelScreen; 57 int ret; 58 59 DBG("%s\n", __FUNCTION__); 60 61 assert(dPriv); 62 63 intel = intelScreenContext(dPriv->driScreenPriv->private); 64 if (!intel) 65 return; 66 67 intelScreen = intel->intelScreen; 68 69 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets 70 * should work regardless. 71 */ 72 LOCK_HARDWARE(intel); 73 74 if (dPriv && dPriv->numClipRects) { 75 struct intel_framebuffer *intel_fb = dPriv->driverPrivate; 76 struct intel_region *src, *dst; 77 int nbox = dPriv->numClipRects; 78 drm_clip_rect_t *pbox = dPriv->pClipRects; 79 int cpp; 80 int src_pitch, dst_pitch; 81 unsigned short src_x, src_y; 82 int BR13, CMD; 83 int i; 84 85 src = intel_get_rb_region(&intel_fb->Base, BUFFER_BACK_LEFT); 86 dst = intel_get_rb_region(&intel_fb->Base, BUFFER_FRONT_LEFT); 87 88 src_pitch = src->pitch * src->cpp; 89 dst_pitch = dst->pitch * dst->cpp; 90 91 cpp = src->cpp; 92 93 ASSERT(intel_fb); 94 ASSERT(intel_fb->Base.Name == 0); /* Not a user-created FBO */ 95 ASSERT(src); 96 ASSERT(dst); 97 ASSERT(src->cpp == dst->cpp); 98 99 if (cpp == 2) { 100 BR13 = (0xCC << 16) | (1 << 24); 101 CMD = XY_SRC_COPY_BLT_CMD; 102 } 103 else { 104 BR13 = (0xCC << 16) | (1 << 24) | (1 << 25); 105 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB; 106 } 107 108#ifndef I915 109 if (src->tiled) { 110 CMD |= XY_SRC_TILED; 111 src_pitch /= 4; 112 } 113 if (dst->tiled) { 114 CMD |= XY_DST_TILED; 115 dst_pitch /= 4; 116 } 117#endif 118 119 again: 120 ret = dri_bufmgr_check_aperture_space(dst->buffer); 121 ret |= dri_bufmgr_check_aperture_space(src->buffer); 122 123 if (ret) { 124 intel_batchbuffer_flush(intel->batch); 125 goto again; 126 } 127 128 for (i = 0; i < nbox; i++, pbox++) { 129 drm_clip_rect_t box = *pbox; 130 131 if (rect) { 132 if (!intel_intersect_cliprects(&box, &box, rect)) 133 continue; 134 } 135 136 if (box.x1 >= box.x2 || 137 box.y1 >= box.y2) 138 continue; 139 140 assert(box.x1 < box.x2); 141 assert(box.y1 < box.y2); 142 src_x = box.x1 - dPriv->x + dPriv->backX; 143 src_y = box.y1 - dPriv->y + dPriv->backY; 144 145 BEGIN_BATCH(8, REFERENCES_CLIPRECTS); 146 OUT_BATCH(CMD); 147 OUT_BATCH(BR13 | dst_pitch); 148 OUT_BATCH((box.y1 << 16) | box.x1); 149 OUT_BATCH((box.y2 << 16) | box.x2); 150 151 OUT_RELOC(dst->buffer, 152 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 153 0); 154 OUT_BATCH((src_y << 16) | src_x); 155 OUT_BATCH(src_pitch); 156 OUT_RELOC(src->buffer, 157 DRM_GEM_DOMAIN_I915_RENDER, 0, 158 0); 159 ADVANCE_BATCH(); 160 } 161 162 /* Emit a flush so that, on systems where we don't have automatic flushing 163 * set (such as 965), the results all land on the screen in a timely 164 * fashion. 165 */ 166 BEGIN_BATCH(1, IGNORE_CLIPRECTS); 167 OUT_BATCH(MI_FLUSH); 168 ADVANCE_BATCH(); 169 170 intel_batchbuffer_flush(intel->batch); 171 } 172 173 UNLOCK_HARDWARE(intel); 174} 175 176 177 178 179void 180intelEmitFillBlit(struct intel_context *intel, 181 GLuint cpp, 182 GLshort dst_pitch, 183 dri_bo *dst_buffer, 184 GLuint dst_offset, 185 GLboolean dst_tiled, 186 GLshort x, GLshort y, 187 GLshort w, GLshort h, 188 GLuint color) 189{ 190 GLuint BR13, CMD; 191 BATCH_LOCALS; 192 193 dst_pitch *= cpp; 194 195 switch (cpp) { 196 case 1: 197 case 2: 198 case 3: 199 BR13 = (0xF0 << 16) | (1 << 24); 200 CMD = XY_COLOR_BLT_CMD; 201 break; 202 case 4: 203 BR13 = (0xF0 << 16) | (1 << 24) | (1 << 25); 204 CMD = XY_COLOR_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB; 205 break; 206 default: 207 return; 208 } 209#ifndef I915 210 if (dst_tiled) { 211 CMD |= XY_DST_TILED; 212 dst_pitch /= 4; 213 } 214#endif 215 216 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n", 217 __FUNCTION__, dst_buffer, dst_pitch, dst_offset, x, y, w, h); 218 219 assert(w > 0); 220 assert(h > 0); 221 222 BEGIN_BATCH(6, NO_LOOP_CLIPRECTS); 223 OUT_BATCH(CMD); 224 OUT_BATCH(BR13 | dst_pitch); 225 OUT_BATCH((y << 16) | x); 226 OUT_BATCH(((y + h) << 16) | (x + w)); 227 OUT_RELOC(dst_buffer, 228 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 229 dst_offset); 230 OUT_BATCH(color); 231 ADVANCE_BATCH(); 232} 233 234static GLuint translate_raster_op(GLenum logicop) 235{ 236 switch(logicop) { 237 case GL_CLEAR: return 0x00; 238 case GL_AND: return 0x88; 239 case GL_AND_REVERSE: return 0x44; 240 case GL_COPY: return 0xCC; 241 case GL_AND_INVERTED: return 0x22; 242 case GL_NOOP: return 0xAA; 243 case GL_XOR: return 0x66; 244 case GL_OR: return 0xEE; 245 case GL_NOR: return 0x11; 246 case GL_EQUIV: return 0x99; 247 case GL_INVERT: return 0x55; 248 case GL_OR_REVERSE: return 0xDD; 249 case GL_COPY_INVERTED: return 0x33; 250 case GL_OR_INVERTED: return 0xBB; 251 case GL_NAND: return 0x77; 252 case GL_SET: return 0xFF; 253 default: return 0; 254 } 255} 256 257 258/* Copy BitBlt 259 */ 260void 261intelEmitCopyBlit(struct intel_context *intel, 262 GLuint cpp, 263 GLshort src_pitch, 264 dri_bo *src_buffer, 265 GLuint src_offset, 266 GLboolean src_tiled, 267 GLshort dst_pitch, 268 dri_bo *dst_buffer, 269 GLuint dst_offset, 270 GLboolean dst_tiled, 271 GLshort src_x, GLshort src_y, 272 GLshort dst_x, GLshort dst_y, 273 GLshort w, GLshort h, 274 GLenum logic_op) 275{ 276 GLuint CMD, BR13; 277 int dst_y2 = dst_y + h; 278 int dst_x2 = dst_x + w; 279 int ret; 280 BATCH_LOCALS; 281 282 again: 283 ret = dri_bufmgr_check_aperture_space(dst_buffer); 284 ret |= dri_bufmgr_check_aperture_space(src_buffer); 285 if (ret) { 286 intel_batchbuffer_flush(intel->batch); 287 goto again; 288 } 289 290 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n", 291 __FUNCTION__, 292 src_buffer, src_pitch, src_offset, src_x, src_y, 293 dst_buffer, dst_pitch, dst_offset, dst_x, dst_y, w, h); 294 295 src_pitch *= cpp; 296 dst_pitch *= cpp; 297 298 BR13 = translate_raster_op(logic_op) << 16; 299 300 switch (cpp) { 301 case 1: 302 case 2: 303 case 3: 304 BR13 |= (1 << 24); 305 CMD = XY_SRC_COPY_BLT_CMD; 306 break; 307 case 4: 308 BR13 |= (1 << 24) | (1 << 25); 309 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB; 310 break; 311 default: 312 return; 313 } 314 315#ifndef I915 316 if (dst_tiled) { 317 CMD |= XY_DST_TILED; 318 dst_pitch /= 4; 319 } 320 if (src_tiled) { 321 CMD |= XY_SRC_TILED; 322 src_pitch /= 4; 323 } 324#endif 325 326 if (dst_y2 <= dst_y || dst_x2 <= dst_x) { 327 return; 328 } 329 330 dst_pitch &= 0xffff; 331 src_pitch &= 0xffff; 332 333 /* Initial y values don't seem to work with negative pitches. If 334 * we adjust the offsets manually (below), it seems to work fine. 335 * 336 * On the other hand, if we always adjust, the hardware doesn't 337 * know which blit directions to use, so overlapping copypixels get 338 * the wrong result. 339 */ 340 if (dst_pitch > 0 && src_pitch > 0) { 341 assert(dst_x < dst_x2); 342 assert(dst_y < dst_y2); 343 344 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS); 345 OUT_BATCH(CMD); 346 OUT_BATCH(BR13 | dst_pitch); 347 OUT_BATCH((dst_y << 16) | dst_x); 348 OUT_BATCH((dst_y2 << 16) | dst_x2); 349 OUT_RELOC(dst_buffer, 350 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 351 dst_offset); 352 OUT_BATCH((src_y << 16) | src_x); 353 OUT_BATCH(src_pitch); 354 OUT_RELOC(src_buffer, 355 DRM_GEM_DOMAIN_I915_RENDER, 0, 356 src_offset); 357 ADVANCE_BATCH(); 358 } 359 else { 360 assert(dst_x < dst_x2); 361 assert(h > 0); 362 363 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS); 364 OUT_BATCH(CMD); 365 OUT_BATCH(BR13 | dst_pitch); 366 OUT_BATCH((0 << 16) | dst_x); 367 OUT_BATCH((h << 16) | dst_x2); 368 OUT_RELOC(dst_buffer, 369 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 370 dst_offset + dst_y * dst_pitch); 371 OUT_BATCH((0 << 16) | src_x); 372 OUT_BATCH(src_pitch); 373 OUT_RELOC(src_buffer, 374 DRM_GEM_DOMAIN_I915_RENDER, 0, 375 src_offset + src_y * src_pitch); 376 ADVANCE_BATCH(); 377 } 378} 379 380 381/** 382 * Use blitting to clear the renderbuffers named by 'flags'. 383 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field 384 * since that might include software renderbuffers or renderbuffers 385 * which we're clearing with triangles. 386 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear 387 */ 388void 389intelClearWithBlit(GLcontext *ctx, GLbitfield mask) 390{ 391 struct intel_context *intel = intel_context(ctx); 392 struct gl_framebuffer *fb = ctx->DrawBuffer; 393 GLuint clear_depth; 394 GLbitfield skipBuffers = 0; 395 BATCH_LOCALS; 396 397 /* 398 * Compute values for clearing the buffers. 399 */ 400 clear_depth = 0; 401 if (mask & BUFFER_BIT_DEPTH) { 402 clear_depth = (GLuint) (fb->_DepthMax * ctx->Depth.Clear); 403 } 404 if (mask & BUFFER_BIT_STENCIL) { 405 clear_depth |= (ctx->Stencil.Clear & 0xff) << 24; 406 } 407 408 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in 409 * the loop below. 410 */ 411 if ((mask & BUFFER_BIT_DEPTH) && (mask & BUFFER_BIT_STENCIL)) { 412 skipBuffers = BUFFER_BIT_STENCIL; 413 } 414 415 /* XXX Move this flush/lock into the following conditional? */ 416 intelFlush(&intel->ctx); 417 LOCK_HARDWARE(intel); 418 419 if (intel->numClipRects) { 420 GLint cx, cy, cw, ch; 421 drm_clip_rect_t clear; 422 int i; 423 424 /* Get clear bounds after locking */ 425 cx = fb->_Xmin; 426 cy = fb->_Ymin; 427 cw = fb->_Xmax - cx; 428 ch = fb->_Ymax - cy; 429 430 if (fb->Name == 0) { 431 /* clearing a window */ 432 433 /* flip top to bottom */ 434 clear.x1 = cx + intel->drawX; 435 clear.y1 = intel->driDrawable->y + intel->driDrawable->h - cy - ch; 436 clear.x2 = clear.x1 + cw; 437 clear.y2 = clear.y1 + ch; 438 } 439 else { 440 /* clearing FBO */ 441 assert(intel->numClipRects == 1); 442 assert(intel->pClipRects == &intel->fboRect); 443 clear.x1 = cx; 444 clear.y1 = cy; 445 clear.x2 = clear.x1 + cw; 446 clear.y2 = clear.y1 + ch; 447 /* no change to mask */ 448 } 449 450 for (i = 0; i < intel->numClipRects; i++) { 451 const drm_clip_rect_t *box = &intel->pClipRects[i]; 452 drm_clip_rect_t b; 453 GLuint buf; 454 GLuint clearMask = mask; /* use copy, since we modify it below */ 455 GLboolean all = (cw == fb->Width && ch == fb->Height); 456 457 if (!all) { 458 intel_intersect_cliprects(&b, &clear, box); 459 } 460 else { 461 b = *box; 462 } 463 464 if (b.x1 >= b.x2 || b.y1 >= b.y2) 465 continue; 466 467 if (0) 468 _mesa_printf("clear %d,%d..%d,%d, mask %x\n", 469 b.x1, b.y1, b.x2, b.y2, mask); 470 471 /* Loop over all renderbuffers */ 472 for (buf = 0; buf < BUFFER_COUNT && clearMask; buf++) { 473 const GLbitfield bufBit = 1 << buf; 474 if ((clearMask & bufBit) && !(bufBit & skipBuffers)) { 475 /* OK, clear this renderbuffer */ 476 struct intel_region *irb_region = 477 intel_get_rb_region(fb, buf); 478 dri_bo *write_buffer = 479 intel_region_buffer(intel, irb_region, 480 all ? INTEL_WRITE_FULL : 481 INTEL_WRITE_PART); 482 483 GLuint clearVal; 484 GLint pitch, cpp; 485 GLuint BR13, CMD; 486 487 ASSERT(irb_region); 488 489 pitch = irb_region->pitch; 490 cpp = irb_region->cpp; 491 492 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n", 493 __FUNCTION__, 494 irb_region->buffer, (pitch * cpp), 495 irb_region->draw_offset, 496 b.x1, b.y1, b.x2 - b.x1, b.y2 - b.y1); 497 498 BR13 = 0xf0 << 16; 499 CMD = XY_COLOR_BLT_CMD; 500 501 /* Setup the blit command */ 502 if (cpp == 4) { 503 BR13 |= (1 << 24) | (1 << 25); 504 if (buf == BUFFER_DEPTH || buf == BUFFER_STENCIL) { 505 if (clearMask & BUFFER_BIT_DEPTH) 506 CMD |= XY_BLT_WRITE_RGB; 507 if (clearMask & BUFFER_BIT_STENCIL) 508 CMD |= XY_BLT_WRITE_ALPHA; 509 } 510 else { 511 /* clearing RGBA */ 512 CMD |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB; 513 } 514 } 515 else { 516 ASSERT(cpp == 2 || cpp == 0); 517 BR13 |= (1 << 24); 518 } 519 520#ifndef I915 521 if (irb_region->tiled) { 522 CMD |= XY_DST_TILED; 523 pitch /= 4; 524 } 525#endif 526 BR13 |= (pitch * cpp); 527 528 if (buf == BUFFER_DEPTH || buf == BUFFER_STENCIL) { 529 clearVal = clear_depth; 530 } 531 else { 532 clearVal = (cpp == 4) 533 ? intel->ClearColor8888 : intel->ClearColor565; 534 } 535 /* 536 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n", 537 buf, irb->Base.Name); 538 */ 539 intel_wait_flips(intel); 540 541 assert(b.x1 < b.x2); 542 assert(b.y1 < b.y2); 543 544 BEGIN_BATCH(6, REFERENCES_CLIPRECTS); 545 OUT_BATCH(CMD); 546 OUT_BATCH(BR13); 547 OUT_BATCH((b.y1 << 16) | b.x1); 548 OUT_BATCH((b.y2 << 16) | b.x2); 549 OUT_RELOC(write_buffer, 550 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 551 irb_region->draw_offset); 552 OUT_BATCH(clearVal); 553 ADVANCE_BATCH(); 554 clearMask &= ~bufBit; /* turn off bit, for faster loop exit */ 555 } 556 } 557 } 558 intel_batchbuffer_flush(intel->batch); 559 } 560 561 UNLOCK_HARDWARE(intel); 562} 563 564void 565intelEmitImmediateColorExpandBlit(struct intel_context *intel, 566 GLuint cpp, 567 GLubyte *src_bits, GLuint src_size, 568 GLuint fg_color, 569 GLshort dst_pitch, 570 dri_bo *dst_buffer, 571 GLuint dst_offset, 572 GLboolean dst_tiled, 573 GLshort x, GLshort y, 574 GLshort w, GLshort h, 575 GLenum logic_op) 576{ 577 int dwords = ALIGN(src_size, 8) / 4; 578 uint32_t opcode, br13, blit_cmd; 579 580 assert( logic_op - GL_CLEAR >= 0 ); 581 assert( logic_op - GL_CLEAR < 0x10 ); 582 583 if (w < 0 || h < 0) 584 return; 585 586 dst_pitch *= cpp; 587 588 if (dst_tiled) 589 dst_pitch /= 4; 590 591 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n", 592 __FUNCTION__, 593 dst_buffer, dst_pitch, dst_offset, x, y, w, h, src_size, dwords); 594 595 intel_batchbuffer_require_space( intel->batch, 596 (8 * 4) + 597 (3 * 4) + 598 dwords, 599 NO_LOOP_CLIPRECTS ); 600 601 opcode = XY_SETUP_BLT_CMD; 602 if (cpp == 4) 603 opcode |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB; 604 if (dst_tiled) 605 opcode |= XY_DST_TILED; 606 607 br13 = dst_pitch | (translate_raster_op(logic_op) << 16) | (1 << 29); 608 if (cpp == 2) 609 br13 |= BR13_565; 610 else 611 br13 |= BR13_8888; 612 613 blit_cmd = XY_TEXT_IMMEDIATE_BLIT_CMD | XY_TEXT_BYTE_PACKED; /* packing? */ 614 if (dst_tiled) 615 blit_cmd |= XY_DST_TILED; 616 617 BEGIN_BATCH(8 + 3, NO_LOOP_CLIPRECTS); 618 OUT_BATCH(opcode); 619 OUT_BATCH(br13); 620 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */ 621 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */ 622 OUT_RELOC(dst_buffer, 623 DRM_GEM_DOMAIN_I915_RENDER, DRM_GEM_DOMAIN_I915_RENDER, 624 dst_offset); 625 OUT_BATCH(0); /* bg */ 626 OUT_BATCH(fg_color); /* fg */ 627 OUT_BATCH(0); /* pattern base addr */ 628 629 OUT_BATCH(blit_cmd | ((3 - 2) + dwords)); 630 OUT_BATCH((y << 16) | x); 631 OUT_BATCH(((y + h) << 16) | (x + w)); 632 ADVANCE_BATCH(); 633 634 intel_batchbuffer_data( intel->batch, 635 src_bits, 636 dwords * 4, 637 NO_LOOP_CLIPRECTS ); 638} 639