Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

boost shell startup #8489

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
583 changes: 328 additions & 255 deletions shell/common/shell.cc

Large diffs are not rendered by default.

52 changes: 24 additions & 28 deletions shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,36 @@ class Shell final : public PlatformView::Delegate,
public Animator::Delegate,
public Engine::Delegate,
public Rasterizer::Delegate,
public ServiceProtocol::Handler {
public ServiceProtocol::Handler,
public std::enable_shared_from_this<flutter::Shell> {
public:
template <class T>
using CreateCallback = std::function<std::unique_ptr<T>(Shell&)>;

// Create a shell with the given task runners and settings. The isolate
// snapshot will be shared with the snapshot of the service isolate.
static std::unique_ptr<Shell> Create(
static std::shared_ptr<Shell> Create(
TaskRunners task_runners,
Settings settings,
CreateCallback<PlatformView> on_create_platform_view,
CreateCallback<Rasterizer> on_create_rasterizer);

// Creates a shell with the given task runners and settings. The isolate
// snapshot is specified upfront.
static std::unique_ptr<Shell> Create(
TaskRunners task_runners,
Settings settings,
fml::RefPtr<const DartSnapshot> isolate_snapshot,
fml::RefPtr<const DartSnapshot> shared_snapshot,
CreateCallback<PlatformView> on_create_platform_view,
CreateCallback<Rasterizer> on_create_rasterizer,
DartVMRef vm);

~Shell();

const Settings& GetSettings() const;

const TaskRunners& GetTaskRunners() const;

fml::WeakPtr<Rasterizer> GetRasterizer();
fml::WeakPtr<Rasterizer> GetRasterizer() const;

fml::WeakPtr<Engine> GetEngine();
fml::WeakPtr<Engine> GetEngine() const;

fml::WeakPtr<PlatformView> GetPlatformView();
fml::WeakPtr<PlatformView> GetPlatformView() const;

fml::WeakPtr<ShellIOManager> GetIOManager() const;

// for access Shell in a closure
std::weak_ptr<Shell> GetWeakPtr() const;

DartVM* GetDartVM();

Expand All @@ -90,13 +85,21 @@ class Shell final : public PlatformView::Delegate,

const TaskRunners task_runners_;
const Settings settings_;
DartVMRef vm_;
DartVM* vm_;

std::weak_ptr<Shell> weak_shell_;
std::unique_ptr<PlatformView> platform_view_; // on platform task runner
std::unique_ptr<Engine> engine_; // on UI task runner
std::unique_ptr<Rasterizer> rasterizer_; // on GPU task runner
std::unique_ptr<ShellIOManager> io_manager_; // on IO task runner

fml::WeakPtr<Engine> weak_engine_; // to be shared across threads
std::promise<fml::WeakPtr<PlatformView>> platform_view_promise_;
std::promise<fml::WeakPtr<Engine>> engine_promise_;
std::promise<fml::WeakPtr<Rasterizer>> rasterizer_promise_;
std::promise<fml::WeakPtr<ShellIOManager>> io_manager_promise_;
std::shared_future<fml::WeakPtr<PlatformView>> platform_view_future_;
std::shared_future<fml::WeakPtr<Engine>> engine_future_;
std::shared_future<fml::WeakPtr<Rasterizer>> rasterizer_future_;
std::shared_future<fml::WeakPtr<ShellIOManager>> io_manager_future_;

std::unordered_map<std::string, // method
std::pair<fml::RefPtr<fml::TaskRunner>,
Expand Down Expand Up @@ -124,21 +127,14 @@ class Shell final : public PlatformView::Delegate,
size_t UnreportedFramesCount() const;

Shell(TaskRunners task_runners, Settings settings);
Shell(DartVMRef vm, TaskRunners task_runners, Settings settings);

static std::unique_ptr<Shell> CreateShellOnPlatformThread(
DartVMRef vm,
static std::shared_ptr<Shell> CreateShellOnPlatformThread(
TaskRunners task_runners,
Settings settings,
fml::RefPtr<const DartSnapshot> isolate_snapshot,
fml::RefPtr<const DartSnapshot> shared_snapshot,
Shell::CreateCallback<PlatformView> on_create_platform_view,
Shell::CreateCallback<Rasterizer> on_create_rasterizer);

bool Setup(std::unique_ptr<PlatformView> platform_view,
std::unique_ptr<Engine> engine,
std::unique_ptr<Rasterizer> rasterizer,
std::unique_ptr<ShellIOManager> io_manager);
bool Setup();

void ReportTimings();

Expand Down
2 changes: 1 addition & 1 deletion shell/common/shell_benchmarks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace flutter {
static void StartupAndShutdownShell(benchmark::State& state,
bool measure_startup,
bool measure_shutdown) {
std::unique_ptr<Shell> shell;
std::shared_ptr<Shell> shell;
std::unique_ptr<ThreadHost> thread_host;
{
benchmarking::ScopedPauseTiming pause(state, !measure_startup);
Expand Down
4 changes: 2 additions & 2 deletions shell/common/shell_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ TaskRunners ShellTest::GetTaskRunnersForFixture() {
};
}

std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings) {
std::shared_ptr<Shell> ShellTest::CreateShell(Settings settings) {
return CreateShell(std::move(settings), GetTaskRunnersForFixture());
}

std::unique_ptr<Shell> ShellTest::CreateShell(Settings settings,
std::shared_ptr<Shell> ShellTest::CreateShell(Settings settings,
TaskRunners task_runners) {
return Shell::Create(
task_runners, settings,
Expand Down
4 changes: 2 additions & 2 deletions shell/common/shell_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ShellTest : public ThreadTest {
~ShellTest();

Settings CreateSettingsForFixture();
std::unique_ptr<Shell> CreateShell(Settings settings);
std::unique_ptr<Shell> CreateShell(Settings settings,
std::shared_ptr<Shell> CreateShell(Settings settings);
std::shared_ptr<Shell> CreateShell(Settings settings,
TaskRunners task_runners);
TaskRunners GetTaskRunnersForFixture();

Expand Down
10 changes: 5 additions & 5 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ TEST_F(ShellTest, WhitelistedDartVMFlag) {

TEST_F(ShellTest, NoNeedToReportTimingsByDefault) {
auto settings = CreateSettingsForFixture();
std::unique_ptr<Shell> shell = CreateShell(std::move(settings));
std::shared_ptr<Shell> shell = CreateShell(std::move(settings));

// Create the surface needed by rasterizer
PlatformViewNotifyCreated(shell.get());
Expand All @@ -280,7 +280,7 @@ TEST_F(ShellTest, NoNeedToReportTimingsByDefault) {

TEST_F(ShellTest, NeedsReportTimingsIsSetWithCallback) {
auto settings = CreateSettingsForFixture();
std::unique_ptr<Shell> shell = CreateShell(std::move(settings));
std::shared_ptr<Shell> shell = CreateShell(std::move(settings));

// Create the surface needed by rasterizer
PlatformViewNotifyCreated(shell.get());
Expand Down Expand Up @@ -317,7 +317,7 @@ static void CheckFrameTimings(const std::vector<FrameTiming>& timings,
TEST_F(ShellTest, ReportTimingsIsCalled) {
fml::TimePoint start = fml::TimePoint::Now();
auto settings = CreateSettingsForFixture();
std::unique_ptr<Shell> shell = CreateShell(std::move(settings));
std::shared_ptr<Shell> shell = CreateShell(std::move(settings));

// Create the surface needed by rasterizer
PlatformViewNotifyCreated(shell.get());
Expand Down Expand Up @@ -383,7 +383,7 @@ TEST_F(ShellTest, FrameRasterizedCallbackIsCalled) {
timingLatch.Signal();
};

std::unique_ptr<Shell> shell = CreateShell(std::move(settings));
std::shared_ptr<Shell> shell = CreateShell(std::move(settings));

// Create the surface needed by rasterizer
PlatformViewNotifyCreated(shell.get());
Expand Down Expand Up @@ -441,7 +441,7 @@ TEST_F(ShellTest, ReportTimingsIsCalledLaterInNonReleaseMode) {
#endif
fml::TimePoint start = fml::TimePoint::Now();
auto settings = CreateSettingsForFixture();
std::unique_ptr<Shell> shell = CreateShell(std::move(settings));
std::shared_ptr<Shell> shell = CreateShell(std::move(settings));

// Create the surface needed by rasterizer
PlatformViewNotifyCreated(shell.get());
Expand Down
23 changes: 19 additions & 4 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ void AndroidShellHolder::Launch(RunConfiguration config) {
}

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
fml::MakeCopyable([engine = shell_->GetEngine(), //
config = std::move(config) //
fml::MakeCopyable([weak_shell = shell_->GetWeakPtr(), //
config = std::move(config) //
]() mutable {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
FML_LOG(INFO) << "Attempting to launch engine configuration...";
if (!engine ||
engine->Run(std::move(config)) == Engine::RunStatus::Failure) {
Expand All @@ -179,7 +184,12 @@ void AndroidShellHolder::SetViewportMetrics(
}

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
[engine = shell_->GetEngine(), metrics]() {
[weak_shell = shell_->GetWeakPtr(), metrics]() {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->SetViewportMetrics(metrics);
}
Expand All @@ -196,8 +206,13 @@ void AndroidShellHolder::DispatchPointerDataPacket(
TRACE_FLOW_BEGIN("flutter", "PointerEvent", next_pointer_flow_id_);

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(fml::MakeCopyable(
[engine = shell_->GetEngine(), packet = std::move(packet),
[weak_shell = shell_->GetWeakPtr(), packet = std::move(packet),
flow_id = next_pointer_flow_id_] {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->DispatchPointerDataPacket(*packet, flow_id);
}
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/android/android_shell_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AndroidShellHolder {
const fml::jni::JavaObjectWeakGlobalRef java_object_;
fml::WeakPtr<PlatformViewAndroid> platform_view_;
ThreadHost thread_host_;
std::unique_ptr<Shell> shell_;
std::shared_ptr<Shell> shell_;
bool is_valid_ = false;
pthread_key_t thread_destruct_key_;
uint64_t next_pointer_flow_id_ = 0;
Expand Down
55 changes: 45 additions & 10 deletions shell/platform/embedder/embedder_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ bool EmbedderEngine::Run(RunConfiguration run_configuration) {
}

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
fml::MakeCopyable([engine = shell_->GetEngine(), // engine
fml::MakeCopyable([weak_shell = shell_->GetWeakPtr(), // shell
config = std::move(run_configuration) // config
]() mutable {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
auto result = engine->Run(std::move(config));
if (result == Engine::RunStatus::Failure) {
Expand All @@ -80,7 +85,12 @@ bool EmbedderEngine::SetViewportMetrics(flutter::ViewportMetrics metrics) {
}

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
[engine = shell_->GetEngine(), metrics = std::move(metrics)]() {
[weak_shell = shell_->GetWeakPtr(), metrics = std::move(metrics)]() {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->SetViewportMetrics(std::move(metrics));
}
Expand All @@ -98,8 +108,13 @@ bool EmbedderEngine::DispatchPointerDataPacket(
TRACE_FLOW_BEGIN("flutter", "PointerEvent", next_pointer_flow_id_);

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(fml::MakeCopyable(
[engine = shell_->GetEngine(), packet = std::move(packet),
[weak_shell = shell_->GetWeakPtr(), packet = std::move(packet),
flow_id = next_pointer_flow_id_] {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->DispatchPointerDataPacket(*packet, flow_id);
}
Expand All @@ -116,7 +131,12 @@ bool EmbedderEngine::SendPlatformMessage(
}

shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
[engine = shell_->GetEngine(), message] {
[weak_shell = shell_->GetWeakPtr(), message] {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->DispatchPlatformMessage(message);
}
Expand Down Expand Up @@ -156,7 +176,12 @@ bool EmbedderEngine::SetSemanticsEnabled(bool enabled) {
return false;
}
shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
[engine = shell_->GetEngine(), enabled] {
[weak_shell = shell_->GetWeakPtr(), enabled] {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->SetSemanticsEnabled(enabled);
}
Expand All @@ -169,7 +194,12 @@ bool EmbedderEngine::SetAccessibilityFeatures(int32_t flags) {
return false;
}
shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
[engine = shell_->GetEngine(), flags] {
[weak_shell = shell_->GetWeakPtr(), flags] {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->SetAccessibilityFeatures(flags);
}
Expand All @@ -184,11 +214,16 @@ bool EmbedderEngine::DispatchSemanticsAction(int id,
return false;
}
shell_->GetTaskRunners().GetUITaskRunner()->PostTask(
fml::MakeCopyable([engine = shell_->GetEngine(), // engine
id, // id
action, // action
args = std::move(args) // args
fml::MakeCopyable([weak_shell = shell_->GetWeakPtr(), // engine
id, // id
action, // action
args = std::move(args) // args
]() mutable {
auto shell = weak_shell.lock();
if (!shell) {
return;
}
auto engine = shell->GetEngine();
if (engine) {
engine->DispatchSemanticsAction(id, action, std::move(args));
}
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/embedder/embedder_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class EmbedderEngine {

private:
const std::unique_ptr<EmbedderThreadHost> thread_host_;
std::unique_ptr<Shell> shell_;
std::shared_ptr<Shell> shell_;
const EmbedderExternalTextureGL::ExternalTextureCallback
external_texture_callback_;
bool is_valid_ = false;
Expand Down