Bug 711901 - Use mallocSizeOf in the source image memory reporters. r=joedrew.

--HG--
extra : rebase_source : 9c39bbb44cb40fa900a8245842a50d894c02f4e0
This commit is contained in:
Nicholas Nethercote 2012-02-19 19:51:48 -08:00
parent 32679407a0
commit b7b9660f94
10 changed files with 112 additions and 96 deletions

View File

@ -58,13 +58,18 @@ Image::Image(imgStatusTracker* aStatusTracker) :
}
PRUint32
Image::GetDataSize()
Image::SizeOfData()
{
if (mError)
return 0;
return GetSourceHeapSize() + GetDecodedHeapSize() +
GetDecodedNonheapSize() + GetDecodedOutOfProcessSize();
// This is not used by memory reporters, but for sizing the cache, which is
// why it uses |moz_malloc_size_of| rather than an
// |NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN|.
return PRUint32(HeapSizeOfSourceWithComputedFallback(moz_malloc_size_of) +
HeapSizeOfDecodedWithComputedFallback(moz_malloc_size_of) +
NonHeapSizeOfDecoded() +
OutOfProcessSizeOfDecoded());
}
// Translates a mimetype into a concrete decoder

View File

@ -97,15 +97,15 @@ public:
* The size, in bytes, occupied by the significant data portions of the image.
* This includes both compressed source data and decoded frames.
*/
PRUint32 GetDataSize();
PRUint32 SizeOfData();
/**
* The components that make up GetDataSize().
* The components that make up SizeOfData().
*/
virtual PRUint32 GetDecodedHeapSize() = 0;
virtual PRUint32 GetDecodedNonheapSize() = 0;
virtual PRUint32 GetDecodedOutOfProcessSize() = 0;
virtual PRUint32 GetSourceHeapSize() = 0;
virtual size_t HeapSizeOfSourceWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const = 0;
virtual size_t HeapSizeOfDecodedWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const = 0;
virtual size_t NonHeapSizeOfDecoded() const = 0;
virtual size_t OutOfProcessSizeOfDecoded() const = 0;
// Mimetype translation
enum eDecoderType {

View File

@ -397,10 +397,7 @@ RasterImage::AdvanceFrame(TimeStamp aTime, nsIntRect* aDirtyRect)
EvaluateAnimation();
}
imgFrame *frameToUse = nsnull;
if (nextFrameIndex == 0) {
frameToUse = nextFrame;
*aDirtyRect = mAnim->firstFrameRefreshArea;
} else {
imgFrame *curFrame = mFrames[currentFrameIndex];
@ -978,50 +975,54 @@ RasterImage::GetImageContainer(ImageContainer **_retval)
return NS_OK;
}
namespace {
PRUint32
GetDecodedSize(const nsTArray<imgFrame *> &aFrames,
gfxASurface::MemoryLocation aLocation)
size_t
RasterImage::HeapSizeOfSourceWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const
{
PRUint32 val = 0;
// n == 0 is possible for two reasons.
// - This is a zero-length image.
// - We're on a platform where moz_malloc_size_of always returns 0.
// In either case the fallback works appropriately.
size_t n = mSourceData.SizeOfExcludingThis(aMallocSizeOf);
if (n == 0) {
n = mSourceData.Length();
NS_ABORT_IF_FALSE(StoringSourceData() || (n == 0),
"Non-zero source data size when we aren't storing it?");
}
return n;
}
static size_t
SizeOfDecodedWithComputedFallbackIfHeap(
const nsTArray<imgFrame*>& aFrames, gfxASurface::MemoryLocation aLocation,
nsMallocSizeOfFun aMallocSizeOf)
{
size_t n = 0;
for (PRUint32 i = 0; i < aFrames.Length(); ++i) {
imgFrame *frame = aFrames.SafeElementAt(i, nsnull);
imgFrame* frame = aFrames.SafeElementAt(i, nsnull);
NS_ABORT_IF_FALSE(frame, "Null frame in frame array!");
val += frame->EstimateMemoryUsed(aLocation);
n += frame->SizeOfExcludingThisWithComputedFallbackIfHeap(aLocation, aMallocSizeOf);
}
return val;
return n;
}
} // anonymous namespace
PRUint32
RasterImage::GetDecodedHeapSize()
size_t
RasterImage::HeapSizeOfDecodedWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const
{
return GetDecodedSize(mFrames, gfxASurface::MEMORY_IN_PROCESS_HEAP);
return SizeOfDecodedWithComputedFallbackIfHeap(
mFrames, gfxASurface::MEMORY_IN_PROCESS_HEAP, aMallocSizeOf);
}
PRUint32
RasterImage::GetDecodedNonheapSize()
size_t
RasterImage::NonHeapSizeOfDecoded() const
{
return GetDecodedSize(mFrames, gfxASurface::MEMORY_IN_PROCESS_NONHEAP);
return SizeOfDecodedWithComputedFallbackIfHeap(mFrames, gfxASurface::MEMORY_IN_PROCESS_NONHEAP, NULL);
}
PRUint32
RasterImage::GetDecodedOutOfProcessSize()
size_t
RasterImage::OutOfProcessSizeOfDecoded() const
{
return GetDecodedSize(mFrames, gfxASurface::MEMORY_OUT_OF_PROCESS);
}
PRUint32
RasterImage::GetSourceHeapSize()
{
PRUint32 sourceDataSize = mSourceData.Length();
NS_ABORT_IF_FALSE(StoringSourceData() || (sourceDataSize == 0),
"Non-zero source data size when we aren't storing it?");
return sourceDataSize;
return SizeOfDecodedWithComputedFallbackIfHeap(mFrames, gfxASurface::MEMORY_OUT_OF_PROCESS, NULL);
}
void
@ -2226,7 +2227,7 @@ RasterImage::DiscardingActive() {
// Helper method to determine if we're storing the source data in a buffer
// or just writing it directly to the decoder
bool
RasterImage::StoringSourceData() {
RasterImage::StoringSourceData() const {
return (mDecodeOnDraw || mDiscardable);
}

View File

@ -228,10 +228,10 @@ public:
/* The total number of frames in this image. */
PRUint32 GetNumFrames();
virtual PRUint32 GetDecodedHeapSize();
virtual PRUint32 GetDecodedNonheapSize();
virtual PRUint32 GetDecodedOutOfProcessSize();
virtual PRUint32 GetSourceHeapSize();
virtual size_t HeapSizeOfSourceWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const;
virtual size_t HeapSizeOfDecodedWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const;
virtual size_t NonHeapSizeOfDecoded() const;
virtual size_t OutOfProcessSizeOfDecoded() const;
/* Triggers discarding. */
void Discard(bool force = false);
@ -699,7 +699,7 @@ private: // data
bool CanDiscard();
bool CanForciblyDiscard();
bool DiscardingActive();
bool StoringSourceData();
bool StoringSourceData() const;
protected:
bool ShouldAnimate();

View File

@ -248,27 +248,8 @@ VectorImage::GetCurrentFrameRect(nsIntRect& aRect)
aRect = nsIntRect::GetMaxSizedIntRect();
}
PRUint32
VectorImage::GetDecodedHeapSize()
{
// XXXdholbert TODO: return num bytes used by helper SVG doc. (bug 590790)
return sizeof(*this);
}
PRUint32
VectorImage::GetDecodedNonheapSize()
{
return 0;
}
PRUint32
VectorImage::GetDecodedOutOfProcessSize()
{
return 0;
}
PRUint32
VectorImage::GetSourceHeapSize()
size_t
VectorImage::HeapSizeOfSourceWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const
{
// We're not storing the source data -- we just feed that directly to
// our helper SVG document as we receive it, for it to parse.
@ -276,6 +257,25 @@ VectorImage::GetSourceHeapSize()
return 0;
}
size_t
VectorImage::HeapSizeOfDecodedWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const
{
// XXXdholbert TODO: return num bytes used by helper SVG doc. (bug 590790)
return 0;
}
size_t
VectorImage::NonHeapSizeOfDecoded() const
{
return 0;
}
size_t
VectorImage::OutOfProcessSizeOfDecoded() const
{
return 0;
}
nsresult
VectorImage::StartAnimation()
{

View File

@ -95,10 +95,10 @@ public:
PRUint32 aFlags);
void GetCurrentFrameRect(nsIntRect& aRect);
virtual PRUint32 GetDecodedHeapSize();
virtual PRUint32 GetDecodedNonheapSize();
virtual PRUint32 GetDecodedOutOfProcessSize();
virtual PRUint32 GetSourceHeapSize();
virtual size_t HeapSizeOfSourceWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const;
virtual size_t HeapSizeOfDecodedWithComputedFallback(nsMallocSizeOfFun aMallocSizeOf) const;
virtual size_t NonHeapSizeOfDecoded() const;
virtual size_t OutOfProcessSizeOfDecoded() const;
// Callback for SVGRootRenderingObserver
void InvalidateObserver();

View File

@ -758,36 +758,44 @@ void imgFrame::SetCompositingFailed(bool val)
mCompositingFailed = val;
}
PRUint32
imgFrame::EstimateMemoryUsed(gfxASurface::MemoryLocation aLocation) const
size_t
imgFrame::SizeOfExcludingThisWithComputedFallbackIfHeap(gfxASurface::MemoryLocation aLocation, nsMallocSizeOfFun aMallocSizeOf) const
{
PRUint32 size = 0;
// aMallocSizeOf is only used if aLocation==MEMORY_IN_PROCESS_HEAP. It
// should be NULL otherwise.
NS_ABORT_IF_FALSE(
(aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP && aMallocSizeOf) ||
(aLocation != gfxASurface::MEMORY_IN_PROCESS_HEAP && !aMallocSizeOf),
"mismatch between aLocation and aMallocSizeOf");
if (mSinglePixel && aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP) {
size += sizeof(gfxRGBA);
}
size_t n = 0;
if (mPalettedImageData && aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP) {
size += GetImageDataLength() + PaletteDataLength();
size_t usable = aMallocSizeOf(mPalettedImageData);
if (!usable) {
usable = GetImageDataLength() + PaletteDataLength();
}
n += usable;
}
// XXX: should pass aMallocSizeOf here. See bug 723827.
#ifdef USE_WIN_SURFACE
if (mWinSurface && aLocation == mWinSurface->GetMemoryLocation()) {
size += mWinSurface->KnownMemoryUsed();
n += mWinSurface->KnownMemoryUsed();
} else
#endif
#ifdef XP_MACOSX
if (mQuartzSurface && aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP) {
size += mSize.width * mSize.height * 4;
n += mSize.width * mSize.height * 4;
} else
#endif
if (mImageSurface && aLocation == mImageSurface->GetMemoryLocation()) {
size += mImageSurface->KnownMemoryUsed();
n += mImageSurface->KnownMemoryUsed();
}
if (mOptSurface && aLocation == mOptSurface->GetMemoryLocation()) {
size += mOptSurface->KnownMemoryUsed();
n += mOptSurface->KnownMemoryUsed();
}
return size;
return n;
}

View File

@ -130,7 +130,9 @@ public:
return mImageSurface;
}
PRUint32 EstimateMemoryUsed(gfxASurface::MemoryLocation aLocation) const;
size_t SizeOfExcludingThisWithComputedFallbackIfHeap(
gfxASurface::MemoryLocation aLocation,
nsMallocSizeOfFun aMallocSizeOf) const;
PRUint8 GetPaletteDepth() const { return mPaletteDepth; }
@ -196,7 +198,6 @@ private: // data
#ifdef XP_WIN
bool mIsDDBSurface;
#endif
};
#endif /* imgFrame_h */

View File

@ -136,7 +136,8 @@ static void PrintImageDecoders()
}
#endif
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(ImagesMallocSizeOf, "images")
class imgMemoryReporter MOZ_FINAL :
public nsIMemoryReporter
{
@ -227,11 +228,11 @@ public:
struct EnumArg {
EnumArg(ReporterType aType)
: rtype(aType), value(0)
: rtype(aType), n(0)
{ }
ReporterType rtype;
PRInt32 value;
size_t n;
};
static PLDHashOperator EnumEntries(const nsACString&,
@ -255,11 +256,11 @@ public:
return PL_DHASH_NEXT;
if (rtype & RAW_BIT) {
arg->value += image->GetSourceHeapSize();
arg->n += image->HeapSizeOfSourceWithComputedFallback(ImagesMallocSizeOf);
} else if (rtype & HEAP_BIT) {
arg->value += image->GetDecodedHeapSize();
arg->n += image->HeapSizeOfDecodedWithComputedFallback(ImagesMallocSizeOf);
} else {
arg->value += image->GetDecodedNonheapSize();
arg->n += image->NonHeapSizeOfDecoded();
}
return PL_DHASH_NEXT;
@ -274,7 +275,7 @@ public:
imgLoader::sCache.EnumerateRead(EnumEntries, &arg);
}
*amount = arg.value;
*amount = arg.n;
return NS_OK;
}
@ -1653,7 +1654,7 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI,
entry->Touch();
#ifdef DEBUG_joe
printf("CACHEGET: %d %s %d\n", time(NULL), spec.get(), entry->GetDataSize());
printf("CACHEGET: %d %s %d\n", time(NULL), spec.get(), entry->SizeOfData());
#endif
}
else {

View File

@ -411,7 +411,7 @@ void imgRequest::SetIsInCache(bool incache)
void imgRequest::UpdateCacheEntrySize()
{
if (mCacheEntry) {
mCacheEntry->SetDataSize(mImage->GetDataSize());
mCacheEntry->SetDataSize(mImage->SizeOfData());
#ifdef DEBUG_joe
nsCAutoString url;