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/. */
5 #ifndef mozilla_widget_CompositorWidget_h__
6 #define mozilla_widget_CompositorWidget_h__
8 #include "nsISupports.h"
9 #include "mozilla/RefPtr.h"
11 #include "mozilla/gfx/Rect.h"
12 #include "mozilla/layers/CompositorOptions.h"
13 #include "mozilla/layers/LayersTypes.h"
16 # include "mozilla/layers/NativeLayer.h"
30 class NativeLayerRoot
;
38 class WinCompositorWidget
;
39 class GtkCompositorWidget
;
40 class AndroidCompositorWidget
;
41 class CompositorWidgetInitData
;
43 // Gecko widgets usually need to communicate with the CompositorWidget with
44 // platform-specific messages (for example to update the window size or
45 // transparency). This functionality is controlled through a "host". Since
46 // this functionality is platform-dependent, it is only forward declared
48 class PlatformCompositorWidgetDelegate
;
50 // Headless mode uses its own, singular CompositorWidget implementation.
51 class HeadlessCompositorWidget
;
53 class CompositorWidgetDelegate
{
55 virtual PlatformCompositorWidgetDelegate
* AsPlatformSpecificDelegate() {
59 virtual HeadlessCompositorWidget
* AsHeadlessCompositorWidget() {
64 // Platforms that support out-of-process widgets.
65 #if defined(XP_WIN) || defined(MOZ_X11) || defined(MOZ_WIDGET_ANDROID)
66 // CompositorWidgetParent should implement CompositorWidget and
67 // PCompositorWidgetParent.
68 class CompositorWidgetParent
;
70 // CompositorWidgetChild should implement CompositorWidgetDelegate and
71 // PCompositorWidgetChild.
72 class CompositorWidgetChild
;
74 # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
77 class WidgetRenderingContext
{
79 #if defined(XP_MACOSX)
80 gl::GLContext
* mGL
= nullptr;
85 * Access to a widget from the compositor is restricted to these methods.
87 class CompositorWidget
{
89 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::widget::CompositorWidget
)
92 * Create an in-process compositor widget. aWidget may be ignored if the
93 * platform does not require it.
95 static RefPtr
<CompositorWidget
> CreateLocal(
96 const CompositorWidgetInitData
& aInitData
,
97 const layers::CompositorOptions
& aOptions
, nsIWidget
* aWidget
);
100 * Called before rendering using OMTC. Returns false when the widget is
101 * not ready to be rendered (for example while the window is closed).
103 * Always called from the compositing thread, which may be the main-thread if
104 * OMTC is not enabled.
106 virtual bool PreRender(WidgetRenderingContext
* aContext
) { return true; }
109 * Called after rendering using OMTC. Not called when rendering was
110 * cancelled by a negative return value from PreRender.
112 * Always called from the compositing thread, which may be the main-thread if
113 * OMTC is not enabled.
115 virtual void PostRender(WidgetRenderingContext
* aContext
) {}
118 * Called before the first composite. If the result is non-null, one or more
119 * native layers will be placed on the window and used for compositing.
120 * When native layers are used, StartRemoteDrawing(InRegion) and
121 * EndRemoteDrawing(InRegion) will not be called.
123 virtual RefPtr
<layers::NativeLayerRoot
> GetNativeLayerRoot() {
128 * Return a DrawTarget for the window which can be composited into.
130 * Only called if GetNativeLayerRoot() returns nullptr.
131 * Called by BasicCompositor on the compositor thread for OMTC drawing
132 * before each composition (unless there's a native layer root).
134 * The window may specify its buffer mode. If unspecified, it is assumed
135 * to require double-buffering.
137 virtual already_AddRefed
<gfx::DrawTarget
> StartRemoteDrawing();
138 virtual already_AddRefed
<gfx::DrawTarget
> StartRemoteDrawingInRegion(
139 const LayoutDeviceIntRegion
& aInvalidRegion
,
140 layers::BufferMode
* aBufferMode
) {
141 return StartRemoteDrawing();
145 * Ensure that what was painted into the DrawTarget returned from
146 * StartRemoteDrawing reaches the screen.
148 * Called by BasicCompositor on the compositor thread for OMTC drawing
149 * after each composition for which StartRemoteDrawing(InRegion) was called.
151 virtual void EndRemoteDrawing() {}
152 virtual void EndRemoteDrawingInRegion(
153 gfx::DrawTarget
* aDrawTarget
,
154 const LayoutDeviceIntRegion
& aInvalidRegion
) {
159 * Return true when it is better to defer EndRemoteDrawing().
161 * Called by BasicCompositor on the compositor thread for OMTC drawing
162 * after each composition.
164 virtual bool NeedsToDeferEndRemoteDrawing() { return false; }
167 * Some widgets (namely Gtk) may need clean up underlying surface
168 * before painting to draw transparent objects correctly. Return
169 * the transparent region where this clearing is required.
171 virtual LayoutDeviceIntRegion
GetTransparentRegion();
174 * Called when shutting down the LayerManager to clean-up any cached
177 * Always called from the compositing thread.
179 virtual void CleanupWindowEffects() {}
182 * A hook for the widget to prepare a Compositor, during the latter's
185 * If this method returns true, it means that the widget will be able to
186 * present frames from the compoositor.
188 * Returning false will cause the compositor's initialization to fail, and
189 * a different compositor backend will be used (if any).
191 virtual bool InitCompositor(layers::Compositor
* aCompositor
) { return true; }
194 * A hook that is ran whenever composition is resumed.
196 * This is called from CompositorBridgeParent::ResumeComposition,
197 * immediately prior to webrender being resumed.
199 * Returns true if composition can be successfully resumed, else false.
201 virtual bool OnResumeComposition() { return true; }
204 * Return the size of the drawable area of the widget.
206 virtual LayoutDeviceIntSize
GetClientSize() = 0;
209 * Return the internal format of the default framebuffer for this
212 virtual uint32_t GetGLFrameBufferFormat();
215 * Access the underlying nsIWidget. This method will be removed when the
216 * compositor no longer depends on nsIWidget on any platform.
218 virtual nsIWidget
* RealWidget() = 0;
221 * Clean up any resources used by Start/EndRemoteDrawing.
223 * Called by BasicCompositor on the compositor thread for OMTC drawing
224 * when the compositor is destroyed.
226 virtual void CleanupRemoteDrawing();
229 * Return a key that can represent the widget object round-trip across the
230 * CompositorBridge channel. This only needs to be implemented on GTK and
233 * The key must be the nsIWidget pointer cast to a uintptr_t. See
234 * CompositorBridgeChild::RecvHideAllPlugins and
235 * CompositorBridgeParent::SendHideAllPlugins.
237 virtual uintptr_t GetWidgetKey() { return 0; }
240 * Create a backbuffer for the software compositor.
242 virtual already_AddRefed
<gfx::DrawTarget
> GetBackBufferDrawTarget(
243 gfx::DrawTarget
* aScreenTarget
, const gfx::IntRect
& aRect
,
244 bool* aOutIsCleared
);
247 * Ensure end of composition to back buffer.
249 * Called by BasicCompositor on the compositor thread for OMTC drawing
250 * after each composition to back buffer.
252 virtual already_AddRefed
<gfx::SourceSurface
> EndBackBufferDrawing();
255 * Observe or unobserve vsync.
257 virtual void ObserveVsync(VsyncObserver
* aObserver
) = 0;
260 * Get the compositor options for the compositor associated with this
263 const layers::CompositorOptions
& GetCompositorOptions() { return mOptions
; }
266 * Return true if the window is hidden and should not be composited.
268 virtual bool IsHidden() const { return false; }
271 * This is only used by out-of-process compositors.
273 virtual RefPtr
<VsyncObserver
> GetVsyncObserver() const;
275 virtual WinCompositorWidget
* AsWindows() { return nullptr; }
276 virtual GtkCompositorWidget
* AsGTK() { return nullptr; }
277 virtual AndroidCompositorWidget
* AsAndroid() { return nullptr; }
280 * Return the platform-specific delegate for the widget, if any.
282 virtual CompositorWidgetDelegate
* AsDelegate() { return nullptr; }
285 explicit CompositorWidget(const layers::CompositorOptions
& aOptions
);
286 virtual ~CompositorWidget();
288 // Back buffer of BasicCompositor
289 RefPtr
<gfx::DrawTarget
> mLastBackBuffer
;
291 layers::CompositorOptions mOptions
;
294 } // namespace widget
295 } // namespace mozilla