From 8a0076fdddc96b4ec2fb67f45051aa490827fc02 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Tue, 2 Apr 2019 09:12:56 -0700 Subject: [PATCH] Revert "Separate the data required to bootstrap the VM into its own class. (#8397)" (flutter/engine#8406) This reverts commit 38f5fc418a08ed43945ad21d19494d6b352e1443. --- .../ci/licenses_golden/licenses_flutter | 4 - engine/src/flutter/common/settings.h | 2 - .../isolate_name_server_natives.cc | 18 +- engine/src/flutter/lib/ui/ui_dart_state.cc | 6 +- engine/src/flutter/lib/ui/ui_dart_state.h | 6 +- engine/src/flutter/runtime/BUILD.gn | 5 - engine/src/flutter/runtime/dart_isolate.cc | 89 +++++----- engine/src/flutter/runtime/dart_isolate.h | 26 +-- .../flutter/runtime/dart_isolate_unittests.cc | 78 ++++---- engine/src/flutter/runtime/dart_vm.cc | 166 ++++++++++-------- engine/src/flutter/runtime/dart_vm.h | 70 +++++--- engine/src/flutter/runtime/dart_vm_data.cc | 79 --------- engine/src/flutter/runtime/dart_vm_data.h | 47 ----- .../src/flutter/runtime/dart_vm_lifecycle.cc | 117 ------------ .../src/flutter/runtime/dart_vm_lifecycle.h | 72 -------- .../src/flutter/runtime/dart_vm_unittests.cc | 8 +- .../src/flutter/runtime/runtime_controller.cc | 10 +- .../src/flutter/runtime/runtime_controller.h | 12 +- engine/src/flutter/shell/common/engine.cc | 4 +- engine/src/flutter/shell/common/engine.h | 4 +- engine/src/flutter/shell/common/shell.cc | 88 ++++------ engine/src/flutter/shell/common/shell.h | 21 +-- engine/src/flutter/testing/testing.cc | 4 +- engine/src/flutter/testing/testing.h | 4 - 24 files changed, 299 insertions(+), 641 deletions(-) delete mode 100644 engine/src/flutter/runtime/dart_vm_data.cc delete mode 100644 engine/src/flutter/runtime/dart_vm_data.h delete mode 100644 engine/src/flutter/runtime/dart_vm_lifecycle.cc delete mode 100644 engine/src/flutter/runtime/dart_vm_lifecycle.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 312e6c329ab..c9914e4c215 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -383,10 +383,6 @@ FILE: ../../../flutter/runtime/dart_snapshot_buffer.cc FILE: ../../../flutter/runtime/dart_snapshot_buffer.h FILE: ../../../flutter/runtime/dart_vm.cc FILE: ../../../flutter/runtime/dart_vm.h -FILE: ../../../flutter/runtime/dart_vm_data.cc -FILE: ../../../flutter/runtime/dart_vm_data.h -FILE: ../../../flutter/runtime/dart_vm_lifecycle.cc -FILE: ../../../flutter/runtime/dart_vm_lifecycle.h FILE: ../../../flutter/runtime/dart_vm_unittests.cc FILE: ../../../flutter/runtime/embedder_resources.cc FILE: ../../../flutter/runtime/embedder_resources.h diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 40b647de1f4..bfbfd4139d3 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -85,8 +85,6 @@ struct Settings { // Font settings bool use_test_fonts = false; - bool leak_vm = true; - // Engine settings TaskObserverAdd task_observer_add; TaskObserverRemove task_observer_remove; diff --git a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc index 3f7f80ef05f..7f4467886f4 100644 --- a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc +++ b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc @@ -14,10 +14,8 @@ namespace blink { Dart_Handle IsolateNameServerNatives::LookupPortByName( const std::string& name) { - auto name_server = UIDartState::Current()->GetIsolateNameServer(); - if (!name_server) { - return Dart_Null(); - } + IsolateNameServer* name_server = + UIDartState::Current()->GetIsolateNameServer(); Dart_Port port = name_server->LookupIsolatePortByName(name); if (port == ILLEGAL_PORT) { return Dart_Null(); @@ -28,10 +26,8 @@ Dart_Handle IsolateNameServerNatives::LookupPortByName( Dart_Handle IsolateNameServerNatives::RegisterPortWithName( Dart_Handle port_handle, const std::string& name) { - auto name_server = UIDartState::Current()->GetIsolateNameServer(); - if (!name_server) { - return Dart_False(); - } + IsolateNameServer* name_server = + UIDartState::Current()->GetIsolateNameServer(); Dart_Port port = ILLEGAL_PORT; Dart_SendPortGetId(port_handle, &port); if (!name_server->RegisterIsolatePortWithName(port, name)) { @@ -42,10 +38,8 @@ Dart_Handle IsolateNameServerNatives::RegisterPortWithName( Dart_Handle IsolateNameServerNatives::RemovePortNameMapping( const std::string& name) { - auto name_server = UIDartState::Current()->GetIsolateNameServer(); - if (!name_server) { - return Dart_False(); - } + IsolateNameServer* name_server = + UIDartState::Current()->GetIsolateNameServer(); if (!name_server->RemoveIsolateNameMapping(name)) { return Dart_False(); } diff --git a/engine/src/flutter/lib/ui/ui_dart_state.cc b/engine/src/flutter/lib/ui/ui_dart_state.cc index be0a5348f31..b42cbaf5ba6 100644 --- a/engine/src/flutter/lib/ui/ui_dart_state.cc +++ b/engine/src/flutter/lib/ui/ui_dart_state.cc @@ -23,7 +23,7 @@ UIDartState::UIDartState( std::string advisory_script_entrypoint, std::string logger_prefix, UnhandledExceptionCallback unhandled_exception_callback, - std::shared_ptr isolate_name_server) + IsolateNameServer* isolate_name_server) : task_runners_(std::move(task_runners)), add_callback_(std::move(add_callback)), remove_callback_(std::move(remove_callback)), @@ -33,7 +33,7 @@ UIDartState::UIDartState( advisory_script_entrypoint_(std::move(advisory_script_entrypoint)), logger_prefix_(std::move(logger_prefix)), unhandled_exception_callback_(unhandled_exception_callback), - isolate_name_server_(std::move(isolate_name_server)) { + isolate_name_server_(isolate_name_server) { AddOrRemoveTaskObserver(true /* add */); } @@ -124,7 +124,7 @@ fml::WeakPtr UIDartState::GetResourceContext() const { return io_manager_->GetResourceContext(); } -std::shared_ptr UIDartState::GetIsolateNameServer() const { +IsolateNameServer* UIDartState::GetIsolateNameServer() { return isolate_name_server_; } diff --git a/engine/src/flutter/lib/ui/ui_dart_state.h b/engine/src/flutter/lib/ui/ui_dart_state.h index afa5e47e83d..f2c63cbacef 100644 --- a/engine/src/flutter/lib/ui/ui_dart_state.h +++ b/engine/src/flutter/lib/ui/ui_dart_state.h @@ -53,7 +53,7 @@ class UIDartState : public tonic::DartState { fml::WeakPtr GetResourceContext() const; - std::shared_ptr GetIsolateNameServer() const; + IsolateNameServer* GetIsolateNameServer(); tonic::DartErrorHandleType GetLastError(); @@ -81,7 +81,7 @@ class UIDartState : public tonic::DartState { std::string advisory_script_entrypoint, std::string logger_prefix, UnhandledExceptionCallback unhandled_exception_callback, - std::shared_ptr isolate_name_server); + IsolateNameServer* isolate_name_server); ~UIDartState() override; @@ -107,7 +107,7 @@ class UIDartState : public tonic::DartState { std::unique_ptr window_; tonic::DartMicrotaskQueue microtask_queue_; UnhandledExceptionCallback unhandled_exception_callback_; - const std::shared_ptr isolate_name_server_; + IsolateNameServer* isolate_name_server_; void AddOrRemoveTaskObserver(bool add); }; diff --git a/engine/src/flutter/runtime/BUILD.gn b/engine/src/flutter/runtime/BUILD.gn index a906e589c22..3d0e807585f 100644 --- a/engine/src/flutter/runtime/BUILD.gn +++ b/engine/src/flutter/runtime/BUILD.gn @@ -50,10 +50,6 @@ source_set("runtime") { "dart_snapshot_buffer.h", "dart_vm.cc", "dart_vm.h", - "dart_vm_data.cc", - "dart_vm_data.h", - "dart_vm_lifecycle.cc", - "dart_vm_lifecycle.h", "embedder_resources.cc", "embedder_resources.h", "runtime_controller.cc", @@ -119,7 +115,6 @@ executable("runtime_unittests") { "$flutter_root/common", "$flutter_root/fml", "$flutter_root/lib/snapshot", - "$flutter_root/shell/common", "$flutter_root/testing:dart", "//third_party/skia", "//third_party/tonic", diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 46df151f40b..7cae71c36e3 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -14,7 +14,6 @@ #include "flutter/lib/ui/dart_ui.h" #include "flutter/runtime/dart_service_isolate.h" #include "flutter/runtime/dart_vm.h" -#include "flutter/runtime/dart_vm_lifecycle.h" #include "third_party/dart/runtime/include/dart_api.h" #include "third_party/dart/runtime/include/dart_tools_api.h" #include "third_party/tonic/converter/dart_converter.h" @@ -30,9 +29,9 @@ namespace blink { std::weak_ptr DartIsolate::CreateRootIsolate( - const Settings& settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + DartVM* vm, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, std::unique_ptr window, fml::WeakPtr snapshot_delegate, @@ -51,7 +50,7 @@ std::weak_ptr DartIsolate::CreateRootIsolate( // isolate lifecycle is entirely managed by the VM). auto root_embedder_data = std::make_unique>( std::make_shared( - settings, // settings + vm, // VM std::move(isolate_snapshot), // isolate snapshot std::move(shared_snapshot), // shared snapshot task_runners, // task runners @@ -94,9 +93,9 @@ std::weak_ptr DartIsolate::CreateRootIsolate( return embedder_isolate; } -DartIsolate::DartIsolate(const Settings& settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, +DartIsolate::DartIsolate(DartVM* vm, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr snapshot_delegate, fml::WeakPtr io_manager, @@ -104,33 +103,38 @@ DartIsolate::DartIsolate(const Settings& settings, std::string advisory_script_entrypoint, ChildIsolatePreparer child_isolate_preparer) : UIDartState(std::move(task_runners), - settings.task_observer_add, - settings.task_observer_remove, + vm->GetSettings().task_observer_add, + vm->GetSettings().task_observer_remove, std::move(snapshot_delegate), std::move(io_manager), advisory_script_uri, advisory_script_entrypoint, - settings.log_tag, - settings.unhandled_exception_callback, - DartVMRef::GetIsolateNameServer()), - settings_(settings), + vm->GetSettings().log_tag, + vm->GetSettings().unhandled_exception_callback, + vm->GetIsolateNameServer()), + vm_(vm), isolate_snapshot_(std::move(isolate_snapshot)), shared_snapshot_(std::move(shared_snapshot)), child_isolate_preparer_(std::move(child_isolate_preparer)) { FML_DCHECK(isolate_snapshot_) << "Must contain a valid isolate snapshot."; + + if (vm_ == nullptr) { + return; + } + phase_ = Phase::Uninitialized; } DartIsolate::~DartIsolate() = default; -const Settings& DartIsolate::GetSettings() const { - return settings_; -} - DartIsolate::Phase DartIsolate::GetPhase() const { return phase_; } +DartVM* DartIsolate::GetDartVM() const { + return vm_; +} + bool DartIsolate::Initialize(Dart_Isolate dart_isolate, bool is_root_isolate) { TRACE_EVENT0("flutter", "DartIsolate::Initialize"); if (phase_ != Phase::Uninitialized) { @@ -496,16 +500,16 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate( const char* package_config, Dart_IsolateFlags* flags, char** error) { - auto vm_data = DartVMRef::GetVMData(); + auto vm = DartVM::ForProcessIfInitialized(); - if (!vm_data) { + if (!vm) { *error = strdup( - "Could not access VM data to initialize isolates. This may be because " - "the VM has initialized shutdown on another thread already."); + "Could not resolve the VM when attempting to create the service " + "isolate."); return nullptr; } - const auto& settings = vm_data->GetSettings(); + const auto& settings = vm->GetSettings(); if (!settings.enable_observatory) { FML_DLOG(INFO) << "Observatory is disabled."; @@ -520,16 +524,16 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate( std::weak_ptr weak_service_isolate = DartIsolate::CreateRootIsolate( - vm_data->GetSettings(), // settings - vm_data->GetIsolateSnapshot(), // isolate snapshot - vm_data->GetSharedSnapshot(), // shared snapshot - null_task_runners, // task runners - nullptr, // window - {}, // snapshot delegate - {}, // IO Manager - DART_VM_SERVICE_ISOLATE_NAME, // script uri - DART_VM_SERVICE_ISOLATE_NAME, // script entrypoint - flags // flags + vm.get(), // vm + vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot + null_task_runners, // task runners + nullptr, // window + {}, // snapshot delegate + {}, // IO Manager + DART_VM_SERVICE_ISOLATE_NAME, // script uri + DART_VM_SERVICE_ISOLATE_NAME, // script entrypoint + flags // flags ); std::shared_ptr service_isolate = weak_service_isolate.lock(); @@ -552,13 +556,7 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate( return nullptr; } - if (auto service_protocol = DartVMRef::GetServiceProtocol()) { - service_protocol->ToggleHooks(true); - } else { - FML_DLOG(ERROR) - << "Could not acquire the service protocol handlers. This might be " - "because the VM has already begun teardown on another thread."; - } + vm->GetServiceProtocol().ToggleHooks(true); return service_isolate->isolate(); } @@ -614,13 +612,16 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair( std::unique_ptr> embedder_isolate( p_parent_embedder_isolate); - if (embedder_isolate == nullptr) { + if (embedder_isolate == nullptr || + (*embedder_isolate)->GetDartVM() == nullptr) { *error = strdup("Parent isolate did not have embedder specific callback data."); FML_DLOG(ERROR) << *error; return {nullptr, {}}; } + DartVM* const vm = (*embedder_isolate)->GetDartVM(); + if (!is_root_isolate) { auto* raw_embedder_isolate = embedder_isolate.release(); @@ -629,7 +630,7 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair( embedder_isolate = std::make_unique>( std::make_shared( - (*raw_embedder_isolate)->GetSettings(), // settings + vm, // vm (*raw_embedder_isolate)->GetIsolateSnapshot(), // isolate_snapshot (*raw_embedder_isolate)->GetSharedSnapshot(), // shared_snapshot null_task_runners, // task_runners @@ -702,11 +703,11 @@ void DartIsolate::DartIsolateCleanupCallback( delete embedder_isolate; } -fml::RefPtr DartIsolate::GetIsolateSnapshot() const { +fml::RefPtr DartIsolate::GetIsolateSnapshot() const { return isolate_snapshot_; } -fml::RefPtr DartIsolate::GetSharedSnapshot() const { +fml::RefPtr DartIsolate::GetSharedSnapshot() const { return shared_snapshot_; } diff --git a/engine/src/flutter/runtime/dart_isolate.h b/engine/src/flutter/runtime/dart_isolate.h index 49c3df3c510..6b92875e1dc 100644 --- a/engine/src/flutter/runtime/dart_isolate.h +++ b/engine/src/flutter/runtime/dart_isolate.h @@ -41,9 +41,9 @@ class DartIsolate : public UIDartState { // bindings. From the VM's perspective, this isolate is not special in any // way. static std::weak_ptr CreateRootIsolate( - const Settings& settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + DartVM* vm, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, std::unique_ptr window, fml::WeakPtr snapshot_delegate, @@ -52,9 +52,9 @@ class DartIsolate : public UIDartState { std::string advisory_script_entrypoint, Dart_IsolateFlags* flags = nullptr); - DartIsolate(const Settings& settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + DartIsolate(DartVM* vm, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr snapshot_delegate, fml::WeakPtr io_manager, @@ -64,8 +64,6 @@ class DartIsolate : public UIDartState { ~DartIsolate() override; - const Settings& GetSettings() const; - Phase GetPhase() const; FML_WARN_UNUSED_RESULT @@ -88,9 +86,11 @@ class DartIsolate : public UIDartState { void AddIsolateShutdownCallback(fml::closure closure); - fml::RefPtr GetIsolateSnapshot() const; + DartVM* GetDartVM() const; - fml::RefPtr GetSharedSnapshot() const; + fml::RefPtr GetIsolateSnapshot() const; + + fml::RefPtr GetSharedSnapshot() const; std::weak_ptr GetWeakIsolatePtr(); @@ -109,10 +109,10 @@ class DartIsolate : public UIDartState { }; friend class DartVM; + DartVM* const vm_ = nullptr; Phase phase_ = Phase::Unknown; - const Settings settings_; - const fml::RefPtr isolate_snapshot_; - const fml::RefPtr shared_snapshot_; + const fml::RefPtr isolate_snapshot_; + const fml::RefPtr shared_snapshot_; std::vector> kernel_buffers_; std::vector> shutdown_callbacks_; ChildIsolatePreparer child_isolate_preparer_ = nullptr; diff --git a/engine/src/flutter/runtime/dart_isolate_unittests.cc b/engine/src/flutter/runtime/dart_isolate_unittests.cc index e2a7c734bb1..d35766f58f8 100644 --- a/engine/src/flutter/runtime/dart_isolate_unittests.cc +++ b/engine/src/flutter/runtime/dart_isolate_unittests.cc @@ -9,7 +9,6 @@ #include "flutter/fml/thread.h" #include "flutter/runtime/dart_isolate.h" #include "flutter/runtime/dart_vm.h" -#include "flutter/runtime/dart_vm_lifecycle.h" #include "flutter/runtime/runtime_test.h" #include "flutter/testing/testing.h" #include "flutter/testing/thread_test.h" @@ -27,10 +26,8 @@ using DartIsolateTest = RuntimeTest; TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) { auto settings = CreateSettingsForFixture(); - auto vm_ref = DartVMRef::Create(settings); - ASSERT_TRUE(vm_ref); - auto vm_data = vm_ref.GetVMData(); - ASSERT_TRUE(vm_data); + auto vm = DartVM::ForProcess(settings); + ASSERT_TRUE(vm); TaskRunners task_runners(CURRENT_TEST_NAME, // GetCurrentTaskRunner(), // GetCurrentTaskRunner(), // @@ -38,15 +35,15 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) { GetCurrentTaskRunner() // ); auto weak_isolate = DartIsolate::CreateRootIsolate( - vm_data->GetSettings(), // settings - vm_data->GetIsolateSnapshot(), // isolate snapshot - vm_data->GetSharedSnapshot(), // shared snapshot - std::move(task_runners), // task runners - nullptr, // window - {}, // snapshot delegate - {}, // io manager - "main.dart", // advisory uri - "main" // advisory entrypoint + vm.get(), // vm + vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot + std::move(task_runners), // task runners + nullptr, // window + {}, // snapshot delegate + {}, // io manager + "main.dart", // advisory uri + "main" // advisory entrypoint ); auto root_isolate = weak_isolate.lock(); ASSERT_TRUE(root_isolate); @@ -56,10 +53,8 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) { TEST_F(DartIsolateTest, IsolateShutdownCallbackIsInIsolateScope) { auto settings = CreateSettingsForFixture(); - auto vm_ref = DartVMRef::Create(settings); - ASSERT_TRUE(vm_ref); - auto vm_data = vm_ref.GetVMData(); - ASSERT_TRUE(vm_data); + auto vm = DartVM::ForProcess(settings); + ASSERT_TRUE(vm); TaskRunners task_runners(CURRENT_TEST_NAME, // GetCurrentTaskRunner(), // GetCurrentTaskRunner(), // @@ -67,15 +62,15 @@ TEST_F(DartIsolateTest, IsolateShutdownCallbackIsInIsolateScope) { GetCurrentTaskRunner() // ); auto weak_isolate = DartIsolate::CreateRootIsolate( - vm_data->GetSettings(), // settings - vm_data->GetIsolateSnapshot(), // isolate snapshot - vm_data->GetSharedSnapshot(), // shared snapshot - std::move(task_runners), // task runners - nullptr, // window - {}, // snapshot delegate - {}, // io manager - "main.dart", // advisory uri - "main" // advisory entrypoint + vm.get(), // vm + vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot + std::move(task_runners), // task runners + nullptr, // window + {}, // snapshot delegate + {}, // io manager + "main.dart", // advisory uri + "main" // advisory entrypoint ); auto root_isolate = weak_isolate.lock(); ASSERT_TRUE(root_isolate); @@ -153,12 +148,11 @@ static void RunDartCodeInIsolate(std::unique_ptr& result, fml::RefPtr task_runner, std::string entrypoint) { FML_CHECK(task_runner->RunsTasksOnCurrentThread()); - auto vm_ref = DartVMRef::Create(settings); + auto vm = DartVM::ForProcess(settings); - if (!vm_ref) { + if (!vm) { return; } - TaskRunners task_runners(CURRENT_TEST_NAME, // task_runner, // task_runner, // @@ -166,22 +160,16 @@ static void RunDartCodeInIsolate(std::unique_ptr& result, task_runner // ); - auto vm_data = vm_ref.GetVMData(); - - if (!vm_data) { - return; - } - auto weak_isolate = DartIsolate::CreateRootIsolate( - vm_data->GetSettings(), // settings - vm_data->GetIsolateSnapshot(), // isolate snapshot - vm_data->GetSharedSnapshot(), // shared snapshot - std::move(task_runners), // task runners - nullptr, // window - {}, // snapshot delegate - {}, // io manager - "main.dart", // advisory uri - "main" // advisory entrypoint + vm.get(), // vm + vm->GetIsolateSnapshot(), // isolate snapshot + vm->GetSharedSnapshot(), // shared snapshot + std::move(task_runners), // task runners + nullptr, // window + {}, // snapshot delegate + {}, // io manager + "main.dart", // advisory uri + "main" // advisory entrypoint ); auto root_isolate = diff --git a/engine/src/flutter/runtime/dart_vm.cc b/engine/src/flutter/runtime/dart_vm.cc index 110f7c2b278..4c6054f7be2 100644 --- a/engine/src/flutter/runtime/dart_vm.cc +++ b/engine/src/flutter/runtime/dart_vm.cc @@ -15,7 +15,6 @@ #include "flutter/fml/file.h" #include "flutter/fml/logging.h" #include "flutter/fml/mapping.h" -#include "flutter/fml/synchronization/thread_annotations.h" #include "flutter/fml/time/time_delta.h" #include "flutter/fml/trace_event.h" #include "flutter/lib/io/dart_io.h" @@ -220,48 +219,66 @@ static void EmbedderInformationCallback(Dart_EmbedderInformation* info) { info->name = "Flutter"; } -std::shared_ptr DartVM::Create( +fml::RefPtr DartVM::ForProcess(Settings settings) { + return ForProcess(settings, nullptr, nullptr, nullptr); +} + +static std::once_flag gVMInitialization; +static std::mutex gVMMutex; +static fml::RefPtr gVM; + +fml::RefPtr DartVM::ForProcess( Settings settings, fml::RefPtr vm_snapshot, fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, - std::shared_ptr isolate_name_server) { - auto vm_data = DartVMData::Create(settings, // - std::move(vm_snapshot), // - std::move(isolate_snapshot), // - std::move(shared_snapshot) // - ); - - if (!vm_data) { - FML_LOG(ERROR) << "Could not setup VM data to bootstrap the VM from."; - return {}; - } - - // Note: std::make_shared unviable due to hidden constructor. - return std::shared_ptr( - new DartVM(std::move(vm_data), std::move(isolate_name_server))); + fml::RefPtr shared_snapshot) { + std::lock_guard lock(gVMMutex); + std::call_once(gVMInitialization, [settings, // + vm_snapshot, // + isolate_snapshot, // + shared_snapshot // + ]() mutable { + if (!vm_snapshot) { + vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings); + } + if (!(vm_snapshot && vm_snapshot->IsValid())) { + FML_LOG(ERROR) << "VM snapshot must be valid."; + return; + } + if (!isolate_snapshot) { + isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings); + } + if (!(isolate_snapshot && isolate_snapshot->IsValid())) { + FML_LOG(ERROR) << "Isolate snapshot must be valid."; + return; + } + if (!shared_snapshot) { + shared_snapshot = DartSnapshot::Empty(); + } + gVM = fml::MakeRefCounted(settings, // + std::move(vm_snapshot), // + std::move(isolate_snapshot), // + std::move(shared_snapshot) // + ); + }); + return gVM; } -static std::atomic_size_t gVMLaunchCount; - -size_t DartVM::GetVMLaunchCount() { - return gVMLaunchCount; +fml::RefPtr DartVM::ForProcessIfInitialized() { + std::lock_guard lock(gVMMutex); + return gVM; } -DartVM::DartVM(std::shared_ptr vm_data, - std::shared_ptr isolate_name_server) - : settings_(vm_data->GetSettings()), - vm_data_(vm_data), - isolate_name_server_(std::move(isolate_name_server)), - service_protocol_(std::make_shared()) { +DartVM::DartVM(const Settings& settings, + fml::RefPtr vm_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot) + : settings_(settings), + vm_snapshot_(std::move(vm_snapshot)), + isolate_snapshot_(std::move(isolate_snapshot)), + shared_snapshot_(std::move(shared_snapshot)), + weak_factory_(this) { TRACE_EVENT0("flutter", "DartVMInitializer"); - - gVMLaunchCount++; - - FML_DCHECK(vm_data_); - FML_DCHECK(isolate_name_server_); - FML_DCHECK(service_protocol_); - FML_DLOG(INFO) << "Attempting Dart VM launch for mode: " << (IsRunningPrecompiledCode() ? "AOT" : "Interpreter"); @@ -269,8 +286,8 @@ DartVM::DartVM(std::shared_ptr vm_data, TRACE_EVENT0("flutter", "dart::bin::BootstrapDartIo"); dart::bin::BootstrapDartIo(); - if (!settings_.temp_directory_path.empty()) { - dart::bin::SetSystemTempDirectory(settings_.temp_directory_path.c_str()); + if (!settings.temp_directory_path.empty()) { + dart::bin::SetSystemTempDirectory(settings.temp_directory_path.c_str()); } } @@ -283,7 +300,7 @@ DartVM::DartVM(std::shared_ptr vm_data, args.push_back("--ignore-unrecognized-flags"); for (auto* const profiler_flag : - ProfilingFlags(settings_.enable_dart_profiling)) { + ProfilingFlags(settings.enable_dart_profiling)) { args.push_back(profiler_flag); } @@ -296,7 +313,7 @@ DartVM::DartVM(std::shared_ptr vm_data, // Enable Dart assertions if we are not running precompiled code. We run non- // precompiled code only in the debug product mode. - bool enable_asserts = !settings_.disable_dart_asserts; + bool enable_asserts = !settings.disable_dart_asserts; #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DYNAMIC_PROFILE || \ FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DYNAMIC_RELEASE @@ -320,24 +337,24 @@ DartVM::DartVM(std::shared_ptr vm_data, PushBackAll(&args, kDartAssertArgs, arraysize(kDartAssertArgs)); } - if (settings_.start_paused) { + if (settings.start_paused) { PushBackAll(&args, kDartStartPausedArgs, arraysize(kDartStartPausedArgs)); } - if (settings_.endless_trace_buffer || settings_.trace_startup) { + if (settings.endless_trace_buffer || settings.trace_startup) { // If we are tracing startup, make sure the trace buffer is endless so we // don't lose early traces. PushBackAll(&args, kDartEndlessTraceBufferArgs, arraysize(kDartEndlessTraceBufferArgs)); } - if (settings_.trace_systrace) { + if (settings.trace_systrace) { PushBackAll(&args, kDartSystraceTraceBufferArgs, arraysize(kDartSystraceTraceBufferArgs)); PushBackAll(&args, kDartTraceStreamsArgs, arraysize(kDartTraceStreamsArgs)); } - if (settings_.trace_startup) { + if (settings.trace_startup) { PushBackAll(&args, kDartTraceStartupArgs, arraysize(kDartTraceStartupArgs)); } @@ -346,8 +363,8 @@ DartVM::DartVM(std::shared_ptr vm_data, PushBackAll(&args, kDartTraceStreamsArgs, arraysize(kDartTraceStreamsArgs)); #endif - for (size_t i = 0; i < settings_.dart_flags.size(); i++) - args.push_back(settings_.dart_flags[i].c_str()); + for (size_t i = 0; i < settings.dart_flags.size(); i++) + args.push_back(settings.dart_flags[i].c_str()); char* flags_error = Dart_SetVMFlags(args.size(), args.data()); if (flags_error) { @@ -357,14 +374,14 @@ DartVM::DartVM(std::shared_ptr vm_data, DartUI::InitForGlobal(); + Dart_SetFileModifiedCallback(&DartFileModifiedCallback); + { TRACE_EVENT0("flutter", "Dart_Initialize"); Dart_InitializeParams params = {}; params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION; - params.vm_snapshot_data = - vm_data_->GetVMSnapshot().GetData()->GetSnapshotPointer(); - params.vm_snapshot_instructions = - vm_data_->GetVMSnapshot().GetInstructionsIfPresent(); + params.vm_snapshot_data = vm_snapshot_->GetData()->GetSnapshotPointer(); + params.vm_snapshot_instructions = vm_snapshot_->GetInstructionsIfPresent(); params.create = reinterpret_cast( DartIsolate::DartIsolateCreateCallback); params.shutdown = reinterpret_cast( @@ -396,58 +413,59 @@ DartVM::DartVM(std::shared_ptr vm_data, } } - Dart_SetFileModifiedCallback(&DartFileModifiedCallback); - // Allow streaming of stdout and stderr by the Dart vm. Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, &ServiceStreamCancelCallback); Dart_SetEmbedderInformationCallback(&EmbedderInformationCallback); - if (settings_.dart_library_sources_kernel != nullptr) { + if (settings.dart_library_sources_kernel != nullptr) { std::unique_ptr dart_library_sources = - settings_.dart_library_sources_kernel(); + settings.dart_library_sources_kernel(); // Set sources for dart:* libraries for debugging. Dart_SetDartLibrarySourcesKernel(dart_library_sources->GetMapping(), dart_library_sources->GetSize()); } - - FML_DLOG(INFO) << "New Dart VM instance created. Instance count: " - << gVMLaunchCount; } DartVM::~DartVM() { if (Dart_CurrentIsolate() != nullptr) { Dart_ExitIsolate(); } - char* result = Dart_Cleanup(); - - dart::bin::CleanupDartIo(); - - FML_CHECK(result == nullptr) - << "Could not cleanly shut down the Dart VM. Error: \"" << result - << "\"."; - free(result); - - FML_DLOG(INFO) << "Dart VM instance destroyed. Instance count: " - << gVMLaunchCount; -} - -std::shared_ptr DartVM::GetVMData() const { - return vm_data_; + if (result != nullptr) { + FML_LOG(ERROR) << "Could not cleanly shut down the Dart VM. Message: \"" + << result << "\"."; + free(result); + } } const Settings& DartVM::GetSettings() const { return settings_; } -std::shared_ptr DartVM::GetServiceProtocol() const { +const DartSnapshot& DartVM::GetVMSnapshot() const { + return *vm_snapshot_.get(); +} + +IsolateNameServer* DartVM::GetIsolateNameServer() { + return &isolate_name_server_; +} + +fml::RefPtr DartVM::GetIsolateSnapshot() const { + return isolate_snapshot_; +} + +fml::RefPtr DartVM::GetSharedSnapshot() const { + return shared_snapshot_; +} + +ServiceProtocol& DartVM::GetServiceProtocol() { return service_protocol_; } -std::shared_ptr DartVM::GetIsolateNameServer() const { - return isolate_name_server_; +fml::WeakPtr DartVM::GetWeakPtr() { + return weak_factory_.GetWeakPtr(); } } // namespace blink diff --git a/engine/src/flutter/runtime/dart_vm.h b/engine/src/flutter/runtime/dart_vm.h index 6724abde0b8..2c3ac0e3038 100644 --- a/engine/src/flutter/runtime/dart_vm.h +++ b/engine/src/flutter/runtime/dart_vm.h @@ -5,8 +5,9 @@ #ifndef FLUTTER_RUNTIME_DART_VM_H_ #define FLUTTER_RUNTIME_DART_VM_H_ -#include +#include #include +#include #include "flutter/common/settings.h" #include "flutter/fml/build_config.h" @@ -18,46 +19,57 @@ #include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" #include "flutter/runtime/dart_isolate.h" #include "flutter/runtime/dart_snapshot.h" -#include "flutter/runtime/dart_vm_data.h" #include "flutter/runtime/service_protocol.h" #include "third_party/dart/runtime/include/dart_api.h" namespace blink { -class DartVM { +class DartVM : public fml::RefCountedThreadSafe { public: - ~DartVM(); + static fml::RefPtr ForProcess(Settings settings); - static bool IsRunningPrecompiledCode(); - - static size_t GetVMLaunchCount(); - - const Settings& GetSettings() const; - - std::shared_ptr GetVMData() const; - - std::shared_ptr GetServiceProtocol() const; - - std::shared_ptr GetIsolateNameServer() const; - - private: - const Settings settings_; - std::shared_ptr vm_data_; - const std::shared_ptr isolate_name_server_; - const std::shared_ptr service_protocol_; - - friend class DartVMRef; - - static std::shared_ptr Create( + static fml::RefPtr ForProcess( Settings settings, fml::RefPtr vm_snapshot, fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, - std::shared_ptr isolate_name_server); + fml::RefPtr shared_snapshot); - DartVM(std::shared_ptr data, - std::shared_ptr isolate_name_server); + static fml::RefPtr ForProcessIfInitialized(); + static bool IsRunningPrecompiledCode(); + + const Settings& GetSettings() const; + + const DartSnapshot& GetVMSnapshot() const; + + IsolateNameServer* GetIsolateNameServer(); + + fml::RefPtr GetIsolateSnapshot() const; + + fml::RefPtr GetSharedSnapshot() const; + + fml::WeakPtr GetWeakPtr(); + + ServiceProtocol& GetServiceProtocol(); + + private: + const Settings settings_; + const fml::RefPtr vm_snapshot_; + IsolateNameServer isolate_name_server_; + const fml::RefPtr isolate_snapshot_; + const fml::RefPtr shared_snapshot_; + ServiceProtocol service_protocol_; + fml::WeakPtrFactory weak_factory_; + + DartVM(const Settings& settings, + fml::RefPtr vm_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot); + + ~DartVM(); + + FML_FRIEND_REF_COUNTED_THREAD_SAFE(DartVM); + FML_FRIEND_MAKE_REF_COUNTED(DartVM); FML_DISALLOW_COPY_AND_ASSIGN(DartVM); }; diff --git a/engine/src/flutter/runtime/dart_vm_data.cc b/engine/src/flutter/runtime/dart_vm_data.cc deleted file mode 100644 index a29074f79c9..00000000000 --- a/engine/src/flutter/runtime/dart_vm_data.cc +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/runtime/dart_vm_data.h" - -namespace blink { - -std::shared_ptr DartVMData::Create( - Settings settings, - fml::RefPtr vm_snapshot, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot) { - if (!vm_snapshot || !vm_snapshot->IsValid()) { - // Caller did not provide a valid VM snapshot. Attempt to infer one - // from the settings. - vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings); - if (!vm_snapshot) { - FML_LOG(ERROR) - << "VM snapshot invalid and could not be inferred from settings."; - return {}; - } - } - - if (!isolate_snapshot || !isolate_snapshot->IsValid()) { - // Caller did not provide a valid isolate snapshot. Attempt to infer one - // from the settings. - isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings); - if (!isolate_snapshot) { - FML_LOG(ERROR) << "Isolate snapshot invalid and could not be inferred " - "from settings."; - return {}; - } - } - - if (!shared_snapshot || !shared_snapshot->IsValid()) { - shared_snapshot = DartSnapshot::Empty(); - if (!shared_snapshot) { - FML_LOG(ERROR) << "Shared snapshot invalid."; - return {}; - } - } - - return std::shared_ptr(new DartVMData( - std::move(settings), // - std::move(vm_snapshot), // - std::move(isolate_snapshot), // - std::move(shared_snapshot) // - )); -} - -DartVMData::DartVMData(Settings settings, - fml::RefPtr vm_snapshot, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot) - : settings_(settings), - vm_snapshot_(vm_snapshot), - isolate_snapshot_(isolate_snapshot), - shared_snapshot_(shared_snapshot) {} - -DartVMData::~DartVMData() = default; - -const Settings& DartVMData::GetSettings() const { - return settings_; -} - -const DartSnapshot& DartVMData::GetVMSnapshot() const { - return *vm_snapshot_; -} - -fml::RefPtr DartVMData::GetIsolateSnapshot() const { - return isolate_snapshot_; -} - -fml::RefPtr DartVMData::GetSharedSnapshot() const { - return shared_snapshot_; -} - -} // namespace blink diff --git a/engine/src/flutter/runtime/dart_vm_data.h b/engine/src/flutter/runtime/dart_vm_data.h deleted file mode 100644 index 906cad1a4ca..00000000000 --- a/engine/src/flutter/runtime/dart_vm_data.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_RUNTIME_DART_VM_DATA_H_ -#define FLUTTER_RUNTIME_DART_VM_DATA_H_ - -#include "flutter/fml/macros.h" -#include "flutter/runtime/dart_snapshot.h" - -namespace blink { - -class DartVMData { - public: - static std::shared_ptr Create( - Settings settings, - fml::RefPtr vm_snapshot, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot); - - ~DartVMData(); - - const Settings& GetSettings() const; - - const DartSnapshot& GetVMSnapshot() const; - - fml::RefPtr GetIsolateSnapshot() const; - - fml::RefPtr GetSharedSnapshot() const; - - private: - const Settings settings_; - const fml::RefPtr vm_snapshot_; - const fml::RefPtr isolate_snapshot_; - const fml::RefPtr shared_snapshot_; - - DartVMData(Settings settings, - fml::RefPtr vm_snapshot, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot); - - FML_DISALLOW_COPY_AND_ASSIGN(DartVMData); -}; - -} // namespace blink - -#endif // FLUTTER_RUNTIME_DART_VM_DATA_H_ diff --git a/engine/src/flutter/runtime/dart_vm_lifecycle.cc b/engine/src/flutter/runtime/dart_vm_lifecycle.cc deleted file mode 100644 index 1ed86e0250c..00000000000 --- a/engine/src/flutter/runtime/dart_vm_lifecycle.cc +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/runtime/dart_vm_lifecycle.h" - -#include - -namespace blink { - -// We need to explicitly put the constructor and destructor of the DartVM in the -// critical section. All accesses (not just const members) to the global VM -// object weak pointer are behind this mutex. -static std::mutex gVMMutex; -static std::weak_ptr gVM FML_GUARDED_BY(gVMMutex); -static std::shared_ptr gVMLeak FML_GUARDED_BY(gVMMutex); - -// We are going to be modifying more than just the control blocks of the -// following weak pointers (in the |Create| case where an old VM could not be -// reused). Ideally, we would use |std::atomic>| specialization -// but that is only available since C++20. We don't expect contention on these -// locks so we just use one mutex for all. -static std::mutex gVMDependentsMutex; -static std::weak_ptr gVMData - FML_GUARDED_BY(gVMDependentsMutex); -static std::weak_ptr gVMServiceProtocol - FML_GUARDED_BY(gVMDependentsMutex); -static std::weak_ptr gVMIsolateNameServer - FML_GUARDED_BY(gVMDependentsMutex); - -DartVMRef::DartVMRef(std::shared_ptr vm) : vm_(vm) {} - -DartVMRef::DartVMRef(DartVMRef&& other) = default; - -DartVMRef::~DartVMRef() { - if (!vm_) { - // If there is no valid VM (possible via a move), there is no way that the - // decrement on the shared pointer can cause a collection. Avoid acquiring - // the lifecycle lock in this case. This is just working around a - // pessimization and not required for correctness. - return; - } - std::lock_guard lifecycle_lock(gVMMutex); - vm_.reset(); -} - -DartVMRef DartVMRef::Create(Settings settings, - fml::RefPtr vm_snapshot, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot) { - std::lock_guard lifecycle_lock(gVMMutex); - - // If there is already a running VM in the process, grab a strong reference to - // it. - if (auto vm = gVM.lock()) { - FML_DLOG(WARNING) << "Attempted to create a VM in a process where one was " - "already running. Ignoring arguments for current VM " - "create call and reusing the old VM."; - // There was already a running VM in the process, - return DartVMRef{std::move(vm)}; - } - - std::lock_guard dependents_lock(gVMDependentsMutex); - - gVMData.reset(); - gVMServiceProtocol.reset(); - gVMIsolateNameServer.reset(); - gVM.reset(); - - // If there is no VM in the process. Initialize one, hold the weak reference - // and pass a strong reference to the caller. - auto isolate_name_server = std::make_shared(); - auto vm = DartVM::Create(std::move(settings), // - std::move(vm_snapshot), // - std::move(isolate_snapshot), // - std::move(shared_snapshot), // - isolate_name_server // - ); - - if (!vm) { - FML_LOG(ERROR) << "Could not create Dart VM instance."; - return {nullptr}; - } - - gVMData = vm->GetVMData(); - gVMServiceProtocol = vm->GetServiceProtocol(); - gVMIsolateNameServer = isolate_name_server; - gVM = vm; - - if (settings.leak_vm) { - gVMLeak = vm; - } - - return DartVMRef{std::move(vm)}; -} - -bool DartVMRef::IsInstanceRunning() { - std::lock_guard lock(gVMMutex); - return !gVM.expired(); -} - -std::shared_ptr DartVMRef::GetVMData() { - std::lock_guard lock(gVMDependentsMutex); - return gVMData.lock(); -} - -std::shared_ptr DartVMRef::GetServiceProtocol() { - std::lock_guard lock(gVMDependentsMutex); - return gVMServiceProtocol.lock(); -} - -std::shared_ptr DartVMRef::GetIsolateNameServer() { - std::lock_guard lock(gVMDependentsMutex); - return gVMIsolateNameServer.lock(); -} - -} // namespace blink diff --git a/engine/src/flutter/runtime/dart_vm_lifecycle.h b/engine/src/flutter/runtime/dart_vm_lifecycle.h deleted file mode 100644 index cfe1f1b6464..00000000000 --- a/engine/src/flutter/runtime/dart_vm_lifecycle.h +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ -#define FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ - -#include - -#include "flutter/fml/macros.h" -#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" -#include "flutter/runtime/dart_vm.h" -#include "flutter/runtime/service_protocol.h" - -namespace blink { - -// A strong reference to the Dart VM. There can only be one VM running in the -// process at any given time. A reference to the VM may only be obtained via the -// |Create| method. In case there is already a running instance of the VM in the -// process, a strong reference to that VM is obtained and the arguments to the -// |Create| call ignored. If there is no VM already running in the process, a VM -// is initialized in a thread safe manner and returned to the caller. The VM -// will shutdown only when all callers relinquish their references (by -// collecting their instances of this class). -// -// DartVMRef instances may be created on any thread. -class DartVMRef { - public: - FML_WARN_UNUSED_RESULT - static DartVMRef Create(Settings settings, - fml::RefPtr vm_snapshot = nullptr, - fml::RefPtr isolate_snapshot = nullptr, - fml::RefPtr shared_snapshot = nullptr); - - DartVMRef(DartVMRef&&); - - ~DartVMRef(); - - // This is an inherently racy way to check if a VM instance is running and - // should not be used outside of unit-tests where there is a known threading - // model. - static bool IsInstanceRunning(); - - static std::shared_ptr GetVMData(); - - static std::shared_ptr GetServiceProtocol(); - - static std::shared_ptr GetIsolateNameServer(); - - operator bool() const { return static_cast(vm_); } - - DartVM* operator->() { - FML_DCHECK(vm_); - return vm_.get(); - } - - DartVM* operator&() { - FML_DCHECK(vm_); - return vm_.get(); - } - - private: - std::shared_ptr vm_; - - DartVMRef(std::shared_ptr vm); - - FML_DISALLOW_COPY_AND_ASSIGN(DartVMRef); -}; - -} // namespace blink - -#endif // FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ diff --git a/engine/src/flutter/runtime/dart_vm_unittests.cc b/engine/src/flutter/runtime/dart_vm_unittests.cc index 2c143432b5b..a0cfdfbbc5f 100644 --- a/engine/src/flutter/runtime/dart_vm_unittests.cc +++ b/engine/src/flutter/runtime/dart_vm_unittests.cc @@ -3,7 +3,6 @@ // found in the LICENSE file. #include "flutter/runtime/dart_vm.h" -#include "flutter/runtime/dart_vm_lifecycle.h" #include "gtest/gtest.h" namespace blink { @@ -12,17 +11,16 @@ TEST(DartVM, SimpleInitialization) { Settings settings = {}; settings.task_observer_add = [](intptr_t, fml::closure) {}; settings.task_observer_remove = [](intptr_t) {}; - auto vm = DartVMRef::Create(settings); + auto vm = DartVM::ForProcess(settings); ASSERT_TRUE(vm); + ASSERT_EQ(vm, DartVM::ForProcess(settings)); } TEST(DartVM, SimpleIsolateNameServer) { Settings settings = {}; settings.task_observer_add = [](intptr_t, fml::closure) {}; settings.task_observer_remove = [](intptr_t) {}; - auto vm = DartVMRef::Create(settings); - ASSERT_TRUE(vm); - ASSERT_TRUE(vm.GetVMData()); + auto vm = DartVM::ForProcess(settings); auto ns = vm->GetIsolateNameServer(); ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), ILLEGAL_PORT); ASSERT_FALSE(ns->RemoveIsolateNameMapping("foobar")); diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index e1aaf51de82..83a848e8795 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -17,8 +17,8 @@ namespace blink { RuntimeController::RuntimeController( RuntimeDelegate& p_client, DartVM* p_vm, - fml::RefPtr p_isolate_snapshot, - fml::RefPtr p_shared_snapshot, + fml::RefPtr p_isolate_snapshot, + fml::RefPtr p_shared_snapshot, TaskRunners p_task_runners, fml::WeakPtr p_snapshot_delegate, fml::WeakPtr p_io_manager, @@ -40,8 +40,8 @@ RuntimeController::RuntimeController( RuntimeController::RuntimeController( RuntimeDelegate& p_client, DartVM* p_vm, - fml::RefPtr p_isolate_snapshot, - fml::RefPtr p_shared_snapshot, + fml::RefPtr p_isolate_snapshot, + fml::RefPtr p_shared_snapshot, TaskRunners p_task_runners, fml::WeakPtr p_snapshot_delegate, fml::WeakPtr p_io_manager, @@ -61,7 +61,7 @@ RuntimeController::RuntimeController( idle_notification_callback_(idle_notification_callback), window_data_(std::move(p_window_data)), root_isolate_( - DartIsolate::CreateRootIsolate(vm_->GetVMData()->GetSettings(), + DartIsolate::CreateRootIsolate(vm_, isolate_snapshot_, shared_snapshot_, task_runners_, diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index 79d020ed383..03f1389d8c6 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -30,8 +30,8 @@ class RuntimeController final : public WindowClient { public: RuntimeController(RuntimeDelegate& client, DartVM* vm, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr snapshot_delegate, fml::WeakPtr io_manager, @@ -121,8 +121,8 @@ class RuntimeController final : public WindowClient { RuntimeDelegate& client_; DartVM* const vm_; - fml::RefPtr isolate_snapshot_; - fml::RefPtr shared_snapshot_; + fml::RefPtr isolate_snapshot_; + fml::RefPtr shared_snapshot_; TaskRunners task_runners_; fml::WeakPtr snapshot_delegate_; fml::WeakPtr io_manager_; @@ -135,8 +135,8 @@ class RuntimeController final : public WindowClient { RuntimeController(RuntimeDelegate& client, DartVM* vm, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, TaskRunners task_runners, fml::WeakPtr snapshot_delegate, fml::WeakPtr io_manager, diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index f1c215570df..5b2d5a5c3dd 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -37,8 +37,8 @@ static constexpr char kSettingsChannel[] = "flutter/settings"; Engine::Engine(Delegate& delegate, blink::DartVM& vm, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, blink::TaskRunners task_runners, blink::Settings settings, std::unique_ptr animator, diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 22160cb7ee3..b390b36a2b1 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -56,8 +56,8 @@ class Engine final : public blink::RuntimeDelegate { Engine(Delegate& delegate, blink::DartVM& vm, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::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 3c824ae8e0c..4d2dcc0d137 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -36,20 +36,17 @@ namespace shell { constexpr char kSkiaChannel[] = "flutter/skia"; std::unique_ptr Shell::CreateShellOnPlatformThread( - blink::DartVMRef vm, blink::TaskRunners task_runners, blink::Settings settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, Shell::CreateCallback on_create_rasterizer) { if (!task_runners.IsValid()) { - FML_LOG(ERROR) << "Task runners to run the shell were invalid."; return nullptr; } - auto shell = - std::unique_ptr(new Shell(std::move(vm), task_runners, settings)); + auto shell = std::unique_ptr(new Shell(task_runners, settings)); // Create the platform view on the platform thread (this thread). auto platform_view = on_create_platform_view(*shell.get()); @@ -78,7 +75,6 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( &platform_view, // io_task_runner // ]() { - TRACE_EVENT0("flutter", "ShellSetupIOSubsystem"); io_manager = std::make_unique( platform_view->CreateResourceContext(), io_task_runner); io_latch.Signal(); @@ -96,7 +92,6 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( shell = shell.get(), // &snapshot_delegate // ]() { - TRACE_EVENT0("flutter", "ShellSetupGPUSubsystem"); if (auto new_rasterizer = on_create_rasterizer(*shell)) { rasterizer = std::move(new_rasterizer); snapshot_delegate = rasterizer->GetSnapshotDelegate(); @@ -120,7 +115,6 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( snapshot_delegate = std::move(snapshot_delegate), // io_manager = io_manager->GetWeakPtr() // ]() mutable { - TRACE_EVENT0("flutter", "ShellSetupUISubsystem"); const auto& task_runners = shell->GetTaskRunners(); // The animator is owned by the UI thread but it gets its vsync pulses @@ -129,7 +123,7 @@ std::unique_ptr Shell::CreateShellOnPlatformThread( std::move(vsync_waiter)); engine = std::make_unique(*shell, // - *shell->GetDartVM(), // + shell->GetDartVM(), // std::move(isolate_snapshot), // std::move(shared_snapshot), // task_runners, // @@ -212,35 +206,26 @@ std::unique_ptr Shell::Create( Shell::CreateCallback on_create_rasterizer) { PerformInitializationTasks(settings); - TRACE_EVENT0("flutter", "Shell::Create"); - - auto vm = blink::DartVMRef::Create(settings); + auto vm = blink::DartVM::ForProcess(settings); FML_CHECK(vm) << "Must be able to initialize the VM."; - - auto vm_data = vm->GetVMData(); - return Shell::Create(std::move(task_runners), // std::move(settings), // - vm_data->GetIsolateSnapshot(), // isolate snapshot - blink::DartSnapshot::Empty(), // shared snapshot + vm->GetIsolateSnapshot(), // + blink::DartSnapshot::Empty(), // std::move(on_create_platform_view), // - std::move(on_create_rasterizer), // - std::move(vm) // + std::move(on_create_rasterizer) // ); } std::unique_ptr Shell::Create( blink::TaskRunners task_runners, blink::Settings settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, - Shell::CreateCallback on_create_rasterizer, - blink::DartVMRef vm) { + Shell::CreateCallback on_create_rasterizer) { PerformInitializationTasks(settings); - TRACE_EVENT0("flutter", "Shell::CreateWithSnapshots"); - if (!task_runners.IsValid() || !on_create_platform_view || !on_create_rasterizer) { return nullptr; @@ -250,18 +235,16 @@ std::unique_ptr Shell::Create( std::unique_ptr shell; fml::TaskRunner::RunNowOrPostTask( task_runners.GetPlatformTaskRunner(), - fml::MakeCopyable([&latch, // - vm = std::move(vm), // - &shell, // - 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 // - ]() mutable { - shell = CreateShellOnPlatformThread(std::move(vm), - std::move(task_runners), // + [&latch, // + &shell, // + 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), // @@ -269,18 +252,15 @@ std::unique_ptr Shell::Create( on_create_rasterizer // ); latch.Signal(); - })); + }); latch.Wait(); return shell; } -Shell::Shell(blink::DartVMRef vm, - blink::TaskRunners task_runners, - blink::Settings settings) +Shell::Shell(blink::TaskRunners task_runners, blink::Settings settings) : task_runners_(std::move(task_runners)), settings_(std::move(settings)), - vm_(std::move(vm)) { - FML_CHECK(vm_) << "Must have access to VM to create a shell."; + vm_(blink::DartVM::ForProcess(settings_)) { FML_DCHECK(task_runners_.IsValid()); FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); @@ -322,7 +302,9 @@ Shell::~Shell() { PersistentCache::GetCacheForProcess()->RemoveWorkerTaskRunner( task_runners_.GetIOTaskRunner()); - vm_->GetServiceProtocol()->RemoveHandler(this); + if (auto vm = blink::DartVM::ForProcessIfInitialized()) { + vm->GetServiceProtocol().RemoveHandler(this); + } fml::AutoResetWaitableEvent ui_latch, gpu_latch, platform_latch, io_latch; @@ -393,7 +375,9 @@ bool Shell::Setup(std::unique_ptr platform_view, is_setup_ = true; - vm_->GetServiceProtocol()->AddHandler(this, GetServiceProtocolDescription()); + if (auto vm = blink::DartVM::ForProcessIfInitialized()) { + vm->GetServiceProtocol().AddHandler(this, GetServiceProtocolDescription()); + } PersistentCache::GetCacheForProcess()->AddWorkerTaskRunner( task_runners_.GetIOTaskRunner()); @@ -427,13 +411,12 @@ fml::WeakPtr Shell::GetPlatformView() { return platform_view_->GetWeakPtr(); } -blink::DartVM* Shell::GetDartVM() { - return &vm_; +blink::DartVM& Shell::GetDartVM() const { + return *vm_; } // |shell::PlatformView::Delegate| void Shell::OnPlatformViewCreated(std::unique_ptr surface) { - TRACE_EVENT0("flutter", "Shell::OnPlatformViewCreated"); FML_DCHECK(is_setup_); FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); @@ -517,7 +500,6 @@ void Shell::OnPlatformViewCreated(std::unique_ptr surface) { // |shell::PlatformView::Delegate| void Shell::OnPlatformViewDestroyed() { - TRACE_EVENT0("flutter", "Shell::OnPlatformViewDestroyed"); FML_DCHECK(is_setup_); FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); @@ -878,8 +860,10 @@ void Shell::OnPreEngineRestart() { // |shell::Engine::Delegate| void Shell::UpdateIsolateDescription(const std::string isolate_name, int64_t isolate_port) { - Handler::Description description(isolate_port, isolate_name); - vm_->GetServiceProtocol()->SetHandlerDescription(this, description); + if (auto vm = blink::DartVM::ForProcessIfInitialized()) { + Handler::Description description(isolate_port, isolate_name); + vm->GetServiceProtocol().SetHandlerDescription(this, description); + } } // |blink::ServiceProtocol::Handler| diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 864235473c9..17b0f170561 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -23,7 +23,6 @@ #include "flutter/lib/ui/semantics/custom_accessibility_action.h" #include "flutter/lib/ui/semantics/semantics_node.h" #include "flutter/lib/ui/window/platform_message.h" -#include "flutter/runtime/dart_vm_lifecycle.h" #include "flutter/runtime/service_protocol.h" #include "flutter/shell/common/animator.h" #include "flutter/shell/common/engine.h" @@ -55,11 +54,10 @@ class Shell final : public PlatformView::Delegate, static std::unique_ptr Create( blink::TaskRunners task_runners, blink::Settings settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, CreateCallback on_create_platform_view, - CreateCallback on_create_rasterizer, - blink::DartVMRef vm); + CreateCallback on_create_rasterizer); ~Shell(); @@ -73,7 +71,7 @@ class Shell final : public PlatformView::Delegate, fml::WeakPtr GetPlatformView(); - blink::DartVM* GetDartVM(); + blink::DartVM& GetDartVM() const; bool IsSetup() const; @@ -87,7 +85,7 @@ class Shell final : public PlatformView::Delegate, const blink::TaskRunners task_runners_; const blink::Settings settings_; - blink::DartVMRef vm_; + fml::RefPtr vm_; std::unique_ptr platform_view_; // on platform task runner std::unique_ptr engine_; // on UI task runner std::unique_ptr rasterizer_; // on GPU task runner @@ -100,19 +98,16 @@ class Shell final : public PlatformView::Delegate, > service_protocol_handlers_; bool is_setup_ = false; + uint64_t next_pointer_flow_id_ = 0; Shell(blink::TaskRunners task_runners, blink::Settings settings); - Shell(blink::DartVMRef vm, - blink::TaskRunners task_runners, - blink::Settings settings); static std::unique_ptr CreateShellOnPlatformThread( - blink::DartVMRef vm, blink::TaskRunners task_runners, blink::Settings settings, - fml::RefPtr isolate_snapshot, - fml::RefPtr shared_snapshot, + fml::RefPtr isolate_snapshot, + fml::RefPtr shared_snapshot, Shell::CreateCallback on_create_platform_view, Shell::CreateCallback on_create_rasterizer); diff --git a/engine/src/flutter/testing/testing.cc b/engine/src/flutter/testing/testing.cc index c7382195414..93e135e7ab4 100644 --- a/engine/src/flutter/testing/testing.cc +++ b/engine/src/flutter/testing/testing.cc @@ -6,8 +6,6 @@ namespace testing { -std::string GetCurrentTestName() { - return UnitTest::GetInstance()->current_test_info()->name(); -} +// } // namespace testing diff --git a/engine/src/flutter/testing/testing.h b/engine/src/flutter/testing/testing.h index 0662055fea5..dac2d229b91 100644 --- a/engine/src/flutter/testing/testing.h +++ b/engine/src/flutter/testing/testing.h @@ -5,8 +5,6 @@ #ifndef TESTING_TESTING_H_ #define TESTING_TESTING_H_ -#include - #include "gtest/gtest.h" namespace testing { @@ -16,8 +14,6 @@ namespace testing { // error. const char* GetFixturesPath(); -std::string GetCurrentTestName(); - } // namespace testing #endif // TESTING_TESTING_H_