2013-07-30 02:59:51 -07: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-11 16:17:23 -07:00
|
|
|
#include "ImageDataSerializer.h"
|
|
|
|
#include "gfx2DGlue.h" // for SurfaceFormatToImageFormat
|
|
|
|
#include "gfxImageSurface.h" // for gfxImageSurface
|
|
|
|
#include "gfxPoint.h" // for gfxIntSize
|
|
|
|
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
|
|
|
|
#include "mozilla/gfx/2D.h" // for DataSourceSurface, Factory
|
|
|
|
#include "mozilla/gfx/Tools.h" // for GetAlignedStride, etc
|
|
|
|
#include "mozilla/mozalloc.h" // for operator delete, etc
|
2013-07-30 02:59:51 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-10-15 18:00:30 -07:00
|
|
|
using namespace gfx;
|
|
|
|
|
2013-07-30 02:59:51 -07: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
|
|
|
|
{
|
|
|
|
uint32_t width;
|
|
|
|
uint32_t height;
|
2013-10-15 18:00:30 -07:00
|
|
|
SurfaceFormat format;
|
2013-07-30 02:59:51 -07:00
|
|
|
|
|
|
|
static uint32_t GetOffset()
|
|
|
|
{
|
2013-10-15 18:00:30 -07:00
|
|
|
return GetAlignedStride<16>(sizeof(SurfaceBufferInfo));
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-08-08 05:53:12 -07:00
|
|
|
static SurfaceBufferInfo*
|
2014-02-20 13:04:11 -08:00
|
|
|
GetBufferInfo(uint8_t* aData, size_t aDataSize)
|
2013-07-30 02:59:51 -07:00
|
|
|
{
|
2014-02-20 13:04:11 -08:00
|
|
|
return aDataSize >= sizeof(SurfaceBufferInfo)
|
|
|
|
? reinterpret_cast<SurfaceBufferInfo*>(aData)
|
|
|
|
: nullptr;
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-15 18:00:30 -07:00
|
|
|
ImageDataSerializer::InitializeBufferInfo(IntSize aSize,
|
|
|
|
SurfaceFormat aFormat)
|
2013-07-30 02:59:51 -07:00
|
|
|
{
|
2014-02-20 13:04:11 -08:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
|
|
|
MOZ_ASSERT(info); // OK to assert here, this method is client-side-only
|
2013-07-30 02:59:51 -07:00
|
|
|
info->width = aSize.width;
|
|
|
|
info->height = aSize.height;
|
|
|
|
info->format = aFormat;
|
2014-02-20 13:04:11 -08:00
|
|
|
Validate();
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
2013-08-25 21:13:22 -07:00
|
|
|
static inline uint32_t
|
2013-10-15 18:00:30 -07:00
|
|
|
ComputeStride(SurfaceFormat aFormat, uint32_t aWidth)
|
2013-08-25 21:13:22 -07:00
|
|
|
{
|
2013-10-15 18:00:30 -07:00
|
|
|
return GetAlignedStride<4>(BytesPerPixel(aFormat) * aWidth);
|
2013-08-25 21:13:22 -07:00
|
|
|
}
|
|
|
|
|
2013-07-30 02:59:51 -07:00
|
|
|
uint32_t
|
2014-02-20 13:04:11 -08:00
|
|
|
ImageDataSerializerBase::ComputeMinBufferSize(IntSize aSize,
|
2013-10-15 18:00:30 -07:00
|
|
|
SurfaceFormat aFormat)
|
2013-07-30 02:59:51 -07:00
|
|
|
{
|
2013-08-25 21:13:22 -07:00
|
|
|
uint32_t bufsize = aSize.height * ComputeStride(aFormat, aSize.width);
|
2013-07-30 02:59:51 -07:00
|
|
|
return SurfaceBufferInfo::GetOffset()
|
2013-10-15 18:00:30 -07:00
|
|
|
+ GetAlignedStride<16>(bufsize);
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
2014-02-20 13:04:11 -08:00
|
|
|
void
|
|
|
|
ImageDataSerializerBase::Validate()
|
2013-07-30 02:59:51 -07:00
|
|
|
{
|
2014-02-20 13:04:11 -08: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 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t*
|
|
|
|
ImageDataSerializerBase::GetData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
|
|
|
return mData + SurfaceBufferInfo::GetOffset();
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:30 -07:00
|
|
|
uint32_t
|
|
|
|
ImageDataSerializerBase::GetStride() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-20 13:04:11 -08:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
2013-10-15 18:00:30 -07:00
|
|
|
return ComputeStride(GetFormat(), info->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
2013-07-30 02:59:51 -07:00
|
|
|
ImageDataSerializerBase::GetSize() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-20 13:04:11 -08:00
|
|
|
SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
|
2013-10-15 18:00:30 -07:00
|
|
|
return IntSize(info->width, info->height);
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:30 -07:00
|
|
|
SurfaceFormat
|
2013-07-30 02:59:51 -07:00
|
|
|
ImageDataSerializerBase::GetFormat() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-02-20 13:04:11 -08:00
|
|
|
return GetBufferInfo(mData, mDataSize)->format;
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<gfxImageSurface>
|
|
|
|
ImageDataSerializerBase::GetAsThebesSurface()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2013-10-15 18:00:30 -07:00
|
|
|
IntSize size = GetSize();
|
|
|
|
return new gfxImageSurface(GetData(),
|
|
|
|
gfxIntSize(size.width, size.height),
|
|
|
|
GetStride(),
|
|
|
|
SurfaceFormatToImageFormat(GetFormat()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<DrawTarget>
|
2014-03-17 14:35:20 -07:00
|
|
|
ImageDataSerializerBase::GetAsDrawTarget(gfx::BackendType aBackend)
|
2013-10-15 18:00:30 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2014-03-17 14:35:20 -07:00
|
|
|
return gfx::Factory::CreateDrawTargetForData(aBackend,
|
|
|
|
GetData(), GetSize(),
|
|
|
|
GetStride(), GetFormat());
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
2013-11-17 18:07:08 -08:00
|
|
|
TemporaryRef<gfx::DataSourceSurface>
|
2013-07-30 02:59:51 -07:00
|
|
|
ImageDataSerializerBase::GetAsSurface()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValid());
|
2013-10-15 18:00:30 -07:00
|
|
|
return Factory::CreateWrappingDataSourceSurface(GetData(),
|
|
|
|
GetStride(),
|
|
|
|
GetSize(),
|
|
|
|
GetFormat());
|
2013-07-30 02:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|