Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
75 changes: 35 additions & 40 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,58 +67,57 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
// first because it has state that the other subsystems depend on. It must
// first be booted and the necessary references obtained to initialize the
// other subsystems.
fml::AutoResetWaitableEvent io_latch;
std::unique_ptr<ShellIOManager> io_manager;
std::promise<std::unique_ptr<ShellIOManager>> io_manager_promise;
auto io_manager = io_manager_promise.get_future();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, please append _future to the ivars or locals that are now futures.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

std::promise<fml::WeakPtr<ShellIOManager>> weak_io_manager_promise;
auto weak_io_manager = weak_io_manager_promise.get_future();
auto io_task_runner = shell->GetTaskRunners().GetIOTaskRunner();
fml::TaskRunner::RunNowOrPostTask(
io_task_runner,
[&io_latch, //
&io_manager, //
&platform_view, //
io_task_runner //
[&io_manager_promise, //
&weak_io_manager_promise, //
&platform_view, //
io_task_runner //
]() {
TRACE_EVENT0("flutter", "ShellSetupIOSubsystem");
io_manager = std::make_unique<ShellIOManager>(
auto io_manager = std::make_unique<ShellIOManager>(
platform_view->CreateResourceContext(), io_task_runner);
io_latch.Signal();
weak_io_manager_promise.set_value(io_manager->GetWeakPtr());
io_manager_promise.set_value(std::move(io_manager));
});
io_latch.Wait();

// Create the rasterizer on the GPU thread.
fml::AutoResetWaitableEvent gpu_latch;
std::unique_ptr<Rasterizer> rasterizer;
std::promise<std::unique_ptr<Rasterizer>> rasterizer_promise;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this before the platform view creation call. You don't have to wait for the platform view to be created before this can be kicked off as there is no dependency. This can be kicked off eagerly so that platform view creation can be concurrent with rasterizer creation as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto rasterizer = rasterizer_promise.get_future();
fml::TaskRunner::RunNowOrPostTask(
task_runners.GetGPUTaskRunner(), [&gpu_latch, //
&rasterizer, //
task_runners.GetGPUTaskRunner(), [&rasterizer_promise, //
on_create_rasterizer, //
shell = shell.get() //
]() {
TRACE_EVENT0("flutter", "ShellSetupGPUSubsystem");
if (auto new_rasterizer = on_create_rasterizer(*shell)) {
rasterizer = std::move(new_rasterizer);
rasterizer_promise.set_value(std::move(new_rasterizer));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be condensed to one line now: rasterizer_promise.set_value(on_create_rasterizer(...))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

} else {
rasterizer_promise.set_value(std::unique_ptr<Rasterizer>());
}
gpu_latch.Signal();
});

gpu_latch.Wait();

// Send dispatcher_maker to the engine constructor because shell won't have
// platform_view set until Shell::Setup is called later.
auto dispatcher_maker = platform_view->GetDispatcherMaker();

// Create the engine on the UI thread.
fml::AutoResetWaitableEvent ui_latch;
std::unique_ptr<Engine> engine;
std::promise<std::unique_ptr<Engine>> engine_promise;
auto engine = engine_promise.get_future();
fml::TaskRunner::RunNowOrPostTask(
shell->GetTaskRunners().GetUITaskRunner(),
fml::MakeCopyable([&ui_latch, //
&engine, //
fml::MakeCopyable([&engine_promise, //
shell = shell.get(), //
&dispatcher_maker, //
isolate_snapshot = std::move(isolate_snapshot), //
shared_snapshot = std::move(shared_snapshot), //
vsync_waiter = std::move(vsync_waiter), //
io_manager = io_manager->GetWeakPtr() //
&weak_io_manager //
]() mutable {
TRACE_EVENT0("flutter", "ShellSetupUISubsystem");
const auto& task_runners = shell->GetTaskRunners();
Expand All @@ -128,27 +127,23 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
auto animator = std::make_unique<Animator>(*shell, task_runners,
std::move(vsync_waiter));

engine = std::make_unique<Engine>(*shell, //
dispatcher_maker, //
*shell->GetDartVM(), //
std::move(isolate_snapshot), //
std::move(shared_snapshot), //
task_runners, //
shell->GetSettings(), //
std::move(animator), //
std::move(io_manager) //
);
ui_latch.Signal();
engine_promise.set_value(std::make_unique<Engine>(
*shell, //
dispatcher_maker, //
*shell->GetDartVM(), //
std::move(isolate_snapshot), //
std::move(shared_snapshot), //
task_runners, //
shell->GetSettings(), //
std::move(animator), //
weak_io_manager.get() //
));
}));

ui_latch.Wait();
// We are already on the platform thread. So there is no platform latch to
// wait on.

if (!shell->Setup(std::move(platform_view), //
std::move(engine), //
std::move(rasterizer), //
std::move(io_manager)) //
engine.get(), //
rasterizer.get(), //
io_manager.get()) //
) {
return nullptr;
}
Expand Down