2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 19:27:30 +04:00
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "SourceSurfaceRawData.h"
|
2014-06-14 01:03:52 +04:00
|
|
|
|
|
|
|
#include "DataSurfaceHelpers.h"
|
2012-05-21 19:27:30 +04:00
|
|
|
#include "Logging.h"
|
2014-06-14 01:03:52 +04:00
|
|
|
#include "mozilla/Types.h" // for decltype
|
2012-05-21 19:27:30 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
void SourceSurfaceRawData::InitWrappingData(
|
|
|
|
uint8_t* aData, const IntSize& aSize, int32_t aStride,
|
2016-05-10 21:44:00 +03:00
|
|
|
SurfaceFormat aFormat, Factory::SourceSurfaceDeallocator aDeallocator,
|
|
|
|
void* aClosure) {
|
2012-05-21 19:27:30 +04:00
|
|
|
mRawData = aData;
|
|
|
|
mSize = aSize;
|
|
|
|
mStride = aStride;
|
|
|
|
mFormat = aFormat;
|
2016-05-10 21:44:00 +03:00
|
|
|
|
|
|
|
if (aDeallocator) {
|
|
|
|
mOwnData = true;
|
|
|
|
}
|
|
|
|
mDeallocator = aDeallocator;
|
|
|
|
mClosure = aClosure;
|
2012-05-21 19:27:30 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 20:01:47 +04:00
|
|
|
void SourceSurfaceRawData::GuaranteePersistance() {
|
|
|
|
if (mOwnData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 22:26:29 +03:00
|
|
|
MOZ_ASSERT(!mDeallocator);
|
2014-08-01 20:01:47 +04:00
|
|
|
uint8_t* oldData = mRawData;
|
|
|
|
mRawData = new uint8_t[mStride * mSize.height];
|
|
|
|
|
|
|
|
memcpy(mRawData, oldData, mStride * mSize.height);
|
|
|
|
mOwnData = true;
|
|
|
|
}
|
|
|
|
|
2020-02-06 01:22:13 +03:00
|
|
|
void SourceSurfaceRawData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
SizeOfInfo& aInfo) const {
|
|
|
|
aInfo.AddType(SurfaceType::DATA);
|
|
|
|
if (mDeallocator) {
|
|
|
|
aInfo.mUnknownBytes = mStride * mSize.height;
|
|
|
|
} else if (mOwnData) {
|
|
|
|
aInfo.mHeapBytes = mStride * mSize.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 18:07:44 +04:00
|
|
|
bool SourceSurfaceAlignedRawData::Init(const IntSize& aSize,
|
2014-09-11 01:54:16 +04:00
|
|
|
SurfaceFormat aFormat, bool aClearMem,
|
2016-07-26 02:36:36 +03:00
|
|
|
uint8_t aClearValue, int32_t aStride) {
|
2013-07-17 18:07:44 +04:00
|
|
|
mFormat = aFormat;
|
2016-08-25 20:57:39 +03:00
|
|
|
mStride = aStride ? aStride
|
|
|
|
: GetAlignedStride<16>(aSize.width, BytesPerPixel(aFormat));
|
2014-06-14 01:03:52 +04:00
|
|
|
|
|
|
|
size_t bufLen = BufferSizeFromStrideAndHeight(mStride, aSize.height);
|
|
|
|
if (bufLen > 0) {
|
2016-07-26 02:36:36 +03:00
|
|
|
bool zeroMem = aClearMem && !aClearValue;
|
2014-06-14 01:03:52 +04:00
|
|
|
static_assert(sizeof(decltype(mArray[0])) == 1,
|
|
|
|
"mArray.Realloc() takes an object count, so its objects must "
|
|
|
|
"be 1-byte sized if we use bufLen");
|
|
|
|
|
2016-07-26 02:36:36 +03:00
|
|
|
// AlignedArray uses cmalloc to zero mem for a fast path.
|
|
|
|
mArray.Realloc(/* actually an object count */ bufLen, zeroMem);
|
2014-06-14 01:03:52 +04:00
|
|
|
mSize = aSize;
|
2016-07-26 02:36:36 +03:00
|
|
|
|
|
|
|
if (mArray && aClearMem && aClearValue) {
|
|
|
|
memset(mArray, aClearValue, mStride * aSize.height);
|
|
|
|
}
|
2014-06-14 01:03:52 +04:00
|
|
|
} else {
|
|
|
|
mArray.Dealloc();
|
|
|
|
mSize.SizeTo(0, 0);
|
|
|
|
}
|
2013-12-19 19:31:07 +04:00
|
|
|
|
|
|
|
return mArray != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-06 01:22:13 +03:00
|
|
|
void SourceSurfaceAlignedRawData::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf, SizeOfInfo& aInfo) const {
|
|
|
|
aInfo.AddType(SurfaceType::DATA_ALIGNED);
|
|
|
|
aInfo.mHeapBytes = mArray.HeapSizeOfExcludingThis(aMallocSizeOf);
|
2018-02-22 22:26:29 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|