From d31c311fdaaa205acfbd66844a3c6ee194784921 Mon Sep 17 00:00:00 2001 From: Barret Rennie Date: Tue, 19 Feb 2019 14:54:12 +0000 Subject: [PATCH] Bug 1444449 - Implement CompositorD3D11::GetWindowRenderTarget API r=mstange,bas Differential Revision: https://phabricator.services.mozilla.com/D18145 --HG-- extra : moz-landing-system : lando --- gfx/layers/d3d11/CompositorD3D11.cpp | 55 ++++++++++++++++++++++++++++ gfx/layers/d3d11/CompositorD3D11.h | 3 ++ 2 files changed, 58 insertions(+) diff --git a/gfx/layers/d3d11/CompositorD3D11.cpp b/gfx/layers/d3d11/CompositorD3D11.cpp index bd00ca1a7abe..f30e45b858f5 100644 --- a/gfx/layers/d3d11/CompositorD3D11.cpp +++ b/gfx/layers/d3d11/CompositorD3D11.cpp @@ -50,6 +50,7 @@ const FLOAT sBlendFactor[] = {0, 0, 0, 0}; CompositorD3D11::CompositorD3D11(CompositorBridgeParent* aParent, widget::CompositorWidget* aWidget) : Compositor(aWidget, aParent), + mWindowRTCopy(nullptr), mAttachments(nullptr), mHwnd(nullptr), mDisableSequenceForNextFrame(false), @@ -397,6 +398,55 @@ CompositorD3D11::CreateRenderTargetFromSource( return rt.forget(); } +already_AddRefed +CompositorD3D11::GetWindowRenderTarget() const { +#ifndef MOZ_GECKO_PROFILER + return nullptr; +#else + if (!profiler_feature_active(ProfilerFeature::Screenshots)) { + return nullptr; + } + + if (!mDefaultRT) { + return nullptr; + } + + const IntSize size = mDefaultRT->GetSize(); + + RefPtr rtTexture; + + if (!mWindowRTCopy || mWindowRTCopy->GetSize() != size) { + /* + * The compositor screenshots infrastructure is going to scale down the + * render target returned by this method. However, mDefaultRT does not + * contain a texture created wth the D3D11_BIND_SHADER_RESOURCE flag, so if + * we were to simply return mDefaultRT then scaling would fail. + */ + CD3D11_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM, size.width, + size.height, 1, 1, D3D11_BIND_SHADER_RESOURCE); + + HRESULT hr = + mDevice->CreateTexture2D(&desc, nullptr, getter_AddRefs(rtTexture)); + if (FAILED(hr)) { + return nullptr; + } + + mWindowRTCopy = MakeRefPtr( + rtTexture, IntPoint(0, 0), DXGI_FORMAT_B8G8R8A8_UNORM); + mWindowRTCopy->SetSize(size); + } else { + rtTexture = mWindowRTCopy->GetD3D11Texture(); + } + + const RefPtr sourceTexture = mDefaultRT->GetD3D11Texture(); + mContext->CopyResource(rtTexture, sourceTexture); + + return RefPtr( + static_cast(mWindowRTCopy)) + .forget(); +#endif +} + bool CompositorD3D11::CopyBackdrop(const gfx::IntRect& aRect, RefPtr* aOutTexture, RefPtr* aOutView) { @@ -1050,6 +1100,10 @@ void CompositorD3D11::BeginFrame(const nsIntRegion& aInvalidRegion, void CompositorD3D11::NormalDrawingDone() { mDiagnostics->End(); } void CompositorD3D11::EndFrame() { + if (!profiler_feature_active(ProfilerFeature::Screenshots) && mWindowRTCopy) { + mWindowRTCopy = nullptr; + } + if (!mDefaultRT) { Compositor::EndFrame(); return; @@ -1281,6 +1335,7 @@ bool CompositorD3D11::VerifyBufferSize() { if (mCurrentRT == mDefaultRT) { mCurrentRT = nullptr; } + MOZ_ASSERT(mDefaultRT->hasOneRef()); mDefaultRT = nullptr; diff --git a/gfx/layers/d3d11/CompositorD3D11.h b/gfx/layers/d3d11/CompositorD3D11.h index 5e78c0158e2c..6ac4865561fe 100644 --- a/gfx/layers/d3d11/CompositorD3D11.h +++ b/gfx/layers/d3d11/CompositorD3D11.h @@ -58,6 +58,8 @@ class CompositorD3D11 : public Compositor { const override { return do_AddRef(mCurrentRT); } + virtual already_AddRefed GetWindowRenderTarget() + const override; virtual void SetDestinationSurfaceSize(const gfx::IntSize& aSize) override {} @@ -206,6 +208,7 @@ class CompositorD3D11 : public Compositor { RefPtr mSwapChain; RefPtr mDefaultRT; RefPtr mCurrentRT; + mutable RefPtr mWindowRTCopy; RefPtr mQuery;