Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / widget / CompositorWidget.h
blobd5e2fb664b617ff6a9edeabaa9cfdc23fa91001b
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/Rect.h"
12 #include "mozilla/layers/CompositorOptions.h"
13 #include "mozilla/layers/LayersTypes.h"
15 #ifdef MOZ_IS_GCC
16 # include "mozilla/layers/NativeLayer.h"
17 #endif
19 class nsIWidget;
20 class nsBaseWidget;
22 namespace mozilla {
23 class VsyncObserver;
24 namespace gl {
25 class GLContext;
26 } // namespace gl
27 namespace layers {
28 class Compositor;
29 class LayerManager;
30 class NativeLayerRoot;
31 } // namespace layers
32 namespace gfx {
33 class DrawTarget;
34 class SourceSurface;
35 } // namespace gfx
36 namespace widget {
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
47 // here.
48 class PlatformCompositorWidgetDelegate;
50 // Headless mode uses its own, singular CompositorWidget implementation.
51 class HeadlessCompositorWidget;
53 class CompositorWidgetDelegate {
54 public:
55 virtual PlatformCompositorWidgetDelegate* AsPlatformSpecificDelegate() {
56 return nullptr;
59 virtual HeadlessCompositorWidget* AsHeadlessCompositorWidget() {
60 return nullptr;
64 // Platforms that support out-of-process widgets.
65 #if defined(XP_WIN) || defined(MOZ_X11) || defined(MOZ_WIDGET_ANDROID) || \
66 defined(MOZ_WAYLAND)
67 // CompositorWidgetParent should implement CompositorWidget and
68 // PCompositorWidgetParent.
69 class CompositorWidgetParent;
71 // CompositorWidgetChild should implement CompositorWidgetDelegate and
72 // PCompositorWidgetChild.
73 class CompositorWidgetChild;
75 # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
76 #endif
78 class WidgetRenderingContext {
79 public:
80 #if defined(XP_MACOSX)
81 gl::GLContext* mGL = nullptr;
82 #endif
85 /**
86 * Access to a widget from the compositor is restricted to these methods.
88 class CompositorWidget {
89 public:
90 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::widget::CompositorWidget)
92 /**
93 * Create an in-process compositor widget. aWidget may be ignored if the
94 * platform does not require it.
96 static RefPtr<CompositorWidget> CreateLocal(
97 const CompositorWidgetInitData& aInitData,
98 const layers::CompositorOptions& aOptions, nsIWidget* aWidget);
101 * Called before rendering using OMTC. Returns false when the widget is
102 * not ready to be rendered (for example while the window is closed).
104 * Always called from the compositing thread, which may be the main-thread if
105 * OMTC is not enabled.
107 virtual bool PreRender(WidgetRenderingContext* aContext) { return true; }
110 * Called after rendering using OMTC. Not called when rendering was
111 * cancelled by a negative return value from PreRender.
113 * Always called from the compositing thread, which may be the main-thread if
114 * OMTC is not enabled.
116 virtual void PostRender(WidgetRenderingContext* aContext) {}
119 * Called before the first composite. If the result is non-null, one or more
120 * native layers will be placed on the window and used for compositing.
121 * When native layers are used, StartRemoteDrawing(InRegion) and
122 * EndRemoteDrawing(InRegion) will not be called.
124 virtual RefPtr<layers::NativeLayerRoot> GetNativeLayerRoot() {
125 return nullptr;
129 * Return a DrawTarget for the window which can be composited into.
131 * Only called if GetNativeLayerRoot() returns nullptr.
132 * Called by BasicCompositor on the compositor thread for OMTC drawing
133 * before each composition (unless there's a native layer root).
135 * The window may specify its buffer mode. If unspecified, it is assumed
136 * to require double-buffering.
138 virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawing();
139 virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawingInRegion(
140 const LayoutDeviceIntRegion& aInvalidRegion,
141 layers::BufferMode* aBufferMode) {
142 return StartRemoteDrawing();
146 * Ensure that what was painted into the DrawTarget returned from
147 * StartRemoteDrawing reaches the screen.
149 * Called by BasicCompositor on the compositor thread for OMTC drawing
150 * after each composition for which StartRemoteDrawing(InRegion) was called.
152 virtual void EndRemoteDrawing() {}
153 virtual void EndRemoteDrawingInRegion(
154 gfx::DrawTarget* aDrawTarget,
155 const LayoutDeviceIntRegion& aInvalidRegion) {
156 EndRemoteDrawing();
160 * Return true when it is better to defer EndRemoteDrawing().
162 * Called by BasicCompositor on the compositor thread for OMTC drawing
163 * after each composition.
165 virtual bool NeedsToDeferEndRemoteDrawing() { return false; }
168 * Some widgets (namely Gtk) may need clean up underlying surface
169 * before painting to draw transparent objects correctly. Return
170 * the transparent region where this clearing is required.
172 virtual LayoutDeviceIntRegion GetTransparentRegion();
175 * Called when shutting down the LayerManager to clean-up any cached
176 * resources.
178 * Always called from the compositing thread.
180 virtual void CleanupWindowEffects() {}
183 * A hook for the widget to prepare a Compositor, during the latter's
184 * initialization.
186 * If this method returns true, it means that the widget will be able to
187 * present frames from the compoositor.
189 * Returning false will cause the compositor's initialization to fail, and
190 * a different compositor backend will be used (if any).
192 virtual bool InitCompositor(layers::Compositor* aCompositor) { return true; }
195 * A hook that is ran whenever composition is resumed.
197 * This is called from CompositorBridgeParent::ResumeComposition,
198 * immediately prior to webrender being resumed.
200 * Returns true if composition can be successfully resumed, else false.
202 virtual bool OnResumeComposition() { return true; }
205 * Return the size of the drawable area of the widget.
207 virtual LayoutDeviceIntSize GetClientSize() = 0;
210 * Return the internal format of the default framebuffer for this
211 * widget.
213 virtual uint32_t GetGLFrameBufferFormat();
216 * Access the underlying nsIWidget. This method will be removed when the
217 * compositor no longer depends on nsIWidget on any platform.
219 virtual nsIWidget* RealWidget() = 0;
222 * Clean up any resources used by Start/EndRemoteDrawing.
224 * Called by BasicCompositor on the compositor thread for OMTC drawing
225 * when the compositor is destroyed.
227 virtual void CleanupRemoteDrawing();
230 * Return a key that can represent the widget object round-trip across the
231 * CompositorBridge channel. This only needs to be implemented on GTK and
232 * Windows.
234 * The key must be the nsIWidget pointer cast to a uintptr_t. See
235 * CompositorBridgeChild::RecvHideAllPlugins and
236 * CompositorBridgeParent::SendHideAllPlugins.
238 virtual uintptr_t GetWidgetKey() { return 0; }
241 * Create a backbuffer for the software compositor.
243 virtual already_AddRefed<gfx::DrawTarget> GetBackBufferDrawTarget(
244 gfx::DrawTarget* aScreenTarget, const gfx::IntRect& aRect,
245 bool* aOutIsCleared);
248 * Ensure end of composition to back buffer.
250 * Called by BasicCompositor on the compositor thread for OMTC drawing
251 * after each composition to back buffer.
253 virtual already_AddRefed<gfx::SourceSurface> EndBackBufferDrawing();
256 * Observe or unobserve vsync.
258 virtual void ObserveVsync(VsyncObserver* aObserver) = 0;
261 * Get the compositor options for the compositor associated with this
262 * CompositorWidget.
264 const layers::CompositorOptions& GetCompositorOptions() { return mOptions; }
267 * Return true if the window is hidden and should not be composited.
269 virtual bool IsHidden() const { return false; }
272 * This is only used by out-of-process compositors.
274 virtual RefPtr<VsyncObserver> GetVsyncObserver() const;
276 virtual WinCompositorWidget* AsWindows() { return nullptr; }
277 virtual GtkCompositorWidget* AsGTK() { return nullptr; }
278 virtual AndroidCompositorWidget* AsAndroid() { return nullptr; }
281 * Return the platform-specific delegate for the widget, if any.
283 virtual CompositorWidgetDelegate* AsDelegate() { return nullptr; }
285 protected:
286 explicit CompositorWidget(const layers::CompositorOptions& aOptions);
287 virtual ~CompositorWidget();
289 // Back buffer of BasicCompositor
290 RefPtr<gfx::DrawTarget> mLastBackBuffer;
292 layers::CompositorOptions mOptions;
295 } // namespace widget
296 } // namespace mozilla
298 #endif