Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / widget / VsyncDispatcher.h
blobfae4e849c4f61e814464b90f462d8f7d07750191
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/Mutex.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_THREADSAFE_REFCOUNTING(VsyncObserver)
21 public:
22 // The method called when a vsync occurs. Return true if some work was done.
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 bool NotifyVsync(const VsyncEvent& aVsync) = 0;
29 protected:
30 VsyncObserver() {}
31 virtual ~VsyncObserver() {}
32 }; // VsyncObserver
34 // Used to dispatch vsync events in the parent process to compositors.
36 // When the compositor is in-process, CompositorWidgets own a
37 // CompositorVsyncDispatcher, and directly attach the compositor's observer
38 // to it.
40 // When the compositor is out-of-process, the CompositorWidgetDelegate owns
41 // the vsync dispatcher instead. The widget receives vsync observer/unobserve
42 // commands via IPDL, and uses this to attach a CompositorWidgetVsyncObserver.
43 // This observer forwards vsync notifications (on the vsync thread) to a
44 // dedicated vsync I/O thread, which then forwards the notification to the
45 // compositor thread in the compositor process.
46 class CompositorVsyncDispatcher final {
47 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorVsyncDispatcher)
49 public:
50 CompositorVsyncDispatcher();
52 // Called on the vsync thread when a hardware vsync occurs
53 void NotifyVsync(const VsyncEvent& aVsync);
55 // Compositor vsync observers must be added/removed on the compositor thread
56 void SetCompositorVsyncObserver(VsyncObserver* aVsyncObserver);
57 void Shutdown();
59 private:
60 virtual ~CompositorVsyncDispatcher();
61 void ObserveVsync(bool aEnable);
63 Mutex mCompositorObserverLock;
64 RefPtr<VsyncObserver> mCompositorVsyncObserver;
65 bool mDidShutdown;
68 // Dispatch vsync event to ipc actor parent and chrome RefreshTimer.
69 class RefreshTimerVsyncDispatcher final {
70 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefreshTimerVsyncDispatcher)
72 public:
73 RefreshTimerVsyncDispatcher();
75 // Please check CompositorVsyncDispatcher::NotifyVsync().
76 void NotifyVsync(const VsyncEvent& aVsync);
78 // Set chrome process's RefreshTimer to this dispatcher.
79 // This function can be called from any thread.
80 void SetParentRefreshTimer(VsyncObserver* aVsyncObserver);
82 // Add or remove the content process' RefreshTimer to this dispatcher. This
83 // will be a no-op for AddChildRefreshTimer() if the observer is already
84 // registered.
85 // These functions can be called from any thread.
86 void AddChildRefreshTimer(VsyncObserver* aVsyncObserver);
87 void RemoveChildRefreshTimer(VsyncObserver* aVsyncObserver);
89 private:
90 virtual ~RefreshTimerVsyncDispatcher();
91 void UpdateVsyncStatus();
92 bool NeedsVsync();
94 Mutex mRefreshTimersLock;
95 RefPtr<VsyncObserver> mParentRefreshTimer;
96 nsTArray<RefPtr<VsyncObserver>> mChildRefreshTimers;
99 } // namespace mozilla
101 #endif // mozilla_widget_VsyncDispatcher_h