2013-10-22 11:37:59 +00: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/. */
|
|
|
|
|
|
|
|
/**
|
2014-09-14 22:22:45 +00:00
|
|
|
* SurfaceCache is a service for caching temporary surfaces and decoded image
|
|
|
|
* data in imagelib.
|
2013-10-22 11:37:59 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-15 03:52:05 +00:00
|
|
|
#ifndef mozilla_image_SurfaceCache_h
|
|
|
|
#define mozilla_image_SurfaceCache_h
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2014-11-26 21:22:11 +00:00
|
|
|
#include "mozilla/Maybe.h" // for Maybe
|
|
|
|
#include "mozilla/MemoryReporting.h" // for MallocSizeOf
|
|
|
|
#include "mozilla/HashFunctions.h" // for HashGeneric and AddToHash
|
|
|
|
#include "gfx2DGlue.h" // for gfxMemoryLocation
|
|
|
|
#include "gfxPoint.h" // for gfxSize
|
|
|
|
#include "nsCOMPtr.h" // for already_AddRefed
|
|
|
|
#include "mozilla/gfx/Point.h" // for mozilla::gfx::IntSize
|
|
|
|
#include "mozilla/gfx/2D.h" // for SourceSurface
|
|
|
|
#include "SVGImageContext.h" // for SVGImageContext
|
2013-10-22 11:37:59 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
class Image;
|
2014-09-14 22:22:45 +00:00
|
|
|
class imgFrame;
|
2015-07-01 01:57:03 +00:00
|
|
|
class LookupResult;
|
2015-04-28 18:46:17 +00:00
|
|
|
struct SurfaceMemoryCounter;
|
2013-10-22 11:37:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ImageKey contains the information we need to look up all cached surfaces for
|
|
|
|
* a particular image.
|
|
|
|
*/
|
|
|
|
typedef Image* ImageKey;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SurfaceKey contains the information we need to look up a specific cached
|
|
|
|
* surface. Together with an ImageKey, this uniquely identifies the surface.
|
|
|
|
*
|
2014-09-19 21:53:28 +00:00
|
|
|
* Callers should construct a SurfaceKey using the appropriate helper function
|
|
|
|
* for their image type - either RasterSurfaceKey or VectorSurfaceKey.
|
2013-10-22 11:37:59 +00:00
|
|
|
*/
|
|
|
|
class SurfaceKey
|
|
|
|
{
|
2014-02-09 08:04:37 +00:00
|
|
|
typedef gfx::IntSize IntSize;
|
2014-09-15 06:17:33 +00:00
|
|
|
|
2014-09-19 21:53:28 +00:00
|
|
|
public:
|
2013-10-22 11:37:59 +00:00
|
|
|
bool operator==(const SurfaceKey& aOther) const
|
|
|
|
{
|
|
|
|
return aOther.mSize == mSize &&
|
2014-08-22 20:12:38 +00:00
|
|
|
aOther.mSVGContext == mSVGContext &&
|
2013-10-22 11:37:59 +00:00
|
|
|
aOther.mAnimationTime == mAnimationTime &&
|
|
|
|
aOther.mFlags == mFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Hash() const
|
|
|
|
{
|
|
|
|
uint32_t hash = HashGeneric(mSize.width, mSize.height);
|
2014-08-22 20:12:38 +00:00
|
|
|
hash = AddToHash(hash, mSVGContext.map(HashSIC).valueOr(0));
|
2013-10-22 11:37:59 +00:00
|
|
|
hash = AddToHash(hash, mAnimationTime, mFlags);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:04:37 +00:00
|
|
|
IntSize Size() const { return mSize; }
|
2015-01-18 22:02:14 +00:00
|
|
|
Maybe<SVGImageContext> SVGContext() const { return mSVGContext; }
|
|
|
|
float AnimationTime() const { return mAnimationTime; }
|
|
|
|
uint32_t Flags() const { return mFlags; }
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2015-01-18 22:02:13 +00:00
|
|
|
SurfaceKey WithNewFlags(uint32_t aFlags) const
|
|
|
|
{
|
|
|
|
return SurfaceKey(mSize, mSVGContext, mAnimationTime, aFlags);
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:37:59 +00:00
|
|
|
private:
|
2014-09-19 21:53:28 +00:00
|
|
|
SurfaceKey(const IntSize& aSize,
|
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
|
|
|
const float aAnimationTime,
|
|
|
|
const uint32_t aFlags)
|
|
|
|
: mSize(aSize)
|
|
|
|
, mSVGContext(aSVGContext)
|
|
|
|
, mAnimationTime(aAnimationTime)
|
|
|
|
, mFlags(aFlags)
|
|
|
|
{ }
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
static uint32_t HashSIC(const SVGImageContext& aSIC) {
|
|
|
|
return aSIC.Hash();
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:40:23 +00:00
|
|
|
friend SurfaceKey RasterSurfaceKey(const IntSize&, uint32_t, uint32_t);
|
2014-09-19 21:53:28 +00:00
|
|
|
friend SurfaceKey VectorSurfaceKey(const IntSize&,
|
|
|
|
const Maybe<SVGImageContext>&,
|
2015-01-07 09:40:23 +00:00
|
|
|
float);
|
2014-09-19 21:53:28 +00:00
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
IntSize mSize;
|
|
|
|
Maybe<SVGImageContext> mSVGContext;
|
|
|
|
float mAnimationTime;
|
|
|
|
uint32_t mFlags;
|
2013-10-22 11:37:59 +00:00
|
|
|
};
|
|
|
|
|
2014-09-19 21:53:28 +00:00
|
|
|
inline SurfaceKey
|
|
|
|
RasterSurfaceKey(const gfx::IntSize& aSize,
|
2015-01-07 09:40:23 +00:00
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aFrameNum)
|
2014-09-19 21:53:28 +00:00
|
|
|
{
|
2015-01-07 09:40:23 +00:00
|
|
|
return SurfaceKey(aSize, Nothing(), float(aFrameNum), aFlags);
|
2014-09-19 21:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline SurfaceKey
|
|
|
|
VectorSurfaceKey(const gfx::IntSize& aSize,
|
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2015-01-07 09:40:23 +00:00
|
|
|
float aAnimationTime)
|
2014-09-19 21:53:28 +00:00
|
|
|
{
|
|
|
|
// We don't care about aFlags for VectorImage because none of the flags we
|
|
|
|
// have right now influence VectorImage's rendering. If we add a new flag that
|
|
|
|
// *does* affect how a VectorImage renders, we'll have to change this.
|
|
|
|
return SurfaceKey(aSize, aSVGContext, aAnimationTime, 0);
|
|
|
|
}
|
|
|
|
|
2015-01-25 22:22:07 +00:00
|
|
|
enum class Lifetime : uint8_t {
|
2014-11-26 21:22:10 +00:00
|
|
|
Transient,
|
|
|
|
Persistent
|
2015-01-25 22:22:07 +00:00
|
|
|
};
|
2014-11-26 21:22:10 +00:00
|
|
|
|
2015-01-25 22:22:07 +00:00
|
|
|
enum class InsertOutcome : uint8_t {
|
2015-01-12 06:29:35 +00:00
|
|
|
SUCCESS, // Success (but see Insert documentation).
|
|
|
|
FAILURE, // Couldn't insert (e.g., for capacity reasons).
|
|
|
|
FAILURE_ALREADY_PRESENT // A surface with the same key is already present.
|
2015-01-25 22:22:07 +00:00
|
|
|
};
|
2015-01-12 06:29:35 +00:00
|
|
|
|
2013-10-22 11:37:59 +00:00
|
|
|
/**
|
|
|
|
* SurfaceCache is an imagelib-global service that allows caching of temporary
|
2014-11-26 21:22:10 +00:00
|
|
|
* surfaces. Surfaces normally expire from the cache automatically if they go
|
|
|
|
* too long without being accessed.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
2014-09-14 22:22:45 +00:00
|
|
|
* SurfaceCache does not hold surfaces directly; instead, it holds imgFrame
|
|
|
|
* objects, which hold surfaces but also layer on additional features specific
|
|
|
|
* to imagelib's needs like animation, padding support, and transparent support
|
|
|
|
* for volatile buffers.
|
|
|
|
*
|
2014-11-26 21:22:10 +00:00
|
|
|
* Sometime it's useful to temporarily prevent surfaces from expiring from the
|
|
|
|
* cache. This is most often because losing the data could harm the user
|
|
|
|
* experience (for example, we often don't want to allow surfaces that are
|
|
|
|
* currently visible to expire) or because it's not possible to rematerialize
|
|
|
|
* the surface. SurfaceCache supports this through the use of image locking and
|
|
|
|
* surface lifetimes; see the comments for Insert() and LockImage() for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* Any image which stores surfaces in the SurfaceCache *must* ensure that it
|
|
|
|
* calls RemoveImage() before it is destroyed. See the comments for
|
|
|
|
* RemoveImage() for more details.
|
2013-10-22 11:37:59 +00:00
|
|
|
*/
|
|
|
|
struct SurfaceCache
|
|
|
|
{
|
2014-02-09 08:04:37 +00:00
|
|
|
typedef gfx::IntSize IntSize;
|
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2013-10-22 11:37:59 +00:00
|
|
|
* Initialize static data. Called during imagelib module initialization.
|
|
|
|
*/
|
|
|
|
static void Initialize();
|
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2013-10-22 11:37:59 +00:00
|
|
|
* Release static data. Called during imagelib module shutdown.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2014-09-14 22:22:45 +00:00
|
|
|
* Look up the imgFrame containing a surface in the cache and returns a
|
|
|
|
* drawable reference to that imgFrame.
|
|
|
|
*
|
2015-01-20 11:06:37 +00:00
|
|
|
* If the image associated with the surface is locked, then the surface will
|
|
|
|
* be locked before it is returned.
|
|
|
|
*
|
2014-09-14 22:22:45 +00:00
|
|
|
* If the imgFrame was found in the cache, but had stored its surface in a
|
|
|
|
* volatile buffer which was discarded by the OS, then it is automatically
|
2015-07-01 01:57:03 +00:00
|
|
|
* removed from the cache and an empty LookupResult is returned. Note that
|
2014-11-26 21:22:10 +00:00
|
|
|
* this will never happen to persistent surfaces associated with a locked
|
|
|
|
* image; the cache keeps a strong reference to such surfaces internally.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
2015-03-24 20:16:00 +00:00
|
|
|
* @param aImageKey Key data identifying which image the surface belongs
|
|
|
|
* to.
|
|
|
|
*
|
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the requested
|
|
|
|
* surface.
|
|
|
|
*
|
2015-01-18 22:02:13 +00:00
|
|
|
* @param aAlternateFlags If not Nothing(), a different set of flags than the
|
|
|
|
* ones specified in @aSurfaceKey which are also
|
|
|
|
* acceptable to the caller. This is more efficient
|
|
|
|
* than calling Lookup() twice, which requires taking a
|
|
|
|
* lock each time.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
2015-07-01 01:57:03 +00:00
|
|
|
* @return a LookupResult, which will either contain a
|
|
|
|
* DrawableFrameRef to the requested surface, or an
|
|
|
|
* empty DrawableFrameRef if the surface was not found.
|
2013-10-22 11:37:59 +00:00
|
|
|
*/
|
2015-07-01 01:57:03 +00:00
|
|
|
static LookupResult Lookup(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags = Nothing());
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2015-01-18 22:02:14 +00:00
|
|
|
/**
|
|
|
|
* Looks up the best matching surface in the cache and returns a drawable
|
|
|
|
* reference to the imgFrame containing it.
|
|
|
|
*
|
|
|
|
* Returned surfaces may vary from the requested surface only in terms of
|
|
|
|
* size, unless @aAlternateFlags is specified.
|
|
|
|
*
|
2015-03-24 20:16:00 +00:00
|
|
|
* @param aImageKey Key data identifying which image the surface belongs
|
|
|
|
* to.
|
2015-01-20 11:06:37 +00:00
|
|
|
*
|
2015-01-18 22:02:14 +00:00
|
|
|
* @param aSurfaceKey Key data which identifies the ideal surface to return.
|
2015-03-24 20:16:00 +00:00
|
|
|
*
|
2015-01-18 22:02:14 +00:00
|
|
|
* @param aAlternateFlags If not Nothing(), a different set of flags than the
|
|
|
|
* ones specified in @aSurfaceKey which are also
|
|
|
|
* acceptable to the caller. This is much more
|
|
|
|
* efficient than calling LookupBestMatch() twice.
|
|
|
|
*
|
2015-07-01 01:57:03 +00:00
|
|
|
* @return a LookupResult, which will either contain a
|
|
|
|
* DrawableFrameRef to a surface similar to the
|
|
|
|
* requested surface, or an empty DrawableFrameRef if
|
|
|
|
* the surface was not found. Callers can use
|
|
|
|
* LookupResult::IsExactMatch() to check whether the
|
|
|
|
* returned surface exactly matches @aSurfaceKey.
|
2015-01-18 22:02:14 +00:00
|
|
|
*/
|
2015-07-01 01:57:03 +00:00
|
|
|
static LookupResult LookupBestMatch(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
const Maybe<uint32_t>& aAlternateFlags
|
|
|
|
= Nothing());
|
2015-01-18 22:02:14 +00:00
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2015-01-12 06:29:35 +00:00
|
|
|
* Insert a surface into the cache. If a surface with the same ImageKey and
|
|
|
|
* SurfaceKey is already in the cache, Insert returns FAILURE_ALREADY_PRESENT.
|
2015-07-20 01:39:44 +00:00
|
|
|
* If a matching placeholder is already present, the placeholder is removed.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
2014-11-26 21:22:10 +00:00
|
|
|
* Each surface in the cache has a lifetime, either Transient or Persistent.
|
|
|
|
* Transient surfaces can expire from the cache at any time. Persistent
|
2015-01-20 11:06:37 +00:00
|
|
|
* surfaces, on the other hand, will never expire as long as they remain
|
|
|
|
* locked, but if they become unlocked, can expire just like transient
|
|
|
|
* surfaces. When it is first inserted, a persistent surface is locked if its
|
|
|
|
* associated image is locked. When that image is later unlocked, the surface
|
|
|
|
* becomes unlocked too. To become locked again at that point, two things must
|
|
|
|
* happen: the image must become locked again (via LockImage()), and the
|
|
|
|
* surface must be touched again (via one of the Lookup() functions).
|
|
|
|
*
|
|
|
|
* All of this means that a very particular procedure has to be followed for
|
|
|
|
* surfaces which cannot be rematerialized. First, they must be inserted
|
|
|
|
* with a persistent lifetime *after* the image is locked with LockImage(); if
|
|
|
|
* you use the other order, the surfaces might expire before LockImage() gets
|
|
|
|
* called or before the surface is touched again by Lookup(). Second, the
|
|
|
|
* image they are associated with must never be unlocked.
|
2014-11-26 21:22:10 +00:00
|
|
|
*
|
|
|
|
* If a surface cannot be rematerialized, it may be important to know whether
|
2015-01-12 06:29:35 +00:00
|
|
|
* it was inserted into the cache successfully. Insert() returns FAILURE if it
|
2014-11-26 21:22:10 +00:00
|
|
|
* failed to insert the surface, which could happen because of capacity
|
|
|
|
* reasons, or because it was already freed by the OS. If you aren't inserting
|
|
|
|
* a surface with persistent lifetime, or if the surface isn't associated with
|
2015-01-12 06:29:35 +00:00
|
|
|
* a locked image, checking for SUCCESS or FAILURE is useless: the surface
|
|
|
|
* might expire immediately after being inserted, even though Insert()
|
|
|
|
* returned SUCCESS. Thus, many callers do not need to check the result of
|
|
|
|
* Insert() at all.
|
2014-11-26 21:22:10 +00:00
|
|
|
*
|
2014-09-14 22:22:45 +00:00
|
|
|
* @param aTarget The new surface (wrapped in an imgFrame) to insert into
|
|
|
|
* the cache.
|
2015-03-24 20:16:00 +00:00
|
|
|
* @param aImageKey Key data identifying which image the surface belongs
|
|
|
|
* to.
|
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the requested
|
|
|
|
* surface.
|
2014-11-26 21:22:10 +00:00
|
|
|
* @param aLifetime Whether this is a transient surface that can always be
|
|
|
|
* allowed to expire, or a persistent surface that
|
|
|
|
* shouldn't expire if the image is locked.
|
2015-01-12 06:29:35 +00:00
|
|
|
* @return SUCCESS if the surface was inserted successfully. (But see above
|
|
|
|
* for more information about when you should check this.)
|
|
|
|
* FAILURE if the surface could not be inserted, e.g. for capacity
|
|
|
|
* reasons. (But see above for more information about when you
|
|
|
|
* should check this.)
|
|
|
|
* FAILURE_ALREADY_PRESENT if a surface with the same ImageKey and
|
|
|
|
* SurfaceKey already exists in the cache.
|
2013-10-22 11:37:59 +00:00
|
|
|
*/
|
2015-01-12 06:29:35 +00:00
|
|
|
static InsertOutcome Insert(imgFrame* aSurface,
|
|
|
|
const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
Lifetime aLifetime);
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2015-07-20 01:39:44 +00:00
|
|
|
/**
|
|
|
|
* Insert a placeholder for a surface into the cache. If a surface with the
|
|
|
|
* same ImageKey and SurfaceKey is already in the cache, InsertPlaceholder()
|
|
|
|
* returns FAILURE_ALREADY_PRESENT.
|
|
|
|
*
|
|
|
|
* Placeholders exist to allow lazy allocation of surfaces. The Lookup*()
|
|
|
|
* methods will report whether a placeholder for an exactly matching surface
|
|
|
|
* existed by returning a MatchType of PENDING or SUBSTITUTE_BECAUSE_PENDING,
|
|
|
|
* but they will never return a placeholder directly. (They couldn't, since
|
|
|
|
* placeholders don't have an associated surface.)
|
|
|
|
*
|
|
|
|
* Once inserted, placeholders can be removed using RemoveSurface() or
|
|
|
|
* RemoveImage(), just like a surface. They're automatically removed when a
|
|
|
|
* real surface that matches the placeholder is inserted with Insert().
|
|
|
|
*
|
|
|
|
* @param aImageKey Key data identifying which image the placeholder
|
|
|
|
* belongs to.
|
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the surface the
|
|
|
|
* placeholder stands in for.
|
|
|
|
* @return SUCCESS if the placeholder was inserted successfully.
|
|
|
|
* FAILURE if the placeholder could not be inserted for some reason.
|
|
|
|
* FAILURE_ALREADY_PRESENT if a surface with the same ImageKey and
|
|
|
|
* SurfaceKey already exists in the cache.
|
|
|
|
*/
|
|
|
|
static InsertOutcome InsertPlaceholder(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey);
|
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2013-10-22 11:37:59 +00:00
|
|
|
* Checks if a surface of a given size could possibly be stored in the cache.
|
|
|
|
* If CanHold() returns false, Insert() will always fail to insert the
|
|
|
|
* surface, but the inverse is not true: Insert() may take more information
|
|
|
|
* into account than just image size when deciding whether to cache the
|
|
|
|
* surface, so Insert() may still fail even if CanHold() returns true.
|
|
|
|
*
|
|
|
|
* Use CanHold() to avoid the need to create a temporary surface when we know
|
|
|
|
* for sure the cache can't hold it.
|
|
|
|
*
|
|
|
|
* @param aSize The dimensions of a surface in pixels.
|
2015-05-06 05:19:30 +00:00
|
|
|
* @param aBytesPerPixel How many bytes each pixel of the surface requires.
|
|
|
|
* Defaults to 4, which is appropriate for RGBA or RGBX
|
|
|
|
* images.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
|
|
|
* @return false if the surface cache can't hold a surface of that size.
|
|
|
|
*/
|
2015-05-06 05:19:30 +00:00
|
|
|
static bool CanHold(const IntSize& aSize, uint32_t aBytesPerPixel = 4);
|
2015-01-15 23:11:35 +00:00
|
|
|
static bool CanHold(size_t aSize);
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2015-01-20 11:06:37 +00:00
|
|
|
* Locks an image. Any of the image's persistent surfaces which are either
|
|
|
|
* inserted or accessed while the image is locked will not expire.
|
|
|
|
*
|
|
|
|
* Locking an image does not automatically lock that image's existing
|
|
|
|
* surfaces. A call to LockImage() guarantees that persistent surfaces which
|
|
|
|
* are inserted afterward will not expire before the next call to
|
|
|
|
* UnlockImage() or UnlockSurfaces() for that image. Surfaces that are
|
|
|
|
* accessed via Lookup() or LookupBestMatch() after a LockImage() call will
|
|
|
|
* also not expire until the next UnlockImage() or UnlockSurfaces() call for
|
|
|
|
* that image. Any other surfaces owned by the image may expire at any time,
|
|
|
|
* whether they are persistent or transient.
|
2014-11-26 21:22:10 +00:00
|
|
|
*
|
|
|
|
* Regardless of locking, any of an image's surfaces may be removed using
|
|
|
|
* RemoveSurface(), and all of an image's surfaces are removed by
|
|
|
|
* RemoveImage(), whether the image is locked or not.
|
|
|
|
*
|
|
|
|
* It's safe to call LockImage() on an image that's already locked; this has
|
|
|
|
* no effect.
|
|
|
|
*
|
|
|
|
* You must always unlock any image you lock. You may do this explicitly by
|
|
|
|
* calling UnlockImage(), or implicitly by calling RemoveImage(). Since you're
|
|
|
|
* required to call RemoveImage() when you destroy an image, this doesn't
|
|
|
|
* impose any additional requirements, but it's preferable to call
|
|
|
|
* UnlockImage() earlier if it's possible.
|
|
|
|
*
|
|
|
|
* @param aImageKey The image to lock.
|
|
|
|
*/
|
|
|
|
static void LockImage(const ImageKey aImageKey);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlocks an image, allowing any of its surfaces to expire at any time.
|
|
|
|
*
|
|
|
|
* It's OK to call UnlockImage() on an image that's already unlocked; this has
|
|
|
|
* no effect.
|
|
|
|
*
|
2015-01-20 11:06:37 +00:00
|
|
|
* @param aImageKey The image to unlock.
|
2014-11-26 21:22:10 +00:00
|
|
|
*/
|
|
|
|
static void UnlockImage(const ImageKey aImageKey);
|
|
|
|
|
2015-01-20 11:06:37 +00:00
|
|
|
/**
|
|
|
|
* Unlocks the existing surfaces of an image, allowing them to expire at any
|
|
|
|
* time.
|
|
|
|
*
|
|
|
|
* This does not unlock the image itself, so accessing the surfaces via
|
|
|
|
* Lookup() or LookupBestMatch() will lock them again, and prevent them from
|
|
|
|
* expiring.
|
|
|
|
*
|
|
|
|
* This is intended to be used in situations where it's no longer clear that
|
|
|
|
* all of the persistent surfaces owned by an image are needed. Calling
|
|
|
|
* UnlockSurfaces() and then taking some action that will cause Lookup() to
|
|
|
|
* touch any surfaces that are still useful will permit the remaining surfaces
|
|
|
|
* to expire from the cache.
|
|
|
|
*
|
|
|
|
* If the image is unlocked, this has no effect.
|
|
|
|
*
|
|
|
|
* @param aImageKey The image which should have its existing surfaces
|
|
|
|
* unlocked.
|
|
|
|
*/
|
|
|
|
static void UnlockSurfaces(const ImageKey aImageKey);
|
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
2015-07-20 01:39:44 +00:00
|
|
|
* Removes a surface or placeholder from the cache, if it's present. If it's
|
|
|
|
* not present, RemoveSurface() has no effect.
|
2014-09-19 21:53:28 +00:00
|
|
|
*
|
|
|
|
* Use this function to remove individual surfaces that have become invalid.
|
2014-11-26 21:22:10 +00:00
|
|
|
* Prefer RemoveImage() or DiscardAll() when they're applicable, as they have
|
|
|
|
* much better performance than calling this function repeatedly.
|
2014-09-19 21:53:28 +00:00
|
|
|
*
|
2015-03-24 20:16:00 +00:00
|
|
|
* @param aImageKey Key data identifying which image the surface belongs
|
|
|
|
to.
|
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the requested
|
|
|
|
surface.
|
2014-09-19 21:53:28 +00:00
|
|
|
*/
|
2014-11-26 21:22:10 +00:00
|
|
|
static void RemoveSurface(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey);
|
|
|
|
|
|
|
|
/**
|
2015-07-20 01:39:44 +00:00
|
|
|
* Removes all cached surfaces and placeholders associated with the given
|
|
|
|
* image from the cache. If the image is locked, it is automatically
|
|
|
|
* unlocked.
|
2014-11-26 21:22:10 +00:00
|
|
|
*
|
|
|
|
* This MUST be called, at a minimum, when an Image which could be storing
|
|
|
|
* surfaces in the surface cache is destroyed. If another image were allocated
|
|
|
|
* at the same address it could result in subtle, difficult-to-reproduce bugs.
|
2013-10-22 11:37:59 +00:00
|
|
|
*
|
|
|
|
* @param aImageKey The image which should be removed from the cache.
|
|
|
|
*/
|
2014-11-26 21:22:10 +00:00
|
|
|
static void RemoveImage(const ImageKey aImageKey);
|
2013-10-22 11:37:59 +00:00
|
|
|
|
2014-11-26 21:22:10 +00:00
|
|
|
/**
|
|
|
|
* Evicts all evictable surfaces from the cache.
|
|
|
|
*
|
|
|
|
* All surfaces are evictable except for persistent surfaces associated with
|
|
|
|
* locked images. Non-evictable surfaces can only be removed by
|
|
|
|
* RemoveSurface() or RemoveImage().
|
2014-02-08 19:37:39 +00:00
|
|
|
*/
|
|
|
|
static void DiscardAll();
|
|
|
|
|
2014-11-26 21:22:11 +00:00
|
|
|
/**
|
2015-04-28 18:46:17 +00:00
|
|
|
* Collects an accounting of the surfaces contained in the SurfaceCache for
|
|
|
|
* the given image, along with their size and various other metadata.
|
2014-11-26 21:22:11 +00:00
|
|
|
*
|
|
|
|
* This is intended for use with memory reporting.
|
|
|
|
*
|
|
|
|
* @param aImageKey The image to report memory usage for.
|
2015-04-28 18:46:17 +00:00
|
|
|
* @param aCounters An array into which the report for each surface will
|
|
|
|
* be written.
|
|
|
|
* @param aMallocSizeOf A fallback malloc memory reporting function.
|
2014-11-26 21:22:11 +00:00
|
|
|
*/
|
2015-04-28 18:46:17 +00:00
|
|
|
static void CollectSizeOfSurfaces(const ImageKey aImageKey,
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf);
|
2014-11-26 21:22:11 +00:00
|
|
|
|
2013-10-22 11:37:59 +00:00
|
|
|
private:
|
|
|
|
virtual ~SurfaceCache() = 0; // Forbid instantiation.
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 03:52:05 +00:00
|
|
|
#endif // mozilla_image_SurfaceCache_h
|