[go: nahoru, domu]

Skip to content

Commit

Permalink
Delay default font manager to run concurrently with isolate setup (fl…
Browse files Browse the repository at this point in the history
…utter#29291)

* Delay default font manager to run concurrently with isolate setup

* test
  • Loading branch information
dnfield authored and kylinchen committed Oct 22, 2021
1 parent f2fbbb4 commit a08ea2f
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 11 deletions.
6 changes: 6 additions & 0 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ std::weak_ptr<DartIsolate> DartIsolate::SpawnIsolate(
GetIsolateGroupData().GetIsolateSnapshot(), //
std::move(platform_configuration), //
flags, //
nullptr, //
isolate_create_callback, //
isolate_shutdown_callback, //
dart_entrypoint, //
Expand All @@ -118,6 +119,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
fml::RefPtr<const DartSnapshot> isolate_snapshot,
std::unique_ptr<PlatformConfiguration> platform_configuration,
Flags isolate_flags,
fml::closure root_isolate_create_callback,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
Expand Down Expand Up @@ -184,6 +186,10 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
settings.root_isolate_create_callback(*isolate.get());
}

if (root_isolate_create_callback) {
root_isolate_create_callback();
}

if (!isolate->RunFromLibrary(dart_entrypoint_library, //
dart_entrypoint, //
settings.dart_entrypoint_args //
Expand Down
10 changes: 9 additions & 1 deletion runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ class DartIsolate : public UIDartState {
/// @param[in] isolate_configuration The isolate configuration used to
/// configure the isolate before
/// invoking the entrypoint.
/// @param[in] root_isolate_create_callback A callback called after the root
/// isolate is created, _without_
/// isolate scope. This gives the
/// caller a chance to finish any
/// setup before running the Dart
/// program, and after any embedder
/// callbacks in the settings object.
/// @param[in] isolate_create_callback The isolate create callback. This
/// will be called when the before the
/// main Dart entrypoint is invoked in
Expand All @@ -186,7 +193,7 @@ class DartIsolate : public UIDartState {
/// isolate is still running at this
/// point and an isolate scope is
/// current.
/// @param[in] context Engine-owned state which is
/// @param[in] context Engine-owned state which is
/// accessed by the root dart isolate.
/// @param[in] spawning_isolate The isolate that is spawning the
/// new isolate. See also
Expand All @@ -202,6 +209,7 @@ class DartIsolate : public UIDartState {
fml::RefPtr<const DartSnapshot> isolate_snapshot,
std::unique_ptr<PlatformConfiguration> platform_configuration,
Flags flags,
fml::closure root_isolate_create_callback,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
Expand Down
5 changes: 5 additions & 0 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down Expand Up @@ -97,6 +98,7 @@ TEST_F(DartIsolateTest, SpawnIsolate) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down Expand Up @@ -170,6 +172,7 @@ TEST_F(DartIsolateTest, IsolateShutdownCallbackIsInIsolateScope) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down Expand Up @@ -392,6 +395,7 @@ TEST_F(DartIsolateTest, CanCreateServiceIsolate) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down Expand Up @@ -490,6 +494,7 @@ TEST_F(DartIsolateTest, InvalidLoadingUnitFails) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down
1 change: 1 addition & 0 deletions runtime/dart_lifecycle_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate(
vm.GetIsolateSnapshot(), // isolate_snapshot
{}, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root isolate create callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback,
entrypoint, // dart entrypoint
Expand Down
2 changes: 2 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ tonic::DartErrorHandleType RuntimeController::GetLastError() {

bool RuntimeController::LaunchRootIsolate(
const Settings& settings,
fml::closure root_isolate_create_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
std::unique_ptr<IsolateConfiguration> isolate_configuration) {
Expand All @@ -369,6 +370,7 @@ bool RuntimeController::LaunchRootIsolate(
isolate_snapshot_, //
std::make_unique<PlatformConfiguration>(this), //
DartIsolate::Flags{}, //
root_isolate_create_callback, //
isolate_create_callback_, //
isolate_shutdown_callback_, //
dart_entrypoint, //
Expand Down
7 changes: 7 additions & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class RuntimeController : public PlatformConfigurationClient {
/// Launch an isolate in that runtime controller instead.
///
/// @param[in] settings The per engine instance settings.
/// @param[in] root_isolate_create_callback A callback invoked before the
/// root isolate has launched the Dart
/// program, but after it has been
/// created. This is called without
/// isolate scope, and after any root
/// isolate callback in the settings.
/// @param[in] dart_entrypoint The dart entrypoint. If
/// `std::nullopt` or empty, `main` will
/// be attempted.
Expand All @@ -133,6 +139,7 @@ class RuntimeController : public PlatformConfigurationClient {
///
[[nodiscard]] bool LaunchRootIsolate(
const Settings& settings,
fml::closure root_isolate_create_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
std::unique_ptr<IsolateConfiguration> isolate_configuration);
Expand Down
17 changes: 10 additions & 7 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,22 @@ 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;
}

// 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.
auto root_isolate_create_callback = [&]() {
if (settings_.prefetched_default_font_manager) {
SetupDefaultFontManager();
}
};

if (!runtime_controller_->LaunchRootIsolate(
settings_, //
root_isolate_create_callback, //
configuration.GetEntrypoint(), //
configuration.GetEntrypointLibrary(), //
configuration.TakeIsolateConfiguration()) //
Expand Down
14 changes: 11 additions & 3 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3093,8 +3093,7 @@ TEST_F(ShellTest, UserTagSetOnStartup) {
TEST_F(ShellTest, PrefetchDefaultFontManager) {
auto settings = CreateSettingsForFixture();
settings.prefetched_default_font_manager = true;

auto shell = CreateShell(std::move(settings));
std::unique_ptr<Shell> shell;

auto get_font_manager_count = [&] {
fml::AutoResetWaitableEvent latch;
Expand All @@ -3109,8 +3108,17 @@ TEST_F(ShellTest, PrefetchDefaultFontManager) {
latch.Wait();
return font_manager_count;
};
size_t initial_font_manager_count = 0;
settings.root_isolate_create_callback = [&](const auto& isolate) {
ASSERT_GT(initial_font_manager_count, 0ul);
// Should not have fetched the default font manager yet, since the root
// isolate was only just created.
ASSERT_EQ(get_font_manager_count(), initial_font_manager_count);
};

shell = CreateShell(std::move(settings));

size_t initial_font_manager_count = get_font_manager_count();
initial_font_manager_count = get_font_manager_count();

auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("emptyMain");
Expand Down
1 change: 1 addition & 0 deletions testing/dart_isolate_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolateOnUITaskRunner(
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root isolate create callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
entrypoint, // entrypoint
Expand Down
1 change: 1 addition & 0 deletions third_party/tonic/tests/dart_state_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TEST_F(DartState, IsShuttingDown) {
vm_data->GetIsolateSnapshot(), // isolate snapshot
nullptr, // platform configuration
DartIsolate::Flags{}, // flags
nullptr, // root_isolate_create_callback
settings.isolate_create_callback, // isolate create callback
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
Expand Down

0 comments on commit a08ea2f

Please sign in to comment.