Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f100e64

Browse files
authored
Migrate layers and layer_tree to DisplayList/Impeller geometry classes (#57153)
Migrates Layers and LayerTree and parts of the `flow/` utility classes to use DlGeometry (Impeller) classes.
1 parent 32f1e6d commit f100e64

File tree

101 files changed

+1951
-2125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1951
-2125
lines changed

display_list/geometry/dl_geometry_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static_assert(sizeof(SkIRect) == sizeof(DlIRect));
4242
static_assert(sizeof(SkVector) == sizeof(DlSize));
4343

4444
static constexpr DlScalar kEhCloseEnough = impeller::kEhCloseEnough;
45+
static constexpr DlScalar kPi = impeller::kPi;
4546

4647
constexpr inline bool DlScalarNearlyZero(DlScalar x,
4748
DlScalar tolerance = kEhCloseEnough) {
@@ -142,6 +143,11 @@ inline const SkIRect& ToSkIRect(const DlIRect& rect) {
142143
return *reinterpret_cast<const SkIRect*>(&rect);
143144
}
144145

146+
inline std::optional<const SkIRect> ToOptSkIRect(
147+
std::optional<const DlIRect> rect) {
148+
return rect.has_value() ? std::optional(ToSkIRect(*rect)) : std::nullopt;
149+
}
150+
145151
inline const SkRect* ToSkRect(const DlRect* rect) {
146152
return rect == nullptr ? nullptr : reinterpret_cast<const SkRect*>(rect);
147153
}
@@ -158,6 +164,10 @@ inline const SkRect* ToSkRects(const DlRect* rects) {
158164
return rects == nullptr ? nullptr : reinterpret_cast<const SkRect*>(rects);
159165
}
160166

167+
inline const SkSize& ToSkSize(const DlSize& size) {
168+
return *reinterpret_cast<const SkSize*>(&size);
169+
}
170+
161171
inline const SkISize& ToSkISize(const DlISize& size) {
162172
return *reinterpret_cast<const SkISize*>(&size);
163173
}

display_list/geometry/dl_path.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@
1010

1111
namespace flutter {
1212

13+
DlPath DlPath::MakeRect(DlRect rect) {
14+
return DlPath(SkPath::Rect(ToSkRect(rect)));
15+
}
16+
17+
DlPath DlPath::MakeRectLTRB(DlScalar left,
18+
DlScalar top,
19+
DlScalar right,
20+
DlScalar bottom) {
21+
return DlPath(SkPath().addRect(left, top, right, bottom));
22+
}
23+
24+
DlPath DlPath::MakeRectXYWH(DlScalar x,
25+
DlScalar y,
26+
DlScalar width,
27+
DlScalar height) {
28+
return DlPath(SkPath().addRect(SkRect::MakeXYWH(x, y, width, height)));
29+
}
30+
31+
DlPath DlPath::MakeOval(DlRect bounds) {
32+
return DlPath(SkPath::Oval(ToSkRect(bounds)));
33+
}
34+
35+
DlPath DlPath::MakeOvalLTRB(DlScalar left,
36+
DlScalar top,
37+
DlScalar right,
38+
DlScalar bottom) {
39+
return DlPath(SkPath::Oval(SkRect::MakeLTRB(left, top, right, bottom)));
40+
}
41+
1342
const SkPath& DlPath::GetSkPath() const {
1443
return data_->sk_path;
1544
}
@@ -76,6 +105,12 @@ bool DlPath::IsVolatile() const {
76105
return data_->sk_path.isVolatile();
77106
}
78107

108+
DlPath DlPath::operator+(const DlPath& other) const {
109+
SkPath path = data_->sk_path;
110+
path.addPath(other.data_->sk_path);
111+
return DlPath(path);
112+
}
113+
79114
using Path = impeller::Path;
80115
using PathBuilder = impeller::PathBuilder;
81116
using FillType = impeller::FillType;

display_list/geometry/dl_path.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ class DlPath {
1515
public:
1616
static constexpr uint32_t kMaxVolatileUses = 2;
1717

18+
static DlPath MakeRect(DlRect rect);
19+
static DlPath MakeRectLTRB(DlScalar left,
20+
DlScalar top,
21+
DlScalar right,
22+
DlScalar bottom);
23+
static DlPath MakeRectXYWH(DlScalar x,
24+
DlScalar y,
25+
DlScalar width,
26+
DlScalar height);
27+
28+
static DlPath MakeOval(DlRect bounds);
29+
static DlPath MakeOvalLTRB(DlScalar left,
30+
DlScalar top,
31+
DlScalar right,
32+
DlScalar bottom);
33+
1834
DlPath() : data_(std::make_shared<Data>(SkPath())) {}
1935
explicit DlPath(const SkPath& path) : data_(std::make_shared<Data>(path)) {}
2036

@@ -50,6 +66,8 @@ class DlPath {
5066
bool IsConverted() const;
5167
bool IsVolatile() const;
5268

69+
DlPath operator+(const DlPath& other) const;
70+
5371
private:
5472
struct Data {
5573
explicit Data(const SkPath& path) : sk_path(path) {}

flow/compositor_context.cc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace flutter {
1313

14-
std::optional<SkRect> FrameDamage::ComputeClipRect(
14+
std::optional<DlRect> FrameDamage::ComputeClipRect(
1515
flutter::LayerTree& layer_tree,
1616
bool has_raster_cache,
1717
bool impeller_enabled) {
@@ -21,17 +21,15 @@ std::optional<SkRect> FrameDamage::ComputeClipRect(
2121
prev_layer_tree_ ? prev_layer_tree_->paint_region_map()
2222
: empty_paint_region_map,
2323
has_raster_cache, impeller_enabled);
24-
context.PushCullRect(SkRect::MakeIWH(layer_tree.frame_size().width(),
25-
layer_tree.frame_size().height()));
24+
context.PushCullRect(DlRect::MakeSize(layer_tree.frame_size()));
2625
{
2726
DiffContext::AutoSubtreeRestore subtree(&context);
2827
const Layer* prev_root_layer = nullptr;
2928
if (!prev_layer_tree_ ||
3029
prev_layer_tree_->frame_size() != layer_tree.frame_size()) {
3130
// If there is no previous layer tree assume the entire frame must be
3231
// repainted.
33-
context.MarkSubtreeDirty(SkRect::MakeIWH(
34-
layer_tree.frame_size().width(), layer_tree.frame_size().height()));
32+
context.MarkSubtreeDirty(DlRect::MakeSize(layer_tree.frame_size()));
3533
} else {
3634
prev_root_layer = prev_layer_tree_->root_layer();
3735
}
@@ -41,7 +39,7 @@ std::optional<SkRect> FrameDamage::ComputeClipRect(
4139
damage_ =
4240
context.ComputeDamage(additional_damage_, horizontal_clip_alignment_,
4341
vertical_clip_alignment_);
44-
return SkRect::Make(damage_->buffer_damage);
42+
return DlRect::Make(damage_->buffer_damage);
4543
}
4644
return std::nullopt;
4745
}
@@ -120,7 +118,7 @@ RasterStatus CompositorContext::ScopedFrame::Raster(
120118
FrameDamage* frame_damage) {
121119
TRACE_EVENT0("flutter", "CompositorContext::ScopedFrame::Raster");
122120

123-
std::optional<SkRect> clip_rect;
121+
std::optional<DlRect> clip_rect;
124122
if (frame_damage) {
125123
clip_rect = frame_damage->ComputeClipRect(layer_tree, !ignore_raster_cache,
126124
!gr_context_);
@@ -159,7 +157,7 @@ RasterStatus CompositorContext::ScopedFrame::Raster(
159157

160158
void CompositorContext::ScopedFrame::PaintLayerTreeSkia(
161159
flutter::LayerTree& layer_tree,
162-
std::optional<SkRect> clip_rect,
160+
std::optional<DlRect> clip_rect,
163161
bool needs_save_layer,
164162
bool ignore_raster_cache) {
165163
DlAutoCanvasRestore restore(canvas(), clip_rect.has_value());
@@ -171,7 +169,7 @@ void CompositorContext::ScopedFrame::PaintLayerTreeSkia(
171169

172170
if (needs_save_layer) {
173171
TRACE_EVENT0("flutter", "Canvas::saveLayer");
174-
SkRect bounds = SkRect::Make(layer_tree.frame_size());
172+
SkRect bounds = SkRect::Make(ToSkISize(layer_tree.frame_size()));
175173
DlPaint paint;
176174
paint.setBlendMode(DlBlendMode::kSrc);
177175
canvas()->SaveLayer(&bounds, &paint);
@@ -185,10 +183,10 @@ void CompositorContext::ScopedFrame::PaintLayerTreeSkia(
185183

186184
void CompositorContext::ScopedFrame::PaintLayerTreeImpeller(
187185
flutter::LayerTree& layer_tree,
188-
std::optional<SkRect> clip_rect,
186+
std::optional<DlRect> clip_rect,
189187
bool ignore_raster_cache) {
190188
if (canvas() && clip_rect) {
191-
canvas()->Translate(-clip_rect->x(), -clip_rect->y());
189+
canvas()->Translate(-clip_rect->GetX(), -clip_rect->GetY());
192190
}
193191

194192
layer_tree.Paint(*this, ignore_raster_cache);
@@ -208,17 +206,17 @@ void CompositorContext::ScopedFrame::PaintLayerTreeImpeller(
208206
constexpr float kImpellerRepaintRatio = 0.7f;
209207

210208
bool CompositorContext::ShouldPerformPartialRepaint(
211-
std::optional<SkRect> damage_rect,
212-
SkISize layer_tree_size) {
209+
std::optional<DlRect> damage_rect,
210+
DlISize layer_tree_size) {
213211
if (!damage_rect.has_value()) {
214212
return false;
215213
}
216-
if (damage_rect->width() >= layer_tree_size.width() &&
217-
damage_rect->height() >= layer_tree_size.height()) {
214+
if (damage_rect->GetWidth() >= layer_tree_size.width &&
215+
damage_rect->GetHeight() >= layer_tree_size.height) {
218216
return false;
219217
}
220-
auto rx = damage_rect->width() / layer_tree_size.width();
221-
auto ry = damage_rect->height() / layer_tree_size.height();
218+
auto rx = damage_rect->GetWidth() / layer_tree_size.width;
219+
auto ry = damage_rect->GetHeight() / layer_tree_size.height;
222220
return rx <= kImpellerRepaintRatio || ry <= kImpellerRepaintRatio;
223221
}
224222

flow/compositor_context.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class FrameDamage {
5757

5858
// Adds additional damage (accumulated for double / triple buffering).
5959
// This is area that will be repainted alongside any changed part.
60-
void AddAdditionalDamage(const SkIRect& damage) {
61-
additional_damage_.join(damage);
60+
void AddAdditionalDamage(const DlIRect& damage) {
61+
additional_damage_ = additional_damage_.Union(damage);
6262
}
6363

6464
// Specifies clip rect alignment.
@@ -72,17 +72,17 @@ class FrameDamage {
7272
// If previous layer tree is not specified, clip rect will be nullopt,
7373
// but the paint region of layer_tree will be calculated so that it can be
7474
// used for diffing of subsequent frames.
75-
std::optional<SkRect> ComputeClipRect(flutter::LayerTree& layer_tree,
75+
std::optional<DlRect> ComputeClipRect(flutter::LayerTree& layer_tree,
7676
bool has_raster_cache,
7777
bool impeller_enabled);
7878

7979
// See Damage::frame_damage.
80-
std::optional<SkIRect> GetFrameDamage() const {
80+
std::optional<DlIRect> GetFrameDamage() const {
8181
return damage_ ? std::make_optional(damage_->frame_damage) : std::nullopt;
8282
}
8383

8484
// See Damage::buffer_damage.
85-
std::optional<SkIRect> GetBufferDamage() {
85+
std::optional<DlIRect> GetBufferDamage() {
8686
return (damage_ && !ignore_damage_)
8787
? std::make_optional(damage_->buffer_damage)
8888
: std::nullopt;
@@ -95,7 +95,7 @@ class FrameDamage {
9595
void Reset() { ignore_damage_ = true; }
9696

9797
private:
98-
SkIRect additional_damage_ = SkIRect::MakeEmpty();
98+
DlIRect additional_damage_;
9999
std::optional<Damage> damage_;
100100
const LayerTree* prev_layer_tree_ = nullptr;
101101
int vertical_clip_alignment_ = 1;
@@ -141,20 +141,20 @@ class CompositorContext {
141141

142142
private:
143143
void PaintLayerTreeSkia(flutter::LayerTree& layer_tree,
144-
std::optional<SkRect> clip_rect,
144+
std::optional<DlRect> clip_rect,
145145
bool needs_save_layer,
146146
bool ignore_raster_cache);
147147

148148
void PaintLayerTreeImpeller(flutter::LayerTree& layer_tree,
149-
std::optional<SkRect> clip_rect,
149+
std::optional<DlRect> clip_rect,
150150
bool ignore_raster_cache);
151151

152152
CompositorContext& context_;
153153
GrDirectContext* gr_context_;
154154
DlCanvas* canvas_;
155155
impeller::AiksContext* aiks_context_;
156156
ExternalViewEmbedder* view_embedder_;
157-
const SkMatrix& root_surface_transformation_;
157+
const SkMatrix root_surface_transformation_;
158158
const bool instrumentation_enabled_;
159159
const bool surface_supports_readback_;
160160
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
@@ -210,8 +210,8 @@ class CompositorContext {
210210
/// @brief Whether Impeller shouild attempt a partial repaint.
211211
/// The Impeller backend requires an additional blit pass, which may
212212
/// not be worthwhile if the damage region is large.
213-
static bool ShouldPerformPartialRepaint(std::optional<SkRect> damage_rect,
214-
SkISize layer_tree_size);
213+
static bool ShouldPerformPartialRepaint(std::optional<DlRect> damage_rect,
214+
DlISize layer_tree_size);
215215

216216
FML_DISALLOW_COPY_AND_ASSIGN(CompositorContext);
217217
};

0 commit comments

Comments
 (0)