2013-04-30 22:03:25 -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/. */
|
|
|
|
|
2013-08-11 16:17:23 -07:00
|
|
|
#include "BasicLayersImpl.h" // for FillWithMask, etc
|
2013-04-30 22:03:25 -07:00
|
|
|
#include "CopyableCanvasLayer.h"
|
2013-08-11 16:17:23 -07:00
|
|
|
#include "GLContext.h" // for GLContext
|
|
|
|
#include "GLScreenBuffer.h" // for GLScreenBuffer
|
|
|
|
#include "SharedSurface.h" // for SharedSurface
|
|
|
|
#include "SharedSurfaceGL.h" // for SharedSurface_GL, etc
|
|
|
|
#include "SurfaceTypes.h" // for APITypeT, APITypeT::OpenGL, etc
|
|
|
|
#include "gfxImageSurface.h" // for gfxImageSurface
|
|
|
|
#include "gfxMatrix.h" // for gfxMatrix
|
|
|
|
#include "gfxPattern.h" // for gfxPattern, etc
|
|
|
|
#include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat
|
|
|
|
#include "gfxRect.h" // for gfxRect
|
|
|
|
#include "gfxUtils.h" // for gfxUtils
|
2013-12-20 08:46:28 -08:00
|
|
|
#include "gfx2DGlue.h" // for thebes --> moz2d transition
|
2013-08-11 16:17:23 -07:00
|
|
|
#include "mozilla/gfx/BaseSize.h" // for BaseSize
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc
|
|
|
|
#include "nsISupportsImpl.h" // for gfxContext::AddRef, etc
|
|
|
|
#include "nsRect.h" // for nsIntRect
|
|
|
|
#include "nsSize.h" // for nsIntSize
|
2013-04-30 22:03:25 -07:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-09-06 19:11:41 -07:00
|
|
|
CopyableCanvasLayer::CopyableCanvasLayer(LayerManager* aLayerManager, void *aImplData) :
|
|
|
|
CanvasLayer(aLayerManager, aImplData)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CopyableCanvasLayer);
|
|
|
|
mForceReadback = Preferences::GetBool("webgl.force-layers-readback", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyableCanvasLayer::~CopyableCanvasLayer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(CopyableCanvasLayer);
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
void
|
|
|
|
CopyableCanvasLayer::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
|
|
|
|
|
|
|
if (aData.mSurface) {
|
|
|
|
mSurface = aData.mSurface;
|
|
|
|
NS_ASSERTION(!aData.mGLContext, "CanvasLayer can't have both surface and GLContext");
|
|
|
|
mNeedsYFlip = false;
|
|
|
|
} else if (aData.mGLContext) {
|
|
|
|
mGLContext = aData.mGLContext;
|
|
|
|
mIsGLAlphaPremult = aData.mIsGLAlphaPremult;
|
|
|
|
mNeedsYFlip = true;
|
|
|
|
MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
|
|
|
|
// [Basic Layers, non-OMTC] WebGL layer init.
|
|
|
|
// `GLScreenBuffer::Morph`ing is only needed in BasicShadowableCanvasLayer.
|
|
|
|
} else if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2013-07-17 20:24:15 -07:00
|
|
|
mSurface =
|
|
|
|
gfxPlatform::GetPlatform()->CreateThebesSurfaceAliasForDrawTarget_hack(mDrawTarget);
|
2013-04-30 22:03:25 -07:00
|
|
|
mNeedsYFlip = false;
|
|
|
|
} else {
|
|
|
|
NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?");
|
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:46:20 -08:00
|
|
|
bool
|
|
|
|
CopyableCanvasLayer::IsDataValid(const Data& aData)
|
|
|
|
{
|
|
|
|
return mGLContext == aData.mGLContext;
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
void
|
|
|
|
CopyableCanvasLayer::UpdateSurface(gfxASurface* aDestSurface, Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
if (!IsDirty())
|
|
|
|
return;
|
|
|
|
Painted();
|
|
|
|
|
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2013-07-17 20:24:15 -07:00
|
|
|
mSurface =
|
|
|
|
gfxPlatform::GetPlatform()->CreateThebesSurfaceAliasForDrawTarget_hack(mDrawTarget);
|
2013-04-30 22:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGLContext && aDestSurface) {
|
|
|
|
nsRefPtr<gfxContext> tmpCtx = new gfxContext(aDestSurface);
|
|
|
|
tmpCtx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
CopyableCanvasLayer::PaintWithOpacity(tmpCtx, 1.0f, aMaskLayer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
|
|
|
nsRefPtr<gfxImageSurface> readSurf;
|
2013-12-10 08:11:58 -08:00
|
|
|
RefPtr<DataSourceSurface> readDSurf;
|
2013-07-31 15:17:41 -07:00
|
|
|
nsRefPtr<gfxASurface> resultSurf;
|
2013-04-30 22:03:25 -07:00
|
|
|
|
|
|
|
SharedSurface* sharedSurf = mGLContext->RequestFrame();
|
|
|
|
if (!sharedSurf) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-10 08:11:58 -08:00
|
|
|
IntSize readSize(sharedSurf->Size());
|
2013-04-30 22:03:25 -07:00
|
|
|
gfxImageFormat format = (GetContentFlags() & CONTENT_OPAQUE)
|
2013-09-24 13:45:13 -07:00
|
|
|
? gfxImageFormatRGB24
|
|
|
|
: gfxImageFormatARGB32;
|
2013-04-30 22:03:25 -07:00
|
|
|
|
2013-07-30 19:28:46 -07:00
|
|
|
if (aDestSurface) {
|
2013-07-31 15:17:41 -07:00
|
|
|
resultSurf = aDestSurface;
|
2013-07-30 19:28:46 -07:00
|
|
|
} else {
|
2013-04-30 22:03:25 -07:00
|
|
|
resultSurf = GetTempSurface(readSize, format);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(resultSurf);
|
|
|
|
if (resultSurf->CairoStatus() != 0) {
|
|
|
|
MOZ_ASSERT(false, "Bad resultSurf->CairoStatus().");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(sharedSurf->APIType() == APITypeT::OpenGL);
|
|
|
|
SharedSurface_GL* surfGL = SharedSurface_GL::Cast(sharedSurf);
|
|
|
|
|
|
|
|
if (surfGL->Type() == SharedSurfaceType::Basic) {
|
2013-12-10 08:11:58 -08:00
|
|
|
// sharedSurf_Basic->mData must outlive readSurf and readDSurf. Alas,
|
|
|
|
// readSurf and readDSurf may not leave the scope they were declared in.
|
2013-04-30 22:03:25 -07:00
|
|
|
SharedSurface_Basic* sharedSurf_Basic = SharedSurface_Basic::Cast(surfGL);
|
2013-12-10 08:11:58 -08:00
|
|
|
readDSurf = sharedSurf_Basic->GetData();
|
|
|
|
readSurf = new gfxImageSurface(readDSurf->GetData(),
|
|
|
|
ThebesIntSize(readDSurf->GetSize()),
|
|
|
|
readDSurf->Stride(),
|
|
|
|
SurfaceFormatToImageFormat(readDSurf->GetFormat()));
|
2013-04-30 22:03:25 -07:00
|
|
|
} else {
|
2013-12-20 08:46:28 -08:00
|
|
|
if (ToIntSize(resultSurf->GetSize()) != readSize ||
|
2013-07-31 15:17:41 -07:00
|
|
|
!(readSurf = resultSurf->GetAsImageSurface()) ||
|
|
|
|
readSurf->Format() != format)
|
2013-04-30 22:03:25 -07:00
|
|
|
{
|
|
|
|
readSurf = GetTempSurface(readSize, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readback handles Flush/MarkDirty.
|
|
|
|
mGLContext->Screen()->Readback(surfGL, readSurf);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(readSurf);
|
|
|
|
|
|
|
|
bool needsPremult = surfGL->HasAlpha() && !mIsGLAlphaPremult;
|
|
|
|
if (needsPremult) {
|
|
|
|
readSurf->Flush();
|
2013-07-31 15:17:41 -07:00
|
|
|
gfxUtils::PremultiplyImageSurface(readSurf);
|
|
|
|
readSurf->MarkDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readSurf != resultSurf) {
|
2013-04-30 22:03:25 -07:00
|
|
|
readSurf->Flush();
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(resultSurf);
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
ctx->SetSource(readSurf);
|
|
|
|
ctx->Paint();
|
|
|
|
}
|
|
|
|
|
2013-07-31 15:17:41 -07:00
|
|
|
// If !aDestSurface then we will end up painting using mSurface, so
|
|
|
|
// stick our surface into mSurface, so that the Paint() path is the same.
|
2013-07-30 19:28:46 -07:00
|
|
|
if (!aDestSurface) {
|
2013-04-30 22:03:25 -07:00
|
|
|
mSurface = resultSurf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyableCanvasLayer::PaintWithOpacity(gfxContext* aContext,
|
|
|
|
float aOpacity,
|
|
|
|
Layer* aMaskLayer,
|
|
|
|
gfxContext::GraphicsOperator aOperator)
|
|
|
|
{
|
|
|
|
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, aOperator);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:45:14 -07:00
|
|
|
gfxImageSurface*
|
2013-12-20 08:46:28 -08:00
|
|
|
CopyableCanvasLayer::GetTempSurface(const IntSize& aSize, const gfxImageFormat aFormat)
|
2013-09-24 13:45:14 -07:00
|
|
|
{
|
|
|
|
if (!mCachedTempSurface ||
|
|
|
|
aSize.width != mCachedSize.width ||
|
|
|
|
aSize.height != mCachedSize.height ||
|
|
|
|
aFormat != mCachedFormat)
|
|
|
|
{
|
2013-12-20 08:46:28 -08:00
|
|
|
mCachedTempSurface = new gfxImageSurface(ThebesIntSize(aSize), aFormat);
|
2013-09-24 13:45:14 -07:00
|
|
|
mCachedSize = aSize;
|
|
|
|
mCachedFormat = aFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCachedTempSurface->Stride() == mCachedTempSurface->Width() * 4);
|
|
|
|
return mCachedTempSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyableCanvasLayer::DiscardTempSurface()
|
|
|
|
{
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
}
|
|
|
|
}
|