Bug 715785: Make ImageContainers independent of LayerManagers. r=roc

This commit is contained in:
Bas Schouten
2012-02-01 03:18:30 +01:00
parent 4762ea78a3
commit 33c8caf378
41 changed files with 1268 additions and 2097 deletions

View File

@@ -56,106 +56,70 @@ using mozilla::ReentrantMonitor;
namespace mozilla {
namespace layers {
/**
* All our images can yield up a cairo surface and their size.
*/
class BasicImageImplData {
class BasicPlanarYCbCrImage : public PlanarYCbCrImage
{
public:
/**
* This must be called on the main thread.
*/
virtual already_AddRefed<gfxASurface> GetAsSurface() = 0;
BasicPlanarYCbCrImage(const gfxIntSize& aScaleHint, gfxImageFormat aOffscreenFormat, BufferRecycleBin *aRecycleBin)
: PlanarYCbCrImage(aRecycleBin)
, mScaleHint(aScaleHint)
, mOffscreenFormat(aOffscreenFormat)
{}
gfxIntSize GetSize() { return mSize; }
protected:
gfxIntSize mSize;
};
/**
* Since BasicLayers only paint on the main thread, handling a CairoImage
* is extremely simple. We just hang on to a reference to the surface and
* return that surface when BasicImageLayer::Paint asks for it via
* BasicImageContainer::GetAsSurface.
*/
class BasicCairoImage : public CairoImage, public BasicImageImplData {
public:
BasicCairoImage() : CairoImage(static_cast<BasicImageImplData*>(this)) {}
virtual void SetData(const Data& aData)
~BasicPlanarYCbCrImage()
{
mSurface = aData.mSurface;
mSize = aData.mSize;
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);
}
}
virtual already_AddRefed<gfxASurface> GetAsSurface()
{
NS_ASSERTION(NS_IsMainThread(), "Must be main thread");
nsRefPtr<gfxASurface> surface = mSurface.get();
return surface.forget();
}
protected:
nsCountedRef<nsMainThreadSurfaceRef> mSurface;
};
/**
* We handle YCbCr by converting to RGB when the image is initialized
* (which should be done off the main thread). The RGB results are stored
* in a memory buffer and converted to a cairo surface lazily.
*/
class BasicPlanarYCbCrImage : public PlanarYCbCrImage, public BasicImageImplData {
typedef gfxASurface::gfxImageFormat gfxImageFormat;
public:
/**
* aScaleHint is a size that the image is expected to be rendered at.
* This is a hint for image backends to optimize scaling.
*/
BasicPlanarYCbCrImage(const gfxIntSize& aScaleHint) :
PlanarYCbCrImage(static_cast<BasicImageImplData*>(this)),
mScaleHint(aScaleHint),
mOffscreenFormat(gfxASurface::ImageFormatUnknown),
mDelayedConversion(false)
{}
virtual void SetData(const Data& aData);
virtual void SetDelayedConversion(bool aDelayed) { mDelayedConversion = aDelayed; }
already_AddRefed<gfxASurface> GetAsSurface();
virtual already_AddRefed<gfxASurface> GetAsSurface();
private:
gfxIntSize mScaleHint;
gfxImageFormat mOffscreenFormat;
int mStride;
nsAutoArrayPtr<PRUint8> mDecodedBuffer;
};
const Data* GetData() { return &mData; }
class BasicImageFactory : public ImageFactory
{
public:
BasicImageFactory() {}
void SetOffscreenFormat(gfxImageFormat aFormat) { mOffscreenFormat = aFormat; }
gfxImageFormat GetOffscreenFormat() { return mOffscreenFormat; }
virtual already_AddRefed<Image> CreateImage(const Image::Format* aFormats,
PRUint32 aNumFormats,
const gfxIntSize &aScaleHint,
BufferRecycleBin *aRecycleBin)
{
if (!aNumFormats) {
return nsnull;
}
PRUint32 GetDataSize() { return mBuffer ? mDelayedConversion ? mBufferSize : mSize.height * mStride : 0; }
nsRefPtr<Image> image;
if (aFormats[0] == Image::PLANAR_YCBCR) {
image = new BasicPlanarYCbCrImage(aScaleHint, gfxPlatform::GetPlatform()->GetOffscreenFormat(), aRecycleBin);
return image.forget();
}
protected:
nsAutoArrayPtr<PRUint8> mBuffer;
nsCountedRef<nsMainThreadSurfaceRef> mSurface;
gfxIntSize mScaleHint;
PRInt32 mStride;
gfxImageFormat mOffscreenFormat;
Data mData;
PRUint32 mBufferSize;
bool mDelayedConversion;
return ImageFactory::CreateImage(aFormats, aNumFormats, aScaleHint, aRecycleBin);
}
};
void
BasicPlanarYCbCrImage::SetData(const Data& aData)
{
PlanarYCbCrImage::SetData(aData);
// Do some sanity checks to prevent integer overflow
if (aData.mYSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
aData.mYSize.height > PlanarYCbCrImage::MAX_DIMENSION) {
NS_ERROR("Illegal image source width or height");
return;
}
if (mDelayedConversion) {
mBuffer = CopyData(mData, mSize, mBufferSize, aData);
return;
}
gfxASurface::gfxImageFormat format = GetOffscreenFormat();
gfxIntSize size(mScaleHint);
@@ -167,13 +131,13 @@ BasicPlanarYCbCrImage::SetData(const Data& aData)
}
mStride = gfxASurface::FormatStrideForWidth(format, size.width);
mBuffer = AllocateBuffer(size.height * mStride);
if (!mBuffer) {
mDecodedBuffer = AllocateBuffer(size.height * mStride);
if (!mDecodedBuffer) {
// out of memory
return;
}
gfxUtils::ConvertYCbCrToRGB(aData, format, size, mBuffer, mStride);
gfxUtils::ConvertYCbCrToRGB(aData, format, size, mDecodedBuffer, mStride);
SetOffscreenFormat(format);
mSize = size;
}
@@ -196,22 +160,20 @@ BasicPlanarYCbCrImage::GetAsSurface()
return result.forget();
}
// XXX: If we forced delayed conversion, are we ever going to hit this?
// We may need to implement the conversion here.
if (!mBuffer || mDelayedConversion) {
return nsnull;
if (!mDecodedBuffer) {
return PlanarYCbCrImage::GetAsSurface();
}
gfxASurface::gfxImageFormat format = GetOffscreenFormat();
nsRefPtr<gfxImageSurface> imgSurface =
new gfxImageSurface(mBuffer, mSize, mStride, format);
new gfxImageSurface(mDecodedBuffer, mSize, mStride, format);
if (!imgSurface || imgSurface->CairoStatus() != 0) {
return nsnull;
}
// Pass ownership of the buffer to the surface
imgSurface->SetData(&imageSurfaceDataKey, mBuffer.forget(), DestroyBuffer);
imgSurface->SetData(&imageSurfaceDataKey, mDecodedBuffer.forget(), DestroyBuffer);
nsRefPtr<gfxASurface> result = imgSurface.get();
#if defined(XP_MACOSX)
@@ -221,144 +183,20 @@ BasicPlanarYCbCrImage::GetAsSurface()
result = quartzSurface.forget();
}
#endif
mSurface = result.get();
mSurface = result;
return result.forget();
}
/**
* Our image container is very simple. It's really just a factory
* for the image objects. We use a ReentrantMonitor to synchronize access to
* mImage.
*/
class BasicImageContainer : public ImageContainer {
public:
typedef gfxASurface::gfxImageFormat gfxImageFormat;
BasicImageContainer() :
ImageContainer(nsnull),
mScaleHint(-1, -1),
mOffscreenFormat(gfxASurface::ImageFormatUnknown),
mDelayed(false)
{}
virtual already_AddRefed<Image> CreateImage(const Image::Format* aFormats,
PRUint32 aNumFormats);
virtual void SetDelayedConversion(bool aDelayed) { mDelayed = aDelayed; }
virtual void SetCurrentImage(Image* aImage);
virtual already_AddRefed<Image> GetCurrentImage();
virtual already_AddRefed<gfxASurface> GetCurrentAsSurface(gfxIntSize* aSize);
virtual gfxIntSize GetCurrentSize();
virtual bool SetLayerManager(LayerManager *aManager);
virtual void SetScaleHint(const gfxIntSize& aScaleHint);
void SetOffscreenFormat(gfxImageFormat aFormat) { mOffscreenFormat = aFormat; }
virtual LayerManager::LayersBackend GetBackendType() { return LayerManager::LAYERS_BASIC; }
protected:
nsRefPtr<Image> mImage;
gfxIntSize mScaleHint;
gfxImageFormat mOffscreenFormat;
bool mDelayed;
};
/**
* Returns true if aFormat is in the given format array.
*/
static bool
FormatInList(const Image::Format* aFormats, PRUint32 aNumFormats,
Image::Format aFormat)
ImageFactory*
BasicLayerManager::GetImageFactory()
{
for (PRUint32 i = 0; i < aNumFormats; ++i) {
if (aFormats[i] == aFormat) {
return true;
}
}
return false;
}
already_AddRefed<Image>
BasicImageContainer::CreateImage(const Image::Format* aFormats,
PRUint32 aNumFormats)
{
nsRefPtr<Image> image;
// Prefer cairo surfaces because they're native for us
if (FormatInList(aFormats, aNumFormats, Image::CAIRO_SURFACE)) {
image = new BasicCairoImage();
} else if (FormatInList(aFormats, aNumFormats, Image::PLANAR_YCBCR)) {
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
image = new BasicPlanarYCbCrImage(mScaleHint);
static_cast<BasicPlanarYCbCrImage*>(image.get())->SetOffscreenFormat(mOffscreenFormat);
static_cast<BasicPlanarYCbCrImage*>(image.get())->SetDelayedConversion(mDelayed);
}
return image.forget();
}
void
BasicImageContainer::SetCurrentImage(Image* aImage)
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mImage = aImage;
CurrentImageChanged();
}
already_AddRefed<Image>
BasicImageContainer::GetCurrentImage()
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
nsRefPtr<Image> image = mImage;
return image.forget();
}
static BasicImageImplData*
ToImageData(Image* aImage)
{
return static_cast<BasicImageImplData*>(aImage->GetImplData());
}
already_AddRefed<gfxASurface>
BasicImageContainer::GetCurrentAsSurface(gfxIntSize* aSizeResult)
{
NS_PRECONDITION(NS_IsMainThread(), "Must be called on main thread");
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
if (!mImage) {
return nsnull;
}
*aSizeResult = ToImageData(mImage)->GetSize();
return ToImageData(mImage)->GetAsSurface();
}
gfxIntSize
BasicImageContainer::GetCurrentSize()
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
return !mImage ? gfxIntSize(0,0) : ToImageData(mImage)->GetSize();
}
void BasicImageContainer::SetScaleHint(const gfxIntSize& aScaleHint)
{
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
mScaleHint = aScaleHint;
}
bool
BasicImageContainer::SetLayerManager(LayerManager *aManager)
{
if (aManager &&
aManager->GetBackendType() != LayerManager::LAYERS_BASIC)
{
return false;
if (!mFactory) {
mFactory = new BasicImageFactory();
}
return true;
}
already_AddRefed<ImageContainer>
BasicLayerManager::CreateImageContainer()
{
nsRefPtr<ImageContainer> container = new BasicImageContainer();
static_cast<BasicImageContainer*>(container.get())->
SetOffscreenFormat(gfxPlatform::GetPlatform()->GetOffscreenFormat());
return container.forget();
return mFactory.get();
}
}