diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0a6f8cd643..25ad2baf61 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -116,6 +116,8 @@ FILE: ../../../flutter/flow/gl_context_switch_unittests.cc FILE: ../../../flutter/flow/instrumentation.cc FILE: ../../../flutter/flow/instrumentation.h FILE: ../../../flutter/flow/instrumentation_unittests.cc +FILE: ../../../flutter/flow/layer_snapshot_store.cc +FILE: ../../../flutter/flow/layer_snapshot_store.h FILE: ../../../flutter/flow/layers/backdrop_filter_layer.cc FILE: ../../../flutter/flow/layers/backdrop_filter_layer.h FILE: ../../../flutter/flow/layers/backdrop_filter_layer_unittests.cc diff --git a/engine/src/flutter/flow/BUILD.gn b/engine/src/flutter/flow/BUILD.gn index 32e2cc2538..a0ced8fb85 100644 --- a/engine/src/flutter/flow/BUILD.gn +++ b/engine/src/flutter/flow/BUILD.gn @@ -18,6 +18,8 @@ source_set("flow") { "frame_timings.h", "instrumentation.cc", "instrumentation.h", + "layer_snapshot_store.cc", + "layer_snapshot_store.h", "layers/backdrop_filter_layer.cc", "layers/backdrop_filter_layer.h", "layers/clip_path_layer.cc", diff --git a/engine/src/flutter/flow/compositor_context.h b/engine/src/flutter/flow/compositor_context.h index 0777a28a2a..a49f4ee2cc 100644 --- a/engine/src/flutter/flow/compositor_context.h +++ b/engine/src/flutter/flow/compositor_context.h @@ -12,6 +12,7 @@ #include "flutter/flow/diff_context.h" #include "flutter/flow/embedded_views.h" #include "flutter/flow/instrumentation.h" +#include "flutter/flow/layer_snapshot_store.h" #include "flutter/flow/raster_cache.h" #include "flutter/fml/macros.h" #include "flutter/fml/raster_thread_merger.h" @@ -180,11 +181,14 @@ class CompositorContext { Stopwatch& ui_time() { return ui_time_; } + LayerSnapshotStore& snapshot_store() { return layer_snapshot_store_; } + private: RasterCache raster_cache_; TextureRegistry texture_registry_; Stopwatch raster_time_; Stopwatch ui_time_; + LayerSnapshotStore layer_snapshot_store_; /// Only used by default constructor of `CompositorContext`. FixedRefreshRateUpdater fixed_refresh_rate_updater_; diff --git a/engine/src/flutter/flow/layer_snapshot_store.cc b/engine/src/flutter/flow/layer_snapshot_store.cc new file mode 100644 index 0000000000..151302233b --- /dev/null +++ b/engine/src/flutter/flow/layer_snapshot_store.cc @@ -0,0 +1,29 @@ +// 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. + +#include "flutter/flow/layer_snapshot_store.h" + +#include "flutter/fml/time/time_delta.h" +#include "flutter/fml/time/time_point.h" + +namespace flutter { + +LayerSnapshotData::LayerSnapshotData(int64_t layer_unique_id, + fml::TimeDelta duration, + sk_sp snapshot) + : layer_unique_id_(layer_unique_id), + duration_(duration), + snapshot_(snapshot) {} + +void LayerSnapshotStore::Clear() { + layer_snapshots_.clear(); +} + +void LayerSnapshotStore::Add(int64_t layer_unique_id, + fml::TimeDelta duration, + sk_sp snapshot) { + layer_snapshots_.emplace_back(layer_unique_id, duration, snapshot); +} + +} // namespace flutter diff --git a/engine/src/flutter/flow/layer_snapshot_store.h b/engine/src/flutter/flow/layer_snapshot_store.h new file mode 100644 index 0000000000..78ede0516a --- /dev/null +++ b/engine/src/flutter/flow/layer_snapshot_store.h @@ -0,0 +1,77 @@ +// 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_FLOW_LAYER_SNAPSHOT_STORE_H_ +#define FLUTTER_FLOW_LAYER_SNAPSHOT_STORE_H_ + +#include + +#include "flutter/fml/logging.h" +#include "flutter/fml/time/time_delta.h" + +#include "third_party/skia/include/core/SkImageEncoder.h" +#include "third_party/skia/include/core/SkPictureRecorder.h" +#include "third_party/skia/include/core/SkSerialProcs.h" +#include "third_party/skia/include/core/SkSurface.h" +#include "third_party/skia/include/core/SkSurfaceCharacterization.h" +#include "third_party/skia/include/utils/SkBase64.h" + +namespace flutter { + +/// Container for snapshot data pertaining to a given layer. A layer is +/// identified by it's unique id. +class LayerSnapshotData { + public: + LayerSnapshotData(int64_t layer_unique_id, + fml::TimeDelta duration, + sk_sp snapshot); + + ~LayerSnapshotData() = default; + + int64_t GetLayerUniqueId() const { return layer_unique_id_; } + + fml::TimeDelta GetDuration() const { return duration_; } + + sk_sp GetSnapshot() const { return snapshot_; } + + private: + const int64_t layer_unique_id_; + const fml::TimeDelta duration_; + const sk_sp snapshot_; +}; + +/// Collects snapshots of layers during frame rasterization. +class LayerSnapshotStore { + public: + typedef std::vector Snapshots; + + LayerSnapshotStore() = default; + + ~LayerSnapshotStore() = default; + + /// Clears all the stored snapshots. + void Clear(); + + /// Adds snapshots for a given layer. `duration` marks the time taken to + /// rasterize this one layer. + void Add(int64_t layer_unique_id, + fml::TimeDelta duration, + sk_sp base64_png_snapshot); + + // Returns the number of snapshots collected. + size_t Size() const { return layer_snapshots_.size(); } + + // make this class iterable + Snapshots::iterator begin() { return layer_snapshots_.begin(); } + Snapshots::iterator end() { return layer_snapshots_.end(); } + + private: + Snapshots layer_snapshots_; + + FML_DISALLOW_COPY_AND_ASSIGN(LayerSnapshotStore); +}; + +} // namespace flutter + +#endif // FLUTTER_FLOW_LAYER_SNAPSHOT_STORE_H_ diff --git a/engine/src/flutter/flow/layers/display_list_layer.cc b/engine/src/flutter/flow/layers/display_list_layer.cc index 6299f9f303..e5fe2554c8 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.cc +++ b/engine/src/flutter/flow/layers/display_list_layer.cc @@ -5,6 +5,7 @@ #include "flutter/flow/layers/display_list_layer.h" #include "flutter/display_list/display_list_builder.h" +#include "flutter/flow/layers/offscreen_surface.h" namespace flutter { @@ -132,6 +133,29 @@ void DisplayListLayer::Paint(PaintContext& context) const { } } + if (context.enable_leaf_layer_tracing) { + const auto canvas_size = context.leaf_nodes_canvas->getBaseLayerSize(); + auto offscreen_surface = + std::make_unique(context.gr_context, canvas_size); + + const auto start_time = fml::TimePoint::Now(); + { + // render display list to offscreen surface. + auto* canvas = offscreen_surface->GetCanvas(); + SkAutoCanvasRestore save(canvas, true); + canvas->clear(SK_ColorTRANSPARENT); + canvas->setMatrix(context.leaf_nodes_canvas->getTotalMatrix()); + display_list()->RenderTo(canvas, context.inherited_opacity); + canvas->flush(); + } + const fml::TimeDelta offscreen_render_time = + fml::TimePoint::Now() - start_time; + + sk_sp raster_data = offscreen_surface->GetRasterData(true); + context.layer_snapshot_store->Add(unique_id(), offscreen_render_time, + raster_data); + } + if (context.leaf_nodes_builder) { display_list()->RenderTo(context.leaf_nodes_builder, context.inherited_opacity); diff --git a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc index 229b548912..02f6f02b64 100644 --- a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc @@ -178,5 +178,40 @@ TEST_F(DisplayListLayerDiffTest, DisplayListCompare) { EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(20, 20, 70, 70)); } +TEST_F(DisplayListLayerTest, LayerTreeSnapshotsWhenEnabled) { + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + DisplayListBuilder builder; + builder.drawRect(picture_bounds); + auto display_list = builder.Build(); + auto layer = std::make_shared( + layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + + layer->Preroll(preroll_context(), SkMatrix()); + + enable_leaf_layer_tracing(); + layer->Paint(paint_context()); + disable_leaf_layer_tracing(); + + auto& snapshot_store = layer_snapshot_store(); + EXPECT_EQ(1u, snapshot_store.Size()); +} + +TEST_F(DisplayListLayerTest, NoLayerTreeSnapshotsWhenDisabledByDefault) { + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + DisplayListBuilder builder; + builder.drawRect(picture_bounds); + auto display_list = builder.Build(); + auto layer = std::make_shared( + layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + + layer->Preroll(preroll_context(), SkMatrix()); + layer->Paint(paint_context()); + + auto& snapshot_store = layer_snapshot_store(); + EXPECT_EQ(0u, snapshot_store.Size()); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/layer.h b/engine/src/flutter/flow/layers/layer.h index 5c255366f4..1f6569ab0a 100644 --- a/engine/src/flutter/flow/layers/layer.h +++ b/engine/src/flutter/flow/layers/layer.h @@ -12,6 +12,7 @@ #include "flutter/flow/diff_context.h" #include "flutter/flow/embedded_views.h" #include "flutter/flow/instrumentation.h" +#include "flutter/flow/layer_snapshot_store.h" #include "flutter/flow/raster_cache.h" #include "flutter/fml/build_config.h" #include "flutter/fml/compiler_specific.h" @@ -168,6 +169,11 @@ class Layer { const bool checkerboard_offscreen_layers; const float frame_device_pixel_ratio; + // Snapshot store to collect leaf layer snapshots. The store is non-null + // only when leaf layer tracing is enabled. + bool enable_leaf_layer_tracing = false; + LayerSnapshotStore* layer_snapshot_store = nullptr; + // The following value should be used to modulate the opacity of the // layer during |Paint|. If the layer does not set the corresponding // |layer_can_inherit_opacity()| flag, then this value should always @@ -180,7 +186,7 @@ class Layer { class AutoCachePaint { public: - AutoCachePaint(PaintContext& context) : context_(context) { + explicit AutoCachePaint(PaintContext& context) : context_(context) { needs_paint_ = context.inherited_opacity < SK_Scalar1; if (needs_paint_) { paint_.setAlphaf(context.inherited_opacity); diff --git a/engine/src/flutter/flow/layers/layer_tree.cc b/engine/src/flutter/flow/layers/layer_tree.cc index 88a4c9d6e2..f2c4e7bc1a 100644 --- a/engine/src/flutter/flow/layers/layer_tree.cc +++ b/engine/src/flutter/flow/layers/layer_tree.cc @@ -5,6 +5,7 @@ #include "flutter/flow/layers/layer_tree.h" #include "flutter/flow/frame_timings.h" +#include "flutter/flow/layer_snapshot_store.h" #include "flutter/flow/layers/layer.h" #include "flutter/fml/time/time_point.h" #include "flutter/fml/trace_event.h" @@ -74,6 +75,13 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame, } } + // clear the previous snapshots. + LayerSnapshotStore* snapshot_store = nullptr; + if (enable_leaf_layer_tracing_) { + frame.context().snapshot_store().Clear(); + snapshot_store = &frame.context().snapshot_store(); + } + Layer::PaintContext context = { static_cast(&internal_nodes_canvas), frame.canvas(), @@ -85,6 +93,8 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame, ignore_raster_cache ? nullptr : &frame.context().raster_cache(), checkerboard_offscreen_layers_, device_pixel_ratio_, + enable_leaf_layer_tracing_, + snapshot_store, SK_Scalar1, frame.display_list_builder(), }; @@ -140,7 +150,9 @@ sk_sp LayerTree::Flatten(const SkRect& bounds) { unused_texture_registry, // texture registry (not supported) nullptr, // raster cache false, // checkerboard offscreen layers - device_pixel_ratio_ // ratio between logical and physical + device_pixel_ratio_, // ratio between logical and physical + false, // enable_leaf_layer_tracing + nullptr // layer_snapshot_store }; // Even if we don't have a root layer, we still need to create an empty diff --git a/engine/src/flutter/flow/layers/layer_tree.h b/engine/src/flutter/flow/layers/layer_tree.h index 17b969dcf6..4ee8b0d9a4 100644 --- a/engine/src/flutter/flow/layers/layer_tree.h +++ b/engine/src/flutter/flow/layers/layer_tree.h @@ -68,6 +68,18 @@ class LayerTree { checkerboard_offscreen_layers_ = checkerboard; } + /// When `Paint` is called, if leaf layer tracing is enabled, additional + /// metadata around raterization of leaf layers is collected. + /// + /// See: `LayerSnapshotStore` + void enable_leaf_layer_tracing(bool enable) { + enable_leaf_layer_tracing_ = enable; + } + + bool is_leaf_layer_tracing_enabled() const { + return enable_leaf_layer_tracing_; + } + private: std::shared_ptr root_layer_; SkISize frame_size_ = SkISize::MakeEmpty(); // Physical pixels. @@ -75,6 +87,7 @@ class LayerTree { uint32_t rasterizer_tracing_threshold_; bool checkerboard_raster_cache_images_; bool checkerboard_offscreen_layers_; + bool enable_leaf_layer_tracing_ = false; PaintRegionMap paint_region_map_; diff --git a/engine/src/flutter/flow/testing/layer_test.h b/engine/src/flutter/flow/testing/layer_test.h index 1e810ec80d..38b29c0769 100644 --- a/engine/src/flutter/flow/testing/layer_test.h +++ b/engine/src/flutter/flow/testing/layer_test.h @@ -5,6 +5,7 @@ #ifndef FLOW_TESTING_LAYER_TEST_H_ #define FLOW_TESTING_LAYER_TEST_H_ +#include "flutter/flow/layer_snapshot_store.h" #include "flutter/flow/layers/layer.h" #include @@ -128,6 +129,17 @@ class LayerTestBase : public CanvasTestBase { PrerollContext* preroll_context() { return &preroll_context_; } Layer::PaintContext& paint_context() { return paint_context_; } Layer::PaintContext& check_board_context() { return check_board_context_; } + LayerSnapshotStore& layer_snapshot_store() { return snapshot_store_; } + + void enable_leaf_layer_tracing() { + paint_context_.enable_leaf_layer_tracing = true; + paint_context_.layer_snapshot_store = &snapshot_store_; + } + + void disable_leaf_layer_tracing() { + paint_context_.enable_leaf_layer_tracing = false; + paint_context_.layer_snapshot_store = nullptr; + } private: void set_raster_cache_(std::unique_ptr raster_cache) { @@ -145,6 +157,7 @@ class LayerTestBase : public CanvasTestBase { PrerollContext preroll_context_; Layer::PaintContext paint_context_; Layer::PaintContext check_board_context_; + LayerSnapshotStore snapshot_store_; FML_DISALLOW_COPY_AND_ASSIGN(LayerTestBase); }; diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index 5941ca68ec..36fff4a281 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -567,7 +567,10 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe( frame_timings_recorder.RecordRasterStart(fml::TimePoint::Now()); std::unique_ptr damage; - if (frame->framebuffer_info().supports_partial_repaint) { + // when leaf layer tracing is enabled we wish to repaing the whole frame for + // accurate performance metrics. + if (frame->framebuffer_info().supports_partial_repaint && + !layer_tree.is_leaf_layer_tracing_enabled()) { // Disable partial repaint if external_view_embedder_ SubmitFrame is // involved - ExternalViewEmbedder unconditionally clears the entire // surface and also partial repaint with platform view present is @@ -586,11 +589,17 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe( } } - RasterStatus raster_status = compositor_frame->Raster( - layer_tree, // layer tree - !surface_->EnableRasterCache(), // ignore raster cache - damage.get() // frame damage - ); + bool ignore_raster_cache = true; + if (surface_->EnableRasterCache() && + !layer_tree.is_leaf_layer_tracing_enabled()) { + ignore_raster_cache = false; + } + + RasterStatus raster_status = + compositor_frame->Raster(layer_tree, // layer tree + ignore_raster_cache, // ignore raster cache + damage.get() // frame damage + ); if (raster_status == RasterStatus::kFailed || raster_status == RasterStatus::kSkipAndRetry) { return raster_status;