blob: 9caf4c5ecb5c910aa08bed834fe0ec1dfb156716 [file]
/**
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "LegacyResampler"
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <map>
#include <ostream>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <ftl/enum.h>
#include <com_android_input_flags.h>
#include <input/Resampler.h>
#include <utils/Timers.h>
namespace android {
namespace {
const bool IS_DEBUGGABLE_BUILD =
#if defined(__ANDROID__)
android::base::GetBoolProperty("ro.debuggable", false);
#else
true;
#endif
/**
* Log debug messages about timestamp and coordinates of event resampling.
* Enable this via "adb shell setprop log.tag.LegacyResamplerResampling DEBUG"
* (requires restart)
*/
bool debugResampling() {
if (!IS_DEBUGGABLE_BUILD) {
static const bool DEBUG_TRANSPORT_RESAMPLING =
__android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling",
ANDROID_LOG_INFO);
return DEBUG_TRANSPORT_RESAMPLING;
}
return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
}
using std::chrono::nanoseconds;
constexpr std::chrono::milliseconds RESAMPLE_LATENCY{5};
constexpr std::chrono::milliseconds RESAMPLE_MIN_DELTA{2};
constexpr std::chrono::milliseconds RESAMPLE_MAX_DELTA{20};
constexpr std::chrono::milliseconds RESAMPLE_MAX_PREDICTION{8};
bool canResampleTool(ToolType toolType) {
return toolType == ToolType::FINGER || toolType == ToolType::MOUSE ||
toolType == ToolType::STYLUS || toolType == ToolType::UNKNOWN;
}
inline float lerp(float a, float b, float alpha) {
return a + alpha * (b - a);
}
// These axis values are relative to the previous event. When synthesizing a resampled event,
// they are reset to their default values, so that consumers of these axis values can correctly
// accumulate the delta.
// Note that they are not cleared in `LegacyResampler::overwriteMotionEventSamples` because the
// overwritten event there is a real event containing delta from the last real event.
const std::map<int32_t, float> relativeAxesDefaultValues = {
{AMOTION_EVENT_AXIS_RELATIVE_X, 0.0f},
{AMOTION_EVENT_AXIS_RELATIVE_Y, 0.0f},
{AMOTION_EVENT_AXIS_GESTURE_X_OFFSET, 0.0f},
{AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET, 0.0f},
{AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE, 0.0f},
{AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE, 0.0f},
// Scale factor is multiplicative, so reset to 1.0.
{AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR, 1.0f},
};
PointerCoords calculateResampledCoords(const PointerCoords& a, const PointerCoords& b,
float alpha) {
// We use the value of alpha to initialize resampledCoords with the latest sample information.
PointerCoords resampledCoords = (alpha < 1.0f) ? a : b;
resampledCoords.isResampled = true;
resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, lerp(a.getX(), b.getX(), alpha));
resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, lerp(a.getY(), b.getY(), alpha));
for (const auto& [axis, defaultValue] : relativeAxesDefaultValues) {
resampledCoords.setAxisValue(axis, defaultValue);
}
return resampledCoords;
}
bool equalXY(const PointerCoords& a, const PointerCoords& b) {
return (a.getX() == b.getX()) && (a.getY() == b.getY());
}
void setMotionEventPointerCoords(MotionEvent& motionEvent, size_t sampleIndex, size_t pointerIndex,
const PointerCoords& pointerCoords) {
// Ideally, we should not cast away const. In this particular case, it's safe to cast away const
// and dereference getHistoricalRawPointerCoords returned pointer because motionEvent is a
// nonconst reference to a MotionEvent object, so mutating the object should not be undefined
// behavior; moreover, the invoked method guarantees to return a valid pointer. Otherwise, it
// fatally logs. Alternatively, we could've created a new MotionEvent from scratch, but this
// approach is simpler and more efficient.
PointerCoords& motionEventCoords = const_cast<PointerCoords&>(
*(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex)));
motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_X, pointerCoords.getX());
motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, pointerCoords.getY());
motionEventCoords.isResampled = pointerCoords.isResampled;
}
std::ostream& operator<<(std::ostream& os, const PointerCoords& pointerCoords) {
os << "(" << pointerCoords.getX() << ", " << pointerCoords.getY() << ")";
return os;
}
} // namespace
LegacyResampler::LegacyResampler(const InputMessage& initialMsg) {
mLatestSamples.pushBack(messageToSample(initialMsg));
}
void LegacyResampler::updateLatestSamples(const MotionEvent& motionEvent) {
const size_t numSamples = motionEvent.getHistorySize() + 1;
const size_t latestIndex = numSamples - 1;
const size_t secondToLatestIndex = (latestIndex > 0) ? (latestIndex - 1) : 0;
for (size_t sampleIndex = secondToLatestIndex; sampleIndex < numSamples; ++sampleIndex) {
PointerMap pointerMap;
for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount();
++pointerIndex) {
pointerMap.insert(Pointer{*(motionEvent.getPointerProperties(pointerIndex)),
*(motionEvent.getHistoricalRawPointerCoords(pointerIndex,
sampleIndex))});
}
mLatestSamples.pushBack(
Sample{nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)}, pointerMap});
}
}
LegacyResampler::Sample LegacyResampler::messageToSample(const InputMessage& message) {
PointerMap pointerMap;
for (uint32_t i = 0; i < message.body.motion.pointerCount; ++i) {
pointerMap.insert(Pointer{message.body.motion.pointers[i].properties,
message.body.motion.pointers[i].coords});
}
return Sample{nanoseconds{message.body.motion.eventTime}, pointerMap};
}
bool LegacyResampler::pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary) {
for (const Pointer& pointer : target.pointerMap) {
const std::optional<Pointer> auxiliaryPointer =
auxiliary.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
if (!auxiliaryPointer.has_value()) {
LOG_IF(INFO, debugResampling())
<< "Not resampled. Auxiliary sample does not contain all pointers from target.";
return false;
}
if (pointer.properties.toolType != auxiliaryPointer->properties.toolType) {
LOG_IF(INFO, debugResampling()) << "Not resampled. Pointer ToolType mismatch.";
return false;
}
if (!canResampleTool(pointer.properties.toolType)) {
LOG_IF(INFO, debugResampling())
<< "Not resampled. Cannot resample "
<< ftl::enum_string(pointer.properties.toolType) << " ToolType.";
return false;
}
}
return true;
}
bool LegacyResampler::canInterpolate(const InputMessage& message) const {
LOG_IF(FATAL, mLatestSamples.empty())
<< "Not resampled. mLatestSamples must not be empty to interpolate.";
const Sample& pastSample = *(mLatestSamples.end() - 1);
const Sample& futureSample = messageToSample(message);
if (!pointerPropertiesResampleable(pastSample, futureSample)) {
return false;
}
const nanoseconds delta = futureSample.eventTime - pastSample.eventTime;
if (delta < RESAMPLE_MIN_DELTA) {
LOG_IF(INFO, debugResampling())
<< "Not resampled. Delta is too small: " << std::setprecision(3)
<< std::chrono::duration<double, std::milli>{delta}.count() << "ms";
return false;
}
return true;
}
/**
* Returns the coordinates from the previous prediction for a pointer if it has been
* stationary.
*
* A pointer is considered stationary if its X and Y coordinates are identical in the last two
* samples of `mLatestSamples`. If this condition is met, and the pointer exists in
* `mPreviousPrediction`, this function returns the coordinates from `mPreviousPrediction`.
*
* This helps prevent jumps when a pointer stops moving but extrapolation had already
* predicted it further.
*
* Returns `std::nullopt` if:
* - There is no `mPreviousPrediction`.
* - `mLatestSamples` has fewer than 2 samples.
* - The pointer is missing from either of the last two samples in `mLatestSamples` or from
* `mPreviousPrediction`.
* - The pointer's coordinates differ between the last two samples in `mLatestSamples`.
*/
std::optional<PointerCoords> LegacyResampler::getStationaryPrediction(int32_t id) const {
// Reuses prediction if pointer stayed stationary between the last two raw samples.
if (!mPreviousPrediction.has_value() || !mResampleValidPointers.test(id)) {
return std::nullopt;
}
if (mLatestSamples.size() < 2) {
return std::nullopt;
}
const Sample& sample2 = *(mLatestSamples.end() - 2);
const Sample& sample1 = *(mLatestSamples.end() - 1);
const auto p2 = sample2.pointerMap.find(PointerMap::PointerId{id});
const auto p1 = sample1.pointerMap.find(PointerMap::PointerId{id});
if (!p2.has_value() || !p1.has_value()) {
return std::nullopt;
}
if (!equalXY(p2->coords, p1->coords)) {
return std::nullopt;
}
const auto predPointer = mPreviousPrediction->pointerMap.find(PointerMap::PointerId{id});
if (!predPointer.has_value()) {
return std::nullopt;
}
LOG_IF(INFO, debugResampling())
<< "Pointer ID: " << id << " stationary. Reusing prediction: ("
<< predPointer->coords.getX() << ", " << predPointer->coords.getY() << ")";
return predPointer->coords;
}
std::optional<LegacyResampler::Sample> LegacyResampler::attemptInterpolation(
nanoseconds resampleTime, const InputMessage& futureMessage) const {
if (!canInterpolate(futureMessage)) {
return std::nullopt;
}
LOG_IF(FATAL, mLatestSamples.empty())
<< "Not resampled. mLatestSamples must not be empty to interpolate.";
const Sample& pastSample = *(mLatestSamples.end() - 1);
const Sample& futureSample = messageToSample(futureMessage);
const nanoseconds delta = nanoseconds{futureSample.eventTime} - pastSample.eventTime;
const float alpha =
std::chrono::duration<float, std::nano>(resampleTime - pastSample.eventTime) / delta;
PointerMap resampledPointerMap;
for (const Pointer& pointer : pastSample.pointerMap) {
if (std::optional<Pointer> futureSamplePointer =
futureSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
futureSamplePointer.has_value()) {
if (const auto stationaryCoords = getStationaryPrediction(pointer.properties.id);
stationaryCoords.has_value()) {
// Reuse prediction for stationary pointers.
resampledPointerMap.insert(Pointer{pointer.properties, *stationaryCoords});
continue;
}
const PointerCoords& resampledCoords =
calculateResampledCoords(pointer.coords, futureSamplePointer->coords, alpha);
resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords});
}
}
return Sample{resampleTime, resampledPointerMap};
}
bool LegacyResampler::canExtrapolate() const {
if (mLatestSamples.size() < 2) {
LOG_IF(INFO, debugResampling()) << "Not resampled. Not enough data.";
return false;
}
const Sample& pastSample = *(mLatestSamples.end() - 2);
const Sample& presentSample = *(mLatestSamples.end() - 1);
if (!pointerPropertiesResampleable(presentSample, pastSample)) {
return false;
}
const nanoseconds delta = presentSample.eventTime - pastSample.eventTime;
if (delta < RESAMPLE_MIN_DELTA) {
LOG_IF(INFO, debugResampling())
<< "Not resampled. Delta is too small: " << std::setprecision(3)
<< std::chrono::duration<double, std::milli>{delta}.count() << "ms";
return false;
} else if (delta > RESAMPLE_MAX_DELTA) {
LOG_IF(INFO, debugResampling())
<< "Not resampled. Delta is too large: " << std::setprecision(3)
<< std::chrono::duration<double, std::milli>{delta}.count() << "ms";
return false;
}
return true;
}
std::optional<LegacyResampler::Sample> LegacyResampler::attemptExtrapolation(
nanoseconds resampleTime) const {
if (!canExtrapolate()) {
return std::nullopt;
}
LOG_IF(FATAL, mLatestSamples.size() < 2)
<< "Not resampled. mLatestSamples must have at least two samples to extrapolate.";
const Sample& pastSample = *(mLatestSamples.end() - 2);
const Sample& presentSample = *(mLatestSamples.end() - 1);
const nanoseconds delta = presentSample.eventTime - pastSample.eventTime;
// The farthest future time to which we can extrapolate. If the given resampleTime exceeds this,
// we use this value as the resample time target.
const nanoseconds farthestPrediction =
presentSample.eventTime + std::min<nanoseconds>(delta / 2, RESAMPLE_MAX_PREDICTION);
const nanoseconds newResampleTime =
(resampleTime > farthestPrediction) ? (farthestPrediction) : (resampleTime);
LOG_IF(INFO, debugResampling() && newResampleTime == farthestPrediction)
<< "Resample time is too far in the future. Adjusting prediction from "
<< std::setprecision(3)
<< std::chrono::duration<double, std::milli>{resampleTime - presentSample.eventTime}
.count()
<< "ms to "
<< std::chrono::duration<double, std::milli>{farthestPrediction -
presentSample.eventTime}
.count()
<< "ms";
const float alpha =
std::chrono::duration<float, std::nano>(newResampleTime - pastSample.eventTime) / delta;
PointerMap resampledPointerMap;
for (const Pointer& pointer : presentSample.pointerMap) {
if (std::optional<Pointer> pastSamplePointer =
pastSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
pastSamplePointer.has_value()) {
if (const auto stationaryCoords = getStationaryPrediction(pointer.properties.id);
stationaryCoords.has_value()) {
// Reuse prediction for stationary pointers.
resampledPointerMap.insert(Pointer{pointer.properties, *stationaryCoords});
continue;
}
const PointerCoords& resampledCoords =
calculateResampledCoords(pastSamplePointer->coords, pointer.coords, alpha);
resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords});
}
}
return Sample{newResampleTime, resampledPointerMap};
}
inline void LegacyResampler::addSampleToMotionEvent(const Sample& sample,
MotionEvent& motionEvent) {
motionEvent.addSample(sample.eventTime.count(), sample.asPointerCoords().data(),
motionEvent.getId());
}
nanoseconds LegacyResampler::getResampleLatency() const {
return RESAMPLE_LATENCY;
}
/**
* The resampler is unaware of ACTION_DOWN. Thus, it needs to constantly check for pointer IDs
* occurrences. This problem could be fixed if the resampler has access to the entire stream of
* MotionEvent actions. That way, both ACTION_DOWN and ACTION_UP will be visible; therefore,
* facilitating pointer tracking between samples.
*/
void LegacyResampler::overwriteMotionEventSamples(MotionEvent& motionEvent) {
if (!mPreviousPrediction.has_value()) {
LOG_IF(INFO, debugResampling()) << "Still pointers not overwritten. Not enough data.";
return;
}
for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); ++pointerIndex) {
const uint32_t id = motionEvent.getPointerId(pointerIndex);
// Resolve pointer properties from preceding batch and the previous prediction.
const std::optional<Pointer> lastRealPointer = mLastRealSample.has_value()
? mLastRealSample->pointerMap.find(PointerMap::PointerId{static_cast<int32_t>(id)})
: std::nullopt;
const std::optional<Pointer> previousPointer = mPreviousPrediction->pointerMap.find(
PointerMap::PointerId{static_cast<int32_t>(id)});
// Skip pointers without active prediction (no trajectory to smooth against).
if (!previousPointer.has_value()) {
continue;
}
// Store the raw coordinates of the previous sample, to allow us to detect stillness.
const std::optional<PointerCoords> rawPrevCoords =
lastRealPointer ? std::make_optional(lastRealPointer->coords) : std::nullopt;
overwritePointerSamples(motionEvent, pointerIndex, id, previousPointer->coords,
rawPrevCoords);
}
}
void LegacyResampler::overwritePointerSamples(MotionEvent& motionEvent, size_t pointerIndex,
uint32_t id, const PointerCoords& previousCoords,
std::optional<PointerCoords> rawPrevCoords) {
const size_t numSamples = motionEvent.getHistorySize() + 1;
for (size_t sampleIndex = 0; sampleIndex < numSamples; ++sampleIndex) {
const PointerCoords& pointerCoords =
*(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex));
PointerCoords currentRawCoords = pointerCoords;
// Smooth historical pointer coordinates using the previous prediction to prevent jumps
// when a stationary pointer has a valid prediction bit.
if (mResampleValidPointers.test(id)) {
const bool isOlderThanPrediction =
nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)} <
mPreviousPrediction->eventTime;
if (isOlderThanPrediction ||
(rawPrevCoords.has_value() && equalXY(pointerCoords, *rawPrevCoords))) {
LOG_IF(INFO, debugResampling())
<< "Pointer ID: " << id
<< " did not move. Overwriting its coordinates from " << pointerCoords
<< " to " << previousCoords;
setMotionEventPointerCoords(motionEvent, sampleIndex, pointerIndex, previousCoords);
} else {
// The pointer has started moving again. Clear its prediction bit to stop
// history overwriting until the next valid prediction pass.
mResampleValidPointers.reset(id);
}
}
rawPrevCoords = currentRawCoords;
}
}
void LegacyResampler::resampleWithFrameTime(nanoseconds frameTime, MotionEvent& motionEvent,
const InputMessage* futureSample) {
const nanoseconds resampleTime = frameTime - RESAMPLE_LATENCY;
if (mPreviousPrediction.has_value()) {
overwriteMotionEventSamples(motionEvent);
}
if (resampleTime.count() == motionEvent.getEventTime()) {
LOG_IF(INFO, debugResampling()) << "Not resampled. Resample time equals motion event time.";
return;
}
const std::optional<Sample> sample = (futureSample != nullptr)
? (attemptInterpolation(resampleTime, *futureSample))
: (attemptExtrapolation(resampleTime));
if (sample.has_value()) {
addSampleToMotionEvent(*sample, motionEvent);
mPreviousPrediction = sample;
mResampleValidPointers.reset();
for (const auto& pointer : sample->pointerMap) {
mResampleValidPointers.set(pointer.properties.id);
}
}
}
void LegacyResampler::resampleWithoutFrameTime(MotionEvent& motionEvent) {
LOG_IF(INFO, debugResampling())
<< "resampleMotionEvent (flush) called: action=" << motionEvent.getAction();
if (!mPreviousPrediction.has_value()) {
return;
}
const int32_t action = motionEvent.getAction() & AMOTION_EVENT_ACTION_MASK;
const bool isUpEvent = (action == AMOTION_EVENT_ACTION_UP);
const bool isPointerUpEvent = (action == AMOTION_EVENT_ACTION_POINTER_UP);
// Rewrite terminal UP coordinates to align with the last prediction under flag.
bool shouldFixUp = com::android::input::flags::fix_action_up_resampling() &&
(isUpEvent || isPointerUpEvent);
LOG_IF(INFO, debugResampling())
<< "shouldFixUp=" << shouldFixUp
<< ", mPreviousPrediction time=" << mPreviousPrediction->eventTime.count();
if (shouldFixUp) {
for (size_t i = 0; i < motionEvent.getPointerCount(); ++i) {
const uint32_t id = motionEvent.getPointerId(i);
const auto previousPointer = mPreviousPrediction->pointerMap.find(
PointerMap::PointerId{static_cast<int32_t>(id)});
if (previousPointer.has_value() && mResampleValidPointers.test(id)) {
LOG_IF(INFO, debugResampling()) << "Rewriting ACTION_UP pointer " << id << " to ("
<< previousPointer->coords.getX() << ", "
<< previousPointer->coords.getY() << ")";
setMotionEventPointerCoords(motionEvent, 0, i, previousPointer->coords);
}
}
} else {
overwriteMotionEventSamples(motionEvent);
}
}
void LegacyResampler::resampleMotionEvent(std::optional<nanoseconds> frameTime,
MotionEvent& motionEvent,
const InputMessage* futureSample) {
updateLatestSamples(motionEvent);
if (frameTime.has_value()) {
resampleWithFrameTime(*frameTime, motionEvent, futureSample);
} else {
resampleWithoutFrameTime(motionEvent);
}
LOG_IF(FATAL, mLatestSamples.empty()) << "mLatestSamples must contain at least one sample.";
mLastRealSample = *(mLatestSamples.end() - 1);
}
// --- FilteredLegacyResampler ---
FilteredLegacyResampler::FilteredLegacyResampler(const InputMessage& initialMsg,
float minCutoffFreq, float beta)
: mResampler(initialMsg), mMinCutoffFreq(minCutoffFreq), mBeta(beta) {}
void FilteredLegacyResampler::resampleMotionEvent(
std::optional<std::chrono::nanoseconds> requestedFrameTime, MotionEvent& motionEvent,
const InputMessage* futureSample) {
mResampler.resampleMotionEvent(requestedFrameTime, motionEvent, futureSample);
const size_t numSamples = motionEvent.getHistorySize() + 1;
for (size_t sampleIndex = 0; sampleIndex < numSamples; ++sampleIndex) {
for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount();
++pointerIndex) {
const int32_t pointerId = motionEvent.getPointerProperties(pointerIndex)->id;
const nanoseconds eventTime =
nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)};
// Refer to the static function `setMotionEventPointerCoords` for a justification of
// casting away const.
PointerCoords& pointerCoords = const_cast<PointerCoords&>(
*(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex)));
const auto& [iter, _] = mFilteredPointers.try_emplace(pointerId, mMinCutoffFreq, mBeta);
iter->second.filter(eventTime, pointerCoords);
}
}
}
std::chrono::nanoseconds FilteredLegacyResampler::getResampleLatency() const {
return mResampler.getResampleLatency();
}
} // namespace android