forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_list_path_effect.h
174 lines (138 loc) · 5.69 KB
/
display_list_path_effect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PATH_EFFECT_H_
#define FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PATH_EFFECT_H_
#include <cstring>
#include <optional>
#include "flutter/display_list/display_list_attributes.h"
#include "flutter/display_list/types.h"
#include "flutter/fml/logging.h"
#include "include/core/SkScalar.h"
namespace flutter {
class DlDashPathEffect;
// The DisplayList PathEffect class. This class implements all of the
// facilities and adheres to the design goals of the |DlAttribute| base
// class.
// An enumerated type for the recognized PathEffect operations.
// In current Flutter we only use the DashPathEffect.
// And another PathEffect outside of the recognized types is needed
// then a |kUnknown| type that simply defers to an SkPathEffect is
// provided as a fallback.
enum class DlPathEffectType {
kDash,
kUnknown,
};
class DlPathEffect
: public DlAttribute<DlPathEffect, SkPathEffect, DlPathEffectType> {
public:
static std::shared_ptr<DlPathEffect> From(SkPathEffect* sk_path_effect);
static std::shared_ptr<DlPathEffect> From(
sk_sp<SkPathEffect> sk_path_effect) {
return From(sk_path_effect.get());
}
virtual const DlDashPathEffect* asDash() const { return nullptr; }
virtual std::optional<SkRect> effect_bounds(SkRect&) const = 0;
protected:
DlPathEffect() = default;
private:
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(DlPathEffect);
};
/// The DashPathEffect which breaks a path up into dash segments, and it
/// only affects stroked paths.
/// intervals: array containing an even number of entries (>=2), with
/// the even indices specifying the length of "on" intervals, and the odd
/// indices specifying the length of "off" intervals. This array will be
/// copied in Make, and can be disposed of freely after.
/// count: number of elements in the intervals array.
/// phase: initial distance into the intervals at which to start the dashing
/// effect for the path.
///
/// For example: if intervals[] = {10, 20}, count = 2, and phase = 25,
/// this will set up a dashed path like so:
/// 5 pixels off
/// 10 pixels on
/// 20 pixels off
/// 10 pixels on
/// 20 pixels off
/// ...
/// A phase of -5, 25, 55, 85, etc. would all result in the same path,
/// because the sum of all the intervals is 30.
///
class DlDashPathEffect final : public DlPathEffect {
public:
static std::shared_ptr<DlPathEffect> Make(const SkScalar intervals[],
int count,
SkScalar phase);
DlPathEffectType type() const override { return DlPathEffectType::kDash; }
size_t size() const override {
return sizeof(*this) + sizeof(SkScalar) * count_;
}
std::shared_ptr<DlPathEffect> shared() const override {
return Make(intervals(), count_, phase_);
}
const DlDashPathEffect* asDash() const override { return this; }
sk_sp<SkPathEffect> skia_object() const override {
return SkDashPathEffect::Make(intervals(), count_, phase_);
}
const SkScalar* intervals() const {
return reinterpret_cast<const SkScalar*>(this + 1);
}
std::optional<SkRect> effect_bounds(SkRect& rect) const override;
protected:
bool equals_(DlPathEffect const& other) const override {
FML_DCHECK(other.type() == DlPathEffectType::kDash);
auto that = static_cast<DlDashPathEffect const*>(&other);
return count_ == that->count_ && phase_ == that->phase_ &&
memcmp(intervals(), that->intervals(), sizeof(SkScalar) * count_) ==
0;
}
private:
// DlDashPathEffect constructor assumes the caller has prealloced storage for
// the intervals. If the intervals is nullptr the intervals will
// uninitialized.
DlDashPathEffect(const SkScalar intervals[], int count, SkScalar phase)
: count_(count), phase_(phase) {
if (intervals != nullptr) {
SkScalar* intervals_ = reinterpret_cast<SkScalar*>(this + 1);
memcpy(intervals_, intervals, sizeof(SkScalar) * count);
}
}
DlDashPathEffect(const DlDashPathEffect* dash_effect)
: DlDashPathEffect(dash_effect->intervals(),
dash_effect->count_,
dash_effect->phase_) {}
SkScalar* intervals_unsafe() { return reinterpret_cast<SkScalar*>(this + 1); }
int count_;
SkScalar phase_;
friend class DisplayListBuilder;
friend class DlPathEffect;
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(DlDashPathEffect);
};
class DlUnknownPathEffect final : public DlPathEffect {
public:
DlUnknownPathEffect(sk_sp<SkPathEffect> effect)
: sk_path_effect_(std::move(effect)) {}
DlUnknownPathEffect(const DlUnknownPathEffect& effect)
: DlUnknownPathEffect(effect.sk_path_effect_) {}
DlUnknownPathEffect(const DlUnknownPathEffect* effect)
: DlUnknownPathEffect(effect->sk_path_effect_) {}
DlPathEffectType type() const override { return DlPathEffectType::kUnknown; }
size_t size() const override { return sizeof(*this); }
std::shared_ptr<DlPathEffect> shared() const override {
return std::make_shared<DlUnknownPathEffect>(this);
}
sk_sp<SkPathEffect> skia_object() const override { return sk_path_effect_; }
virtual ~DlUnknownPathEffect() = default;
std::optional<SkRect> effect_bounds(SkRect& rect) const override;
protected:
bool equals_(const DlPathEffect& other) const override {
FML_DCHECK(other.type() == DlPathEffectType::kUnknown);
auto that = static_cast<DlUnknownPathEffect const*>(&other);
return sk_path_effect_ == that->sk_path_effect_;
}
private:
sk_sp<SkPathEffect> sk_path_effect_;
};
} // namespace flutter
#endif // FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PATH_EFFECT_H_