[Fuchsia] Notify Shell when memory becomes low. (flutter/engine#28821)

This commit is contained in:
Alexander Biggs
2021-09-24 15:23:01 -04:00
committed by GitHub
parent 8ad5aa9031
commit 6dd9777440
7 changed files with 79 additions and 9 deletions

View File

@@ -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",

View File

@@ -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<int>(latest_memory_pressure_level_) << " to "
<< static_cast<int>(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

View File

@@ -9,6 +9,7 @@
#include <fuchsia/intl/cpp/fidl.h>
#include <fuchsia/io/cpp/fidl.h>
#include <fuchsia/memorypressure/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl.h>
#include <fuchsia/ui/gfx/cpp/fidl.h>
#include <fuchsia/ui/views/cpp/fidl.h>
@@ -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<AccessibilityBridge> accessibility_bridge_;
fuchsia::intl::PropertyProviderPtr intl_property_provider_;
fuchsia::memorypressure::ProviderPtr memory_pressure_provider_;
fidl::Binding<fuchsia::memorypressure::Watcher>
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<flutter::ExternalViewEmbedder> GetExternalViewEmbedder();
std::unique_ptr<flutter::Surface> CreateSurface();

View File

@@ -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"
]
}
}
}

View File

@@ -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"
]
}
}
}

View File

@@ -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"
]
}
}
}

View File

@@ -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"
]
}
}
}