From e37bcece75f83e921197fe4b784ee6ae977a6194 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Tue, 28 Sep 2021 14:24:32 -0700 Subject: Use constexpr in VelocityTracker Previously, defining DEBUG_STRATEGY as 1 would fail to compile. In this patch, the defines are changed to bools, in order to fix this going forward. Also, when I was porting the velocitytracker algorithm to python, I realized that one of the velocities was wrong. I updated the value. I didn't check other values, though. It was previously passing likely because the tolerance for velocity is quite large. Bug: 192439431 Test: atest libinput_tests Change-Id: I70b44af6cd5567ee8321b29e509c628fa0b4950e --- libs/input/VelocityTracker.cpp | 140 +++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 69 deletions(-) (limited to 'libs/input/VelocityTracker.cpp') diff --git a/libs/input/VelocityTracker.cpp b/libs/input/VelocityTracker.cpp index a44f0b7fe0..a6465eec24 100644 --- a/libs/input/VelocityTracker.cpp +++ b/libs/input/VelocityTracker.cpp @@ -18,10 +18,10 @@ //#define LOG_NDEBUG 0 // Log debug messages about velocity tracking. -#define DEBUG_VELOCITY 0 +static constexpr bool DEBUG_VELOCITY = false; // Log debug messages about the progress of the algorithm itself. -#define DEBUG_STRATEGY 0 +static constexpr bool DEBUG_STRATEGY = false; #include #include @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -64,7 +63,6 @@ static float vectorNorm(const float* a, uint32_t m) { return sqrtf(r); } -#if DEBUG_STRATEGY || DEBUG_VELOCITY static std::string vectorToString(const float* a, uint32_t m) { std::string str; str += "["; @@ -77,9 +75,11 @@ static std::string vectorToString(const float* a, uint32_t m) { str += " ]"; return str; } -#endif -#if DEBUG_STRATEGY +static std::string vectorToString(const std::vector& v) { + return vectorToString(v.data(), v.size()); +} + static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMajor) { std::string str; str = "["; @@ -99,7 +99,6 @@ static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool r str += " ]"; return str; } -#endif // --- VelocityTracker --- @@ -133,12 +132,18 @@ std::unique_ptr VelocityTracker::createStrategy( VelocityTracker::Strategy strategy) { switch (strategy) { case VelocityTracker::Strategy::IMPULSE: + if (DEBUG_STRATEGY) { + ALOGI("Initializing impulse strategy"); + } return std::make_unique(); case VelocityTracker::Strategy::LSQ1: return std::make_unique(1); case VelocityTracker::Strategy::LSQ2: + if (DEBUG_STRATEGY) { + ALOGI("Initializing lsq2 strategy"); + } return std::make_unique(2); case VelocityTracker::Strategy::LSQ3: @@ -204,10 +209,10 @@ void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits, if ((mCurrentPointerIdBits.value & idBits.value) && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) { -#if DEBUG_VELOCITY - ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.", - (eventTime - mLastEventTime) * 0.000001f); -#endif + if (DEBUG_VELOCITY) { + ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.", + (eventTime - mLastEventTime) * 0.000001f); + } // We have not received any movements for too long. Assume that all pointers // have stopped. mStrategy->clear(); @@ -221,24 +226,24 @@ void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits, mStrategy->addMovement(eventTime, idBits, positions); -#if DEBUG_VELOCITY - ALOGD("VelocityTracker: addMovement eventTime=%" PRId64 ", idBits=0x%08x, activePointerId=%d", - eventTime, idBits.value, mActivePointerId); - for (BitSet32 iterBits(idBits); !iterBits.isEmpty(); ) { - uint32_t id = iterBits.firstMarkedBit(); - uint32_t index = idBits.getIndexOfBit(id); - iterBits.clearBit(id); - Estimator estimator; - getEstimator(id, &estimator); - ALOGD(" %d: position (%0.3f, %0.3f), " - "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)", - id, positions[index].x, positions[index].y, - int(estimator.degree), - vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(), - vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(), - estimator.confidence); + if (DEBUG_VELOCITY) { + ALOGD("VelocityTracker: addMovement eventTime=%" PRId64 + ", idBits=0x%08x, activePointerId=%d", + eventTime, idBits.value, mActivePointerId); + for (BitSet32 iterBits(idBits); !iterBits.isEmpty();) { + uint32_t id = iterBits.firstMarkedBit(); + uint32_t index = idBits.getIndexOfBit(id); + iterBits.clearBit(id); + Estimator estimator; + getEstimator(id, &estimator); + ALOGD(" %d: position (%0.3f, %0.3f), " + "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)", + id, positions[index].x, positions[index].y, int(estimator.degree), + vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(), + vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(), + estimator.confidence); + } } -#endif } void VelocityTracker::addMovement(const MotionEvent* event) { @@ -419,11 +424,10 @@ void LeastSquaresVelocityTrackerStrategy::addMovement( static bool solveLeastSquares(const std::vector& x, const std::vector& y, const std::vector& w, uint32_t n, float* outB, float* outDet) { const size_t m = x.size(); -#if DEBUG_STRATEGY - ALOGD("solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n), - vectorToString(x, m).c_str(), vectorToString(y, m).c_str(), - vectorToString(w, m).c_str()); -#endif + if (DEBUG_STRATEGY) { + ALOGD("solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n), + vectorToString(x).c_str(), vectorToString(y).c_str(), vectorToString(w).c_str()); + } LOG_ALWAYS_FATAL_IF(m != y.size() || m != w.size(), "Mismatched vector sizes"); // Expand the X vector to a matrix A, pre-multiplied by the weights. @@ -434,9 +438,9 @@ static bool solveLeastSquares(const std::vector& x, const std::vector& x, const std::vector& x, const std::vector& x, const std::vector& x, const std::vector 0.000001f ? 1.0f - (sserr / sstot) : 1; -#if DEBUG_STRATEGY - ALOGD(" - sserr=%f", sserr); - ALOGD(" - sstot=%f", sstot); - ALOGD(" - det=%f", *outDet); -#endif + if (DEBUG_STRATEGY) { + ALOGD(" - sserr=%f", sserr); + ALOGD(" - sstot=%f", sstot); + ALOGD(" - det=%f", *outDet); + } return true; } @@ -655,13 +659,11 @@ bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id, outEstimator->time = newestMovement.eventTime; outEstimator->degree = degree; outEstimator->confidence = xdet * ydet; -#if DEBUG_STRATEGY - ALOGD("estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f", - int(outEstimator->degree), - vectorToString(outEstimator->xCoeff, n).c_str(), - vectorToString(outEstimator->yCoeff, n).c_str(), - outEstimator->confidence); -#endif + if (DEBUG_STRATEGY) { + ALOGD("estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f", + int(outEstimator->degree), vectorToString(outEstimator->xCoeff, n).c_str(), + vectorToString(outEstimator->yCoeff, n).c_str(), outEstimator->confidence); + } return true; } } @@ -1169,9 +1171,9 @@ bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id, outEstimator->time = newestMovement.eventTime; outEstimator->degree = 2; // similar results to 2nd degree fit outEstimator->confidence = 1; -#if DEBUG_STRATEGY - ALOGD("velocity: (%f, %f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]); -#endif + if (DEBUG_STRATEGY) { + ALOGD("velocity: (%f, %f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]); + } return true; } -- cgit v1.2.3-59-g8ed1b From 7128e90d5bc5b258e5d4e48421c649aa024c454b Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Thu, 17 Mar 2022 09:43:28 -0700 Subject: Add dynamic debug logs to VelocityTracker This will allow us to debug any issues related to the switch to the 'impulse' velocitytracker algorithm. In addition, print the lsq2 velocity values for the case when 'impulse' is selected, and when debugging is enabled. This will make it simpler for us to follow up on dogfooder's feedback. Bug: 134179997 Test: atest libinput_tests:VelocityTrackerTest#ThreePointsLinearVelocityTest Test: (enable debug logs and make sure the impulse and ... Test: lsq2 velocities match for the test case above) Change-Id: I3c523b67a0cebe8adaddba8f86d335a2f8f4d8f8 (cherry picked from commit 276467bdf6dc1ce1f3fa9623127e4d298bbc0f2d) --- libs/input/VelocityTracker.cpp | 50 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'libs/input/VelocityTracker.cpp') diff --git a/libs/input/VelocityTracker.cpp b/libs/input/VelocityTracker.cpp index a6465eec24..7f427f2364 100644 --- a/libs/input/VelocityTracker.cpp +++ b/libs/input/VelocityTracker.cpp @@ -15,13 +15,6 @@ */ #define LOG_TAG "VelocityTracker" -//#define LOG_NDEBUG 0 - -// Log debug messages about velocity tracking. -static constexpr bool DEBUG_VELOCITY = false; - -// Log debug messages about the progress of the algorithm itself. -static constexpr bool DEBUG_STRATEGY = false; #include #include @@ -36,6 +29,27 @@ static constexpr bool DEBUG_STRATEGY = false; namespace android { +/** + * Log debug messages about velocity tracking. + * Enable this via "adb shell setprop log.tag.VelocityTrackerVelocity DEBUG" (requires restart) + */ +const bool DEBUG_VELOCITY = + __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Velocity", ANDROID_LOG_INFO); + +/** + * Log debug messages about the progress of the algorithm itself. + * Enable this via "adb shell setprop log.tag.VelocityTrackerStrategy DEBUG" (requires restart) + */ +const bool DEBUG_STRATEGY = + __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Strategy", ANDROID_LOG_INFO); + +/** + * Log debug messages about the 'impulse' strategy. + * Enable this via "adb shell setprop log.tag.VelocityTrackerImpulse DEBUG" (requires restart) + */ +const bool DEBUG_IMPULSE = + __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Impulse", ANDROID_LOG_INFO); + // Nanoseconds per milliseconds. static const nsecs_t NANOS_PER_MS = 1000000; @@ -141,7 +155,7 @@ std::unique_ptr VelocityTracker::createStrategy( return std::make_unique(1); case VelocityTracker::Strategy::LSQ2: - if (DEBUG_STRATEGY) { + if (DEBUG_STRATEGY && !DEBUG_IMPULSE) { ALOGI("Initializing lsq2 strategy"); } return std::make_unique(2); @@ -1172,7 +1186,25 @@ bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id, outEstimator->degree = 2; // similar results to 2nd degree fit outEstimator->confidence = 1; if (DEBUG_STRATEGY) { - ALOGD("velocity: (%f, %f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]); + ALOGD("velocity: (%.1f, %.1f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]); + } + if (DEBUG_IMPULSE) { + // TODO(b/134179997): delete this block once the switch to 'impulse' is complete. + // Calculate the lsq2 velocity for the same inputs to allow runtime comparisons + VelocityTracker lsq2(VelocityTracker::Strategy::LSQ2); + BitSet32 idBits; + const uint32_t pointerId = 0; + idBits.markBit(pointerId); + for (ssize_t i = m - 1; i >= 0; i--) { + lsq2.addMovement(time[i], idBits, {{x[i], y[i]}}); + } + float outVx = 0, outVy = 0; + const bool computed = lsq2.getVelocity(pointerId, &outVx, &outVy); + if (computed) { + ALOGD("lsq2 velocity: (%.1f, %.1f)", outVx, outVy); + } else { + ALOGD("lsq2 velocity: could not compute velocity"); + } } return true; } -- cgit v1.2.3-59-g8ed1b