From f779ac9b5b9dae9a673febb9aff1f44f8cda1673 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Mon, 30 Oct 2023 20:49:05 +0000 Subject: [PATCH 1/3] Add helper function to txt to produce default font --- display_list/BUILD.gn | 1 + .../benchmarking/dl_complexity_unittests.cc | 3 +- display_list/testing/BUILD.gn | 1 + display_list/testing/dl_test_snippets.h | 4 +- third_party/txt/BUILD.gn | 1 + third_party/txt/src/txt/platform.cc | 4 ++ third_party/txt/src/txt/platform.h | 7 ++++ third_party/txt/src/txt/platform_common.cc | 40 +++++++++++++++++++ 8 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 third_party/txt/src/txt/platform_common.cc diff --git a/display_list/BUILD.gn b/display_list/BUILD.gn index 1ec13109d0d83..9864c6132e3dc 100644 --- a/display_list/BUILD.gn +++ b/display_list/BUILD.gn @@ -127,6 +127,7 @@ if (enable_unittests) { ":display_list_fixtures", "//flutter/display_list/testing:display_list_testing", "//flutter/testing", + "//flutter/third_party/txt:txt", ] if (!defined(defines)) { diff --git a/display_list/benchmarking/dl_complexity_unittests.cc b/display_list/benchmarking/dl_complexity_unittests.cc index df4fc68497336..63c24d36da5cc 100644 --- a/display_list/benchmarking/dl_complexity_unittests.cc +++ b/display_list/benchmarking/dl_complexity_unittests.cc @@ -14,6 +14,7 @@ #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkImage.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -306,7 +307,7 @@ TEST(DisplayListComplexity, DrawVertices) { TEST(DisplayListComplexity, DrawTextBlob) { auto text_blob = SkTextBlob::MakeFromString( - "The quick brown fox jumps over the lazy dog.", SkFont()); + "The quick brown fox jumps over the lazy dog.", txt::DefaultFont()); DisplayListBuilder builder; builder.DrawTextBlob(text_blob, 0.0f, 0.0f, DlPaint()); diff --git a/display_list/testing/BUILD.gn b/display_list/testing/BUILD.gn index 1839219b1e8e3..11924f964b820 100644 --- a/display_list/testing/BUILD.gn +++ b/display_list/testing/BUILD.gn @@ -17,6 +17,7 @@ source_set("display_list_testing") { deps = [ "//flutter/testing:testing_lib", "//third_party/skia", + "//flutter/third_party/txt:txt", ] public_deps = [ "//flutter/display_list:display_list" ] diff --git a/display_list/testing/dl_test_snippets.h b/display_list/testing/dl_test_snippets.h index 64f54362cae7d..363c36545564c 100644 --- a/display_list/testing/dl_test_snippets.h +++ b/display_list/testing/dl_test_snippets.h @@ -13,6 +13,7 @@ #include "third_party/skia/include/effects/SkDashPathEffect.h" #include "third_party/skia/include/effects/SkGradientShader.h" #include "third_party/skia/include/effects/SkImageFilters.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -223,7 +224,8 @@ static sk_sp TestDisplayList2 = MakeTestDisplayList(25, 25, SK_ColorBLUE); static sk_sp MakeTextBlob(std::string string) { - return SkTextBlob::MakeFromText(string.c_str(), string.size(), SkFont(), + return SkTextBlob::MakeFromText(string.c_str(), string.size(), + txt::DefaultFont(), SkTextEncoding::kUTF8); } static sk_sp TestBlob1 = MakeTextBlob("TestBlob1"); diff --git a/third_party/txt/BUILD.gn b/third_party/txt/BUILD.gn index da4d0615d7b9a..d53c17509bc02 100644 --- a/third_party/txt/BUILD.gn +++ b/third_party/txt/BUILD.gn @@ -61,6 +61,7 @@ source_set("txt") { "src/txt/paragraph_style.h", "src/txt/placeholder_run.cc", "src/txt/placeholder_run.h", + "src/txt/platform_common.cc", "src/txt/platform.h", "src/txt/run_metrics.h", "src/txt/test_font_manager.cc", diff --git a/third_party/txt/src/txt/platform.cc b/third_party/txt/src/txt/platform.cc index f1860efd7e9cf..bc75184dbe0ab 100644 --- a/third_party/txt/src/txt/platform.cc +++ b/third_party/txt/src/txt/platform.cc @@ -4,6 +4,8 @@ #include "txt/platform.h" +#include "fml/logging.h" + namespace txt { std::vector GetDefaultFontFamilies() { @@ -11,6 +13,8 @@ std::vector GetDefaultFontFamilies() { } sk_sp GetDefaultFontManager(uint32_t font_initialization_data) { + // TODO(b/305780908) Replace this with a singleton that depends on which + // platform we are on and which SkFontMgr was compiled in. return SkFontMgr::RefDefault(); } diff --git a/third_party/txt/src/txt/platform.h b/third_party/txt/src/txt/platform.h index 7c29e1900121d..f086738eeec77 100644 --- a/third_party/txt/src/txt/platform.h +++ b/third_party/txt/src/txt/platform.h @@ -10,13 +10,20 @@ #include "flutter/fml/macros.h" #include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/core/SkRefCnt.h" +#include "third_party/skia/include/core/SkFont.h" namespace txt { std::vector GetDefaultFontFamilies(); +// Returns the platform specific SkFontMgr, which is a singleton. sk_sp GetDefaultFontManager(uint32_t font_initialization_data); +// Returns a font using a default typeface returned by a platform-specific +// SkFontMgr. It may be different on different platforms. +SkFont DefaultFont(); + } // namespace txt #endif // TXT_PLATFORM_H_ diff --git a/third_party/txt/src/txt/platform_common.cc b/third_party/txt/src/txt/platform_common.cc new file mode 100644 index 0000000000000..b25ba4925c63c --- /dev/null +++ b/third_party/txt/src/txt/platform_common.cc @@ -0,0 +1,40 @@ +// 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 "txt/platform.h" + +#include "fml/logging.h" +#include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/core/SkRefCnt.h" +#include "third_party/skia/include/core/SkTypeface.h" +#include "third_party/skia/include/core/SkFont.h" +#include "third_party/skia/include/core/SkFontStyle.h" + +namespace txt { + +static sk_sp MakeTypefaceFromName(const char* name, SkFontStyle style) { + sk_sp fm = GetDefaultFontManager(0); + FML_CHECK(fm); + sk_sp face = fm->legacyMakeTypeface(name, style); + return face; +} + +static sk_sp DefaultTypeface() { + sk_sp face = MakeTypefaceFromName(nullptr, SkFontStyle()); + if (face) { + return face; + } + // Due to how SkTypeface::MakeDefault() used to work, many callers of this + // depend on the returned SkTypeface being non-null. + // TODO(kjlubick) replace this with SkTypeface::MakeEmpty() + face = SkTypeface::MakeDefault(); + FML_CHECK(face); + return face; +} + +SkFont DefaultFont() { + return SkFont(DefaultTypeface()); +} + +} // namespace txt From b969613483be1d76be0f73eb61b843f188ca5f55 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Mon, 30 Oct 2023 20:50:30 +0000 Subject: [PATCH 2/3] format --- display_list/testing/BUILD.gn | 2 +- display_list/testing/dl_test_snippets.h | 3 +-- third_party/txt/BUILD.gn | 2 +- third_party/txt/src/txt/platform.h | 2 +- third_party/txt/src/txt/platform_common.cc | 7 ++++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/display_list/testing/BUILD.gn b/display_list/testing/BUILD.gn index 11924f964b820..aae80f7c3c7f3 100644 --- a/display_list/testing/BUILD.gn +++ b/display_list/testing/BUILD.gn @@ -16,8 +16,8 @@ source_set("display_list_testing") { deps = [ "//flutter/testing:testing_lib", - "//third_party/skia", "//flutter/third_party/txt:txt", + "//third_party/skia", ] public_deps = [ "//flutter/display_list:display_list" ] diff --git a/display_list/testing/dl_test_snippets.h b/display_list/testing/dl_test_snippets.h index 363c36545564c..a85881f18eb00 100644 --- a/display_list/testing/dl_test_snippets.h +++ b/display_list/testing/dl_test_snippets.h @@ -225,8 +225,7 @@ static sk_sp TestDisplayList2 = static sk_sp MakeTextBlob(std::string string) { return SkTextBlob::MakeFromText(string.c_str(), string.size(), - txt::DefaultFont(), - SkTextEncoding::kUTF8); + txt::DefaultFont(), SkTextEncoding::kUTF8); } static sk_sp TestBlob1 = MakeTextBlob("TestBlob1"); static sk_sp TestBlob2 = MakeTextBlob("TestBlob2"); diff --git a/third_party/txt/BUILD.gn b/third_party/txt/BUILD.gn index d53c17509bc02..66aff09556071 100644 --- a/third_party/txt/BUILD.gn +++ b/third_party/txt/BUILD.gn @@ -61,8 +61,8 @@ source_set("txt") { "src/txt/paragraph_style.h", "src/txt/placeholder_run.cc", "src/txt/placeholder_run.h", - "src/txt/platform_common.cc", "src/txt/platform.h", + "src/txt/platform_common.cc", "src/txt/run_metrics.h", "src/txt/test_font_manager.cc", "src/txt/test_font_manager.h", diff --git a/third_party/txt/src/txt/platform.h b/third_party/txt/src/txt/platform.h index f086738eeec77..55a9812e4a8bb 100644 --- a/third_party/txt/src/txt/platform.h +++ b/third_party/txt/src/txt/platform.h @@ -9,9 +9,9 @@ #include #include "flutter/fml/macros.h" +#include "third_party/skia/include/core/SkFont.h" #include "third_party/skia/include/core/SkFontMgr.h" #include "third_party/skia/include/core/SkRefCnt.h" -#include "third_party/skia/include/core/SkFont.h" namespace txt { diff --git a/third_party/txt/src/txt/platform_common.cc b/third_party/txt/src/txt/platform_common.cc index b25ba4925c63c..8aa39d4bd64d9 100644 --- a/third_party/txt/src/txt/platform_common.cc +++ b/third_party/txt/src/txt/platform_common.cc @@ -5,15 +5,16 @@ #include "txt/platform.h" #include "fml/logging.h" +#include "third_party/skia/include/core/SkFont.h" #include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/core/SkFontStyle.h" #include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/skia/include/core/SkTypeface.h" -#include "third_party/skia/include/core/SkFont.h" -#include "third_party/skia/include/core/SkFontStyle.h" namespace txt { -static sk_sp MakeTypefaceFromName(const char* name, SkFontStyle style) { +static sk_sp MakeTypefaceFromName(const char* name, + SkFontStyle style) { sk_sp fm = GetDefaultFontManager(0); FML_CHECK(fm); sk_sp face = fm->legacyMakeTypeface(name, style); From 45a878290ff9d35722103e4225babce7f7c45186 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Mon, 30 Oct 2023 20:51:37 +0000 Subject: [PATCH 3/3] one more --- display_list/BUILD.gn | 1 + display_list/benchmarking/dl_benchmarks.cc | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/display_list/BUILD.gn b/display_list/BUILD.gn index 9864c6132e3dc..c1905573cdb28 100644 --- a/display_list/BUILD.gn +++ b/display_list/BUILD.gn @@ -223,6 +223,7 @@ source_set("display_list_benchmarks_source") { "//flutter/fml", "//flutter/testing:skia", "//flutter/testing:testing_lib", + "//flutter/third_party/txt:txt", "//third_party/dart/runtime:libdart_jit", # for tracing "//third_party/skia", ] diff --git a/display_list/benchmarking/dl_benchmarks.cc b/display_list/benchmarking/dl_benchmarks.cc index 2a03b60c7a8e9..7e85ddd09db03 100644 --- a/display_list/benchmarking/dl_benchmarks.cc +++ b/display_list/benchmarking/dl_benchmarks.cc @@ -15,6 +15,7 @@ #include "third_party/skia/include/gpu/GrDirectContext.h" #include "third_party/skia/include/gpu/GrRecordingContext.h" #include "third_party/skia/include/gpu/GrTypes.h" +#include "third_party/txt/src/txt/platform.h" namespace flutter { namespace testing { @@ -1203,7 +1204,7 @@ void BM_DrawTextBlob(benchmark::State& state, for (size_t i = 0; i < draw_calls; i++) { character[0] = 'A' + (i % 26); - auto blob = SkTextBlob::MakeFromString(character, SkFont()); + auto blob = SkTextBlob::MakeFromString(character, txt::DefaultFont()); builder.DrawTextBlob(blob, 50.0f, 50.0f, paint); }