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 #include "InputEventStatistics.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/Preferences.h"
11 #include "nsRefreshDriver.h"
15 /*static*/ InputEventStatistics
& InputEventStatistics::Get() {
16 static UniquePtr
<InputEventStatistics
> sInstance
;
18 sInstance
= MakeUnique
<InputEventStatistics
>(ConstructorCookie());
19 ClearOnShutdown(&sInstance
);
24 TimeDuration
InputEventStatistics::TimeDurationCircularBuffer::GetMean() {
25 return mTotal
/ (int64_t)mSize
;
28 InputEventStatistics::InputEventStatistics(ConstructorCookie
&&)
30 MOZ_ASSERT(Preferences::IsServiceAvailable());
31 uint32_t inputDuration
= Preferences::GetUint(
32 "input_event_queue.default_duration_per_event", sDefaultInputDuration
);
34 TimeDuration defaultDuration
= TimeDuration::FromMilliseconds(inputDuration
);
36 uint32_t count
= Preferences::GetUint(
37 "input_event_queue.count_for_prediction", sInputCountForPrediction
);
40 MakeUnique
<TimeDurationCircularBuffer
>(count
, defaultDuration
);
42 uint32_t maxDuration
= Preferences::GetUint("input_event_queue.duration.max",
43 sMaxReservedTimeForHandlingInput
);
45 uint32_t minDuration
= Preferences::GetUint("input_event_queue.duration.min",
46 sMinReservedTimeForHandlingInput
);
48 mMaxInputDuration
= TimeDuration::FromMilliseconds(maxDuration
);
49 mMinInputDuration
= TimeDuration::FromMilliseconds(minDuration
);
52 TimeStamp
InputEventStatistics::GetInputHandlingStartTime(
53 uint32_t aInputCount
) {
55 Maybe
<TimeStamp
> nextTickHint
= nsRefreshDriver::GetNextTickHint();
57 if (nextTickHint
.isNothing()) {
58 // Return a past time to process input events immediately.
59 return TimeStamp::Now() - TimeDuration::FromMilliseconds(1);
61 TimeDuration inputCost
= mLastInputDurations
->GetMean() * aInputCount
;
62 inputCost
= inputCost
> mMaxInputDuration
? mMaxInputDuration
63 : inputCost
< mMinInputDuration
? mMinInputDuration
66 return nextTickHint
.value() - inputCost
;
69 TimeDuration
InputEventStatistics::GetMaxInputHandlingDuration() const {
70 MOZ_ASSERT(StaticPrefs::dom_input_events_strict_input_vsync_alignment());
71 return mMaxInputDuration
;
73 } // namespace mozilla