Bug 1248861: P2. Use AlignedByteBuffer with MediaRawData. r=cpearce

MozReview-Commit-ID: BGzpFEAisvE

--HG--
extra : rebase_source : 8d533f04754fcee112b05a1f35b02b04c6404a7b
This commit is contained in:
Jean-Yves Avenard 2016-04-03 21:05:23 +10:00
Родитель 66e8803cdb
Коммит b4a39f0f07
2 изменённых файлов: 13 добавлений и 109 удалений

Просмотреть файл

@ -486,35 +486,17 @@ VideoData::Create(const VideoInfo& aInfo,
}
#endif // MOZ_OMX_DECODER
// Alignment value - 1. 0 means that data isn't aligned.
// For 32-bytes aligned, use 31U.
#define RAW_DATA_ALIGNMENT 31U
MediaRawData::MediaRawData()
: MediaData(RAW_DATA, 0)
, mCrypto(mCryptoInternal)
, mData(nullptr)
, mSize(0)
, mBuffer(nullptr)
, mCapacity(0)
{
}
MediaRawData::MediaRawData(const uint8_t* aData, size_t aSize)
: MediaData(RAW_DATA, 0)
, mCrypto(mCryptoInternal)
, mData(nullptr)
, mSize(0)
, mBuffer(nullptr)
, mCapacity(0)
, mBuffer(aData, aSize)
{
if (!EnsureCapacity(aSize)) {
return;
}
// We ensure sufficient capacity above so this shouldn't fail.
memcpy(mData, aData, aSize);
mSize = aSize;
}
already_AddRefed<MediaRawData>
@ -529,46 +511,12 @@ MediaRawData::Clone() const
s->mExtraData = mExtraData;
s->mCryptoInternal = mCryptoInternal;
s->mTrackInfo = mTrackInfo;
if (mSize) {
if (!s->EnsureCapacity(mSize)) {
return nullptr;
}
memcpy(s->mData, mData, mSize);
s->mSize = mSize;
if (!s->mBuffer.Append(mBuffer.Data(), mBuffer.Length())) {
return nullptr;
}
return s.forget();
}
// EnsureCapacity ensures that the buffer is big enough to hold
// aSize. It doesn't set the mSize. It's up to the caller to adjust it.
bool
MediaRawData::EnsureCapacity(size_t aSize)
{
const size_t sizeNeeded = aSize + RAW_DATA_ALIGNMENT * 2;
if (mData && mCapacity >= sizeNeeded) {
return true;
}
auto newBuffer = MakeUniqueFallible<uint8_t[]>(sizeNeeded);
if (!newBuffer) {
return false;
}
// Find alignment address.
const uintptr_t alignmask = RAW_DATA_ALIGNMENT;
uint8_t* newData = reinterpret_cast<uint8_t*>(
(reinterpret_cast<uintptr_t>(newBuffer.get()) + alignmask) & ~alignmask);
MOZ_ASSERT(uintptr_t(newData) % (RAW_DATA_ALIGNMENT+1) == 0);
memcpy(newData, mData, mSize);
mBuffer = Move(newBuffer);
mCapacity = sizeNeeded;
mData = newData;
return true;
}
MediaRawData::~MediaRawData()
{
}
@ -577,7 +525,7 @@ size_t
MediaRawData::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
size_t size = aMallocSizeOf(this);
size += aMallocSizeOf(mBuffer.get());
size += mBuffer.SizeOfExcludingThis(aMallocSizeOf);
return size;
}
@ -593,69 +541,34 @@ MediaRawDataWriter::MediaRawDataWriter(MediaRawData* aMediaRawData)
{
}
bool
MediaRawDataWriter::EnsureSize(size_t aSize)
{
if (aSize <= mTarget->mSize) {
return true;
}
if (!mTarget->EnsureCapacity(aSize)) {
return false;
}
return true;
}
bool
MediaRawDataWriter::SetSize(size_t aSize)
{
if (aSize > mTarget->mSize && !EnsureSize(aSize)) {
return false;
}
mTarget->mSize = aSize;
return true;
return mTarget->mBuffer.SetLength(aSize);
}
bool
MediaRawDataWriter::Prepend(const uint8_t* aData, size_t aSize)
{
if (!EnsureSize(aSize + mTarget->mSize)) {
return false;
}
// Shift the data to the right by aSize to leave room for the new data.
memmove(mTarget->mData + aSize, mTarget->mData, mTarget->mSize);
memcpy(mTarget->mData, aData, aSize);
mTarget->mSize += aSize;
return true;
return mTarget->mBuffer.Prepend(aData, aSize);
}
bool
MediaRawDataWriter::Replace(const uint8_t* aData, size_t aSize)
{
// If aSize is smaller than our current size, we leave the buffer as is,
// only adjusting the reported size.
if (!EnsureSize(aSize)) {
return false;
}
memcpy(mTarget->mData, aData, aSize);
mTarget->mSize = aSize;
return true;
return mTarget->mBuffer.Replace(aData, aSize);
}
void
MediaRawDataWriter::Clear()
{
mTarget->mSize = 0;
mTarget->mData = nullptr;
mTarget->mBuffer.Clear();
}
uint8_t*
MediaRawDataWriter::Data()
{
return mTarget->mData;
return mTarget->mBuffer.Data();
}
size_t

Просмотреть файл

@ -623,12 +623,12 @@ public:
MediaRawData(const uint8_t* aData, size_t mSize);
// Pointer to data or null if not-yet allocated
const uint8_t* Data() const { return mData; }
const uint8_t* Data() const { return mBuffer.Data(); }
// Size of buffer.
size_t Size() const { return mSize; }
size_t Size() const { return mBuffer.Length(); }
size_t ComputedSizeOfIncludingThis() const
{
return sizeof(*this) + mCapacity;
return sizeof(*this) + mBuffer.ComputedSizeOfExcludingThis();
}
const CryptoSample& mCrypto;
@ -648,16 +648,7 @@ protected:
private:
friend class MediaRawDataWriter;
// Ensure that the backend buffer can hold aSize data. Will update mData.
// Will enforce that the start of allocated data is always 32 bytes
// aligned and that it has sufficient end padding to allow for 32 bytes block
// read as required by some data decoders.
// Returns false if memory couldn't be allocated.
bool EnsureCapacity(size_t aSize);
uint8_t* mData;
size_t mSize;
UniquePtr<uint8_t[]> mBuffer;
uint32_t mCapacity;
AlignedByteBuffer mBuffer;
CryptoSample mCryptoInternal;
MediaRawData(const MediaRawData&); // Not implemented
};