epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkScanPriv.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkBlitter.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkAntiRun.h" |
| 15 | |
| 16 | #define SHIFT 2 |
| 17 | #define SCALE (1 << SHIFT) |
| 18 | #define MASK (SCALE - 1) |
| 19 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 20 | /** @file |
reed@google.com | 2f3567c | 2011-06-09 15:54:38 +0000 | [diff] [blame] | 21 | We have two techniques for capturing the output of the supersampler: |
| 22 | - SUPERMASK, which records a large mask-bitmap |
| 23 | this is often faster for small, complex objects |
| 24 | - RLE, which records a rle-encoded scanline |
| 25 | this is often faster for large objects with big spans |
| 26 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 27 | These blitters use two coordinate systems: |
| 28 | - destination coordinates, scale equal to the output - often |
| 29 | abbreviated with 'i' or 'I' in variable names |
| 30 | - supersampled coordinates, scale equal to the output * SCALE |
reed@google.com | 2f3567c | 2011-06-09 15:54:38 +0000 | [diff] [blame] | 31 | */ |
reed@google.com | 5b7d603 | 2012-05-09 14:07:34 +0000 | [diff] [blame] | 32 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 33 | //#define FORCE_SUPERMASK |
| 34 | //#define FORCE_RLE |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 38 | /// Base class for a single-pass supersampled blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | class BaseSuperBlitter : public SkBlitter { |
| 40 | public: |
| 41 | BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 42 | const SkRegion& clip, bool isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 44 | /// Must be explicitly defined on subclasses. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | virtual void blitAntiH(int x, int y, const SkAlpha antialias[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | const int16_t runs[]) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 47 | SkDEBUGFAIL("How did I get here?"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 49 | /// May not be called on BaseSuperBlitter because it blits out of order. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | void blitV(int x, int y, int height, SkAlpha alpha) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 51 | SkDEBUGFAIL("How did I get here?"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
| 54 | protected: |
| 55 | SkBlitter* fRealBlitter; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 56 | /// Current y coordinate, in destination coordinates. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | int fCurrIY; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 58 | /// Widest row of region to be blitted, in destination coordinates. |
| 59 | int fWidth; |
| 60 | /// Leftmost x coordinate in any row, in destination coordinates. |
| 61 | int fLeft; |
| 62 | /// Leftmost x coordinate in any row, in supersampled coordinates. |
| 63 | int fSuperLeft; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | |
| 65 | SkDEBUGCODE(int fCurrX;) |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 66 | /// Current y coordinate in supersampled coordinates. |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 67 | int fCurrY; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 68 | /// Initial y coordinate (top of bounds). |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 69 | int fTop; |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 70 | |
| 71 | SkIRect fSectBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 74 | BaseSuperBlitter::BaseSuperBlitter(SkBlitter* realBlit, const SkIRect& ir, const SkRegion& clip, |
| 75 | bool isInverse) { |
| 76 | fRealBlitter = realBlit; |
| 77 | |
| 78 | SkIRect sectBounds; |
| 79 | if (isInverse) { |
reed | c553b7a | 2014-10-15 09:00:27 -0700 | [diff] [blame] | 80 | // We use the clip bounds instead of the ir, since we may be asked to |
| 81 | //draw outside of the rect when we're a inverse filltype |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 82 | sectBounds = clip.getBounds(); |
| 83 | } else { |
| 84 | if (!sectBounds.intersect(ir, clip.getBounds())) { |
| 85 | sectBounds.setEmpty(); |
| 86 | } |
| 87 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 88 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 89 | const int left = sectBounds.left(); |
| 90 | const int right = sectBounds.right(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | fLeft = left; |
mtklein | cefb459 | 2015-12-09 13:32:58 -0800 | [diff] [blame] | 93 | fSuperLeft = SkLeftShift(left, SHIFT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | fWidth = right - left; |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 95 | fTop = sectBounds.top(); |
| 96 | fCurrIY = fTop - 1; |
mtklein | cefb459 | 2015-12-09 13:32:58 -0800 | [diff] [blame] | 97 | fCurrY = SkLeftShift(fTop, SHIFT) - 1; |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 98 | |
reed@google.com | 34e52a0 | 2011-06-01 17:32:11 +0000 | [diff] [blame] | 99 | SkDEBUGCODE(fCurrX = -1;) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 102 | /// Run-length-encoded supersampling antialiased blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | class SuperBlitter : public BaseSuperBlitter { |
| 104 | public: |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 105 | SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip, bool isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | |
| 107 | virtual ~SuperBlitter() { |
| 108 | this->flush(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | } |
| 110 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 111 | /// Once fRuns contains a complete supersampled row, flush() blits |
| 112 | /// it out through the wrapped blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | void flush(); |
| 114 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 115 | /// Blits a row of pixels, with location and width specified |
| 116 | /// in supersampled coordinates. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | void blitH(int x, int y, int width) override; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 118 | /// Blits a rectangle of pixels, with location and size specified |
| 119 | /// in supersampled coordinates. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 120 | void blitRect(int x, int y, int width, int height) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
| 122 | private: |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 123 | // The next three variables are used to track a circular buffer that |
| 124 | // contains the values used in SkAlphaRuns. These variables should only |
| 125 | // ever be updated in advanceRuns(), and fRuns should always point to |
| 126 | // a valid SkAlphaRuns... |
| 127 | int fRunsToBuffer; |
| 128 | void* fRunsBuffer; |
| 129 | int fCurrentRun; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkAlphaRuns fRuns; |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 131 | |
| 132 | // extra one to store the zero at the end |
| 133 | int getRunsSz() const { return (fWidth + 1 + (fWidth + 2)/2) * sizeof(int16_t); } |
| 134 | |
| 135 | // This function updates the fRuns variable to point to the next buffer space |
| 136 | // with adequate storage for a SkAlphaRuns. It mostly just advances fCurrentRun |
| 137 | // and resets fRuns to point to an empty scanline. |
| 138 | void advanceRuns() { |
| 139 | const size_t kRunsSz = this->getRunsSz(); |
| 140 | fCurrentRun = (fCurrentRun + 1) % fRunsToBuffer; |
| 141 | fRuns.fRuns = reinterpret_cast<int16_t*>( |
| 142 | reinterpret_cast<uint8_t*>(fRunsBuffer) + fCurrentRun * kRunsSz); |
| 143 | fRuns.fAlpha = reinterpret_cast<SkAlpha*>(fRuns.fRuns + fWidth + 1); |
| 144 | fRuns.reset(fWidth); |
| 145 | } |
| 146 | |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 147 | int fOffsetX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 150 | SuperBlitter::SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip, |
| 151 | bool isInverse) |
| 152 | : BaseSuperBlitter(realBlitter, ir, clip, isInverse) |
| 153 | { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 154 | fRunsToBuffer = realBlitter->requestRowsPreserved(); |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 155 | fRunsBuffer = realBlitter->allocBlitMemory(fRunsToBuffer * this->getRunsSz()); |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 156 | fCurrentRun = -1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 158 | this->advanceRuns(); |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 159 | |
| 160 | fOffsetX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 163 | void SuperBlitter::flush() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 164 | if (fCurrIY >= fTop) { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 165 | |
| 166 | SkASSERT(fCurrentRun < fRunsToBuffer); |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 167 | if (!fRuns.empty()) { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 168 | // SkDEBUGCODE(fRuns.dump();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | fRealBlitter->blitAntiH(fLeft, fCurrIY, fRuns.fAlpha, fRuns.fRuns); |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 170 | this->advanceRuns(); |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 171 | fOffsetX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | } |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 173 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 174 | fCurrIY = fTop - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | SkDEBUGCODE(fCurrX = -1;) |
| 176 | } |
| 177 | } |
| 178 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 179 | /** coverage_to_partial_alpha() is being used by SkAlphaRuns, which |
| 180 | *accumulates* SCALE pixels worth of "alpha" in [0,(256/SCALE)] |
| 181 | to produce a final value in [0, 255] and handles clamping 256->255 |
| 182 | itself, with the same (alpha - (alpha >> 8)) correction as |
| 183 | coverage_to_exact_alpha(). |
| 184 | */ |
| 185 | static inline int coverage_to_partial_alpha(int aa) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | aa <<= 8 - 2*SHIFT; |
reed@google.com | 5b7d603 | 2012-05-09 14:07:34 +0000 | [diff] [blame] | 187 | return aa; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | } |
| 189 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 190 | /** coverage_to_exact_alpha() is being used by our blitter, which wants |
| 191 | a final value in [0, 255]. |
| 192 | */ |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 193 | static inline int coverage_to_exact_alpha(int aa) { |
tomhudson@google.com | 31bab39 | 2012-01-03 20:12:42 +0000 | [diff] [blame] | 194 | int alpha = (256 >> SHIFT) * aa; |
| 195 | // clamp 256->255 |
| 196 | return alpha - (alpha >> 8); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 199 | void SuperBlitter::blitH(int x, int y, int width) { |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 200 | SkASSERT(width > 0); |
| 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | int iy = y >> SHIFT; |
| 203 | SkASSERT(iy >= fCurrIY); |
| 204 | |
| 205 | x -= fSuperLeft; |
| 206 | // hack, until I figure out why my cubics (I think) go beyond the bounds |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 207 | if (x < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | width += x; |
| 209 | x = 0; |
| 210 | } |
| 211 | |
| 212 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | SkASSERT(y != fCurrY || x >= fCurrX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | #endif |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 215 | SkASSERT(y >= fCurrY); |
| 216 | if (fCurrY != y) { |
| 217 | fOffsetX = 0; |
| 218 | fCurrY = y; |
| 219 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 220 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 221 | if (iy != fCurrIY) { // new scanline |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | this->flush(); |
| 223 | fCurrIY = iy; |
| 224 | } |
| 225 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 226 | int start = x; |
| 227 | int stop = x + width; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 229 | SkASSERT(start >= 0 && stop > start); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 230 | // integer-pixel-aligned ends of blit, rounded out |
| 231 | int fb = start & MASK; |
| 232 | int fe = stop & MASK; |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 233 | int n = (stop >> SHIFT) - (start >> SHIFT) - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 235 | if (n < 0) { |
| 236 | fb = fe - fb; |
| 237 | n = 0; |
| 238 | fe = 0; |
| 239 | } else { |
| 240 | if (fb == 0) { |
| 241 | n += 1; |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 242 | } else { |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 243 | fb = SCALE - fb; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | } |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 246 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 247 | fOffsetX = fRuns.add(x >> SHIFT, coverage_to_partial_alpha(fb), |
| 248 | n, coverage_to_partial_alpha(fe), |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 249 | (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT), |
| 250 | fOffsetX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef SK_DEBUG |
| 253 | fRuns.assertValid(y & MASK, (1 << (8 - SHIFT))); |
| 254 | fCurrX = x + width; |
| 255 | #endif |
| 256 | } |
| 257 | |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 258 | #if 0 // UNUSED |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 259 | static void set_left_rite_runs(SkAlphaRuns& runs, int ileft, U8CPU leftA, |
| 260 | int n, U8CPU riteA) { |
| 261 | SkASSERT(leftA <= 0xFF); |
| 262 | SkASSERT(riteA <= 0xFF); |
| 263 | |
| 264 | int16_t* run = runs.fRuns; |
| 265 | uint8_t* aa = runs.fAlpha; |
| 266 | |
| 267 | if (ileft > 0) { |
| 268 | run[0] = ileft; |
| 269 | aa[0] = 0; |
| 270 | run += ileft; |
| 271 | aa += ileft; |
| 272 | } |
| 273 | |
| 274 | SkASSERT(leftA < 0xFF); |
| 275 | if (leftA > 0) { |
| 276 | *run++ = 1; |
| 277 | *aa++ = leftA; |
| 278 | } |
| 279 | |
| 280 | if (n > 0) { |
| 281 | run[0] = n; |
| 282 | aa[0] = 0xFF; |
| 283 | run += n; |
| 284 | aa += n; |
| 285 | } |
| 286 | |
| 287 | SkASSERT(riteA < 0xFF); |
| 288 | if (riteA > 0) { |
| 289 | *run++ = 1; |
| 290 | *aa++ = riteA; |
| 291 | } |
| 292 | run[0] = 0; |
| 293 | } |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 294 | #endif |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 295 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 296 | void SuperBlitter::blitRect(int x, int y, int width, int height) { |
| 297 | SkASSERT(width > 0); |
| 298 | SkASSERT(height > 0); |
| 299 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 300 | // blit leading rows |
| 301 | while ((y & MASK)) { |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 302 | this->blitH(x, y++, width); |
| 303 | if (--height <= 0) { |
| 304 | return; |
| 305 | } |
| 306 | } |
| 307 | SkASSERT(height > 0); |
| 308 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 309 | // Since this is a rect, instead of blitting supersampled rows one at a |
| 310 | // time and then resolving to the destination canvas, we can blit |
| 311 | // directly to the destintion canvas one row per SCALE supersampled rows. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 312 | int start_y = y >> SHIFT; |
| 313 | int stop_y = (y + height) >> SHIFT; |
| 314 | int count = stop_y - start_y; |
| 315 | if (count > 0) { |
| 316 | y += count << SHIFT; |
| 317 | height -= count << SHIFT; |
| 318 | |
| 319 | // save original X for our tail blitH() loop at the bottom |
| 320 | int origX = x; |
| 321 | |
| 322 | x -= fSuperLeft; |
| 323 | // hack, until I figure out why my cubics (I think) go beyond the bounds |
| 324 | if (x < 0) { |
| 325 | width += x; |
| 326 | x = 0; |
| 327 | } |
| 328 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 329 | // There is always a left column, a middle, and a right column. |
| 330 | // ileft is the destination x of the first pixel of the entire rect. |
| 331 | // xleft is (SCALE - # of covered supersampled pixels) in that |
| 332 | // destination pixel. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 333 | int ileft = x >> SHIFT; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 334 | int xleft = x & MASK; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 335 | // irite is the destination x of the last pixel of the OPAQUE section. |
| 336 | // xrite is the number of supersampled pixels extending beyond irite; |
| 337 | // xrite/SCALE should give us alpha. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 338 | int irite = (x + width) >> SHIFT; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 339 | int xrite = (x + width) & MASK; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 340 | if (!xrite) { |
| 341 | xrite = SCALE; |
| 342 | irite--; |
| 343 | } |
| 344 | |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 345 | // Need to call flush() to clean up pending draws before we |
| 346 | // even consider blitV(), since otherwise it can look nonmonotonic. |
| 347 | SkASSERT(start_y > fCurrIY); |
| 348 | this->flush(); |
| 349 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 350 | int n = irite - ileft - 1; |
| 351 | if (n < 0) { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 352 | // If n < 0, we'll only have a single partially-transparent column |
| 353 | // of pixels to render. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 354 | xleft = xrite - xleft; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 355 | SkASSERT(xleft <= SCALE); |
| 356 | SkASSERT(xleft > 0); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 357 | fRealBlitter->blitV(ileft + fLeft, start_y, count, |
| 358 | coverage_to_exact_alpha(xleft)); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 359 | } else { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 360 | // With n = 0, we have two possibly-transparent columns of pixels |
| 361 | // to render; with n > 0, we have opaque columns between them. |
| 362 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 363 | xleft = SCALE - xleft; |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 364 | |
| 365 | // Using coverage_to_exact_alpha is not consistent with blitH() |
| 366 | const int coverageL = coverage_to_exact_alpha(xleft); |
| 367 | const int coverageR = coverage_to_exact_alpha(xrite); |
| 368 | |
| 369 | SkASSERT(coverageL > 0 || n > 0 || coverageR > 0); |
| 370 | SkASSERT((coverageL != 0) + n + (coverageR != 0) <= fWidth); |
| 371 | |
| 372 | fRealBlitter->blitAntiRect(ileft + fLeft, start_y, n, count, |
| 373 | coverageL, coverageR); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 374 | } |
| 375 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 376 | // preamble for our next call to blitH() |
| 377 | fCurrIY = stop_y - 1; |
| 378 | fOffsetX = 0; |
| 379 | fCurrY = y - 1; |
| 380 | fRuns.reset(fWidth); |
| 381 | x = origX; |
| 382 | } |
| 383 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 384 | // catch any remaining few rows |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 385 | SkASSERT(height <= MASK); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 386 | while (--height >= 0) { |
| 387 | this->blitH(x, y++, width); |
| 388 | } |
| 389 | } |
| 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | /////////////////////////////////////////////////////////////////////////////// |
| 392 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 393 | /// Masked supersampling antialiased blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | class MaskSuperBlitter : public BaseSuperBlitter { |
| 395 | public: |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 396 | MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion&, bool isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | virtual ~MaskSuperBlitter() { |
| 398 | fRealBlitter->blitMask(fMask, fClipRect); |
| 399 | } |
| 400 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 401 | void blitH(int x, int y, int width) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 403 | static bool CanHandleRect(const SkIRect& bounds) { |
| 404 | #ifdef FORCE_RLE |
| 405 | return false; |
| 406 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 407 | int width = bounds.width(); |
reed@google.com | f9f258c | 2012-03-12 16:09:06 +0000 | [diff] [blame] | 408 | int64_t rb = SkAlign4(width); |
| 409 | // use 64bits to detect overflow |
| 410 | int64_t storage = rb * bounds.height(); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 411 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 412 | return (width <= MaskSuperBlitter::kMAX_WIDTH) && |
reed@google.com | f9f258c | 2012-03-12 16:09:06 +0000 | [diff] [blame] | 413 | (storage <= MaskSuperBlitter::kMAX_STORAGE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 414 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 415 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | private: |
| 417 | enum { |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 418 | #ifdef FORCE_SUPERMASK |
| 419 | kMAX_WIDTH = 2048, |
| 420 | kMAX_STORAGE = 1024 * 1024 * 2 |
| 421 | #else |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | kMAX_WIDTH = 32, // so we don't try to do very wide things, where the RLE blitter would be faster |
| 423 | kMAX_STORAGE = 1024 |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 424 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | SkMask fMask; |
| 428 | SkIRect fClipRect; |
| 429 | // we add 1 because add_aa_span can write (unchanged) 1 extra byte at the end, rather than |
| 430 | // perform a test to see if stopAlpha != 0 |
| 431 | uint32_t fStorage[(kMAX_STORAGE >> 2) + 1]; |
| 432 | }; |
| 433 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 434 | MaskSuperBlitter::MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkRegion& clip, |
| 435 | bool isInverse) |
| 436 | : BaseSuperBlitter(realBlitter, ir, clip, isInverse) |
| 437 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | SkASSERT(CanHandleRect(ir)); |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 439 | SkASSERT(!isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | |
| 441 | fMask.fImage = (uint8_t*)fStorage; |
| 442 | fMask.fBounds = ir; |
| 443 | fMask.fRowBytes = ir.width(); |
| 444 | fMask.fFormat = SkMask::kA8_Format; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 445 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | fClipRect = ir; |
robertphillips | f4e5995 | 2015-01-07 12:16:10 -0800 | [diff] [blame] | 447 | if (!fClipRect.intersect(clip.getBounds())) { |
| 448 | SkASSERT(0); |
| 449 | fClipRect.setEmpty(); |
| 450 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 451 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | // For valgrind, write 1 extra byte at the end so we don't read |
| 453 | // uninitialized memory. See comment in add_aa_span and fStorage[]. |
| 454 | memset(fStorage, 0, fMask.fBounds.height() * fMask.fRowBytes + 1); |
| 455 | } |
| 456 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 457 | static void add_aa_span(uint8_t* alpha, U8CPU startAlpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | /* I should be able to just add alpha[x] + startAlpha. |
| 459 | However, if the trailing edge of the previous span and the leading |
| 460 | edge of the current span round to the same super-sampled x value, |
| 461 | I might overflow to 256 with this add, hence the funny subtract. |
| 462 | */ |
| 463 | unsigned tmp = *alpha + startAlpha; |
| 464 | SkASSERT(tmp <= 256); |
| 465 | *alpha = SkToU8(tmp - (tmp >> 8)); |
| 466 | } |
| 467 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 468 | static inline uint32_t quadplicate_byte(U8CPU value) { |
| 469 | uint32_t pair = (value << 8) | value; |
| 470 | return (pair << 16) | pair; |
| 471 | } |
| 472 | |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 473 | // Perform this tricky subtract, to avoid overflowing to 256. Our caller should |
| 474 | // only ever call us with at most enough to hit 256 (never larger), so it is |
| 475 | // enough to just subtract the high-bit. Actually clamping with a branch would |
| 476 | // be slower (e.g. if (tmp > 255) tmp = 255;) |
| 477 | // |
| 478 | static inline void saturated_add(uint8_t* ptr, U8CPU add) { |
| 479 | unsigned tmp = *ptr + add; |
| 480 | SkASSERT(tmp <= 256); |
| 481 | *ptr = SkToU8(tmp - (tmp >> 8)); |
| 482 | } |
| 483 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 484 | // minimum count before we want to setup an inner loop, adding 4-at-a-time |
| 485 | #define MIN_COUNT_FOR_QUAD_LOOP 16 |
| 486 | |
| 487 | static void add_aa_span(uint8_t* alpha, U8CPU startAlpha, int middleCount, |
| 488 | U8CPU stopAlpha, U8CPU maxValue) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 489 | SkASSERT(middleCount >= 0); |
| 490 | |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 491 | saturated_add(alpha, startAlpha); |
| 492 | alpha += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 494 | if (middleCount >= MIN_COUNT_FOR_QUAD_LOOP) { |
| 495 | // loop until we're quad-byte aligned |
| 496 | while (SkTCast<intptr_t>(alpha) & 0x3) { |
| 497 | alpha[0] = SkToU8(alpha[0] + maxValue); |
| 498 | alpha += 1; |
| 499 | middleCount -= 1; |
| 500 | } |
| 501 | |
| 502 | int bigCount = middleCount >> 2; |
| 503 | uint32_t* qptr = reinterpret_cast<uint32_t*>(alpha); |
| 504 | uint32_t qval = quadplicate_byte(maxValue); |
| 505 | do { |
| 506 | *qptr++ += qval; |
| 507 | } while (--bigCount > 0); |
| 508 | |
| 509 | middleCount &= 3; |
| 510 | alpha = reinterpret_cast<uint8_t*> (qptr); |
| 511 | // fall through to the following while-loop |
| 512 | } |
| 513 | |
| 514 | while (--middleCount >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 515 | alpha[0] = SkToU8(alpha[0] + maxValue); |
| 516 | alpha += 1; |
| 517 | } |
| 518 | |
| 519 | // potentially this can be off the end of our "legal" alpha values, but that |
| 520 | // only happens if stopAlpha is also 0. Rather than test for stopAlpha != 0 |
| 521 | // every time (slow), we just do it, and ensure that we've allocated extra space |
| 522 | // (see the + 1 comment in fStorage[] |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 523 | saturated_add(alpha, stopAlpha); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | } |
| 525 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 526 | void MaskSuperBlitter::blitH(int x, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 527 | int iy = (y >> SHIFT); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 528 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | SkASSERT(iy >= fMask.fBounds.fTop && iy < fMask.fBounds.fBottom); |
| 530 | iy -= fMask.fBounds.fTop; // make it relative to 0 |
| 531 | |
senorblanco@chromium.org | c41513c | 2009-08-21 22:00:12 +0000 | [diff] [blame] | 532 | // This should never happen, but it does. Until the true cause is |
| 533 | // discovered, let's skip this span instead of crashing. |
| 534 | // See http://crbug.com/17569. |
| 535 | if (iy < 0) { |
| 536 | return; |
| 537 | } |
| 538 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | #ifdef SK_DEBUG |
| 540 | { |
| 541 | int ix = x >> SHIFT; |
| 542 | SkASSERT(ix >= fMask.fBounds.fLeft && ix < fMask.fBounds.fRight); |
| 543 | } |
| 544 | #endif |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 545 | |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 546 | x -= SkLeftShift(fMask.fBounds.fLeft, SHIFT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 547 | |
| 548 | // hack, until I figure out why my cubics (I think) go beyond the bounds |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 549 | if (x < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 550 | width += x; |
| 551 | x = 0; |
| 552 | } |
| 553 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 554 | uint8_t* row = fMask.fImage + iy * fMask.fRowBytes + (x >> SHIFT); |
| 555 | |
| 556 | int start = x; |
| 557 | int stop = x + width; |
| 558 | |
| 559 | SkASSERT(start >= 0 && stop > start); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 560 | int fb = start & MASK; |
| 561 | int fe = stop & MASK; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 562 | int n = (stop >> SHIFT) - (start >> SHIFT) - 1; |
| 563 | |
| 564 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 565 | if (n < 0) { |
senorblanco@chromium.org | feef49c | 2009-08-12 20:21:49 +0000 | [diff] [blame] | 566 | SkASSERT(row >= fMask.fImage); |
| 567 | SkASSERT(row < fMask.fImage + kMAX_STORAGE + 1); |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 568 | add_aa_span(row, coverage_to_partial_alpha(fe - fb)); |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 569 | } else { |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 570 | fb = SCALE - fb; |
senorblanco@chromium.org | feef49c | 2009-08-12 20:21:49 +0000 | [diff] [blame] | 571 | SkASSERT(row >= fMask.fImage); |
| 572 | SkASSERT(row + n + 1 < fMask.fImage + kMAX_STORAGE + 1); |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 573 | add_aa_span(row, coverage_to_partial_alpha(fb), |
| 574 | n, coverage_to_partial_alpha(fe), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 575 | (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT)); |
| 576 | } |
| 577 | |
| 578 | #ifdef SK_DEBUG |
| 579 | fCurrX = x + width; |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | /////////////////////////////////////////////////////////////////////////////// |
| 584 | |
reed@google.com | 07db861 | 2012-02-28 15:32:42 +0000 | [diff] [blame] | 585 | static bool fitsInsideLimit(const SkRect& r, SkScalar max) { |
| 586 | const SkScalar min = -max; |
| 587 | return r.fLeft > min && r.fTop > min && |
| 588 | r.fRight < max && r.fBottom < max; |
| 589 | } |
| 590 | |
tomhudson@google.com | 17b6ba4 | 2012-04-12 14:23:58 +0000 | [diff] [blame] | 591 | static int overflows_short_shift(int value, int shift) { |
| 592 | const int s = 16 + shift; |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 593 | return (SkLeftShift(value, s) >> s) - value; |
tomhudson@google.com | 17b6ba4 | 2012-04-12 14:23:58 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
| 597 | Would any of the coordinates of this rectangle not fit in a short, |
| 598 | when left-shifted by shift? |
| 599 | */ |
| 600 | static int rect_overflows_short_shift(SkIRect rect, int shift) { |
| 601 | SkASSERT(!overflows_short_shift(8191, SHIFT)); |
| 602 | SkASSERT(overflows_short_shift(8192, SHIFT)); |
| 603 | SkASSERT(!overflows_short_shift(32767, 0)); |
| 604 | SkASSERT(overflows_short_shift(32768, 0)); |
| 605 | |
| 606 | // Since we expect these to succeed, we bit-or together |
| 607 | // for a tiny extra bit of speed. |
| 608 | return overflows_short_shift(rect.fLeft, SHIFT) | |
| 609 | overflows_short_shift(rect.fRight, SHIFT) | |
| 610 | overflows_short_shift(rect.fTop, SHIFT) | |
| 611 | overflows_short_shift(rect.fBottom, SHIFT); |
| 612 | } |
| 613 | |
reed@google.com | 07db861 | 2012-02-28 15:32:42 +0000 | [diff] [blame] | 614 | static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) { |
| 615 | const SkScalar maxScalar = SkIntToScalar(maxInt); |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 616 | |
reed@google.com | 07db861 | 2012-02-28 15:32:42 +0000 | [diff] [blame] | 617 | if (fitsInsideLimit(src, maxScalar)) { |
| 618 | src.roundOut(dst); |
| 619 | return true; |
| 620 | } |
| 621 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 624 | void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip, |
reed@google.com | 2c508f2 | 2011-10-03 21:15:46 +0000 | [diff] [blame] | 625 | SkBlitter* blitter, bool forceRLE) { |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 626 | if (origClip.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 630 | const bool isInverse = path.isInverseFillType(); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 631 | SkIRect ir; |
reed@google.com | 07db861 | 2012-02-28 15:32:42 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 640 | if (ir.isEmpty()) { |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 641 | if (isInverse) { |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 642 | blitter->blitRegion(origClip); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 643 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | return; |
| 645 | } |
| 646 | |
tomhudson@google.com | 17b6ba4 | 2012-04-12 14:23:58 +0000 | [diff] [blame] | 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; |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 651 | if (isInverse) { |
tomhudson@google.com | 17b6ba4 | 2012-04-12 14:23:58 +0000 | [diff] [blame] | 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 | |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 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 | // |
tomhudson@google.com | 17b6ba4 | 2012-04-12 14:23:58 +0000 | [diff] [blame] | 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... |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 685 | const SkIRect* clipRect = clipper.getClipRect(); |
| 686 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 687 | if (clipper.getBlitter() == nullptr) { // clipped out |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 688 | if (isInverse) { |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 689 | blitter->blitRegion(*clipRgn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | } |
| 691 | return; |
| 692 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 693 | |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 694 | SkASSERT(clipper.getClipRect() == nullptr || |
| 695 | *clipper.getClipRect() == clipRgn->getBounds()); |
| 696 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 697 | // now use the (possibly wrapped) blitter |
| 698 | blitter = clipper.getBlitter(); |
| 699 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 700 | if (isInverse) { |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 701 | sk_blit_above(blitter, ir, *clipRgn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 702 | } |
| 703 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 704 | SkIRect superRect, *superClipRect = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 705 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 706 | if (clipRect) { |
reed | 513d307 | 2016-02-03 10:16:08 -0800 | [diff] [blame] | 707 | superRect.set(SkLeftShift(clipRect->fLeft, SHIFT), |
| 708 | SkLeftShift(clipRect->fTop, SHIFT), |
| 709 | SkLeftShift(clipRect->fRight, SHIFT), |
| 710 | SkLeftShift(clipRect->fBottom, SHIFT)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 711 | superClipRect = &superRect; |
| 712 | } |
| 713 | |
senorblanco@chromium.org | c41513c | 2009-08-21 22:00:12 +0000 | [diff] [blame] | 714 | SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop); |
| 715 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 716 | // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it |
| 717 | // if we're an inverse filltype |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 718 | if (!isInverse && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) { |
| 719 | MaskSuperBlitter superBlit(blitter, ir, *clipRgn, isInverse); |
senorblanco@chromium.org | c41513c | 2009-08-21 22:00:12 +0000 | [diff] [blame] | 720 | SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop); |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 721 | sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT, |
| 722 | superClipRect == nullptr); |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 723 | } else { |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 724 | SuperBlitter superBlit(blitter, ir, *clipRgn, isInverse); |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 725 | sk_fill_path(path, clipRgn->getBounds(), &superBlit, ir.fTop, ir.fBottom, SHIFT, |
| 726 | superClipRect == nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 728 | |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 729 | if (isInverse) { |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 730 | sk_blit_below(blitter, ir, *clipRgn); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 731 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | } |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 733 | |
| 734 | /////////////////////////////////////////////////////////////////////////////// |
| 735 | |
| 736 | #include "SkRasterClip.h" |
| 737 | |
| 738 | void SkScan::FillPath(const SkPath& path, const SkRasterClip& clip, |
| 739 | SkBlitter* blitter) { |
| 740 | if (clip.isEmpty()) { |
| 741 | return; |
| 742 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 743 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 744 | if (clip.isBW()) { |
| 745 | FillPath(path, clip.bwRgn(), blitter); |
| 746 | } else { |
| 747 | SkRegion tmp; |
| 748 | SkAAClipBlitter aaBlitter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 749 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 750 | tmp.setRect(clip.getBounds()); |
| 751 | aaBlitter.init(blitter, &clip.aaRgn()); |
| 752 | SkScan::FillPath(path, tmp, &aaBlitter); |
| 753 | } |
| 754 | } |
| 755 | |
Yuqian Li | 550148b | 2017-01-13 10:13:13 -0500 | [diff] [blame] | 756 | static bool suitableForAAA(const SkPath& path) { |
Yuqian Li | 550148b | 2017-01-13 10:13:13 -0500 | [diff] [blame] | 757 | if (gSkForceAnalyticAA.load()) { |
| 758 | return true; |
| 759 | } |
| 760 | const SkRect& bounds = path.getBounds(); |
| 761 | // When the path have so many points compared to the size of its bounds/resolution, |
| 762 | // it indicates that the path is not quite smooth in the current resolution: |
| 763 | // the expected number of turning points in every pixel row/column is significantly greater than |
Yuqian Li | 8de05ff | 2017-03-06 17:04:58 -0500 | [diff] [blame] | 764 | // zero. Hence Aanlytic AA is not likely to produce visible quality improvements, and Analytic |
| 765 | // AA might be slower than supersampling. |
Yuqian Li | 550148b | 2017-01-13 10:13:13 -0500 | [diff] [blame] | 766 | return path.countPoints() < SkTMax(bounds.width(), bounds.height()) / 2 - 10; |
| 767 | } |
| 768 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 769 | void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip, |
| 770 | SkBlitter* blitter) { |
Yuqian Li | 550148b | 2017-01-13 10:13:13 -0500 | [diff] [blame] | 771 | // Do not use AAA if path is too complicated: |
| 772 | // there won't be any speedup or significant visual improvement. |
| 773 | if (gSkUseAnalyticAA.load() && suitableForAAA(path)) { |
liyuqian | 38911a7 | 2016-10-04 11:23:22 -0700 | [diff] [blame] | 774 | SkScan::AAAFillPath(path, clip, blitter); |
| 775 | return; |
| 776 | } |
| 777 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 778 | if (clip.isEmpty()) { |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (clip.isBW()) { |
| 783 | AntiFillPath(path, clip.bwRgn(), blitter); |
| 784 | } else { |
| 785 | SkRegion tmp; |
| 786 | SkAAClipBlitter aaBlitter; |
| 787 | |
| 788 | tmp.setRect(clip.getBounds()); |
| 789 | aaBlitter.init(blitter, &clip.aaRgn()); |
| 790 | SkScan::AntiFillPath(path, tmp, &aaBlitter, true); |
| 791 | } |
| 792 | } |