3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkBlitter_DEFINED
11 #define SkBlitter_DEFINED
14 #include "SkBitmapProcShader.h"
21 #include "SkSmallAllocator.h"
23 /** SkBlitter and its subclasses are responsible for actually writing pixels
24 into memory. Besides efficiency, they handle clipping and antialiasing.
30 /// Blit a horizontal run of one or more pixels.
31 virtual void blitH(int x, int y, int width);
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[]);
37 /// Blit a vertical run of pixels with a constant alpha value.
38 virtual void blitV(int x, int y, int height, SkAlpha alpha);
39 /// Blit a solid rectangle one or more pixels wide.
40 virtual void blitRect(int x, int y, int width, int height);
41 /** Blit a rectangle with one alpha-blended column on the left,
42 width (zero or more) opaque pixels, and one alpha-blended column
44 The result will always be at least two pixels wide.
46 virtual void blitAntiRect(int x, int y, int width, int height,
47 SkAlpha leftAlpha, SkAlpha rightAlpha);
48 /// Blit a pattern of pixels defined by a rectangle-clipped mask;
49 /// typically used for text.
50 virtual void blitMask(const SkMask&, const SkIRect& clip);
52 /** If the blitter just sets a single value for each pixel, return the
53 bitmap it draws into, and assign value. If not, return NULL and ignore
56 virtual const SkBitmap* justAnOpaqueColor(uint32_t* value);
59 * Special method just to identify the null blitter, which is returned
60 * from Choose() if the request cannot be fulfilled. Default impl
63 virtual bool isNullBlitter() const;
66 * Special methods for SkShaderBlitter. On all other classes this is a no-op.
68 virtual bool resetShaderContext(const SkShader::ContextRec&);
69 virtual SkShader::Context* getShaderContext() const;
72 * Special methods for blitters that can blit more than one row at a time.
73 * This function returns the number of rows that this blitter could optimally
74 * process at a time. It is still required to support blitting one scanline
77 virtual int requestRowsPreserved() const { return 1; }
80 * This function allocates memory for the blitter that the blitter then owns.
81 * The memory can be used by the calling function at will, but it will be
82 * released when the blitter's destructor is called. This function returns
83 * NULL if no persistent memory is needed by the blitter.
85 virtual void* allocBlitMemory(size_t sz) {
86 return fBlitMemory.reset(sz, SkAutoMalloc::kReuse_OnShrink);
89 ///@name non-virtual helpers
90 void blitMaskRegion(const SkMask& mask, const SkRegion& clip);
91 void blitRectRegion(const SkIRect& rect, const SkRegion& clip);
92 void blitRegion(const SkRegion& clip);
96 Return the correct blitter to use given the specified context.
98 static SkBlitter* Choose(const SkBitmap& device,
99 const SkMatrix& matrix,
100 const SkPaint& paint,
101 SkTBlitterAllocator*,
102 bool drawCoverage = false);
104 static SkBlitter* ChooseSprite(const SkBitmap& device,
108 SkTBlitterAllocator*);
113 SkAutoMalloc fBlitMemory;
118 /** This blitter silently never draws anything.
120 class SkNullBlitter : public SkBlitter {
122 void blitH(int x, int y, int width) SK_OVERRIDE;
123 virtual void blitAntiH(int x, int y, const SkAlpha[],
124 const int16_t runs[]) SK_OVERRIDE;
125 void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
126 void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
127 void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
128 const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
129 bool isNullBlitter() const SK_OVERRIDE;
132 /** Wraps another (real) blitter, and ensures that the real blitter is only
133 called with coordinates that have been clipped by the specified clipRect.
134 This means the caller need not perform the clipping ahead of time.
136 class SkRectClipBlitter : public SkBlitter {
138 void init(SkBlitter* blitter, const SkIRect& clipRect) {
139 SkASSERT(!clipRect.isEmpty());
141 fClipRect = clipRect;
144 void blitH(int x, int y, int width) SK_OVERRIDE;
145 virtual void blitAntiH(int x, int y, const SkAlpha[],
146 const int16_t runs[]) SK_OVERRIDE;
147 void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
148 void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
149 virtual void blitAntiRect(int x, int y, int width, int height,
150 SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
151 void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
152 const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
154 int requestRowsPreserved() const SK_OVERRIDE {
155 return fBlitter->requestRowsPreserved();
158 void* allocBlitMemory(size_t sz) SK_OVERRIDE {
159 return fBlitter->allocBlitMemory(sz);
167 /** Wraps another (real) blitter, and ensures that the real blitter is only
168 called with coordinates that have been clipped by the specified clipRgn.
169 This means the caller need not perform the clipping ahead of time.
171 class SkRgnClipBlitter : public SkBlitter {
173 void init(SkBlitter* blitter, const SkRegion* clipRgn) {
174 SkASSERT(clipRgn && !clipRgn->isEmpty());
179 void blitH(int x, int y, int width) SK_OVERRIDE;
180 virtual void blitAntiH(int x, int y, const SkAlpha[],
181 const int16_t runs[]) SK_OVERRIDE;
182 void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
183 void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
184 virtual void blitAntiRect(int x, int y, int width, int height,
185 SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
186 void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
187 const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
189 int requestRowsPreserved() const SK_OVERRIDE {
190 return fBlitter->requestRowsPreserved();
193 void* allocBlitMemory(size_t sz) SK_OVERRIDE {
194 return fBlitter->allocBlitMemory(sz);
199 const SkRegion* fRgn;
202 /** Factory to set up the appropriate most-efficient wrapper blitter
203 to apply a clip. Returns a pointer to a member, so lifetime must
204 be managed carefully.
206 class SkBlitterClipper {
208 SkBlitter* apply(SkBlitter* blitter, const SkRegion* clip,
209 const SkIRect* bounds = NULL);
212 SkNullBlitter fNullBlitter;
213 SkRectClipBlitter fRectBlitter;
214 SkRgnClipBlitter fRgnBlitter;