diff --git a/include/ui/Input.h b/include/ui/Input.h index 1ef36d007..fb6152e9f 100644 --- a/include/ui/Input.h +++ b/include/ui/Input.h @@ -608,6 +608,11 @@ private: // Oldest sample to consider when calculating the velocity. static const nsecs_t MAX_AGE = 200 * 1000000; // 200 ms + // When the total duration of the window of samples being averaged is less + // than the window size, the resulting velocity is scaled to reduce the impact + // of overestimation in short traces. + static const nsecs_t MIN_WINDOW = 100 * 1000000; // 100 ms + // The minimum duration between samples when estimating velocity. static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms diff --git a/libs/ui/Input.cpp b/libs/ui/Input.cpp index 0a53d69be..684c332d1 100644 --- a/libs/ui/Input.cpp +++ b/libs/ui/Input.cpp @@ -831,6 +831,7 @@ bool VelocityTracker::getVelocity(uint32_t id, float* outVx, float* outVy) const const Position& oldestPosition = oldestMovement.positions[oldestMovement.idBits.getIndexOfBit(id)]; nsecs_t lastDuration = 0; + while (numTouches-- > 1) { if (++index == HISTORY_SIZE) { index = 0; @@ -857,6 +858,14 @@ bool VelocityTracker::getVelocity(uint32_t id, float* outVx, float* outVy) const // Make sure we used at least one sample. if (samplesUsed != 0) { + // Scale the velocity linearly if the window of samples is small. + nsecs_t totalDuration = newestMovement.eventTime - oldestMovement.eventTime; + if (totalDuration < MIN_WINDOW) { + float scale = float(totalDuration) / float(MIN_WINDOW); + accumVx *= scale; + accumVy *= scale; + } + *outVx = accumVx; *outVy = accumVy; return true; diff --git a/libs/utils/Looper.cpp b/libs/utils/Looper.cpp index d5dd12606..b54fb9dd7 100644 --- a/libs/utils/Looper.cpp +++ b/libs/utils/Looper.cpp @@ -662,7 +662,8 @@ void Looper::wakeAndLock() { #endif void Looper::sendMessage(const sp& handler, const Message& message) { - sendMessageAtTime(LLONG_MIN, handler, message); + nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); + sendMessageAtTime(now, handler, message); } void Looper::sendMessageDelayed(nsecs_t uptimeDelay, const sp& handler,