Bug 1042305 - Increase ImageCache size and add MemoryPressure support r=gw280

This commit is contained in:
Sotaro Ikeda 2014-08-25 12:01:46 -07:00
parent ff234cdc98
commit fa87ed9a17
2 changed files with 88 additions and 13 deletions

View File

@ -54,7 +54,7 @@ pref("browser.cache.memory_limit", 2048); // 2 MB
/* image cache prefs */
pref("image.cache.size", 1048576); // bytes
pref("image.high_quality_downscaling.enabled", false);
pref("canvas.image.cache.limit", 10485760); // 10 MB
pref("canvas.image.cache.limit", 20971520); // 20 MB
/* offline cache prefs */
pref("browser.offline-apps.notify", false);

View File

@ -85,22 +85,14 @@ public:
static bool sPrefsInitialized = false;
static int32_t sCanvasImageCacheLimit = 0;
class ImageCacheObserver;
class ImageCache MOZ_FINAL : public nsExpirationTracker<ImageCacheEntryData,4> {
public:
// We use 3 generations of 1 second each to get a 2-3 seconds timeout.
enum { GENERATION_MS = 1000 };
ImageCache()
: nsExpirationTracker<ImageCacheEntryData,4>(GENERATION_MS)
, mTotal(0)
{
if (!sPrefsInitialized) {
sPrefsInitialized = true;
Preferences::AddIntVarCache(&sCanvasImageCacheLimit, "canvas.image.cache.limit", 0);
}
}
~ImageCache() {
AgeAllGenerations();
}
ImageCache();
~ImageCache();
virtual void NotifyExpired(ImageCacheEntryData* aObject)
{
@ -112,10 +104,76 @@ public:
nsTHashtable<ImageCacheEntry> mCache;
size_t mTotal;
nsRefPtr<ImageCacheObserver> mImageCacheObserver;
};
static ImageCache* gImageCache = nullptr;
// Listen memory-pressure event for image cache purge
class ImageCacheObserver MOZ_FINAL : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
ImageCacheObserver(ImageCache* aImageCache)
: mImageCache(aImageCache)
{
RegisterMemoryPressureEvent();
}
void Destroy()
{
UnregisterMemoryPressureEvent();
mImageCache = nullptr;
}
NS_IMETHODIMP Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aSomeData)
{
if (!mImageCache || strcmp(aTopic, "memory-pressure")) {
return NS_OK;
}
mImageCache->AgeAllGenerations();
return NS_OK;
}
private:
virtual ~ImageCacheObserver()
{
}
void RegisterMemoryPressureEvent()
{
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
MOZ_ASSERT(observerService);
if (observerService) {
observerService->AddObserver(this, "memory-pressure", false);
}
}
void UnregisterMemoryPressureEvent()
{
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
// Do not assert on observerService here. This might be triggered by
// the cycle collector at a late enough time, that XPCOM services are
// no longer available. See bug 1029504.
if (observerService) {
observerService->RemoveObserver(this, "memory-pressure");
}
}
ImageCache* mImageCache;
};
NS_IMPL_ISUPPORTS(ImageCacheObserver, nsIObserver)
class CanvasImageCacheShutdownObserver MOZ_FINAL : public nsIObserver
{
~CanvasImageCacheShutdownObserver() {}
@ -124,6 +182,23 @@ public:
NS_DECL_NSIOBSERVER
};
ImageCache::ImageCache()
: nsExpirationTracker<ImageCacheEntryData,4>(GENERATION_MS)
, mTotal(0)
{
if (!sPrefsInitialized) {
sPrefsInitialized = true;
Preferences::AddIntVarCache(&sCanvasImageCacheLimit, "canvas.image.cache.limit", 0);
}
mImageCacheObserver = new ImageCacheObserver(this);
MOZ_RELEASE_ASSERT(mImageCacheObserver, "Can't alloc ImageCacheObserver");
}
ImageCache::~ImageCache() {
AgeAllGenerations();
mImageCacheObserver->Destroy();
}
void
CanvasImageCache::NotifyDrawImage(Element* aImage,
HTMLCanvasElement* aCanvas,