From e94cb4f2f987afef5edebe623ec4d3ba149cf941 Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Thu, 24 May 2018 13:24:14 -0700 Subject: [PATCH] [fuchsia] Plumbing for sharing between AOT snapshots. (flutter/engine#5351) --- engine/src/flutter/runtime/dart_isolate.cc | 40 ++++++++++++------- engine/src/flutter/runtime/dart_isolate.h | 4 ++ .../flutter/runtime/dart_isolate_unittests.cc | 3 ++ engine/src/flutter/runtime/dart_snapshot.cc | 8 ++++ engine/src/flutter/runtime/dart_snapshot.h | 4 ++ engine/src/flutter/runtime/dart_vm.cc | 22 +++++++--- engine/src/flutter/runtime/dart_vm.h | 8 +++- .../src/flutter/runtime/runtime_controller.cc | 6 +++ .../src/flutter/runtime/runtime_controller.h | 3 ++ engine/src/flutter/shell/common/engine.cc | 2 + engine/src/flutter/shell/common/engine.h | 1 + engine/src/flutter/shell/common/shell.cc | 10 ++++- engine/src/flutter/shell/common/shell.h | 2 + 13 files changed, 91 insertions(+), 22 deletions(-) diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 22ff724e18b..ded3ff8f6da 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -35,6 +35,7 @@ namespace blink { fml::WeakPtr DartIsolate::CreateRootIsolate( const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, std::unique_ptr window, fml::WeakPtr resource_context, @@ -54,6 +55,7 @@ fml::WeakPtr DartIsolate::CreateRootIsolate( auto root_embedder_data = std::make_unique( vm, // VM std::move(isolate_snapshot), // isolate snapshot + std::move(shared_snapshot), // shared snapshot task_runners, // task runners std::move(resource_context), // resource context std::move(unref_queue), // skia unref queue @@ -94,6 +96,7 @@ fml::WeakPtr DartIsolate::CreateRootIsolate( DartIsolate::DartIsolate(const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr resource_context, fxl::RefPtr unref_queue, @@ -110,6 +113,7 @@ DartIsolate::DartIsolate(const DartVM* vm, vm->GetSettings().log_tag), vm_(vm), isolate_snapshot_(std::move(isolate_snapshot)), + shared_snapshot_(std::move(shared_snapshot)), child_isolate_preparer_(std::move(child_isolate_preparer)), weak_factory_(std::make_unique>(this)) { FXL_DCHECK(isolate_snapshot_) << "Must contain a valid isolate snapshot."; @@ -285,8 +289,8 @@ static bool LoadScriptSnapshot(std::shared_ptr mapping, static bool LoadKernelSnapshot(std::shared_ptr mapping, bool last_piece) { - Dart_Handle library = Dart_LoadLibraryFromKernel(mapping->GetMapping(), - mapping->GetSize()); + Dart_Handle library = + Dart_LoadLibraryFromKernel(mapping->GetMapping(), mapping->GetSize()); if (tonic::LogIfError(library)) { return false; } @@ -533,6 +537,7 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate( DartIsolate::CreateRootIsolate( vm.get(), // vm vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot null_task_runners, // task runners nullptr, // window {}, // resource context @@ -657,6 +662,7 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair( embedder_isolate = std::make_unique( vm, // vm raw_embedder_isolate->GetIsolateSnapshot(), // isolate_snapshot + raw_embedder_isolate->GetSharedSnapshot(), // shared_snapshot null_task_runners, // task_runners fml::WeakPtr{}, // resource_context nullptr, // unref_queue @@ -678,18 +684,20 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair( embedder_isolate.get(), // error // ) - : Dart_CreateIsolate(advisory_script_uri, // - advisory_script_entrypoint, // - embedder_isolate->GetIsolateSnapshot() - ->GetData() - ->GetSnapshotPointer(), // - embedder_isolate->GetIsolateSnapshot() - ->GetInstructionsIfPresent(), // - nullptr, // - nullptr, // - flags, // - embedder_isolate.get(), // - error // + : Dart_CreateIsolate( + advisory_script_uri, // + advisory_script_entrypoint, // + embedder_isolate->GetIsolateSnapshot() + ->GetData() + ->GetSnapshotPointer(), // + embedder_isolate->GetIsolateSnapshot() + ->GetInstructionsIfPresent(), // + embedder_isolate->GetSharedSnapshot()->GetDataIfPresent(), // + embedder_isolate->GetSharedSnapshot() + ->GetInstructionsIfPresent(), // + flags, // + embedder_isolate.get(), // + error // ); if (isolate == nullptr) { @@ -753,6 +761,10 @@ fxl::RefPtr DartIsolate::GetIsolateSnapshot() const { return isolate_snapshot_; } +fxl::RefPtr DartIsolate::GetSharedSnapshot() const { + return shared_snapshot_; +} + fml::WeakPtr DartIsolate::GetWeakIsolatePtr() const { return weak_factory_ ? weak_factory_->GetWeakPtr() : fml::WeakPtr(); diff --git a/engine/src/flutter/runtime/dart_isolate.h b/engine/src/flutter/runtime/dart_isolate.h index 0999d2f6f4f..16b7f376419 100644 --- a/engine/src/flutter/runtime/dart_isolate.h +++ b/engine/src/flutter/runtime/dart_isolate.h @@ -41,6 +41,7 @@ class DartIsolate : public UIDartState { static fml::WeakPtr CreateRootIsolate( const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, std::unique_ptr window, fml::WeakPtr resource_context, @@ -51,6 +52,7 @@ class DartIsolate : public UIDartState { DartIsolate(const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr resource_context, fxl::RefPtr unref_queue, @@ -85,6 +87,7 @@ class DartIsolate : public UIDartState { const DartVM* GetDartVM() const; fxl::RefPtr GetIsolateSnapshot() const; + fxl::RefPtr GetSharedSnapshot() const; fml::WeakPtr GetWeakIsolatePtr() const; @@ -107,6 +110,7 @@ class DartIsolate : public UIDartState { const DartVM* vm_ = nullptr; Phase phase_ = Phase::Unknown; const fxl::RefPtr isolate_snapshot_; + const fxl::RefPtr shared_snapshot_; std::vector> shutdown_callbacks_; ChildIsolatePreparer child_isolate_preparer_; std::unique_ptr> weak_factory_; diff --git a/engine/src/flutter/runtime/dart_isolate_unittests.cc b/engine/src/flutter/runtime/dart_isolate_unittests.cc index d8f2e02382d..ba808e5b74f 100644 --- a/engine/src/flutter/runtime/dart_isolate_unittests.cc +++ b/engine/src/flutter/runtime/dart_isolate_unittests.cc @@ -32,6 +32,7 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) { auto root_isolate = DartIsolate::CreateRootIsolate( vm.get(), // vm vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot std::move(task_runners), // task runners nullptr, // window {}, // resource context @@ -57,6 +58,7 @@ TEST_F(DartIsolateTest, IsolateCanAssociateSnapshot) { auto root_isolate = DartIsolate::CreateRootIsolate( vm.get(), // vm vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot std::move(task_runners), // task runners nullptr, // window {}, // resource context @@ -85,6 +87,7 @@ TEST_F(DartIsolateTest, CanResolveAndInvokeMethod) { auto root_isolate = DartIsolate::CreateRootIsolate( vm.get(), // vm vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot std::move(task_runners), // task runners nullptr, // window {}, // resource context diff --git a/engine/src/flutter/runtime/dart_snapshot.cc b/engine/src/flutter/runtime/dart_snapshot.cc index 1c47cffd2ff..1545b8880e4 100644 --- a/engine/src/flutter/runtime/dart_snapshot.cc +++ b/engine/src/flutter/runtime/dart_snapshot.cc @@ -170,6 +170,10 @@ fxl::RefPtr DartSnapshot::IsolateSnapshotFromSettings( #endif } +fxl::RefPtr DartSnapshot::Empty() { + return fxl::MakeRefCounted(nullptr, nullptr); +} + DartSnapshot::DartSnapshot(std::unique_ptr data, std::unique_ptr instructions) : data_(std::move(data)), instructions_(std::move(instructions)) {} @@ -192,6 +196,10 @@ const DartSnapshotBuffer* DartSnapshot::GetInstructions() const { return instructions_.get(); } +const uint8_t* DartSnapshot::GetDataIfPresent() const { + return data_ ? data_->GetSnapshotPointer() : nullptr; +} + const uint8_t* DartSnapshot::GetInstructionsIfPresent() const { return instructions_ ? instructions_->GetSnapshotPointer() : nullptr; } diff --git a/engine/src/flutter/runtime/dart_snapshot.h b/engine/src/flutter/runtime/dart_snapshot.h index 4f04765b4f5..dfc5001dc99 100644 --- a/engine/src/flutter/runtime/dart_snapshot.h +++ b/engine/src/flutter/runtime/dart_snapshot.h @@ -28,6 +28,8 @@ class DartSnapshot : public fxl::RefCountedThreadSafe { static fxl::RefPtr IsolateSnapshotFromSettings( const Settings& settings); + static fxl::RefPtr Empty(); + bool IsValid() const; bool IsValidForAOT() const; @@ -36,6 +38,8 @@ class DartSnapshot : public fxl::RefCountedThreadSafe { const DartSnapshotBuffer* GetInstructions() const; + const uint8_t* GetDataIfPresent() const; + const uint8_t* GetInstructionsIfPresent() const; private: diff --git a/engine/src/flutter/runtime/dart_vm.cc b/engine/src/flutter/runtime/dart_vm.cc index a8206789d62..a61413b1742 100644 --- a/engine/src/flutter/runtime/dart_vm.cc +++ b/engine/src/flutter/runtime/dart_vm.cc @@ -228,7 +228,7 @@ static void EmbedderInformationCallback(Dart_EmbedderInformation* info) { } fxl::RefPtr DartVM::ForProcess(Settings settings) { - return ForProcess(settings, nullptr, nullptr); + return ForProcess(settings, nullptr, nullptr, nullptr); } static std::once_flag gVMInitialization; @@ -237,10 +237,12 @@ static fxl::RefPtr gVM; fxl::RefPtr DartVM::ForProcess( Settings settings, fxl::RefPtr vm_snapshot, - fxl::RefPtr isolate_snapshot) { + fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot) { std::call_once(gVMInitialization, [settings, // vm_snapshot, // - isolate_snapshot // + isolate_snapshot, // + shared_snapshot // ]() mutable { if (!vm_snapshot) { vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings); @@ -248,9 +250,13 @@ fxl::RefPtr DartVM::ForProcess( if (!isolate_snapshot) { isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings); } + if (!shared_snapshot) { + shared_snapshot = DartSnapshot::Empty(); + } gVM = fxl::MakeRefCounted(settings, // std::move(vm_snapshot), // - std::move(isolate_snapshot) // + std::move(isolate_snapshot), // + std::move(shared_snapshot) // ); }); return gVM; @@ -262,10 +268,12 @@ fxl::RefPtr DartVM::ForProcessIfInitialized() { DartVM::DartVM(const Settings& settings, fxl::RefPtr vm_snapshot, - fxl::RefPtr isolate_snapshot) + fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot) : settings_(settings), vm_snapshot_(std::move(vm_snapshot)), isolate_snapshot_(std::move(isolate_snapshot)), + shared_snapshot_(std::move(shared_snapshot)), platform_kernel_mapping_( std::make_unique(settings.platform_kernel_path)), weak_factory_(this) { @@ -454,6 +462,10 @@ fxl::RefPtr DartVM::GetIsolateSnapshot() const { return isolate_snapshot_; } +fxl::RefPtr DartVM::GetSharedSnapshot() const { + return shared_snapshot_; +} + ServiceProtocol& DartVM::GetServiceProtocol() { return service_protocol_; } diff --git a/engine/src/flutter/runtime/dart_vm.h b/engine/src/flutter/runtime/dart_vm.h index 12ac1c6b21b..8202aad1204 100644 --- a/engine/src/flutter/runtime/dart_vm.h +++ b/engine/src/flutter/runtime/dart_vm.h @@ -30,7 +30,8 @@ class DartVM : public fxl::RefCountedThreadSafe { static fxl::RefPtr ForProcess( Settings settings, fxl::RefPtr vm_snapshot, - fxl::RefPtr isolate_snapshot); + fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot); static fxl::RefPtr ForProcessIfInitialized(); @@ -43,6 +44,7 @@ class DartVM : public fxl::RefCountedThreadSafe { const DartSnapshot& GetVMSnapshot() const; fxl::RefPtr GetIsolateSnapshot() const; + fxl::RefPtr GetSharedSnapshot() const; fxl::WeakPtr GetWeakPtr(); @@ -52,13 +54,15 @@ class DartVM : public fxl::RefCountedThreadSafe { const Settings settings_; const fxl::RefPtr vm_snapshot_; const fxl::RefPtr isolate_snapshot_; + const fxl::RefPtr shared_snapshot_; std::unique_ptr platform_kernel_mapping_; ServiceProtocol service_protocol_; fxl::WeakPtrFactory weak_factory_; DartVM(const Settings& settings, fxl::RefPtr vm_snapshot, - fxl::RefPtr isolate_snapshot); + fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot); ~DartVM(); diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index 8f1d719915b..56ba08314b1 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -22,12 +22,14 @@ RuntimeController::RuntimeController( RuntimeDelegate& p_client, const DartVM* p_vm, fxl::RefPtr p_isolate_snapshot, + fxl::RefPtr p_shared_snapshot, TaskRunners p_task_runners, fml::WeakPtr p_resource_context, fxl::RefPtr p_unref_queue) : RuntimeController(p_client, p_vm, std::move(p_isolate_snapshot), + std::move(p_shared_snapshot), std::move(p_task_runners), std::move(p_resource_context), std::move(p_unref_queue), @@ -37,6 +39,7 @@ RuntimeController::RuntimeController( RuntimeDelegate& p_client, const DartVM* p_vm, fxl::RefPtr p_isolate_snapshot, + fxl::RefPtr p_shared_snapshot, TaskRunners p_task_runners, fml::WeakPtr p_resource_context, fxl::RefPtr p_unref_queue, @@ -44,6 +47,7 @@ RuntimeController::RuntimeController( : client_(p_client), vm_(p_vm), isolate_snapshot_(std::move(p_isolate_snapshot)), + shared_snapshot_(std::move(p_shared_snapshot)), task_runners_(p_task_runners), resource_context_(p_resource_context), unref_queue_(p_unref_queue), @@ -51,6 +55,7 @@ RuntimeController::RuntimeController( root_isolate_( DartIsolate::CreateRootIsolate(vm_, isolate_snapshot_, + shared_snapshot_, task_runners_, std::make_unique(this), resource_context_, @@ -94,6 +99,7 @@ std::unique_ptr RuntimeController::Clone() const { client_, // vm_, // isolate_snapshot_, // + shared_snapshot_, // task_runners_, // resource_context_, // unref_queue_, // diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index 290282b2e6c..a9d32484da1 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -26,6 +26,7 @@ class RuntimeController final : public WindowClient { RuntimeController(RuntimeDelegate& client, const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr resource_context, fxl::RefPtr unref_queue); @@ -81,6 +82,7 @@ class RuntimeController final : public WindowClient { RuntimeDelegate& client_; const DartVM* vm_; fxl::RefPtr isolate_snapshot_; + fxl::RefPtr shared_snapshot_; TaskRunners task_runners_; fml::WeakPtr resource_context_; fxl::RefPtr unref_queue_; @@ -91,6 +93,7 @@ class RuntimeController final : public WindowClient { RuntimeController(RuntimeDelegate& client, const DartVM* vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr resource_context, fxl::RefPtr unref_queue, diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 175ebd54e94..b15a3bc31c1 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -38,6 +38,7 @@ static constexpr char kSettingsChannel[] = "flutter/settings"; Engine::Engine(Delegate& delegate, const blink::DartVM& vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, blink::TaskRunners task_runners, blink::Settings settings, std::unique_ptr animator, @@ -57,6 +58,7 @@ Engine::Engine(Delegate& delegate, *this, // runtime delegate &vm, // VM std::move(isolate_snapshot), // isolate snapshot + std::move(shared_snapshot), // shared snapshot std::move(task_runners), // task runners std::move(resource_context), // resource context std::move(unref_queue) // skia unref queue diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 65e907759fc..e1fed0d683d 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -41,6 +41,7 @@ class Engine final : public blink::RuntimeDelegate { Engine(Delegate& delegate, const blink::DartVM& vm, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, blink::TaskRunners task_runners, blink::Settings settings, std::unique_ptr animator, diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 5d02f8fd191..97385fab65f 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -41,6 +41,7 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( blink::TaskRunners task_runners, blink::Settings settings, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, Shell::CreateCallback on_create_rasterizer) { if (!task_runners.IsValid()) { @@ -112,6 +113,7 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( &engine, // shell = shell.get(), // isolate_snapshot = std::move(isolate_snapshot), // + shared_snapshot = std::move(shared_snapshot), // vsync_waiter = std::move(vsync_waiter), // resource_context = std::move(resource_context), // unref_queue = std::move(unref_queue) // @@ -126,6 +128,7 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( engine = std::make_unique(*shell, // shell->GetDartVM(), // std::move(isolate_snapshot), // + std::move(shared_snapshot), // task_runners, // shell->GetSettings(), // std::move(animator), // @@ -202,7 +205,8 @@ static void PerformInitializationTasks(const blink::Settings& settings) { }); } -std::unique_ptr Shell::Create( + + std::unique_ptr Shell::Create( blink::TaskRunners task_runners, blink::Settings settings, Shell::CreateCallback on_create_platform_view, @@ -214,6 +218,7 @@ std::unique_ptr Shell::Create( return Shell::Create(std::move(task_runners), // std::move(settings), // vm->GetIsolateSnapshot(), // + blink::DartSnapshot::Empty(), // std::move(on_create_platform_view), // std::move(on_create_rasterizer) // ); @@ -223,6 +228,7 @@ std::unique_ptr Shell::Create( blink::TaskRunners task_runners, blink::Settings settings, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, Shell::CreateCallback on_create_rasterizer) { PerformInitializationTasks(settings); @@ -241,12 +247,14 @@ std::unique_ptr Shell::Create( task_runners = std::move(task_runners), // settings, // isolate_snapshot = std::move(isolate_snapshot), // + shared_snapshot = std::move(shared_snapshot), // on_create_platform_view, // on_create_rasterizer // ]() { shell = CreateShellOnPlatformThread(std::move(task_runners), // settings, // std::move(isolate_snapshot), // + std::move(shared_snapshot), // on_create_platform_view, // on_create_rasterizer // ); diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index f1403d2fc55..2a8caeb4765 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -56,6 +56,7 @@ class Shell final : public PlatformView::Delegate, blink::TaskRunners task_runners, blink::Settings settings, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, CreateCallback on_create_platform_view, CreateCallback on_create_rasterizer); @@ -105,6 +106,7 @@ class Shell final : public PlatformView::Delegate, blink::TaskRunners task_runners, blink::Settings settings, fxl::RefPtr isolate_snapshot, + fxl::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, Shell::CreateCallback on_create_rasterizer);