2013-07-30 13:59:51 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "ImageDataSerializer.h"
|
|
|
|
#include "gfx2DGlue.h" // for SurfaceFormatToImageFormat
|
2015-05-29 18:01:46 +03:00
|
|
|
#include "mozilla/gfx/Point.h" // for IntSize
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
|
|
|
|
#include "mozilla/gfx/2D.h" // for DataSourceSurface, Factory
|
2014-12-09 21:19:29 +03:00
|
|
|
#include "mozilla/gfx/Logging.h" // for gfxDebug
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "mozilla/gfx/Tools.h" // for GetAlignedStride, etc
|
|
|
|
#include "mozilla/mozalloc.h" // for operator delete, etc
|
2013-07-30 13:59:51 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-10-16 05:00:30 +04:00
|
|
|
using namespace gfx;
|
|
|
|
|
2013-07-30 13:59:51 +04:00
|
|
|
// The Data is layed out as follows:
|
|
|
|
//
|
|
|
|
// +-------------------+ -++ --+ <-- ImageDataSerializerBase::mData pointer
|
|
|
|
// | SurfaceBufferInfo | | |
|
|
|
|
// +-------------------+ --+ | offset
|
|
|
|
// | ... | |
|
|
|
|
// +-------------------+ ------+
|
|
|
|
// | |
|
|
|
|
// | data |
|
|
|
|
// | |
|
|
|
|
// +-------------------+
|
|
|
|
|
|
|
|
// Structure written at the beginning of the data blob containing the image
|
|
|
|
// (as shown in the figure above). It contains the necessary informations to
|
|
|
|
// read the image in the blob.
|
|
|
|
namespace {
|
|
|
|
struct SurfaceBufferInfo
|
|
|
|
{
|
2014-11-27 06:00:32 +03:00
|
|
|
int32_t width;
|
|
|
|
int32_t height;
|
2013-10-16 05:00:30 +04:00
|
|
|
SurfaceFormat format;
|
2013-07-30 13:59:51 +04:00
|
|
|
|
2014-11-27 06:00:32 +03:00
|
|
|
static int32_t GetOffset()
|
2013-07-30 13:59:51 +04:00
|
|
|
{
|
2013-10-16 05:00:30 +04:00
|
|
|
return GetAlignedStride<16>(sizeof(SurfaceBufferInfo));
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-07-30 13:59:51 +04:00
|
|
|
|
2013-08-08 16:53:12 +04:00
|
|
|
static SurfaceBufferInfo*
|
2014-02-21 01:04:11 +04:00
|
|
|
GetBufferInfo(uint8_t* aData, size_t aDataSize)
|
2013-07-30 13:59:51 +04:00
|
|
|
{
|
2014-02-21 01:04:11 +04:00
|
|
|
return aDataSize >= sizeof(SurfaceBufferInfo)
|
|
|
|
? reinterpret_cast<SurfaceBufferInfo*>(aData)
|
|
|
|
: nullptr;
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-16 05:00:30 +04:00
|
|
|
ImageDataSerializer::InitializeBufferInfo(IntSize aSize,
|
|
|
|
SurfaceFormat aFormat)
|
2013-07-30 13:59:51 +04:00
|
|
|
{
|
2014-02-21 01:04:11 +04:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
|
|
|
MOZ_ASSERT(info); // OK to assert here, this method is client-side-only
|
2013-07-30 13:59:51 +04:00
|
|
|
info->width = aSize.width;
|
|
|
|
info->height = aSize.height;
|
|
|
|
info->format = aFormat;
|
2014-02-21 01:04:11 +04:00
|
|
|
Validate();
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 06:00:32 +03:00
|
|
|
static inline int32_t
|
|
|
|
ComputeStride(SurfaceFormat aFormat, int32_t aWidth)
|
2013-08-26 08:13:22 +04:00
|
|
|
{
|
2014-11-27 06:00:32 +03:00
|
|
|
CheckedInt<int32_t> size = BytesPerPixel(aFormat);
|
2014-12-09 21:19:29 +03:00
|
|
|
size *= aWidth;
|
2014-11-27 06:00:32 +03:00
|
|
|
if (!size.isValid() || size.value() <= 0) {
|
2014-12-09 21:19:29 +03:00
|
|
|
gfxDebug() << "ComputeStride overflow " << aWidth;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetAlignedStride<4>(size.value());
|
2013-08-26 08:13:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-30 13:59:51 +04:00
|
|
|
uint32_t
|
2014-02-21 01:04:11 +04:00
|
|
|
ImageDataSerializerBase::ComputeMinBufferSize(IntSize aSize,
|
2014-12-09 21:19:29 +03:00
|
|
|
SurfaceFormat aFormat)
|
2013-07-30 13:59:51 +04:00
|
|
|
{
|
2014-12-09 21:19:29 +03:00
|
|
|
MOZ_ASSERT(aSize.height >= 0 && aSize.width >= 0);
|
|
|
|
if (aSize.height <= 0 || aSize.width <= 0) {
|
|
|
|
gfxDebug() << "Non-positive image buffer size request " << aSize.width << "x" << aSize.height;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 06:00:32 +03:00
|
|
|
CheckedInt<int32_t> bufsize = ComputeStride(aFormat, aSize.width);
|
2014-12-09 21:19:29 +03:00
|
|
|
bufsize *= aSize.height;
|
|
|
|
|
2014-11-27 06:00:32 +03:00
|
|
|
if (!bufsize.isValid() || bufsize.value() <= 0) {
|
2014-12-09 21:19:29 +03:00
|
|
|
gfxDebug() << "Buffer size overflow " << aSize.width << "x" << aSize.height;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 13:59:51 +04:00
|
|
|
return SurfaceBufferInfo::GetOffset()
|
2014-12-09 21:19:29 +03:00
|
|
|
+ GetAlignedStride<16>(bufsize.value());
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
2014-02-21 01:04:11 +04:00
|
|
|
void
|
|
|
|
ImageDataSerializerBase::Validate()
|
2013-07-30 13:59:51 +04:00
|
|
|
{
|
2014-02-21 01:04:11 +04:00
|
|
|
mIsValid = false;
|
|
|
|
if (!mData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
|
|
|
if (!info) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t requiredSize =
|
|
|
|
ComputeMinBufferSize(IntSize(info->width, info->height), info->format);
|
|
|
|
mIsValid = requiredSize <= mDataSize;
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t*
|
|
|
|
ImageDataSerializerBase::GetData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
|
|
|
return mData + SurfaceBufferInfo::GetOffset();
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:00:30 +04:00
|
|
|
uint32_t
|
|
|
|
ImageDataSerializerBase::GetStride() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-21 01:04:11 +04:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
2013-10-16 05:00:30 +04:00
|
|
|
return ComputeStride(GetFormat(), info->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
2013-07-30 13:59:51 +04:00
|
|
|
ImageDataSerializerBase::GetSize() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-21 01:04:11 +04:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
2013-10-16 05:00:30 +04:00
|
|
|
return IntSize(info->width, info->height);
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
2013-10-16 05:00:30 +04:00
|
|
|
SurfaceFormat
|
2013-07-30 13:59:51 +04:00
|
|
|
ImageDataSerializerBase::GetFormat() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-21 01:04:11 +04:00
|
|
|
return GetBufferInfo(mData, mDataSize)->format;
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
2014-03-18 01:35:20 +04:00
|
|
|
ImageDataSerializerBase::GetAsDrawTarget(gfx::BackendType aBackend)
|
2013-10-16 05:00:30 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-03-18 01:35:20 +04:00
|
|
|
return gfx::Factory::CreateDrawTargetForData(aBackend,
|
|
|
|
GetData(), GetSize(),
|
|
|
|
GetStride(), GetFormat());
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
2013-07-30 13:59:51 +04:00
|
|
|
ImageDataSerializerBase::GetAsSurface()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2013-10-16 05:00:30 +04:00
|
|
|
return Factory::CreateWrappingDataSourceSurface(GetData(),
|
|
|
|
GetStride(),
|
|
|
|
GetSize(),
|
|
|
|
GetFormat());
|
2013-07-30 13:59:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|