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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SurfaceCache is a service for caching temporary surfaces in imagelib.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SurfaceCache.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "ISurfaceProvider.h"
|
|
|
|
#include "Image.h"
|
|
|
|
#include "LookupResult.h"
|
|
|
|
#include "ShutdownTracker.h"
|
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "imgFrame.h"
|
2014-12-19 06:50:56 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-02-11 06:03:04 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2018-09-21 03:22:03 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-10-22 15:37:59 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-12-10 13:53:47 +03:00
|
|
|
#include "mozilla/Likely.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2017-01-31 15:41:14 +03:00
|
|
|
#include "mozilla/StaticMutex.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
2013-11-26 03:57:43 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-08-12 09:50:31 +03:00
|
|
|
#include "mozilla/Tuple.h"
|
2013-10-22 15:37:59 +04:00
|
|
|
#include "nsExpirationTracker.h"
|
|
|
|
#include "nsHashKeys.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "nsIMemoryReporter.h"
|
2013-10-22 15:37:59 +04:00
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
#include "nsTArray.h"
|
2021-01-04 17:20:22 +03:00
|
|
|
#include "Orientation.h"
|
2013-10-22 15:37:59 +04:00
|
|
|
#include "prsystem.h"
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-10 19:00:31 +04:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
namespace image {
|
|
|
|
|
2020-01-21 12:59:15 +03:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(SurfaceCacheMallocSizeOf)
|
|
|
|
|
2013-10-22 15:38:01 +04:00
|
|
|
class CachedSurface;
|
|
|
|
class SurfaceCacheImpl;
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-10-22 15:38:01 +04:00
|
|
|
// Static Data
|
2013-10-22 15:37:59 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-22 15:38:01 +04:00
|
|
|
// The single surface cache instance.
|
2013-11-26 03:57:43 +04:00
|
|
|
static StaticRefPtr<SurfaceCacheImpl> sInstance;
|
2013-10-22 15:38:01 +04:00
|
|
|
|
2017-01-31 15:41:14 +03:00
|
|
|
// The mutex protecting the surface cache.
|
|
|
|
static StaticMutex sInstanceMutex;
|
|
|
|
|
2013-10-22 15:38:01 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceCache Implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04: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;
|
|
|
|
|
2015-05-06 08:19:30 +03:00
|
|
|
static Cost ComputeCost(const IntSize& aSize, uint32_t aBytesPerPixel) {
|
|
|
|
MOZ_ASSERT(aBytesPerPixel == 1 || aBytesPerPixel == 4);
|
|
|
|
return aSize.width * aSize.height * aBytesPerPixel;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04: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:
|
2016-08-20 01:09:52 +03:00
|
|
|
CostEntry(NotNull<CachedSurface*> aSurface, Cost aCost)
|
2013-10-22 15:37:59 +04:00
|
|
|
: mSurface(aSurface), mCost(aCost) {}
|
|
|
|
|
2016-08-20 01:09:52 +03:00
|
|
|
NotNull<CachedSurface*> Surface() const { return mSurface; }
|
2013-10-22 15:37:59 +04:00
|
|
|
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 ||
|
2020-02-27 20:39:15 +03:00
|
|
|
(mCost == aOther.mCost && mSurface < aOther.mSurface);
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-20 01:09:52 +03:00
|
|
|
NotNull<CachedSurface*> mSurface;
|
|
|
|
Cost mCost;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04:00
|
|
|
* A CachedSurface associates a surface with a key that uniquely identifies that
|
|
|
|
* surface.
|
|
|
|
*/
|
2014-04-14 23:04:24 +04:00
|
|
|
class CachedSurface {
|
2015-03-24 23:16:00 +03:00
|
|
|
~CachedSurface() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
public:
|
2015-01-08 11:04:31 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(CachedSurface)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CachedSurface)
|
2014-04-14 23:04:24 +04:00
|
|
|
|
2016-08-20 06:26:59 +03:00
|
|
|
explicit CachedSurface(NotNull<ISurfaceProvider*> aProvider)
|
2016-07-02 08:20:32 +03:00
|
|
|
: mProvider(aProvider), mIsLocked(false) {}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2016-08-18 03:50:31 +03:00
|
|
|
DrawableSurface GetDrawableSurface() const {
|
2015-07-20 04:39:44 +03:00
|
|
|
if (MOZ_UNLIKELY(IsPlaceholder())) {
|
2016-08-18 03:50:31 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Called GetDrawableSurface() on a placeholder");
|
|
|
|
return DrawableSurface();
|
2015-07-20 04:39:44 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 03:50:31 +03:00
|
|
|
return mProvider->Surface();
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
void SetLocked(bool aLocked) {
|
2015-07-20 04:39:44 +03:00
|
|
|
if (IsPlaceholder()) {
|
|
|
|
return; // Can't lock a placeholder.
|
|
|
|
}
|
|
|
|
|
2016-08-27 02:04:15 +03:00
|
|
|
// Update both our state and our provider's state. Some surface providers
|
|
|
|
// are permanently locked; maintaining our own locking state enables us to
|
|
|
|
// respect SetLocked() even when it's meaningless from the provider's
|
|
|
|
// perspective.
|
|
|
|
mIsLocked = aLocked;
|
2016-07-02 08:20:32 +03:00
|
|
|
mProvider->SetLocked(aLocked);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2016-08-27 02:04:15 +03:00
|
|
|
bool IsLocked() const {
|
|
|
|
return !IsPlaceholder() && mIsLocked && mProvider->IsLocked();
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
void SetCannotSubstitute() {
|
|
|
|
mProvider->Availability().SetCannotSubstitute();
|
|
|
|
}
|
|
|
|
bool CannotSubstitute() const {
|
|
|
|
return mProvider->Availability().CannotSubstitute();
|
|
|
|
}
|
|
|
|
|
2016-08-20 01:07:47 +03:00
|
|
|
bool IsPlaceholder() const {
|
|
|
|
return mProvider->Availability().IsPlaceholder();
|
|
|
|
}
|
|
|
|
bool IsDecoded() const { return !IsPlaceholder() && mProvider->IsFinished(); }
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2016-08-20 06:26:59 +03:00
|
|
|
ImageKey GetImageKey() const { return mProvider->GetImageKey(); }
|
2017-11-07 16:10:01 +03:00
|
|
|
const SurfaceKey& GetSurfaceKey() const { return mProvider->GetSurfaceKey(); }
|
2013-10-22 15:37:59 +04:00
|
|
|
nsExpirationState* GetExpirationState() { return &mExpirationState; }
|
2015-07-20 04:39:44 +03:00
|
|
|
|
2016-08-20 06:26:59 +03:00
|
|
|
CostEntry GetCostEntry() {
|
|
|
|
return image::CostEntry(WrapNotNull(this), mProvider->LogicalSizeInBytes());
|
|
|
|
}
|
|
|
|
|
2020-01-21 12:59:15 +03:00
|
|
|
size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + aMallocSizeOf(mProvider.get());
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
// A helper type used by SurfaceCacheImpl::CollectSizeOfSurfaces.
|
|
|
|
struct MOZ_STACK_CLASS SurfaceMemoryReport {
|
|
|
|
SurfaceMemoryReport(nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf)
|
2014-11-27 00:22:11 +03:00
|
|
|
: mCounters(aCounters), mMallocSizeOf(aMallocSizeOf) {}
|
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
void Add(NotNull<CachedSurface*> aCachedSurface, bool aIsFactor2) {
|
2016-08-18 10:06:41 +03:00
|
|
|
if (aCachedSurface->IsPlaceholder()) {
|
|
|
|
return;
|
2016-07-27 02:31:26 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 10:06:41 +03:00
|
|
|
// Record the memory used by the ISurfaceProvider. This may not have a
|
|
|
|
// straightforward relationship to the size of the surface that
|
|
|
|
// DrawableRef() returns if the surface is generated dynamically. (i.e.,
|
|
|
|
// for surfaces with PlaybackType::eAnimated.)
|
2018-09-25 16:13:51 +03:00
|
|
|
aCachedSurface->mProvider->AddSizeOfExcludingThis(
|
|
|
|
mMallocSizeOf, [&](ISurfaceProvider::AddSizeOfCbData& aMetadata) {
|
2021-05-20 15:31:27 +03:00
|
|
|
SurfaceMemoryCounter counter(
|
|
|
|
aCachedSurface->GetSurfaceKey(), aMetadata.mSurface,
|
|
|
|
aCachedSurface->IsLocked(), aCachedSurface->CannotSubstitute(),
|
|
|
|
aIsFactor2, aMetadata.mFinished);
|
2020-02-06 01:22:13 +03:00
|
|
|
|
|
|
|
counter.Values().SetDecodedHeap(aMetadata.mHeapBytes);
|
|
|
|
counter.Values().SetDecodedNonHeap(aMetadata.mNonHeapBytes);
|
|
|
|
counter.Values().SetDecodedUnknown(aMetadata.mUnknownBytes);
|
|
|
|
counter.Values().SetExternalHandles(aMetadata.mExternalHandles);
|
|
|
|
counter.Values().SetFrameIndex(aMetadata.mIndex);
|
|
|
|
counter.Values().SetExternalId(aMetadata.mExternalId);
|
|
|
|
counter.Values().SetSurfaceTypes(aMetadata.mTypes);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
mCounters.AppendElement(counter);
|
|
|
|
});
|
2015-04-28 21:46:17 +03:00
|
|
|
}
|
2014-11-27 00:22:11 +03:00
|
|
|
|
|
|
|
private:
|
2015-04-28 21:46:17 +03:00
|
|
|
nsTArray<SurfaceMemoryCounter>& mCounters;
|
|
|
|
MallocSizeOf mMallocSizeOf;
|
2014-11-27 00:22:11 +03:00
|
|
|
};
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
private:
|
2016-08-20 01:07:47 +03:00
|
|
|
nsExpirationState mExpirationState;
|
|
|
|
NotNull<RefPtr<ISurfaceProvider>> mProvider;
|
2016-08-27 02:04:15 +03:00
|
|
|
bool mIsLocked;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2016-02-27 02:13:59 +03:00
|
|
|
static int64_t AreaOfIntSize(const IntSize& aSize) {
|
|
|
|
return static_cast<int64_t>(aSize.width) * static_cast<int64_t>(aSize.height);
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04: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-27 00:22:10 +03:00
|
|
|
*
|
|
|
|
* ImageSurfaceCache also keeps track of whether its associated image is locked
|
|
|
|
* or unlocked.
|
2017-09-05 14:58:45 +03:00
|
|
|
*
|
|
|
|
* The cache may also enter "factor of 2" mode which occurs when the number of
|
|
|
|
* surfaces in the cache exceeds the "image.cache.factor2.threshold-surfaces"
|
|
|
|
* pref plus the number of native sizes of the image. When in "factor of 2"
|
|
|
|
* mode, the cache will strongly favour sizes which are a factor of 2 of the
|
|
|
|
* largest native size. It accomplishes this by suggesting a factor of 2 size
|
|
|
|
* when lookups fail and substituting the nearest factor of 2 surface to the
|
2019-09-12 12:00:52 +03:00
|
|
|
* ideal size as the "best" available (as opposed to substitution but not
|
|
|
|
* found). This allows us to minimize memory consumption and CPU time spent
|
|
|
|
* decoding when a website requires many variants of the same surface.
|
2013-10-22 15:37:59 +04:00
|
|
|
*/
|
2014-04-14 23:04:24 +04:00
|
|
|
class ImageSurfaceCache {
|
2014-11-27 00:22:10 +03:00
|
|
|
~ImageSurfaceCache() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
public:
|
2018-09-21 03:22:03 +03:00
|
|
|
explicit ImageSurfaceCache(const ImageKey aImageKey)
|
2017-09-05 14:58:45 +03:00
|
|
|
: mLocked(false),
|
|
|
|
mFactor2Mode(false),
|
2017-09-05 14:58:46 +03:00
|
|
|
mFactor2Pruned(false),
|
2018-09-21 03:22:03 +03:00
|
|
|
mIsVectorImage(aImageKey->GetType() == imgIContainer::TYPE_VECTOR) {}
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2015-01-08 11:04:31 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(ImageSurfaceCache)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageSurfaceCache)
|
2014-04-14 23:04:24 +04:00
|
|
|
|
2015-03-24 23:16:00 +03:00
|
|
|
typedef nsRefPtrHashtable<nsGenericHashKey<SurfaceKey>, CachedSurface>
|
|
|
|
SurfaceTable;
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
auto Values() const { return mSurfaces.Values(); }
|
|
|
|
uint32_t Count() const { return mSurfaces.Count(); }
|
2013-10-22 15:37:59 +04:00
|
|
|
bool IsEmpty() const { return mSurfaces.Count() == 0; }
|
2015-03-24 23:16:00 +03:00
|
|
|
|
2020-01-21 12:59:15 +03:00
|
|
|
size_t ShallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t bytes = aMallocSizeOf(this) +
|
|
|
|
mSurfaces.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& value : Values()) {
|
|
|
|
bytes += value->ShallowSizeOfIncludingThis(aMallocSizeOf);
|
2020-01-21 12:59:15 +03:00
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2020-03-30 08:34:51 +03:00
|
|
|
[[nodiscard]] bool Insert(NotNull<CachedSurface*> aSurface) {
|
2015-09-20 02:20:59 +03:00
|
|
|
MOZ_ASSERT(!mLocked || aSurface->IsPlaceholder() || aSurface->IsLocked(),
|
|
|
|
"Inserting an unlocked surface for a locked image");
|
2021-02-26 12:11:46 +03:00
|
|
|
return mSurfaces.InsertOrUpdate(aSurface->GetSurfaceKey(),
|
|
|
|
RefPtr<CachedSurface>{aSurface}, fallible);
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
already_AddRefed<CachedSurface> Remove(NotNull<CachedSurface*> aSurface) {
|
2013-10-22 15:37:59 +04:00
|
|
|
MOZ_ASSERT(mSurfaces.GetWeak(aSurface->GetSurfaceKey()),
|
|
|
|
"Should not be removing a surface we don't have");
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
RefPtr<CachedSurface> surface;
|
|
|
|
mSurfaces.Remove(aSurface->GetSurfaceKey(), getter_AddRefs(surface));
|
2017-09-21 23:56:38 +03:00
|
|
|
AfterMaybeRemove();
|
2017-08-15 22:02:14 +03:00
|
|
|
return surface.forget();
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
already_AddRefed<CachedSurface> Lookup(const SurfaceKey& aSurfaceKey,
|
|
|
|
bool aForAccess) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CachedSurface> surface;
|
2013-10-22 15:37:59 +04:00
|
|
|
mSurfaces.Get(aSurfaceKey, getter_AddRefs(surface));
|
2017-09-05 14:58:45 +03:00
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
if (aForAccess) {
|
|
|
|
if (surface) {
|
|
|
|
// We don't want to allow factor of 2 mode pruning to release surfaces
|
|
|
|
// for which the callers will accept no substitute.
|
|
|
|
surface->SetCannotSubstitute();
|
|
|
|
} else if (!mFactor2Mode) {
|
|
|
|
// If no exact match is found, and this is for use rather than internal
|
|
|
|
// accounting (i.e. insert and removal), we know this will trigger a
|
|
|
|
// decode. Make sure we switch now to factor of 2 mode if necessary.
|
|
|
|
MaybeSetFactor2Mode();
|
|
|
|
}
|
2017-09-05 14:58:45 +03:00
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
/**
|
|
|
|
* @returns A tuple containing the best matching CachedSurface if available,
|
|
|
|
* a MatchType describing how the CachedSurface was selected, and
|
|
|
|
* an IntSize which is the size the caller should choose to decode
|
|
|
|
* at should it attempt to do so.
|
|
|
|
*/
|
|
|
|
Tuple<already_AddRefed<CachedSurface>, MatchType, IntSize> LookupBestMatch(
|
2016-02-29 21:20:50 +03:00
|
|
|
const SurfaceKey& aIdealKey) {
|
2015-07-20 04:39:44 +03:00
|
|
|
// Try for an exact match first.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CachedSurface> exactMatch;
|
2016-02-29 21:20:50 +03:00
|
|
|
mSurfaces.Get(aIdealKey, getter_AddRefs(exactMatch));
|
2017-09-05 14:58:45 +03:00
|
|
|
if (exactMatch) {
|
|
|
|
if (exactMatch->IsDecoded()) {
|
|
|
|
return MakeTuple(exactMatch.forget(), MatchType::EXACT, IntSize());
|
|
|
|
}
|
|
|
|
} else if (!mFactor2Mode) {
|
|
|
|
// If no exact match is found, and we are not in factor of 2 mode, then
|
|
|
|
// we know that we will trigger a decode because at best we will provide
|
|
|
|
// a substitute. Make sure we switch now to factor of 2 mode if necessary.
|
|
|
|
MaybeSetFactor2Mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try for a best match second, if using compact.
|
|
|
|
IntSize suggestedSize = SuggestedSize(aIdealKey.Size());
|
2018-09-21 03:22:03 +03:00
|
|
|
if (suggestedSize != aIdealKey.Size()) {
|
2017-09-05 14:58:45 +03:00
|
|
|
if (!exactMatch) {
|
|
|
|
SurfaceKey compactKey = aIdealKey.CloneWithSize(suggestedSize);
|
|
|
|
mSurfaces.Get(compactKey, getter_AddRefs(exactMatch));
|
|
|
|
if (exactMatch && exactMatch->IsDecoded()) {
|
2017-11-17 14:45:28 +03:00
|
|
|
MOZ_ASSERT(suggestedSize != aIdealKey.Size());
|
2017-09-05 14:58:45 +03:00
|
|
|
return MakeTuple(exactMatch.forget(),
|
|
|
|
MatchType::SUBSTITUTE_BECAUSE_BEST, suggestedSize);
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's no perfect match, so find the best match we can.
|
2015-11-11 10:07:09 +03:00
|
|
|
RefPtr<CachedSurface> bestMatch;
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& value : Values()) {
|
|
|
|
NotNull<CachedSurface*> current = WrapNotNull(value);
|
2016-02-29 21:20:50 +03:00
|
|
|
const SurfaceKey& currentKey = current->GetSurfaceKey();
|
2015-11-11 10:07:09 +03:00
|
|
|
|
|
|
|
// We never match a placeholder.
|
2016-02-29 21:20:50 +03:00
|
|
|
if (current->IsPlaceholder()) {
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-18 10:06:41 +03:00
|
|
|
// Matching the playback type and SVG context is required.
|
|
|
|
if (currentKey.Playback() != aIdealKey.Playback() ||
|
2016-02-29 21:20:50 +03:00
|
|
|
currentKey.SVGContext() != aIdealKey.SVGContext()) {
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Matching the flags is required.
|
2016-02-29 21:20:50 +03:00
|
|
|
if (currentKey.Flags() != aIdealKey.Flags()) {
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Anything is better than nothing! (Within the constraints we just
|
|
|
|
// checked, of course.)
|
|
|
|
if (!bestMatch) {
|
2016-02-29 21:20:50 +03:00
|
|
|
bestMatch = current;
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(bestMatch, "Should have a current best match");
|
|
|
|
|
|
|
|
// Always prefer completely decoded surfaces.
|
|
|
|
bool bestMatchIsDecoded = bestMatch->IsDecoded();
|
2016-02-29 21:20:50 +03:00
|
|
|
if (bestMatchIsDecoded && !current->IsDecoded()) {
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-29 21:20:50 +03:00
|
|
|
if (!bestMatchIsDecoded && current->IsDecoded()) {
|
|
|
|
bestMatch = current;
|
2015-11-11 10:07:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceKey bestMatchKey = bestMatch->GetSurfaceKey();
|
2017-09-05 14:58:45 +03:00
|
|
|
if (CompareArea(aIdealKey.Size(), bestMatchKey.Size(),
|
|
|
|
currentKey.Size())) {
|
2016-02-29 21:20:50 +03:00
|
|
|
bestMatch = current;
|
2015-11-11 10:07:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-20 04:39:44 +03:00
|
|
|
|
|
|
|
MatchType matchType;
|
2015-11-11 10:07:09 +03:00
|
|
|
if (bestMatch) {
|
2015-07-20 04:39:44 +03:00
|
|
|
if (!exactMatch) {
|
2017-11-17 14:45:28 +03:00
|
|
|
// No exact match, neither ideal nor factor of 2.
|
|
|
|
MOZ_ASSERT(suggestedSize != bestMatch->GetSurfaceKey().Size(),
|
|
|
|
"No exact match despite the fact the sizes match!");
|
|
|
|
matchType = MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND;
|
2015-11-11 10:07:09 +03:00
|
|
|
} else if (exactMatch != bestMatch) {
|
2015-07-20 04:39:44 +03:00
|
|
|
// The exact match is still decoding, but we found a substitute.
|
|
|
|
matchType = MatchType::SUBSTITUTE_BECAUSE_PENDING;
|
2017-11-17 14:45:28 +03:00
|
|
|
} else if (aIdealKey.Size() != bestMatch->GetSurfaceKey().Size()) {
|
|
|
|
// The best factor of 2 match is still decoding, but the best we've got.
|
|
|
|
MOZ_ASSERT(suggestedSize != aIdealKey.Size());
|
2018-09-21 03:22:03 +03:00
|
|
|
MOZ_ASSERT(mFactor2Mode || mIsVectorImage);
|
2017-11-17 14:45:28 +03:00
|
|
|
matchType = MatchType::SUBSTITUTE_BECAUSE_BEST;
|
2015-07-20 04:39:44 +03:00
|
|
|
} else {
|
2017-11-17 14:45:28 +03:00
|
|
|
// The exact match is still decoding, but it's the best we've got.
|
|
|
|
matchType = MatchType::EXACT;
|
2015-07-20 04:39:44 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (exactMatch) {
|
2016-08-20 01:04:34 +03:00
|
|
|
// We found an "exact match"; it must have been a placeholder.
|
2015-07-20 04:39:44 +03:00
|
|
|
MOZ_ASSERT(exactMatch->IsPlaceholder());
|
|
|
|
matchType = MatchType::PENDING;
|
|
|
|
} else {
|
|
|
|
// We couldn't find an exact match *or* a substitute.
|
|
|
|
matchType = MatchType::NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
return MakeTuple(bestMatch.forget(), matchType, suggestedSize);
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
void MaybeSetFactor2Mode() {
|
|
|
|
MOZ_ASSERT(!mFactor2Mode);
|
|
|
|
|
|
|
|
// Typically an image cache will not have too many size-varying surfaces, so
|
|
|
|
// if we exceed the given threshold, we should consider using a subset.
|
2019-05-26 17:29:42 +03:00
|
|
|
int32_t thresholdSurfaces =
|
2019-06-26 06:24:21 +03:00
|
|
|
StaticPrefs::image_cache_factor2_threshold_surfaces();
|
2017-09-05 14:58:45 +03:00
|
|
|
if (thresholdSurfaces < 0 ||
|
|
|
|
mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:22:03 +03:00
|
|
|
// Determine how many native surfaces this image has. If it is zero, and it
|
|
|
|
// is a vector image, then we should impute a single native size. Otherwise,
|
|
|
|
// it may be zero because we don't know yet, or the image has an error, or
|
|
|
|
// it isn't supported.
|
2021-03-24 20:56:49 +03:00
|
|
|
NotNull<CachedSurface*> current =
|
|
|
|
WrapNotNull(mSurfaces.ConstIter().UserData());
|
2017-09-05 14:58:45 +03:00
|
|
|
Image* image = static_cast<Image*>(current->GetImageKey());
|
|
|
|
size_t nativeSizes = image->GetNativeSizesLength();
|
2018-09-21 03:22:03 +03:00
|
|
|
if (mIsVectorImage) {
|
|
|
|
MOZ_ASSERT(nativeSizes == 0);
|
|
|
|
nativeSizes = 1;
|
|
|
|
} else if (nativeSizes == 0) {
|
2017-09-05 14:58:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the threshold by the number of native sizes. This ensures that
|
|
|
|
// we do not prevent decoding of the image at all its native sizes. It does
|
|
|
|
// not guarantee we will provide a surface at that size however (i.e. many
|
|
|
|
// other sized surfaces are requested, in addition to the native sizes).
|
|
|
|
thresholdSurfaces += nativeSizes;
|
|
|
|
if (mSurfaces.Count() <= static_cast<uint32_t>(thresholdSurfaces)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
// Get our native size. While we know the image should be fully decoded,
|
|
|
|
// if it is an SVG, it is valid to have a zero size. We can't do compacting
|
|
|
|
// in that case because we need to know the width/height ratio to define a
|
|
|
|
// candidate set.
|
|
|
|
IntSize nativeSize;
|
|
|
|
if (NS_FAILED(image->GetWidth(&nativeSize.width)) ||
|
|
|
|
NS_FAILED(image->GetHeight(&nativeSize.height)) ||
|
|
|
|
nativeSize.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a valid size, we can change modes.
|
2017-09-05 14:58:45 +03:00
|
|
|
mFactor2Mode = true;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
template <typename Function>
|
|
|
|
void Prune(Function&& aRemoveCallback) {
|
|
|
|
if (!mFactor2Mode || mFactor2Pruned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to discard any surfaces which are not factor of 2 and the best
|
|
|
|
// factor of 2 match exists.
|
|
|
|
bool hasNotFactorSize = false;
|
|
|
|
for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
NotNull<CachedSurface*> current = WrapNotNull(iter.UserData());
|
|
|
|
const SurfaceKey& currentKey = current->GetSurfaceKey();
|
|
|
|
const IntSize& currentSize = currentKey.Size();
|
|
|
|
|
|
|
|
// First we check if someone requested this size and would not accept
|
|
|
|
// an alternatively sized surface.
|
|
|
|
if (current->CannotSubstitute()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next we find the best factor of 2 size for this surface. If this
|
|
|
|
// surface is a factor of 2 size, then we want to keep it.
|
|
|
|
IntSize bestSize = SuggestedSize(currentSize);
|
|
|
|
if (bestSize == currentSize) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the cache for a surface with the same parameters except for the
|
|
|
|
// size which uses the closest factor of 2 size.
|
|
|
|
SurfaceKey compactKey = currentKey.CloneWithSize(bestSize);
|
|
|
|
RefPtr<CachedSurface> compactMatch;
|
|
|
|
mSurfaces.Get(compactKey, getter_AddRefs(compactMatch));
|
|
|
|
if (compactMatch && compactMatch->IsDecoded()) {
|
|
|
|
aRemoveCallback(current);
|
|
|
|
iter.Remove();
|
|
|
|
} else {
|
|
|
|
hasNotFactorSize = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have no surfaces that are not factor of 2 sized, so we can stop
|
|
|
|
// pruning henceforth, because we avoid the insertion of new surfaces that
|
|
|
|
// don't match our sizing set (unless the caller won't accept a
|
|
|
|
// substitution.)
|
|
|
|
if (!hasNotFactorSize) {
|
|
|
|
mFactor2Pruned = true;
|
|
|
|
}
|
2017-09-21 23:56:38 +03:00
|
|
|
|
|
|
|
// We should never leave factor of 2 mode due to pruning in of itself, but
|
|
|
|
// if we discarded surfaces due to the volatile buffers getting released,
|
|
|
|
// it is possible.
|
|
|
|
AfterMaybeRemove();
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
IntSize SuggestedSize(const IntSize& aSize) const {
|
2018-09-21 03:22:03 +03:00
|
|
|
IntSize suggestedSize = SuggestedSizeInternal(aSize);
|
|
|
|
if (mIsVectorImage) {
|
2019-04-11 22:30:48 +03:00
|
|
|
suggestedSize = SurfaceCache::ClampVectorSize(suggestedSize);
|
2018-09-21 03:22:03 +03:00
|
|
|
}
|
|
|
|
return suggestedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize SuggestedSizeInternal(const IntSize& aSize) const {
|
2017-09-05 14:58:45 +03:00
|
|
|
// When not in factor of 2 mode, we can always decode at the given size.
|
|
|
|
if (!mFactor2Mode) {
|
|
|
|
return aSize;
|
|
|
|
}
|
|
|
|
|
2017-09-07 06:16:31 +03:00
|
|
|
// We cannot enter factor of 2 mode unless we have a minimum number of
|
|
|
|
// surfaces, and we should have left it if the cache was emptied.
|
2017-09-21 23:56:38 +03:00
|
|
|
if (MOZ_UNLIKELY(IsEmpty())) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should not be empty and in factor of 2 mode!");
|
|
|
|
return aSize;
|
|
|
|
}
|
2017-09-05 14:58:45 +03:00
|
|
|
|
|
|
|
// This bit of awkwardness gets the largest native size of the image.
|
2021-03-24 20:56:49 +03:00
|
|
|
NotNull<CachedSurface*> firstSurface =
|
|
|
|
WrapNotNull(mSurfaces.ConstIter().UserData());
|
2017-09-05 14:58:45 +03:00
|
|
|
Image* image = static_cast<Image*>(firstSurface->GetImageKey());
|
|
|
|
IntSize factorSize;
|
|
|
|
if (NS_FAILED(image->GetWidth(&factorSize.width)) ||
|
|
|
|
NS_FAILED(image->GetHeight(&factorSize.height)) ||
|
|
|
|
factorSize.IsEmpty()) {
|
|
|
|
// We should not have entered factor of 2 mode without a valid size, and
|
2018-09-21 03:22:03 +03:00
|
|
|
// several successfully decoded surfaces. Note that valid vector images
|
|
|
|
// may have a default size of 0x0, and those are not yet supported.
|
2017-09-05 14:58:45 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Expected valid native size!");
|
|
|
|
return aSize;
|
|
|
|
}
|
2021-04-11 20:39:12 +03:00
|
|
|
|
|
|
|
if (image->GetOrientation().SwapsWidthAndHeight()) {
|
2020-04-17 05:57:34 +03:00
|
|
|
std::swap(factorSize.width, factorSize.height);
|
|
|
|
}
|
2017-09-05 14:58:45 +03:00
|
|
|
|
2018-09-21 03:22:03 +03:00
|
|
|
if (mIsVectorImage) {
|
|
|
|
// Ensure the aspect ratio matches the native size before forcing the
|
|
|
|
// caller to accept a factor of 2 size. The difference between the aspect
|
|
|
|
// ratios is:
|
|
|
|
//
|
|
|
|
// delta = nativeWidth/nativeHeight - desiredWidth/desiredHeight
|
|
|
|
//
|
|
|
|
// delta*nativeHeight*desiredHeight = nativeWidth*desiredHeight
|
|
|
|
// - desiredWidth*nativeHeight
|
|
|
|
//
|
|
|
|
// Using the maximum accepted delta as a constant, we can avoid the
|
|
|
|
// floating point division and just compare after some integer ops.
|
|
|
|
int32_t delta =
|
|
|
|
factorSize.width * aSize.height - aSize.width * factorSize.height;
|
|
|
|
int32_t maxDelta = (factorSize.height * aSize.height) >> 4;
|
|
|
|
if (delta > maxDelta || delta < -maxDelta) {
|
|
|
|
return aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the requested size is bigger than the native size, we actually need
|
|
|
|
// to grow the native size instead of shrinking it.
|
|
|
|
if (factorSize.width < aSize.width) {
|
|
|
|
do {
|
|
|
|
IntSize candidate(factorSize.width * 2, factorSize.height * 2);
|
|
|
|
if (!SurfaceCache::IsLegalSize(candidate)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
factorSize = candidate;
|
|
|
|
} while (factorSize.width < aSize.width);
|
|
|
|
|
|
|
|
return factorSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we can find the best fit as normal.
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
// Start with the native size as the best first guess.
|
|
|
|
IntSize bestSize = factorSize;
|
|
|
|
factorSize.width /= 2;
|
|
|
|
factorSize.height /= 2;
|
|
|
|
|
|
|
|
while (!factorSize.IsEmpty()) {
|
|
|
|
if (!CompareArea(aSize, bestSize, factorSize)) {
|
|
|
|
// This size is not better than the last. Since we proceed from largest
|
|
|
|
// to smallest, we know that the next size will not be better if the
|
|
|
|
// previous size was rejected. Break early.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current factor of 2 size is better than the last selected size.
|
|
|
|
bestSize = factorSize;
|
|
|
|
factorSize.width /= 2;
|
|
|
|
factorSize.height /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestSize;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
bool CompareArea(const IntSize& aIdealSize, const IntSize& aBestSize,
|
|
|
|
const IntSize& aSize) const {
|
|
|
|
// 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 = AreaOfIntSize(aIdealSize);
|
|
|
|
int64_t currentArea = AreaOfIntSize(aSize);
|
|
|
|
int64_t bestMatchArea = AreaOfIntSize(aBestSize);
|
|
|
|
|
|
|
|
// If the best match is smaller than the ideal size, prefer bigger sizes.
|
|
|
|
if (bestMatchArea < idealArea) {
|
|
|
|
if (currentArea > bestMatchArea) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other, prefer sizes closer to the ideal size, but still not smaller.
|
|
|
|
if (idealArea <= currentArea && currentArea < bestMatchArea) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This surface isn't an improvement over the current best match.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:19:48 +03:00
|
|
|
template <typename Function>
|
2017-09-05 14:58:46 +03:00
|
|
|
void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
|
2017-09-19 15:19:48 +03:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
Function&& aRemoveCallback) {
|
2017-09-05 14:58:46 +03:00
|
|
|
CachedSurface::SurfaceMemoryReport report(aCounters, aMallocSizeOf);
|
2017-09-19 15:19:48 +03:00
|
|
|
for (auto iter = mSurfaces.Iter(); !iter.Done(); iter.Next()) {
|
2017-09-05 14:58:46 +03:00
|
|
|
NotNull<CachedSurface*> surface = WrapNotNull(iter.UserData());
|
2017-09-19 15:19:48 +03:00
|
|
|
|
|
|
|
// We don't need the drawable surface for ourselves, but adding a surface
|
|
|
|
// to the report will trigger this indirectly. If the surface was
|
|
|
|
// discarded by the OS because it was in volatile memory, we should remove
|
|
|
|
// it from the cache immediately rather than include it in the report.
|
|
|
|
DrawableSurface drawableSurface;
|
|
|
|
if (!surface->IsPlaceholder()) {
|
|
|
|
drawableSurface = surface->GetDrawableSurface();
|
|
|
|
if (!drawableSurface) {
|
|
|
|
aRemoveCallback(surface);
|
|
|
|
iter.Remove();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
const IntSize& size = surface->GetSurfaceKey().Size();
|
|
|
|
bool factor2Size = false;
|
|
|
|
if (mFactor2Mode) {
|
|
|
|
factor2Size = (size == SuggestedSize(size));
|
|
|
|
}
|
|
|
|
report.Add(surface, factor2Size);
|
|
|
|
}
|
2017-09-21 23:56:38 +03:00
|
|
|
|
|
|
|
AfterMaybeRemove();
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
void SetLocked(bool aLocked) { mLocked = aLocked; }
|
|
|
|
bool IsLocked() const { return mLocked; }
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
private:
|
2017-09-21 23:56:38 +03:00
|
|
|
void AfterMaybeRemove() {
|
|
|
|
if (IsEmpty() && mFactor2Mode) {
|
|
|
|
// The last surface for this cache was removed. This can happen if the
|
|
|
|
// surface was stored in a volatile buffer and got purged, or the surface
|
|
|
|
// expired from the cache. If the cache itself lingers for some reason
|
|
|
|
// (e.g. in the process of performing a lookup, the cache itself is
|
|
|
|
// locked), then we need to reset the factor of 2 state because it
|
|
|
|
// requires at least one surface present to get the native size
|
|
|
|
// information from the image.
|
|
|
|
mFactor2Mode = mFactor2Pruned = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
SurfaceTable mSurfaces;
|
|
|
|
|
|
|
|
bool mLocked;
|
|
|
|
|
|
|
|
// True in "factor of 2" mode.
|
|
|
|
bool mFactor2Mode;
|
2017-09-05 14:58:46 +03:00
|
|
|
|
|
|
|
// True if all non-factor of 2 surfaces have been removed from the cache. Note
|
|
|
|
// that this excludes unsubstitutable sizes.
|
|
|
|
bool mFactor2Pruned;
|
2018-09-21 03:22:03 +03:00
|
|
|
|
|
|
|
// True if the surfaces are produced from a vector image. If so, it must match
|
|
|
|
// the aspect ratio when using factor of 2 mode.
|
|
|
|
bool mIsVectorImage;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
/**
|
2013-10-22 15:37:59 +04: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.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class SurfaceCacheImpl final : public nsIMemoryReporter {
|
2013-10-22 15:37:59 +04:00
|
|
|
public:
|
2013-11-26 03:57:43 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
SurfaceCacheImpl(uint32_t aSurfaceCacheExpirationTimeMS,
|
2014-11-27 00:22:10 +03:00
|
|
|
uint32_t aSurfaceCacheDiscardFactor,
|
2013-10-22 15:37:59 +04:00
|
|
|
uint32_t aSurfaceCacheSize)
|
2015-02-01 02:29:48 +03:00
|
|
|
: mExpirationTracker(aSurfaceCacheExpirationTimeMS),
|
2013-10-22 15:38:01 +04:00
|
|
|
mMemoryPressureObserver(new MemoryPressureObserver),
|
2014-11-27 00:22:10 +03:00
|
|
|
mDiscardFactor(aSurfaceCacheDiscardFactor),
|
2013-10-22 15:37:59 +04:00
|
|
|
mMaxCost(aSurfaceCacheSize),
|
|
|
|
mAvailableCost(aSurfaceCacheSize),
|
2014-11-27 00:22:10 +03:00
|
|
|
mLockedCost(0),
|
2020-01-21 12:59:15 +03:00
|
|
|
mOverflowCount(0),
|
|
|
|
mAlreadyPresentCount(0),
|
|
|
|
mTableFailureCount(0),
|
|
|
|
mTrackingFailureCount(0) {
|
2014-07-10 19:00:31 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2015-03-24 23:16:00 +03:00
|
|
|
if (os) {
|
2013-10-22 15:38:01 +04:00
|
|
|
os->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2013-10-22 15:38:01 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
private:
|
2013-11-26 03:57:43 +04:00
|
|
|
virtual ~SurfaceCacheImpl() {
|
2014-07-10 19:00:31 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2015-03-24 23:16:00 +03:00
|
|
|
if (os) {
|
2013-10-22 15:38:01 +04:00
|
|
|
os->RemoveObserver(mMemoryPressureObserver, "memory-pressure");
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2013-10-22 15:38:01 +04:00
|
|
|
|
2013-11-07 09:35:30 +04:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
public:
|
2015-01-08 11:04:31 +03:00
|
|
|
void InitMemoryReporter() { RegisterWeakMemoryReporter(this); }
|
|
|
|
|
2016-08-20 01:07:47 +03:00
|
|
|
InsertOutcome Insert(NotNull<ISurfaceProvider*> aProvider, bool aSetAvailable,
|
2017-03-28 04:47:23 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-01-12 09:29:35 +03:00
|
|
|
// If this is a duplicate surface, refuse to replace the original.
|
2016-07-02 08:33:58 +03:00
|
|
|
// XXX(seth): Calling Lookup() and then RemoveEntry() does the lookup
|
2015-07-20 04:39:44 +03:00
|
|
|
// twice. We'll make this more efficient in bug 1185137.
|
2016-08-20 06:26:59 +03:00
|
|
|
LookupResult result =
|
|
|
|
Lookup(aProvider->GetImageKey(), aProvider->GetSurfaceKey(), aAutoLock,
|
|
|
|
/* aMarkUsed = */ false);
|
2015-07-20 04:39:44 +03:00
|
|
|
if (MOZ_UNLIKELY(result)) {
|
2020-01-21 12:59:15 +03:00
|
|
|
mAlreadyPresentCount++;
|
2015-01-12 09:29:35 +03:00
|
|
|
return InsertOutcome::FAILURE_ALREADY_PRESENT;
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2015-07-20 04:39:44 +03:00
|
|
|
if (result.Type() == MatchType::PENDING) {
|
2017-03-28 04:47:23 +03:00
|
|
|
RemoveEntry(aProvider->GetImageKey(), aProvider->GetSurfaceKey(),
|
|
|
|
aAutoLock);
|
2015-07-20 04:39:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(result.Type() == MatchType::NOT_FOUND ||
|
|
|
|
result.Type() == MatchType::PENDING,
|
|
|
|
"A LookupResult with no surface should be NOT_FOUND or PENDING");
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
// If this is bigger than we can hold after discarding everything we can,
|
|
|
|
// refuse to cache it.
|
2016-08-20 06:26:59 +03:00
|
|
|
Cost cost = aProvider->LogicalSizeInBytes();
|
|
|
|
if (MOZ_UNLIKELY(!CanHoldAfterDiscarding(cost))) {
|
2015-06-03 09:30:11 +03:00
|
|
|
mOverflowCount++;
|
2015-01-12 09:29:35 +03:00
|
|
|
return InsertOutcome::FAILURE;
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:10 +03: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.
|
2016-08-20 06:26:59 +03:00
|
|
|
while (cost > mAvailableCost) {
|
2015-03-24 23:16:00 +03:00
|
|
|
MOZ_ASSERT(!mCosts.IsEmpty(),
|
|
|
|
"Removed everything and it still won't fit");
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
|
|
|
|
aAutoLock);
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locate the appropriate per-image cache. If there's not an existing cache
|
|
|
|
// for this image, create it.
|
2018-09-21 03:22:03 +03:00
|
|
|
const ImageKey imageKey = aProvider->GetImageKey();
|
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
|
2013-10-22 15:37:59 +04:00
|
|
|
if (!cache) {
|
2018-09-21 03:22:03 +03:00
|
|
|
cache = new ImageSurfaceCache(imageKey);
|
2021-02-26 12:11:46 +03:00
|
|
|
if (!mImageCaches.InsertOrUpdate(aProvider->GetImageKey(), RefPtr{cache},
|
|
|
|
fallible)) {
|
2020-01-21 12:59:15 +03:00
|
|
|
mTableFailureCount++;
|
|
|
|
return InsertOutcome::FAILURE;
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2016-08-02 02:44:39 +03:00
|
|
|
// If we were asked to mark the cache entry available, do so.
|
|
|
|
if (aSetAvailable) {
|
|
|
|
aProvider->Availability().SetAvailable();
|
|
|
|
}
|
|
|
|
|
2017-10-20 10:25:33 +03:00
|
|
|
auto surface = MakeNotNull<RefPtr<CachedSurface>>(aProvider);
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2015-09-20 02:20:59 +03:00
|
|
|
// We require that locking succeed if the image is locked and we're not
|
|
|
|
// inserting a placeholder; the caller may need to know this to handle
|
|
|
|
// errors correctly.
|
2017-08-15 04:35:43 +03:00
|
|
|
bool mustLock = cache->IsLocked() && !surface->IsPlaceholder();
|
|
|
|
if (mustLock) {
|
2014-11-27 00:22:10 +03:00
|
|
|
surface->SetLocked(true);
|
|
|
|
if (!surface->IsLocked()) {
|
2015-01-12 09:29:35 +03:00
|
|
|
return InsertOutcome::FAILURE;
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
// Insert.
|
2016-08-20 06:26:59 +03:00
|
|
|
MOZ_ASSERT(cost <= mAvailableCost, "Inserting despite too large a cost");
|
2017-08-15 04:35:43 +03:00
|
|
|
if (!cache->Insert(surface)) {
|
2020-01-21 12:59:15 +03:00
|
|
|
mTableFailureCount++;
|
2017-08-15 04:35:43 +03:00
|
|
|
if (mustLock) {
|
|
|
|
surface->SetLocked(false);
|
|
|
|
}
|
|
|
|
return InsertOutcome::FAILURE;
|
|
|
|
}
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
|
|
|
|
MOZ_ASSERT(!mustLock);
|
|
|
|
Remove(surface, /* aStopTracking */ false, aAutoLock);
|
|
|
|
return InsertOutcome::FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:29:35 +03:00
|
|
|
return InsertOutcome::SUCCESS;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:16:31 +03:00
|
|
|
void Remove(NotNull<CachedSurface*> aSurface, bool aStopTracking,
|
2017-03-28 04:47:23 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2014-11-28 06:55:57 +03:00
|
|
|
ImageKey imageKey = aSurface->GetImageKey();
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(imageKey);
|
2013-10-22 15:37:59 +04:00
|
|
|
MOZ_ASSERT(cache, "Shouldn't try to remove a surface with no image cache");
|
|
|
|
|
2015-09-20 02:20:59 +03:00
|
|
|
// If the surface was not a placeholder, tell its image that we discarded
|
|
|
|
// it.
|
|
|
|
if (!aSurface->IsPlaceholder()) {
|
2017-03-16 11:06:06 +03:00
|
|
|
static_cast<Image*>(imageKey)->OnSurfaceDiscarded(
|
|
|
|
aSurface->GetSurfaceKey());
|
2014-11-28 06:55:57 +03:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
// If we failed during StartTracking, we can skip this step.
|
|
|
|
if (aStopTracking) {
|
|
|
|
StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
// Individual surfaces must be freed outside the lock.
|
|
|
|
mCachedSurfacesDiscard.AppendElement(cache->Remove(aSurface));
|
|
|
|
|
2017-09-21 23:56:38 +03:00
|
|
|
MaybeRemoveEmptyCache(imageKey, cache);
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
bool StartTracking(NotNull<CachedSurface*> aSurface,
|
2017-03-28 04:47:23 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2013-10-22 15:37:59 +04:00
|
|
|
CostEntry costEntry = aSurface->GetCostEntry();
|
|
|
|
MOZ_ASSERT(costEntry.GetCost() <= mAvailableCost,
|
|
|
|
"Cost too large and the caller didn't catch it");
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
if (aSurface->IsLocked()) {
|
|
|
|
mLockedCost += costEntry.GetCost();
|
|
|
|
MOZ_ASSERT(mLockedCost <= mMaxCost, "Locked more than we can hold?");
|
|
|
|
} else {
|
2017-09-07 06:17:04 +03:00
|
|
|
if (NS_WARN_IF(!mCosts.InsertElementSorted(costEntry, fallible))) {
|
2020-01-21 12:59:15 +03:00
|
|
|
mTrackingFailureCount++;
|
2017-09-07 06:17:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-10 13:53:47 +03:00
|
|
|
// This may fail during XPCOM shutdown, so we need to ensure the object is
|
|
|
|
// tracked before calling RemoveObject in StopTracking.
|
2017-09-07 06:17:04 +03:00
|
|
|
nsresult rv = mExpirationTracker.AddObjectLocked(aSurface, aAutoLock);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
|
|
|
|
MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
|
2020-01-21 12:59:15 +03:00
|
|
|
mTrackingFailureCount++;
|
2017-09-07 06:17:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
2017-09-07 06:17:04 +03:00
|
|
|
|
|
|
|
mAvailableCost -= costEntry.GetCost();
|
|
|
|
return true;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void StopTracking(NotNull<CachedSurface*> aSurface, bool aIsTracked,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2013-10-22 15:37:59 +04:00
|
|
|
CostEntry costEntry = aSurface->GetCostEntry();
|
|
|
|
|
2014-11-27 00:22:10 +03: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 13:53:47 +03:00
|
|
|
if (MOZ_LIKELY(aSurface->GetExpirationState()->IsTracked())) {
|
2017-09-07 06:17:04 +03:00
|
|
|
MOZ_ASSERT(aIsTracked, "Expiration-tracking a surface unexpectedly!");
|
2017-03-28 04:47:23 +03:00
|
|
|
mExpirationTracker.RemoveObjectLocked(aSurface, aAutoLock);
|
2014-12-10 13:53:47 +03:00
|
|
|
} else {
|
|
|
|
// Our call to AddObject must have failed in StartTracking; most likely
|
|
|
|
// we're in XPCOM shutdown right now.
|
2017-09-07 06:17:04 +03:00
|
|
|
MOZ_ASSERT(!aIsTracked, "Not expiration-tracking an unlocked surface!");
|
2014-12-10 13:53:47 +03:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
DebugOnly<bool> foundInCosts = mCosts.RemoveElementSorted(costEntry);
|
|
|
|
MOZ_ASSERT(foundInCosts, "Lost track of costs for this surface");
|
|
|
|
}
|
2014-11-26 13:57:11 +03:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
mAvailableCost += costEntry.GetCost();
|
|
|
|
MOZ_ASSERT(mAvailableCost <= mMaxCost,
|
|
|
|
"More available cost than we started with");
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2015-07-01 04:57:03 +03:00
|
|
|
LookupResult Lookup(const ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
|
2017-03-28 04:47:23 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock, bool aMarkUsed) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-03-24 23:16:00 +03:00
|
|
|
if (!cache) {
|
2015-07-20 04:39:40 +03:00
|
|
|
// No cached surfaces for this image.
|
|
|
|
return LookupResult(MatchType::NOT_FOUND);
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2014-09-15 02:22:45 +04:00
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
RefPtr<CachedSurface> surface = cache->Lookup(aSurfaceKey, aMarkUsed);
|
2015-03-24 23:16:00 +03:00
|
|
|
if (!surface) {
|
2015-07-20 04:39:40 +03:00
|
|
|
// Lookup in the per-image cache missed.
|
|
|
|
return LookupResult(MatchType::NOT_FOUND);
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2014-09-15 02:22:45 +04:00
|
|
|
|
2015-07-20 04:39:44 +03:00
|
|
|
if (surface->IsPlaceholder()) {
|
|
|
|
return LookupResult(MatchType::PENDING);
|
|
|
|
}
|
|
|
|
|
2016-08-18 03:50:31 +03:00
|
|
|
DrawableSurface drawableSurface = surface->GetDrawableSurface();
|
|
|
|
if (!drawableSurface) {
|
2016-08-16 03:58:35 +03:00
|
|
|
// The surface was released by the operating system. Remove the cache
|
|
|
|
// entry as well.
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
|
2015-07-20 04:39:40 +03:00
|
|
|
return LookupResult(MatchType::NOT_FOUND);
|
2014-09-15 02:22:45 +04:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
if (aMarkUsed &&
|
|
|
|
!MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
|
|
|
|
Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
|
|
|
|
return LookupResult(MatchType::NOT_FOUND);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2015-07-20 04:39:40 +03:00
|
|
|
MOZ_ASSERT(surface->GetSurfaceKey() == aSurfaceKey,
|
|
|
|
"Lookup() not returning an exact match?");
|
2018-05-30 22:15:35 +03:00
|
|
|
return LookupResult(std::move(drawableSurface), MatchType::EXACT);
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2015-07-01 04:57:03 +03:00
|
|
|
LookupResult LookupBestMatch(const ImageKey aImageKey,
|
2017-03-28 04:47:23 +03:00
|
|
|
const SurfaceKey& aSurfaceKey,
|
2018-10-13 08:31:02 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock,
|
2018-10-13 08:31:03 +03:00
|
|
|
bool aMarkUsed) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-03-24 23:16:00 +03:00
|
|
|
if (!cache) {
|
2015-07-20 04:39:40 +03:00
|
|
|
// No cached surfaces for this image.
|
2019-04-11 22:30:48 +03:00
|
|
|
return LookupResult(
|
|
|
|
MatchType::NOT_FOUND,
|
|
|
|
SurfaceCache::ClampSize(aImageKey, aSurfaceKey.Size()));
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2015-01-19 01:02:14 +03:00
|
|
|
|
2016-08-16 03:58:35 +03:00
|
|
|
// 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.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CachedSurface> surface;
|
2016-08-18 03:50:31 +03:00
|
|
|
DrawableSurface drawableSurface;
|
2015-07-20 04:39:44 +03:00
|
|
|
MatchType matchType = MatchType::NOT_FOUND;
|
2017-09-05 14:58:45 +03:00
|
|
|
IntSize suggestedSize;
|
2016-08-16 03:58:35 +03:00
|
|
|
while (true) {
|
2017-09-05 14:58:45 +03:00
|
|
|
Tie(surface, matchType, suggestedSize) =
|
|
|
|
cache->LookupBestMatch(aSurfaceKey);
|
2016-08-16 03:58:35 +03:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return LookupResult(
|
2019-04-11 22:30:48 +03:00
|
|
|
matchType, suggestedSize); // Lookup in the per-image cache missed.
|
2016-08-16 03:58:35 +03:00
|
|
|
}
|
2015-01-19 01:02:14 +03:00
|
|
|
|
2016-08-18 03:50:31 +03:00
|
|
|
drawableSurface = surface->GetDrawableSurface();
|
|
|
|
if (drawableSurface) {
|
2016-08-16 03:58:35 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The surface was released by the operating system. Remove the cache
|
|
|
|
// entry as well.
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 03:06:34 +03:00
|
|
|
MOZ_ASSERT_IF(matchType == MatchType::EXACT,
|
|
|
|
surface->GetSurfaceKey() == aSurfaceKey);
|
|
|
|
MOZ_ASSERT_IF(
|
|
|
|
matchType == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND ||
|
|
|
|
matchType == MatchType::SUBSTITUTE_BECAUSE_PENDING,
|
|
|
|
surface->GetSurfaceKey().SVGContext() == aSurfaceKey.SVGContext() &&
|
2016-08-18 10:06:41 +03:00
|
|
|
surface->GetSurfaceKey().Playback() == aSurfaceKey.Playback() &&
|
2015-10-06 03:06:34 +03:00
|
|
|
surface->GetSurfaceKey().Flags() == aSurfaceKey.Flags());
|
2015-07-01 04:57:03 +03:00
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
if (matchType == MatchType::EXACT ||
|
|
|
|
matchType == MatchType::SUBSTITUTE_BECAUSE_BEST) {
|
2018-10-13 08:31:02 +03:00
|
|
|
if (aMarkUsed &&
|
|
|
|
!MarkUsed(WrapNotNull(surface), WrapNotNull(cache), aAutoLock)) {
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(WrapNotNull(surface), /* aStopTracking */ false, aAutoLock);
|
|
|
|
}
|
2015-07-15 08:19:02 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
return LookupResult(std::move(drawableSurface), matchType, suggestedSize);
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
bool CanHold(const Cost aCost) const { return aCost <= mMaxCost; }
|
|
|
|
|
2016-07-11 09:48:17 +03:00
|
|
|
size_t MaximumCapacity() const { return size_t(mMaxCost); }
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2016-08-02 02:44:39 +03:00
|
|
|
if (!aProvider->Availability().IsPlaceholder()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Calling SurfaceAvailable on non-placeholder");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reinsert the provider, requesting that Insert() mark it available. This
|
|
|
|
// may or may not succeed, depending on whether some other decoder has
|
|
|
|
// beaten us to the punch and inserted a non-placeholder version of this
|
|
|
|
// surface first, but it's fine either way.
|
|
|
|
// XXX(seth): This could be implemented more efficiently; we should be able
|
|
|
|
// to just update our data structures without reinserting.
|
2017-03-28 04:47:23 +03:00
|
|
|
Insert(aProvider, /* aSetAvailable = */ true, aAutoLock);
|
2016-08-02 02:44:39 +03:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
void LockImage(const ImageKey aImageKey) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2014-11-27 00:22:10 +03:00
|
|
|
if (!cache) {
|
2018-09-21 03:22:03 +03:00
|
|
|
cache = new ImageSurfaceCache(aImageKey);
|
2021-02-26 12:11:46 +03:00
|
|
|
mImageCaches.InsertOrUpdate(aImageKey, RefPtr{cache});
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cache->SetLocked(true);
|
|
|
|
|
2015-01-20 14:06:37 +03: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-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void UnlockImage(const ImageKey aImageKey,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-01-20 14:06:37 +03:00
|
|
|
if (!cache || !cache->IsLocked()) {
|
|
|
|
return; // Already unlocked.
|
|
|
|
}
|
2014-11-27 00:22:10 +03:00
|
|
|
|
|
|
|
cache->SetLocked(false);
|
2017-03-28 04:47:23 +03:00
|
|
|
DoUnlockSurfaces(WrapNotNull(cache), /* aStaticOnly = */ false, aAutoLock);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void UnlockEntries(const ImageKey aImageKey,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-01-20 14:06:37 +03:00
|
|
|
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.)
|
2019-07-22 05:10:14 +03:00
|
|
|
DoUnlockSurfaces(WrapNotNull(cache),
|
|
|
|
/* aStaticOnly = */
|
|
|
|
!StaticPrefs::image_mem_animated_discardable_AtStartup(),
|
|
|
|
aAutoLock);
|
2015-01-20 14:06:37 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
already_AddRefed<ImageSurfaceCache> RemoveImage(
|
|
|
|
const ImageKey aImageKey, const StaticMutexAutoLock& aAutoLock) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2015-03-24 23:16:00 +03:00
|
|
|
if (!cache) {
|
2017-08-15 22:02:14 +03:00
|
|
|
return nullptr; // No cached surfaces for this image, so nothing to do.
|
2015-03-24 23:16:00 +03:00
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
// 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.
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& value : cache->Values()) {
|
|
|
|
StopTracking(WrapNotNull(value),
|
2017-09-07 06:17:04 +03:00
|
|
|
/* aIsTracked */ true, aAutoLock);
|
2015-11-11 10:07:09 +03:00
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
// The per-image cache isn't needed anymore, so remove it as well.
|
2014-11-27 00:22:10 +03:00
|
|
|
// This implicitly unlocks the image if it was locked.
|
2013-10-22 15:37:59 +04:00
|
|
|
mImageCaches.Remove(aImageKey);
|
2017-08-15 22:02:14 +03:00
|
|
|
|
|
|
|
// Since we did not actually remove any of the surfaces from the cache
|
|
|
|
// itself, only stopped tracking them, we should free it outside the lock.
|
|
|
|
return cache.forget();
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:46 +03:00
|
|
|
void PruneImage(const ImageKey aImageKey,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache) {
|
|
|
|
return; // No cached surfaces for this image, so nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->Prune([this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
|
2017-09-07 06:17:04 +03:00
|
|
|
StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
|
2017-09-19 15:19:48 +03:00
|
|
|
// Individual surfaces must be freed outside the lock.
|
|
|
|
mCachedSurfacesDiscard.AppendElement(aSurface);
|
2017-09-05 14:58:46 +03:00
|
|
|
});
|
2017-09-21 23:56:38 +03:00
|
|
|
|
|
|
|
MaybeRemoveEmptyCache(aImageKey, cache);
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void DiscardAll(const StaticMutexAutoLock& aAutoLock) {
|
2013-10-22 15:38:01 +04:00
|
|
|
// Remove in order of cost because mCosts is an array and the other data
|
2015-09-20 02:20:59 +03:00
|
|
|
// structures are all hash tables. Note that locked surfaces are not
|
|
|
|
// removed, since they aren't present in mCosts.
|
2013-10-22 15:38:01 +04:00
|
|
|
while (!mCosts.IsEmpty()) {
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
|
|
|
|
aAutoLock);
|
2013-10-22 15:38:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void DiscardForMemoryPressure(const StaticMutexAutoLock& aAutoLock) {
|
2014-11-27 00:22:10 +03:00
|
|
|
// 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");
|
2017-03-28 04:47:23 +03:00
|
|
|
DiscardAll(aAutoLock);
|
2014-11-27 00:22:10 +03:00
|
|
|
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");
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(mCosts.LastElement().Surface(), /* aStopTracking */ true,
|
|
|
|
aAutoLock);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
void TakeDiscard(nsTArray<RefPtr<CachedSurface>>& aDiscard,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
|
|
|
MOZ_ASSERT(aDiscard.IsEmpty());
|
2018-05-30 22:15:35 +03:00
|
|
|
aDiscard = std::move(mCachedSurfacesDiscard);
|
2017-08-15 22:02:14 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void LockSurface(NotNull<CachedSurface*> aSurface,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-09-20 02:20:59 +03:00
|
|
|
if (aSurface->IsPlaceholder() || aSurface->IsLocked()) {
|
2015-01-20 14:06:37 +03:00
|
|
|
return;
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
|
2014-11-27 00:22:10 +03:00
|
|
|
|
|
|
|
// Lock the surface. This can fail.
|
|
|
|
aSurface->SetLocked(true);
|
2020-01-21 12:59:15 +03:00
|
|
|
DebugOnly<bool> tracked = StartTracking(aSurface, aAutoLock);
|
|
|
|
MOZ_ASSERT(tracked);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ShallowSizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf, const StaticMutexAutoLock& aAutoLock) const {
|
|
|
|
size_t bytes =
|
|
|
|
aMallocSizeOf(this) + mCosts.ShallowSizeOfExcludingThis(aMallocSizeOf) +
|
|
|
|
mImageCaches.ShallowSizeOfExcludingThis(aMallocSizeOf) +
|
|
|
|
mCachedSurfacesDiscard.ShallowSizeOfExcludingThis(aMallocSizeOf) +
|
|
|
|
mExpirationTracker.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& data : mImageCaches.Values()) {
|
|
|
|
bytes += data->ShallowSizeOfIncludingThis(aMallocSizeOf);
|
2020-01-21 12:59:15 +03:00
|
|
|
}
|
|
|
|
return bytes;
|
2015-01-20 14:06:37 +03:00
|
|
|
}
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_IMETHOD
|
2014-11-27 00:22:10 +03:00
|
|
|
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aAnonymize) override {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2015-02-01 02:29:48 +03:00
|
|
|
|
2020-01-21 12:59:15 +03:00
|
|
|
uint32_t lockedImageCount = 0;
|
|
|
|
uint32_t totalSurfaceCount = 0;
|
|
|
|
uint32_t lockedSurfaceCount = 0;
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& cache : mImageCaches.Values()) {
|
|
|
|
totalSurfaceCount += cache->Count();
|
|
|
|
if (cache->IsLocked()) {
|
2020-01-21 12:59:15 +03:00
|
|
|
++lockedImageCount;
|
|
|
|
}
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& value : cache->Values()) {
|
|
|
|
if (value->IsLocked()) {
|
2020-01-21 12:59:15 +03:00
|
|
|
++lockedSurfaceCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 00:41:48 +03:00
|
|
|
// clang-format off
|
2014-11-27 00:22:11 +03: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.
|
2020-01-21 12:59:15 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/images/cache/overhead", KIND_HEAP, UNITS_BYTES,
|
|
|
|
ShallowSizeOfIncludingThis(SurfaceCacheMallocSizeOf, lock),
|
|
|
|
"Memory used by the surface cache data structures, excluding surface data.");
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-estimated-total",
|
|
|
|
KIND_OTHER, UNITS_BYTES, (mMaxCost - mAvailableCost),
|
|
|
|
"Estimated total memory used by the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-estimated-locked",
|
|
|
|
KIND_OTHER, UNITS_BYTES, mLockedCost,
|
|
|
|
"Estimated memory used by locked surfaces in the imagelib surface cache.");
|
|
|
|
|
2020-01-21 12:59:15 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-tracked-cost-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mCosts.Length(),
|
|
|
|
"Total number of surfaces tracked for cost (and expiry) in the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-tracked-expiry-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mExpirationTracker.Length(lock),
|
|
|
|
"Total number of surfaces tracked for expiry (and cost) in the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-image-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mImageCaches.Count(),
|
|
|
|
"Total number of images in the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-locked-image-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, lockedImageCount,
|
|
|
|
"Total number of locked images in the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-image-surface-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, totalSurfaceCount,
|
|
|
|
"Total number of surfaces in the imagelib surface cache.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-locked-surfaces-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, lockedSurfaceCount,
|
|
|
|
"Total number of locked surfaces in the imagelib surface cache.");
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-overflow-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mOverflowCount,
|
|
|
|
"Count of how many times the surface cache has hit its capacity and been "
|
|
|
|
"unable to insert a new surface.");
|
2020-01-21 12:59:15 +03:00
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-tracking-failure-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mTrackingFailureCount,
|
|
|
|
"Count of how many times the surface cache has failed to begin tracking a "
|
|
|
|
"given surface.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-already-present-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mAlreadyPresentCount,
|
|
|
|
"Count of how many times the surface cache has failed to insert a surface "
|
|
|
|
"because it is already present.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"imagelib-surface-cache-table-failure-count",
|
|
|
|
KIND_OTHER, UNITS_COUNT, mTableFailureCount,
|
|
|
|
"Count of how many times the surface cache has failed to insert a surface "
|
|
|
|
"because a hash table could not accept an entry.");
|
2018-11-30 00:41:48 +03:00
|
|
|
// clang-format on
|
2015-06-03 09:30:11 +03:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
return NS_OK;
|
2013-11-07 09:35:30 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
void CollectSizeOfSurfaces(const ImageKey aImageKey,
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
2017-09-19 15:19:48 +03:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
2014-11-27 00:22:11 +03:00
|
|
|
if (!cache) {
|
2015-04-28 21:46:17 +03:00
|
|
|
return; // No surfaces for this image.
|
2014-11-27 00:22:11 +03:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
// Report all surfaces in the per-image cache.
|
2017-09-19 15:19:48 +03:00
|
|
|
cache->CollectSizeOfSurfaces(
|
|
|
|
aCounters, aMallocSizeOf,
|
|
|
|
[this, &aAutoLock](NotNull<CachedSurface*> aSurface) -> void {
|
|
|
|
StopTracking(aSurface, /* aIsTracked */ true, aAutoLock);
|
|
|
|
// Individual surfaces must be freed outside the lock.
|
|
|
|
mCachedSurfacesDiscard.AppendElement(aSurface);
|
|
|
|
});
|
2017-09-21 23:56:38 +03:00
|
|
|
|
|
|
|
MaybeRemoveEmptyCache(aImageKey, cache);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:34:01 +03:00
|
|
|
void ReleaseImageOnMainThread(already_AddRefed<image::Image>&& aImage,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
|
|
|
RefPtr<image::Image> image = aImage;
|
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needsDispatch = mReleasingImagesOnMainThread.IsEmpty();
|
|
|
|
mReleasingImagesOnMainThread.AppendElement(image);
|
|
|
|
|
|
|
|
if (!needsDispatch) {
|
|
|
|
// There is already a ongoing task for ClearReleasingImages().
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"SurfaceCacheImpl::ReleaseImageOnMainThread",
|
|
|
|
[]() -> void { SurfaceCache::ClearReleasingImages(); }));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TakeReleasingImages(nsTArray<RefPtr<image::Image>>& aImage,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
aImage.SwapElements(mReleasingImagesOnMainThread);
|
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
private:
|
|
|
|
already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageSurfaceCache> imageCache;
|
2013-10-22 15:37:59 +04:00
|
|
|
mImageCaches.Get(aImageKey, getter_AddRefs(imageCache));
|
|
|
|
return imageCache.forget();
|
|
|
|
}
|
|
|
|
|
2017-09-21 23:56:38 +03:00
|
|
|
void MaybeRemoveEmptyCache(const ImageKey aImageKey,
|
|
|
|
ImageSurfaceCache* aCache) {
|
|
|
|
// 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. Note that
|
|
|
|
// we don't push it into mImageCachesDiscard because all of its surfaces
|
|
|
|
// have been removed, so it is safe to free while holding the lock.
|
|
|
|
if (aCache->IsEmpty() && !aCache->IsLocked()) {
|
|
|
|
mImageCaches.Remove(aImageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:22:10 +03: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
|
2015-09-20 02:20:59 +03:00
|
|
|
// publicly because we permit multithreaded access to the surface cache, which
|
|
|
|
// means that the result would be meaningless: another thread could insert a
|
|
|
|
// surface or lock an image at any time.
|
2014-11-27 00:22:10 +03:00
|
|
|
bool CanHoldAfterDiscarding(const Cost aCost) const {
|
|
|
|
return aCost <= mMaxCost - mLockedCost;
|
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
bool MarkUsed(NotNull<CachedSurface*> aSurface,
|
2017-03-28 04:47:23 +03:00
|
|
|
NotNull<ImageSurfaceCache*> aCache,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2015-07-15 08:19:02 +03:00
|
|
|
if (aCache->IsLocked()) {
|
2017-03-28 04:47:23 +03:00
|
|
|
LockSurface(aSurface, aAutoLock);
|
2017-09-07 06:17:04 +03:00
|
|
|
return true;
|
2015-07-15 08:19:02 +03:00
|
|
|
}
|
2017-09-07 06:17:04 +03:00
|
|
|
|
|
|
|
nsresult rv = mExpirationTracker.MarkUsedLocked(aSurface, aAutoLock);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// If mark used fails, it is because it failed to reinsert the surface
|
|
|
|
// after removing it from the tracker. Thus we need to update our
|
|
|
|
// own accounting but otherwise expect it to be untracked.
|
|
|
|
StopTracking(aSurface, /* aIsTracked */ false, aAutoLock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-07-15 08:19:02 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
void DoUnlockSurfaces(NotNull<ImageSurfaceCache*> aCache, bool aStaticOnly,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2017-09-07 06:17:04 +03:00
|
|
|
AutoTArray<NotNull<CachedSurface*>, 8> discard;
|
|
|
|
|
2015-11-11 10:07:09 +03:00
|
|
|
// Unlock all the surfaces the per-image cache is holding.
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& value : aCache->Values()) {
|
|
|
|
NotNull<CachedSurface*> surface = WrapNotNull(value);
|
2015-11-11 10:07:09 +03:00
|
|
|
if (surface->IsPlaceholder() || !surface->IsLocked()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-14 09:11:44 +03:00
|
|
|
if (aStaticOnly &&
|
|
|
|
surface->GetSurfaceKey().Playback() != PlaybackType::eStatic) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-07 06:17:04 +03:00
|
|
|
StopTracking(surface, /* aIsTracked */ true, aAutoLock);
|
2015-11-11 10:07:09 +03:00
|
|
|
surface->SetLocked(false);
|
2017-09-07 06:17:04 +03:00
|
|
|
if (MOZ_UNLIKELY(!StartTracking(surface, aAutoLock))) {
|
|
|
|
discard.AppendElement(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard any that we failed to track.
|
|
|
|
for (auto iter = discard.begin(); iter != discard.end(); ++iter) {
|
|
|
|
Remove(*iter, /* aStopTracking */ false, aAutoLock);
|
2015-11-11 10:07:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-02 07:59:00 +03:00
|
|
|
void RemoveEntry(const ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
|
2017-03-28 04:47:23 +03:00
|
|
|
const StaticMutexAutoLock& aAutoLock) {
|
2016-07-02 07:59:00 +03:00
|
|
|
RefPtr<ImageSurfaceCache> cache = GetImageCache(aImageKey);
|
|
|
|
if (!cache) {
|
|
|
|
return; // No cached surfaces for this image.
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:45 +03:00
|
|
|
RefPtr<CachedSurface> surface =
|
|
|
|
cache->Lookup(aSurfaceKey, /* aForAccess = */ false);
|
2016-07-02 07:59:00 +03:00
|
|
|
if (!surface) {
|
|
|
|
return; // Lookup in the per-image cache missed.
|
|
|
|
}
|
|
|
|
|
2017-09-07 06:17:04 +03:00
|
|
|
Remove(WrapNotNull(surface), /* aStopTracking */ true, aAutoLock);
|
2016-07-02 07:59:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
class SurfaceTracker final
|
|
|
|
: public ExpirationTrackerImpl<CachedSurface, 2, StaticMutex,
|
|
|
|
StaticMutexAutoLock> {
|
|
|
|
public:
|
2015-02-01 02:29:48 +03:00
|
|
|
explicit SurfaceTracker(uint32_t aSurfaceCacheExpirationTimeMS)
|
2017-03-28 04:47:23 +03:00
|
|
|
: ExpirationTrackerImpl<CachedSurface, 2, StaticMutex,
|
|
|
|
StaticMutexAutoLock>(
|
2020-04-07 18:17:07 +03:00
|
|
|
aSurfaceCacheExpirationTimeMS, "SurfaceTracker") {}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
protected:
|
2017-03-28 04:47:23 +03:00
|
|
|
void NotifyExpiredLocked(CachedSurface* aSurface,
|
|
|
|
const StaticMutexAutoLock& aAutoLock) override {
|
2017-09-07 06:17:04 +03:00
|
|
|
sInstance->Remove(WrapNotNull(aSurface), /* aStopTracking */ true,
|
|
|
|
aAutoLock);
|
2017-03-28 04:47:23 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
void NotifyHandlerEndLocked(const StaticMutexAutoLock& aAutoLock) override {
|
|
|
|
sInstance->TakeDiscard(mDiscard, aAutoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyHandlerEnd() override {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard(std::move(mDiscard));
|
2017-08-15 22:02:14 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
StaticMutex& GetMutex() override { return sInstanceMutex; }
|
2017-08-15 22:02:14 +03:00
|
|
|
|
|
|
|
nsTArray<RefPtr<CachedSurface>> mDiscard;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
class MemoryPressureObserver final : public nsIObserver {
|
|
|
|
public:
|
2013-10-22 15:38:01 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-11-27 00:22:11 +03:00
|
|
|
NS_IMETHOD Observe(nsISupports*, const char* aTopic,
|
2015-03-21 19:28:04 +03:00
|
|
|
const char16_t*) override {
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance && strcmp(aTopic, "memory-pressure") == 0) {
|
|
|
|
sInstance->DiscardForMemoryPressure(lock);
|
|
|
|
sInstance->TakeDiscard(discard, lock);
|
|
|
|
}
|
2013-10-22 15:38:01 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-23 22:49:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~MemoryPressureObserver() {}
|
2013-10-22 15:38:01 +04:00
|
|
|
};
|
|
|
|
|
2015-03-24 23:16:00 +03:00
|
|
|
nsTArray<CostEntry> mCosts;
|
|
|
|
nsRefPtrHashtable<nsPtrHashKey<Image>, ImageSurfaceCache> mImageCaches;
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> mCachedSurfacesDiscard;
|
2015-03-24 23:16:00 +03:00
|
|
|
SurfaceTracker mExpirationTracker;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
|
2020-07-10 17:34:01 +03:00
|
|
|
nsTArray<RefPtr<image::Image>> mReleasingImagesOnMainThread;
|
2015-03-24 23:16:00 +03:00
|
|
|
const uint32_t mDiscardFactor;
|
|
|
|
const Cost mMaxCost;
|
|
|
|
Cost mAvailableCost;
|
|
|
|
Cost mLockedCost;
|
2015-06-03 09:30:11 +03:00
|
|
|
size_t mOverflowCount;
|
2020-01-21 12:59:15 +03:00
|
|
|
size_t mAlreadyPresentCount;
|
|
|
|
size_t mTableFailureCount;
|
|
|
|
size_t mTrackingFailureCount;
|
2013-10-22 15:37:59 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SurfaceCacheImpl, nsIMemoryReporter)
|
|
|
|
NS_IMPL_ISUPPORTS(SurfaceCacheImpl::MemoryPressureObserver, nsIObserver)
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public API
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::Initialize() {
|
2013-10-22 15:37:59 +04:00
|
|
|
// Initialize preferences.
|
2015-01-08 11:04:31 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-22 15:37:59 +04:00
|
|
|
MOZ_ASSERT(!sInstance, "Shouldn't initialize more than once");
|
|
|
|
|
2019-05-26 17:30:14 +03:00
|
|
|
// See StaticPrefs for the default values of these preferences.
|
2014-09-23 01:30:20 +04:00
|
|
|
|
2014-11-27 00:22:10 +03:00
|
|
|
// Length of time before an unused surface is removed from the cache, in
|
|
|
|
// milliseconds.
|
|
|
|
uint32_t surfaceCacheExpirationTimeMS =
|
2019-07-22 05:10:14 +03:00
|
|
|
StaticPrefs::image_mem_surfacecache_min_expiration_ms_AtStartup();
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:10 +03: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 =
|
2019-07-22 05:10:14 +03:00
|
|
|
max(StaticPrefs::image_mem_surfacecache_discard_factor_AtStartup(), 1u);
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
// Maximum size of the surface cache, in kilobytes.
|
2019-06-26 06:24:21 +03:00
|
|
|
uint64_t surfaceCacheMaxSizeKB =
|
2019-07-22 05:10:14 +03:00
|
|
|
StaticPrefs::image_mem_surfacecache_max_size_kb_AtStartup();
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2019-09-10 03:55:13 +03:00
|
|
|
if (sizeof(uintptr_t) <= 4) {
|
|
|
|
// Limit surface cache to 1 GB if our address space is 32 bit.
|
|
|
|
surfaceCacheMaxSizeKB = 1024 * 1024;
|
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04: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-27 00:22:10 +03:00
|
|
|
// For example, a value of 4 would yield a 256MB cache on a 1GB machine.
|
2013-10-22 15:37:59 +04:00
|
|
|
// The smallest machines we are likely to run this code on have 256MB
|
2014-11-27 00:22:10 +03:00
|
|
|
// of memory, which would yield a 64MB cache on this setting.
|
2014-11-27 00:22:10 +03:00
|
|
|
// We clamp this value to avoid division by zero.
|
|
|
|
uint32_t surfaceCacheSizeFactor =
|
2019-07-22 05:10:14 +03:00
|
|
|
max(StaticPrefs::image_mem_surfacecache_size_factor_AtStartup(), 1u);
|
2013-10-22 15:37:59 +04:00
|
|
|
|
|
|
|
// Compute the size of the surface cache.
|
2014-12-19 06:50:56 +03: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;
|
2015-03-24 23:16:00 +03:00
|
|
|
uint64_t surfaceCacheSizeBytes =
|
|
|
|
min(proposedSize, surfaceCacheMaxSizeKB * 1024);
|
2014-11-27 00:22:10 +03:00
|
|
|
uint32_t finalSurfaceCacheSizeBytes =
|
|
|
|
min(surfaceCacheSizeBytes, uint64_t(UINT32_MAX));
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2014-11-27 00:22:10 +03: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 15:37:59 +04:00
|
|
|
sInstance = new SurfaceCacheImpl(surfaceCacheExpirationTimeMS,
|
2014-11-27 00:22:10 +03:00
|
|
|
surfaceCacheDiscardFactor,
|
2014-11-27 00:22:10 +03:00
|
|
|
finalSurfaceCacheSizeBytes);
|
2013-11-07 09:35:30 +04:00
|
|
|
sInstance->InitMemoryReporter();
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::Shutdown() {
|
2017-08-15 22:02:14 +03:00
|
|
|
RefPtr<SurfaceCacheImpl> cache;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(sInstance, "No singleton - was Shutdown() called twice?");
|
|
|
|
cache = sInstance.forget();
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
LookupResult SurfaceCache::Lookup(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
bool aMarkUsed) {
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
LookupResult rv(MatchType::NOT_FOUND);
|
|
|
|
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (!sInstance) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-10-13 08:31:02 +03:00
|
|
|
rv = sInstance->Lookup(aImageKey, aSurfaceKey, lock, aMarkUsed);
|
2017-08-15 22:02:14 +03:00
|
|
|
sInstance->TakeDiscard(discard, lock);
|
2014-09-20 01:53:29 +04:00
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
return rv;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
LookupResult SurfaceCache::LookupBestMatch(const ImageKey aImageKey,
|
|
|
|
const SurfaceKey& aSurfaceKey,
|
|
|
|
bool aMarkUsed) {
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
LookupResult rv(MatchType::NOT_FOUND);
|
|
|
|
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (!sInstance) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-10-13 08:31:02 +03:00
|
|
|
rv = sInstance->LookupBestMatch(aImageKey, aSurfaceKey, lock, aMarkUsed);
|
2017-08-15 22:02:14 +03:00
|
|
|
sInstance->TakeDiscard(discard, lock);
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
return rv;
|
2015-01-19 01:02:14 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
InsertOutcome SurfaceCache::Insert(NotNull<ISurfaceProvider*> aProvider) {
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
InsertOutcome rv(InsertOutcome::FAILURE);
|
|
|
|
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (!sInstance) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = sInstance->Insert(aProvider, /* aSetAvailable = */ false, lock);
|
|
|
|
sInstance->TakeDiscard(discard, lock);
|
2014-09-20 01:53:29 +04:00
|
|
|
}
|
2014-11-27 00:22:10 +03:00
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
return rv;
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
bool SurfaceCache::CanHold(const IntSize& aSize,
|
|
|
|
uint32_t aBytesPerPixel /* = 4 */) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2014-09-20 01:53:29 +04:00
|
|
|
if (!sInstance) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
|
2015-05-06 08:19:30 +03:00
|
|
|
Cost cost = ComputeCost(aSize, aBytesPerPixel);
|
2013-10-22 15:37:59 +04:00
|
|
|
return sInstance->CanHold(cost);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
bool SurfaceCache::CanHold(size_t aSize) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2015-01-16 02:11:35 +03:00
|
|
|
if (!sInstance) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance->CanHold(aSize);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::SurfaceAvailable(NotNull<ISurfaceProvider*> aProvider) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2016-08-02 02:44:39 +03:00
|
|
|
if (!sInstance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-28 04:47:23 +03:00
|
|
|
sInstance->SurfaceAvailable(aProvider, lock);
|
2016-08-02 02:44:39 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::LockImage(const ImageKey aImageKey) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2014-11-27 00:22:10 +03:00
|
|
|
if (sInstance) {
|
|
|
|
return sInstance->LockImage(aImageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::UnlockImage(const ImageKey aImageKey) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2014-11-27 00:22:10 +03:00
|
|
|
if (sInstance) {
|
2017-03-28 04:47:23 +03:00
|
|
|
return sInstance->UnlockImage(aImageKey, lock);
|
2014-11-27 00:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::UnlockEntries(const ImageKey aImageKey) {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2015-01-20 14:06:37 +03:00
|
|
|
if (sInstance) {
|
2017-03-28 04:47:23 +03:00
|
|
|
return sInstance->UnlockEntries(aImageKey, lock);
|
2015-01-20 14:06:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::RemoveImage(const ImageKey aImageKey) {
|
2017-08-15 22:02:14 +03:00
|
|
|
RefPtr<ImageSurfaceCache> discard;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance) {
|
|
|
|
discard = sInstance->RemoveImage(aImageKey, lock);
|
|
|
|
}
|
2014-09-20 01:53:29 +04:00
|
|
|
}
|
2013-10-22 15:37:59 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::PruneImage(const ImageKey aImageKey) {
|
2017-09-19 15:19:48 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance->PruneImage(aImageKey, lock);
|
|
|
|
sInstance->TakeDiscard(discard, lock);
|
|
|
|
}
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::DiscardAll() {
|
2017-08-15 22:02:14 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance->DiscardAll(lock);
|
|
|
|
sInstance->TakeDiscard(discard, lock);
|
|
|
|
}
|
2014-06-25 17:28:51 +04:00
|
|
|
}
|
2014-02-08 23:37:39 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::CollectSizeOfSurfaces(
|
2015-04-28 21:46:17 +03:00
|
|
|
const ImageKey aImageKey, nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) {
|
2017-09-19 15:19:48 +03:00
|
|
|
nsTArray<RefPtr<CachedSurface>> discard;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (!sInstance) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-19 11:51:21 +03:00
|
|
|
|
2017-09-19 15:19:48 +03:00
|
|
|
sInstance->CollectSizeOfSurfaces(aImageKey, aCounters, aMallocSizeOf, lock);
|
|
|
|
sInstance->TakeDiscard(discard, lock);
|
|
|
|
}
|
2014-11-27 00:22:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
size_t SurfaceCache::MaximumCapacity() {
|
2017-01-31 15:41:14 +03:00
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
2016-07-11 09:48:17 +03:00
|
|
|
if (!sInstance) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance->MaximumCapacity();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
bool SurfaceCache::IsLegalSize(const IntSize& aSize) {
|
2018-09-21 03:22:00 +03:00
|
|
|
// reject over-wide or over-tall images
|
|
|
|
const int32_t k64KLimit = 0x0000FFFF;
|
|
|
|
if (MOZ_UNLIKELY(aSize.width > k64KLimit || aSize.height > k64KLimit)) {
|
|
|
|
NS_WARNING("image too big");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// protect against invalid sizes
|
|
|
|
if (MOZ_UNLIKELY(aSize.height <= 0 || aSize.width <= 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to make sure we don't overflow a 32-bit
|
|
|
|
CheckedInt32 requiredBytes =
|
|
|
|
CheckedInt32(aSize.width) * CheckedInt32(aSize.height) * 4;
|
|
|
|
if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
|
|
|
|
NS_WARNING("width or height too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:30:48 +03:00
|
|
|
IntSize SurfaceCache::ClampVectorSize(const IntSize& aSize) {
|
|
|
|
// If we exceed the maximum, we need to scale the size downwards to fit.
|
|
|
|
// It shouldn't get here if it is significantly larger because
|
|
|
|
// VectorImage::UseSurfaceCacheForSize should prevent us from requesting
|
|
|
|
// a rasterized version of a surface greater than 4x the maximum.
|
2019-06-26 06:24:21 +03:00
|
|
|
int32_t maxSizeKB =
|
|
|
|
StaticPrefs::image_cache_max_rasterized_svg_threshold_kb();
|
2019-04-11 22:30:48 +03:00
|
|
|
if (maxSizeKB <= 0) {
|
|
|
|
return aSize;
|
|
|
|
}
|
|
|
|
|
2019-09-11 23:57:17 +03:00
|
|
|
int64_t proposedKB = int64_t(aSize.width) * aSize.height / 256;
|
2019-04-11 22:30:48 +03:00
|
|
|
if (maxSizeKB >= proposedKB) {
|
|
|
|
return aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
double scale = sqrt(double(maxSizeKB) / proposedKB);
|
|
|
|
return IntSize(int32_t(scale * aSize.width), int32_t(scale * aSize.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize SurfaceCache::ClampSize(ImageKey aImageKey, const IntSize& aSize) {
|
|
|
|
if (aImageKey->GetType() != imgIContainer::TYPE_VECTOR) {
|
|
|
|
return aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ClampVectorSize(aSize);
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:34:01 +03:00
|
|
|
/* static */
|
|
|
|
void SurfaceCache::ReleaseImageOnMainThread(
|
|
|
|
already_AddRefed<image::Image> aImage, bool aAlwaysProxy) {
|
|
|
|
if (NS_IsMainThread() && !aAlwaysProxy) {
|
|
|
|
RefPtr<image::Image> image = std::move(aImage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance->ReleaseImageOnMainThread(std::move(aImage), lock);
|
|
|
|
} else {
|
|
|
|
NS_ReleaseOnMainThread("SurfaceCache::ReleaseImageOnMainThread",
|
|
|
|
std::move(aImage), /* aAlwaysProxy */ true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void SurfaceCache::ClearReleasingImages() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsTArray<RefPtr<image::Image>> images;
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sInstanceMutex);
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance->TakeReleasingImages(images, lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 15:37:59 +04:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|