Files
roytam1 c2f9b5547e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1253124 - Check return values in and around js::ValueToSourceForError r=jandem (dc00ae80bc)
- Bug 1221378: Use a dedicated flag on JS::Zone to disable allocation metadata collection, instead of abusing AutoEnterAnalysis. r=fitzgen (a39a050cea)
- Bug 1255527 - Remove sutagent/watcher; r=jmaher (7f0f536a35)
- Bug 1220716 - Removing nsICurrentCharsetListener. r=emk (d5b01215e8)
- Bug 956899 - Convert exclusive access lock from PRLock to Mutex; r=jandem (d4dc913f51)
- Bug 1252905. Don't invoke the interrupt callback while an exception is pending. r=jandem (762bed231e)
- Bug 1251675 - Add a Mutex<T> type, based on Rust's `std::sync::Mutex<T>`. r=terrence, r=jimb (10eb70bf07)
- NO BUG - Do not print diagnostics messages by default in js/src/jsapi-tests/testMutex.cpp on a CLOSED TREE; r=me (49f3fb6a44)
- Bug 956899 - Move and rename rust-alike Mutex to ExclusiveData; r=fitzgen (fa8421162f)
- Bug 956899 - Add a platform agnostic std::mutex work-alike; r=froydnj (7e991c3452)
- Bug 956899 - Replace ExclusiveData PRLock usage with Mutex; r=fitzgen (32af2e5ac4)
- Bug 956899 - Replace PRLock with Mutex in TraceLoggingGraph; r=h4writer (98feb957f5)
- Bug 956899 - Replace PRLock with Mutex in TraceLogger; r=h4writer (0aaf13825d)
- Bug 1242462 - IonSpewer: Write one log file per process. r=h4writer (89158ca321)
- Bug 956899 - Replace PRLock with Mutex in JitSpewer; r=nbp (0926da57f4)
- Bug 956899 - Add a std::lock_guard work-alike; r=froydnj (9562d7c220)
- Bug 956899 - Teach check_spidermonkey_style.py about mozglue; r=njn (c01c08595f)
- Bug 956899 - Implement an RAII unlocking primitive to compliment LockGuard; r=froydnj (7c366764c6)
- Bug 1258818 - Fix bad search/replace from b411b94f8d91 (from bug 956899). r=terrence (47bac2bc34)
- Bug 1255795 - use UniqueTwoByteChars in AtomizeUTF8Chars (r=jandem) (6187f0f7c2)
- Bug 1240502 - Fix debug-only dense elements check in SpreadCallOperation to not fail after a TI OOM. r=arai (d7d03ac85d)
- Bug 1257040 - Disable C4577 to unblock compilation on VS2015; r=jorendorff (acc571fca5)
- Bug 1255766 - Tracelogger: Mark resizing of memory also as internal tracelogger time, r=bbouvier (767ee0dd73)
- Bug 1257102 - Invoking a trap on a proxy, where the handler has null ¦as the value of that trap, should fall back to operating on the target just like undefined would. r=evilpie (7c72b25a88)
- Bug 1214013 - Add fuzz test. (r=efaust) (a7c3eccf37)
- Bug 1198833 - Variable redeclaration should be a syntax error r=shu (d2c0e9d7ad)
- Bug 1252924 - SIMD.*.prototype.toString(); r=waldo (ee3456280a)
- Bug 1252927 - SIMD: Truncate before range check. r=sunfish (9fe432ead7)
- Bug 1252924 - SIMD.*.prototype.valueOf. r=waldo (ae917c754e)
- Bug 1252270 - SIMD: Coerce non-numeric indexes to load/store functions. r=lth (f16722e671)
- Bug 1256945 - Coerce SIMD lane indexes with ToNumber(). r=bbouvier (c59ed63e98)
- Bug 1241432 - Implement GB18030-2005. r=smontagu (432d698684)
- Bug 1155263 - missing <stdint.h> header in rulebrk.c r=dholbert (a8ca47c0dd)
- Bug 1225934. Better describe the effect of the flag FLAG_HIGH_QUALITY_SCALING. Comment only change. (8ff70e3766)
- Bug 1251405. Part 1. Fix a significant signed/unsigned mismatch in handling the return value of FrameAnimator::GetSingleLoopTime. r=edwin (cd80cc1505)
- Bug 1251405. Part 2. Use 64 bit ints to hold the delay between the current time and the last animation time. r=edwin (71e1af9250)
- Bug 926048. Part 1. Simplify FrameAnimator::AdvanceFrame slightly. We don't need a |timeout| variable, we only check it once. r=edwin (fad20cc1dc)
- Bug 926048. Part 2. Remove useless GetRawFrame call. r=edwin (e5dec6bbf7)
- Bug 926048. Part 3. Correctly check if we are at the end of an animated image. r=edwin (906d4ce883)
- Bug 926048. Part 4. Update the current animation frame time if we hit the end of decoded frames before all frames are decoded. r=edwin (09ca5732f4)
- Bug 1251403. Determine the correct index of the next frame before getting the next frame. r=edwin (024cf9cddf)
- Bug 1220082 - Assign frame ids to animated images so that they get invalidated correctly. r=seth (c80a29d4ae)
- Bug 1225934 - Never allow surface substitution when FLAG_HIGH_QUALITY_SCALING is disabled. r=tn (2f8515aa5c)
- Bug 1251807 - Use the surface's size, not the intrinsic size, in CopyFrame. r=tn (aeb9e251cd)
- Bug 1251806 - In RasterImage::GetFrameInternal(), check if the frame covers the actual surface size rather than the requested surface size. r=tn (c257ec96c2)
- Bug 1251808 - Construct the SourceSurfaceImage with the correct size in RasterImage::GetCurrentImage(). r=seth (f8ed7b0485)
- bug 1223466 - update extended validation information to deal with root removals in NSS 3.21 r=mgoodwin (d0a7a8c2d9)
- bug 1241564 - remove EV treatment for TÃRKTRUST Elektronik Sertifika Hizmet SaÄlayıcısı SHA-1 root certificate r=Cykesiopka (1fa9bdc5b2)
- bug 1236964 - enable Certum Trusted Network CA 2 root certificate for EV treatment r=jcj (d3effc42d0)
- Bug 1254689 - Remove SEC_NORMAL where loadingPrincipal is SystemPrincipal or NullPrincipal. r=sicking (5500cbc930)
- Bug 1228314 - added static_cast<int64> in order to avoid overflow. r=seth (f0503f879b)
- Bug 1251742. Avoid overflow in computing area of surface sizes in SurfaceCache. r=dholbert (69731a1cc2)
- Bug 1251091. Fix surface key comparison in ImageSurfaceCache::LookupBestMatch. r=dholbert (ca003cf8e5)
2024-03-19 23:32:03 +08:00

424 lines
18 KiB
C++

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