gecko-dev/dom/canvas/CanvasImageCache.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

367 строки
10 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2012-05-21 15:12:37 +04: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/. */
#include "CanvasImageCache.h"
#include "nsAutoPtr.h"
#include "nsIImageLoadingContent.h"
#include "nsExpirationTracker.h"
#include "imgIRequest.h"
#include "mozilla/dom/Element.h"
#include "nsTHashtable.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "nsContentUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_canvas.h"
2017-08-15 05:51:54 +03:00
#include "mozilla/SystemGroup.h"
#include "mozilla/gfx/2D.h"
#include "gfx2DGlue.h"
namespace mozilla {
using namespace dom;
using namespace gfx;
/**
* Used for images specific to this one canvas. Required
* due to CORS security.
*/
struct ImageCacheKey {
ImageCacheKey(imgIContainer* aImage, HTMLCanvasElement* aCanvas)
: mImage(aImage), mCanvas(aCanvas) {}
nsCOMPtr<imgIContainer> mImage;
HTMLCanvasElement* mCanvas;
};
/**
* Cache data needs to be separate from the entry
* for nsExpirationTracker.
*/
struct ImageCacheEntryData {
ImageCacheEntryData(const ImageCacheEntryData& aOther)
: mImage(aOther.mImage),
mCanvas(aOther.mCanvas),
mSourceSurface(aOther.mSourceSurface),
mSize(aOther.mSize),
mIntrinsicSize(aOther.mIntrinsicSize) {}
explicit ImageCacheEntryData(const ImageCacheKey& aKey)
: mImage(aKey.mImage), mCanvas(aKey.mCanvas) {}
nsExpirationState* GetExpirationState() { return &mState; }
size_t SizeInBytes() { return mSize.width * mSize.height * 4; }
// Key
nsCOMPtr<imgIContainer> mImage;
HTMLCanvasElement* mCanvas;
// Value
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<SourceSurface> mSourceSurface;
IntSize mSize;
IntSize mIntrinsicSize;
nsExpirationState mState;
};
class ImageCacheEntry : public PLDHashEntryHdr {
public:
typedef ImageCacheKey KeyType;
typedef const ImageCacheKey* KeyTypePointer;
explicit ImageCacheEntry(const KeyType* aKey)
: mData(new ImageCacheEntryData(*aKey)) {}
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) {
return HashGeneric(key->mImage.get(), key->mCanvas);
}
enum { ALLOW_MEMMOVE = true };
nsAutoPtr<ImageCacheEntryData> mData;
};
/**
* Used for all images across all canvases.
*/
struct AllCanvasImageCacheKey {
explicit AllCanvasImageCacheKey(imgIContainer* aImage) : mImage(aImage) {}
nsCOMPtr<imgIContainer> mImage;
};
class AllCanvasImageCacheEntry : public PLDHashEntryHdr {
public:
typedef AllCanvasImageCacheKey KeyType;
typedef const AllCanvasImageCacheKey* KeyTypePointer;
explicit AllCanvasImageCacheEntry(const KeyType* aKey)
: mImage(aKey->mImage) {}
AllCanvasImageCacheEntry(const AllCanvasImageCacheEntry& toCopy)
: mImage(toCopy.mImage), mSourceSurface(toCopy.mSourceSurface) {}
~AllCanvasImageCacheEntry() {}
bool KeyEquals(KeyTypePointer key) const { return mImage == key->mImage; }
static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
static PLDHashNumber HashKey(KeyTypePointer key) {
return HashGeneric(key->mImage.get());
}
enum { ALLOW_MEMMOVE = true };
nsCOMPtr<imgIContainer> mImage;
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<SourceSurface> mSourceSurface;
};
class ImageCacheObserver;
class ImageCache 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();
~ImageCache();
virtual void NotifyExpired(ImageCacheEntryData* aObject) override {
mTotal -= aObject->SizeInBytes();
RemoveObject(aObject);
// Remove from the all canvas cache entry first since nsExpirationTracker
// will delete aObject.
mAllCanvasCache.RemoveEntry(AllCanvasImageCacheKey(aObject->mImage));
// Deleting the entry will delete aObject since the entry owns aObject.
mCache.RemoveEntry(ImageCacheKey(aObject->mImage, aObject->mCanvas));
}
nsTHashtable<ImageCacheEntry> mCache;
nsTHashtable<AllCanvasImageCacheEntry> mAllCanvasCache;
size_t mTotal;
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<ImageCacheObserver> mImageCacheObserver;
};
static ImageCache* gImageCache = nullptr;
// Listen memory-pressure event for image cache purge.
class ImageCacheObserver final : public nsIObserver {
public:
NS_DECL_ISUPPORTS
explicit ImageCacheObserver(ImageCache* aImageCache)
: mImageCache(aImageCache) {
RegisterMemoryPressureEvent();
}
void Destroy() {
UnregisterMemoryPressureEvent();
mImageCache = nullptr;
}
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aSomeData) override {
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 final : public nsIObserver {
~CanvasImageCacheShutdownObserver() {}
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
};
ImageCache::ImageCache()
2017-08-15 05:51:54 +03:00
: nsExpirationTracker<ImageCacheEntryData, 4>(
GENERATION_MS, "ImageCache",
SystemGroup::EventTargetFor(TaskCategory::Other)),
mTotal(0) {
mImageCacheObserver = new ImageCacheObserver(this);
MOZ_RELEASE_ASSERT(mImageCacheObserver,
"GFX: Can't alloc ImageCacheObserver");
}
ImageCache::~ImageCache() {
AgeAllGenerations();
mImageCacheObserver->Destroy();
}
static already_AddRefed<imgIContainer> GetImageContainer(dom::Element* aImage) {
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;
}
nsCOMPtr<imgIContainer> imgContainer;
request->GetImage(getter_AddRefs(imgContainer));
if (!imgContainer) {
return nullptr;
}
return imgContainer.forget();
}
void CanvasImageCache::NotifyDrawImage(Element* aImage,
HTMLCanvasElement* aCanvas,
SourceSurface* aSource,
const IntSize& aSize,
const IntSize& aIntrinsicSize) {
if (!gImageCache) {
gImageCache = new ImageCache();
nsContentUtils::RegisterShutdownObserver(
new CanvasImageCacheShutdownObserver());
}
nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
if (!imgContainer) {
return;
}
AllCanvasImageCacheKey allCanvasCacheKey(imgContainer);
ImageCacheKey canvasCacheKey(imgContainer, aCanvas);
ImageCacheEntry* entry = gImageCache->mCache.PutEntry(canvasCacheKey);
if (entry) {
if (entry->mData->mSourceSurface) {
// We are overwriting an existing entry.
gImageCache->mTotal -= entry->mData->SizeInBytes();
gImageCache->RemoveObject(entry->mData);
gImageCache->mAllCanvasCache.RemoveEntry(allCanvasCacheKey);
}
gImageCache->AddObject(entry->mData);
entry->mData->mSourceSurface = aSource;
entry->mData->mSize = aSize;
entry->mData->mIntrinsicSize = aIntrinsicSize;
gImageCache->mTotal += entry->mData->SizeInBytes();
AllCanvasImageCacheEntry* allEntry =
gImageCache->mAllCanvasCache.PutEntry(allCanvasCacheKey);
if (allEntry) {
allEntry->mSourceSurface = aSource;
}
}
if (!StaticPrefs::canvas_image_cache_limit()) {
return;
}
// Expire the image cache early if its larger than we want it to be.
while (gImageCache->mTotal >
size_t(StaticPrefs::canvas_image_cache_limit())) {
gImageCache->AgeOneGeneration();
}
}
SourceSurface* CanvasImageCache::LookupAllCanvas(Element* aImage) {
if (!gImageCache) {
return nullptr;
}
nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
if (!imgContainer) {
return nullptr;
}
AllCanvasImageCacheEntry* entry = gImageCache->mAllCanvasCache.GetEntry(
AllCanvasImageCacheKey(imgContainer));
if (!entry) {
return nullptr;
}
return entry->mSourceSurface;
}
SourceSurface* CanvasImageCache::LookupCanvas(Element* aImage,
HTMLCanvasElement* aCanvas,
IntSize* aSizeOut,
IntSize* aIntrinsicSizeOut) {
if (!gImageCache) {
return nullptr;
}
nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
if (!imgContainer) {
return nullptr;
}
ImageCacheEntry* entry =
gImageCache->mCache.GetEntry(ImageCacheKey(imgContainer, aCanvas));
if (!entry) {
return nullptr;
}
MOZ_ASSERT(aSizeOut);
gImageCache->MarkUsed(entry->mData);
*aSizeOut = entry->mData->mSize;
*aIntrinsicSizeOut = entry->mData->mIntrinsicSize;
return entry->mData->mSourceSurface;
}
NS_IMPL_ISUPPORTS(CanvasImageCacheShutdownObserver, nsIObserver)
NS_IMETHODIMP
CanvasImageCacheShutdownObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) {
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
delete gImageCache;
gImageCache = nullptr;
nsContentUtils::UnregisterShutdownObserver(this);
}
return NS_OK;
}
} // namespace mozilla