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"
13 #include "mozilla/RefPtr.h"
14 #include "VsyncSource.h"
19 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
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;
30 VsyncObserver() = default;
31 virtual ~VsyncObserver() = default;
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
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
)
50 CompositorVsyncDispatcher();
51 explicit CompositorVsyncDispatcher(RefPtr
<gfx::VsyncSource
> aVsyncSource
);
53 // Called on the vsync thread when a hardware vsync occurs
54 void NotifyVsync(const VsyncEvent
& aVsync
);
56 void MoveToSource(const RefPtr
<gfx::VsyncSource
>& aVsyncSource
);
58 // Compositor vsync observers must be added/removed on the compositor thread
59 void SetCompositorVsyncObserver(VsyncObserver
* aVsyncObserver
);
63 virtual ~CompositorVsyncDispatcher();
64 void ObserveVsync(bool aEnable
);
66 RefPtr
<gfx::VsyncSource
> mVsyncSource
;
67 Mutex mCompositorObserverLock MOZ_UNANNOTATED
;
68 RefPtr
<VsyncObserver
> mCompositorVsyncObserver
;
72 // Dispatch vsync events to various observers. This is used by:
73 // - Parent process refresh driver timers
74 // - IPC for content process refresh driver timers (VsyncParent <->
76 // - IPC for content process worker requestAnimationFrame (VsyncParent <->
78 class RefreshTimerVsyncDispatcher final
{
79 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefreshTimerVsyncDispatcher
)
82 explicit RefreshTimerVsyncDispatcher(gfx::VsyncSource
* aVsyncSource
);
84 // Please check CompositorVsyncDispatcher::NotifyVsync().
85 void NotifyVsync(const VsyncEvent
& aVsync
);
87 void MoveToSource(gfx::VsyncSource
* aVsyncSource
);
89 // Add a vsync observer to this dispatcher. This is a no-op if the observer is
90 // already registered. Can be called from any thread.
91 void AddVsyncObserver(VsyncObserver
* aVsyncObserver
);
93 // Remove a vsync observer from this dispatcher. This is a no-op if the
94 // observer is not registered. Can be called from any thread.
95 void RemoveVsyncObserver(VsyncObserver
* aVsyncObserver
);
98 virtual ~RefreshTimerVsyncDispatcher();
99 void UpdateVsyncStatus();
102 // We need to hold a weak ref to the vsync source we belong to in order to
103 // notify it of our vsync requirement. The vsync source holds a RefPtr to us,
104 // so we can't hold a RefPtr back without causing a cyclic dependency.
105 gfx::VsyncSource
* mVsyncSource
;
106 DataMutex
<nsTArray
<RefPtr
<VsyncObserver
>>> mVsyncObservers
;
109 } // namespace mozilla
111 #endif // mozilla_widget_VsyncDispatcher_h