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/. */
7 * SurfaceCache is a service for caching temporary surfaces in imagelib.
10 #include "SurfaceCache.h"
15 #include "ISurfaceProvider.h"
17 #include "LookupResult.h"
18 #include "ShutdownTracker.h"
19 #include "gfx2DGlue.h"
20 #include "gfxPlatform.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/Pair.h"
28 #include "mozilla/RefPtr.h"
29 #include "mozilla/StaticMutex.h"
30 #include "mozilla/StaticPrefs_image.h"
31 #include "mozilla/StaticPtr.h"
32 #include "mozilla/Tuple.h"
33 #include "nsExpirationTracker.h"
34 #include "nsHashKeys.h"
35 #include "nsIMemoryReporter.h"
36 #include "nsRefPtrHashtable.h"
50 MOZ_DEFINE_MALLOC_SIZE_OF(SurfaceCacheMallocSizeOf
)
53 class SurfaceCacheImpl
;
55 ///////////////////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////////
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
77 static Cost
ComputeCost(const IntSize
& aSize
, uint32_t aBytesPerPixel
) {
78 MOZ_ASSERT(aBytesPerPixel
== 1 || aBytesPerPixel
== 4);
79 return aSize
.width
* aSize
.height
* aBytesPerPixel
;
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
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
&&
108 recordreplay::RecordReplayValue(mSurface
< aOther
.mSurface
));
112 NotNull
<CachedSurface
*> mSurface
;
117 * A CachedSurface associates a surface with a key that uniquely identifies that
120 class CachedSurface
{
124 MOZ_DECLARE_REFCOUNTED_TYPENAME(CachedSurface
)
125 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CachedSurface
)
127 explicit CachedSurface(NotNull
<ISurfaceProvider
*> aProvider
)
128 : mProvider(aProvider
), mIsLocked(false) {}
130 DrawableSurface
GetDrawableSurface() const {
131 if (MOZ_UNLIKELY(IsPlaceholder())) {
132 MOZ_ASSERT_UNREACHABLE("Called GetDrawableSurface() on a placeholder");
133 return DrawableSurface();
136 return mProvider
->Surface();
139 void SetLocked(bool aLocked
) {
140 if (IsPlaceholder()) {
141 return; // Can't lock a placeholder.
144 // Update both our state and our provider's state. Some surface providers
145 // are permanently locked; maintaining our own locking state enables us to
146 // respect SetLocked() even when it's meaningless from the provider's
149 mProvider
->SetLocked(aLocked
);
152 bool IsLocked() const {
153 return !IsPlaceholder() && mIsLocked
&& mProvider
->IsLocked();
156 void SetCannotSubstitute() {
157 mProvider
->Availability().SetCannotSubstitute();
159 bool CannotSubstitute() const {
160 return mProvider
->Availability().CannotSubstitute();
163 bool IsPlaceholder() const {
164 return mProvider
->Availability().IsPlaceholder();
166 bool IsDecoded() const { return !IsPlaceholder() && mProvider
->IsFinished(); }
168 ImageKey
GetImageKey() const { return mProvider
->GetImageKey(); }
169 const SurfaceKey
& GetSurfaceKey() const { return mProvider
->GetSurfaceKey(); }
170 nsExpirationState
* GetExpirationState() { return &mExpirationState
; }
172 CostEntry
GetCostEntry() {
173 return image::CostEntry(WrapNotNull(this), mProvider
->LogicalSizeInBytes());
176 size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
177 return aMallocSizeOf(this) + aMallocSizeOf(mProvider
.get());
180 // A helper type used by SurfaceCacheImpl::CollectSizeOfSurfaces.
181 struct MOZ_STACK_CLASS SurfaceMemoryReport
{
182 SurfaceMemoryReport(nsTArray
<SurfaceMemoryCounter
>& aCounters
,
183 MallocSizeOf aMallocSizeOf
)
184 : mCounters(aCounters
), mMallocSizeOf(aMallocSizeOf
) {}
186 void Add(NotNull
<CachedSurface
*> aCachedSurface
, bool aIsFactor2
) {
187 if (aCachedSurface
->IsPlaceholder()) {
191 // Record the memory used by the ISurfaceProvider. This may not have a
192 // straightforward relationship to the size of the surface that
193 // DrawableRef() returns if the surface is generated dynamically. (i.e.,
194 // for surfaces with PlaybackType::eAnimated.)
195 aCachedSurface
->mProvider
->AddSizeOfExcludingThis(
196 mMallocSizeOf
, [&](ISurfaceProvider::AddSizeOfCbData
& aMetadata
) {
197 SurfaceMemoryCounter
counter(aCachedSurface
->GetSurfaceKey(),
198 aCachedSurface
->IsLocked(),
199 aCachedSurface
->CannotSubstitute(),
200 aIsFactor2
, aMetadata
.finished
);
202 counter
.Values().SetDecodedHeap(aMetadata
.heap
);
203 counter
.Values().SetDecodedNonHeap(aMetadata
.nonHeap
);
204 counter
.Values().SetExternalHandles(aMetadata
.handles
);
205 counter
.Values().SetFrameIndex(aMetadata
.index
);
206 counter
.Values().SetExternalId(aMetadata
.externalId
);
208 mCounters
.AppendElement(counter
);
213 nsTArray
<SurfaceMemoryCounter
>& mCounters
;
214 MallocSizeOf mMallocSizeOf
;
218 nsExpirationState mExpirationState
;
219 NotNull
<RefPtr
<ISurfaceProvider
>> mProvider
;
223 static int64_t AreaOfIntSize(const IntSize
& aSize
) {
224 return static_cast<int64_t>(aSize
.width
) * static_cast<int64_t>(aSize
.height
);
228 * An ImageSurfaceCache is a per-image surface cache. For correctness we must be
229 * able to remove all surfaces associated with an image when the image is
230 * destroyed or invalidated. Since this will happen frequently, it makes sense
231 * to make it cheap by storing the surfaces for each image separately.
233 * ImageSurfaceCache also keeps track of whether its associated image is locked
236 * The cache may also enter "factor of 2" mode which occurs when the number of
237 * surfaces in the cache exceeds the "image.cache.factor2.threshold-surfaces"
238 * pref plus the number of native sizes of the image. When in "factor of 2"
239 * mode, the cache will strongly favour sizes which are a factor of 2 of the
240 * largest native size. It accomplishes this by suggesting a factor of 2 size
241 * when lookups fail and substituting the nearest factor of 2 surface to the
242 * ideal size as the "best" available (as opposed to substitution but not
243 * found). This allows us to minimize memory consumption and CPU time spent
244 * decoding when a website requires many variants of the same surface.
246 class ImageSurfaceCache
{
247 ~ImageSurfaceCache() {}
250 explicit ImageSurfaceCache(const ImageKey aImageKey
)
253 mFactor2Pruned(false),
254 mIsVectorImage(aImageKey
->GetType() == imgIContainer::TYPE_VECTOR
) {}
256 MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageSurfaceCache
)
257 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageSurfaceCache
)
259 typedef nsRefPtrHashtable
<nsGenericHashKey
<SurfaceKey
>, CachedSurface
>
262 bool IsEmpty() const { return mSurfaces
.Count() == 0; }
264 size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
265 size_t bytes
= aMallocSizeOf(this) +
266 mSurfaces
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
267 for (auto iter
= ConstIter(); !iter
.Done(); iter
.Next()) {
268 bytes
+= iter
.UserData()->ShallowSizeOfIncludingThis(aMallocSizeOf
);
273 MOZ_MUST_USE
bool Insert(NotNull
<CachedSurface
*> aSurface
) {
274 MOZ_ASSERT(!mLocked
|| aSurface
->IsPlaceholder() || aSurface
->IsLocked(),
275 "Inserting an unlocked surface for a locked image");
276 return mSurfaces
.Put(aSurface
->GetSurfaceKey(), aSurface
, fallible
);
279 already_AddRefed
<CachedSurface
> Remove(NotNull
<CachedSurface
*> aSurface
) {
280 MOZ_ASSERT(mSurfaces
.GetWeak(aSurface
->GetSurfaceKey()),
281 "Should not be removing a surface we don't have");
283 RefPtr
<CachedSurface
> surface
;
284 mSurfaces
.Remove(aSurface
->GetSurfaceKey(), getter_AddRefs(surface
));
286 return surface
.forget();
289 already_AddRefed
<CachedSurface
> Lookup(const SurfaceKey
& aSurfaceKey
,
291 RefPtr
<CachedSurface
> surface
;
292 mSurfaces
.Get(aSurfaceKey
, getter_AddRefs(surface
));
296 // We don't want to allow factor of 2 mode pruning to release surfaces
297 // for which the callers will accept no substitute.
298 surface
->SetCannotSubstitute();
299 } else if (!mFactor2Mode
) {
300 // If no exact match is found, and this is for use rather than internal
301 // accounting (i.e. insert and removal), we know this will trigger a
302 // decode. Make sure we switch now to factor of 2 mode if necessary.
303 MaybeSetFactor2Mode();
307 return surface
.forget();
311 * @returns A tuple containing the best matching CachedSurface if available,
312 * a MatchType describing how the CachedSurface was selected, and
313 * an IntSize which is the size the caller should choose to decode
314 * at should it attempt to do so.
316 Tuple
<already_AddRefed
<CachedSurface
>, MatchType
, IntSize
> LookupBestMatch(
317 const SurfaceKey
& aIdealKey
) {
318 // Try for an exact match first.
319 RefPtr
<CachedSurface
> exactMatch
;
320 mSurfaces
.Get(aIdealKey
, getter_AddRefs(exactMatch
));
322 if (exactMatch
->IsDecoded()) {
323 return MakeTuple(exactMatch
.forget(), MatchType::EXACT
, IntSize());
325 } else if (!mFactor2Mode
) {
326 // If no exact match is found, and we are not in factor of 2 mode, then
327 // we know that we will trigger a decode because at best we will provide
328 // a substitute. Make sure we switch now to factor of 2 mode if necessary.
329 MaybeSetFactor2Mode();
332 // Try for a best match second, if using compact.
333 IntSize suggestedSize
= SuggestedSize(aIdealKey
.Size());
334 if (suggestedSize
!= aIdealKey
.Size()) {
336 SurfaceKey compactKey
= aIdealKey
.CloneWithSize(suggestedSize
);
337 mSurfaces
.Get(compactKey
, getter_AddRefs(exactMatch
));
338 if (exactMatch
&& exactMatch
->IsDecoded()) {
339 MOZ_ASSERT(suggestedSize
!= aIdealKey
.Size());
340 return MakeTuple(exactMatch
.forget(),
341 MatchType::SUBSTITUTE_BECAUSE_BEST
, suggestedSize
);
346 // There's no perfect match, so find the best match we can.
347 RefPtr
<CachedSurface
> bestMatch
;
348 for (auto iter
= ConstIter(); !iter
.Done(); iter
.Next()) {
349 NotNull
<CachedSurface
*> current
= WrapNotNull(iter
.UserData());
350 const SurfaceKey
& currentKey
= current
->GetSurfaceKey();
352 // We never match a placeholder.
353 if (current
->IsPlaceholder()) {
356 // Matching the playback type and SVG context is required.
357 if (currentKey
.Playback() != aIdealKey
.Playback() ||
358 currentKey
.SVGContext() != aIdealKey
.SVGContext()) {
361 // Matching the flags is required.
362 if (currentKey
.Flags() != aIdealKey
.Flags()) {
365 // Anything is better than nothing! (Within the constraints we just
366 // checked, of course.)
372 MOZ_ASSERT(bestMatch
, "Should have a current best match");
374 // Always prefer completely decoded surfaces.
375 bool bestMatchIsDecoded
= bestMatch
->IsDecoded();
376 if (bestMatchIsDecoded
&& !current
->IsDecoded()) {
379 if (!bestMatchIsDecoded
&& current
->IsDecoded()) {
384 SurfaceKey bestMatchKey
= bestMatch
->GetSurfaceKey();
385 if (CompareArea(aIdealKey
.Size(), bestMatchKey
.Size(),
386 currentKey
.Size())) {
394 // No exact match, neither ideal nor factor of 2.
395 MOZ_ASSERT(suggestedSize
!= bestMatch
->GetSurfaceKey().Size(),
396 "No exact match despite the fact the sizes match!");
397 matchType
= MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
;
398 } else if (exactMatch
!= bestMatch
) {
399 // The exact match is still decoding, but we found a substitute.
400 matchType
= MatchType::SUBSTITUTE_BECAUSE_PENDING
;
401 } else if (aIdealKey
.Size() != bestMatch
->GetSurfaceKey().Size()) {
402 // The best factor of 2 match is still decoding, but the best we've got.
403 MOZ_ASSERT(suggestedSize
!= aIdealKey
.Size());
404 MOZ_ASSERT(mFactor2Mode
|| mIsVectorImage
);
405 matchType
= MatchType::SUBSTITUTE_BECAUSE_BEST
;
407 // The exact match is still decoding, but it's the best we've got.
408 matchType
= MatchType::EXACT
;
412 // We found an "exact match"; it must have been a placeholder.
413 MOZ_ASSERT(exactMatch
->IsPlaceholder());
414 matchType
= MatchType::PENDING
;
416 // We couldn't find an exact match *or* a substitute.
417 matchType
= MatchType::NOT_FOUND
;
421 return MakeTuple(bestMatch
.forget(), matchType
, suggestedSize
);
424 void MaybeSetFactor2Mode() {
425 MOZ_ASSERT(!mFactor2Mode
);
427 // Typically an image cache will not have too many size-varying surfaces, so
428 // if we exceed the given threshold, we should consider using a subset.
429 int32_t thresholdSurfaces
=
430 StaticPrefs::image_cache_factor2_threshold_surfaces();
431 if (thresholdSurfaces
< 0 ||
432 mSurfaces
.Count() <= static_cast<uint32_t>(thresholdSurfaces
)) {
436 // Determine how many native surfaces this image has. If it is zero, and it
437 // is a vector image, then we should impute a single native size. Otherwise,
438 // it may be zero because we don't know yet, or the image has an error, or
439 // it isn't supported.
440 auto first
= ConstIter();
441 NotNull
<CachedSurface
*> current
= WrapNotNull(first
.UserData());
442 Image
* image
= static_cast<Image
*>(current
->GetImageKey());
443 size_t nativeSizes
= image
->GetNativeSizesLength();
444 if (mIsVectorImage
) {
445 MOZ_ASSERT(nativeSizes
== 0);
447 } else if (nativeSizes
== 0) {
451 // Increase the threshold by the number of native sizes. This ensures that
452 // we do not prevent decoding of the image at all its native sizes. It does
453 // not guarantee we will provide a surface at that size however (i.e. many
454 // other sized surfaces are requested, in addition to the native sizes).
455 thresholdSurfaces
+= nativeSizes
;
456 if (mSurfaces
.Count() <= static_cast<uint32_t>(thresholdSurfaces
)) {
460 // Get our native size. While we know the image should be fully decoded,
461 // if it is an SVG, it is valid to have a zero size. We can't do compacting
462 // in that case because we need to know the width/height ratio to define a
465 if (NS_FAILED(image
->GetWidth(&nativeSize
.width
)) ||
466 NS_FAILED(image
->GetHeight(&nativeSize
.height
)) ||
467 nativeSize
.IsEmpty()) {
471 // We have a valid size, we can change modes.
475 template <typename Function
>
476 void Prune(Function
&& aRemoveCallback
) {
477 if (!mFactor2Mode
|| mFactor2Pruned
) {
481 // Attempt to discard any surfaces which are not factor of 2 and the best
482 // factor of 2 match exists.
483 bool hasNotFactorSize
= false;
484 for (auto iter
= mSurfaces
.Iter(); !iter
.Done(); iter
.Next()) {
485 NotNull
<CachedSurface
*> current
= WrapNotNull(iter
.UserData());
486 const SurfaceKey
& currentKey
= current
->GetSurfaceKey();
487 const IntSize
& currentSize
= currentKey
.Size();
489 // First we check if someone requested this size and would not accept
490 // an alternatively sized surface.
491 if (current
->CannotSubstitute()) {
495 // Next we find the best factor of 2 size for this surface. If this
496 // surface is a factor of 2 size, then we want to keep it.
497 IntSize bestSize
= SuggestedSize(currentSize
);
498 if (bestSize
== currentSize
) {
502 // Check the cache for a surface with the same parameters except for the
503 // size which uses the closest factor of 2 size.
504 SurfaceKey compactKey
= currentKey
.CloneWithSize(bestSize
);
505 RefPtr
<CachedSurface
> compactMatch
;
506 mSurfaces
.Get(compactKey
, getter_AddRefs(compactMatch
));
507 if (compactMatch
&& compactMatch
->IsDecoded()) {
508 aRemoveCallback(current
);
511 hasNotFactorSize
= true;
515 // We have no surfaces that are not factor of 2 sized, so we can stop
516 // pruning henceforth, because we avoid the insertion of new surfaces that
517 // don't match our sizing set (unless the caller won't accept a
519 if (!hasNotFactorSize
) {
520 mFactor2Pruned
= true;
523 // We should never leave factor of 2 mode due to pruning in of itself, but
524 // if we discarded surfaces due to the volatile buffers getting released,
529 IntSize
SuggestedSize(const IntSize
& aSize
) const {
530 IntSize suggestedSize
= SuggestedSizeInternal(aSize
);
531 if (mIsVectorImage
) {
532 suggestedSize
= SurfaceCache::ClampVectorSize(suggestedSize
);
534 return suggestedSize
;
537 IntSize
SuggestedSizeInternal(const IntSize
& aSize
) const {
538 // When not in factor of 2 mode, we can always decode at the given size.
543 // We cannot enter factor of 2 mode unless we have a minimum number of
544 // surfaces, and we should have left it if the cache was emptied.
545 if (MOZ_UNLIKELY(IsEmpty())) {
546 MOZ_ASSERT_UNREACHABLE("Should not be empty and in factor of 2 mode!");
550 // This bit of awkwardness gets the largest native size of the image.
551 auto iter
= ConstIter();
552 NotNull
<CachedSurface
*> firstSurface
= WrapNotNull(iter
.UserData());
553 Image
* image
= static_cast<Image
*>(firstSurface
->GetImageKey());
555 if (NS_FAILED(image
->GetWidth(&factorSize
.width
)) ||
556 NS_FAILED(image
->GetHeight(&factorSize
.height
)) ||
557 factorSize
.IsEmpty()) {
558 // We should not have entered factor of 2 mode without a valid size, and
559 // several successfully decoded surfaces. Note that valid vector images
560 // may have a default size of 0x0, and those are not yet supported.
561 MOZ_ASSERT_UNREACHABLE("Expected valid native size!");
565 if (mIsVectorImage
) {
566 // Ensure the aspect ratio matches the native size before forcing the
567 // caller to accept a factor of 2 size. The difference between the aspect
570 // delta = nativeWidth/nativeHeight - desiredWidth/desiredHeight
572 // delta*nativeHeight*desiredHeight = nativeWidth*desiredHeight
573 // - desiredWidth*nativeHeight
575 // Using the maximum accepted delta as a constant, we can avoid the
576 // floating point division and just compare after some integer ops.
578 factorSize
.width
* aSize
.height
- aSize
.width
* factorSize
.height
;
579 int32_t maxDelta
= (factorSize
.height
* aSize
.height
) >> 4;
580 if (delta
> maxDelta
|| delta
< -maxDelta
) {
584 // If the requested size is bigger than the native size, we actually need
585 // to grow the native size instead of shrinking it.
586 if (factorSize
.width
< aSize
.width
) {
588 IntSize
candidate(factorSize
.width
* 2, factorSize
.height
* 2);
589 if (!SurfaceCache::IsLegalSize(candidate
)) {
593 factorSize
= candidate
;
594 } while (factorSize
.width
< aSize
.width
);
599 // Otherwise we can find the best fit as normal.
602 // Start with the native size as the best first guess.
603 IntSize bestSize
= factorSize
;
604 factorSize
.width
/= 2;
605 factorSize
.height
/= 2;
607 while (!factorSize
.IsEmpty()) {
608 if (!CompareArea(aSize
, bestSize
, factorSize
)) {
609 // This size is not better than the last. Since we proceed from largest
610 // to smallest, we know that the next size will not be better if the
611 // previous size was rejected. Break early.
615 // The current factor of 2 size is better than the last selected size.
616 bestSize
= factorSize
;
617 factorSize
.width
/= 2;
618 factorSize
.height
/= 2;
624 bool CompareArea(const IntSize
& aIdealSize
, const IntSize
& aBestSize
,
625 const IntSize
& aSize
) const {
626 // Compare sizes. We use an area-based heuristic here instead of computing a
627 // truly optimal answer, since it seems very unlikely to make a difference
628 // for realistic sizes.
629 int64_t idealArea
= AreaOfIntSize(aIdealSize
);
630 int64_t currentArea
= AreaOfIntSize(aSize
);
631 int64_t bestMatchArea
= AreaOfIntSize(aBestSize
);
633 // If the best match is smaller than the ideal size, prefer bigger sizes.
634 if (bestMatchArea
< idealArea
) {
635 if (currentArea
> bestMatchArea
) {
641 // Other, prefer sizes closer to the ideal size, but still not smaller.
642 if (idealArea
<= currentArea
&& currentArea
< bestMatchArea
) {
646 // This surface isn't an improvement over the current best match.
650 template <typename Function
>
651 void CollectSizeOfSurfaces(nsTArray
<SurfaceMemoryCounter
>& aCounters
,
652 MallocSizeOf aMallocSizeOf
,
653 Function
&& aRemoveCallback
) {
654 CachedSurface::SurfaceMemoryReport
report(aCounters
, aMallocSizeOf
);
655 for (auto iter
= mSurfaces
.Iter(); !iter
.Done(); iter
.Next()) {
656 NotNull
<CachedSurface
*> surface
= WrapNotNull(iter
.UserData());
658 // We don't need the drawable surface for ourselves, but adding a surface
659 // to the report will trigger this indirectly. If the surface was
660 // discarded by the OS because it was in volatile memory, we should remove
661 // it from the cache immediately rather than include it in the report.
662 DrawableSurface drawableSurface
;
663 if (!surface
->IsPlaceholder()) {
664 drawableSurface
= surface
->GetDrawableSurface();
665 if (!drawableSurface
) {
666 aRemoveCallback(surface
);
672 const IntSize
& size
= surface
->GetSurfaceKey().Size();
673 bool factor2Size
= false;
675 factor2Size
= (size
== SuggestedSize(size
));
677 report
.Add(surface
, factor2Size
);
683 SurfaceTable::Iterator
ConstIter() const { return mSurfaces
.ConstIter(); }
684 uint32_t Count() const { return mSurfaces
.Count(); }
686 void SetLocked(bool aLocked
) { mLocked
= aLocked
; }
687 bool IsLocked() const { return mLocked
; }
690 void AfterMaybeRemove() {
691 if (IsEmpty() && mFactor2Mode
) {
692 // The last surface for this cache was removed. This can happen if the
693 // surface was stored in a volatile buffer and got purged, or the surface
694 // expired from the cache. If the cache itself lingers for some reason
695 // (e.g. in the process of performing a lookup, the cache itself is
696 // locked), then we need to reset the factor of 2 state because it
697 // requires at least one surface present to get the native size
698 // information from the image.
699 mFactor2Mode
= mFactor2Pruned
= false;
703 SurfaceTable mSurfaces
;
707 // True in "factor of 2" mode.
710 // True if all non-factor of 2 surfaces have been removed from the cache. Note
711 // that this excludes unsubstitutable sizes.
714 // True if the surfaces are produced from a vector image. If so, it must match
715 // the aspect ratio when using factor of 2 mode.
720 * SurfaceCacheImpl is responsible for determining which surfaces will be cached
721 * and managing the surface cache data structures. Rather than interact with
722 * SurfaceCacheImpl directly, client code interacts with SurfaceCache, which
723 * maintains high-level invariants and encapsulates the details of the surface
724 * cache's implementation.
726 class SurfaceCacheImpl final
: public nsIMemoryReporter
{
730 SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS
,
731 uint32_t aSurfaceCacheDiscardFactor
,
732 uint32_t aSurfaceCacheSize
)
733 : mExpirationTracker(aSurfaceCacheExpirationTimeMS
),
734 mMemoryPressureObserver(new MemoryPressureObserver
),
735 mDiscardFactor(aSurfaceCacheDiscardFactor
),
736 mMaxCost(aSurfaceCacheSize
),
737 mAvailableCost(aSurfaceCacheSize
),
740 mAlreadyPresentCount(0),
741 mTableFailureCount(0),
742 mTrackingFailureCount(0) {
743 nsCOMPtr
<nsIObserverService
> os
= services::GetObserverService();
745 os
->AddObserver(mMemoryPressureObserver
, "memory-pressure", false);
750 virtual ~SurfaceCacheImpl() {
751 nsCOMPtr
<nsIObserverService
> os
= services::GetObserverService();
753 os
->RemoveObserver(mMemoryPressureObserver
, "memory-pressure");
756 UnregisterWeakMemoryReporter(this);
760 void InitMemoryReporter() { RegisterWeakMemoryReporter(this); }
762 InsertOutcome
Insert(NotNull
<ISurfaceProvider
*> aProvider
, bool aSetAvailable
,
763 const StaticMutexAutoLock
& aAutoLock
) {
764 // If this is a duplicate surface, refuse to replace the original.
765 // XXX(seth): Calling Lookup() and then RemoveEntry() does the lookup
766 // twice. We'll make this more efficient in bug 1185137.
767 LookupResult result
=
768 Lookup(aProvider
->GetImageKey(), aProvider
->GetSurfaceKey(), aAutoLock
,
769 /* aMarkUsed = */ false);
770 if (MOZ_UNLIKELY(result
)) {
771 mAlreadyPresentCount
++;
772 return InsertOutcome::FAILURE_ALREADY_PRESENT
;
775 if (result
.Type() == MatchType::PENDING
) {
776 RemoveEntry(aProvider
->GetImageKey(), aProvider
->GetSurfaceKey(),
780 MOZ_ASSERT(result
.Type() == MatchType::NOT_FOUND
||
781 result
.Type() == MatchType::PENDING
,
782 "A LookupResult with no surface should be NOT_FOUND or PENDING");
784 // If this is bigger than we can hold after discarding everything we can,
785 // refuse to cache it.
786 Cost cost
= aProvider
->LogicalSizeInBytes();
787 if (MOZ_UNLIKELY(!CanHoldAfterDiscarding(cost
))) {
789 return InsertOutcome::FAILURE
;
792 // Remove elements in order of cost until we can fit this in the cache. Note
793 // that locked surfaces aren't in mCosts, so we never remove them here.
794 while (cost
> mAvailableCost
) {
795 MOZ_ASSERT(!mCosts
.IsEmpty(),
796 "Removed everything and it still won't fit");
797 Remove(mCosts
.LastElement().Surface(), /* aStopTracking */ true,
801 // Locate the appropriate per-image cache. If there's not an existing cache
802 // for this image, create it.
803 const ImageKey imageKey
= aProvider
->GetImageKey();
804 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(imageKey
);
806 cache
= new ImageSurfaceCache(imageKey
);
807 if (!mImageCaches
.Put(aProvider
->GetImageKey(), cache
, fallible
)) {
808 mTableFailureCount
++;
809 return InsertOutcome::FAILURE
;
813 // If we were asked to mark the cache entry available, do so.
815 aProvider
->Availability().SetAvailable();
818 auto surface
= MakeNotNull
<RefPtr
<CachedSurface
>>(aProvider
);
820 // We require that locking succeed if the image is locked and we're not
821 // inserting a placeholder; the caller may need to know this to handle
823 bool mustLock
= cache
->IsLocked() && !surface
->IsPlaceholder();
825 surface
->SetLocked(true);
826 if (!surface
->IsLocked()) {
827 return InsertOutcome::FAILURE
;
832 MOZ_ASSERT(cost
<= mAvailableCost
, "Inserting despite too large a cost");
833 if (!cache
->Insert(surface
)) {
834 mTableFailureCount
++;
836 surface
->SetLocked(false);
838 return InsertOutcome::FAILURE
;
841 if (MOZ_UNLIKELY(!StartTracking(surface
, aAutoLock
))) {
842 MOZ_ASSERT(!mustLock
);
843 Remove(surface
, /* aStopTracking */ false, aAutoLock
);
844 return InsertOutcome::FAILURE
;
847 return InsertOutcome::SUCCESS
;
850 void Remove(NotNull
<CachedSurface
*> aSurface
, bool aStopTracking
,
851 const StaticMutexAutoLock
& aAutoLock
) {
852 ImageKey imageKey
= aSurface
->GetImageKey();
854 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(imageKey
);
855 MOZ_ASSERT(cache
, "Shouldn't try to remove a surface with no image cache");
857 // If the surface was not a placeholder, tell its image that we discarded
859 if (!aSurface
->IsPlaceholder()) {
860 static_cast<Image
*>(imageKey
)->OnSurfaceDiscarded(
861 aSurface
->GetSurfaceKey());
864 // If we failed during StartTracking, we can skip this step.
866 StopTracking(aSurface
, /* aIsTracked */ true, aAutoLock
);
869 // Individual surfaces must be freed outside the lock.
870 mCachedSurfacesDiscard
.AppendElement(cache
->Remove(aSurface
));
872 MaybeRemoveEmptyCache(imageKey
, cache
);
875 bool StartTracking(NotNull
<CachedSurface
*> aSurface
,
876 const StaticMutexAutoLock
& aAutoLock
) {
877 CostEntry costEntry
= aSurface
->GetCostEntry();
878 MOZ_ASSERT(costEntry
.GetCost() <= mAvailableCost
,
879 "Cost too large and the caller didn't catch it");
881 if (aSurface
->IsLocked()) {
882 mLockedCost
+= costEntry
.GetCost();
883 MOZ_ASSERT(mLockedCost
<= mMaxCost
, "Locked more than we can hold?");
885 if (NS_WARN_IF(!mCosts
.InsertElementSorted(costEntry
, fallible
))) {
886 mTrackingFailureCount
++;
890 // This may fail during XPCOM shutdown, so we need to ensure the object is
891 // tracked before calling RemoveObject in StopTracking.
892 nsresult rv
= mExpirationTracker
.AddObjectLocked(aSurface
, aAutoLock
);
893 if (NS_WARN_IF(NS_FAILED(rv
))) {
894 DebugOnly
<bool> foundInCosts
= mCosts
.RemoveElementSorted(costEntry
);
895 MOZ_ASSERT(foundInCosts
, "Lost track of costs for this surface");
896 mTrackingFailureCount
++;
901 mAvailableCost
-= costEntry
.GetCost();
905 void StopTracking(NotNull
<CachedSurface
*> aSurface
, bool aIsTracked
,
906 const StaticMutexAutoLock
& aAutoLock
) {
907 CostEntry costEntry
= aSurface
->GetCostEntry();
909 if (aSurface
->IsLocked()) {
910 MOZ_ASSERT(mLockedCost
>= costEntry
.GetCost(), "Costs don't balance");
911 mLockedCost
-= costEntry
.GetCost();
912 // XXX(seth): It'd be nice to use an O(log n) lookup here. This is O(n).
913 MOZ_ASSERT(!mCosts
.Contains(costEntry
),
914 "Shouldn't have a cost entry for a locked surface");
916 if (MOZ_LIKELY(aSurface
->GetExpirationState()->IsTracked())) {
917 MOZ_ASSERT(aIsTracked
, "Expiration-tracking a surface unexpectedly!");
918 mExpirationTracker
.RemoveObjectLocked(aSurface
, aAutoLock
);
920 // Our call to AddObject must have failed in StartTracking; most likely
921 // we're in XPCOM shutdown right now.
922 MOZ_ASSERT(!aIsTracked
, "Not expiration-tracking an unlocked surface!");
925 DebugOnly
<bool> foundInCosts
= mCosts
.RemoveElementSorted(costEntry
);
926 MOZ_ASSERT(foundInCosts
, "Lost track of costs for this surface");
929 mAvailableCost
+= costEntry
.GetCost();
930 MOZ_ASSERT(mAvailableCost
<= mMaxCost
,
931 "More available cost than we started with");
934 LookupResult
Lookup(const ImageKey aImageKey
, const SurfaceKey
& aSurfaceKey
,
935 const StaticMutexAutoLock
& aAutoLock
, bool aMarkUsed
) {
936 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
938 // No cached surfaces for this image.
939 return LookupResult(MatchType::NOT_FOUND
);
942 RefPtr
<CachedSurface
> surface
= cache
->Lookup(aSurfaceKey
, aMarkUsed
);
944 // Lookup in the per-image cache missed.
945 return LookupResult(MatchType::NOT_FOUND
);
948 if (surface
->IsPlaceholder()) {
949 return LookupResult(MatchType::PENDING
);
952 DrawableSurface drawableSurface
= surface
->GetDrawableSurface();
953 if (!drawableSurface
) {
954 // The surface was released by the operating system. Remove the cache
956 Remove(WrapNotNull(surface
), /* aStopTracking */ true, aAutoLock
);
957 return LookupResult(MatchType::NOT_FOUND
);
961 !MarkUsed(WrapNotNull(surface
), WrapNotNull(cache
), aAutoLock
)) {
962 Remove(WrapNotNull(surface
), /* aStopTracking */ false, aAutoLock
);
963 return LookupResult(MatchType::NOT_FOUND
);
966 MOZ_ASSERT(surface
->GetSurfaceKey() == aSurfaceKey
,
967 "Lookup() not returning an exact match?");
968 return LookupResult(std::move(drawableSurface
), MatchType::EXACT
);
971 LookupResult
LookupBestMatch(const ImageKey aImageKey
,
972 const SurfaceKey
& aSurfaceKey
,
973 const StaticMutexAutoLock
& aAutoLock
,
975 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
977 // No cached surfaces for this image.
979 MatchType::NOT_FOUND
,
980 SurfaceCache::ClampSize(aImageKey
, aSurfaceKey
.Size()));
983 // Repeatedly look up the best match, trying again if the resulting surface
984 // has been freed by the operating system, until we can either lock a
985 // surface for drawing or there are no matching surfaces left.
986 // XXX(seth): This is O(N^2), but N is expected to be very small. If we
987 // encounter a performance problem here we can revisit this.
989 RefPtr
<CachedSurface
> surface
;
990 DrawableSurface drawableSurface
;
991 MatchType matchType
= MatchType::NOT_FOUND
;
992 IntSize suggestedSize
;
994 Tie(surface
, matchType
, suggestedSize
) =
995 cache
->LookupBestMatch(aSurfaceKey
);
999 matchType
, suggestedSize
); // Lookup in the per-image cache missed.
1002 drawableSurface
= surface
->GetDrawableSurface();
1003 if (drawableSurface
) {
1007 // The surface was released by the operating system. Remove the cache
1009 Remove(WrapNotNull(surface
), /* aStopTracking */ true, aAutoLock
);
1012 MOZ_ASSERT_IF(matchType
== MatchType::EXACT
,
1013 surface
->GetSurfaceKey() == aSurfaceKey
);
1015 matchType
== MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
||
1016 matchType
== MatchType::SUBSTITUTE_BECAUSE_PENDING
,
1017 surface
->GetSurfaceKey().SVGContext() == aSurfaceKey
.SVGContext() &&
1018 surface
->GetSurfaceKey().Playback() == aSurfaceKey
.Playback() &&
1019 surface
->GetSurfaceKey().Flags() == aSurfaceKey
.Flags());
1021 if (matchType
== MatchType::EXACT
||
1022 matchType
== MatchType::SUBSTITUTE_BECAUSE_BEST
) {
1024 !MarkUsed(WrapNotNull(surface
), WrapNotNull(cache
), aAutoLock
)) {
1025 Remove(WrapNotNull(surface
), /* aStopTracking */ false, aAutoLock
);
1029 return LookupResult(std::move(drawableSurface
), matchType
, suggestedSize
);
1032 bool CanHold(const Cost aCost
) const { return aCost
<= mMaxCost
; }
1034 size_t MaximumCapacity() const { return size_t(mMaxCost
); }
1036 void SurfaceAvailable(NotNull
<ISurfaceProvider
*> aProvider
,
1037 const StaticMutexAutoLock
& aAutoLock
) {
1038 if (!aProvider
->Availability().IsPlaceholder()) {
1039 MOZ_ASSERT_UNREACHABLE("Calling SurfaceAvailable on non-placeholder");
1043 // Reinsert the provider, requesting that Insert() mark it available. This
1044 // may or may not succeed, depending on whether some other decoder has
1045 // beaten us to the punch and inserted a non-placeholder version of this
1046 // surface first, but it's fine either way.
1047 // XXX(seth): This could be implemented more efficiently; we should be able
1048 // to just update our data structures without reinserting.
1049 Insert(aProvider
, /* aSetAvailable = */ true, aAutoLock
);
1052 void LockImage(const ImageKey aImageKey
) {
1053 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1055 cache
= new ImageSurfaceCache(aImageKey
);
1056 mImageCaches
.Put(aImageKey
, cache
);
1059 cache
->SetLocked(true);
1061 // We don't relock this image's existing surfaces right away; instead, the
1062 // image should arrange for Lookup() to touch them if they are still useful.
1065 void UnlockImage(const ImageKey aImageKey
,
1066 const StaticMutexAutoLock
& aAutoLock
) {
1067 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1068 if (!cache
|| !cache
->IsLocked()) {
1069 return; // Already unlocked.
1072 cache
->SetLocked(false);
1073 DoUnlockSurfaces(WrapNotNull(cache
), /* aStaticOnly = */ false, aAutoLock
);
1076 void UnlockEntries(const ImageKey aImageKey
,
1077 const StaticMutexAutoLock
& aAutoLock
) {
1078 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1079 if (!cache
|| !cache
->IsLocked()) {
1080 return; // Already unlocked.
1083 // (Note that we *don't* unlock the per-image cache here; that's the
1084 // difference between this and UnlockImage.)
1085 DoUnlockSurfaces(WrapNotNull(cache
),
1087 !StaticPrefs::image_mem_animated_discardable_AtStartup(),
1091 already_AddRefed
<ImageSurfaceCache
> RemoveImage(
1092 const ImageKey aImageKey
, const StaticMutexAutoLock
& aAutoLock
) {
1093 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1095 return nullptr; // No cached surfaces for this image, so nothing to do.
1098 // Discard all of the cached surfaces for this image.
1099 // XXX(seth): This is O(n^2) since for each item in the cache we are
1100 // removing an element from the costs array. Since n is expected to be
1101 // small, performance should be good, but if usage patterns change we should
1102 // change the data structure used for mCosts.
1103 for (auto iter
= cache
->ConstIter(); !iter
.Done(); iter
.Next()) {
1104 StopTracking(WrapNotNull(iter
.UserData()),
1105 /* aIsTracked */ true, aAutoLock
);
1108 // The per-image cache isn't needed anymore, so remove it as well.
1109 // This implicitly unlocks the image if it was locked.
1110 mImageCaches
.Remove(aImageKey
);
1112 // Since we did not actually remove any of the surfaces from the cache
1113 // itself, only stopped tracking them, we should free it outside the lock.
1114 return cache
.forget();
1117 void PruneImage(const ImageKey aImageKey
,
1118 const StaticMutexAutoLock
& aAutoLock
) {
1119 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1121 return; // No cached surfaces for this image, so nothing to do.
1124 cache
->Prune([this, &aAutoLock
](NotNull
<CachedSurface
*> aSurface
) -> void {
1125 StopTracking(aSurface
, /* aIsTracked */ true, aAutoLock
);
1126 // Individual surfaces must be freed outside the lock.
1127 mCachedSurfacesDiscard
.AppendElement(aSurface
);
1130 MaybeRemoveEmptyCache(aImageKey
, cache
);
1133 void DiscardAll(const StaticMutexAutoLock
& aAutoLock
) {
1134 // Remove in order of cost because mCosts is an array and the other data
1135 // structures are all hash tables. Note that locked surfaces are not
1136 // removed, since they aren't present in mCosts.
1137 while (!mCosts
.IsEmpty()) {
1138 Remove(mCosts
.LastElement().Surface(), /* aStopTracking */ true,
1143 void DiscardForMemoryPressure(const StaticMutexAutoLock
& aAutoLock
) {
1144 // Compute our discardable cost. Since locked surfaces aren't discardable,
1146 const Cost discardableCost
= (mMaxCost
- mAvailableCost
) - mLockedCost
;
1147 MOZ_ASSERT(discardableCost
<= mMaxCost
, "Discardable cost doesn't add up");
1149 // Our target is to raise our available cost by (1 / mDiscardFactor) of our
1150 // discardable cost - in other words, we want to end up with about
1151 // (discardableCost / mDiscardFactor) fewer bytes stored in the surface
1152 // cache after we're done.
1153 const Cost targetCost
= mAvailableCost
+ (discardableCost
/ mDiscardFactor
);
1155 if (targetCost
> mMaxCost
- mLockedCost
) {
1156 MOZ_ASSERT_UNREACHABLE("Target cost is more than we can discard");
1157 DiscardAll(aAutoLock
);
1161 // Discard surfaces until we've reduced our cost to our target cost.
1162 while (mAvailableCost
< targetCost
) {
1163 MOZ_ASSERT(!mCosts
.IsEmpty(), "Removed everything and still not done");
1164 Remove(mCosts
.LastElement().Surface(), /* aStopTracking */ true,
1169 void TakeDiscard(nsTArray
<RefPtr
<CachedSurface
>>& aDiscard
,
1170 const StaticMutexAutoLock
& aAutoLock
) {
1171 MOZ_ASSERT(aDiscard
.IsEmpty());
1172 aDiscard
= std::move(mCachedSurfacesDiscard
);
1175 void LockSurface(NotNull
<CachedSurface
*> aSurface
,
1176 const StaticMutexAutoLock
& aAutoLock
) {
1177 if (aSurface
->IsPlaceholder() || aSurface
->IsLocked()) {
1181 StopTracking(aSurface
, /* aIsTracked */ true, aAutoLock
);
1183 // Lock the surface. This can fail.
1184 aSurface
->SetLocked(true);
1185 DebugOnly
<bool> tracked
= StartTracking(aSurface
, aAutoLock
);
1186 MOZ_ASSERT(tracked
);
1189 size_t ShallowSizeOfIncludingThis(
1190 MallocSizeOf aMallocSizeOf
, const StaticMutexAutoLock
& aAutoLock
) const {
1192 aMallocSizeOf(this) + mCosts
.ShallowSizeOfExcludingThis(aMallocSizeOf
) +
1193 mImageCaches
.ShallowSizeOfExcludingThis(aMallocSizeOf
) +
1194 mCachedSurfacesDiscard
.ShallowSizeOfExcludingThis(aMallocSizeOf
) +
1195 mExpirationTracker
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
1196 for (auto iter
= mImageCaches
.ConstIter(); !iter
.Done(); iter
.Next()) {
1197 bytes
+= iter
.UserData()->ShallowSizeOfIncludingThis(aMallocSizeOf
);
1203 CollectReports(nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
1204 bool aAnonymize
) override
{
1205 StaticMutexAutoLock
lock(sInstanceMutex
);
1207 uint32_t lockedImageCount
= 0;
1208 uint32_t totalSurfaceCount
= 0;
1209 uint32_t lockedSurfaceCount
= 0;
1210 for (auto iter
= mImageCaches
.ConstIter(); !iter
.Done(); iter
.Next()) {
1211 totalSurfaceCount
+= iter
.UserData()->Count();
1212 if (iter
.UserData()->IsLocked()) {
1215 for (auto surfIter
= iter
.UserData()->ConstIter(); !surfIter
.Done();
1217 if (surfIter
.UserData()->IsLocked()) {
1218 ++lockedSurfaceCount
;
1224 // We have explicit memory reporting for the surface cache which is more
1225 // accurate than the cost metrics we report here, but these metrics are
1226 // still useful to report, since they control the cache's behavior.
1228 "explicit/images/cache/overhead", KIND_HEAP
, UNITS_BYTES
,
1229 ShallowSizeOfIncludingThis(SurfaceCacheMallocSizeOf
, lock
),
1230 "Memory used by the surface cache data structures, excluding surface data.");
1233 "imagelib-surface-cache-estimated-total",
1234 KIND_OTHER
, UNITS_BYTES
, (mMaxCost
- mAvailableCost
),
1235 "Estimated total memory used by the imagelib surface cache.");
1238 "imagelib-surface-cache-estimated-locked",
1239 KIND_OTHER
, UNITS_BYTES
, mLockedCost
,
1240 "Estimated memory used by locked surfaces in the imagelib surface cache.");
1243 "imagelib-surface-cache-tracked-cost-count",
1244 KIND_OTHER
, UNITS_COUNT
, mCosts
.Length(),
1245 "Total number of surfaces tracked for cost (and expiry) in the imagelib surface cache.");
1248 "imagelib-surface-cache-tracked-expiry-count",
1249 KIND_OTHER
, UNITS_COUNT
, mExpirationTracker
.Length(lock
),
1250 "Total number of surfaces tracked for expiry (and cost) in the imagelib surface cache.");
1253 "imagelib-surface-cache-image-count",
1254 KIND_OTHER
, UNITS_COUNT
, mImageCaches
.Count(),
1255 "Total number of images in the imagelib surface cache.");
1258 "imagelib-surface-cache-locked-image-count",
1259 KIND_OTHER
, UNITS_COUNT
, lockedImageCount
,
1260 "Total number of locked images in the imagelib surface cache.");
1263 "imagelib-surface-cache-image-surface-count",
1264 KIND_OTHER
, UNITS_COUNT
, totalSurfaceCount
,
1265 "Total number of surfaces in the imagelib surface cache.");
1268 "imagelib-surface-cache-locked-surfaces-count",
1269 KIND_OTHER
, UNITS_COUNT
, lockedSurfaceCount
,
1270 "Total number of locked surfaces in the imagelib surface cache.");
1273 "imagelib-surface-cache-overflow-count",
1274 KIND_OTHER
, UNITS_COUNT
, mOverflowCount
,
1275 "Count of how many times the surface cache has hit its capacity and been "
1276 "unable to insert a new surface.");
1279 "imagelib-surface-cache-tracking-failure-count",
1280 KIND_OTHER
, UNITS_COUNT
, mTrackingFailureCount
,
1281 "Count of how many times the surface cache has failed to begin tracking a "
1285 "imagelib-surface-cache-already-present-count",
1286 KIND_OTHER
, UNITS_COUNT
, mAlreadyPresentCount
,
1287 "Count of how many times the surface cache has failed to insert a surface "
1288 "because it is already present.");
1291 "imagelib-surface-cache-table-failure-count",
1292 KIND_OTHER
, UNITS_COUNT
, mTableFailureCount
,
1293 "Count of how many times the surface cache has failed to insert a surface "
1294 "because a hash table could not accept an entry.");
1300 void CollectSizeOfSurfaces(const ImageKey aImageKey
,
1301 nsTArray
<SurfaceMemoryCounter
>& aCounters
,
1302 MallocSizeOf aMallocSizeOf
,
1303 const StaticMutexAutoLock
& aAutoLock
) {
1304 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1306 return; // No surfaces for this image.
1309 // Report all surfaces in the per-image cache.
1310 cache
->CollectSizeOfSurfaces(
1311 aCounters
, aMallocSizeOf
,
1312 [this, &aAutoLock
](NotNull
<CachedSurface
*> aSurface
) -> void {
1313 StopTracking(aSurface
, /* aIsTracked */ true, aAutoLock
);
1314 // Individual surfaces must be freed outside the lock.
1315 mCachedSurfacesDiscard
.AppendElement(aSurface
);
1318 MaybeRemoveEmptyCache(aImageKey
, cache
);
1322 already_AddRefed
<ImageSurfaceCache
> GetImageCache(const ImageKey aImageKey
) {
1323 RefPtr
<ImageSurfaceCache
> imageCache
;
1324 mImageCaches
.Get(aImageKey
, getter_AddRefs(imageCache
));
1325 return imageCache
.forget();
1328 void MaybeRemoveEmptyCache(const ImageKey aImageKey
,
1329 ImageSurfaceCache
* aCache
) {
1330 // Remove the per-image cache if it's unneeded now. Keep it if the image is
1331 // locked, since the per-image cache is where we store that state. Note that
1332 // we don't push it into mImageCachesDiscard because all of its surfaces
1333 // have been removed, so it is safe to free while holding the lock.
1334 if (aCache
->IsEmpty() && !aCache
->IsLocked()) {
1335 mImageCaches
.Remove(aImageKey
);
1339 // This is similar to CanHold() except that it takes into account the costs of
1340 // locked surfaces. It's used internally in Insert(), but it's not exposed
1341 // publicly because we permit multithreaded access to the surface cache, which
1342 // means that the result would be meaningless: another thread could insert a
1343 // surface or lock an image at any time.
1344 bool CanHoldAfterDiscarding(const Cost aCost
) const {
1345 return aCost
<= mMaxCost
- mLockedCost
;
1348 bool MarkUsed(NotNull
<CachedSurface
*> aSurface
,
1349 NotNull
<ImageSurfaceCache
*> aCache
,
1350 const StaticMutexAutoLock
& aAutoLock
) {
1351 if (aCache
->IsLocked()) {
1352 LockSurface(aSurface
, aAutoLock
);
1356 nsresult rv
= mExpirationTracker
.MarkUsedLocked(aSurface
, aAutoLock
);
1357 if (NS_WARN_IF(NS_FAILED(rv
))) {
1358 // If mark used fails, it is because it failed to reinsert the surface
1359 // after removing it from the tracker. Thus we need to update our
1360 // own accounting but otherwise expect it to be untracked.
1361 StopTracking(aSurface
, /* aIsTracked */ false, aAutoLock
);
1367 void DoUnlockSurfaces(NotNull
<ImageSurfaceCache
*> aCache
, bool aStaticOnly
,
1368 const StaticMutexAutoLock
& aAutoLock
) {
1369 AutoTArray
<NotNull
<CachedSurface
*>, 8> discard
;
1371 // Unlock all the surfaces the per-image cache is holding.
1372 for (auto iter
= aCache
->ConstIter(); !iter
.Done(); iter
.Next()) {
1373 NotNull
<CachedSurface
*> surface
= WrapNotNull(iter
.UserData());
1374 if (surface
->IsPlaceholder() || !surface
->IsLocked()) {
1378 surface
->GetSurfaceKey().Playback() != PlaybackType::eStatic
) {
1381 StopTracking(surface
, /* aIsTracked */ true, aAutoLock
);
1382 surface
->SetLocked(false);
1383 if (MOZ_UNLIKELY(!StartTracking(surface
, aAutoLock
))) {
1384 discard
.AppendElement(surface
);
1388 // Discard any that we failed to track.
1389 for (auto iter
= discard
.begin(); iter
!= discard
.end(); ++iter
) {
1390 Remove(*iter
, /* aStopTracking */ false, aAutoLock
);
1394 void RemoveEntry(const ImageKey aImageKey
, const SurfaceKey
& aSurfaceKey
,
1395 const StaticMutexAutoLock
& aAutoLock
) {
1396 RefPtr
<ImageSurfaceCache
> cache
= GetImageCache(aImageKey
);
1398 return; // No cached surfaces for this image.
1401 RefPtr
<CachedSurface
> surface
=
1402 cache
->Lookup(aSurfaceKey
, /* aForAccess = */ false);
1404 return; // Lookup in the per-image cache missed.
1407 Remove(WrapNotNull(surface
), /* aStopTracking */ true, aAutoLock
);
1410 class SurfaceTracker final
1411 : public ExpirationTrackerImpl
<CachedSurface
, 2, StaticMutex
,
1412 StaticMutexAutoLock
> {
1414 explicit SurfaceTracker(uint32_t aSurfaceCacheExpirationTimeMS
)
1415 : ExpirationTrackerImpl
<CachedSurface
, 2, StaticMutex
,
1416 StaticMutexAutoLock
>(
1417 aSurfaceCacheExpirationTimeMS
, "SurfaceTracker",
1418 SystemGroup::EventTargetFor(TaskCategory::Other
)) {}
1421 void NotifyExpiredLocked(CachedSurface
* aSurface
,
1422 const StaticMutexAutoLock
& aAutoLock
) override
{
1423 sInstance
->Remove(WrapNotNull(aSurface
), /* aStopTracking */ true,
1427 void NotifyHandlerEndLocked(const StaticMutexAutoLock
& aAutoLock
) override
{
1428 sInstance
->TakeDiscard(mDiscard
, aAutoLock
);
1431 void NotifyHandlerEnd() override
{
1432 nsTArray
<RefPtr
<CachedSurface
>> discard(std::move(mDiscard
));
1435 StaticMutex
& GetMutex() override
{ return sInstanceMutex
; }
1437 nsTArray
<RefPtr
<CachedSurface
>> mDiscard
;
1440 class MemoryPressureObserver final
: public nsIObserver
{
1444 NS_IMETHOD
Observe(nsISupports
*, const char* aTopic
,
1445 const char16_t
*) override
{
1446 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1448 StaticMutexAutoLock
lock(sInstanceMutex
);
1449 if (sInstance
&& strcmp(aTopic
, "memory-pressure") == 0) {
1450 sInstance
->DiscardForMemoryPressure(lock
);
1451 sInstance
->TakeDiscard(discard
, lock
);
1458 virtual ~MemoryPressureObserver() {}
1461 nsTArray
<CostEntry
> mCosts
;
1462 nsRefPtrHashtable
<nsPtrHashKey
<Image
>, ImageSurfaceCache
> mImageCaches
;
1463 nsTArray
<RefPtr
<CachedSurface
>> mCachedSurfacesDiscard
;
1464 SurfaceTracker mExpirationTracker
;
1465 RefPtr
<MemoryPressureObserver
> mMemoryPressureObserver
;
1466 const uint32_t mDiscardFactor
;
1467 const Cost mMaxCost
;
1468 Cost mAvailableCost
;
1470 size_t mOverflowCount
;
1471 size_t mAlreadyPresentCount
;
1472 size_t mTableFailureCount
;
1473 size_t mTrackingFailureCount
;
1476 NS_IMPL_ISUPPORTS(SurfaceCacheImpl
, nsIMemoryReporter
)
1477 NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver
, nsIObserver
)
1479 ///////////////////////////////////////////////////////////////////////////////
1481 ///////////////////////////////////////////////////////////////////////////////
1484 void SurfaceCache::Initialize() {
1485 // Initialize preferences.
1486 MOZ_ASSERT(NS_IsMainThread());
1487 MOZ_ASSERT(!sInstance
, "Shouldn't initialize more than once");
1489 // See StaticPrefs for the default values of these preferences.
1491 // Length of time before an unused surface is removed from the cache, in
1493 uint32_t surfaceCacheExpirationTimeMS
=
1494 StaticPrefs::image_mem_surfacecache_min_expiration_ms_AtStartup();
1496 // What fraction of the memory used by the surface cache we should discard
1497 // when we get a memory pressure notification. This value is interpreted as
1498 // 1/N, so 1 means to discard everything, 2 means to discard about half of the
1499 // memory we're using, and so forth. We clamp it to avoid division by zero.
1500 uint32_t surfaceCacheDiscardFactor
=
1501 max(StaticPrefs::image_mem_surfacecache_discard_factor_AtStartup(), 1u);
1503 // Maximum size of the surface cache, in kilobytes.
1504 uint64_t surfaceCacheMaxSizeKB
=
1505 StaticPrefs::image_mem_surfacecache_max_size_kb_AtStartup();
1507 if (sizeof(uintptr_t) <= 4) {
1508 // Limit surface cache to 1 GB if our address space is 32 bit.
1509 surfaceCacheMaxSizeKB
= 1024 * 1024;
1512 // A knob determining the actual size of the surface cache. Currently the
1513 // cache is (size of main memory) / (surface cache size factor) KB
1514 // or (surface cache max size) KB, whichever is smaller. The formula
1515 // may change in the future, though.
1516 // For example, a value of 4 would yield a 256MB cache on a 1GB machine.
1517 // The smallest machines we are likely to run this code on have 256MB
1518 // of memory, which would yield a 64MB cache on this setting.
1519 // We clamp this value to avoid division by zero.
1520 uint32_t surfaceCacheSizeFactor
=
1521 max(StaticPrefs::image_mem_surfacecache_size_factor_AtStartup(), 1u);
1523 // Compute the size of the surface cache.
1524 uint64_t memorySize
= PR_GetPhysicalMemorySize();
1525 if (memorySize
== 0) {
1526 MOZ_ASSERT_UNREACHABLE("PR_GetPhysicalMemorySize not implemented here");
1527 memorySize
= 256 * 1024 * 1024; // Fall back to 256MB.
1529 uint64_t proposedSize
= memorySize
/ surfaceCacheSizeFactor
;
1530 uint64_t surfaceCacheSizeBytes
=
1531 min(proposedSize
, surfaceCacheMaxSizeKB
* 1024);
1532 uint32_t finalSurfaceCacheSizeBytes
=
1533 min(surfaceCacheSizeBytes
, uint64_t(UINT32_MAX
));
1535 // Create the surface cache singleton with the requested settings. Note that
1536 // the size is a limit that the cache may not grow beyond, but we do not
1537 // actually allocate any storage for surfaces at this time.
1538 sInstance
= new SurfaceCacheImpl(surfaceCacheExpirationTimeMS
,
1539 surfaceCacheDiscardFactor
,
1540 finalSurfaceCacheSizeBytes
);
1541 sInstance
->InitMemoryReporter();
1545 void SurfaceCache::Shutdown() {
1546 RefPtr
<SurfaceCacheImpl
> cache
;
1548 StaticMutexAutoLock
lock(sInstanceMutex
);
1549 MOZ_ASSERT(NS_IsMainThread());
1550 MOZ_ASSERT(sInstance
, "No singleton - was Shutdown() called twice?");
1551 cache
= sInstance
.forget();
1556 LookupResult
SurfaceCache::Lookup(const ImageKey aImageKey
,
1557 const SurfaceKey
& aSurfaceKey
,
1559 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1560 LookupResult
rv(MatchType::NOT_FOUND
);
1563 StaticMutexAutoLock
lock(sInstanceMutex
);
1568 rv
= sInstance
->Lookup(aImageKey
, aSurfaceKey
, lock
, aMarkUsed
);
1569 sInstance
->TakeDiscard(discard
, lock
);
1576 LookupResult
SurfaceCache::LookupBestMatch(const ImageKey aImageKey
,
1577 const SurfaceKey
& aSurfaceKey
,
1579 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1580 LookupResult
rv(MatchType::NOT_FOUND
);
1583 StaticMutexAutoLock
lock(sInstanceMutex
);
1588 rv
= sInstance
->LookupBestMatch(aImageKey
, aSurfaceKey
, lock
, aMarkUsed
);
1589 sInstance
->TakeDiscard(discard
, lock
);
1596 InsertOutcome
SurfaceCache::Insert(NotNull
<ISurfaceProvider
*> aProvider
) {
1597 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1598 InsertOutcome
rv(InsertOutcome::FAILURE
);
1601 StaticMutexAutoLock
lock(sInstanceMutex
);
1606 rv
= sInstance
->Insert(aProvider
, /* aSetAvailable = */ false, lock
);
1607 sInstance
->TakeDiscard(discard
, lock
);
1614 bool SurfaceCache::CanHold(const IntSize
& aSize
,
1615 uint32_t aBytesPerPixel
/* = 4 */) {
1616 StaticMutexAutoLock
lock(sInstanceMutex
);
1621 Cost cost
= ComputeCost(aSize
, aBytesPerPixel
);
1622 return sInstance
->CanHold(cost
);
1626 bool SurfaceCache::CanHold(size_t aSize
) {
1627 StaticMutexAutoLock
lock(sInstanceMutex
);
1632 return sInstance
->CanHold(aSize
);
1636 void SurfaceCache::SurfaceAvailable(NotNull
<ISurfaceProvider
*> aProvider
) {
1637 StaticMutexAutoLock
lock(sInstanceMutex
);
1642 sInstance
->SurfaceAvailable(aProvider
, lock
);
1646 void SurfaceCache::LockImage(const ImageKey aImageKey
) {
1647 StaticMutexAutoLock
lock(sInstanceMutex
);
1649 return sInstance
->LockImage(aImageKey
);
1654 void SurfaceCache::UnlockImage(const ImageKey aImageKey
) {
1655 StaticMutexAutoLock
lock(sInstanceMutex
);
1657 return sInstance
->UnlockImage(aImageKey
, lock
);
1662 void SurfaceCache::UnlockEntries(const ImageKey aImageKey
) {
1663 StaticMutexAutoLock
lock(sInstanceMutex
);
1665 return sInstance
->UnlockEntries(aImageKey
, lock
);
1670 void SurfaceCache::RemoveImage(const ImageKey aImageKey
) {
1671 RefPtr
<ImageSurfaceCache
> discard
;
1673 StaticMutexAutoLock
lock(sInstanceMutex
);
1675 discard
= sInstance
->RemoveImage(aImageKey
, lock
);
1681 void SurfaceCache::PruneImage(const ImageKey aImageKey
) {
1682 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1684 StaticMutexAutoLock
lock(sInstanceMutex
);
1686 sInstance
->PruneImage(aImageKey
, lock
);
1687 sInstance
->TakeDiscard(discard
, lock
);
1693 void SurfaceCache::DiscardAll() {
1694 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1696 StaticMutexAutoLock
lock(sInstanceMutex
);
1698 sInstance
->DiscardAll(lock
);
1699 sInstance
->TakeDiscard(discard
, lock
);
1705 void SurfaceCache::CollectSizeOfSurfaces(
1706 const ImageKey aImageKey
, nsTArray
<SurfaceMemoryCounter
>& aCounters
,
1707 MallocSizeOf aMallocSizeOf
) {
1708 nsTArray
<RefPtr
<CachedSurface
>> discard
;
1710 StaticMutexAutoLock
lock(sInstanceMutex
);
1715 sInstance
->CollectSizeOfSurfaces(aImageKey
, aCounters
, aMallocSizeOf
, lock
);
1716 sInstance
->TakeDiscard(discard
, lock
);
1721 size_t SurfaceCache::MaximumCapacity() {
1722 StaticMutexAutoLock
lock(sInstanceMutex
);
1727 return sInstance
->MaximumCapacity();
1731 bool SurfaceCache::IsLegalSize(const IntSize
& aSize
) {
1732 // reject over-wide or over-tall images
1733 const int32_t k64KLimit
= 0x0000FFFF;
1734 if (MOZ_UNLIKELY(aSize
.width
> k64KLimit
|| aSize
.height
> k64KLimit
)) {
1735 NS_WARNING("image too big");
1739 // protect against invalid sizes
1740 if (MOZ_UNLIKELY(aSize
.height
<= 0 || aSize
.width
<= 0)) {
1744 // check to make sure we don't overflow a 32-bit
1745 CheckedInt32 requiredBytes
=
1746 CheckedInt32(aSize
.width
) * CheckedInt32(aSize
.height
) * 4;
1747 if (MOZ_UNLIKELY(!requiredBytes
.isValid())) {
1748 NS_WARNING("width or height too large");
1754 IntSize
SurfaceCache::ClampVectorSize(const IntSize
& aSize
) {
1755 // If we exceed the maximum, we need to scale the size downwards to fit.
1756 // It shouldn't get here if it is significantly larger because
1757 // VectorImage::UseSurfaceCacheForSize should prevent us from requesting
1758 // a rasterized version of a surface greater than 4x the maximum.
1760 StaticPrefs::image_cache_max_rasterized_svg_threshold_kb();
1761 if (maxSizeKB
<= 0) {
1765 int64_t proposedKB
= int64_t(aSize
.width
) * aSize
.height
/ 256;
1766 if (maxSizeKB
>= proposedKB
) {
1770 double scale
= sqrt(double(maxSizeKB
) / proposedKB
);
1771 return IntSize(int32_t(scale
* aSize
.width
), int32_t(scale
* aSize
.height
));
1774 IntSize
SurfaceCache::ClampSize(ImageKey aImageKey
, const IntSize
& aSize
) {
1775 if (aImageKey
->GetType() != imgIContainer::TYPE_VECTOR
) {
1779 return ClampVectorSize(aSize
);
1782 } // namespace image
1783 } // namespace mozilla