Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPathEffect.cpp
Go to the documentation of this file.
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 */
8
11#include "include/core/SkPath.h"
17
18#include <cstddef>
19#include <utility>
20
21class SkStrokeRec;
22struct SkDeserialProcs;
23struct SkRect;
24
25///////////////////////////////////////////////////////////////////////////////
26
28 const SkRect* bounds) const {
29 return this->filterPath(dst, src, rec, bounds, SkMatrix::I());
30}
31
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
48 const SkStrokeRec& rec, const SkMatrix& mx, const SkRect* rect) const {
49 return this->onAsPoints(results, src, rec, mx, rect);
50}
51
55
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 */
69protected:
71 : fPE0(std::move(pe0)), fPE1(std::move(pe1))
72 {
73 SkASSERT(fPE0.get());
74 SkASSERT(fPE1.get());
75 }
76
77 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
85
86private:
88};
89
90///////////////////////////////////////////////////////////////////////////////////////////////////
91
93public:
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 */
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
110 : INHERITED(std::move(outer), std::move(inner)) {}
111
112 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
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
131private:
132 // illegal
134 SkComposePathEffect& operator=(const SkComposePathEffect&);
135 friend class SkPathEffect;
136
138};
139
140sk_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 */
154public:
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 */
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
171 : INHERITED(std::move(first), std::move(second)) {}
172
173 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
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
189private:
190 // illegal
192 SkSumPathEffect& operator=(const SkSumPathEffect&);
193 friend class SkPathEffect;
194
196};
197
198sk_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
207 return SkSumPathEffect::Make(std::move(first), std::move(second));
208}
209
211 sk_sp<SkPathEffect> inner) {
212 return SkComposePathEffect::Make(std::move(outer), std::move(inner));
213}
214
219
220sk_sp<SkPathEffect> SkPathEffect::Deserialize(const void* data, size_t size,
221 const SkDeserialProcs* procs) {
222 return sk_sp<SkPathEffect>(static_cast<SkPathEffect*>(
224 kSkPathEffect_Type, data, size, procs).release()));
225}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SK_FLATTENABLE_HOOKS(type)
#define SK_REGISTER_FLATTENABLE(type)
static SkPathEffectBase * as_PEB(SkPathEffect *effect)
SkComposePathEffect(sk_sp< SkPathEffect > outer, sk_sp< SkPathEffect > inner)
static sk_sp< SkPathEffect > Make(sk_sp< SkPathEffect > outer, sk_sp< SkPathEffect > inner)
bool computeFastBounds(SkRect *bounds) const override
bool onFilterPath(SkPath *dst, const SkPath &src, SkStrokeRec *rec, const SkRect *cullRect, const SkMatrix &ctm) const override
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
static const SkMatrix & I()
sk_sp< SkPathEffect > fPE0
sk_sp< SkPathEffect > fPE1
SkPairPathEffect(sk_sp< SkPathEffect > pe0, sk_sp< SkPathEffect > pe1)
void flatten(SkWriteBuffer &buffer) const override
virtual bool onNeedsCTM() const
virtual bool computeFastBounds(SkRect *bounds) const =0
virtual DashType onAsADash(DashInfo *) const
bool asPoints(PointData *results, const SkPath &src, const SkStrokeRec &, const SkMatrix &, const SkRect *cullR) const
static void RegisterFlattenables()
virtual bool onAsPoints(PointData *, const SkPath &, const SkStrokeRec &, const SkMatrix &, const SkRect *) const
static sk_sp< SkPathEffect > MakeCompose(sk_sp< SkPathEffect > outer, sk_sp< SkPathEffect > inner)
static sk_sp< SkPathEffect > Deserialize(const void *data, size_t size, const SkDeserialProcs *procs=nullptr)
DashType asADash(DashInfo *info) const
static sk_sp< SkPathEffect > MakeSum(sk_sp< SkPathEffect > first, sk_sp< SkPathEffect > second)
bool needsCTM() const
bool filterPath(SkPath *dst, const SkPath &src, SkStrokeRec *, const SkRect *cullR) const
SkSumPathEffect(sk_sp< SkPathEffect > first, sk_sp< SkPathEffect > second)
bool computeFastBounds(SkRect *bounds) const override
static sk_sp< SkPathEffect > Make(sk_sp< SkPathEffect > first, sk_sp< SkPathEffect > second)
bool onFilterPath(SkPath *dst, const SkPath &src, SkStrokeRec *rec, const SkRect *cullRect, const SkMatrix &ctm) const override
T * get() const
Definition SkRefCnt.h:303
static const uint8_t buffer[]
Definition ref_ptr.h:256