michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * SurfaceCache is a service for caching temporary surfaces in imagelib. michael@0: */ michael@0: michael@0: #include "SurfaceCache.h" michael@0: michael@0: #include michael@0: #include "mozilla/Attributes.h" // for MOZ_THIS_IN_INITIALIZER_LIST michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "mozilla/StaticPtr.h" michael@0: #include "nsIMemoryReporter.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "gfxASurface.h" michael@0: #include "gfxPattern.h" // Workaround for flaw in bug 921753 part 2. michael@0: #include "gfxDrawable.h" michael@0: #include "gfxPlatform.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsExpirationTracker.h" michael@0: #include "nsHashKeys.h" michael@0: #include "nsRefPtrHashtable.h" michael@0: #include "nsSize.h" michael@0: #include "nsTArray.h" michael@0: #include "prsystem.h" michael@0: #include "SVGImageContext.h" michael@0: michael@0: using std::max; michael@0: using std::min; michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: class CachedSurface; michael@0: class SurfaceCacheImpl; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Static Data michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // The single surface cache instance. michael@0: static StaticRefPtr sInstance; michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // SurfaceCache Implementation michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* michael@0: * Cost models the cost of storing a surface in the cache. Right now, this is michael@0: * simply an estimate of the size of the surface in bytes, but in the future it michael@0: * may be worth taking into account the cost of rematerializing the surface as michael@0: * well. michael@0: */ michael@0: typedef size_t Cost; michael@0: michael@0: static Cost ComputeCost(const IntSize& aSize) michael@0: { michael@0: return aSize.width * aSize.height * 4; // width * height * 4 bytes (32bpp) michael@0: } michael@0: michael@0: /* michael@0: * Since we want to be able to make eviction decisions based on cost, we need to michael@0: * be able to look up the CachedSurface which has a certain cost as well as the michael@0: * cost associated with a certain CachedSurface. To make this possible, in data michael@0: * structures we actually store a CostEntry, which contains a weak pointer to michael@0: * its associated surface. michael@0: * michael@0: * To make usage of the weak pointer safe, SurfaceCacheImpl always calls michael@0: * StartTracking after a surface is stored in the cache and StopTracking before michael@0: * it is removed. michael@0: */ michael@0: class CostEntry michael@0: { michael@0: public: michael@0: CostEntry(CachedSurface* aSurface, Cost aCost) michael@0: : mSurface(aSurface) michael@0: , mCost(aCost) michael@0: { michael@0: MOZ_ASSERT(aSurface, "Must have a surface"); michael@0: } michael@0: michael@0: CachedSurface* GetSurface() const { return mSurface; } michael@0: Cost GetCost() const { return mCost; } michael@0: michael@0: bool operator==(const CostEntry& aOther) const michael@0: { michael@0: return mSurface == aOther.mSurface && michael@0: mCost == aOther.mCost; michael@0: } michael@0: michael@0: bool operator<(const CostEntry& aOther) const michael@0: { michael@0: return mCost < aOther.mCost || michael@0: (mCost == aOther.mCost && mSurface < aOther.mSurface); michael@0: } michael@0: michael@0: private: michael@0: CachedSurface* mSurface; michael@0: Cost mCost; michael@0: }; michael@0: michael@0: /* michael@0: * A CachedSurface associates a surface with a key that uniquely identifies that michael@0: * surface. michael@0: */ michael@0: class CachedSurface michael@0: { michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(CachedSurface) michael@0: michael@0: CachedSurface(DrawTarget* aTarget, michael@0: const IntSize aTargetSize, michael@0: const Cost aCost, michael@0: const ImageKey aImageKey, michael@0: const SurfaceKey& aSurfaceKey) michael@0: : mTarget(aTarget) michael@0: , mTargetSize(aTargetSize) michael@0: , mCost(aCost) michael@0: , mImageKey(aImageKey) michael@0: , mSurfaceKey(aSurfaceKey) michael@0: { michael@0: MOZ_ASSERT(mTarget, "Must have a valid DrawTarget"); michael@0: MOZ_ASSERT(mImageKey, "Must have a valid image key"); michael@0: } michael@0: michael@0: already_AddRefed Drawable() const michael@0: { michael@0: nsRefPtr drawable = michael@0: new gfxSurfaceDrawable(mTarget, ThebesIntSize(mTargetSize)); michael@0: return drawable.forget(); michael@0: } michael@0: michael@0: ImageKey GetImageKey() const { return mImageKey; } michael@0: SurfaceKey GetSurfaceKey() const { return mSurfaceKey; } michael@0: CostEntry GetCostEntry() { return image::CostEntry(this, mCost); } michael@0: nsExpirationState* GetExpirationState() { return &mExpirationState; } michael@0: michael@0: private: michael@0: nsExpirationState mExpirationState; michael@0: nsRefPtr mTarget; michael@0: const IntSize mTargetSize; michael@0: const Cost mCost; michael@0: const ImageKey mImageKey; michael@0: const SurfaceKey mSurfaceKey; michael@0: }; michael@0: michael@0: /* michael@0: * An ImageSurfaceCache is a per-image surface cache. For correctness we must be michael@0: * able to remove all surfaces associated with an image when the image is michael@0: * destroyed or invalidated. Since this will happen frequently, it makes sense michael@0: * to make it cheap by storing the surfaces for each image separately. michael@0: */ michael@0: class ImageSurfaceCache michael@0: { michael@0: public: michael@0: NS_INLINE_DECL_REFCOUNTING(ImageSurfaceCache) michael@0: michael@0: typedef nsRefPtrHashtable, CachedSurface> SurfaceTable; michael@0: michael@0: bool IsEmpty() const { return mSurfaces.Count() == 0; } michael@0: michael@0: void Insert(const SurfaceKey& aKey, CachedSurface* aSurface) michael@0: { michael@0: MOZ_ASSERT(aSurface, "Should have a surface"); michael@0: mSurfaces.Put(aKey, aSurface); michael@0: } michael@0: michael@0: void Remove(CachedSurface* aSurface) michael@0: { michael@0: MOZ_ASSERT(aSurface, "Should have a surface"); michael@0: MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()), michael@0: "Should not be removing a surface we don't have"); michael@0: michael@0: mSurfaces.Remove(aSurface->GetSurfaceKey()); michael@0: } michael@0: michael@0: already_AddRefed Lookup(const SurfaceKey& aSurfaceKey) michael@0: { michael@0: nsRefPtr surface; michael@0: mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface)); michael@0: return surface.forget(); michael@0: } michael@0: michael@0: void ForEach(SurfaceTable::EnumReadFunction aFunction, void* aData) michael@0: { michael@0: mSurfaces.EnumerateRead(aFunction, aData); michael@0: } michael@0: michael@0: private: michael@0: SurfaceTable mSurfaces; michael@0: }; michael@0: michael@0: /* michael@0: * SurfaceCacheImpl is responsible for determining which surfaces will be cached michael@0: * and managing the surface cache data structures. Rather than interact with michael@0: * SurfaceCacheImpl directly, client code interacts with SurfaceCache, which michael@0: * maintains high-level invariants and encapsulates the details of the surface michael@0: * cache's implementation. michael@0: */ michael@0: class SurfaceCacheImpl : public nsIMemoryReporter michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS, michael@0: uint32_t aSurfaceCacheSize) michael@0: : mExpirationTracker(MOZ_THIS_IN_INITIALIZER_LIST(), michael@0: aSurfaceCacheExpirationTimeMS) michael@0: , mMemoryPressureObserver(new MemoryPressureObserver) michael@0: , mMaxCost(aSurfaceCacheSize) michael@0: , mAvailableCost(aSurfaceCacheSize) michael@0: { michael@0: nsCOMPtr os = mozilla::services::GetObserverService(); michael@0: if (os) michael@0: os->AddObserver(mMemoryPressureObserver, "memory-pressure", false); michael@0: } michael@0: michael@0: virtual ~SurfaceCacheImpl() michael@0: { michael@0: nsCOMPtr os = mozilla::services::GetObserverService(); michael@0: if (os) michael@0: os->RemoveObserver(mMemoryPressureObserver, "memory-pressure"); michael@0: michael@0: UnregisterWeakMemoryReporter(this); michael@0: } michael@0: michael@0: void InitMemoryReporter() { michael@0: RegisterWeakMemoryReporter(this); michael@0: } michael@0: michael@0: void Insert(DrawTarget* aTarget, michael@0: IntSize aTargetSize, michael@0: const Cost aCost, michael@0: const ImageKey aImageKey, michael@0: const SurfaceKey& aSurfaceKey) michael@0: { michael@0: MOZ_ASSERT(!Lookup(aImageKey, aSurfaceKey).take(), michael@0: "Inserting a duplicate drawable into the SurfaceCache"); michael@0: michael@0: // If this is bigger than the maximum cache size, refuse to cache it. michael@0: if (!CanHold(aCost)) michael@0: return; michael@0: michael@0: nsRefPtr surface = michael@0: new CachedSurface(aTarget, aTargetSize, aCost, aImageKey, aSurfaceKey); michael@0: michael@0: // Remove elements in order of cost until we can fit this in the cache. michael@0: while (aCost > mAvailableCost) { michael@0: MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and it still won't fit"); michael@0: Remove(mCosts.LastElement().GetSurface()); michael@0: } michael@0: michael@0: // Locate the appropriate per-image cache. If there's not an existing cache michael@0: // for this image, create it. michael@0: nsRefPtr cache = GetImageCache(aImageKey); michael@0: if (!cache) { michael@0: cache = new ImageSurfaceCache; michael@0: mImageCaches.Put(aImageKey, cache); michael@0: } michael@0: michael@0: // Insert. michael@0: MOZ_ASSERT(aCost <= mAvailableCost, "Inserting despite too large a cost"); michael@0: cache->Insert(aSurfaceKey, surface); michael@0: StartTracking(surface); michael@0: } michael@0: michael@0: void Remove(CachedSurface* aSurface) michael@0: { michael@0: MOZ_ASSERT(aSurface, "Should have a surface"); michael@0: const ImageKey imageKey = aSurface->GetImageKey(); michael@0: michael@0: nsRefPtr cache = GetImageCache(imageKey); michael@0: MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache"); michael@0: michael@0: StopTracking(aSurface); michael@0: cache->Remove(aSurface); michael@0: michael@0: // Remove the per-image cache if it's unneeded now. michael@0: if (cache->IsEmpty()) { michael@0: mImageCaches.Remove(imageKey); michael@0: } michael@0: } michael@0: michael@0: void StartTracking(CachedSurface* aSurface) michael@0: { michael@0: CostEntry costEntry = aSurface->GetCostEntry(); michael@0: MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost, michael@0: "Cost too large and the caller didn't catch it"); michael@0: michael@0: mAvailableCost -= costEntry.GetCost(); michael@0: mCosts.InsertElementSorted(costEntry); michael@0: mExpirationTracker.AddObject(aSurface); michael@0: } michael@0: michael@0: void StopTracking(CachedSurface* aSurface) michael@0: { michael@0: MOZ_ASSERT(aSurface, "Should have a surface"); michael@0: CostEntry costEntry = aSurface->GetCostEntry(); michael@0: michael@0: mExpirationTracker.RemoveObject(aSurface); michael@0: DebugOnly foundInCosts = mCosts.RemoveElementSorted(costEntry); michael@0: mAvailableCost += costEntry.GetCost(); michael@0: michael@0: MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface"); michael@0: MOZ_ASSERT(mAvailableCost <= mMaxCost, "More available cost than we started with"); michael@0: } michael@0: michael@0: already_AddRefed Lookup(const ImageKey aImageKey, michael@0: const SurfaceKey& aSurfaceKey) michael@0: { michael@0: nsRefPtr cache = GetImageCache(aImageKey); michael@0: if (!cache) michael@0: return nullptr; // No cached surfaces for this image. michael@0: michael@0: nsRefPtr surface = cache->Lookup(aSurfaceKey); michael@0: if (!surface) michael@0: return nullptr; // Lookup in the per-image cache missed. michael@0: michael@0: mExpirationTracker.MarkUsed(surface); michael@0: return surface->Drawable(); michael@0: } michael@0: michael@0: bool CanHold(const Cost aCost) const michael@0: { michael@0: return aCost <= mMaxCost; michael@0: } michael@0: michael@0: void Discard(const ImageKey aImageKey) michael@0: { michael@0: nsRefPtr cache = GetImageCache(aImageKey); michael@0: if (!cache) michael@0: return; // No cached surfaces for this image, so nothing to do. michael@0: michael@0: // Discard all of the cached surfaces for this image. michael@0: // XXX(seth): This is O(n^2) since for each item in the cache we are michael@0: // removing an element from the costs array. Since n is expected to be michael@0: // small, performance should be good, but if usage patterns change we should michael@0: // change the data structure used for mCosts. michael@0: cache->ForEach(DoStopTracking, this); michael@0: michael@0: // The per-image cache isn't needed anymore, so remove it as well. michael@0: mImageCaches.Remove(aImageKey); michael@0: } michael@0: michael@0: void DiscardAll() michael@0: { michael@0: // Remove in order of cost because mCosts is an array and the other data michael@0: // structures are all hash tables. michael@0: while (!mCosts.IsEmpty()) { michael@0: Remove(mCosts.LastElement().GetSurface()); michael@0: } michael@0: } michael@0: michael@0: static PLDHashOperator DoStopTracking(const SurfaceKey&, michael@0: CachedSurface* aSurface, michael@0: void* aCache) michael@0: { michael@0: static_cast(aCache)->StopTracking(aSurface); michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: NS_IMETHOD michael@0: CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) michael@0: { michael@0: return MOZ_COLLECT_REPORT( michael@0: "imagelib-surface-cache", KIND_OTHER, UNITS_BYTES, michael@0: SizeOfSurfacesEstimate(), michael@0: "Memory used by the imagelib temporary surface cache."); michael@0: } michael@0: michael@0: // XXX(seth): This is currently only an estimate and, since we don't know michael@0: // which surfaces are in GPU memory and which aren't, it's reported as michael@0: // KIND_OTHER and will also show up in heap-unclassified. Bug 923302 will michael@0: // make this nicer. michael@0: Cost SizeOfSurfacesEstimate() const michael@0: { michael@0: return mMaxCost - mAvailableCost; michael@0: } michael@0: michael@0: private: michael@0: already_AddRefed GetImageCache(const ImageKey aImageKey) michael@0: { michael@0: nsRefPtr imageCache; michael@0: mImageCaches.Get(aImageKey, getter_AddRefs(imageCache)); michael@0: return imageCache.forget(); michael@0: } michael@0: michael@0: struct SurfaceTracker : public nsExpirationTracker michael@0: { michael@0: SurfaceTracker(SurfaceCacheImpl* aCache, uint32_t aSurfaceCacheExpirationTimeMS) michael@0: : nsExpirationTracker(aSurfaceCacheExpirationTimeMS) michael@0: , mCache(aCache) michael@0: { } michael@0: michael@0: protected: michael@0: virtual void NotifyExpired(CachedSurface* aSurface) MOZ_OVERRIDE michael@0: { michael@0: if (mCache) { michael@0: mCache->Remove(aSurface); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: SurfaceCacheImpl* const mCache; // Weak pointer to owner. michael@0: }; michael@0: michael@0: struct MemoryPressureObserver : public nsIObserver michael@0: { michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: virtual ~MemoryPressureObserver() { } michael@0: michael@0: NS_IMETHOD Observe(nsISupports*, const char* aTopic, const char16_t*) michael@0: { michael@0: if (sInstance && strcmp(aTopic, "memory-pressure") == 0) { michael@0: sInstance->DiscardAll(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: michael@0: nsTArray mCosts; michael@0: nsRefPtrHashtable, ImageSurfaceCache> mImageCaches; michael@0: SurfaceTracker mExpirationTracker; michael@0: nsRefPtr mMemoryPressureObserver; michael@0: const Cost mMaxCost; michael@0: Cost mAvailableCost; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter) michael@0: NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver) michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Public API michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* static */ void michael@0: SurfaceCache::Initialize() michael@0: { michael@0: // Initialize preferences. michael@0: MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once"); michael@0: michael@0: // Length of time before an unused surface is removed from the cache, in milliseconds. michael@0: // The default value gives an expiration time of 1 minute. michael@0: uint32_t surfaceCacheExpirationTimeMS = michael@0: Preferences::GetUint("image.mem.surfacecache.min_expiration_ms", 60 * 1000); michael@0: michael@0: // Maximum size of the surface cache, in kilobytes. michael@0: // The default is 100MB. (But we may override this for e.g. B2G.) michael@0: uint32_t surfaceCacheMaxSizeKB = michael@0: Preferences::GetUint("image.mem.surfacecache.max_size_kb", 100 * 1024); michael@0: michael@0: // A knob determining the actual size of the surface cache. Currently the michael@0: // cache is (size of main memory) / (surface cache size factor) KB michael@0: // or (surface cache max size) KB, whichever is smaller. The formula michael@0: // may change in the future, though. michael@0: // The default value is 64, which yields a 64MB cache on a 4GB machine. michael@0: // The smallest machines we are likely to run this code on have 256MB michael@0: // of memory, which would yield a 4MB cache on the default setting. michael@0: uint32_t surfaceCacheSizeFactor = michael@0: Preferences::GetUint("image.mem.surfacecache.size_factor", 64); michael@0: michael@0: // Clamp to avoid division by zero below. michael@0: surfaceCacheSizeFactor = max(surfaceCacheSizeFactor, 1u); michael@0: michael@0: // Compute the size of the surface cache. michael@0: uint32_t proposedSize = PR_GetPhysicalMemorySize() / surfaceCacheSizeFactor; michael@0: uint32_t surfaceCacheSizeBytes = min(proposedSize, surfaceCacheMaxSizeKB * 1024); michael@0: michael@0: // Create the surface cache singleton with the requested expiration time and michael@0: // size. Note that the size is a limit that the cache may not grow beyond, but michael@0: // we do not actually allocate any storage for surfaces at this time. michael@0: sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS, michael@0: surfaceCacheSizeBytes); michael@0: sInstance->InitMemoryReporter(); michael@0: } michael@0: michael@0: /* static */ void michael@0: SurfaceCache::Shutdown() michael@0: { michael@0: MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?"); michael@0: sInstance = nullptr; michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: SurfaceCache::Lookup(const ImageKey aImageKey, michael@0: const SurfaceKey& aSurfaceKey) michael@0: { michael@0: MOZ_ASSERT(sInstance, "Should be initialized"); michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: return sInstance->Lookup(aImageKey, aSurfaceKey); michael@0: } michael@0: michael@0: /* static */ void michael@0: SurfaceCache::Insert(DrawTarget* aTarget, michael@0: const ImageKey aImageKey, michael@0: const SurfaceKey& aSurfaceKey) michael@0: { michael@0: MOZ_ASSERT(sInstance, "Should be initialized"); michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: Cost cost = ComputeCost(aSurfaceKey.Size()); michael@0: return sInstance->Insert(aTarget, aSurfaceKey.Size(), cost, aImageKey, michael@0: aSurfaceKey); michael@0: } michael@0: michael@0: /* static */ bool michael@0: SurfaceCache::CanHold(const IntSize& aSize) michael@0: { michael@0: MOZ_ASSERT(sInstance, "Should be initialized"); michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: Cost cost = ComputeCost(aSize); michael@0: return sInstance->CanHold(cost); michael@0: } michael@0: michael@0: /* static */ void michael@0: SurfaceCache::Discard(Image* aImageKey) michael@0: { michael@0: MOZ_ASSERT(sInstance, "Should be initialized"); michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: return sInstance->Discard(aImageKey); michael@0: } michael@0: michael@0: /* static */ void michael@0: SurfaceCache::DiscardAll() michael@0: { michael@0: MOZ_ASSERT(sInstance, "Should be initialized"); michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: return sInstance->DiscardAll(); michael@0: } michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla