From 518212067343760f7bf28b2a0d3f81a25aa043d4 Mon Sep 17 00:00:00 2001 From: sotaro Date: Thu, 19 Sep 2019 18:15:11 +0000 Subject: [PATCH] Bug 1582371 - Add DCLayerTree class r=nical Preparation work for adding DirectComposition support. It does not add a new functionality. Differential Revision: https://phabricator.services.mozilla.com/D46429 --HG-- extra : moz-landing-system : lando --- gfx/webrender_bindings/DCLayerTree.cpp | 66 +++++++++++++++++++ gfx/webrender_bindings/DCLayerTree.h | 47 +++++++++++++ .../RenderCompositorANGLE.cpp | 42 +++--------- .../RenderCompositorANGLE.h | 11 ++-- gfx/webrender_bindings/moz.build | 2 + 5 files changed, 128 insertions(+), 40 deletions(-) create mode 100644 gfx/webrender_bindings/DCLayerTree.cpp create mode 100644 gfx/webrender_bindings/DCLayerTree.h diff --git a/gfx/webrender_bindings/DCLayerTree.cpp b/gfx/webrender_bindings/DCLayerTree.cpp new file mode 100644 index 000000000000..28911cad2942 --- /dev/null +++ b/gfx/webrender_bindings/DCLayerTree.cpp @@ -0,0 +1,66 @@ +/* -*- 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 "DCLayerTree.h" + +#include "mozilla/gfx/DeviceManagerDx.h" + +#undef NTDDI_VERSION +#define NTDDI_VERSION NTDDI_WIN8 + +#include +#include +#include + +namespace mozilla { +namespace wr { + +/* static */ +UniquePtr DCLayerTree::Create(HWND aHwnd) { + RefPtr dCompDevice = + gfx::DeviceManagerDx::Get()->GetDirectCompositionDevice(); + if (!dCompDevice) { + return nullptr; + } + + auto layerTree = MakeUnique(dCompDevice); + if (!layerTree->Initialize(aHwnd)) { + return nullptr; + } + + return layerTree; +} + +DCLayerTree::DCLayerTree(IDCompositionDevice* aCompositionDevice) + : mCompositionDevice(aCompositionDevice) {} + +DCLayerTree::~DCLayerTree() {} + +bool DCLayerTree::Initialize(HWND aHwnd) { + HRESULT hr = mCompositionDevice->CreateTargetForHwnd( + aHwnd, TRUE, getter_AddRefs(mCompositionTarget)); + if (FAILED(hr)) { + gfxCriticalNote << "Could not create DCompositionTarget: " << gfx::hexa(hr); + return false; + } + + hr = mCompositionDevice->CreateVisual(getter_AddRefs(mRootVisual)); + if (FAILED(hr)) { + gfxCriticalNote << "Could not create DCompositionVisualt: " + << gfx::hexa(hr); + return false; + } + return true; +} + +void DCLayerTree::SetDefaultSwapChain(IDXGISwapChain1* aSwapChain) { + mRootVisual->SetContent(aSwapChain); + mCompositionTarget->SetRoot(mRootVisual); + mCompositionDevice->Commit(); +} + +} // namespace wr +} // namespace mozilla diff --git a/gfx/webrender_bindings/DCLayerTree.h b/gfx/webrender_bindings/DCLayerTree.h new file mode 100644 index 000000000000..ee01fb53ddee --- /dev/null +++ b/gfx/webrender_bindings/DCLayerTree.h @@ -0,0 +1,47 @@ +/* -*- 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_DCLAYER_TREE_H +#define MOZILLA_GFX_DCLAYER_TREE_H + +#include + +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" + +struct IDCompositionDevice; +struct IDCompositionTarget; +struct IDCompositionVisual; +struct IDXGISwapChain1; + +namespace mozilla { + +namespace wr { + +/** + * DCLayerTree manages direct composition layers. + * It does not manage gecko's layers::Layer. + */ +class DCLayerTree { + public: + static UniquePtr Create(HWND aHwnd); + explicit DCLayerTree(IDCompositionDevice* aCompositionDevice); + ~DCLayerTree(); + + void SetDefaultSwapChain(IDXGISwapChain1* aSwapChain); + + protected: + bool Initialize(HWND aHwnd); + + RefPtr mCompositionDevice; + RefPtr mCompositionTarget; + RefPtr mRootVisual; +}; + +} // namespace wr +} // namespace mozilla + +#endif diff --git a/gfx/webrender_bindings/RenderCompositorANGLE.cpp b/gfx/webrender_bindings/RenderCompositorANGLE.cpp index fb1d1664afdb..1b7fd2a399a7 100644 --- a/gfx/webrender_bindings/RenderCompositorANGLE.cpp +++ b/gfx/webrender_bindings/RenderCompositorANGLE.cpp @@ -14,6 +14,7 @@ #include "mozilla/gfx/Logging.h" #include "mozilla/layers/HelpersD3D11.h" #include "mozilla/layers/SyncObject.h" +#include "mozilla/webrender/DCLayerTree.h" #include "mozilla/webrender/RenderThread.h" #include "mozilla/widget/CompositorWidget.h" #include "mozilla/widget/WinCompositorWidget.h" @@ -257,44 +258,19 @@ void RenderCompositorANGLE::CreateSwapChainForDCompIfPossible( return; } - RefPtr dCompDevice = - gfx::DeviceManagerDx::Get()->GetDirectCompositionDevice(); - if (!dCompDevice) { - return; - } - MOZ_ASSERT(XRE_IsGPUProcess()); - - RefPtr dxgiDevice; - mDevice->QueryInterface((IDXGIDevice**)getter_AddRefs(dxgiDevice)); - - RefPtr dxgiFactory; - { - RefPtr adapter; - dxgiDevice->GetAdapter(getter_AddRefs(adapter)); - adapter->GetParent( - IID_PPV_ARGS((IDXGIFactory**)getter_AddRefs(dxgiFactory))); - } - HWND hwnd = mWidget->AsWindows()->GetCompositorHwnd(); if (!hwnd) { gfxCriticalNote << "Compositor window was not created "; return; } - HRESULT hr = dCompDevice->CreateTargetForHwnd( - hwnd, TRUE, getter_AddRefs(mCompositionTarget)); - if (FAILED(hr)) { - gfxCriticalNote << "Could not create DCompositionTarget: " << gfx::hexa(hr); - return; - } - - hr = dCompDevice->CreateVisual(getter_AddRefs(mVisual)); - if (FAILED(hr)) { - gfxCriticalNote << "Could not create DCompositionVisualt: " - << gfx::hexa(hr); + mDCLayerTree = DCLayerTree::Create(hwnd); + if (!mDCLayerTree) { return; } + MOZ_ASSERT(XRE_IsGPUProcess()); + HRESULT hr; RefPtr swapChain1; bool useTripleBuffering = gfx::gfxVars::UseWebRenderTripleBufferingWin(); @@ -326,11 +302,11 @@ void RenderCompositorANGLE::CreateSwapChainForDCompIfPossible( DXGI_RGBA color = {1.0f, 1.0f, 1.0f, 1.0f}; swapChain1->SetBackgroundColor(&color); mSwapChain = swapChain1; - mVisual->SetContent(swapChain1); - mCompositionTarget->SetRoot(mVisual); - mCompositionDevice = dCompDevice; - mCompositionDevice->Commit(); + mDCLayerTree->SetDefaultSwapChain(swapChain1); mUseTripleBuffering = useTripleBuffering; + } else { + // Clear CLayerTree on falire + mDCLayerTree = nullptr; } } diff --git a/gfx/webrender_bindings/RenderCompositorANGLE.h b/gfx/webrender_bindings/RenderCompositorANGLE.h index 13d4ecb01b28..d5dd7ffc58db 100644 --- a/gfx/webrender_bindings/RenderCompositorANGLE.h +++ b/gfx/webrender_bindings/RenderCompositorANGLE.h @@ -17,9 +17,6 @@ struct ID3D11DeviceContext; struct ID3D11Device; struct ID3D11Query; -struct IDCompositionDevice; -struct IDCompositionTarget; -struct IDCompositionVisual; struct IDXGIFactory2; struct IDXGISwapChain; @@ -30,6 +27,8 @@ class GLLibraryEGL; namespace wr { +class DCLayerTree; + class RenderCompositorANGLE : public RenderCompositor { public: static UniquePtr Create( @@ -51,7 +50,7 @@ class RenderCompositorANGLE : public RenderCompositor { bool UseANGLE() const override { return true; } - bool UseDComp() const override { return !!mCompositionDevice; } + bool UseDComp() const override { return !!mDCLayerTree; } bool UseTripleBuffering() const override { return mUseTripleBuffering; } @@ -79,9 +78,7 @@ class RenderCompositorANGLE : public RenderCompositor { RefPtr mCtx; RefPtr mSwapChain; - RefPtr mCompositionDevice; - RefPtr mCompositionTarget; - RefPtr mVisual; + UniquePtr mDCLayerTree; std::queue> mWaitForPresentQueries; RefPtr mRecycledQuery; diff --git a/gfx/webrender_bindings/moz.build b/gfx/webrender_bindings/moz.build index aecc37af61da..64d22b7eaa8f 100644 --- a/gfx/webrender_bindings/moz.build +++ b/gfx/webrender_bindings/moz.build @@ -60,6 +60,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android': if CONFIG['MOZ_ENABLE_D3D10_LAYER']: DEFINES['MOZ_ENABLE_D3D10_LAYER'] = True EXPORTS.mozilla.webrender += [ + 'DCLayerTree.h', 'RenderCompositorANGLE.h', 'RenderD3D11TextureHostOGL.h', ] @@ -67,6 +68,7 @@ if CONFIG['MOZ_ENABLE_D3D10_LAYER']: 'RenderD3D11TextureHostOGL.cpp', ] SOURCES += [ + 'DCLayerTree.cpp', 'RenderCompositorANGLE.cpp', ]