Enable leaf layer tracing in PaintContext (flutter/engine#32383)

This commit is contained in:
Kaushik Iska
2022-04-11 21:24:04 -04:00
committed by GitHub
parent cf5855af97
commit 172d0b24e5
12 changed files with 234 additions and 8 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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_;

View File

@@ -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<SkData> 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<SkData> snapshot) {
layer_snapshots_.emplace_back(layer_unique_id, duration, snapshot);
}
} // namespace flutter

View File

@@ -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 <vector>
#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<SkData> snapshot);
~LayerSnapshotData() = default;
int64_t GetLayerUniqueId() const { return layer_unique_id_; }
fml::TimeDelta GetDuration() const { return duration_; }
sk_sp<SkData> GetSnapshot() const { return snapshot_; }
private:
const int64_t layer_unique_id_;
const fml::TimeDelta duration_;
const sk_sp<SkData> snapshot_;
};
/// Collects snapshots of layers during frame rasterization.
class LayerSnapshotStore {
public:
typedef std::vector<LayerSnapshotData> 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<SkData> 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_

View File

@@ -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<OffscreenSurface>(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<SkData> 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);

View File

@@ -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<DisplayListLayer>(
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<DisplayListLayer>(
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

View File

@@ -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);

View File

@@ -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<SkCanvas*>(&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<SkPicture> 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

View File

@@ -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<Layer> 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_;

View File

@@ -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 <optional>
@@ -128,6 +129,17 @@ class LayerTestBase : public CanvasTestBase<BaseT> {
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<RasterCache> raster_cache) {
@@ -145,6 +157,7 @@ class LayerTestBase : public CanvasTestBase<BaseT> {
PrerollContext preroll_context_;
Layer::PaintContext paint_context_;
Layer::PaintContext check_board_context_;
LayerSnapshotStore snapshot_store_;
FML_DISALLOW_COPY_AND_ASSIGN(LayerTestBase);
};

View File

@@ -567,7 +567,10 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe(
frame_timings_recorder.RecordRasterStart(fml::TimePoint::Now());
std::unique_ptr<FrameDamage> 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;