• 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/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 
~SkBlitter()33 SkBlitter::~SkBlitter() {}
34 
isNullBlitter() const35 bool SkBlitter::isNullBlitter() const { return false; }
36 
justAnOpaqueColor(uint32_t * value)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 
ScalarToAlpha(SkScalar a)53 inline static SkAlpha ScalarToAlpha(SkScalar a) {
54     SkAlpha alpha = (SkAlpha)(a * 255);
55     return alpha > 247 ? 0xFF : alpha < 8 ? 0 : alpha;
56 }
57 
blitFatAntiRect(const SkRect & rect)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 
blitV(int x,int y,int height,SkAlpha alpha)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 
blitRect(int x,int y,int width,int height)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.
blitAntiRect(int x,int y,int width,int height,SkAlpha leftAlpha,SkAlpha rightAlpha)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 
bits_to_runs(SkBlitter * blitter,int x,int y,const uint8_t bits[],uint8_t left_mask,ptrdiff_t rowBytes,uint8_t right_mask)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.
generate_right_mask(int maskBitCount)188 static uint8_t generate_right_mask(int maskBitCount) {
189     return static_cast<uint8_t>((0xFF00U >> maskBitCount) & 0xFF);
190 }
191 
blitMask(const SkMask & mask,const SkIRect & clip)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 
blitMaskRegion(const SkMask & mask,const SkRegion & clip)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 
blitRectRegion(const SkIRect & rect,const SkRegion & clip)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 
blitRegion(const SkRegion & clip)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 
blitH(int x,int y,int width)307 void SkNullBlitter::blitH(int x, int y, int width) {}
308 
blitAntiH(int x,int y,const SkAlpha antialias[],const int16_t runs[])309 void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
310                               const int16_t runs[]) {}
311 
blitV(int x,int y,int height,SkAlpha alpha)312 void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
313 
blitRect(int x,int y,int width,int height)314 void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
315 
blitMask(const SkMask & mask,const SkIRect & clip)316 void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
317 
justAnOpaqueColor(uint32_t * value)318 const SkPixmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
319     return nullptr;
320 }
321 
isNullBlitter() const322 bool SkNullBlitter::isNullBlitter() const { return true; }
323 
324 ///////////////////////////////////////////////////////////////////////////////
325 
compute_anti_width(const int16_t runs[])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 
y_in_rect(int y,const SkIRect & rect)342 static inline bool y_in_rect(int y, const SkIRect& rect) {
343     return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
344 }
345 
x_in_rect(int x,const SkIRect & rect)346 static inline bool x_in_rect(int x, const SkIRect& rect) {
347     return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
348 }
349 
blitH(int left,int y,int width)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 
blitAntiH(int left,int y,const SkAlpha aa[],const int16_t runs[])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 
blitV(int x,int y,int height,SkAlpha alpha)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 
blitRect(int left,int y,int width,int height)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 
blitAntiRect(int left,int y,int width,int height,SkAlpha leftAlpha,SkAlpha rightAlpha)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 
blitMask(const SkMask & mask,const SkIRect & clip)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 
justAnOpaqueColor(uint32_t * value)479 const SkPixmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
480     return fBlitter->justAnOpaqueColor(value);
481 }
482 
483 ///////////////////////////////////////////////////////////////////////////////
484 
blitH(int x,int y,int width)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 
blitAntiH(int x,int y,const SkAlpha aa[],const int16_t runs[])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 
blitV(int x,int y,int height,SkAlpha alpha)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 
blitRect(int x,int y,int width,int height)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 
blitAntiRect(int x,int y,int width,int height,SkAlpha leftAlpha,SkAlpha rightAlpha)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 
blitMask(const SkMask & mask,const SkIRect & clip)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 
justAnOpaqueColor(uint32_t * value)615 const SkPixmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
616     return fBlitter->justAnOpaqueColor(value);
617 }
618 
619 ///////////////////////////////////////////////////////////////////////////////
620 
apply(SkBlitter * blitter,const SkRegion * clip,const SkIRect * ir)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 
UseRasterPipelineBlitter(const SkPixmap & device,const SkPaint & paint,const SkMatrix & matrix)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 #if !defined(SK_SUPPORT_LEGACY_DITHER)
655     if (paint.isDither()) {
656         return true;
657     }
658 #endif
659 
660     const SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
661 
662     // The legacy blitters cannot handle any of these complex features (anymore).
663     if (device.alphaType() == kUnpremul_SkAlphaType        ||
664         paint.getBlendMode() > SkBlendMode::kLastCoeffMode ||
665         (mf && mf->getFormat() == SkMask::k3D_Format)) {
666         return true;
667     }
668 
669     // All the real legacy fast paths are for shaders and SrcOver.
670     // Choosing SkRasterPipelineBlitter will also let us to hit its single-color memset path.
671     if (!paint.getShader() && paint.getBlendMode() != SkBlendMode::kSrcOver) {
672         return true;
673     }
674 
675     auto cs = device.colorSpace();
676     // We check (indirectly via makeContext()) later on if the shader can handle the colorspace
677     // in legacy mode, so here we just focus on if a single color needs raster-pipeline.
678     if (cs && !paint.getShader()) {
679         if (!paint.getColor4f().fitsInBytes() || !cs->isSRGB()) {
680             return true;
681         }
682     }
683 
684     // Only kN32 and 565 are handled by legacy blitters now, 565 mostly just for Android.
685     return device.colorType() != kN32_SkColorType
686         && device.colorType() != kRGB_565_SkColorType;
687 #endif
688 }
689 
Choose(const SkPixmap & device,const SkMatrixProvider & matrixProvider,const SkPaint & origPaint,SkArenaAlloc * alloc,bool drawCoverage,sk_sp<SkShader> clipShader)690 SkBlitter* SkBlitter::Choose(const SkPixmap& device,
691                              const SkMatrixProvider& matrixProvider,
692                              const SkPaint& origPaint,
693                              SkArenaAlloc* alloc,
694                              bool drawCoverage,
695                              sk_sp<SkShader> clipShader) {
696     SkASSERT(alloc);
697 
698     if (kUnknown_SkColorType == device.colorType()) {
699         return alloc->make<SkNullBlitter>();
700     }
701 
702     // We may tweak the original paint as we go.
703     SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
704 
705     // We have the most fast-paths for SrcOver, so see if we can act like SrcOver.
706     if (paint->getBlendMode() != SkBlendMode::kSrcOver) {
707         switch (SkInterpretXfermode(*paint, SkColorTypeIsAlwaysOpaque(device.colorType()))) {
708             case kSrcOver_SkXfermodeInterpretation:
709                 paint.writable()->setBlendMode(SkBlendMode::kSrcOver);
710                 break;
711             case kSkipDrawing_SkXfermodeInterpretation:
712                 return alloc->make<SkNullBlitter>();
713             default:
714                 break;
715         }
716     }
717 
718     // A Clear blend mode will ignore the entire color pipeline, as if Src mode with 0x00000000.
719     if (paint->getBlendMode() == SkBlendMode::kClear) {
720         SkPaint* p = paint.writable();
721         p->setShader(nullptr);
722         p->setColorFilter(nullptr);
723         p->setBlendMode(SkBlendMode::kSrc);
724         p->setColor(0x00000000);
725     }
726 
727     if (paint->getColorFilter()) {
728         SkPaintPriv::RemoveColorFilter(paint.writable(), device.colorSpace());
729     }
730     SkASSERT(!paint->getColorFilter());
731 
732     if (drawCoverage) {
733         if (device.colorType() == kAlpha_8_SkColorType) {
734             SkASSERT(!paint->getShader());
735             SkASSERT(paint->isSrcOver());
736             return alloc->make<SkA8_Coverage_Blitter>(device, *paint);
737         }
738         return alloc->make<SkNullBlitter>();
739     }
740 
741     if (paint->isDither() && !SkPaintPriv::ShouldDither(*paint, device.colorType())) {
742         paint.writable()->setDither(false);
743     }
744 
745     if (gUseSkVMBlitter) {
746         if (auto blitter = SkCreateSkVMBlitter(device, *paint, matrixProvider,
747                                                alloc, clipShader)) {
748             return blitter;
749         }
750     }
751 
752     // Same basic idea used a few times: try SkRP, then try SkVM, then give up with a null-blitter.
753     // (Setting gUseSkVMBlitter is the only way we prefer SkVM over SkRP at the moment.)
754     auto create_SkRP_or_SkVMBlitter = [&]() -> SkBlitter* {
755         if (auto blitter = SkCreateRasterPipelineBlitter(device, *paint, matrixProvider,
756                                                          alloc, clipShader)) {
757             return blitter;
758         }
759         if (auto blitter = SkCreateSkVMBlitter(device, *paint, matrixProvider,
760                                                alloc, clipShader)) {
761             return blitter;
762         }
763         return alloc->make<SkNullBlitter>();
764     };
765 
766     SkMatrix ctm = matrixProvider.localToDevice();
767     // We'll end here for many interesting cases: color spaces, color filters, most color types.
768     if (UseRasterPipelineBlitter(device, *paint, ctm) || clipShader) {
769         return create_SkRP_or_SkVMBlitter();
770     }
771 
772     // Everything but legacy kN32_SkColorType and kRGB_565_SkColorType should already be handled.
773     SkASSERT(device.colorType() == kN32_SkColorType ||
774              device.colorType() == kRGB_565_SkColorType);
775 
776     // And we should either have a shader, be blending with SrcOver, or both.
777     SkASSERT(paint->getShader() || paint->getBlendMode() == SkBlendMode::kSrcOver);
778 
779     // Legacy blitters keep their shader state on a shader context.
780     SkShaderBase::Context* shaderContext = nullptr;
781     if (paint->getShader()) {
782         shaderContext = as_SB(paint->getShader())->makeContext(
783                 {*paint, ctm, nullptr, device.colorType(), device.colorSpace()},
784                 alloc);
785 
786         // Creating the context isn't always possible... try fallbacks before giving up.
787         if (!shaderContext) {
788             return create_SkRP_or_SkVMBlitter();
789         }
790     }
791 
792     switch (device.colorType()) {
793         case kN32_SkColorType:
794             if (shaderContext) {
795                 return alloc->make<SkARGB32_Shader_Blitter>(device, *paint, shaderContext);
796             } else if (paint->getColor() == SK_ColorBLACK) {
797                 return alloc->make<SkARGB32_Black_Blitter>(device, *paint);
798             } else if (paint->getAlpha() == 0xFF) {
799                 return alloc->make<SkARGB32_Opaque_Blitter>(device, *paint);
800             } else {
801                 return alloc->make<SkARGB32_Blitter>(device, *paint);
802             }
803 
804         case kRGB_565_SkColorType:
805             if (shaderContext && SkRGB565_Shader_Blitter::Supports(device, *paint)) {
806                 return alloc->make<SkRGB565_Shader_Blitter>(device, *paint, shaderContext);
807             } else {
808                 return create_SkRP_or_SkVMBlitter();
809             }
810 
811         default:
812             SkASSERT(false);
813             return alloc->make<SkNullBlitter>();
814     }
815 }
816 
817 ///////////////////////////////////////////////////////////////////////////////
818 
SkShaderBlitter(const SkPixmap & device,const SkPaint & paint,SkShaderBase::Context * shaderContext)819 SkShaderBlitter::SkShaderBlitter(const SkPixmap& device, const SkPaint& paint,
820                                  SkShaderBase::Context* shaderContext)
821         : INHERITED(device)
822         , fShader(paint.getShader())
823         , fShaderContext(shaderContext) {
824     SkASSERT(fShader);
825     SkASSERT(fShaderContext);
826 
827     fShader->ref();
828     fShaderFlags = fShaderContext->getFlags();
829     fConstInY = SkToBool(fShaderFlags & SkShaderBase::kConstInY32_Flag);
830 }
831 
~SkShaderBlitter()832 SkShaderBlitter::~SkShaderBlitter() {
833     fShader->unref();
834 }
835 
836 ///////////////////////////////////////////////////////////////////////////////////////////////////
837 
838 #ifdef SK_DEBUG
839 
blitH(int x,int y,int width)840 void SkRectClipCheckBlitter::blitH(int x, int y, int width) {
841     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1)));
842     fBlitter->blitH(x, y, width);
843 }
844 
blitAntiH(int x,int y,const SkAlpha aa[],const int16_t runs[])845 void SkRectClipCheckBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
846     const int16_t* iter = runs;
847     for (; *iter; iter += *iter)
848         ;
849     int width = iter - runs;
850     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1)));
851     fBlitter->blitAntiH(x, y, aa, runs);
852 }
853 
blitV(int x,int y,int height,SkAlpha alpha)854 void SkRectClipCheckBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
855     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, height)));
856     fBlitter->blitV(x, y, height, alpha);
857 }
858 
blitRect(int x,int y,int width,int height)859 void SkRectClipCheckBlitter::blitRect(int x, int y, int width, int height) {
860     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, height)));
861     fBlitter->blitRect(x, y, width, height);
862 }
863 
blitAntiRect(int x,int y,int width,int height,SkAlpha leftAlpha,SkAlpha rightAlpha)864 void SkRectClipCheckBlitter::blitAntiRect(int x, int y, int width, int height,
865                                      SkAlpha leftAlpha, SkAlpha rightAlpha) {
866     bool skipLeft = !leftAlpha;
867     bool skipRight = !rightAlpha;
868     SkIRect r = SkIRect::MakeXYWH(x + skipLeft, y, width + 2 - skipRight - skipLeft, height);
869     SkASSERT(r.isEmpty() || fClipRect.contains(r));
870     fBlitter->blitAntiRect(x, y, width, height, leftAlpha, rightAlpha);
871 }
872 
blitMask(const SkMask & mask,const SkIRect & clip)873 void SkRectClipCheckBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
874     SkASSERT(mask.fBounds.contains(clip));
875     SkASSERT(fClipRect.contains(clip));
876     fBlitter->blitMask(mask, clip);
877 }
878 
justAnOpaqueColor(uint32_t * value)879 const SkPixmap* SkRectClipCheckBlitter::justAnOpaqueColor(uint32_t* value) {
880     return fBlitter->justAnOpaqueColor(value);
881 }
882 
blitAntiH2(int x,int y,U8CPU a0,U8CPU a1)883 void SkRectClipCheckBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
884     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 2, 1)));
885     fBlitter->blitAntiH2(x, y, a0, a1);
886 }
887 
blitAntiV2(int x,int y,U8CPU a0,U8CPU a1)888 void SkRectClipCheckBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
889     SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, 2)));
890     fBlitter->blitAntiV2(x, y, a0, a1);
891 }
892 
893 #endif
894