Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * SurfaceCache is a service for caching temporary surfaces in imagelib. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "SurfaceCache.h" |
michael@0 | 11 | |
michael@0 | 12 | #include <algorithm> |
michael@0 | 13 | #include "mozilla/Attributes.h" // for MOZ_THIS_IN_INITIALIZER_LIST |
michael@0 | 14 | #include "mozilla/DebugOnly.h" |
michael@0 | 15 | #include "mozilla/Preferences.h" |
michael@0 | 16 | #include "mozilla/RefPtr.h" |
michael@0 | 17 | #include "mozilla/StaticPtr.h" |
michael@0 | 18 | #include "nsIMemoryReporter.h" |
michael@0 | 19 | #include "gfx2DGlue.h" |
michael@0 | 20 | #include "gfxASurface.h" |
michael@0 | 21 | #include "gfxPattern.h" // Workaround for flaw in bug 921753 part 2. |
michael@0 | 22 | #include "gfxDrawable.h" |
michael@0 | 23 | #include "gfxPlatform.h" |
michael@0 | 24 | #include "nsAutoPtr.h" |
michael@0 | 25 | #include "nsExpirationTracker.h" |
michael@0 | 26 | #include "nsHashKeys.h" |
michael@0 | 27 | #include "nsRefPtrHashtable.h" |
michael@0 | 28 | #include "nsSize.h" |
michael@0 | 29 | #include "nsTArray.h" |
michael@0 | 30 | #include "prsystem.h" |
michael@0 | 31 | #include "SVGImageContext.h" |
michael@0 | 32 | |
michael@0 | 33 | using std::max; |
michael@0 | 34 | using std::min; |
michael@0 | 35 | using namespace mozilla::gfx; |
michael@0 | 36 | |
michael@0 | 37 | namespace mozilla { |
michael@0 | 38 | namespace image { |
michael@0 | 39 | |
michael@0 | 40 | class CachedSurface; |
michael@0 | 41 | class SurfaceCacheImpl; |
michael@0 | 42 | |
michael@0 | 43 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 44 | // Static Data |
michael@0 | 45 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 46 | |
michael@0 | 47 | // The single surface cache instance. |
michael@0 | 48 | static StaticRefPtr<SurfaceCacheImpl> sInstance; |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 52 | // SurfaceCache Implementation |
michael@0 | 53 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 54 | |
michael@0 | 55 | /* |
michael@0 | 56 | * Cost models the cost of storing a surface in the cache. Right now, this is |
michael@0 | 57 | * simply an estimate of the size of the surface in bytes, but in the future it |
michael@0 | 58 | * may be worth taking into account the cost of rematerializing the surface as |
michael@0 | 59 | * well. |
michael@0 | 60 | */ |
michael@0 | 61 | typedef size_t Cost; |
michael@0 | 62 | |
michael@0 | 63 | static Cost ComputeCost(const IntSize& aSize) |
michael@0 | 64 | { |
michael@0 | 65 | return aSize.width * aSize.height * 4; // width * height * 4 bytes (32bpp) |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /* |
michael@0 | 69 | * Since we want to be able to make eviction decisions based on cost, we need to |
michael@0 | 70 | * be able to look up the CachedSurface which has a certain cost as well as the |
michael@0 | 71 | * cost associated with a certain CachedSurface. To make this possible, in data |
michael@0 | 72 | * structures we actually store a CostEntry, which contains a weak pointer to |
michael@0 | 73 | * its associated surface. |
michael@0 | 74 | * |
michael@0 | 75 | * To make usage of the weak pointer safe, SurfaceCacheImpl always calls |
michael@0 | 76 | * StartTracking after a surface is stored in the cache and StopTracking before |
michael@0 | 77 | * it is removed. |
michael@0 | 78 | */ |
michael@0 | 79 | class CostEntry |
michael@0 | 80 | { |
michael@0 | 81 | public: |
michael@0 | 82 | CostEntry(CachedSurface* aSurface, Cost aCost) |
michael@0 | 83 | : mSurface(aSurface) |
michael@0 | 84 | , mCost(aCost) |
michael@0 | 85 | { |
michael@0 | 86 | MOZ_ASSERT(aSurface, "Must have a surface"); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | CachedSurface* GetSurface() const { return mSurface; } |
michael@0 | 90 | Cost GetCost() const { return mCost; } |
michael@0 | 91 | |
michael@0 | 92 | bool operator==(const CostEntry& aOther) const |
michael@0 | 93 | { |
michael@0 | 94 | return mSurface == aOther.mSurface && |
michael@0 | 95 | mCost == aOther.mCost; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | bool operator<(const CostEntry& aOther) const |
michael@0 | 99 | { |
michael@0 | 100 | return mCost < aOther.mCost || |
michael@0 | 101 | (mCost == aOther.mCost && mSurface < aOther.mSurface); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | private: |
michael@0 | 105 | CachedSurface* mSurface; |
michael@0 | 106 | Cost mCost; |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | /* |
michael@0 | 110 | * A CachedSurface associates a surface with a key that uniquely identifies that |
michael@0 | 111 | * surface. |
michael@0 | 112 | */ |
michael@0 | 113 | class CachedSurface |
michael@0 | 114 | { |
michael@0 | 115 | public: |
michael@0 | 116 | NS_INLINE_DECL_REFCOUNTING(CachedSurface) |
michael@0 | 117 | |
michael@0 | 118 | CachedSurface(DrawTarget* aTarget, |
michael@0 | 119 | const IntSize aTargetSize, |
michael@0 | 120 | const Cost aCost, |
michael@0 | 121 | const ImageKey aImageKey, |
michael@0 | 122 | const SurfaceKey& aSurfaceKey) |
michael@0 | 123 | : mTarget(aTarget) |
michael@0 | 124 | , mTargetSize(aTargetSize) |
michael@0 | 125 | , mCost(aCost) |
michael@0 | 126 | , mImageKey(aImageKey) |
michael@0 | 127 | , mSurfaceKey(aSurfaceKey) |
michael@0 | 128 | { |
michael@0 | 129 | MOZ_ASSERT(mTarget, "Must have a valid DrawTarget"); |
michael@0 | 130 | MOZ_ASSERT(mImageKey, "Must have a valid image key"); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | already_AddRefed<gfxDrawable> Drawable() const |
michael@0 | 134 | { |
michael@0 | 135 | nsRefPtr<gfxDrawable> drawable = |
michael@0 | 136 | new gfxSurfaceDrawable(mTarget, ThebesIntSize(mTargetSize)); |
michael@0 | 137 | return drawable.forget(); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | ImageKey GetImageKey() const { return mImageKey; } |
michael@0 | 141 | SurfaceKey GetSurfaceKey() const { return mSurfaceKey; } |
michael@0 | 142 | CostEntry GetCostEntry() { return image::CostEntry(this, mCost); } |
michael@0 | 143 | nsExpirationState* GetExpirationState() { return &mExpirationState; } |
michael@0 | 144 | |
michael@0 | 145 | private: |
michael@0 | 146 | nsExpirationState mExpirationState; |
michael@0 | 147 | nsRefPtr<DrawTarget> mTarget; |
michael@0 | 148 | const IntSize mTargetSize; |
michael@0 | 149 | const Cost mCost; |
michael@0 | 150 | const ImageKey mImageKey; |
michael@0 | 151 | const SurfaceKey mSurfaceKey; |
michael@0 | 152 | }; |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | * An ImageSurfaceCache is a per-image surface cache. For correctness we must be |
michael@0 | 156 | * able to remove all surfaces associated with an image when the image is |
michael@0 | 157 | * destroyed or invalidated. Since this will happen frequently, it makes sense |
michael@0 | 158 | * to make it cheap by storing the surfaces for each image separately. |
michael@0 | 159 | */ |
michael@0 | 160 | class ImageSurfaceCache |
michael@0 | 161 | { |
michael@0 | 162 | public: |
michael@0 | 163 | NS_INLINE_DECL_REFCOUNTING(ImageSurfaceCache) |
michael@0 | 164 | |
michael@0 | 165 | typedef nsRefPtrHashtable<nsGenericHashKey<SurfaceKey>, CachedSurface> SurfaceTable; |
michael@0 | 166 | |
michael@0 | 167 | bool IsEmpty() const { return mSurfaces.Count() == 0; } |
michael@0 | 168 | |
michael@0 | 169 | void Insert(const SurfaceKey& aKey, CachedSurface* aSurface) |
michael@0 | 170 | { |
michael@0 | 171 | MOZ_ASSERT(aSurface, "Should have a surface"); |
michael@0 | 172 | mSurfaces.Put(aKey, aSurface); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | void Remove(CachedSurface* aSurface) |
michael@0 | 176 | { |
michael@0 | 177 | MOZ_ASSERT(aSurface, "Should have a surface"); |
michael@0 | 178 | MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()), |
michael@0 | 179 | "Should not be removing a surface we don't have"); |
michael@0 | 180 | |
michael@0 | 181 | mSurfaces.Remove(aSurface->GetSurfaceKey()); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | already_AddRefed<CachedSurface> Lookup(const SurfaceKey& aSurfaceKey) |
michael@0 | 185 | { |
michael@0 | 186 | nsRefPtr<CachedSurface> surface; |
michael@0 | 187 | mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface)); |
michael@0 | 188 | return surface.forget(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | void ForEach(SurfaceTable::EnumReadFunction aFunction, void* aData) |
michael@0 | 192 | { |
michael@0 | 193 | mSurfaces.EnumerateRead(aFunction, aData); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | private: |
michael@0 | 197 | SurfaceTable mSurfaces; |
michael@0 | 198 | }; |
michael@0 | 199 | |
michael@0 | 200 | /* |
michael@0 | 201 | * SurfaceCacheImpl is responsible for determining which surfaces will be cached |
michael@0 | 202 | * and managing the surface cache data structures. Rather than interact with |
michael@0 | 203 | * SurfaceCacheImpl directly, client code interacts with SurfaceCache, which |
michael@0 | 204 | * maintains high-level invariants and encapsulates the details of the surface |
michael@0 | 205 | * cache's implementation. |
michael@0 | 206 | */ |
michael@0 | 207 | class SurfaceCacheImpl : public nsIMemoryReporter |
michael@0 | 208 | { |
michael@0 | 209 | public: |
michael@0 | 210 | NS_DECL_ISUPPORTS |
michael@0 | 211 | |
michael@0 | 212 | SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS, |
michael@0 | 213 | uint32_t aSurfaceCacheSize) |
michael@0 | 214 | : mExpirationTracker(MOZ_THIS_IN_INITIALIZER_LIST(), |
michael@0 | 215 | aSurfaceCacheExpirationTimeMS) |
michael@0 | 216 | , mMemoryPressureObserver(new MemoryPressureObserver) |
michael@0 | 217 | , mMaxCost(aSurfaceCacheSize) |
michael@0 | 218 | , mAvailableCost(aSurfaceCacheSize) |
michael@0 | 219 | { |
michael@0 | 220 | nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); |
michael@0 | 221 | if (os) |
michael@0 | 222 | os->AddObserver(mMemoryPressureObserver, "memory-pressure", false); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | virtual ~SurfaceCacheImpl() |
michael@0 | 226 | { |
michael@0 | 227 | nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); |
michael@0 | 228 | if (os) |
michael@0 | 229 | os->RemoveObserver(mMemoryPressureObserver, "memory-pressure"); |
michael@0 | 230 | |
michael@0 | 231 | UnregisterWeakMemoryReporter(this); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | void InitMemoryReporter() { |
michael@0 | 235 | RegisterWeakMemoryReporter(this); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | void Insert(DrawTarget* aTarget, |
michael@0 | 239 | IntSize aTargetSize, |
michael@0 | 240 | const Cost aCost, |
michael@0 | 241 | const ImageKey aImageKey, |
michael@0 | 242 | const SurfaceKey& aSurfaceKey) |
michael@0 | 243 | { |
michael@0 | 244 | MOZ_ASSERT(!Lookup(aImageKey, aSurfaceKey).take(), |
michael@0 | 245 | "Inserting a duplicate drawable into the SurfaceCache"); |
michael@0 | 246 | |
michael@0 | 247 | // If this is bigger than the maximum cache size, refuse to cache it. |
michael@0 | 248 | if (!CanHold(aCost)) |
michael@0 | 249 | return; |
michael@0 | 250 | |
michael@0 | 251 | nsRefPtr<CachedSurface> surface = |
michael@0 | 252 | new CachedSurface(aTarget, aTargetSize, aCost, aImageKey, aSurfaceKey); |
michael@0 | 253 | |
michael@0 | 254 | // Remove elements in order of cost until we can fit this in the cache. |
michael@0 | 255 | while (aCost > mAvailableCost) { |
michael@0 | 256 | MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and it still won't fit"); |
michael@0 | 257 | Remove(mCosts.LastElement().GetSurface()); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // Locate the appropriate per-image cache. If there's not an existing cache |
michael@0 | 261 | // for this image, create it. |
michael@0 | 262 | nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey); |
michael@0 | 263 | if (!cache) { |
michael@0 | 264 | cache = new ImageSurfaceCache; |
michael@0 | 265 | mImageCaches.Put(aImageKey, cache); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // Insert. |
michael@0 | 269 | MOZ_ASSERT(aCost <= mAvailableCost, "Inserting despite too large a cost"); |
michael@0 | 270 | cache->Insert(aSurfaceKey, surface); |
michael@0 | 271 | StartTracking(surface); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | void Remove(CachedSurface* aSurface) |
michael@0 | 275 | { |
michael@0 | 276 | MOZ_ASSERT(aSurface, "Should have a surface"); |
michael@0 | 277 | const ImageKey imageKey = aSurface->GetImageKey(); |
michael@0 | 278 | |
michael@0 | 279 | nsRefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey); |
michael@0 | 280 | MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache"); |
michael@0 | 281 | |
michael@0 | 282 | StopTracking(aSurface); |
michael@0 | 283 | cache->Remove(aSurface); |
michael@0 | 284 | |
michael@0 | 285 | // Remove the per-image cache if it's unneeded now. |
michael@0 | 286 | if (cache->IsEmpty()) { |
michael@0 | 287 | mImageCaches.Remove(imageKey); |
michael@0 | 288 | } |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | void StartTracking(CachedSurface* aSurface) |
michael@0 | 292 | { |
michael@0 | 293 | CostEntry costEntry = aSurface->GetCostEntry(); |
michael@0 | 294 | MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost, |
michael@0 | 295 | "Cost too large and the caller didn't catch it"); |
michael@0 | 296 | |
michael@0 | 297 | mAvailableCost -= costEntry.GetCost(); |
michael@0 | 298 | mCosts.InsertElementSorted(costEntry); |
michael@0 | 299 | mExpirationTracker.AddObject(aSurface); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | void StopTracking(CachedSurface* aSurface) |
michael@0 | 303 | { |
michael@0 | 304 | MOZ_ASSERT(aSurface, "Should have a surface"); |
michael@0 | 305 | CostEntry costEntry = aSurface->GetCostEntry(); |
michael@0 | 306 | |
michael@0 | 307 | mExpirationTracker.RemoveObject(aSurface); |
michael@0 | 308 | DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry); |
michael@0 | 309 | mAvailableCost += costEntry.GetCost(); |
michael@0 | 310 | |
michael@0 | 311 | MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface"); |
michael@0 | 312 | MOZ_ASSERT(mAvailableCost <= mMaxCost, "More available cost than we started with"); |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | already_AddRefed<gfxDrawable> Lookup(const ImageKey aImageKey, |
michael@0 | 316 | const SurfaceKey& aSurfaceKey) |
michael@0 | 317 | { |
michael@0 | 318 | nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey); |
michael@0 | 319 | if (!cache) |
michael@0 | 320 | return nullptr; // No cached surfaces for this image. |
michael@0 | 321 | |
michael@0 | 322 | nsRefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey); |
michael@0 | 323 | if (!surface) |
michael@0 | 324 | return nullptr; // Lookup in the per-image cache missed. |
michael@0 | 325 | |
michael@0 | 326 | mExpirationTracker.MarkUsed(surface); |
michael@0 | 327 | return surface->Drawable(); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | bool CanHold(const Cost aCost) const |
michael@0 | 331 | { |
michael@0 | 332 | return aCost <= mMaxCost; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | void Discard(const ImageKey aImageKey) |
michael@0 | 336 | { |
michael@0 | 337 | nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey); |
michael@0 | 338 | if (!cache) |
michael@0 | 339 | return; // No cached surfaces for this image, so nothing to do. |
michael@0 | 340 | |
michael@0 | 341 | // Discard all of the cached surfaces for this image. |
michael@0 | 342 | // XXX(seth): This is O(n^2) since for each item in the cache we are |
michael@0 | 343 | // removing an element from the costs array. Since n is expected to be |
michael@0 | 344 | // small, performance should be good, but if usage patterns change we should |
michael@0 | 345 | // change the data structure used for mCosts. |
michael@0 | 346 | cache->ForEach(DoStopTracking, this); |
michael@0 | 347 | |
michael@0 | 348 | // The per-image cache isn't needed anymore, so remove it as well. |
michael@0 | 349 | mImageCaches.Remove(aImageKey); |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | void DiscardAll() |
michael@0 | 353 | { |
michael@0 | 354 | // Remove in order of cost because mCosts is an array and the other data |
michael@0 | 355 | // structures are all hash tables. |
michael@0 | 356 | while (!mCosts.IsEmpty()) { |
michael@0 | 357 | Remove(mCosts.LastElement().GetSurface()); |
michael@0 | 358 | } |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | static PLDHashOperator DoStopTracking(const SurfaceKey&, |
michael@0 | 362 | CachedSurface* aSurface, |
michael@0 | 363 | void* aCache) |
michael@0 | 364 | { |
michael@0 | 365 | static_cast<SurfaceCacheImpl*>(aCache)->StopTracking(aSurface); |
michael@0 | 366 | return PL_DHASH_NEXT; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | NS_IMETHOD |
michael@0 | 370 | CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) |
michael@0 | 371 | { |
michael@0 | 372 | return MOZ_COLLECT_REPORT( |
michael@0 | 373 | "imagelib-surface-cache", KIND_OTHER, UNITS_BYTES, |
michael@0 | 374 | SizeOfSurfacesEstimate(), |
michael@0 | 375 | "Memory used by the imagelib temporary surface cache."); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | // XXX(seth): This is currently only an estimate and, since we don't know |
michael@0 | 379 | // which surfaces are in GPU memory and which aren't, it's reported as |
michael@0 | 380 | // KIND_OTHER and will also show up in heap-unclassified. Bug 923302 will |
michael@0 | 381 | // make this nicer. |
michael@0 | 382 | Cost SizeOfSurfacesEstimate() const |
michael@0 | 383 | { |
michael@0 | 384 | return mMaxCost - mAvailableCost; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | private: |
michael@0 | 388 | already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey) |
michael@0 | 389 | { |
michael@0 | 390 | nsRefPtr<ImageSurfaceCache> imageCache; |
michael@0 | 391 | mImageCaches.Get(aImageKey, getter_AddRefs(imageCache)); |
michael@0 | 392 | return imageCache.forget(); |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | struct SurfaceTracker : public nsExpirationTracker<CachedSurface, 2> |
michael@0 | 396 | { |
michael@0 | 397 | SurfaceTracker(SurfaceCacheImpl* aCache, uint32_t aSurfaceCacheExpirationTimeMS) |
michael@0 | 398 | : nsExpirationTracker<CachedSurface, 2>(aSurfaceCacheExpirationTimeMS) |
michael@0 | 399 | , mCache(aCache) |
michael@0 | 400 | { } |
michael@0 | 401 | |
michael@0 | 402 | protected: |
michael@0 | 403 | virtual void NotifyExpired(CachedSurface* aSurface) MOZ_OVERRIDE |
michael@0 | 404 | { |
michael@0 | 405 | if (mCache) { |
michael@0 | 406 | mCache->Remove(aSurface); |
michael@0 | 407 | } |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | private: |
michael@0 | 411 | SurfaceCacheImpl* const mCache; // Weak pointer to owner. |
michael@0 | 412 | }; |
michael@0 | 413 | |
michael@0 | 414 | struct MemoryPressureObserver : public nsIObserver |
michael@0 | 415 | { |
michael@0 | 416 | NS_DECL_ISUPPORTS |
michael@0 | 417 | |
michael@0 | 418 | virtual ~MemoryPressureObserver() { } |
michael@0 | 419 | |
michael@0 | 420 | NS_IMETHOD Observe(nsISupports*, const char* aTopic, const char16_t*) |
michael@0 | 421 | { |
michael@0 | 422 | if (sInstance && strcmp(aTopic, "memory-pressure") == 0) { |
michael@0 | 423 | sInstance->DiscardAll(); |
michael@0 | 424 | } |
michael@0 | 425 | return NS_OK; |
michael@0 | 426 | } |
michael@0 | 427 | }; |
michael@0 | 428 | |
michael@0 | 429 | |
michael@0 | 430 | nsTArray<CostEntry> mCosts; |
michael@0 | 431 | nsRefPtrHashtable<nsPtrHashKey<Image>, ImageSurfaceCache> mImageCaches; |
michael@0 | 432 | SurfaceTracker mExpirationTracker; |
michael@0 | 433 | nsRefPtr<MemoryPressureObserver> mMemoryPressureObserver; |
michael@0 | 434 | const Cost mMaxCost; |
michael@0 | 435 | Cost mAvailableCost; |
michael@0 | 436 | }; |
michael@0 | 437 | |
michael@0 | 438 | NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter) |
michael@0 | 439 | NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver) |
michael@0 | 440 | |
michael@0 | 441 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 442 | // Public API |
michael@0 | 443 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 444 | |
michael@0 | 445 | /* static */ void |
michael@0 | 446 | SurfaceCache::Initialize() |
michael@0 | 447 | { |
michael@0 | 448 | // Initialize preferences. |
michael@0 | 449 | MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once"); |
michael@0 | 450 | |
michael@0 | 451 | // Length of time before an unused surface is removed from the cache, in milliseconds. |
michael@0 | 452 | // The default value gives an expiration time of 1 minute. |
michael@0 | 453 | uint32_t surfaceCacheExpirationTimeMS = |
michael@0 | 454 | Preferences::GetUint("image.mem.surfacecache.min_expiration_ms", 60 * 1000); |
michael@0 | 455 | |
michael@0 | 456 | // Maximum size of the surface cache, in kilobytes. |
michael@0 | 457 | // The default is 100MB. (But we may override this for e.g. B2G.) |
michael@0 | 458 | uint32_t surfaceCacheMaxSizeKB = |
michael@0 | 459 | Preferences::GetUint("image.mem.surfacecache.max_size_kb", 100 * 1024); |
michael@0 | 460 | |
michael@0 | 461 | // A knob determining the actual size of the surface cache. Currently the |
michael@0 | 462 | // cache is (size of main memory) / (surface cache size factor) KB |
michael@0 | 463 | // or (surface cache max size) KB, whichever is smaller. The formula |
michael@0 | 464 | // may change in the future, though. |
michael@0 | 465 | // The default value is 64, which yields a 64MB cache on a 4GB machine. |
michael@0 | 466 | // The smallest machines we are likely to run this code on have 256MB |
michael@0 | 467 | // of memory, which would yield a 4MB cache on the default setting. |
michael@0 | 468 | uint32_t surfaceCacheSizeFactor = |
michael@0 | 469 | Preferences::GetUint("image.mem.surfacecache.size_factor", 64); |
michael@0 | 470 | |
michael@0 | 471 | // Clamp to avoid division by zero below. |
michael@0 | 472 | surfaceCacheSizeFactor = max(surfaceCacheSizeFactor, 1u); |
michael@0 | 473 | |
michael@0 | 474 | // Compute the size of the surface cache. |
michael@0 | 475 | uint32_t proposedSize = PR_GetPhysicalMemorySize() / surfaceCacheSizeFactor; |
michael@0 | 476 | uint32_t surfaceCacheSizeBytes = min(proposedSize, surfaceCacheMaxSizeKB * 1024); |
michael@0 | 477 | |
michael@0 | 478 | // Create the surface cache singleton with the requested expiration time and |
michael@0 | 479 | // size. Note that the size is a limit that the cache may not grow beyond, but |
michael@0 | 480 | // we do not actually allocate any storage for surfaces at this time. |
michael@0 | 481 | sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS, |
michael@0 | 482 | surfaceCacheSizeBytes); |
michael@0 | 483 | sInstance->InitMemoryReporter(); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | /* static */ void |
michael@0 | 487 | SurfaceCache::Shutdown() |
michael@0 | 488 | { |
michael@0 | 489 | MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?"); |
michael@0 | 490 | sInstance = nullptr; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | /* static */ already_AddRefed<gfxDrawable> |
michael@0 | 494 | SurfaceCache::Lookup(const ImageKey aImageKey, |
michael@0 | 495 | const SurfaceKey& aSurfaceKey) |
michael@0 | 496 | { |
michael@0 | 497 | MOZ_ASSERT(sInstance, "Should be initialized"); |
michael@0 | 498 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 499 | |
michael@0 | 500 | return sInstance->Lookup(aImageKey, aSurfaceKey); |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | /* static */ void |
michael@0 | 504 | SurfaceCache::Insert(DrawTarget* aTarget, |
michael@0 | 505 | const ImageKey aImageKey, |
michael@0 | 506 | const SurfaceKey& aSurfaceKey) |
michael@0 | 507 | { |
michael@0 | 508 | MOZ_ASSERT(sInstance, "Should be initialized"); |
michael@0 | 509 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 510 | |
michael@0 | 511 | Cost cost = ComputeCost(aSurfaceKey.Size()); |
michael@0 | 512 | return sInstance->Insert(aTarget, aSurfaceKey.Size(), cost, aImageKey, |
michael@0 | 513 | aSurfaceKey); |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | /* static */ bool |
michael@0 | 517 | SurfaceCache::CanHold(const IntSize& aSize) |
michael@0 | 518 | { |
michael@0 | 519 | MOZ_ASSERT(sInstance, "Should be initialized"); |
michael@0 | 520 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 521 | |
michael@0 | 522 | Cost cost = ComputeCost(aSize); |
michael@0 | 523 | return sInstance->CanHold(cost); |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | /* static */ void |
michael@0 | 527 | SurfaceCache::Discard(Image* aImageKey) |
michael@0 | 528 | { |
michael@0 | 529 | MOZ_ASSERT(sInstance, "Should be initialized"); |
michael@0 | 530 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 531 | |
michael@0 | 532 | return sInstance->Discard(aImageKey); |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | /* static */ void |
michael@0 | 536 | SurfaceCache::DiscardAll() |
michael@0 | 537 | { |
michael@0 | 538 | MOZ_ASSERT(sInstance, "Should be initialized"); |
michael@0 | 539 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 540 | |
michael@0 | 541 | return sInstance->DiscardAll(); |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | } // namespace image |
michael@0 | 545 | } // namespace mozilla |