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/. */
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
#include "CopyableCanvasRenderer.h"
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
#include "BasicLayersImpl.h" // for FillWithMask, etc
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "GLContext.h" // for GLContext
|
|
|
|
#include "GLScreenBuffer.h" // for GLScreenBuffer
|
|
|
|
#include "SharedSurface.h" // for SharedSurface
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "SharedSurfaceGL.h" // for SharedSurface
|
2013-08-12 03:17:23 +04: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 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
|
2014-05-13 06:20:27 +04:00
|
|
|
#include "mozilla/gfx/Tools.h"
|
2015-05-29 18:01:46 +03:00
|
|
|
#include "mozilla/gfx/Point.h" // for IntSize
|
2016-03-14 18:39:12 +03:00
|
|
|
#include "mozilla/layers/AsyncCanvasRenderer.h"
|
2015-06-19 02:07:21 +03:00
|
|
|
#include "mozilla/layers/PersistentBufferProvider.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc
|
|
|
|
#include "nsISupportsImpl.h" // for gfxContext::AddRef, etc
|
2015-05-07 12:07:53 +03:00
|
|
|
#include "nsRect.h" // for mozilla::gfx::IntRect
|
2014-05-13 06:20:26 +04:00
|
|
|
#include "gfxUtils.h"
|
2015-06-05 03:15:38 +03:00
|
|
|
#include "client/TextureClientSharedSurface.h"
|
2013-05-01 09:03:25 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-05-22 14:11:45 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::CopyableCanvasRenderer()
|
|
|
|
: mGLContext(nullptr)
|
|
|
|
, mBufferProvider(nullptr)
|
2014-10-08 08:15:39 +04:00
|
|
|
, mGLFrontbuffer(nullptr)
|
2017-08-03 08:55:14 +03:00
|
|
|
, mAsyncRenderer(nullptr)
|
2014-06-19 04:04:06 +04:00
|
|
|
, mIsAlphaPremultiplied(true)
|
2014-11-18 04:02:19 +03:00
|
|
|
, mOriginPos(gl::OriginPos::TopLeft)
|
2016-07-04 23:11:07 +03:00
|
|
|
, mIsMirror(false)
|
2017-08-03 08:55:14 +03:00
|
|
|
, mOpaque(true)
|
|
|
|
, mCachedTempSurface(nullptr)
|
2013-09-07 06:11:41 +04:00
|
|
|
{
|
2017-08-03 08:55:14 +03:00
|
|
|
MOZ_COUNT_CTOR(CopyableCanvasRenderer);
|
2013-09-07 06:11:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::~CopyableCanvasRenderer()
|
2013-09-07 06:11:41 +04:00
|
|
|
{
|
2017-08-03 08:55:14 +03:00
|
|
|
Destroy();
|
|
|
|
MOZ_COUNT_DTOR(CopyableCanvasRenderer);
|
2013-09-07 06:11:41 +04:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
void
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::Initialize(const CanvasInitializeData& aData)
|
2013-05-01 09:03:25 +04:00
|
|
|
{
|
2017-08-03 08:55:14 +03:00
|
|
|
CanvasRenderer::Initialize(aData);
|
|
|
|
|
2014-02-06 18:46:30 +04:00
|
|
|
if (aData.mGLContext) {
|
2013-05-01 09:03:25 +04:00
|
|
|
mGLContext = aData.mGLContext;
|
2014-06-19 04:04:06 +04:00
|
|
|
mIsAlphaPremultiplied = aData.mIsGLAlphaPremult;
|
2014-11-18 04:02:19 +03:00
|
|
|
mOriginPos = gl::OriginPos::BottomLeft;
|
2016-07-04 23:11:07 +03:00
|
|
|
mIsMirror = aData.mIsMirror;
|
2014-11-18 04:02:19 +03:00
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
|
2014-10-08 08:15:39 +04:00
|
|
|
if (aData.mFrontbufferGLTex) {
|
|
|
|
gfx::IntSize size(aData.mSize.width, aData.mSize.height);
|
2015-06-05 03:15:38 +03:00
|
|
|
mGLFrontbuffer = SharedSurface_Basic::Wrap(aData.mGLContext, size, aData.mHasAlpha,
|
|
|
|
aData.mFrontbufferGLTex);
|
2015-10-20 11:35:00 +03:00
|
|
|
mBufferProvider = aData.mBufferProvider;
|
2014-10-08 08:15:39 +04:00
|
|
|
}
|
2015-06-19 02:07:21 +03:00
|
|
|
} else if (aData.mBufferProvider) {
|
|
|
|
mBufferProvider = aData.mBufferProvider;
|
2015-10-12 06:21:03 +03:00
|
|
|
} else if (aData.mRenderer) {
|
|
|
|
mAsyncRenderer = aData.mRenderer;
|
|
|
|
mOriginPos = gl::OriginPos::BottomLeft;
|
2013-05-01 09:03:25 +04:00
|
|
|
} else {
|
2017-08-03 08:55:14 +03:00
|
|
|
MOZ_CRASH("GFX: CanvasRenderer created without BufferProvider, DrawTarget or GLContext?");
|
2013-05-01 09:03:25 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
mOpaque = !aData.mHasAlpha;
|
2013-05-01 09:03:25 +04:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:46:20 +04:00
|
|
|
bool
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::IsDataValid(const CanvasInitializeData& aData)
|
2013-12-05 02:46:20 +04:00
|
|
|
{
|
2014-10-08 08:15:39 +04:00
|
|
|
return mGLContext == aData.mGLContext;
|
2013-12-05 02:46:20 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
void
|
|
|
|
CopyableCanvasRenderer::ClearCachedResources()
|
|
|
|
{
|
|
|
|
if (mBufferProvider) {
|
|
|
|
mBufferProvider->ClearCachedResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyableCanvasRenderer::Destroy()
|
|
|
|
{
|
|
|
|
if (mBufferProvider) {
|
|
|
|
mBufferProvider->ClearCachedResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<SourceSurface>
|
|
|
|
CopyableCanvasRenderer::ReadbackSurface()
|
|
|
|
{
|
2017-08-22 08:47:33 +03:00
|
|
|
struct ScopedFireTransactionCallback {
|
|
|
|
explicit ScopedFireTransactionCallback(CopyableCanvasRenderer* aRenderer)
|
|
|
|
: mRenderer(aRenderer)
|
|
|
|
{
|
|
|
|
mRenderer->FirePreTransactionCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedFireTransactionCallback()
|
|
|
|
{
|
|
|
|
mRenderer->FireDidTransactionCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyableCanvasRenderer* mRenderer;
|
|
|
|
};
|
|
|
|
|
|
|
|
ScopedFireTransactionCallback callback(this);
|
2017-08-03 08:55:14 +03:00
|
|
|
if (mAsyncRenderer) {
|
|
|
|
MOZ_ASSERT(!mBufferProvider);
|
|
|
|
MOZ_ASSERT(!mGLContext);
|
|
|
|
return mAsyncRenderer->GetSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mGLContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface* frontbuffer = nullptr;
|
|
|
|
if (mGLFrontbuffer) {
|
|
|
|
frontbuffer = mGLFrontbuffer.get();
|
|
|
|
} else {
|
|
|
|
GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
const auto& front = screen->Front();
|
|
|
|
if (front) {
|
|
|
|
frontbuffer = front->Surf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontbuffer) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize readSize(frontbuffer->mSize);
|
|
|
|
SurfaceFormat format =
|
|
|
|
mOpaque ? SurfaceFormat::B8G8R8X8 : SurfaceFormat::B8G8R8A8;
|
|
|
|
bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied;
|
|
|
|
|
|
|
|
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 nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readback handles Flush/MarkDirty.
|
|
|
|
if (!mGLContext->Readback(frontbuffer, resultSurf)) {
|
|
|
|
NS_WARNING("Failed to read back canvas surface.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(resultSurf, resultSurf);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(resultSurf);
|
|
|
|
|
|
|
|
return resultSurf.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:46 +04:00
|
|
|
DataSourceSurface*
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::GetTempSurface(const IntSize& aSize,
|
2014-02-12 19:07:46 +04:00
|
|
|
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 06:20:27 +04:00
|
|
|
// Create a surface aligned to 8 bytes since that's the highest alignment WebGL can handle.
|
2016-08-25 20:57:39 +03:00
|
|
|
uint32_t stride = GetAlignedStride<8>(aSize.width, BytesPerPixel(aFormat));
|
2014-05-13 06:20:27 +04:00
|
|
|
mCachedTempSurface = Factory::CreateDataSourceSurfaceWithStride(aSize, aFormat, stride);
|
2013-09-25 00:45:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mCachedTempSurface;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|