Bug 1472338: part 1) Add Chrome tests for the async Clipboard API. r=NeilDeakin
[gecko.git] / image / SurfaceCache.cpp
blob95ac62c627a8c4adebd99552d86cbea1ced801f6
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 /**
7 * SurfaceCache is a service for caching temporary surfaces in imagelib.
8 */
10 #include "SurfaceCache.h"
12 #include <algorithm>
13 #include <utility>
15 #include "ISurfaceProvider.h"
16 #include "Image.h"
17 #include "LookupResult.h"
18 #include "ShutdownTracker.h"
19 #include "gfx2DGlue.h"
20 #include "gfxPlatform.h"
21 #include "imgFrame.h"
22 #include "mozilla/Assertions.h"
23 #include "mozilla/Attributes.h"
24 #include "mozilla/CheckedInt.h"
25 #include "mozilla/DebugOnly.h"
26 #include "mozilla/Likely.h"
27 #include "mozilla/RefPtr.h"
28 #include "mozilla/StaticMutex.h"
29 #include "mozilla/StaticPrefs_image.h"
30 #include "mozilla/StaticPtr.h"
31 #include "mozilla/Tuple.h"
32 #include "nsExpirationTracker.h"
33 #include "nsHashKeys.h"
34 #include "nsIMemoryReporter.h"
35 #include "nsRefPtrHashtable.h"
36 #include "nsSize.h"
37 #include "nsTArray.h"
38 #include "Orientation.h"
39 #include "prsystem.h"
41 using std::max;
42 using std::min;
44 namespace mozilla {
46 using namespace gfx;
48 namespace image {
50 MOZ_DEFINE_MALLOC_SIZE_OF(SurfaceCacheMallocSizeOf)
52 class CachedSurface;
53 class SurfaceCacheImpl;
55 ///////////////////////////////////////////////////////////////////////////////
56 // Static Data
57 ///////////////////////////////////////////////////////////////////////////////
59 // The single surface cache instance.
60 static StaticRefPtr<SurfaceCacheImpl> sInstance;
62 // The mutex protecting the surface cache.
63 static StaticMutex sInstanceMutex;
65 ///////////////////////////////////////////////////////////////////////////////
66 // SurfaceCache Implementation
67 ///////////////////////////////////////////////////////////////////////////////
69 /**
70 * Cost models the cost of storing a surface in the cache. Right now, this is
71 * simply an estimate of the size of the surface in bytes, but in the future it
72 * may be worth taking into account the cost of rematerializing the surface as
73 * well.
75 typedef size_t Cost;
77 static Cost ComputeCost(const IntSize& aSize, uint32_t aBytesPerPixel) {
78 MOZ_ASSERT(aBytesPerPixel == 1 || aBytesPerPixel == 4);
79 return aSize.width * aSize.height * aBytesPerPixel;
82 /**
83 * Since we want to be able to make eviction decisions based on cost, we need to
84 * be able to look up the CachedSurface which has a certain cost as well as the
85 * cost associated with a certain CachedSurface. To make this possible, in data
86 * structures we actually store a CostEntry, which contains a weak pointer to
87 * its associated surface.
89 * To make usage of the weak pointer safe, SurfaceCacheImpl always calls
90 * StartTracking after a surface is stored in the cache and StopTracking before
91 * it is removed.
93 class CostEntry {
94 public:
95 CostEntry(NotNull<CachedSurface*> aSurface, Cost aCost)
96 : mSurface(aSurface), mCost(aCost) {}
98 NotNull<CachedSurface*> Surface() const { return mSurface; }
99 Cost GetCost() const { return mCost; }
101 bool operator==(const CostEntry& aOther) const {
102 return mSurface == aOther.mSurface && mCost == aOther.mCost;
105 bool operator<(const CostEntry& aOther) const {
106 return mCost < aOther.mCost ||
107 (mCost == aOther.mCost && mSurface < aOther.mSurface);
110 private:
111 NotNull<CachedSurface*> mSurface;
112 Cost mCost;
116 * A CachedSurface associates a surface with a key that uniquely identifies that
117 * surface.
119 class CachedSurface {
120 ~CachedSurface() {}
122 public:
123 MOZ_DECLARE_REFCOUNTED_TYPENAME(CachedSurface)
124 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CachedSurface)
126 explicit CachedSurface(NotNull<ISurfaceProvider*> aProvider)
127 : mProvider(aProvider), mIsLocked(false) {}
129 DrawableSurface GetDrawableSurface() const {
130 if (MOZ_UNLIKELY(IsPlaceholder())) {
131 MOZ_ASSERT_UNREACHABLE("Called GetDrawableSurface() on a placeholder");
132 return DrawableSurface();
135 return mProvider->Surface();
138 void SetLocked(bool aLocked) {
139 if (IsPlaceholder()) {
140 return; // Can't lock a placeholder.
143 // Update both our state and our provider's state. Some surface providers
144 // are permanently locked; maintaining our own locking state enables us to
145 // respect SetLocked() even when it's meaningless from the provider's
146 // perspective.
147 mIsLocked = aLocked;
148 mProvider->SetLocked(aLocked);
151 bool IsLocked() const {
152 return !IsPlaceholder() && mIsLocked && mProvider->IsLocked();
155 void SetCannotSubstitute() {
156 mProvider->Availability().SetCannotSubstitute();
158 bool CannotSubstitute() const {
159 return mProvider->Availability().CannotSubstitute();
162 bool IsPlaceholder() const {
163 return mProvider->Availability().IsPlaceholder();
165 bool IsDecoded() const { return !IsPlaceholder() && mProvider->IsFinished(); }
167 ImageKey GetImageKey() const { return mProvider->GetImageKey(); }
168 const SurfaceKey& GetSurfaceKey() const { return mProvider->GetSurfaceKey(); }
169 nsExpirationState* GetExpirationState() { return &mExpirationState; }
171 CostEntry GetCostEntry() {
172 return image::CostEntry(WrapNotNull(this), mProvider->LogicalSizeInBytes());
175 size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
176 return aMallocSizeOf(this) + aMallocSizeOf(mProvider.get());
179 void InvalidateRecording() { mProvider->InvalidateRecording(); }
181 // A helper type used by SurfaceCacheImpl::CollectSizeOfSurfaces.
182 struct MOZ_STACK_CLASS SurfaceMemoryReport {
183 SurfaceMemoryReport(nsTArray<SurfaceMemoryCounter>& aCounters,
184 MallocSizeOf aMallocSizeOf)
185 : mCounters(aCounters), mMallocSizeOf(aMallocSizeOf) {}
187 void Add(NotNull<CachedSurface*> aCachedSurface, bool aIsFactor2) {
188 if (aCachedSurface->IsPlaceholder()) {
189 return;
192 // Record the memory used by the ISurfaceProvider. This may not have a
193 // straightforward relationship to the size of the surface that
194 // DrawableRef() returns if the surface is generated dynamically. (i.e.,
195 // for surfaces with PlaybackType::eAnimated.)
196 aCachedSurface->mProvider->AddSizeOfExcludingThis(
197 mMallocSizeOf, [&](ISurfaceProvider::AddSizeOfCbData& aMetadata) {
198 SurfaceMemoryCounter counter(aCachedSurface->GetSurfaceKey(),
199 aCachedSurface->IsLocked(),
200 aCachedSurface->CannotSubstitute(),
201 aIsFactor2, aMetadata.mFinished);
203 counter.Values().SetDecodedHeap(aMetadata.mHeapBytes);
204 counter.Values().SetDecodedNonHeap(aMetadata.mNonHeapBytes);
205 counter.Values().SetDecodedUnknown(aMetadata.mUnknownBytes);
206 counter.Values().SetExternalHandles(aMetadata.mExternalHandles);
207 counter.Values().SetFrameIndex(aMetadata.mIndex);
208 counter.Values().SetExternalId(aMetadata.mExternalId);
209 counter.Values().SetSurfaceTypes(aMetadata.mTypes);
211 mCounters.AppendElement(counter);
215 private:
216 nsTArray<SurfaceMemoryCounter>& mCounters;
217 MallocSizeOf mMallocSizeOf;
220 private:
221 nsExpirationState mExpirationState;
222 NotNull<RefPtr<ISurfaceProvider>> mProvider;
223 bool mIsLocked;
226 static int64_t AreaOfIntSize(const IntSize& aSize) {
227 return static_cast<int64_t>(aSize.width) * static_cast<int64_t>(aSize.height);
231 * An ImageSurfaceCache is a per-image surface cache. For correctness we must be
232 * able to remove all surfaces associated with an image when the image is
233 * destroyed or invalidated. Since this will happen frequently, it makes sense
234 * to make it cheap by storing the surfaces for each image separately.
236 * ImageSurfaceCache also keeps track of whether its associated image is locked
237 * or unlocked.
239 * The cache may also enter "factor of 2" mode which occurs when the number of
240 * surfaces in the cache exceeds the "image.cache.factor2.threshold-surfaces"
241 * pref plus the number of native sizes of the image. When in "factor of 2"
242 * mode, the cache will strongly favour sizes which are a factor of 2 of the
243 * largest native size. It accomplishes this by suggesting a factor of 2 size
244 * when lookups fail and substituting the nearest factor of 2 surface to the
245 * ideal size as the "best" available (as opposed to substitution but not
246 * found). This allows us to minimize memory consumption and CPU time spent
247 * decoding when a website requires many variants of the same surface.
249 class ImageSurfaceCache {
250 ~ImageSurfaceCache() {}
252 public:
253 explicit ImageSurfaceCache(const ImageKey aImageKey)
254 : mLocked(false),
255 mFactor2Mode(false),
256 mFactor2Pruned(false),
257 mIsVectorImage(aImageKey->GetType() == imgIContainer::TYPE_VECTOR) {}
259 MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageSurfaceCache)
260 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageSurfaceCache)
262 typedef nsRefPtrHashtable<nsGenericHashKey<SurfaceKey>, CachedSurface>
263 SurfaceTable;
265 auto Values() const { return mSurfaces.Values(); }
266 uint32_t Count() const { return mSurfaces.Count(); }
267 bool IsEmpty() const { return mSurfaces.Count() == 0; }
269 size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
270 size_t bytes = aMallocSizeOf(this) +
271 mSurfaces.ShallowSizeOfExcludingThis(aMallocSizeOf);
272 for (const auto& value : Values()) {
273 bytes += value->ShallowSizeOfIncludingThis(aMallocSizeOf);
275 return bytes;
278 [[nodiscard]] bool Insert(NotNull<CachedSurface*> aSurface) {
279 MOZ_ASSERT(!mLocked || aSurface->IsPlaceholder() || aSurface->IsLocked(),
280 "Inserting an unlocked surface for a locked image");
281 const auto& surfaceKey = aSurface->GetSurfaceKey();
282 if (surfaceKey.Region()) {
283 // We don't allow substitutes for surfaces with regions, so we don't want
284 // to allow factor of 2 mode pruning to release these surfaces.
285 aSurface->SetCannotSubstitute();
287 return mSurfaces.InsertOrUpdate(surfaceKey, RefPtr<CachedSurface>{aSurface},
288 fallible);
291 already_AddRefed<CachedSurface> Remove(NotNull<CachedSurface*> aSurface) {
292 MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()),
293 "Should not be removing a surface we don't have");
295 RefPtr<CachedSurface> surface;
296 mSurfaces.Remove(aSurface->GetSurfaceKey(), getter_AddRefs(surface));
297 AfterMaybeRemove();
298 return surface.forget();
301 already_AddRefed<CachedSurface> Lookup(const SurfaceKey& aSurfaceKey,
302 bool aForAccess) {
303 RefPtr<CachedSurface> surface;
304 mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface));
306 if (aForAccess) {
307 if (surface) {
308 // We don't want to allow factor of 2 mode pruning to release surfaces
309 // for which the callers will accept no substitute.
310 surface->SetCannotSubstitute();
311 } else if (!mFactor2Mode) {
312 // If no exact match is found, and this is for use rather than internal
313 // accounting (i.e. insert and removal), we know this will trigger a
314 // decode. Make sure we switch now to factor of 2 mode if necessary.
315 MaybeSetFactor2Mode();
319 return surface.forget();
323 * @returns A tuple containing the best matching CachedSurface if available,
324 * a MatchType describing how the CachedSurface was selected, and
325 * an IntSize which is the size the caller should choose to decode
326 * at should it attempt to do so.
328 Tuple<already_AddRefed<CachedSurface>, MatchType, IntSize> LookupBestMatch(
329 const SurfaceKey& aIdealKey) {
330 // Try for an exact match first.
331 RefPtr<CachedSurface> exactMatch;
332 mSurfaces.Get(aIdealKey, getter_AddRefs(exactMatch));
333 if (exactMatch) {
334 if (exactMatch->IsDecoded()) {
335 return MakeTuple(exactMatch.forget(), MatchType::EXACT, IntSize());
337 } else if (aIdealKey.Region()) {
338 // We cannot substitute if we have a region. Allow it to create an exact
339 // match.
340 return MakeTuple(exactMatch.forget(), MatchType::NOT_FOUND, IntSize());
341 } else if (!mFactor2Mode) {
342 // If no exact match is found, and we are not in factor of 2 mode, then
343 // we know that we will trigger a decode because at best we will provide
344 // a substitute. Make sure we switch now to factor of 2 mode if necessary.
345 MaybeSetFactor2Mode();
348 // Try for a best match second, if using compact.
349 IntSize suggestedSize = SuggestedSize(aIdealKey.Size());
350 if (suggestedSize != aIdealKey.Size()) {
351 if (!exactMatch) {
352 SurfaceKey compactKey = aIdealKey.CloneWithSize(suggestedSize);
353 mSurfaces.Get(compactKey, getter_AddRefs(exactMatch));
354 if (exactMatch && exactMatch->IsDecoded()) {
355 MOZ_ASSERT(suggestedSize != aIdealKey.Size());
356 return MakeTuple(exactMatch.forget(),
357 MatchType::SUBSTITUTE_BECAUSE_BEST, suggestedSize);
362 // There's no perfect match, so find the best match we can.
363 RefPtr<CachedSurface> bestMatch;
364 for (const auto& value : Values()) {
365 NotNull<CachedSurface*> current = WrapNotNull(value);
366 const SurfaceKey& currentKey = current->GetSurfaceKey();
368 // We never match a placeholder or a surface with a region.
369 if (current->IsPlaceholder() || currentKey.Region()) {
370 continue;
372 // Matching the playback type and SVG context is required.
373 if (currentKey.Playback() != aIdealKey.Playback() ||
374 currentKey.SVGContext() != aIdealKey.SVGContext()) {
375 continue;
377 // Matching the flags is required.
378 if (currentKey.Flags() != aIdealKey.Flags()) {
379 continue;
381 // Anything is better than nothing! (Within the constraints we just
382 // checked, of course.)
383 if (!bestMatch) {
384 bestMatch = current;
385 continue;
388 MOZ_ASSERT(bestMatch, "Should have a current best match");
390 // Always prefer completely decoded surfaces.
391 bool bestMatchIsDecoded = bestMatch->IsDecoded();
392 if (bestMatchIsDecoded && !current->IsDecoded()) {
393 continue;
395 if (!bestMatchIsDecoded && current->IsDecoded()) {
396 bestMatch = current;
397 continue;
400 SurfaceKey bestMatchKey = bestMatch->GetSurfaceKey();
401 if (CompareArea(aIdealKey.Size(), bestMatchKey.Size(),
402 currentKey.Size())) {
403 bestMatch = current;
407 MatchType matchType;
408 if (bestMatch) {
409 if (!exactMatch) {
410 // No exact match, neither ideal nor factor of 2.
411 MOZ_ASSERT(suggestedSize != bestMatch->GetSurfaceKey().Size(),
412 "No exact match despite the fact the sizes match!");
413 matchType = MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND;
414 } else if (exactMatch != bestMatch) {
415 // The exact match is still decoding, but we found a substitute.
416 matchType = MatchType::SUBSTITUTE_BECAUSE_PENDING;
417 } else if (aIdealKey.Size() != bestMatch->GetSurfaceKey().Size()) {
418 // The best factor of 2 match is still decoding, but the best we've got.
419 MOZ_ASSERT(suggestedSize != aIdealKey.Size());
420 MOZ_ASSERT(mFactor2Mode || mIsVectorImage);
421 matchType = MatchType::SUBSTITUTE_BECAUSE_BEST;
422 } else {
423 // The exact match is still decoding, but it's the best we've got.
424 matchType = MatchType::EXACT;
426 } else {
427 if (exactMatch) {
428 // We found an "exact match"; it must have been a placeholder.
429 MOZ_ASSERT(exactMatch->IsPlaceholder());
430 matchType = MatchType::PENDING;
431 } else {
432 // We couldn't find an exact match *or* a substitute.
433 matchType = MatchType::NOT_FOUND;
437 return MakeTuple(bestMatch.forget(), matchType, suggestedSize);
440 void MaybeSetFactor2Mode() {
441 MOZ_ASSERT(!mFactor2Mode);
443 // Typically an image cache will not have too many size-varying surfaces, so
444 // if we exceed the given threshold, we should consider using a subset.
445 int32_t thresholdSurfaces =
446 StaticPrefs::image_cache_factor2_threshold_surfaces();
447 if (thresholdSurfaces < 0 ||
448 mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
449 return;
452 // Determine how many native surfaces this image has. If it is zero, and it
453 // is a vector image, then we should impute a single native size. Otherwise,
454 // it may be zero because we don't know yet, or the image has an error, or
455 // it isn't supported.
456 NotNull<CachedSurface*> current =
457 WrapNotNull(mSurfaces.ConstIter().UserData());
458 Image* image = static_cast<Image*>(current->GetImageKey());
459 size_t nativeSizes = image->GetNativeSizesLength();
460 if (mIsVectorImage) {
461 MOZ_ASSERT(nativeSizes == 0);
462 nativeSizes = 1;
463 } else if (nativeSizes == 0) {
464 return;
467 // Increase the threshold by the number of native sizes. This ensures that
468 // we do not prevent decoding of the image at all its native sizes. It does
469 // not guarantee we will provide a surface at that size however (i.e. many
470 // other sized surfaces are requested, in addition to the native sizes).
471 thresholdSurfaces += nativeSizes;
472 if (mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
473 return;
476 // We have a valid size, we can change modes.
477 mFactor2Mode = true;
480 template <typename Function>
481 void Prune(Function&& aRemoveCallback) {
482 if (!mFactor2Mode || mFactor2Pruned) {
483 return;
486 // Attempt to discard any surfaces which are not factor of 2 and the best
487 // factor of 2 match exists.
488 bool hasNotFactorSize = false;
489 for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
490 NotNull<CachedSurface*> current = WrapNotNull(iter.UserData());
491 const SurfaceKey& currentKey = current->GetSurfaceKey();
492 const IntSize& currentSize = currentKey.Size();
494 // First we check if someone requested this size and would not accept
495 // an alternatively sized surface.
496 if (current->CannotSubstitute()) {
497 continue;
500 // Next we find the best factor of 2 size for this surface. If this
501 // surface is a factor of 2 size, then we want to keep it.
502 IntSize bestSize = SuggestedSize(currentSize);
503 if (bestSize == currentSize) {
504 continue;
507 // Check the cache for a surface with the same parameters except for the
508 // size which uses the closest factor of 2 size.
509 SurfaceKey compactKey = currentKey.CloneWithSize(bestSize);
510 RefPtr<CachedSurface> compactMatch;
511 mSurfaces.Get(compactKey, getter_AddRefs(compactMatch));
512 if (compactMatch && compactMatch->IsDecoded()) {
513 aRemoveCallback(current);
514 iter.Remove();
515 } else {
516 hasNotFactorSize = true;
520 // We have no surfaces that are not factor of 2 sized, so we can stop
521 // pruning henceforth, because we avoid the insertion of new surfaces that
522 // don't match our sizing set (unless the caller won't accept a
523 // substitution.)
524 if (!hasNotFactorSize) {
525 mFactor2Pruned = true;
528 // We should never leave factor of 2 mode due to pruning in of itself, but
529 // if we discarded surfaces due to the volatile buffers getting released,
530 // it is possible.
531 AfterMaybeRemove();
534 template <typename Function>
535 bool Invalidate(Function&& aRemoveCallback) {
536 // Remove all non-blob recordings from the cache. Invalidate any blob
537 // recordings.
538 bool foundRecording = false;
539 for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
540 NotNull<CachedSurface*> current = WrapNotNull(iter.UserData());
542 if (current->GetSurfaceKey().Flags() & SurfaceFlags::RECORD_BLOB) {
543 foundRecording = true;
544 current->InvalidateRecording();
545 continue;
548 aRemoveCallback(current);
549 iter.Remove();
552 AfterMaybeRemove();
553 return foundRecording;
556 IntSize SuggestedSize(const IntSize& aSize) const {
557 IntSize suggestedSize = SuggestedSizeInternal(aSize);
558 if (mIsVectorImage) {
559 suggestedSize = SurfaceCache::ClampVectorSize(suggestedSize);
561 return suggestedSize;
564 IntSize SuggestedSizeInternal(const IntSize& aSize) const {
565 // When not in factor of 2 mode, we can always decode at the given size.
566 if (!mFactor2Mode) {
567 return aSize;
570 // We cannot enter factor of 2 mode unless we have a minimum number of
571 // surfaces, and we should have left it if the cache was emptied.
572 if (MOZ_UNLIKELY(IsEmpty())) {
573 MOZ_ASSERT_UNREACHABLE("Should not be empty and in factor of 2 mode!");
574 return aSize;
577 // This bit of awkwardness gets the largest native size of the image.
578 NotNull<CachedSurface*> firstSurface =
579 WrapNotNull(mSurfaces.ConstIter().UserData());
580 Image* image = static_cast<Image*>(firstSurface->GetImageKey());
581 IntSize factorSize;
582 if (NS_FAILED(image->GetWidth(&factorSize.width)) ||
583 NS_FAILED(image->GetHeight(&factorSize.height)) ||
584 factorSize.IsEmpty()) {
585 // Valid vector images may have a default size of 0x0. In that case, just
586 // assume a default size of 100x100 and apply the intrinsic ratio if
587 // available. If our guess was too small, don't use factor-of-scaling.
588 MOZ_ASSERT(mIsVectorImage);
589 factorSize = IntSize(100, 100);
590 Maybe<AspectRatio> aspectRatio = image->GetIntrinsicRatio();
591 if (aspectRatio && *aspectRatio) {
592 factorSize.width =
593 NSToIntRound(aspectRatio->ApplyToFloat(float(factorSize.height)));
594 if (factorSize.IsEmpty()) {
595 return aSize;
600 if (mIsVectorImage) {
601 // Ensure the aspect ratio matches the native size before forcing the
602 // caller to accept a factor of 2 size. The difference between the aspect
603 // ratios is:
605 // delta = nativeWidth/nativeHeight - desiredWidth/desiredHeight
607 // delta*nativeHeight*desiredHeight = nativeWidth*desiredHeight
608 // - desiredWidth*nativeHeight
610 // Using the maximum accepted delta as a constant, we can avoid the
611 // floating point division and just compare after some integer ops.
612 int32_t delta =
613 factorSize.width * aSize.height - aSize.width * factorSize.height;
614 int32_t maxDelta = (factorSize.height * aSize.height) >> 4;
615 if (delta > maxDelta || delta < -maxDelta) {
616 return aSize;
619 // If the requested size is bigger than the native size, we actually need
620 // to grow the native size instead of shrinking it.
621 if (factorSize.width < aSize.width) {
622 do {
623 IntSize candidate(factorSize.width * 2, factorSize.height * 2);
624 if (!SurfaceCache::IsLegalSize(candidate)) {
625 break;
628 factorSize = candidate;
629 } while (factorSize.width < aSize.width);
631 return factorSize;
634 // Otherwise we can find the best fit as normal.
637 // Start with the native size as the best first guess.
638 IntSize bestSize = factorSize;
639 factorSize.width /= 2;
640 factorSize.height /= 2;
642 while (!factorSize.IsEmpty()) {
643 if (!CompareArea(aSize, bestSize, factorSize)) {
644 // This size is not better than the last. Since we proceed from largest
645 // to smallest, we know that the next size will not be better if the
646 // previous size was rejected. Break early.
647 break;
650 // The current factor of 2 size is better than the last selected size.
651 bestSize = factorSize;
652 factorSize.width /= 2;
653 factorSize.height /= 2;
656 return bestSize;
659 bool CompareArea(const IntSize& aIdealSize, const IntSize& aBestSize,
660 const IntSize& aSize) const {
661 // Compare sizes. We use an area-based heuristic here instead of computing a
662 // truly optimal answer, since it seems very unlikely to make a difference
663 // for realistic sizes.
664 int64_t idealArea = AreaOfIntSize(aIdealSize);
665 int64_t currentArea = AreaOfIntSize(aSize);
666 int64_t bestMatchArea = AreaOfIntSize(aBestSize);
668 // If the best match is smaller than the ideal size, prefer bigger sizes.
669 if (bestMatchArea < idealArea) {
670 if (currentArea > bestMatchArea) {
671 return true;
673 return false;
676 // Other, prefer sizes closer to the ideal size, but still not smaller.
677 if (idealArea <= currentArea && currentArea < bestMatchArea) {
678 return true;
681 // This surface isn't an improvement over the current best match.
682 return false;
685 template <typename Function>
686 void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
687 MallocSizeOf aMallocSizeOf,
688 Function&& aRemoveCallback) {
689 CachedSurface::SurfaceMemoryReport report(aCounters, aMallocSizeOf);
690 for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
691 NotNull<CachedSurface*> surface = WrapNotNull(iter.UserData());
693 // We don't need the drawable surface for ourselves, but adding a surface
694 // to the report will trigger this indirectly. If the surface was
695 // discarded by the OS because it was in volatile memory, we should remove
696 // it from the cache immediately rather than include it in the report.
697 DrawableSurface drawableSurface;
698 if (!surface->IsPlaceholder()) {
699 drawableSurface = surface->GetDrawableSurface();
700 if (!drawableSurface) {
701 aRemoveCallback(surface);
702 iter.Remove();
703 continue;
707 const IntSize& size = surface->GetSurfaceKey().Size();
708 bool factor2Size = false;
709 if (mFactor2Mode) {
710 factor2Size = (size == SuggestedSize(size));
712 report.Add(surface, factor2Size);
715 AfterMaybeRemove();
718 void SetLocked(bool aLocked) { mLocked = aLocked; }
719 bool IsLocked() const { return mLocked; }
721 private:
722 void AfterMaybeRemove() {
723 if (IsEmpty() && mFactor2Mode) {
724 // The last surface for this cache was removed. This can happen if the
725 // surface was stored in a volatile buffer and got purged, or the surface
726 // expired from the cache. If the cache itself lingers for some reason
727 // (e.g. in the process of performing a lookup, the cache itself is
728 // locked), then we need to reset the factor of 2 state because it
729 // requires at least one surface present to get the native size
730 // information from the image.
731 mFactor2Mode = mFactor2Pruned = false;
735 SurfaceTable mSurfaces;
737 bool mLocked;
739 // True in "factor of 2" mode.
740 bool mFactor2Mode;
742 // True if all non-factor of 2 surfaces have been removed from the cache. Note
743 // that this excludes unsubstitutable sizes.
744 bool mFactor2Pruned;
746 // True if the surfaces are produced from a vector image. If so, it must match
747 // the aspect ratio when using factor of 2 mode.
748 bool mIsVectorImage;
752 * SurfaceCacheImpl is responsible for determining which surfaces will be cached
753 * and managing the surface cache data structures. Rather than interact with
754 * SurfaceCacheImpl directly, client code interacts with SurfaceCache, which
755 * maintains high-level invariants and encapsulates the details of the surface
756 * cache's implementation.
758 class SurfaceCacheImpl final : public nsIMemoryReporter {
759 public:
760 NS_DECL_ISUPPORTS
762 SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS,
763 uint32_t aSurfaceCacheDiscardFactor,
764 uint32_t aSurfaceCacheSize)
765 : mExpirationTracker(aSurfaceCacheExpirationTimeMS),
766 mMemoryPressureObserver(new MemoryPressureObserver),
767 mDiscardFactor(aSurfaceCacheDiscardFactor),
768 mMaxCost(aSurfaceCacheSize),
769 mAvailableCost(aSurfaceCacheSize),
770 mLockedCost(0),
771 mOverflowCount(0),
772 mAlreadyPresentCount(0),
773 mTableFailureCount(0),
774 mTrackingFailureCount(0) {
775 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
776 if (os) {
777 os->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
781 private:
782 virtual ~SurfaceCacheImpl() {
783 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
784 if (os) {
785 os->RemoveObserver(mMemoryPressureObserver, "memory-pressure");
788 UnregisterWeakMemoryReporter(this);
791 public:
792 void InitMemoryReporter() { RegisterWeakMemoryReporter(this); }
794 InsertOutcome Insert(NotNull<ISurfaceProvider*> aProvider, bool aSetAvailable,
795 const StaticMutexAutoLock& aAutoLock) {
796 // If this is a duplicate surface, refuse to replace the original.
797 // XXX(seth): Calling Lookup() and then RemoveEntry() does the lookup
798 // twice. We'll make this more efficient in bug 1185137.
799 LookupResult result =
800 Lookup(aProvider->GetImageKey(), aProvider->GetSurfaceKey(), aAutoLock,
801 /* aMarkUsed = */ false);
802 if (MOZ_UNLIKELY(result)) {
803 mAlreadyPresentCount++;
804 return InsertOutcome::FAILURE_ALREADY_PRESENT;
807 if (result.Type() == MatchType::PENDING) {
808 RemoveEntry(aProvider->GetImageKey(), aProvider->GetSurfaceKey(),
809 aAutoLock);
812 MOZ_ASSERT(result.Type() == MatchType::NOT_FOUND ||
813 result.Type() == MatchType::PENDING,
814 "A LookupResult with no surface should be NOT_FOUND or PENDING");
816 // If this is bigger than we can hold after discarding everything we can,
817 // refuse to cache it.
818 Cost cost = aProvider->LogicalSizeInBytes();
819 if (MOZ_UNLIKELY(!CanHoldAfterDiscarding(cost))) {
820 mOverflowCount++;
821 return InsertOutcome::FAILURE;
824 // Remove elements in order of cost until we can fit this in the cache. Note
825 // that locked surfaces aren't in mCosts, so we never remove them here.
826 while (cost > mAvailableCost) {
827 MOZ_ASSERT(!mCosts.IsEmpty(),
828 "Removed everything and it still won't fit");
829 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
830 aAutoLock);
833 // Locate the appropriate per-image cache. If there's not an existing cache
834 // for this image, create it.
835 const ImageKey imageKey = aProvider->GetImageKey();
836 RefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
837 if (!cache) {
838 cache = new ImageSurfaceCache(imageKey);
839 if (!mImageCaches.InsertOrUpdate(aProvider->GetImageKey(), RefPtr{cache},
840 fallible)) {
841 mTableFailureCount++;
842 return InsertOutcome::FAILURE;
846 // If we were asked to mark the cache entry available, do so.
847 if (aSetAvailable) {
848 aProvider->Availability().SetAvailable();
851 auto surface = MakeNotNull<RefPtr<CachedSurface>>(aProvider);
853 // We require that locking succeed if the image is locked and we're not
854 // inserting a placeholder; the caller may need to know this to handle
855 // errors correctly.
856 bool mustLock = cache->IsLocked() && !surface->IsPlaceholder();
857 if (mustLock) {
858 surface->SetLocked(true);
859 if (!surface->IsLocked()) {
860 return InsertOutcome::FAILURE;
864 // Insert.
865 MOZ_ASSERT(cost <= mAvailableCost, "Inserting despite too large a cost");
866 if (!cache->Insert(surface)) {
867 mTableFailureCount++;
868 if (mustLock) {
869 surface->SetLocked(false);
871 return InsertOutcome::FAILURE;
874 if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
875 MOZ_ASSERT(!mustLock);
876 Remove(surface, /* aStopTracking */ false, aAutoLock);
877 return InsertOutcome::FAILURE;
880 return InsertOutcome::SUCCESS;
883 void Remove(NotNull<CachedSurface*> aSurface, bool aStopTracking,
884 const StaticMutexAutoLock& aAutoLock) {
885 ImageKey imageKey = aSurface->GetImageKey();
887 RefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
888 MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache");
890 // If the surface was not a placeholder, tell its image that we discarded
891 // it.
892 if (!aSurface->IsPlaceholder()) {
893 static_cast<Image*>(imageKey)->OnSurfaceDiscarded(
894 aSurface->GetSurfaceKey());
897 // If we failed during StartTracking, we can skip this step.
898 if (aStopTracking) {
899 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
902 // Individual surfaces must be freed outside the lock.
903 mCachedSurfacesDiscard.AppendElement(cache->Remove(aSurface));
905 MaybeRemoveEmptyCache(imageKey, cache);
908 bool StartTracking(NotNull<CachedSurface*> aSurface,
909 const StaticMutexAutoLock& aAutoLock) {
910 CostEntry costEntry = aSurface->GetCostEntry();
911 MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost,
912 "Cost too large and the caller didn't catch it");
914 if (aSurface->IsLocked()) {
915 mLockedCost += costEntry.GetCost();
916 MOZ_ASSERT(mLockedCost <= mMaxCost, "Locked more than we can hold?");
917 } else {
918 if (NS_WARN_IF(!mCosts.InsertElementSorted(costEntry, fallible))) {
919 mTrackingFailureCount++;
920 return false;
923 // This may fail during XPCOM shutdown, so we need to ensure the object is
924 // tracked before calling RemoveObject in StopTracking.
925 nsresult rv = mExpirationTracker.AddObjectLocked(aSurface, aAutoLock);
926 if (NS_WARN_IF(NS_FAILED(rv))) {
927 DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
928 MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
929 mTrackingFailureCount++;
930 return false;
934 mAvailableCost -= costEntry.GetCost();
935 return true;
938 void StopTracking(NotNull<CachedSurface*> aSurface, bool aIsTracked,
939 const StaticMutexAutoLock& aAutoLock) {
940 CostEntry costEntry = aSurface->GetCostEntry();
942 if (aSurface->IsLocked()) {
943 MOZ_ASSERT(mLockedCost >= costEntry.GetCost(), "Costs don't balance");
944 mLockedCost -= costEntry.GetCost();
945 // XXX(seth): It'd be nice to use an O(log n) lookup here. This is O(n).
946 MOZ_ASSERT(!mCosts.Contains(costEntry),
947 "Shouldn't have a cost entry for a locked surface");
948 } else {
949 if (MOZ_LIKELY(aSurface->GetExpirationState()->IsTracked())) {
950 MOZ_ASSERT(aIsTracked, "Expiration-tracking a surface unexpectedly!");
951 mExpirationTracker.RemoveObjectLocked(aSurface, aAutoLock);
952 } else {
953 // Our call to AddObject must have failed in StartTracking; most likely
954 // we're in XPCOM shutdown right now.
955 MOZ_ASSERT(!aIsTracked, "Not expiration-tracking an unlocked surface!");
958 DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
959 MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
962 mAvailableCost += costEntry.GetCost();
963 MOZ_ASSERT(mAvailableCost <= mMaxCost,
964 "More available cost than we started with");
967 LookupResult Lookup(const ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
968 const StaticMutexAutoLock& aAutoLock, bool aMarkUsed) {
969 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
970 if (!cache) {
971 // No cached surfaces for this image.
972 return LookupResult(MatchType::NOT_FOUND);
975 RefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey, aMarkUsed);
976 if (!surface) {
977 // Lookup in the per-image cache missed.
978 return LookupResult(MatchType::NOT_FOUND);
981 if (surface->IsPlaceholder()) {
982 return LookupResult(MatchType::PENDING);
985 DrawableSurface drawableSurface = surface->GetDrawableSurface();
986 if (!drawableSurface) {
987 // The surface was released by the operating system. Remove the cache
988 // entry as well.
989 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
990 return LookupResult(MatchType::NOT_FOUND);
993 if (aMarkUsed &&
994 !MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
995 Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
996 return LookupResult(MatchType::NOT_FOUND);
999 MOZ_ASSERT(surface->GetSurfaceKey() == aSurfaceKey,
1000 "Lookup() not returning an exact match?");
1001 return LookupResult(std::move(drawableSurface), MatchType::EXACT);
1004 LookupResult LookupBestMatch(const ImageKey aImageKey,
1005 const SurfaceKey& aSurfaceKey,
1006 const StaticMutexAutoLock& aAutoLock,
1007 bool aMarkUsed) {
1008 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1009 if (!cache) {
1010 // No cached surfaces for this image.
1011 return LookupResult(
1012 MatchType::NOT_FOUND,
1013 SurfaceCache::ClampSize(aImageKey, aSurfaceKey.Size()));
1016 // Repeatedly look up the best match, trying again if the resulting surface
1017 // has been freed by the operating system, until we can either lock a
1018 // surface for drawing or there are no matching surfaces left.
1019 // XXX(seth): This is O(N^2), but N is expected to be very small. If we
1020 // encounter a performance problem here we can revisit this.
1022 RefPtr<CachedSurface> surface;
1023 DrawableSurface drawableSurface;
1024 MatchType matchType = MatchType::NOT_FOUND;
1025 IntSize suggestedSize;
1026 while (true) {
1027 Tie(surface, matchType, suggestedSize) =
1028 cache->LookupBestMatch(aSurfaceKey);
1030 if (!surface) {
1031 return LookupResult(
1032 matchType, suggestedSize); // Lookup in the per-image cache missed.
1035 drawableSurface = surface->GetDrawableSurface();
1036 if (drawableSurface) {
1037 break;
1040 // The surface was released by the operating system. Remove the cache
1041 // entry as well.
1042 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
1045 MOZ_ASSERT_IF(matchType == MatchType::EXACT,
1046 surface->GetSurfaceKey() == aSurfaceKey);
1047 MOZ_ASSERT_IF(
1048 matchType == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND ||
1049 matchType == MatchType::SUBSTITUTE_BECAUSE_PENDING,
1050 surface->GetSurfaceKey().Region() == aSurfaceKey.Region() &&
1051 surface->GetSurfaceKey().SVGContext() == aSurfaceKey.SVGContext() &&
1052 surface->GetSurfaceKey().Playback() == aSurfaceKey.Playback() &&
1053 surface->GetSurfaceKey().Flags() == aSurfaceKey.Flags());
1055 if (matchType == MatchType::EXACT ||
1056 matchType == MatchType::SUBSTITUTE_BECAUSE_BEST) {
1057 if (aMarkUsed &&
1058 !MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
1059 Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
1063 return LookupResult(std::move(drawableSurface), matchType, suggestedSize);
1066 bool CanHold(const Cost aCost) const { return aCost <= mMaxCost; }
1068 size_t MaximumCapacity() const { return size_t(mMaxCost); }
1070 void SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider,
1071 const StaticMutexAutoLock& aAutoLock) {
1072 if (!aProvider->Availability().IsPlaceholder()) {
1073 MOZ_ASSERT_UNREACHABLE("Calling SurfaceAvailable on non-placeholder");
1074 return;
1077 // Reinsert the provider, requesting that Insert() mark it available. This
1078 // may or may not succeed, depending on whether some other decoder has
1079 // beaten us to the punch and inserted a non-placeholder version of this
1080 // surface first, but it's fine either way.
1081 // XXX(seth): This could be implemented more efficiently; we should be able
1082 // to just update our data structures without reinserting.
1083 Insert(aProvider, /* aSetAvailable = */ true, aAutoLock);
1086 void LockImage(const ImageKey aImageKey) {
1087 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1088 if (!cache) {
1089 cache = new ImageSurfaceCache(aImageKey);
1090 mImageCaches.InsertOrUpdate(aImageKey, RefPtr{cache});
1093 cache->SetLocked(true);
1095 // We don't relock this image's existing surfaces right away; instead, the
1096 // image should arrange for Lookup() to touch them if they are still useful.
1099 void UnlockImage(const ImageKey aImageKey,
1100 const StaticMutexAutoLock& aAutoLock) {
1101 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1102 if (!cache || !cache->IsLocked()) {
1103 return; // Already unlocked.
1106 cache->SetLocked(false);
1107 DoUnlockSurfaces(WrapNotNull(cache), /* aStaticOnly = */ false, aAutoLock);
1110 void UnlockEntries(const ImageKey aImageKey,
1111 const StaticMutexAutoLock& aAutoLock) {
1112 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1113 if (!cache || !cache->IsLocked()) {
1114 return; // Already unlocked.
1117 // (Note that we *don't* unlock the per-image cache here; that's the
1118 // difference between this and UnlockImage.)
1119 DoUnlockSurfaces(WrapNotNull(cache),
1120 /* aStaticOnly = */
1121 !StaticPrefs::image_mem_animated_discardable_AtStartup(),
1122 aAutoLock);
1125 already_AddRefed<ImageSurfaceCache> RemoveImage(
1126 const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock) {
1127 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1128 if (!cache) {
1129 return nullptr; // No cached surfaces for this image, so nothing to do.
1132 // Discard all of the cached surfaces for this image.
1133 // XXX(seth): This is O(n^2) since for each item in the cache we are
1134 // removing an element from the costs array. Since n is expected to be
1135 // small, performance should be good, but if usage patterns change we should
1136 // change the data structure used for mCosts.
1137 for (const auto& value : cache->Values()) {
1138 StopTracking(WrapNotNull(value),
1139 /* aIsTracked */ true, aAutoLock);
1142 // The per-image cache isn't needed anymore, so remove it as well.
1143 // This implicitly unlocks the image if it was locked.
1144 mImageCaches.Remove(aImageKey);
1146 // Since we did not actually remove any of the surfaces from the cache
1147 // itself, only stopped tracking them, we should free it outside the lock.
1148 return cache.forget();
1151 void PruneImage(const ImageKey aImageKey,
1152 const StaticMutexAutoLock& aAutoLock) {
1153 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1154 if (!cache) {
1155 return; // No cached surfaces for this image, so nothing to do.
1158 cache->Prune([this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
1159 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1160 // Individual surfaces must be freed outside the lock.
1161 mCachedSurfacesDiscard.AppendElement(aSurface);
1164 MaybeRemoveEmptyCache(aImageKey, cache);
1167 bool InvalidateImage(const ImageKey aImageKey,
1168 const StaticMutexAutoLock& aAutoLock) {
1169 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1170 if (!cache) {
1171 return false; // No cached surfaces for this image, so nothing to do.
1174 bool rv = cache->Invalidate(
1175 [this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
1176 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1177 // Individual surfaces must be freed outside the lock.
1178 mCachedSurfacesDiscard.AppendElement(aSurface);
1181 MaybeRemoveEmptyCache(aImageKey, cache);
1182 return rv;
1185 void DiscardAll(const StaticMutexAutoLock& aAutoLock) {
1186 // Remove in order of cost because mCosts is an array and the other data
1187 // structures are all hash tables. Note that locked surfaces are not
1188 // removed, since they aren't present in mCosts.
1189 while (!mCosts.IsEmpty()) {
1190 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
1191 aAutoLock);
1195 void DiscardForMemoryPressure(const StaticMutexAutoLock& aAutoLock) {
1196 // Compute our discardable cost. Since locked surfaces aren't discardable,
1197 // we exclude them.
1198 const Cost discardableCost = (mMaxCost - mAvailableCost) - mLockedCost;
1199 MOZ_ASSERT(discardableCost <= mMaxCost, "Discardable cost doesn't add up");
1201 // Our target is to raise our available cost by (1 / mDiscardFactor) of our
1202 // discardable cost - in other words, we want to end up with about
1203 // (discardableCost / mDiscardFactor) fewer bytes stored in the surface
1204 // cache after we're done.
1205 const Cost targetCost = mAvailableCost + (discardableCost / mDiscardFactor);
1207 if (targetCost > mMaxCost - mLockedCost) {
1208 MOZ_ASSERT_UNREACHABLE("Target cost is more than we can discard");
1209 DiscardAll(aAutoLock);
1210 return;
1213 // Discard surfaces until we've reduced our cost to our target cost.
1214 while (mAvailableCost < targetCost) {
1215 MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and still not done");
1216 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
1217 aAutoLock);
1221 void TakeDiscard(nsTArray<RefPtr<CachedSurface>>& aDiscard,
1222 const StaticMutexAutoLock& aAutoLock) {
1223 MOZ_ASSERT(aDiscard.IsEmpty());
1224 aDiscard = std::move(mCachedSurfacesDiscard);
1227 void LockSurface(NotNull<CachedSurface*> aSurface,
1228 const StaticMutexAutoLock& aAutoLock) {
1229 if (aSurface->IsPlaceholder() || aSurface->IsLocked()) {
1230 return;
1233 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1235 // Lock the surface. This can fail.
1236 aSurface->SetLocked(true);
1237 DebugOnly<bool> tracked = StartTracking(aSurface, aAutoLock);
1238 MOZ_ASSERT(tracked);
1241 size_t ShallowSizeOfIncludingThis(
1242 MallocSizeOf aMallocSizeOf, const StaticMutexAutoLock& aAutoLock) const {
1243 size_t bytes =
1244 aMallocSizeOf(this) + mCosts.ShallowSizeOfExcludingThis(aMallocSizeOf) +
1245 mImageCaches.ShallowSizeOfExcludingThis(aMallocSizeOf) +
1246 mCachedSurfacesDiscard.ShallowSizeOfExcludingThis(aMallocSizeOf) +
1247 mExpirationTracker.ShallowSizeOfExcludingThis(aMallocSizeOf);
1248 for (const auto& data : mImageCaches.Values()) {
1249 bytes += data->ShallowSizeOfIncludingThis(aMallocSizeOf);
1251 return bytes;
1254 NS_IMETHOD
1255 CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
1256 bool aAnonymize) override {
1257 StaticMutexAutoLock lock(sInstanceMutex);
1259 uint32_t lockedImageCount = 0;
1260 uint32_t totalSurfaceCount = 0;
1261 uint32_t lockedSurfaceCount = 0;
1262 for (const auto& cache : mImageCaches.Values()) {
1263 totalSurfaceCount += cache->Count();
1264 if (cache->IsLocked()) {
1265 ++lockedImageCount;
1267 for (const auto& value : cache->Values()) {
1268 if (value->IsLocked()) {
1269 ++lockedSurfaceCount;
1274 // clang-format off
1275 // We have explicit memory reporting for the surface cache which is more
1276 // accurate than the cost metrics we report here, but these metrics are
1277 // still useful to report, since they control the cache's behavior.
1278 MOZ_COLLECT_REPORT(
1279 "explicit/images/cache/overhead", KIND_HEAP, UNITS_BYTES,
1280 ShallowSizeOfIncludingThis(SurfaceCacheMallocSizeOf, lock),
1281 "Memory used by the surface cache data structures, excluding surface data.");
1283 MOZ_COLLECT_REPORT(
1284 "imagelib-surface-cache-estimated-total",
1285 KIND_OTHER, UNITS_BYTES, (mMaxCost - mAvailableCost),
1286 "Estimated total memory used by the imagelib surface cache.");
1288 MOZ_COLLECT_REPORT(
1289 "imagelib-surface-cache-estimated-locked",
1290 KIND_OTHER, UNITS_BYTES, mLockedCost,
1291 "Estimated memory used by locked surfaces in the imagelib surface cache.");
1293 MOZ_COLLECT_REPORT(
1294 "imagelib-surface-cache-tracked-cost-count",
1295 KIND_OTHER, UNITS_COUNT, mCosts.Length(),
1296 "Total number of surfaces tracked for cost (and expiry) in the imagelib surface cache.");
1298 MOZ_COLLECT_REPORT(
1299 "imagelib-surface-cache-tracked-expiry-count",
1300 KIND_OTHER, UNITS_COUNT, mExpirationTracker.Length(lock),
1301 "Total number of surfaces tracked for expiry (and cost) in the imagelib surface cache.");
1303 MOZ_COLLECT_REPORT(
1304 "imagelib-surface-cache-image-count",
1305 KIND_OTHER, UNITS_COUNT, mImageCaches.Count(),
1306 "Total number of images in the imagelib surface cache.");
1308 MOZ_COLLECT_REPORT(
1309 "imagelib-surface-cache-locked-image-count",
1310 KIND_OTHER, UNITS_COUNT, lockedImageCount,
1311 "Total number of locked images in the imagelib surface cache.");
1313 MOZ_COLLECT_REPORT(
1314 "imagelib-surface-cache-image-surface-count",
1315 KIND_OTHER, UNITS_COUNT, totalSurfaceCount,
1316 "Total number of surfaces in the imagelib surface cache.");
1318 MOZ_COLLECT_REPORT(
1319 "imagelib-surface-cache-locked-surfaces-count",
1320 KIND_OTHER, UNITS_COUNT, lockedSurfaceCount,
1321 "Total number of locked surfaces in the imagelib surface cache.");
1323 MOZ_COLLECT_REPORT(
1324 "imagelib-surface-cache-overflow-count",
1325 KIND_OTHER, UNITS_COUNT, mOverflowCount,
1326 "Count of how many times the surface cache has hit its capacity and been "
1327 "unable to insert a new surface.");
1329 MOZ_COLLECT_REPORT(
1330 "imagelib-surface-cache-tracking-failure-count",
1331 KIND_OTHER, UNITS_COUNT, mTrackingFailureCount,
1332 "Count of how many times the surface cache has failed to begin tracking a "
1333 "given surface.");
1335 MOZ_COLLECT_REPORT(
1336 "imagelib-surface-cache-already-present-count",
1337 KIND_OTHER, UNITS_COUNT, mAlreadyPresentCount,
1338 "Count of how many times the surface cache has failed to insert a surface "
1339 "because it is already present.");
1341 MOZ_COLLECT_REPORT(
1342 "imagelib-surface-cache-table-failure-count",
1343 KIND_OTHER, UNITS_COUNT, mTableFailureCount,
1344 "Count of how many times the surface cache has failed to insert a surface "
1345 "because a hash table could not accept an entry.");
1346 // clang-format on
1348 return NS_OK;
1351 void CollectSizeOfSurfaces(const ImageKey aImageKey,
1352 nsTArray<SurfaceMemoryCounter>& aCounters,
1353 MallocSizeOf aMallocSizeOf,
1354 const StaticMutexAutoLock& aAutoLock) {
1355 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1356 if (!cache) {
1357 return; // No surfaces for this image.
1360 // Report all surfaces in the per-image cache.
1361 cache->CollectSizeOfSurfaces(
1362 aCounters, aMallocSizeOf,
1363 [this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
1364 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1365 // Individual surfaces must be freed outside the lock.
1366 mCachedSurfacesDiscard.AppendElement(aSurface);
1369 MaybeRemoveEmptyCache(aImageKey, cache);
1372 void ReleaseImageOnMainThread(already_AddRefed<image::Image>&& aImage,
1373 const StaticMutexAutoLock& aAutoLock) {
1374 RefPtr<image::Image> image = aImage;
1375 if (!image) {
1376 return;
1379 bool needsDispatch = mReleasingImagesOnMainThread.IsEmpty();
1380 mReleasingImagesOnMainThread.AppendElement(image);
1382 if (!needsDispatch || gXPCOMThreadsShutDown) {
1383 // Either there is already a ongoing task for ClearReleasingImages() or
1384 // it's too late in shutdown to dispatch.
1385 return;
1388 NS_DispatchToMainThread(NS_NewRunnableFunction(
1389 "SurfaceCacheImpl::ReleaseImageOnMainThread",
1390 []() -> void { SurfaceCache::ClearReleasingImages(); }));
1393 void TakeReleasingImages(nsTArray<RefPtr<image::Image>>& aImage,
1394 const StaticMutexAutoLock& aAutoLock) {
1395 MOZ_ASSERT(NS_IsMainThread());
1396 aImage.SwapElements(mReleasingImagesOnMainThread);
1399 private:
1400 already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey) {
1401 RefPtr<ImageSurfaceCache> imageCache;
1402 mImageCaches.Get(aImageKey, getter_AddRefs(imageCache));
1403 return imageCache.forget();
1406 void MaybeRemoveEmptyCache(const ImageKey aImageKey,
1407 ImageSurfaceCache* aCache) {
1408 // Remove the per-image cache if it's unneeded now. Keep it if the image is
1409 // locked, since the per-image cache is where we store that state. Note that
1410 // we don't push it into mImageCachesDiscard because all of its surfaces
1411 // have been removed, so it is safe to free while holding the lock.
1412 if (aCache->IsEmpty() && !aCache->IsLocked()) {
1413 mImageCaches.Remove(aImageKey);
1417 // This is similar to CanHold() except that it takes into account the costs of
1418 // locked surfaces. It's used internally in Insert(), but it's not exposed
1419 // publicly because we permit multithreaded access to the surface cache, which
1420 // means that the result would be meaningless: another thread could insert a
1421 // surface or lock an image at any time.
1422 bool CanHoldAfterDiscarding(const Cost aCost) const {
1423 return aCost <= mMaxCost - mLockedCost;
1426 bool MarkUsed(NotNull<CachedSurface*> aSurface,
1427 NotNull<ImageSurfaceCache*> aCache,
1428 const StaticMutexAutoLock& aAutoLock) {
1429 if (aCache->IsLocked()) {
1430 LockSurface(aSurface, aAutoLock);
1431 return true;
1434 nsresult rv = mExpirationTracker.MarkUsedLocked(aSurface, aAutoLock);
1435 if (NS_WARN_IF(NS_FAILED(rv))) {
1436 // If mark used fails, it is because it failed to reinsert the surface
1437 // after removing it from the tracker. Thus we need to update our
1438 // own accounting but otherwise expect it to be untracked.
1439 StopTracking(aSurface, /* aIsTracked */ false, aAutoLock);
1440 return false;
1442 return true;
1445 void DoUnlockSurfaces(NotNull<ImageSurfaceCache*> aCache, bool aStaticOnly,
1446 const StaticMutexAutoLock& aAutoLock) {
1447 AutoTArray<NotNull<CachedSurface*>, 8> discard;
1449 // Unlock all the surfaces the per-image cache is holding.
1450 for (const auto& value : aCache->Values()) {
1451 NotNull<CachedSurface*> surface = WrapNotNull(value);
1452 if (surface->IsPlaceholder() || !surface->IsLocked()) {
1453 continue;
1455 if (aStaticOnly &&
1456 surface->GetSurfaceKey().Playback() != PlaybackType::eStatic) {
1457 continue;
1459 StopTracking(surface, /* aIsTracked */ true, aAutoLock);
1460 surface->SetLocked(false);
1461 if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
1462 discard.AppendElement(surface);
1466 // Discard any that we failed to track.
1467 for (auto iter = discard.begin(); iter != discard.end(); ++iter) {
1468 Remove(*iter, /* aStopTracking */ false, aAutoLock);
1472 void RemoveEntry(const ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
1473 const StaticMutexAutoLock& aAutoLock) {
1474 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1475 if (!cache) {
1476 return; // No cached surfaces for this image.
1479 RefPtr<CachedSurface> surface =
1480 cache->Lookup(aSurfaceKey, /* aForAccess = */ false);
1481 if (!surface) {
1482 return; // Lookup in the per-image cache missed.
1485 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
1488 class SurfaceTracker final
1489 : public ExpirationTrackerImpl<CachedSurface, 2, StaticMutex,
1490 StaticMutexAutoLock> {
1491 public:
1492 explicit SurfaceTracker(uint32_t aSurfaceCacheExpirationTimeMS)
1493 : ExpirationTrackerImpl<CachedSurface, 2, StaticMutex,
1494 StaticMutexAutoLock>(
1495 aSurfaceCacheExpirationTimeMS, "SurfaceTracker") {}
1497 protected:
1498 void NotifyExpiredLocked(CachedSurface* aSurface,
1499 const StaticMutexAutoLock& aAutoLock) override {
1500 sInstance->Remove(WrapNotNull(aSurface), /* aStopTracking */ true,
1501 aAutoLock);
1504 void NotifyHandlerEndLocked(const StaticMutexAutoLock& aAutoLock) override {
1505 sInstance->TakeDiscard(mDiscard, aAutoLock);
1508 void NotifyHandlerEnd() override {
1509 nsTArray<RefPtr<CachedSurface>> discard(std::move(mDiscard));
1512 StaticMutex& GetMutex() override { return sInstanceMutex; }
1514 nsTArray<RefPtr<CachedSurface>> mDiscard;
1517 class MemoryPressureObserver final : public nsIObserver {
1518 public:
1519 NS_DECL_ISUPPORTS
1521 NS_IMETHOD Observe(nsISupports*, const char* aTopic,
1522 const char16_t*) override {
1523 nsTArray<RefPtr<CachedSurface>> discard;
1525 StaticMutexAutoLock lock(sInstanceMutex);
1526 if (sInstance && strcmp(aTopic, "memory-pressure") == 0) {
1527 sInstance->DiscardForMemoryPressure(lock);
1528 sInstance->TakeDiscard(discard, lock);
1531 return NS_OK;
1534 private:
1535 virtual ~MemoryPressureObserver() {}
1538 nsTArray<CostEntry> mCosts;
1539 nsRefPtrHashtable<nsPtrHashKey<Image>, ImageSurfaceCache> mImageCaches;
1540 nsTArray<RefPtr<CachedSurface>> mCachedSurfacesDiscard;
1541 SurfaceTracker mExpirationTracker;
1542 RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
1543 nsTArray<RefPtr<image::Image>> mReleasingImagesOnMainThread;
1544 const uint32_t mDiscardFactor;
1545 const Cost mMaxCost;
1546 Cost mAvailableCost;
1547 Cost mLockedCost;
1548 size_t mOverflowCount;
1549 size_t mAlreadyPresentCount;
1550 size_t mTableFailureCount;
1551 size_t mTrackingFailureCount;
1554 NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter)
1555 NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver)
1557 ///////////////////////////////////////////////////////////////////////////////
1558 // Public API
1559 ///////////////////////////////////////////////////////////////////////////////
1561 /* static */
1562 void SurfaceCache::Initialize() {
1563 // Initialize preferences.
1564 MOZ_ASSERT(NS_IsMainThread());
1565 MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once");
1567 // See StaticPrefs for the default values of these preferences.
1569 // Length of time before an unused surface is removed from the cache, in
1570 // milliseconds.
1571 uint32_t surfaceCacheExpirationTimeMS =
1572 StaticPrefs::image_mem_surfacecache_min_expiration_ms_AtStartup();
1574 // What fraction of the memory used by the surface cache we should discard
1575 // when we get a memory pressure notification. This value is interpreted as
1576 // 1/N, so 1 means to discard everything, 2 means to discard about half of the
1577 // memory we're using, and so forth. We clamp it to avoid division by zero.
1578 uint32_t surfaceCacheDiscardFactor =
1579 max(StaticPrefs::image_mem_surfacecache_discard_factor_AtStartup(), 1u);
1581 // Maximum size of the surface cache, in kilobytes.
1582 uint64_t surfaceCacheMaxSizeKB =
1583 StaticPrefs::image_mem_surfacecache_max_size_kb_AtStartup();
1585 if (sizeof(uintptr_t) <= 4) {
1586 // Limit surface cache to 1 GB if our address space is 32 bit.
1587 surfaceCacheMaxSizeKB = 1024 * 1024;
1590 // A knob determining the actual size of the surface cache. Currently the
1591 // cache is (size of main memory) / (surface cache size factor) KB
1592 // or (surface cache max size) KB, whichever is smaller. The formula
1593 // may change in the future, though.
1594 // For example, a value of 4 would yield a 256MB cache on a 1GB machine.
1595 // The smallest machines we are likely to run this code on have 256MB
1596 // of memory, which would yield a 64MB cache on this setting.
1597 // We clamp this value to avoid division by zero.
1598 uint32_t surfaceCacheSizeFactor =
1599 max(StaticPrefs::image_mem_surfacecache_size_factor_AtStartup(), 1u);
1601 // Compute the size of the surface cache.
1602 uint64_t memorySize = PR_GetPhysicalMemorySize();
1603 if (memorySize == 0) {
1604 MOZ_ASSERT_UNREACHABLE("PR_GetPhysicalMemorySize not implemented here");
1605 memorySize = 256 * 1024 * 1024; // Fall back to 256MB.
1607 uint64_t proposedSize = memorySize / surfaceCacheSizeFactor;
1608 uint64_t surfaceCacheSizeBytes =
1609 min(proposedSize, surfaceCacheMaxSizeKB * 1024);
1610 uint32_t finalSurfaceCacheSizeBytes =
1611 min(surfaceCacheSizeBytes, uint64_t(UINT32_MAX));
1613 // Create the surface cache singleton with the requested settings. Note that
1614 // the size is a limit that the cache may not grow beyond, but we do not
1615 // actually allocate any storage for surfaces at this time.
1616 sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS,
1617 surfaceCacheDiscardFactor,
1618 finalSurfaceCacheSizeBytes);
1619 sInstance->InitMemoryReporter();
1622 /* static */
1623 void SurfaceCache::Shutdown() {
1624 RefPtr<SurfaceCacheImpl> cache;
1626 StaticMutexAutoLock lock(sInstanceMutex);
1627 MOZ_ASSERT(NS_IsMainThread());
1628 MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?");
1629 cache = sInstance.forget();
1633 /* static */
1634 LookupResult SurfaceCache::Lookup(const ImageKey aImageKey,
1635 const SurfaceKey& aSurfaceKey,
1636 bool aMarkUsed) {
1637 nsTArray<RefPtr<CachedSurface>> discard;
1638 LookupResult rv(MatchType::NOT_FOUND);
1641 StaticMutexAutoLock lock(sInstanceMutex);
1642 if (!sInstance) {
1643 return rv;
1646 rv = sInstance->Lookup(aImageKey, aSurfaceKey, lock, aMarkUsed);
1647 sInstance->TakeDiscard(discard, lock);
1650 return rv;
1653 /* static */
1654 LookupResult SurfaceCache::LookupBestMatch(const ImageKey aImageKey,
1655 const SurfaceKey& aSurfaceKey,
1656 bool aMarkUsed) {
1657 nsTArray<RefPtr<CachedSurface>> discard;
1658 LookupResult rv(MatchType::NOT_FOUND);
1661 StaticMutexAutoLock lock(sInstanceMutex);
1662 if (!sInstance) {
1663 return rv;
1666 rv = sInstance->LookupBestMatch(aImageKey, aSurfaceKey, lock, aMarkUsed);
1667 sInstance->TakeDiscard(discard, lock);
1670 return rv;
1673 /* static */
1674 InsertOutcome SurfaceCache::Insert(NotNull<ISurfaceProvider*> aProvider) {
1675 nsTArray<RefPtr<CachedSurface>> discard;
1676 InsertOutcome rv(InsertOutcome::FAILURE);
1679 StaticMutexAutoLock lock(sInstanceMutex);
1680 if (!sInstance) {
1681 return rv;
1684 rv = sInstance->Insert(aProvider, /* aSetAvailable = */ false, lock);
1685 sInstance->TakeDiscard(discard, lock);
1688 return rv;
1691 /* static */
1692 bool SurfaceCache::CanHold(const IntSize& aSize,
1693 uint32_t aBytesPerPixel /* = 4 */) {
1694 StaticMutexAutoLock lock(sInstanceMutex);
1695 if (!sInstance) {
1696 return false;
1699 Cost cost = ComputeCost(aSize, aBytesPerPixel);
1700 return sInstance->CanHold(cost);
1703 /* static */
1704 bool SurfaceCache::CanHold(size_t aSize) {
1705 StaticMutexAutoLock lock(sInstanceMutex);
1706 if (!sInstance) {
1707 return false;
1710 return sInstance->CanHold(aSize);
1713 /* static */
1714 void SurfaceCache::SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider) {
1715 StaticMutexAutoLock lock(sInstanceMutex);
1716 if (!sInstance) {
1717 return;
1720 sInstance->SurfaceAvailable(aProvider, lock);
1723 /* static */
1724 void SurfaceCache::LockImage(const ImageKey aImageKey) {
1725 StaticMutexAutoLock lock(sInstanceMutex);
1726 if (sInstance) {
1727 return sInstance->LockImage(aImageKey);
1731 /* static */
1732 void SurfaceCache::UnlockImage(const ImageKey aImageKey) {
1733 StaticMutexAutoLock lock(sInstanceMutex);
1734 if (sInstance) {
1735 return sInstance->UnlockImage(aImageKey, lock);
1739 /* static */
1740 void SurfaceCache::UnlockEntries(const ImageKey aImageKey) {
1741 StaticMutexAutoLock lock(sInstanceMutex);
1742 if (sInstance) {
1743 return sInstance->UnlockEntries(aImageKey, lock);
1747 /* static */
1748 void SurfaceCache::RemoveImage(const ImageKey aImageKey) {
1749 RefPtr<ImageSurfaceCache> discard;
1751 StaticMutexAutoLock lock(sInstanceMutex);
1752 if (sInstance) {
1753 discard = sInstance->RemoveImage(aImageKey, lock);
1758 /* static */
1759 void SurfaceCache::PruneImage(const ImageKey aImageKey) {
1760 nsTArray<RefPtr<CachedSurface>> discard;
1762 StaticMutexAutoLock lock(sInstanceMutex);
1763 if (sInstance) {
1764 sInstance->PruneImage(aImageKey, lock);
1765 sInstance->TakeDiscard(discard, lock);
1770 /* static */
1771 bool SurfaceCache::InvalidateImage(const ImageKey aImageKey) {
1772 nsTArray<RefPtr<CachedSurface>> discard;
1773 bool rv = false;
1775 StaticMutexAutoLock lock(sInstanceMutex);
1776 if (sInstance) {
1777 rv = sInstance->InvalidateImage(aImageKey, lock);
1778 sInstance->TakeDiscard(discard, lock);
1781 return rv;
1784 /* static */
1785 void SurfaceCache::DiscardAll() {
1786 nsTArray<RefPtr<CachedSurface>> discard;
1788 StaticMutexAutoLock lock(sInstanceMutex);
1789 if (sInstance) {
1790 sInstance->DiscardAll(lock);
1791 sInstance->TakeDiscard(discard, lock);
1796 /* static */
1797 void SurfaceCache::CollectSizeOfSurfaces(
1798 const ImageKey aImageKey, nsTArray<SurfaceMemoryCounter>& aCounters,
1799 MallocSizeOf aMallocSizeOf) {
1800 nsTArray<RefPtr<CachedSurface>> discard;
1802 StaticMutexAutoLock lock(sInstanceMutex);
1803 if (!sInstance) {
1804 return;
1807 sInstance->CollectSizeOfSurfaces(aImageKey, aCounters, aMallocSizeOf, lock);
1808 sInstance->TakeDiscard(discard, lock);
1812 /* static */
1813 size_t SurfaceCache::MaximumCapacity() {
1814 StaticMutexAutoLock lock(sInstanceMutex);
1815 if (!sInstance) {
1816 return 0;
1819 return sInstance->MaximumCapacity();
1822 /* static */
1823 bool SurfaceCache::IsLegalSize(const IntSize& aSize) {
1824 // reject over-wide or over-tall images
1825 const int32_t k64KLimit = 0x0000FFFF;
1826 if (MOZ_UNLIKELY(aSize.width > k64KLimit || aSize.height > k64KLimit)) {
1827 NS_WARNING("image too big");
1828 return false;
1831 // protect against invalid sizes
1832 if (MOZ_UNLIKELY(aSize.height <= 0 || aSize.width <= 0)) {
1833 return false;
1836 // check to make sure we don't overflow a 32-bit
1837 CheckedInt32 requiredBytes =
1838 CheckedInt32(aSize.width) * CheckedInt32(aSize.height) * 4;
1839 if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
1840 NS_WARNING("width or height too large");
1841 return false;
1843 return true;
1846 IntSize SurfaceCache::ClampVectorSize(const IntSize& aSize) {
1847 // If we exceed the maximum, we need to scale the size downwards to fit.
1848 // It shouldn't get here if it is significantly larger because
1849 // VectorImage::UseSurfaceCacheForSize should prevent us from requesting
1850 // a rasterized version of a surface greater than 4x the maximum.
1851 int32_t maxSizeKB =
1852 StaticPrefs::image_cache_max_rasterized_svg_threshold_kb();
1853 if (maxSizeKB <= 0) {
1854 return aSize;
1857 int64_t proposedKB = int64_t(aSize.width) * aSize.height / 256;
1858 if (maxSizeKB >= proposedKB) {
1859 return aSize;
1862 double scale = sqrt(double(maxSizeKB) / proposedKB);
1863 return IntSize(int32_t(scale * aSize.width), int32_t(scale * aSize.height));
1866 IntSize SurfaceCache::ClampSize(ImageKey aImageKey, const IntSize& aSize) {
1867 if (aImageKey->GetType() != imgIContainer::TYPE_VECTOR) {
1868 return aSize;
1871 return ClampVectorSize(aSize);
1874 /* static */
1875 void SurfaceCache::ReleaseImageOnMainThread(
1876 already_AddRefed<image::Image> aImage, bool aAlwaysProxy) {
1877 if (NS_IsMainThread() && !aAlwaysProxy) {
1878 RefPtr<image::Image> image = std::move(aImage);
1879 return;
1882 // Don't try to dispatch the release after shutdown, we'll just leak the
1883 // runnable.
1884 if (gXPCOMThreadsShutDown) {
1885 return;
1888 StaticMutexAutoLock lock(sInstanceMutex);
1889 if (sInstance) {
1890 sInstance->ReleaseImageOnMainThread(std::move(aImage), lock);
1891 } else {
1892 NS_ReleaseOnMainThread("SurfaceCache::ReleaseImageOnMainThread",
1893 std::move(aImage), /* aAlwaysProxy */ true);
1897 /* static */
1898 void SurfaceCache::ClearReleasingImages() {
1899 MOZ_ASSERT(NS_IsMainThread());
1901 nsTArray<RefPtr<image::Image>> images;
1903 StaticMutexAutoLock lock(sInstanceMutex);
1904 if (sInstance) {
1905 sInstance->TakeReleasingImages(images, lock);
1910 } // namespace image
1911 } // namespace mozilla