CompositorD3D11.h (7127B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_GFX_COMPOSITORD3D11_H 8 #define MOZILLA_GFX_COMPOSITORD3D11_H 9 10 #include "mozilla/gfx/2D.h" 11 #include "gfx2DGlue.h" 12 #include "mozilla/layers/Compositor.h" 13 #include "TextureD3D11.h" 14 #include <d3d11.h> 15 #include <dxgi1_2.h> 16 #include "ShaderDefinitionsD3D11.h" 17 18 class nsWidget; 19 20 namespace mozilla { 21 namespace layers { 22 23 #define LOGD3D11(param) 24 25 class DeviceAttachmentsD3D11; 26 27 class CompositorD3D11 : public Compositor { 28 public: 29 explicit CompositorD3D11(widget::CompositorWidget* aWidget); 30 virtual ~CompositorD3D11(); 31 32 CompositorD3D11* AsCompositorD3D11() override { return this; } 33 34 bool Initialize(nsCString* const out_failureReason) override; 35 36 already_AddRefed<DataTextureSource> CreateDataTextureSource( 37 TextureFlags aFlags = TextureFlags::NO_FLAGS) override; 38 39 int32_t GetMaxTextureSize() const final; 40 41 already_AddRefed<CompositingRenderTarget> CreateRenderTarget( 42 const gfx::IntRect& aRect, SurfaceInitMode aInit) override; 43 44 void SetRenderTarget(CompositingRenderTarget* aSurface) override; 45 already_AddRefed<CompositingRenderTarget> GetCurrentRenderTarget() 46 const override { 47 return do_AddRef(mCurrentRT); 48 } 49 already_AddRefed<CompositingRenderTarget> GetWindowRenderTarget() 50 const override; 51 52 bool ReadbackRenderTarget(CompositingRenderTarget* aSource, 53 AsyncReadbackBuffer* aDest) override; 54 already_AddRefed<AsyncReadbackBuffer> CreateAsyncReadbackBuffer( 55 const gfx::IntSize& aSize) override; 56 57 bool BlitRenderTarget(CompositingRenderTarget* aSource, 58 const gfx::IntSize& aSourceSize, 59 const gfx::IntSize& aDestSize) override; 60 61 void SetDestinationSurfaceSize(const gfx::IntSize& aSize) override {} 62 63 void ClearRect(const gfx::Rect& aRect); 64 65 void DrawQuad(const gfx::Rect& aRect, const gfx::IntRect& aClipRect, 66 const EffectChain& aEffectChain, gfx::Float aOpacity, 67 const gfx::Matrix4x4& aTransform, 68 const gfx::Rect& aVisibleRect) override; 69 70 /** 71 * Start a new frame. 72 */ 73 Maybe<gfx::IntRect> BeginFrameForWindow( 74 const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect, 75 const gfx::IntRect& aRenderBounds, 76 const nsIntRegion& aOpaqueRegion) override; 77 78 /** 79 * Flush the current frame to the screen. 80 */ 81 void EndFrame() override; 82 83 void CancelFrame(bool aNeedFlush = true) override; 84 85 /** 86 * Setup the viewport and projection matrix for rendering 87 * to a window of the given dimensions. 88 */ 89 virtual void PrepareViewport(const gfx::IntSize& aSize); 90 virtual void PrepareViewport(const gfx::IntSize& aSize, 91 const gfx::Matrix4x4& aProjection, float aZNear, 92 float aZFar); 93 94 #ifdef MOZ_DUMP_PAINTING 95 const char* Name() const override { return "Direct3D 11"; } 96 #endif 97 98 // For TextureSourceProvider. 99 ID3D11Device* GetD3D11Device() const override { return mDevice; } 100 101 ID3D11Device* GetDevice() { return mDevice; } 102 103 ID3D11DeviceContext* GetDC() { return mContext; } 104 105 virtual void RequestAllowFrameRecording(bool aWillRecord) override { 106 mAllowFrameRecording = aWillRecord; 107 } 108 109 void Readback(gfx::DrawTarget* aDrawTarget) { 110 mTarget = aDrawTarget; 111 mTargetBounds = gfx::IntRect(); 112 PaintToTarget(); 113 mTarget = nullptr; 114 } 115 116 SyncObjectHost* GetSyncObject(); 117 118 private: 119 enum Severity { 120 Recoverable, 121 DebugAssert, 122 Critical, 123 }; 124 125 void HandleError(HRESULT hr, Severity aSeverity = DebugAssert); 126 127 // Same as Failed(), except the severity is critical (with no abort) and 128 // a string prefix must be provided. 129 bool Failed(HRESULT hr, const char* aContext); 130 131 // ensure mSize is up to date with respect to mWidget 132 void EnsureSize(); 133 bool VerifyBufferSize(); 134 bool UpdateRenderTarget(); 135 bool UpdateConstantBuffers(); 136 void SetSamplerForSamplingFilter(gfx::SamplingFilter aSamplingFilter); 137 138 ID3D11PixelShader* GetPSForEffect(Effect* aEffect, const ClipType aClipType); 139 Maybe<gfx::IntRect> BeginFrame(const nsIntRegion& aInvalidRegion, 140 const Maybe<gfx::IntRect>& aClipRect, 141 const gfx::IntRect& aRenderBounds, 142 const nsIntRegion& aOpaqueRegion); 143 void PaintToTarget(); 144 RefPtr<ID3D11Texture2D> CreateTexture(const gfx::IntRect& aRect, 145 const CompositingRenderTarget* aSource, 146 const gfx::IntPoint& aSourcePoint); 147 148 void PrepareStaticVertexBuffer(); 149 150 void Draw(const gfx::Rect& aGeometry, const gfx::Rect* aTexCoords); 151 152 void Present(); 153 154 template <typename VertexType> 155 void SetVertexBuffer(ID3D11Buffer* aBuffer); 156 157 /** 158 * Whether or not the recorder should be recording frames. 159 * 160 * When this returns true, the CompositorD3D11 will allocate and return window 161 * render targets from |GetWindowRenderTarget|, which otherwise returns 162 * nullptr. 163 * 164 * This will be true when either we are recording a profile with screenshots 165 * enabled or the |LayerManagerComposite| has requested us to record frames 166 * for the |CompositionRecorder|. 167 */ 168 bool ShouldAllowFrameRecording() const; 169 170 // The DrawTarget from BeginFrameForTarget, which EndFrame needs to copy the 171 // window contents into. 172 // Only non-null between BeginFrameForTarget and EndFrame. 173 RefPtr<gfx::DrawTarget> mTarget; 174 gfx::IntRect mTargetBounds; 175 176 RefPtr<ID3D11DeviceContext> mContext; 177 RefPtr<ID3D11Device> mDevice; 178 RefPtr<IDXGISwapChain> mSwapChain; 179 RefPtr<CompositingRenderTargetD3D11> mDefaultRT; 180 RefPtr<CompositingRenderTargetD3D11> mCurrentRT; 181 mutable RefPtr<CompositingRenderTargetD3D11> mWindowRTCopy; 182 183 RefPtr<ID3D11Query> mQuery; 184 RefPtr<ID3D11Query> mRecycledQuery; 185 186 RefPtr<DeviceAttachmentsD3D11> mAttachments; 187 188 LayoutDeviceIntSize mSize; 189 190 // The size that we passed to ResizeBuffers to set 191 // the swapchain buffer size. 192 LayoutDeviceIntSize mBufferSize; 193 194 HWND mHwnd; 195 196 D3D_FEATURE_LEVEL mFeatureLevel; 197 198 VertexShaderConstants mVSConstants; 199 PixelShaderConstants mPSConstants; 200 bool mDisableSequenceForNextFrame; 201 bool mAllowPartialPresents; 202 bool mIsDoubleBuffered; 203 204 gfx::IntRegion mFrontBufferInvalid; 205 gfx::IntRegion mBackBufferInvalid; 206 // This is the clip rect applied to the default DrawTarget (i.e. the window) 207 gfx::IntRect mCurrentClip; 208 209 bool mVerifyBuffersFailed; 210 bool mUseMutexOnPresent; 211 bool mAllowFrameRecording; 212 }; 213 214 namespace TexSlot { 215 static const int RGB = 0; 216 static const int Y = 1; 217 static const int Cb = 2; 218 static const int Cr = 3; 219 static const int RGBWhite = 4; 220 static const int Mask = 5; 221 static const int Backdrop = 6; 222 } // namespace TexSlot 223 224 } // namespace layers 225 } // namespace mozilla 226 227 #endif