Add a DisplayListComplexityCalculator class that calculates the current "complexity score" of a given DisplayList during the Build phase. (flutter/engine#31176)

This commit is contained in:
George Wright
2022-02-01 21:40:35 -08:00
committed by GitHub
parent 630bd6634a
commit a25be9e8d9
12 changed files with 200 additions and 32 deletions

View File

@@ -52,6 +52,8 @@ FILE: ../../../flutter/display_list/display_list_canvas_dispatcher.h
FILE: ../../../flutter/display_list/display_list_canvas_recorder.cc
FILE: ../../../flutter/display_list/display_list_canvas_recorder.h
FILE: ../../../flutter/display_list/display_list_canvas_unittests.cc
FILE: ../../../flutter/display_list/display_list_complexity.cc
FILE: ../../../flutter/display_list/display_list_complexity.h
FILE: ../../../flutter/display_list/display_list_dispatcher.cc
FILE: ../../../flutter/display_list/display_list_dispatcher.h
FILE: ../../../flutter/display_list/display_list_flags.cc

View File

@@ -14,6 +14,8 @@ source_set("display_list") {
"display_list_canvas_dispatcher.h",
"display_list_canvas_recorder.cc",
"display_list_canvas_recorder.h",
"display_list_complexity.cc",
"display_list_complexity.h",
"display_list_dispatcher.cc",
"display_list_dispatcher.h",
"display_list_flags.cc",

View File

@@ -32,9 +32,9 @@ DisplayList::DisplayList()
DisplayList::DisplayList(uint8_t* ptr,
size_t byte_count,
int op_count,
unsigned int op_count,
size_t nested_byte_count,
int nested_op_count,
unsigned int nested_op_count,
const SkRect& cull_rect,
bool can_apply_group_opacity)
: storage_(ptr),

View File

@@ -179,7 +179,7 @@ class DisplayList : public SkRefCnt {
(nested ? nested_byte_count_ : 0);
}
int op_count(bool nested = false) const {
unsigned int op_count(bool nested = false) const {
return op_count_ + (nested ? nested_op_count_ : 0);
}
@@ -203,18 +203,18 @@ class DisplayList : public SkRefCnt {
private:
DisplayList(uint8_t* ptr,
size_t byte_count,
int op_count,
unsigned int op_count,
size_t nested_byte_count,
int nested_op_count,
unsigned int nested_op_count,
const SkRect& cull_rect,
bool can_apply_group_opacity);
std::unique_ptr<uint8_t, SkFunctionWrapper<void(void*), sk_free>> storage_;
size_t byte_count_;
int op_count_;
unsigned int op_count_;
size_t nested_byte_count_;
int nested_op_count_;
unsigned int nested_op_count_;
uint32_t unique_id_;
SkRect bounds_;

View File

@@ -1732,7 +1732,8 @@ class CanvasCompareTester {
// of the embedded calls in the display list and so the op counts
// will not be equal between the two.
if (!testP.is_draw_display_list()) {
EXPECT_EQ(display_list->op_count(), sk_picture->approximateOpCount())
EXPECT_EQ(static_cast<int>(display_list->op_count()),
sk_picture->approximateOpCount())
<< info;
}

View File

@@ -0,0 +1,35 @@
// 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/display_list/display_list_complexity.h"
#include "flutter/display_list/display_list.h"
namespace flutter {
DisplayListNaiveComplexityCalculator*
DisplayListNaiveComplexityCalculator::instance_ = nullptr;
DisplayListComplexityCalculator*
DisplayListNaiveComplexityCalculator::GetInstance() {
if (instance_ == nullptr) {
instance_ = new DisplayListNaiveComplexityCalculator();
}
return instance_;
}
DisplayListComplexityCalculator* DisplayListComplexityCalculator::GetForBackend(
GrBackendApi backend) {
switch (backend) {
default:
return DisplayListNaiveComplexityCalculator::GetInstance();
}
}
DisplayListComplexityCalculator*
DisplayListComplexityCalculator::GetForSoftware() {
return DisplayListNaiveComplexityCalculator::GetInstance();
}
} // namespace flutter

View File

@@ -0,0 +1,49 @@
// 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_DISPLAY_LIST_COMPLEXITY_H_
#define FLUTTER_FLOW_DISPLAY_LIST_COMPLEXITY_H_
#include "flutter/display_list/display_list.h"
#include "flutter/display_list/types.h"
#include "third_party/skia/include/gpu/GrTypes.h"
namespace flutter {
class DisplayListComplexityCalculator {
public:
static DisplayListComplexityCalculator* GetForSoftware();
static DisplayListComplexityCalculator* GetForBackend(GrBackendApi backend);
virtual ~DisplayListComplexityCalculator() = default;
// Returns a calculated complexity score for a given DisplayList object
virtual unsigned int compute(DisplayList* display_list) = 0;
// Returns whether a given complexity score meets the threshold for
// cacheability for this particular ComplexityCalculator
virtual bool should_be_cached(unsigned int complexity_score) = 0;
};
class DisplayListNaiveComplexityCalculator
: public DisplayListComplexityCalculator {
public:
static DisplayListComplexityCalculator* GetInstance();
unsigned int compute(DisplayList* display_list) override {
return display_list->op_count(true);
}
bool should_be_cached(unsigned int complexity_score) override {
return complexity_score > 5u;
}
private:
DisplayListNaiveComplexityCalculator() {}
static DisplayListNaiveComplexityCalculator* instance_;
};
} // namespace flutter
#endif // FLUTTER_FLOW_DISPLAY_LIST_COMPLEXITY_H_

View File

@@ -213,7 +213,7 @@ static sk_sp<SkTextBlob> TestBlob2 = MakeTextBlob("TestBlob2");
typedef const std::function<void(DisplayListBuilder&)> DlInvoker;
struct DisplayListInvocation {
int op_count_;
unsigned int op_count_;
size_t byte_count_;
// in some cases, running the sequence through an SkCanvas will result
@@ -226,7 +226,8 @@ struct DisplayListInvocation {
bool supports_group_opacity_ = false;
bool sk_version_matches() {
return (op_count_ == sk_op_count_ && byte_count_ == sk_byte_count_);
return (static_cast<int>(op_count_) == sk_op_count_ &&
byte_count_ == sk_byte_count_);
}
// A negative sk_op_count means "do not test this op".
@@ -240,7 +241,7 @@ struct DisplayListInvocation {
bool supports_group_opacity() { return supports_group_opacity_; }
int op_count() { return op_count_; }
unsigned int op_count() { return op_count_; }
// byte count for the individual ops, no DisplayList overhead
size_t raw_byte_count() { return byte_count_; }
// byte count for the ops with DisplayList overhead, comparable
@@ -797,7 +798,8 @@ TEST(DisplayList, SingleOpDisplayListsRecapturedViaSkCanvasAreEqual) {
dl->RenderTo(&recorder);
sk_sp<DisplayList> sk_copy = recorder.Build();
auto desc = group.op_name + "[variant " + std::to_string(i + 1) + "]";
EXPECT_EQ(sk_copy->op_count(false), group.variants[i].sk_op_count())
EXPECT_EQ(static_cast<int>(sk_copy->op_count(false)),
group.variants[i].sk_op_count())
<< desc;
EXPECT_EQ(sk_copy->bytes(false), group.variants[i].sk_byte_count())
<< desc;
@@ -861,8 +863,8 @@ TEST(DisplayList, FullRotationsAreNop) {
sk_sp<DisplayList> dl = builder.Build();
ASSERT_EQ(dl->bytes(false), sizeof(DisplayList));
ASSERT_EQ(dl->bytes(true), sizeof(DisplayList));
ASSERT_EQ(dl->op_count(false), 0);
ASSERT_EQ(dl->op_count(true), 0);
ASSERT_EQ(dl->op_count(false), 0u);
ASSERT_EQ(dl->op_count(true), 0u);
}
TEST(DisplayList, AllBlendModeNops) {
@@ -872,13 +874,13 @@ TEST(DisplayList, AllBlendModeNops) {
sk_sp<DisplayList> dl = builder.Build();
ASSERT_EQ(dl->bytes(false), sizeof(DisplayList));
ASSERT_EQ(dl->bytes(true), sizeof(DisplayList));
ASSERT_EQ(dl->op_count(false), 0);
ASSERT_EQ(dl->op_count(true), 0);
ASSERT_EQ(dl->op_count(false), 0u);
ASSERT_EQ(dl->op_count(true), 0u);
}
static sk_sp<DisplayList> Build(size_t g_index, size_t v_index) {
DisplayListBuilder builder;
int op_count = 0;
unsigned int op_count = 0;
size_t byte_count = 0;
for (size_t i = 0; i < allGroups.size(); i++) {
DisplayListInvocationGroup& group = allGroups[i];
@@ -1128,18 +1130,20 @@ TEST(DisplayList, NestedOpCountMetricsSameAsSkPicture) {
outer_builder.drawDisplayList(builder.Build());
auto display_list = outer_builder.Build();
ASSERT_EQ(display_list->op_count(), 1);
ASSERT_EQ(display_list->op_count(true), 36);
ASSERT_EQ(display_list->op_count(), 1u);
ASSERT_EQ(display_list->op_count(true), 36u);
ASSERT_EQ(picture->approximateOpCount(), display_list->op_count());
ASSERT_EQ(picture->approximateOpCount(true), display_list->op_count(true));
ASSERT_EQ(picture->approximateOpCount(),
static_cast<int>(display_list->op_count()));
ASSERT_EQ(picture->approximateOpCount(true),
static_cast<int>(display_list->op_count(true)));
DisplayListCanvasRecorder dl_recorder(SkRect::MakeWH(150, 100));
picture->playback(&dl_recorder);
auto sk_display_list = dl_recorder.Build();
ASSERT_EQ(display_list->op_count(), 1);
ASSERT_EQ(display_list->op_count(true), 36);
ASSERT_EQ(display_list->op_count(), 1u);
ASSERT_EQ(display_list->op_count(true), 36u);
}
class AttributeRefTester {
@@ -1375,7 +1379,7 @@ TEST(DisplayList, SetMaskBlurSigmaZeroResetsMaskFilter) {
EXPECT_EQ(builder.getMaskFilter(), nullptr);
builder.drawRect({30, 30, 40, 40});
sk_sp<DisplayList> display_list = builder.Build();
ASSERT_EQ(display_list->op_count(), 2);
ASSERT_EQ(display_list->op_count(), 2u);
ASSERT_EQ(display_list->bytes(), sizeof(DisplayList) + 8u + 24u + 8u + 24u);
}
@@ -1387,7 +1391,7 @@ TEST(DisplayList, SetMaskFilterNullResetsMaskFilter) {
EXPECT_EQ(builder.getMaskFilter(), nullptr);
builder.drawRect({30, 30, 40, 40});
sk_sp<DisplayList> display_list = builder.Build();
ASSERT_EQ(display_list->op_count(), 2);
ASSERT_EQ(display_list->op_count(), 2u);
ASSERT_EQ(display_list->bytes(), sizeof(DisplayList) + 8u + 24u + 8u + 24u);
}

View File

@@ -20,6 +20,7 @@
#include "third_party/skia/include/core/SkShader.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "third_party/skia/include/core/SkVertices.h"
#include "third_party/skia/include/gpu/GrTypes.h"
#include "third_party/skia/include/utils/SkShadowUtils.h"
#endif // FLUTTER_DISPLAY_LIST_TYPES_H_

View File

@@ -86,9 +86,11 @@ static bool IsPictureWorthRasterizing(SkPicture* picture,
return picture->approximateOpCount(true) > 5;
}
static bool IsDisplayListWorthRasterizing(DisplayList* display_list,
bool will_change,
bool is_complex) {
static bool IsDisplayListWorthRasterizing(
DisplayList* display_list,
bool will_change,
bool is_complex,
DisplayListComplexityCalculator* complexity_calculator) {
if (will_change) {
// If the display list is going to change in the future, there is no point
// in doing to extra work to rasterize.
@@ -109,7 +111,8 @@ static bool IsDisplayListWorthRasterizing(DisplayList* display_list,
// TODO(abarth): We should find a better heuristic here that lets us avoid
// wasting memory on trivial layers that are easy to re-rasterize every frame.
return display_list->op_count(true) > 5;
int complexity_score = complexity_calculator->compute(display_list);
return complexity_calculator->should_be_cached(complexity_score);
}
/// @note Procedure doesn't copy all closures.
@@ -273,7 +276,13 @@ bool RasterCache::Prepare(PrerollContext* context,
return false;
}
if (!IsDisplayListWorthRasterizing(display_list, will_change, is_complex)) {
DisplayListComplexityCalculator* complexity_calculator =
context->gr_context ? DisplayListComplexityCalculator::GetForBackend(
context->gr_context->backend())
: DisplayListComplexityCalculator::GetForSoftware();
if (!IsDisplayListWorthRasterizing(display_list, will_change, is_complex,
complexity_calculator)) {
// We only deal with display lists that are worthy of rasterization.
return false;
}

View File

@@ -9,6 +9,7 @@
#include <unordered_map>
#include "flutter/display_list/display_list.h"
#include "flutter/display_list/display_list_complexity.h"
#include "flutter/flow/raster_cache_key.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"

View File

@@ -64,6 +64,14 @@ sk_sp<DisplayList> GetSampleNestedDisplayList() {
return outer_builder.Build();
}
sk_sp<DisplayList> GetSampleDisplayList(int ops) {
DisplayListBuilder builder(SkRect::MakeWH(150, 100));
for (int i = 0; i < ops; i++) {
builder.drawColor(SK_ColorRED, SkBlendMode::kSrc);
}
return builder.Build();
}
} // namespace
TEST(RasterCache, SimpleInitialization) {
@@ -452,8 +460,8 @@ TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) {
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleNestedDisplayList();
ASSERT_EQ(display_list->op_count(), 1);
ASSERT_EQ(display_list->op_count(true), 36);
ASSERT_EQ(display_list->op_count(), 1u);
ASSERT_EQ(display_list->op_count(true), 36u);
SkCanvas dummy_canvas;
@@ -473,6 +481,62 @@ TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) {
ASSERT_TRUE(cache.Draw(*display_list, dummy_canvas));
}
TEST(RasterCache, NaiveComplexityScoringDisplayList) {
DisplayListComplexityCalculator* calculator =
DisplayListNaiveComplexityCalculator::GetInstance();
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
// Five raster ops will not be cached
auto display_list = GetSampleDisplayList(5);
unsigned int complexity_score = calculator->compute(display_list.get());
ASSERT_EQ(complexity_score, 5u);
ASSERT_EQ(display_list->op_count(), 5u);
ASSERT_FALSE(calculator->should_be_cached(complexity_score));
SkCanvas dummy_canvas;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
cache.PrepareNewFrame();
ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
display_list.get(), false, false, matrix));
ASSERT_FALSE(cache.Draw(*display_list, dummy_canvas));
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
display_list.get(), false, false, matrix));
ASSERT_FALSE(cache.Draw(*display_list, dummy_canvas));
// Six raster ops should be cached
display_list = GetSampleDisplayList(6);
complexity_score = calculator->compute(display_list.get());
ASSERT_EQ(complexity_score, 6u);
ASSERT_EQ(display_list->op_count(), 6u);
ASSERT_TRUE(calculator->should_be_cached(complexity_score));
cache.PrepareNewFrame();
ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
display_list.get(), false, false, matrix));
ASSERT_FALSE(cache.Draw(*display_list, dummy_canvas));
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
ASSERT_TRUE(cache.Prepare(&preroll_context_holder.preroll_context,
display_list.get(), false, false, matrix));
ASSERT_TRUE(cache.Draw(*display_list, dummy_canvas));
}
TEST(RasterCache, SkPictureWithSingularMatrixIsNotCached) {
size_t threshold = 2;
flutter::RasterCache cache(threshold);