• 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 #include "include/core/SkPathEffect.h"
8 
9 #include "include/core/SkFlattenable.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/private/base/SkAssert.h"
14 #include "src/core/SkPathEffectBase.h"
15 #include "src/core/SkReadBuffer.h"
16 #include "src/core/SkWriteBuffer.h"
17 
18 #include <cstddef>
19 #include <utility>
20 
21 class SkStrokeRec;
22 struct SkDeserialProcs;
23 struct SkRect;
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * bounds) const27 bool SkPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
28                               const SkRect* bounds) const {
29     return this->filterPath(dst, src, rec, bounds, SkMatrix::I());
30 }
31 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * bounds,const SkMatrix & ctm) const32 bool SkPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
33                               const SkRect* bounds, const SkMatrix& ctm) const {
34     SkPath tmp, *tmpDst = dst;
35     if (dst == &src) {
36         tmpDst = &tmp;
37     }
38     if (as_PEB(this)->onFilterPath(tmpDst, src, rec, bounds, ctm)) {
39         if (dst == &src) {
40             *dst = tmp;
41         }
42         return true;
43     }
44     return false;
45 }
46 
asPoints(PointData * results,const SkPath & src,const SkStrokeRec & rec,const SkMatrix & mx,const SkRect * rect) const47 bool SkPathEffectBase::asPoints(PointData* results, const SkPath& src,
48                     const SkStrokeRec& rec, const SkMatrix& mx, const SkRect* rect) const {
49     return this->onAsPoints(results, src, rec, mx, rect);
50 }
51 
asADash(DashInfo * info) const52 SkPathEffect::DashType SkPathEffect::asADash(DashInfo* info) const {
53     return as_PEB(this)->onAsADash(info);
54 }
55 
needsCTM() const56 bool SkPathEffect::needsCTM() const {
57     return as_PEB(this)->onNeedsCTM();
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 
62 /** \class SkPairPathEffect
63 
64  Common baseclass for Compose and Sum. This subclass manages two pathEffects,
65  including flattening them. It does nothing in filterPath, and is only useful
66  for managing the lifetimes of its two arguments.
67  */
68 class SkPairPathEffect : public SkPathEffectBase {
69 protected:
SkPairPathEffect(sk_sp<SkPathEffect> pe0,sk_sp<SkPathEffect> pe1)70     SkPairPathEffect(sk_sp<SkPathEffect> pe0, sk_sp<SkPathEffect> pe1)
71         : fPE0(std::move(pe0)), fPE1(std::move(pe1))
72     {
73         SkASSERT(fPE0.get());
74         SkASSERT(fPE1.get());
75     }
76 
flatten(SkWriteBuffer & buffer) const77     void flatten(SkWriteBuffer& buffer) const override {
78         buffer.writeFlattenable(fPE0.get());
79         buffer.writeFlattenable(fPE1.get());
80     }
81 
82     // these are visible to our subclasses
83     sk_sp<SkPathEffect> fPE0;
84     sk_sp<SkPathEffect> fPE1;
85 
86 private:
87     using INHERITED = SkPathEffectBase;
88 };
89 
90 ///////////////////////////////////////////////////////////////////////////////////////////////////
91 
92 class SkComposePathEffect : public SkPairPathEffect {
93 public:
94     /** Construct a pathEffect whose effect is to apply first the inner pathEffect
95      and the the outer pathEffect (e.g. outer(inner(path)))
96      The reference counts for outer and inner are both incremented in the constructor,
97      and decremented in the destructor.
98      */
Make(sk_sp<SkPathEffect> outer,sk_sp<SkPathEffect> inner)99     static sk_sp<SkPathEffect> Make(sk_sp<SkPathEffect> outer, sk_sp<SkPathEffect> inner) {
100         if (!outer) {
101             return inner;
102         }
103         if (!inner) {
104             return outer;
105         }
106         return sk_sp<SkPathEffect>(new SkComposePathEffect(outer, inner));
107     }
108 
SkComposePathEffect(sk_sp<SkPathEffect> outer,sk_sp<SkPathEffect> inner)109     SkComposePathEffect(sk_sp<SkPathEffect> outer, sk_sp<SkPathEffect> inner)
110             : INHERITED(std::move(outer), std::move(inner)) {}
111 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect,const SkMatrix & ctm) const112     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
113                        const SkRect* cullRect, const SkMatrix& ctm) const override {
114         SkPath          tmp;
115         const SkPath*   ptr = &src;
116 
117         if (fPE1->filterPath(&tmp, src, rec, cullRect, ctm)) {
118             ptr = &tmp;
119         }
120         return fPE0->filterPath(dst, *ptr, rec, cullRect, ctm);
121     }
122 
SK_FLATTENABLE_HOOKS(SkComposePathEffect)123     SK_FLATTENABLE_HOOKS(SkComposePathEffect)
124 
125     bool computeFastBounds(SkRect* bounds) const override {
126         // inner (fPE1) is computed first, automatically updating bounds before computing outer.
127         return as_PEB(fPE1)->computeFastBounds(bounds) &&
128                as_PEB(fPE0)->computeFastBounds(bounds);
129     }
130 
131 private:
132     // illegal
133     SkComposePathEffect(const SkComposePathEffect&);
134     SkComposePathEffect& operator=(const SkComposePathEffect&);
135     friend class SkPathEffect;
136 
137     using INHERITED = SkPairPathEffect;
138 };
139 
CreateProc(SkReadBuffer & buffer)140 sk_sp<SkFlattenable> SkComposePathEffect::CreateProc(SkReadBuffer& buffer) {
141     sk_sp<SkPathEffect> pe0(buffer.readPathEffect());
142     sk_sp<SkPathEffect> pe1(buffer.readPathEffect());
143     return SkComposePathEffect::Make(std::move(pe0), std::move(pe1));
144 }
145 
146 ///////////////////////////////////////////////////////////////////////////////
147 
148 /** \class SkSumPathEffect
149 
150  This subclass of SkPathEffect applies two pathEffects, one after the other.
151  Its filterPath() returns true if either of the effects succeeded.
152  */
153 class SkSumPathEffect : public SkPairPathEffect {
154 public:
155     /** Construct a pathEffect whose effect is to apply two effects, in sequence.
156      (e.g. first(path) + second(path))
157      The reference counts for first and second are both incremented in the constructor,
158      and decremented in the destructor.
159      */
Make(sk_sp<SkPathEffect> first,sk_sp<SkPathEffect> second)160     static sk_sp<SkPathEffect> Make(sk_sp<SkPathEffect> first, sk_sp<SkPathEffect> second) {
161         if (!first) {
162             return second;
163         }
164         if (!second) {
165             return first;
166         }
167         return sk_sp<SkPathEffect>(new SkSumPathEffect(first, second));
168     }
169 
SkSumPathEffect(sk_sp<SkPathEffect> first,sk_sp<SkPathEffect> second)170     SkSumPathEffect(sk_sp<SkPathEffect> first, sk_sp<SkPathEffect> second)
171             : INHERITED(std::move(first), std::move(second)) {}
172 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect,const SkMatrix & ctm) const173     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
174                       const SkRect* cullRect, const SkMatrix& ctm) const override {
175         // always call both, even if the first one succeeds
176         bool filteredFirst = fPE0->filterPath(dst, src, rec, cullRect, ctm);
177         bool filteredSecond = fPE1->filterPath(dst, src, rec, cullRect, ctm);
178         return filteredFirst || filteredSecond;
179     }
180 
SK_FLATTENABLE_HOOKS(SkSumPathEffect)181     SK_FLATTENABLE_HOOKS(SkSumPathEffect)
182 
183     bool computeFastBounds(SkRect* bounds) const override {
184         // Unlike Compose(), PE0 modifies the path first for Sum
185         return as_PEB(fPE0)->computeFastBounds(bounds) &&
186                as_PEB(fPE1)->computeFastBounds(bounds);
187     }
188 
189 private:
190     // illegal
191     SkSumPathEffect(const SkSumPathEffect&);
192     SkSumPathEffect& operator=(const SkSumPathEffect&);
193     friend class SkPathEffect;
194 
195     using INHERITED = SkPairPathEffect;
196 };
197 
CreateProc(SkReadBuffer & buffer)198 sk_sp<SkFlattenable> SkSumPathEffect::CreateProc(SkReadBuffer& buffer) {
199     sk_sp<SkPathEffect> pe0(buffer.readPathEffect());
200     sk_sp<SkPathEffect> pe1(buffer.readPathEffect());
201     return SkSumPathEffect::Make(pe0, pe1);
202 }
203 
204 ///////////////////////////////////////////////////////////////////////////////////////////////////
205 
MakeSum(sk_sp<SkPathEffect> first,sk_sp<SkPathEffect> second)206 sk_sp<SkPathEffect> SkPathEffect::MakeSum(sk_sp<SkPathEffect> first, sk_sp<SkPathEffect> second) {
207     return SkSumPathEffect::Make(std::move(first), std::move(second));
208 }
209 
MakeCompose(sk_sp<SkPathEffect> outer,sk_sp<SkPathEffect> inner)210 sk_sp<SkPathEffect> SkPathEffect::MakeCompose(sk_sp<SkPathEffect> outer,
211                                               sk_sp<SkPathEffect> inner) {
212     return SkComposePathEffect::Make(std::move(outer), std::move(inner));
213 }
214 
RegisterFlattenables()215 void SkPathEffectBase::RegisterFlattenables() {
216     SK_REGISTER_FLATTENABLE(SkComposePathEffect);
217     SK_REGISTER_FLATTENABLE(SkSumPathEffect);
218 }
219 
Deserialize(const void * data,size_t size,const SkDeserialProcs * procs)220 sk_sp<SkPathEffect> SkPathEffect::Deserialize(const void* data, size_t size,
221                                               const SkDeserialProcs* procs) {
222     return sk_sp<SkPathEffect>(static_cast<SkPathEffect*>(
223                                SkFlattenable::Deserialize(
224                                kSkPathEffect_Type, data, size, procs).release()));
225 }
226