diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /gfx/layers/client | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
41 files changed, 12025 insertions, 0 deletions
diff --git a/gfx/layers/client/CanvasClient.cpp b/gfx/layers/client/CanvasClient.cpp new file mode 100644 index 0000000000..2040dae6bf --- /dev/null +++ b/gfx/layers/client/CanvasClient.cpp @@ -0,0 +1,97 @@ +/* -*- 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 + * 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 "CanvasClient.h" + +#include "ClientCanvasLayer.h" // for ClientCanvasLayer +#include "gfx2DGlue.h" // for ImageFormatToSurfaceFormat +#include "gfxPlatform.h" // for gfxPlatform +#include "mozilla/gfx/BaseSize.h" // for BaseSize +#include "mozilla/gfx/gfxVars.h" +#include "mozilla/layers/BufferTexture.h" +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/CompositorBridgeChild.h" // for CompositorBridgeChild +#include "mozilla/layers/LayersTypes.h" +#include "mozilla/layers/OOPCanvasRenderer.h" +#include "mozilla/layers/TextureClient.h" // for TextureClient, etc +#include "mozilla/layers/TextureClientOGL.h" +#include "mozilla/layers/TextureClientRecycleAllocator.h" +#include "nsDebug.h" // for printf_stderr, NS_ASSERTION +#include "nsXULAppAPI.h" // for XRE_GetProcessType, etc + +using namespace mozilla::gfx; +using namespace mozilla::gl; + +namespace mozilla { +namespace layers { + +void CanvasClient::UseTexture(TextureClient* const aTexture) { + MOZ_ASSERT(aTexture); + + const auto isClientNonPremult = + bool(mTextureFlags & TextureFlags::NON_PREMULTIPLIED); + const auto isTextureNonPremult = + bool(aTexture->GetFlags() & TextureFlags::NON_PREMULTIPLIED); + MOZ_ALWAYS_TRUE(isTextureNonPremult == isClientNonPremult); + + bool changed = false; + + if (aTexture != mFrontBuffer) { + if (!aTexture->IsSharedWithCompositor()) { + if (!AddTextureClient(aTexture)) { + return; + } + } + changed = true; + mFrontBuffer = aTexture; + } + + AutoTArray<CompositableForwarder::TimedTextureClient, 1> textures; + CompositableForwarder::TimedTextureClient* t = textures.AppendElement(); + t->mTextureClient = aTexture; + t->mPictureRect = nsIntRect(nsIntPoint(0, 0), aTexture->GetSize()); + t->mFrameID = mFrameID; + + GetForwarder()->UseTextures(this, textures); + if (changed) { + aTexture->SyncWithObject(GetForwarder()->GetSyncObject()); + } +} + +static constexpr bool kIsWindows = +#ifdef XP_WIN + true; +#else + false; +#endif + +RefPtr<TextureClient> CanvasClient::CreateTextureClientForCanvas( + const gfx::SurfaceFormat aFormat, const gfx::IntSize aSize, + const TextureFlags aFlags) { + if (kIsWindows) { + // With WebRender, host side uses data of TextureClient longer. + // Then back buffer reuse in CanvasClient2D::Update() does not work. It + // causes a lot of TextureClient allocations. For reducing the allocations, + // TextureClientRecycler is used. + if (GetForwarder() && GetForwarder()->GetCompositorBackendType() == + LayersBackend::LAYERS_WR) { + return GetTextureClientRecycler()->CreateOrRecycle( + aFormat, aSize, BackendSelector::Canvas, mTextureFlags | aFlags); + } + return CreateTextureClientForDrawing( + aFormat, aSize, BackendSelector::Canvas, mTextureFlags | aFlags); + } + + // XXX - We should use CreateTextureClientForDrawing, but we first need + // to use double buffering. + gfx::BackendType backend = + gfxPlatform::GetPlatform()->GetPreferredCanvasBackend(); + return TextureClient::CreateForRawBufferAccess( + GetForwarder(), aFormat, aSize, backend, mTextureFlags | aFlags); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/CanvasClient.h b/gfx/layers/client/CanvasClient.h new file mode 100644 index 0000000000..2136e6f652 --- /dev/null +++ b/gfx/layers/client/CanvasClient.h @@ -0,0 +1,71 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_CANVASCLIENT_H +#define MOZILLA_GFX_CANVASCLIENT_H + +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/Attributes.h" // for override +#include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor +#include "mozilla/layers/TextureClient.h" // for TextureClient, etc +#include "mozilla/layers/PersistentBufferProvider.h" + +#include "mozilla/MaybeOneOf.h" + +#include "mozilla/mozalloc.h" // for operator delete + +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Types.h" // for SurfaceFormat + +namespace mozilla { +namespace layers { + +class CompositableForwarder; + +/** + * Compositable client for 2d and webgl canvas. + */ +class CanvasClient final : public CompositableClient { + int32_t mFrameID = 0; + RefPtr<TextureClient> mFrontBuffer; + + public: + /** + * Creates, configures, and returns a new canvas client. If necessary, a + * message will be sent to the compositor to create a corresponding image + * host. + */ + CanvasClient(CompositableForwarder* aFwd, const TextureFlags flags) + : CompositableClient(aFwd, flags) {} + + virtual ~CanvasClient() = default; + + void Clear() { mFrontBuffer = nullptr; } + + bool AddTextureClient(TextureClient* aTexture) override { + ++mFrameID; + return CompositableClient::AddTextureClient(aTexture); + } + + TextureInfo GetTextureInfo() const override { + return TextureInfo(CompositableType::IMAGE, mTextureFlags); + } + + void OnDetach() override { Clear(); } + + RefPtr<TextureClient> CreateTextureClientForCanvas(gfx::SurfaceFormat, + gfx::IntSize, + TextureFlags); + void UseTexture(TextureClient*); +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ClientCanvasLayer.cpp b/gfx/layers/client/ClientCanvasLayer.cpp new file mode 100644 index 0000000000..ca717b0a89 --- /dev/null +++ b/gfx/layers/client/ClientCanvasLayer.cpp @@ -0,0 +1,41 @@ +/* -*- 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 + * 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 "ClientCanvasLayer.h" +#include "GeckoProfiler.h" // for AUTO_PROFILER_LABEL +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "nsCOMPtr.h" // for already_AddRefed + +namespace mozilla { +namespace layers { + +ClientCanvasLayer::~ClientCanvasLayer() { MOZ_COUNT_DTOR(ClientCanvasLayer); } + +void ClientCanvasLayer::RenderLayer() { + AUTO_PROFILER_LABEL("ClientCanvasLayer::RenderLayer", GRAPHICS); + + RenderMaskLayers(this); + + ClientCanvasRenderer* canvasRenderer = + mCanvasRenderer->AsClientCanvasRenderer(); + MOZ_ASSERT(canvasRenderer); + canvasRenderer->UpdateCompositableClient(); + ClientManager()->Hold(this); +} + +RefPtr<CanvasRenderer> ClientCanvasLayer::CreateCanvasRendererInternal() { + return new ClientCanvasRenderer(this); +} + +already_AddRefed<CanvasLayer> ClientLayerManager::CreateCanvasLayer() { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + RefPtr<ClientCanvasLayer> layer = new ClientCanvasLayer(this); + CREATE_SHADOW(Canvas); + return layer.forget(); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientCanvasLayer.h b/gfx/layers/client/ClientCanvasLayer.h new file mode 100644 index 0000000000..64576f9e79 --- /dev/null +++ b/gfx/layers/client/ClientCanvasLayer.h @@ -0,0 +1,80 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTCANVASLAYER_H +#define GFX_CLIENTCANVASLAYER_H + +#include "ClientCanvasRenderer.h" +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "Layers.h" // for CanvasLayer, etc +#include "mozilla/Attributes.h" // for override +#include "mozilla/layers/CanvasClient.h" // for CanvasClient, etc +#include "mozilla/layers/LayersMessages.h" // for CanvasLayerAttributes, etc +#include "mozilla/mozalloc.h" // for operator delete +#include "nsDebug.h" // for NS_ASSERTION +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc + +namespace mozilla { +namespace layers { + +class CompositableClient; +class ShadowableLayer; + +class ClientCanvasLayer : public CanvasLayer, public ClientLayer { + public: + explicit ClientCanvasLayer(ClientLayerManager* aLayerManager) + : CanvasLayer(aLayerManager, static_cast<ClientLayer*>(this)) { + MOZ_COUNT_CTOR(ClientCanvasLayer); + } + + RefPtr<CanvasRenderer> CreateCanvasRendererInternal() override; + + protected: + virtual ~ClientCanvasLayer(); + + public: + void SetVisibleRegion(const LayerIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + CanvasLayer::SetVisibleRegion(aRegion); + } + + void RenderLayer() override; + + void ClearCachedResources() override { + mCanvasRenderer->ClearCachedResources(); + } + + void HandleMemoryPressure() override { + mCanvasRenderer->ClearCachedResources(); + } + + void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override { + aAttrs = CanvasLayerAttributes(mSamplingFilter, mBounds); + } + + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + void Disconnect() override { mCanvasRenderer->DisconnectClient(); } + + CompositableClient* GetCompositableClient() override { + ClientCanvasRenderer* canvasRenderer = + mCanvasRenderer->AsClientCanvasRenderer(); + MOZ_ASSERT(canvasRenderer); + return canvasRenderer->GetCanvasClient(); + } + + protected: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ClientCanvasRenderer.cpp b/gfx/layers/client/ClientCanvasRenderer.cpp new file mode 100644 index 0000000000..f9b99fb4f8 --- /dev/null +++ b/gfx/layers/client/ClientCanvasRenderer.cpp @@ -0,0 +1,37 @@ +/* -*- 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 + * 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 "ClientCanvasRenderer.h" + +#include "ClientCanvasLayer.h" + +namespace mozilla { +namespace layers { + +CompositableForwarder* ClientCanvasRenderer::GetForwarder() { + return mLayer->Manager()->AsShadowForwarder(); +} + +bool ClientCanvasRenderer::CreateCompositable() { + if (!mCanvasClient) { + auto compositableFlags = TextureFlags::NO_FLAGS; + if (!mData.mIsAlphaPremult) { + // WR needs this flag marked on the compositable, not just the texture. + compositableFlags |= TextureFlags::NON_PREMULTIPLIED; + } + mCanvasClient = new CanvasClient(GetForwarder(), compositableFlags); + + if (mLayer->HasShadow()) { + mCanvasClient->Connect(); + GetForwarder()->AsLayerForwarder()->Attach(mCanvasClient, mLayer); + } + } + + return true; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientCanvasRenderer.h b/gfx/layers/client/ClientCanvasRenderer.h new file mode 100644 index 0000000000..5ea857b10f --- /dev/null +++ b/gfx/layers/client/ClientCanvasRenderer.h @@ -0,0 +1,34 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTCANVASRENDERER_H +#define GFX_CLIENTCANVASRENDERER_H + +#include "ShareableCanvasRenderer.h" + +namespace mozilla { +namespace layers { + +class ClientCanvasLayer; + +class ClientCanvasRenderer final : public ShareableCanvasRenderer { + public: + explicit ClientCanvasRenderer(ClientCanvasLayer* aLayer) : mLayer(aLayer) {} + + ClientCanvasRenderer* AsClientCanvasRenderer() override { return this; } + + CompositableForwarder* GetForwarder() override; + + bool CreateCompositable() override; + + protected: + ClientCanvasLayer* mLayer; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ClientColorLayer.cpp b/gfx/layers/client/ClientColorLayer.cpp new file mode 100644 index 0000000000..4ecaf5829e --- /dev/null +++ b/gfx/layers/client/ClientColorLayer.cpp @@ -0,0 +1,61 @@ +/* -*- 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 + * 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 "ClientLayerManager.h" // for ClientLayerManager, etc +#include "Layers.h" // for ColorLayer, etc +#include "mozilla/layers/LayersMessages.h" // for ColorLayerAttributes, etc +#include "mozilla/mozalloc.h" // for operator new +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsDebug.h" // for NS_ASSERTION +#include "nsISupportsImpl.h" // for Layer::AddRef, etc +#include "nsRegion.h" // for nsIntRegion + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +class ClientColorLayer : public ColorLayer, public ClientLayer { + public: + explicit ClientColorLayer(ClientLayerManager* aLayerManager) + : ColorLayer(aLayerManager, static_cast<ClientLayer*>(this)) { + MOZ_COUNT_CTOR(ClientColorLayer); + } + + protected: + MOZ_COUNTED_DTOR_OVERRIDE(ClientColorLayer) + + public: + void SetVisibleRegion(const LayerIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + ColorLayer::SetVisibleRegion(aRegion); + } + + void RenderLayer() override { RenderMaskLayers(this); } + + void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override { + aAttrs = ColorLayerAttributes(GetColor(), GetBounds()); + } + + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + protected: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } +}; + +already_AddRefed<ColorLayer> ClientLayerManager::CreateColorLayer() { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + RefPtr<ClientColorLayer> layer = new ClientColorLayer(this); + CREATE_SHADOW(Color); + return layer.forget(); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientContainerLayer.cpp b/gfx/layers/client/ClientContainerLayer.cpp new file mode 100644 index 0000000000..3bcb754197 --- /dev/null +++ b/gfx/layers/client/ClientContainerLayer.cpp @@ -0,0 +1,31 @@ +/* -*- 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 + * 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 "ClientContainerLayer.h" +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "mozilla/mozalloc.h" // for operator new +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsISupportsImpl.h" // for Layer::AddRef, etc + +namespace mozilla { +namespace layers { + +already_AddRefed<ContainerLayer> ClientLayerManager::CreateContainerLayer() { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + RefPtr<ClientContainerLayer> layer = new ClientContainerLayer(this); + CREATE_SHADOW(Container); + return layer.forget(); +} + +already_AddRefed<RefLayer> ClientLayerManager::CreateRefLayer() { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + RefPtr<ClientRefLayer> layer = new ClientRefLayer(this); + CREATE_SHADOW(Ref); + return layer.forget(); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientContainerLayer.h b/gfx/layers/client/ClientContainerLayer.h new file mode 100644 index 0000000000..f0331e6d16 --- /dev/null +++ b/gfx/layers/client/ClientContainerLayer.h @@ -0,0 +1,162 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTCONTAINERLAYER_H +#define GFX_CLIENTCONTAINERLAYER_H + +#include <stdint.h> // for uint32_t +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "Layers.h" // for Layer, ContainerLayer, etc +#include "nsDebug.h" // for NS_ASSERTION +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc +#include "nsISupportsUtils.h" // for NS_ADDREF, NS_RELEASE +#include "nsRegion.h" // for nsIntRegion +#include "nsTArray.h" // for AutoTArray +#include "ReadbackProcessor.h" +#include "ClientPaintedLayer.h" + +namespace mozilla { +namespace layers { + +class ShadowableLayer; + +class ClientContainerLayer : public ContainerLayer, public ClientLayer { + public: + explicit ClientContainerLayer(ClientLayerManager* aManager) + : ContainerLayer(aManager, static_cast<ClientLayer*>(this)) { + MOZ_COUNT_CTOR(ClientContainerLayer); + mSupportsComponentAlphaChildren = true; + } + + protected: + virtual ~ClientContainerLayer() { + ContainerLayer::RemoveAllChildren(); + MOZ_COUNT_DTOR(ClientContainerLayer); + } + + public: + void RenderLayer() override { + RenderMaskLayers(this); + + DefaultComputeSupportsComponentAlphaChildren(); + + ReadbackProcessor readback; + readback.BuildUpdates(this); + + nsTArray<Layer*> children = CollectChildren(); + for (uint32_t i = 0; i < children.Length(); i++) { + Layer* child = children.ElementAt(i); + + ToClientLayer(child)->RenderLayerWithReadback(&readback); + + if (!ClientManager()->GetRepeatTransaction() && + !child->GetInvalidRegion().IsEmpty()) { + child->Mutated(); + } + } + } + + void SetVisibleRegion(const LayerIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + ContainerLayer::SetVisibleRegion(aRegion); + } + bool InsertAfter(Layer* aChild, Layer* aAfter) override { + if (!ClientManager()->InConstruction()) { + NS_ERROR("Can only set properties in construction phase"); + return false; + } + + if (!ContainerLayer::InsertAfter(aChild, aAfter)) { + return false; + } + + ClientManager()->AsShadowForwarder()->InsertAfter( + ClientManager()->Hold(this), ClientManager()->Hold(aChild), + aAfter ? ClientManager()->Hold(aAfter) : nullptr); + return true; + } + + bool RemoveChild(Layer* aChild) override { + if (!ClientManager()->InConstruction()) { + NS_ERROR("Can only set properties in construction phase"); + return false; + } + // hold on to aChild before we remove it! + ShadowableLayer* heldChild = ClientManager()->Hold(aChild); + if (!ContainerLayer::RemoveChild(aChild)) { + return false; + } + ClientManager()->AsShadowForwarder()->RemoveChild( + ClientManager()->Hold(this), heldChild); + return true; + } + + bool RepositionChild(Layer* aChild, Layer* aAfter) override { + if (!ClientManager()->InConstruction()) { + NS_ERROR("Can only set properties in construction phase"); + return false; + } + if (!ContainerLayer::RepositionChild(aChild, aAfter)) { + return false; + } + ClientManager()->AsShadowForwarder()->RepositionChild( + ClientManager()->Hold(this), ClientManager()->Hold(aChild), + aAfter ? ClientManager()->Hold(aAfter) : nullptr); + return true; + } + + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + void ComputeEffectiveTransforms( + const gfx::Matrix4x4& aTransformToSurface) override { + DefaultComputeEffectiveTransforms(aTransformToSurface); + } + + void ForceIntermediateSurface() { mUseIntermediateSurface = true; } + + void SetSupportsComponentAlphaChildren(bool aSupports) { + mSupportsComponentAlphaChildren = aSupports; + } + + protected: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } +}; + +class ClientRefLayer : public RefLayer, public ClientLayer { + public: + explicit ClientRefLayer(ClientLayerManager* aManager) + : RefLayer(aManager, static_cast<ClientLayer*>(this)) { + MOZ_COUNT_CTOR(ClientRefLayer); + } + + protected: + MOZ_COUNTED_DTOR_OVERRIDE(ClientRefLayer) + + public: + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + void RenderLayer() override { RenderMaskLayers(this); } + + void ComputeEffectiveTransforms( + const gfx::Matrix4x4& aTransformToSurface) override { + DefaultComputeEffectiveTransforms(aTransformToSurface); + } + + private: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ClientImageLayer.cpp b/gfx/layers/client/ClientImageLayer.cpp new file mode 100644 index 0000000000..a82801fbfe --- /dev/null +++ b/gfx/layers/client/ClientImageLayer.cpp @@ -0,0 +1,152 @@ +/* -*- 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 + * 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 "ClientLayerManager.h" // for ClientLayerManager, etc +#include "ImageContainer.h" // for AutoLockImage, etc +#include "ImageLayers.h" // for ImageLayer +#include "mozilla/Attributes.h" // for override +#include "mozilla/RefPtr.h" // for RefPtr +#include "mozilla/layers/CompositorTypes.h" +#include "mozilla/layers/ImageClient.h" // for ImageClient, etc +#include "mozilla/layers/LayersMessages.h" // for ImageLayerAttributes, etc +#include "mozilla/mozalloc.h" // for operator delete, etc +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsDebug.h" // for NS_ASSERTION +#include "nsISupportsImpl.h" // for Layer::AddRef, etc +#include "nsRegion.h" // for nsIntRegion + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +class ClientImageLayer : public ImageLayer, public ClientLayer { + public: + explicit ClientImageLayer(ClientLayerManager* aLayerManager) + : ImageLayer(aLayerManager, static_cast<ClientLayer*>(this)), + mImageClientTypeContainer(CompositableType::UNKNOWN) { + MOZ_COUNT_CTOR(ClientImageLayer); + } + + protected: + virtual ~ClientImageLayer() { + DestroyBackBuffer(); + MOZ_COUNT_DTOR(ClientImageLayer); + } + + void SetContainer(ImageContainer* aContainer) override { + ImageLayer::SetContainer(aContainer); + mImageClientTypeContainer = CompositableType::UNKNOWN; + } + + void SetVisibleRegion(const LayerIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + ImageLayer::SetVisibleRegion(aRegion); + } + + void RenderLayer() override; + + void ClearCachedResources() override { DestroyBackBuffer(); } + + bool SupportsAsyncUpdate() override { + if (GetImageClientType() == CompositableType::IMAGE_BRIDGE) { + return true; + } + return false; + } + + void HandleMemoryPressure() override { + if (mImageClient) { + mImageClient->HandleMemoryPressure(); + } + } + + void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override { + aAttrs = ImageLayerAttributes(mSamplingFilter, mScaleToSize, mScaleMode); + } + + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + void Disconnect() override { DestroyBackBuffer(); } + + void DestroyBackBuffer() { + if (mImageClient) { + mImageClient->SetLayer(nullptr); + mImageClient->OnDetach(); + mImageClient = nullptr; + } + } + + CompositableClient* GetCompositableClient() override { return mImageClient; } + + protected: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } + + CompositableType GetImageClientType() { + if (mImageClientTypeContainer != CompositableType::UNKNOWN) { + return mImageClientTypeContainer; + } + + if (mContainer->IsAsync()) { + mImageClientTypeContainer = CompositableType::IMAGE_BRIDGE; + return mImageClientTypeContainer; + } + + AutoLockImage autoLock(mContainer); + + mImageClientTypeContainer = autoLock.HasImage() ? CompositableType::IMAGE + : CompositableType::UNKNOWN; + return mImageClientTypeContainer; + } + + RefPtr<ImageClient> mImageClient; + CompositableType mImageClientTypeContainer; +}; + +void ClientImageLayer::RenderLayer() { + RenderMaskLayers(this); + + if (!mContainer) { + return; + } + + if (!mImageClient || + !mImageClient->UpdateImage(mContainer, GetContentFlags())) { + CompositableType type = GetImageClientType(); + if (type == CompositableType::UNKNOWN) { + return; + } + TextureFlags flags = TextureFlags::DEFAULT; + mImageClient = ImageClient::CreateImageClient( + type, ClientManager()->AsShadowForwarder(), flags); + if (!mImageClient) { + return; + } + mImageClient->SetLayer(this); + if (HasShadow() && !mContainer->IsAsync()) { + mImageClient->Connect(); + ClientManager()->AsShadowForwarder()->Attach(mImageClient, this); + } + if (!mImageClient->UpdateImage(mContainer, GetContentFlags())) { + return; + } + } + ClientManager()->Hold(this); +} + +already_AddRefed<ImageLayer> ClientLayerManager::CreateImageLayer() { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + RefPtr<ClientImageLayer> layer = new ClientImageLayer(this); + CREATE_SHADOW(Image); + return layer.forget(); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientLayerManager.cpp b/gfx/layers/client/ClientLayerManager.cpp new file mode 100644 index 0000000000..34a9eb566c --- /dev/null +++ b/gfx/layers/client/ClientLayerManager.cpp @@ -0,0 +1,903 @@ +/* -*- 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 + * 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 "ClientLayerManager.h" +#include "GeckoProfiler.h" // for AUTO_PROFILER_LABEL +#include "gfxEnv.h" // for gfxEnv +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/Hal.h" +#include "mozilla/StaticPrefs_apz.h" +#include "mozilla/StaticPrefs_gfx.h" +#include "mozilla/StaticPrefs_layers.h" +#include "mozilla/dom/BrowserChild.h" // for BrowserChild +#include "mozilla/hal_sandbox/PHal.h" // for ScreenConfiguration +#include "mozilla/layers/CompositableClient.h" +#include "mozilla/layers/CompositorBridgeChild.h" // for CompositorBridgeChild +#include "mozilla/layers/FrameUniformityData.h" +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/LayersMessages.h" // for EditReply, etc +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor +#include "mozilla/layers/LayerTransactionChild.h" +#include "mozilla/layers/PersistentBufferProvider.h" +#include "mozilla/layers/SyncObject.h" +#include "mozilla/layers/TransactionIdAllocator.h" +#include "mozilla/PerfStats.h" +#include "ClientReadbackLayer.h" // for ClientReadbackLayer +#include "nsAString.h" +#include "nsDisplayList.h" +#include "nsIWidgetListener.h" +#include "nsLayoutUtils.h" +#include "nsTArray.h" // for AutoTArray +#include "nsXULAppAPI.h" // for XRE_GetProcessType, etc +#include "TiledLayerBuffer.h" +#include "FrameLayerBuilder.h" // for FrameLayerbuilder +#ifdef MOZ_WIDGET_ANDROID +# include "AndroidBridge.h" +# include "LayerMetricsWrapper.h" +#endif +#ifdef XP_WIN +# include "mozilla/gfx/DeviceManagerDx.h" +# include "gfxDWriteFonts.h" +#endif + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +ClientLayerManager::ClientLayerManager(nsIWidget* aWidget) + : mPhase(PHASE_NONE), + mWidget(aWidget), + mPaintedLayerCallback(nullptr), + mPaintedLayerCallbackData(nullptr), + mLatestTransactionId{0}, + mLastPaintTime(TimeDuration::Forever()), + mTargetRotation(ROTATION_0), + mRepeatTransaction(false), + mIsRepeatTransaction(false), + mTransactionIncomplete(false), + mCompositorMightResample(false), + mNeedsComposite(false), + mQueuedAsyncPaints(false), + mNotifyingWidgetListener(false), + mPaintSequenceNumber(0), + mForwarder(new ShadowLayerForwarder(this)) { + MOZ_COUNT_CTOR(ClientLayerManager); + mMemoryPressureObserver = MemoryPressureObserver::Create(this); +} + +ClientLayerManager::~ClientLayerManager() { + mMemoryPressureObserver->Unregister(); + ClearCachedResources(); + // Stop receiveing AsyncParentMessage at Forwarder. + // After the call, the message is directly handled by LayerTransactionChild. + // Basically this function should be called in ShadowLayerForwarder's + // destructor. But when the destructor is triggered by + // CompositorBridgeChild::Destroy(), the destructor can not handle it + // correctly. See Bug 1000525. + mForwarder->StopReceiveAsyncParentMessge(); + mRoot = nullptr; + + MOZ_COUNT_DTOR(ClientLayerManager); +} + +bool ClientLayerManager::Initialize( + PCompositorBridgeChild* aCBChild, bool aShouldAccelerate, + TextureFactoryIdentifier* aTextureFactoryIdentifier) { + MOZ_ASSERT(mForwarder); + MOZ_ASSERT(aTextureFactoryIdentifier); + + nsTArray<LayersBackend> backendHints; + gfxPlatform::GetPlatform()->GetCompositorBackends(aShouldAccelerate, + backendHints); + if (backendHints.IsEmpty()) { + gfxCriticalNote << "Failed to get backend hints."; + return false; + } + + PLayerTransactionChild* shadowManager = + aCBChild->SendPLayerTransactionConstructor(backendHints, LayersId{0}); + + TextureFactoryIdentifier textureFactoryIdentifier; + shadowManager->SendGetTextureFactoryIdentifier(&textureFactoryIdentifier); + if (textureFactoryIdentifier.mParentBackend == LayersBackend::LAYERS_NONE) { + gfxCriticalNote << "Failed to create an OMT compositor."; + return false; + } + + mForwarder->SetShadowManager(shadowManager); + UpdateTextureFactoryIdentifier(textureFactoryIdentifier); + *aTextureFactoryIdentifier = textureFactoryIdentifier; + return true; +} + +void ClientLayerManager::Destroy() { + MOZ_DIAGNOSTIC_ASSERT(!mNotifyingWidgetListener, + "Try to avoid destroying widgets and layer managers " + "during DidCompositeWindow, if you can"); + + // It's important to call ClearCachedResource before Destroy because the + // former will early-return if the later has already run. + ClearCachedResources(); + LayerManager::Destroy(); + + if (mTransactionIdAllocator) { + // Make sure to notify the refresh driver just in case it's waiting on a + // pending transaction. Do this at the top of the event loop so we don't + // cause a paint to occur during compositor shutdown. + RefPtr<TransactionIdAllocator> allocator = mTransactionIdAllocator; + TransactionId id = mLatestTransactionId; + + RefPtr<Runnable> task = NS_NewRunnableFunction( + "TransactionIdAllocator::NotifyTransactionCompleted", + [allocator, id]() -> void { + allocator->NotifyTransactionCompleted(id); + }); + NS_DispatchToMainThread(task.forget()); + } + + // Forget the widget pointer in case we outlive our owning widget. + mWidget = nullptr; +} + +int32_t ClientLayerManager::GetMaxTextureSize() const { + return mForwarder->GetMaxTextureSize(); +} + +void ClientLayerManager::SetDefaultTargetConfiguration( + BufferMode aDoubleBuffering, ScreenRotation aRotation) { + mTargetRotation = aRotation; +} + +void ClientLayerManager::SetRoot(Layer* aLayer) { + if (mRoot != aLayer) { + // Have to hold the old root and its children in order to + // maintain the same view of the layer tree in this process as + // the parent sees. Otherwise layers can be destroyed + // mid-transaction and bad things can happen (v. bug 612573) + if (mRoot) { + Hold(mRoot); + } + mForwarder->SetRoot(Hold(aLayer)); + NS_ASSERTION(aLayer, "Root can't be null"); + NS_ASSERTION(aLayer->Manager() == this, "Wrong manager"); + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + mRoot = aLayer; + } +} + +void ClientLayerManager::Mutated(Layer* aLayer) { + LayerManager::Mutated(aLayer); + + NS_ASSERTION(InConstruction() || InDrawing(), "wrong phase"); + mForwarder->Mutated(Hold(aLayer)); +} + +void ClientLayerManager::MutatedSimple(Layer* aLayer) { + LayerManager::MutatedSimple(aLayer); + + NS_ASSERTION(InConstruction() || InDrawing(), "wrong phase"); + mForwarder->MutatedSimple(Hold(aLayer)); +} + +already_AddRefed<ReadbackLayer> ClientLayerManager::CreateReadbackLayer() { + RefPtr<ReadbackLayer> layer = new ClientReadbackLayer(this); + return layer.forget(); +} + +bool ClientLayerManager::BeginTransactionWithTarget(gfxContext* aTarget, + const nsCString& aURL) { +#ifdef MOZ_DUMP_PAINTING + // When we are dump painting, we expect to be able to read the contents of + // compositable clients from previous paints inside this layer transaction + // before we flush async paints in EndTransactionInternal. + // So to work around this flush async paints now. + if (gfxEnv::DumpPaint()) { + FlushAsyncPaints(); + } +#endif + + MOZ_ASSERT(mForwarder, + "ClientLayerManager::BeginTransaction without forwarder"); + if (!mForwarder->IPCOpen()) { + gfxCriticalNote << "ClientLayerManager::BeginTransaction with IPC channel " + "down. GPU process may have died."; + return false; + } + + mInTransaction = true; + mTransactionStart = TimeStamp::Now(); + mURL = aURL; + +#ifdef MOZ_LAYERS_HAVE_LOG + MOZ_LAYERS_LOG(("[----- BeginTransaction")); + Log(); +#endif + + NS_ASSERTION(!InTransaction(), "Nested transactions not allowed"); + mPhase = PHASE_CONSTRUCTION; + + MOZ_ASSERT(mKeepAlive.IsEmpty(), "uncommitted txn?"); + + // If the last transaction was incomplete (a failed DoEmptyTransaction), + // don't signal a new transaction to ShadowLayerForwarder. Carry on adding + // to the previous transaction. + hal::ScreenOrientation orientation; + if (dom::BrowserChild* window = mWidget->GetOwningBrowserChild()) { + orientation = window->GetOrientation(); + } else { + hal::ScreenConfiguration currentConfig; + hal::GetCurrentScreenConfiguration(¤tConfig); + orientation = currentConfig.orientation(); + } + LayoutDeviceIntRect targetBounds = mWidget->GetNaturalBounds(); + targetBounds.MoveTo(0, 0); + mForwarder->BeginTransaction(targetBounds.ToUnknownRect(), mTargetRotation, + orientation); + + // If we're drawing on behalf of a context with async pan/zoom + // enabled, then the entire buffer of painted layers might be + // composited (including resampling) asynchronously before we get + // a chance to repaint, so we have to ensure that it's all valid + // and not rotated. + // + // Desktop does not support async zoom yet, so we ignore this for those + // platforms. +#if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_UIKIT) + if (mWidget && mWidget->GetOwningBrowserChild()) { + mCompositorMightResample = AsyncPanZoomEnabled(); + } +#endif + + // If we have a non-default target, we need to let our shadow manager draw + // to it. This will happen at the end of the transaction. + if (aTarget && XRE_IsParentProcess()) { + mShadowTarget = aTarget; + } else { + NS_ASSERTION( + !aTarget, + "Content-process ClientLayerManager::BeginTransactionWithTarget not " + "supported"); + } + + // If this is a new paint, increment the paint sequence number. + if (!mIsRepeatTransaction) { + // Increment the paint sequence number even if test logging isn't + // enabled in this process; it may be enabled in the parent process, + // and the parent process expects unique sequence numbers. + ++mPaintSequenceNumber; + if (StaticPrefs::apz_test_logging_enabled()) { + mApzTestData.StartNewPaint(mPaintSequenceNumber); + } + } + return true; +} + +bool ClientLayerManager::BeginTransaction(const nsCString& aURL) { + return BeginTransactionWithTarget(nullptr, aURL); +} + +bool ClientLayerManager::EndTransactionInternal( + DrawPaintedLayerCallback aCallback, void* aCallbackData, + EndTransactionFlags) { + // This just causes the compositor to check whether the GPU is done with its + // textures or not and unlock them if it is. This helps us avoid the case + // where we take a long time painting asynchronously, turn IPC back on at + // the end of that, and then have to wait for the compositor to to get into + // TiledLayerBufferComposite::UseTiles before getting a response. + if (mForwarder) { + mForwarder->UpdateTextureLocks(); + } + + // Wait for any previous async paints to complete before starting to paint + // again. Do this outside the profiler and telemetry block so this doesn't + // count as time spent rasterizing. + { + PaintTelemetry::AutoRecord record( + PaintTelemetry::Metric::FlushRasterization); + FlushAsyncPaints(); + } + + PaintTelemetry::AutoRecord record(PaintTelemetry::Metric::Rasterization); + AUTO_PROFILER_TRACING_MARKER("Paint", "Rasterize", GRAPHICS); + PerfStats::AutoMetricRecording<PerfStats::Metric::Rasterizing> autoRecording; + + Maybe<TimeStamp> startTime; + if (StaticPrefs::layers_acceleration_draw_fps()) { + startTime = Some(TimeStamp::Now()); + } + + AUTO_PROFILER_LABEL("ClientLayerManager::EndTransactionInternal", GRAPHICS); + +#ifdef MOZ_LAYERS_HAVE_LOG + MOZ_LAYERS_LOG((" ----- (beginning paint)")); + Log(); +#endif + + NS_ASSERTION(InConstruction(), "Should be in construction phase"); + mPhase = PHASE_DRAWING; + + ClientLayer* root = ClientLayer::ToClientLayer(GetRoot()); + + mTransactionIncomplete = false; + mQueuedAsyncPaints = false; + + // Apply pending tree updates before recomputing effective + // properties. + auto scrollIdsUpdated = GetRoot()->ApplyPendingUpdatesToSubtree(); + + mPaintedLayerCallback = aCallback; + mPaintedLayerCallbackData = aCallbackData; + + GetRoot()->ComputeEffectiveTransforms(Matrix4x4()); + + // Skip the painting if the device is in device-reset status. + if (!gfxPlatform::GetPlatform()->DidRenderingDeviceReset()) { + if (StaticPrefs::gfx_content_always_paint() && XRE_IsContentProcess()) { + TimeStamp start = TimeStamp::Now(); + root->RenderLayer(); + mLastPaintTime = TimeStamp::Now() - start; + } else { + root->RenderLayer(); + } + } else { + gfxCriticalNote << "LayerManager::EndTransaction skip RenderLayer()."; + } + + // Once we're sure we're not going to fall back to a full paint, + // notify the scroll frames which had pending updates. + if (!mTransactionIncomplete) { + for (ScrollableLayerGuid::ViewID scrollId : scrollIdsUpdated) { + nsLayoutUtils::NotifyPaintSkipTransaction(scrollId); + } + } + + if (!mRepeatTransaction && !GetRoot()->GetInvalidRegion().IsEmpty()) { + GetRoot()->Mutated(); + } + + if (!mIsRepeatTransaction) { + mAnimationReadyTime = TimeStamp::Now(); + GetRoot()->StartPendingAnimations(mAnimationReadyTime); + } + + mPaintedLayerCallback = nullptr; + mPaintedLayerCallbackData = nullptr; + + // Go back to the construction phase if the transaction isn't complete. + // Layout will update the layer tree and call EndTransaction(). + mPhase = mTransactionIncomplete ? PHASE_CONSTRUCTION : PHASE_NONE; + + NS_ASSERTION(!aCallback || !mTransactionIncomplete, + "If callback is not null, transaction must be complete"); + + if (gfxPlatform::GetPlatform()->DidRenderingDeviceReset()) { + FrameLayerBuilder::InvalidateAllLayers(this); + } + + if (startTime) { + PaintTiming& pt = mForwarder->GetPaintTiming(); + pt.rasterMs() = (TimeStamp::Now() - startTime.value()).ToMilliseconds(); + } + + return !mTransactionIncomplete; +} + +void ClientLayerManager::StorePluginWidgetConfigurations( + const nsTArray<nsIWidget::Configuration>& aConfigurations) { + if (mForwarder) { + mForwarder->StorePluginWidgetConfigurations(aConfigurations); + } +} + +void ClientLayerManager::EndTransaction(DrawPaintedLayerCallback aCallback, + void* aCallbackData, + EndTransactionFlags aFlags) { + if (!mForwarder->IPCOpen()) { + mInTransaction = false; + return; + } + + if (mWidget) { + mWidget->PrepareWindowEffects(); + } + EndTransactionInternal(aCallback, aCallbackData, aFlags); + if (XRE_IsContentProcess()) { + RegisterPayload({CompositionPayloadType::eContentPaint, TimeStamp::Now()}); + } + ForwardTransaction(!(aFlags & END_NO_REMOTE_COMPOSITE)); + + if (mRepeatTransaction) { + mRepeatTransaction = false; + mIsRepeatTransaction = true; + + // BeginTransaction will reset the transaction start time, but we + // would like to keep the original time for telemetry purposes. + TimeStamp transactionStart = mTransactionStart; + if (BeginTransaction(mURL)) { + mTransactionStart = transactionStart; + ClientLayerManager::EndTransaction(aCallback, aCallbackData, aFlags); + } + + mIsRepeatTransaction = false; + } else { + MakeSnapshotIfRequired(); + } + + mInTransaction = false; + mTransactionStart = TimeStamp(); +} + +bool ClientLayerManager::EndEmptyTransaction(EndTransactionFlags aFlags) { + mInTransaction = false; + + if (!mRoot || !mForwarder->IPCOpen()) { + return false; + } + + if (!EndTransactionInternal(nullptr, nullptr, aFlags)) { + // Return without calling ForwardTransaction. This leaves the + // ShadowLayerForwarder transaction open; the following + // EndTransaction will complete it. + if (PaintThread::Get() && mQueuedAsyncPaints) { + PaintThread::Get()->QueueEndLayerTransaction(nullptr); + } + return false; + } + if (mWidget) { + mWidget->PrepareWindowEffects(); + } + ForwardTransaction(!(aFlags & END_NO_REMOTE_COMPOSITE)); + MakeSnapshotIfRequired(); + return true; +} + +CompositorBridgeChild* ClientLayerManager::GetRemoteRenderer() { + if (!mWidget) { + return nullptr; + } + + return mWidget->GetRemoteRenderer(); +} + +CompositorBridgeChild* ClientLayerManager::GetCompositorBridgeChild() { + if (!XRE_IsParentProcess()) { + return CompositorBridgeChild::Get(); + } + return GetRemoteRenderer(); +} + +void ClientLayerManager::FlushAsyncPaints() { + AUTO_PROFILER_LABEL_CATEGORY_PAIR(GRAPHICS_FlushingAsyncPaints); + + CompositorBridgeChild* cbc = GetCompositorBridgeChild(); + if (cbc) { + cbc->FlushAsyncPaints(); + } +} + +void ClientLayerManager::ScheduleComposite() { + mForwarder->ScheduleComposite(); +} + +void ClientLayerManager::DidComposite(TransactionId aTransactionId, + const TimeStamp& aCompositeStart, + const TimeStamp& aCompositeEnd) { + if (!mWidget) { + return; + } + + // Notifying the observers may tick the refresh driver which can cause + // a lot of different things to happen that may affect the lifetime of + // this layer manager. So let's make sure this object stays alive until + // the end of the method invocation. + RefPtr<ClientLayerManager> selfRef = this; + + // |aTransactionId| will be > 0 if the compositor is acknowledging a shadow + // layers transaction. + if (aTransactionId.IsValid()) { + nsIWidgetListener* listener = mWidget->GetWidgetListener(); + if (listener) { + mNotifyingWidgetListener = true; + listener->DidCompositeWindow(aTransactionId, aCompositeStart, + aCompositeEnd); + mNotifyingWidgetListener = false; + } + // DidCompositeWindow might have called Destroy on us and nulled out + // mWidget, see bug 1510058. Re-check it here. + if (mWidget) { + listener = mWidget->GetAttachedWidgetListener(); + if (listener) { + listener->DidCompositeWindow(aTransactionId, aCompositeStart, + aCompositeEnd); + } + } + if (mTransactionIdAllocator) { + mTransactionIdAllocator->NotifyTransactionCompleted(aTransactionId); + } + } + + // These observers fire whether or not we were in a transaction. + for (size_t i = 0; i < mDidCompositeObservers.Length(); i++) { + mDidCompositeObservers[i]->DidComposite(); + } +} + +void ClientLayerManager::GetCompositorSideAPZTestData( + APZTestData* aData) const { + if (mForwarder->HasShadowManager()) { + if (!mForwarder->GetShadowManager()->SendGetAPZTestData(aData)) { + NS_WARNING("Call to PLayerTransactionChild::SendGetAPZTestData() failed"); + } + } +} + +void ClientLayerManager::SetTransactionIdAllocator( + TransactionIdAllocator* aAllocator) { + // When changing the refresh driver, the previous refresh driver may never + // receive updates of pending transactions it's waiting for. So clear the + // waiting state before assigning another refresh driver. + if (mTransactionIdAllocator && (aAllocator != mTransactionIdAllocator)) { + mTransactionIdAllocator->ClearPendingTransactions(); + + // We should also reset the transaction id of the new allocator to previous + // allocator's last transaction id, so that completed transactions for + // previous allocator will be ignored and won't confuse the new allocator. + if (aAllocator) { + aAllocator->ResetInitialTransactionId( + mTransactionIdAllocator->LastTransactionId()); + } + } + + mTransactionIdAllocator = aAllocator; +} + +float ClientLayerManager::RequestProperty(const nsAString& aProperty) { + if (mForwarder->HasShadowManager()) { + float value; + if (!mForwarder->GetShadowManager()->SendRequestProperty( + nsString(aProperty), &value)) { + NS_WARNING("Call to PLayerTransactionChild::SendGetAPZTestData() failed"); + } + return value; + } + return -1; +} + +void ClientLayerManager::StartNewRepaintRequest( + SequenceNumber aSequenceNumber) { + if (StaticPrefs::apz_test_logging_enabled()) { + mApzTestData.StartNewRepaintRequest(aSequenceNumber); + } +} + +void ClientLayerManager::GetFrameUniformity(FrameUniformityData* aOutData) { + if (HasShadowManager()) { + mForwarder->GetShadowManager()->SendGetFrameUniformity(aOutData); + } +} + +void ClientLayerManager::MakeSnapshotIfRequired() { + if (!mShadowTarget) { + return; + } + if (mWidget) { + if (CompositorBridgeChild* remoteRenderer = GetRemoteRenderer()) { + // The compositor doesn't draw to a different sized surface + // when there's a rotation. Instead we rotate the result + // when drawing into dt + LayoutDeviceIntRect outerBounds = mWidget->GetBounds(); + + IntRect bounds = ToOutsideIntRect(mShadowTarget->GetClipExtents()); + if (mTargetRotation) { + bounds = + RotateRect(bounds, outerBounds.ToUnknownRect(), mTargetRotation); + } + + SurfaceDescriptor inSnapshot; + if (!bounds.IsEmpty() && + mForwarder->AllocSurfaceDescriptor( + bounds.Size(), gfxContentType::COLOR_ALPHA, &inSnapshot)) { + // Make a copy of |inSnapshot| because the call to send it over IPC + // will call forget() on the Shmem inside, and zero it out. + SurfaceDescriptor outSnapshot = inSnapshot; + + if (remoteRenderer->SendMakeSnapshot(inSnapshot, bounds)) { + RefPtr<DataSourceSurface> surf = GetSurfaceForDescriptor(outSnapshot); + DrawTarget* dt = mShadowTarget->GetDrawTarget(); + + Rect dstRect(bounds.X(), bounds.Y(), bounds.Width(), bounds.Height()); + Rect srcRect(0, 0, bounds.Width(), bounds.Height()); + + gfx::Matrix rotate = ComputeTransformForUnRotation( + outerBounds.ToUnknownRect(), mTargetRotation); + + gfx::Matrix oldMatrix = dt->GetTransform(); + dt->SetTransform(rotate * oldMatrix); + dt->DrawSurface(surf, dstRect, srcRect, DrawSurfaceOptions(), + DrawOptions(1.0f, CompositionOp::OP_OVER)); + dt->SetTransform(oldMatrix); + } + mForwarder->DestroySurfaceDescriptor(&outSnapshot); + } + } + } + mShadowTarget = nullptr; +} + +void ClientLayerManager::FlushRendering() { + if (mWidget) { + if (CompositorBridgeChild* remoteRenderer = mWidget->GetRemoteRenderer()) { + if (mWidget->SynchronouslyRepaintOnResize() || + StaticPrefs::layers_force_synchronous_resize()) { + remoteRenderer->SendFlushRendering(); + } else { + remoteRenderer->SendFlushRenderingAsync(); + } + } + } +} + +void ClientLayerManager::WaitOnTransactionProcessed() { + CompositorBridgeChild* remoteRenderer = GetCompositorBridgeChild(); + if (remoteRenderer) { + remoteRenderer->SendWaitOnTransactionProcessed(); + } +} +void ClientLayerManager::UpdateTextureFactoryIdentifier( + const TextureFactoryIdentifier& aNewIdentifier) { + mForwarder->IdentifyTextureHost(aNewIdentifier); +} + +void ClientLayerManager::SendInvalidRegion(const nsIntRegion& aRegion) { + if (mWidget) { + if (CompositorBridgeChild* remoteRenderer = mWidget->GetRemoteRenderer()) { + remoteRenderer->SendNotifyRegionInvalidated(aRegion); + } + } +} + +uint32_t ClientLayerManager::StartFrameTimeRecording(int32_t aBufferSize) { + CompositorBridgeChild* renderer = GetRemoteRenderer(); + if (renderer) { + uint32_t startIndex; + renderer->SendStartFrameTimeRecording(aBufferSize, &startIndex); + return startIndex; + } + return -1; +} + +void ClientLayerManager::StopFrameTimeRecording( + uint32_t aStartIndex, nsTArray<float>& aFrameIntervals) { + CompositorBridgeChild* renderer = GetRemoteRenderer(); + if (renderer) { + renderer->SendStopFrameTimeRecording(aStartIndex, &aFrameIntervals); + } +} + +void ClientLayerManager::ForwardTransaction(bool aScheduleComposite) { + AUTO_PROFILER_TRACING_MARKER("Paint", "ForwardTransaction", GRAPHICS); + TimeStamp start = TimeStamp::Now(); + + GetCompositorBridgeChild()->EndCanvasTransaction(); + + // Skip the synchronization for buffer since we also skip the painting during + // device-reset status. With OMTP, we have to wait for async paints + // before we synchronize and it's done on the paint thread. + RefPtr<SyncObjectClient> syncObject = nullptr; + if (!gfxPlatform::GetPlatform()->DidRenderingDeviceReset()) { + if (mForwarder->GetSyncObject() && + mForwarder->GetSyncObject()->IsSyncObjectValid()) { + syncObject = mForwarder->GetSyncObject(); + } + } + + // If there were async paints queued, then we need to notify the paint thread + // that we finished queuing async paints so it can schedule a runnable after + // all async painting is finished to do a texture sync and unblock the main + // thread if it is waiting before doing a new layer transaction. + if (mQueuedAsyncPaints) { + MOZ_ASSERT(PaintThread::Get()); + PaintThread::Get()->QueueEndLayerTransaction(syncObject); + } else if (syncObject) { + syncObject->Synchronize(); + } + + mPhase = PHASE_FORWARD; + + mLatestTransactionId = + mTransactionIdAllocator->GetTransactionId(!mIsRepeatTransaction); + TimeStamp refreshStart = mTransactionIdAllocator->GetTransactionStart(); + if (!refreshStart) { + refreshStart = mTransactionStart; + } + + if (StaticPrefs::gfx_content_always_paint() && XRE_IsContentProcess()) { + mForwarder->SendPaintTime(mLatestTransactionId, mLastPaintTime); + } + + // forward this transaction's changeset to our LayerManagerComposite + bool sent = false; + bool ok = mForwarder->EndTransaction( + mRegionToClear, mLatestTransactionId, aScheduleComposite, + mPaintSequenceNumber, mIsRepeatTransaction, + mTransactionIdAllocator->GetVsyncId(), + mTransactionIdAllocator->GetVsyncStart(), refreshStart, mTransactionStart, + mContainsSVG, mURL, &sent, mPayload); + + if (ok) { + if (sent) { + // Our payload has now been dispatched. + mPayload.Clear(); + mNeedsComposite = false; + } + } else if (HasShadowManager()) { + NS_WARNING("failed to forward Layers transaction"); + } + + if (!sent) { + // Clear the transaction id so that it doesn't get returned + // unless we forwarded to somewhere that doesn't actually + // have a compositor. + mTransactionIdAllocator->RevokeTransactionId(mLatestTransactionId); + mLatestTransactionId = mLatestTransactionId.Prev(); + } + + mPhase = PHASE_NONE; + + // this may result in Layers being deleted, which results in + // PLayer::Send__delete__() and DeallocShmem() + mKeepAlive.Clear(); + + BrowserChild* window = mWidget ? mWidget->GetOwningBrowserChild() : nullptr; + if (window) { + TimeStamp end = TimeStamp::Now(); + window->DidRequestComposite(start, end); + } +} + +ShadowableLayer* ClientLayerManager::Hold(Layer* aLayer) { + MOZ_ASSERT(HasShadowManager(), "top-level tree, no shadow tree to remote to"); + + ShadowableLayer* shadowable = ClientLayer::ToClientLayer(aLayer); + MOZ_ASSERT(shadowable, "trying to remote an unshadowable layer"); + + mKeepAlive.AppendElement(aLayer); + return shadowable; +} + +bool ClientLayerManager::IsCompositingCheap() { + // Whether compositing is cheap depends on the parent backend. + return mForwarder->mShadowManager && + LayerManager::IsCompositingCheap( + mForwarder->GetCompositorBackendType()); +} + +bool ClientLayerManager::AreComponentAlphaLayersEnabled() { + return GetCompositorBackendType() != LayersBackend::LAYERS_BASIC && + AsShadowForwarder()->SupportsComponentAlpha() && + LayerManager::AreComponentAlphaLayersEnabled(); +} + +void ClientLayerManager::SetIsFirstPaint() { mForwarder->SetIsFirstPaint(); } + +void ClientLayerManager::SetFocusTarget(const FocusTarget& aFocusTarget) { + mForwarder->SetFocusTarget(aFocusTarget); +} + +void ClientLayerManager::ClearCachedResources(Layer* aSubtree) { + if (mDestroyed) { + // ClearCachedResource was already called by ClientLayerManager::Destroy + return; + } + MOZ_ASSERT(!HasShadowManager() || !aSubtree); + mForwarder->ClearCachedResources(); + if (aSubtree) { + ClearLayer(aSubtree); + } else if (mRoot) { + ClearLayer(mRoot); + } +} + +void ClientLayerManager::OnMemoryPressure(MemoryPressureReason aWhy) { + if (mRoot) { + HandleMemoryPressureLayer(mRoot); + } + + if (GetCompositorBridgeChild()) { + GetCompositorBridgeChild()->HandleMemoryPressure(); + } +} + +void ClientLayerManager::ClearLayer(Layer* aLayer) { + aLayer->ClearCachedResources(); + for (Layer* child = aLayer->GetFirstChild(); child; + child = child->GetNextSibling()) { + ClearLayer(child); + } +} + +void ClientLayerManager::HandleMemoryPressureLayer(Layer* aLayer) { + ClientLayer::ToClientLayer(aLayer)->HandleMemoryPressure(); + for (Layer* child = aLayer->GetFirstChild(); child; + child = child->GetNextSibling()) { + HandleMemoryPressureLayer(child); + } +} + +void ClientLayerManager::GetBackendName(nsAString& aName) { + switch (mForwarder->GetCompositorBackendType()) { + case LayersBackend::LAYERS_NONE: + aName.AssignLiteral("None"); + return; + case LayersBackend::LAYERS_BASIC: + aName.AssignLiteral("Basic"); + return; + case LayersBackend::LAYERS_OPENGL: + aName.AssignLiteral("OpenGL"); + return; + case LayersBackend::LAYERS_D3D11: { +#ifdef XP_WIN + if (DeviceManagerDx::Get()->IsWARP()) { + aName.AssignLiteral("Direct3D 11 WARP"); + } else { + aName.AssignLiteral("Direct3D 11"); + } +#endif + return; + } + default: + MOZ_CRASH("Invalid backend"); + } +} + +bool ClientLayerManager::AsyncPanZoomEnabled() const { + return mWidget && mWidget->AsyncPanZoomEnabled(); +} + +void ClientLayerManager::SetLayersObserverEpoch(LayersObserverEpoch aEpoch) { + mForwarder->SetLayersObserverEpoch(aEpoch); +} + +void ClientLayerManager::AddDidCompositeObserver( + DidCompositeObserver* aObserver) { + if (!mDidCompositeObservers.Contains(aObserver)) { + mDidCompositeObservers.AppendElement(aObserver); + } +} + +void ClientLayerManager::RemoveDidCompositeObserver( + DidCompositeObserver* aObserver) { + mDidCompositeObservers.RemoveElement(aObserver); +} + +already_AddRefed<PersistentBufferProvider> +ClientLayerManager::CreatePersistentBufferProvider(const gfx::IntSize& aSize, + gfx::SurfaceFormat aFormat) { + // Don't use a shared buffer provider if compositing is considered "not cheap" + // because the canvas will most likely be flattened into a thebes layer + // instead of being sent to the compositor, in which case rendering into + // shared memory is wasteful. + if (IsCompositingCheap()) { + RefPtr<PersistentBufferProvider> provider = + PersistentBufferProviderShared::Create(aSize, aFormat, + AsShadowForwarder()); + if (provider) { + return provider.forget(); + } + } + + return LayerManager::CreatePersistentBufferProvider(aSize, aFormat); +} + +ClientLayer::~ClientLayer() { MOZ_COUNT_DTOR(ClientLayer); } + +ClientLayer* ClientLayer::ToClientLayer(Layer* aLayer) { + return static_cast<ClientLayer*>(aLayer->ImplData()); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientLayerManager.h b/gfx/layers/client/ClientLayerManager.h new file mode 100644 index 0000000000..587f9ec3f2 --- /dev/null +++ b/gfx/layers/client/ClientLayerManager.h @@ -0,0 +1,413 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTLAYERMANAGER_H +#define GFX_CLIENTLAYERMANAGER_H + +#include <cstddef> // for size_t +#include <cstdint> // for uint32_t, int32_t +#include <new> // for operator new +#include <string> // for string +#include <utility> // for forward +#include "gfxContext.h" // for gfxContext +#include "mozilla/AlreadyAddRefed.h" // for already_AddRefed +#include "mozilla/Assertions.h" // for AssertionConditionType, MOZ_ASSERT, MOZ_ASSERT_HELPER2 +#include "mozilla/RefPtr.h" // for RefPtr +#include "mozilla/TimeStamp.h" // for TimeStamp, BaseTimeDuration +#include "mozilla/WidgetUtils.h" // for ScreenRotation +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Types.h" // for SurfaceFormat +#include "mozilla/layers/APZTestData.h" // for APZTestData, SequenceNumber +#include "mozilla/layers/CompositorTypes.h" // for TextureFactoryIdentifier +#include "mozilla/layers/LayerManager.h" // for LayerManager::DrawPaintedLayerCallback, DidCompositeObserver (ptr only), Laye... +#include "mozilla/layers/LayersTypes.h" // for LayerHandle, LayersBackend, TransactionId, BufferMode, LayersBackend::LAYERS_... +#include "mozilla/layers/MemoryPressureObserver.h" // for MemoryPressureListener, MemoryPressureObserver (ptr only), MemoryPressureReason +#include "mozilla/layers/ScrollableLayerGuid.h" // for ScrollableLayerGuid, ScrollableLayerGuid::ViewID +#include "mozilla/layers/ShadowLayers.h" // for ShadowLayerForwarder, ShadowableLayer +#include "nsISupports.h" // for MOZ_COUNTED_DEFAULT_CTOR +#include "nsIThread.h" // for TimeDuration +#include "nsIWidget.h" // for nsIWidget +#include "nsRegion.h" // for nsIntRegion +#include "nsStringFwd.h" // for nsCString, nsAString +#include "nsTArray.h" // for nsTArray + +// XXX Includes that could be avoided by moving function implementation to the +// cpp file. +#include "mozilla/StaticPrefs_apz.h" // for apz_test_logging_enabled + +namespace mozilla { +namespace layers { + +class CanvasLayer; +class ColorLayer; +class ContainerLayer; +class ClientPaintedLayer; +class CompositorBridgeChild; +class FocusTarget; +class FrameUniformityData; +class ImageLayer; +class Layer; +class PCompositorBridgeChild; +class PaintTiming; +class PaintedLayer; +class PersistentBufferProvider; +class ReadbackLayer; +class ReadbackProcessor; +class RefLayer; +class TransactionIdAllocator; + +class ClientLayerManager final : public LayerManager, + public MemoryPressureListener { + typedef nsTArray<RefPtr<Layer> > LayerRefArray; + + public: + explicit ClientLayerManager(nsIWidget* aWidget); + bool Initialize(PCompositorBridgeChild* aCBChild, bool aShouldAccelerate, + TextureFactoryIdentifier* aTextureFactoryIdentifier); + void Destroy() override; + + protected: + virtual ~ClientLayerManager(); + + public: + ShadowLayerForwarder* AsShadowForwarder() override { return mForwarder; } + + KnowsCompositor* AsKnowsCompositor() override { return mForwarder; } + + ClientLayerManager* AsClientLayerManager() override { return this; } + + int32_t GetMaxTextureSize() const override; + + void SetDefaultTargetConfiguration(BufferMode aDoubleBuffering, + ScreenRotation aRotation); + bool BeginTransactionWithTarget(gfxContext* aTarget, + const nsCString& aURL) override; + bool BeginTransaction(const nsCString& aURL) override; + bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) override; + void EndTransaction(DrawPaintedLayerCallback aCallback, void* aCallbackData, + EndTransactionFlags aFlags = END_DEFAULT) override; + + LayersBackend GetBackendType() override { + return LayersBackend::LAYERS_CLIENT; + } + LayersBackend GetCompositorBackendType() override { + return AsShadowForwarder()->GetCompositorBackendType(); + } + void GetBackendName(nsAString& name) override; + const char* Name() const override { return "Client"; } + + void SetRoot(Layer* aLayer) override; + + void Mutated(Layer* aLayer) override; + void MutatedSimple(Layer* aLayer) override; + + already_AddRefed<PaintedLayer> CreatePaintedLayer() override; + already_AddRefed<PaintedLayer> CreatePaintedLayerWithHint( + PaintedLayerCreationHint aHint) override; + already_AddRefed<ContainerLayer> CreateContainerLayer() override; + already_AddRefed<ImageLayer> CreateImageLayer() override; + already_AddRefed<CanvasLayer> CreateCanvasLayer() override; + already_AddRefed<ReadbackLayer> CreateReadbackLayer() override; + already_AddRefed<ColorLayer> CreateColorLayer() override; + already_AddRefed<RefLayer> CreateRefLayer() override; + + void UpdateTextureFactoryIdentifier( + const TextureFactoryIdentifier& aNewIdentifier) override; + TextureFactoryIdentifier GetTextureFactoryIdentifier() override { + return AsShadowForwarder()->GetTextureFactoryIdentifier(); + } + + void FlushRendering() override; + void WaitOnTransactionProcessed() override; + void SendInvalidRegion(const nsIntRegion& aRegion) override; + + uint32_t StartFrameTimeRecording(int32_t aBufferSize) override; + + void StopFrameTimeRecording(uint32_t aStartIndex, + nsTArray<float>& aFrameIntervals) override; + + bool NeedsWidgetInvalidation() override { return false; } + + ShadowableLayer* Hold(Layer* aLayer); + + bool HasShadowManager() const { return mForwarder->HasShadowManager(); } + + bool IsCompositingCheap() override; + bool HasShadowManagerInternal() const override { return HasShadowManager(); } + + void SetIsFirstPaint() override; + bool GetIsFirstPaint() const override { + return mForwarder->GetIsFirstPaint(); + } + + void SetFocusTarget(const FocusTarget& aFocusTarget) override; + + /** + * Pass through call to the forwarder for nsPresContext's + * CollectPluginGeometryUpdates. Passes widget configuration information + * to the compositor for transmission to the chrome process. This + * configuration gets set when the window paints. + */ + void StorePluginWidgetConfigurations( + const nsTArray<nsIWidget::Configuration>& aConfigurations) override; + + // Drop cached resources and ask our shadow manager to do the same, + // if we have one. + void ClearCachedResources(Layer* aSubtree = nullptr) override; + + void OnMemoryPressure(MemoryPressureReason aWhy) override; + + void SetRepeatTransaction() { mRepeatTransaction = true; } + bool GetRepeatTransaction() { return mRepeatTransaction; } + + bool IsRepeatTransaction() { return mIsRepeatTransaction; } + + void SetTransactionIncomplete() { mTransactionIncomplete = true; } + void SetQueuedAsyncPaints() { mQueuedAsyncPaints = true; } + + bool HasShadowTarget() { return !!mShadowTarget; } + + void SetShadowTarget(gfxContext* aTarget) { mShadowTarget = aTarget; } + + bool CompositorMightResample() { return mCompositorMightResample; } + + DrawPaintedLayerCallback GetPaintedLayerCallback() const { + return mPaintedLayerCallback; + } + + void* GetPaintedLayerCallbackData() const { + return mPaintedLayerCallbackData; + } + + CompositorBridgeChild* GetRemoteRenderer(); + + CompositorBridgeChild* GetCompositorBridgeChild() override; + + bool InConstruction() { return mPhase == PHASE_CONSTRUCTION; } +#ifdef DEBUG + bool InDrawing() { return mPhase == PHASE_DRAWING; } + bool InForward() { return mPhase == PHASE_FORWARD; } +#endif + bool InTransaction() { return mPhase != PHASE_NONE; } + + void SetNeedsComposite(bool aNeedsComposite) override { + mNeedsComposite = aNeedsComposite; + } + bool NeedsComposite() const override { return mNeedsComposite; } + + void ScheduleComposite() override; + void GetFrameUniformity(FrameUniformityData* aFrameUniformityData) override; + + void DidComposite(TransactionId aTransactionId, + const mozilla::TimeStamp& aCompositeStart, + const mozilla::TimeStamp& aCompositeEnd) override; + + bool AreComponentAlphaLayersEnabled() override; + + // Log APZ test data for the current paint. We supply the paint sequence + // number ourselves, and take care of calling APZTestData::StartNewPaint() + // when a new paint is started. + void LogTestDataForCurrentPaint(ScrollableLayerGuid::ViewID aScrollId, + const std::string& aKey, + const std::string& aValue) { + MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me"); + mApzTestData.LogTestDataForPaint(mPaintSequenceNumber, aScrollId, aKey, + aValue); + } + + // Log APZ test data for a repaint request. The sequence number must be + // passed in from outside, and APZTestData::StartNewRepaintRequest() needs + // to be called from the outside as well when a new repaint request is + // started. + void StartNewRepaintRequest(SequenceNumber aSequenceNumber); + + // TODO(botond): When we start using this and write a wrapper similar to + // nsLayoutUtils::LogTestDataForPaint(), make sure that wrapper checks + // StaticPrefs::apz_test_logging_enabled(). + void LogTestDataForRepaintRequest(SequenceNumber aSequenceNumber, + ScrollableLayerGuid::ViewID aScrollId, + const std::string& aKey, + const std::string& aValue) { + MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me"); + mApzTestData.LogTestDataForRepaintRequest(aSequenceNumber, aScrollId, aKey, + aValue); + } + void LogAdditionalTestData(const std::string& aKey, + const std::string& aValue) { + MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me"); + mApzTestData.RecordAdditionalData(aKey, aValue); + } + + // Get the content-side APZ test data for reading. For writing, use the + // LogTestData...() functions. + const APZTestData& GetAPZTestData() const { return mApzTestData; } + + // Get a copy of the compositor-side APZ test data for our layers ID. + void GetCompositorSideAPZTestData(APZTestData* aData) const; + + void SetTransactionIdAllocator(TransactionIdAllocator* aAllocator) override; + + TransactionId GetLastTransactionId() override { return mLatestTransactionId; } + + float RequestProperty(const nsAString& aProperty) override; + + bool AsyncPanZoomEnabled() const override; + + void SetLayersObserverEpoch(LayersObserverEpoch aEpoch) override; + + void AddDidCompositeObserver(DidCompositeObserver* aObserver) override; + void RemoveDidCompositeObserver(DidCompositeObserver* aObserver) override; + + already_AddRefed<PersistentBufferProvider> CreatePersistentBufferProvider( + const gfx::IntSize& aSize, gfx::SurfaceFormat aFormat) override; + + static PaintTiming* MaybeGetPaintTiming(LayerManager* aManager) { + if (!aManager) { + return nullptr; + } + if (ClientLayerManager* lm = aManager->AsClientLayerManager()) { + return &lm->AsShadowForwarder()->GetPaintTiming(); + } + return nullptr; + } + + protected: + enum TransactionPhase { + PHASE_NONE, + PHASE_CONSTRUCTION, + PHASE_DRAWING, + PHASE_FORWARD + }; + TransactionPhase mPhase; + + private: + /** + * Forward transaction results to the parent context. + */ + void ForwardTransaction(bool aScheduleComposite); + + /** + * Take a snapshot of the parent context, and copy + * it into mShadowTarget. + */ + void MakeSnapshotIfRequired(); + + void ClearLayer(Layer* aLayer); + + void HandleMemoryPressureLayer(Layer* aLayer); + + bool EndTransactionInternal(DrawPaintedLayerCallback aCallback, + void* aCallbackData, EndTransactionFlags); + + void FlushAsyncPaints(); + + LayerRefArray mKeepAlive; + + nsIWidget* mWidget; + + /* PaintedLayer callbacks; valid at the end of a transaciton, + * while rendering */ + DrawPaintedLayerCallback mPaintedLayerCallback; + void* mPaintedLayerCallbackData; + + // When we're doing a transaction in order to draw to a non-default + // target, the layers transaction is only performed in order to send + // a PLayers:Update. We save the original non-default target to + // mShadowTarget, and then perform the transaction using + // mDummyTarget as the render target. After the transaction ends, + // we send a message to our remote side to capture the actual pixels + // being drawn to the default target, and then copy those pixels + // back to mShadowTarget. + RefPtr<gfxContext> mShadowTarget; + + RefPtr<TransactionIdAllocator> mTransactionIdAllocator; + TransactionId mLatestTransactionId; + TimeDuration mLastPaintTime; + + // Sometimes we draw to targets that don't natively support + // landscape/portrait orientation. When we need to implement that + // ourselves, |mTargetRotation| describes the induced transform we + // need to apply when compositing content to our target. + ScreenRotation mTargetRotation; + + // Used to repeat the transaction right away (to avoid rebuilding + // a display list) to support progressive drawing. + bool mRepeatTransaction; + bool mIsRepeatTransaction; + bool mTransactionIncomplete; + bool mCompositorMightResample; + bool mNeedsComposite; + bool mQueuedAsyncPaints; + bool mNotifyingWidgetListener; + + // An incrementing sequence number for paints. + // Incremented in BeginTransaction(), but not for repeat transactions. + uint32_t mPaintSequenceNumber; + + APZTestData mApzTestData; + + RefPtr<ShadowLayerForwarder> mForwarder; + mozilla::TimeStamp mTransactionStart; + nsCString mURL; + + nsTArray<DidCompositeObserver*> mDidCompositeObservers; + + RefPtr<MemoryPressureObserver> mMemoryPressureObserver; +}; + +class ClientLayer : public ShadowableLayer { + public: + MOZ_COUNTED_DEFAULT_CTOR(ClientLayer) + + ~ClientLayer(); + + // Shrink memory usage. + // Called when "memory-pressure" is observed. + virtual void HandleMemoryPressure() {} + + virtual void RenderLayer() = 0; + virtual void RenderLayerWithReadback(ReadbackProcessor* aReadback) { + RenderLayer(); + } + + virtual ClientPaintedLayer* AsThebes() { return nullptr; } + + static ClientLayer* ToClientLayer(Layer* aLayer); + + template <typename LayerType> + static inline void RenderMaskLayers(LayerType* aLayer) { + if (aLayer->GetMaskLayer()) { + ToClientLayer(aLayer->GetMaskLayer())->RenderLayer(); + } + for (size_t i = 0; i < aLayer->GetAncestorMaskLayerCount(); i++) { + ToClientLayer(aLayer->GetAncestorMaskLayerAt(i))->RenderLayer(); + } + } +}; + +// Create a LayerHandle for aLayer, if we're forwarding our layer tree +// to a parent process. Record the new layer creation in the current +// open transaction as a side effect. +template <typename CreatedMethod> +void CreateShadowFor(ClientLayer* aLayer, ClientLayerManager* aMgr, + CreatedMethod aMethod) { + LayerHandle shadow = aMgr->AsShadowForwarder()->ConstructShadowFor(aLayer); + if (!shadow) { + return; + } + + aLayer->SetShadow(aMgr->AsShadowForwarder(), shadow); + (aMgr->AsShadowForwarder()->*aMethod)(aLayer); + aMgr->Hold(aLayer->AsLayer()); +} + +#define CREATE_SHADOW(_type) \ + CreateShadowFor(layer, this, &ShadowLayerForwarder::Created##_type##Layer) + +} // namespace layers +} // namespace mozilla + +#endif /* GFX_CLIENTLAYERMANAGER_H */ diff --git a/gfx/layers/client/ClientPaintedLayer.cpp b/gfx/layers/client/ClientPaintedLayer.cpp new file mode 100644 index 0000000000..1a3f101cc3 --- /dev/null +++ b/gfx/layers/client/ClientPaintedLayer.cpp @@ -0,0 +1,210 @@ +/* -*- 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 + * 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 "ClientPaintedLayer.h" +#include "ClientTiledPaintedLayer.h" // for ClientTiledPaintedLayer +#include <stdint.h> // for uint32_t +#include "GeckoProfiler.h" // for AUTO_PROFILER_LABEL +#include "client/ClientLayerManager.h" // for ClientLayerManager, etc +#include "gfxContext.h" // for gfxContext +#include "gfx2DGlue.h" +#include "gfxEnv.h" // for gfxEnv +#include "gfxRect.h" // for gfxRect + +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/gfx/2D.h" // for DrawTarget +#include "mozilla/gfx/DrawEventRecorder.h" +#include "mozilla/gfx/Matrix.h" // for Matrix +#include "mozilla/gfx/Rect.h" // for Rect, IntRect +#include "mozilla/gfx/Types.h" // for Float, etc +#include "mozilla/layers/LayersTypes.h" +#include "mozilla/Preferences.h" +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsISupportsImpl.h" // for Layer::AddRef, etc +#include "nsRect.h" // for mozilla::gfx::IntRect +#include "PaintThread.h" +#include "ReadbackProcessor.h" +#include "RotatedBuffer.h" + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +bool ClientPaintedLayer::EnsureContentClient() { + if (!mContentClient) { + mContentClient = ContentClient::CreateContentClient( + ClientManager()->AsShadowForwarder()); + + if (!mContentClient) { + return false; + } + + mContentClient->Connect(); + ClientManager()->AsShadowForwarder()->Attach(mContentClient, this); + MOZ_ASSERT(mContentClient->GetForwarder()); + } + + return true; +} + +void ClientPaintedLayer::UpdateContentClient(PaintState& aState) { + Mutated(); + + AddToValidRegion(aState.mRegionToDraw); + + ContentClientRemoteBuffer* contentClientRemote = + static_cast<ContentClientRemoteBuffer*>(mContentClient.get()); + MOZ_ASSERT(contentClientRemote->GetIPCHandle()); + + // Hold(this) ensures this layer is kept alive through the current transaction + // The ContentClient assumes this layer is kept alive (e.g., in CreateBuffer), + // so deleting this Hold for whatever reason will break things. + ClientManager()->Hold(this); + contentClientRemote->Updated(aState.mRegionToDraw, + mVisibleRegion.ToUnknownRegion()); +} + +bool ClientPaintedLayer::UpdatePaintRegion(PaintState& aState) { + SubtractFromValidRegion(aState.mRegionToInvalidate); + + if (!aState.mRegionToDraw.IsEmpty() && + !ClientManager()->GetPaintedLayerCallback()) { + ClientManager()->SetTransactionIncomplete(); + return false; + } + + // The area that became invalid and is visible needs to be repainted + // (this could be the whole visible area if our buffer switched + // from RGB to RGBA, because we might need to repaint with + // subpixel AA) + aState.mRegionToInvalidate.And(aState.mRegionToInvalidate, + GetLocalVisibleRegion().ToUnknownRegion()); + return true; +} + +void ClientPaintedLayer::FinishPaintState(PaintState& aState) { + if (aState.mAsyncTask && !aState.mAsyncTask->mCapture->IsEmpty()) { + ClientManager()->SetQueuedAsyncPaints(); + PaintThread::Get()->QueuePaintTask(std::move(aState.mAsyncTask)); + } +} + +uint32_t ClientPaintedLayer::GetPaintFlags(ReadbackProcessor* aReadback) { + uint32_t flags = ContentClient::PAINT_CAN_DRAW_ROTATED; +#ifndef MOZ_IGNORE_PAINT_WILL_RESAMPLE + if (ClientManager()->CompositorMightResample()) { + flags |= ContentClient::PAINT_WILL_RESAMPLE; + } + if (!(flags & ContentClient::PAINT_WILL_RESAMPLE)) { + if (MayResample()) { + flags |= ContentClient::PAINT_WILL_RESAMPLE; + } + } +#endif + if ((!aReadback || !UsedForReadback()) && PaintThread::Get()) { + flags |= ContentClient::PAINT_ASYNC; + } + return flags; +} + +void ClientPaintedLayer::RenderLayerWithReadback(ReadbackProcessor* aReadback) { + AUTO_PROFILER_LABEL("ClientPaintedLayer::RenderLayerWithReadback", GRAPHICS); + NS_ASSERTION(ClientManager()->InDrawing(), "Can only draw in drawing phase"); + + RenderMaskLayers(this); + + if (!EnsureContentClient()) { + return; + } + + nsTArray<ReadbackProcessor::Update> readbackUpdates; + nsIntRegion readbackRegion; + if (aReadback && UsedForReadback()) { + aReadback->GetPaintedLayerUpdates(this, &readbackUpdates); + } + + uint32_t flags = GetPaintFlags(aReadback); + + PaintState state = mContentClient->BeginPaint(this, flags); + if (!UpdatePaintRegion(state)) { + mContentClient->EndPaint(state, nullptr); + FinishPaintState(state); + return; + } + + bool didUpdate = false; + RotatedBuffer::DrawIterator iter; + while (DrawTarget* target = + mContentClient->BorrowDrawTargetForPainting(state, &iter)) { + SetAntialiasingFlags(this, target); + + RefPtr<gfxContext> ctx = + gfxContext::CreatePreservingTransformOrNull(target); + MOZ_ASSERT(ctx); // already checked the target above + + if (!gfxEnv::SkipRasterization()) { + if (!target->IsCaptureDT()) { + target->ClearRect(Rect()); + if (target->IsValid()) { + ClientManager()->GetPaintedLayerCallback()( + this, ctx, iter.mDrawRegion, iter.mDrawRegion, state.mClip, + state.mRegionToInvalidate, + ClientManager()->GetPaintedLayerCallbackData()); + } + } else { + ClientManager()->GetPaintedLayerCallback()( + this, ctx, iter.mDrawRegion, iter.mDrawRegion, state.mClip, + state.mRegionToInvalidate, + ClientManager()->GetPaintedLayerCallbackData()); + } + } + + ctx = nullptr; + mContentClient->ReturnDrawTarget(target); + didUpdate = true; + } + + mContentClient->EndPaint(state, &readbackUpdates); + FinishPaintState(state); + + if (didUpdate) { + UpdateContentClient(state); + } +} + +already_AddRefed<PaintedLayer> ClientLayerManager::CreatePaintedLayer() { + return CreatePaintedLayerWithHint(NONE); +} + +already_AddRefed<PaintedLayer> ClientLayerManager::CreatePaintedLayerWithHint( + PaintedLayerCreationHint aHint) { + NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); + if (gfxPlatform::GetPlatform()->UsesTiling()) { + RefPtr<ClientTiledPaintedLayer> layer = + new ClientTiledPaintedLayer(this, aHint); + CREATE_SHADOW(Painted); + return layer.forget(); + } else { + RefPtr<ClientPaintedLayer> layer = new ClientPaintedLayer(this, aHint); + CREATE_SHADOW(Painted); + return layer.forget(); + } +} + +void ClientPaintedLayer::PrintInfo(std::stringstream& aStream, + const char* aPrefix) { + PaintedLayer::PrintInfo(aStream, aPrefix); + if (mContentClient) { + aStream << "\n"; + nsAutoCString pfx(aPrefix); + pfx += " "; + mContentClient->PrintInfo(aStream, pfx.get()); + } +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientPaintedLayer.h b/gfx/layers/client/ClientPaintedLayer.h new file mode 100644 index 0000000000..96398262af --- /dev/null +++ b/gfx/layers/client/ClientPaintedLayer.h @@ -0,0 +1,123 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTPAINTEDLAYER_H +#define GFX_CLIENTPAINTEDLAYER_H + +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "Layers.h" // for PaintedLayer, etc +#include "RotatedBuffer.h" // for RotatedBuffer, etc +#include "mozilla/Attributes.h" // for override +#include "mozilla/RefPtr.h" // for RefPtr +#include "mozilla/layers/ContentClient.h" // for ContentClient +#include "mozilla/mozalloc.h" // for operator delete +#include "nsDebug.h" // for NS_ASSERTION +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc +#include "nsRegion.h" // for nsIntRegion +#include "mozilla/layers/PLayerTransaction.h" // for PaintedLayerAttributes + +namespace mozilla { +namespace gfx { +class DrawEventRecorderMemory; +class DrawTargetCapture; +}; // namespace gfx + +namespace layers { +class CompositableClient; +class ShadowableLayer; +class SpecificLayerAttributes; + +class ClientPaintedLayer : public PaintedLayer, public ClientLayer { + public: + typedef ContentClient::PaintState PaintState; + typedef ContentClient::ContentType ContentType; + + explicit ClientPaintedLayer( + ClientLayerManager* aLayerManager, + LayerManager::PaintedLayerCreationHint aCreationHint = LayerManager::NONE) + : PaintedLayer(aLayerManager, static_cast<ClientLayer*>(this), + aCreationHint), + mContentClient(nullptr) { + MOZ_COUNT_CTOR(ClientPaintedLayer); + } + + protected: + virtual ~ClientPaintedLayer() { + if (mContentClient) { + mContentClient->OnDetach(); + mContentClient = nullptr; + } + MOZ_COUNT_DTOR(ClientPaintedLayer); + } + + public: + void SetVisibleRegion(const LayerIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + PaintedLayer::SetVisibleRegion(aRegion); + } + void InvalidateRegion(const nsIntRegion& aRegion) override { + NS_ASSERTION(ClientManager()->InConstruction(), + "Can only set properties in construction phase"); + mInvalidRegion.Add(aRegion); + UpdateValidRegionAfterInvalidRegionChanged(); + } + + void RenderLayer() override { RenderLayerWithReadback(nullptr); } + + void RenderLayerWithReadback(ReadbackProcessor* aReadback) override; + + void ClearCachedResources() override { + if (mContentClient) { + mContentClient->Clear(); + } + ClearValidRegion(); + DestroyBackBuffer(); + } + + void HandleMemoryPressure() override { + if (mContentClient) { + mContentClient->HandleMemoryPressure(); + } + } + + void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override { + aAttrs = PaintedLayerAttributes(GetValidRegion()); + } + + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } + + Layer* AsLayer() override { return this; } + ShadowableLayer* AsShadowableLayer() override { return this; } + + CompositableClient* GetCompositableClient() override { + return mContentClient; + } + + void Disconnect() override { mContentClient = nullptr; } + + protected: + void RecordThebes(); + bool HasMaskLayers(); + bool EnsureContentClient(); + uint32_t GetPaintFlags(ReadbackProcessor* aReadback); + void UpdateContentClient(PaintState& aState); + bool UpdatePaintRegion(PaintState& aState); + void FinishPaintState(PaintState& aState); + + void PrintInfo(std::stringstream& aStream, const char* aPrefix) override; + + void DestroyBackBuffer() { mContentClient = nullptr; } + + RefPtr<ContentClient> mContentClient; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ClientReadbackLayer.h b/gfx/layers/client/ClientReadbackLayer.h new file mode 100644 index 0000000000..1e6535bb16 --- /dev/null +++ b/gfx/layers/client/ClientReadbackLayer.h @@ -0,0 +1,31 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTREADBACKLAYER_H +#define GFX_CLIENTREADBACKLAYER_H + +#include "ClientLayerManager.h" +#include "ReadbackLayer.h" + +namespace mozilla { +namespace layers { + +class ClientReadbackLayer : public ReadbackLayer, public ClientLayer { + public: + explicit ClientReadbackLayer(ClientLayerManager* aManager) + : ReadbackLayer(aManager, nullptr) { + mImplData = static_cast<ClientLayer*>(this); + } + + ShadowableLayer* AsShadowableLayer() override { return this; } + Layer* AsLayer() override { return this; } + void RenderLayer() override {} +}; + +} // namespace layers +} // namespace mozilla + +#endif /* GFX_CLIENTREADBACKLAYER_H */ diff --git a/gfx/layers/client/ClientTiledPaintedLayer.cpp b/gfx/layers/client/ClientTiledPaintedLayer.cpp new file mode 100644 index 0000000000..1e93c51f72 --- /dev/null +++ b/gfx/layers/client/ClientTiledPaintedLayer.cpp @@ -0,0 +1,653 @@ +/* -*- 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 + * 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 "ClientTiledPaintedLayer.h" +#include "FrameMetrics.h" // for FrameMetrics +#include "Units.h" // for ScreenIntRect, CSSPoint, etc +#include "UnitTransforms.h" // for TransformTo +#include "ClientLayerManager.h" // for ClientLayerManager, etc +#include "gfxPlatform.h" // for gfxPlatform +#include "gfxRect.h" // for gfxRect +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/StaticPrefs_layers.h" +#include "mozilla/StaticPrefs_layout.h" +#include "mozilla/gfx/BaseSize.h" // for BaseSize +#include "mozilla/gfx/gfxVars.h" +#include "mozilla/gfx/Rect.h" // for Rect, RectTyped +#include "mozilla/layers/CompositorBridgeChild.h" +#include "mozilla/layers/LayerMetricsWrapper.h" // for LayerMetricsWrapper +#include "mozilla/layers/LayersMessages.h" +#include "mozilla/layers/PaintThread.h" +#include "mozilla/mozalloc.h" // for operator delete, etc +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc +#include "mozilla/layers/MultiTiledContentClient.h" +#include "mozilla/layers/SingleTiledContentClient.h" + +namespace mozilla { +namespace layers { + +using gfx::IntRect; +using gfx::IntSize; +using gfx::Rect; + +ClientTiledPaintedLayer::ClientTiledPaintedLayer( + ClientLayerManager* const aManager, + ClientLayerManager::PaintedLayerCreationHint aCreationHint) + : PaintedLayer(aManager, static_cast<ClientLayer*>(this), aCreationHint), + mContentClient(), + mHaveSingleTiledContentClient(false) { + MOZ_COUNT_CTOR(ClientTiledPaintedLayer); + mPaintData.mLastScrollOffset = ParentLayerPoint(0, 0); + mPaintData.mFirstPaint = true; +} + +ClientTiledPaintedLayer::~ClientTiledPaintedLayer() { + MOZ_COUNT_DTOR(ClientTiledPaintedLayer); +} + +void ClientTiledPaintedLayer::ClearCachedResources() { + if (mContentClient) { + mContentClient->ClearCachedResources(); + } + ClearValidRegion(); + mContentClient = nullptr; +} + +void ClientTiledPaintedLayer::FillSpecificAttributes( + SpecificLayerAttributes& aAttrs) { + aAttrs = PaintedLayerAttributes(GetValidRegion()); +} + +static Maybe<LayerRect> ApplyParentLayerToLayerTransform( + const ParentLayerToLayerMatrix4x4& aTransform, + const ParentLayerRect& aParentLayerRect, const LayerRect& aClip) { + return UntransformBy(aTransform, aParentLayerRect, aClip); +} + +static LayerToParentLayerMatrix4x4 GetTransformToAncestorsParentLayer( + Layer* aStart, const LayerMetricsWrapper& aAncestor) { + // If the ancestor layer Combines3DTransformWithAncestors, then the + // scroll offset is contained in the transform of the layer at the + // root of the 3D context. So we must first find that layer, then + // calcuate the transform to its parent. + LayerMetricsWrapper root3dAncestor = aAncestor; + while (root3dAncestor.Combines3DTransformWithAncestors()) { + root3dAncestor = root3dAncestor.GetParent(); + } + + gfx::Matrix4x4 transform; + const LayerMetricsWrapper& ancestorParent = root3dAncestor.GetParent(); + for (LayerMetricsWrapper iter(aStart, LayerMetricsWrapper::StartAt::BOTTOM); + ancestorParent ? iter != ancestorParent : iter.IsValid(); + iter = iter.GetParent()) { + transform = transform * iter.GetTransform(); + } + return ViewAs<LayerToParentLayerMatrix4x4>(transform); +} + +void ClientTiledPaintedLayer::GetAncestorLayers( + LayerMetricsWrapper* aOutScrollAncestor, + LayerMetricsWrapper* aOutDisplayPortAncestor, + bool* aOutHasTransformAnimation) { + LayerMetricsWrapper scrollAncestor; + LayerMetricsWrapper displayPortAncestor; + bool hasTransformAnimation = false; + for (LayerMetricsWrapper ancestor(this, LayerMetricsWrapper::StartAt::BOTTOM); + ancestor; ancestor = ancestor.GetParent()) { + hasTransformAnimation |= ancestor.HasTransformAnimation(); + const FrameMetrics& metrics = ancestor.Metrics(); + if (!scrollAncestor && + metrics.GetScrollId() != ScrollableLayerGuid::NULL_SCROLL_ID) { + scrollAncestor = ancestor; + } + if (!metrics.GetDisplayPort().IsEmpty()) { + displayPortAncestor = ancestor; + // Any layer that has a displayport must be scrollable, so we can break + // here. + break; + } + } + if (aOutScrollAncestor) { + *aOutScrollAncestor = scrollAncestor; + } + if (aOutDisplayPortAncestor) { + *aOutDisplayPortAncestor = displayPortAncestor; + } + if (aOutHasTransformAnimation) { + *aOutHasTransformAnimation = hasTransformAnimation; + } +} + +void ClientTiledPaintedLayer::BeginPaint() { + mPaintData.ResetPaintData(); + + if (!GetBaseTransform().Is2D()) { + // Give up if there is a complex CSS transform on the layer. We might + // eventually support these but for now it's too complicated to handle + // given that it's a pretty rare scenario. + return; + } + + // Get the metrics of the nearest scrollable layer and the nearest layer + // with a displayport. + LayerMetricsWrapper scrollAncestor; + LayerMetricsWrapper displayPortAncestor; + bool hasTransformAnimation; + GetAncestorLayers(&scrollAncestor, &displayPortAncestor, + &hasTransformAnimation); + + if (!displayPortAncestor || !scrollAncestor) { + // No displayport or scroll ancestor, so we can't do progressive rendering. +#if defined(MOZ_WIDGET_ANDROID) + // Android are guaranteed to have a displayport set, so this + // should never happen. + NS_WARNING("Tiled PaintedLayer with no scrollable container ancestor"); +#endif + return; + } + + TILING_LOG( + "TILING %p: Found scrollAncestor %p, displayPortAncestor %p, transform " + "%d\n", + this, scrollAncestor.GetLayer(), displayPortAncestor.GetLayer(), + hasTransformAnimation); + + const FrameMetrics& scrollMetrics = scrollAncestor.Metrics(); + const FrameMetrics& displayportMetrics = displayPortAncestor.Metrics(); + + // Calculate the transform required to convert ParentLayer space of our + // display port ancestor to the Layer space of this layer. + ParentLayerToLayerMatrix4x4 transformDisplayPortToLayer = + GetTransformToAncestorsParentLayer(this, displayPortAncestor).Inverse(); + + LayerRect layerBounds(GetVisibleRegion().GetBounds()); + + // Compute the critical display port that applies to this layer in the + // LayoutDevice space of this layer, but only if there is no OMT animation + // on this layer. If there is an OMT animation then we need to draw the whole + // visible region of this layer as determined by layout, because we don't know + // what parts of it might move into view in the compositor. + mPaintData.mHasTransformAnimation = hasTransformAnimation; + if (!mPaintData.mHasTransformAnimation && + mContentClient->GetLowPrecisionTiledBuffer()) { + ParentLayerRect criticalDisplayPort = + (displayportMetrics.GetCriticalDisplayPort() * + displayportMetrics.GetZoom()) + + displayportMetrics.GetCompositionBounds().TopLeft(); + Maybe<LayerRect> criticalDisplayPortTransformed = + ApplyParentLayerToLayerTransform(transformDisplayPortToLayer, + criticalDisplayPort, layerBounds); + if (criticalDisplayPortTransformed) { + mPaintData.mCriticalDisplayPort = + Some(RoundedToInt(*criticalDisplayPortTransformed)); + } else { + mPaintData.mCriticalDisplayPort = Some(LayerIntRect(0, 0, 0, 0)); + } + } + TILING_LOG("TILING %p: Critical displayport %s\n", this, + mPaintData.mCriticalDisplayPort + ? Stringify(*mPaintData.mCriticalDisplayPort).c_str() + : "not set"); + + // Store the resolution from the displayport ancestor layer. Because this is + // Gecko-side, before any async transforms have occurred, we can use the zoom + // for this. + mPaintData.mResolution = displayportMetrics.GetZoom(); + TILING_LOG("TILING %p: Resolution %s\n", this, + Stringify(mPaintData.mResolution).c_str()); + + // Store the applicable composition bounds in this layer's Layer units. + mPaintData.mTransformToCompBounds = + GetTransformToAncestorsParentLayer(this, scrollAncestor); + ParentLayerToLayerMatrix4x4 transformToBounds = + mPaintData.mTransformToCompBounds.Inverse(); + Maybe<LayerRect> compositionBoundsTransformed = + ApplyParentLayerToLayerTransform( + transformToBounds, scrollMetrics.GetCompositionBounds(), layerBounds); + if (compositionBoundsTransformed) { + mPaintData.mCompositionBounds = *compositionBoundsTransformed; + } else { + mPaintData.mCompositionBounds.SetEmpty(); + } + TILING_LOG("TILING %p: Composition bounds %s\n", this, + Stringify(mPaintData.mCompositionBounds).c_str()); + + // Calculate the scroll offset since the last transaction + mPaintData.mScrollOffset = + displayportMetrics.GetLayoutScrollOffset() * displayportMetrics.GetZoom(); + TILING_LOG("TILING %p: Scroll offset %s\n", this, + Stringify(mPaintData.mScrollOffset).c_str()); +} + +bool ClientTiledPaintedLayer::IsScrollingOnCompositor( + const FrameMetrics& aParentMetrics) { + CompositorBridgeChild* compositor = nullptr; + if (Manager() && Manager()->AsClientLayerManager()) { + compositor = Manager()->AsClientLayerManager()->GetCompositorBridgeChild(); + } + + if (!compositor) { + return false; + } + + FrameMetrics compositorMetrics; + if (!compositor->LookupCompositorFrameMetrics(aParentMetrics.GetScrollId(), + compositorMetrics)) { + return false; + } + + // 1 is a tad high for a fuzzy equals epsilon however if our scroll delta + // is so small then we have nothing to gain from using paint heuristics. + float COORDINATE_EPSILON = 1.f; + + return !FuzzyEqualsAdditive(compositorMetrics.GetVisualScrollOffset().x, + aParentMetrics.GetVisualScrollOffset().x, + COORDINATE_EPSILON) || + !FuzzyEqualsAdditive(compositorMetrics.GetVisualScrollOffset().y, + aParentMetrics.GetVisualScrollOffset().y, + COORDINATE_EPSILON); +} + +bool ClientTiledPaintedLayer::UseProgressiveDraw() { + if (!StaticPrefs::layers_progressive_paint()) { + // pref is disabled, so never do progressive + return false; + } + + if (!mContentClient->GetTiledBuffer()->SupportsProgressiveUpdate()) { + return false; + } + + if (ClientManager()->HasShadowTarget()) { + // This condition is true when we are in a reftest scenario. We don't want + // to draw progressively here because it can cause intermittent reftest + // failures because the harness won't wait for all the tiles to be drawn. + return false; + } + + if (GetIsFixedPosition() || GetParent()->GetIsFixedPosition()) { + // This layer is fixed-position and so even if it does have a scrolling + // ancestor it will likely be entirely on-screen all the time, so we + // should draw it all at once + return false; + } + + if (mPaintData.mHasTransformAnimation) { + // The compositor is going to animate this somehow, so we want it all + // on the screen at once. + return false; + } + + if (ClientManager()->AsyncPanZoomEnabled()) { + LayerMetricsWrapper scrollAncestor; + GetAncestorLayers(&scrollAncestor, nullptr, nullptr); + MOZ_ASSERT( + scrollAncestor); // because mPaintData.mCriticalDisplayPort is set + if (!scrollAncestor) { + return false; + } + const FrameMetrics& parentMetrics = scrollAncestor.Metrics(); + if (!IsScrollingOnCompositor(parentMetrics)) { + return false; + } + } + + return true; +} + +bool ClientTiledPaintedLayer::RenderHighPrecision( + const nsIntRegion& aInvalidRegion, const nsIntRegion& aVisibleRegion, + LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData) { + // If we have started drawing low-precision already, then we + // shouldn't do anything there. + if (mPaintData.mLowPrecisionPaintCount != 0) { + return false; + } + + // Only draw progressively when there is something to paint and the + // resolution is unchanged + if (!aInvalidRegion.IsEmpty() && UseProgressiveDraw() && + mContentClient->GetTiledBuffer()->GetFrameResolution() == + mPaintData.mResolution) { + // Store the old valid region, then clear it before painting. + // We clip the old valid region to the visible region, as it only gets + // used to decide stale content (currently valid and previously visible) + nsIntRegion oldValidRegion = + mContentClient->GetTiledBuffer()->GetValidRegion(); + oldValidRegion.And(oldValidRegion, aVisibleRegion); + if (mPaintData.mCriticalDisplayPort) { + oldValidRegion.And(oldValidRegion, + mPaintData.mCriticalDisplayPort->ToUnknownRect()); + } + + TILING_LOG("TILING %p: Progressive update with old valid region %s\n", this, + Stringify(oldValidRegion).c_str()); + + nsIntRegion drawnRegion; + bool updatedBuffer = mContentClient->GetTiledBuffer()->ProgressiveUpdate( + GetValidRegion(), aInvalidRegion, oldValidRegion, drawnRegion, + &mPaintData, aCallback, aCallbackData); + AddToValidRegion(drawnRegion); + return updatedBuffer; + } + + // Otherwise do a non-progressive paint. We must do this even when + // the region to paint is empty as the valid region may have shrunk. + + nsIntRegion validRegion = aVisibleRegion; + if (mPaintData.mCriticalDisplayPort) { + validRegion.AndWith(mPaintData.mCriticalDisplayPort->ToUnknownRect()); + } + SetValidRegion(validRegion); + + TILING_LOG("TILING %p: Non-progressive paint invalid region %s\n", this, + Stringify(aInvalidRegion).c_str()); + TILING_LOG("TILING %p: Non-progressive paint new valid region %s\n", this, + Stringify(GetValidRegion()).c_str()); + + TilePaintFlags flags = + PaintThread::Get() ? TilePaintFlags::Async : TilePaintFlags::None; + + mContentClient->GetTiledBuffer()->SetFrameResolution(mPaintData.mResolution); + mContentClient->GetTiledBuffer()->PaintThebes( + GetValidRegion(), aInvalidRegion, aInvalidRegion, aCallback, + aCallbackData, flags); + mPaintData.mPaintFinished = true; + return true; +} + +bool ClientTiledPaintedLayer::RenderLowPrecision( + const nsIntRegion& aInvalidRegion, const nsIntRegion& aVisibleRegion, + LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData) { + nsIntRegion invalidRegion = aInvalidRegion; + + // Render the low precision buffer, if the visible region is larger than the + // critical display port. + if (!mPaintData.mCriticalDisplayPort || + !nsIntRegion(mPaintData.mCriticalDisplayPort->ToUnknownRect()) + .Contains(aVisibleRegion)) { + nsIntRegion oldValidRegion = + mContentClient->GetLowPrecisionTiledBuffer()->GetValidRegion(); + oldValidRegion.And(oldValidRegion, aVisibleRegion); + + bool updatedBuffer = false; + + // If the frame resolution or format have changed, invalidate the buffer + if (mContentClient->GetLowPrecisionTiledBuffer()->GetFrameResolution() != + mPaintData.mResolution || + mContentClient->GetLowPrecisionTiledBuffer()->HasFormatChanged()) { + if (!mLowPrecisionValidRegion.IsEmpty()) { + updatedBuffer = true; + } + oldValidRegion.SetEmpty(); + mLowPrecisionValidRegion.SetEmpty(); + mContentClient->GetLowPrecisionTiledBuffer()->ResetPaintedAndValidState(); + mContentClient->GetLowPrecisionTiledBuffer()->SetFrameResolution( + mPaintData.mResolution); + invalidRegion = aVisibleRegion; + } + + // Invalidate previously valid content that is no longer visible + if (mPaintData.mLowPrecisionPaintCount == 1) { + mLowPrecisionValidRegion.And(mLowPrecisionValidRegion, aVisibleRegion); + } + mPaintData.mLowPrecisionPaintCount++; + + // Remove the valid high-precision region from the invalid low-precision + // region. We don't want to spend time drawing things twice. + invalidRegion.SubOut(GetValidRegion()); + + TILING_LOG( + "TILING %p: Progressive paint: low-precision invalid region is %s\n", + this, Stringify(invalidRegion).c_str()); + TILING_LOG( + "TILING %p: Progressive paint: low-precision old valid region is %s\n", + this, Stringify(oldValidRegion).c_str()); + + if (!invalidRegion.IsEmpty()) { + nsIntRegion drawnRegion; + updatedBuffer = + mContentClient->GetLowPrecisionTiledBuffer()->ProgressiveUpdate( + mLowPrecisionValidRegion, invalidRegion, oldValidRegion, + drawnRegion, &mPaintData, aCallback, aCallbackData); + mLowPrecisionValidRegion.OrWith(drawnRegion); + } + + TILING_LOG( + "TILING %p: Progressive paint: low-precision new valid region is %s\n", + this, Stringify(mLowPrecisionValidRegion).c_str()); + return updatedBuffer; + } + if (!mLowPrecisionValidRegion.IsEmpty()) { + TILING_LOG("TILING %p: Clearing low-precision buffer\n", this); + // Clear the low precision tiled buffer. + mLowPrecisionValidRegion.SetEmpty(); + mContentClient->GetLowPrecisionTiledBuffer()->ResetPaintedAndValidState(); + // Return true here so we send a Painted callback after clearing the valid + // region of the low precision buffer. This allows the shadow buffer's valid + // region to be updated and the associated resources to be freed. + return true; + } + return false; +} + +void ClientTiledPaintedLayer::EndPaint() { + mPaintData.mLastScrollOffset = mPaintData.mScrollOffset; + mPaintData.mPaintFinished = true; + mPaintData.mFirstPaint = false; + TILING_LOG("TILING %p: Paint finished\n", this); +} + +void ClientTiledPaintedLayer::RenderLayer() { + if (!ClientManager()->IsRepeatTransaction()) { + // Only paint the mask layers on the first transaction. + RenderMaskLayers(this); + } + + LayerManager::DrawPaintedLayerCallback callback = + ClientManager()->GetPaintedLayerCallback(); + void* data = ClientManager()->GetPaintedLayerCallbackData(); + + IntSize layerSize = mVisibleRegion.GetBounds().ToUnknownRect().Size(); + IntSize tileSize = gfx::gfxVars::TileSize(); + bool isHalfTileWidthOrHeight = layerSize.width <= tileSize.width / 2 || + layerSize.height <= tileSize.height / 2; + + // Use single tile when layer is not scrollable, is smaller than one + // tile, or when more than half of the tiles' pixels in either + // dimension would be wasted. + bool wantSingleTiledContentClient = + (mCreationHint == LayerManager::NONE || layerSize <= tileSize || + isHalfTileWidthOrHeight) && + SingleTiledContentClient::ClientSupportsLayerSize(layerSize, + ClientManager()) && + StaticPrefs::layers_single_tile_enabled(); + + if (mContentClient && mHaveSingleTiledContentClient && + !wantSingleTiledContentClient) { + mContentClient = nullptr; + ClearValidRegion(); + } + + if (!mContentClient) { + if (wantSingleTiledContentClient) { + mContentClient = new SingleTiledContentClient(*this, ClientManager()); + mHaveSingleTiledContentClient = true; + } else { + mContentClient = new MultiTiledContentClient(*this, ClientManager()); + mHaveSingleTiledContentClient = false; + } + + mContentClient->Connect(); + ClientManager()->AsShadowForwarder()->Attach(mContentClient, this); + MOZ_ASSERT(mContentClient->GetForwarder()); + } + + if (mContentClient->GetTiledBuffer()->HasFormatChanged()) { + ClearValidRegion(); + mContentClient->GetTiledBuffer()->ResetPaintedAndValidState(); + } + + TILING_LOG("TILING %p: Initial visible region %s\n", this, + Stringify(mVisibleRegion).c_str()); + TILING_LOG("TILING %p: Initial valid region %s\n", this, + Stringify(GetValidRegion()).c_str()); + TILING_LOG("TILING %p: Initial low-precision valid region %s\n", this, + Stringify(mLowPrecisionValidRegion).c_str()); + + nsIntRegion neededRegion = mVisibleRegion.ToUnknownRegion(); +#ifndef MOZ_IGNORE_PAINT_WILL_RESAMPLE + // This is handled by PadDrawTargetOutFromRegion in TiledContentClient for + // mobile + if (MayResample()) { + // If we're resampling then bilinear filtering can read up to 1 pixel + // outside of our texture coords. Make the visible region a single rect, + // and pad it out by 1 pixel (restricted to tile boundaries) so that + // we always have valid content or transparent pixels to sample from. + IntRect bounds = neededRegion.GetBounds(); + IntRect wholeTiles = bounds; + wholeTiles.InflateToMultiple(gfx::gfxVars::TileSize()); + IntRect padded = bounds; + padded.Inflate(1); + padded.IntersectRect(padded, wholeTiles); + neededRegion = padded; + } +#endif + + nsIntRegion invalidRegion; + invalidRegion.Sub(neededRegion, GetValidRegion()); + if (invalidRegion.IsEmpty()) { + EndPaint(); + return; + } + + if (!callback) { + ClientManager()->SetTransactionIncomplete(); + return; + } + + if (!ClientManager()->IsRepeatTransaction()) { + // For more complex cases we need to calculate a bunch of metrics before we + // can do the paint. + BeginPaint(); + if (mPaintData.mPaintFinished) { + return; + } + + // Make sure that tiles that fall outside of the visible region or outside + // of the critical displayport are discarded on the first update. Also make + // sure that we only draw stuff inside the critical displayport on the first + // update. + nsIntRegion validRegion; + validRegion.And(GetValidRegion(), neededRegion); + if (mPaintData.mCriticalDisplayPort) { + validRegion.AndWith(mPaintData.mCriticalDisplayPort->ToUnknownRect()); + invalidRegion.And(invalidRegion, + mPaintData.mCriticalDisplayPort->ToUnknownRect()); + } + SetValidRegion(validRegion); + + TILING_LOG("TILING %p: First-transaction valid region %s\n", this, + Stringify(validRegion).c_str()); + TILING_LOG("TILING %p: First-transaction invalid region %s\n", this, + Stringify(invalidRegion).c_str()); + } else { + if (mPaintData.mCriticalDisplayPort) { + invalidRegion.And(invalidRegion, + mPaintData.mCriticalDisplayPort->ToUnknownRect()); + } + TILING_LOG("TILING %p: Repeat-transaction invalid region %s\n", this, + Stringify(invalidRegion).c_str()); + } + + nsIntRegion lowPrecisionInvalidRegion; + if (mContentClient->GetLowPrecisionTiledBuffer()) { + // Calculate the invalid region for the low precision buffer. Make sure + // to remove the valid high-precision area so we don't double-paint it. + lowPrecisionInvalidRegion.Sub(neededRegion, mLowPrecisionValidRegion); + lowPrecisionInvalidRegion.Sub(lowPrecisionInvalidRegion, GetValidRegion()); + } + TILING_LOG("TILING %p: Low-precision invalid region %s\n", this, + Stringify(lowPrecisionInvalidRegion).c_str()); + + bool updatedHighPrecision = + RenderHighPrecision(invalidRegion, neededRegion, callback, data); + if (updatedHighPrecision) { + ClientManager()->Hold(this); + mContentClient->UpdatedBuffer(TiledContentClient::TILED_BUFFER); + + if (!mPaintData.mPaintFinished) { + // There is still more high-res stuff to paint, so we're not + // done yet. A subsequent transaction will take care of this. + ClientManager()->SetRepeatTransaction(); + return; + } + } + + // If there is nothing to draw in low-precision, then we're done. + if (lowPrecisionInvalidRegion.IsEmpty()) { + EndPaint(); + return; + } + + if (updatedHighPrecision) { + // If there are low precision updates, but we just did some high-precision + // updates, then mark the paint as unfinished and request a repeat + // transaction. This is so that we don't perform low-precision updates in + // the same transaction as high-precision updates. + TILING_LOG( + "TILING %p: Scheduling repeat transaction for low-precision painting\n", + this); + ClientManager()->SetRepeatTransaction(); + mPaintData.mLowPrecisionPaintCount = 1; + mPaintData.mPaintFinished = false; + return; + } + + bool updatedLowPrecision = RenderLowPrecision(lowPrecisionInvalidRegion, + neededRegion, callback, data); + if (updatedLowPrecision) { + ClientManager()->Hold(this); + mContentClient->UpdatedBuffer( + TiledContentClient::LOW_PRECISION_TILED_BUFFER); + + if (!mPaintData.mPaintFinished) { + // There is still more low-res stuff to paint, so we're not + // done yet. A subsequent transaction will take care of this. + ClientManager()->SetRepeatTransaction(); + return; + } + } + + // If we get here, we've done all the high- and low-precision + // paints we wanted to do, so we can finish the paint and chill. + EndPaint(); +} + +bool ClientTiledPaintedLayer::IsOptimizedFor( + LayerManager::PaintedLayerCreationHint aHint) { + // The only creation hint is whether the layer is scrollable or not, and this + // is only respected on OSX, where it's used to determine whether to + // use a tiled content client or not. + // There are pretty nasty performance consequences for not using tiles on + // large, scrollable layers, so we want the layer to be recreated in this + // situation. + return aHint == GetCreationHint(); +} + +void ClientTiledPaintedLayer::PrintInfo(std::stringstream& aStream, + const char* aPrefix) { + PaintedLayer::PrintInfo(aStream, aPrefix); + if (mContentClient) { + aStream << "\n"; + nsAutoCString pfx(aPrefix); + pfx += " "; + mContentClient->PrintInfo(aStream, pfx.get()); + } +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ClientTiledPaintedLayer.h b/gfx/layers/client/ClientTiledPaintedLayer.h new file mode 100644 index 0000000000..6043da0b94 --- /dev/null +++ b/gfx/layers/client/ClientTiledPaintedLayer.h @@ -0,0 +1,150 @@ +/* -*- 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 + * 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/. */ + +#ifndef GFX_CLIENTTILEDPAINTEDLAYER_H +#define GFX_CLIENTTILEDPAINTEDLAYER_H + +#include "ClientLayerManager.h" // for ClientLayer, etc +#include "Layers.h" // for PaintedLayer, etc +#include "mozilla/RefPtr.h" // for RefPtr +#include "mozilla/layers/TiledContentClient.h" +#include "nsRegion.h" // for nsIntRegion + +namespace mozilla { +namespace layers { + +class ShadowableLayer; +class SpecificLayerAttributes; + +/** + * An implementation of PaintedLayer that ONLY supports remote + * composition that is backed by tiles. This painted layer implementation + * is better suited to mobile hardware to work around slow implementation + * of glTexImage2D (for OGL compositors), and restrait memory bandwidth. + * + * Tiled PaintedLayers use a different protocol compared with other + * layers. A copy of the tiled buffer is made and sent to the compositing + * thread via the layers protocol. Tiles are uploaded by the buffers + * asynchonously without using IPC, that means they are not safe for cross- + * process use (bug 747811). Each tile has a TextureHost/Client pair but + * they communicate directly rather than using the Texture protocol. + * + * There is no ContentClient for tiled layers. There is a ContentHost, however. + */ +class ClientTiledPaintedLayer : public PaintedLayer, public ClientLayer { + typedef PaintedLayer Base; + + public: + explicit ClientTiledPaintedLayer(ClientLayerManager* const aManager, + ClientLayerManager::PaintedLayerCreationHint + aCreationHint = LayerManager::NONE); + + protected: + virtual ~ClientTiledPaintedLayer(); + + void PrintInfo(std::stringstream& aStream, const char* aPrefix) override; + + public: + // Override name to distinguish it from ClientPaintedLayer in layer dumps + const char* Name() const override { return "TiledPaintedLayer"; } + + // PaintedLayer + Layer* AsLayer() override { return this; } + void InvalidateRegion(const nsIntRegion& aRegion) override { + mInvalidRegion.Add(aRegion); + UpdateValidRegionAfterInvalidRegionChanged(); + if (!mLowPrecisionValidRegion.IsEmpty()) { + // Also update mLowPrecisionValidRegion. Unfortunately we call + // mInvalidRegion.GetRegion() here, which is expensive. + mLowPrecisionValidRegion.SubOut(mInvalidRegion.GetRegion()); + } + } + + // Shadow methods + void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override; + ShadowableLayer* AsShadowableLayer() override { return this; } + + void RenderLayer() override; + + void ClearCachedResources() override; + + void HandleMemoryPressure() override { + if (mContentClient) { + mContentClient->HandleMemoryPressure(); + } + } + + /** + * Helper method to find the nearest ancestor layers which + * scroll and have a displayport. The parameters are out-params + * which hold the return values; the values passed in may be null. + */ + void GetAncestorLayers(LayerMetricsWrapper* aOutScrollAncestor, + LayerMetricsWrapper* aOutDisplayPortAncestor, + bool* aOutHasTransformAnimation); + + bool IsOptimizedFor( + LayerManager::PaintedLayerCreationHint aCreationHint) override; + + private: + ClientLayerManager* ClientManager() { + return static_cast<ClientLayerManager*>(mManager); + } + + /** + * For the initial PaintThebes of a transaction, calculates all the data + * needed for that paint and any repeated transactions. + */ + void BeginPaint(); + + /** + * Check if the layer is being scrolled by APZ on the compositor. + */ + bool IsScrollingOnCompositor(const FrameMetrics& aParentMetrics); + + /** + * Check if we should use progressive draw on this layer. We will + * disable progressive draw based on a preference or if the layer + * is not being scrolled. + */ + bool UseProgressiveDraw(); + + /** + * Helper function to do the high-precision paint. + * This function returns true if it updated the paint buffer. + */ + bool RenderHighPrecision(const nsIntRegion& aInvalidRegion, + const nsIntRegion& aVisibleRegion, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData); + + /** + * Helper function to do the low-precision paint. + * This function returns true if it updated the paint buffer. + */ + bool RenderLowPrecision(const nsIntRegion& aInvalidRegion, + const nsIntRegion& aVisibleRegion, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData); + + /** + * This causes the paint to be marked as finished, and updates any data + * necessary to persist until the next paint. + */ + void EndPaint(); + + RefPtr<TiledContentClient> mContentClient; + // Flag to indicate if mContentClient is a SingleTiledContentClient. This is + // only valid when mContentClient is non-null. + bool mHaveSingleTiledContentClient; + nsIntRegion mLowPrecisionValidRegion; + BasicTiledLayerPaintData mPaintData; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/CompositableClient.cpp b/gfx/layers/client/CompositableClient.cpp new file mode 100644 index 0000000000..9178f8f5c1 --- /dev/null +++ b/gfx/layers/client/CompositableClient.cpp @@ -0,0 +1,182 @@ +/* -*- 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 + * 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 "mozilla/layers/CompositableClient.h" + +#include <stdint.h> // for uint64_t, uint32_t + +#include "gfxPlatform.h" // for gfxPlatform +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/ImageBridgeChild.h" +#include "mozilla/layers/TextureClient.h" // for TextureClient, etc +#include "mozilla/layers/TextureClientOGL.h" +#include "mozilla/layers/TextureClientRecycleAllocator.h" +#include "mozilla/mozalloc.h" // for operator delete, etc +#ifdef XP_WIN +# include "gfxWindowsPlatform.h" // for gfxWindowsPlatform +# include "mozilla/layers/TextureD3D11.h" +#endif +#include "IPDLActor.h" +#include "gfxUtils.h" + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +void CompositableClient::InitIPDL(const CompositableHandle& aHandle) { + MOZ_ASSERT(aHandle); + + mForwarder->AssertInForwarderThread(); + + mHandle = aHandle; + mIsAsync |= !NS_IsMainThread(); + // By simply taking the lock on mTextureClientRecycler we force memory barrier + // on mHandle and mIsAsync which makes them behave as Atomic as they are only + // ever set in this method. + auto l = mTextureClientRecycler.Lock(); +} + +CompositableClient::CompositableClient(CompositableForwarder* aForwarder, + TextureFlags aTextureFlags) + : mForwarder(aForwarder), + mTextureFlags(aTextureFlags), + mTextureClientRecycler("CompositableClient::mTextureClientRecycler"), + mIsAsync(false) {} + +CompositableClient::~CompositableClient() { Destroy(); } + +LayersBackend CompositableClient::GetCompositorBackendType() const { + return mForwarder->GetCompositorBackendType(); +} + +bool CompositableClient::Connect(ImageContainer* aImageContainer) { + MOZ_ASSERT(!mHandle); + if (!GetForwarder() || mHandle) { + return false; + } + + GetForwarder()->AssertInForwarderThread(); + GetForwarder()->Connect(this, aImageContainer); + return true; +} + +bool CompositableClient::IsConnected() const { + // CanSend() is only reliable in the same thread as the IPDL channel. + mForwarder->AssertInForwarderThread(); + return !!mHandle; +} + +void CompositableClient::Destroy() { + auto l = mTextureClientRecycler.Lock(); + if (*l) { + (*l)->Destroy(); + } + + if (mHandle) { + mForwarder->ReleaseCompositable(mHandle); + mHandle = CompositableHandle(); + } +} + +CompositableHandle CompositableClient::GetAsyncHandle() const { + if (mIsAsync) { + return mHandle; + } + return CompositableHandle(); +} + +already_AddRefed<TextureClient> CompositableClient::CreateBufferTextureClient( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + gfx::BackendType aMoz2DBackend, TextureFlags aTextureFlags) { + return TextureClient::CreateForRawBufferAccess(GetForwarder(), aFormat, aSize, + aMoz2DBackend, + aTextureFlags | mTextureFlags); +} + +already_AddRefed<TextureClient> +CompositableClient::CreateTextureClientForDrawing( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) { + return TextureClient::CreateForDrawing( + GetForwarder(), aFormat, aSize, aSelector, aTextureFlags | mTextureFlags, + aAllocFlags); +} + +already_AddRefed<TextureClient> +CompositableClient::CreateTextureClientFromSurface( + gfx::SourceSurface* aSurface, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) { + return TextureClient::CreateFromSurface(GetForwarder(), aSurface, aSelector, + aTextureFlags | mTextureFlags, + aAllocFlags); +} + +bool CompositableClient::AddTextureClient(TextureClient* aClient) { + if (!aClient) { + return false; + } + aClient->SetAddedToCompositableClient(); + return aClient->InitIPDLActor(mForwarder); +} + +void CompositableClient::ClearCachedResources() { + auto l = mTextureClientRecycler.Lock(); + if (*l) { + (*l)->ShrinkToMinimumSize(); + } +} + +void CompositableClient::HandleMemoryPressure() { + auto l = mTextureClientRecycler.Lock(); + if (*l) { + (*l)->ShrinkToMinimumSize(); + } +} + +void CompositableClient::RemoveTexture(TextureClient* aTexture) { + mForwarder->RemoveTextureFromCompositable(this, aTexture); +} + +TextureClientRecycleAllocator* CompositableClient::GetTextureClientRecycler() { + auto l = mTextureClientRecycler.Lock(); + if (*l) { + return *l; + } + + if (!mForwarder || !mForwarder->GetTextureForwarder()) { + return nullptr; + } + + *l = new layers::TextureClientRecycleAllocator(mForwarder); + return *l; +} + +void CompositableClient::DumpTextureClient(std::stringstream& aStream, + TextureClient* aTexture, + TextureDumpMode aCompress) { + if (!aTexture) { + return; + } + RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface(); + if (!dSurf) { + return; + } + if (aCompress == TextureDumpMode::Compress) { + aStream << gfxUtils::GetAsLZ4Base64Str(dSurf).get(); + } else { + aStream << gfxUtils::GetAsDataURI(dSurf).get(); + } +} + +AutoRemoveTexture::~AutoRemoveTexture() { + if (mCompositable && mTexture && mCompositable->IsConnected()) { + mCompositable->RemoveTexture(mTexture); + } +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/CompositableClient.h b/gfx/layers/client/CompositableClient.h new file mode 100644 index 0000000000..90b4235f8e --- /dev/null +++ b/gfx/layers/client/CompositableClient.h @@ -0,0 +1,213 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_BUFFERCLIENT_H +#define MOZILLA_GFX_BUFFERCLIENT_H + +#include <stdint.h> // for uint64_t + +#include <map> // for map +#include <vector> // for vector + +#include "mozilla/Assertions.h" // for MOZ_CRASH +#include "mozilla/DataMutex.h" +#include "mozilla/RefPtr.h" // for already_AddRefed, RefCounted +#include "mozilla/gfx/Types.h" // for SurfaceFormat +#include "mozilla/layers/CompositorTypes.h" +#include "mozilla/layers/LayersTypes.h" // for LayersBackend, TextureDumpMode +#include "mozilla/layers/TextureClient.h" // for TextureClient +#include "mozilla/webrender/WebRenderTypes.h" // for RenderRoot +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc + +namespace mozilla { +namespace layers { + +class CompositableClient; +class ImageBridgeChild; +class ImageContainer; +class CompositableForwarder; +class CompositableChild; +class TextureClientRecycleAllocator; +class ContentClientRemoteBuffer; + +/** + * CompositableClient manages the texture-specific logic for composite layers, + * independently of the layer. It is the content side of a CompositableClient/ + * CompositableHost pair. + * + * CompositableClient's purpose is to send texture data to the compositor side + * along with any extra information about how the texture is to be composited. + * Things like opacity or transformation belong to layer and not compositable. + * + * Since Compositables are independent of layers it is possible to create one, + * connect it to the compositor side, and start sending images to it. This alone + * is arguably not very useful, but it means that as long as a shadow layer can + * do the proper magic to find a reference to the right CompositableHost on the + * Compositor side, a Compositable client can be used outside of the main + * shadow layer forwarder machinery that is used on the main thread. + * + * The first step is to create a Compositable client and call Connect(). + * Connect() creates the underlying IPDL actor (see CompositableChild) and the + * corresponding CompositableHost on the other side. + * + * To do in-transaction texture transfer (the default), call + * ShadowLayerForwarder::Attach(CompositableClient*, ShadowableLayer*). This + * will let the LayerComposite on the compositor side know which + * CompositableHost to use for compositing. + * + * To do async texture transfer (like async-video), the CompositableClient + * should be created with a different CompositableForwarder (like + * ImageBridgeChild) and attachment is done with + * CompositableForwarder::AttachAsyncCompositable that takes an identifier + * instead of a CompositableChild, since the CompositableClient is not managed + * by this layer forwarder (the matching uses a global map on the compositor + * side, see CompositableMap in ImageBridgeParent.cpp) + * + * Subclasses: Painted layers use ContentClients, ImageLayers use ImageClients, + * Canvas layers use CanvasClients (but ImageHosts). We have a different + * subclass where we have a different way of interfacing with the textures - in + * terms of drawing into the compositable and/or passing its contents to the + * compostior. + */ +class CompositableClient { + protected: + virtual ~CompositableClient(); + + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositableClient) + + explicit CompositableClient(CompositableForwarder* aForwarder, + TextureFlags aFlags = TextureFlags::NO_FLAGS); + + virtual void Dump(std::stringstream& aStream, const char* aPrefix = "", + bool aDumpHtml = false, + TextureDumpMode aCompress = TextureDumpMode::Compress){}; + + virtual TextureInfo GetTextureInfo() const = 0; + + LayersBackend GetCompositorBackendType() const; + + already_AddRefed<TextureClient> CreateBufferTextureClient( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + gfx::BackendType aMoz2dBackend = gfx::BackendType::NONE, + TextureFlags aFlags = TextureFlags::DEFAULT); + + already_AddRefed<TextureClient> CreateTextureClientForDrawing( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT); + + already_AddRefed<TextureClient> CreateTextureClientFromSurface( + gfx::SourceSurface* aSurface, BackendSelector aSelector, + TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT); + + /** + * Establishes the connection with compositor side through IPDL + */ + virtual bool Connect(ImageContainer* aImageContainer = nullptr); + + void Destroy(); + + bool IsConnected() const; + + CompositableForwarder* GetForwarder() const { return mForwarder; } + + /** + * This identifier is what lets us attach async compositables with a shadow + * layer. It is not used if the compositable is used with the regular shadow + * layer forwarder. + * + * If this returns empty, it means the compositable is not async (it is used + * on the main thread). + */ + CompositableHandle GetAsyncHandle() const; + + /** + * Handle for IPDL communication. + */ + CompositableHandle GetIPCHandle() const { return mHandle; } + + /** + * Tells the Compositor to create a TextureHost for this TextureClient. + */ + virtual bool AddTextureClient(TextureClient* aClient); + + /** + * A hook for the when the Compositable is detached from it's layer. + */ + virtual void OnDetach() {} + + /** + * Clear any resources that are not immediately necessary. This may be called + * in low-memory conditions. + */ + virtual void ClearCachedResources(); + + /** + * Shrink memory usage. + * Called when "memory-pressure" is observed. + */ + virtual void HandleMemoryPressure(); + + /** + * Should be called when deataching a TextureClient from a Compositable, + * because some platforms need to do some extra book keeping when this + * happens. + * + * See AutoRemoveTexture to automatically invoke this at the end of a scope. + */ + virtual void RemoveTexture(TextureClient* aTexture); + + void InitIPDL(const CompositableHandle& aHandle); + + TextureFlags GetTextureFlags() const { return mTextureFlags; } + + TextureClientRecycleAllocator* GetTextureClientRecycler(); + + bool HasTextureClientRecycler() { + auto lock = mTextureClientRecycler.Lock(); + return !!(*lock); + } + + static void DumpTextureClient(std::stringstream& aStream, + TextureClient* aTexture, + TextureDumpMode aCompress); + + protected: + RefPtr<CompositableForwarder> mForwarder; + // Some layers may want to enforce some flags to all their textures + // (like disallowing tiling) + Atomic<TextureFlags> mTextureFlags; + DataMutex<RefPtr<TextureClientRecycleAllocator>> mTextureClientRecycler; + + // Only ever accessed via mTextureClientRecycler's Lock + CompositableHandle mHandle; + bool mIsAsync; + + friend class CompositableChild; +}; + +/** + * Helper to call RemoveTexture at the end of a scope. + */ +struct AutoRemoveTexture { + explicit AutoRemoveTexture(CompositableClient* aCompositable, + TextureClient* aTexture = nullptr) + : mTexture(aTexture), mCompositable(aCompositable) {} + + ~AutoRemoveTexture(); + + RefPtr<TextureClient> mTexture; + + private: + CompositableClient* mCompositable; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/ContentClient.cpp b/gfx/layers/client/ContentClient.cpp new file mode 100644 index 0000000000..4e465f0a02 --- /dev/null +++ b/gfx/layers/client/ContentClient.cpp @@ -0,0 +1,881 @@ +/* -*- 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 + * 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 "mozilla/layers/ContentClient.h" +#include "BasicLayers.h" // for BasicLayerManager +#include "gfxContext.h" // for gfxContext, etc +#include "gfxPlatform.h" // for gfxPlatform +#include "gfxEnv.h" // for gfxEnv + +#include "gfxPoint.h" // for IntSize, gfxPoint +#include "gfxUtils.h" // for gfxUtils +#include "ipc/ShadowLayers.h" // for ShadowLayerForwarder +#include "mozilla/ArrayUtils.h" // for ArrayLength +#include "mozilla/gfx/2D.h" // for DrawTarget, Factory +#include "mozilla/gfx/BasePoint.h" // for BasePoint +#include "mozilla/gfx/BaseSize.h" // for BaseSize +#include "mozilla/gfx/Rect.h" // for Rect +#include "mozilla/gfx/Types.h" +#include "mozilla/layers/CompositorBridgeChild.h" // for CompositorBridgeChild +#include "mozilla/layers/LayerManagerComposite.h" +#include "mozilla/layers/LayersMessages.h" // for ThebesBufferData +#include "mozilla/layers/LayersTypes.h" +#include "mozilla/layers/PaintThread.h" +#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc +#include "nsISupportsImpl.h" // for gfxContext::Release, etc +#include "nsIWidget.h" // for nsIWidget +#include "nsLayoutUtils.h" +#ifdef XP_WIN +# include "gfxWindowsPlatform.h" +#endif +#ifdef MOZ_WIDGET_GTK +# include "gfxPlatformGtk.h" +#endif +#ifdef MOZ_WAYLAND +# include "mozilla/widget/nsWaylandDisplay.h" +#endif +#include "ReadbackLayer.h" + +#include <utility> +#include <vector> + +namespace mozilla { + +using namespace gfx; + +namespace layers { + +static TextureFlags TextureFlagsForContentClientFlags(uint32_t aBufferFlags) { + TextureFlags result = TextureFlags::NO_FLAGS; + + if (aBufferFlags & ContentClient::BUFFER_COMPONENT_ALPHA) { + result |= TextureFlags::COMPONENT_ALPHA; + } + + return result; +} + +static IntRect ComputeBufferRect(const IntRect& aRequestedRect) { + IntRect rect(aRequestedRect); + // Set a minimum width to guarantee a minimum size of buffers we + // allocate (and work around problems on some platforms with smaller + // dimensions). 64 used to be the magic number needed to work around + // a rendering glitch on b2g (see bug 788411). Now that we don't support + // this device anymore we should be fine with 8 pixels as the minimum. + rect.SetWidth(std::max(aRequestedRect.Width(), 8)); + return rect; +} + +/* static */ +already_AddRefed<ContentClient> ContentClient::CreateContentClient( + CompositableForwarder* aForwarder) { + LayersBackend backend = aForwarder->GetCompositorBackendType(); + if (backend != LayersBackend::LAYERS_OPENGL && + backend != LayersBackend::LAYERS_D3D11 && + backend != LayersBackend::LAYERS_WR && + backend != LayersBackend::LAYERS_BASIC) { + return nullptr; + } + + bool useDoubleBuffering = false; + +#ifdef XP_WIN + if (backend == LayersBackend::LAYERS_D3D11) { + useDoubleBuffering = gfxWindowsPlatform::GetPlatform()->IsDirect2DBackend(); + } else +#endif + { +#ifdef MOZ_WIDGET_GTK +# ifdef MOZ_WAYLAND + if (widget::GetDMABufDevice()->IsDMABufTexturesEnabled()) { + useDoubleBuffering = true; + } else +# endif + // We can't use double buffering when using image content with + // Xrender support on Linux, as ContentHostDoubleBuffered is not + // suited for direct uploads to the server. + if (!gfxPlatformGtk::GetPlatform()->UseImageOffscreenSurfaces() || + !gfxVars::UseXRender()) +#endif + { + useDoubleBuffering = backend == LayersBackend::LAYERS_BASIC; + } + } + + if (useDoubleBuffering || gfxEnv::ForceDoubleBuffering()) { + return MakeAndAddRef<ContentClientDoubleBuffered>(aForwarder); + } + return MakeAndAddRef<ContentClientSingleBuffered>(aForwarder); +} + +void ContentClient::Clear() { mBuffer = nullptr; } + +ContentClient::PaintState ContentClient::BeginPaint(PaintedLayer* aLayer, + uint32_t aFlags) { + BufferDecision dest = CalculateBufferForPaint(aLayer, aFlags); + + PaintState result; + result.mAsyncPaint = (aFlags & PAINT_ASYNC); + result.mContentType = dest.mBufferContentType; + + if (!dest.mCanKeepBufferContents) { + // We're effectively clearing the valid region, so we need to draw + // the entire needed region now. + MOZ_ASSERT(!dest.mCanReuseBuffer); + MOZ_ASSERT(dest.mValidRegion.IsEmpty()); + + result.mRegionToInvalidate = aLayer->GetValidRegion(); + +#if defined(MOZ_DUMP_PAINTING) + if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) { + if (result.mContentType != mBuffer->GetContentType()) { + printf_stderr( + "Invalidating entire rotated buffer (layer %p): content type " + "changed\n", + aLayer); + } else if ((dest.mBufferMode == SurfaceMode::SURFACE_COMPONENT_ALPHA) != + mBuffer->HaveBufferOnWhite()) { + printf_stderr( + "Invalidating entire rotated buffer (layer %p): component alpha " + "changed\n", + aLayer); + } + } +#endif + Clear(); + } + + result.mRegionToDraw.Sub(dest.mNeededRegion, dest.mValidRegion); + + if (result.mRegionToDraw.IsEmpty()) return result; + + // We need to disable rotation if we're going to be resampled when + // drawing, because we might sample across the rotation boundary. + // Also disable buffer rotation when using webrender. + bool canHaveRotation = + gfxPlatform::BufferRotationEnabled() && + !(aFlags & (PAINT_WILL_RESAMPLE | PAINT_NO_ROTATION)) && + !(aLayer->Manager()->AsWebRenderLayerManager()); + bool canDrawRotated = aFlags & PAINT_CAN_DRAW_ROTATED; + OpenMode readMode = + result.mAsyncPaint ? OpenMode::OPEN_READ_ASYNC : OpenMode::OPEN_READ; + OpenMode writeMode = result.mAsyncPaint ? OpenMode::OPEN_READ_WRITE_ASYNC + : OpenMode::OPEN_READ_WRITE; + + IntRect drawBounds = result.mRegionToDraw.GetBounds(); + + if (result.mAsyncPaint) { + result.mAsyncTask.reset(new PaintTask()); + } + + // Try to acquire the back buffer, copy over contents if we are using a new + // buffer, and rotate or unrotate the buffer as necessary + if (mBuffer && dest.mCanReuseBuffer) { + if (mBuffer->Lock(writeMode)) { + auto newParameters = mBuffer->AdjustedParameters(dest.mBufferRect); + + bool needsUnrotate = + (!canHaveRotation && newParameters.IsRotated()) || + (!canDrawRotated && newParameters.RectWrapsBuffer(drawBounds)); + bool canUnrotate = + !result.mAsyncPaint || mBuffer->BufferRotation() == IntPoint(0, 0); + + // Only begin a frame and copy over the previous frame if we don't need + // to unrotate, or we can try to unrotate it. This is to ensure that we + // don't have a paint task that depends on another paint task. + if (!needsUnrotate || canUnrotate) { + // If we're async painting then begin to capture draw commands + if (result.mAsyncPaint) { + mBuffer->BeginCapture(); + } + + // Do not modify result.mRegionToDraw or result.mContentType after this + // call. + FinalizeFrame(result); + } + + // Try to rotate the buffer or unrotate it if we cannot be rotated + if (needsUnrotate) { + if (canUnrotate && mBuffer->UnrotateBufferTo(newParameters)) { + newParameters.SetUnrotated(); + mBuffer->SetParameters(newParameters); + } else { + MOZ_ASSERT(GetFrontBuffer()); + mBuffer->Unlock(); + dest.mBufferRect = ComputeBufferRect(dest.mNeededRegion.GetBounds()); + dest.mCanReuseBuffer = false; + } + } else { + mBuffer->SetParameters(newParameters); + } + } else { + result.mRegionToDraw = dest.mNeededRegion; + dest.mCanReuseBuffer = false; + Clear(); + } + } + + MOZ_ASSERT(dest.mBufferRect.Contains(result.mRegionToDraw.GetBounds())); + + NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || + dest.mBufferRect == dest.mNeededRegion.GetBounds(), + "If we're resampling, we need to validate the entire buffer"); + + // We never had a buffer, the buffer wasn't big enough, the content changed + // types, or we failed to unrotate the buffer when requested. In any case, + // we need to allocate a new one and prepare it for drawing. + if (!dest.mCanReuseBuffer) { + uint32_t bufferFlags = 0; + if (dest.mBufferMode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { + bufferFlags |= BUFFER_COMPONENT_ALPHA; + } + + RefPtr<RotatedBuffer> newBuffer = + CreateBuffer(result.mContentType, dest.mBufferRect, bufferFlags); + + if (!newBuffer) { + if (Factory::ReasonableSurfaceSize( + IntSize(dest.mBufferRect.Width(), dest.mBufferRect.Height()))) { + gfxCriticalNote << "Failed buffer for " << dest.mBufferRect.X() << ", " + << dest.mBufferRect.Y() << ", " + << dest.mBufferRect.Width() << ", " + << dest.mBufferRect.Height(); + } + result.mAsyncTask = nullptr; + Clear(); + return result; + } + + if (!newBuffer->Lock(writeMode)) { + gfxCriticalNote << "Failed to lock new back buffer."; + result.mAsyncTask = nullptr; + Clear(); + return result; + } + + if (result.mAsyncPaint) { + newBuffer->BeginCapture(); + } + + // If we have an existing front buffer, copy it into the new back buffer + RefPtr<RotatedBuffer> frontBuffer = GetFrontBuffer(); + + if (frontBuffer && frontBuffer->Lock(readMode)) { + nsIntRegion updateRegion = newBuffer->BufferRect(); + updateRegion.Sub(updateRegion, result.mRegionToDraw); + + if (!updateRegion.IsEmpty()) { + newBuffer->UpdateDestinationFrom(*frontBuffer, + updateRegion.GetBounds()); + } + + frontBuffer->Unlock(); + } else { + result.mRegionToDraw = dest.mNeededRegion; + } + + Clear(); + mBuffer = newBuffer; + } + + NS_ASSERTION(canHaveRotation || mBuffer->BufferRotation() == IntPoint(0, 0), + "Rotation disabled, but we have nonzero rotation?"); + + if (result.mAsyncPaint) { + result.mAsyncTask->mTarget = mBuffer->GetBufferTarget(); + result.mAsyncTask->mClients.AppendElement(mBuffer->GetClient()); + if (mBuffer->GetClientOnWhite()) { + result.mAsyncTask->mClients.AppendElement(mBuffer->GetClientOnWhite()); + } + } + + nsIntRegion invalidate; + invalidate.Sub(aLayer->GetValidRegion(), dest.mBufferRect); + result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate); + + result.mClip = DrawRegionClip::DRAW; + result.mMode = dest.mBufferMode; + + return result; +} + +void ContentClient::EndPaint( + PaintState& aPaintState, + nsTArray<ReadbackProcessor::Update>* aReadbackUpdates) { + if (aPaintState.mAsyncTask) { + aPaintState.mAsyncTask->mCapture = mBuffer->EndCapture(); + } +} + +static nsIntRegion ExpandDrawRegion(ContentClient::PaintState& aPaintState, + RotatedBuffer::DrawIterator* aIter, + BackendType aBackendType) { + nsIntRegion* drawPtr = &aPaintState.mRegionToDraw; + if (aIter) { + // The iterators draw region currently only contains the bounds of the + // region, this makes it the precise region. + aIter->mDrawRegion.And(aIter->mDrawRegion, aPaintState.mRegionToDraw); + drawPtr = &aIter->mDrawRegion; + } + if (aBackendType == BackendType::DIRECT2D || + aBackendType == BackendType::DIRECT2D1_1) { + // Simplify the draw region to avoid hitting expensive drawing paths + // for complex regions. + drawPtr->SimplifyOutwardByArea(100 * 100); + } + return *drawPtr; +} + +DrawTarget* ContentClient::BorrowDrawTargetForPainting( + ContentClient::PaintState& aPaintState, + RotatedBuffer::DrawIterator* aIter /* = nullptr */) { + if (aPaintState.mMode == SurfaceMode::SURFACE_NONE || !mBuffer) { + return nullptr; + } + + DrawTarget* result = mBuffer->BorrowDrawTargetForQuadrantUpdate( + aPaintState.mRegionToDraw.GetBounds(), aIter); + if (!result || !result->IsValid()) { + if (result) { + mBuffer->ReturnDrawTarget(result); + } + return nullptr; + } + + nsIntRegion regionToDraw = + ExpandDrawRegion(aPaintState, aIter, result->GetBackendType()); + + if (aPaintState.mMode == SurfaceMode::SURFACE_COMPONENT_ALPHA || + aPaintState.mContentType == gfxContentType::COLOR_ALPHA) { + // HaveBuffer() => we have an existing buffer that we must clear + for (auto iter = regionToDraw.RectIter(); !iter.Done(); iter.Next()) { + const IntRect& rect = iter.Get(); + result->ClearRect(Rect(rect.X(), rect.Y(), rect.Width(), rect.Height())); + } + } + + return result; +} + +void ContentClient::ReturnDrawTarget(gfx::DrawTarget*& aReturned) { + mBuffer->ReturnDrawTarget(aReturned); +} + +ContentClient::BufferDecision ContentClient::CalculateBufferForPaint( + PaintedLayer* aLayer, uint32_t aFlags) { + gfxContentType layerContentType = aLayer->CanUseOpaqueSurface() + ? gfxContentType::COLOR + : gfxContentType::COLOR_ALPHA; + + SurfaceMode mode; + gfxContentType contentType; + IntRect destBufferRect; + nsIntRegion neededRegion; + nsIntRegion validRegion = aLayer->GetValidRegion(); + + bool canReuseBuffer = !!mBuffer; + bool canKeepBufferContents = true; + + while (true) { + mode = aLayer->GetSurfaceMode(); + neededRegion = aLayer->GetVisibleRegion().ToUnknownRegion(); + canReuseBuffer = + canReuseBuffer && + ValidBufferSize(mBufferSizePolicy, mBuffer->BufferRect().Size(), + neededRegion.GetBounds().Size()); + contentType = layerContentType; + + if (canReuseBuffer) { + if (mBuffer->BufferRect().Contains(neededRegion.GetBounds())) { + // We don't need to adjust mBufferRect. + destBufferRect = mBuffer->BufferRect(); + } else if (neededRegion.GetBounds().Size() <= + mBuffer->BufferRect().Size()) { + // The buffer's big enough but doesn't contain everything that's + // going to be visible. We'll move it. + destBufferRect = IntRect(neededRegion.GetBounds().TopLeft(), + mBuffer->BufferRect().Size()); + } else { + destBufferRect = neededRegion.GetBounds(); + } + } else { + destBufferRect = ComputeBufferRect(neededRegion.GetBounds()); + } + + if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { +#if defined(MOZ_GFX_OPTIMIZE_MOBILE) + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; +#else + if (!aLayer->GetParent() || + !aLayer->GetParent()->SupportsComponentAlphaChildren() || + !aLayer->AsShadowableLayer() || + !aLayer->AsShadowableLayer()->HasShadow()) { + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; + } else { + contentType = gfxContentType::COLOR; + } +#endif + } + + if ((aFlags & PAINT_WILL_RESAMPLE) && + (!neededRegion.GetBounds().IsEqualInterior(destBufferRect) || + neededRegion.GetNumRects() > 1)) { + // The area we add to neededRegion might not be painted opaquely. + if (mode == SurfaceMode::SURFACE_OPAQUE) { + contentType = gfxContentType::COLOR_ALPHA; + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; + } + + // We need to validate the entire buffer, to make sure that only valid + // pixels are sampled. + neededRegion = destBufferRect; + } + + // If we have an existing buffer, but the content type has changed or we + // have transitioned into/out of component alpha, then we need to recreate + // it. + bool needsComponentAlpha = (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA); + bool backBufferChangedSurface = + mBuffer && (contentType != mBuffer->GetContentType() || + needsComponentAlpha != mBuffer->HaveBufferOnWhite()); + if (canKeepBufferContents && backBufferChangedSurface) { + // Restart the decision process; we won't re-enter since we guard on + // being able to keep the buffer contents. + canReuseBuffer = false; + canKeepBufferContents = false; + validRegion.SetEmpty(); + continue; + } + break; + } + + NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()), + "Destination rect doesn't contain what we need to paint"); + + BufferDecision dest; + dest.mNeededRegion = std::move(neededRegion); + dest.mValidRegion = std::move(validRegion); + dest.mBufferRect = destBufferRect; + dest.mBufferMode = mode; + dest.mBufferContentType = contentType; + dest.mCanReuseBuffer = canReuseBuffer; + dest.mCanKeepBufferContents = canKeepBufferContents; + return dest; +} + +bool ContentClient::ValidBufferSize(BufferSizePolicy aPolicy, + const gfx::IntSize& aBufferSize, + const gfx::IntSize& aVisibleBoundsSize) { + return ( + aVisibleBoundsSize == aBufferSize || + (SizedToVisibleBounds != aPolicy && aVisibleBoundsSize < aBufferSize)); +} + +void ContentClient::PrintInfo(std::stringstream& aStream, const char* aPrefix) { + aStream << aPrefix; + aStream << nsPrintfCString("ContentClient (0x%p)", this).get(); +} + +// We pass a null pointer for the ContentClient Forwarder argument, which means +// this client will not have a ContentHost on the other side. +ContentClientBasic::ContentClientBasic(gfx::BackendType aBackend) + : ContentClient(nullptr, ContainsVisibleBounds), mBackend(aBackend) {} + +void ContentClientBasic::DrawTo(PaintedLayer* aLayer, gfx::DrawTarget* aTarget, + float aOpacity, gfx::CompositionOp aOp, + gfx::SourceSurface* aMask, + const gfx::Matrix* aMaskTransform) { + if (!mBuffer) { + return; + } + + mBuffer->DrawTo(aLayer, aTarget, aOpacity, aOp, aMask, aMaskTransform); +} + +RefPtr<RotatedBuffer> ContentClientBasic::CreateBuffer(gfxContentType aType, + const IntRect& aRect, + uint32_t aFlags) { + MOZ_ASSERT(!(aFlags & BUFFER_COMPONENT_ALPHA)); + if (aFlags & BUFFER_COMPONENT_ALPHA) { + gfxDevCrash(LogReason::AlphaWithBasicClient) + << "Asking basic content client for component alpha"; + } + + IntSize size(aRect.Width(), aRect.Height()); + RefPtr<gfx::DrawTarget> drawTarget; + +#ifdef XP_WIN + if (mBackend == BackendType::CAIRO && + (aType == gfxContentType::COLOR || + aType == gfxContentType::COLOR_ALPHA)) { + RefPtr<gfxASurface> surf = new gfxWindowsSurface( + size, aType == gfxContentType::COLOR ? gfxImageFormat::X8R8G8B8_UINT32 + : gfxImageFormat::A8R8G8B8_UINT32); + drawTarget = gfxPlatform::CreateDrawTargetForSurface(surf, size); + } +#endif + + if (!drawTarget) { + drawTarget = gfxPlatform::GetPlatform()->CreateDrawTargetForBackend( + mBackend, size, + gfxPlatform::GetPlatform()->Optimal2DFormatForContent(aType)); + } + + if (!drawTarget) { + return nullptr; + } + + return new DrawTargetRotatedBuffer(drawTarget, nullptr, aRect, + IntPoint(0, 0)); +} + +class RemoteBufferReadbackProcessor : public TextureReadbackSink { + public: + RemoteBufferReadbackProcessor( + nsTArray<ReadbackProcessor::Update>* aReadbackUpdates, + const IntRect& aBufferRect, const nsIntPoint& aBufferRotation) + : mReadbackUpdates(aReadbackUpdates->Clone()), + mBufferRect(aBufferRect), + mBufferRotation(aBufferRotation) { + for (uint32_t i = 0; i < mReadbackUpdates.Length(); ++i) { + mLayerRefs.push_back(mReadbackUpdates[i].mLayer); + } + } + + virtual void ProcessReadback( + gfx::DataSourceSurface* aSourceSurface) override { + SourceRotatedBuffer rotBuffer(aSourceSurface, nullptr, mBufferRect, + mBufferRotation); + + for (uint32_t i = 0; i < mReadbackUpdates.Length(); ++i) { + ReadbackProcessor::Update& update = mReadbackUpdates[i]; + nsIntPoint offset = update.mLayer->GetBackgroundLayerOffset(); + + ReadbackSink* sink = update.mLayer->GetSink(); + + if (!sink) { + continue; + } + + if (!aSourceSurface) { + sink->SetUnknown(update.mSequenceCounter); + continue; + } + + RefPtr<DrawTarget> dt = sink->BeginUpdate(update.mUpdateRect + offset, + update.mSequenceCounter); + if (!dt) { + continue; + } + + dt->SetTransform(Matrix::Translation(offset.x, offset.y)); + + rotBuffer.DrawBufferWithRotation(dt); + + update.mLayer->GetSink()->EndUpdate(update.mUpdateRect + offset); + } + } + + private: + nsTArray<ReadbackProcessor::Update> mReadbackUpdates; + // This array is used to keep the layers alive until the callback. + std::vector<RefPtr<Layer>> mLayerRefs; + + IntRect mBufferRect; + nsIntPoint mBufferRotation; +}; + +void ContentClientRemoteBuffer::EndPaint( + PaintState& aPaintState, + nsTArray<ReadbackProcessor::Update>* aReadbackUpdates) { + MOZ_ASSERT(!mBuffer || !mBuffer->HaveBufferOnWhite() || !aReadbackUpdates || + aReadbackUpdates->Length() == 0); + + RemoteRotatedBuffer* remoteBuffer = GetRemoteBuffer(); + + if (remoteBuffer && remoteBuffer->IsLocked()) { + if (aReadbackUpdates && aReadbackUpdates->Length() > 0) { + RefPtr<TextureReadbackSink> readbackSink = + new RemoteBufferReadbackProcessor(aReadbackUpdates, + remoteBuffer->BufferRect(), + remoteBuffer->BufferRotation()); + + remoteBuffer->GetClient()->SetReadbackSink(readbackSink); + } + + remoteBuffer->Unlock(); + remoteBuffer->SyncWithObject(mForwarder->GetSyncObject()); + } + + ContentClient::EndPaint(aPaintState, aReadbackUpdates); +} + +RefPtr<RotatedBuffer> ContentClientRemoteBuffer::CreateBuffer( + gfxContentType aType, const IntRect& aRect, uint32_t aFlags) { + // If we hit this assertion, then it might be due to an empty transaction + // followed by a real transaction. Our buffers should be created (but not + // painted in the empty transaction) and then painted (but not created) in the + // real transaction. That is kind of fragile, and this assert will catch + // circumstances where we screw that up, e.g., by unnecessarily recreating our + // buffers. + MOZ_ASSERT(!mIsNewBuffer, + "Bad! Did we create a buffer twice without painting?"); + + gfx::SurfaceFormat format = + gfxPlatform::GetPlatform()->Optimal2DFormatForContent(aType); + + TextureFlags textureFlags = TextureFlagsForContentClientFlags(aFlags); + if (aFlags & BUFFER_COMPONENT_ALPHA) { + textureFlags |= TextureFlags::COMPONENT_ALPHA; + } + + RefPtr<RotatedBuffer> buffer = + CreateBufferInternal(aRect, format, textureFlags); + + if (!buffer) { + return nullptr; + } + + mIsNewBuffer = true; + mTextureFlags = textureFlags; + + return buffer; +} + +RefPtr<RotatedBuffer> ContentClientRemoteBuffer::CreateBufferInternal( + const gfx::IntRect& aRect, gfx::SurfaceFormat aFormat, + TextureFlags aFlags) { + TextureAllocationFlags textureAllocFlags = + TextureAllocationFlags::ALLOC_DEFAULT; + + RefPtr<TextureClient> textureClient = CreateTextureClientForDrawing( + aFormat, aRect.Size(), BackendSelector::Content, + aFlags | ExtraTextureFlags() | TextureFlags::BLOCKING_READ_LOCK, + textureAllocFlags); + + if (!textureClient || !AddTextureClient(textureClient)) { + return nullptr; + } + + RefPtr<TextureClient> textureClientOnWhite; + if (aFlags & TextureFlags::COMPONENT_ALPHA) { + TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT; + if (mForwarder->SupportsTextureDirectMapping()) { + allocFlags = + TextureAllocationFlags(allocFlags | ALLOC_ALLOW_DIRECT_MAPPING); + } + textureClientOnWhite = + textureClient->CreateSimilar(mForwarder->GetCompositorBackendType(), + aFlags | ExtraTextureFlags(), allocFlags); + if (!textureClientOnWhite || !AddTextureClient(textureClientOnWhite)) { + return nullptr; + } + // We don't enable the readlock for the white buffer since we always + // use them together and waiting on the lock for the black + // should be sufficient. + } + + return new RemoteRotatedBuffer(textureClient, textureClientOnWhite, aRect, + IntPoint(0, 0)); +} + +nsIntRegion ContentClientRemoteBuffer::GetUpdatedRegion( + const nsIntRegion& aRegionToDraw, const nsIntRegion& aVisibleRegion) { + nsIntRegion updatedRegion; + if (mIsNewBuffer || mBuffer->DidSelfCopy()) { + // A buffer reallocation clears both buffers. The front buffer has all the + // content by now, but the back buffer is still clear. Here, in effect, we + // are saying to copy all of the pixels of the front buffer to the back. + // Also when we self-copied in the buffer, the buffer space + // changes and some changed buffer content isn't reflected in the + // draw or invalidate region (on purpose!). When this happens, we + // need to read back the entire buffer too. + updatedRegion = aVisibleRegion.GetBounds(); + mIsNewBuffer = false; + } else { + updatedRegion = aRegionToDraw; + } + + MOZ_ASSERT(mBuffer, "should have a back buffer by now"); + NS_ASSERTION(mBuffer->BufferRect().Contains(aRegionToDraw.GetBounds()), + "Update outside of buffer rect!"); + + return updatedRegion; +} + +void ContentClientRemoteBuffer::Updated(const nsIntRegion& aRegionToDraw, + const nsIntRegion& aVisibleRegion) { + nsIntRegion updatedRegion = GetUpdatedRegion(aRegionToDraw, aVisibleRegion); + + RemoteRotatedBuffer* remoteBuffer = GetRemoteBuffer(); + + MOZ_ASSERT(remoteBuffer && remoteBuffer->GetClient()); + if (remoteBuffer->HaveBufferOnWhite()) { + mForwarder->UseComponentAlphaTextures(this, remoteBuffer->GetClient(), + remoteBuffer->GetClientOnWhite()); + } else { + AutoTArray<CompositableForwarder::TimedTextureClient, 1> textures; + CompositableForwarder::TimedTextureClient* t = textures.AppendElement(); + t->mTextureClient = remoteBuffer->GetClient(); + IntSize size = remoteBuffer->GetClient()->GetSize(); + t->mPictureRect = nsIntRect(0, 0, size.width, size.height); + + GetForwarder()->UseTextures(this, textures); + } + + // This forces a synchronous transaction, so we can swap buffers now + // and know that we'll have sole ownership of the old front buffer + // by the time we paint next. + mForwarder->UpdateTextureRegion( + this, + ThebesBufferData(remoteBuffer->BufferRect(), + remoteBuffer->BufferRotation()), + updatedRegion); + SwapBuffers(updatedRegion); +} + +void ContentClientRemoteBuffer::Dump(std::stringstream& aStream, + const char* aPrefix, bool aDumpHtml, + TextureDumpMode aCompress) { + RemoteRotatedBuffer* remoteBuffer = GetRemoteBuffer(); + + // TODO We should combine the OnWhite/OnBlack here an just output a single + // image. + if (!aDumpHtml) { + aStream << "\n" << aPrefix << "Surface: "; + } + CompositableClient::DumpTextureClient( + aStream, remoteBuffer ? remoteBuffer->GetClient() : nullptr, aCompress); +} + +void ContentClientDoubleBuffered::Dump(std::stringstream& aStream, + const char* aPrefix, bool aDumpHtml, + TextureDumpMode aCompress) { + // TODO We should combine the OnWhite/OnBlack here an just output a single + // image. + if (!aDumpHtml) { + aStream << "\n" << aPrefix << "Surface: "; + } + CompositableClient::DumpTextureClient( + aStream, mFrontBuffer ? mFrontBuffer->GetClient() : nullptr, aCompress); +} + +void ContentClientDoubleBuffered::Clear() { + ContentClient::Clear(); + mFrontBuffer = nullptr; +} + +void ContentClientDoubleBuffered::SwapBuffers( + const nsIntRegion& aFrontUpdatedRegion) { + mFrontUpdatedRegion = aFrontUpdatedRegion; + + RefPtr<RemoteRotatedBuffer> frontBuffer = mFrontBuffer; + RefPtr<RemoteRotatedBuffer> backBuffer = GetRemoteBuffer(); + + std::swap(frontBuffer, backBuffer); + + mFrontBuffer = frontBuffer; + mBuffer = backBuffer; + + mFrontAndBackBufferDiffer = true; +} + +ContentClient::PaintState ContentClientDoubleBuffered::BeginPaint( + PaintedLayer* aLayer, uint32_t aFlags) { + EnsureBackBufferIfFrontBuffer(); + + mIsNewBuffer = false; + + if (!mFrontBuffer || !mBuffer) { + mFrontAndBackBufferDiffer = false; + } + + if (mFrontAndBackBufferDiffer) { + if (mFrontBuffer->DidSelfCopy()) { + // We can't easily draw our front buffer into us, since we're going to be + // copying stuff around anyway it's easiest if we just move our situation + // to non-rotated while we're at it. If this situation occurs we'll have + // hit a self-copy path in PaintThebes before as well anyway. + gfx::IntRect backBufferRect = mBuffer->BufferRect(); + backBufferRect.MoveTo(mFrontBuffer->BufferRect().TopLeft()); + + mBuffer->SetBufferRect(backBufferRect); + mBuffer->SetBufferRotation(IntPoint(0, 0)); + } else { + mBuffer->SetBufferRect(mFrontBuffer->BufferRect()); + mBuffer->SetBufferRotation(mFrontBuffer->BufferRotation()); + } + } + + return ContentClient::BeginPaint(aLayer, aFlags); +} + +// Sync front/back buffers content +// After executing, the new back buffer has the same (interesting) pixels as +// the new front buffer, and mValidRegion et al. are correct wrt the new +// back buffer (i.e. as they were for the old back buffer) +void ContentClientDoubleBuffered::FinalizeFrame(PaintState& aPaintState) { + if (!mFrontAndBackBufferDiffer) { + MOZ_ASSERT(!mFrontBuffer || !mFrontBuffer->DidSelfCopy(), + "If the front buffer did a self copy then our front and back " + "buffer must be different."); + return; + } + + MOZ_ASSERT(mFrontBuffer && mBuffer); + if (!mFrontBuffer || !mBuffer) { + return; + } + + MOZ_LAYERS_LOG( + ("BasicShadowableThebes(%p): reading back <x=%d,y=%d,w=%d,h=%d>", this, + mFrontUpdatedRegion.GetBounds().X(), mFrontUpdatedRegion.GetBounds().Y(), + mFrontUpdatedRegion.GetBounds().Width(), + mFrontUpdatedRegion.GetBounds().Height())); + + mFrontAndBackBufferDiffer = false; + + nsIntRegion updateRegion = mFrontUpdatedRegion; + if (mFrontBuffer->DidSelfCopy()) { + mFrontBuffer->ClearDidSelfCopy(); + updateRegion = mBuffer->BufferRect(); + } + + // No point in sync'ing what we are going to draw over anyway. And if there is + // nothing to sync at all, there is nothing to do and we can go home early. + updateRegion.Sub(updateRegion, aPaintState.mRegionToDraw); + if (updateRegion.IsEmpty()) { + return; + } + + OpenMode openMode = aPaintState.mAsyncPaint ? OpenMode::OPEN_READ_ASYNC + : OpenMode::OPEN_READ_ONLY; + + if (mFrontBuffer->Lock(openMode)) { + mBuffer->UpdateDestinationFrom(*mFrontBuffer, updateRegion.GetBounds()); + + if (aPaintState.mAsyncPaint) { + aPaintState.mAsyncTask->mClients.AppendElement(mFrontBuffer->GetClient()); + if (mFrontBuffer->GetClientOnWhite()) { + aPaintState.mAsyncTask->mClients.AppendElement( + mFrontBuffer->GetClientOnWhite()); + } + } + + mFrontBuffer->Unlock(); + } +} + +void ContentClientDoubleBuffered::EnsureBackBufferIfFrontBuffer() { + if (!mBuffer && mFrontBuffer) { + mBuffer = CreateBufferInternal(mFrontBuffer->BufferRect(), + mFrontBuffer->GetFormat(), mTextureFlags); + MOZ_ASSERT(mBuffer); + } +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ContentClient.h b/gfx/layers/client/ContentClient.h new file mode 100644 index 0000000000..0763e41b3a --- /dev/null +++ b/gfx/layers/client/ContentClient.h @@ -0,0 +1,362 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_CONTENTCLIENT_H +#define MOZILLA_GFX_CONTENTCLIENT_H + +#include <stdint.h> // for uint32_t +#include "RotatedBuffer.h" // for RotatedBuffer, etc +#include "gfxTypes.h" +#include "gfxPlatform.h" // for gfxPlatform +#include "mozilla/Assertions.h" // for MOZ_CRASH +#include "mozilla/Attributes.h" // for override +#include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor +#include "mozilla/layers/LayersTypes.h" // for TextureDumpMode +#include "mozilla/layers/TextureClient.h" // for TextureClient +#include "mozilla/layers/PaintThread.h" // for PaintTask +#include "mozilla/Maybe.h" // for Maybe +#include "mozilla/mozalloc.h" // for operator delete +#include "mozilla/UniquePtr.h" // for UniquePtr +#include "ReadbackProcessor.h" // For ReadbackProcessor::Update +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsPoint.h" // for nsIntPoint +#include "nsRect.h" // for mozilla::gfx::IntRect +#include "nsRegion.h" // for nsIntRegion +#include "nsTArray.h" // for nsTArray + +namespace mozilla { +namespace gfx { +class DrawTarget; +} // namespace gfx + +namespace layers { + +class PaintedLayer; + +/** + * A compositable client for PaintedLayers. These are different to Image/Canvas + * clients due to sending a valid region across IPC and because we do a lot more + * optimisation work, encapsulated in RotatedBuffers. + * + * We use content clients for OMTC and non-OMTC, basic rendering so that + * BasicPaintedLayer has only one interface to deal with. We support single and + * double buffered flavours. For tiled layers, we do not use a ContentClient + * although we do have a ContentHost, and we do use texture clients and texture + * hosts. + * + * The interface presented by ContentClient is used by the BasicPaintedLayer + * methods - PaintThebes, which is the same for MT and OMTC, and PaintBuffer + * which is different (the OMTC one does a little more). + */ +class ContentClient : public CompositableClient { + public: + typedef gfxContentType ContentType; + + /** + * Creates, configures, and returns a new content client. If necessary, a + * message will be sent to the compositor to create a corresponding content + * host. + */ + static already_AddRefed<ContentClient> CreateContentClient( + CompositableForwarder* aFwd); + + /** + * Controls the size of the backing buffer of this. + * - SizedToVisibleBounds: the backing buffer is exactly the same + * size as the bounds of PaintedLayer's visible region + * - ContainsVisibleBounds: the backing buffer is large enough to + * fit visible bounds. May be larger. + */ + enum BufferSizePolicy { SizedToVisibleBounds, ContainsVisibleBounds }; + + ContentClient(CompositableForwarder* aForwarder, + BufferSizePolicy aBufferSizePolicy) + : CompositableClient(aForwarder), mBufferSizePolicy(aBufferSizePolicy) {} + + virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix); + + virtual void Clear(); + + /** + * This is returned by BeginPaint. The caller should draw into mTarget. + * mRegionToDraw must be drawn. mRegionToInvalidate has been invalidated + * by ContentClient and must be redrawn on the screen. + * mRegionToInvalidate is set when the buffer has changed from + * opaque to transparent or vice versa, since the details of rendering can + * depend on the buffer type. + */ + struct PaintState { + PaintState() + : mRegionToDraw(), + mRegionToInvalidate(), + mMode(SurfaceMode::SURFACE_NONE), + mClip(DrawRegionClip::NONE), + mContentType(gfxContentType::SENTINEL), + mAsyncPaint(false), + mAsyncTask(nullptr) {} + + nsIntRegion mRegionToDraw; + nsIntRegion mRegionToInvalidate; + SurfaceMode mMode; + DrawRegionClip mClip; + gfxContentType mContentType; + bool mAsyncPaint; + UniquePtr<PaintTask> mAsyncTask; + }; + + enum { + PAINT_WILL_RESAMPLE = 0x01, + PAINT_NO_ROTATION = 0x02, + PAINT_CAN_DRAW_ROTATED = 0x04, + PAINT_ASYNC = 0x08, + }; + + /** + * Start a drawing operation. This returns a PaintState describing what + * needs to be drawn to bring the buffer up to date in the visible region. + * This queries aLayer to get the currently valid and visible regions. + * The returned mTarget may be null if mRegionToDraw is empty. + * Otherwise it must not be null. + * mRegionToInvalidate will contain mRegionToDraw. + * @param aFlags when PAINT_WILL_RESAMPLE is passed, this indicates that + * buffer will be resampled when rendering (i.e the effective transform + * combined with the scale for the resolution is not just an integer + * translation). This will disable buffer rotation (since we don't want + * to resample across the rotation boundary) and will ensure that we + * make the entire buffer contents valid (since we don't want to sample + * invalid pixels outside the visible region, if the visible region doesn't + * fill the buffer bounds). + * PAINT_CAN_DRAW_ROTATED can be passed if the caller supports drawing + * rotated content that crosses the physical buffer boundary. The caller + * will need to call BorrowDrawTargetForPainting multiple times to achieve + * this. + */ + virtual PaintState BeginPaint(PaintedLayer* aLayer, uint32_t aFlags); + virtual void EndPaint( + PaintState& aPaintState, + nsTArray<ReadbackProcessor::Update>* aReadbackUpdates = nullptr); + + /** + * Fetch a DrawTarget for rendering. The DrawTarget remains owned by + * this. See notes on BorrowDrawTargetForQuadrantUpdate. + * May return null. If the return value is non-null, it must be + * 'un-borrowed' using ReturnDrawTarget. + * + * If PAINT_CAN_DRAW_ROTATED was specified for BeginPaint, then the caller + * must call this function repeatedly (with an iterator) until it returns + * nullptr. The caller should draw the mDrawRegion of the iterator instead + * of mRegionToDraw in the PaintState. + * + * @param aPaintState Paint state data returned by a call to BeginPaint + * @param aIter Paint state iterator. Only required if PAINT_CAN_DRAW_ROTATED + * was specified to BeginPaint. + */ + virtual gfx::DrawTarget* BorrowDrawTargetForPainting( + PaintState& aPaintState, RotatedBuffer::DrawIterator* aIter = nullptr); + + void ReturnDrawTarget(gfx::DrawTarget*& aReturned); + + enum { + BUFFER_COMPONENT_ALPHA = 0x02 // Dual buffers should be created for drawing + // with component alpha. + }; + + protected: + struct BufferDecision { + nsIntRegion mNeededRegion; + nsIntRegion mValidRegion; + gfx::IntRect mBufferRect; + SurfaceMode mBufferMode; + gfxContentType mBufferContentType; + bool mCanReuseBuffer; + bool mCanKeepBufferContents; + }; + + /** + * Decide whether we can keep our current buffer and its contents, + * and return a struct containing the regions to paint, invalidate, + * the new buffer rect, surface mode, and content type. + */ + BufferDecision CalculateBufferForPaint(PaintedLayer* aLayer, uint32_t aFlags); + + static bool ValidBufferSize(BufferSizePolicy aPolicy, + const gfx::IntSize& aBufferSize, + const gfx::IntSize& aVisibleBoundsSize); + + /** + * Any actions that should be performed at the last moment before we begin + * rendering the next frame. I.e., after we calculate what we will draw, + * but before we rotate the buffer and possibly create new buffers. + * aRegionToDraw is the region which is guaranteed to be overwritten when + * drawing the next frame. + */ + virtual void FinalizeFrame(PaintState& aPaintState) {} + + virtual RefPtr<RotatedBuffer> GetFrontBuffer() const { return mBuffer; } + + /** + * Create a new rotated buffer for the specified content type, buffer rect, + * and buffer flags. + */ + virtual RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType, + const gfx::IntRect& aRect, + uint32_t aFlags) = 0; + + RefPtr<RotatedBuffer> mBuffer; + BufferSizePolicy mBufferSizePolicy; +}; + +// Thin wrapper around DrawTargetRotatedBuffer, for on-mtc +class ContentClientBasic final : public ContentClient { + public: + explicit ContentClientBasic(gfx::BackendType aBackend); + + void DrawTo(PaintedLayer* aLayer, gfx::DrawTarget* aTarget, float aOpacity, + gfx::CompositionOp aOp, gfx::SourceSurface* aMask, + const gfx::Matrix* aMaskTransform); + + TextureInfo GetTextureInfo() const override { + MOZ_CRASH("GFX: Should not be called on non-remote ContentClient"); + } + + protected: + RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType, + const gfx::IntRect& aRect, + uint32_t aFlags) override; + + private: + gfx::BackendType mBackend; +}; + +/** + * A ContentClient backed by a RemoteRotatedBuffer. + * + * When using a ContentClientRemoteBuffer, SurfaceDescriptors are created on + * the rendering side and destroyed on the compositing side. They are only + * passed from one side to the other when the TextureClient/Hosts are created. + * *Ownership* of the SurfaceDescriptor moves from the rendering side to the + * compositing side with the create message (send from CreateBuffer) which + * tells the compositor that TextureClients have been created and that the + * compositor should assign the corresponding TextureHosts to our corresponding + * ContentHost. + * + * If the size or type of our buffer(s) change(s), then we simply destroy and + * create them. + */ +class ContentClientRemoteBuffer : public ContentClient { + public: + explicit ContentClientRemoteBuffer(CompositableForwarder* aForwarder) + : ContentClient(aForwarder, ContainsVisibleBounds), mIsNewBuffer(false) {} + + void Dump(std::stringstream& aStream, const char* aPrefix = "", + bool aDumpHtml = false, + TextureDumpMode aCompress = TextureDumpMode::Compress) override; + + void EndPaint( + PaintState& aPaintState, + nsTArray<ReadbackProcessor::Update>* aReadbackUpdates = nullptr) override; + + void Updated(const nsIntRegion& aRegionToDraw, + const nsIntRegion& aVisibleRegion); + + TextureFlags ExtraTextureFlags() const { + return TextureFlags::IMMEDIATE_UPLOAD; + } + + protected: + /** + * Called when we have been updated and should swap references to our + * buffers. + */ + virtual void SwapBuffers(const nsIntRegion& aFrontUpdatedRegion) {} + + virtual nsIntRegion GetUpdatedRegion(const nsIntRegion& aRegionToDraw, + const nsIntRegion& aVisibleRegion); + + RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType, + const gfx::IntRect& aRect, + uint32_t aFlags) override; + + RefPtr<RotatedBuffer> CreateBufferInternal(const gfx::IntRect& aRect, + gfx::SurfaceFormat aFormat, + TextureFlags aFlags); + + RemoteRotatedBuffer* GetRemoteBuffer() const { + return static_cast<RemoteRotatedBuffer*>(mBuffer.get()); + } + + bool mIsNewBuffer; +}; + +/** + * A double buffered ContentClientRemoteBuffer. mBuffer is the back buffer, + * which we draw into. mFrontBuffer is the front buffer which we may read from, + * but not write to, when the compositor does not have the 'soft' lock. + * + * The ContentHost keeps a reference to both corresponding texture hosts, in + * response to our UpdateTextureRegion message, the compositor swaps its + * references. + */ +class ContentClientDoubleBuffered : public ContentClientRemoteBuffer { + public: + explicit ContentClientDoubleBuffered(CompositableForwarder* aFwd) + : ContentClientRemoteBuffer(aFwd), mFrontAndBackBufferDiffer(false) {} + + void Dump(std::stringstream& aStream, const char* aPrefix = "", + bool aDumpHtml = false, + TextureDumpMode aCompress = TextureDumpMode::Compress) override; + + void Clear() override; + + void SwapBuffers(const nsIntRegion& aFrontUpdatedRegion) override; + + PaintState BeginPaint(PaintedLayer* aLayer, uint32_t aFlags) override; + + void FinalizeFrame(PaintState& aPaintState) override; + + RefPtr<RotatedBuffer> GetFrontBuffer() const override { return mFrontBuffer; } + + TextureInfo GetTextureInfo() const override { + return TextureInfo(CompositableType::CONTENT_DOUBLE, mTextureFlags); + } + + private: + void EnsureBackBufferIfFrontBuffer(); + + RefPtr<RemoteRotatedBuffer> mFrontBuffer; + nsIntRegion mFrontUpdatedRegion; + bool mFrontAndBackBufferDiffer; +}; + +/** + * A single buffered ContentClientRemoteBuffer. We have a single + * TextureClient/Host which we update and then send a message to the + * compositor that we are done updating. It is not safe for the compositor + * to use the corresponding TextureHost's memory directly, it must upload + * it to video memory of some kind. We are free to modify the TextureClient + * once we receive reply from the compositor. + */ +class ContentClientSingleBuffered : public ContentClientRemoteBuffer { + public: + explicit ContentClientSingleBuffered(CompositableForwarder* aFwd) + : ContentClientRemoteBuffer(aFwd) {} + + TextureInfo GetTextureInfo() const override { + return TextureInfo(CompositableType::CONTENT_SINGLE, + mTextureFlags | ExtraTextureFlags()); + } +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/GPUVideoTextureClient.cpp b/gfx/layers/client/GPUVideoTextureClient.cpp new file mode 100644 index 0000000000..8a8f6f6e69 --- /dev/null +++ b/gfx/layers/client/GPUVideoTextureClient.cpp @@ -0,0 +1,58 @@ +/* -*- 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 + * 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 "GPUVideoTextureClient.h" +#include "GPUVideoImage.h" +#include "mozilla/gfx/2D.h" + +namespace mozilla { +namespace layers { + +using namespace gfx; + +GPUVideoTextureData::GPUVideoTextureData(IGPUVideoSurfaceManager* aManager, + const SurfaceDescriptorGPUVideo& aSD, + const gfx::IntSize& aSize) + : mManager(aManager), mSD(aSD), mSize(aSize) {} + +GPUVideoTextureData::~GPUVideoTextureData() = default; + +bool GPUVideoTextureData::Serialize(SurfaceDescriptor& aOutDescriptor) { + aOutDescriptor = mSD; + return true; +} + +void GPUVideoTextureData::FillInfo(TextureData::Info& aInfo) const { + aInfo.size = mSize; + // TODO: We should probably try do better for this. + // layers::Image doesn't expose a format, so it's hard + // to figure out in VideoDecoderParent. + aInfo.format = SurfaceFormat::B8G8R8X8; + aInfo.hasIntermediateBuffer = false; + aInfo.hasSynchronization = false; + aInfo.supportsMoz2D = false; + aInfo.canExposeMappedData = false; +} + +already_AddRefed<SourceSurface> GPUVideoTextureData::GetAsSourceSurface() { + return mManager->Readback(mSD); +} + +void GPUVideoTextureData::Deallocate(LayersIPCChannel* aAllocator) { + mManager->DeallocateSurfaceDescriptor(mSD); + mSD = SurfaceDescriptorGPUVideo(); +} + +void GPUVideoTextureData::Forget(LayersIPCChannel* aAllocator) { + // We always need to manually deallocate on the client side. + // Ideally we'd set up our TextureClient with the DEALLOCATE_CLIENT + // flag, but that forces texture destruction to be synchronous. + // Instead let's just deallocate from here as well. + Deallocate(aAllocator); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/GPUVideoTextureClient.h b/gfx/layers/client/GPUVideoTextureClient.h new file mode 100644 index 0000000000..93b2e761ca --- /dev/null +++ b/gfx/layers/client/GPUVideoTextureClient.h @@ -0,0 +1,55 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_GPUVIDEOTEXTURECLIENT_H +#define MOZILLA_GFX_GPUVIDEOTEXTURECLIENT_H + +#include "mozilla/layers/TextureClient.h" + +namespace mozilla { +namespace gfx { +class SourceSurface; +} + +namespace layers { +class IGPUVideoSurfaceManager; + +class GPUVideoTextureData : public TextureData { + public: + GPUVideoTextureData(IGPUVideoSurfaceManager* aManager, + const SurfaceDescriptorGPUVideo& aSD, + const gfx::IntSize& aSize); + virtual ~GPUVideoTextureData(); + + void FillInfo(TextureData::Info& aInfo) const override; + + bool Lock(OpenMode) override { return true; }; + + void Unlock() override{}; + + bool Serialize(SurfaceDescriptor& aOutDescriptor) override; + + void Deallocate(LayersIPCChannel* aAllocator) override; + + void Forget(LayersIPCChannel* aAllocator) override; + + already_AddRefed<gfx::SourceSurface> GetAsSourceSurface(); + + GPUVideoTextureData* AsGPUVideoTextureData() override { return this; } + + protected: + RefPtr<IGPUVideoSurfaceManager> mManager; + SurfaceDescriptorGPUVideo mSD; + gfx::IntSize mSize; + + public: + const decltype(mSD)& SD() const { return mSD; } +}; + +} // namespace layers +} // namespace mozilla + +#endif // MOZILLA_GFX_GPUVIDEOTEXTURECLIENT_H diff --git a/gfx/layers/client/ImageClient.cpp b/gfx/layers/client/ImageClient.cpp new file mode 100644 index 0000000000..2aed939b9d --- /dev/null +++ b/gfx/layers/client/ImageClient.cpp @@ -0,0 +1,306 @@ +/* -*- 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 + * 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 "ImageClient.h" + +#include <stdint.h> // for uint32_t + +#include "ClientLayerManager.h" // for ClientLayer +#include "ImageContainer.h" // for Image, PlanarYCbCrImage, etc +#include "ImageTypes.h" // for ImageFormat::PLANAR_YCBCR, etc +#include "GLImages.h" // for SurfaceTextureImage::Data, etc +#include "gfx2DGlue.h" // for ImageFormatToSurfaceFormat +#include "gfxPlatform.h" // for gfxPlatform +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed +#include "mozilla/gfx/2D.h" +#include "mozilla/gfx/BaseSize.h" // for BaseSize +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Types.h" // for SurfaceFormat, etc +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/CompositorTypes.h" // for CompositableType, etc +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc +#include "mozilla/layers/ShadowLayers.h" // for ShadowLayerForwarder +#include "mozilla/layers/TextureClient.h" // for TextureClient, etc +#include "mozilla/layers/TextureClientOGL.h" // for SurfaceTextureClient +#include "mozilla/mozalloc.h" // for operator delete, etc +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsDebug.h" // for NS_WARNING, NS_ASSERTION +#include "nsISupportsImpl.h" // for Image::Release, etc +#include "nsRect.h" // for mozilla::gfx::IntRect + +namespace mozilla { +namespace layers { + +using namespace mozilla::gfx; + +/* static */ +already_AddRefed<ImageClient> ImageClient::CreateImageClient( + CompositableType aCompositableHostType, CompositableForwarder* aForwarder, + TextureFlags aFlags) { + RefPtr<ImageClient> result = nullptr; + switch (aCompositableHostType) { + case CompositableType::IMAGE: + result = + new ImageClientSingle(aForwarder, aFlags, CompositableType::IMAGE); + break; + case CompositableType::IMAGE_BRIDGE: + result = new ImageClientBridge(aForwarder, aFlags); + break; + case CompositableType::UNKNOWN: + result = nullptr; + break; + default: + MOZ_CRASH("GFX: unhandled program type image"); + } + + NS_ASSERTION(result, "Failed to create ImageClient"); + + return result.forget(); +} + +void ImageClient::RemoveTexture(TextureClient* aTexture) { + GetForwarder()->RemoveTextureFromCompositable(this, aTexture); +} + +ImageClientSingle::ImageClientSingle(CompositableForwarder* aFwd, + TextureFlags aFlags, + CompositableType aType) + : ImageClient(aFwd, aFlags, aType) {} + +TextureInfo ImageClientSingle::GetTextureInfo() const { + return TextureInfo(CompositableType::IMAGE); +} + +void ImageClientSingle::FlushAllImages() { + for (auto& b : mBuffers) { + // It should be safe to just assume a default render root here, even if + // the texture actually presents in a content render root, as the only + // risk would be if the content render root has not / is not going to + // generate a frame before the texture gets cleared. + RemoveTexture(b.mTextureClient); + } + mBuffers.Clear(); +} + +/* static */ +already_AddRefed<TextureClient> ImageClient::CreateTextureClientForImage( + Image* aImage, KnowsCompositor* aKnowsCompositor) { + RefPtr<TextureClient> texture; + if (aImage->GetFormat() == ImageFormat::PLANAR_YCBCR) { + PlanarYCbCrImage* ycbcr = static_cast<PlanarYCbCrImage*>(aImage); + const PlanarYCbCrData* data = ycbcr->GetData(); + if (!data) { + return nullptr; + } + texture = TextureClient::CreateForYCbCr( + aKnowsCompositor, data->GetPictureRect(), data->mYSize, data->mYStride, + data->mCbCrSize, data->mCbCrStride, data->mStereoMode, + data->mColorDepth, data->mYUVColorSpace, data->mColorRange, + TextureFlags::DEFAULT); + if (!texture) { + return nullptr; + } + + TextureClientAutoLock autoLock(texture, OpenMode::OPEN_WRITE_ONLY); + if (!autoLock.Succeeded()) { + return nullptr; + } + + bool status = UpdateYCbCrTextureClient(texture, *data); + MOZ_ASSERT(status); + if (!status) { + return nullptr; + } +#ifdef MOZ_WIDGET_ANDROID + } else if (aImage->GetFormat() == ImageFormat::SURFACE_TEXTURE) { + gfx::IntSize size = aImage->GetSize(); + SurfaceTextureImage* typedImage = aImage->AsSurfaceTextureImage(); + texture = AndroidSurfaceTextureData::CreateTextureClient( + typedImage->GetHandle(), size, typedImage->GetContinuous(), + typedImage->GetOriginPos(), typedImage->GetHasAlpha(), + aKnowsCompositor->GetTextureForwarder(), TextureFlags::DEFAULT); +#endif + } else { + RefPtr<gfx::SourceSurface> surface = aImage->GetAsSourceSurface(); + MOZ_ASSERT(surface); + texture = TextureClient::CreateForDrawing( + aKnowsCompositor, surface->GetFormat(), aImage->GetSize(), + BackendSelector::Content, TextureFlags::DEFAULT); + if (!texture) { + return nullptr; + } + + MOZ_ASSERT(texture->CanExposeDrawTarget()); + + if (!texture->Lock(OpenMode::OPEN_WRITE_ONLY)) { + return nullptr; + } + + { + // We must not keep a reference to the DrawTarget after it has been + // unlocked. + DrawTarget* dt = texture->BorrowDrawTarget(); + if (!dt) { + gfxWarning() + << "ImageClientSingle::UpdateImage failed in BorrowDrawTarget"; + return nullptr; + } + MOZ_ASSERT(surface.get()); + dt->CopySurface(surface, IntRect(IntPoint(), surface->GetSize()), + IntPoint()); + } + + texture->Unlock(); + } + return texture.forget(); +} + +bool ImageClientSingle::UpdateImage(ImageContainer* aContainer, + uint32_t aContentFlags) { + AutoTArray<ImageContainer::OwningImage, 4> images; + uint32_t generationCounter; + aContainer->GetCurrentImages(&images, &generationCounter); + + if (mLastUpdateGenerationCounter == generationCounter) { + return true; + } + mLastUpdateGenerationCounter = generationCounter; + + // Don't try to update to invalid images. + images.RemoveElementsBy( + [](const auto& image) { return !image.mImage->IsValid(); }); + if (images.IsEmpty()) { + // This can happen if a ClearAllImages raced with SetCurrentImages from + // another thread and ClearImagesFromImageBridge ran after the + // SetCurrentImages call but before UpdateImageClientNow. + // This can also happen if all images in the list are invalid. + // We return true because the caller would attempt to recreate the + // ImageClient otherwise, and that isn't going to help. + for (auto& b : mBuffers) { + RemoveTexture(b.mTextureClient); + } + mBuffers.Clear(); + return true; + } + + nsTArray<Buffer> newBuffers; + AutoTArray<CompositableForwarder::TimedTextureClient, 4> textures; + + for (auto& img : images) { + Image* image = img.mImage; + + RefPtr<TextureClient> texture = image->GetTextureClient(GetForwarder()); + const bool hasTextureClient = !!texture; + + for (int32_t i = mBuffers.Length() - 1; i >= 0; --i) { + if (mBuffers[i].mImageSerial == image->GetSerial()) { + if (hasTextureClient) { + MOZ_ASSERT(image->GetTextureClient(GetForwarder()) == + mBuffers[i].mTextureClient); + } else { + texture = mBuffers[i].mTextureClient; + } + // Remove this element from mBuffers so mBuffers only contains + // images that aren't present in 'images' + mBuffers.RemoveElementAt(i); + } + } + + if (!texture) { + // Slow path, we should not be hitting it very often and if we do it means + // we are using an Image class that is not backed by textureClient and we + // should fix it. + texture = CreateTextureClientForImage(image, GetForwarder()); + } + + if (!texture) { + return false; + } + + // We check if the texture's allocator is still open, since in between media + // decoding a frame and adding it to the compositable, we could have + // restarted the GPU process. + if (!texture->GetAllocator()->IPCOpen()) { + continue; + } + if (!AddTextureClient(texture)) { + return false; + } + + CompositableForwarder::TimedTextureClient* t = textures.AppendElement(); + t->mTextureClient = texture; + t->mTimeStamp = img.mTimeStamp; + t->mPictureRect = image->GetPictureRect(); + t->mFrameID = img.mFrameID; + t->mProducerID = img.mProducerID; + + Buffer* newBuf = newBuffers.AppendElement(); + newBuf->mImageSerial = image->GetSerial(); + newBuf->mTextureClient = texture; + + texture->SyncWithObject(GetForwarder()->GetSyncObject()); + } + + GetForwarder()->UseTextures(this, textures); + + for (auto& b : mBuffers) { + RemoveTexture(b.mTextureClient); + } + mBuffers = std::move(newBuffers); + + return true; +} + +RefPtr<TextureClient> ImageClientSingle::GetForwardedTexture() { + if (mBuffers.Length() == 0) { + return nullptr; + } + return mBuffers[0].mTextureClient; +} + +bool ImageClientSingle::AddTextureClient(TextureClient* aTexture) { + MOZ_ASSERT((mTextureFlags & aTexture->GetFlags()) == mTextureFlags); + return CompositableClient::AddTextureClient(aTexture); +} + +void ImageClientSingle::OnDetach() { mBuffers.Clear(); } + +ImageClient::ImageClient(CompositableForwarder* aFwd, TextureFlags aFlags, + CompositableType aType) + : CompositableClient(aFwd, aFlags), + mLayer(nullptr), + mType(aType), + mLastUpdateGenerationCounter(0) {} + +ImageClientBridge::ImageClientBridge(CompositableForwarder* aFwd, + TextureFlags aFlags) + : ImageClient(aFwd, aFlags, CompositableType::IMAGE_BRIDGE) {} + +bool ImageClientBridge::UpdateImage(ImageContainer* aContainer, + uint32_t aContentFlags) { + if (!GetForwarder() || !mLayer) { + return false; + } + if (mAsyncContainerHandle == aContainer->GetAsyncContainerHandle()) { + return true; + } + + mAsyncContainerHandle = aContainer->GetAsyncContainerHandle(); + if (!mAsyncContainerHandle) { + // If we couldn't contact a working ImageBridgeParent, just return. + return true; + } + + static_cast<ShadowLayerForwarder*>(GetForwarder()) + ->AttachAsyncCompositable(mAsyncContainerHandle, mLayer); + return true; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/ImageClient.h b/gfx/layers/client/ImageClient.h new file mode 100644 index 0000000000..0e699179c8 --- /dev/null +++ b/gfx/layers/client/ImageClient.h @@ -0,0 +1,143 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_IMAGECLIENT_H +#define MOZILLA_GFX_IMAGECLIENT_H + +#include <stdint.h> // for uint32_t, uint64_t +#include <sys/types.h> // for int32_t +#include "mozilla/Attributes.h" // for override +#include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed +#include "mozilla/gfx/Types.h" // for SurfaceFormat +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/CompositorTypes.h" // for CompositableType, etc +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor +#include "mozilla/layers/TextureClient.h" // for TextureClient, etc +#include "mozilla/mozalloc.h" // for operator delete +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsRect.h" // for mozilla::gfx::IntRect + +namespace mozilla { +namespace layers { + +class ClientLayer; +class CompositableForwarder; +class Image; +class ImageContainer; +class ShadowableLayer; +class ImageClientSingle; + +/** + * Image clients are used by basic image layers on the content thread, they + * always match with an ImageHost on the compositor thread. See + * CompositableClient.h for information on connecting clients to hosts. + */ +class ImageClient : public CompositableClient { + public: + /** + * Creates, configures, and returns a new image client. If necessary, a + * message will be sent to the compositor to create a corresponding image + * host. + */ + static already_AddRefed<ImageClient> CreateImageClient( + CompositableType aImageHostType, CompositableForwarder* aFwd, + TextureFlags aFlags); + + virtual ~ImageClient() = default; + + /** + * Update this ImageClient from aContainer in aLayer + * returns false if this is the wrong kind of ImageClient for aContainer. + * Note that returning true does not necessarily imply success + */ + virtual bool UpdateImage(ImageContainer* aContainer, + uint32_t aContentFlags) = 0; + + void SetLayer(ClientLayer* aLayer) { mLayer = aLayer; } + ClientLayer* GetLayer() const { return mLayer; } + + /** + * asynchronously remove all the textures used by the image client. + * + */ + virtual void FlushAllImages() {} + + virtual void RemoveTexture(TextureClient* aTexture) override; + + virtual ImageClientSingle* AsImageClientSingle() { return nullptr; } + + static already_AddRefed<TextureClient> CreateTextureClientForImage( + Image* aImage, KnowsCompositor* aForwarder); + + uint32_t GetLastUpdateGenerationCounter() { + return mLastUpdateGenerationCounter; + } + + virtual RefPtr<TextureClient> GetForwardedTexture() { return nullptr; } + + protected: + ImageClient(CompositableForwarder* aFwd, TextureFlags aFlags, + CompositableType aType); + + ClientLayer* mLayer; + CompositableType mType; + uint32_t mLastUpdateGenerationCounter; +}; + +/** + * An image client which uses a single texture client. + */ +class ImageClientSingle : public ImageClient { + public: + ImageClientSingle(CompositableForwarder* aFwd, TextureFlags aFlags, + CompositableType aType); + + bool UpdateImage(ImageContainer* aContainer, uint32_t aContentFlag) override; + + void OnDetach() override; + + bool AddTextureClient(TextureClient* aTexture) override; + + TextureInfo GetTextureInfo() const override; + + void FlushAllImages() override; + + ImageClientSingle* AsImageClientSingle() override { return this; } + + RefPtr<TextureClient> GetForwardedTexture() override; + + bool IsEmpty() { return mBuffers.IsEmpty(); } + + protected: + struct Buffer { + RefPtr<TextureClient> mTextureClient; + int32_t mImageSerial; + }; + nsTArray<Buffer> mBuffers; +}; + +/** + * Image class to be used for async image uploads using the image bridge + * protocol. + * We store the ImageBridge id in the TextureClientIdentifier. + */ +class ImageClientBridge : public ImageClient { + public: + ImageClientBridge(CompositableForwarder* aFwd, TextureFlags aFlags); + + bool UpdateImage(ImageContainer* aContainer, uint32_t aContentFlags) override; + bool Connect(ImageContainer* aImageContainer) override { return false; } + + TextureInfo GetTextureInfo() const override { return TextureInfo(mType); } + + protected: + CompositableHandle mAsyncContainerHandle; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/MultiTiledContentClient.cpp b/gfx/layers/client/MultiTiledContentClient.cpp new file mode 100644 index 0000000000..2ab5c83e39 --- /dev/null +++ b/gfx/layers/client/MultiTiledContentClient.cpp @@ -0,0 +1,685 @@ +/* -*- 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 + * 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 "mozilla/layers/MultiTiledContentClient.h" + +#include "ClientTiledPaintedLayer.h" +#include "mozilla/StaticPrefs_layers.h" +#include "mozilla/layers/APZUtils.h" +#include "mozilla/layers/LayerMetricsWrapper.h" + +namespace mozilla { + +using namespace gfx; + +namespace layers { + +MultiTiledContentClient::MultiTiledContentClient( + ClientTiledPaintedLayer& aPaintedLayer, ClientLayerManager* aManager) + : TiledContentClient(aManager, "Multi"), + mTiledBuffer(aPaintedLayer, *this, aManager, &mSharedFrameMetricsHelper), + mLowPrecisionTiledBuffer(aPaintedLayer, *this, aManager, + &mSharedFrameMetricsHelper) { + MOZ_COUNT_CTOR(MultiTiledContentClient); + mLowPrecisionTiledBuffer.SetResolution( + StaticPrefs::layers_low_precision_resolution()); + mHasLowPrecision = StaticPrefs::layers_low_precision_buffer(); +} + +void MultiTiledContentClient::ClearCachedResources() { + CompositableClient::ClearCachedResources(); + mTiledBuffer.DiscardBuffers(); + mLowPrecisionTiledBuffer.DiscardBuffers(); +} + +void MultiTiledContentClient::UpdatedBuffer(TiledBufferType aType) { + ClientMultiTiledLayerBuffer* buffer = aType == LOW_PRECISION_TILED_BUFFER + ? &mLowPrecisionTiledBuffer + : &mTiledBuffer; + + MOZ_ASSERT(aType != LOW_PRECISION_TILED_BUFFER || mHasLowPrecision); + + mForwarder->UseTiledLayerBuffer(this, buffer->GetSurfaceDescriptorTiles()); +} + +ClientMultiTiledLayerBuffer::ClientMultiTiledLayerBuffer( + ClientTiledPaintedLayer& aPaintedLayer, + CompositableClient& aCompositableClient, ClientLayerManager* aManager, + SharedFrameMetricsHelper* aHelper) + : ClientTiledLayerBuffer(aPaintedLayer, aCompositableClient), + mManager(aManager), + mCallback(nullptr), + mCallbackData(nullptr), + mSharedFrameMetricsHelper(aHelper) {} + +void ClientMultiTiledLayerBuffer::DiscardBuffers() { + for (TileClient& tile : mRetainedTiles) { + tile.DiscardBuffers(); + } +} + +SurfaceDescriptorTiles +ClientMultiTiledLayerBuffer::GetSurfaceDescriptorTiles() { + nsTArray<TileDescriptor> tiles; + + for (TileClient& tile : mRetainedTiles) { + TileDescriptor tileDesc = tile.GetTileDescriptor(); + tiles.AppendElement(tileDesc); + // Reset the update rect + tile.mUpdateRect = IntRect(); + } + return SurfaceDescriptorTiles( + mValidRegion, tiles, mTileOrigin, mTileSize, mTiles.mFirst.x, + mTiles.mFirst.y, mTiles.mSize.width, mTiles.mSize.height, mResolution, + mFrameResolution.xScale, mFrameResolution.yScale, + mWasLastPaintProgressive); +} + +void ClientMultiTiledLayerBuffer::PaintThebes( + const nsIntRegion& aNewValidRegion, const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData, + TilePaintFlags aFlags) { + TILING_LOG("TILING %p: PaintThebes painting region %s\n", &mPaintedLayer, + Stringify(aPaintRegion).c_str()); + TILING_LOG("TILING %p: PaintThebes new valid region %s\n", &mPaintedLayer, + Stringify(aNewValidRegion).c_str()); + + mCallback = aCallback; + mCallbackData = aCallbackData; + mWasLastPaintProgressive = !!(aFlags & TilePaintFlags::Progressive); + +#ifdef GFX_TILEDLAYER_PREF_WARNINGS + long start = PR_IntervalNow(); +#endif + +#ifdef GFX_TILEDLAYER_PREF_WARNINGS + if (PR_IntervalNow() - start > 30) { + const IntRect bounds = aPaintRegion.GetBounds(); + printf_stderr("Time to draw %i: %i, %i, %i, %i\n", PR_IntervalNow() - start, + bounds.x, bounds.y, bounds.width, bounds.height); + if (aPaintRegion.IsComplex()) { + printf_stderr("Complex region\n"); + for (auto iter = aPaintRegion.RectIter(); !iter.Done(); iter.Next()) { + const IntRect& rect = iter.Get(); + printf_stderr(" rect %i, %i, %i, %i\n", rect.x, rect.y, rect.width, + rect.height); + } + } + } + start = PR_IntervalNow(); +#endif + + AUTO_PROFILER_LABEL("ClientMultiTiledLayerBuffer::PaintThebes", GRAPHICS); + + mNewValidRegion = aNewValidRegion; + Update(aNewValidRegion, aPaintRegion, aDirtyRegion, aFlags); + +#ifdef GFX_TILEDLAYER_PREF_WARNINGS + if (PR_IntervalNow() - start > 10) { + const IntRect bounds = aPaintRegion.GetBounds(); + printf_stderr("Time to tile %i: %i, %i, %i, %i\n", PR_IntervalNow() - start, + bounds.x, bounds.y, bounds.width, bounds.height); + } +#endif + + mLastPaintContentType = GetContentType(&mLastPaintSurfaceMode); + mCallback = nullptr; + mCallbackData = nullptr; +} + +void ClientMultiTiledLayerBuffer::MaybeSyncTextures( + const nsIntRegion& aPaintRegion, const TilesPlacement& aNewTiles, + const IntSize& aScaledTileSize) { + if (mManager->AsShadowForwarder()->SupportsTextureDirectMapping()) { + AutoTArray<uint64_t, 10> syncTextureSerials; + SurfaceMode mode; + Unused << GetContentType(&mode); + + // Pre-pass through the tiles (mirroring the filter logic below) to gather + // texture IDs that we need to ensure are unused by the GPU before we + // continue. + if (!aPaintRegion.IsEmpty()) { + MOZ_ASSERT(mPaintTasks.IsEmpty()); + for (size_t i = 0; i < mRetainedTiles.Length(); ++i) { + const TileCoordIntPoint tileCoord = aNewTiles.TileCoord(i); + + IntPoint tileOffset = GetTileOffset(tileCoord); + nsIntRegion tileDrawRegion = IntRect(tileOffset, aScaledTileSize); + tileDrawRegion.AndWith(aPaintRegion); + + if (tileDrawRegion.IsEmpty()) { + continue; + } + + TileClient& tile = mRetainedTiles[i]; + tile.GetSyncTextureSerials(mode, syncTextureSerials); + } + } + + if (syncTextureSerials.Length() > 0) { + mManager->AsShadowForwarder()->SyncTextures(syncTextureSerials); + } + } +} + +void ClientMultiTiledLayerBuffer::Update(const nsIntRegion& newValidRegion, + const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + TilePaintFlags aFlags) { + const IntSize scaledTileSize = GetScaledTileSize(); + const gfx::IntRect newBounds = newValidRegion.GetBounds(); + + const TilesPlacement oldTiles = mTiles; + const TilesPlacement newTiles( + floor_div(newBounds.X(), scaledTileSize.width), + floor_div(newBounds.Y(), scaledTileSize.height), + floor_div( + GetTileStart(newBounds.X(), scaledTileSize.width) + newBounds.Width(), + scaledTileSize.width) + + 1, + floor_div(GetTileStart(newBounds.Y(), scaledTileSize.height) + + newBounds.Height(), + scaledTileSize.height) + + 1); + + const size_t oldTileCount = mRetainedTiles.Length(); + const size_t newTileCount = newTiles.mSize.width * newTiles.mSize.height; + + nsTArray<TileClient> oldRetainedTiles = std::move(mRetainedTiles); + mRetainedTiles.SetLength(newTileCount); + + for (size_t oldIndex = 0; oldIndex < oldTileCount; oldIndex++) { + const TileCoordIntPoint tileCoord = oldTiles.TileCoord(oldIndex); + const size_t newIndex = newTiles.TileIndex(tileCoord); + // First, get the already existing tiles to the right place in the new + // array. Leave placeholders (default constructor) where there was no tile. + if (newTiles.HasTile(tileCoord)) { + mRetainedTiles[newIndex] = oldRetainedTiles[oldIndex]; + } else { + // release tiles that we are not going to reuse before allocating new ones + // to avoid allocating unnecessarily. + oldRetainedTiles[oldIndex].DiscardBuffers(); + } + } + + oldRetainedTiles.Clear(); + + nsIntRegion paintRegion = aPaintRegion; + nsIntRegion dirtyRegion = aDirtyRegion; + + MaybeSyncTextures(paintRegion, newTiles, scaledTileSize); + + if (!paintRegion.IsEmpty()) { + MOZ_ASSERT(mPaintTasks.IsEmpty()); + + for (size_t i = 0; i < newTileCount; ++i) { + const TileCoordIntPoint tileCoord = newTiles.TileCoord(i); + + IntPoint tileOffset = GetTileOffset(tileCoord); + nsIntRegion tileDrawRegion = IntRect(tileOffset, scaledTileSize); + tileDrawRegion.AndWith(paintRegion); + + if (tileDrawRegion.IsEmpty()) { + continue; + } + + TileClient& tile = mRetainedTiles[i]; + if (!ValidateTile(tile, GetTileOffset(tileCoord), tileDrawRegion, + aFlags)) { + gfxCriticalError() << "ValidateTile failed"; + } + + // Validating the tile may have required more to be painted. + paintRegion.OrWith(tileDrawRegion); + dirtyRegion.OrWith(tileDrawRegion); + } + + if (!mPaintTiles.IsEmpty()) { + // Create a tiled draw target + gfx::TileSet tileset; + for (size_t i = 0; i < mPaintTiles.Length(); ++i) { + mPaintTiles[i].mTileOrigin -= mTilingOrigin; + } + tileset.mTiles = mPaintTiles.Elements(); + tileset.mTileCount = mPaintTiles.Length(); + RefPtr<DrawTarget> drawTarget = + gfx::Factory::CreateTiledDrawTarget(tileset); + if (!drawTarget || !drawTarget->IsValid()) { + gfxDevCrash(LogReason::InvalidContext) << "Invalid tiled draw target"; + return; + } + drawTarget->SetTransform(Matrix()); + + // Draw into the tiled draw target + RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(drawTarget); + MOZ_ASSERT(ctx); // already checked the draw target above + ctx->SetMatrix(ctx->CurrentMatrix() + .PreScale(mResolution, mResolution) + .PreTranslate(-mTilingOrigin)); + + mCallback(&mPaintedLayer, ctx, paintRegion, dirtyRegion, + DrawRegionClip::DRAW, nsIntRegion(), mCallbackData); + ctx = nullptr; + + // Edge padding allows us to avoid resampling artifacts + if (StaticPrefs::layers_tiles_edge_padding_AtStartup() && + mResolution == 1) { + drawTarget->PadEdges(newValidRegion.MovedBy(-mTilingOrigin)); + } + + // Reset + mPaintTiles.Clear(); + mTilingOrigin = IntPoint(std::numeric_limits<int32_t>::max(), + std::numeric_limits<int32_t>::max()); + } + + // Dispatch to the paint thread + if (aFlags & TilePaintFlags::Async) { + bool queuedTask = false; + + while (!mPaintTasks.IsEmpty()) { + UniquePtr<PaintTask> task = mPaintTasks.PopLastElement(); + if (!task->mCapture->IsEmpty()) { + PaintThread::Get()->QueuePaintTask(std::move(task)); + queuedTask = true; + } + } + + if (queuedTask) { + mManager->SetQueuedAsyncPaints(); + } + + mPaintTasks.Clear(); + } + + for (uint32_t i = 0; i < mRetainedTiles.Length(); ++i) { + TileClient& tile = mRetainedTiles[i]; + UnlockTile(tile); + } + } + + mTiles = newTiles; + mValidRegion = newValidRegion; +} + +bool ClientMultiTiledLayerBuffer::ValidateTile(TileClient& aTile, + const nsIntPoint& aTileOrigin, + nsIntRegion& aDirtyRegion, + TilePaintFlags aFlags) { +#ifdef GFX_TILEDLAYER_PREF_WARNINGS + if (aDirtyRegion.IsComplex()) { + printf_stderr("Complex region\n"); + } +#endif + + SurfaceMode mode; + gfxContentType content = GetContentType(&mode); + + if (!aTile.mAllocator) { + aTile.SetTextureAllocator( + mManager->GetCompositorBridgeChild()->GetTexturePool( + mManager->AsShadowForwarder(), + gfxPlatform::GetPlatform()->Optimal2DFormatForContent(content), + TextureFlags::DISALLOW_BIGIMAGE | TextureFlags::IMMEDIATE_UPLOAD | + TextureFlags::NON_BLOCKING_READ_LOCK)); + MOZ_ASSERT(aTile.mAllocator); + } + + nsIntRegion tileDirtyRegion = aDirtyRegion.MovedBy(-aTileOrigin); + tileDirtyRegion.ScaleRoundOut(mResolution, mResolution); + + nsIntRegion tileVisibleRegion = mNewValidRegion.MovedBy(-aTileOrigin); + tileVisibleRegion.ScaleRoundOut(mResolution, mResolution); + + std::vector<RefPtr<TextureClient>> asyncPaintClients; + + Maybe<AcquiredBackBuffer> backBuffer = + aTile.AcquireBackBuffer(mCompositableClient, tileDirtyRegion, + tileVisibleRegion, content, mode, aFlags); + + if (!backBuffer) { + return false; + } + + // Mark the area we need to paint in the back buffer as invalid in the + // front buffer as they will become out of sync. + aTile.mInvalidFront.OrWith(tileDirtyRegion); + + // Add the backbuffer's invalid region intersected with the visible region to + // the dirty region we will be painting. This will be empty if we are able to + // copy from the front into the back. + nsIntRegion tileInvalidRegion = aTile.mInvalidBack; + tileInvalidRegion.AndWith(tileVisibleRegion); + + nsIntRegion invalidRegion = tileInvalidRegion; + invalidRegion.MoveBy(aTileOrigin); + invalidRegion.ScaleInverseRoundOut(mResolution, mResolution); + + tileDirtyRegion.OrWith(tileInvalidRegion); + aDirtyRegion.OrWith(invalidRegion); + + // Mark the region we will be painting and the region we copied from the front + // buffer as needing to be uploaded to the compositor + aTile.mUpdateRect = + tileDirtyRegion.GetBounds().Union(backBuffer->mUpdatedRect); + + // We need to clear the dirty region of the tile before painting + // if we are painting non-opaque content + if (mode != SurfaceMode::SURFACE_OPAQUE) { + for (auto iter = tileDirtyRegion.RectIter(); !iter.Done(); iter.Next()) { + const gfx::Rect drawRect(iter.Get().X(), iter.Get().Y(), + iter.Get().Width(), iter.Get().Height()); + backBuffer->mTarget->ClearRect(drawRect); + } + } + + gfx::Tile paintTile; + paintTile.mTileOrigin = gfx::IntPoint(aTileOrigin.x, aTileOrigin.y); + paintTile.mDrawTarget = backBuffer->mTarget; + mPaintTiles.AppendElement(paintTile); + + if (aFlags & TilePaintFlags::Async) { + UniquePtr<PaintTask> task(new PaintTask()); + task->mCapture = backBuffer->mCapture; + task->mTarget = backBuffer->mBackBuffer; + task->mClients = std::move(backBuffer->mTextureClients); + mPaintTasks.AppendElement(std::move(task)); + } else { + MOZ_RELEASE_ASSERT(backBuffer->mTarget == backBuffer->mBackBuffer); + MOZ_RELEASE_ASSERT(backBuffer->mCapture == nullptr); + } + + mTilingOrigin.x = std::min(mTilingOrigin.x, paintTile.mTileOrigin.x); + mTilingOrigin.y = std::min(mTilingOrigin.y, paintTile.mTileOrigin.y); + + // The new buffer is now validated, remove the dirty region from it. + aTile.mInvalidBack.SubOut(tileDirtyRegion); + + aTile.Flip(); + + return true; +} + +/** + * This function takes the transform stored in aTransformToCompBounds + * (which was generated in GetTransformToAncestorsParentLayer), and + * modifies it with the ViewTransform from the compositor side so that + * it reflects what the compositor is actually rendering. This operation + * basically adds in the layer's async transform. + * This function then returns the scroll ancestor's composition bounds, + * transformed into the painted layer's LayerPixel coordinates, accounting + * for the compositor state. + */ +static Maybe<LayerRect> GetCompositorSideCompositionBounds( + const LayerMetricsWrapper& aScrollAncestor, + const LayerToParentLayerMatrix4x4& aTransformToCompBounds, + const AsyncTransform& aAPZTransform, const LayerRect& aClip) { + LayerToParentLayerMatrix4x4 transform = + aTransformToCompBounds * AsyncTransformComponentMatrix(aAPZTransform); + + return UntransformBy(transform.Inverse(), + aScrollAncestor.Metrics().GetCompositionBounds(), aClip); +} + +bool ClientMultiTiledLayerBuffer::ComputeProgressiveUpdateRegion( + const nsIntRegion& aInvalidRegion, const nsIntRegion& aOldValidRegion, + nsIntRegion& aRegionToPaint, BasicTiledLayerPaintData* aPaintData, + bool aIsRepeated) { + aRegionToPaint = aInvalidRegion; + + // If the composition bounds rect is empty, we can't make any sensible + // decision about how to update coherently. In this case, just update + // everything in one transaction. + if (aPaintData->mCompositionBounds.IsEmpty()) { + aPaintData->mPaintFinished = true; + return false; + } + + // If this is a low precision buffer, we force progressive updates. The + // assumption is that the contents is less important, so visual coherency + // is lower priority than speed. + bool drawingLowPrecision = IsLowPrecision(); + + // Find out if we have any non-stale content to update. + nsIntRegion staleRegion; + staleRegion.And(aInvalidRegion, aOldValidRegion); + + TILING_LOG("TILING %p: Progressive update stale region %s\n", &mPaintedLayer, + Stringify(staleRegion).c_str()); + + LayerMetricsWrapper scrollAncestor; + mPaintedLayer.GetAncestorLayers(&scrollAncestor, nullptr, nullptr); + + // Find out the current view transform to determine which tiles to draw + // first, and see if we should just abort this paint. Aborting is usually + // caused by there being an incoming, more relevant paint. + AsyncTransform viewTransform; + MOZ_ASSERT(mSharedFrameMetricsHelper); + + bool abortPaint = mSharedFrameMetricsHelper->UpdateFromCompositorFrameMetrics( + scrollAncestor, !staleRegion.Contains(aInvalidRegion), + drawingLowPrecision, viewTransform); + + TILING_LOG( + "TILING %p: Progressive update view transform %s zoom %f abort %d\n", + &mPaintedLayer, ToString(viewTransform.mTranslation).c_str(), + viewTransform.mScale.scale, abortPaint); + + if (abortPaint) { + // We ignore if front-end wants to abort if this is the first, + // non-low-precision paint, as in that situation, we're about to override + // front-end's page/viewport metrics. + if (!aPaintData->mFirstPaint || drawingLowPrecision) { + AUTO_PROFILER_LABEL( + "ClientMultiTiledLayerBuffer::ComputeProgressiveUpdateRegion", + GRAPHICS); + + aRegionToPaint.SetEmpty(); + return aIsRepeated; + } + } + + Maybe<LayerRect> transformedCompositionBounds = + GetCompositorSideCompositionBounds( + scrollAncestor, aPaintData->mTransformToCompBounds, viewTransform, + LayerRect(mPaintedLayer.GetVisibleRegion().GetBounds())); + + if (!transformedCompositionBounds) { + aPaintData->mPaintFinished = true; + return false; + } + + TILING_LOG("TILING %p: Progressive update transformed compositor bounds %s\n", + &mPaintedLayer, Stringify(*transformedCompositionBounds).c_str()); + + // Compute a "coherent update rect" that we should paint all at once in a + // single transaction. This is to avoid rendering glitches on animated + // page content, and when layers change size/shape. + // On Fennec uploads are more expensive because we're not using gralloc, so + // we use a coherent update rect that is intersected with the screen at the + // time of issuing the draw command. This will paint faster but also + // potentially make the progressive paint more visible to the user while + // scrolling. + IntRect coherentUpdateRect(RoundedOut( +#ifdef MOZ_WIDGET_ANDROID + transformedCompositionBounds->Intersect( + aPaintData->mCompositionBounds) +#else + *transformedCompositionBounds +#endif + ) + .ToUnknownRect()); + + TILING_LOG("TILING %p: Progressive update final coherency rect %s\n", + &mPaintedLayer, Stringify(coherentUpdateRect).c_str()); + + aRegionToPaint.And(aInvalidRegion, coherentUpdateRect); + aRegionToPaint.Or(aRegionToPaint, staleRegion); + bool drawingStale = !aRegionToPaint.IsEmpty(); + if (!drawingStale) { + aRegionToPaint = aInvalidRegion; + } + + // Prioritise tiles that are currently visible on the screen. + bool paintingVisible = false; + if (aRegionToPaint.Intersects(coherentUpdateRect)) { + aRegionToPaint.And(aRegionToPaint, coherentUpdateRect); + paintingVisible = true; + } + + TILING_LOG("TILING %p: Progressive update final paint region %s\n", + &mPaintedLayer, Stringify(aRegionToPaint).c_str()); + + // Paint area that's visible and overlaps previously valid content to avoid + // visible glitches in animated elements, such as gifs. + bool paintInSingleTransaction = + paintingVisible && (drawingStale || aPaintData->mFirstPaint); + + TILING_LOG( + "TILING %p: paintingVisible %d drawingStale %d firstPaint %d " + "singleTransaction %d\n", + &mPaintedLayer, paintingVisible, drawingStale, aPaintData->mFirstPaint, + paintInSingleTransaction); + + // The following code decides what order to draw tiles in, based on the + // current scroll direction of the primary scrollable layer. + NS_ASSERTION(!aRegionToPaint.IsEmpty(), "Unexpectedly empty paint region!"); + IntRect paintBounds = aRegionToPaint.GetBounds(); + + int startX, incX, startY, incY; + gfx::IntSize scaledTileSize = GetScaledTileSize(); + if (aPaintData->mScrollOffset.x >= aPaintData->mLastScrollOffset.x) { + startX = RoundDownToTileEdge(paintBounds.X(), scaledTileSize.width); + incX = scaledTileSize.width; + } else { + startX = RoundDownToTileEdge(paintBounds.XMost() - 1, scaledTileSize.width); + incX = -scaledTileSize.width; + } + + if (aPaintData->mScrollOffset.y >= aPaintData->mLastScrollOffset.y) { + startY = RoundDownToTileEdge(paintBounds.Y(), scaledTileSize.height); + incY = scaledTileSize.height; + } else { + startY = + RoundDownToTileEdge(paintBounds.YMost() - 1, scaledTileSize.height); + incY = -scaledTileSize.height; + } + + // Find a tile to draw. + IntRect tileBounds(startX, startY, scaledTileSize.width, + scaledTileSize.height); + int32_t scrollDiffX = + aPaintData->mScrollOffset.x - aPaintData->mLastScrollOffset.x; + int32_t scrollDiffY = + aPaintData->mScrollOffset.y - aPaintData->mLastScrollOffset.y; + // This loop will always terminate, as there is at least one tile area + // along the first/last row/column intersecting with regionToPaint, or its + // bounds would have been smaller. + while (true) { + aRegionToPaint.And(aInvalidRegion, tileBounds); + if (!aRegionToPaint.IsEmpty()) { + if (mResolution != 1) { + // Paint the entire tile for low-res. This is aimed to fixing low-res + // resampling and to avoid doing costly region accurate painting for a + // small area. + aRegionToPaint = tileBounds; + } + break; + } + if (Abs(scrollDiffY) >= Abs(scrollDiffX)) { + tileBounds.MoveByX(incX); + } else { + tileBounds.MoveByY(incY); + } + } + + if (!aRegionToPaint.Contains(aInvalidRegion)) { + // The region needed to paint is larger then our progressive chunk size + // therefore update what we want to paint and ask for a new paint + // transaction. + + // If we need to draw more than one tile to maintain coherency, make + // sure it happens in the same transaction by requesting this work be + // repeated immediately. + // If this is unnecessary, the remaining work will be done tile-by-tile in + // subsequent transactions. The caller code is responsible for scheduling + // the subsequent transactions as long as we don't set the mPaintFinished + // flag to true. + return (!drawingLowPrecision && paintInSingleTransaction); + } + + // We're not repeating painting and we've not requested a repeat transaction, + // so the paint is finished. If there's still a separate low precision + // paint to do, it will get marked as unfinished later. + aPaintData->mPaintFinished = true; + return false; +} + +bool ClientMultiTiledLayerBuffer::ProgressiveUpdate( + const nsIntRegion& aValidRegion, const nsIntRegion& aInvalidRegion, + const nsIntRegion& aOldValidRegion, nsIntRegion& aOutDrawnRegion, + BasicTiledLayerPaintData* aPaintData, + LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData) { + TILING_LOG("TILING %p: Progressive update valid region %s\n", &mPaintedLayer, + Stringify(aValidRegion).c_str()); + TILING_LOG("TILING %p: Progressive update invalid region %s\n", + &mPaintedLayer, Stringify(aInvalidRegion).c_str()); + TILING_LOG("TILING %p: Progressive update old valid region %s\n", + &mPaintedLayer, Stringify(aOldValidRegion).c_str()); + + bool repeat = false; + bool isBufferChanged = false; + nsIntRegion remainingInvalidRegion = aInvalidRegion; + nsIntRegion updatedValidRegion = aValidRegion; + do { + // Compute the region that should be updated. Repeat as many times as + // is required. + nsIntRegion regionToPaint; + repeat = + ComputeProgressiveUpdateRegion(remainingInvalidRegion, aOldValidRegion, + regionToPaint, aPaintData, repeat); + + TILING_LOG( + "TILING %p: Progressive update computed paint region %s repeat %d\n", + &mPaintedLayer, Stringify(regionToPaint).c_str(), repeat); + + // There's no further work to be done. + if (regionToPaint.IsEmpty()) { + break; + } + + isBufferChanged = true; + + // Keep track of what we're about to refresh. + aOutDrawnRegion.OrWith(regionToPaint); + updatedValidRegion.OrWith(regionToPaint); + + // aValidRegion may have been altered by InvalidateRegion, but we still + // want to display stale content until it gets progressively updated. + // Create a region that includes stale content. + nsIntRegion validOrStale; + validOrStale.Or(updatedValidRegion, aOldValidRegion); + + // Paint the computed region and subtract it from the invalid region. + PaintThebes(validOrStale, regionToPaint, remainingInvalidRegion, aCallback, + aCallbackData, TilePaintFlags::Progressive); + remainingInvalidRegion.SubOut(regionToPaint); + } while (repeat); + + TILING_LOG( + "TILING %p: Progressive update final valid region %s buffer changed %d\n", + &mPaintedLayer, Stringify(updatedValidRegion).c_str(), isBufferChanged); + TILING_LOG("TILING %p: Progressive update final invalid region %s\n", + &mPaintedLayer, Stringify(remainingInvalidRegion).c_str()); + + // Return false if nothing has been drawn, or give what has been drawn + // to the shadow layer to upload. + return isBufferChanged; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/MultiTiledContentClient.h b/gfx/layers/client/MultiTiledContentClient.h new file mode 100644 index 0000000000..8b51c3b0d0 --- /dev/null +++ b/gfx/layers/client/MultiTiledContentClient.h @@ -0,0 +1,199 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_MULTITILEDCONTENTCLIENT_H +#define MOZILLA_GFX_MULTITILEDCONTENTCLIENT_H + +#include "ClientLayerManager.h" // for ClientLayerManager +#include "nsRegion.h" // for nsIntRegion +#include "mozilla/gfx/2D.h" // for gfx::Tile +#include "mozilla/gfx/Point.h" // for IntPoint +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/LayersMessages.h" // for TileDescriptor +#include "mozilla/layers/TiledContentClient.h" // for ClientTiledPaintedLayer +#include "mozilla/UniquePtr.h" // for UniquePtr +#include "TiledLayerBuffer.h" // for TiledLayerBuffer + +namespace mozilla { +namespace layers { + +class ClientLayerManager; + +class ClientMultiTiledLayerBuffer + : public TiledLayerBuffer<ClientMultiTiledLayerBuffer, TileClient>, + public ClientTiledLayerBuffer { + friend class TiledLayerBuffer<ClientMultiTiledLayerBuffer, TileClient>; + + public: + ClientMultiTiledLayerBuffer(ClientTiledPaintedLayer& aPaintedLayer, + CompositableClient& aCompositableClient, + ClientLayerManager* aManager, + SharedFrameMetricsHelper* aHelper); + + void PaintThebes(const nsIntRegion& aNewValidRegion, + const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData, + TilePaintFlags aFlags = TilePaintFlags::None) override; + + bool SupportsProgressiveUpdate() override { return true; } + /** + * Performs a progressive update of a given tiled buffer. + * See ComputeProgressiveUpdateRegion below for parameter documentation. + * aOutDrawnRegion is an outparameter that contains the region that was + * drawn, and which can now be added to the layer's valid region. + */ + bool ProgressiveUpdate(const nsIntRegion& aValidRegion, + const nsIntRegion& aInvalidRegion, + const nsIntRegion& aOldValidRegion, + nsIntRegion& aOutDrawnRegion, + BasicTiledLayerPaintData* aPaintData, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData) override; + + void ResetPaintedAndValidState() override { + mValidRegion.SetEmpty(); + mTiles.mSize.width = 0; + mTiles.mSize.height = 0; + DiscardBuffers(); + mRetainedTiles.Clear(); + } + + const nsIntRegion& GetValidRegion() override { + return TiledLayerBuffer::GetValidRegion(); + } + + bool IsLowPrecision() const override { + return TiledLayerBuffer::IsLowPrecision(); + } + + void Dump(std::stringstream& aStream, const char* aPrefix, bool aDumpHtml, + TextureDumpMode aCompress) override { + TiledLayerBuffer::Dump(aStream, aPrefix, aDumpHtml, aCompress); + } + + void ReadLock(); + + void Release(); + + void DiscardBuffers(); + + SurfaceDescriptorTiles GetSurfaceDescriptorTiles(); + + void SetResolution(float aResolution) { + if (mResolution == aResolution) { + return; + } + + Update(nsIntRegion(), nsIntRegion(), nsIntRegion(), TilePaintFlags::None); + mResolution = aResolution; + } + + protected: + bool ValidateTile(TileClient& aTile, const nsIntPoint& aTileRect, + nsIntRegion& aDirtyRegion, TilePaintFlags aFlags); + + void Update(const nsIntRegion& aNewValidRegion, + const nsIntRegion& aPaintRegion, const nsIntRegion& aDirtyRegion, + TilePaintFlags aFlags); + + TileClient GetPlaceholderTile() const { return TileClient(); } + + private: + RefPtr<ClientLayerManager> mManager; + LayerManager::DrawPaintedLayerCallback mCallback; + void* mCallbackData; + + // The region that will be made valid during Update(). Once Update() is + // completed then this is identical to mValidRegion. + nsIntRegion mNewValidRegion; + + SharedFrameMetricsHelper* mSharedFrameMetricsHelper; + + // Parameters that are collected during Update for a paint before they + // are either executed or replayed on the paint thread. + AutoTArray<gfx::Tile, 4> mPaintTiles; + AutoTArray<UniquePtr<PaintTask>, 4> mPaintTasks; + + /** + * While we're adding tiles, this is used to keep track of the position of + * the top-left of the top-left-most tile. When we come to wrap the tiles in + * TiledDrawTarget we subtract the value of this member from each tile's + * offset so that all the tiles have a positive offset, then add a + * translation to the TiledDrawTarget to compensate. This is important so + * that the mRect of the TiledDrawTarget is always at a positive x/y + * position, otherwise its GetSize() methods will be broken. + */ + gfx::IntPoint mTilingOrigin; + /** + * Calculates the region to update in a single progressive update transaction. + * This employs some heuristics to update the most 'sensible' region to + * update at this point in time, and how large an update should be performed + * at once to maintain visual coherency. + * + * aInvalidRegion is the current invalid region. + * aOldValidRegion is the valid region of mTiledBuffer at the beginning of the + * current transaction. + * aRegionToPaint will be filled with the region to update. This may be empty, + * which indicates that there is no more work to do. + * aIsRepeated should be true if this function has already been called during + * this transaction. + * + * Returns true if it should be called again, false otherwise. In the case + * that aRegionToPaint is empty, this will return aIsRepeated for convenience. + */ + bool ComputeProgressiveUpdateRegion(const nsIntRegion& aInvalidRegion, + const nsIntRegion& aOldValidRegion, + nsIntRegion& aRegionToPaint, + BasicTiledLayerPaintData* aPaintData, + bool aIsRepeated); + + void MaybeSyncTextures(const nsIntRegion& aPaintRegion, + const TilesPlacement& aNewTiles, + const gfx::IntSize& aScaledTileSize); +}; + +/** + * An implementation of TiledContentClient that supports + * multiple tiles and a low precision buffer. + */ +class MultiTiledContentClient : public TiledContentClient { + public: + MultiTiledContentClient(ClientTiledPaintedLayer& aPaintedLayer, + ClientLayerManager* aManager); + + protected: + ~MultiTiledContentClient() { + MOZ_COUNT_DTOR(MultiTiledContentClient); + + mTiledBuffer.DiscardBuffers(); + mLowPrecisionTiledBuffer.DiscardBuffers(); + } + + public: + void ClearCachedResources() override; + void UpdatedBuffer(TiledBufferType aType) override; + + ClientTiledLayerBuffer* GetTiledBuffer() override { return &mTiledBuffer; } + ClientTiledLayerBuffer* GetLowPrecisionTiledBuffer() override { + if (mHasLowPrecision) { + return &mLowPrecisionTiledBuffer; + } + return nullptr; + } + + private: + SharedFrameMetricsHelper mSharedFrameMetricsHelper; + ClientMultiTiledLayerBuffer mTiledBuffer; + ClientMultiTiledLayerBuffer mLowPrecisionTiledBuffer; + bool mHasLowPrecision; +}; + +} // namespace layers +} // namespace mozilla + +#endif // MOZILLA_GFX_MULTITILEDCONTENTCLIENT_H diff --git a/gfx/layers/client/SingleTiledContentClient.cpp b/gfx/layers/client/SingleTiledContentClient.cpp new file mode 100644 index 0000000000..eabbc1a9ca --- /dev/null +++ b/gfx/layers/client/SingleTiledContentClient.cpp @@ -0,0 +1,266 @@ +/* -*- 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 + * 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 "mozilla/layers/SingleTiledContentClient.h" + +#include "ClientTiledPaintedLayer.h" +#include "mozilla/Maybe.h" +#include "mozilla/UniquePtr.h" +#include "TiledLayerBuffer.h" + +namespace mozilla { +namespace layers { + +SingleTiledContentClient::SingleTiledContentClient( + ClientTiledPaintedLayer& aPaintedLayer, ClientLayerManager* aManager) + : TiledContentClient(aManager, "Single") { + MOZ_COUNT_CTOR(SingleTiledContentClient); + + mTiledBuffer = + new ClientSingleTiledLayerBuffer(aPaintedLayer, *this, aManager); +} + +void SingleTiledContentClient::ClearCachedResources() { + CompositableClient::ClearCachedResources(); + mTiledBuffer->DiscardBuffers(); +} + +void SingleTiledContentClient::UpdatedBuffer(TiledBufferType aType) { + mForwarder->UseTiledLayerBuffer(this, + mTiledBuffer->GetSurfaceDescriptorTiles()); +} + +/* static */ +bool SingleTiledContentClient::ClientSupportsLayerSize( + const gfx::IntSize& aSize, ClientLayerManager* aManager) { + int32_t maxTextureSize = aManager->GetMaxTextureSize(); + return aSize.width <= maxTextureSize && aSize.height <= maxTextureSize; +} + +ClientSingleTiledLayerBuffer::ClientSingleTiledLayerBuffer( + ClientTiledPaintedLayer& aPaintedLayer, + CompositableClient& aCompositableClient, ClientLayerManager* aManager) + : ClientTiledLayerBuffer(aPaintedLayer, aCompositableClient), + mManager(aManager), + mWasLastPaintProgressive(false), + mFormat(gfx::SurfaceFormat::UNKNOWN) {} + +void ClientSingleTiledLayerBuffer::ReleaseTiles() { + if (!mTile.IsPlaceholderTile()) { + mTile.DiscardBuffers(); + } + mTile.SetTextureAllocator(nullptr); +} + +void ClientSingleTiledLayerBuffer::DiscardBuffers() { + if (!mTile.IsPlaceholderTile()) { + mTile.DiscardFrontBuffer(); + mTile.DiscardBackBuffer(); + } +} + +SurfaceDescriptorTiles +ClientSingleTiledLayerBuffer::GetSurfaceDescriptorTiles() { + nsTArray<TileDescriptor> tiles; + + TileDescriptor tileDesc = mTile.GetTileDescriptor(); + tiles.AppendElement(tileDesc); + mTile.mUpdateRect = gfx::IntRect(); + + return SurfaceDescriptorTiles(mValidRegion, tiles, mTilingOrigin, mSize, 0, 0, + 1, 1, 1.0, mFrameResolution.xScale, + mFrameResolution.yScale, + mWasLastPaintProgressive); +} + +already_AddRefed<TextureClient> +ClientSingleTiledLayerBuffer::GetTextureClient() { + MOZ_ASSERT(mFormat != gfx::SurfaceFormat::UNKNOWN); + return mCompositableClient.CreateTextureClientForDrawing( + gfx::ImageFormatToSurfaceFormat(mFormat), mSize, BackendSelector::Content, + TextureFlags::DISALLOW_BIGIMAGE | TextureFlags::IMMEDIATE_UPLOAD | + TextureFlags::NON_BLOCKING_READ_LOCK); +} + +void ClientSingleTiledLayerBuffer::PaintThebes( + const nsIntRegion& aNewValidRegion, const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData, + TilePaintFlags aFlags) { + mWasLastPaintProgressive = !!(aFlags & TilePaintFlags::Progressive); + bool asyncPaint = !!(aFlags & TilePaintFlags::Async); + + // Compare layer valid region size to current backbuffer size, discard if not + // matching. + gfx::IntSize size = aNewValidRegion.GetBounds().Size(); + gfx::IntPoint origin = aNewValidRegion.GetBounds().TopLeft(); + nsIntRegion paintRegion = aPaintRegion; + + RefPtr<TextureClient> discardedFrontBuffer = nullptr; + RefPtr<TextureClient> discardedFrontBufferOnWhite = nullptr; + nsIntRegion discardedValidRegion; + + if (mSize != size || mTilingOrigin != origin) { + discardedFrontBuffer = mTile.mFrontBuffer; + discardedFrontBufferOnWhite = mTile.mFrontBufferOnWhite; + discardedValidRegion = mValidRegion; + + TILING_LOG( + "TILING %p: Single-tile valid region changed. Discarding buffers.\n", + &mPaintedLayer); + ResetPaintedAndValidState(); + mSize = size; + mTilingOrigin = origin; + paintRegion = aNewValidRegion; + } + + SurfaceMode mode; + gfxContentType content = GetContentType(&mode); + mFormat = gfxPlatform::GetPlatform()->OptimalFormatForContent(content); + + if (mTile.IsPlaceholderTile()) { + mTile.SetTextureAllocator(this); + } + + if (mManager->AsShadowForwarder()->SupportsTextureDirectMapping()) { + AutoTArray<uint64_t, 2> syncTextureSerials; + mTile.GetSyncTextureSerials(mode, syncTextureSerials); + if (syncTextureSerials.Length() > 0) { + mManager->AsShadowForwarder()->SyncTextures(syncTextureSerials); + } + } + + // The dirty region relative to the top-left of the tile. + nsIntRegion tileVisibleRegion = aNewValidRegion.MovedBy(-mTilingOrigin); + nsIntRegion tileDirtyRegion = paintRegion.MovedBy(-mTilingOrigin); + + Maybe<AcquiredBackBuffer> backBuffer = + mTile.AcquireBackBuffer(mCompositableClient, tileDirtyRegion, + tileVisibleRegion, content, mode, aFlags); + + if (!backBuffer) { + return; + } + + // Mark the area we need to paint in the back buffer as invalid in the + // front buffer as they will become out of sync. + mTile.mInvalidFront.OrWith(tileDirtyRegion); + + // Add backbuffer's invalid region to the dirty region to be painted. + // This will be empty if we were able to copy from the front in to the back. + nsIntRegion tileInvalidRegion = mTile.mInvalidBack; + tileInvalidRegion.AndWith(tileVisibleRegion); + + paintRegion.OrWith(tileInvalidRegion.MovedBy(mTilingOrigin)); + tileDirtyRegion.OrWith(tileInvalidRegion); + + // Mark the region we will be painting and the region we copied from the front + // buffer as needing to be uploaded to the compositor + mTile.mUpdateRect = + tileDirtyRegion.GetBounds().Union(backBuffer->mUpdatedRect); + + // If the old frontbuffer was discarded then attempt to copy what we + // can from it to the new backbuffer. + if (discardedFrontBuffer) { + nsIntRegion copyableRegion; + copyableRegion.And(aNewValidRegion, discardedValidRegion); + copyableRegion.SubOut(aDirtyRegion); + + OpenMode readMode = + asyncPaint ? OpenMode::OPEN_READ_ASYNC : OpenMode::OPEN_READ; + + DualTextureClientAutoLock discardedBuffer( + discardedFrontBuffer, discardedFrontBufferOnWhite, readMode); + + if (discardedBuffer.Succeeded()) { + RefPtr<gfx::SourceSurface> discardedSurface = discardedBuffer->Snapshot(); + + for (auto iter = copyableRegion.RectIter(); !iter.Done(); iter.Next()) { + const gfx::IntRect src = + iter.Get() - discardedValidRegion.GetBounds().TopLeft(); + const gfx::IntPoint dest = iter.Get().TopLeft() - mTilingOrigin; + + backBuffer->mTarget->CopySurface(discardedSurface, src, dest); + } + + TILING_LOG("TILING %p: Region copied from discarded frontbuffer %s\n", + &mPaintedLayer, Stringify(copyableRegion).c_str()); + + // We don't need to repaint valid content that was just copied. + paintRegion.SubOut(copyableRegion); + copyableRegion.MoveBy(-mTilingOrigin); + tileDirtyRegion.SubOut(copyableRegion); + } else { + gfxWarning() << "[Tiling:Client] Failed to aquire the discarded front " + "buffer's draw target"; + } + } + + if (mode != SurfaceMode::SURFACE_OPAQUE) { + for (auto iter = tileDirtyRegion.RectIter(); !iter.Done(); iter.Next()) { + const gfx::Rect drawRect(iter.Get().X(), iter.Get().Y(), + iter.Get().Width(), iter.Get().Height()); + backBuffer->mTarget->ClearRect(drawRect); + } + } + + // Paint into the target + { + RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(backBuffer->mTarget); + if (!ctx) { + gfxDevCrash(gfx::LogReason::InvalidContext) + << "SingleTiledContextClient context problem " + << gfx::hexa(backBuffer->mTarget); + return; + } + ctx->SetMatrix( + ctx->CurrentMatrix().PreTranslate(-mTilingOrigin.x, -mTilingOrigin.y)); + + aCallback(&mPaintedLayer, ctx, paintRegion, paintRegion, + DrawRegionClip::DRAW, nsIntRegion(), aCallbackData); + } + + if (asyncPaint) { + if (!backBuffer->mCapture->IsEmpty()) { + UniquePtr<PaintTask> task(new PaintTask()); + task->mCapture = backBuffer->mCapture; + task->mTarget = backBuffer->mBackBuffer; + task->mClients = std::move(backBuffer->mTextureClients); + if (discardedFrontBuffer) { + task->mClients.AppendElement(discardedFrontBuffer); + } + if (discardedFrontBufferOnWhite) { + task->mClients.AppendElement(discardedFrontBufferOnWhite); + } + + // The target is an alias for the capture, and the paint thread expects + // to be the only one with a reference to the capture + backBuffer->mTarget = nullptr; + backBuffer->mCapture = nullptr; + + PaintThread::Get()->QueuePaintTask(std::move(task)); + mManager->SetQueuedAsyncPaints(); + } + } else { + MOZ_ASSERT(backBuffer->mTarget == backBuffer->mBackBuffer); + MOZ_ASSERT(!backBuffer->mCapture); + } + + // The new buffer is now validated, remove the dirty region from it. + mTile.mInvalidBack.SubOut(tileDirtyRegion); + + backBuffer = Nothing(); + + mTile.Flip(); + UnlockTile(mTile); + + mValidRegion = aNewValidRegion; + mLastPaintSurfaceMode = mode; + mLastPaintContentType = content; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/SingleTiledContentClient.h b/gfx/layers/client/SingleTiledContentClient.h new file mode 100644 index 0000000000..70cb37c147 --- /dev/null +++ b/gfx/layers/client/SingleTiledContentClient.h @@ -0,0 +1,128 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_SINGLETILEDCONTENTCLIENT_H +#define MOZILLA_GFX_SINGLETILEDCONTENTCLIENT_H + +#include "TiledContentClient.h" + +namespace mozilla { +namespace layers { + +class ClientTiledPaintedLayer; +class ClientLayerManager; + +/** + * Provide an instance of TiledLayerBuffer backed by drawable TextureClients. + * This buffer provides an implementation of ValidateTile using a + * thebes callback and can support painting using a single paint buffer. + * Whether a single paint buffer is used is controlled by + * StaticPrefs::PerTileDrawing(). + */ +class ClientSingleTiledLayerBuffer : public ClientTiledLayerBuffer, + public TextureClientAllocator { + virtual ~ClientSingleTiledLayerBuffer() = default; + + public: + ClientSingleTiledLayerBuffer(ClientTiledPaintedLayer& aPaintedLayer, + CompositableClient& aCompositableClient, + ClientLayerManager* aManager); + + // TextureClientAllocator + already_AddRefed<TextureClient> GetTextureClient() override; + void ReturnTextureClientDeferred(TextureClient* aClient) override {} + void ReportClientLost() override {} + + // ClientTiledLayerBuffer + void PaintThebes(const nsIntRegion& aNewValidRegion, + const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData, + TilePaintFlags aFlags = TilePaintFlags::None) override; + + bool SupportsProgressiveUpdate() override { return false; } + bool ProgressiveUpdate(const nsIntRegion& aValidRegion, + const nsIntRegion& aInvalidRegion, + const nsIntRegion& aOldValidRegion, + nsIntRegion& aOutDrawnRegion, + BasicTiledLayerPaintData* aPaintData, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData) override { + MOZ_ASSERT(false, "ProgressiveUpdate not supported!"); + return false; + } + + void ResetPaintedAndValidState() override { + mValidRegion.SetEmpty(); + mTile.DiscardBuffers(); + } + + const nsIntRegion& GetValidRegion() override { return mValidRegion; } + + bool IsLowPrecision() const override { return false; } + + void ReleaseTiles(); + + void DiscardBuffers(); + + SurfaceDescriptorTiles GetSurfaceDescriptorTiles(); + + private: + TileClient mTile; + + RefPtr<ClientLayerManager> mManager; + + nsIntRegion mValidRegion; + bool mWasLastPaintProgressive; + + /** + * While we're adding tiles, this is used to keep track of the position of + * the top-left of the top-left-most tile. When we come to wrap the tiles in + * TiledDrawTarget we subtract the value of this member from each tile's + * offset so that all the tiles have a positive offset, then add a + * translation to the TiledDrawTarget to compensate. This is important so + * that the mRect of the TiledDrawTarget is always at a positive x/y + * position, otherwise its GetSize() methods will be broken. + */ + gfx::IntPoint mTilingOrigin; + gfx::IntSize mSize; + gfxImageFormat mFormat; +}; + +class SingleTiledContentClient : public TiledContentClient { + public: + SingleTiledContentClient(ClientTiledPaintedLayer& aPaintedLayer, + ClientLayerManager* aManager); + + protected: + ~SingleTiledContentClient() { + MOZ_COUNT_DTOR(SingleTiledContentClient); + + mTiledBuffer->ReleaseTiles(); + } + + public: + static bool ClientSupportsLayerSize(const gfx::IntSize& aSize, + ClientLayerManager* aManager); + + void ClearCachedResources() override; + + void UpdatedBuffer(TiledBufferType aType) override; + + ClientTiledLayerBuffer* GetTiledBuffer() override { return mTiledBuffer; } + ClientTiledLayerBuffer* GetLowPrecisionTiledBuffer() override { + return nullptr; + } + + private: + RefPtr<ClientSingleTiledLayerBuffer> mTiledBuffer; +}; + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/TextureClient.cpp b/gfx/layers/client/TextureClient.cpp new file mode 100644 index 0000000000..c4d4077223 --- /dev/null +++ b/gfx/layers/client/TextureClient.cpp @@ -0,0 +1,1934 @@ +/* -*- 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 + * 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 "mozilla/layers/TextureClient.h" + +#include <stdint.h> // for uint8_t, uint32_t, etc + +#include "BufferTexture.h" +#include "GeckoProfiler.h" +#include "IPDLActor.h" +#include "ImageContainer.h" // for PlanarYCbCrData, etc +#include "Layers.h" // for Layer, etc +#include "MainThreadUtils.h" +#include "gfx2DGlue.h" +#include "gfxPlatform.h" // for gfxPlatform +#include "gfxUtils.h" // for gfxUtils::GetAsLZ4Base64Str +#include "mozilla/Atomics.h" +#include "mozilla/Mutex.h" +#include "mozilla/SchedulerGroup.h" +#include "mozilla/StaticPrefs_gfx.h" +#include "mozilla/StaticPrefs_layers.h" +#include "mozilla/gfx/2D.h" +#include "mozilla/gfx/DataSurfaceHelpers.h" // for CreateDataSourceSurfaceByCloning +#include "mozilla/gfx/Logging.h" // for gfxDebug +#include "mozilla/gfx/gfxVars.h" +#include "mozilla/ipc/CrossProcessSemaphore.h" +#include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/ImageBridgeChild.h" +#include "mozilla/layers/ImageDataSerializer.h" +#include "mozilla/layers/PTextureChild.h" +#include "mozilla/layers/PaintThread.h" +#include "mozilla/layers/ShadowLayers.h" +#include "mozilla/layers/TextureClientOGL.h" +#include "mozilla/layers/TextureClientRecycleAllocator.h" +#include "mozilla/layers/TextureRecorded.h" +#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc +#include "nsISerialEventTarget.h" +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc +#include "nsPrintfCString.h" // for nsPrintfCString + +#ifdef XP_WIN +# include "gfx2DGlue.h" +# include "gfxWindowsPlatform.h" +# include "mozilla/gfx/DeviceManagerDx.h" +# include "mozilla/layers/TextureD3D11.h" +# include "mozilla/layers/TextureDIB.h" +#endif +#ifdef MOZ_X11 +# include "GLXLibrary.h" +# include "mozilla/layers/TextureClientX11.h" +#endif +#ifdef MOZ_WAYLAND +# include <gtk/gtkx.h> + +# include "gfxPlatformGtk.h" +# include "mozilla/layers/DMABUFTextureClientOGL.h" +# include "mozilla/widget/nsWaylandDisplay.h" +#endif + +#ifdef XP_MACOSX +# include "mozilla/layers/MacIOSurfaceTextureClientOGL.h" +#endif + +#if 0 +# define RECYCLE_LOG(...) printf_stderr(__VA_ARGS__) +#else +# define RECYCLE_LOG(...) \ + do { \ + } while (0) +#endif + +namespace mozilla::layers { + +using namespace mozilla::ipc; +using namespace mozilla::gl; +using namespace mozilla::gfx; + +struct TextureDeallocParams { + TextureData* data; + RefPtr<TextureChild> actor; + RefPtr<LayersIPCChannel> allocator; + bool clientDeallocation; + bool syncDeallocation; + bool workAroundSharedSurfaceOwnershipIssue; +}; + +void DeallocateTextureClient(TextureDeallocParams params); + +/** + * TextureChild is the content-side incarnation of the PTexture IPDL actor. + * + * TextureChild is used to synchronize a texture client and its corresponding + * TextureHost if needed (a TextureClient that is not shared with the compositor + * does not have a TextureChild) + * + * During the deallocation phase, a TextureChild may hold its recently destroyed + * TextureClient's data until the compositor side confirmed that it is safe to + * deallocte or recycle the it. + */ +class TextureChild final : PTextureChild { + ~TextureChild() { + // We should have deallocated mTextureData in ActorDestroy + MOZ_ASSERT(!mTextureData); + MOZ_ASSERT_IF(!mOwnerCalledDestroy, !mTextureClient); + } + + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureChild) + + TextureChild() + : mCompositableForwarder(nullptr), + mTextureForwarder(nullptr), + mTextureClient(nullptr), + mTextureData(nullptr), + mDestroyed(false), + mMainThreadOnly(false), + mIPCOpen(false), + mOwnsTextureData(false), + mOwnerCalledDestroy(false), + mUsesImageBridge(false) {} + + mozilla::ipc::IPCResult Recv__delete__() override { return IPC_OK(); } + + LayersIPCChannel* GetAllocator() { return mTextureForwarder; } + + void ActorDestroy(ActorDestroyReason why) override; + + bool IPCOpen() const { return mIPCOpen; } + + void Lock() const { + if (mUsesImageBridge) { + mLock.Enter(); + } + } + + void Unlock() const { + if (mUsesImageBridge) { + mLock.Leave(); + } + } + + private: + // AddIPDLReference and ReleaseIPDLReference are only to be called by + // CreateIPDLActor and DestroyIPDLActor, respectively. We intentionally make + // them private to prevent misuse. The purpose of these methods is to be aware + // of when the IPC system around this actor goes down: mIPCOpen is then set to + // false. + void AddIPDLReference() { + MOZ_ASSERT(mIPCOpen == false); + mIPCOpen = true; + AddRef(); + } + void ReleaseIPDLReference() { + MOZ_ASSERT(mIPCOpen == false); + Release(); + } + + /// The normal way to destroy the actor. + /// + /// This will asynchronously send a Destroy message to the parent actor, whom + /// will send the delete message. + void Destroy(const TextureDeallocParams& aParams); + + // This lock is used order to prevent several threads to access the + // TextureClient's data concurrently. In particular, it prevents shutdown + // code to destroy a texture while another thread is reading or writing into + // it. + // In most places, the lock is held in short and bounded scopes in which we + // don't block on any other resource. There are few exceptions to this, which + // are discussed below. + // + // The locking pattern of TextureClient may in some case upset deadlock + // detection tools such as TSan. Typically our tile rendering code will lock + // all of its tiles, render into them and unlock them all right after that, + // which looks something like: + // + // Lock tile A + // Lock tile B + // Lock tile C + // Apply drawing commands to tiles A, B and C + // Unlock tile A + // Unlock tile B + // Unlock tile C + // + // And later, we may end up rendering a tile buffer that has the same tiles, + // in a different order, for example: + // + // Lock tile B + // Lock tile A + // Lock tile D + // Apply drawing commands to tiles A, B and D + // Unlock tile B + // Unlock tile A + // Unlock tile D + // + // This is because textures being expensive to create, we recycle them as much + // as possible and they may reappear in the tile buffer in a different order. + // + // Unfortunately this is not very friendly to TSan's analysis, which will see + // that B was once locked while A was locked, and then A locked while B was + // locked. TSan identifies this as a potential dead-lock which would be the + // case if this kind of inconsistent and dependent locking order was happening + // concurrently. + // In the case of TextureClient, dependent locking only ever happens on the + // thread that draws into the texture (let's call it the producer thread). + // Other threads may call into a method that can lock the texture in a short + // and bounded scope inside of which it is not allowed to do anything that + // could cause the thread to block. A given texture can only have one producer + // thread. + // + // Another example of TSan-unfriendly locking pattern is when copying a + // texture into another, which also never happens outside of the producer + // thread. Copying A into B looks like this: + // + // Lock texture B + // Lock texture A + // Copy A into B + // Unlock A + // Unlock B + // + // In a given frame we may need to copy A into B and in another frame copy + // B into A. For example A and B can be the Front and Back buffers, + // alternating roles and the copy is needed to avoid the cost of re-drawing + // the valid region. + // + // The important rule is that all of the dependent locking must occur only + // in the texture's producer thread to avoid deadlocks. + mutable gfx::CriticalSection mLock; + + RefPtr<CompositableForwarder> mCompositableForwarder; + RefPtr<TextureForwarder> mTextureForwarder; + + TextureClient* mTextureClient; + TextureData* mTextureData; + Atomic<bool> mDestroyed; + bool mMainThreadOnly; + bool mIPCOpen; + bool mOwnsTextureData; + bool mOwnerCalledDestroy; + bool mUsesImageBridge; + + friend class TextureClient; + friend void DeallocateTextureClient(TextureDeallocParams params); +}; + +static inline gfx::BackendType BackendTypeForBackendSelector( + LayersBackend aLayersBackend, BackendSelector aSelector) { + switch (aSelector) { + case BackendSelector::Canvas: + return gfxPlatform::GetPlatform()->GetPreferredCanvasBackend(); + case BackendSelector::Content: + return gfxPlatform::GetPlatform()->GetContentBackendFor(aLayersBackend); + default: + MOZ_ASSERT_UNREACHABLE("Unknown backend selector"); + return gfx::BackendType::NONE; + } +}; + +static TextureType GetTextureType(gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, + KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector, + TextureAllocationFlags aAllocFlags) { + LayersBackend layersBackend = aKnowsCompositor->GetCompositorBackendType(); + gfx::BackendType moz2DBackend = + BackendTypeForBackendSelector(layersBackend, aSelector); + Unused << moz2DBackend; + +#ifdef XP_WIN + int32_t maxTextureSize = aKnowsCompositor->GetMaxTextureSize(); + if ((layersBackend == LayersBackend::LAYERS_D3D11 || + (layersBackend == LayersBackend::LAYERS_WR && + !aKnowsCompositor->UsingSoftwareWebRender())) && + (moz2DBackend == gfx::BackendType::DIRECT2D || + moz2DBackend == gfx::BackendType::DIRECT2D1_1 || + (!!(aAllocFlags & ALLOC_FOR_OUT_OF_BAND_CONTENT))) && + aSize.width <= maxTextureSize && aSize.height <= maxTextureSize && + !(aAllocFlags & ALLOC_UPDATE_FROM_SURFACE)) { + return TextureType::D3D11; + } + + if (layersBackend != LayersBackend::LAYERS_WR && + aFormat == SurfaceFormat::B8G8R8X8 && + moz2DBackend == gfx::BackendType::CAIRO && NS_IsMainThread()) { + return TextureType::DIB; + } +#endif + +#ifdef MOZ_WAYLAND + if ((layersBackend == LayersBackend::LAYERS_OPENGL || + (layersBackend == LayersBackend::LAYERS_WR && + !aKnowsCompositor->UsingSoftwareWebRender())) && + widget::GetDMABufDevice()->IsDMABufTexturesEnabled() && + aFormat != SurfaceFormat::A8) { + return TextureType::DMABUF; + } +#endif + +#ifdef MOZ_X11 + gfxSurfaceType type = + gfxPlatform::GetPlatform()->ScreenReferenceSurface()->GetType(); + + if (layersBackend == LayersBackend::LAYERS_BASIC && + moz2DBackend == gfx::BackendType::CAIRO && type == gfxSurfaceType::Xlib) { + return TextureType::X11; + } + if (layersBackend == LayersBackend::LAYERS_OPENGL && + type == gfxSurfaceType::Xlib && aFormat != SurfaceFormat::A8 && + gl::sGLXLibrary.UseTextureFromPixmap()) { + return TextureType::X11; + } +#endif + +#ifdef XP_MACOSX + if (StaticPrefs::gfx_use_iosurface_textures_AtStartup()) { + return TextureType::MacIOSurface; + } +#endif + +#ifdef MOZ_WIDGET_ANDROID + if (gfxVars::UseAHardwareBufferContent() && + aSelector == BackendSelector::Content) { + return TextureType::AndroidHardwareBuffer; + } + if (StaticPrefs::gfx_use_surfacetexture_textures_AtStartup()) { + return TextureType::AndroidNativeWindow; + } +#endif + + return TextureType::Unknown; +} + +TextureType PreferredCanvasTextureType(KnowsCompositor* aKnowsCompositor) { + return GetTextureType(gfx::SurfaceFormat::R8G8B8A8, {1, 1}, aKnowsCompositor, + BackendSelector::Canvas, + TextureAllocationFlags::ALLOC_DEFAULT); +} + +static bool ShouldRemoteTextureType(TextureType aTextureType, + BackendSelector aSelector) { + if (!XRE_IsContentProcess()) { + return false; + } + + if (aSelector != BackendSelector::Canvas || !gfxVars::RemoteCanvasEnabled()) { + return false; + } + + switch (aTextureType) { + case TextureType::D3D11: + return true; + default: + return false; + } +} + +/* static */ +TextureData* TextureData::Create(TextureForwarder* aAllocator, + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector, + TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + TextureType textureType = + GetTextureType(aFormat, aSize, aKnowsCompositor, aSelector, aAllocFlags); + + if (ShouldRemoteTextureType(textureType, aSelector)) { + RefPtr<CanvasChild> canvasChild = aAllocator->GetCanvasChild(); + if (canvasChild) { + return new RecordedTextureData(canvasChild.forget(), aSize, aFormat, + textureType); + } + + // We don't have a CanvasChild, but are supposed to be remote. + // Fall back to software. + textureType = TextureType::Unknown; + } + +#if defined(XP_MACOSX) || defined(MOZ_WAYLAND) + gfx::BackendType moz2DBackend = BackendTypeForBackendSelector( + aKnowsCompositor->GetCompositorBackendType(), aSelector); +#endif + + switch (textureType) { +#ifdef XP_WIN + case TextureType::D3D11: + return D3D11TextureData::Create(aSize, aFormat, aAllocFlags); + case TextureType::DIB: + return DIBTextureData::Create(aSize, aFormat, aAllocator); +#endif + +#ifdef MOZ_WAYLAND + case TextureType::DMABUF: + return DMABUFTextureData::Create(aSize, aFormat, moz2DBackend); +#endif + +#ifdef MOZ_X11 + case TextureType::X11: + return X11TextureData::Create(aSize, aFormat, aTextureFlags, aAllocator); +#endif +#ifdef XP_MACOSX + case TextureType::MacIOSurface: + return MacIOSurfaceTextureData::Create(aSize, aFormat, moz2DBackend); +#endif +#ifdef MOZ_WIDGET_ANDROID + case TextureType::AndroidHardwareBuffer: + return AndroidHardwareBufferTextureData::Create(aSize, aFormat); + case TextureType::AndroidNativeWindow: + return AndroidNativeWindowTextureData::Create(aSize, aFormat); +#endif + default: + return nullptr; + } +} + +/* static */ +bool TextureData::IsRemote(KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector) { + TextureType textureType = GetTextureType( + gfx::SurfaceFormat::UNKNOWN, gfx::IntSize(1, 1), aKnowsCompositor, + aSelector, TextureAllocationFlags::ALLOC_DEFAULT); + + return ShouldRemoteTextureType(textureType, aSelector); +} + +static void DestroyTextureData(TextureData* aTextureData, + LayersIPCChannel* aAllocator, bool aDeallocate, + bool aMainThreadOnly) { + if (!aTextureData) { + return; + } + + if (aMainThreadOnly && !NS_IsMainThread()) { + RefPtr<LayersIPCChannel> allocatorRef = aAllocator; + SchedulerGroup::Dispatch( + TaskCategory::Other, + NS_NewRunnableFunction( + "layers::DestroyTextureData", + [aTextureData, allocatorRef, aDeallocate]() -> void { + DestroyTextureData(aTextureData, allocatorRef, aDeallocate, true); + })); + return; + } + + if (aDeallocate) { + aTextureData->Deallocate(aAllocator); + } else { + aTextureData->Forget(aAllocator); + } + delete aTextureData; +} + +void TextureChild::ActorDestroy(ActorDestroyReason why) { + AUTO_PROFILER_LABEL("TextureChild::ActorDestroy", GRAPHICS); + MOZ_ASSERT(mIPCOpen); + mIPCOpen = false; + + if (mTextureData) { + DestroyTextureData(mTextureData, GetAllocator(), mOwnsTextureData, + mMainThreadOnly); + mTextureData = nullptr; + } +} + +void TextureChild::Destroy(const TextureDeallocParams& aParams) { + MOZ_ASSERT(!mOwnerCalledDestroy); + if (mOwnerCalledDestroy) { + return; + } + + mOwnerCalledDestroy = true; + + if (!IPCOpen()) { + DestroyTextureData(aParams.data, aParams.allocator, + aParams.clientDeallocation, mMainThreadOnly); + return; + } + + // DestroyTextureData will be called by TextureChild::ActorDestroy + mTextureData = aParams.data; + mOwnsTextureData = aParams.clientDeallocation; + + if (!mCompositableForwarder || + !mCompositableForwarder->DestroyInTransaction(this)) { + this->SendDestroy(); + } +} + +/* static */ +Atomic<uint64_t> TextureClient::sSerialCounter(0); + +static void DeallocateTextureClientSyncProxy(TextureDeallocParams params, + ReentrantMonitor* aBarrier, + bool* aDone) { + DeallocateTextureClient(params); + ReentrantMonitorAutoEnter autoMon(*aBarrier); + *aDone = true; + aBarrier->NotifyAll(); +} + +/// The logic for synchronizing a TextureClient's deallocation goes here. +/// +/// This funciton takes care of dispatching work to the right thread using +/// a synchronous proxy if needed, and handles client/host deallocation. +void DeallocateTextureClient(TextureDeallocParams params) { + if (!params.actor && !params.data) { + // Nothing to do + return; + } + + TextureChild* actor = params.actor; + nsCOMPtr<nsISerialEventTarget> ipdlThread; + + if (params.allocator) { + ipdlThread = params.allocator->GetThread(); + if (!ipdlThread) { + // An allocator with no thread means we are too late in the shutdown + // sequence. + gfxCriticalError() << "Texture deallocated too late during shutdown"; + return; + } + } + + // First make sure that the work is happening on the IPDL thread. + if (ipdlThread && !ipdlThread->IsOnCurrentThread()) { + if (params.syncDeallocation) { + bool done = false; + ReentrantMonitor barrier("DeallocateTextureClient"); + ReentrantMonitorAutoEnter autoMon(barrier); + ipdlThread->Dispatch(NewRunnableFunction( + "DeallocateTextureClientSyncProxyRunnable", + DeallocateTextureClientSyncProxy, params, &barrier, &done)); + while (!done) { + barrier.Wait(); + } + } else { + ipdlThread->Dispatch(NewRunnableFunction( + "DeallocateTextureClientRunnable", DeallocateTextureClient, params)); + } + // The work has been forwarded to the IPDL thread, we are done. + return; + } + + // Below this line, we are either in the IPDL thread or ther is no IPDL + // thread anymore. + + if (!ipdlThread) { + // If we don't have a thread we can't know for sure that we are in + // the IPDL thread and use the LayersIPCChannel. + // This should ideally not happen outside of gtest, but some shutdown + // raciness could put us in this situation. + params.allocator = nullptr; + } + + if (!actor) { + // We don't have an IPDL actor, probably because we destroyed the + // TextureClient before sharing it with the compositor. It means the data + // cannot be owned by the TextureHost since we never created the + // TextureHost... + // ..except if the lovely mWorkaroundAnnoyingSharedSurfaceOwnershipIssues + // member is set to true. In this case we are in a special situation where + // this TextureClient is in wrapped into another TextureClient which assumes + // it owns our data. + bool shouldDeallocate = !params.workAroundSharedSurfaceOwnershipIssue; + DestroyTextureData(params.data, params.allocator, shouldDeallocate, + false); // main-thread deallocation + return; + } + + actor->Destroy(params); +} + +void TextureClient::Destroy() { + // Async paints should have been flushed by now. + MOZ_RELEASE_ASSERT(mPaintThreadRefs == 0); + + if (mActor && !mIsLocked) { + mActor->Lock(); + } + + mBorrowedDrawTarget = nullptr; + mReadLock = nullptr; + + RefPtr<TextureChild> actor = mActor; + mActor = nullptr; + + if (actor && !actor->mDestroyed.compareExchange(false, true)) { + actor->Unlock(); + actor = nullptr; + } + + TextureData* data = mData; + if (!mWorkaroundAnnoyingSharedSurfaceLifetimeIssues) { + mData = nullptr; + } + + if (data || actor) { + TextureDeallocParams params; + params.actor = actor; + params.allocator = mAllocator; + params.clientDeallocation = !!(mFlags & TextureFlags::DEALLOCATE_CLIENT); + params.workAroundSharedSurfaceOwnershipIssue = + mWorkaroundAnnoyingSharedSurfaceOwnershipIssues; + if (mWorkaroundAnnoyingSharedSurfaceLifetimeIssues) { + params.data = nullptr; + } else { + params.data = data; + } + // At the moment we always deallocate synchronously when deallocating on the + // client side, but having asynchronous deallocate in some of the cases will + // be a worthwhile optimization. + params.syncDeallocation = !!(mFlags & TextureFlags::DEALLOCATE_CLIENT); + + // Release the lock before calling DeallocateTextureClient because the + // latter may wait for the main thread which could create a dead-lock. + + if (actor) { + actor->Unlock(); + } + + DeallocateTextureClient(params); + } +} + +void TextureClient::LockActor() const { + if (mActor) { + mActor->Lock(); + } +} + +void TextureClient::UnlockActor() const { + if (mActor) { + mActor->Unlock(); + } +} + +bool TextureClient::IsReadLocked() const { + if (!mReadLock) { + return false; + } + MOZ_ASSERT(mReadLock->AsNonBlockingLock(), + "Can only check locked for non-blocking locks!"); + return mReadLock->AsNonBlockingLock()->GetReadCount() > 1; +} + +bool TextureClient::TryReadLock() { + if (!mReadLock || mIsReadLocked) { + return true; + } + + if (mReadLock->AsNonBlockingLock()) { + if (IsReadLocked()) { + return false; + } + } + + if (!mReadLock->TryReadLock(TimeDuration::FromMilliseconds(500))) { + return false; + } + + mIsReadLocked = true; + return true; +} + +void TextureClient::ReadUnlock() { + if (!mIsReadLocked) { + return; + } + MOZ_ASSERT(mReadLock); + mReadLock->ReadUnlock(); + mIsReadLocked = false; +} + +bool TextureClient::Lock(OpenMode aMode) { + MOZ_ASSERT(IsValid()); + MOZ_ASSERT(!mIsLocked); + if (!IsValid()) { + return false; + } + if (mIsLocked) { + return mOpenMode == aMode; + } + + if ((aMode & OpenMode::OPEN_WRITE || !mInfo.canConcurrentlyReadLock) && + !TryReadLock()) { + // Only warn if attempting to write. Attempting to read is acceptable usage. + if (aMode & OpenMode::OPEN_WRITE) { + NS_WARNING( + "Attempt to Lock a texture that is being read by the compositor!"); + } + return false; + } + + LockActor(); + + mIsLocked = mData->Lock(aMode); + mOpenMode = aMode; + + auto format = GetFormat(); + if (mIsLocked && CanExposeDrawTarget() && + (aMode & OpenMode::OPEN_READ_WRITE) == OpenMode::OPEN_READ_WRITE && + NS_IsMainThread() && + // the formats that we apparently expect, in the cairo backend. Any other + // format will trigger an assertion in GfxFormatToCairoFormat. + (format == SurfaceFormat::A8R8G8B8_UINT32 || + format == SurfaceFormat::X8R8G8B8_UINT32 || + format == SurfaceFormat::A8 || format == SurfaceFormat::R5G6B5_UINT16)) { + if (!BorrowDrawTarget()) { + // Failed to get a DrawTarget, means we won't be able to write into the + // texture, might as well fail now. + Unlock(); + return false; + } + } + + if (!mIsLocked) { + UnlockActor(); + ReadUnlock(); + } + + return mIsLocked; +} + +void TextureClient::Unlock() { + MOZ_ASSERT(IsValid()); + MOZ_ASSERT(mIsLocked); + if (!IsValid() || !mIsLocked) { + return; + } + + if (mBorrowedDrawTarget) { + if (!(mOpenMode & OpenMode::OPEN_ASYNC)) { + if (mOpenMode & OpenMode::OPEN_WRITE) { + mBorrowedDrawTarget->Flush(); + if (mReadbackSink && !mData->ReadBack(mReadbackSink)) { + // Fallback implementation for reading back, because mData does not + // have a backend-specific implementation and returned false. + RefPtr<SourceSurface> snapshot = mBorrowedDrawTarget->Snapshot(); + RefPtr<DataSourceSurface> dataSurf = snapshot->GetDataSurface(); + mReadbackSink->ProcessReadback(dataSurf); + } + } + + mBorrowedDrawTarget->DetachAllSnapshots(); + // If this assertion is hit, it means something is holding a strong + // reference to our DrawTarget externally, which is not allowed. + MOZ_ASSERT(mBorrowedDrawTarget->refCount() <= mExpectedDtRefs); + } + + mBorrowedDrawTarget = nullptr; + } + + if (mOpenMode & OpenMode::OPEN_WRITE) { + mUpdated = true; + } + + if (mData) { + mData->Unlock(); + } + mIsLocked = false; + mOpenMode = OpenMode::OPEN_NONE; + + UnlockActor(); + ReadUnlock(); +} + +void TextureClient::EnableReadLock() { + if (!mReadLock) { + if (mAllocator->GetTileLockAllocator()) { + mReadLock = NonBlockingTextureReadLock::Create(mAllocator); + } else { + // IPC is down + gfxCriticalError() << "TextureClient::EnableReadLock IPC is down"; + } + } +} + +bool TextureClient::OnForwardedToHost() { + if (mData) { + mData->OnForwardedToHost(); + } + + if (mReadLock && mUpdated) { + // Take a read lock on behalf of the TextureHost. The latter will unlock + // after the shared data is available again for drawing. + mReadLock->ReadLock(); + mUpdated = false; + return true; + } + + return false; +} + +TextureClient::~TextureClient() { + // TextureClients should be kept alive while there are references on the + // paint thread. + MOZ_ASSERT(mPaintThreadRefs == 0); + mReadLock = nullptr; + Destroy(); +} + +void TextureClient::UpdateFromSurface(gfx::SourceSurface* aSurface) { + MOZ_ASSERT(IsValid()); + MOZ_ASSERT(mIsLocked); + MOZ_ASSERT(aSurface); + // If you run into this assertion, make sure the texture was locked write-only + // rather than read-write. + MOZ_ASSERT(!mBorrowedDrawTarget); + + // XXX - It would be better to first try the DrawTarget approach and fallback + // to the backend-specific implementation because the latter will usually do + // an expensive read-back + cpu-side copy if the texture is on the gpu. + // There is a bug with the DrawTarget approach, though specific to reading + // back from WebGL (where R and B channel end up inverted) to figure out + // first. + if (mData->UpdateFromSurface(aSurface)) { + return; + } + if (CanExposeDrawTarget() && NS_IsMainThread()) { + RefPtr<DrawTarget> dt = BorrowDrawTarget(); + + MOZ_ASSERT(dt); + if (dt) { + dt->CopySurface(aSurface, + gfx::IntRect(gfx::IntPoint(0, 0), aSurface->GetSize()), + gfx::IntPoint(0, 0)); + return; + } + } + NS_WARNING("TextureClient::UpdateFromSurface failed"); +} + +already_AddRefed<TextureClient> TextureClient::CreateSimilar( + LayersBackend aLayersBackend, TextureFlags aFlags, + TextureAllocationFlags aAllocFlags) const { + MOZ_ASSERT(IsValid()); + + MOZ_ASSERT(!mIsLocked); + if (mIsLocked) { + return nullptr; + } + + LockActor(); + TextureData* data = + mData->CreateSimilar(mAllocator, aLayersBackend, aFlags, aAllocFlags); + UnlockActor(); + + if (!data) { + return nullptr; + } + + return MakeAndAddRef<TextureClient>(data, aFlags, mAllocator); +} + +gfx::DrawTarget* TextureClient::BorrowDrawTarget() { + MOZ_ASSERT(IsValid()); + MOZ_ASSERT(mIsLocked); + // TODO- We can't really assert that at the moment because there is code that + // Borrows the DrawTarget, just to get a snapshot, which is legit in term of + // OpenMode but we should have a way to get a SourceSurface directly instead. + // MOZ_ASSERT(mOpenMode & OpenMode::OPEN_WRITE); + + if (!IsValid() || !mIsLocked) { + return nullptr; + } + + if (!mBorrowedDrawTarget) { + mBorrowedDrawTarget = mData->BorrowDrawTarget(); +#ifdef DEBUG + mExpectedDtRefs = mBorrowedDrawTarget ? mBorrowedDrawTarget->refCount() : 0; +#endif + } + + return mBorrowedDrawTarget; +} + +already_AddRefed<gfx::SourceSurface> TextureClient::BorrowSnapshot() { + MOZ_ASSERT(mIsLocked); + + RefPtr<gfx::SourceSurface> surface = mData->BorrowSnapshot(); + if (!surface) { + surface = BorrowDrawTarget()->Snapshot(); + } + + return surface.forget(); +} + +bool TextureClient::BorrowMappedData(MappedTextureData& aMap) { + MOZ_ASSERT(IsValid()); + + // TODO - SharedRGBImage just accesses the buffer without properly locking + // the texture. It's bad. + // MOZ_ASSERT(mIsLocked); + // if (!mIsLocked) { + // return nullptr; + //} + + return mData ? mData->BorrowMappedData(aMap) : false; +} + +bool TextureClient::BorrowMappedYCbCrData(MappedYCbCrTextureData& aMap) { + MOZ_ASSERT(IsValid()); + + return mData ? mData->BorrowMappedYCbCrData(aMap) : false; +} + +bool TextureClient::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) { + MOZ_ASSERT(IsValid()); + + return mData ? mData->Serialize(aOutDescriptor) : false; +} + +// static +PTextureChild* TextureClient::CreateIPDLActor() { + TextureChild* c = new TextureChild(); + c->AddIPDLReference(); + return c; +} + +// static +bool TextureClient::DestroyIPDLActor(PTextureChild* actor) { + static_cast<TextureChild*>(actor)->ReleaseIPDLReference(); + return true; +} + +// static +already_AddRefed<TextureClient> TextureClient::AsTextureClient( + PTextureChild* actor) { + if (!actor) { + return nullptr; + } + + TextureChild* tc = static_cast<TextureChild*>(actor); + + tc->Lock(); + + // Since TextureClient may be destroyed asynchronously with respect to its + // IPDL actor, we must acquire a reference within a lock. The mDestroyed bit + // tells us whether or not the main thread has disconnected the TextureClient + // from its actor. + if (tc->mDestroyed) { + tc->Unlock(); + return nullptr; + } + + RefPtr<TextureClient> texture = tc->mTextureClient; + tc->Unlock(); + + return texture.forget(); +} + +bool TextureClient::IsSharedWithCompositor() const { + return mActor && mActor->IPCOpen(); +} + +void TextureClient::AddFlags(TextureFlags aFlags) { + MOZ_ASSERT( + !IsSharedWithCompositor() || + ((GetFlags() & TextureFlags::RECYCLE) && !IsAddedToCompositableClient())); + mFlags |= aFlags; +} + +void TextureClient::RemoveFlags(TextureFlags aFlags) { + MOZ_ASSERT( + !IsSharedWithCompositor() || + ((GetFlags() & TextureFlags::RECYCLE) && !IsAddedToCompositableClient())); + mFlags &= ~aFlags; +} + +void TextureClient::RecycleTexture(TextureFlags aFlags) { + MOZ_ASSERT(GetFlags() & TextureFlags::RECYCLE); + MOZ_ASSERT(!mIsLocked); + + mAddedToCompositableClient = false; + if (mFlags != aFlags) { + mFlags = aFlags; + } +} + +void TextureClient::SetAddedToCompositableClient() { + if (!mAddedToCompositableClient) { + mAddedToCompositableClient = true; + if (!(GetFlags() & TextureFlags::RECYCLE)) { + return; + } + MOZ_ASSERT(!mIsLocked); + LockActor(); + if (IsValid() && mActor && !mActor->mDestroyed && mActor->IPCOpen()) { + mActor->SendRecycleTexture(mFlags); + } + UnlockActor(); + } +} + +static void CancelTextureClientNotifyNotUsed(uint64_t aTextureId, + LayersIPCChannel* aAllocator) { + if (!aAllocator) { + return; + } + nsCOMPtr<nsISerialEventTarget> thread = aAllocator->GetThread(); + if (!thread) { + return; + } + if (thread->IsOnCurrentThread()) { + aAllocator->CancelWaitForNotifyNotUsed(aTextureId); + } else { + thread->Dispatch(NewRunnableFunction( + "CancelTextureClientNotifyNotUsedRunnable", + CancelTextureClientNotifyNotUsed, aTextureId, aAllocator)); + } +} + +void TextureClient::CancelWaitForNotifyNotUsed() { + if (GetFlags() & TextureFlags::RECYCLE) { + CancelTextureClientNotifyNotUsed(mSerial, GetAllocator()); + return; + } +} + +/* static */ +void TextureClient::TextureClientRecycleCallback(TextureClient* aClient, + void* aClosure) { + MOZ_ASSERT(aClient->GetRecycleAllocator()); + aClient->GetRecycleAllocator()->RecycleTextureClient(aClient); +} + +void TextureClient::SetRecycleAllocator( + ITextureClientRecycleAllocator* aAllocator) { + mRecycleAllocator = aAllocator; + if (aAllocator) { + SetRecycleCallback(TextureClientRecycleCallback, nullptr); + } else { + ClearRecycleCallback(); + } +} + +bool TextureClient::InitIPDLActor(CompositableForwarder* aForwarder) { + MOZ_ASSERT(aForwarder && aForwarder->GetTextureForwarder()->GetThread() == + mAllocator->GetThread()); + + if (mActor && !mActor->IPCOpen()) { + return false; + } + + if (mActor && !mActor->mDestroyed) { + CompositableForwarder* currentFwd = mActor->mCompositableForwarder; + TextureForwarder* currentTexFwd = mActor->mTextureForwarder; + if (currentFwd != aForwarder) { + // It's a bit iffy but right now ShadowLayerForwarder inherits + // TextureForwarder even though it should not. + // ShadowLayerForwarder::GetTextureForwarder actually returns a pointer to + // the CompositorBridgeChild. It's Ok for a texture to move from a + // ShadowLayerForwarder to another, but not form a CompositorBridgeChild + // to another (they use different channels). + if (currentTexFwd && currentTexFwd != aForwarder->GetTextureForwarder()) { + gfxCriticalError() + << "Attempt to move a texture to a different channel CF."; + MOZ_ASSERT_UNREACHABLE("unexpected to be called"); + return false; + } + if (currentFwd && currentFwd->GetCompositorBackendType() != + aForwarder->GetCompositorBackendType()) { + gfxCriticalError() + << "Attempt to move a texture to different compositor backend."; + MOZ_ASSERT_UNREACHABLE("unexpected to be called"); + return false; + } + if (ShadowLayerForwarder* forwarder = aForwarder->AsLayerForwarder()) { + // Do the DOM labeling. + if (nsISerialEventTarget* target = forwarder->GetEventTarget()) { + forwarder->GetCompositorBridgeChild()->ReplaceEventTargetForActor( + mActor, target); + } + } + mActor->mCompositableForwarder = aForwarder; + mActor->mUsesImageBridge = + aForwarder->GetTextureForwarder()->UsesImageBridge(); + } + return true; + } + MOZ_ASSERT(!mActor || mActor->mDestroyed, + "Cannot use a texture on several IPC channels."); + + SurfaceDescriptor desc; + if (!ToSurfaceDescriptor(desc)) { + return false; + } + + // Try external image id allocation. + mExternalImageId = + aForwarder->GetTextureForwarder()->GetNextExternalImageId(); + + nsISerialEventTarget* target = nullptr; + // Get the layers id if the forwarder is a ShadowLayerForwarder. + if (ShadowLayerForwarder* forwarder = aForwarder->AsLayerForwarder()) { + target = forwarder->GetEventTarget(); + } + + ReadLockDescriptor readLockDescriptor = null_t(); + if (mReadLock) { + mReadLock->Serialize(readLockDescriptor, GetAllocator()->GetParentPid()); + } + + PTextureChild* actor = aForwarder->GetTextureForwarder()->CreateTexture( + desc, readLockDescriptor, aForwarder->GetCompositorBackendType(), + GetFlags(), mSerial, mExternalImageId, target); + + if (!actor) { + gfxCriticalNote << static_cast<int32_t>(desc.type()) << ", " + << static_cast<int32_t>( + aForwarder->GetCompositorBackendType()) + << ", " << static_cast<uint32_t>(GetFlags()) << ", " + << mSerial; + return false; + } + + mActor = static_cast<TextureChild*>(actor); + mActor->mCompositableForwarder = aForwarder; + mActor->mTextureForwarder = aForwarder->GetTextureForwarder(); + mActor->mTextureClient = this; + mActor->mMainThreadOnly = !!(mFlags & TextureFlags::DEALLOCATE_MAIN_THREAD); + + // If the TextureClient is already locked, we have to lock TextureChild's + // mutex since it will be unlocked in TextureClient::Unlock. + if (mIsLocked) { + LockActor(); + } + + return mActor->IPCOpen(); +} + +bool TextureClient::InitIPDLActor(KnowsCompositor* aKnowsCompositor) { + MOZ_ASSERT(aKnowsCompositor && + aKnowsCompositor->GetTextureForwarder()->GetThread() == + mAllocator->GetThread()); + TextureForwarder* fwd = aKnowsCompositor->GetTextureForwarder(); + if (mActor && !mActor->mDestroyed) { + CompositableForwarder* currentFwd = mActor->mCompositableForwarder; + TextureForwarder* currentTexFwd = mActor->mTextureForwarder; + + if (currentFwd) { + gfxCriticalError() + << "Attempt to remove a texture from a CompositableForwarder."; + return false; + } + + if (currentTexFwd && currentTexFwd != fwd) { + gfxCriticalError() + << "Attempt to move a texture to a different channel TF."; + return false; + } + mActor->mTextureForwarder = fwd; + return true; + } + MOZ_ASSERT(!mActor || mActor->mDestroyed, + "Cannot use a texture on several IPC channels."); + + SurfaceDescriptor desc; + if (!ToSurfaceDescriptor(desc)) { + return false; + } + + // Try external image id allocation. + mExternalImageId = + aKnowsCompositor->GetTextureForwarder()->GetNextExternalImageId(); + + ReadLockDescriptor readLockDescriptor = null_t(); + if (mReadLock) { + mReadLock->Serialize(readLockDescriptor, GetAllocator()->GetParentPid()); + } + + PTextureChild* actor = fwd->CreateTexture( + desc, readLockDescriptor, aKnowsCompositor->GetCompositorBackendType(), + GetFlags(), mSerial, mExternalImageId); + if (!actor) { + gfxCriticalNote << static_cast<int32_t>(desc.type()) << ", " + << static_cast<int32_t>( + aKnowsCompositor->GetCompositorBackendType()) + << ", " << static_cast<uint32_t>(GetFlags()) << ", " + << mSerial; + return false; + } + + mActor = static_cast<TextureChild*>(actor); + mActor->mTextureForwarder = fwd; + mActor->mTextureClient = this; + mActor->mMainThreadOnly = !!(mFlags & TextureFlags::DEALLOCATE_MAIN_THREAD); + + // If the TextureClient is already locked, we have to lock TextureChild's + // mutex since it will be unlocked in TextureClient::Unlock. + if (mIsLocked) { + LockActor(); + } + + return mActor->IPCOpen(); +} + +PTextureChild* TextureClient::GetIPDLActor() { return mActor; } + +// static +already_AddRefed<TextureClient> TextureClient::CreateForDrawing( + KnowsCompositor* aAllocator, gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + if (aAllocator->SupportsTextureDirectMapping() && + std::max(aSize.width, aSize.height) <= aAllocator->GetMaxTextureSize()) { + aAllocFlags = + TextureAllocationFlags(aAllocFlags | ALLOC_ALLOW_DIRECT_MAPPING); + } + return TextureClient::CreateForDrawing(aAllocator->GetTextureForwarder(), + aFormat, aSize, aAllocator, aSelector, + aTextureFlags, aAllocFlags); +} + +// static +already_AddRefed<TextureClient> TextureClient::CreateForDrawing( + TextureForwarder* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + LayersBackend layersBackend = aKnowsCompositor->GetCompositorBackendType(); + gfx::BackendType moz2DBackend = + BackendTypeForBackendSelector(layersBackend, aSelector); + + // also test the validity of aAllocator + if (!aAllocator || !aAllocator->IPCOpen()) { + return nullptr; + } + + if (!gfx::Factory::AllowedSurfaceSize(aSize)) { + return nullptr; + } + + TextureData* data = + TextureData::Create(aAllocator, aFormat, aSize, aKnowsCompositor, + aSelector, aTextureFlags, aAllocFlags); + + if (data) { + return MakeAndAddRef<TextureClient>(data, aTextureFlags, aAllocator); + } + + // Can't do any better than a buffer texture client. + return TextureClient::CreateForRawBufferAccess(aAllocator, aFormat, aSize, + moz2DBackend, layersBackend, + aTextureFlags, aAllocFlags); +} + +// static +already_AddRefed<TextureClient> TextureClient::CreateFromSurface( + KnowsCompositor* aAllocator, gfx::SourceSurface* aSurface, + BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + // also test the validity of aAllocator + if (!aAllocator || !aAllocator->GetTextureForwarder()->IPCOpen()) { + return nullptr; + } + + gfx::IntSize size = aSurface->GetSize(); + + if (!gfx::Factory::AllowedSurfaceSize(size)) { + return nullptr; + } + + TextureData* data = nullptr; +#if defined(XP_WIN) + LayersBackend layersBackend = aAllocator->GetCompositorBackendType(); + gfx::BackendType moz2DBackend = + BackendTypeForBackendSelector(layersBackend, aSelector); + + int32_t maxTextureSize = aAllocator->GetMaxTextureSize(); + + if ((layersBackend == LayersBackend::LAYERS_D3D11 || + layersBackend == LayersBackend::LAYERS_WR) && + (moz2DBackend == gfx::BackendType::DIRECT2D || + moz2DBackend == gfx::BackendType::DIRECT2D1_1 || + (!!(aAllocFlags & ALLOC_FOR_OUT_OF_BAND_CONTENT) && + DeviceManagerDx::Get()->GetContentDevice())) && + size.width <= maxTextureSize && size.height <= maxTextureSize) { + data = D3D11TextureData::Create(aSurface, aAllocFlags); + } +#endif + + if (data) { + return MakeAndAddRef<TextureClient>(data, aTextureFlags, + aAllocator->GetTextureForwarder()); + } + + // Fall back to using UpdateFromSurface + + TextureAllocationFlags allocFlags = + TextureAllocationFlags(aAllocFlags | ALLOC_UPDATE_FROM_SURFACE); + RefPtr<TextureClient> client = + CreateForDrawing(aAllocator, aSurface->GetFormat(), size, aSelector, + aTextureFlags, allocFlags); + if (!client) { + return nullptr; + } + + TextureClientAutoLock autoLock(client, OpenMode::OPEN_WRITE_ONLY); + if (!autoLock.Succeeded()) { + return nullptr; + } + + client->UpdateFromSurface(aSurface); + return client.forget(); +} + +// static +already_AddRefed<TextureClient> TextureClient::CreateForRawBufferAccess( + KnowsCompositor* aAllocator, gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + gfx::BackendType aMoz2DBackend, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + // If we exceed the max texture size for the GPU, then just fall back to no + // texture direct mapping. If it becomes a problem we can implement tiling + // logic inside DirectMapTextureSource to allow this. + bool supportsTextureDirectMapping = + aAllocator->SupportsTextureDirectMapping() && + std::max(aSize.width, aSize.height) <= aAllocator->GetMaxTextureSize(); + if (supportsTextureDirectMapping) { + aAllocFlags = + TextureAllocationFlags(aAllocFlags | ALLOC_ALLOW_DIRECT_MAPPING); + } else { + aAllocFlags = + TextureAllocationFlags(aAllocFlags & ~ALLOC_ALLOW_DIRECT_MAPPING); + } + return CreateForRawBufferAccess( + aAllocator->GetTextureForwarder(), aFormat, aSize, aMoz2DBackend, + aAllocator->GetCompositorBackendType(), aTextureFlags, aAllocFlags); +} + +// static +already_AddRefed<TextureClient> TextureClient::CreateForRawBufferAccess( + LayersIPCChannel* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, gfx::BackendType aMoz2DBackend, + LayersBackend aLayersBackend, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags) { + // also test the validity of aAllocator + if (!aAllocator || !aAllocator->IPCOpen()) { + return nullptr; + } + + if (aAllocFlags & ALLOC_DISALLOW_BUFFERTEXTURECLIENT) { + return nullptr; + } + + if (!gfx::Factory::AllowedSurfaceSize(aSize)) { + return nullptr; + } + + if (aFormat == SurfaceFormat::B8G8R8X8) { + // Skia doesn't support RGBX, so ensure we clear the buffer for the proper + // alpha values. + aAllocFlags = TextureAllocationFlags(aAllocFlags | ALLOC_CLEAR_BUFFER); + } + + // Note that we ignore the backend type if we get here. It should only be D2D + // or Skia, and D2D does not support data surfaces. Therefore it is safe to + // force the buffer to be Skia. + NS_WARNING_ASSERTION(aMoz2DBackend == gfx::BackendType::SKIA || + aMoz2DBackend == gfx::BackendType::DIRECT2D || + aMoz2DBackend == gfx::BackendType::DIRECT2D1_1, + "Unsupported TextureClient backend type"); + + TextureData* texData = BufferTextureData::Create( + aSize, aFormat, gfx::BackendType::SKIA, aLayersBackend, aTextureFlags, + aAllocFlags, aAllocator); + if (!texData) { + return nullptr; + } + + return MakeAndAddRef<TextureClient>(texData, aTextureFlags, aAllocator); +} + +// static +already_AddRefed<TextureClient> TextureClient::CreateForYCbCr( + KnowsCompositor* aAllocator, const gfx::IntRect& aDisplay, + const gfx::IntSize& aYSize, uint32_t aYStride, + const gfx::IntSize& aCbCrSize, uint32_t aCbCrStride, StereoMode aStereoMode, + gfx::ColorDepth aColorDepth, gfx::YUVColorSpace aYUVColorSpace, + gfx::ColorRange aColorRange, TextureFlags aTextureFlags) { + if (!aAllocator || !aAllocator->GetLayersIPCActor()->IPCOpen()) { + return nullptr; + } + + if (!gfx::Factory::AllowedSurfaceSize(aYSize)) { + return nullptr; + } + + TextureData* data = BufferTextureData::CreateForYCbCr( + aAllocator, aDisplay, aYSize, aYStride, aCbCrSize, aCbCrStride, + aStereoMode, aColorDepth, aYUVColorSpace, aColorRange, aTextureFlags); + if (!data) { + return nullptr; + } + + return MakeAndAddRef<TextureClient>(data, aTextureFlags, + aAllocator->GetTextureForwarder()); +} + +TextureClient::TextureClient(TextureData* aData, TextureFlags aFlags, + LayersIPCChannel* aAllocator) + : AtomicRefCountedWithFinalize("TextureClient"), + mAllocator(aAllocator), + mActor(nullptr), + mData(aData), + mFlags(aFlags), + mOpenMode(OpenMode::OPEN_NONE) +#ifdef DEBUG + , + mExpectedDtRefs(0) +#endif + , + mIsLocked(false), + mIsReadLocked(false), + mUpdated(false), + mAddedToCompositableClient(false), + mWorkaroundAnnoyingSharedSurfaceLifetimeIssues(false), + mWorkaroundAnnoyingSharedSurfaceOwnershipIssues(false), + mFwdTransactionId(0), + mSerial(++sSerialCounter) +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL + , + mPoolTracker(nullptr) +#endif +{ + mData->FillInfo(mInfo); + mFlags |= mData->GetTextureFlags(); + + if (mFlags & TextureFlags::NON_BLOCKING_READ_LOCK) { + MOZ_ASSERT(!(mFlags & TextureFlags::BLOCKING_READ_LOCK)); + EnableReadLock(); + } else if (mFlags & TextureFlags::BLOCKING_READ_LOCK) { + MOZ_ASSERT(!(mFlags & TextureFlags::NON_BLOCKING_READ_LOCK)); + EnableBlockingReadLock(); + } +} + +bool TextureClient::CopyToTextureClient(TextureClient* aTarget, + const gfx::IntRect* aRect, + const gfx::IntPoint* aPoint) { + MOZ_ASSERT(IsLocked()); + MOZ_ASSERT(aTarget->IsLocked()); + + if (!aTarget->CanExposeDrawTarget() || !CanExposeDrawTarget()) { + return false; + } + + RefPtr<DrawTarget> destinationTarget = aTarget->BorrowDrawTarget(); + if (!destinationTarget) { + gfxWarning() << "TextureClient::CopyToTextureClient (dest) failed in " + "BorrowDrawTarget"; + return false; + } + + RefPtr<DrawTarget> sourceTarget = BorrowDrawTarget(); + if (!sourceTarget) { + gfxWarning() << "TextureClient::CopyToTextureClient (src) failed in " + "BorrowDrawTarget"; + return false; + } + + RefPtr<gfx::SourceSurface> source = sourceTarget->Snapshot(); + destinationTarget->CopySurface( + source, aRect ? *aRect : gfx::IntRect(gfx::IntPoint(0, 0), GetSize()), + aPoint ? *aPoint : gfx::IntPoint(0, 0)); + return true; +} + +already_AddRefed<gfx::DataSourceSurface> TextureClient::GetAsSurface() { + if (!Lock(OpenMode::OPEN_READ)) { + return nullptr; + } + RefPtr<gfx::DataSourceSurface> data; + { // scope so that the DrawTarget is destroyed before Unlock() + RefPtr<gfx::DrawTarget> dt = BorrowDrawTarget(); + if (dt) { + RefPtr<gfx::SourceSurface> surf = dt->Snapshot(); + if (surf) { + data = surf->GetDataSurface(); + } + } + } + Unlock(); + return data.forget(); +} + +void TextureClient::PrintInfo(std::stringstream& aStream, const char* aPrefix) { + aStream << aPrefix; + aStream << nsPrintfCString("TextureClient (0x%p)", this).get() + << " [size=" << GetSize() << "]" + << " [format=" << GetFormat() << "]" + << " [flags=" << mFlags << "]"; + +#ifdef MOZ_DUMP_PAINTING + if (StaticPrefs::layers_dump_texture()) { + nsAutoCString pfx(aPrefix); + pfx += " "; + + aStream << "\n" << pfx.get() << "Surface: "; + RefPtr<gfx::DataSourceSurface> dSurf = GetAsSurface(); + if (dSurf) { + aStream << gfxUtils::GetAsLZ4Base64Str(dSurf).get(); + } + } +#endif +} + +void TextureClient::GetSurfaceDescriptorRemoteDecoder( + SurfaceDescriptorRemoteDecoder* const aOutDesc) { + const auto handle = GetSerial(); + + RemoteDecoderVideoSubDescriptor subDesc = null_t(); + MOZ_RELEASE_ASSERT(mData); + mData->GetSubDescriptor(&subDesc); + + *aOutDesc = + SurfaceDescriptorRemoteDecoder(handle, std::move(subDesc), Nothing()); +} + +class MemoryTextureReadLock : public NonBlockingTextureReadLock { + public: + MemoryTextureReadLock(); + + virtual ~MemoryTextureReadLock(); + + bool ReadLock() override; + + int32_t ReadUnlock() override; + + int32_t GetReadCount() override; + + LockType GetType() override { return TYPE_NONBLOCKING_MEMORY; } + + bool IsValid() const override { return true; }; + + bool Serialize(ReadLockDescriptor& aOutput, base::ProcessId aOther) override; + + Atomic<int32_t> mReadCount; +}; + +// The cross-prcess implementation of TextureReadLock. +// +// Since we don't use cross-process reference counting for the ReadLock objects, +// we use the lock's internal counter as a way to know when to deallocate the +// underlying shmem section: when the counter is equal to 1, it means that the +// lock is not "held" (the texture is writable), when the counter is equal to 0 +// it means that we can safely deallocate the shmem section without causing a +// race condition with the other process. +class ShmemTextureReadLock : public NonBlockingTextureReadLock { + public: + struct ShmReadLockInfo { + int32_t readCount; + }; + + explicit ShmemTextureReadLock(LayersIPCChannel* aAllocator); + + virtual ~ShmemTextureReadLock(); + + bool ReadLock() override; + + int32_t ReadUnlock() override; + + int32_t GetReadCount() override; + + bool IsValid() const override { return mAllocSuccess; }; + + LockType GetType() override { return TYPE_NONBLOCKING_SHMEM; } + + bool Serialize(ReadLockDescriptor& aOutput, base::ProcessId aOther) override; + + mozilla::layers::ShmemSection& GetShmemSection() { return mShmemSection; } + + explicit ShmemTextureReadLock( + const mozilla::layers::ShmemSection& aShmemSection) + : mShmemSection(aShmemSection), mAllocSuccess(true) { + MOZ_COUNT_CTOR(ShmemTextureReadLock); + } + + ShmReadLockInfo* GetShmReadLockInfoPtr() { + return reinterpret_cast<ShmReadLockInfo*>( + mShmemSection.shmem().get<char>() + mShmemSection.offset()); + } + + RefPtr<LayersIPCChannel> mClientAllocator; + mozilla::layers::ShmemSection mShmemSection; + bool mAllocSuccess; +}; + +class CrossProcessSemaphoreReadLock : public TextureReadLock { + public: + CrossProcessSemaphoreReadLock() + : mSemaphore(CrossProcessSemaphore::Create("TextureReadLock", 1)), + mShared(false) {} + explicit CrossProcessSemaphoreReadLock(CrossProcessSemaphoreHandle aHandle) + : mSemaphore(CrossProcessSemaphore::Create(aHandle)), mShared(false) {} + + bool ReadLock() override { + if (!IsValid()) { + return false; + } + return mSemaphore->Wait(); + } + bool TryReadLock(TimeDuration aTimeout) override { + if (!IsValid()) { + return false; + } + return mSemaphore->Wait(Some(aTimeout)); + } + int32_t ReadUnlock() override { + if (!IsValid()) { + return 1; + } + mSemaphore->Signal(); + return 1; + } + bool IsValid() const override { return !!mSemaphore; } + + bool Serialize(ReadLockDescriptor& aOutput, base::ProcessId aOther) override; + + LockType GetType() override { return TYPE_CROSS_PROCESS_SEMAPHORE; } + + UniquePtr<CrossProcessSemaphore> mSemaphore; + bool mShared; +}; + +// static +already_AddRefed<TextureReadLock> TextureReadLock::Deserialize( + const ReadLockDescriptor& aDescriptor, ISurfaceAllocator* aAllocator) { + switch (aDescriptor.type()) { + case ReadLockDescriptor::TShmemSection: { + const ShmemSection& section = aDescriptor.get_ShmemSection(); + MOZ_RELEASE_ASSERT(section.shmem().IsReadable()); + return MakeAndAddRef<ShmemTextureReadLock>(section); + } + case ReadLockDescriptor::Tuintptr_t: { + if (!aAllocator->IsSameProcess()) { + // Trying to use a memory based lock instead of a shmem based one in + // the cross-process case is a bad security violation. + NS_ERROR( + "A client process may be trying to peek at the host's address " + "space!"); + return nullptr; + } + RefPtr<TextureReadLock> lock = + reinterpret_cast<MemoryTextureReadLock*>(aDescriptor.get_uintptr_t()); + + MOZ_ASSERT(lock); + if (lock) { + // The corresponding AddRef is in MemoryTextureReadLock::Serialize + lock.get()->Release(); + } + + return lock.forget(); + } + case ReadLockDescriptor::TCrossProcessSemaphoreDescriptor: { + return MakeAndAddRef<CrossProcessSemaphoreReadLock>( + aDescriptor.get_CrossProcessSemaphoreDescriptor().sem()); + } + case ReadLockDescriptor::Tnull_t: { + return nullptr; + } + default: { + // Invalid descriptor. + MOZ_DIAGNOSTIC_ASSERT(false); + } + } + return nullptr; +} +// static +already_AddRefed<TextureReadLock> NonBlockingTextureReadLock::Create( + LayersIPCChannel* aAllocator) { + if (aAllocator->IsSameProcess()) { + // If our compositor is in the same process, we can save some cycles by not + // using shared memory. + return MakeAndAddRef<MemoryTextureReadLock>(); + } + + return MakeAndAddRef<ShmemTextureReadLock>(aAllocator); +} + +MemoryTextureReadLock::MemoryTextureReadLock() : mReadCount(1) { + MOZ_COUNT_CTOR(MemoryTextureReadLock); +} + +MemoryTextureReadLock::~MemoryTextureReadLock() { + // One read count that is added in constructor. + MOZ_ASSERT(mReadCount == 1); + MOZ_COUNT_DTOR(MemoryTextureReadLock); +} + +bool MemoryTextureReadLock::Serialize(ReadLockDescriptor& aOutput, + base::ProcessId aOther) { + // AddRef here and Release when receiving on the host side to make sure the + // reference count doesn't go to zero before the host receives the message. + // see TextureReadLock::Deserialize + this->AddRef(); + aOutput = ReadLockDescriptor(uintptr_t(this)); + return true; +} + +bool MemoryTextureReadLock::ReadLock() { + NS_ASSERT_OWNINGTHREAD(MemoryTextureReadLock); + + ++mReadCount; + return true; +} + +int32_t MemoryTextureReadLock::ReadUnlock() { + int32_t readCount = --mReadCount; + MOZ_ASSERT(readCount >= 0); + + return readCount; +} + +int32_t MemoryTextureReadLock::GetReadCount() { + NS_ASSERT_OWNINGTHREAD(MemoryTextureReadLock); + return mReadCount; +} + +ShmemTextureReadLock::ShmemTextureReadLock(LayersIPCChannel* aAllocator) + : mClientAllocator(aAllocator), mAllocSuccess(false) { + MOZ_COUNT_CTOR(ShmemTextureReadLock); + MOZ_ASSERT(mClientAllocator); + MOZ_ASSERT(mClientAllocator->GetTileLockAllocator()); +#define MOZ_ALIGN_WORD(x) (((x) + 3) & ~3) + if (mClientAllocator->GetTileLockAllocator()->AllocShmemSection( + MOZ_ALIGN_WORD(sizeof(ShmReadLockInfo)), &mShmemSection)) { + ShmReadLockInfo* info = GetShmReadLockInfoPtr(); + info->readCount = 1; + mAllocSuccess = true; + } +} + +ShmemTextureReadLock::~ShmemTextureReadLock() { + if (mClientAllocator) { + // Release one read count that is added in constructor. + // The count is kept for calling GetReadCount() by TextureClientPool. + ReadUnlock(); + } + MOZ_COUNT_DTOR(ShmemTextureReadLock); +} + +bool ShmemTextureReadLock::Serialize(ReadLockDescriptor& aOutput, + base::ProcessId aOther) { + aOutput = ReadLockDescriptor(GetShmemSection()); + return true; +} + +bool ShmemTextureReadLock::ReadLock() { + NS_ASSERT_OWNINGTHREAD(ShmemTextureReadLock); + if (!mAllocSuccess) { + return false; + } + ShmReadLockInfo* info = GetShmReadLockInfoPtr(); + PR_ATOMIC_INCREMENT(&info->readCount); + return true; +} + +int32_t ShmemTextureReadLock::ReadUnlock() { + if (!mAllocSuccess) { + return 0; + } + ShmReadLockInfo* info = GetShmReadLockInfoPtr(); + int32_t readCount = PR_ATOMIC_DECREMENT(&info->readCount); + MOZ_ASSERT(readCount >= 0); + if (readCount <= 0) { + if (mClientAllocator && mClientAllocator->GetTileLockAllocator()) { + mClientAllocator->GetTileLockAllocator()->DeallocShmemSection( + mShmemSection); + } else { + // we are on the compositor process, or IPC is down. + FixedSizeSmallShmemSectionAllocator::FreeShmemSection(mShmemSection); + } + } + return readCount; +} + +int32_t ShmemTextureReadLock::GetReadCount() { + NS_ASSERT_OWNINGTHREAD(ShmemTextureReadLock); + if (!mAllocSuccess) { + return 0; + } + ShmReadLockInfo* info = GetShmReadLockInfoPtr(); + return info->readCount; +} + +bool CrossProcessSemaphoreReadLock::Serialize(ReadLockDescriptor& aOutput, + base::ProcessId aOther) { + if (!mShared && IsValid()) { + aOutput = ReadLockDescriptor( + CrossProcessSemaphoreDescriptor(mSemaphore->ShareToProcess(aOther))); + mSemaphore->CloseHandle(); + mShared = true; + return true; + } else { + return mShared; + } +} + +void TextureClient::EnableBlockingReadLock() { + if (!mReadLock) { + mReadLock = new CrossProcessSemaphoreReadLock(); + } +} + +void TextureClient::AddPaintThreadRef() { + MOZ_ASSERT(NS_IsMainThread()); + mPaintThreadRefs += 1; +} + +void TextureClient::DropPaintThreadRef() { + MOZ_RELEASE_ASSERT(PaintThread::Get()->IsOnPaintWorkerThread()); + MOZ_RELEASE_ASSERT(mPaintThreadRefs >= 1); + mPaintThreadRefs -= 1; +} + +bool UpdateYCbCrTextureClient(TextureClient* aTexture, + const PlanarYCbCrData& aData) { + MOZ_ASSERT(aTexture); + MOZ_ASSERT(aTexture->IsLocked()); + MOZ_ASSERT(aTexture->GetFormat() == gfx::SurfaceFormat::YUV, + "This textureClient can only use YCbCr data"); + MOZ_ASSERT(!aTexture->IsImmutable()); + MOZ_ASSERT(aTexture->IsValid()); + MOZ_ASSERT(aData.mCbSkip == aData.mCrSkip); + + MappedYCbCrTextureData mapped; + if (!aTexture->BorrowMappedYCbCrData(mapped)) { + NS_WARNING("Failed to extract YCbCr info!"); + return false; + } + + uint32_t bytesPerPixel = + BytesPerPixel(SurfaceFormatForColorDepth(aData.mColorDepth)); + MappedYCbCrTextureData srcData; + srcData.y.data = aData.mYChannel; + srcData.y.size = aData.mYSize; + srcData.y.stride = aData.mYStride; + srcData.y.skip = aData.mYSkip; + srcData.y.bytesPerPixel = bytesPerPixel; + srcData.cb.data = aData.mCbChannel; + srcData.cb.size = aData.mCbCrSize; + srcData.cb.stride = aData.mCbCrStride; + srcData.cb.skip = aData.mCbSkip; + srcData.cb.bytesPerPixel = bytesPerPixel; + srcData.cr.data = aData.mCrChannel; + srcData.cr.size = aData.mCbCrSize; + srcData.cr.stride = aData.mCbCrStride; + srcData.cr.skip = aData.mCrSkip; + srcData.cr.bytesPerPixel = bytesPerPixel; + srcData.metadata = nullptr; + + if (!srcData.CopyInto(mapped)) { + NS_WARNING("Failed to copy image data!"); + return false; + } + + if (TextureRequiresLocking(aTexture->GetFlags())) { + // We don't have support for proper locking yet, so we'll + // have to be immutable instead. + aTexture->MarkImmutable(); + } + return true; +} + +already_AddRefed<TextureClient> TextureClient::CreateWithData( + TextureData* aData, TextureFlags aFlags, LayersIPCChannel* aAllocator) { + if (!aData) { + return nullptr; + } + return MakeAndAddRef<TextureClient>(aData, aFlags, aAllocator); +} + +template <class PixelDataType> +static void copyData(PixelDataType* aDst, + const MappedYCbCrChannelData& aChannelDst, + PixelDataType* aSrc, + const MappedYCbCrChannelData& aChannelSrc) { + uint8_t* srcByte = reinterpret_cast<uint8_t*>(aSrc); + const int32_t srcSkip = aChannelSrc.skip + 1; + uint8_t* dstByte = reinterpret_cast<uint8_t*>(aDst); + const int32_t dstSkip = aChannelDst.skip + 1; + for (int32_t i = 0; i < aChannelSrc.size.height; ++i) { + for (int32_t j = 0; j < aChannelSrc.size.width; ++j) { + *aDst = *aSrc; + aSrc += srcSkip; + aDst += dstSkip; + } + srcByte += aChannelSrc.stride; + aSrc = reinterpret_cast<PixelDataType*>(srcByte); + dstByte += aChannelDst.stride; + aDst = reinterpret_cast<PixelDataType*>(dstByte); + } +} + +bool MappedYCbCrChannelData::CopyInto(MappedYCbCrChannelData& aDst) { + if (!data || !aDst.data || size != aDst.size) { + return false; + } + + if (stride == aDst.stride && skip == aDst.skip) { + // fast path! + // We assume that the padding in the destination is there for alignment + // purposes and doesn't contain useful data. + memcpy(aDst.data, data, stride * size.height); + return true; + } + + if (aDst.skip == 0 && skip == 0) { + // fast-ish path + for (int32_t i = 0; i < size.height; ++i) { + memcpy(aDst.data + i * aDst.stride, data + i * stride, + size.width * bytesPerPixel); + } + return true; + } + + MOZ_ASSERT(bytesPerPixel == 1 || bytesPerPixel == 2); + // slow path + if (bytesPerPixel == 1) { + copyData(aDst.data, aDst, data, *this); + } else if (bytesPerPixel == 2) { + copyData(reinterpret_cast<uint16_t*>(aDst.data), aDst, + reinterpret_cast<uint16_t*>(data), *this); + } + return true; +} + +} // namespace mozilla::layers diff --git a/gfx/layers/client/TextureClient.h b/gfx/layers/client/TextureClient.h new file mode 100644 index 0000000000..e8812bf6d3 --- /dev/null +++ b/gfx/layers/client/TextureClient.h @@ -0,0 +1,932 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_TEXTURECLIENT_H +#define MOZILLA_GFX_TEXTURECLIENT_H + +#include <stddef.h> // for size_t +#include <stdint.h> // for uint32_t, uint8_t, uint64_t + +#include "GLTextureImage.h" // for TextureImage +#include "GfxTexturesReporter.h" +#include "ImageTypes.h" // for StereoMode +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/Atomics.h" +#include "mozilla/Attributes.h" // for override +#include "mozilla/DebugOnly.h" +#include "mozilla/RefPtr.h" // for RefPtr, RefCounted +#include "mozilla/gfx/2D.h" // for DrawTarget +#include "mozilla/gfx/CriticalSection.h" +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Types.h" // for SurfaceFormat +#include "mozilla/ipc/FileDescriptor.h" +#include "mozilla/ipc/Shmem.h" // for Shmem +#include "mozilla/layers/AtomicRefCountedWithFinalize.h" +#include "mozilla/layers/CompositorTypes.h" // for TextureFlags, etc +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor +#include "mozilla/layers/LayersTypes.h" +#include "mozilla/layers/SyncObject.h" +#include "mozilla/mozalloc.h" // for operator delete +#include "mozilla/webrender/WebRenderTypes.h" +#include "nsCOMPtr.h" // for already_AddRefed +#include "nsISupportsImpl.h" // for TextureImage::AddRef, etc +#include "nsThreadUtils.h" +#include "pratom.h" + +class gfxImageSurface; +struct ID3D11Device; + +namespace mozilla { + +// When defined, we track which pool the tile came from and test for +// any inconsistencies. This can be defined in release build as well. +#ifdef DEBUG +# define GFX_DEBUG_TRACK_CLIENTS_IN_POOL 1 +#endif + +namespace layers { + +class AndroidHardwareBufferTextureData; +class BufferTextureData; +class CompositableForwarder; +class KnowsCompositor; +class LayersIPCChannel; +class CompositableClient; +struct PlanarYCbCrData; +class Image; +class PTextureChild; +class TextureChild; +class TextureData; +class GPUVideoTextureData; +struct RawTextureBuffer; +class RawYCbCrTextureBuffer; +class TextureClient; +class ITextureClientRecycleAllocator; +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL +class TextureClientPool; +#endif +class TextureForwarder; +class KeepAlive; + +/** + * TextureClient is the abstraction that allows us to share data between the + * content and the compositor side. + */ + +enum TextureAllocationFlags { + ALLOC_DEFAULT = 0, + ALLOC_CLEAR_BUFFER = + 1 << 1, // Clear the buffer to whatever is best for the draw target + ALLOC_CLEAR_BUFFER_WHITE = 1 << 2, // explicit all white + ALLOC_CLEAR_BUFFER_BLACK = 1 << 3, // explicit all black + ALLOC_DISALLOW_BUFFERTEXTURECLIENT = 1 << 4, + + // Allocate the texture for out-of-band content updates. This is mostly for + // TextureClientD3D11, which may otherwise choose D3D10 or non-KeyedMutex + // surfaces when used on the main thread. + ALLOC_FOR_OUT_OF_BAND_CONTENT = 1 << 5, + + // Disable any cross-device synchronization. This is also for + // TextureClientD3D11, and creates a texture without KeyedMutex. + ALLOC_MANUAL_SYNCHRONIZATION = 1 << 6, + + // The texture is going to be updated using UpdateFromSurface and needs to + // support that call. + ALLOC_UPDATE_FROM_SURFACE = 1 << 7, + + // In practice, this means we support the APPLE_client_storage extension, + // meaning the buffer will not be internally copied by the graphics driver. + ALLOC_ALLOW_DIRECT_MAPPING = 1 << 8, +}; + +/** + * This class may be used to asynchronously receive an update when the content + * drawn to this texture client is available for reading in CPU memory. This + * can only be used on texture clients that support draw target creation. + */ +class TextureReadbackSink { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureReadbackSink) + public: + /** + * Callback function to implement in order to receive a DataSourceSurface + * containing the data read back from the texture client. This will always + * be called on the main thread, and this may not hold on to the + * DataSourceSurface beyond the execution of this function. + */ + virtual void ProcessReadback(gfx::DataSourceSurface* aSourceSurface) = 0; + + protected: + virtual ~TextureReadbackSink() = default; +}; + +enum class BackendSelector { Content, Canvas }; + +/// Temporary object providing direct access to a Texture's memory. +/// +/// see TextureClient::CanExposeMappedData() and +/// TextureClient::BorrowMappedData(). +struct MappedTextureData { + uint8_t* data; + gfx::IntSize size; + int32_t stride; + gfx::SurfaceFormat format; +}; + +struct MappedYCbCrChannelData { + uint8_t* data; + gfx::IntSize size; + int32_t stride; + int32_t skip; + uint32_t bytesPerPixel; + + bool CopyInto(MappedYCbCrChannelData& aDst); +}; + +struct MappedYCbCrTextureData { + MappedYCbCrChannelData y; + MappedYCbCrChannelData cb; + MappedYCbCrChannelData cr; + // Sad but because of how SharedPlanarYCbCrData is used we have to expose this + // for now. + uint8_t* metadata; + StereoMode stereoMode; + + bool CopyInto(MappedYCbCrTextureData& aDst) { + return y.CopyInto(aDst.y) && cb.CopyInto(aDst.cb) && cr.CopyInto(aDst.cr); + } +}; + +class ReadLockDescriptor; +class NonBlockingTextureReadLock; + +// A class to help implement copy-on-write semantics for shared textures. +// +// A TextureClient/Host pair can opt into using a ReadLock by calling +// TextureClient::EnableReadLock. This will equip the TextureClient with a +// ReadLock object that will be automatically ReadLock()'ed by the texture +// itself when it is written into (see TextureClient::Unlock). A +// TextureReadLock's counter starts at 1 and is expected to be equal to 1 when +// the lock is destroyed. See ShmemTextureReadLock for explanations about why we +// use 1 instead of 0 as the initial state. TextureReadLock is mostly internally +// managed by the TextureClient/Host pair, and the compositable only has to +// forward it during updates. If an update message contains a null_t lock, it +// means that the texture was not written into on the content side, and there is +// no synchronization required on the compositor side (or it means that the +// texture pair did not opt into using ReadLocks). On the compositor side, the +// TextureHost can receive a ReadLock during a transaction, and will both +// ReadUnlock() it and drop it as soon as the shared data is available again for +// writing (the texture upload is done, or the compositor not reading the +// texture anymore). The lock is dropped to make sure it is ReadUnlock()'ed only +// once. +class TextureReadLock { + protected: + virtual ~TextureReadLock() = default; + + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureReadLock) + + virtual bool ReadLock() = 0; + virtual bool TryReadLock(TimeDuration aTimeout) { return ReadLock(); } + virtual int32_t ReadUnlock() = 0; + virtual bool IsValid() const = 0; + + static already_AddRefed<TextureReadLock> Deserialize( + const ReadLockDescriptor& aDescriptor, ISurfaceAllocator* aAllocator); + + virtual bool Serialize(ReadLockDescriptor& aOutput, + base::ProcessId aOther) = 0; + + enum LockType { + TYPE_NONBLOCKING_MEMORY, + TYPE_NONBLOCKING_SHMEM, + TYPE_CROSS_PROCESS_SEMAPHORE + }; + virtual LockType GetType() = 0; + + virtual NonBlockingTextureReadLock* AsNonBlockingLock() { return nullptr; } + + protected: + NS_DECL_OWNINGTHREAD +}; + +class NonBlockingTextureReadLock : public TextureReadLock { + public: + virtual int32_t GetReadCount() = 0; + + static already_AddRefed<TextureReadLock> Create(LayersIPCChannel* aAllocator); + + NonBlockingTextureReadLock* AsNonBlockingLock() override { return this; } +}; + +#ifdef XP_WIN +class D3D11TextureData; +class DXGIYCbCrTextureData; +#endif + +class TextureData { + public: + struct Info { + gfx::IntSize size; + gfx::SurfaceFormat format; + bool hasIntermediateBuffer; + bool hasSynchronization; + bool supportsMoz2D; + bool canExposeMappedData; + bool canConcurrentlyReadLock; + + Info() + : format(gfx::SurfaceFormat::UNKNOWN), + hasIntermediateBuffer(false), + hasSynchronization(false), + supportsMoz2D(false), + canExposeMappedData(false), + canConcurrentlyReadLock(true) {} + }; + + static TextureData* Create(TextureForwarder* aAllocator, + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector, + TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags); + + static bool IsRemote(KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector); + + MOZ_COUNTED_DTOR_VIRTUAL(TextureData) + + virtual void FillInfo(TextureData::Info& aInfo) const = 0; + + virtual bool Lock(OpenMode aMode) = 0; + + virtual void Unlock() = 0; + + virtual already_AddRefed<gfx::DrawTarget> BorrowDrawTarget() { + return nullptr; + } + + virtual already_AddRefed<gfx::SourceSurface> BorrowSnapshot() { + return nullptr; + } + + virtual bool BorrowMappedData(MappedTextureData&) { return false; } + + virtual bool BorrowMappedYCbCrData(MappedYCbCrTextureData&) { return false; } + + virtual void Deallocate(LayersIPCChannel* aAllocator) = 0; + + /// Depending on the texture's flags either Deallocate or Forget is called. + virtual void Forget(LayersIPCChannel* aAllocator) {} + + virtual bool Serialize(SurfaceDescriptor& aDescriptor) = 0; + virtual void GetSubDescriptor(RemoteDecoderVideoSubDescriptor* aOutDesc) {} + + virtual void OnForwardedToHost() {} + + virtual TextureData* CreateSimilar( + LayersIPCChannel* aAllocator, LayersBackend aLayersBackend, + TextureFlags aFlags = TextureFlags::DEFAULT, + TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT) const { + return nullptr; + } + + virtual bool UpdateFromSurface(gfx::SourceSurface* aSurface) { + return false; + }; + + virtual bool ReadBack(TextureReadbackSink* aReadbackSink) { return false; } + + virtual void SyncWithObject(RefPtr<SyncObjectClient> aSyncObject){}; + + virtual TextureFlags GetTextureFlags() const { + return TextureFlags::NO_FLAGS; + } + +#ifdef XP_WIN + virtual D3D11TextureData* AsD3D11TextureData() { return nullptr; } + virtual DXGIYCbCrTextureData* AsDXGIYCbCrTextureData() { return nullptr; } +#endif + + virtual BufferTextureData* AsBufferTextureData() { return nullptr; } + + virtual GPUVideoTextureData* AsGPUVideoTextureData() { return nullptr; } + + virtual AndroidHardwareBufferTextureData* + AsAndroidHardwareBufferTextureData() { + return nullptr; + } + + // It is used by AndroidHardwareBufferTextureData and + // SharedSurfaceTextureData. Returns buffer id when it owns + // AndroidHardwareBuffer. It is used only on android. + virtual Maybe<uint64_t> GetBufferId() const { return Nothing(); } + + // The acquire fence is a fence that is used for waiting until rendering to + // its AHardwareBuffer is completed. + // It is used only on android. + virtual mozilla::ipc::FileDescriptor GetAcquireFence() { + return mozilla::ipc::FileDescriptor(); + } + + protected: + MOZ_COUNTED_DEFAULT_CTOR(TextureData) +}; + +/** + * TextureClient is a thin abstraction over texture data that need to be shared + * between the content process and the compositor process. It is the + * content-side half of a TextureClient/TextureHost pair. A corresponding + * TextureHost lives on the compositor-side. + * + * TextureClient's primary purpose is to present texture data in a way that is + * understood by the IPC system. There are two ways to use it: + * - Use it to serialize image data that is not IPC-friendly (most likely + * involving a copy into shared memory) + * - preallocate it and paint directly into it, which avoids copy but requires + * the painting code to be aware of TextureClient (or at least the underlying + * shared memory). + * + * There is always one and only one TextureClient per TextureHost, and the + * TextureClient/Host pair only owns one buffer of image data through its + * lifetime. This means that the lifetime of the underlying shared data + * matches the lifetime of the TextureClient/Host pair. It also means + * TextureClient/Host do not implement double buffering, which is the + * responsibility of the compositable (which would use pairs of Textures). + * In order to send several different buffers to the compositor side, use + * several TextureClients. + */ +class TextureClient : public AtomicRefCountedWithFinalize<TextureClient> { + public: + TextureClient(TextureData* aData, TextureFlags aFlags, + LayersIPCChannel* aAllocator); + + virtual ~TextureClient(); + + static already_AddRefed<TextureClient> CreateWithData( + TextureData* aData, TextureFlags aFlags, LayersIPCChannel* aAllocator); + + // Creates and allocates a TextureClient usable with Moz2D. + static already_AddRefed<TextureClient> CreateForDrawing( + KnowsCompositor* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags flags = ALLOC_DEFAULT); + + static already_AddRefed<TextureClient> CreateFromSurface( + KnowsCompositor* aAllocator, gfx::SourceSurface* aSurface, + BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags); + + // Creates and allocates a TextureClient supporting the YCbCr format. + static already_AddRefed<TextureClient> CreateForYCbCr( + KnowsCompositor* aAllocator, const gfx::IntRect& aDisplay, + const gfx::IntSize& aYSize, uint32_t aYStride, + const gfx::IntSize& aCbCrSize, uint32_t aCbCrStride, + StereoMode aStereoMode, gfx::ColorDepth aColorDepth, + gfx::YUVColorSpace aYUVColorSpace, gfx::ColorRange aColorRange, + TextureFlags aTextureFlags); + + // Creates and allocates a TextureClient (can be accessed through raw + // pointers). + static already_AddRefed<TextureClient> CreateForRawBufferAccess( + KnowsCompositor* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, gfx::BackendType aMoz2dBackend, + TextureFlags aTextureFlags, TextureAllocationFlags flags = ALLOC_DEFAULT); + + // Creates and allocates a TextureClient of the same type. + already_AddRefed<TextureClient> CreateSimilar( + LayersBackend aLayersBackend = LayersBackend::LAYERS_NONE, + TextureFlags aFlags = TextureFlags::DEFAULT, + TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT) const; + + /** + * Locks the shared data, allowing the caller to get access to it. + * + * Please always lock/unlock when accessing the shared data. + * If Lock() returns false, you should not attempt to access the shared data. + */ + bool Lock(OpenMode aMode); + + void Unlock(); + + bool IsLocked() const { return mIsLocked; } + + gfx::IntSize GetSize() const { return mInfo.size; } + + gfx::SurfaceFormat GetFormat() const { return mInfo.format; } + + /** + * Returns true if this texture has a synchronization mechanism (mutex, fence, + * etc.). Textures that do not implement synchronization should be immutable + * or should use immediate uploads (see TextureFlags in CompositorTypes.h) + * Even if a texture does not implement synchronization, Lock and Unlock need + * to be used appropriately since the latter are also there to map/numap data. + */ + bool HasSynchronization() const { return mInfo.hasSynchronization; } + + /** + * Indicates whether the TextureClient implementation is backed by an + * in-memory buffer. The consequence of this is that locking the + * TextureClient does not contend with locking the texture on the host side. + */ + bool HasIntermediateBuffer() const { return mInfo.hasIntermediateBuffer; } + + bool CanExposeDrawTarget() const { return mInfo.supportsMoz2D; } + + bool CanExposeMappedData() const { return mInfo.canExposeMappedData; } + + /** + * Returns a DrawTarget to draw into the TextureClient. + * This function should never be called when not on the main thread! + * + * This must never be called on a TextureClient that is not sucessfully + * locked. When called several times within one Lock/Unlock pair, this method + * should return the same DrawTarget. The DrawTarget is automatically flushed + * by the TextureClient when the latter is unlocked, and the DrawTarget that + * will be returned within the next lock/unlock pair may or may not be the + * same object. Do not keep references to the DrawTarget outside of the + * lock/unlock pair. + * + * This is typically used as follows: + * + * if (!texture->Lock(OpenMode::OPEN_READ_WRITE)) { + * return false; + * } + * { + * // Restrict this code's scope to ensure all references to dt are gone + * // when Unlock is called. + * DrawTarget* dt = texture->BorrowDrawTarget(); + * // use the draw target ... + * } + * texture->Unlock(); + * + */ + gfx::DrawTarget* BorrowDrawTarget(); + + already_AddRefed<gfx::SourceSurface> BorrowSnapshot(); + + /** + * Similar to BorrowDrawTarget but provides direct access to the texture's + * bits instead of a DrawTarget. + */ + bool BorrowMappedData(MappedTextureData&); + bool BorrowMappedYCbCrData(MappedYCbCrTextureData&); + + /** + * This function can be used to update the contents of the TextureClient + * off the main thread. + */ + void UpdateFromSurface(gfx::SourceSurface* aSurface); + + /** + * This method is strictly for debugging. It causes locking and + * needless copies. + */ + already_AddRefed<gfx::DataSourceSurface> GetAsSurface(); + + virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix); + + /** + * Copies a rectangle from this texture client to a position in aTarget. + * It is assumed that the necessary locks are in place; so this should at + * least have a read lock and aTarget should at least have a write lock. + */ + bool CopyToTextureClient(TextureClient* aTarget, const gfx::IntRect* aRect, + const gfx::IntPoint* aPoint); + + /** + * Allocate and deallocate a TextureChild actor. + * + * TextureChild is an implementation detail of TextureClient that is not + * exposed to the rest of the code base. CreateIPDLActor and DestroyIPDLActor + * are for use with the managing IPDL protocols only (so that they can + * implement AllocPextureChild and DeallocPTextureChild). + */ + static PTextureChild* CreateIPDLActor(); + static bool DestroyIPDLActor(PTextureChild* actor); + + /** + * Get the TextureClient corresponding to the actor passed in parameter. + */ + static already_AddRefed<TextureClient> AsTextureClient(PTextureChild* actor); + + /** + * TextureFlags contain important information about various aspects + * of the texture, like how its liferime is managed, and how it + * should be displayed. + * See TextureFlags in CompositorTypes.h. + */ + TextureFlags GetFlags() const { return mFlags; } + + bool HasFlags(TextureFlags aFlags) const { + return (mFlags & aFlags) == aFlags; + } + + void AddFlags(TextureFlags aFlags); + + void RemoveFlags(TextureFlags aFlags); + + // Must not be called when TextureClient is in use by CompositableClient. + void RecycleTexture(TextureFlags aFlags); + + /** + * After being shared with the compositor side, an immutable texture is never + * modified, it can only be read. It is safe to not Lock/Unlock immutable + * textures. + */ + bool IsImmutable() const { return !!(mFlags & TextureFlags::IMMUTABLE); } + + void MarkImmutable() { AddFlags(TextureFlags::IMMUTABLE); } + + bool IsSharedWithCompositor() const; + + /** + * If this method returns false users of TextureClient are not allowed + * to access the shared data. + */ + bool IsValid() const { return !!mData; } + + /** + * Called when TextureClient is added to CompositableClient. + */ + void SetAddedToCompositableClient(); + + /** + * If this method retuns false, TextureClient is already added to + * CompositableClient, since its creation or recycling. + */ + bool IsAddedToCompositableClient() const { + return mAddedToCompositableClient; + } + + /** + * Create and init the TextureChild/Parent IPDL actor pair + * with a CompositableForwarder. + * + * Should be called only once per TextureClient. + * The TextureClient must not be locked when calling this method. + */ + bool InitIPDLActor(CompositableForwarder* aForwarder); + + /** + * Create and init the TextureChild/Parent IPDL actor pair + * with a TextureForwarder. + * + * Should be called only once per TextureClient. + * The TextureClient must not be locked when calling this method. + */ + bool InitIPDLActor(KnowsCompositor* aKnowsCompositor); + + /** + * Return a pointer to the IPDLActor. + * + * This is to be used with IPDL messages only. Do not store the returned + * pointer. + */ + PTextureChild* GetIPDLActor(); + + /** + * Triggers the destruction of the shared data and the corresponding + * TextureHost. + * + * If the texture flags contain TextureFlags::DEALLOCATE_CLIENT, the + * destruction will be synchronously coordinated with the compositor side, + * otherwise it will be done asynchronously. + */ + void Destroy(); + + /** + * Track how much of this texture is wasted. + * For example we might allocate a 256x256 tile but only use 10x10. + */ + void SetWaste(int aWasteArea) { + mWasteTracker.Update(aWasteArea, BytesPerPixel(GetFormat())); + } + + /** + * This sets the readback sink that this texture is to use. This will + * receive the data for this texture as soon as it becomes available after + * texture unlock. + */ + virtual void SetReadbackSink(TextureReadbackSink* aReadbackSink) { + mReadbackSink = aReadbackSink; + } + + void SyncWithObject(RefPtr<SyncObjectClient> aSyncObject) { + mData->SyncWithObject(aSyncObject); + } + + LayersIPCChannel* GetAllocator() { return mAllocator; } + + ITextureClientRecycleAllocator* GetRecycleAllocator() { + return mRecycleAllocator; + } + void SetRecycleAllocator(ITextureClientRecycleAllocator* aAllocator); + + /// If you add new code that uses this method, you are probably doing + /// something wrong. + TextureData* GetInternalData() { return mData; } + const TextureData* GetInternalData() const { return mData; } + + uint64_t GetSerial() const { return mSerial; } + void GetSurfaceDescriptorRemoteDecoder( + SurfaceDescriptorRemoteDecoder* aOutDesc); + + void CancelWaitForNotifyNotUsed(); + + /** + * Set last transaction id of CompositableForwarder. + * + * Called when TextureClient has TextureFlags::RECYCLE flag. + * When CompositableForwarder forwards the TextureClient with + * TextureFlags::RECYCLE, it holds TextureClient's ref until host side + * releases it. The host side sends TextureClient release message. + * The id is used to check if the message is for the last TextureClient + * forwarding. + */ + void SetLastFwdTransactionId(uint64_t aTransactionId) { + MOZ_ASSERT(mFwdTransactionId <= aTransactionId); + mFwdTransactionId = aTransactionId; + } + + uint64_t GetLastFwdTransactionId() { return mFwdTransactionId; } + + TextureReadLock* GetReadLock() { return mReadLock; } + + bool IsReadLocked() const; + + bool TryReadLock(); + void ReadUnlock(); + + bool OnForwardedToHost(); + + // Mark that the TextureClient will be used by the paint thread, and should + // not free its underlying texture data. This must only be called from the + // main thread. + void AddPaintThreadRef(); + + // Mark that the TextureClient is no longer in use by the PaintThread. This + // must only be called from the PaintThread. + void DropPaintThreadRef(); + + wr::MaybeExternalImageId GetExternalImageKey() { return mExternalImageId; } + + private: + static void TextureClientRecycleCallback(TextureClient* aClient, + void* aClosure); + + // Internal helpers for creating texture clients using the actual forwarder + // instead of KnowsCompositor. TextureClientPool uses these to let it cache + // texture clients per-process instead of per ShadowLayerForwarder, but + // everyone else should use the public functions instead. + friend class TextureClientPool; + static already_AddRefed<TextureClient> CreateForDrawing( + TextureForwarder* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, KnowsCompositor* aKnowsCompositor, + BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT); + + static already_AddRefed<TextureClient> CreateForRawBufferAccess( + LayersIPCChannel* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, gfx::BackendType aMoz2dBackend, + LayersBackend aLayersBackend, TextureFlags aTextureFlags, + TextureAllocationFlags flags = ALLOC_DEFAULT); + + void EnableReadLock(); + void EnableBlockingReadLock(); + + /** + * Called once, during the destruction of the Texture, on the thread in which + * texture's reference count reaches 0 (could be any thread). + * + * Here goes the shut-down code that uses virtual methods. + * Must only be called by Release(). + */ + void Finalize() {} + + friend class AtomicRefCountedWithFinalize<TextureClient>; + + protected: + /** + * Should only be called *once* per texture, in TextureClient::InitIPDLActor. + * Some texture implementations rely on the fact that the descriptor will be + * deserialized. + * Calling ToSurfaceDescriptor again after it has already returned true, + * or never constructing a TextureHost with aDescriptor may result in a memory + * leak (see TextureClientD3D9 for example). + */ + bool ToSurfaceDescriptor(SurfaceDescriptor& aDescriptor); + + void LockActor() const; + void UnlockActor() const; + + TextureData::Info mInfo; + + RefPtr<LayersIPCChannel> mAllocator; + RefPtr<TextureChild> mActor; + RefPtr<ITextureClientRecycleAllocator> mRecycleAllocator; + RefPtr<TextureReadLock> mReadLock; + + TextureData* mData; + RefPtr<gfx::DrawTarget> mBorrowedDrawTarget; + + TextureFlags mFlags; + + gl::GfxTextureWasteTracker mWasteTracker; + + OpenMode mOpenMode; +#ifdef DEBUG + uint32_t mExpectedDtRefs; +#endif + bool mIsLocked; + bool mIsReadLocked; + // This member tracks that the texture was written into until the update + // is sent to the compositor. We need this remember to lock mReadLock on + // behalf of the compositor just before sending the notification. + bool mUpdated; + + // Used when TextureClient is recycled with TextureFlags::RECYCLE flag. + bool mAddedToCompositableClient; + + bool mWorkaroundAnnoyingSharedSurfaceLifetimeIssues; + bool mWorkaroundAnnoyingSharedSurfaceOwnershipIssues; + + RefPtr<TextureReadbackSink> mReadbackSink; + + uint64_t mFwdTransactionId; + + // Serial id of TextureClient. It is unique in current process. + const uint64_t mSerial; + + // When non-zero, texture data must not be freed. + mozilla::Atomic<uintptr_t> mPaintThreadRefs; + + // External image id. It is unique if it is allocated. + // The id is allocated in TextureClient::InitIPDLActor(). + // Its allocation is supported by + // CompositorBridgeChild and ImageBridgeChild for now. + wr::MaybeExternalImageId mExternalImageId; + + // Used to assign serial ids of TextureClient. + static mozilla::Atomic<uint64_t> sSerialCounter; + + friend class TextureChild; + friend void TestTextureClientSurface(TextureClient*, gfxImageSurface*); + friend void TestTextureClientYCbCr(TextureClient*, PlanarYCbCrData&); + friend already_AddRefed<TextureHost> CreateTextureHostWithBackend( + TextureClient*, ISurfaceAllocator*, LayersBackend&); + +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL + public: + // Pointer to the pool this tile came from. + TextureClientPool* mPoolTracker; +#endif +}; + +/** + * Task that releases TextureClient pointer on a specified thread. + */ +class TextureClientReleaseTask : public Runnable { + public: + explicit TextureClientReleaseTask(TextureClient* aClient) + : Runnable("layers::TextureClientReleaseTask"), mTextureClient(aClient) {} + + NS_IMETHOD Run() override { + mTextureClient = nullptr; + return NS_OK; + } + + private: + RefPtr<TextureClient> mTextureClient; +}; + +// Automatically lock and unlock a texture. Since texture locking is fallible, +// Succeeded() must be checked on the guard object before proceeding. +class MOZ_RAII TextureClientAutoLock { + public: + TextureClientAutoLock(TextureClient* aTexture, OpenMode aMode) + : mTexture(aTexture), mSucceeded(false) { + mSucceeded = mTexture->Lock(aMode); +#ifdef DEBUG + mChecked = false; +#endif + } + ~TextureClientAutoLock() { + MOZ_ASSERT(mChecked); + if (mSucceeded) { + mTexture->Unlock(); + } + } + + bool Succeeded() { +#ifdef DEBUG + mChecked = true; +#endif + return mSucceeded; + } + + private: + TextureClient* mTexture; +#ifdef DEBUG + bool mChecked; +#endif + bool mSucceeded; +}; + +// Automatically locks and unlocks two texture clients, and exposes them as a +// a single draw target dual. Since texture locking is fallible, Succeeded() +// must be checked on the guard object before proceeding. +class MOZ_RAII DualTextureClientAutoLock { + public: + DualTextureClientAutoLock(TextureClient* aTexture, + TextureClient* aTextureOnWhite, OpenMode aMode) + : mTarget(nullptr), mTexture(aTexture), mTextureOnWhite(aTextureOnWhite) { + if (!mTexture->Lock(aMode)) { + return; + } + + mTarget = mTexture->BorrowDrawTarget(); + + if (!mTarget) { + mTexture->Unlock(); + return; + } + + if (!mTextureOnWhite) { + return; + } + + if (!mTextureOnWhite->Lock(aMode)) { + mTarget = nullptr; + mTexture->Unlock(); + return; + } + + RefPtr<gfx::DrawTarget> targetOnWhite = mTextureOnWhite->BorrowDrawTarget(); + + if (!targetOnWhite) { + mTarget = nullptr; + mTexture->Unlock(); + mTextureOnWhite->Unlock(); + return; + } + + mTarget = gfx::Factory::CreateDualDrawTarget(mTarget, targetOnWhite); + + if (!mTarget) { + mTarget = nullptr; + mTexture->Unlock(); + mTextureOnWhite->Unlock(); + } + } + + ~DualTextureClientAutoLock() { + if (Succeeded()) { + mTarget = nullptr; + + mTexture->Unlock(); + if (mTextureOnWhite) { + mTextureOnWhite->Unlock(); + } + } + } + + bool Succeeded() const { return !!mTarget; } + + operator gfx::DrawTarget*() const { return mTarget; } + gfx::DrawTarget* operator->() const { return mTarget; } + + RefPtr<gfx::DrawTarget> mTarget; + + private: + RefPtr<TextureClient> mTexture; + RefPtr<TextureClient> mTextureOnWhite; +}; + +class KeepAlive { + public: + virtual ~KeepAlive() = default; +}; + +template <typename T> +class TKeepAlive : public KeepAlive { + public: + explicit TKeepAlive(T* aData) : mData(aData) {} + + protected: + RefPtr<T> mData; +}; + +/// Convenience function to set the content of ycbcr texture. +bool UpdateYCbCrTextureClient(TextureClient* aTexture, + const PlanarYCbCrData& aData); + +TextureType PreferredCanvasTextureType(KnowsCompositor* aKnowsCompositor); + +} // namespace layers +} // namespace mozilla + +#endif diff --git a/gfx/layers/client/TextureClientPool.cpp b/gfx/layers/client/TextureClientPool.cpp new file mode 100644 index 0000000000..7dacbe1ab3 --- /dev/null +++ b/gfx/layers/client/TextureClientPool.cpp @@ -0,0 +1,314 @@ +/* -*- 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 + * 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 "TextureClientPool.h" +#include "CompositableClient.h" +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/TextureForwarder.h" +#include "mozilla/layers/TiledContentClient.h" +#include "mozilla/StaticPrefs_layers.h" + +#include "nsComponentManagerUtils.h" + +#define TCP_LOG(...) +//#define TCP_LOG(...) printf_stderr(__VA_ARGS__); + +namespace mozilla { +namespace layers { + +// We want to shrink to our maximum size of N unused tiles +// after a timeout to allow for short-term budget requirements +static void ShrinkCallback(nsITimer* aTimer, void* aClosure) { + static_cast<TextureClientPool*>(aClosure)->ShrinkToMaximumSize(); +} + +// After a certain amount of inactivity, let's clear the pool so that +// we don't hold onto tiles needlessly. In general, allocations are +// cheap enough that re-allocating isn't an issue unless we're allocating +// at an inopportune time (e.g. mid-animation). +static void ClearCallback(nsITimer* aTimer, void* aClosure) { + static_cast<TextureClientPool*>(aClosure)->Clear(); +} + +TextureClientPool::TextureClientPool( + KnowsCompositor* aKnowsCompositor, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, TextureFlags aFlags, uint32_t aShrinkTimeoutMsec, + uint32_t aClearTimeoutMsec, uint32_t aInitialPoolSize, + uint32_t aPoolUnusedSize, TextureForwarder* aAllocator) + : mKnowsCompositor(aKnowsCompositor), + mFormat(aFormat), + mSize(aSize), + mFlags(aFlags), + mShrinkTimeoutMsec(aShrinkTimeoutMsec), + mClearTimeoutMsec(aClearTimeoutMsec), + mInitialPoolSize(aInitialPoolSize), + mPoolUnusedSize(aPoolUnusedSize), + mOutstandingClients(0), + mSurfaceAllocator(aAllocator), + mDestroyed(false) { + TCP_LOG("TexturePool %p created with maximum unused texture clients %u\n", + this, mInitialPoolSize); + mShrinkTimer = NS_NewTimer(); + mClearTimer = NS_NewTimer(); + if (aFormat == gfx::SurfaceFormat::UNKNOWN) { + gfxWarning() << "Creating texture pool for SurfaceFormat::UNKNOWN format"; + } +} + +TextureClientPool::~TextureClientPool() { + mShrinkTimer->Cancel(); + mClearTimer->Cancel(); +} + +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL +static bool TestClientPool(const char* what, TextureClient* aClient, + TextureClientPool* aPool) { + if (!aClient || !aPool) { + return false; + } + + TextureClientPool* actual = aClient->mPoolTracker; + bool ok = (actual == aPool); + if (ok) { + ok = (aClient->GetFormat() == aPool->GetFormat()); + } + + if (!ok) { + if (actual) { + gfxCriticalError() << "Pool error(" << what << "): " << aPool << "-" + << aPool->GetFormat() << ", " << actual << "-" + << actual->GetFormat() << ", " << aClient->GetFormat(); + MOZ_CRASH("GFX: Crashing with actual"); + } else { + gfxCriticalError() << "Pool error(" << what << "): " << aPool << "-" + << aPool->GetFormat() << ", nullptr, " + << aClient->GetFormat(); + MOZ_CRASH("GFX: Crashing without actual"); + } + } + return ok; +} +#endif + +already_AddRefed<TextureClient> TextureClientPool::GetTextureClient() { + // Try to fetch a client from the pool + RefPtr<TextureClient> textureClient; + + // We initially allocate mInitialPoolSize for our pool. If we run + // out of TextureClients, we allocate additional TextureClients to try and + // keep around mPoolUnusedSize + if (mTextureClients.empty()) { + AllocateTextureClient(); + } + + if (mTextureClients.empty()) { + // All our allocations failed, return nullptr + return nullptr; + } + + mOutstandingClients++; + textureClient = mTextureClients.top(); + mTextureClients.pop(); +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL + if (textureClient) { + textureClient->mPoolTracker = this; + } + DebugOnly<bool> ok = TestClientPool("fetch", textureClient, this); + MOZ_ASSERT(ok); +#endif + TCP_LOG("TexturePool %p giving %p from pool; size %u outstanding %u\n", this, + textureClient.get(), mTextureClients.size(), mOutstandingClients); + + return textureClient.forget(); +} + +void TextureClientPool::AllocateTextureClient() { + TCP_LOG("TexturePool %p allocating TextureClient, outstanding %u\n", this, + mOutstandingClients); + + TextureAllocationFlags allocFlags = ALLOC_DEFAULT; + + if (mKnowsCompositor->SupportsTextureDirectMapping() && + std::max(mSize.width, mSize.height) <= GetMaxTextureSize()) { + allocFlags = + TextureAllocationFlags(allocFlags | ALLOC_ALLOW_DIRECT_MAPPING); + } + + RefPtr<TextureClient> newClient; + if (StaticPrefs::layers_force_shmem_tiles_AtStartup()) { + // gfx::BackendType::NONE means use the content backend + newClient = TextureClient::CreateForRawBufferAccess( + mSurfaceAllocator, mFormat, mSize, gfx::BackendType::NONE, GetBackend(), + mFlags, allocFlags); + } else { + newClient = TextureClient::CreateForDrawing( + mSurfaceAllocator, mFormat, mSize, mKnowsCompositor, + BackendSelector::Content, mFlags, allocFlags); + } + + if (newClient) { + mTextureClients.push(newClient); + } +} + +void TextureClientPool::ResetTimers() { + // Shrink down if we're beyond our maximum size + if (mShrinkTimeoutMsec && + mTextureClients.size() + mTextureClientsDeferred.size() > + mPoolUnusedSize) { + TCP_LOG("TexturePool %p scheduling a shrink-to-max-size\n", this); + mShrinkTimer->InitWithNamedFuncCallback( + ShrinkCallback, this, mShrinkTimeoutMsec, nsITimer::TYPE_ONE_SHOT, + "layers::TextureClientPool::ResetTimers"); + } + + // Clear pool after a period of inactivity to reduce memory consumption + if (mClearTimeoutMsec) { + TCP_LOG("TexturePool %p scheduling a clear\n", this); + mClearTimer->InitWithNamedFuncCallback( + ClearCallback, this, mClearTimeoutMsec, nsITimer::TYPE_ONE_SHOT, + "layers::TextureClientPool::ResetTimers"); + } +} + +void TextureClientPool::ReturnTextureClient(TextureClient* aClient) { + if (!aClient || mDestroyed) { + return; + } +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL + DebugOnly<bool> ok = TestClientPool("return", aClient, this); + MOZ_ASSERT(ok); +#endif + // Add the client to the pool: + MOZ_ASSERT(mOutstandingClients > mTextureClientsDeferred.size()); + mOutstandingClients--; + mTextureClients.push(aClient); + TCP_LOG("TexturePool %p had client %p returned; size %u outstanding %u\n", + this, aClient, mTextureClients.size(), mOutstandingClients); + + ResetTimers(); +} + +void TextureClientPool::ReturnTextureClientDeferred(TextureClient* aClient) { + if (!aClient || mDestroyed) { + return; + } + MOZ_ASSERT(aClient->GetReadLock()); +#ifdef GFX_DEBUG_TRACK_CLIENTS_IN_POOL + DebugOnly<bool> ok = TestClientPool("defer", aClient, this); + MOZ_ASSERT(ok); +#endif + mTextureClientsDeferred.push_back(aClient); + TCP_LOG( + "TexturePool %p had client %p defer-returned, size %u outstanding %u\n", + this, aClient, mTextureClientsDeferred.size(), mOutstandingClients); + + ResetTimers(); +} + +void TextureClientPool::ShrinkToMaximumSize() { + // We're over our desired maximum size, immediately shrink down to the + // maximum. + // + // We cull from the deferred TextureClients first, as we can't reuse those + // until they get returned. + uint32_t totalUnusedTextureClients = + mTextureClients.size() + mTextureClientsDeferred.size(); + + // If we have > mInitialPoolSize outstanding, then we want to keep around + // mPoolUnusedSize at a maximum. If we have fewer than mInitialPoolSize + // outstanding, then keep around the entire initial pool size. + uint32_t targetUnusedClients; + if (mOutstandingClients > mInitialPoolSize) { + targetUnusedClients = mPoolUnusedSize; + } else { + targetUnusedClients = mInitialPoolSize; + } + + TCP_LOG( + "TexturePool %p shrinking to maximum unused size %u; current pool size " + "%u; total outstanding %u\n", + this, targetUnusedClients, totalUnusedTextureClients, + mOutstandingClients); + + while (totalUnusedTextureClients > targetUnusedClients) { + if (!mTextureClientsDeferred.empty()) { + mOutstandingClients--; + TCP_LOG("TexturePool %p dropped deferred client %p; %u remaining\n", this, + mTextureClientsDeferred.front().get(), + mTextureClientsDeferred.size() - 1); + mTextureClientsDeferred.pop_front(); + } else { + TCP_LOG("TexturePool %p dropped non-deferred client %p; %u remaining\n", + this, mTextureClients.top().get(), mTextureClients.size() - 1); + mTextureClients.pop(); + } + totalUnusedTextureClients--; + } +} + +void TextureClientPool::ReturnDeferredClients() { + if (mTextureClientsDeferred.empty()) { + return; + } + + TCP_LOG("TexturePool %p returning %u deferred clients to pool\n", this, + mTextureClientsDeferred.size()); + + ReturnUnlockedClients(); + ShrinkToMaximumSize(); +} + +void TextureClientPool::ReturnUnlockedClients() { + for (auto it = mTextureClientsDeferred.begin(); + it != mTextureClientsDeferred.end();) { + MOZ_ASSERT((*it)->GetReadLock()->AsNonBlockingLock()->GetReadCount() >= 1); + // Last count is held by the lock itself. + if (!(*it)->IsReadLocked()) { + mTextureClients.push(*it); + it = mTextureClientsDeferred.erase(it); + + MOZ_ASSERT(mOutstandingClients > 0); + mOutstandingClients--; + } else { + it++; + } + } +} + +void TextureClientPool::ReportClientLost() { + MOZ_ASSERT(mOutstandingClients > mTextureClientsDeferred.size()); + mOutstandingClients--; + TCP_LOG("TexturePool %p getting report client lost; down to %u outstanding\n", + this, mOutstandingClients); +} + +void TextureClientPool::Clear() { + TCP_LOG("TexturePool %p getting cleared\n", this); + while (!mTextureClients.empty()) { + TCP_LOG("TexturePool %p releasing client %p\n", this, + mTextureClients.top().get()); + mTextureClients.pop(); + } + while (!mTextureClientsDeferred.empty()) { + MOZ_ASSERT(mOutstandingClients > 0); + mOutstandingClients--; + TCP_LOG("TexturePool %p releasing deferred client %p\n", this, + mTextureClientsDeferred.front().get()); + mTextureClientsDeferred.pop_front(); + } +} + +void TextureClientPool::Destroy() { + Clear(); + mDestroyed = true; + mInitialPoolSize = 0; + mPoolUnusedSize = 0; + mKnowsCompositor = nullptr; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/TextureClientPool.h b/gfx/layers/client/TextureClientPool.h new file mode 100644 index 0000000000..d557ca9a03 --- /dev/null +++ b/gfx/layers/client/TextureClientPool.h @@ -0,0 +1,175 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_TEXTURECLIENTPOOL_H +#define MOZILLA_GFX_TEXTURECLIENTPOOL_H + +#include "mozilla/gfx/Types.h" +#include "mozilla/gfx/Point.h" +#include "mozilla/RefPtr.h" +#include "mozilla/layers/KnowsCompositor.h" +#include "TextureClient.h" +#include "nsITimer.h" +#include <stack> +#include <list> + +namespace mozilla { +namespace layers { + +class ISurfaceAllocator; +class TextureForwarder; +class TextureReadLock; + +class TextureClientAllocator { + protected: + virtual ~TextureClientAllocator() = default; + + public: + NS_INLINE_DECL_REFCOUNTING(TextureClientAllocator) + + virtual already_AddRefed<TextureClient> GetTextureClient() = 0; + + /** + * Return a TextureClient that is not yet ready to be reused, but will be + * imminently. + */ + virtual void ReturnTextureClientDeferred(TextureClient* aClient) = 0; + + virtual void ReportClientLost() = 0; +}; + +class TextureClientPool final : public TextureClientAllocator { + virtual ~TextureClientPool(); + + public: + TextureClientPool(KnowsCompositor* aKnowsCompositor, + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + TextureFlags aFlags, uint32_t aShrinkTimeoutMsec, + uint32_t aClearTimeoutMsec, uint32_t aInitialPoolSize, + uint32_t aPoolUnusedSize, TextureForwarder* aAllocator); + + /** + * Gets an allocated TextureClient of size and format that are determined + * by the initialisation parameters given to the pool. This will either be + * a cached client that was returned to the pool, or a newly allocated + * client if one isn't available. + * + * All clients retrieved by this method should be returned using the return + * functions, or reported lost so that the pool can manage its size correctly. + */ + already_AddRefed<TextureClient> GetTextureClient() override; + + /** + * Return a TextureClient that is no longer being used and is ready for + * immediate re-use or destruction. + */ + void ReturnTextureClient(TextureClient* aClient); + + /** + * Return a TextureClient that is not yet ready to be reused, but will be + * imminently. + */ + void ReturnTextureClientDeferred(TextureClient* aClient) override; + + /** + * Return any clients to the pool that were previously returned in + * ReturnTextureClientDeferred. + */ + void ReturnDeferredClients(); + + /** + * Attempt to shrink the pool so that there are no more than + * mInitialPoolSize outstanding. + */ + void ShrinkToMaximumSize(); + + /** + * Report that a client retrieved via GetTextureClient() has become + * unusable, so that it will no longer be tracked. + */ + void ReportClientLost() override; + + /** + * Calling this will cause the pool to attempt to relinquish any unused + * clients. + */ + void Clear(); + + LayersBackend GetBackend() const { + return mKnowsCompositor->GetCompositorBackendType(); + } + int32_t GetMaxTextureSize() const { + return mKnowsCompositor->GetMaxTextureSize(); + } + gfx::SurfaceFormat GetFormat() { return mFormat; } + TextureFlags GetFlags() const { return mFlags; } + + /** + * Clear the pool and put it in a state where it won't recycle any new + * texture. + */ + void Destroy(); + + private: + void ReturnUnlockedClients(); + + /// Allocate a single TextureClient to be returned from the pool. + void AllocateTextureClient(); + + /// Reset and/or initialise timers for shrinking/clearing the pool. + void ResetTimers(); + + /// KnowsCompositor passed to the TextureClient for buffer creation. + RefPtr<KnowsCompositor> mKnowsCompositor; + + /// Format is passed to the TextureClient for buffer creation. + gfx::SurfaceFormat mFormat; + + /// The width and height of the tiles to be used. + gfx::IntSize mSize; + + /// Flags passed to the TextureClient for buffer creation. + const TextureFlags mFlags; + + /// How long to wait after a TextureClient is returned before trying + /// to shrink the pool to its maximum size of mPoolUnusedSize. + uint32_t mShrinkTimeoutMsec; + + /// How long to wait after a TextureClient is returned before trying + /// to clear the pool. + uint32_t mClearTimeoutMsec; + + // The initial number of unused texture clients to seed the pool with + // on construction + uint32_t mInitialPoolSize; + + // How many unused texture clients to try and keep around if we go over + // the initial allocation + uint32_t mPoolUnusedSize; + + /// This is a total number of clients in the wild and in the stack of + /// deferred clients (see below). So, the total number of clients in + /// existence is always mOutstandingClients + the size of mTextureClients. + uint32_t mOutstandingClients; + + std::stack<RefPtr<TextureClient>> mTextureClients; + + std::list<RefPtr<TextureClient>> mTextureClientsDeferred; + RefPtr<nsITimer> mShrinkTimer; + RefPtr<nsITimer> mClearTimer; + // This mSurfaceAllocator owns us, so no need to hold a ref to it + TextureForwarder* mSurfaceAllocator; + + // Keep track of whether this pool has been destroyed or not. If it has, + // we won't accept returns of TextureClients anymore, and the refcounting + // should take care of their destruction. + bool mDestroyed; +}; + +} // namespace layers +} // namespace mozilla + +#endif /* MOZILLA_GFX_TEXTURECLIENTPOOL_H */ diff --git a/gfx/layers/client/TextureClientRecycleAllocator.cpp b/gfx/layers/client/TextureClientRecycleAllocator.cpp new file mode 100644 index 0000000000..6407fd8bc2 --- /dev/null +++ b/gfx/layers/client/TextureClientRecycleAllocator.cpp @@ -0,0 +1,246 @@ +/* -*- 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 + * 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 "gfxPlatform.h" +#include "ImageContainer.h" +#include "mozilla/layers/BufferTexture.h" +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/layers/TextureForwarder.h" +#include "TextureClientRecycleAllocator.h" + +namespace mozilla { +namespace layers { + +// Used to keep TextureClient's reference count stable as not to disrupt +// recycling. +class TextureClientHolder final { + ~TextureClientHolder() = default; + + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureClientHolder) + + explicit TextureClientHolder(TextureClient* aClient) + : mTextureClient(aClient), mWillRecycle(true) {} + + TextureClient* GetTextureClient() { return mTextureClient; } + + bool WillRecycle() { return mWillRecycle; } + + void ClearWillRecycle() { mWillRecycle = false; } + + void ClearTextureClient() { mTextureClient = nullptr; } + + protected: + RefPtr<TextureClient> mTextureClient; + bool mWillRecycle; +}; + +class MOZ_RAII DefaultTextureClientAllocationHelper + : public ITextureClientAllocationHelper { + public: + DefaultTextureClientAllocationHelper( + TextureClientRecycleAllocator* aAllocator, gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, BackendSelector aSelector, TextureFlags aTextureFlags, + TextureAllocationFlags aAllocationFlags) + : ITextureClientAllocationHelper(aFormat, aSize, aSelector, aTextureFlags, + aAllocationFlags), + mAllocator(aAllocator) {} + + bool IsCompatible(TextureClient* aTextureClient) override { + if (aTextureClient->GetFormat() != mFormat || + aTextureClient->GetSize() != mSize) { + return false; + } + return true; + } + + already_AddRefed<TextureClient> Allocate( + KnowsCompositor* aKnowsCompositor) override { + return mAllocator->Allocate(mFormat, mSize, mSelector, mTextureFlags, + mAllocationFlags); + } + + protected: + TextureClientRecycleAllocator* mAllocator; +}; + +YCbCrTextureClientAllocationHelper::YCbCrTextureClientAllocationHelper( + const PlanarYCbCrData& aData, TextureFlags aTextureFlags) + : ITextureClientAllocationHelper(gfx::SurfaceFormat::YUV, aData.mYSize, + BackendSelector::Content, aTextureFlags, + ALLOC_DEFAULT), + mData(aData) {} + +bool YCbCrTextureClientAllocationHelper::IsCompatible( + TextureClient* aTextureClient) { + MOZ_ASSERT(aTextureClient->GetFormat() == gfx::SurfaceFormat::YUV); + + BufferTextureData* bufferData = + aTextureClient->GetInternalData()->AsBufferTextureData(); + if (!bufferData || aTextureClient->GetSize() != mData.mYSize || + bufferData->GetCbCrSize().isNothing() || + bufferData->GetCbCrSize().ref() != mData.mCbCrSize || + bufferData->GetYStride().isNothing() || + bufferData->GetYStride().ref() != mData.mYStride || + bufferData->GetCbCrStride().isNothing() || + bufferData->GetCbCrStride().ref() != mData.mCbCrStride || + bufferData->GetYUVColorSpace().isNothing() || + bufferData->GetYUVColorSpace().ref() != mData.mYUVColorSpace || + bufferData->GetColorDepth().isNothing() || + bufferData->GetColorDepth().ref() != mData.mColorDepth || + bufferData->GetStereoMode().isNothing() || + bufferData->GetStereoMode().ref() != mData.mStereoMode) { + return false; + } + return true; +} + +already_AddRefed<TextureClient> YCbCrTextureClientAllocationHelper::Allocate( + KnowsCompositor* aKnowsCompositor) { + return TextureClient::CreateForYCbCr( + aKnowsCompositor, mData.GetPictureRect(), mData.mYSize, mData.mYStride, + mData.mCbCrSize, mData.mCbCrStride, mData.mStereoMode, mData.mColorDepth, + mData.mYUVColorSpace, mData.mColorRange, mTextureFlags); +} + +TextureClientRecycleAllocator::TextureClientRecycleAllocator( + KnowsCompositor* aKnowsCompositor) + : mKnowsCompositor(aKnowsCompositor), + mMaxPooledSize(kMaxPooledSized), + mLock("TextureClientRecycleAllocatorImp.mLock"), + mIsDestroyed(false) {} + +TextureClientRecycleAllocator::~TextureClientRecycleAllocator() { + MutexAutoLock lock(mLock); + while (!mPooledClients.empty()) { + mPooledClients.pop(); + } + MOZ_ASSERT(mInUseClients.empty()); +} + +void TextureClientRecycleAllocator::SetMaxPoolSize(uint32_t aMax) { + mMaxPooledSize = aMax; +} + +already_AddRefed<TextureClient> TextureClientRecycleAllocator::CreateOrRecycle( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) { + MOZ_ASSERT(!(aTextureFlags & TextureFlags::RECYCLE)); + DefaultTextureClientAllocationHelper helper(this, aFormat, aSize, aSelector, + aTextureFlags, aAllocFlags); + return CreateOrRecycle(helper); +} + +already_AddRefed<TextureClient> TextureClientRecycleAllocator::CreateOrRecycle( + ITextureClientAllocationHelper& aHelper) { + MOZ_ASSERT(aHelper.mTextureFlags & TextureFlags::RECYCLE); + + RefPtr<TextureClientHolder> textureHolder; + + { + MutexAutoLock lock(mLock); + if (mIsDestroyed || !mKnowsCompositor->GetTextureForwarder()) { + return nullptr; + } + if (!mPooledClients.empty()) { + textureHolder = mPooledClients.top(); + mPooledClients.pop(); + // If the texture's allocator is not open or a pooled TextureClient is + // not compatible, release it. + if (!textureHolder->GetTextureClient()->GetAllocator()->IPCOpen() || + !aHelper.IsCompatible(textureHolder->GetTextureClient())) { + // Release TextureClient. + RefPtr<Runnable> task = + new TextureClientReleaseTask(textureHolder->GetTextureClient()); + textureHolder->ClearTextureClient(); + textureHolder = nullptr; + mKnowsCompositor->GetTextureForwarder()->GetThread()->Dispatch( + task.forget()); + } else { + textureHolder->GetTextureClient()->RecycleTexture( + aHelper.mTextureFlags); + } + } + } + + if (!textureHolder) { + // Allocate new TextureClient + RefPtr<TextureClient> texture = aHelper.Allocate(mKnowsCompositor); + if (!texture) { + return nullptr; + } + textureHolder = new TextureClientHolder(texture); + } + + { + MutexAutoLock lock(mLock); + MOZ_ASSERT(mInUseClients.find(textureHolder->GetTextureClient()) == + mInUseClients.end()); + // Register TextureClient + mInUseClients[textureHolder->GetTextureClient()] = textureHolder; + } + RefPtr<TextureClient> client(textureHolder->GetTextureClient()); + + // Make sure the texture holds a reference to us, and ask it to call + // RecycleTextureClient when its ref count drops to 1. + client->SetRecycleAllocator(this); + return client.forget(); +} + +already_AddRefed<TextureClient> TextureClientRecycleAllocator::Allocate( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) { + return TextureClient::CreateForDrawing(mKnowsCompositor, aFormat, aSize, + aSelector, aTextureFlags, aAllocFlags); +} + +void TextureClientRecycleAllocator::ShrinkToMinimumSize() { + MutexAutoLock lock(mLock); + while (!mPooledClients.empty()) { + mPooledClients.pop(); + } + // We can not clear using TextureClients safely. + // Just clear WillRecycle here. + std::map<TextureClient*, RefPtr<TextureClientHolder> >::iterator it; + for (it = mInUseClients.begin(); it != mInUseClients.end(); it++) { + RefPtr<TextureClientHolder> holder = it->second; + holder->ClearWillRecycle(); + } +} + +void TextureClientRecycleAllocator::Destroy() { + MutexAutoLock lock(mLock); + while (!mPooledClients.empty()) { + mPooledClients.pop(); + } + mIsDestroyed = true; +} + +void TextureClientRecycleAllocator::RecycleTextureClient( + TextureClient* aClient) { + // Clearing the recycle allocator drops a reference, so make sure we stay + // alive for the duration of this function. + RefPtr<TextureClientRecycleAllocator> kungFuDeathGrip(this); + aClient->SetRecycleAllocator(nullptr); + + RefPtr<TextureClientHolder> textureHolder; + { + MutexAutoLock lock(mLock); + if (mInUseClients.find(aClient) != mInUseClients.end()) { + textureHolder = + mInUseClients[aClient]; // Keep reference count of + // TextureClientHolder within lock. + if (textureHolder->WillRecycle() && !mIsDestroyed && + mPooledClients.size() < mMaxPooledSize) { + mPooledClients.push(textureHolder); + } + mInUseClients.erase(aClient); + } + } +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/TextureClientRecycleAllocator.h b/gfx/layers/client/TextureClientRecycleAllocator.h new file mode 100644 index 0000000000..2cace0e1f0 --- /dev/null +++ b/gfx/layers/client/TextureClientRecycleAllocator.h @@ -0,0 +1,133 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H +#define MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H + +#include <map> +#include <stack> +#include "mozilla/gfx/Types.h" +#include "mozilla/layers/TextureForwarder.h" +#include "mozilla/RefPtr.h" +#include "TextureClient.h" +#include "mozilla/Mutex.h" + +namespace mozilla { +namespace layers { + +class TextureClientHolder; +struct PlanarYCbCrData; + +class ITextureClientRecycleAllocator { + protected: + virtual ~ITextureClientRecycleAllocator() = default; + + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ITextureClientRecycleAllocator) + + protected: + friend class TextureClient; + virtual void RecycleTextureClient(TextureClient* aClient) = 0; +}; + +class ITextureClientAllocationHelper { + public: + ITextureClientAllocationHelper(gfx::SurfaceFormat aFormat, gfx::IntSize aSize, + BackendSelector aSelector, + TextureFlags aTextureFlags, + TextureAllocationFlags aAllocationFlags) + : mFormat(aFormat), + mSize(aSize), + mSelector(aSelector), + mTextureFlags(aTextureFlags | + TextureFlags::RECYCLE) // Set recycle flag + , + mAllocationFlags(aAllocationFlags) {} + + virtual already_AddRefed<TextureClient> Allocate( + KnowsCompositor* aKnowsCompositor) = 0; + virtual bool IsCompatible(TextureClient* aTextureClient) = 0; + + const gfx::SurfaceFormat mFormat; + const gfx::IntSize mSize; + const BackendSelector mSelector; + const TextureFlags mTextureFlags; + const TextureAllocationFlags mAllocationFlags; +}; + +class MOZ_RAII YCbCrTextureClientAllocationHelper + : public ITextureClientAllocationHelper { + public: + YCbCrTextureClientAllocationHelper(const PlanarYCbCrData& aData, + TextureFlags aTextureFlags); + + bool IsCompatible(TextureClient* aTextureClient) override; + + already_AddRefed<TextureClient> Allocate( + KnowsCompositor* aKnowsCompositor) override; + + protected: + const PlanarYCbCrData& mData; +}; + +/** + * TextureClientRecycleAllocator provides TextureClients allocation and + * recycling capabilities. It expects allocations of same sizes and + * attributres. If a recycled TextureClient is different from + * requested one, the recycled one is dropped and new TextureClient is + * allocated. + * + * By default this uses TextureClient::CreateForDrawing to allocate new texture + * clients. + */ +class TextureClientRecycleAllocator : public ITextureClientRecycleAllocator { + protected: + virtual ~TextureClientRecycleAllocator(); + + public: + explicit TextureClientRecycleAllocator(KnowsCompositor* aKnowsCompositor); + + void SetMaxPoolSize(uint32_t aMax); + + // Creates and allocates a TextureClient. + already_AddRefed<TextureClient> CreateOrRecycle( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags flags = ALLOC_DEFAULT); + + already_AddRefed<TextureClient> CreateOrRecycle( + ITextureClientAllocationHelper& aHelper); + + void ShrinkToMinimumSize(); + + void Destroy(); + + KnowsCompositor* GetKnowsCompositor() { return mKnowsCompositor; } + + protected: + virtual already_AddRefed<TextureClient> Allocate( + gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector, + TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags); + + const RefPtr<KnowsCompositor> mKnowsCompositor; + + friend class DefaultTextureClientAllocationHelper; + void RecycleTextureClient(TextureClient* aClient) override; + + static const uint32_t kMaxPooledSized = 2; + uint32_t mMaxPooledSize; + + std::map<TextureClient*, RefPtr<TextureClientHolder> > mInUseClients; + + // stack is good from Graphics cache usage point of view. + std::stack<RefPtr<TextureClientHolder> > mPooledClients; + Mutex mLock; + bool mIsDestroyed; +}; + +} // namespace layers +} // namespace mozilla + +#endif /* MOZILLA_GFX_TEXTURECLIENT_RECYCLE_ALLOCATOR_H */ diff --git a/gfx/layers/client/TextureClientSharedSurface.cpp b/gfx/layers/client/TextureClientSharedSurface.cpp new file mode 100644 index 0000000000..d7a9f5bced --- /dev/null +++ b/gfx/layers/client/TextureClientSharedSurface.cpp @@ -0,0 +1,177 @@ +/* -*- 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 + * 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 "TextureClientSharedSurface.h" + +#include "GLContext.h" +#include "mozilla/gfx/2D.h" +#include "mozilla/gfx/Logging.h" // for gfxDebug +#include "mozilla/layers/ISurfaceAllocator.h" +#include "mozilla/Unused.h" +#include "nsThreadUtils.h" +#include "SharedSurface.h" + +#ifdef MOZ_WIDGET_ANDROID +# include "mozilla/layers/AndroidHardwareBuffer.h" +#endif + +using namespace mozilla::gl; + +namespace mozilla { +namespace layers { + +/* static */ +already_AddRefed<TextureClient> SharedSurfaceTextureData::CreateTextureClient( + const layers::SurfaceDescriptor& aDesc, const gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, TextureFlags aFlags, LayersIPCChannel* aAllocator) { + auto data = MakeUnique<SharedSurfaceTextureData>(aDesc, aFormat, aSize); + return TextureClient::CreateWithData(data.release(), aFlags, aAllocator); +} + +SharedSurfaceTextureData::SharedSurfaceTextureData( + const SurfaceDescriptor& desc, const gfx::SurfaceFormat format, + const gfx::IntSize size) + : mDesc(desc), mFormat(format), mSize(size) {} + +SharedSurfaceTextureData::~SharedSurfaceTextureData() = default; + +void SharedSurfaceTextureData::Deallocate(LayersIPCChannel*) {} + +void SharedSurfaceTextureData::FillInfo(TextureData::Info& aInfo) const { + aInfo.size = mSize; + aInfo.format = mFormat; + aInfo.hasIntermediateBuffer = false; + aInfo.hasSynchronization = false; + aInfo.supportsMoz2D = false; + aInfo.canExposeMappedData = false; +} + +bool SharedSurfaceTextureData::Serialize(SurfaceDescriptor& aOutDescriptor) { + if (mDesc.type() != + SurfaceDescriptor::TSurfaceDescriptorAndroidHardwareBuffer) { + aOutDescriptor = mDesc; + return true; + } + +#ifdef MOZ_WIDGET_ANDROID + // File descriptor is created heare for reducing its allocation. + const SurfaceDescriptorAndroidHardwareBuffer& desc = + mDesc.get_SurfaceDescriptorAndroidHardwareBuffer(); + RefPtr<AndroidHardwareBuffer> buffer = + AndroidHardwareBufferManager::Get()->GetBuffer(desc.bufferId()); + if (!buffer) { + return false; + } + + int fd[2] = {}; + if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fd) != 0) { + return false; + } + + UniqueFileHandle readerFd(fd[0]); + UniqueFileHandle writerFd(fd[1]); + + // Send the AHardwareBuffer to an AF_UNIX socket. It does not acquire or + // retain a reference to the buffer object. The caller is therefore + // responsible for ensuring that the buffer remains alive through the lifetime + // of this file descriptor. + int ret = buffer->SendHandleToUnixSocket(writerFd.get()); + if (ret < 0) { + return false; + } + + aOutDescriptor = layers::SurfaceDescriptorAndroidHardwareBuffer( + ipc::FileDescriptor(readerFd.release()), buffer->mId, buffer->mSize, + buffer->mFormat); + return true; +#else + MOZ_ASSERT_UNREACHABLE("unexpected to be called"); + return false; +#endif +} + +TextureFlags SharedSurfaceTextureData::GetTextureFlags() const { + TextureFlags flags = TextureFlags::NO_FLAGS; + +#ifdef MOZ_WIDGET_ANDROID + if (mDesc.type() == + SurfaceDescriptor::TSurfaceDescriptorAndroidHardwareBuffer) { + flags |= TextureFlags::WAIT_HOST_USAGE_END; + } +#endif + return flags; +} + +Maybe<uint64_t> SharedSurfaceTextureData::GetBufferId() const { +#ifdef MOZ_WIDGET_ANDROID + if (mDesc.type() == + SurfaceDescriptor::TSurfaceDescriptorAndroidHardwareBuffer) { + const SurfaceDescriptorAndroidHardwareBuffer& desc = + mDesc.get_SurfaceDescriptorAndroidHardwareBuffer(); + return Some(desc.bufferId()); + } +#endif + return Nothing(); +} + +mozilla::ipc::FileDescriptor SharedSurfaceTextureData::GetAcquireFence() { +#ifdef MOZ_WIDGET_ANDROID + if (mDesc.type() == + SurfaceDescriptor::TSurfaceDescriptorAndroidHardwareBuffer) { + const SurfaceDescriptorAndroidHardwareBuffer& desc = + mDesc.get_SurfaceDescriptorAndroidHardwareBuffer(); + RefPtr<AndroidHardwareBuffer> buffer = + AndroidHardwareBufferManager::Get()->GetBuffer(desc.bufferId()); + if (!buffer) { + return ipc::FileDescriptor(); + } + + return buffer->GetAcquireFence(); + } +#endif + return ipc::FileDescriptor(); +} + +/* +static TextureFlags FlagsFrom(const SharedSurfaceDescriptor& desc) { + auto flags = TextureFlags::ORIGIN_BOTTOM_LEFT; + if (!desc.isPremultAlpha) { + flags |= TextureFlags::NON_PREMULTIPLIED; + } + return flags; +} + +SharedSurfaceTextureClient::SharedSurfaceTextureClient( + const SharedSurfaceDescriptor& aDesc, LayersIPCChannel* aAllocator) + : TextureClient(new SharedSurfaceTextureData(desc), FlagsFrom(desc), +aAllocator) { mWorkaroundAnnoyingSharedSurfaceLifetimeIssues = true; +} + +SharedSurfaceTextureClient::~SharedSurfaceTextureClient() { + // XXX - Things break when using the proper destruction handshake with + // SharedSurfaceTextureData because the TextureData outlives its gl + // context. Having a strong reference to the gl context creates a cycle. + // This needs to be fixed in a better way, though, because deleting + // the TextureData here can race with the compositor and cause flashing. + TextureData* data = mData; + mData = nullptr; + + Destroy(); + + if (data) { + // Destroy mData right away without doing the proper deallocation handshake, + // because SharedSurface depends on things that may not outlive the + // texture's destructor so we can't wait until we know the compositor isn't + // using the texture anymore. It goes without saying that this is really bad + // and we should fix the bugs that block doing the right thing such as bug + // 1224199 sooner rather than later. + delete data; + } +} +*/ + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/TextureClientSharedSurface.h b/gfx/layers/client/TextureClientSharedSurface.h new file mode 100644 index 0000000000..d18eb9e2cc --- /dev/null +++ b/gfx/layers/client/TextureClientSharedSurface.h @@ -0,0 +1,85 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_TEXTURECLIENT_SHAREDSURFACE_H +#define MOZILLA_GFX_TEXTURECLIENT_SHAREDSURFACE_H + +#include <cstddef> // for size_t +#include <stdint.h> // for uint32_t, uint8_t, uint64_t +#include "GLContextTypes.h" // for GLContext (ptr only), etc +#include "TextureClient.h" +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc +#include "mozilla/RefPtr.h" // for RefPtr, RefCounted +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Types.h" // for SurfaceFormat +#include "mozilla/layers/CompositorTypes.h" // for TextureFlags, etc +#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor + +namespace mozilla { +namespace gl { +class GLContext; +class SharedSurface; +class SurfaceFactory; +} // namespace gl + +namespace layers { + +class SharedSurfaceTextureClient; + +class SharedSurfaceTextureData : public TextureData { + friend class SharedSurfaceTextureClient; + + public: + const SurfaceDescriptor mDesc; + const gfx::SurfaceFormat mFormat; + const gfx::IntSize mSize; + + static already_AddRefed<TextureClient> CreateTextureClient( + const layers::SurfaceDescriptor& aDesc, const gfx::SurfaceFormat aFormat, + gfx::IntSize aSize, TextureFlags aFlags, LayersIPCChannel* aAllocator); + + SharedSurfaceTextureData(const SurfaceDescriptor&, gfx::SurfaceFormat, + gfx::IntSize); + virtual ~SharedSurfaceTextureData(); + + bool Lock(OpenMode) override { return false; } + + void Unlock() override {} + + void FillInfo(TextureData::Info& aInfo) const override; + + bool Serialize(SurfaceDescriptor& aOutDescriptor) override; + + void Deallocate(LayersIPCChannel*) override; + + TextureFlags GetTextureFlags() const override; + + Maybe<uint64_t> GetBufferId() const override; + + mozilla::ipc::FileDescriptor GetAcquireFence() override; +}; +/* +class SharedSurfaceTextureClient : public TextureClient { + public: + SharedSurfaceTextureClient(SharedSurfaceTextureData* aData, + TextureFlags aFlags, LayersIPCChannel* aAllocator); + + ~SharedSurfaceTextureClient(); + + static RefPtr<SharedSurfaceTextureClient> Create( + UniquePtr<gl::SharedSurface> surf, gl::SurfaceFactory* factory, + LayersIPCChannel* aAllocator, TextureFlags aFlags); + + const auto& Desc() const { + return static_cast<const SharedSurfaceTextureData*>(GetInternalData()) + ->mDesc; + } +}; +*/ +} // namespace layers +} // namespace mozilla + +#endif // MOZILLA_GFX_TEXTURECLIENT_SHAREDSURFACE_H diff --git a/gfx/layers/client/TextureRecorded.cpp b/gfx/layers/client/TextureRecorded.cpp new file mode 100644 index 0000000000..a53b42a81b --- /dev/null +++ b/gfx/layers/client/TextureRecorded.cpp @@ -0,0 +1,105 @@ +/* -*- 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 + * 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 "TextureRecorded.h" + +#include "mozilla/gfx/gfxVars.h" +#include "RecordedCanvasEventImpl.h" + +namespace mozilla { +namespace layers { + +// The texture ID is used in the GPU process both to lookup the real texture in +// the canvas threads and to lookup the SurfaceDescriptor for that texture in +// the compositor thread. It is therefore important that the ID is unique (per +// recording process), otherwise an old descriptor can be picked up. This means +// we can't use the pointer in the recording process as an ID like we do for +// other objects. +static int64_t sNextRecordedTextureId = 0; + +RecordedTextureData::RecordedTextureData( + already_AddRefed<CanvasChild> aCanvasChild, gfx::IntSize aSize, + gfx::SurfaceFormat aFormat, TextureType aTextureType) + : mCanvasChild(aCanvasChild), mSize(aSize), mFormat(aFormat) { + mCanvasChild->EnsureRecorder(aTextureType); +} + +RecordedTextureData::~RecordedTextureData() { + mCanvasChild->RecordEvent(RecordedTextureDestruction(mTextureId)); +} + +void RecordedTextureData::FillInfo(TextureData::Info& aInfo) const { + aInfo.size = mSize; + aInfo.format = mFormat; + aInfo.supportsMoz2D = true; + aInfo.hasIntermediateBuffer = false; + aInfo.hasSynchronization = true; +} + +bool RecordedTextureData::Lock(OpenMode aMode) { + mCanvasChild->EnsureBeginTransaction(); + if (!mDT) { + mTextureId = sNextRecordedTextureId++; + mCanvasChild->RecordEvent(RecordedNextTextureId(mTextureId)); + mDT = mCanvasChild->CreateDrawTarget(mSize, mFormat); + if (!mDT) { + return false; + } + + // We lock the TextureData when we create it to get the remote DrawTarget. + mCanvasChild->OnTextureWriteLock(); + return true; + } + + mCanvasChild->RecordEvent(RecordedTextureLock(mTextureId, aMode)); + if (aMode & OpenMode::OPEN_WRITE) { + mCanvasChild->OnTextureWriteLock(); + mSnapshot = nullptr; + } + return true; +} + +void RecordedTextureData::Unlock() { + mCanvasChild->RecordEvent(RecordedTextureUnlock(mTextureId)); +} + +already_AddRefed<gfx::DrawTarget> RecordedTextureData::BorrowDrawTarget() { + return do_AddRef(mDT); +} + +already_AddRefed<gfx::SourceSurface> RecordedTextureData::BorrowSnapshot() { + MOZ_ASSERT(mDT); + + mSnapshot = mDT->Snapshot(); + return mCanvasChild->WrapSurface(mSnapshot); +} + +void RecordedTextureData::Deallocate(LayersIPCChannel* aAllocator) {} + +bool RecordedTextureData::Serialize(SurfaceDescriptor& aDescriptor) { + aDescriptor = SurfaceDescriptorRecorded(mTextureId); + return true; +} + +void RecordedTextureData::OnForwardedToHost() { + mCanvasChild->OnTextureForwarded(); + if (mSnapshot && mCanvasChild->ShouldCacheDataSurface()) { + mCanvasChild->RecordEvent(RecordedCacheDataSurface(mSnapshot.get())); + } +} + +TextureFlags RecordedTextureData::GetTextureFlags() const { + TextureFlags flags = TextureFlags::NO_FLAGS; + // With WebRender, resource open happens asynchronously on RenderThread. + // Use WAIT_HOST_USAGE_END to keep TextureClient alive during host side usage. + if (gfx::gfxVars::UseWebRender()) { + flags |= TextureFlags::WAIT_HOST_USAGE_END; + } + return flags; +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/TextureRecorded.h b/gfx/layers/client/TextureRecorded.h new file mode 100644 index 0000000000..2db9caf8a6 --- /dev/null +++ b/gfx/layers/client/TextureRecorded.h @@ -0,0 +1,57 @@ +/* -*- 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 + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_layers_TextureRecorded_h +#define mozilla_layers_TextureRecorded_h + +#include "TextureClient.h" +#include "mozilla/layers/CanvasChild.h" +#include "mozilla/layers/LayersTypes.h" + +namespace mozilla { +namespace layers { + +class RecordedTextureData final : public TextureData { + public: + RecordedTextureData(already_AddRefed<CanvasChild> aCanvasChild, + gfx::IntSize aSize, gfx::SurfaceFormat aFormat, + TextureType aTextureType); + + void FillInfo(TextureData::Info& aInfo) const final; + + bool Lock(OpenMode aMode) final; + + void Unlock() final; + + already_AddRefed<gfx::DrawTarget> BorrowDrawTarget() final; + + already_AddRefed<gfx::SourceSurface> BorrowSnapshot() final; + + void Deallocate(LayersIPCChannel* aAllocator) final; + + bool Serialize(SurfaceDescriptor& aDescriptor) final; + + void OnForwardedToHost() final; + + TextureFlags GetTextureFlags() const final; + + private: + DISALLOW_COPY_AND_ASSIGN(RecordedTextureData); + + ~RecordedTextureData() override; + + int64_t mTextureId; + RefPtr<CanvasChild> mCanvasChild; + gfx::IntSize mSize; + gfx::SurfaceFormat mFormat; + RefPtr<gfx::DrawTarget> mDT; + RefPtr<gfx::SourceSurface> mSnapshot; +}; + +} // namespace layers +} // namespace mozilla + +#endif // mozilla_layers_TextureRecorded_h diff --git a/gfx/layers/client/TiledContentClient.cpp b/gfx/layers/client/TiledContentClient.cpp new file mode 100644 index 0000000000..a5a95ef36b --- /dev/null +++ b/gfx/layers/client/TiledContentClient.cpp @@ -0,0 +1,734 @@ +/* -*- 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 + * 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 "mozilla/layers/TiledContentClient.h" +#include <math.h> // for ceil, ceilf, floor +#include <algorithm> +#include "ClientTiledPaintedLayer.h" // for ClientTiledPaintedLayer +#include "GeckoProfiler.h" // for AUTO_PROFILER_LABEL +#include "ClientLayerManager.h" // for ClientLayerManager +#include "gfxContext.h" // for gfxContext, etc +#include "gfxPlatform.h" // for gfxPlatform +#include "gfxRect.h" // for gfxRect +#include "mozilla/MathAlgorithms.h" // for Abs +#include "mozilla/gfx/Point.h" // for IntSize +#include "mozilla/gfx/Rect.h" // for Rect +#include "mozilla/gfx/Tools.h" // for BytesPerPixel +#include "mozilla/layers/APZUtils.h" // for AboutToCheckerboard +#include "mozilla/layers/CompositableForwarder.h" +#include "mozilla/layers/CompositorBridgeChild.h" // for CompositorBridgeChild +#include "mozilla/layers/LayerMetricsWrapper.h" +#include "mozilla/layers/ShadowLayers.h" // for ShadowLayerForwarder +#include "mozilla/layers/PaintThread.h" // for PaintThread +#include "TextureClientPool.h" +#include "nsISupportsImpl.h" // for gfxContext::AddRef, etc +#include "nsExpirationTracker.h" // for nsExpirationTracker +#include "nsMathUtils.h" // for NS_lroundf +#include "TiledLayerBuffer.h" +#include "UnitTransforms.h" // for TransformTo +#include "mozilla/StaticPrefs_layers.h" +#include "mozilla/UniquePtr.h" + +#ifdef GFX_TILEDLAYER_DEBUG_OVERLAY +# include "cairo.h" +# include <sstream> +using mozilla::layers::Layer; +static void DrawDebugOverlay(mozilla::gfx::DrawTarget* dt, int x, int y, + int width, int height) { + gfxContext c(dt); + + // Draw border + c.NewPath(); + c.SetDeviceColor(Color(0.f, 0.f, 0.f)); + c.Rectangle(gfxRect(0, 0, width, height)); + c.Stroke(); + + // Build tile description + std::stringstream ss; + ss << x << ", " << y; + + // Draw text using cairo toy text API + // XXX: this drawing will silently fail if |dt| doesn't have a Cairo backend + cairo_t* cr = gfxFont::RefCairo(dt); + cairo_set_font_size(cr, 25); + cairo_text_extents_t extents; + cairo_text_extents(cr, ss.str().c_str(), &extents); + + int textWidth = extents.width + 6; + + c.NewPath(); + c.SetDeviceColor(Color(0.f, 0.f, 0.f)); + c.Rectangle(gfxRect(gfxPoint(2, 2), gfxSize(textWidth, 30))); + c.Fill(); + + c.NewPath(); + c.SetDeviceColor(Color(1.0, 0.0, 0.0)); + c.Rectangle(gfxRect(gfxPoint(2, 2), gfxSize(textWidth, 30))); + c.Stroke(); + + c.NewPath(); + cairo_move_to(cr, 4, 28); + cairo_show_text(cr, ss.str().c_str()); +} + +#endif + +namespace mozilla { + +using namespace gfx; + +namespace layers { + +SharedFrameMetricsHelper::SharedFrameMetricsHelper() + : mLastProgressiveUpdateWasLowPrecision(false), + mProgressiveUpdateWasInDanger(false) { + MOZ_COUNT_CTOR(SharedFrameMetricsHelper); +} + +SharedFrameMetricsHelper::~SharedFrameMetricsHelper() { + MOZ_COUNT_DTOR(SharedFrameMetricsHelper); +} + +static inline bool FuzzyEquals(float a, float b) { + return (fabsf(a - b) < 1e-6); +} + +static AsyncTransform ComputeViewTransform( + const FrameMetrics& aContentMetrics, + const FrameMetrics& aCompositorMetrics) { + // This is basically the same code as + // AsyncPanZoomController::GetCurrentAsyncTransform but with aContentMetrics + // used in place of mLastContentPaintMetrics, because they should be + // equivalent, modulo race conditions while transactions are inflight. + + ParentLayerPoint translation = (aCompositorMetrics.GetVisualScrollOffset() - + aContentMetrics.GetLayoutScrollOffset()) * + aCompositorMetrics.GetZoom(); + return AsyncTransform(aCompositorMetrics.GetAsyncZoom(), -translation); +} + +bool SharedFrameMetricsHelper::UpdateFromCompositorFrameMetrics( + const LayerMetricsWrapper& aLayer, bool aHasPendingNewThebesContent, + bool aLowPrecision, AsyncTransform& aViewTransform) { + MOZ_ASSERT(aLayer); + + CompositorBridgeChild* compositor = nullptr; + if (aLayer.Manager() && aLayer.Manager()->AsClientLayerManager()) { + compositor = + aLayer.Manager()->AsClientLayerManager()->GetCompositorBridgeChild(); + } + + if (!compositor) { + return false; + } + + const FrameMetrics& contentMetrics = aLayer.Metrics(); + FrameMetrics compositorMetrics; + + if (!compositor->LookupCompositorFrameMetrics(contentMetrics.GetScrollId(), + compositorMetrics)) { + return false; + } + + aViewTransform = ComputeViewTransform(contentMetrics, compositorMetrics); + + // Reset the checkerboard risk flag when switching to low precision + // rendering. + if (aLowPrecision && !mLastProgressiveUpdateWasLowPrecision) { + // Skip low precision rendering until we're at risk of checkerboarding. + if (!mProgressiveUpdateWasInDanger) { + TILING_LOG( + "TILING: Aborting low-precision rendering because not at risk of " + "checkerboarding\n"); + return true; + } + mProgressiveUpdateWasInDanger = false; + } + mLastProgressiveUpdateWasLowPrecision = aLowPrecision; + + // Always abort updates if the resolution has changed. There's no use + // in drawing at the incorrect resolution. + if (!FuzzyEquals(compositorMetrics.GetZoom().xScale, + contentMetrics.GetZoom().xScale) || + !FuzzyEquals(compositorMetrics.GetZoom().yScale, + contentMetrics.GetZoom().yScale)) { + TILING_LOG("TILING: Aborting because resolution changed from %s to %s\n", + ToString(contentMetrics.GetZoom()).c_str(), + ToString(compositorMetrics.GetZoom()).c_str()); + return true; + } + + // Never abort drawing if we can't be sure we've sent a more recent + // display-port. If we abort updating when we shouldn't, we can end up + // with blank regions on the screen and we open up the risk of entering + // an endless updating cycle. + // XXX Suspicious comparisons between layout and visual scroll offsets. + // This may not do the right thing when we're zoomed in. + // However, note that the code as written will err on the side of returning + // false (whenever we're zoomed in and there's a persistent nonzero offset + // between the layout and visual viewports), which is the safer option. + if (fabsf(contentMetrics.GetLayoutScrollOffset().x - + compositorMetrics.GetVisualScrollOffset().x) <= 2 && + fabsf(contentMetrics.GetLayoutScrollOffset().y - + compositorMetrics.GetVisualScrollOffset().y) <= 2 && + fabsf(contentMetrics.GetDisplayPort().X() - + compositorMetrics.GetDisplayPort().X()) <= 2 && + fabsf(contentMetrics.GetDisplayPort().Y() - + compositorMetrics.GetDisplayPort().Y()) <= 2 && + fabsf(contentMetrics.GetDisplayPort().Width() - + compositorMetrics.GetDisplayPort().Width()) <= 2 && + fabsf(contentMetrics.GetDisplayPort().Height() - + compositorMetrics.GetDisplayPort().Height()) <= 2) { + return false; + } + + // When not a low precision pass and the page is in danger of checker boarding + // abort update. + if (!aLowPrecision && !mProgressiveUpdateWasInDanger) { + const nsTArray<ScrollPositionUpdate>& scrollUpdates = + aLayer.Metadata().GetScrollUpdates(); + bool scrollUpdatePending = !scrollUpdates.IsEmpty() && + compositorMetrics.GetScrollGeneration() < + scrollUpdates.LastElement().GetGeneration(); + // If scrollUpdatePending is true, then that means the content-side + // metrics has a new scroll offset that is going to be forced into the + // compositor but it hasn't gotten there yet. + // Even though right now comparing the metrics might indicate we're + // about to checkerboard (and that's true), the checkerboarding will + // disappear as soon as the new scroll offset update is processed + // on the compositor side. To avoid leaving things in a low-precision + // paint, we need to detect and handle this case (bug 1026756). + if (!scrollUpdatePending && + apz::AboutToCheckerboard(contentMetrics, compositorMetrics)) { + mProgressiveUpdateWasInDanger = true; + return true; + } + } + + // Abort drawing stale low-precision content if there's a more recent + // display-port in the pipeline. + if (aLowPrecision && !aHasPendingNewThebesContent) { + TILING_LOG( + "TILING: Aborting low-precision because of new pending content\n"); + return true; + } + + return false; +} + +bool ClientTiledLayerBuffer::HasFormatChanged() const { + SurfaceMode mode; + gfxContentType content = GetContentType(&mode); + return content != mLastPaintContentType || mode != mLastPaintSurfaceMode; +} + +gfxContentType ClientTiledLayerBuffer::GetContentType( + SurfaceMode* aMode) const { + gfxContentType content = mPaintedLayer.CanUseOpaqueSurface() + ? gfxContentType::COLOR + : gfxContentType::COLOR_ALPHA; + SurfaceMode mode = mPaintedLayer.GetSurfaceMode(); + + if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { +#if defined(MOZ_GFX_OPTIMIZE_MOBILE) + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; +#else + if (!mPaintedLayer.GetParent() || + !mPaintedLayer.GetParent()->SupportsComponentAlphaChildren()) { + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; + } else { + content = gfxContentType::COLOR; + } +#endif + } else if (mode == SurfaceMode::SURFACE_OPAQUE) { +#if defined(MOZ_GFX_OPTIMIZE_MOBILE) + if (IsLowPrecision()) { + // If we're in low-res mode, drawing can sample from outside the visible + // region. Make sure that we only sample transparency if that happens. + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; + content = gfxContentType::COLOR_ALPHA; + } +#else + if (mPaintedLayer.MayResample()) { + mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; + content = gfxContentType::COLOR_ALPHA; + } +#endif + } + + if (aMode) { + *aMode = mode; + } + return content; +} + +class TileExpiry final : public nsExpirationTracker<TileClient, 3> { + public: + TileExpiry() : nsExpirationTracker<TileClient, 3>(1000, "TileExpiry") {} + + static void AddTile(TileClient* aTile) { + if (!sTileExpiry) { + sTileExpiry = MakeUnique<TileExpiry>(); + } + + sTileExpiry->AddObject(aTile); + } + + static void RemoveTile(TileClient* aTile) { + MOZ_ASSERT(sTileExpiry); + sTileExpiry->RemoveObject(aTile); + } + + static void Shutdown() { sTileExpiry = nullptr; } + + private: + virtual void NotifyExpired(TileClient* aTile) override { + aTile->DiscardBackBuffer(); + } + + static UniquePtr<TileExpiry> sTileExpiry; +}; +UniquePtr<TileExpiry> TileExpiry::sTileExpiry; + +void ShutdownTileCache() { TileExpiry::Shutdown(); } + +void TileClient::PrivateProtector::Set(TileClient* const aContainer, + RefPtr<TextureClient> aNewValue) { + if (mBuffer) { + TileExpiry::RemoveTile(aContainer); + } + mBuffer = aNewValue; + if (mBuffer) { + TileExpiry::AddTile(aContainer); + } +} + +void TileClient::PrivateProtector::Set(TileClient* const aContainer, + TextureClient* aNewValue) { + Set(aContainer, RefPtr<TextureClient>(aNewValue)); +} + +// Placeholder +TileClient::TileClient() : mWasPlaceholder(false) {} + +TileClient::~TileClient() { + if (mExpirationState.IsTracked()) { + MOZ_ASSERT(mBackBuffer); + TileExpiry::RemoveTile(this); + } +} + +TileClient::TileClient(const TileClient& o) { + mBackBuffer.Set(this, o.mBackBuffer); + mBackBufferOnWhite = o.mBackBufferOnWhite; + mFrontBuffer = o.mFrontBuffer; + mFrontBufferOnWhite = o.mFrontBufferOnWhite; + mWasPlaceholder = o.mWasPlaceholder; + mUpdateRect = o.mUpdateRect; +#ifdef GFX_TILEDLAYER_DEBUG_OVERLAY + mLastUpdate = o.mLastUpdate; +#endif + mAllocator = o.mAllocator; + mInvalidFront = o.mInvalidFront; + mInvalidBack = o.mInvalidBack; +} + +TileClient& TileClient::operator=(const TileClient& o) { + if (this == &o) return *this; + mBackBuffer.Set(this, o.mBackBuffer); + mBackBufferOnWhite = o.mBackBufferOnWhite; + mFrontBuffer = o.mFrontBuffer; + mFrontBufferOnWhite = o.mFrontBufferOnWhite; + mWasPlaceholder = o.mWasPlaceholder; + mUpdateRect = o.mUpdateRect; +#ifdef GFX_TILEDLAYER_DEBUG_OVERLAY + mLastUpdate = o.mLastUpdate; +#endif + mAllocator = o.mAllocator; + mInvalidFront = o.mInvalidFront; + mInvalidBack = o.mInvalidBack; + return *this; +} + +void TileClient::Dump(std::stringstream& aStream) { + aStream << "TileClient(bb=" << (TextureClient*)mBackBuffer + << " fb=" << mFrontBuffer.get(); + if (mBackBufferOnWhite) { + aStream << " bbow=" << mBackBufferOnWhite.get(); + } + if (mFrontBufferOnWhite) { + aStream << " fbow=" << mFrontBufferOnWhite.get(); + } + aStream << ")"; +} + +void TileClient::Flip() { + RefPtr<TextureClient> frontBuffer = mFrontBuffer; + RefPtr<TextureClient> frontBufferOnWhite = mFrontBufferOnWhite; + mFrontBuffer = mBackBuffer; + mFrontBufferOnWhite = mBackBufferOnWhite; + mBackBuffer.Set(this, frontBuffer); + mBackBufferOnWhite = frontBufferOnWhite; + nsIntRegion invalidFront = mInvalidFront; + mInvalidFront = mInvalidBack; + mInvalidBack = invalidFront; +} + +void TileClient::ValidateFromFront( + const nsIntRegion& aDirtyRegion, const nsIntRegion& aVisibleRegion, + gfx::DrawTarget* aBackBuffer, TilePaintFlags aFlags, IntRect* aCopiedRect, + AutoTArray<RefPtr<TextureClient>, 4>* aClients) { + if (!mBackBuffer || !mFrontBuffer) { + return; + } + + gfx::IntSize tileSize = mFrontBuffer->GetSize(); + const IntRect tileRect = IntRect(0, 0, tileSize.width, tileSize.height); + + if (aDirtyRegion.Contains(tileRect)) { + // The dirty region means that we no longer need the front buffer, so + // discard it. + DiscardFrontBuffer(); + return; + } + + // Region that needs copying. + nsIntRegion regionToCopy = mInvalidBack; + + regionToCopy.Sub(regionToCopy, aDirtyRegion); + regionToCopy.And(regionToCopy, aVisibleRegion); + + *aCopiedRect = regionToCopy.GetBounds(); + + if (regionToCopy.IsEmpty()) { + // Just redraw it all. + return; + } + + // Copy the bounding rect of regionToCopy. As tiles are quite small, it + // is unlikely that we'd save much by copying each individual rect of the + // region, but we can reevaluate this if it becomes an issue. + const IntRect rectToCopy = regionToCopy.GetBounds(); + OpenMode readMode = !!(aFlags & TilePaintFlags::Async) + ? OpenMode::OPEN_READ_ASYNC + : OpenMode::OPEN_READ; + + DualTextureClientAutoLock frontBuffer(mFrontBuffer, mFrontBufferOnWhite, + readMode); + if (!frontBuffer.Succeeded()) { + return; + } + + RefPtr<gfx::SourceSurface> frontSurface = frontBuffer->Snapshot(); + aBackBuffer->CopySurface(frontSurface, rectToCopy, rectToCopy.TopLeft()); + + if (aFlags & TilePaintFlags::Async) { + aClients->AppendElement(mFrontBuffer); + if (mFrontBufferOnWhite) { + aClients->AppendElement(mFrontBufferOnWhite); + } + } + + mInvalidBack.Sub(mInvalidBack, aVisibleRegion); +} + +void TileClient::DiscardFrontBuffer() { + if (mFrontBuffer) { + MOZ_ASSERT(mFrontBuffer->GetReadLock()); + + MOZ_ASSERT(mAllocator); + if (mAllocator) { + mAllocator->ReturnTextureClientDeferred(mFrontBuffer); + if (mFrontBufferOnWhite) { + mAllocator->ReturnTextureClientDeferred(mFrontBufferOnWhite); + } + } + + if (mFrontBuffer->IsLocked()) { + mFrontBuffer->Unlock(); + } + if (mFrontBufferOnWhite && mFrontBufferOnWhite->IsLocked()) { + mFrontBufferOnWhite->Unlock(); + } + mFrontBuffer = nullptr; + mFrontBufferOnWhite = nullptr; + } +} + +static void DiscardTexture(TextureClient* aTexture, + TextureClientAllocator* aAllocator) { + MOZ_ASSERT(aAllocator); + if (aTexture && aAllocator) { + MOZ_ASSERT(aTexture->GetReadLock()); + if (!aTexture->HasSynchronization() && aTexture->IsReadLocked()) { + // Our current back-buffer is still locked by the compositor. This can + // occur when the client is producing faster than the compositor can + // consume. In this case we just want to drop it and not return it to the + // pool. + aAllocator->ReportClientLost(); + } else { + aAllocator->ReturnTextureClientDeferred(aTexture); + } + if (aTexture->IsLocked()) { + aTexture->Unlock(); + } + } +} + +void TileClient::DiscardBackBuffer() { + if (mBackBuffer) { + DiscardTexture(mBackBuffer, mAllocator); + mBackBuffer.Set(this, nullptr); + DiscardTexture(mBackBufferOnWhite, mAllocator); + mBackBufferOnWhite = nullptr; + } +} + +static already_AddRefed<TextureClient> CreateBackBufferTexture( + TextureClient* aCurrentTexture, CompositableClient& aCompositable, + TextureClientAllocator* aAllocator) { + if (aCurrentTexture) { + // Our current back-buffer is still locked by the compositor. This can occur + // when the client is producing faster than the compositor can consume. In + // this case we just want to drop it and not return it to the pool. + aAllocator->ReportClientLost(); + } + + RefPtr<TextureClient> texture = aAllocator->GetTextureClient(); + + if (!texture) { + gfxCriticalError() << "[Tiling:Client] Failed to allocate a TextureClient"; + return nullptr; + } + + if (!aCompositable.AddTextureClient(texture)) { + gfxCriticalError() << "[Tiling:Client] Failed to connect a TextureClient"; + return nullptr; + } + + return texture.forget(); +} + +void TileClient::GetSyncTextureSerials(SurfaceMode aMode, + nsTArray<uint64_t>& aSerials) { + if (mFrontBuffer && mFrontBuffer->HasIntermediateBuffer() && + !mFrontBuffer->IsReadLocked() && + (aMode != SurfaceMode::SURFACE_COMPONENT_ALPHA || + (mFrontBufferOnWhite && !mFrontBufferOnWhite->IsReadLocked()))) { + return; + } + + if (mBackBuffer && !mBackBuffer->HasIntermediateBuffer() && + mBackBuffer->IsReadLocked()) { + aSerials.AppendElement(mBackBuffer->GetSerial()); + } + + if (aMode == SurfaceMode::SURFACE_COMPONENT_ALPHA && mBackBufferOnWhite && + !mBackBufferOnWhite->HasIntermediateBuffer() && + mBackBufferOnWhite->IsReadLocked()) { + aSerials.AppendElement(mBackBufferOnWhite->GetSerial()); + } +} + +Maybe<AcquiredBackBuffer> TileClient::AcquireBackBuffer( + CompositableClient& aCompositable, const nsIntRegion& aDirtyRegion, + const nsIntRegion& aVisibleRegion, gfxContentType aContent, + SurfaceMode aMode, TilePaintFlags aFlags) { + AUTO_PROFILER_LABEL("TileClient::AcquireBackBuffer", GRAPHICS_TileAllocation); + if (!mAllocator) { + gfxCriticalError() << "[TileClient] Missing TextureClientAllocator."; + return Nothing(); + } + if (aMode != SurfaceMode::SURFACE_COMPONENT_ALPHA) { + // It can happen that a component-alpha layer stops being on component alpha + // on the next frame, just drop the buffers on white if that happens. + if (mBackBufferOnWhite) { + mAllocator->ReportClientLost(); + mBackBufferOnWhite = nullptr; + } + if (mFrontBufferOnWhite) { + mAllocator->ReportClientLost(); + mFrontBufferOnWhite = nullptr; + } + } + + // Try to re-use the front-buffer if possible + if (mFrontBuffer && mFrontBuffer->HasIntermediateBuffer() && + !mFrontBuffer->IsReadLocked() && + (aMode != SurfaceMode::SURFACE_COMPONENT_ALPHA || + (mFrontBufferOnWhite && !mFrontBufferOnWhite->IsReadLocked()))) { + // If we had a backbuffer we no longer need it since we can re-use the + // front buffer here. It can be worth it to hold on to the back buffer + // so we don't need to pay the cost of initializing a new back buffer + // later (copying pixels and texture upload). But this could increase + // our memory usage and lead to OOM more frequently from spikes in usage, + // so we have this behavior behind a pref. + if (!StaticPrefs::layers_tiles_retain_back_buffer()) { + DiscardBackBuffer(); + } + Flip(); + } else { + if (!mBackBuffer || mBackBuffer->IsReadLocked()) { + mBackBuffer.Set(this, CreateBackBufferTexture(mBackBuffer, aCompositable, + mAllocator)); + if (!mBackBuffer) { + DiscardBackBuffer(); + DiscardFrontBuffer(); + return Nothing(); + } + mInvalidBack = IntRect(IntPoint(), mBackBuffer->GetSize()); + } + + if (aMode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { + if (!mBackBufferOnWhite || mBackBufferOnWhite->IsReadLocked()) { + mBackBufferOnWhite = CreateBackBufferTexture(mBackBufferOnWhite, + aCompositable, mAllocator); + if (!mBackBufferOnWhite) { + DiscardBackBuffer(); + DiscardFrontBuffer(); + return Nothing(); + } + mInvalidBack = IntRect(IntPoint(), mBackBufferOnWhite->GetSize()); + } + } + } + + // Lock the texture clients + OpenMode lockMode = aFlags & TilePaintFlags::Async + ? OpenMode::OPEN_READ_WRITE_ASYNC + : OpenMode::OPEN_READ_WRITE; + + if (!mBackBuffer->Lock(lockMode)) { + gfxCriticalError() << "[Tiling:Client] Failed to lock a tile (B)"; + DiscardBackBuffer(); + DiscardFrontBuffer(); + return Nothing(); + } + + if (mBackBufferOnWhite && !mBackBufferOnWhite->Lock(lockMode)) { + gfxCriticalError() << "[Tiling:Client] Failed to lock a tile (W)"; + DiscardBackBuffer(); + DiscardFrontBuffer(); + return Nothing(); + } + + // Borrow their draw targets + RefPtr<gfx::DrawTarget> backBuffer = mBackBuffer->BorrowDrawTarget(); + RefPtr<gfx::DrawTarget> backBufferOnWhite = nullptr; + if (mBackBufferOnWhite) { + backBufferOnWhite = mBackBufferOnWhite->BorrowDrawTarget(); + } + + if (!backBuffer || (mBackBufferOnWhite && !backBufferOnWhite)) { + gfxCriticalError() + << "[Tiling:Client] Failed to acquire draw targets for tile"; + DiscardBackBuffer(); + DiscardFrontBuffer(); + return Nothing(); + } + + // Construct a dual target if necessary + RefPtr<DrawTarget> target; + if (backBufferOnWhite) { + backBuffer = Factory::CreateDualDrawTarget(backBuffer, backBufferOnWhite); + backBufferOnWhite = nullptr; + target = backBuffer; + } else { + target = backBuffer; + } + + // Construct a capture draw target if necessary + RefPtr<DrawTargetCapture> capture; + if (aFlags & TilePaintFlags::Async) { + capture = Factory::CreateCaptureDrawTargetForTarget( + target, StaticPrefs::layers_omtp_capture_limit_AtStartup()); + target = capture; + } + + // Gather texture clients + AutoTArray<RefPtr<TextureClient>, 4> clients; + clients.AppendElement(RefPtr<TextureClient>(mBackBuffer)); + if (mBackBufferOnWhite) { + clients.AppendElement(mBackBufferOnWhite); + } + + // Copy from the front buerr to the back if necessary + IntRect updatedRect; + ValidateFromFront(aDirtyRegion, aVisibleRegion, target, aFlags, &updatedRect, + &clients); + + return Some(AcquiredBackBuffer{ + target, + capture, + backBuffer, + std::move(updatedRect), + std::move(clients), + }); +} + +TileDescriptor TileClient::GetTileDescriptor() { + if (IsPlaceholderTile()) { + mWasPlaceholder = true; + return PlaceholderTileDescriptor(); + } + bool wasPlaceholder = mWasPlaceholder; + mWasPlaceholder = false; + + bool readLocked = mFrontBuffer->OnForwardedToHost(); + bool readLockedOnWhite = false; + + if (mFrontBufferOnWhite) { + readLockedOnWhite = mFrontBufferOnWhite->OnForwardedToHost(); + } + + return TexturedTileDescriptor( + nullptr, mFrontBuffer->GetIPDLActor(), Nothing(), + mFrontBufferOnWhite ? Some(mFrontBufferOnWhite->GetIPDLActor()) + : Nothing(), + mUpdateRect, readLocked, readLockedOnWhite, wasPlaceholder); +} + +void ClientTiledLayerBuffer::UnlockTile(TileClient& aTile) { + // We locked the back buffer, and flipped so we now need to unlock the front + if (aTile.mFrontBuffer && aTile.mFrontBuffer->IsLocked()) { + aTile.mFrontBuffer->Unlock(); + aTile.mFrontBuffer->SyncWithObject( + mCompositableClient.GetForwarder()->GetSyncObject()); + } + if (aTile.mFrontBufferOnWhite && aTile.mFrontBufferOnWhite->IsLocked()) { + aTile.mFrontBufferOnWhite->Unlock(); + aTile.mFrontBufferOnWhite->SyncWithObject( + mCompositableClient.GetForwarder()->GetSyncObject()); + } + if (aTile.mBackBuffer && aTile.mBackBuffer->IsLocked()) { + aTile.mBackBuffer->Unlock(); + } + if (aTile.mBackBufferOnWhite && aTile.mBackBufferOnWhite->IsLocked()) { + aTile.mBackBufferOnWhite->Unlock(); + } +} + +void TiledContentClient::PrintInfo(std::stringstream& aStream, + const char* aPrefix) { + aStream << aPrefix; + aStream << nsPrintfCString("%sTiledContentClient (0x%p)", mName, this).get(); +} + +void TiledContentClient::Dump(std::stringstream& aStream, const char* aPrefix, + bool aDumpHtml, TextureDumpMode aCompress) { + GetTiledBuffer()->Dump(aStream, aPrefix, aDumpHtml, aCompress); +} + +void BasicTiledLayerPaintData::ResetPaintData() { + mLowPrecisionPaintCount = 0; + mPaintFinished = false; + mHasTransformAnimation = false; + mCompositionBounds.SetEmpty(); + mCriticalDisplayPort = Nothing(); +} + +} // namespace layers +} // namespace mozilla diff --git a/gfx/layers/client/TiledContentClient.h b/gfx/layers/client/TiledContentClient.h new file mode 100644 index 0000000000..5657572a15 --- /dev/null +++ b/gfx/layers/client/TiledContentClient.h @@ -0,0 +1,406 @@ +/* -*- 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 + * 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/. */ + +#ifndef MOZILLA_GFX_TILEDCONTENTCLIENT_H +#define MOZILLA_GFX_TILEDCONTENTCLIENT_H + +#include <cstdint> // for uint64_t, uint16_t, uint8_t +#include <iosfwd> // for stringstream +#include <utility> // for move +#include "ClientLayerManager.h" // for ClientLayerManager +#include "Units.h" // for CSSToParentLayerScale2D, ParentLayerPoint, LayerIntRect, LayerRect, LayerToParent... +#include "gfxTypes.h" // for gfxContentType, gfxContentType::COLOR +#include "mozilla/Maybe.h" // for Maybe +#include "mozilla/RefPtr.h" // for RefPtr, operator==, operator!= +#include "mozilla/TypedEnumBits.h" // for CastableTypedEnumResult, UnsignedIntegerTypeForEnum, MOZ_MAKE_ENUM_CLASS_BITWISE_... +#include "mozilla/gfx/2D.h" // for DrawTarget, DrawTargetCapture +#include "mozilla/gfx/Rect.h" // for IntRect +#include "mozilla/layers/CompositableClient.h" // for CompositableClient +#include "mozilla/layers/CompositorTypes.h" // for TextureInfo, CompositableType, CompositableType::CONTENT_TILED +#include "mozilla/layers/LayerManager.h" // for LayerManager, LayerManager::DrawPaintedLayerCallback +#include "mozilla/layers/LayersMessages.h" // for TileDescriptor +#include "mozilla/layers/LayersTypes.h" // for SurfaceMode, TextureDumpMode, Compress, SurfaceMode::SURFACE_OPAQUE +#include "mozilla/layers/ShadowLayers.h" // for ShadowLayerForwarder +#include "mozilla/layers/TextureClient.h" // for TextureClient +#include "mozilla/layers/TextureClientPool.h" // for TextureClientAllocator +#include "nsExpirationTracker.h" // for nsExpirationState +#include "nsRegion.h" // for nsIntRegion +#include "nsTArray.h" // for AutoTArray, nsTArray (ptr only) + +namespace mozilla { +namespace layers { + +class ClientTiledPaintedLayer; +class LayerMetricsWrapper; +struct AsyncTransform; +struct FrameMetrics; + +enum class TilePaintFlags : uint8_t { + None = 0x0, + Async = 0x1, + Progressive = 0x2, +}; +MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(TilePaintFlags) + +void ShutdownTileCache(); + +struct AcquiredBackBuffer { + AcquiredBackBuffer(gfx::DrawTarget* aTarget, gfx::DrawTargetCapture* aCapture, + gfx::DrawTarget* aBackBuffer, + const gfx::IntRect& aUpdatedRect, + AutoTArray<RefPtr<TextureClient>, 4>&& aTextureClients) + : mTarget(aTarget), + mCapture(aCapture), + mBackBuffer(aBackBuffer), + mUpdatedRect(aUpdatedRect), + mTextureClients(std::move(aTextureClients)) {} + + AcquiredBackBuffer(const AcquiredBackBuffer&) = delete; + AcquiredBackBuffer& operator=(const AcquiredBackBuffer&) = delete; + + AcquiredBackBuffer(AcquiredBackBuffer&&) = default; + AcquiredBackBuffer& operator=(AcquiredBackBuffer&&) = default; + + RefPtr<gfx::DrawTarget> mTarget; + RefPtr<gfx::DrawTargetCapture> mCapture; + RefPtr<gfx::DrawTarget> mBackBuffer; + gfx::IntRect mUpdatedRect; + AutoTArray<RefPtr<TextureClient>, 4> mTextureClients; +}; + +/** + * Represent a single tile in tiled buffer. The buffer keeps tiles, + * each tile keeps a reference to a texture client and a read-lock. This + * read-lock is used to help implement a copy-on-write mechanism. The tile + * should be locked before being sent to the compositor. The compositor should + * unlock the read-lock as soon as it has finished with the buffer in the + * TextureHost to prevent more textures being created than is necessary. + * Ideal place to store per tile debug information. + */ +struct TileClient { + // Placeholder + TileClient(); + ~TileClient(); + + TileClient(const TileClient& o); + + TileClient& operator=(const TileClient& o); + + bool operator==(const TileClient& o) const { + return mFrontBuffer == o.mFrontBuffer; + } + + bool operator!=(const TileClient& o) const { + return mFrontBuffer != o.mFrontBuffer; + } + + void SetTextureAllocator(TextureClientAllocator* aAllocator) { + mAllocator = aAllocator; + } + + bool IsPlaceholderTile() const { + return mBackBuffer == nullptr && mFrontBuffer == nullptr; + } + + void DiscardBuffers() { + DiscardFrontBuffer(); + DiscardBackBuffer(); + } + + nsExpirationState* GetExpirationState() { return &mExpirationState; } + + TileDescriptor GetTileDescriptor(); + + /** + * For debugging. + */ + void Dump(std::stringstream& aStream); + + /** + * Swaps the front and back buffers. + */ + void Flip(); + + void DumpTexture(std::stringstream& aStream, TextureDumpMode aCompress) { + // TODO We should combine the OnWhite/OnBlack here an just output a single + // image. + CompositableClient::DumpTextureClient(aStream, mFrontBuffer, aCompress); + } + + void GetSyncTextureSerials(SurfaceMode aMode, nsTArray<uint64_t>& aSerials); + + /** + * Returns an unlocked TextureClient that can be used for writing new + * data to the tile. This may flip the front-buffer to the back-buffer if + * the front-buffer is still locked by the host, or does not have an + * internal buffer (and so will always be locked). + * + * If getting the back buffer required copying pixels from the front buffer + * then the copied region is stored in aAddPaintedRegion so the host side + * knows to upload it. + * + * If nullptr is returned, aTextureClientOnWhite is undefined. + */ + Maybe<AcquiredBackBuffer> AcquireBackBuffer(CompositableClient&, + const nsIntRegion& aDirtyRegion, + const nsIntRegion& aVisibleRegion, + gfxContentType aContent, + SurfaceMode aMode, + TilePaintFlags aFlags); + + void DiscardFrontBuffer(); + + void DiscardBackBuffer(); + + /* We wrap the back buffer in a class that disallows assignment + * so that we can track when ever it changes so that we can update + * the expiry tracker for expiring the back buffers */ + class PrivateProtector { + public: + void Set(TileClient* container, RefPtr<TextureClient>); + void Set(TileClient* container, TextureClient*); + // Implicitly convert to TextureClient* because we can't chain + // implicit conversion that would happen on RefPtr<TextureClient> + operator TextureClient*() const { return mBuffer; } + RefPtr<TextureClient> operator->() { return mBuffer; } + + private: + PrivateProtector& operator=(const PrivateProtector&); + RefPtr<TextureClient> mBuffer; + } mBackBuffer; + RefPtr<TextureClient> mBackBufferOnWhite; + RefPtr<TextureClient> mFrontBuffer; + RefPtr<TextureClient> mFrontBufferOnWhite; + RefPtr<TextureClientAllocator> mAllocator; + gfx::IntRect mUpdateRect; + bool mWasPlaceholder; +#ifdef GFX_TILEDLAYER_DEBUG_OVERLAY + TimeStamp mLastUpdate; +#endif + nsIntRegion mInvalidFront; + nsIntRegion mInvalidBack; + nsExpirationState mExpirationState; + + private: + /* + * Copies dirty pixels from the front buffer into the back buffer, + * and records the copied region in aAddPaintedRegion. + */ + void ValidateFromFront(const nsIntRegion& aDirtyRegion, + const nsIntRegion& aVisibleRegion, + gfx::DrawTarget* aBackBuffer, TilePaintFlags aFlags, + gfx::IntRect* aCopiedRegion, + AutoTArray<RefPtr<TextureClient>, 4>* aClients); +}; + +/** + * This struct stores all the data necessary to perform a paint so that it + * doesn't need to be recalculated on every repeated transaction. + */ +struct BasicTiledLayerPaintData { + /* + * The scroll offset of the content from the nearest ancestor layer that + * represents scrollable content with a display port set. + */ + ParentLayerPoint mScrollOffset; + + /* + * The scroll offset of the content from the nearest ancestor layer that + * represents scrollable content with a display port set, for the last + * layer update transaction. + */ + ParentLayerPoint mLastScrollOffset; + + /* + * The transform matrix to go from this layer's Layer units to + * the scroll ancestor's ParentLayer units. The "scroll ancestor" is + * the closest ancestor layer which scrolls, and is used to obtain + * the composition bounds that are relevant for this layer. + */ + LayerToParentLayerMatrix4x4 mTransformToCompBounds; + + /* + * The critical displayport of the content from the nearest ancestor layer + * that represents scrollable content with a display port set. isNothing() + * if a critical displayport is not set. + */ + Maybe<LayerIntRect> mCriticalDisplayPort; + + /* + * The render resolution of the document that the content this layer + * represents is in. + */ + CSSToParentLayerScale2D mResolution; + + /* + * The composition bounds of the layer, in Layer coordinates. This is + * used to make sure that tiled updates to regions that are visible to the + * user are grouped coherently. + */ + LayerRect mCompositionBounds; + + /* + * Low precision updates are always executed a tile at a time in repeated + * transactions. This counter is set to 1 on the first transaction of a low + * precision update, and incremented for each subsequent transaction. + */ + uint16_t mLowPrecisionPaintCount; + + /* + * Whether this is the first time this layer is painting + */ + bool mFirstPaint : 1; + + /* + * Whether there is further work to complete this paint. This is used to + * determine whether or not to repeat the transaction when painting + * progressively. + */ + bool mPaintFinished : 1; + + /* + * Whether or not there is an async transform animation active + */ + bool mHasTransformAnimation : 1; + + /* + * Initializes/clears data to prepare for paint action. + */ + void ResetPaintData(); +}; + +class SharedFrameMetricsHelper { + public: + SharedFrameMetricsHelper(); + ~SharedFrameMetricsHelper(); + + /** + * This is called by the BasicTileLayer to determine if it is still interested + * in the update of this display-port to continue. We can return true here + * to abort the current update and continue with any subsequent ones. This + * is useful for slow-to-render pages when the display-port starts lagging + * behind enough that continuing to draw it is wasted effort. + */ + bool UpdateFromCompositorFrameMetrics(const LayerMetricsWrapper& aLayer, + bool aHasPendingNewThebesContent, + bool aLowPrecision, + AsyncTransform& aViewTransform); + + /** + * Determines if the compositor's upcoming composition bounds has fallen + * outside of the contents display port. If it has then the compositor + * will start to checker board. Checker boarding is when the compositor + * tries to composite a tile and it is not available. Historically + * a tile with a checker board pattern was used. Now a blank tile is used. + */ + bool AboutToCheckerboard(const FrameMetrics& aContentMetrics, + const FrameMetrics& aCompositorMetrics); + + private: + bool mLastProgressiveUpdateWasLowPrecision; + bool mProgressiveUpdateWasInDanger; +}; + +/** + * Provide an instance of TiledLayerBuffer backed by drawable TextureClients. + * This buffer provides an implementation of ValidateTile using a + * thebes callback and can support painting using a single paint buffer. + * Whether a single paint buffer is used is controlled by + * StaticPrefs::PerTileDrawing(). + */ +class ClientTiledLayerBuffer { + public: + ClientTiledLayerBuffer(ClientTiledPaintedLayer& aPaintedLayer, + CompositableClient& aCompositableClient) + : mPaintedLayer(aPaintedLayer), + mCompositableClient(aCompositableClient), + mLastPaintContentType(gfxContentType::COLOR), + mLastPaintSurfaceMode(SurfaceMode::SURFACE_OPAQUE), + mWasLastPaintProgressive(false) {} + + virtual void PaintThebes(const nsIntRegion& aNewValidRegion, + const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData, TilePaintFlags aFlags) = 0; + virtual void GetSyncTextureSerials(const nsIntRegion& aPaintRegion, + const nsIntRegion& aDirtyRegion, + nsTArray<uint64_t>& aSerials) { + return; + } + + virtual bool SupportsProgressiveUpdate() = 0; + virtual bool ProgressiveUpdate( + const nsIntRegion& aValidRegion, const nsIntRegion& aInvalidRegion, + const nsIntRegion& aOldValidRegion, nsIntRegion& aOutDrawnRegion, + BasicTiledLayerPaintData* aPaintData, + LayerManager::DrawPaintedLayerCallback aCallback, + void* aCallbackData) = 0; + virtual void ResetPaintedAndValidState() = 0; + + virtual const nsIntRegion& GetValidRegion() = 0; + + virtual bool IsLowPrecision() const = 0; + + virtual void Dump(std::stringstream& aStream, const char* aPrefix, + bool aDumpHtml, TextureDumpMode aCompress) {} + + const CSSToParentLayerScale2D& GetFrameResolution() { + return mFrameResolution; + } + void SetFrameResolution(const CSSToParentLayerScale2D& aResolution) { + mFrameResolution = aResolution; + } + + bool HasFormatChanged() const; + + protected: + void UnlockTile(TileClient& aTile); + gfxContentType GetContentType(SurfaceMode* aMode = nullptr) const; + + ClientTiledPaintedLayer& mPaintedLayer; + CompositableClient& mCompositableClient; + + gfxContentType mLastPaintContentType; + SurfaceMode mLastPaintSurfaceMode; + CSSToParentLayerScale2D mFrameResolution; + + bool mWasLastPaintProgressive; +}; + +class TiledContentClient : public CompositableClient { + public: + TiledContentClient(ClientLayerManager* aManager, const char* aName = "") + : CompositableClient(aManager->AsShadowForwarder()), mName(aName) {} + + protected: + ~TiledContentClient() {} + + public: + virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix); + + void Dump(std::stringstream& aStream, const char* aPrefix = "", + bool aDumpHtml = false, + TextureDumpMode aCompress = TextureDumpMode::Compress) override; + + TextureInfo GetTextureInfo() const override { + return TextureInfo(CompositableType::CONTENT_TILED); + } + + virtual ClientTiledLayerBuffer* GetTiledBuffer() = 0; + virtual ClientTiledLayerBuffer* GetLowPrecisionTiledBuffer() = 0; + + enum TiledBufferType { TILED_BUFFER, LOW_PRECISION_TILED_BUFFER }; + virtual void UpdatedBuffer(TiledBufferType aType) = 0; + + private: + const char* mName; +}; + +} // namespace layers +} // namespace mozilla + +#endif // MOZILLA_GFX_TILEDCONTENTCLIENT_H |