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

Commit 865fce8

Browse files
committed
Centralize system-composite and elevation logic
1 parent a8e5a08 commit 865fce8

13 files changed

+295
-113
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ FILE: ../../../flutter/flow/layers/color_filter_layer_unittests.cc
4848
FILE: ../../../flutter/flow/layers/container_layer.cc
4949
FILE: ../../../flutter/flow/layers/container_layer.h
5050
FILE: ../../../flutter/flow/layers/container_layer_unittests.cc
51+
FILE: ../../../flutter/flow/layers/elevated_container_layer.cc
52+
FILE: ../../../flutter/flow/layers/elevated_container_layer.h
53+
FILE: ../../../flutter/flow/layers/fuchsia_system_composited_layer.cc
54+
FILE: ../../../flutter/flow/layers/fuchsia_system_composited_layer.h
5155
FILE: ../../../flutter/flow/layers/layer.cc
5256
FILE: ../../../flutter/flow/layers/layer.h
5357
FILE: ../../../flutter/flow/layers/layer_tree.cc

flow/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ source_set("flow") {
2828
"layers/color_filter_layer.h",
2929
"layers/container_layer.cc",
3030
"layers/container_layer.h",
31+
"layers/elevated_container_layer.cc",
32+
"layers/elevated_container_layer.h",
3133
"layers/layer.cc",
3234
"layers/layer.h",
3335
"layers/layer_tree.cc",
@@ -76,6 +78,8 @@ source_set("flow") {
7678
sources += [
7779
"layers/child_scene_layer.cc",
7880
"layers/child_scene_layer.h",
81+
"layers/fuchsia_system_composited_layer.cc",
82+
"layers/fuchsia_system_composited_layer.h",
7983
"scene_update_context.cc",
8084
"scene_update_context.h",
8185
"view_holder.cc",

flow/layers/container_layer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ void ContainerLayer::UpdateScene(SceneUpdateContext& context) {
7676
void ContainerLayer::UpdateSceneChildren(SceneUpdateContext& context) {
7777
FML_DCHECK(needs_system_composite());
7878

79-
// Paint all of the layers which need to be drawn into the container.
80-
// These may be flattened down to a containing
81-
for (auto& layer : layers_) {
79+
// Update all of the Layers which are part of the container. This may cause
80+
// additional child |Frame|s to be created.
81+
for (auto& layer : layers()) {
8282
if (layer->needs_system_composite()) {
8383
layer->UpdateScene(context);
8484
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/flow/layers/elevated_container_layer.h"
6+
7+
namespace flutter {
8+
namespace {
9+
10+
float ClampElevation(float elevation,
11+
float parent_elevation,
12+
float max_elevation) {
13+
// TODO(mklim): Deal with bounds overflow more elegantly. We'd like to be
14+
// able to have developers specify the behavior here to alternatives besides
15+
// clamping, like normalization on some arbitrary curve.
16+
float clamped_elevation = elevation;
17+
if (max_elevation > -1 && (parent_elevation + elevation) > max_elevation) {
18+
// Clamp the local z coordinate at our max bound. Take into account the
19+
// parent z position here to fix clamping in cases where the child is
20+
// overflowing because of its parents.
21+
clamped_elevation = max_elevation - parent_elevation;
22+
}
23+
24+
return clamped_elevation;
25+
}
26+
27+
} // namespace
28+
29+
ElevatedContainerLayer::ElevatedContainerLayer(float elevation)
30+
: elevation_(elevation), clamped_elevation_(elevation) {}
31+
32+
void ElevatedContainerLayer::Preroll(PrerollContext* context,
33+
const SkMatrix& matrix) {
34+
TRACE_EVENT0("flutter", "ElevatedContainerLayer::Preroll");
35+
36+
// Track total elevation as we walk the tree, in order to deal with bounds
37+
// overflow in z.
38+
parent_elevation_ = context->total_elevation;
39+
clamped_elevation_ = ClampElevation(elevation_, parent_elevation_,
40+
context->frame_physical_depth);
41+
context->total_elevation += clamped_elevation_;
42+
43+
ContainerLayer::Preroll(context, matrix);
44+
45+
// Restore the elevation for our parent.
46+
context->total_elevation = parent_elevation_;
47+
}
48+
49+
} // namespace flutter
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_FLOW_LAYERS_ELEVATED_CONTAINER_LAYER_H_
6+
#define FLUTTER_FLOW_LAYERS_ELEVATED_CONTAINER_LAYER_H_
7+
8+
#include "flutter/flow/layers/container_layer.h"
9+
10+
namespace flutter {
11+
12+
class ElevatedContainerLayer : public ContainerLayer {
13+
public:
14+
ElevatedContainerLayer(float elevation);
15+
~ElevatedContainerLayer() override = default;
16+
17+
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
18+
19+
float elevation() const { return clamped_elevation_; }
20+
float total_elevation() const {
21+
return parent_elevation_ + clamped_elevation_;
22+
}
23+
24+
private:
25+
float parent_elevation_ = 0.0f;
26+
float elevation_ = 0.0f;
27+
float clamped_elevation_ = 0.0f;
28+
29+
FML_DISALLOW_COPY_AND_ASSIGN(ElevatedContainerLayer);
30+
};
31+
32+
} // namespace flutter
33+
34+
#endif // FLUTTER_FLOW_LAYERS_ELEVATED_CONTAINER_LAYER_H_
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/flow/layers/fuchsia_system_composited_layer.h"
6+
7+
namespace flutter {
8+
9+
FuchsiaSystemCompositedLayer::FuchsiaSystemCompositedLayer(
10+
SkColor color,
11+
float elevation)
12+
: ElevatedContainerLayer(elevation), color_(color) {}
13+
14+
void FuchsiaSystemCompositedLayer::Preroll(PrerollContext* context,
15+
const SkMatrix& matrix) {
16+
TRACE_EVENT0("flutter", "FuchsiaSystemCompositedLayer::Preroll");
17+
#if !defined(OS_FUCHSIA)
18+
FML_NOTIMPLEMENTED();
19+
#endif // !defined(OS_FUCHSIA)
20+
21+
ElevatedContainerLayer::Preroll(context, matrix);
22+
23+
// System-composite this layer if its elevated.
24+
if (elevation() != 0.0f) {
25+
set_needs_system_composite(true);
26+
}
27+
}
28+
29+
void FuchsiaSystemCompositedLayer::Paint(PaintContext& context) const {
30+
TRACE_EVENT0("flutter", "FuchsiaSystemCompositedLayer::Paint");
31+
#if !defined(OS_FUCHSIA)
32+
FML_NOTIMPLEMENTED();
33+
#endif // !defined(OS_FUCHSIA)
34+
35+
// TODO: hole punch
36+
// If this is actually called, it's because flow is attempting to paint this
37+
// layer onto the frame behind it which gives us a chance to hole-punch
38+
}
39+
40+
#if defined(OS_FUCHSIA)
41+
42+
void FuchsiaSystemCompositedLayer::UpdateScene(
43+
SceneUpdateContext& context) {
44+
FML_DCHECK(needs_system_composite());
45+
46+
// Retained rendering: speedup by reusing a retained entity node if
47+
// possible. When an entity node is reused, no paint layer is added to the
48+
// frame so we won't call Paint.
49+
LayerRasterCacheKey key(unique_id(), context.Matrix());
50+
if (context.HasRetainedNode(key)) {
51+
TRACE_EVENT_INSTANT0("flutter", "retained layer cache hit");
52+
const scenic::EntityNode& retained_node = context.GetRetainedNode(key);
53+
FML_DCHECK(context.top_entity());
54+
FML_DCHECK(retained_node.session() == context.session());
55+
context.top_entity()->embedder_node().AddChild(retained_node);
56+
return;
57+
}
58+
59+
TRACE_EVENT_INSTANT0("flutter", "retained cache miss, creating");
60+
// If we can't find an existing retained surface, create one.
61+
SceneUpdateContext::Frame frame(context, rrect_, color_, elevation(), this);
62+
for (auto& layer : layers()) {
63+
if (layer->needs_painting()) {
64+
frame.AddPaintLayer(layer.get());
65+
}
66+
}
67+
68+
ContainerLayer::UpdateScene(context);
69+
}
70+
71+
#endif // defined(OS_FUCHSIA)
72+
73+
} // namespace flutter
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_FLOW_LAYERS_FUCHSIA_SYSTEM_COMPOSITED_LAYER_H_
6+
#define FLUTTER_FLOW_LAYERS_FUCHSIA_SYSTEM_COMPOSITED_LAYER_H_
7+
8+
#include "flutter/flow/layers/elevated_container_layer.h"
9+
10+
namespace flutter {
11+
12+
class FuchsiaSystemCompositedLayer : public ElevatedContainerLayer {
13+
public:
14+
static bool can_system_composite() { return true; }
15+
16+
FuchsiaSystemCompositedLayer(SkColor color, float elevation);
17+
~FuchsiaSystemCompositedLayer() override = default;
18+
19+
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
20+
#if defined(OS_FUCHSIA)
21+
void UpdateScene(SceneUpdateContext& context) override;
22+
#endif // defined(OS_FUCHSIA)
23+
void Paint(PaintContext& context) const override;
24+
25+
void set_dimensions(SkRRect rrect) { rrect_ = rrect; }
26+
27+
SkColor color() const { return color_; }
28+
29+
private:
30+
SkRRect rrect_ = SkRRect::MakeEmpty();
31+
SkColor color_ = SK_ColorTRANSPARENT;
32+
33+
FML_DISALLOW_COPY_AND_ASSIGN(FuchsiaSystemCompositedLayer);
34+
};
35+
36+
} // namespace flutter
37+
38+
#endif // FLUTTER_FLOW_LAYERS_FUCHSIA_SYSTEM_COMPOSITED_LAYER_H_

flow/layers/layer_tree.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void LayerTree::UpdateScene(SceneUpdateContext& context,
8181
context,
8282
SkRRect::MakeRect(
8383
SkRect::MakeWH(frame_size_.width(), frame_size_.height())),
84-
SK_ColorTRANSPARENT);
84+
SK_ColorTRANSPARENT, /* elevation */ 0.0f);
8585
if (root_layer_->needs_system_composite()) {
8686
root_layer_->UpdateScene(context);
8787
}

0 commit comments

Comments
 (0)