Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2999d0c

Browse files
committed
Setup default font manager after engine created, to improve startup performance.
1 parent cb139f7 commit 2999d0c

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

lib/ui/text/font_collection.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void _LoadFontFromList(Dart_NativeArguments args) {
4747

4848
FontCollection::FontCollection()
4949
: collection_(std::make_shared<txt::FontCollection>()) {
50-
collection_->SetupDefaultFontManager();
51-
5250
dynamic_font_manager_ = sk_make_sp<txt::DynamicFontManager>();
5351
collection_->SetDynamicFontManager(dynamic_font_manager_);
5452
}
@@ -68,6 +66,10 @@ std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
6866
return collection_;
6967
}
7068

69+
void FontCollection::SetupDefaultFontManager() {
70+
collection_->SetupDefaultFontManager();
71+
}
72+
7173
void FontCollection::RegisterFonts(
7274
std::shared_ptr<AssetManager> asset_manager) {
7375
std::unique_ptr<fml::Mapping> manifest_mapping =

lib/ui/text/font_collection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class FontCollection {
2929

3030
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
3131

32+
void SetupDefaultFontManager();
33+
3234
void RegisterFonts(std::shared_ptr<AssetManager> asset_manager);
3335

3436
void RegisterTestFonts();

shell/common/engine.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ fml::WeakPtr<Engine> Engine::GetWeakPtr() const {
9090
return weak_factory_.GetWeakPtr();
9191
}
9292

93+
void Engine::SetupDefaultFontManager() {
94+
TRACE_EVENT0("flutter", "Engine::SetupDefaultFontManager");
95+
font_collection_.SetupDefaultFontManager();
96+
}
97+
9398
bool Engine::UpdateAssetManager(
9499
std::shared_ptr<AssetManager> new_asset_manager) {
95100
if (asset_manager_ == new_asset_manager) {

shell/common/engine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
363363
///
364364
[[nodiscard]] bool Restart(RunConfiguration configuration);
365365

366+
//----------------------------------------------------------------------------
367+
/// @brief Setup default font manager according to specific platform.
368+
///
369+
/// @attention This operation calls `SkFontMgr::RefDefault` which is
370+
/// time-consuming except running at linux and windows.
371+
void SetupDefaultFontManager();
372+
366373
//----------------------------------------------------------------------------
367374
/// @brief Updates the asset manager referenced by the root isolate of a
368375
/// Flutter application. This happens implicitly in the call to

shell/common/shell.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,14 @@ bool Shell::Setup(std::unique_ptr<PlatformView> platform_view,
542542
weak_rasterizer_ = rasterizer_->GetWeakPtr();
543543
weak_platform_view_ = platform_view_->GetWeakPtr();
544544

545+
// Setup the time-consuming default font manager right after engine created.
546+
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(),
547+
[engine = weak_engine_] {
548+
if (engine) {
549+
engine->SetupDefaultFontManager();
550+
}
551+
});
552+
545553
is_setup_ = true;
546554

547555
vm_->GetServiceProtocol()->AddHandler(this, GetServiceProtocolDescription());

0 commit comments

Comments
 (0)