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
|
2016-12-28 05:16:43 +03: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 "ShareableCanvasRenderer.h"
|
2016-12-28 05:16:43 +03:00
|
|
|
|
|
|
|
#include "GLContext.h" // for GLContext
|
|
|
|
#include "GLScreenBuffer.h" // for GLScreenBuffer
|
|
|
|
#include "SharedSurfaceGL.h" // for SurfaceFactory_GLTexture, etc
|
2018-08-10 18:15:23 +03:00
|
|
|
#include "gfxUtils.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2016-12-28 05:16:43 +03:00
|
|
|
#include "mozilla/layers/AsyncCanvasRenderer.h"
|
|
|
|
#include "mozilla/layers/TextureClientSharedSurface.h"
|
2018-08-10 18:15:23 +03:00
|
|
|
#include "mozilla/layers/CompositableForwarder.h"
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2016-12-28 05:16:43 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
ShareableCanvasRenderer::ShareableCanvasRenderer()
|
|
|
|
: mCanvasClient(nullptr),
|
|
|
|
mFactory(nullptr),
|
2016-12-28 05:16:43 +03:00
|
|
|
mFlags(TextureFlags::NO_FLAGS) {
|
2017-08-03 08:55:14 +03:00
|
|
|
MOZ_COUNT_CTOR(ShareableCanvasRenderer);
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
ShareableCanvasRenderer::~ShareableCanvasRenderer() {
|
|
|
|
MOZ_COUNT_DTOR(ShareableCanvasRenderer);
|
|
|
|
|
|
|
|
Destroy();
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
void ShareableCanvasRenderer::Initialize(const CanvasInitializeData& aData) {
|
|
|
|
CopyableCanvasRenderer::Initialize(aData);
|
2016-12-28 05:16:43 +03:00
|
|
|
|
|
|
|
mCanvasClient = nullptr;
|
|
|
|
|
|
|
|
if (!mGLContext) return;
|
|
|
|
|
|
|
|
gl::GLScreenBuffer* screen = mGLContext->Screen();
|
2019-02-28 18:00:06 +03:00
|
|
|
MOZ_ASSERT(screen);
|
|
|
|
gl::SurfaceCaps caps = screen->mCaps;
|
2016-12-28 05:16:43 +03:00
|
|
|
|
|
|
|
auto forwarder = GetForwarder();
|
|
|
|
|
|
|
|
mFlags = TextureFlags::ORIGIN_BOTTOM_LEFT;
|
|
|
|
if (!aData.mIsGLAlphaPremult) {
|
|
|
|
mFlags |= TextureFlags::NON_PREMULTIPLIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<gl::SurfaceFactory> factory =
|
|
|
|
gl::GLScreenBuffer::CreateFactory(mGLContext, caps, forwarder, mFlags);
|
2019-02-28 18:00:06 +03:00
|
|
|
if (factory) {
|
|
|
|
screen->Morph(std::move(factory));
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
void ShareableCanvasRenderer::ClearCachedResources() {
|
|
|
|
CopyableCanvasRenderer::ClearCachedResources();
|
|
|
|
|
|
|
|
if (mCanvasClient) {
|
|
|
|
mCanvasClient->Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareableCanvasRenderer::Destroy() {
|
|
|
|
CopyableCanvasRenderer::Destroy();
|
|
|
|
|
|
|
|
if (mCanvasClient) {
|
|
|
|
mCanvasClient->OnDetach();
|
|
|
|
mCanvasClient = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShareableCanvasRenderer::UpdateTarget(DrawTarget* aDestTarget) {
|
2020-01-09 01:19:14 +03:00
|
|
|
MOZ_ASSERT(!mOOPRenderer);
|
|
|
|
|
2016-12-28 05:16:43 +03:00
|
|
|
MOZ_ASSERT(aDestTarget);
|
|
|
|
if (!aDestTarget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> surface;
|
|
|
|
|
|
|
|
if (!mGLContext) {
|
|
|
|
AutoReturnSnapshot autoReturn;
|
|
|
|
|
|
|
|
if (mAsyncRenderer) {
|
|
|
|
surface = mAsyncRenderer->GetSurface();
|
|
|
|
} else if (mBufferProvider) {
|
|
|
|
surface = mBufferProvider->BorrowSnapshot();
|
|
|
|
autoReturn.mSnapshot = &surface;
|
|
|
|
autoReturn.mBufferProvider = mBufferProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(surface);
|
|
|
|
if (!surface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
aDestTarget->CopySurface(surface, IntRect(0, 0, mSize.width, mSize.height),
|
2016-12-28 05:16:43 +03:00
|
|
|
IntPoint(0, 0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl::SharedSurface* frontbuffer = nullptr;
|
2019-02-28 18:00:06 +03:00
|
|
|
|
|
|
|
gl::GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
const auto& front = screen->Front();
|
|
|
|
if (front) {
|
|
|
|
frontbuffer = front->Surf();
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontbuffer) {
|
|
|
|
NS_WARNING("Null frame received.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize readSize(frontbuffer->mSize);
|
2017-08-03 08:55:14 +03:00
|
|
|
SurfaceFormat format =
|
|
|
|
mOpaque ? SurfaceFormat::B8G8R8X8 : SurfaceFormat::B8G8R8A8;
|
2016-12-28 05:16:43 +03:00
|
|
|
bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied;
|
|
|
|
|
|
|
|
// Try to read back directly into aDestTarget's output buffer
|
|
|
|
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);
|
2017-07-24 13:24:18 +03:00
|
|
|
if (!mGLContext->Readback(frontbuffer, data)) {
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-28 05:16:43 +03:00
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(data, data);
|
|
|
|
}
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
aDestTarget->ReleaseBits(destData);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readback handles Flush/MarkDirty.
|
2017-07-24 13:24:18 +03:00
|
|
|
if (!mGLContext->Readback(frontbuffer, resultSurf)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-28 05:16:43 +03:00
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(resultSurf, resultSurf);
|
|
|
|
}
|
|
|
|
|
|
|
|
aDestTarget->CopySurface(resultSurf,
|
|
|
|
IntRect(0, 0, readSize.width, readSize.height),
|
|
|
|
IntPoint(0, 0));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
CanvasClient::CanvasClientType ShareableCanvasRenderer::GetCanvasClientType() {
|
2016-12-28 05:16:43 +03:00
|
|
|
if (mAsyncRenderer) {
|
|
|
|
return CanvasClient::CanvasClientAsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGLContext) {
|
|
|
|
return CanvasClient::CanvasClientTypeShSurf;
|
|
|
|
}
|
2020-01-09 01:19:14 +03:00
|
|
|
|
|
|
|
if (mOOPRenderer) {
|
|
|
|
return CanvasClient::CanvasClientTypeOOP;
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:16:43 +03:00
|
|
|
return CanvasClient::CanvasClientSurface;
|
|
|
|
}
|
|
|
|
|
2019-03-22 21:28:42 +03:00
|
|
|
void ShareableCanvasRenderer::UpdateCompositableClient(
|
|
|
|
wr::RenderRoot aRenderRoot) {
|
2017-08-03 08:55:14 +03:00
|
|
|
if (!CreateCompositable()) {
|
|
|
|
return;
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCanvasClient && mAsyncRenderer) {
|
|
|
|
mCanvasClient->UpdateAsync(mAsyncRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsDirty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 08:55:14 +03:00
|
|
|
ResetDirty();
|
2016-12-28 05:16:43 +03:00
|
|
|
|
|
|
|
FirePreTransactionCallback();
|
|
|
|
if (mBufferProvider && mBufferProvider->GetTextureClient()) {
|
2017-11-16 08:47:26 +03:00
|
|
|
if (!mBufferProvider->SetKnowsCompositor(GetForwarder())) {
|
2016-12-28 05:16:43 +03:00
|
|
|
gfxCriticalNote << "BufferProvider::SetForwarder failed";
|
|
|
|
return;
|
|
|
|
}
|
2019-03-22 21:28:42 +03:00
|
|
|
mCanvasClient->UpdateFromTexture(mBufferProvider->GetTextureClient(),
|
|
|
|
aRenderRoot);
|
2016-12-28 05:16:43 +03:00
|
|
|
} else {
|
2019-03-22 21:28:42 +03:00
|
|
|
mCanvasClient->Update(gfx::IntSize(mSize.width, mSize.height), this,
|
|
|
|
aRenderRoot);
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FireDidTransactionCallback();
|
|
|
|
|
2019-03-22 21:28:42 +03:00
|
|
|
mCanvasClient->Updated(aRenderRoot);
|
2016-12-28 05:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|