blob: 1fc49594c91e510ef780e8bba9fb687299109b75 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkBlitter_DEFINED
9#define SkBlitter_DEFINED
10
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000012#include "SkBitmapProcShader.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkColor.h"
14#include "SkRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000016#include "SkShader.h"
bungemand3ebb482015-08-05 13:57:49 -070017
Herb Derby57bfa022017-02-09 17:25:43 -050018class SkArenaAlloc;
bungemand3ebb482015-08-05 13:57:49 -070019class SkMatrix;
20class SkPaint;
21class SkPixmap;
22struct SkMask;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000024/** SkBlitter and its subclasses are responsible for actually writing pixels
25 into memory. Besides efficiency, they handle clipping and antialiasing.
herb3be72b02016-06-24 13:02:31 -070026 A SkBlitter subclass contains all the context needed to generate pixels
27 for the destination and how src/generated pixels map to the destination.
28 The coordinates passed to the blitX calls are in destination pixel space.
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000029*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkBlitter {
31public:
32 virtual ~SkBlitter();
33
tomhudson@google.coma31ac732011-12-29 16:09:31 +000034 /// Blit a horizontal run of one or more pixels.
herb7df9e4a2016-06-10 13:01:27 -070035 virtual void blitH(int x, int y, int width) = 0;
36
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000037 /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse*
38 /// zero-terminated run-length encoding of spans of constant alpha values.
herb3be72b02016-06-24 13:02:31 -070039 /// The runs[] and antialias[] work together to represent long runs of pixels with the same
40 /// alphas. The runs[] contains the number of pixels with the same alpha, and antialias[]
41 /// contain the coverage value for that number of pixels. The runs[] (and antialias[]) are
42 /// encoded in a clever way. The runs array is zero terminated, and has enough entries for
43 /// each pixel plus one, in most cases some of the entries will not contain valid data. An entry
44 /// in the runs array contains the number of pixels (np) that have the same alpha value. The
45 /// next np value is found np entries away. For example, if runs[0] = 7, then the next valid
46 /// entry will by at runs[7]. The runs array and antialias[] are coupled by index. So, if the
47 /// np entry is at runs[45] = 12 then the alpha value can be found at antialias[45] = 0x88.
48 /// This would mean to use an alpha value of 0x88 for the next 12 pixels starting at pixel 45.
herb7df9e4a2016-06-10 13:01:27 -070049 virtual void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) = 0;
krajcevski2ec93fc2014-07-16 13:31:41 -070050
reed@google.coma89c77b2011-12-01 21:47:26 +000051 /// Blit a vertical run of pixels with a constant alpha value.
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 virtual void blitV(int x, int y, int height, SkAlpha alpha);
herb7df9e4a2016-06-10 13:01:27 -070053
tomhudson@google.coma31ac732011-12-29 16:09:31 +000054 /// Blit a solid rectangle one or more pixels wide.
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 virtual void blitRect(int x, int y, int width, int height);
herb7df9e4a2016-06-10 13:01:27 -070056
tomhudson@google.com49eac192011-12-27 13:59:20 +000057 /** Blit a rectangle with one alpha-blended column on the left,
tomhudson@google.com47143592011-12-28 17:58:07 +000058 width (zero or more) opaque pixels, and one alpha-blended column
59 on the right.
60 The result will always be at least two pixels wide.
tomhudson@google.com49eac192011-12-27 13:59:20 +000061 */
62 virtual void blitAntiRect(int x, int y, int width, int height,
63 SkAlpha leftAlpha, SkAlpha rightAlpha);
herb7df9e4a2016-06-10 13:01:27 -070064
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000065 /// Blit a pattern of pixels defined by a rectangle-clipped mask;
66 /// typically used for text.
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 virtual void blitMask(const SkMask&, const SkIRect& clip);
68
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000069 /** If the blitter just sets a single value for each pixel, return the
halcanary96fcdcc2015-08-27 07:41:13 -070070 bitmap it draws into, and assign value. If not, return nullptr and ignore
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 the value parameter.
72 */
reed41e010c2015-06-09 12:16:53 -070073 virtual const SkPixmap* justAnOpaqueColor(uint32_t* value);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
reed793a6dd2015-04-15 07:51:15 -070075 // (x, y), (x + 1, y)
reeda8ec4802015-04-20 05:18:01 -070076 virtual void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
reed793a6dd2015-04-15 07:51:15 -070077 int16_t runs[3];
78 uint8_t aa[2];
halcanary9d524f22016-03-29 09:03:52 -070079
reed793a6dd2015-04-15 07:51:15 -070080 runs[0] = 1;
81 runs[1] = 1;
82 runs[2] = 0;
83 aa[0] = SkToU8(a0);
84 aa[1] = SkToU8(a1);
85 this->blitAntiH(x, y, aa, runs);
86 }
87
88 // (x, y), (x, y + 1)
reeda8ec4802015-04-20 05:18:01 -070089 virtual void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
reed793a6dd2015-04-15 07:51:15 -070090 int16_t runs[2];
91 uint8_t aa[1];
halcanary9d524f22016-03-29 09:03:52 -070092
reed793a6dd2015-04-15 07:51:15 -070093 runs[0] = 1;
94 runs[1] = 0;
95 aa[0] = SkToU8(a0);
96 this->blitAntiH(x, y, aa, runs);
97 // reset in case the clipping blitter modified runs
98 runs[0] = 1;
99 runs[1] = 0;
100 aa[0] = SkToU8(a1);
101 this->blitAntiH(x, y + 1, aa, runs);
102 }
halcanary9d524f22016-03-29 09:03:52 -0700103
reed@google.comea033602012-12-14 13:13:55 +0000104 /**
105 * Special method just to identify the null blitter, which is returned
106 * from Choose() if the request cannot be fulfilled. Default impl
107 * returns false.
108 */
109 virtual bool isNullBlitter() const;
110
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000111 /**
krajcevski2ec93fc2014-07-16 13:31:41 -0700112 * Special methods for blitters that can blit more than one row at a time.
113 * This function returns the number of rows that this blitter could optimally
114 * process at a time. It is still required to support blitting one scanline
115 * at a time.
116 */
117 virtual int requestRowsPreserved() const { return 1; }
118
krajcevski75f88512014-07-21 09:54:23 -0700119 /**
120 * This function allocates memory for the blitter that the blitter then owns.
121 * The memory can be used by the calling function at will, but it will be
122 * released when the blitter's destructor is called. This function returns
halcanary96fcdcc2015-08-27 07:41:13 -0700123 * nullptr if no persistent memory is needed by the blitter.
krajcevski75f88512014-07-21 09:54:23 -0700124 */
125 virtual void* allocBlitMemory(size_t sz) {
126 return fBlitMemory.reset(sz, SkAutoMalloc::kReuse_OnShrink);
127 }
128
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000129 ///@name non-virtual helpers
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 void blitMaskRegion(const SkMask& mask, const SkRegion& clip);
131 void blitRectRegion(const SkIRect& rect, const SkRegion& clip);
132 void blitRegion(const SkRegion& clip);
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000133 ///@}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000135 /** @name Factories
136 Return the correct blitter to use given the specified context.
137 */
reed41e010c2015-06-09 12:16:53 -0700138 static SkBlitter* Choose(const SkPixmap& dst,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 const SkMatrix& matrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 const SkPaint& paint,
Herb Derby83e939b2017-02-07 14:25:11 -0500141 SkArenaAlloc*,
reed@google.com126f7f52013-11-07 16:06:53 +0000142 bool drawCoverage = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
reedcb674142015-06-05 06:58:22 -0700144 static SkBlitter* ChooseSprite(const SkPixmap& dst,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 const SkPaint&,
reedc240e712015-05-23 12:26:41 -0700146 const SkPixmap& src,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 int left, int top,
Herb Derby57bfa022017-02-09 17:25:43 -0500148 SkArenaAlloc*);
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000149 ///@}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150
fmalitad0c4e092016-02-22 17:19:04 -0800151 static SkShader::ContextRec::DstType PreferredShaderDest(const SkImageInfo&);
152
krajcevski75f88512014-07-21 09:54:23 -0700153protected:
krajcevski75f88512014-07-21 09:54:23 -0700154 SkAutoMalloc fBlitMemory;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155};
156
157/** This blitter silently never draws anything.
158*/
159class SkNullBlitter : public SkBlitter {
160public:
mtklein36352bf2015-03-25 18:17:31 -0700161 void blitH(int x, int y, int width) override;
reed3dfd1332015-06-25 14:26:11 -0700162 void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
mtklein36352bf2015-03-25 18:17:31 -0700163 void blitV(int x, int y, int height, SkAlpha alpha) override;
164 void blitRect(int x, int y, int width, int height) override;
165 void blitMask(const SkMask&, const SkIRect& clip) override;
reed41e010c2015-06-09 12:16:53 -0700166 const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
mtklein36352bf2015-03-25 18:17:31 -0700167 bool isNullBlitter() const override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168};
169
170/** Wraps another (real) blitter, and ensures that the real blitter is only
171 called with coordinates that have been clipped by the specified clipRect.
172 This means the caller need not perform the clipping ahead of time.
173*/
174class SkRectClipBlitter : public SkBlitter {
175public:
176 void init(SkBlitter* blitter, const SkIRect& clipRect) {
177 SkASSERT(!clipRect.isEmpty());
178 fBlitter = blitter;
179 fClipRect = clipRect;
180 }
181
mtklein36352bf2015-03-25 18:17:31 -0700182 void blitH(int x, int y, int width) override;
reed3dfd1332015-06-25 14:26:11 -0700183 void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
mtklein36352bf2015-03-25 18:17:31 -0700184 void blitV(int x, int y, int height, SkAlpha alpha) override;
185 void blitRect(int x, int y, int width, int height) override;
tomhudson@google.com49eac192011-12-27 13:59:20 +0000186 virtual void blitAntiRect(int x, int y, int width, int height,
mtklein36352bf2015-03-25 18:17:31 -0700187 SkAlpha leftAlpha, SkAlpha rightAlpha) override;
188 void blitMask(const SkMask&, const SkIRect& clip) override;
reed41e010c2015-06-09 12:16:53 -0700189 const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
mtklein36352bf2015-03-25 18:17:31 -0700191 int requestRowsPreserved() const override {
krajcevskifdd7d2b2014-07-29 07:21:41 -0700192 return fBlitter->requestRowsPreserved();
193 }
194
mtklein36352bf2015-03-25 18:17:31 -0700195 void* allocBlitMemory(size_t sz) override {
krajcevski75f88512014-07-21 09:54:23 -0700196 return fBlitter->allocBlitMemory(sz);
197 }
198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199private:
200 SkBlitter* fBlitter;
201 SkIRect fClipRect;
202};
203
204/** Wraps another (real) blitter, and ensures that the real blitter is only
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000205 called with coordinates that have been clipped by the specified clipRgn.
206 This means the caller need not perform the clipping ahead of time.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207*/
208class SkRgnClipBlitter : public SkBlitter {
209public:
210 void init(SkBlitter* blitter, const SkRegion* clipRgn) {
211 SkASSERT(clipRgn && !clipRgn->isEmpty());
212 fBlitter = blitter;
213 fRgn = clipRgn;
214 }
215
mtklein36352bf2015-03-25 18:17:31 -0700216 void blitH(int x, int y, int width) override;
reed3dfd1332015-06-25 14:26:11 -0700217 void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
mtklein36352bf2015-03-25 18:17:31 -0700218 void blitV(int x, int y, int height, SkAlpha alpha) override;
219 void blitRect(int x, int y, int width, int height) override;
reed3dfd1332015-06-25 14:26:11 -0700220 void blitAntiRect(int x, int y, int width, int height,
221 SkAlpha leftAlpha, SkAlpha rightAlpha) override;
mtklein36352bf2015-03-25 18:17:31 -0700222 void blitMask(const SkMask&, const SkIRect& clip) override;
reed41e010c2015-06-09 12:16:53 -0700223 const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224
mtklein36352bf2015-03-25 18:17:31 -0700225 int requestRowsPreserved() const override {
krajcevskifdd7d2b2014-07-29 07:21:41 -0700226 return fBlitter->requestRowsPreserved();
227 }
228
mtklein36352bf2015-03-25 18:17:31 -0700229 void* allocBlitMemory(size_t sz) override {
krajcevski75f88512014-07-21 09:54:23 -0700230 return fBlitter->allocBlitMemory(sz);
231 }
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233private:
234 SkBlitter* fBlitter;
235 const SkRegion* fRgn;
236};
237
Mike Reed28930b42016-11-09 15:23:26 -0500238#ifdef SK_DEBUG
239class SkRectClipCheckBlitter : public SkBlitter {
240public:
241 void init(SkBlitter* blitter, const SkIRect& clipRect) {
242 SkASSERT(blitter);
243 SkASSERT(!clipRect.isEmpty());
244 fBlitter = blitter;
245 fClipRect = clipRect;
246 }
247
248 void blitH(int x, int y, int width) override;
249 void blitAntiH(int x, int y, const SkAlpha[], const int16_t runs[]) override;
250 void blitV(int x, int y, int height, SkAlpha alpha) override;
251 void blitRect(int x, int y, int width, int height) override;
252 void blitAntiRect(int x, int y, int width, int height,
253 SkAlpha leftAlpha, SkAlpha rightAlpha) override;
254 void blitMask(const SkMask&, const SkIRect& clip) override;
255 const SkPixmap* justAnOpaqueColor(uint32_t* value) override;
Yuqian Li99bba9e2016-11-21 09:44:59 -0500256 void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
257 void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
Mike Reed28930b42016-11-09 15:23:26 -0500258
259 int requestRowsPreserved() const override {
260 return fBlitter->requestRowsPreserved();
261 }
262
263 void* allocBlitMemory(size_t sz) override {
264 return fBlitter->allocBlitMemory(sz);
265 }
266
267private:
268 SkBlitter* fBlitter;
269 SkIRect fClipRect;
270};
271#endif
272
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000273/** Factory to set up the appropriate most-efficient wrapper blitter
274 to apply a clip. Returns a pointer to a member, so lifetime must
275 be managed carefully.
276*/
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277class SkBlitterClipper {
278public:
279 SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip,
halcanary96fcdcc2015-08-27 07:41:13 -0700280 const SkIRect* bounds = nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281
282private:
283 SkNullBlitter fNullBlitter;
284 SkRectClipBlitter fRectBlitter;
285 SkRgnClipBlitter fRgnBlitter;
286};
287
288#endif