2010-08-07 09:09:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-03-02 02:09:35 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2010-03-02 02:09:35 +03:00
|
|
|
|
|
|
|
#include "ImageLayers.h"
|
|
|
|
#include "BasicLayers.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "gfxQuartzImageSurface.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
|
2011-09-28 02:19:24 +04:00
|
|
|
#include "gfxUtils.h"
|
2010-04-19 05:17:06 +04:00
|
|
|
|
2010-12-15 21:17:27 +03:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
using mozilla::ReentrantMonitor;
|
2010-03-02 02:09:35 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
class BasicPlanarYCbCrImage : public PlanarYCbCrImage
|
|
|
|
{
|
2010-03-02 02:09:35 +03:00
|
|
|
public:
|
2012-02-01 06:18:30 +04:00
|
|
|
BasicPlanarYCbCrImage(const gfxIntSize& aScaleHint, gfxImageFormat aOffscreenFormat, BufferRecycleBin *aRecycleBin)
|
|
|
|
: PlanarYCbCrImage(aRecycleBin)
|
|
|
|
, mScaleHint(aScaleHint)
|
2012-02-16 02:32:11 +04:00
|
|
|
{
|
|
|
|
SetOffscreenFormat(aOffscreenFormat);
|
|
|
|
}
|
2010-03-02 02:09:35 +03:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
~BasicPlanarYCbCrImage()
|
2010-03-02 02:09:35 +03:00
|
|
|
{
|
2012-02-01 06:18:30 +04:00
|
|
|
if (mDecodedBuffer) {
|
|
|
|
// Right now this only happens if the Image was never drawn, otherwise
|
|
|
|
// this will have been tossed away at surface destruction.
|
|
|
|
mRecycleBin->RecycleBuffer(mDecodedBuffer.forget(), mSize.height * mStride);
|
|
|
|
}
|
2010-03-02 02:09:35 +03:00
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
virtual void SetData(const Data& aData);
|
|
|
|
already_AddRefed<gfxASurface> GetAsSurface();
|
2010-03-02 02:09:35 +03:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
private:
|
|
|
|
gfxIntSize mScaleHint;
|
|
|
|
int mStride;
|
|
|
|
nsAutoArrayPtr<PRUint8> mDecodedBuffer;
|
2010-03-02 02:09:35 +03:00
|
|
|
};
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
class BasicImageFactory : public ImageFactory
|
|
|
|
{
|
2010-03-02 02:09:35 +03:00
|
|
|
public:
|
2012-02-01 06:18:30 +04:00
|
|
|
BasicImageFactory() {}
|
2010-03-02 02:09:35 +03:00
|
|
|
|
2012-08-19 23:33:25 +04:00
|
|
|
virtual already_AddRefed<Image> CreateImage(const ImageFormat* aFormats,
|
2012-02-01 06:18:30 +04:00
|
|
|
PRUint32 aNumFormats,
|
|
|
|
const gfxIntSize &aScaleHint,
|
|
|
|
BufferRecycleBin *aRecycleBin)
|
|
|
|
{
|
|
|
|
if (!aNumFormats) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
2010-12-15 21:17:27 +03:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
nsRefPtr<Image> image;
|
2012-08-19 23:33:25 +04:00
|
|
|
if (aFormats[0] == ImageFormat::PLANAR_YCBCR) {
|
2012-02-01 06:18:30 +04:00
|
|
|
image = new BasicPlanarYCbCrImage(aScaleHint, gfxPlatform::GetPlatform()->GetOffscreenFormat(), aRecycleBin);
|
|
|
|
return image.forget();
|
|
|
|
}
|
2011-07-22 07:17:23 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
return ImageFactory::CreateImage(aFormats, aNumFormats, aScaleHint, aRecycleBin);
|
|
|
|
}
|
2010-03-02 02:09:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicPlanarYCbCrImage::SetData(const Data& aData)
|
|
|
|
{
|
2012-02-01 06:18:30 +04:00
|
|
|
PlanarYCbCrImage::SetData(aData);
|
|
|
|
|
2010-03-02 02:09:35 +03:00
|
|
|
// Do some sanity checks to prevent integer overflow
|
2011-11-15 00:35:46 +04:00
|
|
|
if (aData.mYSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
|
|
|
|
aData.mYSize.height > PlanarYCbCrImage::MAX_DIMENSION) {
|
|
|
|
NS_ERROR("Illegal image source width or height");
|
2010-03-02 02:09:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2012-02-01 06:18:30 +04:00
|
|
|
|
2011-04-21 08:38:39 +04:00
|
|
|
gfxASurface::gfxImageFormat format = GetOffscreenFormat();
|
|
|
|
|
2011-09-28 02:19:24 +04:00
|
|
|
gfxIntSize size(mScaleHint);
|
|
|
|
gfxUtils::GetYCbCrToRGBDestFormatAndSize(aData, format, size);
|
2011-11-15 00:35:46 +04:00
|
|
|
if (size.width > PlanarYCbCrImage::MAX_DIMENSION ||
|
|
|
|
size.height > PlanarYCbCrImage::MAX_DIMENSION) {
|
|
|
|
NS_ERROR("Illegal image dest width or height");
|
|
|
|
return;
|
|
|
|
}
|
2010-10-26 07:11:13 +04:00
|
|
|
|
2011-03-16 01:01:00 +03:00
|
|
|
mStride = gfxASurface::FormatStrideForWidth(format, size.width);
|
2012-02-01 06:18:30 +04:00
|
|
|
mDecodedBuffer = AllocateBuffer(size.height * mStride);
|
|
|
|
if (!mDecodedBuffer) {
|
2010-03-02 02:09:35 +03:00
|
|
|
// out of memory
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
gfxUtils::ConvertYCbCrToRGB(aData, format, size, mDecodedBuffer, mStride);
|
2011-03-12 03:07:00 +03:00
|
|
|
SetOffscreenFormat(format);
|
2010-10-26 07:11:13 +04:00
|
|
|
mSize = size;
|
2010-03-02 02:09:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static cairo_user_data_key_t imageSurfaceDataKey;
|
|
|
|
|
|
|
|
static void
|
|
|
|
DestroyBuffer(void* aBuffer)
|
|
|
|
{
|
|
|
|
delete[] static_cast<PRUint8*>(aBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
BasicPlanarYCbCrImage::GetAsSurface()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be main thread");
|
|
|
|
|
|
|
|
if (mSurface) {
|
|
|
|
nsRefPtr<gfxASurface> result = mSurface.get();
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
if (!mDecodedBuffer) {
|
|
|
|
return PlanarYCbCrImage::GetAsSurface();
|
2010-03-02 02:09:35 +03:00
|
|
|
}
|
2010-12-10 22:32:52 +03:00
|
|
|
|
|
|
|
gfxASurface::gfxImageFormat format = GetOffscreenFormat();
|
|
|
|
|
2010-03-02 02:09:35 +03:00
|
|
|
nsRefPtr<gfxImageSurface> imgSurface =
|
2012-02-01 06:18:30 +04:00
|
|
|
new gfxImageSurface(mDecodedBuffer, mSize, mStride, format);
|
2011-03-16 01:01:00 +03:00
|
|
|
if (!imgSurface || imgSurface->CairoStatus() != 0) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-02 02:09:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass ownership of the buffer to the surface
|
2012-02-01 06:18:30 +04:00
|
|
|
imgSurface->SetData(&imageSurfaceDataKey, mDecodedBuffer.forget(), DestroyBuffer);
|
2010-03-02 02:09:35 +03:00
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> result = imgSurface.get();
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
nsRefPtr<gfxQuartzImageSurface> quartzSurface =
|
|
|
|
new gfxQuartzImageSurface(imgSurface);
|
|
|
|
if (quartzSurface) {
|
|
|
|
result = quartzSurface.forget();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
mSurface = result;
|
2010-03-02 02:09:35 +03:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
return result.forget();
|
2010-10-26 07:11:13 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
ImageFactory*
|
|
|
|
BasicLayerManager::GetImageFactory()
|
2010-08-07 09:09:18 +04:00
|
|
|
{
|
2012-02-01 06:18:30 +04:00
|
|
|
if (!mFactory) {
|
|
|
|
mFactory = new BasicImageFactory();
|
2010-08-07 09:09:18 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
return mFactory.get();
|
2010-03-02 02:09:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|