content/canvas/src/CanvasImageCache.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "CanvasImageCache.h"
michael@0 7 #include "nsIImageLoadingContent.h"
michael@0 8 #include "nsExpirationTracker.h"
michael@0 9 #include "imgIRequest.h"
michael@0 10 #include "mozilla/dom/Element.h"
michael@0 11 #include "nsTHashtable.h"
michael@0 12 #include "mozilla/dom/HTMLCanvasElement.h"
michael@0 13 #include "nsContentUtils.h"
michael@0 14 #include "mozilla/Preferences.h"
michael@0 15 #include "mozilla/gfx/2D.h"
michael@0 16
michael@0 17 namespace mozilla {
michael@0 18
michael@0 19 using namespace dom;
michael@0 20 using namespace gfx;
michael@0 21
michael@0 22 struct ImageCacheKey {
michael@0 23 ImageCacheKey(Element* aImage, HTMLCanvasElement* aCanvas)
michael@0 24 : mImage(aImage), mCanvas(aCanvas) {}
michael@0 25 Element* mImage;
michael@0 26 HTMLCanvasElement* mCanvas;
michael@0 27 };
michael@0 28
michael@0 29 struct ImageCacheEntryData {
michael@0 30 ImageCacheEntryData(const ImageCacheEntryData& aOther)
michael@0 31 : mImage(aOther.mImage)
michael@0 32 , mILC(aOther.mILC)
michael@0 33 , mCanvas(aOther.mCanvas)
michael@0 34 , mRequest(aOther.mRequest)
michael@0 35 , mSourceSurface(aOther.mSourceSurface)
michael@0 36 , mSize(aOther.mSize)
michael@0 37 {}
michael@0 38 ImageCacheEntryData(const ImageCacheKey& aKey)
michael@0 39 : mImage(aKey.mImage)
michael@0 40 , mILC(nullptr)
michael@0 41 , mCanvas(aKey.mCanvas)
michael@0 42 {}
michael@0 43
michael@0 44 nsExpirationState* GetExpirationState() { return &mState; }
michael@0 45
michael@0 46 size_t SizeInBytes() { return mSize.width * mSize.height * 4; }
michael@0 47
michael@0 48 // Key
michael@0 49 nsRefPtr<Element> mImage;
michael@0 50 nsIImageLoadingContent* mILC;
michael@0 51 nsRefPtr<HTMLCanvasElement> mCanvas;
michael@0 52 // Value
michael@0 53 nsCOMPtr<imgIRequest> mRequest;
michael@0 54 RefPtr<SourceSurface> mSourceSurface;
michael@0 55 gfxIntSize mSize;
michael@0 56 nsExpirationState mState;
michael@0 57 };
michael@0 58
michael@0 59 class ImageCacheEntry : public PLDHashEntryHdr {
michael@0 60 public:
michael@0 61 typedef ImageCacheKey KeyType;
michael@0 62 typedef const ImageCacheKey* KeyTypePointer;
michael@0 63
michael@0 64 ImageCacheEntry(const KeyType *key) :
michael@0 65 mData(new ImageCacheEntryData(*key)) {}
michael@0 66 ImageCacheEntry(const ImageCacheEntry &toCopy) :
michael@0 67 mData(new ImageCacheEntryData(*toCopy.mData)) {}
michael@0 68 ~ImageCacheEntry() {}
michael@0 69
michael@0 70 bool KeyEquals(KeyTypePointer key) const
michael@0 71 {
michael@0 72 return mData->mImage == key->mImage && mData->mCanvas == key->mCanvas;
michael@0 73 }
michael@0 74
michael@0 75 static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
michael@0 76 static PLDHashNumber HashKey(KeyTypePointer key)
michael@0 77 {
michael@0 78 return HashGeneric(key->mImage, key->mCanvas);
michael@0 79 }
michael@0 80 enum { ALLOW_MEMMOVE = true };
michael@0 81
michael@0 82 nsAutoPtr<ImageCacheEntryData> mData;
michael@0 83 };
michael@0 84
michael@0 85 static bool sPrefsInitialized = false;
michael@0 86 static int32_t sCanvasImageCacheLimit = 0;
michael@0 87
michael@0 88 class ImageCache MOZ_FINAL : public nsExpirationTracker<ImageCacheEntryData,4> {
michael@0 89 public:
michael@0 90 // We use 3 generations of 1 second each to get a 2-3 seconds timeout.
michael@0 91 enum { GENERATION_MS = 1000 };
michael@0 92 ImageCache()
michael@0 93 : nsExpirationTracker<ImageCacheEntryData,4>(GENERATION_MS)
michael@0 94 , mTotal(0)
michael@0 95 {
michael@0 96 if (!sPrefsInitialized) {
michael@0 97 sPrefsInitialized = true;
michael@0 98 Preferences::AddIntVarCache(&sCanvasImageCacheLimit, "canvas.image.cache.limit", 0);
michael@0 99 }
michael@0 100 }
michael@0 101 ~ImageCache() {
michael@0 102 AgeAllGenerations();
michael@0 103 }
michael@0 104
michael@0 105 virtual void NotifyExpired(ImageCacheEntryData* aObject)
michael@0 106 {
michael@0 107 mTotal -= aObject->SizeInBytes();
michael@0 108 RemoveObject(aObject);
michael@0 109 // Deleting the entry will delete aObject since the entry owns aObject
michael@0 110 mCache.RemoveEntry(ImageCacheKey(aObject->mImage, aObject->mCanvas));
michael@0 111 }
michael@0 112
michael@0 113 nsTHashtable<ImageCacheEntry> mCache;
michael@0 114 size_t mTotal;
michael@0 115 };
michael@0 116
michael@0 117 static ImageCache* gImageCache = nullptr;
michael@0 118
michael@0 119 class CanvasImageCacheShutdownObserver MOZ_FINAL : public nsIObserver
michael@0 120 {
michael@0 121 public:
michael@0 122 NS_DECL_ISUPPORTS
michael@0 123 NS_DECL_NSIOBSERVER
michael@0 124 };
michael@0 125
michael@0 126 void
michael@0 127 CanvasImageCache::NotifyDrawImage(Element* aImage,
michael@0 128 HTMLCanvasElement* aCanvas,
michael@0 129 imgIRequest* aRequest,
michael@0 130 SourceSurface* aSource,
michael@0 131 const gfxIntSize& aSize)
michael@0 132 {
michael@0 133 if (!gImageCache) {
michael@0 134 gImageCache = new ImageCache();
michael@0 135 nsContentUtils::RegisterShutdownObserver(new CanvasImageCacheShutdownObserver());
michael@0 136 }
michael@0 137
michael@0 138 ImageCacheEntry* entry = gImageCache->mCache.PutEntry(ImageCacheKey(aImage, aCanvas));
michael@0 139 if (entry) {
michael@0 140 if (entry->mData->mSourceSurface) {
michael@0 141 // We are overwriting an existing entry.
michael@0 142 gImageCache->mTotal -= entry->mData->SizeInBytes();
michael@0 143 gImageCache->RemoveObject(entry->mData);
michael@0 144 }
michael@0 145 gImageCache->AddObject(entry->mData);
michael@0 146
michael@0 147 nsCOMPtr<nsIImageLoadingContent> ilc = do_QueryInterface(aImage);
michael@0 148 if (ilc) {
michael@0 149 ilc->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
michael@0 150 getter_AddRefs(entry->mData->mRequest));
michael@0 151 }
michael@0 152 entry->mData->mILC = ilc;
michael@0 153 entry->mData->mSourceSurface = aSource;
michael@0 154 entry->mData->mSize = aSize;
michael@0 155
michael@0 156 gImageCache->mTotal += entry->mData->SizeInBytes();
michael@0 157 }
michael@0 158
michael@0 159 if (!sCanvasImageCacheLimit)
michael@0 160 return;
michael@0 161
michael@0 162 // Expire the image cache early if its larger than we want it to be.
michael@0 163 while (gImageCache->mTotal > size_t(sCanvasImageCacheLimit))
michael@0 164 gImageCache->AgeOneGeneration();
michael@0 165 }
michael@0 166
michael@0 167 SourceSurface*
michael@0 168 CanvasImageCache::Lookup(Element* aImage,
michael@0 169 HTMLCanvasElement* aCanvas,
michael@0 170 gfxIntSize* aSize)
michael@0 171 {
michael@0 172 if (!gImageCache)
michael@0 173 return nullptr;
michael@0 174
michael@0 175 ImageCacheEntry* entry = gImageCache->mCache.GetEntry(ImageCacheKey(aImage, aCanvas));
michael@0 176 if (!entry || !entry->mData->mILC)
michael@0 177 return nullptr;
michael@0 178
michael@0 179 nsCOMPtr<imgIRequest> request;
michael@0 180 entry->mData->mILC->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(request));
michael@0 181 if (request != entry->mData->mRequest)
michael@0 182 return nullptr;
michael@0 183
michael@0 184 gImageCache->MarkUsed(entry->mData);
michael@0 185
michael@0 186 *aSize = entry->mData->mSize;
michael@0 187 return entry->mData->mSourceSurface;
michael@0 188 }
michael@0 189
michael@0 190 NS_IMPL_ISUPPORTS(CanvasImageCacheShutdownObserver, nsIObserver)
michael@0 191
michael@0 192 NS_IMETHODIMP
michael@0 193 CanvasImageCacheShutdownObserver::Observe(nsISupports *aSubject,
michael@0 194 const char *aTopic,
michael@0 195 const char16_t *aData)
michael@0 196 {
michael@0 197 if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
michael@0 198 delete gImageCache;
michael@0 199 gImageCache = nullptr;
michael@0 200
michael@0 201 nsContentUtils::UnregisterShutdownObserver(this);
michael@0 202 }
michael@0 203
michael@0 204 return NS_OK;
michael@0 205 }
michael@0 206
michael@0 207 } // namespace mozilla

mercurial