From d9ad4ed4f3235328baa9c4f6861d6fbe36d90e8c Mon Sep 17 00:00:00 2001 From: George Wright Date: Fri, 18 Feb 2022 20:53:33 -0800 Subject: [PATCH] Tweak drawTextBlob cost based on multiple draw calls and update the benchmark accordingly (flutter/engine#31561) --- .../display_list/display_list_benchmarks.cc | 46 +++----------- .../display_list_complexity_gl.cc | 57 ++++++++--------- .../display_list/display_list_complexity_gl.h | 14 ++++- .../display_list_complexity_helper.h | 29 +++------ .../display_list_complexity_metal.cc | 63 +++++++++---------- .../display_list_complexity_metal.h | 14 ++++- .../display_list_complexity_unittests.cc | 7 +++ 7 files changed, 106 insertions(+), 124 deletions(-) diff --git a/engine/src/flutter/display_list/display_list_benchmarks.cc b/engine/src/flutter/display_list/display_list_benchmarks.cc index 0960649e91..ee41cd1e46 100644 --- a/engine/src/flutter/display_list/display_list_benchmarks.cc +++ b/engine/src/flutter/display_list/display_list_benchmarks.cc @@ -1182,49 +1182,21 @@ void BM_DrawTextBlob(benchmark::State& state, DisplayListOpFlags::kDrawTextBlobFlags); AnnotateAttributes(attributes, state, DisplayListOpFlags::kDrawTextBlobFlags); - size_t glyph_runs = state.range(0); + size_t draw_calls = state.range(0); size_t canvas_size = kFixedCanvasSize; canvas_provider->InitializeSurface(canvas_size, canvas_size); auto canvas = canvas_provider->GetSurface()->getCanvas(); - // We're just using plain Latin-1 where glyph count == character count - const char* string_fragment = "This text has exactly 32 glyphs."; - size_t fragment_length = strlen(string_fragment); - state.SetComplexityN(glyph_runs * fragment_length); + state.counters["DrawCallCount_Varies"] = draw_calls; + state.counters["GlyphCount"] = draw_calls; + char character[2] = {'A', '\0'}; - // TODO(gw280): different fonts - SkFont font; - - auto blob_fragment = SkTextBlob::MakeFromString(string_fragment, font); - auto bounds = blob_fragment->bounds(); - - // Calculate the approximate number of these glyph runs we can fit on a single - // canvas. - size_t x_count_max = canvas_size / bounds.width(); - size_t y_count_max = canvas_size / bounds.height(); - size_t remaining_runs = glyph_runs; - - SkTextBlobBuilder blob_builder; - size_t current_y = 0; - while (remaining_runs > 0) { - size_t runs_this_pass = std::min(x_count_max, remaining_runs); - auto buffer = blob_builder.allocRun( - font, runs_this_pass * fragment_length, 0, - ((current_y % y_count_max) + 1) * bounds.height()); - for (size_t i = 0; i < runs_this_pass; i++) { - font.textToGlyphs(string_fragment, fragment_length, SkTextEncoding::kUTF8, - buffer.glyphs + (i * fragment_length), fragment_length); - } - remaining_runs -= runs_this_pass; - current_y++; + for (size_t i = 0; i < draw_calls; i++) { + character[0] = 'A' + (i % 26); + auto blob = SkTextBlob::MakeFromString(character, SkFont()); + builder.drawTextBlob(blob, 50.0f, 50.0f); } - auto blob = blob_builder.make(); - - state.counters["DrawCallCount"] = 1; - state.counters["GlyphCount"] = glyph_runs * fragment_length; - builder.drawTextBlob(blob, 0.0f, 0.0f); - auto display_list = builder.Build(); for ([[maybe_unused]] auto _ : state) { @@ -1233,7 +1205,7 @@ void BM_DrawTextBlob(benchmark::State& state, } auto filename = canvas_provider->BackendName() + "-DrawTextBlob-" + - std::to_string(glyph_runs * fragment_length) + ".png"; + std::to_string(draw_calls) + ".png"; canvas_provider->Snapshot(filename); } diff --git a/engine/src/flutter/display_list/display_list_complexity_gl.cc b/engine/src/flutter/display_list/display_list_complexity_gl.cc index c08a038f1c..e7364f57ab 100644 --- a/engine/src/flutter/display_list/display_list_complexity_gl.cc +++ b/engine/src/flutter/display_list/display_list_complexity_gl.cc @@ -25,19 +25,36 @@ DisplayListGLComplexityCalculator::GetInstance() { return instance_; } -unsigned int -DisplayListGLComplexityCalculator::GLHelper::SaveLayerComplexity() { +unsigned int DisplayListGLComplexityCalculator::GLHelper::BatchedComplexity() { // Calculate the impact of saveLayer. unsigned int save_layer_complexity; - if (SaveLayerCount() == 0) { + if (save_layer_count_ == 0) { save_layer_complexity = 0; } else { // m = 1/5 // c = 10 - save_layer_complexity = (SaveLayerCount() + 50) * 40000; + save_layer_complexity = (save_layer_count_ + 50) * 40000; } - return save_layer_complexity; + unsigned int draw_text_blob_complexity; + if (draw_text_blob_count_ == 0) { + draw_text_blob_complexity = 0; + } else { + // m = 1/240 + // c = 0.25 + draw_text_blob_complexity = (draw_text_blob_count_ + 60) * 2500 / 3; + } + + return save_layer_complexity + draw_text_blob_complexity; +} + +void DisplayListGLComplexityCalculator::GLHelper::saveLayer( + const SkRect* bounds, + const SaveLayerOptions options) { + if (IsComplex()) { + return; + } + save_layer_count_++; } void DisplayListGLComplexityCalculator::GLHelper::drawLine(const SkPoint& p0, @@ -611,33 +628,13 @@ void DisplayListGLComplexityCalculator::GLHelper::drawTextBlob( if (IsComplex()) { return; } - // There are two classes here, hairline vs non-hairline. - // Hairline scales loglinearly with the number of glyphs. - // Non-hairline scales linearly. - // Unfortunately there is currently no way for us to figure out the glyph - // count from an SkTextBlob. We will need to figure out a better solution - // here, but for now just use a placeholder value of 100 glyphs. - unsigned int glyph_count = 100; + // DrawTextBlob has a high fixed cost, but if we call it multiple times + // per frame, that fixed cost is greatly reduced per subsequent call. This + // is likely because there is batching being done in SkCanvas. - // These values were worked out by creating a straight line graph (y=mx+c) - // approximately matching the measured data, normalising the data so that - // 0.0005ms resulted in a score of 100 then simplifying down the formula. - unsigned int complexity; - if (IsHairline() && Style() == SkPaint::Style::kStroke_Style) { - // If hairlines are on, we hit a degenerative case within Skia that causes - // our time to skyrocket. - // - // m = 1/65 - // c = 1 - complexity = (glyph_count + 65) * 40000 / 13; - } else { - // m = 1/3500 - // c = 0.5 - complexity = (glyph_count + 1750) * 40 / 7; - } - - AccumulateComplexity(complexity); + // Increment draw_text_blob_count_ and calculate the cost at the end. + draw_text_blob_count_++; } void DisplayListGLComplexityCalculator::GLHelper::drawShadow( diff --git a/engine/src/flutter/display_list/display_list_complexity_gl.h b/engine/src/flutter/display_list/display_list_complexity_gl.h index ae26a05de8..55732c4e38 100644 --- a/engine/src/flutter/display_list/display_list_complexity_gl.h +++ b/engine/src/flutter/display_list/display_list_complexity_gl.h @@ -32,7 +32,13 @@ class DisplayListGLComplexityCalculator private: class GLHelper : public ComplexityCalculatorHelper { public: - GLHelper(unsigned int ceiling) : ComplexityCalculatorHelper(ceiling) {} + GLHelper(unsigned int ceiling) + : ComplexityCalculatorHelper(ceiling), + save_layer_count_(0), + draw_text_blob_count_(0) {} + + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options) override; void drawLine(const SkPoint& p0, const SkPoint& p1) override; void drawRect(const SkRect& rect) override; @@ -75,7 +81,11 @@ class DisplayListGLComplexityCalculator bool render_with_attributes, SkCanvas::SrcRectConstraint constraint) override; - unsigned int SaveLayerComplexity() override; + unsigned int BatchedComplexity() override; + + private: + unsigned int save_layer_count_; + unsigned int draw_text_blob_count_; }; DisplayListGLComplexityCalculator() diff --git a/engine/src/flutter/display_list/display_list_complexity_helper.h b/engine/src/flutter/display_list/display_list_complexity_helper.h index 0196a391d0..1a37d6188c 100644 --- a/engine/src/flutter/display_list/display_list_complexity_helper.h +++ b/engine/src/flutter/display_list/display_list_complexity_helper.h @@ -96,10 +96,7 @@ class ComplexityCalculatorHelper public virtual IgnoreTransformDispatchHelper { public: ComplexityCalculatorHelper(unsigned int ceiling) - : is_complex_(false), - ceiling_(ceiling), - save_layer_count_(0), - complexity_score_(0) {} + : is_complex_(false), ceiling_(ceiling), complexity_score_(0) {} virtual ~ComplexityCalculatorHelper() = default; @@ -131,14 +128,6 @@ class ComplexityCalculatorHelper current_paint_.setStrokeWidth(width); } - void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) override { - if (IsComplex()) { - return; - } - save_layer_count_++; - } - void drawColor(SkColor color, SkBlendMode mode) override { if (IsComplex()) { return; @@ -221,15 +210,16 @@ class ComplexityCalculatorHelper return Ceiling(); } - // Calculate the impact of saveLayer. - unsigned int save_layer_complexity = SaveLayerComplexity(); + // Calculate the impact of any draw ops where the complexity is dependent + // on the number of calls made. + unsigned int batched_complexity = BatchedComplexity(); // Check for overflow - if (Ceiling() - complexity_score_ < save_layer_complexity) { + if (Ceiling() - complexity_score_ < batched_complexity) { return Ceiling(); } - return complexity_score_ + save_layer_complexity; + return complexity_score_ + batched_complexity; } protected: @@ -248,7 +238,6 @@ class ComplexityCalculatorHelper inline SkPaint::Style Style() { return current_paint_.getStyle(); } inline bool IsComplex() { return is_complex_; } inline unsigned int Ceiling() { return ceiling_; } - inline unsigned int SaveLayerCount() { return save_layer_count_; } inline unsigned int CurrentComplexityScore() { return complexity_score_; } unsigned int CalculatePathComplexity(const SkPath& path, @@ -285,7 +274,10 @@ class ComplexityCalculatorHelper bool render_with_attributes, SkCanvas::SrcRectConstraint constraint) = 0; - virtual unsigned int SaveLayerComplexity() = 0; + // This calculates and returns the cost of draw calls which are batched and + // thus have a time cost proportional to the number of draw calls made, such + // as saveLayer and drawTextBlob. + virtual unsigned int BatchedComplexity() = 0; private: SkPaint current_paint_; @@ -296,7 +288,6 @@ class ComplexityCalculatorHelper bool is_complex_; unsigned int ceiling_; - unsigned int save_layer_count_; unsigned int complexity_score_; }; diff --git a/engine/src/flutter/display_list/display_list_complexity_metal.cc b/engine/src/flutter/display_list/display_list_complexity_metal.cc index a8520fa443..ed1a48cd2f 100644 --- a/engine/src/flutter/display_list/display_list_complexity_metal.cc +++ b/engine/src/flutter/display_list/display_list_complexity_metal.cc @@ -26,27 +26,45 @@ DisplayListMetalComplexityCalculator::GetInstance() { } unsigned int -DisplayListMetalComplexityCalculator::MetalHelper::SaveLayerComplexity() { +DisplayListMetalComplexityCalculator::MetalHelper::BatchedComplexity() { // Calculate the impact of saveLayer. unsigned int save_layer_complexity; - if (SaveLayerCount() == 0) { + if (save_layer_count_ == 0) { save_layer_complexity = 0; } else { // saveLayer seems to have two trends; if the count is < 200, // then the individual cost of a saveLayer is higher than if // the count is > 200. - if (SaveLayerCount() > 200) { + if (save_layer_count_ > 200) { // m = 1/5 // c = 1 - save_layer_complexity = (SaveLayerCount() + 5) * 40000; + save_layer_complexity = (save_layer_count_ + 5) * 40000; } else { // m = 1/2 // c = 1 - save_layer_complexity = (SaveLayerCount() + 2) * 100000; + save_layer_complexity = (save_layer_count_ + 2) * 100000; } } - return save_layer_complexity; + unsigned int draw_text_blob_complexity; + if (draw_text_blob_count_ == 0) { + draw_text_blob_complexity = 0; + } else { + // m = 1/240 + // c = 0.75 + draw_text_blob_complexity = (draw_text_blob_count_ + 180) * 2500 / 3; + } + + return save_layer_complexity + draw_text_blob_complexity; +} + +void DisplayListMetalComplexityCalculator::MetalHelper::saveLayer( + const SkRect* bounds, + const SaveLayerOptions options) { + if (IsComplex()) { + return; + } + save_layer_count_++; } void DisplayListMetalComplexityCalculator::MetalHelper::drawLine( @@ -552,36 +570,13 @@ void DisplayListMetalComplexityCalculator::MetalHelper::drawTextBlob( if (IsComplex()) { return; } - // There are two classes here, hairline vs non-hairline. - // - // Hairline scales loglinearly with the number of glyphs. - // Non-hairline scales linearly. - // Unfortunately there is currently no way for us to figure out the glyph - // count from an SkTextBlob. We will need to figure out a better solution - // here, but for now just use a placeholder value of 100 glyphs. - unsigned int glyph_count = 100; + // DrawTextBlob has a high fixed cost, but if we call it multiple times + // per frame, that fixed cost is greatly reduced per subsequent call. This + // is likely because there is batching being done in SkCanvas. - // These values were worked out by creating a straight line graph (y=mx+c) - // approximately matching the measured data, normalising the data so that - // 0.0005ms resulted in a score of 100 then simplifying down the formula. - unsigned int complexity; - if (IsHairline() && Style() == SkPaint::Style::kStroke_Style) { - // If hairlines are on, we hit a degenerative case within Skia that causes - // our time to skyrocket. - // - // m = 1/3000 - // c = 1.75 - // x = glyph_count * log2(glyph_count) - unsigned int x = glyph_count * log(glyph_count); - complexity = (x + 5250) * 200 / 3; - } else { - // m = 1/5000 - // c = 0.75 - complexity = 40 * (glyph_count + 3750); - } - - AccumulateComplexity(complexity); + // Increment draw_text_blob_count_ and calculate the cost at the end. + draw_text_blob_count_++; } void DisplayListMetalComplexityCalculator::MetalHelper::drawShadow( diff --git a/engine/src/flutter/display_list/display_list_complexity_metal.h b/engine/src/flutter/display_list/display_list_complexity_metal.h index aae8685cbc..26131dee17 100644 --- a/engine/src/flutter/display_list/display_list_complexity_metal.h +++ b/engine/src/flutter/display_list/display_list_complexity_metal.h @@ -32,7 +32,13 @@ class DisplayListMetalComplexityCalculator private: class MetalHelper : public ComplexityCalculatorHelper { public: - MetalHelper(unsigned int ceiling) : ComplexityCalculatorHelper(ceiling) {} + MetalHelper(unsigned int ceiling) + : ComplexityCalculatorHelper(ceiling), + save_layer_count_(0), + draw_text_blob_count_(0) {} + + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options) override; void drawLine(const SkPoint& p0, const SkPoint& p1) override; void drawRect(const SkRect& rect) override; @@ -75,7 +81,11 @@ class DisplayListMetalComplexityCalculator bool render_with_attributes, SkCanvas::SrcRectConstraint constraint) override; - unsigned int SaveLayerComplexity() override; + unsigned int BatchedComplexity() override; + + private: + unsigned int save_layer_count_; + unsigned int draw_text_blob_count_; }; DisplayListMetalComplexityCalculator() diff --git a/engine/src/flutter/display_list/display_list_complexity_unittests.cc b/engine/src/flutter/display_list/display_list_complexity_unittests.cc index d75c6f47e2..88c6b3c947 100644 --- a/engine/src/flutter/display_list/display_list_complexity_unittests.cc +++ b/engine/src/flutter/display_list/display_list_complexity_unittests.cc @@ -304,9 +304,16 @@ TEST(DisplayListComplexity, DrawTextBlob) { builder.drawTextBlob(text_blob, 0.0f, 0.0f); auto display_list = builder.Build(); + DisplayListBuilder builder_multiple; + builder_multiple.drawTextBlob(text_blob, 0.0f, 0.0f); + builder_multiple.drawTextBlob(text_blob, 0.0f, 0.0f); + auto display_list_multiple = builder_multiple.Build(); + auto calculators = AccumulatorCalculators(); for (auto calculator : calculators) { ASSERT_NE(calculator->Compute(display_list.get()), 0u); + ASSERT_GT(calculator->Compute(display_list_multiple.get()), + calculator->Compute(display_list.get())); } }