Bug 1839316: part 11) Factor preparing the incremental stream loader out of `StartLoa...
[gecko.git] / widget / VsyncDispatcher.h
blob1001a748881ba417dde6fd14d987bcbb6a070f91
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_widget_VsyncDispatcher_h
7 #define mozilla_widget_VsyncDispatcher_h
9 #include "mozilla/DataMutex.h"
10 #include "mozilla/TimeStamp.h"
11 #include "nsISupportsImpl.h"
12 #include "nsTArray.h"
13 #include "mozilla/RefPtr.h"
14 #include "VsyncSource.h"
16 namespace mozilla {
18 class VsyncObserver {
19 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
21 public:
22 // The method is called when a vsync occurs.
23 // In general, this vsync notification will occur on the hardware vsync
24 // thread from VsyncSource. But it might also be called on PVsync ipc thread
25 // if this notification is cross process. Thus all observer should check the
26 // thread model before handling the real task.
27 virtual void NotifyVsync(const VsyncEvent& aVsync) = 0;
29 protected:
30 VsyncObserver() = default;
31 virtual ~VsyncObserver() = default;
32 }; // VsyncObserver
34 class VsyncDispatcher;
36 // Used to dispatch vsync events in the parent process to compositors.
38 // When the compositor is in-process, CompositorWidgets own a
39 // CompositorVsyncDispatcher, and directly attach the compositor's observer
40 // to it.
42 // When the compositor is out-of-process, the CompositorWidgetDelegate owns
43 // the vsync dispatcher instead. The widget receives vsync observer/unobserve
44 // commands via IPDL, and uses this to attach a CompositorWidgetVsyncObserver.
45 // This observer forwards vsync notifications (on the vsync thread) to a
46 // dedicated vsync I/O thread, which then forwards the notification to the
47 // compositor thread in the compositor process.
48 class CompositorVsyncDispatcher final : public VsyncObserver {
49 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorVsyncDispatcher, override)
51 public:
52 explicit CompositorVsyncDispatcher(RefPtr<VsyncDispatcher> aVsyncDispatcher);
54 // Called on the vsync thread when a hardware vsync occurs
55 void NotifyVsync(const VsyncEvent& aVsync) override;
57 // Compositor vsync observers must be added/removed on the compositor thread
58 void SetCompositorVsyncObserver(VsyncObserver* aVsyncObserver);
59 void Shutdown();
61 private:
62 virtual ~CompositorVsyncDispatcher();
63 void ObserveVsync(bool aEnable);
65 RefPtr<VsyncDispatcher> mVsyncDispatcher;
66 Mutex mCompositorObserverLock MOZ_UNANNOTATED;
67 RefPtr<VsyncObserver> mCompositorVsyncObserver;
68 bool mDidShutdown;
71 // Dispatch vsync events to various observers. This is used by:
72 // - CompositorVsyncDispatcher
73 // - Parent process refresh driver timers
74 // - IPC for content process refresh driver timers (VsyncParent <->
75 // VsyncMainChild)
76 // - IPC for content process worker requestAnimationFrame (VsyncParent <->
77 // VsyncWorkerChild)
79 // This class is only used in the parent process.
80 // There is one global vsync dispatcher which is managed by gfxPlatform.
81 // On Linux Wayland, there is also one vsync source and vsync dispatcher per
82 // widget.
83 // A vsync dispatcher can swap out its underlying VsyncSource. This happens, for
84 // example, when the layout.frame_rate pref is modified, causing us to switch
85 // between hardware vsync and software vsync.
86 class VsyncDispatcher final {
87 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VsyncDispatcher)
89 public:
90 explicit VsyncDispatcher(gfx::VsyncSource* aVsyncSource);
92 // Please check CompositorVsyncDispatcher::NotifyVsync().
93 void NotifyVsync(const VsyncEvent& aVsync);
95 // Swap out the underlying vsync source. Can be called on any thread.
96 // aVsyncSource must be non-null.
97 void SetVsyncSource(gfx::VsyncSource* aVsyncSource);
99 // Always non-null.
100 RefPtr<gfx::VsyncSource> GetCurrentVsyncSource();
102 TimeDuration GetVsyncRate();
104 // Add a vsync observer to this dispatcher. This is a no-op if the observer is
105 // already registered. Can be called from any thread.
106 void AddVsyncObserver(VsyncObserver* aVsyncObserver);
108 // Remove a vsync observer from this dispatcher. This is a no-op if the
109 // observer is not registered. Can be called from any thread.
110 void RemoveVsyncObserver(VsyncObserver* aVsyncObserver);
112 // Add and remove an observer for vsync which can only be notified on the
113 // main thread. Note that keeping an observer registered means vsync will keep
114 // firing, which may impact power usage. So this is intended only for "short
115 // term" vsync observers.
116 // These methods must be called on the parent process main thread, and the
117 // observer will likewise be notified on the parent process main thread.
118 void AddMainThreadObserver(VsyncObserver* aObserver);
119 void RemoveMainThreadObserver(VsyncObserver* aObserver);
121 private:
122 virtual ~VsyncDispatcher();
124 // Can be called on any thread.
125 void UpdateVsyncStatus();
127 // Can only be called on the main thread.
128 void NotifyMainThreadObservers(VsyncEvent aEvent);
130 struct State {
131 explicit State(gfx::VsyncSource* aVsyncSource)
132 : mCurrentVsyncSource(aVsyncSource) {}
133 State(State&&) = default;
134 ~State() = default;
136 nsTArray<RefPtr<VsyncObserver>> mObservers;
137 nsTArray<RefPtr<VsyncObserver>> mMainThreadObservers;
138 VsyncId mLastVsyncIdSentToMainThread;
139 VsyncId mLastMainThreadProcessedVsyncId;
141 // Always non-null.
142 RefPtr<gfx::VsyncSource> mCurrentVsyncSource;
144 // The number of skipped vsyncs since the last non-skipped vsync.
145 // Reset to zero every n vsyncs, where n is given by the pref
146 // gfx.display.frame-rate-divisor.
147 int32_t mVsyncSkipCounter = 0;
149 bool mIsObservingVsync = false;
152 DataMutex<State> mState;
155 } // namespace mozilla
157 #endif // mozilla_widget_VsyncDispatcher_h