Skip to content

Commit e108b01

Browse files
gaaclarkejochen.holzer@web.de
authored andcommitted
1 parent 40441de commit e108b01

6 files changed

Lines changed: 169 additions & 9 deletions

File tree

shell/common/shell.cc

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ std::unique_ptr<Engine> CreateEngine(
6868
}
6969
} // namespace
7070

71+
<<<<<<< HEAD
72+
=======
73+
std::unique_ptr<Shell> Shell::Create(
74+
const PlatformData& platform_data,
75+
TaskRunners task_runners,
76+
Settings settings,
77+
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
78+
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
79+
bool is_gpu_disabled) {
80+
// This must come first as it initializes tracing.
81+
PerformInitializationTasks(settings);
82+
83+
TRACE_EVENT0("flutter", "Shell::Create");
84+
85+
// Always use the `vm_snapshot` and `isolate_snapshot` provided by the
86+
// settings to launch the VM. If the VM is already running, the snapshot
87+
// arguments are ignored.
88+
auto vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
89+
auto isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
90+
auto vm = DartVMRef::Create(settings, vm_snapshot, isolate_snapshot);
91+
FML_CHECK(vm) << "Must be able to initialize the VM.";
92+
93+
// If the settings did not specify an `isolate_snapshot`, fall back to the
94+
// one the VM was launched with.
95+
if (!isolate_snapshot) {
96+
isolate_snapshot = vm->GetVMData()->GetIsolateSnapshot();
97+
}
98+
return CreateWithSnapshot(std::move(platform_data), //
99+
std::move(task_runners), //
100+
std::move(settings), //
101+
std::move(vm), //
102+
std::move(isolate_snapshot), //
103+
std::move(on_create_platform_view), //
104+
std::move(on_create_rasterizer), //
105+
CreateEngine, is_gpu_disabled);
106+
}
107+
108+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
71109
std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
72110
DartVMRef vm,
73111
TaskRunners task_runners,
@@ -76,7 +114,8 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
76114
fml::RefPtr<const DartSnapshot> isolate_snapshot,
77115
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
78116
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
79-
const Shell::EngineCreateCallback& on_create_engine) {
117+
const Shell::EngineCreateCallback& on_create_engine,
118+
bool is_gpu_disabled) {
80119
if (!task_runners.IsValid()) {
81120
FML_LOG(ERROR) << "Task runners to run the shell were invalid.";
82121
return nullptr;
@@ -86,7 +125,8 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
86125
new Shell(std::move(vm), task_runners, settings,
87126
std::make_shared<VolatilePathTracker>(
88127
task_runners.GetUITaskRunner(),
89-
!settings.skia_deterministic_rendering_on_cpu)));
128+
!settings.skia_deterministic_rendering_on_cpu),
129+
is_gpu_disabled));
90130

91131
// Create the rasterizer on the raster thread.
92132
std::promise<std::unique_ptr<Rasterizer>> rasterizer_promise;
@@ -315,8 +355,14 @@ std::unique_ptr<Shell> Shell::Create(
315355
fml::RefPtr<const DartSnapshot> isolate_snapshot,
316356
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
317357
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
358+
<<<<<<< HEAD
318359
DartVMRef vm,
319360
const Shell::EngineCreateCallback& on_create_engine) {
361+
=======
362+
const Shell::EngineCreateCallback& on_create_engine,
363+
bool is_gpu_disabled) {
364+
// This must come first as it initializes tracing.
365+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
320366
PerformInitializationTasks(settings);
321367
PersistentCache::SetCacheSkSL(settings.cache_sksl);
322368

@@ -331,6 +377,7 @@ std::unique_ptr<Shell> Shell::Create(
331377
std::unique_ptr<Shell> shell;
332378
fml::TaskRunner::RunNowOrPostTask(
333379
task_runners.GetPlatformTaskRunner(),
380+
<<<<<<< HEAD
334381
fml::MakeCopyable([&latch, //
335382
vm = std::move(vm), //
336383
&shell, //
@@ -351,18 +398,44 @@ std::unique_ptr<Shell> Shell::Create(
351398
on_create_engine);
352399
latch.Signal();
353400
}));
401+
=======
402+
fml::MakeCopyable(
403+
[&latch, //
404+
&shell, //
405+
task_runners = std::move(task_runners), //
406+
platform_data = std::move(platform_data), //
407+
settings = std::move(settings), //
408+
vm = std::move(vm), //
409+
isolate_snapshot = std::move(isolate_snapshot), //
410+
on_create_platform_view = std::move(on_create_platform_view), //
411+
on_create_rasterizer = std::move(on_create_rasterizer), //
412+
on_create_engine = std::move(on_create_engine),
413+
is_gpu_disabled]() mutable {
414+
shell = CreateShellOnPlatformThread(
415+
std::move(vm), //
416+
std::move(task_runners), //
417+
std::move(platform_data), //
418+
std::move(settings), //
419+
std::move(isolate_snapshot), //
420+
std::move(on_create_platform_view), //
421+
std::move(on_create_rasterizer), //
422+
std::move(on_create_engine), is_gpu_disabled);
423+
latch.Signal();
424+
}));
425+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
354426
latch.Wait();
355427
return shell;
356428
}
357429

358430
Shell::Shell(DartVMRef vm,
359431
TaskRunners task_runners,
360432
Settings settings,
361-
std::shared_ptr<VolatilePathTracker> volatile_path_tracker)
433+
std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
434+
bool is_gpu_disabled)
362435
: task_runners_(std::move(task_runners)),
363436
settings_(std::move(settings)),
364437
vm_(std::move(vm)),
365-
is_gpu_disabled_sync_switch_(new fml::SyncSwitch()),
438+
is_gpu_disabled_sync_switch_(new fml::SyncSwitch(is_gpu_disabled)),
366439
volatile_path_tracker_(std::move(volatile_path_tracker)),
367440
weak_factory_gpu_(nullptr),
368441
weak_factory_(this) {
@@ -479,6 +552,7 @@ std::unique_ptr<Shell> Shell::Spawn(
479552
const CreateCallback<PlatformView>& on_create_platform_view,
480553
const CreateCallback<Rasterizer>& on_create_rasterizer) const {
481554
FML_DCHECK(task_runners_.IsValid());
555+
<<<<<<< HEAD
482556
std::unique_ptr<Shell> result(Shell::Create(
483557
task_runners_, PlatformData{}, GetSettings(),
484558
vm_->GetVMData()->GetIsolateSnapshot(), on_create_platform_view,
@@ -498,6 +572,36 @@ std::unique_ptr<Shell> Shell::Spawn(
498572
/*settings=*/settings,
499573
/*animator=*/std::move(animator));
500574
}));
575+
=======
576+
auto shell_maker = [&](bool is_gpu_disabled) {
577+
std::unique_ptr<Shell> result(CreateWithSnapshot(
578+
PlatformData{}, task_runners_, GetSettings(), vm_,
579+
vm_->GetVMData()->GetIsolateSnapshot(), on_create_platform_view,
580+
on_create_rasterizer,
581+
[engine = this->engine_.get()](
582+
Engine::Delegate& delegate,
583+
const PointerDataDispatcherMaker& dispatcher_maker, DartVM& vm,
584+
fml::RefPtr<const DartSnapshot> isolate_snapshot,
585+
TaskRunners task_runners, const PlatformData& platform_data,
586+
Settings settings, std::unique_ptr<Animator> animator,
587+
fml::WeakPtr<IOManager> io_manager,
588+
fml::RefPtr<SkiaUnrefQueue> unref_queue,
589+
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
590+
std::shared_ptr<VolatilePathTracker> volatile_path_tracker) {
591+
return engine->Spawn(/*delegate=*/delegate,
592+
/*dispatcher_maker=*/dispatcher_maker,
593+
/*settings=*/settings,
594+
/*animator=*/std::move(animator));
595+
},
596+
is_gpu_disabled));
597+
return result;
598+
};
599+
std::unique_ptr<Shell> result;
600+
GetIsGpuDisabledSyncSwitch()->Execute(
601+
fml::SyncSwitch::Handlers()
602+
.SetIfFalse([&] { result = shell_maker(false); })
603+
.SetIfTrue([&] { result = shell_maker(true); }));
604+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
501605
result->shared_resource_context_ = io_manager_->GetSharedResourceContext();
502606
result->RunEngine(std::move(run_configuration));
503607

shell/common/shell.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class Shell final : public PlatformView::Delegate,
130130
/// valid rasterizer. This will be called
131131
/// on the render task runner before this
132132
/// method returns.
133+
/// @param[in] is_gpu_disabled The default value for the switch that
134+
/// turns off the GPU.
133135
///
134136
/// @return A full initialized shell if the settings and callbacks are
135137
/// valid. The root isolate has been created but not yet launched.
@@ -143,7 +145,8 @@ class Shell final : public PlatformView::Delegate,
143145
TaskRunners task_runners,
144146
Settings settings,
145147
const CreateCallback<PlatformView>& on_create_platform_view,
146-
const CreateCallback<Rasterizer>& on_create_rasterizer);
148+
const CreateCallback<Rasterizer>& on_create_rasterizer,
149+
bool is_gpu_disabled = false);
147150

148151
//----------------------------------------------------------------------------
149152
/// @brief Creates a shell instance using the provided settings. The
@@ -467,7 +470,8 @@ class Shell final : public PlatformView::Delegate,
467470
Shell(DartVMRef vm,
468471
TaskRunners task_runners,
469472
Settings settings,
470-
std::shared_ptr<VolatilePathTracker> volatile_path_tracker);
473+
std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
474+
bool is_gpu_disabled);
471475

472476
static std::unique_ptr<Shell> CreateShellOnPlatformThread(
473477
DartVMRef vm,
@@ -477,7 +481,22 @@ class Shell final : public PlatformView::Delegate,
477481
fml::RefPtr<const DartSnapshot> isolate_snapshot,
478482
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
479483
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
484+
<<<<<<< HEAD
480485
const EngineCreateCallback& on_create_engine);
486+
=======
487+
const EngineCreateCallback& on_create_engine,
488+
bool is_gpu_disabled);
489+
static std::unique_ptr<Shell> CreateWithSnapshot(
490+
const PlatformData& platform_data,
491+
TaskRunners task_runners,
492+
Settings settings,
493+
DartVMRef vm,
494+
fml::RefPtr<const DartSnapshot> isolate_snapshot,
495+
const CreateCallback<PlatformView>& on_create_platform_view,
496+
const CreateCallback<Rasterizer>& on_create_rasterizer,
497+
const EngineCreateCallback& on_create_engine,
498+
bool is_gpu_disabled);
499+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
481500

482501
bool Setup(std::unique_ptr<PlatformView> platform_view,
483502
std::unique_ptr<Engine> engine,

shell/common/shell_test.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ std::unique_ptr<Shell> ShellTest::CreateShell(
314314
TaskRunners task_runners,
315315
bool simulate_vsync,
316316
std::shared_ptr<ShellTestExternalViewEmbedder>
317-
shell_test_external_view_embedder) {
317+
shell_test_external_view_embedder,
318+
bool is_gpu_disabled) {
318319
const auto vsync_clock = std::make_shared<ShellTestVsyncClock>();
319320
CreateVsyncWaiter create_vsync_waiter = [&]() {
320321
if (simulate_vsync) {
@@ -335,7 +336,8 @@ std::unique_ptr<Shell> ShellTest::CreateShell(
335336
ShellTestPlatformView::BackendType::kDefaultBackend,
336337
shell_test_external_view_embedder);
337338
},
338-
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
339+
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); },
340+
is_gpu_disabled);
339341
}
340342
void ShellTest::DestroyShell(std::unique_ptr<Shell> shell) {
341343
DestroyShell(std::move(shell), GetTaskRunnersForFixture());

shell/common/shell_test.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class ShellTest : public FixtureTest {
3939
TaskRunners task_runners,
4040
bool simulate_vsync = false,
4141
std::shared_ptr<ShellTestExternalViewEmbedder>
42-
shell_test_external_view_embedder = nullptr);
42+
shell_test_external_view_embedder = nullptr,
43+
bool is_gpu_disabled = false);
4344
void DestroyShell(std::unique_ptr<Shell> shell);
4445
void DestroyShell(std::unique_ptr<Shell> shell, TaskRunners task_runners);
4546
TaskRunners GetTaskRunnersForFixture();

shell/common/shell_unittests.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,29 @@ TEST_F(ShellTest,
317317
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
318318
}
319319

320+
TEST_F(ShellTest, InitializeWithDisabledGpu) {
321+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
322+
Settings settings = CreateSettingsForFixture();
323+
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
324+
ThreadHost::Type::Platform);
325+
auto task_runner = thread_host.platform_thread->GetTaskRunner();
326+
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
327+
task_runner);
328+
auto shell = CreateShell(
329+
std::move(settings), std::move(task_runners), /*simulate_vsync=*/false,
330+
/*shell_test_external_view_embedder=*/nullptr, /*is_gpu_disabled=*/true);
331+
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
332+
ASSERT_TRUE(ValidateShell(shell.get()));
333+
334+
bool is_disabled = false;
335+
shell->GetIsGpuDisabledSyncSwitch()->Execute(
336+
fml::SyncSwitch::Handlers().SetIfTrue([&] { is_disabled = true; }));
337+
ASSERT_TRUE(is_disabled);
338+
339+
DestroyShell(std::move(shell), std::move(task_runners));
340+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
341+
}
342+
320343
TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) {
321344
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
322345
Settings settings = CreateSettingsForFixture();

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,24 @@ - (BOOL)createShell:(NSString*)entrypoint
612612
);
613613

614614
// Create the shell. This is a blocking operation.
615+
<<<<<<< HEAD
615616
std::unique_ptr<flutter::Shell> shell =
616617
flutter::Shell::Create(std::move(task_runners), // task runners
617618
std::move(platformData), // window data
618619
std::move(settings), // settings
619620
on_create_platform_view, // platform view creation
620621
on_create_rasterizer // rasterzier creation
621622
);
623+
=======
624+
std::unique_ptr<flutter::Shell> shell = flutter::Shell::Create(
625+
std::move(platformData), // window data
626+
std::move(task_runners), // task runners
627+
std::move(settings), // settings
628+
on_create_platform_view, // platform view creation
629+
on_create_rasterizer, // rasterzier creation
630+
/*is_gpu_disabled=*/[UIApplication sharedApplication].applicationState !=
631+
UIApplicationStateActive);
632+
>>>>>>> 7a672981c... Started initializing the gpu disable syncswitch based on the app state.
622633

623634
if (shell == nullptr) {
624635
FML_LOG(ERROR) << "Could not start a shell FlutterEngine with entrypoint: "

0 commit comments

Comments
 (0)