void
YCbCrImageDataSerializer::InitializeBufferInfo(uint32_t aYStride,
        uint32_t aCbCrStride,
        const gfx::IntSize& aYSize,
        const gfx::IntSize& aCbCrSize,
        StereoMode aStereoMode)
{
    uint32_t yOffset = 0;
    uint32_t cbOffset = yOffset + MOZ_ALIGN_WORD(aYStride * aYSize.height);
    uint32_t crOffset = cbOffset + MOZ_ALIGN_WORD(aCbCrStride * aCbCrSize.height);
    return InitializeBufferInfo(yOffset, cbOffset, crOffset,
                                aYStride, aCbCrStride, aYSize, aCbCrSize, aStereoMode);
}
void
YCbCrImageDataSerializer::InitializeBufferInfo(const gfx::IntSize& aYSize,
                                               const gfx::IntSize& aCbCrSize)
{
  YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
  info->mYOffset = MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
  info->mCbOffset = info->mYOffset
                  + MOZ_ALIGN_WORD(aYSize.width * aYSize.height);
  info->mCrOffset = info->mCbOffset
                  + MOZ_ALIGN_WORD(aCbCrSize.width * aCbCrSize.height);

  info->mYWidth = aYSize.width;
  info->mYHeight = aYSize.height;
  info->mCbCrWidth = aCbCrSize.width;
  info->mCbCrHeight = aCbCrSize.height;
}
void
YCbCrImageDataSerializer::InitializeBufferInfo(uint32_t aYOffset,
        uint32_t aCbOffset,
        uint32_t aCrOffset,
        uint32_t aYStride,
        uint32_t aCbCrStride,
        const gfx::IntSize& aYSize,
        const gfx::IntSize& aCbCrSize,
        StereoMode aStereoMode)
{
    YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData, mDataSize);
    MOZ_ASSERT(info); // OK to assert here, this method is client-side-only
    uint32_t info_size = MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
    info->mYOffset = info_size + aYOffset;
    info->mCbOffset = info_size + aCbOffset;
    info->mCrOffset = info_size + aCrOffset;
    info->mYStride = aYStride;
    info->mYWidth = aYSize.width;
    info->mYHeight = aYSize.height;
    info->mCbCrStride = aCbCrStride;
    info->mCbCrWidth = aCbCrSize.width;
    info->mCbCrHeight = aCbCrSize.height;
    info->mStereoMode = aStereoMode;
    Validate();
}
// Minimum required shmem size in bytes
size_t
YCbCrImageDataDeserializerBase::ComputeMinBufferSize(const gfx::IntSize& aYSize,
                                                   uint32_t aYStride,
                                                   const gfx::IntSize& aCbCrSize,
                                                   uint32_t aCbCrStride)
{
  return ComputeOffset(aYSize.height, aYStride)
         + 2 * ComputeOffset(aCbCrSize.height, aCbCrStride)
         + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
}
// Minimum required shmem size in bytes
size_t
YCbCrImageDataSerializer::ComputeMinBufferSize(const gfx::IntSize& aYSize,
                                               const gfx::IntSize& aCbCrSize)
{
  uint32_t yStride = aYSize.width;
  uint32_t CbCrStride = aCbCrSize.width;

  return ComputeOffset(aYSize.height, yStride)
         + 2 * ComputeOffset(aCbCrSize.height, CbCrStride)
         + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
}
// Minimum required shmem size in bytes
size_t
YCbCrImageDataDeserializerBase::ComputeMinBufferSize(const gfx::IntSize& aYSize,
        uint32_t aYStride,
        const gfx::IntSize& aCbCrSize,
        uint32_t aCbCrStride)
{
    MOZ_ASSERT(aYSize.height >= 0 && aYSize.width >= 0);
    if (aYSize.height <= 0 || aYSize.width <= 0 || aCbCrSize.height <= 0 || aCbCrSize.width <= 0) {
        gfxDebug() << "Non-positive YCbCr buffer size request " << aYSize.height << "x" << aYSize.width << ", " << aCbCrSize.height << "x" << aCbCrSize.width;
        return 0;
    }
    return ComputeOffset(aYSize.height, aYStride)
           + 2 * ComputeOffset(aCbCrSize.height, aCbCrStride)
           + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
}
gfxShmSharedReadLock::gfxShmSharedReadLock(ISurfaceAllocator* aAllocator)
  : mAllocator(aAllocator)
  , mAllocSuccess(false)
{
  MOZ_COUNT_CTOR(gfxShmSharedReadLock);
  MOZ_ASSERT(mAllocator);
  if (mAllocator) {
#define MOZ_ALIGN_WORD(x) (((x) + 3) & ~3)
    if (mAllocator->AllocShmemSection(MOZ_ALIGN_WORD(sizeof(ShmReadLockInfo)), &mShmemSection)) {
      ShmReadLockInfo* info = GetShmReadLockInfoPtr();
      info->readCount = 1;
      mAllocSuccess = true;
    }
  }
}
uint8_t* YCbCrImageDataDeserializerBase::GetData()
{
  YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
  return (reinterpret_cast<uint8_t*>(info)) + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
}
// Minimum required shmem size in bytes
size_t
YCbCrImageDataSerializer::ComputeMinBufferSize(uint32_t aSize)
{
  return ComputeOffset(aSize) + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
}
// Offset in bytes
static size_t ComputeOffset(uint32_t aSize)
{
  return MOZ_ALIGN_WORD(aSize);
}
// Offset in bytes
static size_t ComputeOffset(uint32_t aHeight, uint32_t aStride)
{
  return MOZ_ALIGN_WORD(aHeight * aStride);
}