• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #ifndef SkPathEffect_DEFINED
9 #define SkPathEffect_DEFINED
10 
11 #include "SkFlattenable.h"
12 #include "SkPath.h"
13 #include "SkPoint.h"
14 #include "SkRect.h"
15 
16 class SkPath;
17 class SkStrokeRec;
18 
19 /** \class SkPathEffect
20 
21     SkPathEffect is the base class for objects in the SkPaint that affect
22     the geometry of a drawing primitive before it is transformed by the
23     canvas' matrix and drawn.
24 
25     Dashing is implemented as a subclass of SkPathEffect.
26 */
27 class SK_API SkPathEffect : public SkFlattenable {
28 public:
29     /**
30      *  Returns a patheffect that apples each effect (first and second) to the original path,
31      *  and returns a path with the sum of these.
32      *
33      *  result = first(path) + second(path)
34      *
35      */
36     static sk_sp<SkPathEffect> MakeSum(sk_sp<SkPathEffect> first, sk_sp<SkPathEffect> second);
37 
38     /**
39      *  Returns a patheffect that applies the inner effect to the path, and then applies the
40      *  outer effect to the result of the inner's.
41      *
42      *  result = outer(inner(path))
43      */
44     static sk_sp<SkPathEffect> MakeCompose(sk_sp<SkPathEffect> outer, sk_sp<SkPathEffect> inner);
45 
46     /**
47      *  Given a src path (input) and a stroke-rec (input and output), apply
48      *  this effect to the src path, returning the new path in dst, and return
49      *  true. If this effect cannot be applied, return false and ignore dst
50      *  and stroke-rec.
51      *
52      *  The stroke-rec specifies the initial request for stroking (if any).
53      *  The effect can treat this as input only, or it can choose to change
54      *  the rec as well. For example, the effect can decide to change the
55      *  stroke's width or join, or the effect can change the rec from stroke
56      *  to fill (or fill to stroke) in addition to returning a new (dst) path.
57      *
58      *  If this method returns true, the caller will apply (as needed) the
59      *  resulting stroke-rec to dst and then draw.
60      */
61     virtual bool filterPath(SkPath* dst, const SkPath& src,
62                             SkStrokeRec*, const SkRect* cullR) const = 0;
63 
64     /**
65      *  Compute a conservative bounds for its effect, given the src bounds.
66      *  The baseline implementation just assigns src to dst.
67      */
68     virtual void computeFastBounds(SkRect* dst, const SkRect& src) const;
69 
70     /** \class PointData
71 
72         PointData aggregates all the information needed to draw the point
73         primitives returned by an 'asPoints' call.
74     */
75     class PointData {
76     public:
PointData()77         PointData()
78             : fFlags(0)
79             , fPoints(NULL)
80             , fNumPoints(0) {
81             fSize.set(SK_Scalar1, SK_Scalar1);
82             // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets
83             // the kUseClip flag
84         }
~PointData()85         ~PointData() {
86             delete [] fPoints;
87         }
88 
89         // TODO: consider using passed-in flags to limit the work asPoints does.
90         // For example, a kNoPath flag could indicate don't bother generating
91         // stamped solutions.
92 
93         // Currently none of these flags are supported.
94         enum PointFlags {
95             kCircles_PointFlag            = 0x01,   // draw points as circles (instead of rects)
96             kUsePath_PointFlag            = 0x02,   // draw points as stamps of the returned path
97             kUseClip_PointFlag            = 0x04,   // apply 'fClipRect' before drawing the points
98         };
99 
100         uint32_t           fFlags;      // flags that impact the drawing of the points
101         SkPoint*           fPoints;     // the center point of each generated point
102         int                fNumPoints;  // number of points in fPoints
103         SkVector           fSize;       // the size to draw the points
104         SkRect             fClipRect;   // clip required to draw the points (if kUseClip is set)
105         SkPath             fPath;       // 'stamp' to be used at each point (if kUsePath is set)
106 
107         SkPath             fFirst;      // If not empty, contains geometry for first point
108         SkPath             fLast;       // If not empty, contains geometry for last point
109     };
110 
111     /**
112      *  Does applying this path effect to 'src' yield a set of points? If so,
113      *  optionally return the points in 'results'.
114      */
115     virtual bool asPoints(PointData* results, const SkPath& src,
116                           const SkStrokeRec&, const SkMatrix&,
117                           const SkRect* cullR) const;
118 
119     /**
120      *  If the PathEffect can be represented as a dash pattern, asADash will return kDash_DashType
121      *  and None otherwise. If a non NULL info is passed in, the various DashInfo will be filled
122      *  in if the PathEffect can be a dash pattern. If passed in info has an fCount equal or
123      *  greater to that of the effect, it will memcpy the values of the dash intervals into the
124      *  info. Thus the general approach will be call asADash once with default info to get DashType
125      *  and fCount. If effect can be represented as a dash pattern, allocate space for the intervals
126      *  in info, then call asADash again with the same info and the intervals will get copied in.
127      */
128 
129     enum DashType {
130         kNone_DashType, //!< ignores the info parameter
131         kDash_DashType, //!< fills in all of the info parameter
132     };
133 
134     struct DashInfo {
DashInfoDashInfo135         DashInfo() : fIntervals(NULL), fCount(0), fPhase(0) {}
DashInfoDashInfo136         DashInfo(SkScalar* intervals, int32_t count, SkScalar phase)
137             : fIntervals(intervals), fCount(count), fPhase(phase) {}
138 
139         SkScalar*   fIntervals;         //!< Length of on/off intervals for dashed lines
140                                         //   Even values represent ons, and odds offs
141         int32_t     fCount;             //!< Number of intervals in the dash. Should be even number
142         SkScalar    fPhase;             //!< Offset into the dashed interval pattern
143                                         //   mod the sum of all intervals
144     };
145 
146     virtual DashType asADash(DashInfo* info) const;
147 
148     SK_TO_STRING_PUREVIRT()
SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)149     SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)
150 
151 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
152     /// Override for subclasses as appropriate.
153     virtual bool exposedInAndroidJavaAPI() const { return false; }
154 #endif
155 
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()156     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
157 
158 protected:
159     SkPathEffect() {}
160 
161 private:
162     // illegal
163     SkPathEffect(const SkPathEffect&);
164     SkPathEffect& operator=(const SkPathEffect&);
165 
166     typedef SkFlattenable INHERITED;
167 };
168 
169 #endif
170