| 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 | |
| 8 | #ifndef SkBlitter_DEFINED |
| 9 | #define SkBlitter_DEFINED |
| 10 | |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkRegion.h" |
| 14 | #include "include/private/SkTo.h" |
| 15 | #include "src/core/SkAutoMalloc.h" |
| 16 | #include "src/core/SkImagePriv.h" |
| 17 | #include "src/shaders/SkShaderBase.h" |
| 18 | |
| 19 | class SkArenaAlloc; |
| 20 | class SkMatrix; |
| 21 | class SkPaint; |
| 22 | class SkPixmap; |
| 23 | struct SkMask; |
| 24 | |
| 25 | /** SkBlitter and its subclasses are responsible for actually writing pixels |
| 26 | into memory. Besides efficiency, they handle clipping and antialiasing. |
| 27 | A SkBlitter subclass contains all the context needed to generate pixels |
| 28 | for the destination and how src/generated pixels map to the destination. |
| 29 | The coordinates passed to the blitX calls are in destination pixel space. |
| 30 | */ |
| 31 | class SkBlitter { |
| 32 | public: |
| 33 | virtual ~SkBlitter(); |
| 34 | |
| 35 | /// Blit a horizontal run of one or more pixels. |
| 36 | virtual void blitH(int x, int y, int width) = 0; |
| 37 | |
| 38 | /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse* |
| 39 | /// zero-terminated run-length encoding of spans of constant alpha values. |
| 40 | /// The runs[] and antialias[] work together to represent long runs of pixels with the same |
| 41 | /// alphas. The runs[] contains the number of pixels with the same alpha, and antialias[] |
| 42 | /// contain the coverage value for that number of pixels. The runs[] (and antialias[]) are |
| 43 | /// encoded in a clever way. The runs array is zero terminated, and has enough entries for |
| 44 | /// each pixel plus one, in most cases some of the entries will not contain valid data. An entry |
| 45 | /// in the runs array contains the number of pixels (np) that have the same alpha value. The |
| 46 | /// next np value is found np entries away. For example, if runs[0] = 7, then the next valid |
| 47 | /// entry will by at runs[7]. The runs array and antialias[] are coupled by index. So, if the |
| 48 | /// np entry is at runs[45] = 12 then the alpha value can be found at antialias[45] = 0x88. |
| 49 | /// This would mean to use an alpha value of 0x88 for the next 12 pixels starting at pixel 45. |
| 50 | virtual void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) = 0; |
| 51 | |
| 52 | /// Blit a vertical run of pixels with a constant alpha value. |
| 53 | virtual void blitV(int x, int y, int height, SkAlpha alpha); |
| 54 | |
| 55 | /// Blit a solid rectangle one or more pixels wide. |
| 56 | virtual void blitRect(int x, int y, int width, int height); |
| 57 | |
| 58 | /** Blit a rectangle with one alpha-blended column on the left, |
| 59 | width (zero or more) opaque pixels, and one alpha-blended column |
| 60 | on the right. |
| 61 | The result will always be at least two pixels wide. |
| 62 | */ |
| 63 | virtual void blitAntiRect(int x, int y, int width, int height, |
| 64 | SkAlpha leftAlpha, SkAlpha rightAlpha); |
| 65 | |
| 66 | // Blit a rect in AA with size at least 3 x 3 (small rect has too many edge cases...) |
| 67 | void blitFatAntiRect(const SkRect& rect); |
| 68 | |
| 69 | /// Blit a pattern of pixels defined by a rectangle-clipped mask; |
| 70 | /// typically used for text. |
| 71 | virtual void blitMask(const SkMask&, const SkIRect& clip); |
| 72 | |
| 73 | /** If the blitter just sets a single value for each pixel, return the |
| 74 | bitmap it draws into, and assign value. If not, return nullptr and ignore |
| 75 | the value parameter. |
| 76 | */ |
| 77 | virtual const SkPixmap* justAnOpaqueColor(uint32_t* value); |
| 78 | |
| 79 | // (x, y), (x + 1, y) |
| 80 | virtual void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
| 81 | int16_t runs[3]; |
| 82 | uint8_t aa[2]; |
| 83 | |
| 84 | runs[0] = 1; |
| 85 | runs[1] = 1; |
| 86 | runs[2] = 0; |
| 87 | aa[0] = SkToU8(a0); |
| 88 | aa[1] = SkToU8(a1); |
| 89 | this->blitAntiH(x, y, aa, runs); |
| 90 | } |
| 91 | |
| 92 | // (x, y), (x, y + 1) |
| 93 | virtual void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) { |
| 94 | int16_t runs[2]; |
| 95 | uint8_t aa[1]; |
| 96 | |
| 97 | runs[0] = 1; |
| 98 | runs[1] = 0; |
| 99 | aa[0] = SkToU8(a0); |
| 100 | this->blitAntiH(x, y, aa, runs); |
| 101 | // reset in case the clipping blitter modified runs |
| 102 | runs[0] = 1; |
| 103 | runs[1] = 0; |
| 104 | aa[0] = SkToU8(a1); |
| 105 | this->blitAntiH(x, y + 1, aa, runs); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Special method just to identify the null blitter, which is returned |
| 110 | * from Choose() if the request cannot be fulfilled. Default impl |
| 111 | * returns false. |
| 112 | */ |
| 113 | virtual bool isNullBlitter() const; |
| 114 | |
| 115 | /** |
| 116 | * Special methods for blitters that can blit more than one row at a time. |
| 117 | * This function returns the number of rows that this blitter could optimally |
| 118 | * process at a time. It is still required to support blitting one scanline |
| 119 | * at a time. |
| 120 | */ |
| 121 | virtual int requestRowsPreserved() const { return 1; } |
| 122 | |
| 123 | /** |
| 124 | * This function allocates memory for the blitter that the blitter then owns. |
| 125 | * The memory can be used by the calling function at will, but it will be |
| 126 | * released when the blitter's destructor is called. This function returns |
| 127 | * nullptr if no persistent memory is needed by the blitter. |
| 128 | */ |
| 129 | virtual void* allocBlitMemory(size_t sz) { |
| 130 | return fBlitMemory.reset(sz, SkAutoMalloc::kReuse_OnShrink); |
| 131 | } |
| 132 | |
| 133 | ///@name non-virtual helpers |
| 134 | void blitMaskRegion(const SkMask& mask, const SkRegion& clip); |
| 135 | void blitRectRegion(const SkIRect& rect, const SkRegion& clip); |
| 136 | void blitRegion(const SkRegion& clip); |
| 137 | ///@} |
| 138 | |
| 139 | /** @name Factories |
| 140 | Return the correct blitter to use given the specified context. |
| 141 | */ |
| 142 | static SkBlitter* Choose(const SkPixmap& dst, |
| 143 | const SkMatrix& matrix, |
| 144 | const SkPaint& paint, |
| 145 | SkArenaAlloc*, |
| 146 | bool drawCoverage, |
| 147 | sk_sp<SkShader> clipShader); |
| 148 | |
| 149 | static SkBlitter* ChooseSprite(const SkPixmap& dst, |
| 150 | const SkPaint&, |
| 151 | const SkPixmap& src, |
| 152 | int left, int top, |
| 153 | SkArenaAlloc*, sk_sp<SkShader> clipShader); |
| 154 | ///@} |
| 155 | |
| 156 | static bool UseRasterPipelineBlitter(const SkPixmap&, const SkPaint&, const SkMatrix&); |
| 157 | |
| 158 | protected: |
| 159 | SkAutoMalloc fBlitMemory; |
| 160 | }; |
| 161 | |
| 162 | /** This blitter silently never draws anything. |
| 163 | */ |
| 164 | class SkNullBlitter : public SkBlitter { |
| 165 | public: |
| 166 | void blitH(int x, int y, int width) override; |
| 167 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 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; |
| 171 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
| 172 | bool isNullBlitter() const override; |
| 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 | |
| 187 | void blitH(int x, int y, int width) override; |
| 188 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 189 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 190 | void blitRect(int x, int y, int width, int height) override; |
| 191 | virtual void blitAntiRect(int x, int y, int width, int height, |
| 192 | SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 193 | void blitMask(const SkMask&, const SkIRect& clip) override; |
| 194 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
| 195 | |
| 196 | int requestRowsPreserved() const override { |
| 197 | return fBlitter->requestRowsPreserved(); |
| 198 | } |
| 199 | |
| 200 | void* allocBlitMemory(size_t sz) override { |
| 201 | return fBlitter->allocBlitMemory(sz); |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | SkBlitter* fBlitter; |
| 206 | SkIRect fClipRect; |
| 207 | }; |
| 208 | |
| 209 | /** Wraps another (real) blitter, and ensures that the real blitter is only |
| 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. |
| 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 | |
| 221 | void blitH(int x, int y, int width) override; |
| 222 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 223 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 224 | void blitRect(int x, int y, int width, int height) override; |
| 225 | void blitAntiRect(int x, int y, int width, int height, |
| 226 | SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 227 | void blitMask(const SkMask&, const SkIRect& clip) override; |
| 228 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
| 229 | |
| 230 | int requestRowsPreserved() const override { |
| 231 | return fBlitter->requestRowsPreserved(); |
| 232 | } |
| 233 | |
| 234 | void* allocBlitMemory(size_t sz) override { |
| 235 | return fBlitter->allocBlitMemory(sz); |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | SkBlitter* fBlitter; |
| 240 | const SkRegion* fRgn; |
| 241 | }; |
| 242 | |
| 243 | #ifdef SK_DEBUG |
| 244 | class SkRectClipCheckBlitter : public SkBlitter { |
| 245 | public: |
| 246 | void init(SkBlitter* blitter, const SkIRect& clipRect) { |
| 247 | SkASSERT(blitter); |
| 248 | SkASSERT(!clipRect.isEmpty()); |
| 249 | fBlitter = blitter; |
| 250 | fClipRect = clipRect; |
| 251 | } |
| 252 | |
| 253 | void blitH(int x, int y, int width) override; |
| 254 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 255 | void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 256 | void blitRect(int x, int y, int width, int height) override; |
| 257 | void blitAntiRect(int x, int y, int width, int height, |
| 258 | SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 259 | void blitMask(const SkMask&, const SkIRect& clip) override; |
| 260 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override; |
| 261 | void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override; |
| 262 | void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override; |
| 263 | |
| 264 | int requestRowsPreserved() const override { |
| 265 | return fBlitter->requestRowsPreserved(); |
| 266 | } |
| 267 | |
| 268 | void* allocBlitMemory(size_t sz) override { |
| 269 | return fBlitter->allocBlitMemory(sz); |
| 270 | } |
| 271 | |
| 272 | private: |
| 273 | SkBlitter* fBlitter; |
| 274 | SkIRect fClipRect; |
| 275 | }; |
| 276 | #endif |
| 277 | |
| 278 | /** Factory to set up the appropriate most-efficient wrapper blitter |
| 279 | to apply a clip. Returns a pointer to a member, so lifetime must |
| 280 | be managed carefully. |
| 281 | */ |
| 282 | class SkBlitterClipper { |
| 283 | public: |
| 284 | SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip, |
| 285 | const SkIRect* bounds = nullptr); |
| 286 | |
| 287 | private: |
| 288 | SkNullBlitter fNullBlitter; |
| 289 | SkRectClipBlitter fRectBlitter; |
| 290 | SkRgnClipBlitter fRgnBlitter; |
| 291 | }; |
| 292 | |
| 293 | #define SHARD(code) fA->code; fB->code; |
| 294 | |
| 295 | class SkPairBlitter : public SkBlitter { |
| 296 | SkBlitter* fA = nullptr; |
| 297 | SkBlitter* fB = nullptr; |
| 298 | public: |
| 299 | SkPairBlitter(SkBlitter* a, SkBlitter* b) : fA(a), fB(b) {} |
| 300 | |
| 301 | void blitH(int x, int y, int width) override { SHARD(blitH(x, y, width)) } |
| 302 | void blitAntiH(int x, int y, const SkAlpha alphas[], const int16_t runs[]) override { |
| 303 | SHARD(blitAntiH(x, y, alphas, runs)) |
| 304 | } |
| 305 | void blitV(int x, int y, int height, SkAlpha alpha) override { |
| 306 | SHARD(blitV(x, y, height, alpha)) |
| 307 | } |
| 308 | void blitRect(int x, int y, int width, int height) override { |
| 309 | SHARD(blitRect(x, y, width, height)) |
| 310 | } |
| 311 | void blitAntiRect(int x, int y, int width, int height, |
| 312 | SkAlpha leftAlpha, SkAlpha rightAlpha) override { |
| 313 | SHARD(blitAntiRect(x, y, width, height, leftAlpha, rightAlpha)) |
| 314 | } |
| 315 | void blitMask(const SkMask& mask, const SkIRect& clip) override { SHARD(blitMask(mask, clip)) } |
| 316 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override { return nullptr; } |
| 317 | void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override { SHARD(blitAntiH2(x, y, a0, a1)) } |
| 318 | void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override { SHARD(blitAntiV2(x, y, a0, a1)) } |
| 319 | }; |
| 320 | #undef SHARD |
| 321 | |
| 322 | #endif |
| 323 | |