diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 729cbf88bf..3126402859 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -95,6 +95,7 @@ std::weak_ptr DartIsolate::SpawnIsolate( GetIsolateGroupData().GetIsolateSnapshot(), // std::move(platform_configuration), // flags, // + nullptr, // isolate_create_callback, // isolate_shutdown_callback, // dart_entrypoint, // @@ -118,6 +119,7 @@ std::weak_ptr DartIsolate::CreateRunningRootIsolate( fml::RefPtr isolate_snapshot, std::unique_ptr 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 dart_entrypoint, @@ -184,6 +186,10 @@ std::weak_ptr 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 // diff --git a/engine/src/flutter/runtime/dart_isolate.h b/engine/src/flutter/runtime/dart_isolate.h index 47f2ea88c2..39a98dd9ed 100644 --- a/engine/src/flutter/runtime/dart_isolate.h +++ b/engine/src/flutter/runtime/dart_isolate.h @@ -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 @@ -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 @@ -202,6 +209,7 @@ class DartIsolate : public UIDartState { fml::RefPtr isolate_snapshot, std::unique_ptr platform_configuration, Flags flags, + fml::closure root_isolate_create_callback, const fml::closure& isolate_create_callback, const fml::closure& isolate_shutdown_callback, std::optional dart_entrypoint, diff --git a/engine/src/flutter/runtime/dart_isolate_unittests.cc b/engine/src/flutter/runtime/dart_isolate_unittests.cc index bd66b1d195..5656bf327e 100644 --- a/engine/src/flutter/runtime/dart_isolate_unittests.cc +++ b/engine/src/flutter/runtime/dart_isolate_unittests.cc @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/engine/src/flutter/runtime/dart_lifecycle_unittests.cc b/engine/src/flutter/runtime/dart_lifecycle_unittests.cc index b2beed0dd9..0992b729d8 100644 --- a/engine/src/flutter/runtime/dart_lifecycle_unittests.cc +++ b/engine/src/flutter/runtime/dart_lifecycle_unittests.cc @@ -64,6 +64,7 @@ static std::shared_ptr 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 diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index 35ce7e3e80..8d2e3f878b 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -355,6 +355,7 @@ tonic::DartErrorHandleType RuntimeController::GetLastError() { bool RuntimeController::LaunchRootIsolate( const Settings& settings, + fml::closure root_isolate_create_callback, std::optional dart_entrypoint, std::optional dart_entrypoint_library, std::unique_ptr isolate_configuration) { @@ -369,6 +370,7 @@ bool RuntimeController::LaunchRootIsolate( isolate_snapshot_, // std::make_unique(this), // DartIsolate::Flags{}, // + root_isolate_create_callback, // isolate_create_callback_, // isolate_shutdown_callback_, // dart_entrypoint, // diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index e1fa9946e2..37e5eadcfe 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -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. @@ -133,6 +139,7 @@ class RuntimeController : public PlatformConfigurationClient { /// [[nodiscard]] bool LaunchRootIsolate( const Settings& settings, + fml::closure root_isolate_create_callback, std::optional dart_entrypoint, std::optional dart_entrypoint_library, std::unique_ptr isolate_configuration); diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 4085674356..a3d1862fbf 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -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()) // diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 2b0ca11e21..b76cc6560e 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -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; auto get_font_manager_count = [&] { fml::AutoResetWaitableEvent latch; @@ -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); + }; - size_t initial_font_manager_count = get_font_manager_count(); + shell = CreateShell(std::move(settings)); + + initial_font_manager_count = get_font_manager_count(); auto configuration = RunConfiguration::InferFromSettings(settings); configuration.SetEntrypoint("emptyMain"); diff --git a/engine/src/flutter/testing/dart_isolate_runner.cc b/engine/src/flutter/testing/dart_isolate_runner.cc index cfe0aa1a9c..6d9fb28e77 100644 --- a/engine/src/flutter/testing/dart_isolate_runner.cc +++ b/engine/src/flutter/testing/dart_isolate_runner.cc @@ -130,6 +130,7 @@ std::unique_ptr 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 diff --git a/engine/src/flutter/third_party/tonic/tests/dart_state_unittest.cc b/engine/src/flutter/third_party/tonic/tests/dart_state_unittest.cc index dd0c15f26f..b5ee4d8e2f 100644 --- a/engine/src/flutter/third_party/tonic/tests/dart_state_unittest.cc +++ b/engine/src/flutter/third_party/tonic/tests/dart_state_unittest.cc @@ -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