2013-10-22 04:37:59 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SurfaceCache is a service for caching temporary surfaces in imagelib.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SurfaceCache.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2014-12-18 19:50:56 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-02-10 19:03:04 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-10-22 04:37:59 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-12-10 02:53:47 -08:00
|
|
|
#include "mozilla/Likely.h"
|
2014-09-14 15:22:45 -07:00
|
|
|
#include "mozilla/Move.h"
|
2015-01-08 00:04:31 -08:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-10-22 04:37:59 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-11-25 15:57:43 -08:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-03-04 15:39:42 -08:00
|
|
|
#include "nsIMemoryReporter.h"
|
2014-02-09 00:04:37 -08:00
|
|
|
#include "gfx2DGlue.h"
|
2013-10-22 04:37:59 -07:00
|
|
|
#include "gfxPattern.h" // Workaround for flaw in bug 921753 part 2.
|
|
|
|
#include "gfxPlatform.h"
|
2014-09-22 14:30:20 -07:00
|
|
|
#include "gfxPrefs.h"
|
2014-09-14 15:22:45 -07:00
|
|
|
#include "imgFrame.h"
|
2014-11-27 19:55:57 -08:00
|
|
|
#include "Image.h"
|
2013-10-22 04:37:59 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsExpirationTracker.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "prsystem.h"
|
|
|
|
#include "SVGImageContext.h"
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-10 08:00:31 -07:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
namespace image {
|
|
|
|
|
2013-10-22 04:38:01 -07:00
|
|
|
class CachedSurface;
|
|
|
|
class SurfaceCacheImpl;
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-10-22 04:38:01 -07:00
|
|
|
// Static Data
|
2013-10-22 04:37:59 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-22 04:38:01 -07:00
|
|
|
// The single surface cache instance.
|
2013-11-25 15:57:43 -08:00
|
|
|
static StaticRefPtr<SurfaceCacheImpl> sInstance;
|
2013-10-22 04:38:01 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceCache Implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/**
|
2013-10-22 04:37:59 -07:00
|
|
|
* Cost models the cost of storing a surface in the cache. Right now, this is
|
|
|
|
* simply an estimate of the size of the surface in bytes, but in the future it
|
|
|
|
* may be worth taking into account the cost of rematerializing the surface as
|
|
|
|
* well.
|
|
|
|
*/
|
|
|
|
typedef size_t Cost;
|
|
|
|
|
2014-02-09 00:04:37 -08:00
|
|
|
static Cost ComputeCost(const IntSize& aSize)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
|
|
|
return aSize.width * aSize.height * 4; // width * height * 4 bytes (32bpp)
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/**
|
2013-10-22 04:37:59 -07:00
|
|
|
* Since we want to be able to make eviction decisions based on cost, we need to
|
|
|
|
* be able to look up the CachedSurface which has a certain cost as well as the
|
|
|
|
* cost associated with a certain CachedSurface. To make this possible, in data
|
|
|
|
* structures we actually store a CostEntry, which contains a weak pointer to
|
|
|
|
* its associated surface.
|
|
|
|
*
|
|
|
|
* To make usage of the weak pointer safe, SurfaceCacheImpl always calls
|
|
|
|
* StartTracking after a surface is stored in the cache and StopTracking before
|
|
|
|
* it is removed.
|
|
|
|
*/
|
|
|
|
class CostEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CostEntry(CachedSurface* aSurface, Cost aCost)
|
|
|
|
: mSurface(aSurface)
|
|
|
|
, mCost(aCost)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSurface, "Must have a surface");
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedSurface* GetSurface() const { return mSurface; }
|
|
|
|
Cost GetCost() const { return mCost; }
|
|
|
|
|
|
|
|
bool operator==(const CostEntry& aOther) const
|
|
|
|
{
|
|
|
|
return mSurface == aOther.mSurface &&
|
|
|
|
mCost == aOther.mCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CostEntry& aOther) const
|
|
|
|
{
|
|
|
|
return mCost < aOther.mCost ||
|
|
|
|
(mCost == aOther.mCost && mSurface < aOther.mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CachedSurface* mSurface;
|
|
|
|
Cost mCost;
|
|
|
|
};
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/**
|
2013-10-22 04:37:59 -07:00
|
|
|
* A CachedSurface associates a surface with a key that uniquely identifies that
|
|
|
|
* surface.
|
|
|
|
*/
|
2014-04-14 12:04:24 -07:00
|
|
|
class CachedSurface
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-06-18 19:29:00 -07:00
|
|
|
~CachedSurface() {}
|
2013-10-22 04:37:59 -07:00
|
|
|
public:
|
2015-01-08 00:04:31 -08:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(CachedSurface)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CachedSurface)
|
2014-04-14 12:04:24 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
CachedSurface(imgFrame* aSurface,
|
|
|
|
const Cost aCost,
|
|
|
|
const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Lifetime aLifetime)
|
2014-07-28 17:05:40 -07:00
|
|
|
: mSurface(aSurface)
|
2013-10-22 04:37:59 -07:00
|
|
|
, mCost(aCost)
|
|
|
|
, mImageKey(aImageKey)
|
|
|
|
, mSurfaceKey(aSurfaceKey)
|
2014-11-26 13:22:10 -08:00
|
|
|
, mLifetime(aLifetime)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-11-26 13:22:10 -08:00
|
|
|
MOZ_ASSERT(mSurface, "Must have a valid surface");
|
2013-10-22 04:37:59 -07:00
|
|
|
MOZ_ASSERT(mImageKey, "Must have a valid image key");
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:22:45 -07:00
|
|
|
DrawableFrameRef DrawableRef() const
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-09-14 15:22:45 -07:00
|
|
|
return mSurface->DrawableRef();
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void SetLocked(bool aLocked)
|
|
|
|
{
|
|
|
|
if (aLocked && mLifetime == Lifetime::Persistent) {
|
|
|
|
// This may fail, and that's OK. We make no guarantees about whether
|
|
|
|
// locking is successful if you call SurfaceCache::LockImage() after
|
|
|
|
// SurfaceCache::Insert().
|
|
|
|
mDrawableRef = mSurface->DrawableRef();
|
|
|
|
} else {
|
|
|
|
mDrawableRef.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLocked() const { return bool(mDrawableRef); }
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
ImageKey GetImageKey() const { return mImageKey; }
|
|
|
|
SurfaceKey GetSurfaceKey() const { return mSurfaceKey; }
|
|
|
|
CostEntry GetCostEntry() { return image::CostEntry(this, mCost); }
|
|
|
|
nsExpirationState* GetExpirationState() { return &mExpirationState; }
|
2014-11-26 13:22:10 -08:00
|
|
|
Lifetime GetLifetime() const { return mLifetime; }
|
2015-02-10 04:20:45 -08:00
|
|
|
bool IsDecoded() const { return mSurface->IsImageComplete(); }
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
// A helper type used by SurfaceCacheImpl::SizeOfSurfacesSum.
|
|
|
|
struct SizeOfSurfacesSum
|
|
|
|
{
|
|
|
|
SizeOfSurfacesSum(gfxMemoryLocation aLocation,
|
|
|
|
MallocSizeOf aMallocSizeOf)
|
|
|
|
: mLocation(aLocation)
|
|
|
|
, mMallocSizeOf(aMallocSizeOf)
|
|
|
|
, mSum(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Add(CachedSurface* aCachedSurface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCachedSurface, "Should have a CachedSurface");
|
|
|
|
|
|
|
|
if (!aCachedSurface->mSurface) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 18:00:15 -08:00
|
|
|
mSum += aCachedSurface->mSurface->SizeOfExcludingThis(mLocation,
|
|
|
|
mMallocSizeOf);
|
2014-11-26 13:22:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Result() const { return mSum; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
gfxMemoryLocation mLocation;
|
|
|
|
MallocSizeOf mMallocSizeOf;
|
|
|
|
size_t mSum;
|
|
|
|
};
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
private:
|
2014-09-14 15:22:45 -07:00
|
|
|
nsExpirationState mExpirationState;
|
|
|
|
nsRefPtr<imgFrame> mSurface;
|
2014-11-26 13:22:10 -08:00
|
|
|
DrawableFrameRef mDrawableRef;
|
2014-09-14 15:22:45 -07:00
|
|
|
const Cost mCost;
|
|
|
|
const ImageKey mImageKey;
|
|
|
|
const SurfaceKey mSurfaceKey;
|
2014-11-26 13:22:10 -08:00
|
|
|
const Lifetime mLifetime;
|
2013-10-22 04:37:59 -07:00
|
|
|
};
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/**
|
2013-10-22 04:37:59 -07:00
|
|
|
* An ImageSurfaceCache is a per-image surface cache. For correctness we must be
|
|
|
|
* able to remove all surfaces associated with an image when the image is
|
|
|
|
* destroyed or invalidated. Since this will happen frequently, it makes sense
|
|
|
|
* to make it cheap by storing the surfaces for each image separately.
|
2014-11-26 13:22:10 -08:00
|
|
|
*
|
|
|
|
* ImageSurfaceCache also keeps track of whether its associated image is locked
|
|
|
|
* or unlocked.
|
2013-10-22 04:37:59 -07:00
|
|
|
*/
|
2014-04-14 12:04:24 -07:00
|
|
|
class ImageSurfaceCache
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-11-26 13:22:10 -08:00
|
|
|
~ImageSurfaceCache() { }
|
2013-10-22 04:37:59 -07:00
|
|
|
public:
|
2014-11-26 13:22:10 -08:00
|
|
|
ImageSurfaceCache() : mLocked(false) { }
|
|
|
|
|
2015-01-08 00:04:31 -08:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageSurfaceCache)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageSurfaceCache)
|
2014-04-14 12:04:24 -07:00
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
typedef nsRefPtrHashtable<nsGenericHashKey<SurfaceKey>, CachedSurface> SurfaceTable;
|
|
|
|
|
|
|
|
bool IsEmpty() const { return mSurfaces.Count() == 0; }
|
|
|
|
|
|
|
|
void Insert(const SurfaceKey& aKey, CachedSurface* aSurface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSurface, "Should have a surface");
|
2014-11-26 13:22:10 -08:00
|
|
|
MOZ_ASSERT(!mLocked || aSurface->GetLifetime() != Lifetime::Persistent ||
|
|
|
|
aSurface->IsLocked(),
|
|
|
|
"Inserting an unlocked persistent surface for a locked image");
|
2013-10-22 04:37:59 -07:00
|
|
|
mSurfaces.Put(aKey, aSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(CachedSurface* aSurface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSurface, "Should have a surface");
|
|
|
|
MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()),
|
|
|
|
"Should not be removing a surface we don't have");
|
|
|
|
|
|
|
|
mSurfaces.Remove(aSurface->GetSurfaceKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<CachedSurface> Lookup(const SurfaceKey& aSurfaceKey)
|
|
|
|
{
|
|
|
|
nsRefPtr<CachedSurface> surface;
|
|
|
|
mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface));
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-18 14:02:14 -08:00
|
|
|
already_AddRefed<CachedSurface>
|
|
|
|
LookupBestMatch(const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags)
|
|
|
|
{
|
|
|
|
// Try for a perfect match first.
|
|
|
|
nsRefPtr<CachedSurface> surface;
|
|
|
|
mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface));
|
2015-03-18 20:31:38 -07:00
|
|
|
if (surface && surface->IsDecoded()) {
|
2015-01-18 14:02:14 -08:00
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's no perfect match, so find the best match we can.
|
|
|
|
MatchContext matchContext(aSurfaceKey, aAlternateFlags);
|
|
|
|
ForEach(TryToImproveMatch, &matchContext);
|
|
|
|
return matchContext.mBestMatch.forget();
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
void ForEach(SurfaceTable::EnumReadFunction aFunction, void* aData)
|
|
|
|
{
|
|
|
|
mSurfaces.EnumerateRead(aFunction, aData);
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void SetLocked(bool aLocked) { mLocked = aLocked; }
|
|
|
|
bool IsLocked() const { return mLocked; }
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
private:
|
2015-01-18 14:02:14 -08:00
|
|
|
struct MatchContext
|
|
|
|
{
|
|
|
|
MatchContext(const SurfaceKey& aIdealKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags)
|
|
|
|
: mIdealKey(aIdealKey)
|
|
|
|
, mAlternateFlags(aAlternateFlags)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
const SurfaceKey& mIdealKey;
|
|
|
|
const Maybe<uint32_t> mAlternateFlags;
|
|
|
|
nsRefPtr<CachedSurface> mBestMatch;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator TryToImproveMatch(const SurfaceKey& aSurfaceKey,
|
|
|
|
CachedSurface* aSurface,
|
|
|
|
void* aContext)
|
|
|
|
{
|
|
|
|
auto context = static_cast<MatchContext*>(aContext);
|
|
|
|
const SurfaceKey& idealKey = context->mIdealKey;
|
|
|
|
|
|
|
|
// Matching the animation time and SVG context is required.
|
|
|
|
if (aSurfaceKey.AnimationTime() != idealKey.AnimationTime() ||
|
|
|
|
aSurfaceKey.SVGContext() != idealKey.SVGContext()) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matching the flags is required, but we can match the alternate flags as
|
|
|
|
// well if some were provided.
|
|
|
|
if (aSurfaceKey.Flags() != idealKey.Flags() &&
|
|
|
|
Some(aSurfaceKey.Flags()) != context->mAlternateFlags) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything is better than nothing! (Within the constraints we just
|
|
|
|
// checked, of course.)
|
|
|
|
if (!context->mBestMatch) {
|
|
|
|
context->mBestMatch = aSurface;
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(context->mBestMatch, "Should have a current best match");
|
2015-02-10 04:20:45 -08:00
|
|
|
|
|
|
|
// Always prefer completely decoded surfaces.
|
|
|
|
bool bestMatchIsDecoded = context->mBestMatch->IsDecoded();
|
|
|
|
if (bestMatchIsDecoded && !aSurface->IsDecoded()) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
if (!bestMatchIsDecoded && aSurface->IsDecoded()) {
|
|
|
|
context->mBestMatch = aSurface;
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2015-01-18 14:02:14 -08:00
|
|
|
SurfaceKey bestMatchKey = context->mBestMatch->GetSurfaceKey();
|
|
|
|
|
|
|
|
// Compare sizes. We use an area-based heuristic here instead of computing a
|
|
|
|
// truly optimal answer, since it seems very unlikely to make a difference
|
|
|
|
// for realistic sizes.
|
|
|
|
int64_t idealArea = idealKey.Size().width * idealKey.Size().height;
|
|
|
|
int64_t surfaceArea = aSurfaceKey.Size().width * aSurfaceKey.Size().height;
|
|
|
|
int64_t bestMatchArea =
|
|
|
|
bestMatchKey.Size().width * bestMatchKey.Size().height;
|
|
|
|
|
|
|
|
// If the best match is smaller than the ideal size, prefer bigger sizes.
|
|
|
|
if (bestMatchArea < idealArea) {
|
|
|
|
if (surfaceArea > bestMatchArea) {
|
|
|
|
context->mBestMatch = aSurface;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other, prefer sizes closer to the ideal size, but still not smaller.
|
|
|
|
if (idealArea <= surfaceArea && surfaceArea < bestMatchArea) {
|
|
|
|
context->mBestMatch = aSurface;
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This surface isn't an improvement over the current best match.
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
SurfaceTable mSurfaces;
|
2014-11-26 13:22:10 -08:00
|
|
|
bool mLocked;
|
2013-10-22 04:37:59 -07:00
|
|
|
};
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/**
|
2013-10-22 04:37:59 -07:00
|
|
|
* SurfaceCacheImpl is responsible for determining which surfaces will be cached
|
|
|
|
* and managing the surface cache data structures. Rather than interact with
|
|
|
|
* SurfaceCacheImpl directly, client code interacts with SurfaceCache, which
|
|
|
|
* maintains high-level invariants and encapsulates the details of the surface
|
|
|
|
* cache's implementation.
|
|
|
|
*/
|
2014-08-05 10:33:55 -07:00
|
|
|
class SurfaceCacheImpl MOZ_FINAL : public nsIMemoryReporter
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-11-25 15:57:43 -08:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS,
|
2014-11-26 13:22:10 -08:00
|
|
|
uint32_t aSurfaceCacheDiscardFactor,
|
2013-10-22 04:37:59 -07:00
|
|
|
uint32_t aSurfaceCacheSize)
|
2015-01-31 15:29:48 -08:00
|
|
|
: mExpirationTracker(aSurfaceCacheExpirationTimeMS)
|
2013-10-22 04:38:01 -07:00
|
|
|
, mMemoryPressureObserver(new MemoryPressureObserver)
|
2015-01-08 00:04:31 -08:00
|
|
|
, mMutex("SurfaceCache")
|
2014-11-26 13:22:10 -08:00
|
|
|
, mDiscardFactor(aSurfaceCacheDiscardFactor)
|
2013-10-22 04:37:59 -07:00
|
|
|
, mMaxCost(aSurfaceCacheSize)
|
|
|
|
, mAvailableCost(aSurfaceCacheSize)
|
2014-11-26 13:22:10 -08:00
|
|
|
, mLockedCost(0)
|
2013-10-22 04:38:01 -07:00
|
|
|
{
|
2014-07-10 08:00:31 -07:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2013-10-22 04:38:01 -07:00
|
|
|
if (os)
|
|
|
|
os->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
|
|
|
|
}
|
|
|
|
|
2014-06-23 11:49:08 -07:00
|
|
|
private:
|
2013-11-25 15:57:43 -08:00
|
|
|
virtual ~SurfaceCacheImpl()
|
2013-10-22 04:38:01 -07:00
|
|
|
{
|
2014-07-10 08:00:31 -07:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2013-10-22 04:38:01 -07:00
|
|
|
if (os)
|
|
|
|
os->RemoveObserver(mMemoryPressureObserver, "memory-pressure");
|
|
|
|
|
2013-11-06 21:35:30 -08:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
2014-06-23 11:49:08 -07:00
|
|
|
public:
|
2015-01-08 00:04:31 -08:00
|
|
|
void InitMemoryReporter() { RegisterWeakMemoryReporter(this); }
|
|
|
|
|
|
|
|
Mutex& GetMutex() { return mMutex; }
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2015-01-11 22:29:35 -08:00
|
|
|
InsertOutcome Insert(imgFrame* aSurface,
|
|
|
|
const Cost aCost,
|
|
|
|
const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
Lifetime aLifetime)
|
|
|
|
{
|
|
|
|
// If this is a duplicate surface, refuse to replace the original.
|
|
|
|
if (MOZ_UNLIKELY(Lookup(aImageKey, aSurfaceKey))) {
|
|
|
|
return InsertOutcome::FAILURE_ALREADY_PRESENT;
|
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// If this is bigger than we can hold after discarding everything we can,
|
|
|
|
// refuse to cache it.
|
2015-01-11 22:29:35 -08:00
|
|
|
if (MOZ_UNLIKELY(!CanHoldAfterDiscarding(aCost))) {
|
|
|
|
return InsertOutcome::FAILURE;
|
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// Remove elements in order of cost until we can fit this in the cache. Note
|
|
|
|
// that locked surfaces aren't in mCosts, so we never remove them here.
|
2013-10-22 04:37:59 -07:00
|
|
|
while (aCost > mAvailableCost) {
|
|
|
|
MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and it still won't fit");
|
|
|
|
Remove(mCosts.LastElement().GetSurface());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locate the appropriate per-image cache. If there's not an existing cache
|
|
|
|
// for this image, create it.
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache) {
|
|
|
|
cache = new ImageSurfaceCache;
|
|
|
|
mImageCaches.Put(aImageKey, cache);
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
nsRefPtr<CachedSurface> surface =
|
|
|
|
new CachedSurface(aSurface, aCost, aImageKey, aSurfaceKey, aLifetime);
|
|
|
|
|
|
|
|
// We require that locking succeed if the image is locked and the surface is
|
|
|
|
// persistent; the caller may need to know this to handle errors correctly.
|
|
|
|
if (cache->IsLocked() && aLifetime == Lifetime::Persistent) {
|
|
|
|
surface->SetLocked(true);
|
|
|
|
if (!surface->IsLocked()) {
|
2015-01-11 22:29:35 -08:00
|
|
|
return InsertOutcome::FAILURE;
|
2014-11-26 13:22:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
// Insert.
|
|
|
|
MOZ_ASSERT(aCost <= mAvailableCost, "Inserting despite too large a cost");
|
|
|
|
cache->Insert(aSurfaceKey, surface);
|
|
|
|
StartTracking(surface);
|
2014-11-26 13:22:10 -08:00
|
|
|
|
2015-01-11 22:29:35 -08:00
|
|
|
return InsertOutcome::SUCCESS;
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(CachedSurface* aSurface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSurface, "Should have a surface");
|
2014-11-27 19:55:57 -08:00
|
|
|
ImageKey imageKey = aSurface->GetImageKey();
|
2013-10-22 04:37:59 -07:00
|
|
|
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
|
|
|
|
MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache");
|
|
|
|
|
2014-11-27 19:55:57 -08:00
|
|
|
// If the surface was persistent, tell its image that we discarded it.
|
|
|
|
if (aSurface->GetLifetime() == Lifetime::Persistent) {
|
|
|
|
static_cast<Image*>(imageKey)->OnSurfaceDiscarded();
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
StopTracking(aSurface);
|
|
|
|
cache->Remove(aSurface);
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// Remove the per-image cache if it's unneeded now. (Keep it if the image is
|
|
|
|
// locked, since the per-image cache is where we store that state.)
|
|
|
|
if (cache->IsEmpty() && !cache->IsLocked()) {
|
2013-10-22 04:37:59 -07:00
|
|
|
mImageCaches.Remove(imageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartTracking(CachedSurface* aSurface)
|
|
|
|
{
|
|
|
|
CostEntry costEntry = aSurface->GetCostEntry();
|
|
|
|
MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost,
|
|
|
|
"Cost too large and the caller didn't catch it");
|
|
|
|
|
|
|
|
mAvailableCost -= costEntry.GetCost();
|
2014-11-26 13:22:10 -08:00
|
|
|
|
|
|
|
if (aSurface->IsLocked()) {
|
|
|
|
mLockedCost += costEntry.GetCost();
|
|
|
|
MOZ_ASSERT(mLockedCost <= mMaxCost, "Locked more than we can hold?");
|
|
|
|
} else {
|
|
|
|
mCosts.InsertElementSorted(costEntry);
|
2014-12-10 02:53:47 -08:00
|
|
|
// This may fail during XPCOM shutdown, so we need to ensure the object is
|
|
|
|
// tracked before calling RemoveObject in StopTracking.
|
2014-11-26 13:22:10 -08:00
|
|
|
mExpirationTracker.AddObject(aSurface);
|
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void StopTracking(CachedSurface* aSurface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSurface, "Should have a surface");
|
|
|
|
CostEntry costEntry = aSurface->GetCostEntry();
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
if (aSurface->IsLocked()) {
|
|
|
|
MOZ_ASSERT(mLockedCost >= costEntry.GetCost(), "Costs don't balance");
|
|
|
|
mLockedCost -= costEntry.GetCost();
|
|
|
|
// XXX(seth): It'd be nice to use an O(log n) lookup here. This is O(n).
|
|
|
|
MOZ_ASSERT(!mCosts.Contains(costEntry),
|
|
|
|
"Shouldn't have a cost entry for a locked surface");
|
|
|
|
} else {
|
2014-12-10 02:53:47 -08:00
|
|
|
if (MOZ_LIKELY(aSurface->GetExpirationState()->IsTracked())) {
|
|
|
|
mExpirationTracker.RemoveObject(aSurface);
|
|
|
|
} else {
|
|
|
|
// Our call to AddObject must have failed in StartTracking; most likely
|
|
|
|
// we're in XPCOM shutdown right now.
|
|
|
|
NS_WARNING("Not expiration-tracking an unlocked surface!");
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
|
|
|
|
MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
|
|
|
|
}
|
2014-11-26 02:57:11 -08:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
mAvailableCost += costEntry.GetCost();
|
|
|
|
MOZ_ASSERT(mAvailableCost <= mMaxCost,
|
|
|
|
"More available cost than we started with");
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
2014-09-14 15:22:45 -07:00
|
|
|
DrawableFrameRef Lookup(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache)
|
2014-09-14 15:22:45 -07:00
|
|
|
return DrawableFrameRef(); // No cached surfaces for this image.
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
nsRefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey);
|
|
|
|
if (!surface)
|
2014-09-14 15:22:45 -07:00
|
|
|
return DrawableFrameRef(); // Lookup in the per-image cache missed.
|
|
|
|
|
|
|
|
DrawableFrameRef ref = surface->DrawableRef();
|
|
|
|
if (!ref) {
|
|
|
|
// The surface was released by the operating system. Remove the cache
|
|
|
|
// entry as well.
|
|
|
|
Remove(surface);
|
|
|
|
return DrawableFrameRef();
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
if (cache->IsLocked()) {
|
|
|
|
LockSurface(surface);
|
|
|
|
} else {
|
2014-11-26 13:22:10 -08:00
|
|
|
mExpirationTracker.MarkUsed(surface);
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:22:45 -07:00
|
|
|
return ref;
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
2015-01-18 14:02:14 -08:00
|
|
|
DrawableFrameRef LookupBestMatch(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags)
|
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache)
|
|
|
|
return DrawableFrameRef(); // No cached surfaces for this image.
|
|
|
|
|
|
|
|
// Repeatedly look up the best match, trying again if the resulting surface
|
|
|
|
// has been freed by the operating system, until we can either lock a
|
|
|
|
// surface for drawing or there are no matching surfaces left.
|
|
|
|
// XXX(seth): This is O(N^2), but N is expected to be very small. If we
|
|
|
|
// encounter a performance problem here we can revisit this.
|
|
|
|
|
|
|
|
nsRefPtr<CachedSurface> surface;
|
|
|
|
DrawableFrameRef ref;
|
|
|
|
while (true) {
|
|
|
|
surface = cache->LookupBestMatch(aSurfaceKey, aAlternateFlags);
|
|
|
|
if (!surface) {
|
|
|
|
return DrawableFrameRef(); // Lookup in the per-image cache missed.
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = surface->DrawableRef();
|
|
|
|
if (ref) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The surface was released by the operating system. Remove the cache
|
|
|
|
// entry as well.
|
|
|
|
Remove(surface);
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
if (cache->IsLocked()) {
|
|
|
|
LockSurface(surface);
|
|
|
|
} else {
|
2015-01-18 14:02:14 -08:00
|
|
|
mExpirationTracker.MarkUsed(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void RemoveSurface(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey)
|
2014-09-19 14:53:28 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache)
|
|
|
|
return; // No cached surfaces for this image.
|
|
|
|
|
|
|
|
nsRefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey);
|
|
|
|
if (!surface)
|
|
|
|
return; // Lookup in the per-image cache missed.
|
|
|
|
|
|
|
|
Remove(surface);
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
bool CanHold(const Cost aCost) const
|
|
|
|
{
|
|
|
|
return aCost <= mMaxCost;
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void LockImage(const ImageKey aImageKey)
|
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache) {
|
|
|
|
cache = new ImageSurfaceCache;
|
|
|
|
mImageCaches.Put(aImageKey, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->SetLocked(true);
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
// We don't relock this image's existing surfaces right away; instead, the
|
|
|
|
// image should arrange for Lookup() to touch them if they are still useful.
|
2014-11-26 13:22:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockImage(const ImageKey aImageKey)
|
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-01-20 03:06:37 -08:00
|
|
|
if (!cache || !cache->IsLocked()) {
|
|
|
|
return; // Already unlocked.
|
|
|
|
}
|
2014-11-26 13:22:10 -08:00
|
|
|
|
|
|
|
cache->SetLocked(false);
|
|
|
|
|
|
|
|
// Unlock all the surfaces the per-image cache is holding.
|
|
|
|
cache->ForEach(DoUnlockSurface, this);
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
void UnlockSurfaces(const ImageKey aImageKey)
|
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache || !cache->IsLocked()) {
|
|
|
|
return; // Already unlocked.
|
|
|
|
}
|
|
|
|
|
|
|
|
// (Note that we *don't* unlock the per-image cache here; that's the
|
|
|
|
// difference between this and UnlockImage.)
|
|
|
|
|
|
|
|
// Unlock all the surfaces the per-image cache is holding.
|
|
|
|
cache->ForEach(DoUnlockSurface, this);
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void RemoveImage(const ImageKey aImageKey)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache)
|
|
|
|
return; // No cached surfaces for this image, so nothing to do.
|
|
|
|
|
|
|
|
// Discard all of the cached surfaces for this image.
|
|
|
|
// XXX(seth): This is O(n^2) since for each item in the cache we are
|
|
|
|
// removing an element from the costs array. Since n is expected to be
|
|
|
|
// small, performance should be good, but if usage patterns change we should
|
|
|
|
// change the data structure used for mCosts.
|
|
|
|
cache->ForEach(DoStopTracking, this);
|
|
|
|
|
|
|
|
// The per-image cache isn't needed anymore, so remove it as well.
|
2014-11-26 13:22:10 -08:00
|
|
|
// This implicitly unlocks the image if it was locked.
|
2013-10-22 04:37:59 -07:00
|
|
|
mImageCaches.Remove(aImageKey);
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:38:01 -07:00
|
|
|
void DiscardAll()
|
|
|
|
{
|
|
|
|
// Remove in order of cost because mCosts is an array and the other data
|
2014-11-26 13:22:10 -08:00
|
|
|
// structures are all hash tables. Note that locked surfaces (persistent
|
|
|
|
// surfaces belonging to locked images) are not removed, since they aren't
|
|
|
|
// present in mCosts.
|
2013-10-22 04:38:01 -07:00
|
|
|
while (!mCosts.IsEmpty()) {
|
|
|
|
Remove(mCosts.LastElement().GetSurface());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
void DiscardForMemoryPressure()
|
|
|
|
{
|
|
|
|
// Compute our discardable cost. Since locked surfaces aren't discardable,
|
|
|
|
// we exclude them.
|
|
|
|
const Cost discardableCost = (mMaxCost - mAvailableCost) - mLockedCost;
|
|
|
|
MOZ_ASSERT(discardableCost <= mMaxCost, "Discardable cost doesn't add up");
|
|
|
|
|
|
|
|
// Our target is to raise our available cost by (1 / mDiscardFactor) of our
|
|
|
|
// discardable cost - in other words, we want to end up with about
|
|
|
|
// (discardableCost / mDiscardFactor) fewer bytes stored in the surface
|
|
|
|
// cache after we're done.
|
|
|
|
const Cost targetCost = mAvailableCost + (discardableCost / mDiscardFactor);
|
|
|
|
|
|
|
|
if (targetCost > mMaxCost - mLockedCost) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Target cost is more than we can discard");
|
|
|
|
DiscardAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard surfaces until we've reduced our cost to our target cost.
|
|
|
|
while (mAvailableCost < targetCost) {
|
|
|
|
MOZ_ASSERT(!mCosts.IsEmpty(), "Removed everything and still not done");
|
|
|
|
Remove(mCosts.LastElement().GetSurface());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
void LockSurface(CachedSurface* aSurface)
|
2014-11-26 13:22:10 -08:00
|
|
|
{
|
|
|
|
if (aSurface->GetLifetime() == Lifetime::Transient ||
|
|
|
|
aSurface->IsLocked()) {
|
2015-01-20 03:06:37 -08:00
|
|
|
return;
|
2014-11-26 13:22:10 -08:00
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
StopTracking(aSurface);
|
2014-11-26 13:22:10 -08:00
|
|
|
|
|
|
|
// Lock the surface. This can fail.
|
|
|
|
aSurface->SetLocked(true);
|
2015-01-20 03:06:37 -08:00
|
|
|
StartTracking(aSurface);
|
|
|
|
}
|
2014-11-26 13:22:10 -08:00
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
static PLDHashOperator DoStopTracking(const SurfaceKey&,
|
|
|
|
CachedSurface* aSurface,
|
|
|
|
void* aCache)
|
|
|
|
{
|
|
|
|
static_cast<SurfaceCacheImpl*>(aCache)->StopTracking(aSurface);
|
2014-11-26 13:22:10 -08:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator DoUnlockSurface(const SurfaceKey&,
|
|
|
|
CachedSurface* aSurface,
|
|
|
|
void* aCache)
|
|
|
|
{
|
|
|
|
if (aSurface->GetLifetime() == Lifetime::Transient ||
|
|
|
|
!aSurface->IsLocked()) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cache = static_cast<SurfaceCacheImpl*>(aCache);
|
|
|
|
cache->StopTracking(aSurface);
|
|
|
|
|
|
|
|
aSurface->SetLocked(false);
|
|
|
|
cache->StartTracking(aSurface);
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
NS_IMETHOD
|
2014-11-26 13:22:10 -08:00
|
|
|
CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData,
|
2014-11-26 13:22:11 -08:00
|
|
|
bool aAnonymize) MOZ_OVERRIDE
|
2013-10-22 04:38:01 -07:00
|
|
|
{
|
2015-01-31 15:29:48 -08:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
// We have explicit memory reporting for the surface cache which is more
|
|
|
|
// accurate than the cost metrics we report here, but these metrics are
|
|
|
|
// still useful to report, since they control the cache's behavior.
|
2014-11-26 13:22:10 -08:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
rv = MOZ_COLLECT_REPORT("imagelib-surface-cache-estimated-total",
|
2014-11-26 13:22:10 -08:00
|
|
|
KIND_OTHER, UNITS_BYTES,
|
2014-11-26 13:22:11 -08:00
|
|
|
(mMaxCost - mAvailableCost),
|
|
|
|
"Estimated total memory used by the imagelib "
|
|
|
|
"surface cache.");
|
2014-11-26 13:22:10 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
rv = MOZ_COLLECT_REPORT("imagelib-surface-cache-estimated-locked",
|
2014-11-26 13:22:10 -08:00
|
|
|
KIND_OTHER, UNITS_BYTES,
|
2014-11-26 13:22:11 -08:00
|
|
|
mLockedCost,
|
|
|
|
"Estimated memory used by locked surfaces in the "
|
|
|
|
"imagelib surface cache.");
|
2014-11-26 13:22:10 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-11-06 21:35:30 -08:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
size_t SizeOfSurfaces(const ImageKey aImageKey,
|
|
|
|
gfxMemoryLocation aLocation,
|
|
|
|
MallocSizeOf aMallocSizeOf)
|
2013-11-06 21:35:30 -08:00
|
|
|
{
|
2014-11-26 13:22:11 -08:00
|
|
|
nsRefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache) {
|
|
|
|
return 0; // No surfaces for this image.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum the size of all surfaces in the per-image cache.
|
|
|
|
CachedSurface::SizeOfSurfacesSum sum(aLocation, aMallocSizeOf);
|
|
|
|
cache->ForEach(DoSizeOfSurfacesSum, &sum);
|
|
|
|
|
|
|
|
return sum.Result();
|
2013-10-22 04:38:01 -07:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
static PLDHashOperator DoSizeOfSurfacesSum(const SurfaceKey&,
|
|
|
|
CachedSurface* aSurface,
|
|
|
|
void* aSum)
|
2014-11-26 13:22:10 -08:00
|
|
|
{
|
2014-11-26 13:22:11 -08:00
|
|
|
auto sum = static_cast<CachedSurface::SizeOfSurfacesSum*>(aSum);
|
|
|
|
sum->Add(aSurface);
|
|
|
|
return PL_DHASH_NEXT;
|
2014-11-26 13:22:10 -08:00
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
private:
|
|
|
|
already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey)
|
|
|
|
{
|
|
|
|
nsRefPtr<ImageSurfaceCache> imageCache;
|
|
|
|
mImageCaches.Get(aImageKey, getter_AddRefs(imageCache));
|
|
|
|
return imageCache.forget();
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// This is similar to CanHold() except that it takes into account the costs of
|
|
|
|
// locked surfaces. It's used internally in Insert(), but it's not exposed
|
|
|
|
// publicly because if we start permitting multithreaded access to the surface
|
|
|
|
// cache, which seems likely, then the result would be meaningless: another
|
|
|
|
// thread could insert a persistent surface or lock an image at any time.
|
|
|
|
bool CanHoldAfterDiscarding(const Cost aCost) const
|
|
|
|
{
|
|
|
|
return aCost <= mMaxCost - mLockedCost;
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
struct SurfaceTracker : public nsExpirationTracker<CachedSurface, 2>
|
|
|
|
{
|
2015-01-31 15:29:48 -08:00
|
|
|
explicit SurfaceTracker(uint32_t aSurfaceCacheExpirationTimeMS)
|
2013-10-22 04:37:59 -07:00
|
|
|
: nsExpirationTracker<CachedSurface, 2>(aSurfaceCacheExpirationTimeMS)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void NotifyExpired(CachedSurface* aSurface) MOZ_OVERRIDE
|
|
|
|
{
|
2015-01-31 15:29:48 -08:00
|
|
|
if (sInstance) {
|
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
|
|
|
sInstance->Remove(aSurface);
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-22 04:38:01 -07:00
|
|
|
struct MemoryPressureObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
NS_IMETHOD Observe(nsISupports*,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t*) MOZ_OVERRIDE
|
2013-10-22 04:38:01 -07:00
|
|
|
{
|
|
|
|
if (sInstance && strcmp(aTopic, "memory-pressure") == 0) {
|
2015-01-31 15:29:48 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
sInstance->DiscardForMemoryPressure();
|
2013-10-22 04:38:01 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-23 11:49:08 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~MemoryPressureObserver() { }
|
2013-10-22 04:38:01 -07:00
|
|
|
};
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
nsTArray<CostEntry> mCosts;
|
|
|
|
nsRefPtrHashtable<nsPtrHashKey<Image>, ImageSurfaceCache> mImageCaches;
|
|
|
|
SurfaceTracker mExpirationTracker;
|
2013-10-22 04:38:01 -07:00
|
|
|
nsRefPtr<MemoryPressureObserver> mMemoryPressureObserver;
|
2015-01-08 00:04:31 -08:00
|
|
|
Mutex mMutex;
|
2014-11-26 13:22:10 -08:00
|
|
|
const uint32_t mDiscardFactor;
|
2013-10-22 04:37:59 -07:00
|
|
|
const Cost mMaxCost;
|
|
|
|
Cost mAvailableCost;
|
2014-11-26 13:22:10 -08:00
|
|
|
Cost mLockedCost;
|
2013-10-22 04:37:59 -07:00
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter)
|
|
|
|
NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver)
|
2013-10-22 04:37:59 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public API
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::Initialize()
|
|
|
|
{
|
|
|
|
// Initialize preferences.
|
2015-01-08 00:04:31 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-22 04:37:59 -07:00
|
|
|
MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once");
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// See gfxPrefs for the default values of these preferences.
|
2014-09-22 14:30:20 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// Length of time before an unused surface is removed from the cache, in
|
|
|
|
// milliseconds.
|
|
|
|
uint32_t surfaceCacheExpirationTimeMS =
|
|
|
|
gfxPrefs::ImageMemSurfaceCacheMinExpirationMS();
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// What fraction of the memory used by the surface cache we should discard
|
|
|
|
// when we get a memory pressure notification. This value is interpreted as
|
|
|
|
// 1/N, so 1 means to discard everything, 2 means to discard about half of the
|
|
|
|
// memory we're using, and so forth. We clamp it to avoid division by zero.
|
|
|
|
uint32_t surfaceCacheDiscardFactor =
|
|
|
|
max(gfxPrefs::ImageMemSurfaceCacheDiscardFactor(), 1u);
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
// Maximum size of the surface cache, in kilobytes.
|
2014-11-26 13:22:10 -08:00
|
|
|
uint64_t surfaceCacheMaxSizeKB = gfxPrefs::ImageMemSurfaceCacheMaxSizeKB();
|
2013-10-22 04:37:59 -07:00
|
|
|
|
|
|
|
// A knob determining the actual size of the surface cache. Currently the
|
|
|
|
// cache is (size of main memory) / (surface cache size factor) KB
|
|
|
|
// or (surface cache max size) KB, whichever is smaller. The formula
|
|
|
|
// may change in the future, though.
|
2014-11-26 13:22:10 -08:00
|
|
|
// For example, a value of 4 would yield a 256MB cache on a 1GB machine.
|
2013-10-22 04:37:59 -07:00
|
|
|
// The smallest machines we are likely to run this code on have 256MB
|
2014-11-26 13:22:10 -08:00
|
|
|
// of memory, which would yield a 64MB cache on this setting.
|
2014-11-26 13:22:10 -08:00
|
|
|
// We clamp this value to avoid division by zero.
|
|
|
|
uint32_t surfaceCacheSizeFactor =
|
|
|
|
max(gfxPrefs::ImageMemSurfaceCacheSizeFactor(), 1u);
|
2013-10-22 04:37:59 -07:00
|
|
|
|
|
|
|
// Compute the size of the surface cache.
|
2014-12-18 19:50:56 -08:00
|
|
|
uint64_t memorySize = PR_GetPhysicalMemorySize();
|
|
|
|
if (memorySize == 0) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("PR_GetPhysicalMemorySize not implemented here");
|
|
|
|
memorySize = 256 * 1024 * 1024; // Fall back to 256MB.
|
|
|
|
}
|
|
|
|
uint64_t proposedSize = memorySize / surfaceCacheSizeFactor;
|
2014-11-26 13:22:10 -08:00
|
|
|
uint64_t surfaceCacheSizeBytes = min(proposedSize, surfaceCacheMaxSizeKB * 1024);
|
|
|
|
uint32_t finalSurfaceCacheSizeBytes =
|
|
|
|
min(surfaceCacheSizeBytes, uint64_t(UINT32_MAX));
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
// Create the surface cache singleton with the requested settings. Note that
|
|
|
|
// the size is a limit that the cache may not grow beyond, but we do not
|
|
|
|
// actually allocate any storage for surfaces at this time.
|
2013-10-22 04:37:59 -07:00
|
|
|
sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS,
|
2014-11-26 13:22:10 -08:00
|
|
|
surfaceCacheDiscardFactor,
|
2014-11-26 13:22:10 -08:00
|
|
|
finalSurfaceCacheSizeBytes);
|
2013-11-06 21:35:30 -08:00
|
|
|
sInstance->InitMemoryReporter();
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::Shutdown()
|
|
|
|
{
|
2015-01-08 00:04:31 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-22 04:37:59 -07:00
|
|
|
MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?");
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:22:45 -07:00
|
|
|
/* static */ DrawableFrameRef
|
2015-01-18 14:02:13 -08:00
|
|
|
SurfaceCache::Lookup(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags /* = Nothing() */)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-09-19 14:53:29 -07:00
|
|
|
if (!sInstance) {
|
|
|
|
return DrawableFrameRef();
|
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2015-01-18 14:02:13 -08:00
|
|
|
|
|
|
|
DrawableFrameRef ref = sInstance->Lookup(aImageKey, aSurfaceKey);
|
|
|
|
if (!ref && aAlternateFlags) {
|
|
|
|
ref = sInstance->Lookup(aImageKey,
|
|
|
|
aSurfaceKey.WithNewFlags(*aAlternateFlags));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
2015-01-18 14:02:14 -08:00
|
|
|
/* static */ DrawableFrameRef
|
|
|
|
SurfaceCache::LookupBestMatch(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags
|
|
|
|
/* = Nothing() */)
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
return DrawableFrameRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
|
|
|
return sInstance->LookupBestMatch(aImageKey, aSurfaceKey, aAlternateFlags);
|
|
|
|
}
|
|
|
|
|
2015-01-11 22:29:35 -08:00
|
|
|
/* static */ InsertOutcome
|
2014-09-14 15:22:45 -07:00
|
|
|
SurfaceCache::Insert(imgFrame* aSurface,
|
2013-10-22 04:37:59 -07:00
|
|
|
const ImageKey aImageKey,
|
2014-11-26 13:22:10 -08:00
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
Lifetime aLifetime)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-11-26 13:22:10 -08:00
|
|
|
if (!sInstance) {
|
2015-01-11 22:29:35 -08:00
|
|
|
return InsertOutcome::FAILURE;
|
2014-09-19 14:53:29 -07:00
|
|
|
}
|
2014-11-26 13:22:10 -08:00
|
|
|
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
Cost cost = ComputeCost(aSurfaceKey.Size());
|
|
|
|
return sInstance->Insert(aSurface, cost, aImageKey, aSurfaceKey, aLifetime);
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
2014-02-09 00:04:37 -08:00
|
|
|
SurfaceCache::CanHold(const IntSize& aSize)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-09-19 14:53:29 -07:00
|
|
|
if (!sInstance) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
|
|
|
|
Cost cost = ComputeCost(aSize);
|
|
|
|
return sInstance->CanHold(cost);
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:35 -08:00
|
|
|
/* static */ bool
|
|
|
|
SurfaceCache::CanHold(size_t aSize)
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance->CanHold(aSize);
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:53:28 -07:00
|
|
|
/* static */ void
|
2014-11-26 13:22:10 -08:00
|
|
|
SurfaceCache::LockImage(Image* aImageKey)
|
|
|
|
{
|
|
|
|
if (sInstance) {
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
return sInstance->LockImage(aImageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::UnlockImage(Image* aImageKey)
|
|
|
|
{
|
|
|
|
if (sInstance) {
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
return sInstance->UnlockImage(aImageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:06:37 -08:00
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::UnlockSurfaces(const ImageKey aImageKey)
|
|
|
|
{
|
|
|
|
if (sInstance) {
|
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
|
|
|
return sInstance->UnlockSurfaces(aImageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:10 -08:00
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::RemoveSurface(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey)
|
2014-09-19 14:53:28 -07:00
|
|
|
{
|
2014-09-19 14:53:29 -07:00
|
|
|
if (sInstance) {
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
sInstance->RemoveSurface(aImageKey, aSurfaceKey);
|
2014-09-19 14:53:29 -07:00
|
|
|
}
|
2014-09-19 14:53:28 -07:00
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
/* static */ void
|
2014-11-26 13:22:10 -08:00
|
|
|
SurfaceCache::RemoveImage(Image* aImageKey)
|
2013-10-22 04:37:59 -07:00
|
|
|
{
|
2014-09-19 14:53:29 -07:00
|
|
|
if (sInstance) {
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:10 -08:00
|
|
|
sInstance->RemoveImage(aImageKey);
|
2014-09-19 14:53:29 -07:00
|
|
|
}
|
2013-10-22 04:37:59 -07:00
|
|
|
}
|
|
|
|
|
2014-02-08 11:37:39 -08:00
|
|
|
/* static */ void
|
|
|
|
SurfaceCache::DiscardAll()
|
|
|
|
{
|
2014-06-25 06:28:51 -07:00
|
|
|
if (sInstance) {
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-06-25 06:28:51 -07:00
|
|
|
sInstance->DiscardAll();
|
|
|
|
}
|
2014-02-08 11:37:39 -08:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:22:11 -08:00
|
|
|
/* static */ size_t
|
|
|
|
SurfaceCache::SizeOfSurfaces(const ImageKey aImageKey,
|
|
|
|
gfxMemoryLocation aLocation,
|
|
|
|
MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:04:31 -08:00
|
|
|
MutexAutoLock lock(sInstance->GetMutex());
|
2014-11-26 13:22:11 -08:00
|
|
|
return sInstance->SizeOfSurfaces(aImageKey, aLocation, aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-10-22 04:37:59 -07:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|