未验证 提交 da82361d 编写于 作者: D Dan Field 提交者: GitHub

Purge caches on low memory on iOS (#9491)

上级 3122ecc6
......@@ -68,6 +68,19 @@ void Rasterizer::Teardown() {
last_layer_tree_.reset();
}
void Rasterizer::NotifyLowMemoryWarning() const {
if (!surface_) {
FML_DLOG(INFO) << "Rasterizer::PurgeCaches called with no surface.";
return;
}
auto context = surface_->GetContext();
if (!context) {
FML_DLOG(INFO) << "Rasterizer::PurgeCaches called with no GrContext.";
return;
}
context->freeGpuResources();
}
flutter::TextureRegistry* Rasterizer::GetTextureRegistry() {
return &compositor_context_->texture_registry();
}
......
......@@ -46,6 +46,11 @@ class Rasterizer final : public SnapshotDelegate {
void Teardown();
// Frees up Skia GPU resources.
//
// This method must be called from the GPU task runner.
void NotifyLowMemoryWarning() const;
fml::WeakPtr<Rasterizer> GetWeakPtr() const;
fml::WeakPtr<SnapshotDelegate> GetSnapshotDelegate() const;
......
......@@ -369,6 +369,17 @@ Shell::~Shell() {
platform_latch.Wait();
}
void Shell::NotifyLowMemoryWarning() const {
task_runners_.GetGPUTaskRunner()->PostTask(
[rasterizer = rasterizer_->GetWeakPtr()]() {
if (rasterizer) {
rasterizer->NotifyLowMemoryWarning();
}
});
// The IO Manager uses resource cache limits of 0, so it is not necessary
// to purge them.
}
bool Shell::IsSetup() const {
return is_setup_;
}
......
......@@ -78,6 +78,13 @@ class Shell final : public PlatformView::Delegate,
DartVM* GetDartVM();
// Embedders should call this under low memory conditions to free up
// internal caches used.
//
// This method posts a task to the GPU threads to signal the Rasterizer to
// free resources.
void NotifyLowMemoryWarning() const;
bool IsSetup() const;
Rasterizer::Screenshot Screenshot(Rasterizer::ScreenshotType type,
......
......@@ -93,11 +93,21 @@
[self setupChannels];
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(onMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
return self;
}
- (void)dealloc {
[_pluginPublications release];
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[super dealloc];
}
......@@ -574,6 +584,15 @@
return _pluginPublications[pluginKey];
}
#pragma mark - Memory Notifications
- (void)onMemoryWarning:(NSNotification*)notification {
if (_shell) {
_shell->NotifyLowMemoryWarning();
}
[_systemChannel sendMessage:@{@"type" : @"memoryPressure"}];
}
@end
@implementation FlutterEngineRegistrar {
......
......@@ -208,11 +208,6 @@ NSNotificationName const FlutterSemanticsUpdateNotification = @"FlutterSemantics
name:UIAccessibilityBoldTextStatusDidChangeNotification
object:nil];
[center addObserver:self
selector:@selector(onMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[center addObserver:self
selector:@selector(onUserSettingsChanged:)
name:UIContentSizeCategoryDidChangeNotification
......@@ -794,12 +789,6 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
#endif
}
#pragma mark - Memory Notifications
- (void)onMemoryWarning:(NSNotification*)notification {
[[_engine.get() systemChannel] sendMessage:@{@"type" : @"memoryPressure"}];
}
#pragma mark - Locale updates
- (void)onLocaleUpdated:(NSNotification*)notification {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册