Debian Codegraph @

src/core/SkPathEffect.cpp

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