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 | #ifndef SkBlitter_DEFINED |
| 9 | #define SkBlitter_DEFINED |
| 10 | |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 11 | #include "SkBitmapProcShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkColor.h" |
| 13 | #include "SkRect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkRegion.h" |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 15 | #include "SkShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkTypes.h" |
| 17 | |
| 18 | class SkMatrix; |
| 19 | class SkPaint; |
| 20 | class SkPixmap; |
| 21 | struct SkMask; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 23 | /** SkBlitter and its subclasses are responsible for actually writing pixels |
| 24 | into memory. Besides efficiency, they handle clipping and antialiasing. |
herb | 3be72b0 | 2016-06-24 13:02:31 -0700 | [diff] [blame^] | 25 | A SkBlitter subclass contains all the context needed to generate pixels |
| 26 | for the destination and how src/generated pixels map to the destination. |
| 27 | The coordinates passed to the blitX calls are in destination pixel space. |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 28 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | class SkBlitter { |
| 30 | public: |
| 31 | virtual ~SkBlitter(); |
| 32 | |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 33 | /// Blit a horizontal run of one or more pixels. |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 34 | virtual void blitH(int x, int y, int width) = 0; |
| 35 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 36 | /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse* |
| 37 | /// zero-terminated run-length encoding of spans of constant alpha values. |
herb | 3be72b0 | 2016-06-24 13:02:31 -0700 | [diff] [blame^] | 38 | /// The runs[] and antialias[] work together to represent long runs of pixels with the same |
| 39 | /// alphas. The runs[] contains the number of pixels with the same alpha, and antialias[] |
| 40 | /// contain the coverage value for that number of pixels. The runs[] (and antialias[]) are |
| 41 | /// encoded in a clever way. The runs array is zero terminated, and has enough entries for |
| 42 | /// each pixel plus one, in most cases some of the entries will not contain valid data. An entry |
| 43 | /// in the runs array contains the number of pixels (np) that have the same alpha value. The |
| 44 | /// next np value is found np entries away. For example, if runs[0] = 7, then the next valid |
| 45 | /// entry will by at runs[7]. The runs array and antialias[] are coupled by index. So, if the |
| 46 | /// np entry is at runs[45] = 12 then the alpha value can be found at antialias[45] = 0x88. |
| 47 | /// This would mean to use an alpha value of 0x88 for the next 12 pixels starting at pixel 45. |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 48 | virtual void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) = 0; |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 49 | |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 50 | /// Blit a vertical run of pixels with a constant alpha value. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | virtual void blitV(int x, int y, int height, SkAlpha alpha); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 52 | |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 53 | /// Blit a solid rectangle one or more pixels wide. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | virtual void blitRect(int x, int y, int width, int height); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 55 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 56 | /** Blit a rectangle with one alpha-blended column on the left, |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 57 | width (zero or more) opaque pixels, and one alpha-blended column |
| 58 | on the right. |
| 59 | The result will always be at least two pixels wide. |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 60 | */ |
| 61 | virtual void blitAntiRect(int x, int y, int width, int height, |
| 62 | SkAlpha leftAlpha, SkAlpha rightAlpha); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 63 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 64 | /// Blit a pattern of pixels defined by a rectangle-clipped mask; |
| 65 | /// typically used for text. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | virtual void blitMask(const SkMask&, const SkIRect& clip); |
| 67 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 68 | /** If the blitter just sets a single value for each pixel, return the |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | bitmap it draws into, and assign value. If not, return nullptr and ignore |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | the value parameter. |
| 71 | */ |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 72 | virtual const SkPixmap* justAnOpaqueColor(uint32_t* value); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | |
reed | 793a6dd | 2015-04-15 07:51:15 -0700 | [diff] [blame] | 74 | // (x, y), (x + 1, y) |
reed | a8ec480 | 2015-04-20 05:18:01 -0700 | [diff] [blame] | 75 | virtual void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
reed | 793a6dd | 2015-04-15 07:51:15 -0700 | [diff] [blame] | 76 | int16_t runs[3]; |
| 77 | uint8_t aa[2]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 78 | |
reed | 793a6dd | 2015-04-15 07:51:15 -0700 | [diff] [blame] | 79 | runs[0] = 1; |
| 80 | runs[1] = 1; |
| 81 | runs[2] = 0; |
| 82 | aa[0] = SkToU8(a0); |
| 83 | aa[1] = SkToU8(a1); |
| 84 | this->blitAntiH(x, y, aa, runs); |
| 85 | } |
| 86 | |
| 87 | // (x, y), (x, y + 1) |
reed | a8ec480 | 2015-04-20 05:18:01 -0700 | [diff] [blame] | 88 | virtual void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) { |
reed | 793a6dd | 2015-04-15 07:51:15 -0700 | [diff] [blame] | 89 | int16_t runs[2]; |
| 90 | uint8_t aa[1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
reed | 793a6dd | 2015-04-15 07:51:15 -0700 | [diff] [blame] | 92 | runs[0] = 1; |
| 93 | runs[1] = 0; |
| 94 | aa[0] = SkToU8(a0); |
| 95 | this->blitAntiH(x, y, aa, runs); |
| 96 | // reset in case the clipping blitter modified runs |
| 97 | runs[0] = 1; |
| 98 | runs[1] = 0; |
| 99 | aa[0] = SkToU8(a1); |
| 100 | this->blitAntiH(x, y + 1, aa, runs); |
| 101 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 102 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 103 | /** |
| 104 | * Special method just to identify the null blitter, which is returned |
| 105 | * from Choose() if the request cannot be fulfilled. Default impl |
| 106 | * returns false. |
| 107 | */ |
| 108 | virtual bool isNullBlitter() const; |
| 109 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 110 | /** |
| 111 | * Special methods for SkShaderBlitter. On all other classes this is a no-op. |
| 112 | */ |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 113 | virtual bool resetShaderContext(const SkShader::ContextRec&); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 114 | virtual SkShader::Context* getShaderContext() const; |
| 115 | |
krajcevski | 2ec93fc | 2014-07-16 13:31:41 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Special methods for blitters that can blit more than one row at a time. |
| 118 | * This function returns the number of rows that this blitter could optimally |
| 119 | * process at a time. It is still required to support blitting one scanline |
| 120 | * at a time. |
| 121 | */ |
| 122 | virtual int requestRowsPreserved() const { return 1; } |
| 123 | |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 124 | /** |
| 125 | * This function allocates memory for the blitter that the blitter then owns. |
| 126 | * The memory can be used by the calling function at will, but it will be |
| 127 | * released when the blitter's destructor is called. This function returns |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | * nullptr if no persistent memory is needed by the blitter. |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 129 | */ |
| 130 | virtual void* allocBlitMemory(size_t sz) { |
| 131 | return fBlitMemory.reset(sz, SkAutoMalloc::kReuse_OnShrink); |
| 132 | } |
| 133 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 134 | ///@name non-virtual helpers |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | void blitMaskRegion(const SkMask& mask, const SkRegion& clip); |
| 136 | void blitRectRegion(const SkIRect& rect, const SkRegion& clip); |
| 137 | void blitRegion(const SkRegion& clip); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 138 | ///@} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 140 | /** @name Factories |
| 141 | Return the correct blitter to use given the specified context. |
| 142 | */ |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 143 | static SkBlitter* Choose(const SkPixmap& dst, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | const SkMatrix& matrix, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | const SkPaint& paint, |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 146 | SkTBlitterAllocator*, |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 147 | bool drawCoverage = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 149 | static SkBlitter* ChooseSprite(const SkPixmap& dst, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | const SkPaint&, |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 151 | const SkPixmap& src, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | int left, int top, |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 153 | SkTBlitterAllocator*); |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 154 | ///@} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 156 | static SkShader::ContextRec::DstType PreferredShaderDest(const SkImageInfo&); |
| 157 | |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 158 | protected: |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 159 | SkAutoMalloc fBlitMemory; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /** This blitter silently never draws anything. |
| 163 | */ |
| 164 | class SkNullBlitter : public SkBlitter { |
| 165 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | void blitH(int x, int y, int width) override; |
reed | 3dfd133 | 2015-06-25 14:26:11 -0700 | [diff] [blame] | 167 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 168 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 169 | void blitRect(int x, int y, int width, int height) override; |
| 170 | void blitMask(const SkMask&, const SkIRect& clip) override; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 171 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 172 | bool isNullBlitter() const override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /** Wraps another (real) blitter, and ensures that the real blitter is only |
| 176 | called with coordinates that have been clipped by the specified clipRect. |
| 177 | This means the caller need not perform the clipping ahead of time. |
| 178 | */ |
| 179 | class SkRectClipBlitter : public SkBlitter { |
| 180 | public: |
| 181 | void init(SkBlitter* blitter, const SkIRect& clipRect) { |
| 182 | SkASSERT(!clipRect.isEmpty()); |
| 183 | fBlitter = blitter; |
| 184 | fClipRect = clipRect; |
| 185 | } |
| 186 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 187 | void blitH(int x, int y, int width) override; |
reed | 3dfd133 | 2015-06-25 14:26:11 -0700 | [diff] [blame] | 188 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 189 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 190 | void blitRect(int x, int y, int width, int height) override; |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 191 | virtual void blitAntiRect(int x, int y, int width, int height, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 192 | SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 193 | void blitMask(const SkMask&, const SkIRect& clip) override; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 194 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 196 | int requestRowsPreserved() const override { |
krajcevski | fdd7d2b | 2014-07-29 07:21:41 -0700 | [diff] [blame] | 197 | return fBlitter->requestRowsPreserved(); |
| 198 | } |
| 199 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 200 | void* allocBlitMemory(size_t sz) override { |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 201 | return fBlitter->allocBlitMemory(sz); |
| 202 | } |
| 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | private: |
| 205 | SkBlitter* fBlitter; |
| 206 | SkIRect fClipRect; |
| 207 | }; |
| 208 | |
| 209 | /** Wraps another (real) blitter, and ensures that the real blitter is only |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 210 | called with coordinates that have been clipped by the specified clipRgn. |
| 211 | This means the caller need not perform the clipping ahead of time. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | */ |
| 213 | class SkRgnClipBlitter : public SkBlitter { |
| 214 | public: |
| 215 | void init(SkBlitter* blitter, const SkRegion* clipRgn) { |
| 216 | SkASSERT(clipRgn && !clipRgn->isEmpty()); |
| 217 | fBlitter = blitter; |
| 218 | fRgn = clipRgn; |
| 219 | } |
| 220 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 221 | void blitH(int x, int y, int width) override; |
reed | 3dfd133 | 2015-06-25 14:26:11 -0700 | [diff] [blame] | 222 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 223 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 224 | void blitRect(int x, int y, int width, int height) override; |
reed | 3dfd133 | 2015-06-25 14:26:11 -0700 | [diff] [blame] | 225 | void blitAntiRect(int x, int y, int width, int height, |
| 226 | SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 227 | void blitMask(const SkMask&, const SkIRect& clip) override; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 228 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 230 | int requestRowsPreserved() const override { |
krajcevski | fdd7d2b | 2014-07-29 07:21:41 -0700 | [diff] [blame] | 231 | return fBlitter->requestRowsPreserved(); |
| 232 | } |
| 233 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | void* allocBlitMemory(size_t sz) override { |
krajcevski | 75f8851 | 2014-07-21 09:54:23 -0700 | [diff] [blame] | 235 | return fBlitter->allocBlitMemory(sz); |
| 236 | } |
| 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | private: |
| 239 | SkBlitter* fBlitter; |
| 240 | const SkRegion* fRgn; |
| 241 | }; |
| 242 | |
tomhudson@google.com | 05fffdc | 2011-12-01 20:41:24 +0000 | [diff] [blame] | 243 | /** Factory to set up the appropriate most-efficient wrapper blitter |
| 244 | to apply a clip. Returns a pointer to a member, so lifetime must |
| 245 | be managed carefully. |
| 246 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | class SkBlitterClipper { |
| 248 | public: |
| 249 | SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 250 | const SkIRect* bounds = nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | |
| 252 | private: |
| 253 | SkNullBlitter fNullBlitter; |
| 254 | SkRectClipBlitter fRectBlitter; |
| 255 | SkRgnClipBlitter fRgnBlitter; |
| 256 | }; |
| 257 | |
| 258 | #endif |