Debian Codegraph @

src/core/SkBlitter.cpp

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