SkPathEffect.h revision 5f74cf8c49701f514b69dc6f1a8b5c0ffd78af0a
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 SkPathEffect_DEFINED
11#define SkPathEffect_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkPath.h"
15#include "SkPoint.h"
16#include "SkRect.h"
17#include "SkStrokeRec.h"
18#include "SkTDArray.h"
19
20class SkPath;
21
22/** \class SkPathEffect
23
24    SkPathEffect is the base class for objects in the SkPaint that affect
25    the geometry of a drawing primitive before it is transformed by the
26    canvas' matrix and drawn.
27
28    Dashing is implemented as a subclass of SkPathEffect.
29*/
30class SK_API SkPathEffect : public SkFlattenable {
31public:
32    SK_DECLARE_INST_COUNT(SkPathEffect)
33
34    SkPathEffect() {}
35
36    /**
37     *  Given a src path (input) and a stroke-rec (input and output), apply
38     *  this effect to the src path, returning the new path in dst, and return
39     *  true. If this effect cannot be applied, return false and ignore dst
40     *  and stroke-rec.
41     *
42     *  The stroke-rec specifies the initial request for stroking (if any).
43     *  The effect can treat this as input only, or it can choose to change
44     *  the rec as well. For example, the effect can decide to change the
45     *  stroke's width or join, or the effect can change the rec from stroke
46     *  to fill (or fill to stroke) in addition to returning a new (dst) path.
47     *
48     *  If this method returns true, the caller will apply (as needed) the
49     *  resulting stroke-rec to dst and then draw.
50     */
51    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) = 0;
52
53    /**
54     *  Compute a conservative bounds for its effect, given the src bounds.
55     *  The baseline implementation just assigns src to dst.
56     */
57    virtual void computeFastBounds(SkRect* dst, const SkRect& src);
58
59    /** \class PointData
60
61        PointData aggregates all the information needed to draw the point
62        primitives returned by an 'asPoints' call.
63    */
64    class PointData {
65    public:
66        PointData()
67            : fFlags(0)
68            , fPoints(NULL)
69            , fNumPoints(0) {
70            fSize.set(SK_Scalar1, SK_Scalar1);
71            // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets
72            // the kUseClip flag
73        };
74        ~PointData() {
75            delete [] fPoints;
76        }
77
78        // TODO: consider using passed-in flags to limit the work asPoints does.
79        // For example, a kNoPath flag could indicate don't bother generating
80        // stamped solutions.
81
82        // Currently none of these flags are supported.
83        enum PointFlags {
84            kCircles_PointFlag            = 0x01,   // draw points as circles (instead of rects)
85            kUsePath_PointFlag            = 0x02,   // draw points as stamps of the returned path
86            kUseClip_PointFlag            = 0x04,   // apply 'fClipRect' before drawing the points
87        };
88
89        uint32_t           fFlags;      // flags that impact the drawing of the points
90        SkPoint*           fPoints;     // the center point of each generated point
91        int                fNumPoints;  // number of points in fPoints
92        SkVector           fSize;       // the size to draw the points
93        SkRect             fClipRect;   // clip required to draw the points (if kUseClip is set)
94        SkPath             fPath;       // 'stamp' to be used at each point (if kUsePath is set)
95
96        SkPath             fFirst;      // If not empty, contains geometry for first point
97        SkPath             fLast;       // If not empty, contains geometry for last point
98    };
99
100    /**
101     *  Does applying this path effect to 'src' yield a set of points? If so,
102     *  optionally return the points in 'results'.
103     */
104    virtual bool asPoints(PointData* results, const SkPath& src,
105                          const SkStrokeRec&, const SkMatrix&) const;
106
107protected:
108    SkPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
109
110private:
111    // illegal
112    SkPathEffect(const SkPathEffect&);
113    SkPathEffect& operator=(const SkPathEffect&);
114
115    typedef SkFlattenable INHERITED;
116};
117
118/** \class SkPairPathEffect
119
120    Common baseclass for Compose and Sum. This subclass manages two pathEffects,
121    including flattening them. It does nothing in filterPath, and is only useful
122    for managing the lifetimes of its two arguments.
123*/
124class SkPairPathEffect : public SkPathEffect {
125public:
126    SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
127    virtual ~SkPairPathEffect();
128
129protected:
130    SkPairPathEffect(SkFlattenableReadBuffer&);
131    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
132
133    // these are visible to our subclasses
134    SkPathEffect* fPE0, *fPE1;
135
136private:
137    typedef SkPathEffect INHERITED;
138};
139
140/** \class SkComposePathEffect
141
142    This subclass of SkPathEffect composes its two arguments, to create
143    a compound pathEffect.
144*/
145class SkComposePathEffect : public SkPairPathEffect {
146public:
147    /** Construct a pathEffect whose effect is to apply first the inner pathEffect
148        and the the outer pathEffect (e.g. outer(inner(path)))
149        The reference counts for outer and inner are both incremented in the constructor,
150        and decremented in the destructor.
151    */
152    SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner)
153        : INHERITED(outer, inner) {}
154
155    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE;
156
157    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
158
159protected:
160    SkComposePathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
161
162private:
163    // illegal
164    SkComposePathEffect(const SkComposePathEffect&);
165    SkComposePathEffect& operator=(const SkComposePathEffect&);
166
167    typedef SkPairPathEffect INHERITED;
168};
169
170/** \class SkSumPathEffect
171
172    This subclass of SkPathEffect applies two pathEffects, one after the other.
173    Its filterPath() returns true if either of the effects succeeded.
174*/
175class SkSumPathEffect : public SkPairPathEffect {
176public:
177    /** Construct a pathEffect whose effect is to apply two effects, in sequence.
178        (e.g. first(path) + second(path))
179        The reference counts for first and second are both incremented in the constructor,
180        and decremented in the destructor.
181    */
182    SkSumPathEffect(SkPathEffect* first, SkPathEffect* second)
183        : INHERITED(first, second) {}
184
185    virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE;
186
187    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
188
189protected:
190    SkSumPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
191
192private:
193    // illegal
194    SkSumPathEffect(const SkSumPathEffect&);
195    SkSumPathEffect& operator=(const SkSumPathEffect&);
196
197    typedef SkPairPathEffect INHERITED;
198};
199
200#endif
201
202