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 
20 class 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 */
30 class SK_API SkPathEffect : public SkFlattenable {
31 public:
32     SK_DECLARE_INST_COUNT(SkPathEffect)
33 
34     /**
35      *  Given a src path (input) and a stroke-rec (input and output), apply
36      *  this effect to the src path, returning the new path in dst, and return
37      *  true. If this effect cannot be applied, return false and ignore dst
38      *  and stroke-rec.
39      *
40      *  The stroke-rec specifies the initial request for stroking (if any).
41      *  The effect can treat this as input only, or it can choose to change
42      *  the rec as well. For example, the effect can decide to change the
43      *  stroke's width or join, or the effect can change the rec from stroke
44      *  to fill (or fill to stroke) in addition to returning a new (dst) path.
45      *
46      *  If this method returns true, the caller will apply (as needed) the
47      *  resulting stroke-rec to dst and then draw.
48      */
49     virtual bool filterPath(SkPath* dst, const SkPath& src,
50                             SkStrokeRec*, const SkRect* cullR) const = 0;
51 
52     /**
53      *  Compute a conservative bounds for its effect, given the src bounds.
54      *  The baseline implementation just assigns src to dst.
55      */
56     virtual void computeFastBounds(SkRect* dst, const SkRect& src) const;
57 
58     /** \class PointData
59 
60         PointData aggregates all the information needed to draw the point
61         primitives returned by an 'asPoints' call.
62     */
63     class PointData {
64     public:
PointData()65         PointData()
66             : fFlags(0)
67             , fPoints(NULL)
68             , fNumPoints(0) {
69             fSize.set(SK_Scalar1, SK_Scalar1);
70             // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets
71             // the kUseClip flag
72         };
~PointData()73         ~PointData() {
74             delete [] fPoints;
75         }
76 
77         // TODO: consider using passed-in flags to limit the work asPoints does.
78         // For example, a kNoPath flag could indicate don't bother generating
79         // stamped solutions.
80 
81         // Currently none of these flags are supported.
82         enum PointFlags {
83             kCircles_PointFlag            = 0x01,   // draw points as circles (instead of rects)
84             kUsePath_PointFlag            = 0x02,   // draw points as stamps of the returned path
85             kUseClip_PointFlag            = 0x04,   // apply 'fClipRect' before drawing the points
86         };
87 
88         uint32_t           fFlags;      // flags that impact the drawing of the points
89         SkPoint*           fPoints;     // the center point of each generated point
90         int                fNumPoints;  // number of points in fPoints
91         SkVector           fSize;       // the size to draw the points
92         SkRect             fClipRect;   // clip required to draw the points (if kUseClip is set)
93         SkPath             fPath;       // 'stamp' to be used at each point (if kUsePath is set)
94 
95         SkPath             fFirst;      // If not empty, contains geometry for first point
96         SkPath             fLast;       // If not empty, contains geometry for last point
97     };
98 
99     /**
100      *  Does applying this path effect to 'src' yield a set of points? If so,
101      *  optionally return the points in 'results'.
102      */
103     virtual bool asPoints(PointData* results, const SkPath& src,
104                           const SkStrokeRec&, const SkMatrix&,
105                           const SkRect* cullR) const;
106 
107     /**
108      *  If the PathEffect can be represented as a dash pattern, asADash will return kDash_DashType
109      *  and None otherwise. If a non NULL info is passed in, the various DashInfo will be filled
110      *  in if the PathEffect can be a dash pattern. If passed in info has an fCount equal or
111      *  greater to that of the effect, it will memcpy the values of the dash intervals into the
112      *  info. Thus the general approach will be call asADash once with default info to get DashType
113      *  and fCount. If effect can be represented as a dash pattern, allocate space for the intervals
114      *  in info, then call asADash again with the same info and the intervals will get copied in.
115      */
116 
117     enum DashType {
118         kNone_DashType, //!< ignores the info parameter
119         kDash_DashType, //!< fills in all of the info parameter
120     };
121 
122     struct DashInfo {
DashInfoDashInfo123         DashInfo() : fIntervals(NULL), fCount(0), fPhase(0) {}
124 
125         SkScalar*   fIntervals;         //!< Length of on/off intervals for dashed lines
126                                         //   Even values represent ons, and odds offs
127         int32_t     fCount;             //!< Number of intervals in the dash. Should be even number
128         SkScalar    fPhase;             //!< Offset into the dashed interval pattern
129                                         //   mod the sum of all intervals
130     };
131 
132     virtual DashType asADash(DashInfo* info) const;
133 
134     SK_TO_STRING_PUREVIRT()
SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)135     SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)
136 
137 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
138     /// Override for subclasses as appropriate.
139     virtual bool exposedInAndroidJavaAPI() const { return false; }
140 #endif
141 
142 protected:
SkPathEffect()143     SkPathEffect() {}
144 
145 private:
146     // illegal
147     SkPathEffect(const SkPathEffect&);
148     SkPathEffect& operator=(const SkPathEffect&);
149 
150     typedef SkFlattenable INHERITED;
151 };
152 
153 /** \class SkPairPathEffect
154 
155     Common baseclass for Compose and Sum. This subclass manages two pathEffects,
156     including flattening them. It does nothing in filterPath, and is only useful
157     for managing the lifetimes of its two arguments.
158 */
159 class SkPairPathEffect : public SkPathEffect {
160 public:
161     virtual ~SkPairPathEffect();
162 
163 protected:
164     SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
165 
166     void flatten(SkWriteBuffer&) const override;
167 
168     // these are visible to our subclasses
169     SkPathEffect* fPE0, *fPE1;
170 
171     SK_TO_STRING_OVERRIDE()
172 
173 private:
174     typedef SkPathEffect INHERITED;
175 };
176 
177 /** \class SkComposePathEffect
178 
179     This subclass of SkPathEffect composes its two arguments, to create
180     a compound pathEffect.
181 */
182 class SkComposePathEffect : public SkPairPathEffect {
183 public:
184     /** Construct a pathEffect whose effect is to apply first the inner pathEffect
185         and the the outer pathEffect (e.g. outer(inner(path)))
186         The reference counts for outer and inner are both incremented in the constructor,
187         and decremented in the destructor.
188     */
Create(SkPathEffect * outer,SkPathEffect * inner)189     static SkComposePathEffect* Create(SkPathEffect* outer, SkPathEffect* inner) {
190         return SkNEW_ARGS(SkComposePathEffect, (outer, inner));
191     }
192 
193     virtual bool filterPath(SkPath* dst, const SkPath& src,
194                             SkStrokeRec*, const SkRect*) const override;
195 
196     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)197     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
198 
199 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
200     bool exposedInAndroidJavaAPI() const override { return true; }
201 #endif
202 
203 protected:
SkComposePathEffect(SkPathEffect * outer,SkPathEffect * inner)204     SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner) : INHERITED(outer, inner) {}
205 
206 private:
207     // illegal
208     SkComposePathEffect(const SkComposePathEffect&);
209     SkComposePathEffect& operator=(const SkComposePathEffect&);
210 
211     typedef SkPairPathEffect INHERITED;
212 };
213 
214 /** \class SkSumPathEffect
215 
216     This subclass of SkPathEffect applies two pathEffects, one after the other.
217     Its filterPath() returns true if either of the effects succeeded.
218 */
219 class SkSumPathEffect : public SkPairPathEffect {
220 public:
221     /** Construct a pathEffect whose effect is to apply two effects, in sequence.
222         (e.g. first(path) + second(path))
223         The reference counts for first and second are both incremented in the constructor,
224         and decremented in the destructor.
225     */
Create(SkPathEffect * first,SkPathEffect * second)226     static SkSumPathEffect* Create(SkPathEffect* first, SkPathEffect* second) {
227         return SkNEW_ARGS(SkSumPathEffect, (first, second));
228     }
229 
230     virtual bool filterPath(SkPath* dst, const SkPath& src,
231                             SkStrokeRec*, const SkRect*) const override;
232 
233     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)234     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
235 
236 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
237     bool exposedInAndroidJavaAPI() const override { return true; }
238 #endif
239 
240 protected:
SkSumPathEffect(SkPathEffect * first,SkPathEffect * second)241     SkSumPathEffect(SkPathEffect* first, SkPathEffect* second) : INHERITED(first, second) {}
242 
243 private:
244     // illegal
245     SkSumPathEffect(const SkSumPathEffect&);
246     SkSumPathEffect& operator=(const SkSumPathEffect&);
247 
248     typedef SkPairPathEffect INHERITED;
249 };
250 
251 #endif
252