Bug 1444460 [wpt PR 9948] - gyroscope: Rename LocalCoordinateSystem to GyroscopeLocal...
[gecko.git] / image / SurfaceCache.cpp
blobe5567fcd184dbacb3cd3709774eabf69711c5130
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 "mozilla/Assertions.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/DebugOnly.h"
16 #include "mozilla/Likely.h"
17 #include "mozilla/Move.h"
18 #include "mozilla/Pair.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/StaticMutex.h"
21 #include "mozilla/StaticPtr.h"
22 #include "mozilla/Tuple.h"
23 #include "nsIMemoryReporter.h"
24 #include "gfx2DGlue.h"
25 #include "gfxPlatform.h"
26 #include "gfxPrefs.h"
27 #include "imgFrame.h"
28 #include "Image.h"
29 #include "ISurfaceProvider.h"
30 #include "LookupResult.h"
31 #include "nsExpirationTracker.h"
32 #include "nsHashKeys.h"
33 #include "nsRefPtrHashtable.h"
34 #include "nsSize.h"
35 #include "nsTArray.h"
36 #include "prsystem.h"
37 #include "ShutdownTracker.h"
39 using std::max;
40 using std::min;
42 namespace mozilla {
44 using namespace gfx;
46 namespace image {
48 class CachedSurface;
49 class SurfaceCacheImpl;
51 ///////////////////////////////////////////////////////////////////////////////
52 // Static Data
53 ///////////////////////////////////////////////////////////////////////////////
55 // The single surface cache instance.
56 static StaticRefPtr<SurfaceCacheImpl> sInstance;
58 // The mutex protecting the surface cache.
59 static StaticMutex sInstanceMutex;
61 ///////////////////////////////////////////////////////////////////////////////
62 // SurfaceCache Implementation
63 ///////////////////////////////////////////////////////////////////////////////
65 /**
66 * Cost models the cost of storing a surface in the cache. Right now, this is
67 * simply an estimate of the size of the surface in bytes, but in the future it
68 * may be worth taking into account the cost of rematerializing the surface as
69 * well.
71 typedef size_t Cost;
73 static Cost
74 ComputeCost(const IntSize& aSize, uint32_t aBytesPerPixel)
76 MOZ_ASSERT(aBytesPerPixel == 1 || aBytesPerPixel == 4);
77 return aSize.width * aSize.height * aBytesPerPixel;
80 /**
81 * Since we want to be able to make eviction decisions based on cost, we need to
82 * be able to look up the CachedSurface which has a certain cost as well as the
83 * cost associated with a certain CachedSurface. To make this possible, in data
84 * structures we actually store a CostEntry, which contains a weak pointer to
85 * its associated surface.
87 * To make usage of the weak pointer safe, SurfaceCacheImpl always calls
88 * StartTracking after a surface is stored in the cache and StopTracking before
89 * it is removed.
91 class CostEntry
93 public:
94 CostEntry(NotNull<CachedSurface*> aSurface, Cost aCost)
95 : mSurface(aSurface)
96 , mCost(aCost)
97 { }
99 NotNull<CachedSurface*> Surface() const { return mSurface; }
100 Cost GetCost() const { return mCost; }
102 bool operator==(const CostEntry& aOther) const
104 return mSurface == aOther.mSurface &&
105 mCost == aOther.mCost;
108 bool operator<(const CostEntry& aOther) const
110 return mCost < aOther.mCost ||
111 (mCost == aOther.mCost && mSurface < aOther.mSurface);
114 private:
115 NotNull<CachedSurface*> mSurface;
116 Cost mCost;
120 * A CachedSurface associates a surface with a key that uniquely identifies that
121 * surface.
123 class CachedSurface
125 ~CachedSurface() { }
126 public:
127 MOZ_DECLARE_REFCOUNTED_TYPENAME(CachedSurface)
128 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CachedSurface)
130 explicit CachedSurface(NotNull<ISurfaceProvider*> aProvider)
131 : mProvider(aProvider)
132 , mIsLocked(false)
135 DrawableSurface GetDrawableSurface() const
137 if (MOZ_UNLIKELY(IsPlaceholder())) {
138 MOZ_ASSERT_UNREACHABLE("Called GetDrawableSurface() on a placeholder");
139 return DrawableSurface();
142 return mProvider->Surface();
145 void SetLocked(bool aLocked)
147 if (IsPlaceholder()) {
148 return; // Can't lock a placeholder.
151 // Update both our state and our provider's state. Some surface providers
152 // are permanently locked; maintaining our own locking state enables us to
153 // respect SetLocked() even when it's meaningless from the provider's
154 // perspective.
155 mIsLocked = aLocked;
156 mProvider->SetLocked(aLocked);
159 bool IsLocked() const
161 return !IsPlaceholder() && mIsLocked && mProvider->IsLocked();
164 void SetCannotSubstitute() { mProvider->Availability().SetCannotSubstitute(); }
165 bool CannotSubstitute() const { return mProvider->Availability().CannotSubstitute(); }
167 bool IsPlaceholder() const { return mProvider->Availability().IsPlaceholder(); }
168 bool IsDecoded() const { return !IsPlaceholder() && mProvider->IsFinished(); }
170 ImageKey GetImageKey() const { return mProvider->GetImageKey(); }
171 const SurfaceKey& GetSurfaceKey() const { return mProvider->GetSurfaceKey(); }
172 nsExpirationState* GetExpirationState() { return &mExpirationState; }
174 CostEntry GetCostEntry()
176 return image::CostEntry(WrapNotNull(this), mProvider->LogicalSizeInBytes());
179 // A helper type used by SurfaceCacheImpl::CollectSizeOfSurfaces.
180 struct MOZ_STACK_CLASS SurfaceMemoryReport
182 SurfaceMemoryReport(nsTArray<SurfaceMemoryCounter>& aCounters,
183 MallocSizeOf aMallocSizeOf)
184 : mCounters(aCounters)
185 , mMallocSizeOf(aMallocSizeOf)
188 void Add(NotNull<CachedSurface*> aCachedSurface, bool aIsFactor2)
190 SurfaceMemoryCounter counter(aCachedSurface->GetSurfaceKey(),
191 aCachedSurface->IsLocked(),
192 aCachedSurface->CannotSubstitute(),
193 aIsFactor2);
195 if (aCachedSurface->IsPlaceholder()) {
196 return;
199 // Record the memory used by the ISurfaceProvider. This may not have a
200 // straightforward relationship to the size of the surface that
201 // DrawableRef() returns if the surface is generated dynamically. (i.e.,
202 // for surfaces with PlaybackType::eAnimated.)
203 size_t heap = 0;
204 size_t nonHeap = 0;
205 size_t handles = 0;
206 aCachedSurface->mProvider
207 ->AddSizeOfExcludingThis(mMallocSizeOf, heap, nonHeap, handles);
208 counter.Values().SetDecodedHeap(heap);
209 counter.Values().SetDecodedNonHeap(nonHeap);
210 counter.Values().SetExternalHandles(handles);
212 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
227 AreaOfIntSize(const IntSize& aSize) {
228 return static_cast<int64_t>(aSize.width) * static_cast<int64_t>(aSize.height);
232 * An ImageSurfaceCache is a per-image surface cache. For correctness we must be
233 * able to remove all surfaces associated with an image when the image is
234 * destroyed or invalidated. Since this will happen frequently, it makes sense
235 * to make it cheap by storing the surfaces for each image separately.
237 * ImageSurfaceCache also keeps track of whether its associated image is locked
238 * or unlocked.
240 * The cache may also enter "factor of 2" mode which occurs when the number of
241 * surfaces in the cache exceeds the "image.cache.factor2.threshold-surfaces"
242 * pref plus the number of native sizes of the image. When in "factor of 2"
243 * mode, the cache will strongly favour sizes which are a factor of 2 of the
244 * largest native size. It accomplishes this by suggesting a factor of 2 size
245 * when lookups fail and substituting the nearest factor of 2 surface to the
246 * ideal size as the "best" available (as opposed to subsitution but not found).
247 * This allows us to minimize memory consumption and CPU time spent decoding
248 * when a website requires many variants of the same surface.
250 class ImageSurfaceCache
252 ~ImageSurfaceCache() { }
253 public:
254 ImageSurfaceCache()
255 : mLocked(false)
256 , mFactor2Mode(false)
257 , mFactor2Pruned(false)
260 MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageSurfaceCache)
261 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageSurfaceCache)
263 typedef
264 nsRefPtrHashtable<nsGenericHashKey<SurfaceKey>, CachedSurface> SurfaceTable;
266 bool IsEmpty() const { return mSurfaces.Count() == 0; }
268 MOZ_MUST_USE bool Insert(NotNull<CachedSurface*> aSurface)
270 MOZ_ASSERT(!mLocked || aSurface->IsPlaceholder() || aSurface->IsLocked(),
271 "Inserting an unlocked surface for a locked image");
272 return mSurfaces.Put(aSurface->GetSurfaceKey(), aSurface, fallible);
275 already_AddRefed<CachedSurface> Remove(NotNull<CachedSurface*> aSurface)
277 MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()),
278 "Should not be removing a surface we don't have");
280 RefPtr<CachedSurface> surface;
281 mSurfaces.Remove(aSurface->GetSurfaceKey(), getter_AddRefs(surface));
282 AfterMaybeRemove();
283 return surface.forget();
286 already_AddRefed<CachedSurface> Lookup(const SurfaceKey& aSurfaceKey,
287 bool aForAccess)
289 RefPtr<CachedSurface> surface;
290 mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface));
292 if (aForAccess) {
293 if (surface) {
294 // We don't want to allow factor of 2 mode pruning to release surfaces
295 // for which the callers will accept no substitute.
296 surface->SetCannotSubstitute();
297 } else if (!mFactor2Mode) {
298 // If no exact match is found, and this is for use rather than internal
299 // accounting (i.e. insert and removal), we know this will trigger a
300 // decode. Make sure we switch now to factor of 2 mode if necessary.
301 MaybeSetFactor2Mode();
305 return surface.forget();
309 * @returns A tuple containing the best matching CachedSurface if available,
310 * a MatchType describing how the CachedSurface was selected, and
311 * an IntSize which is the size the caller should choose to decode
312 * at should it attempt to do so.
314 Tuple<already_AddRefed<CachedSurface>, MatchType, IntSize>
315 LookupBestMatch(const SurfaceKey& aIdealKey)
317 // Try for an exact match first.
318 RefPtr<CachedSurface> exactMatch;
319 mSurfaces.Get(aIdealKey, getter_AddRefs(exactMatch));
320 if (exactMatch) {
321 if (exactMatch->IsDecoded()) {
322 return MakeTuple(exactMatch.forget(), MatchType::EXACT, IntSize());
324 } else if (!mFactor2Mode) {
325 // If no exact match is found, and we are not in factor of 2 mode, then
326 // we know that we will trigger a decode because at best we will provide
327 // a substitute. Make sure we switch now to factor of 2 mode if necessary.
328 MaybeSetFactor2Mode();
331 // Try for a best match second, if using compact.
332 IntSize suggestedSize = SuggestedSize(aIdealKey.Size());
333 if (mFactor2Mode) {
334 if (!exactMatch) {
335 SurfaceKey compactKey = aIdealKey.CloneWithSize(suggestedSize);
336 mSurfaces.Get(compactKey, getter_AddRefs(exactMatch));
337 if (exactMatch && exactMatch->IsDecoded()) {
338 MOZ_ASSERT(suggestedSize != aIdealKey.Size());
339 return MakeTuple(exactMatch.forget(),
340 MatchType::SUBSTITUTE_BECAUSE_BEST,
341 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()) {
354 continue;
356 // Matching the playback type and SVG context is required.
357 if (currentKey.Playback() != aIdealKey.Playback() ||
358 currentKey.SVGContext() != aIdealKey.SVGContext()) {
359 continue;
361 // Matching the flags is required.
362 if (currentKey.Flags() != aIdealKey.Flags()) {
363 continue;
365 // Anything is better than nothing! (Within the constraints we just
366 // checked, of course.)
367 if (!bestMatch) {
368 bestMatch = current;
369 continue;
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()) {
377 continue;
379 if (!bestMatchIsDecoded && current->IsDecoded()) {
380 bestMatch = current;
381 continue;
384 SurfaceKey bestMatchKey = bestMatch->GetSurfaceKey();
385 if (CompareArea(aIdealKey.Size(), bestMatchKey.Size(),
386 currentKey.Size())) {
387 bestMatch = current;
391 MatchType matchType;
392 if (bestMatch) {
393 if (!exactMatch) {
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);
405 matchType = MatchType::SUBSTITUTE_BECAUSE_BEST;
406 } else {
407 // The exact match is still decoding, but it's the best we've got.
408 matchType = MatchType::EXACT;
410 } else {
411 if (exactMatch) {
412 // We found an "exact match"; it must have been a placeholder.
413 MOZ_ASSERT(exactMatch->IsPlaceholder());
414 matchType = MatchType::PENDING;
415 } else {
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()
426 MOZ_ASSERT(!mFactor2Mode);
428 // Typically an image cache will not have too many size-varying surfaces, so
429 // if we exceed the given threshold, we should consider using a subset.
430 int32_t thresholdSurfaces = gfxPrefs::ImageCacheFactor2ThresholdSurfaces();
431 if (thresholdSurfaces < 0 ||
432 mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
433 return;
436 // Determine how many native surfaces this image has. Zero means we either
437 // don't know yet (in which case do nothing), or we don't want to limit the
438 // number of surfaces for this image.
440 // XXX(aosmond): Vector images have zero native sizes. This is because they
441 // are regenerated at the given size. There isn't an equivalent concept to
442 // the native size (and w/h ratio) to provide a frame of reference to what
443 // are "good" sizes. While it is desirable to have a similar mechanism as
444 // that for raster images, it will need a different approach.
445 auto first = ConstIter();
446 NotNull<CachedSurface*> current = WrapNotNull(first.UserData());
447 Image* image = static_cast<Image*>(current->GetImageKey());
448 size_t nativeSizes = image->GetNativeSizesLength();
449 if (nativeSizes == 0) {
450 return;
453 // Increase the threshold by the number of native sizes. This ensures that
454 // we do not prevent decoding of the image at all its native sizes. It does
455 // not guarantee we will provide a surface at that size however (i.e. many
456 // other sized surfaces are requested, in addition to the native sizes).
457 thresholdSurfaces += nativeSizes;
458 if (mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
459 return;
462 // Get our native size. While we know the image should be fully decoded,
463 // if it is an SVG, it is valid to have a zero size. We can't do compacting
464 // in that case because we need to know the width/height ratio to define a
465 // candidate set.
466 IntSize nativeSize;
467 if (NS_FAILED(image->GetWidth(&nativeSize.width)) ||
468 NS_FAILED(image->GetHeight(&nativeSize.height)) ||
469 nativeSize.IsEmpty()) {
470 return;
473 // We have a valid size, we can change modes.
474 mFactor2Mode = true;
477 template<typename Function>
478 void Prune(Function&& aRemoveCallback)
480 if (!mFactor2Mode || mFactor2Pruned) {
481 return;
484 // Attempt to discard any surfaces which are not factor of 2 and the best
485 // factor of 2 match exists.
486 bool hasNotFactorSize = false;
487 for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
488 NotNull<CachedSurface*> current = WrapNotNull(iter.UserData());
489 const SurfaceKey& currentKey = current->GetSurfaceKey();
490 const IntSize& currentSize = currentKey.Size();
492 // First we check if someone requested this size and would not accept
493 // an alternatively sized surface.
494 if (current->CannotSubstitute()) {
495 continue;
498 // Next we find the best factor of 2 size for this surface. If this
499 // surface is a factor of 2 size, then we want to keep it.
500 IntSize bestSize = SuggestedSize(currentSize);
501 if (bestSize == currentSize) {
502 continue;
505 // Check the cache for a surface with the same parameters except for the
506 // size which uses the closest factor of 2 size.
507 SurfaceKey compactKey = currentKey.CloneWithSize(bestSize);
508 RefPtr<CachedSurface> compactMatch;
509 mSurfaces.Get(compactKey, getter_AddRefs(compactMatch));
510 if (compactMatch && compactMatch->IsDecoded()) {
511 aRemoveCallback(current);
512 iter.Remove();
513 } else {
514 hasNotFactorSize = true;
518 // We have no surfaces that are not factor of 2 sized, so we can stop
519 // pruning henceforth, because we avoid the insertion of new surfaces that
520 // don't match our sizing set (unless the caller won't accept a
521 // substitution.)
522 if (!hasNotFactorSize) {
523 mFactor2Pruned = true;
526 // We should never leave factor of 2 mode due to pruning in of itself, but
527 // if we discarded surfaces due to the volatile buffers getting released,
528 // it is possible.
529 AfterMaybeRemove();
532 IntSize SuggestedSize(const IntSize& aSize) const
534 // When not in factor of 2 mode, we can always decode at the given size.
535 if (!mFactor2Mode) {
536 return aSize;
539 // We cannot enter factor of 2 mode unless we have a minimum number of
540 // surfaces, and we should have left it if the cache was emptied.
541 if (MOZ_UNLIKELY(IsEmpty())) {
542 MOZ_ASSERT_UNREACHABLE("Should not be empty and in factor of 2 mode!");
543 return aSize;
546 // This bit of awkwardness gets the largest native size of the image.
547 auto iter = ConstIter();
548 NotNull<CachedSurface*> firstSurface = WrapNotNull(iter.UserData());
549 Image* image = static_cast<Image*>(firstSurface->GetImageKey());
550 IntSize factorSize;
551 if (NS_FAILED(image->GetWidth(&factorSize.width)) ||
552 NS_FAILED(image->GetHeight(&factorSize.height)) ||
553 factorSize.IsEmpty()) {
554 // We should not have entered factor of 2 mode without a valid size, and
555 // several successfully decoded surfaces.
556 MOZ_ASSERT_UNREACHABLE("Expected valid native size!");
557 return aSize;
560 // Start with the native size as the best first guess.
561 IntSize bestSize = factorSize;
562 factorSize.width /= 2;
563 factorSize.height /= 2;
565 while (!factorSize.IsEmpty()) {
566 if (!CompareArea(aSize, bestSize, factorSize)) {
567 // This size is not better than the last. Since we proceed from largest
568 // to smallest, we know that the next size will not be better if the
569 // previous size was rejected. Break early.
570 break;
573 // The current factor of 2 size is better than the last selected size.
574 bestSize = factorSize;
575 factorSize.width /= 2;
576 factorSize.height /= 2;
579 return bestSize;
582 bool CompareArea(const IntSize& aIdealSize,
583 const IntSize& aBestSize,
584 const IntSize& aSize) const
586 // Compare sizes. We use an area-based heuristic here instead of computing a
587 // truly optimal answer, since it seems very unlikely to make a difference
588 // for realistic sizes.
589 int64_t idealArea = AreaOfIntSize(aIdealSize);
590 int64_t currentArea = AreaOfIntSize(aSize);
591 int64_t bestMatchArea = AreaOfIntSize(aBestSize);
593 // If the best match is smaller than the ideal size, prefer bigger sizes.
594 if (bestMatchArea < idealArea) {
595 if (currentArea > bestMatchArea) {
596 return true;
598 return false;
601 // Other, prefer sizes closer to the ideal size, but still not smaller.
602 if (idealArea <= currentArea && currentArea < bestMatchArea) {
603 return true;
606 // This surface isn't an improvement over the current best match.
607 return false;
610 template<typename Function>
611 void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
612 MallocSizeOf aMallocSizeOf,
613 Function&& aRemoveCallback)
615 CachedSurface::SurfaceMemoryReport report(aCounters, aMallocSizeOf);
616 for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
617 NotNull<CachedSurface*> surface = WrapNotNull(iter.UserData());
619 // We don't need the drawable surface for ourselves, but adding a surface
620 // to the report will trigger this indirectly. If the surface was
621 // discarded by the OS because it was in volatile memory, we should remove
622 // it from the cache immediately rather than include it in the report.
623 DrawableSurface drawableSurface;
624 if (!surface->IsPlaceholder()) {
625 drawableSurface = surface->GetDrawableSurface();
626 if (!drawableSurface) {
627 aRemoveCallback(surface);
628 iter.Remove();
629 continue;
633 const IntSize& size = surface->GetSurfaceKey().Size();
634 bool factor2Size = false;
635 if (mFactor2Mode) {
636 factor2Size = (size == SuggestedSize(size));
638 report.Add(surface, factor2Size);
641 AfterMaybeRemove();
644 SurfaceTable::Iterator ConstIter() const
646 return mSurfaces.ConstIter();
649 void SetLocked(bool aLocked) { mLocked = aLocked; }
650 bool IsLocked() const { return mLocked; }
652 private:
653 void AfterMaybeRemove()
655 if (IsEmpty() && mFactor2Mode) {
656 // The last surface for this cache was removed. This can happen if the
657 // surface was stored in a volatile buffer and got purged, or the surface
658 // expired from the cache. If the cache itself lingers for some reason
659 // (e.g. in the process of performing a lookup, the cache itself is
660 // locked), then we need to reset the factor of 2 state because it
661 // requires at least one surface present to get the native size
662 // information from the image.
663 mFactor2Mode = mFactor2Pruned = false;
667 SurfaceTable mSurfaces;
669 bool mLocked;
671 // True in "factor of 2" mode.
672 bool mFactor2Mode;
674 // True if all non-factor of 2 surfaces have been removed from the cache. Note
675 // that this excludes unsubstitutable sizes.
676 bool mFactor2Pruned;
680 * SurfaceCacheImpl is responsible for determining which surfaces will be cached
681 * and managing the surface cache data structures. Rather than interact with
682 * SurfaceCacheImpl directly, client code interacts with SurfaceCache, which
683 * maintains high-level invariants and encapsulates the details of the surface
684 * cache's implementation.
686 class SurfaceCacheImpl final : public nsIMemoryReporter
688 public:
689 NS_DECL_ISUPPORTS
691 SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS,
692 uint32_t aSurfaceCacheDiscardFactor,
693 uint32_t aSurfaceCacheSize)
694 : mExpirationTracker(aSurfaceCacheExpirationTimeMS)
695 , mMemoryPressureObserver(new MemoryPressureObserver)
696 , mDiscardFactor(aSurfaceCacheDiscardFactor)
697 , mMaxCost(aSurfaceCacheSize)
698 , mAvailableCost(aSurfaceCacheSize)
699 , mLockedCost(0)
700 , mOverflowCount(0)
702 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
703 if (os) {
704 os->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
708 private:
709 virtual ~SurfaceCacheImpl()
711 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
712 if (os) {
713 os->RemoveObserver(mMemoryPressureObserver, "memory-pressure");
716 UnregisterWeakMemoryReporter(this);
719 public:
720 void InitMemoryReporter() { RegisterWeakMemoryReporter(this); }
722 InsertOutcome Insert(NotNull<ISurfaceProvider*> aProvider,
723 bool aSetAvailable,
724 const StaticMutexAutoLock& aAutoLock)
726 // If this is a duplicate surface, refuse to replace the original.
727 // XXX(seth): Calling Lookup() and then RemoveEntry() does the lookup
728 // twice. We'll make this more efficient in bug 1185137.
729 LookupResult result = Lookup(aProvider->GetImageKey(),
730 aProvider->GetSurfaceKey(),
731 aAutoLock,
732 /* aMarkUsed = */ false);
733 if (MOZ_UNLIKELY(result)) {
734 return InsertOutcome::FAILURE_ALREADY_PRESENT;
737 if (result.Type() == MatchType::PENDING) {
738 RemoveEntry(aProvider->GetImageKey(), aProvider->GetSurfaceKey(), aAutoLock);
741 MOZ_ASSERT(result.Type() == MatchType::NOT_FOUND ||
742 result.Type() == MatchType::PENDING,
743 "A LookupResult with no surface should be NOT_FOUND or PENDING");
745 // If this is bigger than we can hold after discarding everything we can,
746 // refuse to cache it.
747 Cost cost = aProvider->LogicalSizeInBytes();
748 if (MOZ_UNLIKELY(!CanHoldAfterDiscarding(cost))) {
749 mOverflowCount++;
750 return InsertOutcome::FAILURE;
753 // Remove elements in order of cost until we can fit this in the cache. Note
754 // that locked surfaces aren't in mCosts, so we never remove them here.
755 while (cost > mAvailableCost) {
756 MOZ_ASSERT(!mCosts.IsEmpty(),
757 "Removed everything and it still won't fit");
758 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true, aAutoLock);
761 // Locate the appropriate per-image cache. If there's not an existing cache
762 // for this image, create it.
763 RefPtr<ImageSurfaceCache> cache = GetImageCache(aProvider->GetImageKey());
764 if (!cache) {
765 cache = new ImageSurfaceCache;
766 mImageCaches.Put(aProvider->GetImageKey(), cache);
769 // If we were asked to mark the cache entry available, do so.
770 if (aSetAvailable) {
771 aProvider->Availability().SetAvailable();
774 auto surface = MakeNotNull<RefPtr<CachedSurface>>(aProvider);
776 // We require that locking succeed if the image is locked and we're not
777 // inserting a placeholder; the caller may need to know this to handle
778 // errors correctly.
779 bool mustLock = cache->IsLocked() && !surface->IsPlaceholder();
780 if (mustLock) {
781 surface->SetLocked(true);
782 if (!surface->IsLocked()) {
783 return InsertOutcome::FAILURE;
787 // Insert.
788 MOZ_ASSERT(cost <= mAvailableCost, "Inserting despite too large a cost");
789 if (!cache->Insert(surface)) {
790 if (mustLock) {
791 surface->SetLocked(false);
793 return InsertOutcome::FAILURE;
796 if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
797 MOZ_ASSERT(!mustLock);
798 Remove(surface, /* aStopTracking */ false, aAutoLock);
799 return InsertOutcome::FAILURE;
802 return InsertOutcome::SUCCESS;
805 void Remove(NotNull<CachedSurface*> aSurface,
806 bool aStopTracking,
807 const StaticMutexAutoLock& aAutoLock)
809 ImageKey imageKey = aSurface->GetImageKey();
811 RefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
812 MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache");
814 // If the surface was not a placeholder, tell its image that we discarded it.
815 if (!aSurface->IsPlaceholder()) {
816 static_cast<Image*>(imageKey)->OnSurfaceDiscarded(aSurface->GetSurfaceKey());
819 // If we failed during StartTracking, we can skip this step.
820 if (aStopTracking) {
821 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
824 // Individual surfaces must be freed outside the lock.
825 mCachedSurfacesDiscard.AppendElement(cache->Remove(aSurface));
827 MaybeRemoveEmptyCache(imageKey, cache);
830 bool StartTracking(NotNull<CachedSurface*> aSurface,
831 const StaticMutexAutoLock& aAutoLock)
833 CostEntry costEntry = aSurface->GetCostEntry();
834 MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost,
835 "Cost too large and the caller didn't catch it");
837 if (aSurface->IsLocked()) {
838 mLockedCost += costEntry.GetCost();
839 MOZ_ASSERT(mLockedCost <= mMaxCost, "Locked more than we can hold?");
840 } else {
841 if (NS_WARN_IF(!mCosts.InsertElementSorted(costEntry, fallible))) {
842 return false;
845 // This may fail during XPCOM shutdown, so we need to ensure the object is
846 // tracked before calling RemoveObject in StopTracking.
847 nsresult rv = mExpirationTracker.AddObjectLocked(aSurface, aAutoLock);
848 if (NS_WARN_IF(NS_FAILED(rv))) {
849 DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
850 MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
851 return false;
855 mAvailableCost -= costEntry.GetCost();
856 return true;
859 void StopTracking(NotNull<CachedSurface*> aSurface,
860 bool aIsTracked,
861 const StaticMutexAutoLock& aAutoLock)
863 CostEntry costEntry = aSurface->GetCostEntry();
865 if (aSurface->IsLocked()) {
866 MOZ_ASSERT(mLockedCost >= costEntry.GetCost(), "Costs don't balance");
867 mLockedCost -= costEntry.GetCost();
868 // XXX(seth): It'd be nice to use an O(log n) lookup here. This is O(n).
869 MOZ_ASSERT(!mCosts.Contains(costEntry),
870 "Shouldn't have a cost entry for a locked surface");
871 } else {
872 if (MOZ_LIKELY(aSurface->GetExpirationState()->IsTracked())) {
873 MOZ_ASSERT(aIsTracked, "Expiration-tracking a surface unexpectedly!");
874 mExpirationTracker.RemoveObjectLocked(aSurface, aAutoLock);
875 } else {
876 // Our call to AddObject must have failed in StartTracking; most likely
877 // we're in XPCOM shutdown right now.
878 MOZ_ASSERT(!aIsTracked, "Not expiration-tracking an unlocked surface!");
881 DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
882 MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
885 mAvailableCost += costEntry.GetCost();
886 MOZ_ASSERT(mAvailableCost <= mMaxCost,
887 "More available cost than we started with");
890 LookupResult Lookup(const ImageKey aImageKey,
891 const SurfaceKey& aSurfaceKey,
892 const StaticMutexAutoLock& aAutoLock,
893 bool aMarkUsed = true)
895 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
896 if (!cache) {
897 // No cached surfaces for this image.
898 return LookupResult(MatchType::NOT_FOUND);
901 RefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey, aMarkUsed);
902 if (!surface) {
903 // Lookup in the per-image cache missed.
904 return LookupResult(MatchType::NOT_FOUND);
907 if (surface->IsPlaceholder()) {
908 return LookupResult(MatchType::PENDING);
911 DrawableSurface drawableSurface = surface->GetDrawableSurface();
912 if (!drawableSurface) {
913 // The surface was released by the operating system. Remove the cache
914 // entry as well.
915 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
916 return LookupResult(MatchType::NOT_FOUND);
919 if (aMarkUsed &&
920 !MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
921 Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
922 return LookupResult(MatchType::NOT_FOUND);
925 MOZ_ASSERT(surface->GetSurfaceKey() == aSurfaceKey,
926 "Lookup() not returning an exact match?");
927 return LookupResult(Move(drawableSurface), MatchType::EXACT);
930 LookupResult LookupBestMatch(const ImageKey aImageKey,
931 const SurfaceKey& aSurfaceKey,
932 const StaticMutexAutoLock& aAutoLock)
934 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
935 if (!cache) {
936 // No cached surfaces for this image.
937 return LookupResult(MatchType::NOT_FOUND);
940 // Repeatedly look up the best match, trying again if the resulting surface
941 // has been freed by the operating system, until we can either lock a
942 // surface for drawing or there are no matching surfaces left.
943 // XXX(seth): This is O(N^2), but N is expected to be very small. If we
944 // encounter a performance problem here we can revisit this.
946 RefPtr<CachedSurface> surface;
947 DrawableSurface drawableSurface;
948 MatchType matchType = MatchType::NOT_FOUND;
949 IntSize suggestedSize;
950 while (true) {
951 Tie(surface, matchType, suggestedSize)
952 = cache->LookupBestMatch(aSurfaceKey);
954 if (!surface) {
955 return LookupResult(matchType); // Lookup in the per-image cache missed.
958 drawableSurface = surface->GetDrawableSurface();
959 if (drawableSurface) {
960 break;
963 // The surface was released by the operating system. Remove the cache
964 // entry as well.
965 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
968 MOZ_ASSERT_IF(matchType == MatchType::EXACT,
969 surface->GetSurfaceKey() == aSurfaceKey);
970 MOZ_ASSERT_IF(matchType == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND ||
971 matchType == MatchType::SUBSTITUTE_BECAUSE_PENDING,
972 surface->GetSurfaceKey().SVGContext() == aSurfaceKey.SVGContext() &&
973 surface->GetSurfaceKey().Playback() == aSurfaceKey.Playback() &&
974 surface->GetSurfaceKey().Flags() == aSurfaceKey.Flags());
976 if (matchType == MatchType::EXACT ||
977 matchType == MatchType::SUBSTITUTE_BECAUSE_BEST) {
978 if (!MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
979 Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
983 return LookupResult(Move(drawableSurface), matchType, suggestedSize);
986 bool CanHold(const Cost aCost) const
988 return aCost <= mMaxCost;
991 size_t MaximumCapacity() const
993 return size_t(mMaxCost);
996 void SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider,
997 const StaticMutexAutoLock& aAutoLock)
999 if (!aProvider->Availability().IsPlaceholder()) {
1000 MOZ_ASSERT_UNREACHABLE("Calling SurfaceAvailable on non-placeholder");
1001 return;
1004 // Reinsert the provider, requesting that Insert() mark it available. This
1005 // may or may not succeed, depending on whether some other decoder has
1006 // beaten us to the punch and inserted a non-placeholder version of this
1007 // surface first, but it's fine either way.
1008 // XXX(seth): This could be implemented more efficiently; we should be able
1009 // to just update our data structures without reinserting.
1010 Insert(aProvider, /* aSetAvailable = */ true, aAutoLock);
1013 void LockImage(const ImageKey aImageKey)
1015 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1016 if (!cache) {
1017 cache = new ImageSurfaceCache;
1018 mImageCaches.Put(aImageKey, cache);
1021 cache->SetLocked(true);
1023 // We don't relock this image's existing surfaces right away; instead, the
1024 // image should arrange for Lookup() to touch them if they are still useful.
1027 void UnlockImage(const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock)
1029 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1030 if (!cache || !cache->IsLocked()) {
1031 return; // Already unlocked.
1034 cache->SetLocked(false);
1035 DoUnlockSurfaces(WrapNotNull(cache), /* aStaticOnly = */ false, aAutoLock);
1038 void UnlockEntries(const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock)
1040 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1041 if (!cache || !cache->IsLocked()) {
1042 return; // Already unlocked.
1045 // (Note that we *don't* unlock the per-image cache here; that's the
1046 // difference between this and UnlockImage.)
1047 DoUnlockSurfaces(WrapNotNull(cache),
1048 /* aStaticOnly = */ !gfxPrefs::ImageMemAnimatedDiscardable(), aAutoLock);
1051 already_AddRefed<ImageSurfaceCache>
1052 RemoveImage(const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock)
1054 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1055 if (!cache) {
1056 return nullptr; // No cached surfaces for this image, so nothing to do.
1059 // Discard all of the cached surfaces for this image.
1060 // XXX(seth): This is O(n^2) since for each item in the cache we are
1061 // removing an element from the costs array. Since n is expected to be
1062 // small, performance should be good, but if usage patterns change we should
1063 // change the data structure used for mCosts.
1064 for (auto iter = cache->ConstIter(); !iter.Done(); iter.Next()) {
1065 StopTracking(WrapNotNull(iter.UserData()),
1066 /* aIsTracked */ true, aAutoLock);
1069 // The per-image cache isn't needed anymore, so remove it as well.
1070 // This implicitly unlocks the image if it was locked.
1071 mImageCaches.Remove(aImageKey);
1073 // Since we did not actually remove any of the surfaces from the cache
1074 // itself, only stopped tracking them, we should free it outside the lock.
1075 return cache.forget();
1078 void PruneImage(const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock)
1080 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1081 if (!cache) {
1082 return; // No cached surfaces for this image, so nothing to do.
1085 cache->Prune([this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
1086 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1087 // Individual surfaces must be freed outside the lock.
1088 mCachedSurfacesDiscard.AppendElement(aSurface);
1091 MaybeRemoveEmptyCache(aImageKey, cache);
1094 void DiscardAll(const StaticMutexAutoLock& aAutoLock)
1096 // Remove in order of cost because mCosts is an array and the other data
1097 // structures are all hash tables. Note that locked surfaces are not
1098 // removed, since they aren't present in mCosts.
1099 while (!mCosts.IsEmpty()) {
1100 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true, aAutoLock);
1104 void DiscardForMemoryPressure(const StaticMutexAutoLock& aAutoLock)
1106 // Compute our discardable cost. Since locked surfaces aren't discardable,
1107 // we exclude them.
1108 const Cost discardableCost = (mMaxCost - mAvailableCost) - mLockedCost;
1109 MOZ_ASSERT(discardableCost <= mMaxCost, "Discardable cost doesn't add up");
1111 // Our target is to raise our available cost by (1 / mDiscardFactor) of our
1112 // discardable cost - in other words, we want to end up with about
1113 // (discardableCost / mDiscardFactor) fewer bytes stored in the surface
1114 // cache after we're done.
1115 const Cost targetCost = mAvailableCost + (discardableCost / mDiscardFactor);
1117 if (targetCost > mMaxCost - mLockedCost) {
1118 MOZ_ASSERT_UNREACHABLE("Target cost is more than we can discard");
1119 DiscardAll(aAutoLock);
1120 return;
1123 // Discard surfaces until we've reduced our cost to our target cost.
1124 while (mAvailableCost < targetCost) {
1125 MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and still not done");
1126 Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true, aAutoLock);
1130 void TakeDiscard(nsTArray<RefPtr<CachedSurface>>& aDiscard,
1131 const StaticMutexAutoLock& aAutoLock)
1133 MOZ_ASSERT(aDiscard.IsEmpty());
1134 aDiscard = Move(mCachedSurfacesDiscard);
1137 void LockSurface(NotNull<CachedSurface*> aSurface,
1138 const StaticMutexAutoLock& aAutoLock)
1140 if (aSurface->IsPlaceholder() || aSurface->IsLocked()) {
1141 return;
1144 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1146 // Lock the surface. This can fail.
1147 aSurface->SetLocked(true);
1148 DebugOnly<bool> tracking = StartTracking(aSurface, aAutoLock);
1149 MOZ_ASSERT(tracking);
1152 NS_IMETHOD
1153 CollectReports(nsIHandleReportCallback* aHandleReport,
1154 nsISupports* aData,
1155 bool aAnonymize) override
1157 StaticMutexAutoLock lock(sInstanceMutex);
1159 // We have explicit memory reporting for the surface cache which is more
1160 // accurate than the cost metrics we report here, but these metrics are
1161 // still useful to report, since they control the cache's behavior.
1162 MOZ_COLLECT_REPORT(
1163 "imagelib-surface-cache-estimated-total",
1164 KIND_OTHER, UNITS_BYTES, (mMaxCost - mAvailableCost),
1165 "Estimated total memory used by the imagelib surface cache.");
1167 MOZ_COLLECT_REPORT(
1168 "imagelib-surface-cache-estimated-locked",
1169 KIND_OTHER, UNITS_BYTES, mLockedCost,
1170 "Estimated memory used by locked surfaces in the imagelib surface cache.");
1172 MOZ_COLLECT_REPORT(
1173 "imagelib-surface-cache-overflow-count",
1174 KIND_OTHER, UNITS_COUNT, mOverflowCount,
1175 "Count of how many times the surface cache has hit its capacity and been "
1176 "unable to insert a new surface.");
1178 return NS_OK;
1181 void CollectSizeOfSurfaces(const ImageKey aImageKey,
1182 nsTArray<SurfaceMemoryCounter>& aCounters,
1183 MallocSizeOf aMallocSizeOf,
1184 const StaticMutexAutoLock& aAutoLock)
1186 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1187 if (!cache) {
1188 return; // No surfaces for this image.
1191 // Report all surfaces in the per-image cache.
1192 cache->CollectSizeOfSurfaces(aCounters, aMallocSizeOf,
1193 [this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
1194 StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
1195 // Individual surfaces must be freed outside the lock.
1196 mCachedSurfacesDiscard.AppendElement(aSurface);
1199 MaybeRemoveEmptyCache(aImageKey, cache);
1202 private:
1203 already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey)
1205 RefPtr<ImageSurfaceCache> imageCache;
1206 mImageCaches.Get(aImageKey, getter_AddRefs(imageCache));
1207 return imageCache.forget();
1210 void MaybeRemoveEmptyCache(const ImageKey aImageKey,
1211 ImageSurfaceCache* aCache)
1213 // Remove the per-image cache if it's unneeded now. Keep it if the image is
1214 // locked, since the per-image cache is where we store that state. Note that
1215 // we don't push it into mImageCachesDiscard because all of its surfaces
1216 // have been removed, so it is safe to free while holding the lock.
1217 if (aCache->IsEmpty() && !aCache->IsLocked()) {
1218 mImageCaches.Remove(aImageKey);
1222 // This is similar to CanHold() except that it takes into account the costs of
1223 // locked surfaces. It's used internally in Insert(), but it's not exposed
1224 // publicly because we permit multithreaded access to the surface cache, which
1225 // means that the result would be meaningless: another thread could insert a
1226 // surface or lock an image at any time.
1227 bool CanHoldAfterDiscarding(const Cost aCost) const
1229 return aCost <= mMaxCost - mLockedCost;
1232 bool MarkUsed(NotNull<CachedSurface*> aSurface,
1233 NotNull<ImageSurfaceCache*> aCache,
1234 const StaticMutexAutoLock& aAutoLock)
1236 if (aCache->IsLocked()) {
1237 LockSurface(aSurface, aAutoLock);
1238 return true;
1241 nsresult rv = mExpirationTracker.MarkUsedLocked(aSurface, aAutoLock);
1242 if (NS_WARN_IF(NS_FAILED(rv))) {
1243 // If mark used fails, it is because it failed to reinsert the surface
1244 // after removing it from the tracker. Thus we need to update our
1245 // own accounting but otherwise expect it to be untracked.
1246 StopTracking(aSurface, /* aIsTracked */ false, aAutoLock);
1247 return false;
1249 return true;
1252 void DoUnlockSurfaces(NotNull<ImageSurfaceCache*> aCache, bool aStaticOnly,
1253 const StaticMutexAutoLock& aAutoLock)
1255 AutoTArray<NotNull<CachedSurface*>, 8> discard;
1257 // Unlock all the surfaces the per-image cache is holding.
1258 for (auto iter = aCache->ConstIter(); !iter.Done(); iter.Next()) {
1259 NotNull<CachedSurface*> surface = WrapNotNull(iter.UserData());
1260 if (surface->IsPlaceholder() || !surface->IsLocked()) {
1261 continue;
1263 if (aStaticOnly && surface->GetSurfaceKey().Playback() != PlaybackType::eStatic) {
1264 continue;
1266 StopTracking(surface, /* aIsTracked */ true, aAutoLock);
1267 surface->SetLocked(false);
1268 if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
1269 discard.AppendElement(surface);
1273 // Discard any that we failed to track.
1274 for (auto iter = discard.begin(); iter != discard.end(); ++iter) {
1275 Remove(*iter, /* aStopTracking */ false, aAutoLock);
1279 void RemoveEntry(const ImageKey aImageKey,
1280 const SurfaceKey& aSurfaceKey,
1281 const StaticMutexAutoLock& aAutoLock)
1283 RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
1284 if (!cache) {
1285 return; // No cached surfaces for this image.
1288 RefPtr<CachedSurface> surface =
1289 cache->Lookup(aSurfaceKey, /* aForAccess = */ false);
1290 if (!surface) {
1291 return; // Lookup in the per-image cache missed.
1294 Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
1297 class SurfaceTracker final :
1298 public ExpirationTrackerImpl<CachedSurface, 2,
1299 StaticMutex,
1300 StaticMutexAutoLock>
1302 public:
1303 explicit SurfaceTracker(uint32_t aSurfaceCacheExpirationTimeMS)
1304 : ExpirationTrackerImpl<CachedSurface, 2,
1305 StaticMutex, StaticMutexAutoLock>(
1306 aSurfaceCacheExpirationTimeMS, "SurfaceTracker",
1307 SystemGroup::EventTargetFor(TaskCategory::Other))
1310 protected:
1311 void NotifyExpiredLocked(CachedSurface* aSurface,
1312 const StaticMutexAutoLock& aAutoLock) override
1314 sInstance->Remove(WrapNotNull(aSurface), /* aStopTracking */ true, aAutoLock);
1317 void NotifyHandlerEndLocked(const StaticMutexAutoLock& aAutoLock) override
1319 sInstance->TakeDiscard(mDiscard, aAutoLock);
1322 void NotifyHandlerEnd() override
1324 nsTArray<RefPtr<CachedSurface>> discard(Move(mDiscard));
1327 StaticMutex& GetMutex() override
1329 return sInstanceMutex;
1332 nsTArray<RefPtr<CachedSurface>> mDiscard;
1335 class MemoryPressureObserver final : public nsIObserver
1337 public:
1338 NS_DECL_ISUPPORTS
1340 NS_IMETHOD Observe(nsISupports*,
1341 const char* aTopic,
1342 const char16_t*) override
1344 nsTArray<RefPtr<CachedSurface>> discard;
1346 StaticMutexAutoLock lock(sInstanceMutex);
1347 if (sInstance && strcmp(aTopic, "memory-pressure") == 0) {
1348 sInstance->DiscardForMemoryPressure(lock);
1349 sInstance->TakeDiscard(discard, lock);
1352 return NS_OK;
1355 private:
1356 virtual ~MemoryPressureObserver() { }
1359 nsTArray<CostEntry> mCosts;
1360 nsRefPtrHashtable<nsPtrHashKey<Image>,
1361 ImageSurfaceCache> mImageCaches;
1362 nsTArray<RefPtr<CachedSurface>> mCachedSurfacesDiscard;
1363 SurfaceTracker mExpirationTracker;
1364 RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
1365 const uint32_t mDiscardFactor;
1366 const Cost mMaxCost;
1367 Cost mAvailableCost;
1368 Cost mLockedCost;
1369 size_t mOverflowCount;
1372 NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter)
1373 NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver)
1375 ///////////////////////////////////////////////////////////////////////////////
1376 // Public API
1377 ///////////////////////////////////////////////////////////////////////////////
1379 /* static */ void
1380 SurfaceCache::Initialize()
1382 // Initialize preferences.
1383 MOZ_ASSERT(NS_IsMainThread());
1384 MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once");
1386 // See gfxPrefs for the default values of these preferences.
1388 // Length of time before an unused surface is removed from the cache, in
1389 // milliseconds.
1390 uint32_t surfaceCacheExpirationTimeMS =
1391 gfxPrefs::ImageMemSurfaceCacheMinExpirationMS();
1393 // What fraction of the memory used by the surface cache we should discard
1394 // when we get a memory pressure notification. This value is interpreted as
1395 // 1/N, so 1 means to discard everything, 2 means to discard about half of the
1396 // memory we're using, and so forth. We clamp it to avoid division by zero.
1397 uint32_t surfaceCacheDiscardFactor =
1398 max(gfxPrefs::ImageMemSurfaceCacheDiscardFactor(), 1u);
1400 // Maximum size of the surface cache, in kilobytes.
1401 uint64_t surfaceCacheMaxSizeKB = gfxPrefs::ImageMemSurfaceCacheMaxSizeKB();
1403 // A knob determining the actual size of the surface cache. Currently the
1404 // cache is (size of main memory) / (surface cache size factor) KB
1405 // or (surface cache max size) KB, whichever is smaller. The formula
1406 // may change in the future, though.
1407 // For example, a value of 4 would yield a 256MB cache on a 1GB machine.
1408 // The smallest machines we are likely to run this code on have 256MB
1409 // of memory, which would yield a 64MB cache on this setting.
1410 // We clamp this value to avoid division by zero.
1411 uint32_t surfaceCacheSizeFactor =
1412 max(gfxPrefs::ImageMemSurfaceCacheSizeFactor(), 1u);
1414 // Compute the size of the surface cache.
1415 uint64_t memorySize = PR_GetPhysicalMemorySize();
1416 if (memorySize == 0) {
1417 MOZ_ASSERT_UNREACHABLE("PR_GetPhysicalMemorySize not implemented here");
1418 memorySize = 256 * 1024 * 1024; // Fall back to 256MB.
1420 uint64_t proposedSize = memorySize / surfaceCacheSizeFactor;
1421 uint64_t surfaceCacheSizeBytes = min(proposedSize,
1422 surfaceCacheMaxSizeKB * 1024);
1423 uint32_t finalSurfaceCacheSizeBytes =
1424 min(surfaceCacheSizeBytes, uint64_t(UINT32_MAX));
1426 // Create the surface cache singleton with the requested settings. Note that
1427 // the size is a limit that the cache may not grow beyond, but we do not
1428 // actually allocate any storage for surfaces at this time.
1429 sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS,
1430 surfaceCacheDiscardFactor,
1431 finalSurfaceCacheSizeBytes);
1432 sInstance->InitMemoryReporter();
1435 /* static */ void
1436 SurfaceCache::Shutdown()
1438 RefPtr<SurfaceCacheImpl> cache;
1440 StaticMutexAutoLock lock(sInstanceMutex);
1441 MOZ_ASSERT(NS_IsMainThread());
1442 MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?");
1443 cache = sInstance.forget();
1447 /* static */ LookupResult
1448 SurfaceCache::Lookup(const ImageKey aImageKey,
1449 const SurfaceKey& aSurfaceKey)
1451 nsTArray<RefPtr<CachedSurface>> discard;
1452 LookupResult rv(MatchType::NOT_FOUND);
1455 StaticMutexAutoLock lock(sInstanceMutex);
1456 if (!sInstance) {
1457 return rv;
1460 rv = sInstance->Lookup(aImageKey, aSurfaceKey, lock);
1461 sInstance->TakeDiscard(discard, lock);
1464 return rv;
1467 /* static */ LookupResult
1468 SurfaceCache::LookupBestMatch(const ImageKey aImageKey,
1469 const SurfaceKey& aSurfaceKey)
1471 nsTArray<RefPtr<CachedSurface>> discard;
1472 LookupResult rv(MatchType::NOT_FOUND);
1475 StaticMutexAutoLock lock(sInstanceMutex);
1476 if (!sInstance) {
1477 return rv;
1480 rv = sInstance->LookupBestMatch(aImageKey, aSurfaceKey, lock);
1481 sInstance->TakeDiscard(discard, lock);
1484 return rv;
1487 /* static */ InsertOutcome
1488 SurfaceCache::Insert(NotNull<ISurfaceProvider*> aProvider)
1490 nsTArray<RefPtr<CachedSurface>> discard;
1491 InsertOutcome rv(InsertOutcome::FAILURE);
1494 StaticMutexAutoLock lock(sInstanceMutex);
1495 if (!sInstance) {
1496 return rv;
1499 rv = sInstance->Insert(aProvider, /* aSetAvailable = */ false, lock);
1500 sInstance->TakeDiscard(discard, lock);
1503 return rv;
1506 /* static */ bool
1507 SurfaceCache::CanHold(const IntSize& aSize, uint32_t aBytesPerPixel /* = 4 */)
1509 StaticMutexAutoLock lock(sInstanceMutex);
1510 if (!sInstance) {
1511 return false;
1514 Cost cost = ComputeCost(aSize, aBytesPerPixel);
1515 return sInstance->CanHold(cost);
1518 /* static */ bool
1519 SurfaceCache::CanHold(size_t aSize)
1521 StaticMutexAutoLock lock(sInstanceMutex);
1522 if (!sInstance) {
1523 return false;
1526 return sInstance->CanHold(aSize);
1529 /* static */ void
1530 SurfaceCache::SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider)
1532 StaticMutexAutoLock lock(sInstanceMutex);
1533 if (!sInstance) {
1534 return;
1537 sInstance->SurfaceAvailable(aProvider, lock);
1540 /* static */ void
1541 SurfaceCache::LockImage(const ImageKey aImageKey)
1543 StaticMutexAutoLock lock(sInstanceMutex);
1544 if (sInstance) {
1545 return sInstance->LockImage(aImageKey);
1549 /* static */ void
1550 SurfaceCache::UnlockImage(const ImageKey aImageKey)
1552 StaticMutexAutoLock lock(sInstanceMutex);
1553 if (sInstance) {
1554 return sInstance->UnlockImage(aImageKey, lock);
1558 /* static */ void
1559 SurfaceCache::UnlockEntries(const ImageKey aImageKey)
1561 StaticMutexAutoLock lock(sInstanceMutex);
1562 if (sInstance) {
1563 return sInstance->UnlockEntries(aImageKey, lock);
1567 /* static */ void
1568 SurfaceCache::RemoveImage(const ImageKey aImageKey)
1570 RefPtr<ImageSurfaceCache> discard;
1572 StaticMutexAutoLock lock(sInstanceMutex);
1573 if (sInstance) {
1574 discard = sInstance->RemoveImage(aImageKey, lock);
1579 /* static */ void
1580 SurfaceCache::PruneImage(const ImageKey aImageKey)
1582 nsTArray<RefPtr<CachedSurface>> discard;
1584 StaticMutexAutoLock lock(sInstanceMutex);
1585 if (sInstance) {
1586 sInstance->PruneImage(aImageKey, lock);
1587 sInstance->TakeDiscard(discard, lock);
1592 /* static */ void
1593 SurfaceCache::DiscardAll()
1595 nsTArray<RefPtr<CachedSurface>> discard;
1597 StaticMutexAutoLock lock(sInstanceMutex);
1598 if (sInstance) {
1599 sInstance->DiscardAll(lock);
1600 sInstance->TakeDiscard(discard, lock);
1605 /* static */ void
1606 SurfaceCache::CollectSizeOfSurfaces(const ImageKey aImageKey,
1607 nsTArray<SurfaceMemoryCounter>& aCounters,
1608 MallocSizeOf aMallocSizeOf)
1610 nsTArray<RefPtr<CachedSurface>> discard;
1612 StaticMutexAutoLock lock(sInstanceMutex);
1613 if (!sInstance) {
1614 return;
1617 sInstance->CollectSizeOfSurfaces(aImageKey, aCounters, aMallocSizeOf, lock);
1618 sInstance->TakeDiscard(discard, lock);
1622 /* static */ size_t
1623 SurfaceCache::MaximumCapacity()
1625 StaticMutexAutoLock lock(sInstanceMutex);
1626 if (!sInstance) {
1627 return 0;
1630 return sInstance->MaximumCapacity();
1633 } // namespace image
1634 } // namespace mozilla