| 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/core/SkBlitter.h" |
| 9 | |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkColorFilter.h" |
| 12 | #include "include/core/SkString.h" |
| 13 | #include "include/private/SkColorData.h" |
| 14 | #include "include/private/SkTo.h" |
| 15 | #include "src/core/SkAntiRun.h" |
| 16 | #include "src/core/SkArenaAlloc.h" |
| 17 | #include "src/core/SkMask.h" |
| 18 | #include "src/core/SkMaskFilterBase.h" |
| 19 | #include "src/core/SkMatrixProvider.h" |
| 20 | #include "src/core/SkPaintPriv.h" |
| 21 | #include "src/core/SkReadBuffer.h" |
| 22 | #include "src/core/SkRegionPriv.h" |
| 23 | #include "src/core/SkTLazy.h" |
| 24 | #include "src/core/SkUtils.h" |
| 25 | #include "src/core/SkWriteBuffer.h" |
| 26 | #include "src/core/SkXfermodeInterpretation.h" |
| 27 | #include "src/shaders/SkShaderBase.h" |
| 28 | |
| 29 | // Hacks for testing. |
| 30 | bool gUseSkVMBlitter{false}; |
| 31 | bool gSkForceRasterPipelineBlitter{false}; |
| 32 | |
| 33 | SkBlitter::~SkBlitter() {} |
| 34 | |
| 35 | bool SkBlitter::isNullBlitter() const { return false; } |
| 36 | |
| 37 | const SkPixmap* SkBlitter::justAnOpaqueColor(uint32_t* value) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | void SkBlitter::blitH(int x, int y, int width) { |
| 43 | SkDEBUGFAIL("unimplemented"); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 48 | const int16_t runs[]) { |
| 49 | SkDEBUGFAIL("unimplemented"); |
| 50 | } |
| 51 | */ |
| 52 | |
| 53 | inline static SkAlpha ScalarToAlpha(SkScalar a) { |
| 54 | SkAlpha alpha = (SkAlpha)(a * 255); |
| 55 | return alpha > 247 ? 0xFF : alpha < 8 ? 0 : alpha; |
| 56 | } |
| 57 | |
| 58 | void SkBlitter::blitFatAntiRect(const SkRect& rect) { |
| 59 | SkIRect bounds = rect.roundOut(); |
| 60 | SkASSERT(bounds.width() >= 3); |
| 61 | |
| 62 | // skbug.com/7813 |
| 63 | // To ensure consistency of the threaded backend (a rect that's considered fat in the init-once |
| 64 | // phase must also be considered fat in the draw phase), we have to deal with rects with small |
| 65 | // heights because the horizontal tiling in the threaded backend may change the height. |
| 66 | // |
| 67 | // This also implies that we cannot do vertical tiling unless we can blit any rect (not just the |
| 68 | // fat one.) |
| 69 | if (bounds.height() == 0) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | int runSize = bounds.width() + 1; // +1 so we can set runs[bounds.width()] = 0 |
| 74 | void* storage = this->allocBlitMemory(runSize * (sizeof(int16_t) + sizeof(SkAlpha))); |
| 75 | int16_t* runs = reinterpret_cast<int16_t*>(storage); |
| 76 | SkAlpha* alphas = reinterpret_cast<SkAlpha*>(runs + runSize); |
| 77 | |
| 78 | runs[0] = 1; |
| 79 | runs[1] = bounds.width() - 2; |
| 80 | runs[bounds.width() - 1] = 1; |
| 81 | runs[bounds.width()] = 0; |
| 82 | |
| 83 | SkScalar partialL = bounds.fLeft + 1 - rect.fLeft; |
| 84 | SkScalar partialR = rect.fRight - (bounds.fRight - 1); |
| 85 | SkScalar partialT = bounds.fTop + 1 - rect.fTop; |
| 86 | SkScalar partialB = rect.fBottom - (bounds.fBottom - 1); |
| 87 | |
| 88 | if (bounds.height() == 1) { |
| 89 | partialT = rect.fBottom - rect.fTop; |
| 90 | } |
| 91 | |
| 92 | alphas[0] = ScalarToAlpha(partialL * partialT); |
| 93 | alphas[1] = ScalarToAlpha(partialT); |
| 94 | alphas[bounds.width() - 1] = ScalarToAlpha(partialR * partialT); |
| 95 | this->blitAntiH(bounds.fLeft, bounds.fTop, alphas, runs); |
| 96 | |
| 97 | if (bounds.height() > 2) { |
| 98 | this->blitAntiRect(bounds.fLeft, bounds.fTop + 1, bounds.width() - 2, bounds.height() - 2, |
| 99 | ScalarToAlpha(partialL), ScalarToAlpha(partialR)); |
| 100 | } |
| 101 | |
| 102 | if (bounds.height() > 1) { |
| 103 | alphas[0] = ScalarToAlpha(partialL * partialB); |
| 104 | alphas[1] = ScalarToAlpha(partialB); |
| 105 | alphas[bounds.width() - 1] = ScalarToAlpha(partialR * partialB); |
| 106 | this->blitAntiH(bounds.fLeft, bounds.fBottom - 1, alphas, runs); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 111 | if (alpha == 255) { |
| 112 | this->blitRect(x, y, 1, height); |
| 113 | } else { |
| 114 | int16_t runs[2]; |
| 115 | runs[0] = 1; |
| 116 | runs[1] = 0; |
| 117 | |
| 118 | while (--height >= 0) { |
| 119 | this->blitAntiH(x, y++, &alpha, runs); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void SkBlitter::blitRect(int x, int y, int width, int height) { |
| 125 | SkASSERT(width > 0); |
| 126 | while (--height >= 0) { |
| 127 | this->blitH(x, y++, width); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// Default implementation doesn't check for easy optimizations |
| 132 | /// such as alpha == 255; also uses blitV(), which some subclasses |
| 133 | /// may not support. |
| 134 | void SkBlitter::blitAntiRect(int x, int y, int width, int height, |
| 135 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 136 | if (leftAlpha > 0) { // we may send in x = -1 with leftAlpha = 0 |
| 137 | this->blitV(x, y, height, leftAlpha); |
| 138 | } |
| 139 | x++; |
| 140 | if (width > 0) { |
| 141 | this->blitRect(x, y, width, height); |
| 142 | x += width; |
| 143 | } |
| 144 | if (rightAlpha > 0) { |
| 145 | this->blitV(x, y, height, rightAlpha); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | static inline void bits_to_runs(SkBlitter* blitter, int x, int y, |
| 152 | const uint8_t bits[], |
| 153 | uint8_t left_mask, ptrdiff_t rowBytes, |
| 154 | uint8_t right_mask) { |
| 155 | int inFill = 0; |
| 156 | int pos = 0; |
| 157 | |
| 158 | while (--rowBytes >= 0) { |
| 159 | uint8_t b = *bits++ & left_mask; |
| 160 | if (rowBytes == 0) { |
| 161 | b &= right_mask; |
| 162 | } |
| 163 | |
| 164 | for (uint8_t test = 0x80U; test != 0; test >>= 1) { |
| 165 | if (b & test) { |
| 166 | if (!inFill) { |
| 167 | pos = x; |
| 168 | inFill = true; |
| 169 | } |
| 170 | } else { |
| 171 | if (inFill) { |
| 172 | blitter->blitH(pos, y, x - pos); |
| 173 | inFill = false; |
| 174 | } |
| 175 | } |
| 176 | x += 1; |
| 177 | } |
| 178 | left_mask = 0xFFU; |
| 179 | } |
| 180 | |
| 181 | // final cleanup |
| 182 | if (inFill) { |
| 183 | blitter->blitH(pos, y, x - pos); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // maskBitCount is the number of 1's to place in the mask. It must be in the range between 1 and 8. |
| 188 | static uint8_t generate_right_mask(int maskBitCount) { |
| 189 | return static_cast<uint8_t>((0xFF00U >> maskBitCount) & 0xFF); |
| 190 | } |
| 191 | |
| 192 | void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 193 | SkASSERT(mask.fBounds.contains(clip)); |
| 194 | |
| 195 | if (mask.fFormat == SkMask::kLCD16_Format) { |
| 196 | return; // needs to be handled by subclass |
| 197 | } |
| 198 | |
| 199 | if (mask.fFormat == SkMask::kBW_Format) { |
| 200 | int cx = clip.fLeft; |
| 201 | int cy = clip.fTop; |
| 202 | int maskLeft = mask.fBounds.fLeft; |
| 203 | int maskRowBytes = mask.fRowBytes; |
| 204 | int height = clip.height(); |
| 205 | |
| 206 | const uint8_t* bits = mask.getAddr1(cx, cy); |
| 207 | |
| 208 | SkDEBUGCODE(const uint8_t* endOfImage = |
| 209 | mask.fImage + (mask.fBounds.height() - 1) * maskRowBytes |
| 210 | + ((mask.fBounds.width() + 7) >> 3)); |
| 211 | |
| 212 | if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) { |
| 213 | while (--height >= 0) { |
| 214 | int affectedRightBit = mask.fBounds.width() - 1; |
| 215 | ptrdiff_t rowBytes = (affectedRightBit >> 3) + 1; |
| 216 | SkASSERT(bits + rowBytes <= endOfImage); |
| 217 | U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1); |
| 218 | bits_to_runs(this, cx, cy, bits, 0xFF, rowBytes, rightMask); |
| 219 | bits += maskRowBytes; |
| 220 | cy += 1; |
| 221 | } |
| 222 | } else { |
| 223 | // Bits is calculated as the offset into the mask at the point {cx, cy} therefore, all |
| 224 | // addressing into the bit mask is relative to that point. Since this is an address |
| 225 | // calculated from a arbitrary bit in that byte, calculate the left most bit. |
| 226 | int bitsLeft = cx - ((cx - maskLeft) & 7); |
| 227 | |
| 228 | // Everything is relative to the bitsLeft. |
| 229 | int leftEdge = cx - bitsLeft; |
| 230 | SkASSERT(leftEdge >= 0); |
| 231 | int rightEdge = clip.fRight - bitsLeft; |
| 232 | SkASSERT(rightEdge > leftEdge); |
| 233 | |
| 234 | // Calculate left byte and mask |
| 235 | const uint8_t* leftByte = bits; |
| 236 | U8CPU leftMask = 0xFFU >> (leftEdge & 7); |
| 237 | |
| 238 | // Calculate right byte and mask |
| 239 | int affectedRightBit = rightEdge - 1; |
| 240 | const uint8_t* rightByte = bits + (affectedRightBit >> 3); |
| 241 | U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1); |
| 242 | |
| 243 | // leftByte and rightByte are byte locations therefore, to get a count of bytes the |
| 244 | // code must add one. |
| 245 | ptrdiff_t rowBytes = rightByte - leftByte + 1; |
| 246 | |
| 247 | while (--height >= 0) { |
| 248 | SkASSERT(bits + rowBytes <= endOfImage); |
| 249 | bits_to_runs(this, bitsLeft, cy, bits, leftMask, rowBytes, rightMask); |
| 250 | bits += maskRowBytes; |
| 251 | cy += 1; |
| 252 | } |
| 253 | } |
| 254 | } else { |
| 255 | int width = clip.width(); |
| 256 | SkAutoSTMalloc<64, int16_t> runStorage(width + 1); |
| 257 | int16_t* runs = runStorage.get(); |
| 258 | const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop); |
| 259 | |
| 260 | sk_memset16((uint16_t*)runs, 1, width); |
| 261 | runs[width] = 0; |
| 262 | |
| 263 | int height = clip.height(); |
| 264 | int y = clip.fTop; |
| 265 | while (--height >= 0) { |
| 266 | this->blitAntiH(clip.fLeft, y, aa, runs); |
| 267 | aa += mask.fRowBytes; |
| 268 | y += 1; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /////////////////////// these are not virtual, just helpers |
| 274 | |
| 275 | void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) { |
| 276 | if (clip.quickReject(mask.fBounds)) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | SkRegion::Cliperator clipper(clip, mask.fBounds); |
| 281 | |
| 282 | while (!clipper.done()) { |
| 283 | const SkIRect& cr = clipper.rect(); |
| 284 | this->blitMask(mask, cr); |
| 285 | clipper.next(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) { |
| 290 | SkRegion::Cliperator clipper(clip, rect); |
| 291 | |
| 292 | while (!clipper.done()) { |
| 293 | const SkIRect& cr = clipper.rect(); |
| 294 | this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 295 | clipper.next(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void SkBlitter::blitRegion(const SkRegion& clip) { |
| 300 | SkRegionPriv::VisitSpans(clip, [this](const SkIRect& r) { |
| 301 | this->blitRect(r.left(), r.top(), r.width(), r.height()); |
| 302 | }); |
| 303 | } |
| 304 | |
| 305 | /////////////////////////////////////////////////////////////////////////////// |
| 306 | |
| 307 | void SkNullBlitter::blitH(int x, int y, int width) {} |
| 308 | |
| 309 | void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 310 | const int16_t runs[]) {} |
| 311 | |
| 312 | void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {} |
| 313 | |
| 314 | void SkNullBlitter::blitRect(int x, int y, int width, int height) {} |
| 315 | |
| 316 | void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {} |
| 317 | |
| 318 | const SkPixmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) { |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
| 322 | bool SkNullBlitter::isNullBlitter() const { return true; } |
| 323 | |
| 324 | /////////////////////////////////////////////////////////////////////////////// |
| 325 | |
| 326 | static int compute_anti_width(const int16_t runs[]) { |
| 327 | int width = 0; |
| 328 | |
| 329 | for (;;) { |
| 330 | int count = runs[0]; |
| 331 | |
| 332 | SkASSERT(count >= 0); |
| 333 | if (count == 0) { |
| 334 | break; |
| 335 | } |
| 336 | width += count; |
| 337 | runs += count; |
| 338 | } |
| 339 | return width; |
| 340 | } |
| 341 | |
| 342 | static inline bool y_in_rect(int y, const SkIRect& rect) { |
| 343 | return (unsigned)(y - rect.fTop) < (unsigned)rect.height(); |
| 344 | } |
| 345 | |
| 346 | static inline bool x_in_rect(int x, const SkIRect& rect) { |
| 347 | return (unsigned)(x - rect.fLeft) < (unsigned)rect.width(); |
| 348 | } |
| 349 | |
| 350 | void SkRectClipBlitter::blitH(int left, int y, int width) { |
| 351 | SkASSERT(width > 0); |
| 352 | |
| 353 | if (!y_in_rect(y, fClipRect)) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | int right = left + width; |
| 358 | |
| 359 | if (left < fClipRect.fLeft) { |
| 360 | left = fClipRect.fLeft; |
| 361 | } |
| 362 | if (right > fClipRect.fRight) { |
| 363 | right = fClipRect.fRight; |
| 364 | } |
| 365 | |
| 366 | width = right - left; |
| 367 | if (width > 0) { |
| 368 | fBlitter->blitH(left, y, width); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[], |
| 373 | const int16_t runs[]) { |
| 374 | if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | int x0 = left; |
| 379 | int x1 = left + compute_anti_width(runs); |
| 380 | |
| 381 | if (x1 <= fClipRect.fLeft) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | SkASSERT(x0 < x1); |
| 386 | if (x0 < fClipRect.fLeft) { |
| 387 | int dx = fClipRect.fLeft - x0; |
| 388 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx); |
| 389 | runs += dx; |
| 390 | aa += dx; |
| 391 | x0 = fClipRect.fLeft; |
| 392 | } |
| 393 | |
| 394 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
| 395 | if (x1 > fClipRect.fRight) { |
| 396 | x1 = fClipRect.fRight; |
| 397 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0); |
| 398 | ((int16_t*)runs)[x1 - x0] = 0; |
| 399 | } |
| 400 | |
| 401 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
| 402 | SkASSERT(compute_anti_width(runs) == x1 - x0); |
| 403 | |
| 404 | fBlitter->blitAntiH(x0, y, aa, runs); |
| 405 | } |
| 406 | |
| 407 | void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 408 | SkASSERT(height > 0); |
| 409 | |
| 410 | if (!x_in_rect(x, fClipRect)) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | int y0 = y; |
| 415 | int y1 = y + height; |
| 416 | |
| 417 | if (y0 < fClipRect.fTop) { |
| 418 | y0 = fClipRect.fTop; |
| 419 | } |
| 420 | if (y1 > fClipRect.fBottom) { |
| 421 | y1 = fClipRect.fBottom; |
| 422 | } |
| 423 | |
| 424 | if (y0 < y1) { |
| 425 | fBlitter->blitV(x, y0, y1 - y0, alpha); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void SkRectClipBlitter::blitRect(int left, int y, int width, int height) { |
| 430 | SkIRect r; |
| 431 | |
| 432 | r.setLTRB(left, y, left + width, y + height); |
| 433 | if (r.intersect(fClipRect)) { |
| 434 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height, |
| 439 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 440 | SkIRect r; |
| 441 | |
| 442 | // The *true* width of the rectangle blitted is width+2: |
| 443 | r.setLTRB(left, y, left + width + 2, y + height); |
| 444 | if (r.intersect(fClipRect)) { |
| 445 | if (r.fLeft != left) { |
| 446 | SkASSERT(r.fLeft > left); |
| 447 | leftAlpha = 255; |
| 448 | } |
| 449 | if (r.fRight != left + width + 2) { |
| 450 | SkASSERT(r.fRight < left + width + 2); |
| 451 | rightAlpha = 255; |
| 452 | } |
| 453 | if (255 == leftAlpha && 255 == rightAlpha) { |
| 454 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 455 | } else if (1 == r.width()) { |
| 456 | if (r.fLeft == left) { |
| 457 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha); |
| 458 | } else { |
| 459 | SkASSERT(r.fLeft == left + width + 1); |
| 460 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha); |
| 461 | } |
| 462 | } else { |
| 463 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 464 | leftAlpha, rightAlpha); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 470 | SkASSERT(mask.fBounds.contains(clip)); |
| 471 | |
| 472 | SkIRect r = clip; |
| 473 | |
| 474 | if (r.intersect(fClipRect)) { |
| 475 | fBlitter->blitMask(mask, r); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | const SkPixmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) { |
| 480 | return fBlitter->justAnOpaqueColor(value); |
| 481 | } |
| 482 | |
| 483 | /////////////////////////////////////////////////////////////////////////////// |
| 484 | |
| 485 | void SkRgnClipBlitter::blitH(int x, int y, int width) { |
| 486 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 487 | int left, right; |
| 488 | |
| 489 | while (span.next(&left, &right)) { |
| 490 | SkASSERT(left < right); |
| 491 | fBlitter->blitH(left, y, right - left); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], |
| 496 | const int16_t runs[]) { |
| 497 | int width = compute_anti_width(runs); |
| 498 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 499 | int left, right; |
| 500 | SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();) |
| 501 | |
| 502 | int prevRite = x; |
| 503 | while (span.next(&left, &right)) { |
| 504 | SkASSERT(x <= left); |
| 505 | SkASSERT(left < right); |
| 506 | SkASSERT(left >= bounds.fLeft && right <= bounds.fRight); |
| 507 | |
| 508 | SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left); |
| 509 | |
| 510 | // now zero before left |
| 511 | if (left > prevRite) { |
| 512 | int index = prevRite - x; |
| 513 | ((uint8_t*)aa)[index] = 0; // skip runs after right |
| 514 | ((int16_t*)runs)[index] = SkToS16(left - prevRite); |
| 515 | } |
| 516 | |
| 517 | prevRite = right; |
| 518 | } |
| 519 | |
| 520 | if (prevRite > x) { |
| 521 | ((int16_t*)runs)[prevRite - x] = 0; |
| 522 | |
| 523 | if (x < 0) { |
| 524 | int skip = runs[0]; |
| 525 | SkASSERT(skip >= -x); |
| 526 | aa += skip; |
| 527 | runs += skip; |
| 528 | x += skip; |
| 529 | } |
| 530 | fBlitter->blitAntiH(x, y, aa, runs); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 535 | SkIRect bounds; |
| 536 | bounds.setXYWH(x, y, 1, height); |
| 537 | |
| 538 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 539 | |
| 540 | while (!iter.done()) { |
| 541 | const SkIRect& r = iter.rect(); |
| 542 | SkASSERT(bounds.contains(r)); |
| 543 | |
| 544 | fBlitter->blitV(x, r.fTop, r.height(), alpha); |
| 545 | iter.next(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) { |
| 550 | SkIRect bounds; |
| 551 | bounds.setXYWH(x, y, width, height); |
| 552 | |
| 553 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 554 | |
| 555 | while (!iter.done()) { |
| 556 | const SkIRect& r = iter.rect(); |
| 557 | SkASSERT(bounds.contains(r)); |
| 558 | |
| 559 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 560 | iter.next(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height, |
| 565 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 566 | // The *true* width of the rectangle to blit is width + 2 |
| 567 | SkIRect bounds; |
| 568 | bounds.setXYWH(x, y, width + 2, height); |
| 569 | |
| 570 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 571 | |
| 572 | while (!iter.done()) { |
| 573 | const SkIRect& r = iter.rect(); |
| 574 | SkASSERT(bounds.contains(r)); |
| 575 | SkASSERT(r.fLeft >= x); |
| 576 | SkASSERT(r.fRight <= x + width + 2); |
| 577 | |
| 578 | SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255; |
| 579 | SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ? |
| 580 | rightAlpha : 255; |
| 581 | |
| 582 | if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) { |
| 583 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 584 | } else if (1 == r.width()) { |
| 585 | if (r.fLeft == x) { |
| 586 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
| 587 | effectiveLeftAlpha); |
| 588 | } else { |
| 589 | SkASSERT(r.fLeft == x + width + 1); |
| 590 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
| 591 | effectiveRightAlpha); |
| 592 | } |
| 593 | } else { |
| 594 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 595 | effectiveLeftAlpha, effectiveRightAlpha); |
| 596 | } |
| 597 | iter.next(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
| 602 | void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 603 | SkASSERT(mask.fBounds.contains(clip)); |
| 604 | |
| 605 | SkRegion::Cliperator iter(*fRgn, clip); |
| 606 | const SkIRect& r = iter.rect(); |
| 607 | SkBlitter* blitter = fBlitter; |
| 608 | |
| 609 | while (!iter.done()) { |
| 610 | blitter->blitMask(mask, r); |
| 611 | iter.next(); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | const SkPixmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) { |
| 616 | return fBlitter->justAnOpaqueColor(value); |
| 617 | } |
| 618 | |
| 619 | /////////////////////////////////////////////////////////////////////////////// |
| 620 | |
| 621 | SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip, |
| 622 | const SkIRect* ir) { |
| 623 | if (clip) { |
| 624 | const SkIRect& clipR = clip->getBounds(); |
| 625 | |
| 626 | if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) { |
| 627 | blitter = &fNullBlitter; |
| 628 | } else if (clip->isRect()) { |
| 629 | if (ir == nullptr || !clipR.contains(*ir)) { |
| 630 | fRectBlitter.init(blitter, clipR); |
| 631 | blitter = &fRectBlitter; |
| 632 | } |
| 633 | } else { |
| 634 | fRgnBlitter.init(blitter, clip); |
| 635 | blitter = &fRgnBlitter; |
| 636 | } |
| 637 | } |
| 638 | return blitter; |
| 639 | } |
| 640 | |
| 641 | /////////////////////////////////////////////////////////////////////////////// |
| 642 | |
| 643 | #include "src/core/SkCoreBlitters.h" |
| 644 | |
| 645 | bool SkBlitter::UseRasterPipelineBlitter(const SkPixmap& device, const SkPaint& paint, |
| 646 | const SkMatrix& matrix) { |
| 647 | if (gSkForceRasterPipelineBlitter) { |
| 648 | return true; |
| 649 | } |
| 650 | #if 0 || defined(SK_FORCE_RASTER_PIPELINE_BLITTER) |
| 651 | return true; |
| 652 | #else |
| 653 | |
| 654 | const SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
| 655 | |
| 656 | // The legacy blitters cannot handle any of these complex features (anymore). |
| 657 | if (device.alphaType() == kUnpremul_SkAlphaType || |
| 658 | paint.getBlendMode() > SkBlendMode::kLastCoeffMode || |
| 659 | (mf && mf->getFormat() == SkMask::k3D_Format)) { |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | // All the real legacy fast paths are for shaders and SrcOver. |
| 664 | // Choosing SkRasterPipelineBlitter will also let us to hit its single-color memset path. |
| 665 | if (!paint.getShader() && paint.getBlendMode() != SkBlendMode::kSrcOver) { |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | auto cs = device.colorSpace(); |
| 670 | // We check (indirectly via makeContext()) later on if the shader can handle the colorspace |
| 671 | // in legacy mode, so here we just focus on if a single color needs raster-pipeline. |
| 672 | if (cs && !paint.getShader()) { |
| 673 | if (!paint.getColor4f().fitsInBytes() || !cs->isSRGB()) { |
| 674 | return true; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // Only kN32 and 565 are handled by legacy blitters now, 565 mostly just for Android. |
| 679 | return device.colorType() != kN32_SkColorType |
| 680 | && device.colorType() != kRGB_565_SkColorType; |
| 681 | #endif |
| 682 | } |
| 683 | |
| 684 | SkBlitter* SkBlitter::Choose(const SkPixmap& device, |
| 685 | const SkMatrixProvider& matrixProvider, |
| 686 | const SkPaint& origPaint, |
| 687 | SkArenaAlloc* alloc, |
| 688 | bool drawCoverage, |
| 689 | sk_sp<SkShader> clipShader) { |
| 690 | SkASSERT(alloc); |
| 691 | |
| 692 | if (kUnknown_SkColorType == device.colorType()) { |
| 693 | return alloc->make<SkNullBlitter>(); |
| 694 | } |
| 695 | |
| 696 | // We may tweak the original paint as we go. |
| 697 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 698 | |
| 699 | // We have the most fast-paths for SrcOver, so see if we can act like SrcOver. |
| 700 | if (paint->getBlendMode() != SkBlendMode::kSrcOver) { |
| 701 | switch (SkInterpretXfermode(*paint, SkColorTypeIsAlwaysOpaque(device.colorType()))) { |
| 702 | case kSrcOver_SkXfermodeInterpretation: |
| 703 | paint.writable()->setBlendMode(SkBlendMode::kSrcOver); |
| 704 | break; |
| 705 | case kSkipDrawing_SkXfermodeInterpretation: |
| 706 | return alloc->make<SkNullBlitter>(); |
| 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // A Clear blend mode will ignore the entire color pipeline, as if Src mode with 0x00000000. |
| 713 | if (paint->getBlendMode() == SkBlendMode::kClear) { |
| 714 | SkPaint* p = paint.writable(); |
| 715 | p->setShader(nullptr); |
| 716 | p->setColorFilter(nullptr); |
| 717 | p->setBlendMode(SkBlendMode::kSrc); |
| 718 | p->setColor(0x00000000); |
| 719 | } |
| 720 | |
| 721 | if (paint->getColorFilter()) { |
| 722 | SkPaintPriv::RemoveColorFilter(paint.writable(), device.colorSpace()); |
| 723 | } |
| 724 | SkASSERT(!paint->getColorFilter()); |
| 725 | |
| 726 | if (drawCoverage) { |
| 727 | if (device.colorType() == kAlpha_8_SkColorType) { |
| 728 | SkASSERT(!paint->getShader()); |
| 729 | SkASSERT(paint->isSrcOver()); |
| 730 | return alloc->make<SkA8_Coverage_Blitter>(device, *paint); |
| 731 | } |
| 732 | return alloc->make<SkNullBlitter>(); |
| 733 | } |
| 734 | |
| 735 | if (paint->isDither() && !SkPaintPriv::ShouldDither(*paint, device.colorType())) { |
| 736 | paint.writable()->setDither(false); |
| 737 | } |
| 738 | |
| 739 | if (gUseSkVMBlitter) { |
| 740 | if (auto blitter = SkCreateSkVMBlitter(device, *paint, matrixProvider, |
| 741 | alloc, clipShader)) { |
| 742 | return blitter; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // Same basic idea used a few times: try SkRP, then try SkVM, then give up with a null-blitter. |
| 747 | // (Setting gUseSkVMBlitter is the only way we prefer SkVM over SkRP at the moment.) |
| 748 | auto create_SkRP_or_SkVMBlitter = [&]() -> SkBlitter* { |
| 749 | if (auto blitter = SkCreateRasterPipelineBlitter(device, *paint, matrixProvider, |
| 750 | alloc, clipShader)) { |
| 751 | return blitter; |
| 752 | } |
| 753 | if (auto blitter = SkCreateSkVMBlitter(device, *paint, matrixProvider, |
| 754 | alloc, clipShader)) { |
| 755 | return blitter; |
| 756 | } |
| 757 | return alloc->make<SkNullBlitter>(); |
| 758 | }; |
| 759 | |
| 760 | SkMatrix ctm = matrixProvider.localToDevice(); |
| 761 | // We'll end here for many interesting cases: color spaces, color filters, most color types. |
| 762 | if (UseRasterPipelineBlitter(device, *paint, ctm) || clipShader) { |
| 763 | return create_SkRP_or_SkVMBlitter(); |
| 764 | } |
| 765 | |
| 766 | // Everything but legacy kN32_SkColorType and kRGB_565_SkColorType should already be handled. |
| 767 | SkASSERT(device.colorType() == kN32_SkColorType || |
| 768 | device.colorType() == kRGB_565_SkColorType); |
| 769 | |
| 770 | // And we should either have a shader, be blending with SrcOver, or both. |
| 771 | SkASSERT(paint->getShader() || paint->getBlendMode() == SkBlendMode::kSrcOver); |
| 772 | |
| 773 | // Legacy blitters keep their shader state on a shader context. |
| 774 | SkShaderBase::Context* shaderContext = nullptr; |
| 775 | if (paint->getShader()) { |
| 776 | shaderContext = as_SB(paint->getShader())->makeContext( |
| 777 | {*paint, ctm, nullptr, device.colorType(), device.colorSpace()}, |
| 778 | alloc); |
| 779 | |
| 780 | // Creating the context isn't always possible... try fallbacks before giving up. |
| 781 | if (!shaderContext) { |
| 782 | return create_SkRP_or_SkVMBlitter(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | switch (device.colorType()) { |
| 787 | case kN32_SkColorType: |
| 788 | if (shaderContext) { |
| 789 | return alloc->make<SkARGB32_Shader_Blitter>(device, *paint, shaderContext); |
| 790 | } else if (paint->getColor() == SK_ColorBLACK) { |
| 791 | return alloc->make<SkARGB32_Black_Blitter>(device, *paint); |
| 792 | } else if (paint->getAlpha() == 0xFF) { |
| 793 | return alloc->make<SkARGB32_Opaque_Blitter>(device, *paint); |
| 794 | } else { |
| 795 | return alloc->make<SkARGB32_Blitter>(device, *paint); |
| 796 | } |
| 797 | |
| 798 | case kRGB_565_SkColorType: |
| 799 | if (shaderContext && SkRGB565_Shader_Blitter::Supports(device, *paint)) { |
| 800 | return alloc->make<SkRGB565_Shader_Blitter>(device, *paint, shaderContext); |
| 801 | } else { |
| 802 | return create_SkRP_or_SkVMBlitter(); |
| 803 | } |
| 804 | |
| 805 | default: |
| 806 | SkASSERT(false); |
| 807 | return alloc->make<SkNullBlitter>(); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /////////////////////////////////////////////////////////////////////////////// |
| 812 | |
| 813 | SkShaderBlitter::SkShaderBlitter(const SkPixmap& device, const SkPaint& paint, |
| 814 | SkShaderBase::Context* shaderContext) |
| 815 | : INHERITED(device) |
| 816 | , fShader(paint.getShader()) |
| 817 | , fShaderContext(shaderContext) { |
| 818 | SkASSERT(fShader); |
| 819 | SkASSERT(fShaderContext); |
| 820 | |
| 821 | fShader->ref(); |
| 822 | fShaderFlags = fShaderContext->getFlags(); |
| 823 | fConstInY = SkToBool(fShaderFlags & SkShaderBase::kConstInY32_Flag); |
| 824 | } |
| 825 | |
| 826 | SkShaderBlitter::~SkShaderBlitter() { |
| 827 | fShader->unref(); |
| 828 | } |
| 829 | |
| 830 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 831 | |
| 832 | #ifdef SK_DEBUG |
| 833 | |
| 834 | void SkRectClipCheckBlitter::blitH(int x, int y, int width) { |
| 835 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1))); |
| 836 | fBlitter->blitH(x, y, width); |
| 837 | } |
| 838 | |
| 839 | void SkRectClipCheckBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) { |
| 840 | const int16_t* iter = runs; |
| 841 | for (; *iter; iter += *iter) |
| 842 | ; |
| 843 | int width = iter - runs; |
| 844 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1))); |
| 845 | fBlitter->blitAntiH(x, y, aa, runs); |
| 846 | } |
| 847 | |
| 848 | void SkRectClipCheckBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 849 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, height))); |
| 850 | fBlitter->blitV(x, y, height, alpha); |
| 851 | } |
| 852 | |
| 853 | void SkRectClipCheckBlitter::blitRect(int x, int y, int width, int height) { |
| 854 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, height))); |
| 855 | fBlitter->blitRect(x, y, width, height); |
| 856 | } |
| 857 | |
| 858 | void SkRectClipCheckBlitter::blitAntiRect(int x, int y, int width, int height, |
| 859 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 860 | bool skipLeft = !leftAlpha; |
| 861 | bool skipRight = !rightAlpha; |
| 862 | SkIRect r = SkIRect::MakeXYWH(x + skipLeft, y, width + 2 - skipRight - skipLeft, height); |
| 863 | SkASSERT(r.isEmpty() || fClipRect.contains(r)); |
| 864 | fBlitter->blitAntiRect(x, y, width, height, leftAlpha, rightAlpha); |
| 865 | } |
| 866 | |
| 867 | void SkRectClipCheckBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 868 | SkASSERT(mask.fBounds.contains(clip)); |
| 869 | SkASSERT(fClipRect.contains(clip)); |
| 870 | fBlitter->blitMask(mask, clip); |
| 871 | } |
| 872 | |
| 873 | const SkPixmap* SkRectClipCheckBlitter::justAnOpaqueColor(uint32_t* value) { |
| 874 | return fBlitter->justAnOpaqueColor(value); |
| 875 | } |
| 876 | |
| 877 | void SkRectClipCheckBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
| 878 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 2, 1))); |
| 879 | fBlitter->blitAntiH2(x, y, a0, a1); |
| 880 | } |
| 881 | |
| 882 | void SkRectClipCheckBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) { |
| 883 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, 2))); |
| 884 | fBlitter->blitAntiV2(x, y, a0, a1); |
| 885 | } |
| 886 | |
| 887 | #endif |
| 888 | |