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"
19 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VsyncObserver
)
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(TimeStamp aVsyncTimestamp
) = 0;
31 virtual ~VsyncObserver() {}
34 // Used to dispatch vsync events in the parent process to compositors
35 class CompositorVsyncDispatcher MOZ_FINAL
37 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorVsyncDispatcher
)
40 CompositorVsyncDispatcher();
42 // Called on the vsync thread when a hardware vsync occurs
43 // The aVsyncTimestamp can mean different things depending on the platform:
44 // b2g - The vsync timestamp of the previous frame that was just displayed
45 // OSX - The vsync timestamp of the upcoming frame
46 // TODO: Windows / Linux. DOCUMENT THIS WHEN IMPLEMENTING ON THOSE PLATFORMS
48 void NotifyVsync(TimeStamp aVsyncTimestamp
);
50 // Compositor vsync observers must be added/removed on the compositor thread
51 void SetCompositorVsyncObserver(VsyncObserver
* aVsyncObserver
);
55 virtual ~CompositorVsyncDispatcher();
56 void ObserveVsync(bool aEnable
);
58 Mutex mCompositorObserverLock
;
59 nsRefPtr
<VsyncObserver
> mCompositorVsyncObserver
;
63 // Dispatch vsync event to ipc actor parent and chrome RefreshTimer.
64 class RefreshTimerVsyncDispatcher MOZ_FINAL
66 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefreshTimerVsyncDispatcher
)
69 RefreshTimerVsyncDispatcher();
71 // Please check CompositorVsyncDispatcher::NotifyVsync().
72 void NotifyVsync(TimeStamp aVsyncTimestamp
);
74 // Set chrome process's RefreshTimer to this dispatcher.
75 // This function can be called from any thread.
76 void SetParentRefreshTimer(VsyncObserver
* aVsyncObserver
);
78 // Add or remove the content process' RefreshTimer to this dispatcher. This
79 // will be a no-op for AddChildRefreshTimer() if the observer is already
81 // These functions can be called from any thread.
82 void AddChildRefreshTimer(VsyncObserver
* aVsyncObserver
);
83 void RemoveChildRefreshTimer(VsyncObserver
* aVsyncObserver
);
86 virtual ~RefreshTimerVsyncDispatcher();
87 void UpdateVsyncStatus();
90 Mutex mRefreshTimersLock
;
91 nsRefPtr
<VsyncObserver
> mParentRefreshTimer
;
92 nsTArray
<nsRefPtr
<VsyncObserver
>> mChildRefreshTimers
;
95 } // namespace mozilla
97 #endif // mozilla_widget_VsyncDispatcher_h