Defer setup of the default font manager if the embedding prefetched the font manager (flutter/engine#28987)

This commit is contained in:
Jason Simmons
2021-10-04 13:23:02 -07:00
committed by GitHub
parent afd577c733
commit 79c7cfdc91
7 changed files with 61 additions and 6 deletions

View File

@@ -181,6 +181,10 @@ struct Settings {
// Font settings
bool use_test_fonts = false;
// Indicates whether the embedding started a prefetch of the default font
// manager before creating the engine.
bool prefetched_default_font_manager = false;
// Selects the SkParagraph implementation of the text layout engine.
bool enable_skparagraph = false;

View File

@@ -202,6 +202,13 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) {
UpdateAssetManager(configuration.GetAssetManager());
// If the embedding prefetched the default font manager, then set up the
// font manager later in the engine launch process. This makes it less
// likely that the setup will need to wait for the prefetch to complete.
if (settings_.prefetched_default_font_manager) {
SetupDefaultFontManager();
}
if (runtime_controller_->IsRootIsolateRunning()) {
return RunStatus::FailureAlreadyRunning;
}

View File

@@ -642,12 +642,14 @@ bool Shell::Setup(std::unique_ptr<PlatformView> platform_view,
weak_platform_view_ = platform_view_->GetWeakPtr();
// Setup the time-consuming default font manager right after engine created.
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(),
[engine = weak_engine_] {
if (engine) {
engine->SetupDefaultFontManager();
}
});
if (!settings_.prefetched_default_font_manager) {
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(),
[engine = weak_engine_] {
if (engine) {
engine->SetupDefaultFontManager();
}
});
}
is_setup_ = true;

View File

@@ -3044,5 +3044,38 @@ TEST_F(ShellTest, UserTagSetOnStartup) {
isolate_create_latch.Wait();
}
TEST_F(ShellTest, PrefetchDefaultFontManager) {
auto settings = CreateSettingsForFixture();
settings.prefetched_default_font_manager = true;
auto shell = CreateShell(std::move(settings));
auto get_font_manager_count = [&] {
fml::AutoResetWaitableEvent latch;
size_t font_manager_count;
fml::TaskRunner::RunNowOrPostTask(
shell->GetTaskRunners().GetUITaskRunner(),
[this, &shell, &latch, &font_manager_count]() {
font_manager_count =
GetFontCollection(shell.get())->GetFontManagersCount();
latch.Signal();
});
latch.Wait();
return font_manager_count;
};
size_t initial_font_manager_count = get_font_manager_count();
auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("emptyMain");
RunEngine(shell.get(), std::move(configuration));
// If the prefetched_default_font_manager flag is set, then the default font
// manager will not be added until the engine starts running.
ASSERT_EQ(get_font_manager_count(), initial_font_manager_count + 1);
DestroyShell(std::move(shell));
}
} // namespace testing
} // namespace flutter

View File

@@ -399,6 +399,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
settings.enable_skparagraph =
command_line.HasOption(FlagForSwitch(Switch::EnableSkParagraph));
settings.prefetched_default_font_manager = command_line.HasOption(
FlagForSwitch(Switch::PrefetchedDefaultFontManager));
std::string all_dart_flags;
if (command_line.GetOptionValue(FlagForSwitch(Switch::DartFlags),
&all_dart_flags)) {

View File

@@ -169,6 +169,10 @@ DEF_SWITCH(UseTestFonts,
"will make font resolution default to the Ahem test font on all "
"platforms (See https://www.w3.org/Style/CSS/Test/Fonts/Ahem/). "
"This option is only available on the desktop test shells.")
DEF_SWITCH(PrefetchedDefaultFontManager,
"prefetched-default-font-manager",
"Indicates whether the embedding started a prefetch of the "
"default font manager before creating the engine.")
DEF_SWITCH(VerboseLogging,
"verbose-logging",
"By default, only errors are logged. This flag enabled logging at "

View File

@@ -288,6 +288,8 @@ public class FlutterLoader {
shellArgs.add("--old-gen-heap-size=" + oldGenHeapSizeMegaBytes);
shellArgs.add("--prefetched-default-font-manager");
if (metaData != null && metaData.getBoolean(ENABLE_SKPARAGRAPH_META_DATA_KEY)) {
shellArgs.add("--enable-skparagraph");
}