Skip to content

Commit

Permalink
FlutterEngineGroup for Android (flutter#23675)
Browse files Browse the repository at this point in the history
  • Loading branch information
xster authored Jan 19, 2021
1 parent f6162dc commit 915e2ca
Show file tree
Hide file tree
Showing 34 changed files with 1,238 additions and 311 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.*.sw?
.DS_Store
.ccls-cache
.cache
.classpath
.clangd/
.cproject
Expand Down
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/Trans
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineCache.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineGroup.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterOverlaySurface.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java
Expand Down
2 changes: 1 addition & 1 deletion runtime/isolate_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IsolateConfiguration {
/// using the legacy settings fields that specify
/// the asset by name instead of a mappings
/// callback.
/// @param[in] io_worker An optional IO worker. Specify `nullptr` is a
/// @param[in] io_worker An optional IO worker. Specify `nullptr` if a
/// worker should not be used or one is not
/// available.
///
Expand Down
8 changes: 3 additions & 5 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ Shell::~Shell() {
}

std::unique_ptr<Shell> Shell::Spawn(
Settings settings,
RunConfiguration run_configuration,
const CreateCallback<PlatformView>& on_create_platform_view,
const CreateCallback<Rasterizer>& on_create_rasterizer) const {
FML_DCHECK(task_runners_.IsValid());
std::unique_ptr<Shell> result(Shell::Create(
task_runners_, PlatformData{}, settings,
task_runners_, PlatformData{}, GetSettings(),
vm_->GetVMData()->GetIsolateSnapshot(), on_create_platform_view,
on_create_rasterizer, vm_,
[engine = this->engine_.get()](
Expand All @@ -498,10 +498,8 @@ std::unique_ptr<Shell> Shell::Spawn(
/*settings=*/settings,
/*animator=*/std::move(animator));
}));
RunConfiguration configuration =
RunConfiguration::InferFromSettings(settings);
result->shared_resource_context_ = io_manager_->GetSharedResourceContext();
result->RunEngine(std::move(configuration));
result->RunEngine(std::move(run_configuration));
return result;
}

Expand Down
12 changes: 11 additions & 1 deletion shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,19 @@ class Shell final : public PlatformView::Delegate,
/// and a smaller memory footprint than an Shell created with a
/// Create function.
///
/// The new Shell is returned in a running state so RunEngine
/// shouldn't be called again on the Shell. Once running, the
/// second Shell is mostly independent from the original Shell
/// and the original Shell doesn't need to keep running for the
/// spawned Shell to keep functioning.
/// @param[in] run_configuration A RunConfiguration used to run the Isolate
/// associated with this new Shell. It doesn't have to be the same
/// configuration as the current Shell but it needs to be in the
/// same snapshot or AOT.
///
/// @see http://flutter.dev/go/multiple-engines
std::unique_ptr<Shell> Spawn(
Settings settings,
RunConfiguration run_configuration,
const CreateCallback<PlatformView>& on_create_platform_view,
const CreateCallback<Rasterizer>& on_create_rasterizer) const;

Expand Down
74 changes: 48 additions & 26 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2438,39 +2438,61 @@ TEST_F(ShellTest, Spawn) {
ASSERT_TRUE(configuration.IsValid());
configuration.SetEntrypoint("fixturesAreFunctionalMain");

auto second_configuration = RunConfiguration::InferFromSettings(settings);
ASSERT_TRUE(second_configuration.IsValid());
second_configuration.SetEntrypoint("testCanLaunchSecondaryIsolate");

fml::AutoResetWaitableEvent main_latch;
std::string last_entry_point;
// Fulfill native function for the first Shell's entrypoint.
AddNativeCallback(
"SayHiFromFixturesAreFunctionalMain",
CREATE_NATIVE_ENTRY([&main_latch](auto args) { main_latch.Signal(); }));
"SayHiFromFixturesAreFunctionalMain", CREATE_NATIVE_ENTRY([&](auto args) {
last_entry_point = shell->GetEngine()->GetLastEntrypoint();
main_latch.Signal();
}));
// Fulfill native function for the second Shell's entrypoint.
AddNativeCallback(
// The Dart native function names aren't very consistent but this is just
// the native function name of the second vm entrypoint in the fixture.
"NotifyNative", CREATE_NATIVE_ENTRY([&](auto args) {}));

RunEngine(shell.get(), std::move(configuration));
main_latch.Wait();
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
// Check first Shell ran the first entrypoint.
ASSERT_EQ("fixturesAreFunctionalMain", last_entry_point);

PostSync(shell->GetTaskRunners().GetPlatformTaskRunner(), [this,
&spawner = shell,
settings]() {
MockPlatformViewDelegate platform_view_delegate;
auto spawn = spawner->Spawn(
settings,
[&platform_view_delegate](Shell& shell) {
auto result = std::make_unique<MockPlatformView>(
platform_view_delegate, shell.GetTaskRunners());
ON_CALL(*result, CreateRenderingSurface())
.WillByDefault(::testing::Invoke(
[] { return std::make_unique<MockSurface>(); }));
return result;
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
ASSERT_NE(nullptr, spawn.get());
ASSERT_TRUE(ValidateShell(spawn.get()));

PostSync(spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
spawn->GetIOManager()->GetResourceContext().get());
});
DestroyShell(std::move(spawn));
});
PostSync(
shell->GetTaskRunners().GetPlatformTaskRunner(),
[this, &spawner = shell, &second_configuration]() {
MockPlatformViewDelegate platform_view_delegate;
auto spawn = spawner->Spawn(
std::move(second_configuration),
[&platform_view_delegate](Shell& shell) {
auto result = std::make_unique<MockPlatformView>(
platform_view_delegate, shell.GetTaskRunners());
ON_CALL(*result, CreateRenderingSurface())
.WillByDefault(::testing::Invoke(
[] { return std::make_unique<MockSurface>(); }));
return result;
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
ASSERT_NE(nullptr, spawn.get());
ASSERT_TRUE(ValidateShell(spawn.get()));

PostSync(spawner->GetTaskRunners().GetUITaskRunner(), [&spawn] {
// Check second shell ran the second entrypoint.
ASSERT_EQ("testCanLaunchSecondaryIsolate",
spawn->GetEngine()->GetLastEntrypoint());
});

PostSync(
spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
spawn->GetIOManager()->GetResourceContext().get());
});
DestroyShell(std::move(spawn));
});

DestroyShell(std::move(shell));
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ android_java_sources = [
"io/flutter/embedding/engine/FlutterEngine.java",
"io/flutter/embedding/engine/FlutterEngineCache.java",
"io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java",
"io/flutter/embedding/engine/FlutterEngineGroup.java",
"io/flutter/embedding/engine/FlutterJNI.java",
"io/flutter/embedding/engine/FlutterOverlaySurface.java",
"io/flutter/embedding/engine/FlutterShellArgs.java",
Expand Down Expand Up @@ -465,6 +466,7 @@ action("robolectric_tests") {
"test/io/flutter/embedding/android/RobolectricFlutterActivity.java",
"test/io/flutter/embedding/engine/FlutterEngineCacheTest.java",
"test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java",
"test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java",
"test/io/flutter/embedding/engine/FlutterEngineTest.java",
"test/io/flutter/embedding/engine/FlutterJNITest.java",
"test/io/flutter/embedding/engine/FlutterShellArgsTest.java",
Expand Down
Loading

0 comments on commit 915e2ca

Please sign in to comment.