Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #include "SkScanPriv.h"
     11 #include "SkPath.h"
     12 #include "SkMatrix.h"
     13 #include "SkBlitter.h"
     14 #include "SkRegion.h"
     15 #include "SkAntiRun.h"
     16 
     17 #define SHIFT   2
     18 #define SCALE   (1 << SHIFT)
     19 #define MASK    (SCALE - 1)
     20 
     21 /** @file
     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
     27 
     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
     32 
     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
     35     supersamplers.
     36  */
     37 
     38 //#define FORCE_SUPERMASK
     39 //#define FORCE_RLE
     40 //#define SK_USE_LEGACY_AA_COVERAGE
     41 
     42 ///////////////////////////////////////////////////////////////////////////////
     43 
     44 /// Base class for a single-pass supersampled blitter.
     45 class BaseSuperBlitter : public SkBlitter {
     46 public:
     47     BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
     48                      const SkRegion& clip);
     49 
     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?");
     54     }
     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?");
     58     }
     59 
     60 protected:
     61     SkBlitter*  fRealBlitter;
     62     /// Current y coordinate, in destination coordinates.
     63     int         fCurrIY;
     64     /// Widest row of region to be blitted, in destination coordinates.
     65     int         fWidth;
     66     /// Leftmost x coordinate in any row, in destination coordinates.
     67     int         fLeft;
     68     /// Leftmost x coordinate in any row, in supersampled coordinates.
     69     int         fSuperLeft;
     70 
     71     SkDEBUGCODE(int fCurrX;)
     72     /// Current y coordinate in supersampled coordinates.
     73     int fCurrY;
     74     /// Initial y coordinate (top of bounds).
     75     int fTop;
     76 };
     77 
     78 BaseSuperBlitter::BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
     79                                    const SkRegion& clip) {
     80     fRealBlitter = realBlitter;
     81 
     82     /*
     83      *  We use the clip bounds instead of the ir, since we may be asked to
     84      *  draw outside of the rect if we're a inverse filltype
     85      */
     86     const int left = clip.getBounds().fLeft;
     87     const int right = clip.getBounds().fRight;
     88 
     89     fLeft = left;
     90     fSuperLeft = left << SHIFT;
     91     fWidth = right - left;
     92 #if 0
     93     fCurrIY = -1;
     94     fCurrY = -1;
     95 #else
     96     fTop = ir.fTop;
     97     fCurrIY = ir.fTop - 1;
     98     fCurrY = (ir.fTop << SHIFT) - 1;
     99 #endif
    100     SkDEBUGCODE(fCurrX = -1;)
    101 }
    102 
    103 /// Run-length-encoded supersampling antialiased blitter.
    104 class SuperBlitter : public BaseSuperBlitter {
    105 public:
    106     SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
    107                  const SkRegion& clip);
    108 
    109     virtual ~SuperBlitter() {
    110         this->flush();
    111     }
    112 
    113     /// Once fRuns contains a complete supersampled row, flush() blits
    114     /// it out through the wrapped blitter.
    115     void flush();
    116 
    117     /// Blits a row of pixels, with location and width specified
    118     /// in supersampled coordinates.
    119     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    120     /// Blits a rectangle of pixels, with location and size specified
    121     /// in supersampled coordinates.
    122     virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
    123 
    124 private:
    125     // The next three variables are used to track a circular buffer that
    126     // contains the values used in SkAlphaRuns. These variables should only
    127     // ever be updated in advanceRuns(), and fRuns should always point to
    128     // a valid SkAlphaRuns...
    129     int         fRunsToBuffer;
    130     void*       fRunsBuffer;
    131     int         fCurrentRun;
    132     SkAlphaRuns fRuns;
    133 
    134     // extra one to store the zero at the end
    135     int getRunsSz() const { return (fWidth + 1 + (fWidth + 2)/2) * sizeof(int16_t); }
    136 
    137     // This function updates the fRuns variable to point to the next buffer space
    138     // with adequate storage for a SkAlphaRuns. It mostly just advances fCurrentRun
    139     // and resets fRuns to point to an empty scanline.
    140     void advanceRuns() {
    141         const size_t kRunsSz = this->getRunsSz();
    142         fCurrentRun = (fCurrentRun + 1) % fRunsToBuffer;
    143         fRuns.fRuns = reinterpret_cast<int16_t*>(
    144             reinterpret_cast<uint8_t*>(fRunsBuffer) + fCurrentRun * kRunsSz);
    145         fRuns.fAlpha = reinterpret_cast<SkAlpha*>(fRuns.fRuns + fWidth + 1);
    146         fRuns.reset(fWidth);
    147     }
    148 
    149     int         fOffsetX;
    150 };
    151 
    152 SuperBlitter::SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
    153                            const SkRegion& clip)
    154         : BaseSuperBlitter(realBlitter, ir, clip) {
    155     fRunsToBuffer = realBlitter->requestRowsPreserved();
    156     fRunsBuffer = realBlitter->allocBlitMemory(fRunsToBuffer * this->getRunsSz());
    157     fCurrentRun = -1;
    158 
    159     this->advanceRuns();
    160 
    161     fOffsetX = 0;
    162 }
    163 
    164 void SuperBlitter::flush() {
    165     if (fCurrIY >= fTop) {
    166 
    167         SkASSERT(fCurrentRun < fRunsToBuffer);
    168         if (!fRuns.empty()) {
    169             // SkDEBUGCODE(fRuns.dump();)
    170             fRealBlitter->blitAntiH(fLeft, fCurrIY, fRuns.fAlpha, fRuns.fRuns);
    171             this->advanceRuns();
    172             fOffsetX = 0;
    173         }
    174 
    175         fCurrIY = fTop - 1;
    176         SkDEBUGCODE(fCurrX = -1;)
    177     }
    178 }
    179 
    180 /** coverage_to_partial_alpha() is being used by SkAlphaRuns, which
    181     *accumulates* SCALE pixels worth of "alpha" in [0,(256/SCALE)]
    182     to produce a final value in [0, 255] and handles clamping 256->255
    183     itself, with the same (alpha - (alpha >> 8)) correction as
    184     coverage_to_exact_alpha().
    185 */
    186 static inline int coverage_to_partial_alpha(int aa) {
    187     aa <<= 8 - 2*SHIFT;
    188 #ifdef SK_USE_LEGACY_AA_COVERAGE
    189     aa -= aa >> (8 - SHIFT - 1);
    190 #endif
    191     return aa;
    192 }
    193 
    194 /** coverage_to_exact_alpha() is being used by our blitter, which wants
    195     a final value in [0, 255].
    196 */
    197 static inline int coverage_to_exact_alpha(int aa) {
    198     int alpha = (256 >> SHIFT) * aa;
    199     // clamp 256->255
    200     return alpha - (alpha >> 8);
    201 }
    202 
    203 void SuperBlitter::blitH(int x, int y, int width) {
    204     SkASSERT(width > 0);
    205 
    206     int iy = y >> SHIFT;
    207     SkASSERT(iy >= fCurrIY);
    208 
    209     x -= fSuperLeft;
    210     // hack, until I figure out why my cubics (I think) go beyond the bounds
    211     if (x < 0) {
    212         width += x;
    213         x = 0;
    214     }
    215 
    216 #ifdef SK_DEBUG
    217     SkASSERT(y != fCurrY || x >= fCurrX);
    218 #endif
    219     SkASSERT(y >= fCurrY);
    220     if (fCurrY != y) {
    221         fOffsetX = 0;
    222         fCurrY = y;
    223     }
    224 
    225     if (iy != fCurrIY) {  // new scanline
    226         this->flush();
    227         fCurrIY = iy;
    228     }
    229 
    230     int start = x;
    231     int stop = x + width;
    232 
    233     SkASSERT(start >= 0 && stop > start);
    234     // integer-pixel-aligned ends of blit, rounded out
    235     int fb = start & MASK;
    236     int fe = stop & MASK;
    237     int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
    238 
    239     if (n < 0) {
    240         fb = fe - fb;
    241         n = 0;
    242         fe = 0;
    243     } else {
    244         if (fb == 0) {
    245             n += 1;
    246         } else {
    247             fb = SCALE - fb;
    248         }
    249     }
    250 
    251     fOffsetX = fRuns.add(x >> SHIFT, coverage_to_partial_alpha(fb),
    252                          n, coverage_to_partial_alpha(fe),
    253                          (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT),
    254                          fOffsetX);
    255 
    256 #ifdef SK_DEBUG
    257     fRuns.assertValid(y & MASK, (1 << (8 - SHIFT)));
    258     fCurrX = x + width;
    259 #endif
    260 }
    261 
    262 #if 0 // UNUSED
    263 static void set_left_rite_runs(SkAlphaRuns& runs, int ileft, U8CPU leftA,
    264                                int n, U8CPU riteA) {
    265     SkASSERT(leftA <= 0xFF);
    266     SkASSERT(riteA <= 0xFF);
    267 
    268     int16_t* run = runs.fRuns;
    269     uint8_t* aa = runs.fAlpha;
    270 
    271     if (ileft > 0) {
    272         run[0] = ileft;
    273         aa[0] = 0;
    274         run += ileft;
    275         aa += ileft;
    276     }
    277 
    278     SkASSERT(leftA < 0xFF);
    279     if (leftA > 0) {
    280         *run++ = 1;
    281         *aa++ = leftA;
    282     }
    283 
    284     if (n > 0) {
    285         run[0] = n;
    286         aa[0] = 0xFF;
    287         run += n;
    288         aa += n;
    289     }
    290 
    291     SkASSERT(riteA < 0xFF);
    292     if (riteA > 0) {
    293         *run++ = 1;
    294         *aa++ = riteA;
    295     }
    296     run[0] = 0;
    297 }
    298 #endif
    299 
    300 void SuperBlitter::blitRect(int x, int y, int width, int height) {
    301     SkASSERT(width > 0);
    302     SkASSERT(height > 0);
    303 
    304     // blit leading rows
    305     while ((y & MASK)) {
    306         this->blitH(x, y++, width);
    307         if (--height <= 0) {
    308             return;
    309         }
    310     }
    311     SkASSERT(height > 0);
    312 
    313     // Since this is a rect, instead of blitting supersampled rows one at a
    314     // time and then resolving to the destination canvas, we can blit
    315     // directly to the destintion canvas one row per SCALE supersampled rows.
    316     int start_y = y >> SHIFT;
    317     int stop_y = (y + height) >> SHIFT;
    318     int count = stop_y - start_y;
    319     if (count > 0) {
    320         y += count << SHIFT;
    321         height -= count << SHIFT;
    322 
    323         // save original X for our tail blitH() loop at the bottom
    324         int origX = x;
    325 
    326         x -= fSuperLeft;
    327         // hack, until I figure out why my cubics (I think) go beyond the bounds
    328         if (x < 0) {
    329             width += x;
    330             x = 0;
    331         }
    332 
    333         // There is always a left column, a middle, and a right column.
    334         // ileft is the destination x of the first pixel of the entire rect.
    335         // xleft is (SCALE - # of covered supersampled pixels) in that
    336         // destination pixel.
    337         int ileft = x >> SHIFT;
    338         int xleft = x & MASK;
    339         // irite is the destination x of the last pixel of the OPAQUE section.
    340         // xrite is the number of supersampled pixels extending beyond irite;
    341         // xrite/SCALE should give us alpha.
    342         int irite = (x + width) >> SHIFT;
    343         int xrite = (x + width) & MASK;
    344         if (!xrite) {
    345             xrite = SCALE;
    346             irite--;
    347         }
    348 
    349         // Need to call flush() to clean up pending draws before we
    350         // even consider blitV(), since otherwise it can look nonmonotonic.
    351         SkASSERT(start_y > fCurrIY);
    352         this->flush();
    353 
    354         int n = irite - ileft - 1;
    355         if (n < 0) {
    356             // If n < 0, we'll only have a single partially-transparent column
    357             // of pixels to render.
    358             xleft = xrite - xleft;
    359             SkASSERT(xleft <= SCALE);
    360             SkASSERT(xleft > 0);
    361             xrite = 0;
    362             fRealBlitter->blitV(ileft + fLeft, start_y, count,
    363                 coverage_to_exact_alpha(xleft));
    364         } else {
    365             // With n = 0, we have two possibly-transparent columns of pixels
    366             // to render; with n > 0, we have opaque columns between them.
    367 
    368             xleft = SCALE - xleft;
    369 
    370             // Using coverage_to_exact_alpha is not consistent with blitH()
    371             const int coverageL = coverage_to_exact_alpha(xleft);
    372             const int coverageR = coverage_to_exact_alpha(xrite);
    373 
    374             SkASSERT(coverageL > 0 || n > 0 || coverageR > 0);
    375             SkASSERT((coverageL != 0) + n + (coverageR != 0) <= fWidth);
    376 
    377             fRealBlitter->blitAntiRect(ileft + fLeft, start_y, n, count,
    378                                        coverageL, coverageR);
    379         }
    380 
    381         // preamble for our next call to blitH()
    382         fCurrIY = stop_y - 1;
    383         fOffsetX = 0;
    384         fCurrY = y - 1;
    385         fRuns.reset(fWidth);
    386         x = origX;
    387     }
    388 
    389     // catch any remaining few rows
    390     SkASSERT(height <= MASK);
    391     while (--height >= 0) {
    392         this->blitH(x, y++, width);
    393     }
    394 }
    395 
    396 ///////////////////////////////////////////////////////////////////////////////
    397 
    398 /// Masked supersampling antialiased blitter.
    399 class MaskSuperBlitter : public BaseSuperBlitter {
    400 public:
    401     MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
    402                      const SkRegion& clip);
    403     virtual ~MaskSuperBlitter() {
    404         fRealBlitter->blitMask(fMask, fClipRect);
    405     }
    406 
    407     virtual void blitH(int x, int y, int width) SK_OVERRIDE;
    408 
    409     static bool CanHandleRect(const SkIRect& bounds) {
    410 #ifdef FORCE_RLE
    411         return false;
    412 #endif
    413         int width = bounds.width();
    414         int64_t rb = SkAlign4(width);
    415         // use 64bits to detect overflow
    416         int64_t storage = rb * bounds.height();
    417 
    418         return (width <= MaskSuperBlitter::kMAX_WIDTH) &&
    419                (storage <= MaskSuperBlitter::kMAX_STORAGE);
    420     }
    421 
    422 private:
    423     enum {
    424 #ifdef FORCE_SUPERMASK
    425         kMAX_WIDTH = 2048,
    426         kMAX_STORAGE = 1024 * 1024 * 2
    427 #else
    428         kMAX_WIDTH = 32,    // so we don't try to do very wide things, where the RLE blitter would be faster
    429         kMAX_STORAGE = 1024
    430 #endif
    431     };
    432 
    433     SkMask      fMask;
    434     SkIRect     fClipRect;
    435     // we add 1 because add_aa_span can write (unchanged) 1 extra byte at the end, rather than
    436     // perform a test to see if stopAlpha != 0
    437     uint32_t    fStorage[(kMAX_STORAGE >> 2) + 1];
    438 };
    439 
    440 MaskSuperBlitter::MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
    441                                    const SkRegion& clip)
    442         : BaseSuperBlitter(realBlitter, ir, clip) {
    443     SkASSERT(CanHandleRect(ir));
    444 
    445     fMask.fImage    = (uint8_t*)fStorage;
    446     fMask.fBounds   = ir;
    447     fMask.fRowBytes = ir.width();
    448     fMask.fFormat   = SkMask::kA8_Format;
    449 
    450     fClipRect = ir;
    451     fClipRect.intersect(clip.getBounds());
    452 
    453     // For valgrind, write 1 extra byte at the end so we don't read
    454     // uninitialized memory. See comment in add_aa_span and fStorage[].
    455     memset(fStorage, 0, fMask.fBounds.height() * fMask.fRowBytes + 1);
    456 }
    457 
    458 static void add_aa_span(uint8_t* alpha, U8CPU startAlpha) {
    459     /*  I should be able to just add alpha[x] + startAlpha.
    460         However, if the trailing edge of the previous span and the leading
    461         edge of the current span round to the same super-sampled x value,
    462         I might overflow to 256 with this add, hence the funny subtract.
    463     */
    464     unsigned tmp = *alpha + startAlpha;
    465     SkASSERT(tmp <= 256);
    466     *alpha = SkToU8(tmp - (tmp >> 8));
    467 }
    468 
    469 static inline uint32_t quadplicate_byte(U8CPU value) {
    470     uint32_t pair = (value << 8) | value;
    471     return (pair << 16) | pair;
    472 }
    473 
    474 // Perform this tricky subtract, to avoid overflowing to 256. Our caller should
    475 // only ever call us with at most enough to hit 256 (never larger), so it is
    476 // enough to just subtract the high-bit. Actually clamping with a branch would
    477 // be slower (e.g. if (tmp > 255) tmp = 255;)
    478 //
    479 static inline void saturated_add(uint8_t* ptr, U8CPU add) {
    480     unsigned tmp = *ptr + add;
    481     SkASSERT(tmp <= 256);
    482     *ptr = SkToU8(tmp - (tmp >> 8));
    483 }
    484 
    485 // minimum count before we want to setup an inner loop, adding 4-at-a-time
    486 #define MIN_COUNT_FOR_QUAD_LOOP  16
    487 
    488 static void add_aa_span(uint8_t* alpha, U8CPU startAlpha, int middleCount,
    489                         U8CPU stopAlpha, U8CPU maxValue) {
    490     SkASSERT(middleCount >= 0);
    491 
    492     saturated_add(alpha, startAlpha);
    493     alpha += 1;
    494 
    495     if (middleCount >= MIN_COUNT_FOR_QUAD_LOOP) {
    496         // loop until we're quad-byte aligned
    497         while (SkTCast<intptr_t>(alpha) & 0x3) {
    498             alpha[0] = SkToU8(alpha[0] + maxValue);
    499             alpha += 1;
    500             middleCount -= 1;
    501         }
    502 
    503         int bigCount = middleCount >> 2;
    504         uint32_t* qptr = reinterpret_cast<uint32_t*>(alpha);
    505         uint32_t qval = quadplicate_byte(maxValue);
    506         do {
    507             *qptr++ += qval;
    508         } while (--bigCount > 0);
    509 
    510         middleCount &= 3;
    511         alpha = reinterpret_cast<uint8_t*> (qptr);
    512         // fall through to the following while-loop
    513     }
    514 
    515     while (--middleCount >= 0) {
    516         alpha[0] = SkToU8(alpha[0] + maxValue);
    517         alpha += 1;
    518     }
    519 
    520     // potentially this can be off the end of our "legal" alpha values, but that
    521     // only happens if stopAlpha is also 0. Rather than test for stopAlpha != 0
    522     // every time (slow), we just do it, and ensure that we've allocated extra space
    523     // (see the + 1 comment in fStorage[]
    524     saturated_add(alpha, stopAlpha);
    525 }
    526 
    527 void MaskSuperBlitter::blitH(int x, int y, int width) {
    528     int iy = (y >> SHIFT);
    529 
    530     SkASSERT(iy >= fMask.fBounds.fTop && iy < fMask.fBounds.fBottom);
    531     iy -= fMask.fBounds.fTop;   // make it relative to 0
    532 
    533     // This should never happen, but it does.  Until the true cause is
    534     // discovered, let's skip this span instead of crashing.
    535     // See http://crbug.com/17569.
    536     if (iy < 0) {
    537         return;
    538     }
    539 
    540 #ifdef SK_DEBUG
    541     {
    542         int ix = x >> SHIFT;
    543         SkASSERT(ix >= fMask.fBounds.fLeft && ix < fMask.fBounds.fRight);
    544     }
    545 #endif
    546 
    547     x -= (fMask.fBounds.fLeft << SHIFT);
    548 
    549     // hack, until I figure out why my cubics (I think) go beyond the bounds
    550     if (x < 0) {
    551         width += x;
    552         x = 0;
    553     }
    554 
    555     uint8_t* row = fMask.fImage + iy * fMask.fRowBytes + (x >> SHIFT);
    556 
    557     int start = x;
    558     int stop = x + width;
    559 
    560     SkASSERT(start >= 0 && stop > start);
    561     int fb = start & MASK;
    562     int fe = stop & MASK;
    563     int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
    564 
    565 
    566     if (n < 0) {
    567         SkASSERT(row >= fMask.fImage);
    568         SkASSERT(row < fMask.fImage + kMAX_STORAGE + 1);
    569         add_aa_span(row, coverage_to_partial_alpha(fe - fb));
    570     } else {
    571         fb = SCALE - fb;
    572         SkASSERT(row >= fMask.fImage);
    573         SkASSERT(row + n + 1 < fMask.fImage + kMAX_STORAGE + 1);
    574         add_aa_span(row,  coverage_to_partial_alpha(fb),
    575                     n, coverage_to_partial_alpha(fe),
    576                     (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT));
    577     }
    578 
    579 #ifdef SK_DEBUG
    580     fCurrX = x + width;
    581 #endif
    582 }
    583 
    584 ///////////////////////////////////////////////////////////////////////////////
    585 
    586 static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
    587     const SkScalar min = -max;
    588     return  r.fLeft > min && r.fTop > min &&
    589             r.fRight < max && r.fBottom < max;
    590 }
    591 
    592 static int overflows_short_shift(int value, int shift) {
    593     const int s = 16 + shift;
    594     return (value << s >> s) - value;
    595 }
    596 
    597 /**
    598   Would any of the coordinates of this rectangle not fit in a short,
    599   when left-shifted by shift?
    600 */
    601 static int rect_overflows_short_shift(SkIRect rect, int shift) {
    602     SkASSERT(!overflows_short_shift(8191, SHIFT));
    603     SkASSERT(overflows_short_shift(8192, SHIFT));
    604     SkASSERT(!overflows_short_shift(32767, 0));
    605     SkASSERT(overflows_short_shift(32768, 0));
    606 
    607     // Since we expect these to succeed, we bit-or together
    608     // for a tiny extra bit of speed.
    609     return overflows_short_shift(rect.fLeft, SHIFT) |
    610            overflows_short_shift(rect.fRight, SHIFT) |
    611            overflows_short_shift(rect.fTop, SHIFT) |
    612            overflows_short_shift(rect.fBottom, SHIFT);
    613 }
    614 
    615 static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
    616     const SkScalar maxScalar = SkIntToScalar(maxInt);
    617 
    618     if (fitsInsideLimit(src, maxScalar)) {
    619         src.roundOut(dst);
    620         return true;
    621     }
    622     return false;
    623 }
    624 
    625 void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip,
    626                           SkBlitter* blitter, bool forceRLE) {
    627     if (origClip.isEmpty()) {
    628         return;
    629     }
    630 
    631     SkIRect ir;
    632 
    633     if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
    634 #if 0
    635         const SkRect& r = path.getBounds();
    636         SkDebugf("--- bounds can't fit in SkIRect\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
    637 #endif
    638         return;
    639     }
    640     if (ir.isEmpty()) {
    641         if (path.isInverseFillType()) {
    642             blitter->blitRegion(origClip);
    643         }
    644         return;
    645     }
    646 
    647     // If the intersection of the path bounds and the clip bounds
    648     // will overflow 32767 when << by SHIFT, we can't supersample,
    649     // so draw without antialiasing.
    650     SkIRect clippedIR;
    651     if (path.isInverseFillType()) {
    652        // If the path is an inverse fill, it's going to fill the entire
    653        // clip, and we care whether the entire clip exceeds our limits.
    654        clippedIR = origClip.getBounds();
    655     } else {
    656        if (!clippedIR.intersect(ir, origClip.getBounds())) {
    657            return;
    658        }
    659     }
    660     if (rect_overflows_short_shift(clippedIR, SHIFT)) {
    661         SkScan::FillPath(path, origClip, blitter);
    662         return;
    663     }
    664 
    665     // Our antialiasing can't handle a clip larger than 32767, so we restrict
    666     // the clip to that limit here. (the runs[] uses int16_t for its index).
    667     //
    668     // A more general solution (one that could also eliminate the need to
    669     // disable aa based on ir bounds (see overflows_short_shift) would be
    670     // to tile the clip/target...
    671     SkRegion tmpClipStorage;
    672     const SkRegion* clipRgn = &origClip;
    673     {
    674         static const int32_t kMaxClipCoord = 32767;
    675         const SkIRect& bounds = origClip.getBounds();
    676         if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
    677             SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
    678             tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
    679             clipRgn = &tmpClipStorage;
    680         }
    681     }
    682     // for here down, use clipRgn, not origClip
    683 
    684     SkScanClipper   clipper(blitter, clipRgn, ir);
    685     const SkIRect*  clipRect = clipper.getClipRect();
    686 
    687     if (clipper.getBlitter() == NULL) { // clipped out
    688         if (path.isInverseFillType()) {
    689             blitter->blitRegion(*clipRgn);
    690         }
    691         return;
    692     }
    693 
    694     // now use the (possibly wrapped) blitter
    695     blitter = clipper.getBlitter();
    696 
    697     if (path.isInverseFillType()) {
    698         sk_blit_above(blitter, ir, *clipRgn);
    699     }
    700 
    701     SkIRect superRect, *superClipRect = NULL;
    702 
    703     if (clipRect) {
    704         superRect.set(  clipRect->fLeft << SHIFT, clipRect->fTop << SHIFT,
    705                         clipRect->fRight << SHIFT, clipRect->fBottom << SHIFT);
    706         superClipRect = &superRect;
    707     }
    708 
    709     SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
    710 
    711     // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it
    712     // if we're an inverse filltype
    713     if (!path.isInverseFillType() && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) {
    714         MaskSuperBlitter    superBlit(blitter, ir, *clipRgn);
    715         SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
    716         sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
    717     } else {
    718         SuperBlitter    superBlit(blitter, ir, *clipRgn);
    719         sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
    720     }
    721 
    722     if (path.isInverseFillType()) {
    723         sk_blit_below(blitter, ir, *clipRgn);
    724     }
    725 }
    726 
    727 ///////////////////////////////////////////////////////////////////////////////
    728 
    729 #include "SkRasterClip.h"
    730 
    731 void SkScan::FillPath(const SkPath& path, const SkRasterClip& clip,
    732                           SkBlitter* blitter) {
    733     if (clip.isEmpty()) {
    734         return;
    735     }
    736 
    737     if (clip.isBW()) {
    738         FillPath(path, clip.bwRgn(), blitter);
    739     } else {
    740         SkRegion        tmp;
    741         SkAAClipBlitter aaBlitter;
    742 
    743         tmp.setRect(clip.getBounds());
    744         aaBlitter.init(blitter, &clip.aaRgn());
    745         SkScan::FillPath(path, tmp, &aaBlitter);
    746     }
    747 }
    748 
    749 void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip,
    750                           SkBlitter* blitter) {
    751     if (clip.isEmpty()) {
    752         return;
    753     }
    754 
    755     if (clip.isBW()) {
    756         AntiFillPath(path, clip.bwRgn(), blitter);
    757     } else {
    758         SkRegion        tmp;
    759         SkAAClipBlitter aaBlitter;
    760 
    761         tmp.setRect(clip.getBounds());
    762         aaBlitter.init(blitter, &clip.aaRgn());
    763         SkScan::AntiFillPath(path, tmp, &aaBlitter, true);
    764     }
    765 }
    766