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