3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkScanPriv.h"
13 #include "SkBlitter.h"
15 #include "SkAntiRun.h"
18 #define SCALE (1 << SHIFT)
19 #define MASK (SCALE - 1)
22 We have two techniques for capturing the output of the supersampler:
23 - SUPERMASK, which records a large mask-bitmap
24 this is often faster for small, complex objects
25 - RLE, which records a rle-encoded scanline
26 this is often faster for large objects with big spans
28 These blitters use two coordinate systems:
29 - destination coordinates, scale equal to the output - often
30 abbreviated with 'i' or 'I' in variable names
31 - supersampled coordinates, scale equal to the output * SCALE
33 Enabling SK_USE_LEGACY_AA_COVERAGE keeps the aa coverage calculations as
34 they were before the fix that unified the output of the RLE and MASK
38 //#define FORCE_SUPERMASK
40 //#define SK_USE_LEGACY_AA_COVERAGE
42 ///////////////////////////////////////////////////////////////////////////////
44 /// Base class for a single-pass supersampled blitter.
45 class BaseSuperBlitter : public SkBlitter {
47 BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
48 const SkRegion& clip, bool isInverse);
50 /// Must be explicitly defined on subclasses.
51 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
52 const int16_t runs[]) SK_OVERRIDE {
53 SkDEBUGFAIL("How did I get here?");
55 /// May not be called on BaseSuperBlitter because it blits out of order.
56 virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE {
57 SkDEBUGFAIL("How did I get here?");
61 SkBlitter* fRealBlitter;
62 /// Current y coordinate, in destination coordinates.
64 /// Widest row of region to be blitted, in destination coordinates.
66 /// Leftmost x coordinate in any row, in destination coordinates.
68 /// Leftmost x coordinate in any row, in supersampled coordinates.
71 SkDEBUGCODE(int fCurrX;)
72 /// Current y coordinate in supersampled coordinates.
74 /// Initial y coordinate (top of bounds).
80 BaseSuperBlitter::BaseSuperBlitter(SkBlitter* realBlit, const SkIRect& ir, const SkRegion& clip,
82 fRealBlitter = realBlit;
86 // We use the clip bounds instead of the ir, since we may be asked to
87 //draw outside of the rect when we're a inverse filltype
88 sectBounds = clip.getBounds();
90 if (!sectBounds.intersect(ir, clip.getBounds())) {
91 sectBounds.setEmpty();
95 const int left = sectBounds.left();
96 const int right = sectBounds.right();
99 fSuperLeft = left << SHIFT;
100 fWidth = right - left;
101 fTop = sectBounds.top();
103 fCurrY = (fTop << SHIFT) - 1;
105 SkDEBUGCODE(fCurrX = -1;)
108 /// Run-length-encoded supersampling antialiased blitter.
109 class SuperBlitter : public BaseSuperBlitter {
111 SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip, bool isInverse);
113 virtual ~SuperBlitter() {
117 /// Once fRuns contains a complete supersampled row, flush() blits
118 /// it out through the wrapped blitter.
121 /// Blits a row of pixels, with location and width specified
122 /// in supersampled coordinates.
123 virtual void blitH(int x, int y, int width) SK_OVERRIDE;
124 /// Blits a rectangle of pixels, with location and size specified
125 /// in supersampled coordinates.
126 virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
129 // The next three variables are used to track a circular buffer that
130 // contains the values used in SkAlphaRuns. These variables should only
131 // ever be updated in advanceRuns(), and fRuns should always point to
132 // a valid SkAlphaRuns...
138 // extra one to store the zero at the end
139 int getRunsSz() const { return (fWidth + 1 + (fWidth + 2)/2) * sizeof(int16_t); }
141 // This function updates the fRuns variable to point to the next buffer space
142 // with adequate storage for a SkAlphaRuns. It mostly just advances fCurrentRun
143 // and resets fRuns to point to an empty scanline.
145 const size_t kRunsSz = this->getRunsSz();
146 fCurrentRun = (fCurrentRun + 1) % fRunsToBuffer;
147 fRuns.fRuns = reinterpret_cast<int16_t*>(
148 reinterpret_cast<uint8_t*>(fRunsBuffer) + fCurrentRun * kRunsSz);
149 fRuns.fAlpha = reinterpret_cast<SkAlpha*>(fRuns.fRuns + fWidth + 1);
156 SuperBlitter::SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip,
158 : BaseSuperBlitter(realBlitter, ir, clip, isInverse)
160 fRunsToBuffer = realBlitter->requestRowsPreserved();
161 fRunsBuffer = realBlitter->allocBlitMemory(fRunsToBuffer * this->getRunsSz());
169 void SuperBlitter::flush() {
170 if (fCurrIY >= fTop) {
172 SkASSERT(fCurrentRun < fRunsToBuffer);
173 if (!fRuns.empty()) {
174 // SkDEBUGCODE(fRuns.dump();)
175 fRealBlitter->blitAntiH(fLeft, fCurrIY, fRuns.fAlpha, fRuns.fRuns);
181 SkDEBUGCODE(fCurrX = -1;)
185 /** coverage_to_partial_alpha() is being used by SkAlphaRuns, which
186 *accumulates* SCALE pixels worth of "alpha" in [0,(256/SCALE)]
187 to produce a final value in [0, 255] and handles clamping 256->255
188 itself, with the same (alpha - (alpha >> 8)) correction as
189 coverage_to_exact_alpha().
191 static inline int coverage_to_partial_alpha(int aa) {
193 #ifdef SK_USE_LEGACY_AA_COVERAGE
194 aa -= aa >> (8 - SHIFT - 1);
199 /** coverage_to_exact_alpha() is being used by our blitter, which wants
200 a final value in [0, 255].
202 static inline int coverage_to_exact_alpha(int aa) {
203 int alpha = (256 >> SHIFT) * aa;
205 return alpha - (alpha >> 8);
208 void SuperBlitter::blitH(int x, int y, int width) {
212 SkASSERT(iy >= fCurrIY);
215 // hack, until I figure out why my cubics (I think) go beyond the bounds
222 SkASSERT(y != fCurrY || x >= fCurrX);
224 SkASSERT(y >= fCurrY);
230 if (iy != fCurrIY) { // new scanline
236 int stop = x + width;
238 SkASSERT(start >= 0 && stop > start);
239 // integer-pixel-aligned ends of blit, rounded out
240 int fb = start & MASK;
241 int fe = stop & MASK;
242 int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
256 fOffsetX = fRuns.add(x >> SHIFT, coverage_to_partial_alpha(fb),
257 n, coverage_to_partial_alpha(fe),
258 (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT),
262 fRuns.assertValid(y & MASK, (1 << (8 - SHIFT)));
268 static void set_left_rite_runs(SkAlphaRuns& runs, int ileft, U8CPU leftA,
269 int n, U8CPU riteA) {
270 SkASSERT(leftA <= 0xFF);
271 SkASSERT(riteA <= 0xFF);
273 int16_t* run = runs.fRuns;
274 uint8_t* aa = runs.fAlpha;
283 SkASSERT(leftA < 0xFF);
296 SkASSERT(riteA < 0xFF);
305 void SuperBlitter::blitRect(int x, int y, int width, int height) {
307 SkASSERT(height > 0);
311 this->blitH(x, y++, width);
316 SkASSERT(height > 0);
318 // Since this is a rect, instead of blitting supersampled rows one at a
319 // time and then resolving to the destination canvas, we can blit
320 // directly to the destintion canvas one row per SCALE supersampled rows.
321 int start_y = y >> SHIFT;
322 int stop_y = (y + height) >> SHIFT;
323 int count = stop_y - start_y;
326 height -= count << SHIFT;
328 // save original X for our tail blitH() loop at the bottom
332 // hack, until I figure out why my cubics (I think) go beyond the bounds
338 // There is always a left column, a middle, and a right column.
339 // ileft is the destination x of the first pixel of the entire rect.
340 // xleft is (SCALE - # of covered supersampled pixels) in that
341 // destination pixel.
342 int ileft = x >> SHIFT;
343 int xleft = x & MASK;
344 // irite is the destination x of the last pixel of the OPAQUE section.
345 // xrite is the number of supersampled pixels extending beyond irite;
346 // xrite/SCALE should give us alpha.
347 int irite = (x + width) >> SHIFT;
348 int xrite = (x + width) & MASK;
354 // Need to call flush() to clean up pending draws before we
355 // even consider blitV(), since otherwise it can look nonmonotonic.
356 SkASSERT(start_y > fCurrIY);
359 int n = irite - ileft - 1;
361 // If n < 0, we'll only have a single partially-transparent column
362 // of pixels to render.
363 xleft = xrite - xleft;
364 SkASSERT(xleft <= SCALE);
367 fRealBlitter->blitV(ileft + fLeft, start_y, count,
368 coverage_to_exact_alpha(xleft));
370 // With n = 0, we have two possibly-transparent columns of pixels
371 // to render; with n > 0, we have opaque columns between them.
373 xleft = SCALE - xleft;
375 // Using coverage_to_exact_alpha is not consistent with blitH()
376 const int coverageL = coverage_to_exact_alpha(xleft);
377 const int coverageR = coverage_to_exact_alpha(xrite);
379 SkASSERT(coverageL > 0 || n > 0 || coverageR > 0);
380 SkASSERT((coverageL != 0) + n + (coverageR != 0) <= fWidth);
382 fRealBlitter->blitAntiRect(ileft + fLeft, start_y, n, count,
383 coverageL, coverageR);
386 // preamble for our next call to blitH()
387 fCurrIY = stop_y - 1;
394 // catch any remaining few rows
395 SkASSERT(height <= MASK);
396 while (--height >= 0) {
397 this->blitH(x, y++, width);
401 ///////////////////////////////////////////////////////////////////////////////
403 /// Masked supersampling antialiased blitter.
404 class MaskSuperBlitter : public BaseSuperBlitter {
406 MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion&, bool isInverse);
407 virtual ~MaskSuperBlitter() {
408 fRealBlitter->blitMask(fMask, fClipRect);
411 virtual void blitH(int x, int y, int width) SK_OVERRIDE;
413 static bool CanHandleRect(const SkIRect& bounds) {
417 int width = bounds.width();
418 int64_t rb = SkAlign4(width);
419 // use 64bits to detect overflow
420 int64_t storage = rb * bounds.height();
422 return (width <= MaskSuperBlitter::kMAX_WIDTH) &&
423 (storage <= MaskSuperBlitter::kMAX_STORAGE);
428 #ifdef FORCE_SUPERMASK
430 kMAX_STORAGE = 1024 * 1024 * 2
432 kMAX_WIDTH = 32, // so we don't try to do very wide things, where the RLE blitter would be faster
439 // we add 1 because add_aa_span can write (unchanged) 1 extra byte at the end, rather than
440 // perform a test to see if stopAlpha != 0
441 uint32_t fStorage[(kMAX_STORAGE >> 2) + 1];
444 MaskSuperBlitter::MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip,
446 : BaseSuperBlitter(realBlitter, ir, clip, isInverse)
448 SkASSERT(CanHandleRect(ir));
449 SkASSERT(!isInverse);
451 fMask.fImage = (uint8_t*)fStorage;
453 fMask.fRowBytes = ir.width();
454 fMask.fFormat = SkMask::kA8_Format;
457 fClipRect.intersect(clip.getBounds());
459 // For valgrind, write 1 extra byte at the end so we don't read
460 // uninitialized memory. See comment in add_aa_span and fStorage[].
461 memset(fStorage, 0, fMask.fBounds.height() * fMask.fRowBytes + 1);
464 static void add_aa_span(uint8_t* alpha, U8CPU startAlpha) {
465 /* I should be able to just add alpha[x] + startAlpha.
466 However, if the trailing edge of the previous span and the leading
467 edge of the current span round to the same super-sampled x value,
468 I might overflow to 256 with this add, hence the funny subtract.
470 unsigned tmp = *alpha + startAlpha;
471 SkASSERT(tmp <= 256);
472 *alpha = SkToU8(tmp - (tmp >> 8));
475 static inline uint32_t quadplicate_byte(U8CPU value) {
476 uint32_t pair = (value << 8) | value;
477 return (pair << 16) | pair;
480 // Perform this tricky subtract, to avoid overflowing to 256. Our caller should
481 // only ever call us with at most enough to hit 256 (never larger), so it is
482 // enough to just subtract the high-bit. Actually clamping with a branch would
483 // be slower (e.g. if (tmp > 255) tmp = 255;)
485 static inline void saturated_add(uint8_t* ptr, U8CPU add) {
486 unsigned tmp = *ptr + add;
487 SkASSERT(tmp <= 256);
488 *ptr = SkToU8(tmp - (tmp >> 8));
491 // minimum count before we want to setup an inner loop, adding 4-at-a-time
492 #define MIN_COUNT_FOR_QUAD_LOOP 16
494 static void add_aa_span(uint8_t* alpha, U8CPU startAlpha, int middleCount,
495 U8CPU stopAlpha, U8CPU maxValue) {
496 SkASSERT(middleCount >= 0);
498 saturated_add(alpha, startAlpha);
501 if (middleCount >= MIN_COUNT_FOR_QUAD_LOOP) {
502 // loop until we're quad-byte aligned
503 while (SkTCast<intptr_t>(alpha) & 0x3) {
504 alpha[0] = SkToU8(alpha[0] + maxValue);
509 int bigCount = middleCount >> 2;
510 uint32_t* qptr = reinterpret_cast<uint32_t*>(alpha);
511 uint32_t qval = quadplicate_byte(maxValue);
514 } while (--bigCount > 0);
517 alpha = reinterpret_cast<uint8_t*> (qptr);
518 // fall through to the following while-loop
521 while (--middleCount >= 0) {
522 alpha[0] = SkToU8(alpha[0] + maxValue);
526 // potentially this can be off the end of our "legal" alpha values, but that
527 // only happens if stopAlpha is also 0. Rather than test for stopAlpha != 0
528 // every time (slow), we just do it, and ensure that we've allocated extra space
529 // (see the + 1 comment in fStorage[]
530 saturated_add(alpha, stopAlpha);
533 void MaskSuperBlitter::blitH(int x, int y, int width) {
534 int iy = (y >> SHIFT);
536 SkASSERT(iy >= fMask.fBounds.fTop && iy < fMask.fBounds.fBottom);
537 iy -= fMask.fBounds.fTop; // make it relative to 0
539 // This should never happen, but it does. Until the true cause is
540 // discovered, let's skip this span instead of crashing.
541 // See http://crbug.com/17569.
549 SkASSERT(ix >= fMask.fBounds.fLeft && ix < fMask.fBounds.fRight);
553 x -= (fMask.fBounds.fLeft << SHIFT);
555 // hack, until I figure out why my cubics (I think) go beyond the bounds
561 uint8_t* row = fMask.fImage + iy * fMask.fRowBytes + (x >> SHIFT);
564 int stop = x + width;
566 SkASSERT(start >= 0 && stop > start);
567 int fb = start & MASK;
568 int fe = stop & MASK;
569 int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
573 SkASSERT(row >= fMask.fImage);
574 SkASSERT(row < fMask.fImage + kMAX_STORAGE + 1);
575 add_aa_span(row, coverage_to_partial_alpha(fe - fb));
578 SkASSERT(row >= fMask.fImage);
579 SkASSERT(row + n + 1 < fMask.fImage + kMAX_STORAGE + 1);
580 add_aa_span(row, coverage_to_partial_alpha(fb),
581 n, coverage_to_partial_alpha(fe),
582 (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT));
590 ///////////////////////////////////////////////////////////////////////////////
592 static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
593 const SkScalar min = -max;
594 return r.fLeft > min && r.fTop > min &&
595 r.fRight < max && r.fBottom < max;
598 static int overflows_short_shift(int value, int shift) {
599 const int s = 16 + shift;
600 return (value << s >> s) - value;
604 Would any of the coordinates of this rectangle not fit in a short,
605 when left-shifted by shift?
607 static int rect_overflows_short_shift(SkIRect rect, int shift) {
608 SkASSERT(!overflows_short_shift(8191, SHIFT));
609 SkASSERT(overflows_short_shift(8192, SHIFT));
610 SkASSERT(!overflows_short_shift(32767, 0));
611 SkASSERT(overflows_short_shift(32768, 0));
613 // Since we expect these to succeed, we bit-or together
614 // for a tiny extra bit of speed.
615 return overflows_short_shift(rect.fLeft, SHIFT) |
616 overflows_short_shift(rect.fRight, SHIFT) |
617 overflows_short_shift(rect.fTop, SHIFT) |
618 overflows_short_shift(rect.fBottom, SHIFT);
621 static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
622 const SkScalar maxScalar = SkIntToScalar(maxInt);
624 if (fitsInsideLimit(src, maxScalar)) {
631 void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip,
632 SkBlitter* blitter, bool forceRLE) {
633 if (origClip.isEmpty()) {
637 const bool isInverse = path.isInverseFillType();
640 if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
642 const SkRect& r = path.getBounds();
643 SkDebugf("--- bounds can't fit in SkIRect\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
649 blitter->blitRegion(origClip);
654 // If the intersection of the path bounds and the clip bounds
655 // will overflow 32767 when << by SHIFT, we can't supersample,
656 // so draw without antialiasing.
659 // If the path is an inverse fill, it's going to fill the entire
660 // clip, and we care whether the entire clip exceeds our limits.
661 clippedIR = origClip.getBounds();
663 if (!clippedIR.intersect(ir, origClip.getBounds())) {
667 if (rect_overflows_short_shift(clippedIR, SHIFT)) {
668 SkScan::FillPath(path, origClip, blitter);
672 // Our antialiasing can't handle a clip larger than 32767, so we restrict
673 // the clip to that limit here. (the runs[] uses int16_t for its index).
675 // A more general solution (one that could also eliminate the need to
676 // disable aa based on ir bounds (see overflows_short_shift) would be
677 // to tile the clip/target...
678 SkRegion tmpClipStorage;
679 const SkRegion* clipRgn = &origClip;
681 static const int32_t kMaxClipCoord = 32767;
682 const SkIRect& bounds = origClip.getBounds();
683 if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
684 SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
685 tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
686 clipRgn = &tmpClipStorage;
689 // for here down, use clipRgn, not origClip
691 SkScanClipper clipper(blitter, clipRgn, ir);
692 const SkIRect* clipRect = clipper.getClipRect();
694 if (clipper.getBlitter() == NULL) { // clipped out
696 blitter->blitRegion(*clipRgn);
701 // now use the (possibly wrapped) blitter
702 blitter = clipper.getBlitter();
705 sk_blit_above(blitter, ir, *clipRgn);
708 SkIRect superRect, *superClipRect = NULL;
711 superRect.set( clipRect->fLeft << SHIFT, clipRect->fTop << SHIFT,
712 clipRect->fRight << SHIFT, clipRect->fBottom << SHIFT);
713 superClipRect = &superRect;
716 SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
718 // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it
719 // if we're an inverse filltype
720 if (!isInverse && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) {
721 MaskSuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
722 SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
723 sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
725 SuperBlitter superBlit(blitter, ir, *clipRgn, isInverse);
726 sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
730 sk_blit_below(blitter, ir, *clipRgn);
734 ///////////////////////////////////////////////////////////////////////////////
736 #include "SkRasterClip.h"
738 void SkScan::FillPath(const SkPath& path, const SkRasterClip& clip,
739 SkBlitter* blitter) {
740 if (clip.isEmpty()) {
745 FillPath(path, clip.bwRgn(), blitter);
748 SkAAClipBlitter aaBlitter;
750 tmp.setRect(clip.getBounds());
751 aaBlitter.init(blitter, &clip.aaRgn());
752 SkScan::FillPath(path, tmp, &aaBlitter);
756 void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip,
757 SkBlitter* blitter) {
758 if (clip.isEmpty()) {
763 AntiFillPath(path, clip.bwRgn(), blitter);
766 SkAAClipBlitter aaBlitter;
768 tmp.setRect(clip.getBounds());
769 aaBlitter.init(blitter, &clip.aaRgn());
770 SkScan::AntiFillPath(path, tmp, &aaBlitter, true);