| 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/SkPixmap.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkRegion.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "include/private/base/SkCPUTypes.h" |
| 18 |
#include "include/private/base/SkDebug.h" |
| 19 |
#include "include/private/base/SkTo.h" |
| 20 |
#include "src/base/SkAutoMalloc.h" |
| 21 |
|
| 22 |
#include <cstddef> |
| 23 |
#include <cstdint> |
| 24 |
#include <optional> |
| 25 |
|
| 26 |
class SkArenaAlloc; |
| 27 |
class SkMatrix; |
| 28 |
class SkPaint; |
| 29 |
class SkShader; |
| 30 |
class SkSurfaceProps; |
| 31 |
struct SkMask; |
| 32 |
enum class SkDrawCoverage : bool; |
| 33 |
|
| 34 |
/** SkBlitter and its subclasses are responsible for actually writing pixels |
| 35 |
into memory. Besides efficiency, they handle clipping and antialiasing. |
| 36 |
A SkBlitter subclass contains all the context needed to generate pixels |
| 37 |
for the destination and how src/generated pixels map to the destination. |
| 38 |
The coordinates passed to the blitX calls are in destination pixel space. |
| 39 |
*/ |
| 40 |
class SkBlitter { |
| 41 |
public: |
| 42 |
virtual ~SkBlitter(); |
| 43 |
SkBlitter() = default; |
| 44 |
SkBlitter(const SkBlitter&) = delete; |
| 45 |
SkBlitter(SkBlitter&&) = delete; |
| 46 |
SkBlitter& operator=(const SkBlitter&) = delete; |
| 47 |
SkBlitter& operator=(SkBlitter&&) = delete; |
| 48 |
|
| 49 |
/// Blit a horizontal run of one or more pixels. |
| 50 |
virtual void blitH(int x, int y, int width) = 0; |
| 51 |
|
| 52 |
/// Blit a horizontal run of antialiased pixels; runs[] is a *sparse* |
| 53 |
/// zero-terminated run-length encoding of spans of constant alpha values. |
| 54 |
/// The runs[] and antialias[] work together to represent long runs of pixels with the same |
| 55 |
/// alphas. The runs[] contains the number of pixels with the same alpha, and antialias[] |
| 56 |
/// contain the coverage value for that number of pixels. The runs[] (and antialias[]) are |
| 57 |
/// encoded in a clever way. The runs array is zero terminated, and has enough entries for |
| 58 |
/// each pixel plus one, in most cases some of the entries will not contain valid data. An entry |
| 59 |
/// in the runs array contains the number of pixels (np) that have the same alpha value. The |
| 60 |
/// next np value is found np entries away. For example, if runs[0] = 7, then the next valid |
| 61 |
/// entry will by at runs[7]. The runs array and antialias[] are coupled by index. So, if the |
| 62 |
/// np entry is at runs[45] = 12 then the alpha value can be found at antialias[45] = 0x88. |
| 63 |
/// This would mean to use an alpha value of 0x88 for the next 12 pixels starting at pixel 45. |
| 64 |
virtual void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) = 0; |
| 65 |
|
| 66 |
/// Blit a vertical run of pixels with a constant alpha value. |
| 67 |
virtual void blitV(int x, int y, int height, SkAlpha alpha); |
| 68 |
|
| 69 |
/// Blit a solid rectangle one or more pixels wide. |
| 70 |
virtual void blitRect(int x, int y, int width, int height); |
| 71 |
|
| 72 |
/** Blit a rectangle with one alpha-blended column on the left, |
| 73 |
width (zero or more) opaque pixels, and one alpha-blended column |
| 74 |
on the right. |
| 75 |
The result will always be at least two pixels wide. |
| 76 |
*/ |
| 77 |
virtual void blitAntiRect(int x, int y, int width, int height, |
| 78 |
SkAlpha leftAlpha, SkAlpha rightAlpha); |
| 79 |
|
| 80 |
// Blit a rect in AA with size at least 3 x 3 (small rect has too many edge cases...) |
| 81 |
void blitFatAntiRect(const SkRect& rect); |
| 82 |
|
| 83 |
/// Blit a pattern of pixels defined by a rectangle-clipped mask; |
| 84 |
/// typically used for text. |
| 85 |
virtual void blitMask(const SkMask&, const SkIRect& clip); |
| 86 |
|
| 87 |
// (x, y), (x + 1, y) |
| 88 |
virtual void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
| 89 |
int16_t runs[3]; |
| 90 |
uint8_t aa[2]; |
| 91 |
|
| 92 |
runs[0] = 1; |
| 93 |
runs[1] = 1; |
| 94 |
runs[2] = 0; |
| 95 |
aa[0] = SkToU8(a0); |
| 96 |
aa[1] = SkToU8(a1); |
| 97 |
this->blitAntiH(x, y, aa, runs); |
| 98 |
} |
| 99 |
|
| 100 |
// (x, y), (x, y + 1) |
| 101 |
virtual void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) { |
| 102 |
int16_t runs[2]; |
| 103 |
uint8_t aa[1]; |
| 104 |
|
| 105 |
runs[0] = 1; |
| 106 |
runs[1] = 0; |
| 107 |
aa[0] = SkToU8(a0); |
| 108 |
this->blitAntiH(x, y, aa, runs); |
| 109 |
// reset in case the clipping blitter modified runs |
| 110 |
runs[0] = 1; |
| 111 |
runs[1] = 0; |
| 112 |
aa[0] = SkToU8(a1); |
| 113 |
this->blitAntiH(x, y + 1, aa, runs); |
| 114 |
} |
| 115 |
|
| 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 |
|
| 124 |
|
| 125 |
struct DirectBlit { |
| 126 |
SkPixmap pm; |
| 127 |
uint64_t value; // low bits match pixmap's bitdepth |
| 128 |
}; |
| 129 |
virtual std::optional<DirectBlit> canDirectBlit() { return {}; } |
| 130 |
|
| 131 |
/** |
| 132 |
* This function allocates memory for the blitter that the blitter then owns. |
| 133 |
* The memory can be used by the calling function at will, but it will be |
| 134 |
* released when the blitter's destructor is called. This function returns |
| 135 |
* nullptr if no persistent memory is needed by the blitter. |
| 136 |
*/ |
| 137 |
virtual void* allocBlitMemory(size_t sz) { |
| 138 |
return fBlitMemory.reset(sz, SkAutoMalloc::kReuse_OnShrink); |
| 139 |
} |
| 140 |
|
| 141 |
///@name non-virtual helpers |
| 142 |
void blitMaskRegion(const SkMask& mask, const SkRegion& clip); |
| 143 |
void blitRectRegion(const SkIRect& rect, const SkRegion& clip); |
| 144 |
void blitRegion(const SkRegion& clip); |
| 145 |
///@} |
| 146 |
|
| 147 |
/** @name Factories |
| 148 |
Return the correct blitter to use given the specified context. |
| 149 |
*/ |
| 150 |
static SkBlitter* Choose(const SkPixmap& dst, |
| 151 |
const SkMatrix& ctm, |
| 152 |
const SkPaint& paint, |
| 153 |
SkArenaAlloc*, |
| 154 |
SkDrawCoverage, |
| 155 |
sk_sp<SkShader> clipShader, |
| 156 |
const SkSurfaceProps& props, |
| 157 |
const SkRect& devBounds); |
| 158 |
|
| 159 |
static SkBlitter* ChooseSprite(const SkPixmap& dst, |
| 160 |
const SkPaint&, |
| 161 |
const SkPixmap& src, |
| 162 |
int left, int top, |
| 163 |
SkArenaAlloc*, sk_sp<SkShader> clipShader); |
| 164 |
///@} |
| 165 |
|
| 166 |
static bool UseLegacyBlitter(const SkPixmap&, const SkPaint&, const SkMatrix&); |
| 167 |
|
| 168 |
protected: |
| 169 |
SkAutoMalloc fBlitMemory; |
| 170 |
}; |
| 171 |
|
| 172 |
/** This blitter silently never draws anything. |
| 173 |
*/ |
| 174 |
class SkNullBlitter final : public SkBlitter { |
| 175 |
public: |
| 176 |
void blitH(int x, int y, int width) override {} |
| 177 |
void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override {} |
| 178 |
void blitV(int x, int y, int height, SkAlpha alpha) override {} |
| 179 |
void blitRect(int x, int y, int width, int height) override {} |
| 180 |
void blitMask(const SkMask&, const SkIRect& clip) override {} |
| 181 |
}; |
| 182 |
|
| 183 |
/** Wraps another (real) blitter, and ensures that the real blitter is only |
| 184 |
called with coordinates that have been clipped by the specified clipRect. |
| 185 |
This means the caller need not perform the clipping ahead of time. |
| 186 |
*/ |
| 187 |
class SkRectClipBlitter final : public SkBlitter { |
| 188 |
public: |
| 189 |
void init(SkBlitter* blitter, const SkIRect& clipRect) { |
| 190 |
SkASSERT(!clipRect.isEmpty()); |
| 191 |
fBlitter = blitter; |
| 192 |
fClipRect = clipRect; |
| 193 |
} |
| 194 |
|
| 195 |
void blitH(int x, int y, int width) override; |
| 196 |
void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 197 |
void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 198 |
void blitRect(int x, int y, int width, int height) override; |
| 199 |
void blitAntiRect(int x, int y, int width, int height, |
| 200 |
SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 201 |
void blitMask(const SkMask&, const SkIRect& clip) override; |
| 202 |
|
| 203 |
int requestRowsPreserved() const override { |
| 204 |
return fBlitter->requestRowsPreserved(); |
| 205 |
} |
| 206 |
|
| 207 |
void* allocBlitMemory(size_t sz) override { |
| 208 |
return fBlitter->allocBlitMemory(sz); |
| 209 |
} |
| 210 |
|
| 211 |
private: |
| 212 |
SkBlitter* fBlitter; |
| 213 |
SkIRect fClipRect; |
| 214 |
}; |
| 215 |
|
| 216 |
/** Wraps another (real) blitter, and ensures that the real blitter is only |
| 217 |
called with coordinates that have been clipped by the specified clipRgn. |
| 218 |
This means the caller need not perform the clipping ahead of time. |
| 219 |
*/ |
| 220 |
class SkRgnClipBlitter final : public SkBlitter { |
| 221 |
public: |
| 222 |
void init(SkBlitter* blitter, const SkRegion* clipRgn) { |
| 223 |
SkASSERT(clipRgn && !clipRgn->isEmpty()); |
| 224 |
fBlitter = blitter; |
| 225 |
fRgn = clipRgn; |
| 226 |
} |
| 227 |
|
| 228 |
void blitH(int x, int y, int width) override; |
| 229 |
void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 230 |
void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 231 |
void blitRect(int x, int y, int width, int height) override; |
| 232 |
void blitAntiRect(int x, int y, int width, int height, |
| 233 |
SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 234 |
void blitMask(const SkMask&, const SkIRect& clip) override; |
| 235 |
|
| 236 |
int requestRowsPreserved() const override { |
| 237 |
return fBlitter->requestRowsPreserved(); |
| 238 |
} |
| 239 |
|
| 240 |
void* allocBlitMemory(size_t sz) override { |
| 241 |
return fBlitter->allocBlitMemory(sz); |
| 242 |
} |
| 243 |
|
| 244 |
private: |
| 245 |
SkBlitter* fBlitter; |
| 246 |
const SkRegion* fRgn; |
| 247 |
}; |
| 248 |
|
| 249 |
#ifdef SK_DEBUG |
| 250 |
class SkRectClipCheckBlitter final : public SkBlitter { |
| 251 |
public: |
| 252 |
void init(SkBlitter* blitter, const SkIRect& clipRect) { |
| 253 |
SkASSERT(blitter); |
| 254 |
SkASSERT(!clipRect.isEmpty()); |
| 255 |
fBlitter = blitter; |
| 256 |
fClipRect = clipRect; |
| 257 |
} |
| 258 |
|
| 259 |
void blitH(int x, int y, int width) override; |
| 260 |
void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override; |
| 261 |
void blitV(int x, int y, int height, SkAlpha alpha) override; |
| 262 |
void blitRect(int x, int y, int width, int height) override; |
| 263 |
void blitAntiRect(int x, int y, int width, int height, |
| 264 |
SkAlpha leftAlpha, SkAlpha rightAlpha) override; |
| 265 |
void blitMask(const SkMask&, const SkIRect& clip) override; |
| 266 |
void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override; |
| 267 |
void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override; |
| 268 |
|
| 269 |
int requestRowsPreserved() const override { |
| 270 |
return fBlitter->requestRowsPreserved(); |
| 271 |
} |
| 272 |
|
| 273 |
void* allocBlitMemory(size_t sz) override { |
| 274 |
return fBlitter->allocBlitMemory(sz); |
| 275 |
} |
| 276 |
|
| 277 |
private: |
| 278 |
SkBlitter* fBlitter; |
| 279 |
SkIRect fClipRect; |
| 280 |
}; |
| 281 |
#endif |
| 282 |
|
| 283 |
/** Factory to set up the appropriate most-efficient wrapper blitter |
| 284 |
to apply a clip. Returns a pointer to a member, so lifetime must |
| 285 |
be managed carefully. |
| 286 |
*/ |
| 287 |
class SkBlitterClipper { |
| 288 |
public: |
| 289 |
SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip, |
| 290 |
const SkIRect* bounds = nullptr); |
| 291 |
|
| 292 |
private: |
| 293 |
SkNullBlitter fNullBlitter; |
| 294 |
SkRectClipBlitter fRectBlitter; |
| 295 |
SkRgnClipBlitter fRgnBlitter; |
| 296 |
}; |
| 297 |
|
| 298 |
#endif |