2013-05-01 09:03:25 +04: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-12 03:17:23 +04:00
|
|
|
#include "BasicLayersImpl.h" // for FillWithMask, etc
|
2013-05-01 09:03:25 +04:00
|
|
|
#include "CopyableCanvasLayer.h"
|
2013-08-12 03:17:23 +04: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 "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 20:46:28 +04:00
|
|
|
#include "gfx2DGlue.h" // for thebes --> moz2d transition
|
2013-08-12 03:17:23 +04: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
|
2014-05-13 07:47:52 +04:00
|
|
|
#include "LayerUtils.h"
|
2013-05-01 09:03:25 +04:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-09-07 06:11:41 +04:00
|
|
|
CopyableCanvasLayer::CopyableCanvasLayer(LayerManager* aLayerManager, void *aImplData) :
|
|
|
|
CanvasLayer(aLayerManager, aImplData)
|
2014-03-06 01:49:37 +04:00
|
|
|
, mStream(nullptr)
|
2013-09-07 06:11:41 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CopyableCanvasLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyableCanvasLayer::~CopyableCanvasLayer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(CopyableCanvasLayer);
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
void
|
|
|
|
CopyableCanvasLayer::Initialize(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!");
|
|
|
|
|
2014-02-06 18:46:30 +04:00
|
|
|
if (aData.mGLContext) {
|
2013-05-01 09:03:25 +04:00
|
|
|
mGLContext = aData.mGLContext;
|
2014-03-06 01:49:37 +04:00
|
|
|
mStream = aData.mStream;
|
2013-05-01 09:03:25 +04:00
|
|
|
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;
|
2014-02-12 19:07:46 +04:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
2013-05-01 09:03:25 +04: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-05 02:46:20 +04:00
|
|
|
bool
|
|
|
|
CopyableCanvasLayer::IsDataValid(const Data& aData)
|
|
|
|
{
|
2014-03-06 01:49:37 +04:00
|
|
|
return mGLContext == aData.mGLContext && mStream == aData.mStream;
|
2013-12-05 02:46:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
void
|
2014-04-01 11:51:35 +04:00
|
|
|
CopyableCanvasLayer::UpdateTarget(DrawTarget* aDestTarget)
|
2013-05-01 09:03:25 +04:00
|
|
|
{
|
|
|
|
if (!IsDirty())
|
|
|
|
return;
|
|
|
|
Painted();
|
|
|
|
|
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2014-02-12 19:07:46 +04:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGLContext && aDestTarget) {
|
2014-04-01 11:51:35 +04:00
|
|
|
NS_ASSERTION(mSurface, "Must have surface to draw!");
|
|
|
|
if (mSurface) {
|
2014-04-01 11:53:15 +04:00
|
|
|
aDestTarget->CopySurface(mSurface,
|
|
|
|
IntRect(0, 0, mBounds.width, mBounds.height),
|
|
|
|
IntPoint(0, 0));
|
2014-05-09 19:52:11 +04:00
|
|
|
mSurface = nullptr;
|
2014-04-01 11:51:35 +04:00
|
|
|
}
|
2014-02-12 19:07:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
2014-04-10 23:19:02 +04:00
|
|
|
SharedSurface_GL* sharedSurf = nullptr;
|
|
|
|
if (mStream) {
|
|
|
|
sharedSurf = SharedSurface_GL::Cast(mStream->SwapConsumer());
|
|
|
|
} else {
|
|
|
|
sharedSurf = mGLContext->RequestFrame();
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:46 +04:00
|
|
|
if (!sharedSurf) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize readSize(sharedSurf->Size());
|
|
|
|
SurfaceFormat format = (GetContentFlags() & CONTENT_OPAQUE)
|
|
|
|
? SurfaceFormat::B8G8R8X8
|
|
|
|
: SurfaceFormat::B8G8R8A8;
|
2014-05-01 03:12:41 +04:00
|
|
|
bool needsPremult = sharedSurf->HasAlpha() && !mIsGLAlphaPremult;
|
2014-02-12 19:07:46 +04:00
|
|
|
|
2014-05-01 03:12:41 +04:00
|
|
|
// Try to read back directly into aDestTarget's output buffer
|
2014-02-12 19:07:46 +04:00
|
|
|
if (aDestTarget) {
|
2014-05-01 03:12:41 +04:00
|
|
|
uint8_t* destData;
|
|
|
|
IntSize destSize;
|
|
|
|
int32_t destStride;
|
|
|
|
SurfaceFormat destFormat;
|
|
|
|
if (aDestTarget->LockBits(&destData, &destSize, &destStride, &destFormat)) {
|
|
|
|
if (destSize == readSize && destFormat == format) {
|
|
|
|
RefPtr<DataSourceSurface> data =
|
|
|
|
Factory::CreateWrappingDataSourceSurface(destData, destStride, destSize, destFormat);
|
|
|
|
mGLContext->Screen()->Readback(sharedSurf, data);
|
|
|
|
if (needsPremult) {
|
2014-05-13 07:47:52 +04:00
|
|
|
PremultiplySurface(data);
|
2014-05-01 03:12:41 +04:00
|
|
|
}
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
2014-02-12 19:07:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 03:12:41 +04:00
|
|
|
RefPtr<SourceSurface> resultSurf;
|
|
|
|
if (sharedSurf->Type() == SharedSurfaceType::Basic && !needsPremult) {
|
|
|
|
SharedSurface_Basic* sharedSurf_Basic = SharedSurface_Basic::Cast(sharedSurf);
|
|
|
|
resultSurf = sharedSurf_Basic->GetData();
|
|
|
|
} else {
|
|
|
|
RefPtr<DataSourceSurface> data = GetTempSurface(readSize, format);
|
2014-02-12 19:07:46 +04:00
|
|
|
// Readback handles Flush/MarkDirty.
|
2014-05-01 03:12:41 +04:00
|
|
|
mGLContext->Screen()->Readback(sharedSurf, data);
|
|
|
|
if (needsPremult) {
|
2014-05-13 07:47:52 +04:00
|
|
|
PremultiplySurface(data);
|
2014-05-01 03:12:41 +04:00
|
|
|
}
|
|
|
|
resultSurf = data;
|
2014-02-12 19:07:46 +04:00
|
|
|
}
|
2014-05-01 03:12:41 +04:00
|
|
|
MOZ_ASSERT(resultSurf);
|
2014-02-12 19:07:46 +04:00
|
|
|
|
2014-05-01 03:12:41 +04:00
|
|
|
if (aDestTarget) {
|
|
|
|
aDestTarget->CopySurface(resultSurf,
|
|
|
|
IntRect(0, 0, readSize.width, readSize.height),
|
|
|
|
IntPoint(0, 0));
|
|
|
|
} else {
|
|
|
|
// If !aDestSurface then we will end up painting using mSurface, so
|
|
|
|
// stick our surface into mSurface, so that the Paint() path is the same.
|
2014-02-12 19:07:46 +04:00
|
|
|
mSurface = resultSurf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface*
|
|
|
|
CopyableCanvasLayer::GetTempSurface(const IntSize& aSize,
|
|
|
|
const SurfaceFormat aFormat)
|
2013-09-25 00:45:14 +04:00
|
|
|
{
|
|
|
|
if (!mCachedTempSurface ||
|
2014-05-01 03:12:41 +04:00
|
|
|
aSize != mCachedTempSurface->GetSize() ||
|
|
|
|
aFormat != mCachedTempSurface->GetFormat())
|
2013-09-25 00:45:14 +04:00
|
|
|
{
|
2014-05-13 07:47:52 +04:00
|
|
|
mCachedTempSurface = Factory::CreateDataSourceSurface(aSize, aFormat);
|
2013-09-25 00:45:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mCachedTempSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyableCanvasLayer::DiscardTempSurface()
|
|
|
|
{
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
}
|
|
|
|
}
|