2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-05-01 09:03:25 +04:00
|
|
|
* 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),
|
|
|
|
mAsyncRenderer(nullptr),
|
2014-06-19 04:04:06 +04:00
|
|
|
mIsAlphaPremultiplied(true),
|
2014-11-18 04:02:19 +03:00
|
|
|
mOriginPos(gl::OriginPos::TopLeft),
|
2017-08-03 08:55:14 +03:00
|
|
|
mOpaque(true),
|
|
|
|
mCachedTempSurface(nullptr) {
|
|
|
|
MOZ_COUNT_CTOR(CopyableCanvasRenderer);
|
2013-09-07 06:11:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
CopyableCanvasRenderer::~CopyableCanvasRenderer() {
|
|
|
|
Destroy();
|
|
|
|
MOZ_COUNT_DTOR(CopyableCanvasRenderer);
|
2013-09-07 06:11:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
void CopyableCanvasRenderer::Initialize(const CanvasInitializeData& aData) {
|
|
|
|
CanvasRenderer::Initialize(aData);
|
|
|
|
|
2014-02-06 18:46:30 +04:00
|
|
|
if (aData.mGLContext) {
|
2019-03-02 01:37:37 +03:00
|
|
|
if (aData.mGLContext->IsDestroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
|
2013-05-01 09:03:25 +04:00
|
|
|
MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
mOpaque = !aData.mHasAlpha;
|
2013-05-01 09:03:25 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
bool CopyableCanvasRenderer::IsDataValid(const CanvasInitializeData& aData) {
|
2018-03-07 02:24:46 +03:00
|
|
|
return mGLContext == aData.mGLContext &&
|
|
|
|
mBufferProvider == aData.mBufferProvider;
|
2013-12-05 02:46:20 +04:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
void CopyableCanvasRenderer::ClearCachedResources() {
|
2018-02-10 02:13:52 +03:00
|
|
|
SetDirty();
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
if (mBufferProvider) {
|
|
|
|
mBufferProvider->ClearCachedResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
mCachedTempSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyableCanvasRenderer::Destroy() {
|
|
|
|
if (mBufferProvider) {
|
|
|
|
mBufferProvider->ClearCachedResources();
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:31:55 +03:00
|
|
|
mBufferProvider = nullptr;
|
2017-08-03 08:55:14 +03:00
|
|
|
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;
|
2019-02-28 18:00:06 +03:00
|
|
|
if (mGLContext->Screen()) {
|
2018-03-14 15:30:42 +03:00
|
|
|
const auto& front = mGLContext->Screen()->Front();
|
2017-08-03 08:55:14 +03:00
|
|
|
if (front) {
|
|
|
|
frontbuffer = front->Surf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontbuffer) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize readSize(frontbuffer->mSize);
|
2017-12-22 04:42:52 +03:00
|
|
|
SurfaceFormat format = frontbuffer->mHasAlpha ? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8;
|
2017-08-03 08:55:14 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface* CopyableCanvasRenderer::GetTempSurface(
|
2014-02-12 19:07:46 +04:00
|
|
|
const IntSize& aSize, const SurfaceFormat aFormat) {
|
2014-05-01 03:12:41 +04:00
|
|
|
if (!mCachedTempSurface || aSize != mCachedTempSurface->GetSize() ||
|
|
|
|
aFormat != mCachedTempSurface->GetFormat()) {
|
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
|