Bug 1523562 [wpt PR 14965] - Sync Mozilla CSS tests as of 2019-01-20, a=testonly
[gecko.git] / widget / CompositorWidget.h
blob7732d7bd8192febc8cd35b06c94162a6ccacb12d
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"
10 #include "Units.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/layers/CompositorOptions.h"
13 #include "mozilla/layers/LayersTypes.h"
15 class nsIWidget;
16 class nsBaseWidget;
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 {
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
44 // here.
45 class PlatformCompositorWidgetDelegate;
47 // Headless mode uses its own, singular CompositorWidget implementation.
48 class HeadlessCompositorWidget;
50 class CompositorWidgetDelegate {
51 public:
52 virtual PlatformCompositorWidgetDelegate* AsPlatformSpecificDelegate() {
53 return nullptr;
56 virtual HeadlessCompositorWidget* AsHeadlessCompositorWidget() {
57 return nullptr;
61 // Platforms that support out-of-process widgets.
62 #if defined(XP_WIN) || defined(MOZ_X11)
63 // CompositorWidgetParent should implement CompositorWidget and
64 // PCompositorWidgetParent.
65 class CompositorWidgetParent;
67 // CompositorWidgetChild should implement CompositorWidgetDelegate and
68 // PCompositorWidgetChild.
69 class CompositorWidgetChild;
71 # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
72 #endif
74 class WidgetRenderingContext {
75 public:
76 #if defined(XP_MACOSX)
77 WidgetRenderingContext() : mLayerManager(nullptr), mGL(nullptr) {}
78 layers::LayerManagerComposite* mLayerManager;
79 gl::GLContext* mGL;
80 #elif defined(MOZ_WIDGET_ANDROID)
81 WidgetRenderingContext() : mCompositor(nullptr) {}
82 layers::Compositor* mCompositor;
83 #endif
86 /**
87 * Access to a widget from the compositor is restricted to these methods.
89 class CompositorWidget {
90 public:
91 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::widget::CompositorWidget)
93 /**
94 * Create an in-process compositor widget. aWidget may be ignored if the
95 * platform does not require it.
97 static RefPtr<CompositorWidget> CreateLocal(
98 const CompositorWidgetInitData& aInitData,
99 const layers::CompositorOptions& aOptions, nsIWidget* aWidget);
102 * Called before rendering using OMTC. Returns false when the widget is
103 * not ready to be rendered (for example while the window is closed).
105 * Always called from the compositing thread, which may be the main-thread if
106 * OMTC is not enabled.
108 virtual bool PreRender(WidgetRenderingContext* aContext) { return true; }
111 * Called after rendering using OMTC. Not called when rendering was
112 * cancelled by a negative return value from PreRender.
114 * Always called from the compositing thread, which may be the main-thread if
115 * OMTC is not enabled.
117 virtual void PostRender(WidgetRenderingContext* aContext) {}
120 * Called before the LayerManager draws the layer tree.
122 * Always called from the compositing thread.
124 virtual void DrawWindowUnderlay(WidgetRenderingContext* aContext,
125 LayoutDeviceIntRect aRect) {}
128 * Called after the LayerManager draws the layer tree
130 * Always called from the compositing thread.
132 virtual void DrawWindowOverlay(WidgetRenderingContext* aContext,
133 LayoutDeviceIntRect aRect) {}
136 * Return a DrawTarget for the window which can be composited into.
138 * Called by BasicCompositor on the compositor thread for OMTC drawing
139 * before each composition.
141 * The window may specify its buffer mode. If unspecified, it is assumed
142 * to require double-buffering.
144 virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawing();
145 virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawingInRegion(
146 LayoutDeviceIntRegion& aInvalidRegion, layers::BufferMode* aBufferMode) {
147 return StartRemoteDrawing();
151 * Ensure that what was painted into the DrawTarget returned from
152 * StartRemoteDrawing reaches the screen.
154 * Called by BasicCompositor on the compositor thread for OMTC drawing
155 * after each composition.
157 virtual void EndRemoteDrawing() {}
158 virtual void EndRemoteDrawingInRegion(gfx::DrawTarget* aDrawTarget,
159 LayoutDeviceIntRegion& aInvalidRegion) {
160 EndRemoteDrawing();
164 * Return true when it is better to defer EndRemoteDrawing().
166 * Called by BasicCompositor on the compositor thread for OMTC drawing
167 * after each composition.
169 virtual bool NeedsToDeferEndRemoteDrawing() { return false; }
172 * Called when shutting down the LayerManager to clean-up any cached
173 * resources.
175 * Always called from the compositing thread.
177 virtual void CleanupWindowEffects() {}
180 * A hook for the widget to prepare a Compositor, during the latter's
181 * initialization.
183 * If this method returns true, it means that the widget will be able to
184 * present frames from the compoositor.
186 * Returning false will cause the compositor's initialization to fail, and
187 * a different compositor backend will be used (if any).
189 virtual bool InitCompositor(layers::Compositor* aCompositor) { return true; }
192 * Return the size of the drawable area of the widget.
194 virtual LayoutDeviceIntSize GetClientSize() = 0;
197 * Return the internal format of the default framebuffer for this
198 * widget.
200 virtual uint32_t GetGLFrameBufferFormat();
203 * Access the underlying nsIWidget. This method will be removed when the
204 * compositor no longer depends on nsIWidget on any platform.
206 virtual nsIWidget* RealWidget() = 0;
209 * Clean up any resources used by Start/EndRemoteDrawing.
211 * Called by BasicCompositor on the compositor thread for OMTC drawing
212 * when the compositor is destroyed.
214 virtual void CleanupRemoteDrawing();
217 * Return a key that can represent the widget object round-trip across the
218 * CompositorBridge channel. This only needs to be implemented on GTK and
219 * Windows.
221 * The key must be the nsIWidget pointer cast to a uintptr_t. See
222 * CompositorBridgeChild::RecvHideAllPlugins and
223 * CompositorBridgeParent::SendHideAllPlugins.
225 virtual uintptr_t GetWidgetKey() { return 0; }
228 * Create a backbuffer for the software compositor.
230 virtual already_AddRefed<gfx::DrawTarget> GetBackBufferDrawTarget(
231 gfx::DrawTarget* aScreenTarget, const LayoutDeviceIntRect& aRect,
232 const LayoutDeviceIntRect& aClearRect);
235 * Ensure end of composition to back buffer.
237 * Called by BasicCompositor on the compositor thread for OMTC drawing
238 * after each composition to back buffer.
240 virtual already_AddRefed<gfx::SourceSurface> EndBackBufferDrawing();
243 * Observe or unobserve vsync.
245 virtual void ObserveVsync(VsyncObserver* aObserver) = 0;
248 * Get the compositor options for the compositor associated with this
249 * CompositorWidget.
251 const layers::CompositorOptions& GetCompositorOptions() { return mOptions; }
254 * Return true if the window is hidden and should not be composited.
256 virtual bool IsHidden() const { return false; }
259 * This is only used by out-of-process compositors.
261 virtual RefPtr<VsyncObserver> GetVsyncObserver() const;
263 virtual WinCompositorWidget* AsWindows() { return nullptr; }
264 virtual GtkCompositorWidget* AsX11() { return nullptr; }
265 virtual AndroidCompositorWidget* AsAndroid() { return nullptr; }
268 * Return the platform-specific delegate for the widget, if any.
270 virtual CompositorWidgetDelegate* AsDelegate() { return nullptr; }
272 protected:
273 explicit CompositorWidget(const layers::CompositorOptions& aOptions);
274 virtual ~CompositorWidget();
276 // Back buffer of BasicCompositor
277 RefPtr<gfx::DrawTarget> mLastBackBuffer;
279 layers::CompositorOptions mOptions;
282 } // namespace widget
283 } // namespace mozilla
285 #endif