1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* -*- 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_COMPOSITORD3D11_H
#define MOZILLA_GFX_COMPOSITORD3D11_H
#include "mozilla/gfx/2D.h"
#include "gfx2DGlue.h"
#include "mozilla/layers/Compositor.h"
#include "TextureD3D11.h"
#include <d3d11.h>
#include <dxgi1_2.h>
#include "ShaderDefinitionsD3D11.h"
class nsWidget;
namespace mozilla {
namespace layers {
#define LOGD3D11(param)
class DeviceAttachmentsD3D11;
class DiagnosticsD3D11;
class CompositorD3D11 : public Compositor {
public:
CompositorD3D11(CompositorBridgeParent* aParent,
widget::CompositorWidget* aWidget);
virtual ~CompositorD3D11();
CompositorD3D11* AsCompositorD3D11() override { return this; }
bool Initialize(nsCString* const out_failureReason) override;
TextureFactoryIdentifier GetTextureFactoryIdentifier() override;
already_AddRefed<DataTextureSource> CreateDataTextureSource(
TextureFlags aFlags = TextureFlags::NO_FLAGS) override;
bool CanUseCanvasLayerForSize(const gfx::IntSize& aSize) override;
int32_t GetMaxTextureSize() const final;
void MakeCurrent(MakeCurrentFlags aFlags = 0) override {}
already_AddRefed<CompositingRenderTarget> CreateRenderTarget(
const gfx::IntRect& aRect, SurfaceInitMode aInit) override;
already_AddRefed<CompositingRenderTarget> CreateRenderTargetFromSource(
const gfx::IntRect& aRect, const CompositingRenderTarget* aSource,
const gfx::IntPoint& aSourcePoint) override;
void SetRenderTarget(CompositingRenderTarget* aSurface) override;
already_AddRefed<CompositingRenderTarget> GetCurrentRenderTarget()
const override {
return do_AddRef(mCurrentRT);
}
already_AddRefed<CompositingRenderTarget> GetWindowRenderTarget()
const override;
bool ReadbackRenderTarget(CompositingRenderTarget* aSource,
AsyncReadbackBuffer* aDest) override;
already_AddRefed<AsyncReadbackBuffer> CreateAsyncReadbackBuffer(
const gfx::IntSize& aSize) override;
bool BlitRenderTarget(CompositingRenderTarget* aSource,
const gfx::IntSize& aSourceSize,
const gfx::IntSize& aDestSize) override;
void SetDestinationSurfaceSize(const gfx::IntSize& aSize) override {}
void ClearRect(const gfx::Rect& aRect) override;
void DrawQuad(const gfx::Rect& aRect, const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect) override;
/**
* Start a new frame.
*/
Maybe<gfx::IntRect> BeginFrameForWindow(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds,
const nsIntRegion& aOpaqueRegion) override;
Maybe<gfx::IntRect> BeginFrameForTarget(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds, const nsIntRegion& aOpaqueRegion,
gfx::DrawTarget* aTarget, const gfx::IntRect& aTargetBounds) override;
void BeginFrameForNativeLayers() override;
Maybe<gfx::IntRect> BeginRenderingToNativeLayer(
const nsIntRegion& aInvalidRegion, const Maybe<gfx::IntRect>& aClipRect,
const nsIntRegion& aOpaqueRegion, NativeLayer* aNativeLayer) override;
void EndRenderingToNativeLayer() override;
void NormalDrawingDone() override;
/**
* Flush the current frame to the screen.
*/
void EndFrame() override;
void CancelFrame(bool aNeedFlush = true) override;
/**
* Setup the viewport and projection matrix for rendering
* to a window of the given dimensions.
*/
virtual void PrepareViewport(const gfx::IntSize& aSize);
virtual void PrepareViewport(const gfx::IntSize& aSize,
const gfx::Matrix4x4& aProjection, float aZNear,
float aZFar);
bool SupportsPartialTextureUpdate() override { return true; }
bool SupportsLayerGeometry() const override;
#ifdef MOZ_DUMP_PAINTING
const char* Name() const override { return "Direct3D 11"; }
#endif
LayersBackend GetBackendType() const override {
return LayersBackend::LAYERS_D3D11;
}
virtual void ForcePresent();
// For TextureSourceProvider.
ID3D11Device* GetD3D11Device() const override { return mDevice; }
ID3D11Device* GetDevice() { return mDevice; }
ID3D11DeviceContext* GetDC() { return mContext; }
virtual void RequestAllowFrameRecording(bool aWillRecord) override {
mAllowFrameRecording = aWillRecord;
}
void Readback(gfx::DrawTarget* aDrawTarget) {
mTarget = aDrawTarget;
mTargetBounds = gfx::IntRect();
PaintToTarget();
mTarget = nullptr;
}
SyncObjectHost* GetSyncObject();
void UseForSoftwareWebRender() { mUseForSoftwareWebRender = true; }
private:
enum Severity {
Recoverable,
DebugAssert,
Critical,
};
void HandleError(HRESULT hr, Severity aSeverity = DebugAssert);
// Same as Failed(), except the severity is critical (with no abort) and
// a string prefix must be provided.
bool Failed(HRESULT hr, const char* aContext);
// ensure mSize is up to date with respect to mWidget
void EnsureSize();
bool VerifyBufferSize();
bool UpdateRenderTarget();
bool UpdateConstantBuffers();
void SetSamplerForSamplingFilter(gfx::SamplingFilter aSamplingFilter);
ID3D11PixelShader* GetPSForEffect(Effect* aEffect, const bool aUseBlendShader,
const MaskType aMaskType);
Maybe<gfx::IntRect> BeginFrame(const nsIntRegion& aInvalidRegion,
const Maybe<gfx::IntRect>& aClipRect,
const gfx::IntRect& aRenderBounds,
const nsIntRegion& aOpaqueRegion);
void PaintToTarget();
RefPtr<ID3D11Texture2D> CreateTexture(const gfx::IntRect& aRect,
const CompositingRenderTarget* aSource,
const gfx::IntPoint& aSourcePoint);
bool CopyBackdrop(const gfx::IntRect& aRect,
RefPtr<ID3D11Texture2D>* aOutTexture,
RefPtr<ID3D11ShaderResourceView>* aOutView);
void DrawTriangles(const nsTArray<gfx::TexturedTriangle>& aTriangles,
const gfx::Rect& aRect, const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect) override;
template <typename Geometry>
void DrawGeometry(const Geometry& aGeometry, const gfx::Rect& aRect,
const gfx::IntRect& aClipRect,
const EffectChain& aEffectChain, gfx::Float aOpacity,
const gfx::Matrix4x4& aTransform,
const gfx::Rect& aVisibleRect);
bool UpdateDynamicVertexBuffer(
const nsTArray<gfx::TexturedTriangle>& aTriangles);
void PrepareDynamicVertexBuffer();
void PrepareStaticVertexBuffer();
// Overloads for rendering both rects and triangles with same rendering path
void Draw(const nsTArray<gfx::TexturedTriangle>& aGeometry,
const gfx::Rect* aTexCoords);
void Draw(const gfx::Rect& aGeometry, const gfx::Rect* aTexCoords);
void GetFrameStats(GPUStats* aStats) override;
void Present();
ID3D11VertexShader* GetVSForGeometry(
const nsTArray<gfx::TexturedTriangle>& aTriangles,
const bool aUseBlendShader, const MaskType aMaskType);
ID3D11VertexShader* GetVSForGeometry(const gfx::Rect& aRect,
const bool aUseBlendShader,
const MaskType aMaskType);
template <typename VertexType>
void SetVertexBuffer(ID3D11Buffer* aBuffer);
/**
* Whether or not the recorder should be recording frames.
*
* When this returns true, the CompositorD3D11 will allocate and return window
* render targets from |GetWindowRenderTarget|, which otherwise returns
* nullptr.
*
* This will be true when either we are recording a profile with screenshots
* enabled or the |LayerManagerComposite| has requested us to record frames
* for the |CompositionRecorder|.
*/
bool ShouldAllowFrameRecording() const;
// The DrawTarget from BeginFrameForTarget, which EndFrame needs to copy the
// window contents into.
// Only non-null between BeginFrameForTarget and EndFrame.
RefPtr<gfx::DrawTarget> mTarget;
gfx::IntRect mTargetBounds;
RefPtr<ID3D11DeviceContext> mContext;
RefPtr<ID3D11Device> mDevice;
RefPtr<IDXGISwapChain> mSwapChain;
RefPtr<CompositingRenderTargetD3D11> mDefaultRT;
RefPtr<CompositingRenderTargetD3D11> mCurrentRT;
mutable RefPtr<CompositingRenderTargetD3D11> mWindowRTCopy;
RefPtr<ID3D11Query> mQuery;
RefPtr<ID3D11Query> mRecycledQuery;
RefPtr<DeviceAttachmentsD3D11> mAttachments;
UniquePtr<DiagnosticsD3D11> mDiagnostics;
LayoutDeviceIntSize mSize;
// The size that we passed to ResizeBuffers to set
// the swapchain buffer size.
LayoutDeviceIntSize mBufferSize;
HWND mHwnd;
D3D_FEATURE_LEVEL mFeatureLevel;
VertexShaderConstants mVSConstants;
PixelShaderConstants mPSConstants;
bool mDisableSequenceForNextFrame;
bool mAllowPartialPresents;
bool mIsDoubleBuffered;
gfx::IntRegion mFrontBufferInvalid;
gfx::IntRegion mBackBufferInvalid;
// This is the clip rect applied to the default DrawTarget (i.e. the window)
gfx::IntRect mCurrentClip;
bool mVerifyBuffersFailed;
bool mUseMutexOnPresent;
bool mAllowFrameRecording;
bool mUseForSoftwareWebRender;
};
namespace TexSlot {
static const int RGB = 0;
static const int Y = 1;
static const int Cb = 2;
static const int Cr = 3;
static const int RGBWhite = 4;
static const int Mask = 5;
static const int Backdrop = 6;
} // namespace TexSlot
} // namespace layers
} // namespace mozilla
#endif
|