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