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