From 9f1f39c29ed4442085959bd49de7c23de74225ba Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 8 Aug 2017 16:09:52 -0700 Subject: [PATCH] Remove redundant height_ variable and add Paragraph::Paint Benchmark. Change-Id: I82c4aee759f6ae8a869782f4fb9d979d1551eed9 --- .../benchmarks/paragraph_benchmarks.cc | 31 +++++++++++++++++++ engine/src/flutter/src/paragraph.cc | 4 +-- engine/src/flutter/src/paragraph.h | 1 - 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc index e497168b08..ff8770b053 100644 --- a/engine/src/flutter/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/benchmarks/paragraph_benchmarks.cc @@ -28,6 +28,8 @@ #include "lib/txt/src/paragraph_builder.h" #include "lib/txt/src/text_align.h" #include "third_party/icu/source/common/unicode/unistr.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkColor.h" #include "utils.h" @@ -223,6 +225,35 @@ BENCHMARK(BM_ParagraphStylesBigO) ->Range(1 << 3, 1 << 12) ->Complexity(benchmark::oN); +static void BM_ParagraphPaintSimple(benchmark::State& state) { + const char* text = "Hello world! This is a simple sentence to test drawing."; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + + txt::TextStyle text_style; + text_style.color = SK_ColorBLACK; + auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + builder.PushStyle(text_style); + builder.AddText(u16_text); + auto paragraph = builder.Build(); + paragraph->Layout(300, true); + + std::unique_ptr bitmap = std::make_unique(); + std::unique_ptr canvas = std::make_unique(*bitmap); + bitmap->allocN32Pixels(1000, 1000); + canvas->clear(SK_ColorWHITE); + int offset = 0; + while (state.KeepRunning()) { + paragraph->Paint(canvas.get(), offset % 700, 10); + offset++; + } +} +BENCHMARK(BM_ParagraphPaintSimple); + // ----------------------------------------------------------------------------- // // The following benchmarks break down the layout function and attempts to time diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 4108503edc..04b77f1488 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -221,7 +221,6 @@ void Paragraph::Layout(double width, bool force) { line_heights_ = std::vector(); line_heights_.push_back(0); records_ = std::vector(); - height_ = 0.0f; // Set padding elements to have a minimum point. glyph_position_x_ = std::vector>(); @@ -477,7 +476,6 @@ void Paragraph::Layout(double width, bool force) { } // Handle last line tasks. y += roundf(max_line_spacing + prev_max_descent); - height_ = y + roundf(max_descent); postprocess_line(); if (line_width != 0) line_widths_.push_back(line_width); @@ -589,7 +587,7 @@ size_t Paragraph::TextSize() const { } double Paragraph::GetHeight() const { - return height_; + return line_heights_[line_heights_.size() - 2]; } double Paragraph::GetLayoutWidth() const { diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 9fb8865056..614e50197b 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -187,7 +187,6 @@ class Paragraph { // The max width of the paragraph as provided in the most recent Layout() // call. double width_ = -1.0f; - SkScalar height_ = 0.0f; size_t lines_ = 0; double max_intrinsic_width_ = 0; double min_intrinsic_width_ = 0;