|
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_COMPOSITORD3D9_H |
|
7 #define MOZILLA_GFX_COMPOSITORD3D9_H |
|
8 |
|
9 #include "mozilla/gfx/2D.h" |
|
10 #include "gfx2DGlue.h" |
|
11 #include "mozilla/layers/Compositor.h" |
|
12 #include "mozilla/layers/TextureD3D9.h" |
|
13 #include "DeviceManagerD3D9.h" |
|
14 |
|
15 class nsWidget; |
|
16 |
|
17 namespace mozilla { |
|
18 namespace layers { |
|
19 |
|
20 class CompositorD3D9 : public Compositor |
|
21 { |
|
22 public: |
|
23 CompositorD3D9(PCompositorParent* aParent, nsIWidget *aWidget); |
|
24 ~CompositorD3D9(); |
|
25 |
|
26 virtual bool Initialize() MOZ_OVERRIDE; |
|
27 virtual void Destroy() MOZ_OVERRIDE {} |
|
28 |
|
29 virtual TextureFactoryIdentifier |
|
30 GetTextureFactoryIdentifier() MOZ_OVERRIDE; |
|
31 |
|
32 virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize) MOZ_OVERRIDE; |
|
33 virtual int32_t GetMaxTextureSize() const MOZ_FINAL; |
|
34 |
|
35 virtual void SetTargetContext(gfx::DrawTarget *aTarget) MOZ_OVERRIDE |
|
36 { |
|
37 mTarget = aTarget; |
|
38 } |
|
39 |
|
40 virtual void MakeCurrent(MakeCurrentFlags aFlags = 0) MOZ_OVERRIDE {} |
|
41 |
|
42 virtual TemporaryRef<CompositingRenderTarget> |
|
43 CreateRenderTarget(const gfx::IntRect &aRect, |
|
44 SurfaceInitMode aInit) MOZ_OVERRIDE; |
|
45 |
|
46 virtual TemporaryRef<CompositingRenderTarget> |
|
47 CreateRenderTargetFromSource(const gfx::IntRect &aRect, |
|
48 const CompositingRenderTarget *aSource, |
|
49 const gfx::IntPoint &aSourcePoint) MOZ_OVERRIDE; |
|
50 |
|
51 virtual void SetRenderTarget(CompositingRenderTarget *aSurface); |
|
52 virtual CompositingRenderTarget* GetCurrentRenderTarget() const MOZ_OVERRIDE |
|
53 { |
|
54 return mCurrentRT; |
|
55 } |
|
56 |
|
57 virtual void SetDestinationSurfaceSize(const gfx::IntSize& aSize) MOZ_OVERRIDE {} |
|
58 |
|
59 virtual void ClearRect(const gfx::Rect& aRect) MOZ_OVERRIDE; |
|
60 |
|
61 virtual void DrawQuad(const gfx::Rect &aRect, |
|
62 const gfx::Rect &aClipRect, |
|
63 const EffectChain &aEffectChain, |
|
64 gfx::Float aOpacity, |
|
65 const gfx::Matrix4x4 &aTransform) MOZ_OVERRIDE; |
|
66 |
|
67 virtual void BeginFrame(const nsIntRegion& aInvalidRegion, |
|
68 const gfx::Rect *aClipRectIn, |
|
69 const gfx::Matrix& aTransform, |
|
70 const gfx::Rect& aRenderBounds, |
|
71 gfx::Rect *aClipRectOut = nullptr, |
|
72 gfx::Rect *aRenderBoundsOut = nullptr) MOZ_OVERRIDE; |
|
73 |
|
74 virtual void EndFrame() MOZ_OVERRIDE; |
|
75 |
|
76 virtual void EndFrameForExternalComposition(const gfx::Matrix& aTransform) MOZ_OVERRIDE {} |
|
77 |
|
78 virtual void AbortFrame() MOZ_OVERRIDE {} |
|
79 |
|
80 virtual void PrepareViewport(const gfx::IntSize& aSize, |
|
81 const gfx::Matrix& aWorldTransform) MOZ_OVERRIDE; |
|
82 |
|
83 virtual bool SupportsPartialTextureUpdate() MOZ_OVERRIDE{ return true; } |
|
84 |
|
85 #ifdef MOZ_DUMP_PAINTING |
|
86 virtual const char* Name() const MOZ_OVERRIDE { return "Direct3D9"; } |
|
87 #endif |
|
88 |
|
89 virtual LayersBackend GetBackendType() const MOZ_OVERRIDE { |
|
90 return LayersBackend::LAYERS_D3D9; |
|
91 } |
|
92 |
|
93 virtual nsIWidget* GetWidget() const MOZ_OVERRIDE { return mWidget; } |
|
94 |
|
95 IDirect3DDevice9* device() const |
|
96 { |
|
97 return mDeviceManager |
|
98 ? mDeviceManager->device() |
|
99 : nullptr; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Returns true if the Compositor is ready to go. |
|
104 * D3D9 devices can be awkward and there is a bunch of logic around |
|
105 * resetting/recreating devices and swap chains. That is handled by this method. |
|
106 * If we don't have a device and swap chain ready for rendering, we will return |
|
107 * false and if necessary destroy the device and/or swap chain. We will also |
|
108 * schedule another composite so we get another go at rendering, thus we shouldn't |
|
109 * miss a composite due to re-creating a device. |
|
110 */ |
|
111 virtual bool Ready() MOZ_OVERRIDE; |
|
112 |
|
113 /** |
|
114 * Declare an offset to use when rendering layers. This will be ignored when |
|
115 * rendering to a target instead of the screen. |
|
116 */ |
|
117 virtual void SetScreenRenderOffset(const ScreenPoint& aOffset) MOZ_OVERRIDE |
|
118 { |
|
119 if (aOffset.x || aOffset.y) { |
|
120 NS_RUNTIMEABORT("SetScreenRenderOffset not supported by CompositorD3D9."); |
|
121 } |
|
122 // If the offset is 0, 0 that's okay. |
|
123 } |
|
124 |
|
125 virtual TemporaryRef<DataTextureSource> |
|
126 CreateDataTextureSource(TextureFlags aFlags = 0) MOZ_OVERRIDE; |
|
127 private: |
|
128 // ensure mSize is up to date with respect to mWidget |
|
129 void EnsureSize(); |
|
130 void SetSamplerForFilter(gfx::Filter aFilter); |
|
131 void PaintToTarget(); |
|
132 void SetMask(const EffectChain &aEffectChain, uint32_t aMaskTexture); |
|
133 /** |
|
134 * Ensure we have a swap chain and it is ready for rendering. |
|
135 * Requires mDeviceManger to be non-null. |
|
136 * Returns true if we have a working swap chain; false otherwise. |
|
137 * If we cannot create or validate the swap chain due to a bad device manager, |
|
138 * then the device will be destroyed and set mDeviceManager to null. We will |
|
139 * schedule another composite if it is a good idea to try again or we need to |
|
140 * recreate the device. |
|
141 */ |
|
142 bool EnsureSwapChain(); |
|
143 |
|
144 /** |
|
145 * DeviceManagerD3D9 keeps a count of the number of times its device is |
|
146 * reset or recreated. We keep a parallel count (mDeviceResetCount). It |
|
147 * is possible that we miss a reset if it is 'caused' by another |
|
148 * compositor (for another window). In which case we need to invalidate |
|
149 * everything and render it all. This method checks the reset counts |
|
150 * match and if not invalidates everything (a long comment on that in |
|
151 * the cpp file). |
|
152 */ |
|
153 void CheckResetCount(); |
|
154 |
|
155 void ReportFailure(const nsACString &aMsg, HRESULT aCode); |
|
156 |
|
157 virtual gfx::IntSize GetWidgetSize() const MOZ_OVERRIDE |
|
158 { |
|
159 return gfx::ToIntSize(mSize); |
|
160 } |
|
161 |
|
162 /* Device manager instance for this compositor */ |
|
163 nsRefPtr<DeviceManagerD3D9> mDeviceManager; |
|
164 |
|
165 /* Swap chain associated with this compositor */ |
|
166 nsRefPtr<SwapChainD3D9> mSwapChain; |
|
167 |
|
168 /* Widget associated with this layer manager */ |
|
169 nsIWidget *mWidget; |
|
170 |
|
171 /* |
|
172 * Context target, nullptr when drawing directly to our swap chain. |
|
173 */ |
|
174 RefPtr<gfx::DrawTarget> mTarget; |
|
175 |
|
176 RefPtr<CompositingRenderTargetD3D9> mDefaultRT; |
|
177 RefPtr<CompositingRenderTargetD3D9> mCurrentRT; |
|
178 |
|
179 nsIntSize mSize; |
|
180 |
|
181 uint32_t mDeviceResetCount; |
|
182 }; |
|
183 |
|
184 } |
|
185 } |
|
186 |
|
187 #endif |