Skip to content

Commit b71a2fd

Browse files
Defer setup of the default font manager if the embedding prefetched the font manager (flutter#28987)
1 parent f7fb3b8 commit b71a2fd

7 files changed

Lines changed: 61 additions & 6 deletions

File tree

common/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ struct Settings {
181181
// Font settings
182182
bool use_test_fonts = false;
183183

184+
// Indicates whether the embedding started a prefetch of the default font
185+
// manager before creating the engine.
186+
bool prefetched_default_font_manager = false;
187+
184188
// Selects the SkParagraph implementation of the text layout engine.
185189
bool enable_skparagraph = false;
186190

shell/common/engine.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) {
202202

203203
UpdateAssetManager(configuration.GetAssetManager());
204204

205+
// If the embedding prefetched the default font manager, then set up the
206+
// font manager later in the engine launch process. This makes it less
207+
// likely that the setup will need to wait for the prefetch to complete.
208+
if (settings_.prefetched_default_font_manager) {
209+
SetupDefaultFontManager();
210+
}
211+
205212
if (runtime_controller_->IsRootIsolateRunning()) {
206213
return RunStatus::FailureAlreadyRunning;
207214
}

shell/common/shell.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,12 +642,14 @@ bool Shell::Setup(std::unique_ptr<PlatformView> platform_view,
642642
weak_platform_view_ = platform_view_->GetWeakPtr();
643643

644644
// Setup the time-consuming default font manager right after engine created.
645-
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(),
646-
[engine = weak_engine_] {
647-
if (engine) {
648-
engine->SetupDefaultFontManager();
649-
}
650-
});
645+
if (!settings_.prefetched_default_font_manager) {
646+
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(),
647+
[engine = weak_engine_] {
648+
if (engine) {
649+
engine->SetupDefaultFontManager();
650+
}
651+
});
652+
}
651653

652654
is_setup_ = true;
653655

shell/common/shell_unittests.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,5 +3044,38 @@ TEST_F(ShellTest, UserTagSetOnStartup) {
30443044
isolate_create_latch.Wait();
30453045
}
30463046

3047+
TEST_F(ShellTest, PrefetchDefaultFontManager) {
3048+
auto settings = CreateSettingsForFixture();
3049+
settings.prefetched_default_font_manager = true;
3050+
3051+
auto shell = CreateShell(std::move(settings));
3052+
3053+
auto get_font_manager_count = [&] {
3054+
fml::AutoResetWaitableEvent latch;
3055+
size_t font_manager_count;
3056+
fml::TaskRunner::RunNowOrPostTask(
3057+
shell->GetTaskRunners().GetUITaskRunner(),
3058+
[this, &shell, &latch, &font_manager_count]() {
3059+
font_manager_count =
3060+
GetFontCollection(shell.get())->GetFontManagersCount();
3061+
latch.Signal();
3062+
});
3063+
latch.Wait();
3064+
return font_manager_count;
3065+
};
3066+
3067+
size_t initial_font_manager_count = get_font_manager_count();
3068+
3069+
auto configuration = RunConfiguration::InferFromSettings(settings);
3070+
configuration.SetEntrypoint("emptyMain");
3071+
RunEngine(shell.get(), std::move(configuration));
3072+
3073+
// If the prefetched_default_font_manager flag is set, then the default font
3074+
// manager will not be added until the engine starts running.
3075+
ASSERT_EQ(get_font_manager_count(), initial_font_manager_count + 1);
3076+
3077+
DestroyShell(std::move(shell));
3078+
}
3079+
30473080
} // namespace testing
30483081
} // namespace flutter

shell/common/switches.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
399399
settings.enable_skparagraph =
400400
command_line.HasOption(FlagForSwitch(Switch::EnableSkParagraph));
401401

402+
settings.prefetched_default_font_manager = command_line.HasOption(
403+
FlagForSwitch(Switch::PrefetchedDefaultFontManager));
404+
402405
std::string all_dart_flags;
403406
if (command_line.GetOptionValue(FlagForSwitch(Switch::DartFlags),
404407
&all_dart_flags)) {

shell/common/switches.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ DEF_SWITCH(UseTestFonts,
169169
"will make font resolution default to the Ahem test font on all "
170170
"platforms (See https://www.w3.org/Style/CSS/Test/Fonts/Ahem/). "
171171
"This option is only available on the desktop test shells.")
172+
DEF_SWITCH(PrefetchedDefaultFontManager,
173+
"prefetched-default-font-manager",
174+
"Indicates whether the embedding started a prefetch of the "
175+
"default font manager before creating the engine.")
172176
DEF_SWITCH(VerboseLogging,
173177
"verbose-logging",
174178
"By default, only errors are logged. This flag enabled logging at "

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ public void ensureInitializationComplete(
288288

289289
shellArgs.add("--old-gen-heap-size=" + oldGenHeapSizeMegaBytes);
290290

291+
shellArgs.add("--prefetched-default-font-manager");
292+
291293
if (metaData != null && metaData.getBoolean(ENABLE_SKPARAGRAPH_META_DATA_KEY)) {
292294
shellArgs.add("--enable-skparagraph");
293295
}

0 commit comments

Comments
 (0)