2012-06-28 23:01:34 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/layers/PLayersParent.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "GLContext.h"
|
|
|
|
#include "gfxUtils.h"
|
2012-08-19 12:33:25 -07:00
|
|
|
#include "gfxPlatform.h"
|
2012-08-21 20:29:06 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
#include "BasicLayersImpl.h"
|
2012-05-31 18:30:08 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2012-10-25 13:12:59 -07:00
|
|
|
using namespace mozilla::gl;
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-07-10 08:06:42 -07:00
|
|
|
namespace layers {
|
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
class BasicCanvasLayer : public CanvasLayer,
|
|
|
|
public BasicImplData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BasicCanvasLayer(BasicLayerManager* aLayerManager) :
|
|
|
|
CanvasLayer(aLayerManager, static_cast<BasicImplData*>(this))
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BasicCanvasLayer);
|
2012-08-21 20:29:06 -07:00
|
|
|
mForceReadback = Preferences::GetBool("webgl.force-layers-readback", false);
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
virtual ~BasicCanvasLayer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(BasicCanvasLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetVisibleRegion(const nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(BasicManager()->InConstruction(),
|
|
|
|
"Can only set properties in construction phase");
|
|
|
|
CanvasLayer::SetVisibleRegion(aRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Initialize(const Data& aData);
|
|
|
|
virtual void Paint(gfxContext* aContext, Layer* aMaskLayer);
|
|
|
|
|
|
|
|
virtual void PaintWithOpacity(gfxContext* aContext,
|
|
|
|
float aOpacity,
|
|
|
|
Layer* aMaskLayer);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
BasicLayerManager* BasicManager()
|
|
|
|
{
|
|
|
|
return static_cast<BasicLayerManager*>(mManager);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
void UpdateSurface(gfxASurface* aDestSurface = nullptr, Layer* aMaskLayer = nullptr);
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> mSurface;
|
|
|
|
nsRefPtr<mozilla::gl::GLContext> mGLContext;
|
|
|
|
mozilla::RefPtr<mozilla::gfx::DrawTarget> mDrawTarget;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mCanvasFramebuffer;
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
bool mGLBufferIsPremultiplied;
|
|
|
|
bool mNeedsYFlip;
|
2012-08-21 20:29:06 -07:00
|
|
|
bool mForceReadback;
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> mCachedTempSurface;
|
|
|
|
gfxIntSize mCachedSize;
|
|
|
|
gfxImageFormat mCachedFormat;
|
|
|
|
|
|
|
|
gfxImageSurface* GetTempSurface(const gfxIntSize& aSize, const gfxImageFormat aFormat)
|
|
|
|
{
|
|
|
|
if (!mCachedTempSurface ||
|
|
|
|
aSize.width != mCachedSize.width ||
|
|
|
|
aSize.height != mCachedSize.height ||
|
|
|
|
aFormat != mCachedFormat)
|
|
|
|
{
|
|
|
|
mCachedTempSurface = new gfxImageSurface(aSize, aFormat);
|
|
|
|
mCachedSize = aSize;
|
|
|
|
mCachedFormat = aFormat;
|
|
|
|
}
|
|
|
|
|
2012-08-21 16:13:26 -07:00
|
|
|
MOZ_ASSERT(mCachedTempSurface->Stride() == mCachedTempSurface->Width() * 4);
|
2012-06-28 23:01:34 -07:00
|
|
|
return mCachedTempSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscardTempSurface()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mCachedTempSurface = nullptr;
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicCanvasLayer::Initialize(const Data& aData)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
if (aData.mSurface) {
|
|
|
|
mSurface = aData.mSurface;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aData.mGLContext == nullptr,
|
2012-06-28 23:01:34 -07:00
|
|
|
"CanvasLayer can't have both surface and GLContext");
|
|
|
|
mNeedsYFlip = false;
|
|
|
|
} else if (aData.mGLContext) {
|
|
|
|
NS_ASSERTION(aData.mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
mGLContext = aData.mGLContext;
|
|
|
|
mGLBufferIsPremultiplied = aData.mGLBufferIsPremultiplied;
|
|
|
|
mCanvasFramebuffer = mGLContext->GetOffscreenFBO();
|
|
|
|
mNeedsYFlip = true;
|
|
|
|
} else if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2012-11-02 13:24:37 -07:00
|
|
|
mSurface = gfxPlatform::GetPlatform()->CreateThebesSurfaceAliasForDrawTarget_hack(mDrawTarget);
|
2012-06-28 23:01:34 -07:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
} else {
|
|
|
|
NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?");
|
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicCanvasLayer::UpdateSurface(gfxASurface* aDestSurface, Layer* aMaskLayer)
|
|
|
|
{
|
2012-09-29 23:20:25 -07:00
|
|
|
if (!IsDirty())
|
2012-08-23 11:56:23 -07:00
|
|
|
return;
|
2012-09-29 23:20:25 -07:00
|
|
|
Painted();
|
2012-08-23 11:56:23 -07:00
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2012-11-02 13:24:37 -07:00
|
|
|
if (mDrawTarget->GetType() == BACKEND_COREGRAPHICS_ACCELERATED) {
|
|
|
|
// We have an accelerated CG context which has changed, unlike a bitmap surface
|
|
|
|
// where we can alias the bits on initializing the mDrawTarget, we need to readback
|
|
|
|
// and copy the accelerated surface each frame. We want to support this for quick
|
|
|
|
// thumbnail but if we're going to be doing this every frame it likely is better
|
|
|
|
// to use a non accelerated (bitmap) canvas.
|
|
|
|
mSurface = gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mDrawTarget);
|
|
|
|
}
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGLContext && aDestSurface) {
|
|
|
|
nsRefPtr<gfxContext> tmpCtx = new gfxContext(aDestSurface);
|
|
|
|
tmpCtx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
BasicCanvasLayer::PaintWithOpacity(tmpCtx, 1.0f, aMaskLayer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
|
|
|
if (aDestSurface && aDestSurface->GetType() != gfxASurface::SurfaceTypeImage) {
|
|
|
|
NS_ASSERTION(aDestSurface->GetType() == gfxASurface::SurfaceTypeImage,
|
|
|
|
"Destination surface must be ImageSurface type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to read from the GLContext
|
|
|
|
mGLContext->MakeCurrent();
|
|
|
|
|
2012-08-21 16:13:26 -07:00
|
|
|
gfxIntSize readSize(mBounds.width, mBounds.height);
|
|
|
|
gfxImageFormat format = (GetContentFlags() & CONTENT_OPAQUE)
|
|
|
|
? gfxASurface::ImageFormatRGB24
|
|
|
|
: gfxASurface::ImageFormatARGB32;
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> readSurf;
|
|
|
|
nsRefPtr<gfxImageSurface> resultSurf;
|
|
|
|
|
|
|
|
bool usingTempSurface = false;
|
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
if (aDestSurface) {
|
2012-08-21 16:13:26 -07:00
|
|
|
resultSurf = static_cast<gfxImageSurface*>(aDestSurface);
|
|
|
|
|
|
|
|
if (resultSurf->GetSize() != readSize ||
|
|
|
|
resultSurf->Stride() != resultSurf->Width() * 4)
|
|
|
|
{
|
|
|
|
readSurf = GetTempSurface(readSize, format);
|
|
|
|
usingTempSurface = true;
|
|
|
|
}
|
2012-06-28 23:01:34 -07:00
|
|
|
} else {
|
2012-08-21 16:13:26 -07:00
|
|
|
resultSurf = GetTempSurface(readSize, format);
|
|
|
|
usingTempSurface = true;
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 16:13:26 -07:00
|
|
|
if (!usingTempSurface)
|
|
|
|
DiscardTempSurface();
|
|
|
|
|
|
|
|
if (!readSurf)
|
|
|
|
readSurf = resultSurf;
|
2012-06-28 23:01:34 -07:00
|
|
|
|
2012-08-21 16:13:26 -07:00
|
|
|
if (!resultSurf || resultSurf->CairoStatus() != 0)
|
2012-06-28 23:01:34 -07:00
|
|
|
return;
|
|
|
|
|
2012-08-21 16:13:26 -07:00
|
|
|
MOZ_ASSERT(readSurf);
|
|
|
|
MOZ_ASSERT(readSurf->Stride() == mBounds.width * 4, "gfxImageSurface stride isn't what we expect!");
|
2012-06-28 23:01:34 -07:00
|
|
|
|
2012-07-17 21:27:49 -07:00
|
|
|
// We need to Flush() the surface before modifying it outside of cairo.
|
2012-08-21 16:13:26 -07:00
|
|
|
readSurf->Flush();
|
|
|
|
mGLContext->ReadScreenIntoImageSurface(readSurf);
|
|
|
|
readSurf->MarkDirty();
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
// If the underlying GLContext doesn't have a framebuffer into which
|
|
|
|
// premultiplied values were written, we have to do this ourselves here.
|
|
|
|
// Note that this is a WebGL attribute; GL itself has no knowledge of
|
|
|
|
// premultiplied or unpremultiplied alpha.
|
|
|
|
if (!mGLBufferIsPremultiplied)
|
2012-08-21 16:13:26 -07:00
|
|
|
gfxUtils::PremultiplyImageSurface(readSurf);
|
|
|
|
|
|
|
|
if (readSurf != resultSurf) {
|
|
|
|
MOZ_ASSERT(resultSurf->Width() >= readSurf->Width());
|
|
|
|
MOZ_ASSERT(resultSurf->Height() >= readSurf->Height());
|
|
|
|
|
|
|
|
resultSurf->Flush();
|
|
|
|
resultSurf->CopyFrom(readSurf);
|
|
|
|
resultSurf->MarkDirty();
|
|
|
|
}
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
// stick our surface into mSurface, so that the Paint() path is the same
|
|
|
|
if (!aDestSurface) {
|
2012-08-21 16:13:26 -07:00
|
|
|
mSurface = resultSurf;
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicCanvasLayer::Paint(gfxContext* aContext, Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
if (IsHidden())
|
|
|
|
return;
|
|
|
|
UpdateSurface();
|
|
|
|
FireDidTransactionCallback();
|
|
|
|
PaintWithOpacity(aContext, GetEffectiveOpacity(), aMaskLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicCanvasLayer::PaintWithOpacity(gfxContext* aContext,
|
|
|
|
float aOpacity,
|
|
|
|
Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(BasicManager()->InDrawing(),
|
|
|
|
"Can only draw in drawing phase");
|
|
|
|
|
|
|
|
if (!mSurface) {
|
|
|
|
NS_WARNING("No valid surface to draw!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxPattern> pat = new gfxPattern(mSurface);
|
|
|
|
|
|
|
|
pat->SetFilter(mFilter);
|
|
|
|
pat->SetExtend(gfxPattern::EXTEND_PAD);
|
|
|
|
|
|
|
|
gfxMatrix m;
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
m = aContext->CurrentMatrix();
|
|
|
|
aContext->Translate(gfxPoint(0.0, mBounds.height));
|
|
|
|
aContext->Scale(1.0, -1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If content opaque, then save off current operator and set to source.
|
|
|
|
// This ensures that alpha is not applied even if the source surface
|
|
|
|
// has an alpha channel
|
|
|
|
gfxContext::GraphicsOperator savedOp;
|
|
|
|
if (GetContentFlags() & CONTENT_OPAQUE) {
|
|
|
|
savedOp = aContext->CurrentOperator();
|
|
|
|
aContext->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoSetOperator setOperator(aContext, GetOperator());
|
|
|
|
aContext->NewPath();
|
|
|
|
// No need to snap here; our transform is already set up to snap our rect
|
|
|
|
aContext->Rectangle(gfxRect(0, 0, mBounds.width, mBounds.height));
|
|
|
|
aContext->SetPattern(pat);
|
|
|
|
|
|
|
|
FillWithMask(aContext, aOpacity, aMaskLayer);
|
|
|
|
|
|
|
|
// Restore surface operator
|
|
|
|
if (GetContentFlags() & CONTENT_OPAQUE) {
|
|
|
|
aContext->SetOperator(savedOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
aContext->SetMatrix(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BasicShadowableCanvasLayer : public BasicCanvasLayer,
|
|
|
|
public BasicShadowableLayer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BasicShadowableCanvasLayer(BasicShadowLayerManager* aManager) :
|
|
|
|
BasicCanvasLayer(aManager),
|
|
|
|
mBufferIsOpaque(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BasicShadowableCanvasLayer);
|
|
|
|
}
|
|
|
|
virtual ~BasicShadowableCanvasLayer()
|
|
|
|
{
|
|
|
|
DestroyBackBuffer();
|
|
|
|
MOZ_COUNT_DTOR(BasicShadowableCanvasLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Initialize(const Data& aData);
|
|
|
|
virtual void Paint(gfxContext* aContext, Layer* aMaskLayer);
|
|
|
|
|
2012-11-07 19:51:55 -08:00
|
|
|
virtual void ClearCachedResources() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
DestroyBackBuffer();
|
|
|
|
}
|
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
virtual void FillSpecificAttributes(SpecificLayerAttributes& aAttrs)
|
|
|
|
{
|
|
|
|
aAttrs = CanvasLayerAttributes(mFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Layer* AsLayer() { return this; }
|
|
|
|
virtual ShadowableLayer* AsShadowableLayer() { return this; }
|
|
|
|
|
|
|
|
virtual void SetBackBuffer(const SurfaceDescriptor& aBuffer)
|
|
|
|
{
|
|
|
|
mBackBuffer = aBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Disconnect()
|
|
|
|
{
|
|
|
|
mBackBuffer = SurfaceDescriptor();
|
|
|
|
BasicShadowableLayer::Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyBackBuffer()
|
|
|
|
{
|
2012-05-31 18:30:08 -07:00
|
|
|
if (mBackBuffer.type() == SurfaceDescriptor::TSharedTextureDescriptor) {
|
|
|
|
SharedTextureDescriptor handle = mBackBuffer.get_SharedTextureDescriptor();
|
|
|
|
if (mGLContext && handle.handle()) {
|
|
|
|
mGLContext->ReleaseSharedHandle(handle.shareType(), handle.handle());
|
|
|
|
mBackBuffer = SurfaceDescriptor();
|
|
|
|
}
|
|
|
|
} else if (IsSurfaceDescriptorValid(mBackBuffer)) {
|
2012-06-28 23:01:34 -07:00
|
|
|
BasicManager()->ShadowLayerForwarder::DestroySharedSurface(&mBackBuffer);
|
|
|
|
mBackBuffer = SurfaceDescriptor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-05-31 18:30:08 -07:00
|
|
|
typedef mozilla::gl::SharedTextureHandle SharedTextureHandle;
|
|
|
|
typedef mozilla::gl::TextureImage TextureImage;
|
|
|
|
SharedTextureHandle GetSharedBackBufferHandle()
|
|
|
|
{
|
|
|
|
if (mBackBuffer.type() == SurfaceDescriptor::TSharedTextureDescriptor)
|
|
|
|
return mBackBuffer.get_SharedTextureDescriptor().handle();
|
2012-07-20 04:16:17 -07:00
|
|
|
return 0;
|
2012-05-31 18:30:08 -07:00
|
|
|
}
|
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
BasicShadowLayerManager* BasicManager()
|
|
|
|
{
|
|
|
|
return static_cast<BasicShadowLayerManager*>(mManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceDescriptor mBackBuffer;
|
|
|
|
bool mBufferIsOpaque;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicShadowableCanvasLayer::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
BasicCanvasLayer::Initialize(aData);
|
|
|
|
if (!HasShadow())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// XXX won't get here currently; need to figure out what to do on
|
|
|
|
// canvas resizes
|
|
|
|
|
|
|
|
if (IsSurfaceDescriptorValid(mBackBuffer)) {
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface backSurface(OPEN_READ_ONLY, mBackBuffer);
|
|
|
|
if (gfxIntSize(mBounds.width, mBounds.height) != backSurface.Size()) {
|
2012-06-28 23:01:34 -07:00
|
|
|
DestroyBackBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicShadowableCanvasLayer::Paint(gfxContext* aContext, Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
if (!HasShadow()) {
|
|
|
|
BasicCanvasLayer::Paint(aContext, aMaskLayer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-29 23:20:25 -07:00
|
|
|
if (!IsDirty())
|
2012-08-23 11:56:23 -07:00
|
|
|
return;
|
|
|
|
|
2012-05-31 18:30:08 -07:00
|
|
|
if (mGLContext &&
|
2012-08-21 20:29:06 -07:00
|
|
|
!mForceReadback &&
|
2012-11-26 14:23:27 -08:00
|
|
|
BasicManager()->GetParentBackendType() == mozilla::layers::LAYERS_OPENGL)
|
|
|
|
{
|
|
|
|
GLContext::SharedTextureShareType shareType;
|
2012-05-31 18:30:08 -07:00
|
|
|
// if process type is default, then it is single-process (non-e10s)
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default)
|
2012-11-26 14:23:27 -08:00
|
|
|
shareType = GLContext::SameProcess;
|
2012-05-31 18:30:08 -07:00
|
|
|
else
|
2012-11-26 14:23:27 -08:00
|
|
|
shareType = GLContext::CrossProcess;
|
2012-05-31 18:30:08 -07:00
|
|
|
|
|
|
|
SharedTextureHandle handle = GetSharedBackBufferHandle();
|
|
|
|
if (!handle) {
|
2012-11-26 14:23:27 -08:00
|
|
|
handle = mGLContext->CreateSharedHandle(shareType);
|
2012-05-31 18:30:08 -07:00
|
|
|
if (handle) {
|
2012-11-26 14:23:27 -08:00
|
|
|
mBackBuffer = SharedTextureDescriptor(shareType, handle, mBounds.Size(), false);
|
2012-05-31 18:30:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (handle) {
|
|
|
|
mGLContext->MakeCurrent();
|
2012-11-26 14:23:27 -08:00
|
|
|
mGLContext->UpdateSharedHandle(shareType, handle);
|
2012-10-18 01:40:32 -07:00
|
|
|
// call Painted() to reset our dirty 'bit'
|
|
|
|
Painted();
|
2012-05-31 18:30:08 -07:00
|
|
|
FireDidTransactionCallback();
|
|
|
|
BasicManager()->PaintedCanvas(BasicManager()->Hold(this),
|
|
|
|
mNeedsYFlip,
|
|
|
|
mBackBuffer);
|
|
|
|
// Move SharedTextureHandle ownership to ShadowLayer
|
|
|
|
mBackBuffer = SurfaceDescriptor();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 23:01:34 -07:00
|
|
|
bool isOpaque = (GetContentFlags() & CONTENT_OPAQUE);
|
|
|
|
if (!IsSurfaceDescriptorValid(mBackBuffer) ||
|
|
|
|
isOpaque != mBufferIsOpaque) {
|
|
|
|
DestroyBackBuffer();
|
|
|
|
mBufferIsOpaque = isOpaque;
|
2012-08-21 20:29:06 -07:00
|
|
|
|
|
|
|
gfxIntSize size(mBounds.width, mBounds.height);
|
|
|
|
gfxASurface::gfxContentType type = isOpaque ?
|
|
|
|
gfxASurface::CONTENT_COLOR : gfxASurface::CONTENT_COLOR_ALPHA;
|
|
|
|
|
|
|
|
if (!BasicManager()->AllocBuffer(size, type, &mBackBuffer)) {
|
|
|
|
NS_RUNTIMEABORT("creating CanvasLayer back buffer failed!");
|
|
|
|
}
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface autoBackSurface(OPEN_READ_WRITE, mBackBuffer);
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
if (aMaskLayer) {
|
|
|
|
static_cast<BasicImplData*>(aMaskLayer->ImplData())
|
2012-07-30 07:20:58 -07:00
|
|
|
->Paint(aContext, nullptr);
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
UpdateSurface(autoBackSurface.Get(), nullptr);
|
2012-06-28 23:01:34 -07:00
|
|
|
FireDidTransactionCallback();
|
|
|
|
|
|
|
|
BasicManager()->PaintedCanvas(BasicManager()->Hold(this),
|
2012-05-31 18:30:08 -07:00
|
|
|
mNeedsYFlip, mBackBuffer);
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class BasicShadowCanvasLayer : public ShadowCanvasLayer,
|
|
|
|
public BasicImplData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BasicShadowCanvasLayer(BasicShadowLayerManager* aLayerManager) :
|
|
|
|
ShadowCanvasLayer(aLayerManager, static_cast<BasicImplData*>(this))
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BasicShadowCanvasLayer);
|
|
|
|
}
|
|
|
|
virtual ~BasicShadowCanvasLayer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(BasicShadowCanvasLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Disconnect()
|
|
|
|
{
|
|
|
|
DestroyFrontBuffer();
|
|
|
|
ShadowCanvasLayer::Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Initialize(const Data& aData);
|
|
|
|
void Swap(const CanvasSurface& aNewFront, bool needYFlip, CanvasSurface* aNewBack);
|
|
|
|
|
|
|
|
virtual void DestroyFrontBuffer()
|
|
|
|
{
|
|
|
|
if (IsSurfaceDescriptorValid(mFrontSurface)) {
|
|
|
|
mAllocator->DestroySharedSurface(&mFrontSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Paint(gfxContext* aContext, Layer* aMaskLayer);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BasicShadowLayerManager* BasicManager()
|
|
|
|
{
|
|
|
|
return static_cast<BasicShadowLayerManager*>(mManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceDescriptor mFrontSurface;
|
|
|
|
bool mNeedsYFlip;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicShadowCanvasLayer::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("Incompatibe surface type");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicShadowCanvasLayer::Swap(const CanvasSurface& aNewFront, bool needYFlip,
|
|
|
|
CanvasSurface* aNewBack)
|
|
|
|
{
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface autoSurface(OPEN_READ_ONLY, aNewFront);
|
2012-06-28 23:01:34 -07:00
|
|
|
// Destroy mFrontBuffer if size different
|
2012-07-12 05:51:58 -07:00
|
|
|
gfxIntSize sz = autoSurface.Size();
|
2012-06-28 23:01:34 -07:00
|
|
|
bool surfaceConfigChanged = sz != gfxIntSize(mBounds.width, mBounds.height);
|
|
|
|
if (IsSurfaceDescriptorValid(mFrontSurface)) {
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface autoFront(OPEN_READ_ONLY, mFrontSurface);
|
2012-06-28 23:01:34 -07:00
|
|
|
surfaceConfigChanged = surfaceConfigChanged ||
|
2012-07-12 05:51:58 -07:00
|
|
|
autoSurface.ContentType() != autoFront.ContentType();
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
if (surfaceConfigChanged) {
|
|
|
|
DestroyFrontBuffer();
|
|
|
|
mBounds.SetRect(0, 0, sz.width, sz.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
mNeedsYFlip = needYFlip;
|
|
|
|
// If mFrontBuffer
|
|
|
|
if (IsSurfaceDescriptorValid(mFrontSurface)) {
|
|
|
|
*aNewBack = mFrontSurface;
|
|
|
|
} else {
|
|
|
|
*aNewBack = null_t();
|
|
|
|
}
|
2012-07-12 05:51:58 -07:00
|
|
|
mFrontSurface = aNewFront;
|
2012-06-28 23:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicShadowCanvasLayer::Paint(gfxContext* aContext, Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(BasicManager()->InDrawing(),
|
|
|
|
"Can only draw in drawing phase");
|
|
|
|
|
|
|
|
if (!IsSurfaceDescriptorValid(mFrontSurface)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-12 05:51:58 -07:00
|
|
|
AutoOpenSurface autoSurface(OPEN_READ_ONLY, mFrontSurface);
|
|
|
|
nsRefPtr<gfxPattern> pat = new gfxPattern(autoSurface.Get());
|
2012-06-28 23:01:34 -07:00
|
|
|
|
|
|
|
pat->SetFilter(mFilter);
|
|
|
|
pat->SetExtend(gfxPattern::EXTEND_PAD);
|
|
|
|
|
|
|
|
gfxRect r(0, 0, mBounds.width, mBounds.height);
|
|
|
|
|
|
|
|
gfxMatrix m;
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
m = aContext->CurrentMatrix();
|
|
|
|
aContext->Translate(gfxPoint(0.0, mBounds.height));
|
|
|
|
aContext->Scale(1.0, -1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoSetOperator setOperator(aContext, GetOperator());
|
|
|
|
aContext->NewPath();
|
|
|
|
// No need to snap here; our transform has already taken care of it
|
|
|
|
aContext->Rectangle(r);
|
|
|
|
aContext->SetPattern(pat);
|
|
|
|
FillWithMask(aContext, GetEffectiveOpacity(), aMaskLayer);
|
|
|
|
|
|
|
|
if (mNeedsYFlip) {
|
|
|
|
aContext->SetMatrix(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<CanvasLayer>
|
|
|
|
BasicLayerManager::CreateCanvasLayer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
|
|
|
|
nsRefPtr<CanvasLayer> layer = new BasicCanvasLayer(this);
|
|
|
|
return layer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<CanvasLayer>
|
|
|
|
BasicShadowLayerManager::CreateCanvasLayer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
|
|
|
|
nsRefPtr<BasicShadowableCanvasLayer> layer =
|
|
|
|
new BasicShadowableCanvasLayer(this);
|
|
|
|
MAYBE_CREATE_SHADOW(Canvas);
|
|
|
|
return layer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ShadowCanvasLayer>
|
|
|
|
BasicShadowLayerManager::CreateShadowCanvasLayer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
|
|
|
|
nsRefPtr<ShadowCanvasLayer> layer = new BasicShadowCanvasLayer(this);
|
|
|
|
return layer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|