2012-02-05 12:23:08 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
#include "CanvasImageCache.h"
|
|
|
|
#include "nsIImageLoadingContent.h"
|
|
|
|
#include "nsExpirationTracker.h"
|
|
|
|
#include "imgIRequest.h"
|
2012-06-30 05:53:21 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2012-02-05 12:23:08 +00:00
|
|
|
#include "nsTHashtable.h"
|
2013-01-04 05:16:14 +00:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2012-02-05 12:23:08 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-04-26 14:16:12 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-12-11 21:05:27 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-11-04 13:00:36 +00:00
|
|
|
#include "gfx2DGlue.h"
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-06-30 05:53:21 +00:00
|
|
|
using namespace dom;
|
2013-12-11 21:05:27 +00:00
|
|
|
using namespace gfx;
|
2012-06-30 05:53:21 +00:00
|
|
|
|
2012-02-05 12:23:08 +00:00
|
|
|
struct ImageCacheKey {
|
2013-01-04 05:16:14 +00:00
|
|
|
ImageCacheKey(Element* aImage, HTMLCanvasElement* aCanvas)
|
2012-02-05 12:23:08 +00:00
|
|
|
: mImage(aImage), mCanvas(aCanvas) {}
|
2012-06-30 05:53:21 +00:00
|
|
|
Element* mImage;
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement* mCanvas;
|
2012-02-05 12:23:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ImageCacheEntryData {
|
|
|
|
ImageCacheEntryData(const ImageCacheEntryData& aOther)
|
|
|
|
: mImage(aOther.mImage)
|
|
|
|
, mILC(aOther.mILC)
|
|
|
|
, mCanvas(aOther.mCanvas)
|
|
|
|
, mRequest(aOther.mRequest)
|
2013-12-11 21:05:27 +00:00
|
|
|
, mSourceSurface(aOther.mSourceSurface)
|
2012-02-05 12:23:08 +00:00
|
|
|
, mSize(aOther.mSize)
|
|
|
|
{}
|
2014-09-01 22:26:43 +00:00
|
|
|
explicit ImageCacheEntryData(const ImageCacheKey& aKey)
|
2012-02-05 12:23:08 +00:00
|
|
|
: mImage(aKey.mImage)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mILC(nullptr)
|
2012-02-05 12:23:08 +00:00
|
|
|
, mCanvas(aKey.mCanvas)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsExpirationState* GetExpirationState() { return &mState; }
|
|
|
|
|
2013-04-26 14:16:12 +00:00
|
|
|
size_t SizeInBytes() { return mSize.width * mSize.height * 4; }
|
|
|
|
|
2012-02-05 12:23:08 +00:00
|
|
|
// Key
|
2012-06-30 05:53:21 +00:00
|
|
|
nsRefPtr<Element> mImage;
|
2012-02-05 12:23:08 +00:00
|
|
|
nsIImageLoadingContent* mILC;
|
2013-01-04 05:16:14 +00:00
|
|
|
nsRefPtr<HTMLCanvasElement> mCanvas;
|
2012-02-05 12:23:08 +00:00
|
|
|
// Value
|
|
|
|
nsCOMPtr<imgIRequest> mRequest;
|
2013-12-11 21:05:27 +00:00
|
|
|
RefPtr<SourceSurface> mSourceSurface;
|
2012-02-05 12:23:08 +00:00
|
|
|
gfxIntSize mSize;
|
|
|
|
nsExpirationState mState;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ImageCacheEntry : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef ImageCacheKey KeyType;
|
|
|
|
typedef const ImageCacheKey* KeyTypePointer;
|
|
|
|
|
2014-09-01 22:26:43 +00:00
|
|
|
explicit ImageCacheEntry(const KeyType* aKey) :
|
|
|
|
mData(new ImageCacheEntryData(*aKey)) {}
|
2012-02-05 12:23:08 +00:00
|
|
|
ImageCacheEntry(const ImageCacheEntry &toCopy) :
|
|
|
|
mData(new ImageCacheEntryData(*toCopy.mData)) {}
|
|
|
|
~ImageCacheEntry() {}
|
|
|
|
|
|
|
|
bool KeyEquals(KeyTypePointer key) const
|
|
|
|
{
|
|
|
|
return mData->mImage == key->mImage && mData->mCanvas == key->mCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer key)
|
|
|
|
{
|
2012-03-12 22:53:18 +00:00
|
|
|
return HashGeneric(key->mImage, key->mCanvas);
|
2012-02-05 12:23:08 +00:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
nsAutoPtr<ImageCacheEntryData> mData;
|
|
|
|
};
|
|
|
|
|
2014-10-29 15:17:00 +00:00
|
|
|
class SimpleImageCacheEntry : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef imgIRequest& KeyType;
|
|
|
|
typedef const imgIRequest* KeyTypePointer;
|
|
|
|
|
|
|
|
explicit SimpleImageCacheEntry(KeyTypePointer aKey)
|
|
|
|
: mRequest(const_cast<imgIRequest*>(aKey))
|
|
|
|
{}
|
|
|
|
SimpleImageCacheEntry(const SimpleImageCacheEntry &toCopy)
|
|
|
|
: mRequest(toCopy.mRequest)
|
|
|
|
, mSourceSurface(toCopy.mSourceSurface)
|
|
|
|
{}
|
|
|
|
~SimpleImageCacheEntry() {}
|
|
|
|
|
|
|
|
bool KeyEquals(KeyTypePointer key) const
|
|
|
|
{
|
|
|
|
return key == mRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType key) { return &key; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer key)
|
|
|
|
{
|
|
|
|
return NS_PTR_TO_UINT32(key) >> 2;
|
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> mRequest;
|
|
|
|
RefPtr<SourceSurface> mSourceSurface;
|
|
|
|
};
|
|
|
|
|
2013-04-26 14:16:12 +00:00
|
|
|
static bool sPrefsInitialized = false;
|
|
|
|
static int32_t sCanvasImageCacheLimit = 0;
|
|
|
|
|
2014-08-25 19:01:46 +00:00
|
|
|
class ImageCacheObserver;
|
|
|
|
|
2012-02-05 12:23:08 +00:00
|
|
|
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 };
|
2014-08-25 19:01:46 +00:00
|
|
|
ImageCache();
|
|
|
|
~ImageCache();
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
virtual void NotifyExpired(ImageCacheEntryData* aObject)
|
|
|
|
{
|
2013-04-26 14:16:12 +00:00
|
|
|
mTotal -= aObject->SizeInBytes();
|
2012-02-05 12:23:08 +00:00
|
|
|
RemoveObject(aObject);
|
|
|
|
// Deleting the entry will delete aObject since the entry owns aObject
|
2014-10-29 15:17:00 +00:00
|
|
|
mSimpleCache.RemoveEntry(*aObject->mRequest);
|
2012-02-05 12:23:08 +00:00
|
|
|
mCache.RemoveEntry(ImageCacheKey(aObject->mImage, aObject->mCanvas));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTHashtable<ImageCacheEntry> mCache;
|
2014-10-29 15:17:00 +00:00
|
|
|
nsTHashtable<SimpleImageCacheEntry> mSimpleCache;
|
2013-04-26 14:16:12 +00:00
|
|
|
size_t mTotal;
|
2014-08-25 19:01:46 +00:00
|
|
|
nsRefPtr<ImageCacheObserver> mImageCacheObserver;
|
2012-02-05 12:23:08 +00:00
|
|
|
};
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
static ImageCache* gImageCache = nullptr;
|
2012-02-05 12:23:08 +00:00
|
|
|
|
2014-08-25 19:01:46 +00:00
|
|
|
// Listen memory-pressure event for image cache purge
|
|
|
|
class ImageCacheObserver MOZ_FINAL : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-09-01 22:26:43 +00:00
|
|
|
explicit ImageCacheObserver(ImageCache* aImageCache)
|
2014-08-25 19:01:46 +00:00
|
|
|
: 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)
|
|
|
|
|
2012-02-05 12:23:08 +00:00
|
|
|
class CanvasImageCacheShutdownObserver MOZ_FINAL : public nsIObserver
|
|
|
|
{
|
2014-06-26 13:30:49 +00:00
|
|
|
~CanvasImageCacheShutdownObserver() {}
|
2012-02-05 12:23:08 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2014-08-25 19:01:46 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:23:08 +00:00
|
|
|
void
|
2012-06-30 05:53:21 +00:00
|
|
|
CanvasImageCache::NotifyDrawImage(Element* aImage,
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement* aCanvas,
|
2012-02-05 12:23:08 +00:00
|
|
|
imgIRequest* aRequest,
|
2013-12-11 21:05:27 +00:00
|
|
|
SourceSurface* aSource,
|
2012-02-05 12:23:08 +00:00
|
|
|
const gfxIntSize& aSize)
|
|
|
|
{
|
|
|
|
if (!gImageCache) {
|
|
|
|
gImageCache = new ImageCache();
|
|
|
|
nsContentUtils::RegisterShutdownObserver(new CanvasImageCacheShutdownObserver());
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageCacheEntry* entry = gImageCache->mCache.PutEntry(ImageCacheKey(aImage, aCanvas));
|
|
|
|
if (entry) {
|
2013-12-11 21:05:27 +00:00
|
|
|
if (entry->mData->mSourceSurface) {
|
2012-02-05 12:23:08 +00:00
|
|
|
// We are overwriting an existing entry.
|
2013-04-26 14:16:12 +00:00
|
|
|
gImageCache->mTotal -= entry->mData->SizeInBytes();
|
2012-02-05 12:23:08 +00:00
|
|
|
gImageCache->RemoveObject(entry->mData);
|
|
|
|
}
|
|
|
|
gImageCache->AddObject(entry->mData);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> ilc = do_QueryInterface(aImage);
|
|
|
|
if (ilc) {
|
|
|
|
ilc->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(entry->mData->mRequest));
|
|
|
|
}
|
|
|
|
entry->mData->mILC = ilc;
|
2013-12-11 21:05:27 +00:00
|
|
|
entry->mData->mSourceSurface = aSource;
|
2012-02-05 12:23:08 +00:00
|
|
|
entry->mData->mSize = aSize;
|
2013-04-26 14:16:12 +00:00
|
|
|
|
|
|
|
gImageCache->mTotal += entry->mData->SizeInBytes();
|
2014-10-29 15:17:00 +00:00
|
|
|
|
|
|
|
if (entry->mData->mRequest) {
|
|
|
|
SimpleImageCacheEntry* simpleentry =
|
|
|
|
gImageCache->mSimpleCache.PutEntry(*entry->mData->mRequest);
|
|
|
|
simpleentry->mSourceSurface = aSource;
|
|
|
|
}
|
2012-02-05 12:23:08 +00:00
|
|
|
}
|
2013-04-26 14:16:12 +00:00
|
|
|
|
|
|
|
if (!sCanvasImageCacheLimit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Expire the image cache early if its larger than we want it to be.
|
|
|
|
while (gImageCache->mTotal > size_t(sCanvasImageCacheLimit))
|
|
|
|
gImageCache->AgeOneGeneration();
|
2012-02-05 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 21:05:27 +00:00
|
|
|
SourceSurface*
|
2012-06-30 05:53:21 +00:00
|
|
|
CanvasImageCache::Lookup(Element* aImage,
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement* aCanvas,
|
2014-11-04 13:00:36 +00:00
|
|
|
gfx::IntSize* aSize)
|
2012-02-05 12:23:08 +00:00
|
|
|
{
|
|
|
|
if (!gImageCache)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
ImageCacheEntry* entry = gImageCache->mCache.GetEntry(ImageCacheKey(aImage, aCanvas));
|
|
|
|
if (!entry || !entry->mData->mILC)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> request;
|
|
|
|
entry->mData->mILC->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(request));
|
|
|
|
if (request != entry->mData->mRequest)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
gImageCache->MarkUsed(entry->mData);
|
|
|
|
|
2014-11-04 13:00:36 +00:00
|
|
|
*aSize = gfx::ToIntSize(entry->mData->mSize);
|
2013-12-11 21:05:27 +00:00
|
|
|
return entry->mData->mSourceSurface;
|
2012-02-05 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 15:17:00 +00:00
|
|
|
SourceSurface*
|
|
|
|
CanvasImageCache::SimpleLookup(Element* aImage)
|
|
|
|
{
|
|
|
|
if (!gImageCache)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> request;
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> ilc = do_QueryInterface(aImage);
|
|
|
|
if (!ilc)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
ilc->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(request));
|
|
|
|
if (!request)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
SimpleImageCacheEntry* entry = gImageCache->mSimpleCache.GetEntry(*request);
|
|
|
|
if (!entry)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return entry->mSourceSurface;
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(CanvasImageCacheShutdownObserver, nsIObserver)
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasImageCacheShutdownObserver::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *aData)
|
2012-02-05 12:23:08 +00:00
|
|
|
{
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
delete gImageCache;
|
2012-07-30 14:20:58 +00:00
|
|
|
gImageCache = nullptr;
|
2012-02-05 12:23:08 +00:00
|
|
|
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-11 13:29:50 +00:00
|
|
|
} // namespace mozilla
|