From 65cbbd034d9fabfe2531aa5667418f6fb892df89 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 26 Nov 2012 16:29:56 -0800 Subject: [PATCH] Bug 711901 - When possible, measure, don't compute, the size of imgFrame::mImageSurface. r=joedrew. --HG-- extra : rebase_source : 72dc61c2618166262ec6d83778d7bd2a3a7b5d61 --- gfx/thebes/gfxASurface.cpp | 13 +++++++++++++ gfx/thebes/gfxASurface.h | 3 +++ gfx/thebes/gfxImageSurface.cpp | 16 ++++++++++++++++ gfx/thebes/gfxImageSurface.h | 6 ++++++ image/src/imgFrame.cpp | 20 +++++++++++++++----- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/gfx/thebes/gfxASurface.cpp b/gfx/thebes/gfxASurface.cpp index fd97800d7aac..8a003a4d4594 100644 --- a/gfx/thebes/gfxASurface.cpp +++ b/gfx/thebes/gfxASurface.cpp @@ -649,6 +649,19 @@ gfxASurface::RecordMemoryFreed() } } +size_t +gfxASurface::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const +{ + // We don't measure mSurface because cairo doesn't allow it. + return 0; +} + +size_t +gfxASurface::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const +{ + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); +} + #ifdef MOZ_DUMP_IMAGES void gfxASurface::WriteAsPNG(const char* aFile) diff --git a/gfx/thebes/gfxASurface.h b/gfx/thebes/gfxASurface.h index 3343d2cbbd18..257faed155fd 100644 --- a/gfx/thebes/gfxASurface.h +++ b/gfx/thebes/gfxASurface.h @@ -197,6 +197,9 @@ public: virtual int32_t KnownMemoryUsed() { return mBytesRecorded; } + virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const; + virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const; + /** * The memory used by this surface (as reported by KnownMemoryUsed()) can * either live in this process's heap, in this process but outside the diff --git a/gfx/thebes/gfxImageSurface.cpp b/gfx/thebes/gfxImageSurface.cpp index 926d109708cc..97cd0707d234 100644 --- a/gfx/thebes/gfxImageSurface.cpp +++ b/gfx/thebes/gfxImageSurface.cpp @@ -174,6 +174,22 @@ gfxImageSurface::ComputeStride(const gfxIntSize& aSize, gfxImageFormat aFormat) return stride; } +size_t +gfxImageSurface::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const +{ + size_t n = gfxASurface::SizeOfExcludingThis(aMallocSizeOf); + if (mOwnsData) { + n += aMallocSizeOf(mData); + } + return n; +} + +size_t +gfxImageSurface::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const +{ + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); +} + // helper function for the CopyFrom methods static void CopyForStride(unsigned char* aDest, unsigned char* aSrc, const gfxIntSize& aSize, long aDestStride, long aSrcStride) diff --git a/gfx/thebes/gfxImageSurface.h b/gfx/thebes/gfxImageSurface.h index 23f68ebb363d..dc24cb6d61fe 100644 --- a/gfx/thebes/gfxImageSurface.h +++ b/gfx/thebes/gfxImageSurface.h @@ -93,6 +93,12 @@ public: const nsIntPoint& aDestTopLeft) MOZ_OVERRIDE; static long ComputeStride(const gfxIntSize&, gfxImageFormat); + + virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const + MOZ_OVERRIDE; + virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const + MOZ_OVERRIDE; + protected: gfxImageSurface(); void InitWithData(unsigned char *aData, const gfxIntSize& aSize, diff --git a/image/src/imgFrame.cpp b/image/src/imgFrame.cpp index 1e11b09d5926..e1049f98bb2b 100644 --- a/image/src/imgFrame.cpp +++ b/image/src/imgFrame.cpp @@ -790,6 +790,9 @@ void imgFrame::SetCompositingFailed(bool val) mCompositingFailed = val; } +// If |aLocation| indicates this is heap memory, we try to measure things with +// |aMallocSizeOf|. If that fails (because the platform doesn't support it) or +// it's non-heap memory, we fall back to computing the size analytically. size_t imgFrame::SizeOfExcludingThisWithComputedFallbackIfHeap(gfxASurface::MemoryLocation aLocation, nsMallocSizeOfFun aMallocSizeOf) const { @@ -803,11 +806,11 @@ imgFrame::SizeOfExcludingThisWithComputedFallbackIfHeap(gfxASurface::MemoryLocat size_t n = 0; if (mPalettedImageData && aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP) { - size_t usable = aMallocSizeOf(mPalettedImageData); - if (!usable) { - usable = GetImageDataLength() + PaletteDataLength(); + size_t n2 = aMallocSizeOf(mPalettedImageData); + if (n2 == 0) { + n2 = GetImageDataLength() + PaletteDataLength(); } - n += usable; + n += n2; } // XXX: should pass aMallocSizeOf here. See bug 723827. @@ -822,7 +825,14 @@ imgFrame::SizeOfExcludingThisWithComputedFallbackIfHeap(gfxASurface::MemoryLocat } else #endif if (mImageSurface && aLocation == mImageSurface->GetMemoryLocation()) { - n += mImageSurface->KnownMemoryUsed(); + size_t n2 = 0; + if (aLocation == gfxASurface::MEMORY_IN_PROCESS_HEAP) { // HEAP: measure + n2 = mImageSurface->SizeOfIncludingThis(aMallocSizeOf); + } + if (n2 == 0) { // non-HEAP or computed fallback for HEAP + n2 = mImageSurface->KnownMemoryUsed(); + } + n += n2; } if (mOptSurface && aLocation == mOptSurface->GetMemoryLocation()) {