From efd666334479dcb8393c1b78bd5e9faddadd1dbc Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 13 Jun 2018 14:28:21 -0700 Subject: [PATCH] Maintain a FontCollection for each engine instance instead of a process-wide singleton (#5521) --- lib/ui/BUILD.gn | 3 ++- lib/ui/text/font_collection.cc | 7 ------- lib/ui/text/font_collection.h | 8 +++----- lib/ui/text/paragraph_builder.cc | 12 +++++++----- lib/ui/window/window.h | 2 ++ runtime/runtime_controller.cc | 4 ++++ runtime/runtime_controller.h | 4 ++++ runtime/runtime_delegate.h | 3 +++ shell/common/BUILD.gn | 2 +- shell/common/engine.cc | 8 ++++++-- shell/common/engine.h | 5 +++++ 11 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/ui/BUILD.gn b/lib/ui/BUILD.gn index 027159fb897a9..b90a8514a8dcc 100644 --- a/lib/ui/BUILD.gn +++ b/lib/ui/BUILD.gn @@ -96,7 +96,6 @@ source_set("ui") { "$flutter_root/fml", "$flutter_root/glue", "$flutter_root/runtime:test_font", - "$flutter_root/third_party/txt", "//third_party/dart/runtime/bin:embedded_dart_io", "//third_party/rapidjson", "//third_party/skia", @@ -107,4 +106,6 @@ source_set("ui") { if (is_fuchsia) { deps += [ "//topaz/public/dart-pkg/zircon" ] } + + public_deps = ["$flutter_root/third_party/txt"] } diff --git a/lib/ui/text/font_collection.cc b/lib/ui/text/font_collection.cc index 248468cc9701d..d45a2d92c33ae 100644 --- a/lib/ui/text/font_collection.cc +++ b/lib/ui/text/font_collection.cc @@ -16,13 +16,6 @@ namespace blink { -FontCollection& FontCollection::ForProcess() { - static std::once_flag once = {}; - static FontCollection* gCollection = nullptr; - std::call_once(once, []() { gCollection = new FontCollection(); }); - return *gCollection; -} - FontCollection::FontCollection() : collection_(std::make_shared()) { collection_->SetDefaultFontManager(SkFontMgr::RefDefault()); diff --git a/lib/ui/text/font_collection.h b/lib/ui/text/font_collection.h index ea9c2f46a96a0..62c94ffd8deb4 100644 --- a/lib/ui/text/font_collection.h +++ b/lib/ui/text/font_collection.h @@ -17,7 +17,9 @@ namespace blink { class FontCollection { public: - static FontCollection& ForProcess(); + FontCollection(); + + ~FontCollection(); std::shared_ptr GetFontCollection() const; @@ -28,10 +30,6 @@ class FontCollection { private: std::shared_ptr collection_; - FontCollection(); - - ~FontCollection(); - FXL_DISALLOW_COPY_AND_ASSIGN(FontCollection); }; diff --git a/lib/ui/text/paragraph_builder.cc b/lib/ui/text/paragraph_builder.cc index c9b896294c59c..80cc817c1bd60 100644 --- a/lib/ui/text/paragraph_builder.cc +++ b/lib/ui/text/paragraph_builder.cc @@ -8,6 +8,7 @@ #include "flutter/common/task_runners.h" #include "flutter/lib/ui/text/font_collection.h" #include "flutter/lib/ui/ui_dart_state.h" +#include "flutter/lib/ui/window/window.h" #include "flutter/third_party/txt/src/txt/font_style.h" #include "flutter/third_party/txt/src/txt/font_weight.h" #include "flutter/third_party/txt/src/txt/paragraph_style.h" @@ -110,8 +111,8 @@ fxl::RefPtr ParagraphBuilder::create( double lineHeight, const std::u16string& ellipsis, const std::string& locale) { - return fxl::MakeRefCounted( - encoded, fontFamily, fontSize, lineHeight, ellipsis, locale); + return fxl::MakeRefCounted(encoded, fontFamily, fontSize, + lineHeight, ellipsis, locale); } ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded, @@ -155,8 +156,10 @@ ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded, style.locale = locale; } + FontCollection& font_collection = + UIDartState::Current()->window()->client()->GetFontCollection(); m_paragraphBuilder = std::make_unique( - style, blink::FontCollection::ForProcess().GetFontCollection()); + style, font_collection.GetFontCollection()); } // namespace blink ParagraphBuilder::~ParagraphBuilder() = default; @@ -207,8 +210,7 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded, static_cast(encoded[tsFontWeightIndex]); if (mask & tsFontStyleMask) - style.font_style = - static_cast(encoded[tsFontStyleIndex]); + style.font_style = static_cast(encoded[tsFontStyleIndex]); if (mask & tsFontFamilyMask) style.font_family = fontFamily; diff --git a/lib/ui/window/window.h b/lib/ui/window/window.h index 2feeccb65f9cc..892812a363a7d 100644 --- a/lib/ui/window/window.h +++ b/lib/ui/window/window.h @@ -20,6 +20,7 @@ class DartLibraryNatives; } // namespace tonic namespace blink { +class FontCollection; class Scene; Dart_Handle ToByteData(const std::vector& buffer); @@ -31,6 +32,7 @@ class WindowClient { virtual void Render(Scene* scene) = 0; virtual void UpdateSemantics(SemanticsUpdate* update) = 0; virtual void HandlePlatformMessage(fxl::RefPtr message) = 0; + virtual FontCollection& GetFontCollection() = 0; protected: virtual ~WindowClient(); diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index 18eaaebad68bc..c1acc813f320c 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -249,6 +249,10 @@ void RuntimeController::HandlePlatformMessage( client_.HandlePlatformMessage(std::move(message)); } +FontCollection& RuntimeController::GetFontCollection() { + return client_.GetFontCollection(); +} + Dart_Port RuntimeController::GetMainPort() { return root_isolate_ ? root_isolate_->main_port() : ILLEGAL_PORT; } diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index 599d9c73b274c..07ec70a912164 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -10,6 +10,7 @@ #include "flutter/common/task_runners.h" #include "flutter/flow/layers/layer_tree.h" #include "flutter/lib/ui/ui_dart_state.h" +#include "flutter/lib/ui/text/font_collection.h" #include "flutter/lib/ui/window/pointer_data_packet.h" #include "flutter/lib/ui/window/window.h" #include "flutter/runtime/dart_vm.h" @@ -124,6 +125,9 @@ class RuntimeController final : public WindowClient { // |blink::WindowClient| void HandlePlatformMessage(fxl::RefPtr message) override; + // |blink::WindowClient| + FontCollection& GetFontCollection() override; + FXL_DISALLOW_COPY_AND_ASSIGN(RuntimeController); }; diff --git a/runtime/runtime_delegate.h b/runtime/runtime_delegate.h index c6d6c0a92b2b4..75e89e62e44ef 100644 --- a/runtime/runtime_delegate.h +++ b/runtime/runtime_delegate.h @@ -10,6 +10,7 @@ #include "flutter/flow/layers/layer_tree.h" #include "flutter/lib/ui/semantics/semantics_node.h" +#include "flutter/lib/ui/text/font_collection.h" #include "flutter/lib/ui/window/platform_message.h" #include "third_party/dart/runtime/include/dart_api.h" @@ -27,6 +28,8 @@ class RuntimeDelegate { virtual void HandlePlatformMessage(fxl::RefPtr message) = 0; + virtual FontCollection& GetFontCollection() = 0; + protected: virtual ~RuntimeDelegate(); }; diff --git a/shell/common/BUILD.gn b/shell/common/BUILD.gn index 02e5f12b7d01e..28718a96062e3 100644 --- a/shell/common/BUILD.gn +++ b/shell/common/BUILD.gn @@ -100,7 +100,6 @@ source_set("common") { "$flutter_root/lib/ui", "$flutter_root/runtime", "$flutter_root/synchronization", - "$flutter_root/third_party/txt", "//garnet/public/lib/fxl", "//third_party/dart/runtime:dart_api", "//third_party/rapidjson", @@ -109,6 +108,7 @@ source_set("common") { ] public_deps = [ + "$flutter_root/third_party/txt", "//topaz/lib/tonic", ] diff --git a/shell/common/engine.cc b/shell/common/engine.cc index d2f6e57807200..8689789fa4bed 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -87,9 +87,9 @@ bool Engine::UpdateAssetManager( // Using libTXT as the text engine. if (settings_.use_test_fonts) { - blink::FontCollection::ForProcess().RegisterTestFonts(); + font_collection_.RegisterTestFonts(); } else { - blink::FontCollection::ForProcess().RegisterFonts(*asset_manager_.get()); + font_collection_.RegisterFonts(*asset_manager_.get()); } return true; @@ -377,6 +377,10 @@ void Engine::HandlePlatformMessage( } } +blink::FontCollection& Engine::GetFontCollection() { + return font_collection_; +} + void Engine::HandleAssetPlatformMessage( fxl::RefPtr message) { fxl::RefPtr response = message->response(); diff --git a/shell/common/engine.h b/shell/common/engine.h index 46602cdea9fd6..a6ac5b413f2f6 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -11,6 +11,7 @@ #include "flutter/assets/asset_manager.h" #include "flutter/common/task_runners.h" #include "flutter/lib/ui/semantics/semantics_node.h" +#include "flutter/lib/ui/text/font_collection.h" #include "flutter/lib/ui/window/platform_message.h" #include "flutter/lib/ui/window/viewport_metrics.h" #include "flutter/runtime/dart_vm.h" @@ -109,6 +110,7 @@ class Engine final : public blink::RuntimeDelegate { fml::RefPtr asset_manager_; bool activity_running_; bool have_surface_; + blink::FontCollection font_collection_; fml::WeakPtrFactory weak_factory_; // |blink::RuntimeDelegate| @@ -124,6 +126,9 @@ class Engine final : public blink::RuntimeDelegate { void HandlePlatformMessage( fxl::RefPtr message) override; + // |blink::RuntimeDelegate| + blink::FontCollection& GetFontCollection() override; + void StopAnimator(); void StartAnimatorIfPossible();