2013-10-22 15:37:59 +04: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-15 02:22:45 +04:00
|
|
|
* SurfaceCache is a service for caching temporary surfaces and decoded image
|
|
|
|
* data in imagelib.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#ifndef mozilla_image_SurfaceCache_h
|
|
|
|
#define mozilla_image_SurfaceCache_h
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
#include "mozilla/Maybe.h" // for Maybe
|
2016-07-02 08:20:32 +03:00
|
|
|
#include "mozilla/NotNull.h"
|
2014-11-27 00:22:11 +03:00
|
|
|
#include "mozilla/MemoryReporting.h" // for MallocSizeOf
|
|
|
|
#include "mozilla/HashFunctions.h" // for HashGeneric and AddToHash
|
2015-07-29 07:02:45 +03:00
|
|
|
#include "gfx2DGlue.h"
|
2014-11-27 00:22:11 +03:00
|
|
|
#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
|
2016-08-18 10:06:41 +03:00
|
|
|
#include "PlaybackType.h"
|
2015-08-15 03:56:44 +03:00
|
|
|
#include "SurfaceFlags.h"
|
2014-11-27 00:22:11 +03:00
|
|
|
#include "SVGImageContext.h" // for SVGImageContext
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
class Image;
|
2016-07-02 08:20:32 +03:00
|
|
|
class ISurfaceProvider;
|
2015-07-01 04:57:03 +03:00
|
|
|
class LookupResult;
|
2016-08-02 02:44:39 +03:00
|
|
|
class SurfaceCacheImpl;
|
2015-04-28 21:46:17 +03:00
|
|
|
struct SurfaceMemoryCounter;
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
/*
|
2016-07-02 08:33:58 +03:00
|
|
|
* ImageKey contains the information we need to look up all SurfaceCache entries
|
|
|
|
* for a particular image.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
|
|
|
typedef Image* ImageKey;
|
|
|
|
|
|
|
|
/*
|
2016-07-02 08:33:58 +03:00
|
|
|
* SurfaceKey contains the information we need to look up a specific
|
|
|
|
* SurfaceCache entry. Together with an ImageKey, this uniquely identifies the
|
|
|
|
* surface.
|
2013-10-22 15:37:59 +04:00
|
|
|
*
|
2014-09-20 01:53:28 +04:00
|
|
|
* Callers should construct a SurfaceKey using the appropriate helper function
|
|
|
|
* for their image type - either RasterSurfaceKey or VectorSurfaceKey.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
|
|
|
class SurfaceKey {
|
2014-02-09 12:04:37 +04:00
|
|
|
typedef gfx::IntSize IntSize;
|
2014-09-15 10:17:33 +04:00
|
|
|
|
2014-09-20 01:53:28 +04:00
|
|
|
public:
|
2013-10-22 15:37:59 +04:00
|
|
|
bool operator==(const SurfaceKey& aOther) const {
|
2014-08-23 00:12:38 +04:00
|
|
|
return aOther.mSize == mSize && aOther.mSVGContext == mSVGContext &&
|
2016-08-18 10:06:41 +03:00
|
|
|
aOther.mPlayback == mPlayback && aOther.mFlags == mFlags;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-07-04 07:54:15 +03:00
|
|
|
PLDHashNumber Hash() const {
|
|
|
|
PLDHashNumber hash = HashGeneric(mSize.width, mSize.height);
|
2014-08-23 00:12:38 +04:00
|
|
|
hash = AddToHash(hash, mSVGContext.map(HashSIC).valueOr(0));
|
2016-08-18 10:06:41 +03:00
|
|
|
hash = AddToHash(hash, uint8_t(mPlayback), uint32_t(mFlags));
|
2013-10-22 15:37:59 +04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
SurfaceKey CloneWithSize(const IntSize& aSize) const {
|
|
|
|
return SurfaceKey(aSize, mSVGContext, mPlayback, mFlags);
|
|
|
|
}
|
|
|
|
|
2016-02-27 02:13:59 +03:00
|
|
|
const IntSize& Size() const { return mSize; }
|
2017-11-22 03:41:35 +03:00
|
|
|
const Maybe<SVGImageContext>& SVGContext() const { return mSVGContext; }
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType Playback() const { return mPlayback; }
|
2015-08-15 03:56:44 +03:00
|
|
|
SurfaceFlags Flags() const { return mFlags; }
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
private:
|
2014-09-20 01:53:28 +04:00
|
|
|
SurfaceKey(const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType aPlayback, SurfaceFlags aFlags)
|
2014-09-20 01:53:28 +04:00
|
|
|
: mSize(aSize),
|
|
|
|
mSVGContext(aSVGContext),
|
2016-08-18 10:06:41 +03:00
|
|
|
mPlayback(aPlayback),
|
2014-09-20 01:53:28 +04:00
|
|
|
mFlags(aFlags) {}
|
|
|
|
|
2017-07-04 07:54:15 +03:00
|
|
|
static PLDHashNumber HashSIC(const SVGImageContext& aSIC) {
|
2014-08-23 00:12:38 +04:00
|
|
|
return aSIC.Hash();
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:06:41 +03:00
|
|
|
friend SurfaceKey RasterSurfaceKey(const IntSize&, SurfaceFlags,
|
|
|
|
PlaybackType);
|
2014-09-20 01:53:28 +04:00
|
|
|
friend SurfaceKey VectorSurfaceKey(const IntSize&,
|
2016-08-18 10:06:41 +03:00
|
|
|
const Maybe<SVGImageContext>&);
|
2014-09-20 01:53:28 +04:00
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
IntSize mSize;
|
|
|
|
Maybe<SVGImageContext> mSVGContext;
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType mPlayback;
|
2015-08-15 03:56:44 +03:00
|
|
|
SurfaceFlags mFlags;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2014-09-20 01:53:28 +04:00
|
|
|
inline SurfaceKey RasterSurfaceKey(const gfx::IntSize& aSize,
|
2015-08-15 03:56:44 +03:00
|
|
|
SurfaceFlags aFlags,
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType aPlayback) {
|
|
|
|
return SurfaceKey(aSize, Nothing(), aPlayback, aFlags);
|
2014-09-20 01:53:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline SurfaceKey VectorSurfaceKey(const gfx::IntSize& aSize,
|
2016-08-18 10:06:41 +03:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext) {
|
2014-09-20 01:53:28 +04: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.
|
2016-08-18 10:06:41 +03:00
|
|
|
// Similarly, we don't accept a PlaybackType parameter because we don't
|
|
|
|
// currently cache frames of animated SVG images.
|
|
|
|
return SurfaceKey(aSize, aSVGContext, PlaybackType::eStatic,
|
|
|
|
DefaultSurfaceFlags());
|
2014-09-20 01:53:28 +04:00
|
|
|
}
|
|
|
|
|
2016-08-02 02:44:39 +03:00
|
|
|
/**
|
|
|
|
* AvailabilityState is used to track whether an ISurfaceProvider has a surface
|
|
|
|
* available or is just a placeholder.
|
|
|
|
*
|
|
|
|
* To ensure that availability changes are atomic (and especially that internal
|
|
|
|
* SurfaceCache code doesn't have to deal with asynchronous availability
|
|
|
|
* changes), an ISurfaceProvider which starts as a placeholder can only reveal
|
|
|
|
* the fact that it now has a surface available via a call to
|
|
|
|
* SurfaceCache::SurfaceAvailable().
|
2017-09-05 14:58:45 +03:00
|
|
|
*
|
|
|
|
* It also tracks whether or not there are "explicit" users of this surface
|
|
|
|
* which will not accept substitutes. This is used by SurfaceCache when pruning
|
|
|
|
* unnecessary surfaces from the cache.
|
2016-08-02 02:44:39 +03:00
|
|
|
*/
|
|
|
|
class AvailabilityState {
|
|
|
|
public:
|
|
|
|
static AvailabilityState StartAvailable() { return AvailabilityState(true); }
|
|
|
|
static AvailabilityState StartAsPlaceholder() {
|
|
|
|
return AvailabilityState(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAvailable() const { return mIsAvailable; }
|
|
|
|
bool IsPlaceholder() const { return !mIsAvailable; }
|
2017-09-05 14:58:45 +03:00
|
|
|
bool CannotSubstitute() const { return mCannotSubstitute; }
|
|
|
|
|
|
|
|
void SetCannotSubstitute() { mCannotSubstitute = true; }
|
2016-08-02 02:44:39 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class SurfaceCacheImpl;
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
explicit AvailabilityState(bool aIsAvailable)
|
|
|
|
: mIsAvailable(aIsAvailable), mCannotSubstitute(false) {}
|
2016-08-02 02:44:39 +03:00
|
|
|
|
|
|
|
void SetAvailable() { mIsAvailable = true; }
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
bool mIsAvailable : 1;
|
|
|
|
bool mCannotSubstitute : 1;
|
2016-08-02 02:44:39 +03:00
|
|
|
};
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class InsertOutcome : uint8_t {
|
2015-01-12 09:29:35 +03: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-26 01:22:07 +03:00
|
|
|
};
|
2015-01-12 09:29:35 +03:00
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* SurfaceCache is an ImageLib-global service that allows caching of decoded
|
|
|
|
* image surfaces, temporary surfaces (e.g. for caching rotated or clipped
|
|
|
|
* versions of images), or dynamically generated surfaces (e.g. for animations).
|
|
|
|
* SurfaceCache entries normally expire from the cache automatically if they go
|
2014-11-27 00:22:10 +03:00
|
|
|
* too long without being accessed.
|
2013-10-22 15:37:59 +04:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* Because SurfaceCache must support both normal surfaces and dynamically
|
|
|
|
* generated surfaces, it does not actually hold surfaces directly. Instead, it
|
|
|
|
* holds ISurfaceProvider objects which can provide access to a surface when
|
|
|
|
* requested; SurfaceCache doesn't care about the details of how this is
|
|
|
|
* accomplished.
|
2014-09-15 02:22:45 +04:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* Sometime it's useful to temporarily prevent entries from expiring from the
|
2014-11-27 00:22:10 +03:00
|
|
|
* 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
|
2015-09-20 02:20:59 +03:00
|
|
|
* the surface. SurfaceCache supports this through the use of image locking; see
|
|
|
|
* the comments for Insert() and LockImage() for more details.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
|
|
|
* 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 15:37:59 +04:00
|
|
|
*/
|
|
|
|
struct SurfaceCache {
|
2014-02-09 12:04:37 +04:00
|
|
|
typedef gfx::IntSize IntSize;
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04:00
|
|
|
* Initialize static data. Called during imagelib module initialization.
|
|
|
|
*/
|
|
|
|
static void Initialize();
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04:00
|
|
|
* Release static data. Called during imagelib module shutdown.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2016-08-16 03:58:35 +03:00
|
|
|
* Looks up the requested cache entry and returns a drawable reference to its
|
|
|
|
* associated surface.
|
2014-09-15 02:22:45 +04:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* If the image associated with the cache entry is locked, then the entry will
|
2015-01-20 14:06:37 +03:00
|
|
|
* be locked before it is returned.
|
|
|
|
*
|
2016-08-16 03:58:35 +03:00
|
|
|
* If a matching ISurfaceProvider was found in the cache, but SurfaceCache
|
|
|
|
* couldn't obtain a surface from it (e.g. because it had stored its surface
|
|
|
|
* in a volatile buffer which was discarded by the OS) then it is
|
|
|
|
* automatically removed from the cache and an empty LookupResult is returned.
|
|
|
|
* Note that this will never happen to ISurfaceProviders associated with a
|
|
|
|
* locked image; SurfaceCache tells such ISurfaceProviders to keep a strong
|
|
|
|
* references to their data internally.
|
2015-03-24 23:16:00 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* @param aImageKey Key data identifying which image the cache entry
|
|
|
|
* belongs to.
|
2015-03-24 23:16:00 +03:00
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the requested
|
2016-07-02 08:33:58 +03:00
|
|
|
* cache entry.
|
2016-08-18 03:50:31 +03:00
|
|
|
* @return a LookupResult which will contain a DrawableSurface
|
|
|
|
* if the cache entry was found.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
2015-07-01 04:57:03 +03:00
|
|
|
static LookupResult Lookup(const ImageKey aImageKey,
|
2018-10-13 08:31:02 +03:00
|
|
|
const SurfaceKey& aSurfaceKey, bool aMarkUsed);
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2015-01-19 01:02:14 +03:00
|
|
|
/**
|
2016-08-16 03:58:35 +03:00
|
|
|
* Looks up the best matching cache entry and returns a drawable reference to
|
|
|
|
* its associated surface.
|
2015-01-20 14:06:37 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* The result may vary from the requested cache entry only in terms of size.
|
2015-03-24 23:16:00 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* @param aImageKey Key data identifying which image the cache entry
|
|
|
|
* belongs to.
|
|
|
|
* @param aSurfaceKey Key data which uniquely identifies the requested
|
|
|
|
* cache entry.
|
2016-08-18 03:50:31 +03:00
|
|
|
* @return a LookupResult which will contain a DrawableSurface
|
|
|
|
* if a cache entry similar to the one the caller
|
|
|
|
* requested could be found. Callers can use
|
|
|
|
* LookupResult::IsExactMatch() to check whether the
|
|
|
|
* returned surface exactly matches @aSurfaceKey.
|
2015-01-19 01:02:14 +03:00
|
|
|
*/
|
2015-07-01 04:57:03 +03:00
|
|
|
static LookupResult LookupBestMatch(const ImageKey aImageKey,
|
2018-10-13 08:31:02 +03:00
|
|
|
const SurfaceKey& aSurfaceKey,
|
2018-10-13 08:31:03 +03:00
|
|
|
bool aMarkUsed);
|
2015-01-19 01:02:14 +03:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* Insert an ISurfaceProvider into the cache. If an entry with the same
|
|
|
|
* ImageKey and SurfaceKey is already in the cache, Insert returns
|
2016-08-20 01:04:34 +03:00
|
|
|
* FAILURE_ALREADY_PRESENT. If a matching placeholder is already present, it
|
|
|
|
* is replaced.
|
2013-10-22 15:37:59 +04:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* Cache entries will never expire as long as they remain locked, but if they
|
2015-09-20 02:20:59 +03:00
|
|
|
* become unlocked, they can expire either because the SurfaceCache runs out
|
|
|
|
* of capacity or because they've gone too long without being used. When it
|
2016-07-02 08:33:58 +03:00
|
|
|
* is first inserted, a cache entry is locked if its associated image is
|
|
|
|
* locked. When that image is later unlocked, the cache entry becomes
|
|
|
|
* unlocked too. To become locked again at that point, two things must happen:
|
|
|
|
* the image must become locked again (via LockImage()), and the cache entry
|
|
|
|
* must be touched again (via one of the Lookup() functions).
|
2015-01-20 14:06:37 +03:00
|
|
|
*
|
|
|
|
* All of this means that a very particular procedure has to be followed for
|
2016-07-02 08:33:58 +03:00
|
|
|
* cache entries which cannot be rematerialized. First, they must be inserted
|
2015-09-20 02:20:59 +03:00
|
|
|
* *after* the image is locked with LockImage(); if you use the other order,
|
2016-07-02 08:33:58 +03:00
|
|
|
* the cache entry might expire before LockImage() gets called or before the
|
|
|
|
* entry is touched again by Lookup(). Second, the image they are associated
|
2015-09-20 02:20:59 +03:00
|
|
|
* with must never be unlocked.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* If a cache entry cannot be rematerialized, it may be important to know
|
|
|
|
* whether it was inserted into the cache successfully. Insert() returns
|
|
|
|
* FAILURE if it failed to insert the cache entry, which could happen because
|
|
|
|
* of capacity reasons, or because it was already freed by the OS. If the
|
|
|
|
* cache entry isn't associated with a locked image, checking for SUCCESS or
|
|
|
|
* FAILURE is useless: the entry 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.
|
|
|
|
*
|
|
|
|
* @param aProvider The new cache entry to insert into the cache.
|
|
|
|
* @return SUCCESS if the cache entry was inserted successfully. (But see
|
2015-01-12 09:29:35 +03:00
|
|
|
* above for more information about when you should check this.)
|
2016-07-02 08:33:58 +03:00
|
|
|
* FAILURE if the cache entry could not be inserted, e.g. for capacity
|
2015-01-12 09:29:35 +03:00
|
|
|
* reasons. (But see above for more information about when you
|
|
|
|
* should check this.)
|
2016-07-02 08:33:58 +03:00
|
|
|
* FAILURE_ALREADY_PRESENT if an entry with the same ImageKey and
|
2015-01-12 09:29:35 +03:00
|
|
|
* SurfaceKey already exists in the cache.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
2016-08-20 06:26:59 +03:00
|
|
|
static InsertOutcome Insert(NotNull<ISurfaceProvider*> aProvider);
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2016-08-02 02:44:39 +03:00
|
|
|
/**
|
|
|
|
* Mark the cache entry @aProvider as having an available surface. This turns
|
|
|
|
* a placeholder cache entry into a normal cache entry. The cache entry
|
|
|
|
* becomes locked if the associated image is locked; otherwise, it starts in
|
|
|
|
* the unlocked state.
|
|
|
|
*
|
|
|
|
* If the cache entry containing @aProvider has already been evicted from the
|
|
|
|
* surface cache, this function has no effect.
|
|
|
|
*
|
|
|
|
* It's illegal to call this function if @aProvider is not a placeholder; by
|
|
|
|
* definition, non-placeholder ISurfaceProviders should have a surface
|
|
|
|
* available already.
|
|
|
|
*
|
|
|
|
* @param aProvider The cache entry that now has a surface available.
|
|
|
|
*/
|
2016-08-20 06:26:59 +03:00
|
|
|
static void SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider);
|
2016-08-02 02:44:39 +03:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04: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 08:19:30 +03: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 15:37:59 +04:00
|
|
|
*
|
|
|
|
* @return false if the surface cache can't hold a surface of that size.
|
|
|
|
*/
|
2015-05-06 08:19:30 +03:00
|
|
|
static bool CanHold(const IntSize& aSize, uint32_t aBytesPerPixel = 4);
|
2015-01-16 02:11:35 +03:00
|
|
|
static bool CanHold(size_t aSize);
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* Locks an image. Any of the image's cache entries which are either inserted
|
|
|
|
* or accessed while the image is locked will not expire.
|
2015-01-20 14:06:37 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* Locking an image does not automatically lock that image's existing cache
|
|
|
|
* entries. A call to LockImage() guarantees that entries which are inserted
|
2015-09-20 02:20:59 +03:00
|
|
|
* afterward will not expire before the next call to UnlockImage() or
|
2016-07-02 08:33:58 +03:00
|
|
|
* UnlockSurfaces() for that image. Cache entries 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 cache entries owned by the image may expire at any time.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
2016-07-02 07:59:00 +03:00
|
|
|
* All of an image's cache entries are removed by RemoveImage(), whether the
|
|
|
|
* image is locked or not.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* Unlocks an image, allowing any of its cache entries to expire at any time.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
|
|
|
* It's OK to call UnlockImage() on an image that's already unlocked; this has
|
|
|
|
* no effect.
|
|
|
|
*
|
2015-01-20 14:06:37 +03:00
|
|
|
* @param aImageKey The image to unlock.
|
2014-11-27 00:22:10 +03:00
|
|
|
*/
|
|
|
|
static void UnlockImage(const ImageKey aImageKey);
|
|
|
|
|
2015-01-20 14:06:37 +03:00
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* Unlocks the existing cache entries of an image, allowing them to expire at
|
|
|
|
* any time.
|
2015-01-20 14:06:37 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* This does not unlock the image itself, so accessing the cache entries via
|
2015-01-20 14:06:37 +03:00
|
|
|
* 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
|
2016-07-02 08:33:58 +03:00
|
|
|
* all of the cache entries owned by an image are needed. Calling
|
|
|
|
* UnlockSurfaces() and then taking some action that will cause Lookup() to
|
|
|
|
* touch any cache entries that are still useful will permit the remaining
|
|
|
|
* entries to expire from the cache.
|
2015-01-20 14:06:37 +03:00
|
|
|
*
|
|
|
|
* If the image is unlocked, this has no effect.
|
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* @param aImageKey The image which should have its existing cache entries
|
2015-01-20 14:06:37 +03:00
|
|
|
* unlocked.
|
|
|
|
*/
|
2016-07-02 08:33:58 +03:00
|
|
|
static void UnlockEntries(const ImageKey aImageKey);
|
2015-01-20 14:06:37 +03:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2016-08-20 01:04:34 +03:00
|
|
|
* Removes all cache entries (including placeholders) associated with the
|
2016-07-02 08:33:58 +03:00
|
|
|
* given image from the cache. If the image is locked, it is automatically
|
2015-07-20 04:39:44 +03:00
|
|
|
* unlocked.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
|
|
|
* This MUST be called, at a minimum, when an Image which could be storing
|
2016-07-02 08:33:58 +03:00
|
|
|
* entries in the surface cache is destroyed. If another image were allocated
|
2014-11-27 00:22:10 +03:00
|
|
|
* at the same address it could result in subtle, difficult-to-reproduce bugs.
|
2013-10-22 15:37:59 +04:00
|
|
|
*
|
|
|
|
* @param aImageKey The image which should be removed from the cache.
|
|
|
|
*/
|
2014-11-27 00:22:10 +03:00
|
|
|
static void RemoveImage(const ImageKey aImageKey);
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
/**
|
|
|
|
* Attempts to remove cache entries (including placeholders) associated with
|
|
|
|
* the given image from the cache, assuming there is an equivalent entry that
|
|
|
|
* it is able substitute that entry with. Note that this only applies if the
|
|
|
|
* image is in factor of 2 mode. If it is not, this operation does nothing.
|
|
|
|
*
|
|
|
|
* @param aImageKey The image whose cache which should be pruned.
|
|
|
|
*/
|
|
|
|
static void PruneImage(const ImageKey aImageKey);
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
/**
|
2016-07-02 08:33:58 +03:00
|
|
|
* Evicts all evictable entries from the cache.
|
2014-11-27 00:22:10 +03:00
|
|
|
*
|
2016-07-02 08:33:58 +03:00
|
|
|
* All entries are evictable except for entries associated with locked images.
|
2016-07-02 07:59:00 +03:00
|
|
|
* Non-evictable entries can only be removed by RemoveImage().
|
2014-02-08 23:37:39 +04:00
|
|
|
*/
|
|
|
|
static void DiscardAll();
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2015-04-28 21:46:17 +03: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-27 00:22:11 +03:00
|
|
|
*
|
|
|
|
* This is intended for use with memory reporting.
|
|
|
|
*
|
|
|
|
* @param aImageKey The image to report memory usage for.
|
2015-04-28 21:46:17 +03: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-27 00:22:11 +03:00
|
|
|
*/
|
2015-04-28 21:46:17 +03:00
|
|
|
static void CollectSizeOfSurfaces(const ImageKey aImageKey,
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf);
|
2014-11-27 00:22:11 +03:00
|
|
|
|
2016-07-11 09:48:17 +03:00
|
|
|
/**
|
|
|
|
* @return maximum capacity of the SurfaceCache in bytes. This is only exposed
|
|
|
|
* for use by tests; normal code should use CanHold() instead.
|
|
|
|
*/
|
|
|
|
static size_t MaximumCapacity();
|
|
|
|
|
2018-09-21 03:22:00 +03:00
|
|
|
/**
|
|
|
|
* @return true if the given size is valid.
|
|
|
|
*/
|
|
|
|
static bool IsLegalSize(const IntSize& aSize);
|
|
|
|
|
2019-04-11 22:30:48 +03:00
|
|
|
/**
|
|
|
|
* @return clamped size for the given vector image size to rasterize at.
|
|
|
|
*/
|
|
|
|
static IntSize ClampVectorSize(const IntSize& aSize);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return clamped size for the given image and size to rasterize at.
|
|
|
|
*/
|
|
|
|
static IntSize ClampSize(const ImageKey aImageKey, const IntSize& aSize);
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
private:
|
|
|
|
virtual ~SurfaceCache() = 0; // Forbid instantiation.
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_SurfaceCache_h
|