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

Commit f2dbeb8

Browse files
authored
Reland Wire up Opacity on Fuchsia (#14559)
This reverts commit 6ea69a0. On top of the revert, it reverted a commit in the PR: #14024 This reverts commit ea67e5b.
1 parent 9c1bd8a commit f2dbeb8

Some content is hidden

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

43 files changed

+498
-342
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace flutter {
99
ContainerLayer::ContainerLayer() {}
1010

1111
void ContainerLayer::Add(std::shared_ptr<Layer> layer) {
12-
layer->set_parent(this);
13-
layers_.push_back(std::move(layer));
12+
layers_.emplace_back(std::move(layer));
1413
}
1514

1615
void ContainerLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {

flow/layers/container_layer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ContainerLayer : public Layer {
1414
public:
1515
ContainerLayer();
1616

17-
void Add(std::shared_ptr<Layer> layer);
17+
virtual void Add(std::shared_ptr<Layer> layer);
1818

1919
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
2020
void Paint(PaintContext& context) const override;
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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(SkColor color,
10+
float elevation)
11+
: ElevatedContainerLayer(elevation), color_(color) {}
12+
13+
void FuchsiaSystemCompositedLayer::UpdateScene(SceneUpdateContext& context) {
14+
FML_DCHECK(needs_system_composite());
15+
16+
// Retained rendering: speedup by reusing a retained entity node if
17+
// possible. When an entity node is reused, no paint layer is added to the
18+
// frame so we won't call Paint.
19+
LayerRasterCacheKey key(unique_id(), context.Matrix());
20+
if (context.HasRetainedNode(key)) {
21+
TRACE_EVENT_INSTANT0("flutter", "retained layer cache hit");
22+
const scenic::EntityNode& retained_node = context.GetRetainedNode(key);
23+
FML_DCHECK(context.top_entity());
24+
FML_DCHECK(retained_node.session() == context.session());
25+
context.top_entity()->embedder_node().AddChild(retained_node);
26+
return;
27+
}
28+
29+
TRACE_EVENT_INSTANT0("flutter", "retained cache miss, creating");
30+
// If we can't find an existing retained surface, create one.
31+
SceneUpdateContext::Frame frame(context, rrect_, color_, elevation(), this);
32+
for (auto& layer : layers()) {
33+
if (layer->needs_painting()) {
34+
frame.AddPaintLayer(layer.get());
35+
}
36+
}
37+
38+
ContainerLayer::UpdateScene(context);
39+
}
40+
41+
} // 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_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+
#include "flutter/flow/scene_update_context.h"
10+
11+
namespace flutter {
12+
13+
class FuchsiaSystemCompositedLayer : public ElevatedContainerLayer {
14+
public:
15+
static bool can_system_composite() { return true; }
16+
17+
FuchsiaSystemCompositedLayer(SkColor color, float elevation);
18+
19+
void UpdateScene(SceneUpdateContext& context) override;
20+
21+
void set_dimensions(SkRRect rrect) { rrect_ = rrect; }
22+
23+
SkColor color() const { return color_; }
24+
25+
private:
26+
SkRRect rrect_ = SkRRect::MakeEmpty();
27+
SkColor color_ = SK_ColorTRANSPARENT;
28+
29+
FML_DISALLOW_COPY_AND_ASSIGN(FuchsiaSystemCompositedLayer);
30+
};
31+
32+
} // namespace flutter
33+
34+
#endif // FLUTTER_FLOW_LAYERS_FUCHSIA_SYSTEM_COMPOSITED_LAYER_H_

flow/layers/layer.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
namespace flutter {
1111

1212
Layer::Layer()
13-
: parent_(nullptr),
14-
needs_system_composite_(false),
15-
paint_bounds_(SkRect::MakeEmpty()),
16-
unique_id_(NextUniqueID()) {}
13+
: paint_bounds_(SkRect::MakeEmpty()),
14+
unique_id_(NextUniqueID()),
15+
needs_system_composite_(false) {}
1716

1817
Layer::~Layer() = default;
1918

flow/layers/layer.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ static constexpr SkRect kGiantRect = SkRect::MakeLTRB(-1E9F, -1E9F, 1E9F, 1E9F);
4242
// This should be an exact copy of the Clip enum in painting.dart.
4343
enum Clip { none, hardEdge, antiAlias, antiAliasWithSaveLayer };
4444

45-
class ContainerLayer;
46-
4745
struct PrerollContext {
4846
RasterCache* raster_cache;
4947
GrContext* gr_context;
@@ -53,11 +51,18 @@ struct PrerollContext {
5351
SkRect cull_rect;
5452
bool surface_needs_readback;
5553

56-
// The following allows us to paint in the end of subtree preroll
54+
// These allow us to paint in the end of subtree Preroll.
5755
const Stopwatch& raster_time;
5856
const Stopwatch& ui_time;
5957
TextureRegistry& texture_registry;
6058
const bool checkerboard_offscreen_layers;
59+
60+
// These allow us to make use of the scene metrics during Preroll.
61+
float frame_physical_depth;
62+
float frame_device_pixel_ratio;
63+
64+
// These allow us to track properties like elevation and opacity which stack
65+
// with each other during Preroll.
6166
float total_elevation = 0.0f;
6267
bool has_platform_view = false;
6368
};
@@ -117,6 +122,10 @@ class Layer {
117122
TextureRegistry& texture_registry;
118123
const RasterCache* raster_cache;
119124
const bool checkerboard_offscreen_layers;
125+
126+
// These allow us to make use of the scene metrics during Paint.
127+
float frame_physical_depth;
128+
float frame_device_pixel_ratio;
120129
};
121130

122131
// Calls SkCanvas::saveLayer and restores the layer upon destruction. Also
@@ -153,10 +162,6 @@ class Layer {
153162
virtual void UpdateScene(SceneUpdateContext& context);
154163
#endif
155164

156-
ContainerLayer* parent() const { return parent_; }
157-
158-
void set_parent(ContainerLayer* parent) { parent_ = parent; }
159-
160165
bool needs_system_composite() const { return needs_system_composite_; }
161166
void set_needs_system_composite(bool value) {
162167
needs_system_composite_ = value;
@@ -175,10 +180,9 @@ class Layer {
175180
uint64_t unique_id() const { return unique_id_; }
176181

177182
private:
178-
ContainerLayer* parent_;
179-
bool needs_system_composite_;
180183
SkRect paint_bounds_;
181184
uint64_t unique_id_;
185+
bool needs_system_composite_;
182186

183187
static uint64_t NextUniqueID();
184188

flow/layers/layer_tree.cc

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace flutter {
1313

14-
LayerTree::LayerTree()
15-
: frame_size_{},
14+
LayerTree::LayerTree(const SkISize& frame_size,
15+
float frame_physical_depth,
16+
float frame_device_pixel_ratio)
17+
: frame_size_(frame_size),
18+
frame_physical_depth_(frame_physical_depth),
19+
frame_device_pixel_ratio_(frame_device_pixel_ratio),
1620
rasterizer_tracing_threshold_(0),
1721
checkerboard_raster_cache_images_(false),
1822
checkerboard_offscreen_layers_(false) {}
@@ -47,7 +51,9 @@ bool LayerTree::Preroll(CompositorContext::ScopedFrame& frame,
4751
frame.context().raster_time(),
4852
frame.context().ui_time(),
4953
frame.context().texture_registry(),
50-
checkerboard_offscreen_layers_};
54+
checkerboard_offscreen_layers_,
55+
frame_physical_depth_,
56+
frame_device_pixel_ratio_};
5157

5258
root_layer_->Preroll(&context, frame.root_surface_transformation());
5359
return context.surface_needs_readback;
@@ -57,12 +63,22 @@ bool LayerTree::Preroll(CompositorContext::ScopedFrame& frame,
5763
void LayerTree::UpdateScene(SceneUpdateContext& context,
5864
scenic::ContainerNode& container) {
5965
TRACE_EVENT0("flutter", "LayerTree::UpdateScene");
66+
67+
// Ensure the context is aware of the view metrics.
68+
context.set_dimensions(frame_size_, frame_physical_depth_,
69+
frame_device_pixel_ratio_);
70+
6071
const auto& metrics = context.metrics();
72+
FML_DCHECK(metrics->scale_x > 0.0f);
73+
FML_DCHECK(metrics->scale_y > 0.0f);
74+
FML_DCHECK(metrics->scale_z > 0.0f);
75+
6176
SceneUpdateContext::Transform transform(context, // context
6277
1.0f / metrics->scale_x, // X
6378
1.0f / metrics->scale_y, // Y
6479
1.0f / metrics->scale_z // Z
6580
);
81+
6682
SceneUpdateContext::Frame frame(
6783
context,
6884
SkRRect::MakeRect(
@@ -106,7 +122,9 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
106122
frame.context().ui_time(),
107123
frame.context().texture_registry(),
108124
ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
109-
checkerboard_offscreen_layers_};
125+
checkerboard_offscreen_layers_,
126+
frame_physical_depth_,
127+
frame_device_pixel_ratio_};
110128

111129
if (root_layer_->needs_painting())
112130
root_layer_->Paint(context);
@@ -130,17 +148,19 @@ sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
130148
root_surface_transformation.reset();
131149

132150
PrerollContext preroll_context{
133-
nullptr, // raster_cache (don't consult the cache)
134-
nullptr, // gr_context (used for the raster cache)
135-
nullptr, // external view embedder
136-
unused_stack, // mutator stack
137-
nullptr, // SkColorSpace* dst_color_space
138-
kGiantRect, // SkRect cull_rect
139-
false, // layer reads from surface
140-
unused_stopwatch, // frame time (dont care)
141-
unused_stopwatch, // engine time (dont care)
142-
unused_texture_registry, // texture registry (not supported)
143-
false, // checkerboard_offscreen_layers
151+
nullptr, // raster_cache (don't consult the cache)
152+
nullptr, // gr_context (used for the raster cache)
153+
nullptr, // external view embedder
154+
unused_stack, // mutator stack
155+
nullptr, // SkColorSpace* dst_color_space
156+
kGiantRect, // SkRect cull_rect
157+
false, // layer reads from surface
158+
unused_stopwatch, // frame time (dont care)
159+
unused_stopwatch, // engine time (dont care)
160+
unused_texture_registry, // texture registry (not supported)
161+
false, // checkerboard_offscreen_layers
162+
frame_physical_depth_, // maximum depth allowed for rendering
163+
frame_device_pixel_ratio_ // ratio between logical and physical
144164
};
145165

146166
SkISize canvas_size = canvas->getBaseLayerSize();
@@ -152,11 +172,13 @@ sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
152172
canvas, // canvas
153173
nullptr,
154174
nullptr,
155-
unused_stopwatch, // frame time (dont care)
156-
unused_stopwatch, // engine time (dont care)
157-
unused_texture_registry, // texture registry (not supported)
158-
nullptr, // raster cache
159-
false // checkerboard offscreen layers
175+
unused_stopwatch, // frame time (dont care)
176+
unused_stopwatch, // engine time (dont care)
177+
unused_texture_registry, // texture registry (not supported)
178+
nullptr, // raster cache
179+
false, // checkerboard offscreen layers
180+
frame_physical_depth_, // maximum depth allowed for rendering
181+
frame_device_pixel_ratio_ // ratio between logical and physical
160182
};
161183

162184
// Even if we don't have a root layer, we still need to create an empty

0 commit comments

Comments
 (0)