Purge caches on low memory on iOS (#9491)

This commit is contained in:
Dan Field 2019-06-25 20:47:55 -07:00 committed by GitHub
parent 3122ecc62e
commit da82361dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 11 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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_;
}

View File

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

View File

@ -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 {

View File

@ -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 {