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
|
2014-07-11 15:10:49 -07:00
|
|
|
#include "SharedSurfaceGL.h" // for SharedSurface
|
2013-08-11 16:17:23 -07:00
|
|
|
#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
|
2014-05-12 19:20:27 -07:00
|
|
|
#include "mozilla/gfx/Tools.h"
|
2013-08-11 16:17:23 -07:00
|
|
|
#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-12 19:20:26 -07:00
|
|
|
#include "gfxUtils.h"
|
2013-04-30 22:03:25 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-05-22 03:11:45 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2013-09-06 19:11:41 -07:00
|
|
|
CopyableCanvasLayer::CopyableCanvasLayer(LayerManager* aLayerManager, void *aImplData) :
|
|
|
|
CanvasLayer(aLayerManager, aImplData)
|
2014-10-07 21:15:39 -07:00
|
|
|
, mGLFrontbuffer(nullptr)
|
2014-06-18 17:04:06 -07:00
|
|
|
, mIsAlphaPremultiplied(true)
|
2014-11-17 17:02:19 -08:00
|
|
|
, mOriginPos(gl::OriginPos::TopLeft)
|
2013-09-06 19:11:41 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(CopyableCanvasLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
2014-02-06 06:46:30 -08:00
|
|
|
if (aData.mGLContext) {
|
2013-04-30 22:03:25 -07:00
|
|
|
mGLContext = aData.mGLContext;
|
2014-06-18 17:04:06 -07:00
|
|
|
mIsAlphaPremultiplied = aData.mIsGLAlphaPremult;
|
2014-11-17 17:02:19 -08:00
|
|
|
mOriginPos = gl::OriginPos::BottomLeft;
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
|
2014-10-07 21:15:39 -07:00
|
|
|
if (aData.mFrontbufferGLTex) {
|
|
|
|
gfx::IntSize size(aData.mSize.width, aData.mSize.height);
|
|
|
|
mGLFrontbuffer = SharedSurface_GLTexture::Create(aData.mGLContext,
|
|
|
|
nullptr,
|
|
|
|
aData.mGLContext->GetGLFormats(),
|
|
|
|
size, aData.mHasAlpha,
|
|
|
|
aData.mFrontbufferGLTex);
|
|
|
|
}
|
2013-04-30 22:03:25 -07:00
|
|
|
} else if (aData.mDrawTarget) {
|
|
|
|
mDrawTarget = aData.mDrawTarget;
|
2014-02-12 07:07:46 -08:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
2013-04-30 22:03:25 -07:00
|
|
|
} else {
|
2014-11-17 17:02:19 -08:00
|
|
|
MOZ_CRASH("CanvasLayer created without mSurface, mDrawTarget or mGLContext?");
|
2013-04-30 22:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:46:20 -08:00
|
|
|
bool
|
|
|
|
CopyableCanvasLayer::IsDataValid(const Data& aData)
|
|
|
|
{
|
2014-10-07 21:15:39 -07:00
|
|
|
return mGLContext == aData.mGLContext;
|
2013-12-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
void
|
2014-04-01 00:51:35 -07:00
|
|
|
CopyableCanvasLayer::UpdateTarget(DrawTarget* aDestTarget)
|
2013-04-30 22:03:25 -07:00
|
|
|
{
|
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->Flush();
|
2014-02-12 07:07:46 -08:00
|
|
|
mSurface = mDrawTarget->Snapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGLContext && aDestTarget) {
|
2014-04-01 00:51:35 -07:00
|
|
|
NS_ASSERTION(mSurface, "Must have surface to draw!");
|
|
|
|
if (mSurface) {
|
2014-04-01 00:53:15 -07:00
|
|
|
aDestTarget->CopySurface(mSurface,
|
|
|
|
IntRect(0, 0, mBounds.width, mBounds.height),
|
|
|
|
IntPoint(0, 0));
|
2014-05-09 08:52:11 -07:00
|
|
|
mSurface = nullptr;
|
2014-04-01 00:51:35 -07:00
|
|
|
}
|
2014-02-12 07:07:46 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:14 -07:00
|
|
|
if (mDrawTarget) {
|
2014-10-07 21:15:39 -07:00
|
|
|
return;
|
2014-10-07 21:16:14 -07:00
|
|
|
}
|
2014-04-10 12:19:02 -07:00
|
|
|
|
2014-10-07 21:15:39 -07:00
|
|
|
MOZ_ASSERT(mGLContext);
|
|
|
|
|
|
|
|
SharedSurface* frontbuffer = nullptr;
|
|
|
|
if (mGLFrontbuffer) {
|
|
|
|
frontbuffer = mGLFrontbuffer.get();
|
|
|
|
} else {
|
2014-10-08 13:04:19 -07:00
|
|
|
GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
ShSurfHandle* front = screen->Front();
|
2014-10-07 21:15:39 -07:00
|
|
|
if (front) {
|
|
|
|
frontbuffer = front->Surf();
|
2014-02-12 07:07:46 -08:00
|
|
|
}
|
2014-10-07 21:15:39 -07:00
|
|
|
}
|
2014-02-12 07:07:46 -08:00
|
|
|
|
2014-10-07 21:15:39 -07:00
|
|
|
if (!frontbuffer) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize readSize(frontbuffer->mSize);
|
|
|
|
SurfaceFormat format = (GetContentFlags() & CONTENT_OPAQUE)
|
|
|
|
? SurfaceFormat::B8G8R8X8
|
|
|
|
: SurfaceFormat::B8G8R8A8;
|
|
|
|
bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied;
|
|
|
|
|
|
|
|
// Try to read back directly into aDestTarget's output buffer
|
|
|
|
if (aDestTarget) {
|
|
|
|
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);
|
2015-02-12 19:00:41 -08:00
|
|
|
mGLContext->Readback(frontbuffer, data);
|
2014-10-07 21:15:39 -07:00
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(data, data);
|
2014-04-30 16:12:41 -07:00
|
|
|
}
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
2014-10-07 21:15:39 -07:00
|
|
|
return;
|
2014-02-12 07:07:46 -08:00
|
|
|
}
|
2014-10-07 21:15:39 -07:00
|
|
|
aDestTarget->ReleaseBits(destData);
|
2014-02-12 07:07:46 -08:00
|
|
|
}
|
2014-10-07 21:15:39 -07:00
|
|
|
}
|
2014-02-12 07:07:46 -08:00
|
|
|
|
2014-10-07 21:15:39 -07:00
|
|
|
RefPtr<DataSourceSurface> resultSurf = GetTempSurface(readSize, format);
|
|
|
|
// There will already be a warning from inside of GetTempSurface, but
|
|
|
|
// it doesn't hurt to complain:
|
|
|
|
if (NS_WARN_IF(!resultSurf)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-27 08:57:43 -07:00
|
|
|
|
2014-10-07 21:15:39 -07:00
|
|
|
// Readback handles Flush/MarkDirty.
|
2015-02-12 19:00:41 -08:00
|
|
|
mGLContext->Readback(frontbuffer, resultSurf);
|
2014-10-07 21:15:39 -07:00
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(resultSurf, resultSurf);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(resultSurf);
|
2014-02-12 07:07:46 -08:00
|
|
|
|
2014-10-07 21:15:39 -07: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.
|
|
|
|
mSurface = resultSurf;
|
2014-02-12 07:07:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface*
|
|
|
|
CopyableCanvasLayer::GetTempSurface(const IntSize& aSize,
|
|
|
|
const SurfaceFormat aFormat)
|
2013-09-24 13:45:14 -07:00
|
|
|
{
|
|
|
|
if (!mCachedTempSurface ||
|
2014-04-30 16:12:41 -07:00
|
|
|
aSize != mCachedTempSurface->GetSize() ||
|
|
|
|
aFormat != mCachedTempSurface->GetFormat())
|
2013-09-24 13:45:14 -07:00
|
|
|
{
|
2014-05-12 19:20:27 -07:00
|
|
|
// Create a surface aligned to 8 bytes since that's the highest alignment WebGL can handle.
|
|
|
|
uint32_t stride = GetAlignedStride<8>(aSize.width * BytesPerPixel(aFormat));
|
|
|
|
mCachedTempSurface = Factory::CreateDataSourceSurfaceWithStride(aSize, aFormat, stride);
|
2013-09-24 13:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return mCachedTempSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyableCanvasLayer::DiscardTempSurface()
|
|
|
|
{
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:03:25 -07:00
|
|
|
}
|
|
|
|
}
|