Skip to content

Commit 7dd40b0

Browse files
xstergspencergoog
authored andcommitted
FlutterEngineGroup for Android (flutter#23675)
1 parent ff45a2c commit 7dd40b0

34 files changed

+1238
-311
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.*.sw?
55
.DS_Store
66
.ccls-cache
7+
.cache
78
.classpath
89
.clangd/
910
.cproject

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/Trans
748748
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java
749749
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineCache.java
750750
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java
751+
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineGroup.java
751752
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java
752753
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterOverlaySurface.java
753754
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java

runtime/isolate_configuration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class IsolateConfiguration {
5454
/// using the legacy settings fields that specify
5555
/// the asset by name instead of a mappings
5656
/// callback.
57-
/// @param[in] io_worker An optional IO worker. Specify `nullptr` is a
57+
/// @param[in] io_worker An optional IO worker. Specify `nullptr` if a
5858
/// worker should not be used or one is not
5959
/// available.
6060
///

shell/common/shell.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ Shell::~Shell() {
475475
}
476476

477477
std::unique_ptr<Shell> Shell::Spawn(
478-
Settings settings,
478+
RunConfiguration run_configuration,
479479
const CreateCallback<PlatformView>& on_create_platform_view,
480480
const CreateCallback<Rasterizer>& on_create_rasterizer) const {
481481
FML_DCHECK(task_runners_.IsValid());
482482
std::unique_ptr<Shell> result(Shell::Create(
483-
task_runners_, PlatformData{}, settings,
483+
task_runners_, PlatformData{}, GetSettings(),
484484
vm_->GetVMData()->GetIsolateSnapshot(), on_create_platform_view,
485485
on_create_rasterizer, vm_,
486486
[engine = this->engine_.get()](
@@ -498,10 +498,8 @@ std::unique_ptr<Shell> Shell::Spawn(
498498
/*settings=*/settings,
499499
/*animator=*/std::move(animator));
500500
}));
501-
RunConfiguration configuration =
502-
RunConfiguration::InferFromSettings(settings);
503501
result->shared_resource_context_ = io_manager_->GetSharedResourceContext();
504-
result->RunEngine(std::move(configuration));
502+
result->RunEngine(std::move(run_configuration));
505503
return result;
506504
}
507505

shell/common/shell.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,19 @@ class Shell final : public PlatformView::Delegate,
219219
/// and a smaller memory footprint than an Shell created with a
220220
/// Create function.
221221
///
222+
/// The new Shell is returned in a running state so RunEngine
223+
/// shouldn't be called again on the Shell. Once running, the
224+
/// second Shell is mostly independent from the original Shell
225+
/// and the original Shell doesn't need to keep running for the
226+
/// spawned Shell to keep functioning.
227+
/// @param[in] run_configuration A RunConfiguration used to run the Isolate
228+
/// associated with this new Shell. It doesn't have to be the same
229+
/// configuration as the current Shell but it needs to be in the
230+
/// same snapshot or AOT.
231+
///
222232
/// @see http://flutter.dev/go/multiple-engines
223233
std::unique_ptr<Shell> Spawn(
224-
Settings settings,
234+
RunConfiguration run_configuration,
225235
const CreateCallback<PlatformView>& on_create_platform_view,
226236
const CreateCallback<Rasterizer>& on_create_rasterizer) const;
227237

shell/common/shell_unittests.cc

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,39 +2438,61 @@ TEST_F(ShellTest, Spawn) {
24382438
ASSERT_TRUE(configuration.IsValid());
24392439
configuration.SetEntrypoint("fixturesAreFunctionalMain");
24402440

2441+
auto second_configuration = RunConfiguration::InferFromSettings(settings);
2442+
ASSERT_TRUE(second_configuration.IsValid());
2443+
second_configuration.SetEntrypoint("testCanLaunchSecondaryIsolate");
2444+
24412445
fml::AutoResetWaitableEvent main_latch;
2446+
std::string last_entry_point;
2447+
// Fulfill native function for the first Shell's entrypoint.
24422448
AddNativeCallback(
2443-
"SayHiFromFixturesAreFunctionalMain",
2444-
CREATE_NATIVE_ENTRY([&main_latch](auto args) { main_latch.Signal(); }));
2449+
"SayHiFromFixturesAreFunctionalMain", CREATE_NATIVE_ENTRY([&](auto args) {
2450+
last_entry_point = shell->GetEngine()->GetLastEntrypoint();
2451+
main_latch.Signal();
2452+
}));
2453+
// Fulfill native function for the second Shell's entrypoint.
2454+
AddNativeCallback(
2455+
// The Dart native function names aren't very consistent but this is just
2456+
// the native function name of the second vm entrypoint in the fixture.
2457+
"NotifyNative", CREATE_NATIVE_ENTRY([&](auto args) {}));
24452458

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

2450-
PostSync(shell->GetTaskRunners().GetPlatformTaskRunner(), [this,
2451-
&spawner = shell,
2452-
settings]() {
2453-
MockPlatformViewDelegate platform_view_delegate;
2454-
auto spawn = spawner->Spawn(
2455-
settings,
2456-
[&platform_view_delegate](Shell& shell) {
2457-
auto result = std::make_unique<MockPlatformView>(
2458-
platform_view_delegate, shell.GetTaskRunners());
2459-
ON_CALL(*result, CreateRenderingSurface())
2460-
.WillByDefault(::testing::Invoke(
2461-
[] { return std::make_unique<MockSurface>(); }));
2462-
return result;
2463-
},
2464-
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2465-
ASSERT_NE(nullptr, spawn.get());
2466-
ASSERT_TRUE(ValidateShell(spawn.get()));
2467-
2468-
PostSync(spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
2469-
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
2470-
spawn->GetIOManager()->GetResourceContext().get());
2471-
});
2472-
DestroyShell(std::move(spawn));
2473-
});
2465+
PostSync(
2466+
shell->GetTaskRunners().GetPlatformTaskRunner(),
2467+
[this, &spawner = shell, &second_configuration]() {
2468+
MockPlatformViewDelegate platform_view_delegate;
2469+
auto spawn = spawner->Spawn(
2470+
std::move(second_configuration),
2471+
[&platform_view_delegate](Shell& shell) {
2472+
auto result = std::make_unique<MockPlatformView>(
2473+
platform_view_delegate, shell.GetTaskRunners());
2474+
ON_CALL(*result, CreateRenderingSurface())
2475+
.WillByDefault(::testing::Invoke(
2476+
[] { return std::make_unique<MockSurface>(); }));
2477+
return result;
2478+
},
2479+
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
2480+
ASSERT_NE(nullptr, spawn.get());
2481+
ASSERT_TRUE(ValidateShell(spawn.get()));
2482+
2483+
PostSync(spawner->GetTaskRunners().GetUITaskRunner(), [&spawn] {
2484+
// Check second shell ran the second entrypoint.
2485+
ASSERT_EQ("testCanLaunchSecondaryIsolate",
2486+
spawn->GetEngine()->GetLastEntrypoint());
2487+
});
2488+
2489+
PostSync(
2490+
spawner->GetTaskRunners().GetIOTaskRunner(), [&spawner, &spawn] {
2491+
ASSERT_EQ(spawner->GetIOManager()->GetResourceContext().get(),
2492+
spawn->GetIOManager()->GetResourceContext().get());
2493+
});
2494+
DestroyShell(std::move(spawn));
2495+
});
24742496

24752497
DestroyShell(std::move(shell));
24762498
ASSERT_FALSE(DartVMRef::IsInstanceRunning());

shell/platform/android/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ android_java_sources = [
151151
"io/flutter/embedding/engine/FlutterEngine.java",
152152
"io/flutter/embedding/engine/FlutterEngineCache.java",
153153
"io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java",
154+
"io/flutter/embedding/engine/FlutterEngineGroup.java",
154155
"io/flutter/embedding/engine/FlutterJNI.java",
155156
"io/flutter/embedding/engine/FlutterOverlaySurface.java",
156157
"io/flutter/embedding/engine/FlutterShellArgs.java",
@@ -465,6 +466,7 @@ action("robolectric_tests") {
465466
"test/io/flutter/embedding/android/RobolectricFlutterActivity.java",
466467
"test/io/flutter/embedding/engine/FlutterEngineCacheTest.java",
467468
"test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java",
469+
"test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java",
468470
"test/io/flutter/embedding/engine/FlutterEngineTest.java",
469471
"test/io/flutter/embedding/engine/FlutterJNITest.java",
470472
"test/io/flutter/embedding/engine/FlutterShellArgsTest.java",

0 commit comments

Comments
 (0)