1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_InputTaskManager_h
8 #define mozilla_InputTaskManager_h
11 #include "nsXULAppAPI.h"
12 #include "TaskController.h"
13 #include "mozilla/StaticPtr.h"
14 #include "mozilla/StaticPrefs_dom.h"
18 class InputTaskManager
: public TaskManager
{
20 int32_t GetPriorityModifierForEventLoopTurn(
21 const MutexAutoLock
& aProofOfLock
) final
;
22 void WillRunTask() final
;
23 void DidRunTask() final
;
25 enum InputEventQueueState
{
32 void EnableInputEventPrioritization();
33 void FlushInputEventPrioritization();
34 void SuspendInputEventPrioritization();
35 void ResumeInputEventPrioritization();
37 InputEventQueueState
State() { return mInputQueueState
; }
39 void SetState(InputEventQueueState aState
) { mInputQueueState
= aState
; }
41 TimeStamp
InputHandlingStartTime() { return mInputHandlingStartTime
; }
43 void SetInputHandlingStartTime(TimeStamp aStartTime
) {
44 mInputHandlingStartTime
= aStartTime
;
47 static InputTaskManager
* Get() { return gInputTaskManager
.get(); }
48 static void Cleanup() { gInputTaskManager
= nullptr; }
51 bool IsSuspended(const MutexAutoLock
& aProofOfLock
) override
{
52 MOZ_ASSERT(NS_IsMainThread());
53 return mInputQueueState
== STATE_SUSPEND
|| mSuspensionLevel
> 0;
57 MOZ_ASSERT(NS_IsMainThread());
58 return mSuspensionLevel
> 0;
61 void IncSuspensionLevel() {
62 MOZ_ASSERT(NS_IsMainThread());
66 void DecSuspensionLevel() {
67 MOZ_ASSERT(NS_IsMainThread());
71 static bool CanSuspendInputEvent() {
72 // Ensure it's content process because InputTaskManager only
75 // Input tasks will have nullptr as their task manager when the
76 // event queue state is STATE_DISABLED, so we can't suspend
78 return XRE_IsContentProcess() &&
79 StaticPrefs::dom_input_events_canSuspendInBCG_enabled() &&
80 InputTaskManager::Get()->State() !=
81 InputEventQueueState::STATE_DISABLED
;
85 MOZ_ASSERT(StaticPrefs::dom_input_events_strict_input_vsync_alignment());
86 mInputPriorityController
.WillRunVsync();
90 InputTaskManager() : mInputQueueState(STATE_DISABLED
) {}
92 class InputPriorityController
{
94 InputPriorityController();
95 // Determines whether we should use the highest input priority for input
97 bool ShouldUseHighestPriority(InputTaskManager
*);
101 // Gets called when a input task is going to run; If the current
102 // input vsync state is `HasPendingVsync`, determines whether we
103 // should continue running input tasks or leave the `HasPendingVsync` state
105 // 1. Whether we still have time to process input tasks
106 // 2. Whether we have processed the max number of tasks that
107 // we should process.
111 // Used to represents the relationship between Input and Vsync.
113 // HasPendingVsync: There are pending vsync tasks and we are using
114 // InputHighest priority for inputs.
115 // NoPendingVsync: No pending vsync tasks and no need to use InputHighest
117 // RunVsync: Finished running input tasks and the vsync task
119 enum class InputVsyncState
{
125 void EnterPendingVsyncState(uint32_t aNumPendingTasks
);
126 void LeavePendingVsyncState(bool aRunVsync
);
128 // Stores the number of pending input tasks when we enter the
129 // InputVsyncState::HasPendingVsync state.
130 uint32_t mMaxInputTasksToRun
= 0;
133 InputVsyncState mInputVsyncState
;
135 TimeStamp mRunInputStartTime
;
136 TimeDuration mMaxInputHandlingDuration
;
139 int32_t GetPriorityModifierForEventLoopTurnForStrictVsyncAlignment();
141 TimeStamp mInputHandlingStartTime
;
142 Atomic
<InputEventQueueState
> mInputQueueState
;
143 AutoTArray
<TimeStamp
, 4> mStartTimes
;
145 static StaticRefPtr
<InputTaskManager
> gInputTaskManager
;
147 // Number of BCGs have asked InputTaskManager to suspend input events
148 uint32_t mSuspensionLevel
= 0;
150 InputPriorityController mInputPriorityController
;
153 } // namespace mozilla
155 #endif // mozilla_InputTaskManager_h