/src/mozilla-central/widget/CompositorWidget.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #ifndef mozilla_widget_CompositorWidget_h__ |
6 | | #define mozilla_widget_CompositorWidget_h__ |
7 | | |
8 | | #include "nsISupports.h" |
9 | | #include "mozilla/RefPtr.h" |
10 | | #include "Units.h" |
11 | | #include "mozilla/gfx/2D.h" |
12 | | #include "mozilla/layers/CompositorOptions.h" |
13 | | #include "mozilla/layers/LayersTypes.h" |
14 | | |
15 | | class nsIWidget; |
16 | | class nsBaseWidget; |
17 | | |
18 | | namespace mozilla { |
19 | | class VsyncObserver; |
20 | | namespace gl { |
21 | | class GLContext; |
22 | | } // namespace gl |
23 | | namespace layers { |
24 | | class Compositor; |
25 | | class LayerManager; |
26 | | class LayerManagerComposite; |
27 | | class Compositor; |
28 | | } // namespace layers |
29 | | namespace gfx { |
30 | | class DrawTarget; |
31 | | class SourceSurface; |
32 | | } // namespace gfx |
33 | | namespace widget { |
34 | | |
35 | | class WinCompositorWidget; |
36 | | class GtkCompositorWidget; |
37 | | class AndroidCompositorWidget; |
38 | | class CompositorWidgetInitData; |
39 | | |
40 | | // Gecko widgets usually need to communicate with the CompositorWidget with |
41 | | // platform-specific messages (for example to update the window size or |
42 | | // transparency). This functionality is controlled through a "host". Since |
43 | | // this functionality is platform-dependent, it is only forward declared |
44 | | // here. |
45 | | class PlatformCompositorWidgetDelegate; |
46 | | |
47 | | // Headless mode uses its own, singular CompositorWidget implementation. |
48 | | class HeadlessCompositorWidget; |
49 | | |
50 | | class CompositorWidgetDelegate |
51 | | { |
52 | | public: |
53 | 0 | virtual PlatformCompositorWidgetDelegate* AsPlatformSpecificDelegate() { |
54 | 0 | return nullptr; |
55 | 0 | } |
56 | | |
57 | 0 | virtual HeadlessCompositorWidget* AsHeadlessCompositorWidget() { |
58 | 0 | return nullptr; |
59 | 0 | } |
60 | | }; |
61 | | |
62 | | // Platforms that support out-of-process widgets. |
63 | | #if defined(XP_WIN) || defined(MOZ_X11) |
64 | | // CompositorWidgetParent should implement CompositorWidget and |
65 | | // PCompositorWidgetParent. |
66 | | class CompositorWidgetParent; |
67 | | |
68 | | // CompositorWidgetChild should implement CompositorWidgetDelegate and |
69 | | // PCompositorWidgetChild. |
70 | | class CompositorWidgetChild; |
71 | | |
72 | | # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING |
73 | | #endif |
74 | | |
75 | | class WidgetRenderingContext |
76 | | { |
77 | | public: |
78 | | #if defined(XP_MACOSX) |
79 | | WidgetRenderingContext() |
80 | | : mLayerManager(nullptr) |
81 | | , mGL(nullptr) {} |
82 | | layers::LayerManagerComposite* mLayerManager; |
83 | | gl::GLContext* mGL; |
84 | | #elif defined(MOZ_WIDGET_ANDROID) |
85 | | WidgetRenderingContext() : mCompositor(nullptr) {} |
86 | | layers::Compositor* mCompositor; |
87 | | #endif |
88 | | }; |
89 | | |
90 | | /** |
91 | | * Access to a widget from the compositor is restricted to these methods. |
92 | | */ |
93 | | class CompositorWidget |
94 | | { |
95 | | public: |
96 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::widget::CompositorWidget) |
97 | | |
98 | | /** |
99 | | * Create an in-process compositor widget. aWidget may be ignored if the |
100 | | * platform does not require it. |
101 | | */ |
102 | | static RefPtr<CompositorWidget> CreateLocal(const CompositorWidgetInitData& aInitData, |
103 | | const layers::CompositorOptions& aOptions, |
104 | | nsIWidget* aWidget); |
105 | | |
106 | | /** |
107 | | * Called before rendering using OMTC. Returns false when the widget is |
108 | | * not ready to be rendered (for example while the window is closed). |
109 | | * |
110 | | * Always called from the compositing thread, which may be the main-thread if |
111 | | * OMTC is not enabled. |
112 | | */ |
113 | | virtual bool PreRender(WidgetRenderingContext* aContext) { |
114 | | return true; |
115 | | } |
116 | | |
117 | | /** |
118 | | * Called after rendering using OMTC. Not called when rendering was |
119 | | * cancelled by a negative return value from PreRender. |
120 | | * |
121 | | * Always called from the compositing thread, which may be the main-thread if |
122 | | * OMTC is not enabled. |
123 | | */ |
124 | | virtual void PostRender(WidgetRenderingContext* aContext) |
125 | | {} |
126 | | |
127 | | /** |
128 | | * Called before the LayerManager draws the layer tree. |
129 | | * |
130 | | * Always called from the compositing thread. |
131 | | */ |
132 | | virtual void DrawWindowUnderlay(WidgetRenderingContext* aContext, |
133 | | LayoutDeviceIntRect aRect) |
134 | | {} |
135 | | |
136 | | /** |
137 | | * Called after the LayerManager draws the layer tree |
138 | | * |
139 | | * Always called from the compositing thread. |
140 | | */ |
141 | | virtual void DrawWindowOverlay(WidgetRenderingContext* aContext, |
142 | | LayoutDeviceIntRect aRect) |
143 | | {} |
144 | | |
145 | | /** |
146 | | * Return a DrawTarget for the window which can be composited into. |
147 | | * |
148 | | * Called by BasicCompositor on the compositor thread for OMTC drawing |
149 | | * before each composition. |
150 | | * |
151 | | * The window may specify its buffer mode. If unspecified, it is assumed |
152 | | * to require double-buffering. |
153 | | */ |
154 | | virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawing(); |
155 | | virtual already_AddRefed<gfx::DrawTarget> |
156 | | StartRemoteDrawingInRegion(LayoutDeviceIntRegion& aInvalidRegion, |
157 | | layers::BufferMode* aBufferMode) |
158 | | { |
159 | | return StartRemoteDrawing(); |
160 | | } |
161 | | |
162 | | /** |
163 | | * Ensure that what was painted into the DrawTarget returned from |
164 | | * StartRemoteDrawing reaches the screen. |
165 | | * |
166 | | * Called by BasicCompositor on the compositor thread for OMTC drawing |
167 | | * after each composition. |
168 | | */ |
169 | | virtual void EndRemoteDrawing() |
170 | | {} |
171 | | virtual void EndRemoteDrawingInRegion(gfx::DrawTarget* aDrawTarget, |
172 | | LayoutDeviceIntRegion& aInvalidRegion) |
173 | | { |
174 | | EndRemoteDrawing(); |
175 | | } |
176 | | |
177 | | /** |
178 | | * Return true when it is better to defer EndRemoteDrawing(). |
179 | | * |
180 | | * Called by BasicCompositor on the compositor thread for OMTC drawing |
181 | | * after each composition. |
182 | | */ |
183 | | virtual bool NeedsToDeferEndRemoteDrawing() { |
184 | | return false; |
185 | | } |
186 | | |
187 | | /** |
188 | | * Called when shutting down the LayerManager to clean-up any cached resources. |
189 | | * |
190 | | * Always called from the compositing thread. |
191 | | */ |
192 | | virtual void CleanupWindowEffects() |
193 | | {} |
194 | | |
195 | | /** |
196 | | * A hook for the widget to prepare a Compositor, during the latter's initialization. |
197 | | * |
198 | | * If this method returns true, it means that the widget will be able to |
199 | | * present frames from the compoositor. |
200 | | * |
201 | | * Returning false will cause the compositor's initialization to fail, and |
202 | | * a different compositor backend will be used (if any). |
203 | | */ |
204 | | virtual bool InitCompositor(layers::Compositor* aCompositor) { |
205 | | return true; |
206 | | } |
207 | | |
208 | | /** |
209 | | * Return the size of the drawable area of the widget. |
210 | | */ |
211 | | virtual LayoutDeviceIntSize GetClientSize() = 0; |
212 | | |
213 | | /** |
214 | | * Return the internal format of the default framebuffer for this |
215 | | * widget. |
216 | | */ |
217 | | virtual uint32_t GetGLFrameBufferFormat(); |
218 | | |
219 | | /* |
220 | | * Access the underlying nsIWidget. This method will be removed when the compositor no longer |
221 | | * depends on nsIWidget on any platform. |
222 | | */ |
223 | | virtual nsIWidget* RealWidget() = 0; |
224 | | |
225 | | /** |
226 | | * Clean up any resources used by Start/EndRemoteDrawing. |
227 | | * |
228 | | * Called by BasicCompositor on the compositor thread for OMTC drawing |
229 | | * when the compositor is destroyed. |
230 | | */ |
231 | | virtual void CleanupRemoteDrawing(); |
232 | | |
233 | | /** |
234 | | * Return a key that can represent the widget object round-trip across the |
235 | | * CompositorBridge channel. This only needs to be implemented on GTK and |
236 | | * Windows. |
237 | | * |
238 | | * The key must be the nsIWidget pointer cast to a uintptr_t. See |
239 | | * CompositorBridgeChild::RecvHideAllPlugins and |
240 | | * CompositorBridgeParent::SendHideAllPlugins. |
241 | | */ |
242 | | virtual uintptr_t GetWidgetKey() { |
243 | | return 0; |
244 | | } |
245 | | |
246 | | /** |
247 | | * Create a backbuffer for the software compositor. |
248 | | */ |
249 | | virtual already_AddRefed<gfx::DrawTarget> |
250 | | GetBackBufferDrawTarget(gfx::DrawTarget* aScreenTarget, |
251 | | const LayoutDeviceIntRect& aRect, |
252 | | const LayoutDeviceIntRect& aClearRect); |
253 | | |
254 | | /** |
255 | | * Ensure end of composition to back buffer. |
256 | | * |
257 | | * Called by BasicCompositor on the compositor thread for OMTC drawing |
258 | | * after each composition to back buffer. |
259 | | */ |
260 | | virtual already_AddRefed<gfx::SourceSurface> EndBackBufferDrawing(); |
261 | | |
262 | | /** |
263 | | * Observe or unobserve vsync. |
264 | | */ |
265 | | virtual void ObserveVsync(VsyncObserver* aObserver) = 0; |
266 | | |
267 | | /** |
268 | | * Get the compositor options for the compositor associated with this |
269 | | * CompositorWidget. |
270 | | */ |
271 | | const layers::CompositorOptions& GetCompositorOptions() { |
272 | | return mOptions; |
273 | | } |
274 | | |
275 | | /** |
276 | | * Return true if the window is hidden and should not be composited. |
277 | | */ |
278 | | virtual bool IsHidden() const { |
279 | | return false; |
280 | | } |
281 | | |
282 | | /** |
283 | | * This is only used by out-of-process compositors. |
284 | | */ |
285 | | virtual RefPtr<VsyncObserver> GetVsyncObserver() const; |
286 | | |
287 | | virtual WinCompositorWidget* AsWindows() { |
288 | | return nullptr; |
289 | | } |
290 | | virtual GtkCompositorWidget* AsX11() { |
291 | | return nullptr; |
292 | | } |
293 | | virtual AndroidCompositorWidget* AsAndroid() { |
294 | | return nullptr; |
295 | | } |
296 | | |
297 | | /** |
298 | | * Return the platform-specific delegate for the widget, if any. |
299 | | */ |
300 | | virtual CompositorWidgetDelegate* AsDelegate() { |
301 | | return nullptr; |
302 | | } |
303 | | |
304 | | protected: |
305 | | explicit CompositorWidget(const layers::CompositorOptions& aOptions); |
306 | | virtual ~CompositorWidget(); |
307 | | |
308 | | // Back buffer of BasicCompositor |
309 | | RefPtr<gfx::DrawTarget> mLastBackBuffer; |
310 | | |
311 | | layers::CompositorOptions mOptions; |
312 | | }; |
313 | | |
314 | | } // namespace widget |
315 | | } // namespace mozilla |
316 | | |
317 | | #endif |