Bug 1154231 - Part 1. Reclaim cached resources when memory-pressure occurs. r=mattwoodrow

This commit is contained in:
Kan-Ru Chen 2015-04-29 11:54:04 +08:00
parent 1af068a3cc
commit 32816c108b
2 changed files with 50 additions and 4 deletions

View File

@ -118,6 +118,11 @@ PuppetWidget::Create(nsIWidget *aParent,
else {
Resize(mBounds.x, mBounds.y, mBounds.width, mBounds.height, false);
}
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
mMemoryPressureObserver = new MemoryPressureObserver(this);
obs->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
}
return NS_OK;
}
@ -153,6 +158,9 @@ PuppetWidget::Destroy()
Base::OnDestroy();
Base::Destroy();
mPaintTask.Revoke();
if (mMemoryPressureObserver) {
mMemoryPressureObserver->Remove();
}
mChild = nullptr;
if (mLayerManager) {
mLayerManager->Destroy();
@ -175,10 +183,6 @@ PuppetWidget::Show(bool aState)
mChild->mVisible = aState;
}
if (!mVisible && mLayerManager) {
mLayerManager->ClearCachedResources();
}
if (!wasVisible && mVisible) {
Resize(mBounds.width, mBounds.height, false);
Invalidate(mBounds);
@ -1030,6 +1034,35 @@ PuppetWidget::PaintTask::Run()
return NS_OK;
}
NS_IMPL_ISUPPORTS(PuppetWidget::MemoryPressureObserver, nsIObserver)
NS_IMETHODIMP
PuppetWidget::MemoryPressureObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData)
{
if (!mWidget) {
return NS_OK;
}
if (strcmp("memory-pressure", aTopic) == 0) {
if (!mWidget->mVisible && mWidget->mLayerManager) {
mWidget->mLayerManager->ClearCachedResources();
}
}
return NS_OK;
}
void
PuppetWidget::MemoryPressureObserver::Remove()
{
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, "memory-pressure");
}
mWidget = nullptr;
}
bool
PuppetWidget::NeedsPaint()
{

View File

@ -280,6 +280,18 @@ private:
PuppetWidget* mWidget;
};
class MemoryPressureObserver : public nsIObserver {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
explicit MemoryPressureObserver(PuppetWidget* aWidget) : mWidget(aWidget) {}
void Remove();
private:
virtual ~MemoryPressureObserver() {}
PuppetWidget* mWidget;
};
friend class MemoryPressureObserver;
// TabChild normally holds a strong reference to this PuppetWidget
// or its root ancestor, but each PuppetWidget also needs a
// reference back to TabChild (e.g. to delegate nsIWidget IME calls
@ -292,6 +304,7 @@ private:
nsRefPtr<PuppetWidget> mChild;
nsIntRegion mDirtyRegion;
nsRevocableEventPtr<PaintTask> mPaintTask;
nsRefPtr<MemoryPressureObserver> mMemoryPressureObserver;
// XXX/cjones: keeping this around until we teach LayerManager to do
// retained-content-only transactions
mozilla::RefPtr<DrawTarget> mDrawTarget;