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: */
|
2015-10-12 06:21:02 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "AsyncCanvasRenderer.h"
|
|
|
|
|
|
|
|
#include "gfxUtils.h"
|
|
|
|
#include "GLContext.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "GLReadTexImageHelper.h"
|
|
|
|
#include "GLScreenBuffer.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2015-10-15 18:53:33 +03:00
|
|
|
#include "mozilla/layers/BufferTexture.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "mozilla/layers/CanvasClient.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/layers/TextureClient.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/layers/TextureClientSharedSurface.h"
|
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
AsyncCanvasRenderer::AsyncCanvasRenderer()
|
2015-10-12 06:21:03 +03:00
|
|
|
: mHTMLCanvasElement(nullptr)
|
|
|
|
, mContext(nullptr)
|
|
|
|
, mGLContext(nullptr)
|
|
|
|
, mIsAlphaPremultiplied(true)
|
|
|
|
, mWidth(0)
|
2015-10-12 06:21:02 +03:00
|
|
|
, mHeight(0)
|
|
|
|
, mCanvasClient(nullptr)
|
2015-10-12 06:21:03 +03:00
|
|
|
, mMutex("AsyncCanvasRenderer::mMutex")
|
2015-10-12 06:21:02 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(AsyncCanvasRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncCanvasRenderer::~AsyncCanvasRenderer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(AsyncCanvasRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncCanvasRenderer::NotifyElementAboutAttributesChanged()
|
|
|
|
{
|
2016-04-26 03:23:21 +03:00
|
|
|
class Runnable final : public mozilla::Runnable
|
2015-10-12 06:21:02 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Runnable(AsyncCanvasRenderer* aRenderer)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("Runnable")
|
|
|
|
, mRenderer(aRenderer)
|
2015-10-12 06:21:02 +03:00
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-10-12 06:21:02 +03:00
|
|
|
{
|
|
|
|
if (mRenderer) {
|
|
|
|
dom::HTMLCanvasElement::SetAttrFromAsyncCanvasRenderer(mRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCanvasRenderer> mRenderer;
|
2015-10-12 06:21:02 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new Runnable(this);
|
2015-10-12 06:21:02 +03:00
|
|
|
nsresult rv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch a runnable to the main-thread.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
|
|
|
AsyncCanvasRenderer::NotifyElementAboutInvalidation()
|
|
|
|
{
|
2016-04-26 03:23:21 +03:00
|
|
|
class Runnable final : public mozilla::Runnable
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Runnable(AsyncCanvasRenderer* aRenderer)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("Runnable")
|
|
|
|
, mRenderer(aRenderer)
|
2015-10-12 06:21:03 +03:00
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
if (mRenderer) {
|
|
|
|
dom::HTMLCanvasElement::InvalidateFromAsyncCanvasRenderer(mRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCanvasRenderer> mRenderer;
|
2015-10-12 06:21:03 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new Runnable(this);
|
2015-10-12 06:21:03 +03:00
|
|
|
nsresult rv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch a runnable to the main-thread.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
void
|
|
|
|
AsyncCanvasRenderer::SetCanvasClient(CanvasClient* aClient)
|
|
|
|
{
|
|
|
|
mCanvasClient = aClient;
|
|
|
|
if (aClient) {
|
2017-01-18 05:47:05 +03:00
|
|
|
mCanvasClientAsyncHandle = aClient->GetAsyncHandle();
|
2015-10-12 06:21:02 +03:00
|
|
|
} else {
|
2017-01-18 05:47:05 +03:00
|
|
|
mCanvasClientAsyncHandle = CompositableHandle();
|
2015-10-12 06:21:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
2017-06-01 23:43:00 +03:00
|
|
|
AsyncCanvasRenderer::SetActiveEventTarget()
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-06-01 23:43:00 +03:00
|
|
|
mActiveEventTarget = GetCurrentThreadSerialEventTarget();
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-01 23:43:00 +03:00
|
|
|
AsyncCanvasRenderer::ResetActiveEventTarget()
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-06-01 23:43:00 +03:00
|
|
|
mActiveEventTarget = nullptr;
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 23:43:00 +03:00
|
|
|
already_AddRefed<nsISerialEventTarget>
|
|
|
|
AsyncCanvasRenderer::GetActiveEventTarget()
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-06-01 23:43:00 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> result = mActiveEventTarget;
|
2015-10-12 06:21:03 +03:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
|
|
|
AsyncCanvasRenderer::CopyFromTextureClient(TextureClient* aTextureClient)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-04-26 06:08:56 +03:00
|
|
|
|
|
|
|
if (!aTextureClient) {
|
|
|
|
mSurfaceForBasic = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:33 +03:00
|
|
|
TextureClientAutoLock texLock(aTextureClient, layers::OpenMode::OPEN_READ);
|
2015-11-20 16:25:03 +03:00
|
|
|
if (!texLock.Succeeded()) {
|
2015-10-12 06:21:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx::IntSize& size = aTextureClient->GetSize();
|
|
|
|
// This buffer would be used later for content rendering. So we choose
|
|
|
|
// B8G8R8A8 format here.
|
|
|
|
const gfx::SurfaceFormat format = gfx::SurfaceFormat::B8G8R8A8;
|
|
|
|
// Avoid to create buffer every time.
|
|
|
|
if (!mSurfaceForBasic ||
|
|
|
|
size != mSurfaceForBasic->GetSize() ||
|
|
|
|
format != mSurfaceForBasic->GetFormat())
|
|
|
|
{
|
2016-08-25 20:57:39 +03:00
|
|
|
uint32_t stride = gfx::GetAlignedStride<8>(size.width, BytesPerPixel(format));
|
2015-10-12 06:21:03 +03:00
|
|
|
mSurfaceForBasic = gfx::Factory::CreateDataSourceSurfaceWithStride(size, format, stride);
|
2018-01-11 16:26:42 +03:00
|
|
|
if (!mSurfaceForBasic) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:33 +03:00
|
|
|
MappedTextureData mapped;
|
|
|
|
if (!aTextureClient->BorrowMappedData(mapped)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* lockedBytes = mapped.data;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfx::DataSourceSurface::ScopedMap map(mSurfaceForBasic,
|
|
|
|
gfx::DataSourceSurface::MapType::WRITE);
|
|
|
|
if (!map.IsMapped()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:33 +03:00
|
|
|
MOZ_ASSERT(map.GetStride() == mapped.stride);
|
2015-10-12 06:21:03 +03:00
|
|
|
memcpy(map.GetData(), lockedBytes, map.GetStride() * mSurfaceForBasic->GetSize().height);
|
|
|
|
|
|
|
|
if (mSurfaceForBasic->GetFormat() == gfx::SurfaceFormat::R8G8B8A8 ||
|
|
|
|
mSurfaceForBasic->GetFormat() == gfx::SurfaceFormat::R8G8B8X8) {
|
|
|
|
gl::SwapRAndBComponents(mSurfaceForBasic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
|
|
|
AsyncCanvasRenderer::UpdateTarget()
|
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!mGLContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl::SharedSurface* frontbuffer = nullptr;
|
|
|
|
gl::GLScreenBuffer* screen = mGLContext->Screen();
|
|
|
|
const auto& front = screen->Front();
|
|
|
|
if (front) {
|
|
|
|
frontbuffer = front->Surf();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontbuffer) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frontbuffer->mType == gl::SharedSurfaceType::Basic) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx::IntSize& size = frontbuffer->mSize;
|
|
|
|
// This buffer would be used later for content rendering. So we choose
|
|
|
|
// B8G8R8A8 format here.
|
|
|
|
const gfx::SurfaceFormat format = gfx::SurfaceFormat::B8G8R8A8;
|
2016-08-25 20:57:39 +03:00
|
|
|
uint32_t stride = gfx::GetAlignedStride<8>(size.width, BytesPerPixel(format));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
2015-10-12 06:21:03 +03:00
|
|
|
gfx::Factory::CreateDataSourceSurfaceWithStride(size, format, stride);
|
|
|
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!surface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frontbuffer->ReadbackBySharedHandle(surface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied;
|
|
|
|
if (needsPremult) {
|
|
|
|
gfxUtils::PremultiplyDataSurface(surface, surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface.forget();
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
|
|
|
AsyncCanvasRenderer::GetSurface()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-12 06:21:03 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2016-03-05 18:20:47 +03:00
|
|
|
if (mSurfaceForBasic) {
|
|
|
|
// Since SourceSurface isn't thread-safe, we need copy to a new SourceSurface.
|
2017-11-09 11:43:29 +03:00
|
|
|
gfx::DataSourceSurface::ScopedMap srcMap(mSurfaceForBasic, gfx::DataSourceSurface::READ);
|
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> result =
|
|
|
|
gfx::Factory::CreateDataSourceSurfaceWithStride(mSurfaceForBasic->GetSize(),
|
|
|
|
mSurfaceForBasic->GetFormat(),
|
2017-11-09 11:43:29 +03:00
|
|
|
srcMap.GetStride());
|
2018-01-11 16:26:42 +03:00
|
|
|
if (NS_WARN_IF(!result)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-05 18:20:47 +03:00
|
|
|
|
|
|
|
gfx::DataSourceSurface::ScopedMap dstMap(result, gfx::DataSourceSurface::WRITE);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!srcMap.IsMapped()) ||
|
|
|
|
NS_WARN_IF(!dstMap.IsMapped())) {
|
2016-03-04 10:24:00 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
memcpy(dstMap.GetData(),
|
|
|
|
srcMap.GetData(),
|
|
|
|
srcMap.GetStride() * mSurfaceForBasic->GetSize().height);
|
|
|
|
return result.forget();
|
|
|
|
} else {
|
|
|
|
return UpdateTarget();
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AsyncCanvasRenderer::GetInputStream(const char *aMimeType,
|
|
|
|
const char16_t *aEncoderOptions,
|
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> surface = GetSurface();
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!surface) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:43:30 +03:00
|
|
|
gfx::DataSourceSurface::ScopedMap map(surface, gfx::DataSourceSurface::READ);
|
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
// Handle y flip.
|
2017-11-09 11:43:30 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> dataSurf = gl::YInvertImageSurface(surface, map.GetStride());
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
return gfxUtils::GetInputStream(dataSurf, false, aMimeType, aEncoderOptions, aStream);
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|