summaryrefslogtreecommitdiffstats
path: root/xpcom/threads/InputEventStatistics.cpp
blob: 66391d17cbae7c3397c68d8bb516eb5b524ee9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "InputEventStatistics.h"

#include "mozilla/Preferences.h"
#include "nsRefreshDriver.h"

namespace mozilla {

/*static*/ InputEventStatistics& InputEventStatistics::Get() {
  static UniquePtr<InputEventStatistics> sInstance;
  if (!sInstance) {
    sInstance = MakeUnique<InputEventStatistics>(ConstructorCookie());
    ClearOnShutdown(&sInstance);
  }
  return *sInstance;
}

TimeDuration InputEventStatistics::TimeDurationCircularBuffer::GetMean() {
  return mTotal / (int64_t)mSize;
}

InputEventStatistics::InputEventStatistics(ConstructorCookie&&)
    : mEnable(false) {
  MOZ_ASSERT(Preferences::IsServiceAvailable());
  uint32_t inputDuration = Preferences::GetUint(
      "input_event_queue.default_duration_per_event", sDefaultInputDuration);

  TimeDuration defaultDuration = TimeDuration::FromMilliseconds(inputDuration);

  uint32_t count = Preferences::GetUint(
      "input_event_queue.count_for_prediction", sInputCountForPrediction);

  mLastInputDurations =
      MakeUnique<TimeDurationCircularBuffer>(count, defaultDuration);

  uint32_t maxDuration = Preferences::GetUint("input_event_queue.duration.max",
                                              sMaxReservedTimeForHandlingInput);

  uint32_t minDuration = Preferences::GetUint("input_event_queue.duration.min",
                                              sMinReservedTimeForHandlingInput);

  mMaxInputDuration = TimeDuration::FromMilliseconds(maxDuration);
  mMinInputDuration = TimeDuration::FromMilliseconds(minDuration);
}

TimeStamp InputEventStatistics::GetInputHandlingStartTime(
    uint32_t aInputCount) {
  MOZ_ASSERT(mEnable);
  Maybe<TimeStamp> nextTickHint = nsRefreshDriver::GetNextTickHint();

  if (nextTickHint.isNothing()) {
    // Return a past time to process input events immediately.
    return TimeStamp::Now() - TimeDuration::FromMilliseconds(1);
  }
  TimeDuration inputCost = mLastInputDurations->GetMean() * aInputCount;
  inputCost = inputCost > mMaxInputDuration   ? mMaxInputDuration
              : inputCost < mMinInputDuration ? mMinInputDuration
                                              : inputCost;

  return nextTickHint.value() - inputCost;
}

}  // namespace mozilla