Bug 1700051: part 49) Add some documentation to `Selection::GetRangesForInterval...
[gecko.git] / xpcom / threads / InputEventStatistics.cpp
blobcd9eef36e86181f9b2bea4d231f822330cee4d3d
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/Preferences.h"
10 #include "nsRefreshDriver.h"
12 namespace mozilla {
14 /*static*/ InputEventStatistics& InputEventStatistics::Get() {
15 static UniquePtr<InputEventStatistics> sInstance;
16 if (!sInstance) {
17 sInstance = MakeUnique<InputEventStatistics>(ConstructorCookie());
18 ClearOnShutdown(&sInstance);
20 return *sInstance;
23 TimeDuration InputEventStatistics::TimeDurationCircularBuffer::GetMean() {
24 return mTotal / (int64_t)mSize;
27 InputEventStatistics::InputEventStatistics(ConstructorCookie&&)
28 : mEnable(false) {
29 MOZ_ASSERT(Preferences::IsServiceAvailable());
30 uint32_t inputDuration = Preferences::GetUint(
31 "input_event_queue.default_duration_per_event", sDefaultInputDuration);
33 TimeDuration defaultDuration = TimeDuration::FromMilliseconds(inputDuration);
35 uint32_t count = Preferences::GetUint(
36 "input_event_queue.count_for_prediction", sInputCountForPrediction);
38 mLastInputDurations =
39 MakeUnique<TimeDurationCircularBuffer>(count, defaultDuration);
41 uint32_t maxDuration = Preferences::GetUint("input_event_queue.duration.max",
42 sMaxReservedTimeForHandlingInput);
44 uint32_t minDuration = Preferences::GetUint("input_event_queue.duration.min",
45 sMinReservedTimeForHandlingInput);
47 mMaxInputDuration = TimeDuration::FromMilliseconds(maxDuration);
48 mMinInputDuration = TimeDuration::FromMilliseconds(minDuration);
51 TimeStamp InputEventStatistics::GetInputHandlingStartTime(
52 uint32_t aInputCount) {
53 MOZ_ASSERT(mEnable);
54 Maybe<TimeStamp> nextTickHint = nsRefreshDriver::GetNextTickHint();
56 if (nextTickHint.isNothing()) {
57 // Return a past time to process input events immediately.
58 return TimeStamp::Now() - TimeDuration::FromMilliseconds(1);
60 TimeDuration inputCost = mLastInputDurations->GetMean() * aInputCount;
61 inputCost = inputCost > mMaxInputDuration ? mMaxInputDuration
62 : inputCost < mMinInputDuration ? mMinInputDuration
63 : inputCost;
65 return nextTickHint.value() - inputCost;
68 TimeDuration InputEventStatistics::GetMaxInputHandlingDuration() const {
69 MOZ_ASSERT(StaticPrefs::dom_input_events_strict_input_vsync_alignment());
70 return mMaxInputDuration;
72 } // namespace mozilla