blob: 86ccf86a9223eedb59dfb7361102bfb067177868 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkPathEffect_DEFINED
11#define SkPathEffect_DEFINED
12
13#include "SkFlattenable.h"
robertphillips@google.com629ab542012-11-28 17:18:11 +000014#include "SkPath.h"
15#include "SkPoint.h"
16#include "SkRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000017#include "SkStrokeRec.h"
robertphillips@google.com629ab542012-11-28 17:18:11 +000018#include "SkTDArray.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
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*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000030class SK_API SkPathEffect : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000032 SK_DECLARE_INST_COUNT(SkPathEffect)
33
reed@google.comfd4be262012-05-25 01:04:12 +000034 /**
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 */
reed@google.com548a1f32012-12-18 16:12:09 +000049 virtual bool filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000050 SkStrokeRec*, const SkRect* cullR) const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
reed@google.come4f10a72012-05-15 20:47:50 +000052 /**
53 * Compute a conservative bounds for its effect, given the src bounds.
54 * The baseline implementation just assigns src to dst.
55 */
reed@google.com548a1f32012-12-18 16:12:09 +000056 virtual void computeFastBounds(SkRect* dst, const SkRect& src) const;
reed@google.come4f10a72012-05-15 20:47:50 +000057
robertphillips@google.com629ab542012-11-28 17:18:11 +000058 /** \class PointData
59
60 PointData aggregates all the information needed to draw the point
skia.committer@gmail.com687c57c2012-11-29 02:01:19 +000061 primitives returned by an 'asPoints' call.
robertphillips@google.com629ab542012-11-28 17:18:11 +000062 */
63 class PointData {
64 public:
65 PointData()
robertphillips@google.com935ad022012-12-05 19:07:21 +000066 : fFlags(0)
67 , fPoints(NULL)
68 , fNumPoints(0) {
robertphillips@google.com629ab542012-11-28 17:18:11 +000069 fSize.set(SK_Scalar1, SK_Scalar1);
skia.committer@gmail.com687c57c2012-11-29 02:01:19 +000070 // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets
robertphillips@google.com629ab542012-11-28 17:18:11 +000071 // the kUseClip flag
72 };
robertphillips@google.com935ad022012-12-05 19:07:21 +000073 ~PointData() {
74 delete [] fPoints;
75 }
robertphillips@google.com629ab542012-11-28 17:18:11 +000076
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
robertphillips@google.com935ad022012-12-05 19:07:21 +000089 SkPoint* fPoints; // the center point of each generated point
90 int fNumPoints; // number of points in fPoints
robertphillips@google.com629ab542012-11-28 17:18:11 +000091 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)
robertphillips@google.com6d875572012-12-17 18:56:29 +000094
95 SkPath fFirst; // If not empty, contains geometry for first point
96 SkPath fLast; // If not empty, contains geometry for last point
robertphillips@google.com629ab542012-11-28 17:18:11 +000097 };
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 */
skia.committer@gmail.com687c57c2012-11-29 02:01:19 +0000103 virtual bool asPoints(PointData* results, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000104 const SkStrokeRec&, const SkMatrix&,
105 const SkRect* cullR) const;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000106
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000107 SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)
108
djsollen@google.comba28d032012-03-26 17:57:35 +0000109protected:
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000110 SkPathEffect() {}
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000111 SkPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {}
djsollen@google.comba28d032012-03-26 17:57:35 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113private:
114 // illegal
115 SkPathEffect(const SkPathEffect&);
116 SkPathEffect& operator=(const SkPathEffect&);
djsollen@google.comba28d032012-03-26 17:57:35 +0000117
118 typedef SkFlattenable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119};
120
121/** \class SkPairPathEffect
122
123 Common baseclass for Compose and Sum. This subclass manages two pathEffects,
124 including flattening them. It does nothing in filterPath, and is only useful
125 for managing the lifetimes of its two arguments.
126*/
127class SkPairPathEffect : public SkPathEffect {
128public:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 virtual ~SkPairPathEffect();
130
131protected:
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000132 SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000133 SkPairPathEffect(SkReadBuffer&);
134 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
djsollen@google.com54924242012-03-29 15:18:04 +0000135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 // these are visible to our subclasses
137 SkPathEffect* fPE0, *fPE1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139private:
140 typedef SkPathEffect INHERITED;
141};
142
143/** \class SkComposePathEffect
144
145 This subclass of SkPathEffect composes its two arguments, to create
146 a compound pathEffect.
147*/
148class SkComposePathEffect : public SkPairPathEffect {
149public:
150 /** Construct a pathEffect whose effect is to apply first the inner pathEffect
151 and the the outer pathEffect (e.g. outer(inner(path)))
152 The reference counts for outer and inner are both incremented in the constructor,
153 and decremented in the destructor.
154 */
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000155 static SkComposePathEffect* Create(SkPathEffect* outer, SkPathEffect* inner) {
156 return SkNEW_ARGS(SkComposePathEffect, (outer, inner));
157 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158
reed@google.com548a1f32012-12-18 16:12:09 +0000159 virtual bool filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000160 SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
djsollen@google.comba28d032012-03-26 17:57:35 +0000162 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163
djsollen@google.com54924242012-03-29 15:18:04 +0000164protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000165 SkComposePathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000167#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
168public:
169#endif
170 SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner)
171 : INHERITED(outer, inner) {}
172
djsollen@google.com54924242012-03-29 15:18:04 +0000173private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 // illegal
175 SkComposePathEffect(const SkComposePathEffect&);
176 SkComposePathEffect& operator=(const SkComposePathEffect&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 typedef SkPairPathEffect INHERITED;
179};
180
181/** \class SkSumPathEffect
182
183 This subclass of SkPathEffect applies two pathEffects, one after the other.
184 Its filterPath() returns true if either of the effects succeeded.
185*/
186class SkSumPathEffect : public SkPairPathEffect {
187public:
188 /** Construct a pathEffect whose effect is to apply two effects, in sequence.
189 (e.g. first(path) + second(path))
190 The reference counts for first and second are both incremented in the constructor,
191 and decremented in the destructor.
192 */
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000193 static SkSumPathEffect* Create(SkPathEffect* first, SkPathEffect* second) {
194 return SkNEW_ARGS(SkSumPathEffect, (first, second));
195 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196
reed@google.com548a1f32012-12-18 16:12:09 +0000197 virtual bool filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000198 SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199
djsollen@google.comba28d032012-03-26 17:57:35 +0000200 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201
djsollen@google.com54924242012-03-29 15:18:04 +0000202protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000203 SkSumPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000205#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
206public:
207#endif
208 SkSumPathEffect(SkPathEffect* first, SkPathEffect* second)
209 : INHERITED(first, second) {}
210
djsollen@google.com54924242012-03-29 15:18:04 +0000211private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 // illegal
213 SkSumPathEffect(const SkSumPathEffect&);
214 SkSumPathEffect& operator=(const SkSumPathEffect&);
215
216 typedef SkPairPathEffect INHERITED;
217};
218
219#endif