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)
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 * Return the size of the drawable area of the widget.
196 virtual LayoutDeviceIntSize
GetClientSize() = 0;
199 * Return the internal format of the default framebuffer for this
202 virtual uint32_t GetGLFrameBufferFormat();
205 * Access the underlying nsIWidget. This method will be removed when the
206 * compositor no longer depends on nsIWidget on any platform.
208 virtual nsIWidget
* RealWidget() = 0;
211 * Clean up any resources used by Start/EndRemoteDrawing.
213 * Called by BasicCompositor on the compositor thread for OMTC drawing
214 * when the compositor is destroyed.
216 virtual void CleanupRemoteDrawing();
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
223 * The key must be the nsIWidget pointer cast to a uintptr_t. See
224 * CompositorBridgeChild::RecvHideAllPlugins and
225 * CompositorBridgeParent::SendHideAllPlugins.
227 virtual uintptr_t GetWidgetKey() { return 0; }
230 * Create a backbuffer for the software compositor.
232 virtual already_AddRefed
<gfx::DrawTarget
> GetBackBufferDrawTarget(
233 gfx::DrawTarget
* aScreenTarget
, const gfx::IntRect
& aRect
,
234 bool* aOutIsCleared
);
237 * Ensure end of composition to back buffer.
239 * Called by BasicCompositor on the compositor thread for OMTC drawing
240 * after each composition to back buffer.
242 virtual already_AddRefed
<gfx::SourceSurface
> EndBackBufferDrawing();
245 * Observe or unobserve vsync.
247 virtual void ObserveVsync(VsyncObserver
* aObserver
) = 0;
250 * Get the compositor options for the compositor associated with this
253 const layers::CompositorOptions
& GetCompositorOptions() { return mOptions
; }
256 * Return true if the window is hidden and should not be composited.
258 virtual bool IsHidden() const { return false; }
261 * This is only used by out-of-process compositors.
263 virtual RefPtr
<VsyncObserver
> GetVsyncObserver() const;
265 virtual WinCompositorWidget
* AsWindows() { return nullptr; }
266 virtual GtkCompositorWidget
* AsGTK() { return nullptr; }
267 virtual AndroidCompositorWidget
* AsAndroid() { return nullptr; }
270 * Return the platform-specific delegate for the widget, if any.
272 virtual CompositorWidgetDelegate
* AsDelegate() { return nullptr; }
275 explicit CompositorWidget(const layers::CompositorOptions
& aOptions
);
276 virtual ~CompositorWidget();
278 // Back buffer of BasicCompositor
279 RefPtr
<gfx::DrawTarget
> mLastBackBuffer
;
281 layers::CompositorOptions mOptions
;
284 } // namespace widget
285 } // namespace mozilla