1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef MOZILLA_GFX_COMPOSITORD3D11_H
7 #define MOZILLA_GFX_COMPOSITORD3D11_H
8 
9 #include "mozilla/gfx/2D.h"
10 #include "gfx2DGlue.h"
11 #include "mozilla/layers/Compositor.h"
12 #include "TextureD3D11.h"
13 #include <d3d11.h>
14 
15 class nsWidget;
16 
17 namespace mozilla {
18 namespace layers {
19 
20 #define LOGD3D11(param)
21 
22 struct VertexShaderConstants
23 {
24   float layerTransform[4][4];
25   float projection[4][4];
26   float renderTargetOffset[4];
27   gfx::Rect textureCoords;
28   gfx::Rect layerQuad;
29   gfx::Rect maskQuad;
30   float backdropTransform[4][4];
31 };
32 
33 struct PixelShaderConstants
34 {
35   float layerColor[4];
36   float layerOpacity[4];
37   int blendConfig[4];
38   float yuvColorMatrix[3][4];
39 };
40 
41 struct DeviceAttachmentsD3D11;
42 
43 class CompositorD3D11 : public Compositor
44 {
45 public:
46   CompositorD3D11(CompositorBridgeParent* aParent, widget::CompositorWidget* aWidget);
47   ~CompositorD3D11();
48 
AsCompositorD3D11()49   virtual CompositorD3D11* AsCompositorD3D11() override { return this; }
50 
51   virtual bool Initialize(nsCString* const out_failureReason) override;
52 
53   virtual TextureFactoryIdentifier
54     GetTextureFactoryIdentifier() override;
55 
56   virtual already_AddRefed<DataTextureSource>
57     CreateDataTextureSource(TextureFlags aFlags = TextureFlags::NO_FLAGS) override;
58 
59   virtual bool CanUseCanvasLayerForSize(const gfx::IntSize& aSize) override;
60   virtual int32_t GetMaxTextureSize() const final;
61 
62   virtual void MakeCurrent(MakeCurrentFlags aFlags = 0)  override {}
63 
64   virtual already_AddRefed<CompositingRenderTarget>
65     CreateRenderTarget(const gfx::IntRect &aRect,
66                        SurfaceInitMode aInit) override;
67 
68   virtual already_AddRefed<CompositingRenderTarget>
69     CreateRenderTargetFromSource(const gfx::IntRect& aRect,
70                                  const CompositingRenderTarget* aSource,
71                                  const gfx::IntPoint& aSourcePoint) override;
72 
73   virtual void SetRenderTarget(CompositingRenderTarget* aSurface) override;
GetCurrentRenderTarget()74   virtual CompositingRenderTarget* GetCurrentRenderTarget() const override
75   {
76     return mCurrentRT;
77   }
78 
SetDestinationSurfaceSize(const gfx::IntSize & aSize)79   virtual void SetDestinationSurfaceSize(const gfx::IntSize& aSize) override {}
80 
81   /**
82    * Declare an offset to use when rendering layers. This will be ignored when
83    * rendering to a target instead of the screen.
84    */
SetScreenRenderOffset(const ScreenPoint & aOffset)85   virtual void SetScreenRenderOffset(const ScreenPoint& aOffset) override
86   {
87     if (aOffset.x || aOffset.y) {
88       NS_RUNTIMEABORT("SetScreenRenderOffset not supported by CompositorD3D11.");
89     }
90     // If the offset is 0, 0 that's okay.
91   }
92 
93   virtual void ClearRect(const gfx::Rect& aRect) override;
94 
95   virtual void DrawQuad(const gfx::Rect &aRect,
96                         const gfx::IntRect &aClipRect,
97                         const EffectChain &aEffectChain,
98                         gfx::Float aOpacity,
99                         const gfx::Matrix4x4& aTransform,
100                         const gfx::Rect& aVisibleRect) override;
101 
102   /**
103    * Start a new frame. If aClipRectIn is null, sets *aClipRectOut to the
104    * screen dimensions.
105    */
106   virtual void BeginFrame(const nsIntRegion& aInvalidRegion,
107                           const gfx::IntRect *aClipRectIn,
108                           const gfx::IntRect& aRenderBounds,
109                           const nsIntRegion& aOpaqueRegion,
110                           gfx::IntRect *aClipRectOut = nullptr,
111                           gfx::IntRect *aRenderBoundsOut = nullptr) override;
112 
113   /**
114    * Flush the current frame to the screen.
115    */
116   virtual void EndFrame() override;
117 
118   /**
119    * Post rendering stuff if the rendering is outside of this Compositor
120    * e.g., by Composer2D
121    */
EndFrameForExternalComposition(const gfx::Matrix & aTransform)122   virtual void EndFrameForExternalComposition(const gfx::Matrix& aTransform) override {}
123 
124   /**
125    * Setup the viewport and projection matrix for rendering
126    * to a window of the given dimensions.
127    */
128   virtual void PrepareViewport(const gfx::IntSize& aSize);
129   virtual void PrepareViewport(const gfx::IntSize& aSize, const gfx::Matrix4x4& aProjection,
130                                float aZNear, float aZFar);
131 
SupportsPartialTextureUpdate()132   virtual bool SupportsPartialTextureUpdate() override { return true; }
133 
134 #ifdef MOZ_DUMP_PAINTING
Name()135   virtual const char* Name() const override { return "Direct3D 11"; }
136 #endif
137 
GetBackendType()138   virtual LayersBackend GetBackendType() const override {
139     return LayersBackend::LAYERS_D3D11;
140   }
141 
142   virtual void ForcePresent();
143 
GetDevice()144   ID3D11Device* GetDevice() { return mDevice; }
145 
GetDC()146   ID3D11DeviceContext* GetDC() { return mContext; }
147 
148 private:
149   enum Severity {
150     Recoverable,
151     DebugAssert,
152     Critical,
153   };
154 
155   void HandleError(HRESULT hr, Severity aSeverity = DebugAssert);
156 
157   // Same as Failed(), except the severity is critical (with no abort) and
158   // a string prefix must be provided.
159   bool Failed(HRESULT hr, const char* aContext);
160 
161   // ensure mSize is up to date with respect to mWidget
162   void EnsureSize();
163   bool VerifyBufferSize();
164   bool UpdateRenderTarget();
165   bool UpdateConstantBuffers();
166   void SetSamplerForSamplingFilter(gfx::SamplingFilter aSamplingFilter);
167   ID3D11PixelShader* GetPSForEffect(Effect *aEffect, MaskType aMaskType);
168   void PaintToTarget();
169   RefPtr<ID3D11Texture2D> CreateTexture(const gfx::IntRect& aRect,
170                                         const CompositingRenderTarget* aSource,
171                                         const gfx::IntPoint& aSourcePoint);
172   bool CopyBackdrop(const gfx::IntRect& aRect,
173                     RefPtr<ID3D11Texture2D>* aOutTexture,
174                     RefPtr<ID3D11ShaderResourceView>* aOutView);
175 
176   RefPtr<ID3D11DeviceContext> mContext;
177   RefPtr<ID3D11Device> mDevice;
178   RefPtr<IDXGISwapChain> mSwapChain;
179   RefPtr<CompositingRenderTargetD3D11> mDefaultRT;
180   RefPtr<CompositingRenderTargetD3D11> mCurrentRT;
181 
182   RefPtr<ID3D11Query> mQuery;
183 
184   DeviceAttachmentsD3D11* mAttachments;
185 
186   LayoutDeviceIntSize mSize;
187 
188   HWND mHwnd;
189 
190   D3D_FEATURE_LEVEL mFeatureLevel;
191 
192   VertexShaderConstants mVSConstants;
193   PixelShaderConstants mPSConstants;
194   bool mDisableSequenceForNextFrame;
195   bool mAllowPartialPresents;
196 
197   gfx::IntRect mInvalidRect;
198   // This is the clip rect applied to the default DrawTarget (i.e. the window)
199   gfx::IntRect mCurrentClip;
200   nsIntRegion mInvalidRegion;
201 
202   bool mVerifyBuffersFailed;
203 };
204 
205 }
206 }
207 
208 #endif
209