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, |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 42 | const SkIRect& clipBounds, 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 | |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 74 | BaseSuperBlitter::BaseSuperBlitter(SkBlitter* realBlit, const SkIRect& ir, |
| 75 | const SkIRect& clipBounds, bool isInverse) { |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 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 |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 82 | sectBounds = clipBounds; |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 83 | } else { |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 84 | if (!sectBounds.intersect(ir, clipBounds)) { |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 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: |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 105 | SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkIRect& clipBounds, |
| 106 | bool isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 108 | ~SuperBlitter() override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | this->flush(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | } |
| 111 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 112 | /// Once fRuns contains a complete supersampled row, flush() blits |
| 113 | /// it out through the wrapped blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | void flush(); |
| 115 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 116 | /// Blits a row of pixels, with location and width specified |
| 117 | /// in supersampled coordinates. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 118 | void blitH(int x, int y, int width) override; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 119 | /// Blits a rectangle of pixels, with location and size specified |
| 120 | /// in supersampled coordinates. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 121 | void blitRect(int x, int y, int width, int height) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 124 | // The next three variables are used to track a circular buffer that |
| 125 | // contains the values used in SkAlphaRuns. These variables should only |
| 126 | // ever be updated in advanceRuns(), and fRuns should always point to |
| 127 | // a valid SkAlphaRuns... |
| 128 | int fRunsToBuffer; |
| 129 | void* fRunsBuffer; |
| 130 | int fCurrentRun; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | SkAlphaRuns fRuns; |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 132 | |
| 133 | // extra one to store the zero at the end |
| 134 | int getRunsSz() const { return (fWidth + 1 + (fWidth + 2)/2) * sizeof(int16_t); } |
| 135 | |
| 136 | // This function updates the fRuns variable to point to the next buffer space |
| 137 | // with adequate storage for a SkAlphaRuns. It mostly just advances fCurrentRun |
| 138 | // and resets fRuns to point to an empty scanline. |
| 139 | void advanceRuns() { |
| 140 | const size_t kRunsSz = this->getRunsSz(); |
| 141 | fCurrentRun = (fCurrentRun + 1) % fRunsToBuffer; |
| 142 | fRuns.fRuns = reinterpret_cast<int16_t*>( |
| 143 | reinterpret_cast<uint8_t*>(fRunsBuffer) + fCurrentRun * kRunsSz); |
| 144 | fRuns.fAlpha = reinterpret_cast<SkAlpha*>(fRuns.fRuns + fWidth + 1); |
| 145 | fRuns.reset(fWidth); |
| 146 | } |
| 147 | |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 148 | int fOffsetX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 151 | SuperBlitter::SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkIRect& clipBounds, |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 152 | bool isInverse) |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 153 | : BaseSuperBlitter(realBlitter, ir, clipBounds, isInverse) |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 154 | { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 155 | fRunsToBuffer = realBlitter->requestRowsPreserved(); |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 156 | fRunsBuffer = realBlitter->allocBlitMemory(fRunsToBuffer * this->getRunsSz()); |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 157 | fCurrentRun = -1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 159 | this->advanceRuns(); |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 160 | |
| 161 | fOffsetX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 164 | void SuperBlitter::flush() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 165 | if (fCurrIY >= fTop) { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 166 | |
| 167 | SkASSERT(fCurrentRun < fRunsToBuffer); |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 168 | if (!fRuns.empty()) { |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 169 | // SkDEBUGCODE(fRuns.dump();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | fRealBlitter->blitAntiH(fLeft, fCurrIY, fRuns.fAlpha, fRuns.fRuns); |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 171 | this->advanceRuns(); |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 172 | fOffsetX = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | } |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 174 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 175 | fCurrIY = fTop - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | SkDEBUGCODE(fCurrX = -1;) |
| 177 | } |
| 178 | } |
| 179 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 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) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | aa <<= 8 - 2*SHIFT; |
reed@google.com | 5b7d603 | 2012-05-09 14:07:34 +0000 | [diff] [blame] | 188 | return aa; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | } |
| 190 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 191 | /** coverage_to_exact_alpha() is being used by our blitter, which wants |
| 192 | a final value in [0, 255]. |
| 193 | */ |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 194 | static inline int coverage_to_exact_alpha(int aa) { |
tomhudson@google.com | 31bab39 | 2012-01-03 20:12:42 +0000 | [diff] [blame] | 195 | int alpha = (256 >> SHIFT) * aa; |
| 196 | // clamp 256->255 |
| 197 | return alpha - (alpha >> 8); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 198 | } |
| 199 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 200 | void SuperBlitter::blitH(int x, int y, int width) { |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 201 | SkASSERT(width > 0); |
| 202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | int iy = y >> SHIFT; |
| 204 | SkASSERT(iy >= fCurrIY); |
| 205 | |
| 206 | x -= fSuperLeft; |
| 207 | // 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] | 208 | if (x < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | width += x; |
| 210 | x = 0; |
| 211 | } |
| 212 | |
| 213 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | SkASSERT(y != fCurrY || x >= fCurrX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | #endif |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 216 | SkASSERT(y >= fCurrY); |
| 217 | if (fCurrY != y) { |
| 218 | fOffsetX = 0; |
| 219 | fCurrY = y; |
| 220 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 221 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 222 | if (iy != fCurrIY) { // new scanline |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | this->flush(); |
| 224 | fCurrIY = iy; |
| 225 | } |
| 226 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 227 | int start = x; |
| 228 | int stop = x + width; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 230 | SkASSERT(start >= 0 && stop > start); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 231 | // integer-pixel-aligned ends of blit, rounded out |
| 232 | int fb = start & MASK; |
| 233 | int fe = stop & MASK; |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 234 | int n = (stop >> SHIFT) - (start >> SHIFT) - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | |
reed@google.com | 5a1e795 | 2011-05-31 14:36:21 +0000 | [diff] [blame] | 236 | if (n < 0) { |
| 237 | fb = fe - fb; |
| 238 | n = 0; |
| 239 | fe = 0; |
| 240 | } else { |
| 241 | if (fb == 0) { |
| 242 | n += 1; |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 243 | } else { |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 244 | fb = SCALE - fb; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | } |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 247 | |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 248 | fOffsetX = fRuns.add(x >> SHIFT, coverage_to_partial_alpha(fb), |
| 249 | n, coverage_to_partial_alpha(fe), |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 250 | (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT), |
| 251 | fOffsetX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | |
| 253 | #ifdef SK_DEBUG |
| 254 | fRuns.assertValid(y & MASK, (1 << (8 - SHIFT))); |
| 255 | fCurrX = x + width; |
| 256 | #endif |
| 257 | } |
| 258 | |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 259 | #if 0 // UNUSED |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 260 | static void set_left_rite_runs(SkAlphaRuns& runs, int ileft, U8CPU leftA, |
| 261 | int n, U8CPU riteA) { |
| 262 | SkASSERT(leftA <= 0xFF); |
| 263 | SkASSERT(riteA <= 0xFF); |
| 264 | |
| 265 | int16_t* run = runs.fRuns; |
| 266 | uint8_t* aa = runs.fAlpha; |
| 267 | |
| 268 | if (ileft > 0) { |
| 269 | run[0] = ileft; |
| 270 | aa[0] = 0; |
| 271 | run += ileft; |
| 272 | aa += ileft; |
| 273 | } |
| 274 | |
| 275 | SkASSERT(leftA < 0xFF); |
| 276 | if (leftA > 0) { |
| 277 | *run++ = 1; |
| 278 | *aa++ = leftA; |
| 279 | } |
| 280 | |
| 281 | if (n > 0) { |
| 282 | run[0] = n; |
| 283 | aa[0] = 0xFF; |
| 284 | run += n; |
| 285 | aa += n; |
| 286 | } |
| 287 | |
| 288 | SkASSERT(riteA < 0xFF); |
| 289 | if (riteA > 0) { |
| 290 | *run++ = 1; |
| 291 | *aa++ = riteA; |
| 292 | } |
| 293 | run[0] = 0; |
| 294 | } |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 295 | #endif |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 296 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 297 | void SuperBlitter::blitRect(int x, int y, int width, int height) { |
| 298 | SkASSERT(width > 0); |
| 299 | SkASSERT(height > 0); |
| 300 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 301 | // blit leading rows |
| 302 | while ((y & MASK)) { |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 303 | this->blitH(x, y++, width); |
| 304 | if (--height <= 0) { |
| 305 | return; |
| 306 | } |
| 307 | } |
| 308 | SkASSERT(height > 0); |
| 309 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 310 | // Since this is a rect, instead of blitting supersampled rows one at a |
| 311 | // time and then resolving to the destination canvas, we can blit |
| 312 | // directly to the destintion canvas one row per SCALE supersampled rows. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 313 | int start_y = y >> SHIFT; |
| 314 | int stop_y = (y + height) >> SHIFT; |
| 315 | int count = stop_y - start_y; |
| 316 | if (count > 0) { |
| 317 | y += count << SHIFT; |
| 318 | height -= count << SHIFT; |
| 319 | |
| 320 | // save original X for our tail blitH() loop at the bottom |
| 321 | int origX = x; |
| 322 | |
| 323 | x -= fSuperLeft; |
| 324 | // hack, until I figure out why my cubics (I think) go beyond the bounds |
| 325 | if (x < 0) { |
| 326 | width += x; |
| 327 | x = 0; |
| 328 | } |
| 329 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 330 | // There is always a left column, a middle, and a right column. |
| 331 | // ileft is the destination x of the first pixel of the entire rect. |
| 332 | // xleft is (SCALE - # of covered supersampled pixels) in that |
| 333 | // destination pixel. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 334 | int ileft = x >> SHIFT; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 335 | int xleft = x & MASK; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 336 | // irite is the destination x of the last pixel of the OPAQUE section. |
| 337 | // xrite is the number of supersampled pixels extending beyond irite; |
| 338 | // xrite/SCALE should give us alpha. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 339 | int irite = (x + width) >> SHIFT; |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 340 | int xrite = (x + width) & MASK; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 341 | if (!xrite) { |
| 342 | xrite = SCALE; |
| 343 | irite--; |
| 344 | } |
| 345 | |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 346 | // Need to call flush() to clean up pending draws before we |
| 347 | // even consider blitV(), since otherwise it can look nonmonotonic. |
| 348 | SkASSERT(start_y > fCurrIY); |
| 349 | this->flush(); |
| 350 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 351 | int n = irite - ileft - 1; |
| 352 | if (n < 0) { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 353 | // If n < 0, we'll only have a single partially-transparent column |
| 354 | // of pixels to render. |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 355 | xleft = xrite - xleft; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 356 | SkASSERT(xleft <= SCALE); |
| 357 | SkASSERT(xleft > 0); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 358 | fRealBlitter->blitV(ileft + fLeft, start_y, count, |
| 359 | coverage_to_exact_alpha(xleft)); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 360 | } else { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 361 | // With n = 0, we have two possibly-transparent columns of pixels |
| 362 | // to render; with n > 0, we have opaque columns between them. |
| 363 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 364 | xleft = SCALE - xleft; |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 365 | |
| 366 | // Using coverage_to_exact_alpha is not consistent with blitH() |
| 367 | const int coverageL = coverage_to_exact_alpha(xleft); |
| 368 | const int coverageR = coverage_to_exact_alpha(xrite); |
| 369 | |
| 370 | SkASSERT(coverageL > 0 || n > 0 || coverageR > 0); |
| 371 | SkASSERT((coverageL != 0) + n + (coverageR != 0) <= fWidth); |
| 372 | |
| 373 | fRealBlitter->blitAntiRect(ileft + fLeft, start_y, n, count, |
| 374 | coverageL, coverageR); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 375 | } |
| 376 | |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 377 | // preamble for our next call to blitH() |
| 378 | fCurrIY = stop_y - 1; |
| 379 | fOffsetX = 0; |
| 380 | fCurrY = y - 1; |
| 381 | fRuns.reset(fWidth); |
| 382 | x = origX; |
| 383 | } |
| 384 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 385 | // catch any remaining few rows |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 386 | SkASSERT(height <= MASK); |
reed@google.com | 967a35d | 2011-10-31 19:37:58 +0000 | [diff] [blame] | 387 | while (--height >= 0) { |
| 388 | this->blitH(x, y++, width); |
| 389 | } |
| 390 | } |
| 391 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | /////////////////////////////////////////////////////////////////////////////// |
| 393 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 394 | /// Masked supersampling antialiased blitter. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | class MaskSuperBlitter : public BaseSuperBlitter { |
| 396 | public: |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 397 | MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, const SkIRect&, bool isInverse); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 398 | ~MaskSuperBlitter() override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 399 | fRealBlitter->blitMask(fMask, fClipRect); |
| 400 | } |
| 401 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 402 | void blitH(int x, int y, int width) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 404 | static bool CanHandleRect(const SkIRect& bounds) { |
| 405 | #ifdef FORCE_RLE |
| 406 | return false; |
| 407 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | int width = bounds.width(); |
reed@google.com | f9f258c | 2012-03-12 16:09:06 +0000 | [diff] [blame] | 409 | int64_t rb = SkAlign4(width); |
| 410 | // use 64bits to detect overflow |
| 411 | int64_t storage = rb * bounds.height(); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 412 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | return (width <= MaskSuperBlitter::kMAX_WIDTH) && |
reed@google.com | f9f258c | 2012-03-12 16:09:06 +0000 | [diff] [blame] | 414 | (storage <= MaskSuperBlitter::kMAX_STORAGE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 416 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 417 | private: |
| 418 | enum { |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 419 | #ifdef FORCE_SUPERMASK |
| 420 | kMAX_WIDTH = 2048, |
| 421 | kMAX_STORAGE = 1024 * 1024 * 2 |
| 422 | #else |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | kMAX_WIDTH = 32, // so we don't try to do very wide things, where the RLE blitter would be faster |
| 424 | kMAX_STORAGE = 1024 |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 425 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | SkMask fMask; |
| 429 | SkIRect fClipRect; |
| 430 | // we add 1 because add_aa_span can write (unchanged) 1 extra byte at the end, rather than |
| 431 | // perform a test to see if stopAlpha != 0 |
| 432 | uint32_t fStorage[(kMAX_STORAGE >> 2) + 1]; |
| 433 | }; |
| 434 | |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 435 | MaskSuperBlitter::MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir, |
| 436 | const SkIRect& clipBounds, bool isInverse) |
| 437 | : BaseSuperBlitter(realBlitter, ir, clipBounds, isInverse) |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 438 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 439 | SkASSERT(CanHandleRect(ir)); |
reed | bcba2c9 | 2014-10-15 08:52:00 -0700 | [diff] [blame] | 440 | SkASSERT(!isInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | |
| 442 | fMask.fImage = (uint8_t*)fStorage; |
| 443 | fMask.fBounds = ir; |
| 444 | fMask.fRowBytes = ir.width(); |
| 445 | fMask.fFormat = SkMask::kA8_Format; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 446 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | fClipRect = ir; |
Yuqian Li | 3a5e1fe | 2017-09-28 10:58:38 -0400 | [diff] [blame] | 448 | if (!fClipRect.intersect(clipBounds)) { |
robertphillips | f4e5995 | 2015-01-07 12:16:10 -0800 | [diff] [blame] | 449 | SkASSERT(0); |
| 450 | fClipRect.setEmpty(); |
| 451 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 452 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 458 | static void add_aa_span(uint8_t* alpha, U8CPU startAlpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 469 | static inline uint32_t quadplicate_byte(U8CPU value) { |
| 470 | uint32_t pair = (value << 8) | value; |
| 471 | return (pair << 16) | pair; |
| 472 | } |
| 473 | |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 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 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 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) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 490 | SkASSERT(middleCount >= 0); |
| 491 | |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 492 | saturated_add(alpha, startAlpha); |
| 493 | alpha += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 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) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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[] |
reed@google.com | 6997e5d | 2012-05-09 13:48:50 +0000 | [diff] [blame] | 524 | saturated_add(alpha, stopAlpha); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 525 | } |
| 526 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 527 | void MaskSuperBlitter::blitH(int x, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | int iy = (y >> SHIFT); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 529 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | SkASSERT(iy >= fMask.fBounds.fTop && iy < fMask.fBounds.fBottom); |
| 531 | iy -= fMask.fBounds.fTop; // make it relative to 0 |
| 532 | |
senorblanco@chromium.org | c41513c | 2009-08-21 22:00:12 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 540 | #ifdef SK_DEBUG |
| 541 | { |
| 542 | int ix = x >> SHIFT; |
| 543 | SkASSERT(ix >= fMask.fBounds.fLeft && ix < fMask.fBounds.fRight); |
| 544 | } |
| 545 | #endif |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 546 | |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 547 | x -= SkLeftShift(fMask.fBounds.fLeft, SHIFT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | |
| 549 | // 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] | 550 | if (x < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | width += x; |
| 552 | x = 0; |
| 553 | } |
| 554 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 561 | int fb = start & MASK; |
| 562 | int fe = stop & MASK; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | int n = (stop >> SHIFT) - (start >> SHIFT) - 1; |
| 564 | |
| 565 | |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 566 | if (n < 0) { |
senorblanco@chromium.org | feef49c | 2009-08-12 20:21:49 +0000 | [diff] [blame] | 567 | SkASSERT(row >= fMask.fImage); |
| 568 | SkASSERT(row < fMask.fImage + kMAX_STORAGE + 1); |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 569 | add_aa_span(row, coverage_to_partial_alpha(fe - fb)); |
reed@google.com | 9f2f0a8 | 2011-04-11 19:43:58 +0000 | [diff] [blame] | 570 | } else { |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 571 | fb = SCALE - fb; |
senorblanco@chromium.org | feef49c | 2009-08-12 20:21:49 +0000 | [diff] [blame] | 572 | SkASSERT(row >= fMask.fImage); |
| 573 | SkASSERT(row + n + 1 < fMask.fImage + kMAX_STORAGE + 1); |
tomhudson@google.com | ffc9248 | 2012-02-23 19:34:34 +0000 | [diff] [blame] | 574 | add_aa_span(row, coverage_to_partial_alpha(fb), |
| 575 | n, coverage_to_partial_alpha(fe), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 576 | (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT)); |
| 577 | } |
| 578 | |
| 579 | #ifdef SK_DEBUG |
| 580 | fCurrX = x + width; |
| 581 | #endif |
| 582 | } |
| 583 | |
| 584 | /////////////////////////////////////////////////////////////////////////////// |
| 585 | |
Yuqian Li | 7d99dad | 2017-07-26 13:47:26 -0400 | [diff] [blame] | 586 | static bool ShouldUseDAA(const SkPath& path) { |
| 587 | if (gSkForceDeltaAA) { |
| 588 | return true; |
| 589 | } |
| 590 | if (!gSkUseDeltaAA) { |
| 591 | return false; |
| 592 | } |
| 593 | const SkRect& bounds = path.getBounds(); |
| 594 | return !path.isConvex() && path.countPoints() >= SkTMax(bounds.width(), bounds.height()) / 8; |
| 595 | } |
| 596 | |
| 597 | static bool ShouldUseAAA(const SkPath& path) { |
| 598 | if (gSkForceAnalyticAA) { |
| 599 | return true; |
| 600 | } |
| 601 | if (!gSkUseAnalyticAA) { |
| 602 | return false; |
| 603 | } |
| 604 | if (path.isRect(nullptr)) { |
| 605 | return true; |
| 606 | } |
| 607 | const SkRect& bounds = path.getBounds(); |
| 608 | // When the path have so many points compared to the size of its bounds/resolution, |
| 609 | // it indicates that the path is not quite smooth in the current resolution: |
| 610 | // the expected number of turning points in every pixel row/column is significantly greater than |
| 611 | // zero. Hence Aanlytic AA is not likely to produce visible quality improvements, and Analytic |
| 612 | // AA might be slower than supersampling. |
| 613 | return path.countPoints() < SkTMax(bounds.width(), bounds.height()) / 2 - 10; |
| 614 | } |
| 615 | |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 616 | void SkScan::SAAFillPath(const SkPath& path, SkBlitter* blitter, const SkIRect& ir, |
| 617 | const SkIRect& clipBounds, bool containedInClip, bool forceRLE) { |
Yuqian Li | 75309d1 | 2017-11-09 10:07:12 -0500 | [diff] [blame] | 618 | #if !defined(SK_SUPPORT_LEGACY_AA_BEHAVIOR) |
| 619 | containedInClip = clipBounds.contains(ir); |
| 620 | #endif |
| 621 | |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 622 | bool isInverse = path.isInverseFillType(); |
| 623 | |
| 624 | // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it |
| 625 | // if we're an inverse filltype |
| 626 | if (!isInverse && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) { |
| 627 | MaskSuperBlitter superBlit(blitter, ir, clipBounds, isInverse); |
| 628 | SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop); |
| 629 | sk_fill_path(path, clipBounds, &superBlit, ir.fTop, ir.fBottom, SHIFT, containedInClip); |
| 630 | } else { |
| 631 | SuperBlitter superBlit(blitter, ir, clipBounds, isInverse); |
| 632 | sk_fill_path(path, clipBounds, &superBlit, ir.fTop, ir.fBottom, SHIFT, containedInClip); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | static bool fitsInsideLimit(const SkRect& r, SkScalar max) { |
| 637 | const SkScalar min = -max; |
| 638 | return r.fLeft > min && r.fTop > min && |
| 639 | r.fRight < max && r.fBottom < max; |
| 640 | } |
| 641 | |
| 642 | static int overflows_short_shift(int value, int shift) { |
| 643 | const int s = 16 + shift; |
| 644 | return (SkLeftShift(value, s) >> s) - value; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | Would any of the coordinates of this rectangle not fit in a short, |
| 649 | when left-shifted by shift? |
| 650 | */ |
| 651 | static int rect_overflows_short_shift(SkIRect rect, int shift) { |
| 652 | SkASSERT(!overflows_short_shift(8191, shift)); |
| 653 | SkASSERT(overflows_short_shift(8192, shift)); |
| 654 | SkASSERT(!overflows_short_shift(32767, 0)); |
| 655 | SkASSERT(overflows_short_shift(32768, 0)); |
| 656 | |
| 657 | // Since we expect these to succeed, we bit-or together |
| 658 | // for a tiny extra bit of speed. |
| 659 | return overflows_short_shift(rect.fLeft, shift) | |
| 660 | overflows_short_shift(rect.fRight, shift) | |
| 661 | overflows_short_shift(rect.fTop, shift) | |
| 662 | overflows_short_shift(rect.fBottom, shift); |
| 663 | } |
| 664 | |
| 665 | static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) { |
| 666 | const SkScalar maxScalar = SkIntToScalar(maxInt); |
| 667 | |
| 668 | if (fitsInsideLimit(src, maxScalar)) { |
| 669 | src.roundOut(dst); |
| 670 | return true; |
| 671 | } |
| 672 | return false; |
| 673 | } |
| 674 | |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 675 | void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip, |
Yuqian Li | 57899c7 | 2017-11-27 12:01:49 -0500 | [diff] [blame] | 676 | SkBlitter* blitter, bool forceRLE, bool forceDAA) { |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 677 | if (origClip.isEmpty()) { |
Yuqian Li | c8f2d19 | 2017-11-06 19:55:25 +0000 | [diff] [blame] | 678 | return; |
Yuqian Li | 12f322b | 2017-11-06 10:56:30 -0500 | [diff] [blame] | 679 | } |
| 680 | |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 681 | const bool isInverse = path.isInverseFillType(); |
| 682 | SkIRect ir; |
Yuqian Li | c8f2d19 | 2017-11-06 19:55:25 +0000 | [diff] [blame] | 683 | |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 684 | if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) { |
| 685 | // Bounds can't fit in SkIRect; we'll return without drawing |
| 686 | return; |
| 687 | } |
| 688 | if (ir.isEmpty()) { |
| 689 | if (isInverse) { |
| 690 | blitter->blitRegion(origClip); |
| 691 | } |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | // If the intersection of the path bounds and the clip bounds |
| 696 | // will overflow 32767 when << by SHIFT, we can't supersample, |
| 697 | // so draw without antialiasing. |
| 698 | SkIRect clippedIR; |
| 699 | if (isInverse) { |
| 700 | // If the path is an inverse fill, it's going to fill the entire |
| 701 | // clip, and we care whether the entire clip exceeds our limits. |
| 702 | clippedIR = origClip.getBounds(); |
| 703 | } else { |
| 704 | if (!clippedIR.intersect(ir, origClip.getBounds())) { |
| 705 | return; |
| 706 | } |
| 707 | } |
| 708 | if (rect_overflows_short_shift(clippedIR, SHIFT)) { |
| 709 | SkScan::FillPath(path, origClip, blitter); |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | // Our antialiasing can't handle a clip larger than 32767, so we restrict |
| 714 | // the clip to that limit here. (the runs[] uses int16_t for its index). |
| 715 | // |
| 716 | // A more general solution (one that could also eliminate the need to |
| 717 | // disable aa based on ir bounds (see overflows_short_shift) would be |
| 718 | // to tile the clip/target... |
| 719 | SkRegion tmpClipStorage; |
| 720 | const SkRegion* clipRgn = &origClip; |
| 721 | { |
| 722 | static const int32_t kMaxClipCoord = 32767; |
| 723 | const SkIRect& bounds = origClip.getBounds(); |
| 724 | if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) { |
| 725 | SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord }; |
| 726 | tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op); |
| 727 | clipRgn = &tmpClipStorage; |
| 728 | } |
| 729 | } |
| 730 | // for here down, use clipRgn, not origClip |
| 731 | |
| 732 | SkScanClipper clipper(blitter, clipRgn, ir); |
| 733 | |
| 734 | if (clipper.getBlitter() == nullptr) { // clipped out |
| 735 | if (isInverse) { |
| 736 | blitter->blitRegion(*clipRgn); |
| 737 | } |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | SkASSERT(clipper.getClipRect() == nullptr || |
| 742 | *clipper.getClipRect() == clipRgn->getBounds()); |
| 743 | |
| 744 | // now use the (possibly wrapped) blitter |
| 745 | blitter = clipper.getBlitter(); |
| 746 | |
| 747 | if (isInverse) { |
| 748 | sk_blit_above(blitter, ir, *clipRgn); |
| 749 | } |
| 750 | |
| 751 | SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop); |
| 752 | |
Yuqian Li | 9847418 | 2018-01-03 15:37:56 -0500 | [diff] [blame^] | 753 | #ifdef SK_SUPPORT_LEGACY_CONTAINED_IN_CLIP |
| 754 | bool containedInClip = clipper.getClipRect() == nullptr; |
| 755 | #else |
| 756 | bool containedInClip = clipRgn->getBounds().contains(ir); |
| 757 | #endif |
| 758 | |
Yuqian Li | 57899c7 | 2017-11-27 12:01:49 -0500 | [diff] [blame] | 759 | if (forceDAA || ShouldUseDAA(path)) { |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 760 | SkScan::DAAFillPath(path, blitter, ir, clipRgn->getBounds(), |
Yuqian Li | 9847418 | 2018-01-03 15:37:56 -0500 | [diff] [blame^] | 761 | containedInClip, forceRLE); |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 762 | } else if (ShouldUseAAA(path)) { |
| 763 | // Do not use AAA if path is too complicated: |
| 764 | // there won't be any speedup or significant visual improvement. |
| 765 | SkScan::AAAFillPath(path, blitter, ir, clipRgn->getBounds(), |
Yuqian Li | 9847418 | 2018-01-03 15:37:56 -0500 | [diff] [blame^] | 766 | containedInClip, forceRLE); |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 767 | } else { |
| 768 | SkScan::SAAFillPath(path, blitter, ir, clipRgn->getBounds(), |
Yuqian Li | 9847418 | 2018-01-03 15:37:56 -0500 | [diff] [blame^] | 769 | containedInClip, forceRLE); |
Yuqian Li | b49d7b0 | 2017-11-06 16:21:06 -0500 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | if (isInverse) { |
| 773 | sk_blit_below(blitter, ir, *clipRgn); |
| 774 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | } |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 776 | |
| 777 | /////////////////////////////////////////////////////////////////////////////// |
| 778 | |
| 779 | #include "SkRasterClip.h" |
| 780 | |
| 781 | void SkScan::FillPath(const SkPath& path, const SkRasterClip& clip, |
| 782 | SkBlitter* blitter) { |
| 783 | if (clip.isEmpty()) { |
| 784 | return; |
| 785 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 786 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 787 | if (clip.isBW()) { |
| 788 | FillPath(path, clip.bwRgn(), blitter); |
| 789 | } else { |
| 790 | SkRegion tmp; |
| 791 | SkAAClipBlitter aaBlitter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 792 | |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 793 | tmp.setRect(clip.getBounds()); |
| 794 | aaBlitter.init(blitter, &clip.aaRgn()); |
| 795 | SkScan::FillPath(path, tmp, &aaBlitter); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip, |
Yuqian Li | 57899c7 | 2017-11-27 12:01:49 -0500 | [diff] [blame] | 800 | SkBlitter* blitter, bool forceDAA) { |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 801 | if (clip.isEmpty()) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | if (clip.isBW()) { |
Yuqian Li | 57899c7 | 2017-11-27 12:01:49 -0500 | [diff] [blame] | 806 | AntiFillPath(path, clip.bwRgn(), blitter, false, forceDAA); |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 807 | } else { |
| 808 | SkRegion tmp; |
| 809 | SkAAClipBlitter aaBlitter; |
| 810 | |
| 811 | tmp.setRect(clip.getBounds()); |
| 812 | aaBlitter.init(blitter, &clip.aaRgn()); |
Yuqian Li | 57899c7 | 2017-11-27 12:01:49 -0500 | [diff] [blame] | 813 | AntiFillPath(path, tmp, &aaBlitter, true, forceDAA); // SkAAClipBlitter can blitMask, why forceRLE? |
reed@google.com | 1ba7137 | 2011-10-12 20:42:05 +0000 | [diff] [blame] | 814 | } |
| 815 | } |