/src/mozilla-central/xpcom/threads/InputEventStatistics.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "InputEventStatistics.h" |
8 | | |
9 | | #include "nsRefreshDriver.h" |
10 | | |
11 | | namespace mozilla { |
12 | | |
13 | | /*static*/ InputEventStatistics& |
14 | | InputEventStatistics::Get() |
15 | 0 | { |
16 | 0 | static UniquePtr<InputEventStatistics> sInstance; |
17 | 0 | if (!sInstance) { |
18 | 0 | sInstance = MakeUnique<InputEventStatistics>(ConstructorCookie()); |
19 | 0 | ClearOnShutdown(&sInstance); |
20 | 0 | } |
21 | 0 | return *sInstance; |
22 | 0 | } |
23 | | |
24 | | TimeDuration |
25 | | InputEventStatistics::TimeDurationCircularBuffer::GetMean() |
26 | 0 | { |
27 | 0 | return mTotal / (int64_t)mSize; |
28 | 0 | } |
29 | | |
30 | | InputEventStatistics::InputEventStatistics(ConstructorCookie&&) |
31 | | : mEnable(false) |
32 | 0 | { |
33 | 0 | MOZ_ASSERT(Preferences::IsServiceAvailable()); |
34 | 0 | uint32_t inputDuration = |
35 | 0 | Preferences::GetUint("input_event_queue.default_duration_per_event", |
36 | 0 | sDefaultInputDuration); |
37 | 0 |
|
38 | 0 | TimeDuration defaultDuration = TimeDuration::FromMilliseconds(inputDuration); |
39 | 0 |
|
40 | 0 | uint32_t count = |
41 | 0 | Preferences::GetUint("input_event_queue.count_for_prediction", |
42 | 0 | sInputCountForPrediction); |
43 | 0 |
|
44 | 0 | mLastInputDurations = |
45 | 0 | MakeUnique<TimeDurationCircularBuffer>(count, defaultDuration); |
46 | 0 |
|
47 | 0 | uint32_t maxDuration = |
48 | 0 | Preferences::GetUint("input_event_queue.duration.max", |
49 | 0 | sMaxReservedTimeForHandlingInput); |
50 | 0 |
|
51 | 0 | uint32_t minDuration = |
52 | 0 | Preferences::GetUint("input_event_queue.duration.min", |
53 | 0 | sMinReservedTimeForHandlingInput); |
54 | 0 |
|
55 | 0 | mMaxInputDuration = TimeDuration::FromMilliseconds(maxDuration); |
56 | 0 | mMinInputDuration = TimeDuration::FromMilliseconds(minDuration); |
57 | 0 | } |
58 | | |
59 | | TimeStamp |
60 | | InputEventStatistics::GetInputHandlingStartTime(uint32_t aInputCount) |
61 | 0 | { |
62 | 0 | MOZ_ASSERT(mEnable); |
63 | 0 | Maybe<TimeStamp> nextTickHint = nsRefreshDriver::GetNextTickHint(); |
64 | 0 |
|
65 | 0 | if (nextTickHint.isNothing()) { |
66 | 0 | // Return a past time to process input events immediately. |
67 | 0 | return TimeStamp::Now() - TimeDuration::FromMilliseconds(1); |
68 | 0 | } |
69 | 0 | TimeDuration inputCost = mLastInputDurations->GetMean() * aInputCount; |
70 | 0 | inputCost = inputCost > mMaxInputDuration |
71 | 0 | ? mMaxInputDuration |
72 | 0 | : inputCost < mMinInputDuration |
73 | 0 | ? mMinInputDuration |
74 | 0 | : inputCost; |
75 | 0 |
|
76 | 0 | return nextTickHint.value() - inputCost; |
77 | 0 | } |
78 | | |
79 | | } // namespace mozilla |