2017-03-21 10:44:12 +03:00
|
|
|
/* -*- 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 "nsRefreshDriver.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
TimeDuration
|
|
|
|
InputEventStatistics::TimeDurationCircularBuffer::GetMean()
|
|
|
|
{
|
|
|
|
return mTotal / (int64_t)mSize;
|
|
|
|
}
|
|
|
|
|
2017-09-20 03:38:05 +03:00
|
|
|
InputEventStatistics::InputEventStatistics(ConstructorCookie&&)
|
2017-03-21 10:44:12 +03:00
|
|
|
: mEnable(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Preferences::IsServiceAvailable());
|
|
|
|
uint32_t inputDuration =
|
2017-07-27 11:20:01 +03:00
|
|
|
Preferences::GetUint("input_event_queue.default_duration_per_event",
|
2017-03-21 10:44:12 +03:00
|
|
|
sDefaultInputDuration);
|
|
|
|
|
|
|
|
TimeDuration defaultDuration = TimeDuration::FromMilliseconds(inputDuration);
|
|
|
|
|
|
|
|
uint32_t count =
|
2017-07-27 11:20:01 +03:00
|
|
|
Preferences::GetUint("input_event_queue.count_for_prediction",
|
2017-03-21 10:44:12 +03:00
|
|
|
sInputCountForPrediction);
|
|
|
|
|
|
|
|
mLastInputDurations =
|
|
|
|
MakeUnique<TimeDurationCircularBuffer>(count, defaultDuration);
|
|
|
|
|
|
|
|
uint32_t maxDuration =
|
2017-07-27 11:20:01 +03:00
|
|
|
Preferences::GetUint("input_event_queue.duration.max",
|
2017-03-21 10:44:12 +03:00
|
|
|
sMaxReservedTimeForHandlingInput);
|
|
|
|
|
|
|
|
uint32_t minDuration =
|
2017-07-27 11:20:01 +03:00
|
|
|
Preferences::GetUint("input_event_queue.duration.min",
|
2017-03-21 10:44:12 +03:00
|
|
|
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
|