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