Bug 1216644 - part 3 - make BufferRecycleBin store UniquePtrs; r=jrmuizel

Changing mRecycledBuffers to store UniquePtrs instead of nsAutoArrayPtrs
opens up the possibility of a reasonable facsimile of ownership in
function signatures.
This commit is contained in:
Nathan Froyd 2015-10-20 13:16:04 -04:00
parent ccb2eeeffe
commit 3ce57b5162
3 changed files with 24 additions and 24 deletions

View File

@ -116,7 +116,7 @@ BufferRecycleBin::BufferRecycleBin()
}
void
BufferRecycleBin::RecycleBuffer(uint8_t* aBuffer, uint32_t aSize)
BufferRecycleBin::RecycleBuffer(UniquePtr<uint8_t[]> aBuffer, uint32_t aSize)
{
MutexAutoLock lock(mLock);
@ -124,19 +124,19 @@ BufferRecycleBin::RecycleBuffer(uint8_t* aBuffer, uint32_t aSize)
mRecycledBuffers.Clear();
}
mRecycledBufferSize = aSize;
mRecycledBuffers.AppendElement(aBuffer);
mRecycledBuffers.AppendElement(Move(aBuffer));
}
uint8_t*
UniquePtr<uint8_t[]>
BufferRecycleBin::GetBuffer(uint32_t aSize)
{
MutexAutoLock lock(mLock);
if (mRecycledBuffers.IsEmpty() || mRecycledBufferSize != aSize)
return new uint8_t[aSize];
return MakeUnique<uint8_t[]>(aSize);
uint32_t last = mRecycledBuffers.Length() - 1;
uint8_t* result = mRecycledBuffers[last].forget();
UniquePtr<uint8_t[]> result = Move(mRecycledBuffers[last]);
mRecycledBuffers.RemoveElementAt(last);
return result;
}
@ -439,7 +439,7 @@ PlanarYCbCrImage::PlanarYCbCrImage()
RecyclingPlanarYCbCrImage::~RecyclingPlanarYCbCrImage()
{
if (mBuffer) {
mRecycleBin->RecycleBuffer(mBuffer.forget(), mBufferSize);
mRecycleBin->RecycleBuffer(Move(mBuffer), mBufferSize);
}
}
@ -454,7 +454,7 @@ RecyclingPlanarYCbCrImage::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
// - mImplData is not used
// Not owned:
// - mRecycleBin
size_t size = mBuffer.SizeOfExcludingThis(aMallocSizeOf);
size_t size = aMallocSizeOf(mBuffer.get());
// Could add in the future:
// - mBackendData (from base class)
@ -462,7 +462,7 @@ RecyclingPlanarYCbCrImage::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
return size;
}
uint8_t*
UniquePtr<uint8_t[]>
RecyclingPlanarYCbCrImage::AllocateBuffer(uint32_t aSize)
{
return mRecycleBin->GetBuffer(aSize);
@ -509,7 +509,7 @@ RecyclingPlanarYCbCrImage::CopyData(const Data& aData)
// update buffer size
mBufferSize = size;
mData.mYChannel = mBuffer;
mData.mYChannel = mBuffer.get();
mData.mCbChannel = mData.mYChannel + mData.mYStride * mData.mYSize.height;
mData.mCrChannel = mData.mCbChannel + mData.mCbCrStride * mData.mCbCrSize.height;
@ -555,7 +555,7 @@ RecyclingPlanarYCbCrImage::AllocateAndGetNewBuffer(uint32_t aSize)
// update buffer size
mBufferSize = aSize;
}
return mBuffer;
return mBuffer.get();
}
already_AddRefed<gfx::SourceSurface>

View File

@ -30,6 +30,7 @@
#include "mozilla/gfx/2D.h"
#include "nsDataHashtable.h"
#include "mozilla/EnumeratedArray.h"
#include "mozilla/UniquePtr.h"
#ifndef XPCOM_GLUE_AVOID_NSPR
/**
@ -203,9 +204,9 @@ class BufferRecycleBin final {
public:
BufferRecycleBin();
void RecycleBuffer(uint8_t* aBuffer, uint32_t aSize);
void RecycleBuffer(mozilla::UniquePtr<uint8_t[]> aBuffer, uint32_t aSize);
// Returns a recycled buffer of the right size, or allocates a new buffer.
uint8_t* GetBuffer(uint32_t aSize);
mozilla::UniquePtr<uint8_t[]> GetBuffer(uint32_t aSize);
private:
typedef mozilla::Mutex Mutex;
@ -221,7 +222,7 @@ private:
// We should probably do something to prune this list on a timer so we don't
// eat excess memory while video is paused...
nsTArray<nsAutoArrayPtr<uint8_t> > mRecycledBuffers;
nsTArray<mozilla::UniquePtr<uint8_t[]>> mRecycledBuffers;
// This is only valid if mRecycledBuffers is non-empty
uint32_t mRecycledBufferSize;
};
@ -733,13 +734,11 @@ protected:
/**
* Return a buffer to store image data in.
* The default implementation returns memory that can
* be freed wit delete[]
*/
uint8_t* AllocateBuffer(uint32_t aSize);
mozilla::UniquePtr<uint8_t[]> AllocateBuffer(uint32_t aSize);
RefPtr<BufferRecycleBin> mRecycleBin;
nsAutoArrayPtr<uint8_t> mBuffer;
mozilla::UniquePtr<uint8_t[]> mBuffer;
};
/**

View File

@ -12,7 +12,8 @@
#include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat
#include "gfxUtils.h" // for gfxUtils
#include "mozilla/mozalloc.h" // for operator delete[], etc
#include "nsAutoPtr.h" // for nsRefPtr, nsAutoArrayPtr
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"
#include "nsAutoRef.h" // for nsCountedRef
#include "nsCOMPtr.h" // for already_AddRefed
#include "nsDebug.h" // for NS_ERROR, NS_ASSERTION
@ -44,7 +45,7 @@ public:
if (mDecodedBuffer) {
// Right now this only happens if the Image was never drawn, otherwise
// this will have been tossed away at surface destruction.
mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride);
mRecycleBin->RecycleBuffer(Move(mDecodedBuffer), mSize.height * mStride);
}
}
@ -61,12 +62,12 @@ public:
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
{
size_t size = RecyclingPlanarYCbCrImage::SizeOfExcludingThis(aMallocSizeOf);
size += mDecodedBuffer.SizeOfExcludingThis(aMallocSizeOf);
size += aMallocSizeOf(mDecodedBuffer.get());
return size;
}
private:
nsAutoArrayPtr<uint8_t> mDecodedBuffer;
UniquePtr<uint8_t[]> mDecodedBuffer;
gfx::IntSize mScaleHint;
int mStride;
bool mDelayedConversion;
@ -125,7 +126,7 @@ BasicPlanarYCbCrImage::SetData(const Data& aData)
return false;
}
gfx::ConvertYCbCrToRGB(aData, format, size, mDecodedBuffer, mStride);
gfx::ConvertYCbCrToRGB(aData, format, size, mDecodedBuffer.get(), mStride);
SetOffscreenFormat(iFormat);
mSize = size;
@ -154,7 +155,7 @@ BasicPlanarYCbCrImage::GetAsSourceSurface()
// We create the target out of mDecodedBuffer, and get a snapshot from it.
// The draw target is destroyed on scope exit and the surface owns the data.
RefPtr<gfx::DrawTarget> drawTarget
= gfxPlatform::GetPlatform()->CreateDrawTargetForData(mDecodedBuffer,
= gfxPlatform::GetPlatform()->CreateDrawTargetForData(mDecodedBuffer.get(),
mSize,
mStride,
gfx::ImageFormatToSurfaceFormat(format));
@ -165,7 +166,7 @@ BasicPlanarYCbCrImage::GetAsSourceSurface()
surface = drawTarget->Snapshot();
}
mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride);
mRecycleBin->RecycleBuffer(Move(mDecodedBuffer), mSize.height * mStride);
mSourceSurface = surface;
return surface.forget();