SkBlitter.h revision 53783b026a00683c1fb504127c3398dabb61ea73
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkBlitter_DEFINED
11#define SkBlitter_DEFINED
12
13#include "SkBitmap.h"
14#include "SkBitmapProcShader.h"
15#include "SkMask.h"
16#include "SkMatrix.h"
17#include "SkPaint.h"
18#include "SkRefCnt.h"
19#include "SkRegion.h"
20#include "SkShader.h"
21#include "SkSmallAllocator.h"
22
23/** SkBlitter and its subclasses are responsible for actually writing pixels
24    into memory. Besides efficiency, they handle clipping and antialiasing.
25*/
26class SkBlitter {
27public:
28    virtual ~SkBlitter();
29
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[]);
36    /// Blit a vertical run of pixels with a constant alpha value.
37    virtual void blitV(int x, int y, int height, SkAlpha alpha);
38    /// Blit a solid rectangle one or more pixels wide.
39    virtual void blitRect(int x, int y, int width, int height);
40    /** Blit a rectangle with one alpha-blended column on the left,
41        width (zero or more) opaque pixels, and one alpha-blended column
42        on the right.
43        The result will always be at least two pixels wide.
44    */
45    virtual void blitAntiRect(int x, int y, int width, int height,
46                              SkAlpha leftAlpha, SkAlpha rightAlpha);
47    /// Blit a pattern of pixels defined by a rectangle-clipped mask;
48    /// typically used for text.
49    virtual void blitMask(const SkMask&, const SkIRect& clip);
50
51    /** If the blitter just sets a single value for each pixel, return the
52        bitmap it draws into, and assign value. If not, return NULL and ignore
53        the value parameter.
54    */
55    virtual const SkBitmap* justAnOpaqueColor(uint32_t* value);
56
57    /**
58     *  Special method just to identify the null blitter, which is returned
59     *  from Choose() if the request cannot be fulfilled. Default impl
60     *  returns false.
61     */
62    virtual bool isNullBlitter() const;
63
64    ///@name non-virtual helpers
65    void blitMaskRegion(const SkMask& mask, const SkRegion& clip);
66    void blitRectRegion(const SkIRect& rect, const SkRegion& clip);
67    void blitRegion(const SkRegion& clip);
68    ///@}
69
70    /** @name Factories
71        Return the correct blitter to use given the specified context.
72     */
73    static SkBlitter* Choose(const SkBitmap& device,
74                             const SkMatrix& matrix,
75                             const SkPaint& paint,
76                             SkTBlitterAllocator*,
77                             bool drawCoverage = false);
78
79    static SkBlitter* ChooseSprite(const SkBitmap& device,
80                                   const SkPaint&,
81                                   const SkBitmap& src,
82                                   int left, int top,
83                                   SkTBlitterAllocator*);
84    ///@}
85
86private:
87};
88
89/** This blitter silently never draws anything.
90*/
91class SkNullBlitter : public SkBlitter {
92public:
93    virtual void blitH(int x, int y, int width) SK_OVERRIDE;
94    virtual void blitAntiH(int x, int y, const SkAlpha[],
95                           const int16_t runs[]) SK_OVERRIDE;
96    virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
97    virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
98    virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
99    virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
100    virtual bool isNullBlitter() const SK_OVERRIDE;
101};
102
103/** Wraps another (real) blitter, and ensures that the real blitter is only
104    called with coordinates that have been clipped by the specified clipRect.
105    This means the caller need not perform the clipping ahead of time.
106*/
107class SkRectClipBlitter : public SkBlitter {
108public:
109    void init(SkBlitter* blitter, const SkIRect& clipRect) {
110        SkASSERT(!clipRect.isEmpty());
111        fBlitter = blitter;
112        fClipRect = clipRect;
113    }
114
115    virtual void blitH(int x, int y, int width) SK_OVERRIDE;
116    virtual void blitAntiH(int x, int y, const SkAlpha[],
117                           const int16_t runs[]) SK_OVERRIDE;
118    virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
119    virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
120    virtual void blitAntiRect(int x, int y, int width, int height,
121                     SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
122    virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
123    virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
124
125private:
126    SkBlitter*  fBlitter;
127    SkIRect     fClipRect;
128};
129
130/** Wraps another (real) blitter, and ensures that the real blitter is only
131    called with coordinates that have been clipped by the specified clipRgn.
132    This means the caller need not perform the clipping ahead of time.
133*/
134class SkRgnClipBlitter : public SkBlitter {
135public:
136    void init(SkBlitter* blitter, const SkRegion* clipRgn) {
137        SkASSERT(clipRgn && !clipRgn->isEmpty());
138        fBlitter = blitter;
139        fRgn = clipRgn;
140    }
141
142    virtual void blitH(int x, int y, int width) SK_OVERRIDE;
143    virtual void blitAntiH(int x, int y, const SkAlpha[],
144                           const int16_t runs[]) SK_OVERRIDE;
145    virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE;
146    virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
147    virtual void blitAntiRect(int x, int y, int width, int height,
148                     SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE;
149    virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE;
150    virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE;
151
152private:
153    SkBlitter*      fBlitter;
154    const SkRegion* fRgn;
155};
156
157/** Factory to set up the appropriate most-efficient wrapper blitter
158    to apply a clip. Returns a pointer to a member, so lifetime must
159    be managed carefully.
160*/
161class SkBlitterClipper {
162public:
163    SkBlitter*  apply(SkBlitter* blitter, const SkRegion* clip,
164                      const SkIRect* bounds = NULL);
165
166private:
167    SkNullBlitter       fNullBlitter;
168    SkRectClipBlitter   fRectBlitter;
169    SkRgnClipBlitter    fRgnBlitter;
170};
171
172#endif
173