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/2D.h"
12 #include "mozilla/layers/CompositorOptions.h"
13 #include "mozilla/layers/LayersTypes.h"
26 class LayerManagerComposite
;
35 class WinCompositorWidget
;
36 class GtkCompositorWidget
;
37 class AndroidCompositorWidget
;
38 class CompositorWidgetInitData
;
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
45 class PlatformCompositorWidgetDelegate
;
47 // Headless mode uses its own, singular CompositorWidget implementation.
48 class HeadlessCompositorWidget
;
50 class CompositorWidgetDelegate
53 virtual PlatformCompositorWidgetDelegate
* AsPlatformSpecificDelegate() {
57 virtual HeadlessCompositorWidget
* AsHeadlessCompositorWidget() {
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
;
68 // CompositorWidgetChild should implement CompositorWidgetDelegate and
69 // PCompositorWidgetChild.
70 class CompositorWidgetChild
;
72 # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
75 class WidgetRenderingContext
78 #if defined(XP_MACOSX)
79 WidgetRenderingContext()
80 : mLayerManager(nullptr)
82 layers::LayerManagerComposite
* mLayerManager
;
84 #elif defined(MOZ_WIDGET_ANDROID)
85 WidgetRenderingContext() : mCompositor(nullptr) {}
86 layers::Compositor
* mCompositor
;
91 * Access to a widget from the compositor is restricted to these methods.
93 class CompositorWidget
96 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::widget::CompositorWidget
)
99 * Create an in-process compositor widget. aWidget may be ignored if the
100 * platform does not require it.
102 static RefPtr
<CompositorWidget
> CreateLocal(const CompositorWidgetInitData
& aInitData
,
103 const layers::CompositorOptions
& aOptions
,
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).
110 * Always called from the compositing thread, which may be the main-thread if
111 * OMTC is not enabled.
113 virtual bool PreRender(WidgetRenderingContext
* aContext
) {
118 * Called after rendering using OMTC. Not called when rendering was
119 * cancelled by a negative return value from PreRender.
121 * Always called from the compositing thread, which may be the main-thread if
122 * OMTC is not enabled.
124 virtual void PostRender(WidgetRenderingContext
* aContext
)
128 * Called before the LayerManager draws the layer tree.
130 * Always called from the compositing thread.
132 virtual void DrawWindowUnderlay(WidgetRenderingContext
* aContext
,
133 LayoutDeviceIntRect aRect
)
137 * Called after the LayerManager draws the layer tree
139 * Always called from the compositing thread.
141 virtual void DrawWindowOverlay(WidgetRenderingContext
* aContext
,
142 LayoutDeviceIntRect aRect
)
146 * Return a DrawTarget for the window which can be composited into.
148 * Called by BasicCompositor on the compositor thread for OMTC drawing
149 * before each composition.
151 * The window may specify its buffer mode. If unspecified, it is assumed
152 * to require double-buffering.
154 virtual already_AddRefed
<gfx::DrawTarget
> StartRemoteDrawing();
155 virtual already_AddRefed
<gfx::DrawTarget
>
156 StartRemoteDrawingInRegion(LayoutDeviceIntRegion
& aInvalidRegion
,
157 layers::BufferMode
* aBufferMode
)
159 return StartRemoteDrawing();
163 * Ensure that what was painted into the DrawTarget returned from
164 * StartRemoteDrawing reaches the screen.
166 * Called by BasicCompositor on the compositor thread for OMTC drawing
167 * after each composition.
169 virtual void EndRemoteDrawing()
171 virtual void EndRemoteDrawingInRegion(gfx::DrawTarget
* aDrawTarget
,
172 LayoutDeviceIntRegion
& aInvalidRegion
)
178 * Return true when it is better to defer EndRemoteDrawing().
180 * Called by BasicCompositor on the compositor thread for OMTC drawing
181 * after each composition.
183 virtual bool NeedsToDeferEndRemoteDrawing() {
188 * Called when shutting down the LayerManager to clean-up any cached resources.
190 * Always called from the compositing thread.
192 virtual void CleanupWindowEffects()
196 * A hook for the widget to prepare a Compositor, during the latter's initialization.
198 * If this method returns true, it means that the widget will be able to
199 * present frames from the compoositor.
201 * Returning false will cause the compositor's initialization to fail, and
202 * a different compositor backend will be used (if any).
204 virtual bool InitCompositor(layers::Compositor
* aCompositor
) {
209 * Return the size of the drawable area of the widget.
211 virtual LayoutDeviceIntSize
GetClientSize() = 0;
214 * Return the internal format of the default framebuffer for this
217 virtual uint32_t GetGLFrameBufferFormat();
220 * Access the underlying nsIWidget. This method will be removed when the compositor no longer
221 * depends on nsIWidget on any platform.
223 virtual nsIWidget
* RealWidget() = 0;
226 * Clean up any resources used by Start/EndRemoteDrawing.
228 * Called by BasicCompositor on the compositor thread for OMTC drawing
229 * when the compositor is destroyed.
231 virtual void CleanupRemoteDrawing();
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
238 * The key must be the nsIWidget pointer cast to a uintptr_t. See
239 * CompositorBridgeChild::RecvHideAllPlugins and
240 * CompositorBridgeParent::SendHideAllPlugins.
242 virtual uintptr_t GetWidgetKey() {
247 * Create a backbuffer for the software compositor.
249 virtual already_AddRefed
<gfx::DrawTarget
>
250 GetBackBufferDrawTarget(gfx::DrawTarget
* aScreenTarget
,
251 const LayoutDeviceIntRect
& aRect
,
252 const LayoutDeviceIntRect
& aClearRect
);
255 * Ensure end of composition to back buffer.
257 * Called by BasicCompositor on the compositor thread for OMTC drawing
258 * after each composition to back buffer.
260 virtual already_AddRefed
<gfx::SourceSurface
> EndBackBufferDrawing();
263 * Observe or unobserve vsync.
265 virtual void ObserveVsync(VsyncObserver
* aObserver
) = 0;
268 * Get the compositor options for the compositor associated with this
271 const layers::CompositorOptions
& GetCompositorOptions() {
276 * Return true if the window is hidden and should not be composited.
278 virtual bool IsHidden() const {
283 * This is only used by out-of-process compositors.
285 virtual RefPtr
<VsyncObserver
> GetVsyncObserver() const;
287 virtual WinCompositorWidget
* AsWindows() {
290 virtual GtkCompositorWidget
* AsX11() {
293 virtual AndroidCompositorWidget
* AsAndroid() {
298 * Return the platform-specific delegate for the widget, if any.
300 virtual CompositorWidgetDelegate
* AsDelegate() {
305 explicit CompositorWidget(const layers::CompositorOptions
& aOptions
);
306 virtual ~CompositorWidget();
308 // Back buffer of BasicCompositor
309 RefPtr
<gfx::DrawTarget
> mLastBackBuffer
;
311 layers::CompositorOptions mOptions
;
314 } // namespace widget
315 } // namespace mozilla