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

This commit is contained in:
Kan-Ru Chen 2015-05-20 15:45:41 +08:00
parent 93b42a9fee
commit ebdea08bb3
2 changed files with 53 additions and 0 deletions

View File

@ -75,6 +75,7 @@ NS_IMPL_ISUPPORTS_INHERITED0(PuppetWidget, nsBaseWidget)
PuppetWidget::PuppetWidget(TabChild* aTabChild)
: mTabChild(aTabChild)
, mMemoryPressureObserver(nullptr)
, mDPI(-1)
, mDefaultScale(-1)
, mNativeKeyCommandsValid(false)
@ -120,6 +121,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;
}
@ -155,6 +161,10 @@ PuppetWidget::Destroy()
Base::OnDestroy();
Base::Destroy();
mPaintTask.Revoke();
if (mMemoryPressureObserver) {
mMemoryPressureObserver->Remove();
}
mMemoryPressureObserver = nullptr;
mChild = nullptr;
if (mLayerManager) {
mLayerManager->Destroy();
@ -1083,6 +1093,36 @@ 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 &&
XRE_GetProcessType() == GeckoProcessType_Content) {
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

@ -279,6 +279,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
@ -291,6 +303,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;