From db6a6c880929541f6af7486294290e4eaf0ddfcc Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 1 Sep 2017 12:27:35 -0700 Subject: [PATCH] Fix the libTXT benchmarking target for API updates and compile on Mac. (flutter/engine#4049) --- engine/src/flutter/BUILD.gn | 4 +- engine/src/flutter/third_party/txt/BUILD.gn | 1 - .../benchmarks/font_collection_benchmarks.cc | 68 ------------------- .../txt/benchmarks/paragraph_benchmarks.cc | 39 +++++------ .../paragraph_builder_benchmarks.cc | 33 +++++---- .../third_party/txt/benchmarks/utils.cc | 11 ++- .../third_party/txt/benchmarks/utils.h | 6 +- 7 files changed, 51 insertions(+), 111 deletions(-) delete mode 100644 engine/src/flutter/third_party/txt/benchmarks/font_collection_benchmarks.cc diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index f8a0f8f06a..978b6e74b5 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -43,12 +43,10 @@ group("flutter") { "//flutter/fml:fml_unittests", "//flutter/sky/engine/wtf:wtf_unittests", "//flutter/synchronization:synchronization_unittests", + "//flutter/third_party/txt:txt_benchmarks", "//flutter/third_party/txt:txt_unittests", "//garnet/public/lib/ftl:ftl_unittests", ] - if (is_linux) { - deps += [ "//flutter/third_party/txt:txt_benchmarks" ] - } } } diff --git a/engine/src/flutter/third_party/txt/BUILD.gn b/engine/src/flutter/third_party/txt/BUILD.gn index 3c75760bdc..60bb416ee1 100644 --- a/engine/src/flutter/third_party/txt/BUILD.gn +++ b/engine/src/flutter/third_party/txt/BUILD.gn @@ -167,7 +167,6 @@ executable("txt_benchmarks") { testonly = true sources = [ - "benchmarks/font_collection_benchmarks.cc", "benchmarks/paint_record_benchmarks.cc", "benchmarks/paragraph_benchmarks.cc", "benchmarks/paragraph_builder_benchmarks.cc", diff --git a/engine/src/flutter/third_party/txt/benchmarks/font_collection_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/font_collection_benchmarks.cc deleted file mode 100644 index 78377b3a6c..0000000000 --- a/engine/src/flutter/third_party/txt/benchmarks/font_collection_benchmarks.cc +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2017 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "third_party/benchmark/include/benchmark/benchmark_api.h" - -#include "lib/ftl/command_line.h" -#include "lib/ftl/logging.h" -#include "third_party/skia/include/ports/SkFontMgr.h" -#include "third_party/skia/include/ports/SkFontMgr_directory.h" -#include "txt/font_collection.h" -#include "utils.h" - -namespace txt { - -// Include this fake bench first because the first benchmark produces -// inconsistent times. -static void BM_FAKE_BENCHMARK(benchmark::State& state) { - while (state.KeepRunning()) { - continue; - } -} -BENCHMARK(BM_FAKE_BENCHMARK); - -static void BM_FontCollectionCustomInit(benchmark::State& state) { - while (state.KeepRunning()) { - benchmark::DoNotOptimize( - FontCollection::GetFontCollection(txt::GetFontDir())); - } -} -BENCHMARK(BM_FontCollectionCustomInit); - -static void BM_FontCollectionInit(benchmark::State& state) { - while (state.KeepRunning()) { - benchmark::DoNotOptimize(FontCollection::GetFontCollection()); - } -} -BENCHMARK(BM_FontCollectionInit); - -static void BM_FontCollectionSkFontMgr(benchmark::State& state) { - while (state.KeepRunning()) { - auto mgr = SkFontMgr_New_Custom_Directory(txt::GetFontDir().c_str()); - } -} -BENCHMARK(BM_FontCollectionSkFontMgr); - -static void BM_FontCollectionGetMinikinFontCollectionForFamily( - benchmark::State& state) { - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - while (state.KeepRunning()) { - font_collection.GetMinikinFontCollectionForFamily("Roboto"); - } -} -BENCHMARK(BM_FontCollectionGetMinikinFontCollectionForFamily); - -} // namespace txt diff --git a/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc index 5b84728347..3e189c8e9a 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc @@ -45,8 +45,7 @@ static void BM_ParagraphShortLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); @@ -86,8 +85,8 @@ static void BM_ParagraphLongLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); @@ -128,8 +127,8 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); @@ -152,8 +151,7 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); for (int i = 0; i < 1000; ++i) { builder.PushStyle(text_style); builder.AddText(u16_text); @@ -177,9 +175,8 @@ static void BM_ParagraphTextBigO(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); @@ -206,8 +203,8 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); for (int i = 0; i < state.range(0); ++i) { builder.PushStyle(text_style); @@ -235,8 +232,7 @@ static void BM_ParagraphPaintSimple(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); auto paragraph = builder.Build(); @@ -282,8 +278,7 @@ static void BM_ParagraphPaintLarge(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); auto paragraph = builder.Build(); @@ -316,8 +311,7 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) { text_style.decoration_style = TextDecorationStyle(kSolid); text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); builder.PushStyle(text_style); builder.AddText(u16_text); @@ -367,9 +361,8 @@ static void BM_ParagraphMinikinDoLayout(benchmark::State& state) { paint.letterSpacing = text_style.letter_spacing; paint.wordSpacing = text_style.word_spacing; - auto collection = - FontCollection::GetFontCollection(txt::GetFontDir()) - .GetMinikinFontCollectionForFamily(text_style.font_family); + auto collection = GetTestFontCollection()->GetMinikinFontCollectionForFamily( + text_style.font_family); while (state.KeepRunning()) { minikin::Layout layout; @@ -398,7 +391,7 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) { paint.letterSpacing = text_style.letter_spacing; paint.wordSpacing = text_style.word_spacing; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); minikin::LineBreaker breaker; breaker.setLocale(icu::Locale(), nullptr); @@ -409,7 +402,7 @@ static void BM_ParagraphMinikinAddStyleRun(benchmark::State& state) { while (state.KeepRunning()) { for (int i = 0; i < 20; ++i) { breaker.addStyleRun( - &paint, font_collection.GetMinikinFontCollectionForFamily("Roboto"), + &paint, font_collection->GetMinikinFontCollectionForFamily("Roboto"), font, state.range(0) / 20 * i, state.range(0) / 20 * (i + 1), false, 0); } diff --git a/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc index 94b7f39cf8..2fa4882cb9 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc @@ -31,8 +31,9 @@ namespace txt { static void BM_ParagraphBuilderConstruction(benchmark::State& state) { txt::ParagraphStyle paragraph_style; + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style); + txt::ParagraphBuilder builder(paragraph_style, font_collection); } } BENCHMARK(BM_ParagraphBuilderConstruction); @@ -42,9 +43,9 @@ static void BM_ParagraphBuilderPushStyle(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.PushStyle(text_style); } } @@ -52,7 +53,7 @@ BENCHMARK(BM_ParagraphBuilderPushStyle); static void BM_ParagraphBuilderPushPop(benchmark::State& state) { txt::ParagraphStyle paragraph_style; - txt::ParagraphBuilder builder(paragraph_style); + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); txt::TextStyle text_style; text_style.color = SK_ColorBLACK; @@ -66,10 +67,12 @@ BENCHMARK(BM_ParagraphBuilderPushPop); static void BM_ParagraphBuilderAddTextString(benchmark::State& state) { std::string text = "Hello World"; + auto font_collection = GetTestFontCollection(); + txt::ParagraphStyle paragraph_style; while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.AddText(text); } } @@ -79,9 +82,9 @@ static void BM_ParagraphBuilderAddTextChar(benchmark::State& state) { const char* text = "Hello World"; txt::ParagraphStyle paragraph_style; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.AddText(text); } } @@ -94,9 +97,9 @@ static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State& state) { icu_text.getBuffer() + icu_text.length()); txt::ParagraphStyle paragraph_style; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.AddText(u16_text); } } @@ -125,10 +128,12 @@ static void BM_ParagraphBuilderAddTextU16stringLong(benchmark::State& state) { std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); + auto font_collection = GetTestFontCollection(); + txt::ParagraphStyle paragraph_style; while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.AddText(u16_text); } } @@ -145,9 +150,9 @@ static void BM_ParagraphBuilderShortParagraphConstruct( txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.PushStyle(text_style); builder.AddText(u16_text); builder.Pop(); @@ -183,9 +188,9 @@ static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State& state) { txt::TextStyle text_style; text_style.color = SK_ColorBLACK; - auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + auto font_collection = GetTestFontCollection(); while (state.KeepRunning()) { - txt::ParagraphBuilder builder(paragraph_style, &font_collection); + txt::ParagraphBuilder builder(paragraph_style, font_collection); builder.PushStyle(text_style); builder.AddText(u16_text); builder.Pop(); diff --git a/engine/src/flutter/third_party/txt/benchmarks/utils.cc b/engine/src/flutter/third_party/txt/benchmarks/utils.cc index ff7fe1ec25..48bed676ee 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/utils.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/utils.cc @@ -17,6 +17,8 @@ #include #include "lib/ftl/command_line.h" +#include "txt/directory_asset_data_provider.h" +#include "txt/font_collection.h" #include "utils.h" namespace txt { @@ -24,7 +26,7 @@ namespace txt { static std::string gFontDir; static ftl::CommandLine gCommandLine; -const std::string GetFontDir() { +const std::string& GetFontDir() { return gFontDir; } @@ -40,4 +42,11 @@ void SetCommandLine(ftl::CommandLine cmd) { gCommandLine = std::move(cmd); } +std::shared_ptr GetTestFontCollection() { + auto collection = std::make_shared(); + collection->PushBack(sk_make_sp( + std::make_unique(GetFontDir()))); + return collection; +} + } // namespace txt diff --git a/engine/src/flutter/third_party/txt/benchmarks/utils.h b/engine/src/flutter/third_party/txt/benchmarks/utils.h index 5b6db12718..b164078c20 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/utils.h +++ b/engine/src/flutter/third_party/txt/benchmarks/utils.h @@ -20,7 +20,9 @@ namespace txt { -const std::string GetFontDir(); +class FontCollection; + +const std::string& GetFontDir(); void SetFontDir(const std::string& dir); @@ -28,4 +30,6 @@ const ftl::CommandLine& GetCommandLineForProcess(); void SetCommandLine(ftl::CommandLine cmd); +std::shared_ptr GetTestFontCollection(); + } // namespace txt