Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / canvas / CanvasImageCache.cpp
blob9723403bc2c29f0a1ca615ebabe1d76811dc763c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "CanvasImageCache.h"
7 #include "nsIImageLoadingContent.h"
8 #include "nsExpirationTracker.h"
9 #include "imgIRequest.h"
10 #include "mozilla/dom/Element.h"
11 #include "nsTHashtable.h"
12 #include "mozilla/dom/HTMLCanvasElement.h"
13 #include "nsContentUtils.h"
14 #include "mozilla/Preferences.h"
15 #include "mozilla/StaticPrefs_canvas.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/gfx/2D.h"
18 #include "gfx2DGlue.h"
20 namespace mozilla {
22 using namespace dom;
23 using namespace gfx;
25 /**
26 * Used for images specific to this one canvas. Required
27 * due to CORS security.
29 struct ImageCacheKey {
30 ImageCacheKey(imgIContainer* aImage, HTMLCanvasElement* aCanvas,
31 BackendType aBackendType)
32 : mImage(aImage), mCanvas(aCanvas), mBackendType(aBackendType) {}
33 nsCOMPtr<imgIContainer> mImage;
34 HTMLCanvasElement* mCanvas;
35 BackendType mBackendType;
38 /**
39 * Cache data needs to be separate from the entry
40 * for nsExpirationTracker.
42 struct ImageCacheEntryData {
43 ImageCacheEntryData(const ImageCacheEntryData& aOther)
44 : mImage(aOther.mImage),
45 mCanvas(aOther.mCanvas),
46 mBackendType(aOther.mBackendType),
47 mSourceSurface(aOther.mSourceSurface),
48 mSize(aOther.mSize),
49 mIntrinsicSize(aOther.mIntrinsicSize),
50 mCropRect(aOther.mCropRect) {}
51 explicit ImageCacheEntryData(const ImageCacheKey& aKey)
52 : mImage(aKey.mImage),
53 mCanvas(aKey.mCanvas),
54 mBackendType(aKey.mBackendType) {}
56 nsExpirationState* GetExpirationState() { return &mState; }
57 size_t SizeInBytes() { return mSize.width * mSize.height * 4; }
59 // Key
60 nsCOMPtr<imgIContainer> mImage;
61 HTMLCanvasElement* mCanvas;
62 BackendType mBackendType;
63 // Value
64 RefPtr<SourceSurface> mSourceSurface;
65 IntSize mSize;
66 IntSize mIntrinsicSize;
67 Maybe<IntRect> mCropRect;
68 nsExpirationState mState;
71 class ImageCacheEntry : public PLDHashEntryHdr {
72 public:
73 using KeyType = ImageCacheKey;
74 using KeyTypePointer = const ImageCacheKey*;
76 explicit ImageCacheEntry(const KeyType* aKey)
77 : mData(new ImageCacheEntryData(*aKey)) {}
78 ImageCacheEntry(const ImageCacheEntry& toCopy)
79 : mData(new ImageCacheEntryData(*toCopy.mData)) {}
80 ~ImageCacheEntry() = default;
82 bool KeyEquals(KeyTypePointer key) const {
83 return mData->mImage == key->mImage && mData->mCanvas == key->mCanvas &&
84 mData->mBackendType == key->mBackendType;
87 static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
88 static PLDHashNumber HashKey(KeyTypePointer key) {
89 return HashGeneric(key->mImage.get(), key->mCanvas, key->mBackendType);
91 enum { ALLOW_MEMMOVE = true };
93 UniquePtr<ImageCacheEntryData> mData;
96 /**
97 * Used for all images across all canvases.
99 struct AllCanvasImageCacheKey {
100 explicit AllCanvasImageCacheKey(imgIContainer* aImage,
101 BackendType aBackendType)
102 : mImage(aImage), mBackendType(aBackendType) {}
104 nsCOMPtr<imgIContainer> mImage;
105 BackendType mBackendType;
108 class AllCanvasImageCacheEntry : public PLDHashEntryHdr {
109 public:
110 using KeyType = AllCanvasImageCacheKey;
111 using KeyTypePointer = const AllCanvasImageCacheKey*;
113 explicit AllCanvasImageCacheEntry(const KeyType* aKey)
114 : mImage(aKey->mImage), mBackendType(aKey->mBackendType) {}
116 AllCanvasImageCacheEntry(const AllCanvasImageCacheEntry& toCopy)
117 : mImage(toCopy.mImage),
118 mSourceSurface(toCopy.mSourceSurface),
119 mBackendType(toCopy.mBackendType) {}
121 ~AllCanvasImageCacheEntry() = default;
123 bool KeyEquals(KeyTypePointer key) const {
124 return mImage == key->mImage && mBackendType == key->mBackendType;
127 static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
128 static PLDHashNumber HashKey(KeyTypePointer key) {
129 return HashGeneric(key->mImage.get(), key->mBackendType);
131 enum { ALLOW_MEMMOVE = true };
133 nsCOMPtr<imgIContainer> mImage;
134 RefPtr<SourceSurface> mSourceSurface;
135 BackendType mBackendType;
138 class ImageCacheObserver;
140 class ImageCache final : public nsExpirationTracker<ImageCacheEntryData, 4> {
141 public:
142 // We use 3 generations of 1 second each to get a 2-3 seconds timeout.
143 enum { GENERATION_MS = 1000 };
144 ImageCache();
145 ~ImageCache();
147 virtual void NotifyExpired(ImageCacheEntryData* aObject) override {
148 RemoveObject(aObject);
150 // Remove from the all canvas cache entry first since nsExpirationTracker
151 // will delete aObject.
152 mAllCanvasCache.RemoveEntry(
153 AllCanvasImageCacheKey(aObject->mImage, aObject->mBackendType));
155 // Deleting the entry will delete aObject since the entry owns aObject.
156 mCache.RemoveEntry(ImageCacheKey(aObject->mImage, aObject->mCanvas,
157 aObject->mBackendType));
160 nsTHashtable<ImageCacheEntry> mCache;
161 nsTHashtable<AllCanvasImageCacheEntry> mAllCanvasCache;
162 RefPtr<ImageCacheObserver> mImageCacheObserver;
165 static ImageCache* gImageCache = nullptr;
167 // Listen memory-pressure event for image cache purge.
168 class ImageCacheObserver final : public nsIObserver {
169 public:
170 NS_DECL_ISUPPORTS
172 explicit ImageCacheObserver(ImageCache* aImageCache)
173 : mImageCache(aImageCache) {
174 RegisterObserverEvents();
177 void Destroy() {
178 UnregisterObserverEvents();
179 mImageCache = nullptr;
182 NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
183 const char16_t* aSomeData) override {
184 if (!mImageCache || (strcmp(aTopic, "memory-pressure") != 0 &&
185 strcmp(aTopic, "canvas-device-reset") != 0)) {
186 return NS_OK;
189 mImageCache->AgeAllGenerations();
190 return NS_OK;
193 private:
194 virtual ~ImageCacheObserver() = default;
196 void RegisterObserverEvents() {
197 nsCOMPtr<nsIObserverService> observerService =
198 mozilla::services::GetObserverService();
200 MOZ_ASSERT(observerService);
202 if (observerService) {
203 observerService->AddObserver(this, "memory-pressure", false);
204 observerService->AddObserver(this, "canvas-device-reset", false);
208 void UnregisterObserverEvents() {
209 nsCOMPtr<nsIObserverService> observerService =
210 mozilla::services::GetObserverService();
212 // Do not assert on observerService here. This might be triggered by
213 // the cycle collector at a late enough time, that XPCOM services are
214 // no longer available. See bug 1029504.
215 if (observerService) {
216 observerService->RemoveObserver(this, "memory-pressure");
217 observerService->RemoveObserver(this, "canvas-device-reset");
221 ImageCache* mImageCache;
224 NS_IMPL_ISUPPORTS(ImageCacheObserver, nsIObserver)
226 class CanvasImageCacheShutdownObserver final : public nsIObserver {
227 ~CanvasImageCacheShutdownObserver() = default;
229 public:
230 NS_DECL_ISUPPORTS
231 NS_DECL_NSIOBSERVER
234 ImageCache::ImageCache()
235 : nsExpirationTracker<ImageCacheEntryData, 4>(GENERATION_MS, "ImageCache") {
236 mImageCacheObserver = new ImageCacheObserver(this);
237 MOZ_RELEASE_ASSERT(mImageCacheObserver,
238 "GFX: Can't alloc ImageCacheObserver");
241 ImageCache::~ImageCache() {
242 AgeAllGenerations();
243 mImageCacheObserver->Destroy();
246 static already_AddRefed<imgIContainer> GetImageContainer(dom::Element* aImage) {
247 nsCOMPtr<imgIRequest> request;
248 nsCOMPtr<nsIImageLoadingContent> ilc = do_QueryInterface(aImage);
249 if (!ilc) {
250 return nullptr;
253 ilc->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
254 getter_AddRefs(request));
255 if (!request) {
256 return nullptr;
259 nsCOMPtr<imgIContainer> imgContainer;
260 request->GetImage(getter_AddRefs(imgContainer));
261 if (!imgContainer) {
262 return nullptr;
265 return imgContainer.forget();
268 void CanvasImageCache::NotifyDrawImage(
269 Element* aImage, HTMLCanvasElement* aCanvas, DrawTarget* aTarget,
270 SourceSurface* aSource, const IntSize& aSize, const IntSize& aIntrinsicSize,
271 const Maybe<IntRect>& aCropRect) {
272 if (!aTarget) {
273 return;
276 if (!gImageCache) {
277 gImageCache = new ImageCache();
278 nsContentUtils::RegisterShutdownObserver(
279 new CanvasImageCacheShutdownObserver());
282 nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
283 if (!imgContainer) {
284 return;
287 BackendType backendType = aTarget->GetBackendType();
288 AllCanvasImageCacheKey allCanvasCacheKey(imgContainer, backendType);
289 ImageCacheKey canvasCacheKey(imgContainer, aCanvas, backendType);
290 ImageCacheEntry* entry = gImageCache->mCache.PutEntry(canvasCacheKey);
292 if (entry) {
293 if (entry->mData->mSourceSurface) {
294 // We are overwriting an existing entry.
295 gImageCache->RemoveObject(entry->mData.get());
296 gImageCache->mAllCanvasCache.RemoveEntry(allCanvasCacheKey);
299 gImageCache->AddObject(entry->mData.get());
300 entry->mData->mSourceSurface = aSource;
301 entry->mData->mSize = aSize;
302 entry->mData->mIntrinsicSize = aIntrinsicSize;
303 entry->mData->mCropRect = aCropRect;
305 AllCanvasImageCacheEntry* allEntry =
306 gImageCache->mAllCanvasCache.PutEntry(allCanvasCacheKey);
307 if (allEntry) {
308 allEntry->mSourceSurface = aSource;
313 SourceSurface* CanvasImageCache::LookupAllCanvas(Element* aImage,
314 DrawTarget* aTarget) {
315 if (!gImageCache || !aTarget) {
316 return nullptr;
319 nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
320 if (!imgContainer) {
321 return nullptr;
324 AllCanvasImageCacheEntry* entry = gImageCache->mAllCanvasCache.GetEntry(
325 AllCanvasImageCacheKey(imgContainer, aTarget->GetBackendType()));
326 if (!entry) {
327 return nullptr;
330 return entry->mSourceSurface;
333 SourceSurface* CanvasImageCache::LookupCanvas(Element* aImage,
334 HTMLCanvasElement* aCanvas,
335 DrawTarget* aTarget,
336 IntSize* aSizeOut,
337 IntSize* aIntrinsicSizeOut,
338 Maybe<IntRect>* aCropRectOut) {
339 if (!gImageCache || !aTarget) {
340 return nullptr;
343 nsCOMPtr<imgIContainer> imgContainer = GetImageContainer(aImage);
344 if (!imgContainer) {
345 return nullptr;
348 // We filter based on the backend type because we don't want a surface that
349 // was optimized for one backend to be preferred for another backend, as this
350 // could have performance implications. For example, a Skia optimized surface
351 // given to a D2D backend would cause an upload each time, and similarly a D2D
352 // optimized surface given to a Skia backend would cause a readback. For
353 // details, see bug 1794442.
354 ImageCacheEntry* entry = gImageCache->mCache.GetEntry(
355 ImageCacheKey(imgContainer, aCanvas, aTarget->GetBackendType()));
356 if (!entry) {
357 return nullptr;
360 MOZ_ASSERT(aSizeOut);
362 gImageCache->MarkUsed(entry->mData.get());
363 *aSizeOut = entry->mData->mSize;
364 *aIntrinsicSizeOut = entry->mData->mIntrinsicSize;
365 *aCropRectOut = entry->mData->mCropRect;
366 return entry->mData->mSourceSurface;
369 NS_IMPL_ISUPPORTS(CanvasImageCacheShutdownObserver, nsIObserver)
371 NS_IMETHODIMP
372 CanvasImageCacheShutdownObserver::Observe(nsISupports* aSubject,
373 const char* aTopic,
374 const char16_t* aData) {
375 if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
376 delete gImageCache;
377 gImageCache = nullptr;
379 nsContentUtils::UnregisterShutdownObserver(this);
382 return NS_OK;
385 } // namespace mozilla