diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn index 0a25f697c0..40677a962e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn @@ -138,6 +138,7 @@ template("runner_sources") { "$fuchsia_sdk_root/fidl:fuchsia.images", "$fuchsia_sdk_root/fidl:fuchsia.intl", "$fuchsia_sdk_root/fidl:fuchsia.io", + "$fuchsia_sdk_root/fidl:fuchsia.memorypressure", "$fuchsia_sdk_root/fidl:fuchsia.sys", "$fuchsia_sdk_root/fidl:fuchsia.ui.app", "$fuchsia_sdk_root/fidl:fuchsia.ui.composition", diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index 5c9c34fdc4..95d4624b0e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -65,6 +65,8 @@ Engine::Engine(Delegate& delegate, thread_host_(CreateThreadHost(thread_label_)), view_token_(std::move(view_token)), view_ref_pair_(std::move(view_ref_pair)), + memory_pressure_watcher_binding_(this), + latest_memory_pressure_level_(fuchsia::memorypressure::Level::NORMAL), intercept_all_input_(product_config.get_intercept_all_input()), weak_factory_(this) { Initialize(/*=use_flatland*/ false, std::move(svc), @@ -88,6 +90,8 @@ Engine::Engine(Delegate& delegate, thread_host_(CreateThreadHost(thread_label_)), view_creation_token_(std::move(view_creation_token)), view_ref_pair_(std::move(view_ref_pair)), + memory_pressure_watcher_binding_(this), + latest_memory_pressure_level_(fuchsia::memorypressure::Level::NORMAL), intercept_all_input_(product_config.get_intercept_all_input()), weak_factory_(this) { Initialize(/*=use_flatland*/ true, std::move(svc), std::move(runner_services), @@ -498,6 +502,34 @@ void Engine::Initialize( // notification. Fire one eagerly. shell_->GetPlatformView()->NotifyCreated(); + // Connect to the memory pressure provider. If the connection fails, the + // initialization of the engine will simply proceed, printing a warning + // message. The engine will be fully functional, except that the Flutter + // shell will not be notified when memory is low. + { + memory_pressure_provider_.set_error_handler([](zx_status_t status) { + FML_LOG(WARNING) + << "Failed to connect to " << fuchsia::memorypressure::Provider::Name_ + << ": " << zx_status_get_string(status) + << " This is not a fatal error, but the heap will not be " + << " compacted when memory is low."; + }); + + // Note that we're using the runner's services, not the component's. + // The Flutter Shell should be notified when memory is low regardless of + // whether the component has direct access to the + // fuchsia.memorypressure.Provider service. + ZX_ASSERT(runner_services->Connect( + memory_pressure_provider_.NewRequest()) == ZX_OK); + + FML_VLOG(-1) << "Registering memorypressure watcher"; + + // Register for changes, which will make the request for the initial + // memory level. + memory_pressure_provider_->RegisterWatcher( + memory_pressure_watcher_binding_.NewBinding()); + } + // Connect to the intl property provider. If the connection fails, the // initialization of the engine will simply proceed, printing a warning // message. The engine will be fully functional, except that the user's @@ -517,10 +549,9 @@ void Engine::Initialize( ZX_ASSERT(runner_services->Connect(intl_property_provider_.NewRequest()) == ZX_OK); - auto get_profile_callback = [flutter_runner_engine = - weak_factory_.GetWeakPtr()]( + auto get_profile_callback = [weak = weak_factory_.GetWeakPtr()]( const fuchsia::intl::Profile& profile) { - if (!flutter_runner_engine) { + if (!weak) { return; } if (!profile.has_locales()) { @@ -528,7 +559,7 @@ void Engine::Initialize( } auto message = MakeLocalizationPlatformMessage(profile); FML_VLOG(-1) << "Sending LocalizationPlatformMessage"; - flutter_runner_engine->shell_->GetPlatformView()->DispatchPlatformMessage( + weak->shell_->GetPlatformView()->DispatchPlatformMessage( std::move(message)); }; @@ -822,4 +853,26 @@ void Engine::WarmupSkps( }); } +void Engine::OnLevelChanged( + fuchsia::memorypressure::Level level, + fuchsia::memorypressure::Watcher::OnLevelChangedCallback callback) { + // The callback must be invoked immediately to acknowledge the message. + // This is the "Throttle push using acknowledgements" pattern: + // https://fuchsia.dev/fuchsia-src/concepts/api/fidl#throttle_push_using_acknowledgements + callback(); + + FML_LOG(WARNING) << "memorypressure watcher: OnLevelChanged from " + << static_cast(latest_memory_pressure_level_) << " to " + << static_cast(level); + + if (latest_memory_pressure_level_ == fuchsia::memorypressure::Level::NORMAL && + (level == fuchsia::memorypressure::Level::WARNING || + level == fuchsia::memorypressure::Level::CRITICAL)) { + FML_LOG(WARNING) + << "memorypressure watcher: notifying Flutter that memory is low"; + shell_->NotifyLowMemoryWarning(); + } + latest_memory_pressure_level_ = level; +} + } // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h index ddf84eac13..2197baa3c4 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -41,7 +42,7 @@ class EngineTest; // Represents an instance of running Flutter engine along with the threads // that host the same. -class Engine final { +class Engine final : public fuchsia::memorypressure::Watcher { public: class Delegate { public: @@ -107,6 +108,12 @@ class Engine final { std::unique_ptr accessibility_bridge_; fuchsia::intl::PropertyProviderPtr intl_property_provider_; + fuchsia::memorypressure::ProviderPtr memory_pressure_provider_; + fidl::Binding + memory_pressure_watcher_binding_; + // We need to track the latest memory pressure level to determine + // the direction of change when a new level is provided. + fuchsia::memorypressure::Level latest_memory_pressure_level_; zx::event vsync_event_; @@ -151,6 +158,11 @@ class Engine final { bool focusable); void DestroyView(int64_t view_id, ViewIdCallback on_view_unbound); + // |fuchsia::memorypressure::Watcher| + void OnLevelChanged(fuchsia::memorypressure::Level level, + fuchsia::memorypressure::Watcher::OnLevelChangedCallback + callback) override; + std::shared_ptr GetExternalViewEmbedder(); std::unique_ptr CreateSurface(); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx index b22290a321..dd8aa536d9 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_product_runner.cmx @@ -18,6 +18,7 @@ "fuchsia.fonts.Provider", "fuchsia.intl.PropertyProvider", "fuchsia.logger.LogSink", + "fuchsia.memorypressure.Provider", "fuchsia.net.name.Lookup", "fuchsia.posix.socket.Provider", "fuchsia.sysmem.Allocator", @@ -28,4 +29,4 @@ "fuchsia.vulkan.loader.Loader" ] } -} \ No newline at end of file +} diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx index b22290a321..dd8aa536d9 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_aot_runner.cmx @@ -18,6 +18,7 @@ "fuchsia.fonts.Provider", "fuchsia.intl.PropertyProvider", "fuchsia.logger.LogSink", + "fuchsia.memorypressure.Provider", "fuchsia.net.name.Lookup", "fuchsia.posix.socket.Provider", "fuchsia.sysmem.Allocator", @@ -28,4 +29,4 @@ "fuchsia.vulkan.loader.Loader" ] } -} \ No newline at end of file +} diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_product_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_product_runner.cmx index efaeff479e..e67218d9f1 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_product_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_product_runner.cmx @@ -19,6 +19,7 @@ "fuchsia.fonts.Provider", "fuchsia.intl.PropertyProvider", "fuchsia.logger.LogSink", + "fuchsia.memorypressure.Provider", "fuchsia.net.name.Lookup", "fuchsia.posix.socket.Provider", "fuchsia.sysmem.Allocator", @@ -29,4 +30,4 @@ "fuchsia.vulkan.loader.Loader" ] } -} \ No newline at end of file +} diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_runner.cmx b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_runner.cmx index efaeff479e..e67218d9f1 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_runner.cmx +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/meta/flutter_jit_runner.cmx @@ -19,6 +19,7 @@ "fuchsia.fonts.Provider", "fuchsia.intl.PropertyProvider", "fuchsia.logger.LogSink", + "fuchsia.memorypressure.Provider", "fuchsia.net.name.Lookup", "fuchsia.posix.socket.Provider", "fuchsia.sysmem.Allocator", @@ -29,4 +30,4 @@ "fuchsia.vulkan.loader.Loader" ] } -} \ No newline at end of file +}