1*38e8c45fSAndroid Build Coastguard Worker /**
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2024 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "LegacyResampler"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <algorithm>
20*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
21*38e8c45fSAndroid Build Coastguard Worker #include <iomanip>
22*38e8c45fSAndroid Build Coastguard Worker #include <ostream>
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <android-base/properties.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <ftl/enum.h>
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker #include <input/Resampler.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
30*38e8c45fSAndroid Build Coastguard Worker
31*38e8c45fSAndroid Build Coastguard Worker namespace android {
32*38e8c45fSAndroid Build Coastguard Worker namespace {
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker const bool IS_DEBUGGABLE_BUILD =
35*38e8c45fSAndroid Build Coastguard Worker #if defined(__ANDROID__)
36*38e8c45fSAndroid Build Coastguard Worker android::base::GetBoolProperty("ro.debuggable", false);
37*38e8c45fSAndroid Build Coastguard Worker #else
38*38e8c45fSAndroid Build Coastguard Worker true;
39*38e8c45fSAndroid Build Coastguard Worker #endif
40*38e8c45fSAndroid Build Coastguard Worker
41*38e8c45fSAndroid Build Coastguard Worker /**
42*38e8c45fSAndroid Build Coastguard Worker * Log debug messages about timestamp and coordinates of event resampling.
43*38e8c45fSAndroid Build Coastguard Worker * Enable this via "adb shell setprop log.tag.LegacyResamplerResampling DEBUG"
44*38e8c45fSAndroid Build Coastguard Worker * (requires restart)
45*38e8c45fSAndroid Build Coastguard Worker */
debugResampling()46*38e8c45fSAndroid Build Coastguard Worker bool debugResampling() {
47*38e8c45fSAndroid Build Coastguard Worker if (!IS_DEBUGGABLE_BUILD) {
48*38e8c45fSAndroid Build Coastguard Worker static const bool DEBUG_TRANSPORT_RESAMPLING =
49*38e8c45fSAndroid Build Coastguard Worker __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling",
50*38e8c45fSAndroid Build Coastguard Worker ANDROID_LOG_INFO);
51*38e8c45fSAndroid Build Coastguard Worker return DEBUG_TRANSPORT_RESAMPLING;
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
54*38e8c45fSAndroid Build Coastguard Worker }
55*38e8c45fSAndroid Build Coastguard Worker
56*38e8c45fSAndroid Build Coastguard Worker using std::chrono::nanoseconds;
57*38e8c45fSAndroid Build Coastguard Worker
58*38e8c45fSAndroid Build Coastguard Worker constexpr std::chrono::milliseconds RESAMPLE_LATENCY{5};
59*38e8c45fSAndroid Build Coastguard Worker
60*38e8c45fSAndroid Build Coastguard Worker constexpr std::chrono::milliseconds RESAMPLE_MIN_DELTA{2};
61*38e8c45fSAndroid Build Coastguard Worker
62*38e8c45fSAndroid Build Coastguard Worker constexpr std::chrono::milliseconds RESAMPLE_MAX_DELTA{20};
63*38e8c45fSAndroid Build Coastguard Worker
64*38e8c45fSAndroid Build Coastguard Worker constexpr std::chrono::milliseconds RESAMPLE_MAX_PREDICTION{8};
65*38e8c45fSAndroid Build Coastguard Worker
canResampleTool(ToolType toolType)66*38e8c45fSAndroid Build Coastguard Worker bool canResampleTool(ToolType toolType) {
67*38e8c45fSAndroid Build Coastguard Worker return toolType == ToolType::FINGER || toolType == ToolType::MOUSE ||
68*38e8c45fSAndroid Build Coastguard Worker toolType == ToolType::STYLUS || toolType == ToolType::UNKNOWN;
69*38e8c45fSAndroid Build Coastguard Worker }
70*38e8c45fSAndroid Build Coastguard Worker
lerp(float a,float b,float alpha)71*38e8c45fSAndroid Build Coastguard Worker inline float lerp(float a, float b, float alpha) {
72*38e8c45fSAndroid Build Coastguard Worker return a + alpha * (b - a);
73*38e8c45fSAndroid Build Coastguard Worker }
74*38e8c45fSAndroid Build Coastguard Worker
calculateResampledCoords(const PointerCoords & a,const PointerCoords & b,float alpha)75*38e8c45fSAndroid Build Coastguard Worker PointerCoords calculateResampledCoords(const PointerCoords& a, const PointerCoords& b,
76*38e8c45fSAndroid Build Coastguard Worker float alpha) {
77*38e8c45fSAndroid Build Coastguard Worker // We use the value of alpha to initialize resampledCoords with the latest sample information.
78*38e8c45fSAndroid Build Coastguard Worker PointerCoords resampledCoords = (alpha < 1.0f) ? a : b;
79*38e8c45fSAndroid Build Coastguard Worker resampledCoords.isResampled = true;
80*38e8c45fSAndroid Build Coastguard Worker resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, lerp(a.getX(), b.getX(), alpha));
81*38e8c45fSAndroid Build Coastguard Worker resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, lerp(a.getY(), b.getY(), alpha));
82*38e8c45fSAndroid Build Coastguard Worker return resampledCoords;
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker
equalXY(const PointerCoords & a,const PointerCoords & b)85*38e8c45fSAndroid Build Coastguard Worker bool equalXY(const PointerCoords& a, const PointerCoords& b) {
86*38e8c45fSAndroid Build Coastguard Worker return (a.getX() == b.getX()) && (a.getY() == b.getY());
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker
setMotionEventPointerCoords(MotionEvent & motionEvent,size_t sampleIndex,size_t pointerIndex,const PointerCoords & pointerCoords)89*38e8c45fSAndroid Build Coastguard Worker void setMotionEventPointerCoords(MotionEvent& motionEvent, size_t sampleIndex, size_t pointerIndex,
90*38e8c45fSAndroid Build Coastguard Worker const PointerCoords& pointerCoords) {
91*38e8c45fSAndroid Build Coastguard Worker // Ideally, we should not cast away const. In this particular case, it's safe to cast away const
92*38e8c45fSAndroid Build Coastguard Worker // and dereference getHistoricalRawPointerCoords returned pointer because motionEvent is a
93*38e8c45fSAndroid Build Coastguard Worker // nonconst reference to a MotionEvent object, so mutating the object should not be undefined
94*38e8c45fSAndroid Build Coastguard Worker // behavior; moreover, the invoked method guarantees to return a valid pointer. Otherwise, it
95*38e8c45fSAndroid Build Coastguard Worker // fatally logs. Alternatively, we could've created a new MotionEvent from scratch, but this
96*38e8c45fSAndroid Build Coastguard Worker // approach is simpler and more efficient.
97*38e8c45fSAndroid Build Coastguard Worker PointerCoords& motionEventCoords = const_cast<PointerCoords&>(
98*38e8c45fSAndroid Build Coastguard Worker *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex)));
99*38e8c45fSAndroid Build Coastguard Worker motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_X, pointerCoords.getX());
100*38e8c45fSAndroid Build Coastguard Worker motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, pointerCoords.getY());
101*38e8c45fSAndroid Build Coastguard Worker motionEventCoords.isResampled = pointerCoords.isResampled;
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker
operator <<(std::ostream & os,const PointerCoords & pointerCoords)104*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const PointerCoords& pointerCoords) {
105*38e8c45fSAndroid Build Coastguard Worker os << "(" << pointerCoords.getX() << ", " << pointerCoords.getY() << ")";
106*38e8c45fSAndroid Build Coastguard Worker return os;
107*38e8c45fSAndroid Build Coastguard Worker }
108*38e8c45fSAndroid Build Coastguard Worker
109*38e8c45fSAndroid Build Coastguard Worker } // namespace
110*38e8c45fSAndroid Build Coastguard Worker
updateLatestSamples(const MotionEvent & motionEvent)111*38e8c45fSAndroid Build Coastguard Worker void LegacyResampler::updateLatestSamples(const MotionEvent& motionEvent) {
112*38e8c45fSAndroid Build Coastguard Worker const size_t numSamples = motionEvent.getHistorySize() + 1;
113*38e8c45fSAndroid Build Coastguard Worker const size_t latestIndex = numSamples - 1;
114*38e8c45fSAndroid Build Coastguard Worker const size_t secondToLatestIndex = (latestIndex > 0) ? (latestIndex - 1) : 0;
115*38e8c45fSAndroid Build Coastguard Worker for (size_t sampleIndex = secondToLatestIndex; sampleIndex < numSamples; ++sampleIndex) {
116*38e8c45fSAndroid Build Coastguard Worker PointerMap pointerMap;
117*38e8c45fSAndroid Build Coastguard Worker for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount();
118*38e8c45fSAndroid Build Coastguard Worker ++pointerIndex) {
119*38e8c45fSAndroid Build Coastguard Worker pointerMap.insert(Pointer{*(motionEvent.getPointerProperties(pointerIndex)),
120*38e8c45fSAndroid Build Coastguard Worker *(motionEvent.getHistoricalRawPointerCoords(pointerIndex,
121*38e8c45fSAndroid Build Coastguard Worker sampleIndex))});
122*38e8c45fSAndroid Build Coastguard Worker }
123*38e8c45fSAndroid Build Coastguard Worker mLatestSamples.pushBack(
124*38e8c45fSAndroid Build Coastguard Worker Sample{nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)}, pointerMap});
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker }
127*38e8c45fSAndroid Build Coastguard Worker
messageToSample(const InputMessage & message)128*38e8c45fSAndroid Build Coastguard Worker LegacyResampler::Sample LegacyResampler::messageToSample(const InputMessage& message) {
129*38e8c45fSAndroid Build Coastguard Worker PointerMap pointerMap;
130*38e8c45fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < message.body.motion.pointerCount; ++i) {
131*38e8c45fSAndroid Build Coastguard Worker pointerMap.insert(Pointer{message.body.motion.pointers[i].properties,
132*38e8c45fSAndroid Build Coastguard Worker message.body.motion.pointers[i].coords});
133*38e8c45fSAndroid Build Coastguard Worker }
134*38e8c45fSAndroid Build Coastguard Worker return Sample{nanoseconds{message.body.motion.eventTime}, pointerMap};
135*38e8c45fSAndroid Build Coastguard Worker }
136*38e8c45fSAndroid Build Coastguard Worker
pointerPropertiesResampleable(const Sample & target,const Sample & auxiliary)137*38e8c45fSAndroid Build Coastguard Worker bool LegacyResampler::pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary) {
138*38e8c45fSAndroid Build Coastguard Worker for (const Pointer& pointer : target.pointerMap) {
139*38e8c45fSAndroid Build Coastguard Worker const std::optional<Pointer> auxiliaryPointer =
140*38e8c45fSAndroid Build Coastguard Worker auxiliary.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
141*38e8c45fSAndroid Build Coastguard Worker if (!auxiliaryPointer.has_value()) {
142*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
143*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. Auxiliary sample does not contain all pointers from target.";
144*38e8c45fSAndroid Build Coastguard Worker return false;
145*38e8c45fSAndroid Build Coastguard Worker }
146*38e8c45fSAndroid Build Coastguard Worker if (pointer.properties.toolType != auxiliaryPointer->properties.toolType) {
147*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling()) << "Not resampled. Pointer ToolType mismatch.";
148*38e8c45fSAndroid Build Coastguard Worker return false;
149*38e8c45fSAndroid Build Coastguard Worker }
150*38e8c45fSAndroid Build Coastguard Worker if (!canResampleTool(pointer.properties.toolType)) {
151*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
152*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. Cannot resample "
153*38e8c45fSAndroid Build Coastguard Worker << ftl::enum_string(pointer.properties.toolType) << " ToolType.";
154*38e8c45fSAndroid Build Coastguard Worker return false;
155*38e8c45fSAndroid Build Coastguard Worker }
156*38e8c45fSAndroid Build Coastguard Worker }
157*38e8c45fSAndroid Build Coastguard Worker return true;
158*38e8c45fSAndroid Build Coastguard Worker }
159*38e8c45fSAndroid Build Coastguard Worker
canInterpolate(const InputMessage & message) const160*38e8c45fSAndroid Build Coastguard Worker bool LegacyResampler::canInterpolate(const InputMessage& message) const {
161*38e8c45fSAndroid Build Coastguard Worker LOG_IF(FATAL, mLatestSamples.empty())
162*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. mLatestSamples must not be empty to interpolate.";
163*38e8c45fSAndroid Build Coastguard Worker
164*38e8c45fSAndroid Build Coastguard Worker const Sample& pastSample = *(mLatestSamples.end() - 1);
165*38e8c45fSAndroid Build Coastguard Worker const Sample& futureSample = messageToSample(message);
166*38e8c45fSAndroid Build Coastguard Worker
167*38e8c45fSAndroid Build Coastguard Worker if (!pointerPropertiesResampleable(pastSample, futureSample)) {
168*38e8c45fSAndroid Build Coastguard Worker return false;
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker
171*38e8c45fSAndroid Build Coastguard Worker const nanoseconds delta = futureSample.eventTime - pastSample.eventTime;
172*38e8c45fSAndroid Build Coastguard Worker if (delta < RESAMPLE_MIN_DELTA) {
173*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
174*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. Delta is too small: " << std::setprecision(3)
175*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{delta}.count() << "ms";
176*38e8c45fSAndroid Build Coastguard Worker return false;
177*38e8c45fSAndroid Build Coastguard Worker }
178*38e8c45fSAndroid Build Coastguard Worker return true;
179*38e8c45fSAndroid Build Coastguard Worker }
180*38e8c45fSAndroid Build Coastguard Worker
attemptInterpolation(nanoseconds resampleTime,const InputMessage & futureMessage) const181*38e8c45fSAndroid Build Coastguard Worker std::optional<LegacyResampler::Sample> LegacyResampler::attemptInterpolation(
182*38e8c45fSAndroid Build Coastguard Worker nanoseconds resampleTime, const InputMessage& futureMessage) const {
183*38e8c45fSAndroid Build Coastguard Worker if (!canInterpolate(futureMessage)) {
184*38e8c45fSAndroid Build Coastguard Worker return std::nullopt;
185*38e8c45fSAndroid Build Coastguard Worker }
186*38e8c45fSAndroid Build Coastguard Worker LOG_IF(FATAL, mLatestSamples.empty())
187*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. mLatestSamples must not be empty to interpolate.";
188*38e8c45fSAndroid Build Coastguard Worker
189*38e8c45fSAndroid Build Coastguard Worker const Sample& pastSample = *(mLatestSamples.end() - 1);
190*38e8c45fSAndroid Build Coastguard Worker const Sample& futureSample = messageToSample(futureMessage);
191*38e8c45fSAndroid Build Coastguard Worker
192*38e8c45fSAndroid Build Coastguard Worker const nanoseconds delta = nanoseconds{futureSample.eventTime} - pastSample.eventTime;
193*38e8c45fSAndroid Build Coastguard Worker const float alpha =
194*38e8c45fSAndroid Build Coastguard Worker std::chrono::duration<float, std::nano>(resampleTime - pastSample.eventTime) / delta;
195*38e8c45fSAndroid Build Coastguard Worker
196*38e8c45fSAndroid Build Coastguard Worker PointerMap resampledPointerMap;
197*38e8c45fSAndroid Build Coastguard Worker for (const Pointer& pointer : pastSample.pointerMap) {
198*38e8c45fSAndroid Build Coastguard Worker if (std::optional<Pointer> futureSamplePointer =
199*38e8c45fSAndroid Build Coastguard Worker futureSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
200*38e8c45fSAndroid Build Coastguard Worker futureSamplePointer.has_value()) {
201*38e8c45fSAndroid Build Coastguard Worker const PointerCoords& resampledCoords =
202*38e8c45fSAndroid Build Coastguard Worker calculateResampledCoords(pointer.coords, futureSamplePointer->coords, alpha);
203*38e8c45fSAndroid Build Coastguard Worker resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords});
204*38e8c45fSAndroid Build Coastguard Worker }
205*38e8c45fSAndroid Build Coastguard Worker }
206*38e8c45fSAndroid Build Coastguard Worker return Sample{resampleTime, resampledPointerMap};
207*38e8c45fSAndroid Build Coastguard Worker }
208*38e8c45fSAndroid Build Coastguard Worker
canExtrapolate() const209*38e8c45fSAndroid Build Coastguard Worker bool LegacyResampler::canExtrapolate() const {
210*38e8c45fSAndroid Build Coastguard Worker if (mLatestSamples.size() < 2) {
211*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling()) << "Not resampled. Not enough data.";
212*38e8c45fSAndroid Build Coastguard Worker return false;
213*38e8c45fSAndroid Build Coastguard Worker }
214*38e8c45fSAndroid Build Coastguard Worker
215*38e8c45fSAndroid Build Coastguard Worker const Sample& pastSample = *(mLatestSamples.end() - 2);
216*38e8c45fSAndroid Build Coastguard Worker const Sample& presentSample = *(mLatestSamples.end() - 1);
217*38e8c45fSAndroid Build Coastguard Worker
218*38e8c45fSAndroid Build Coastguard Worker if (!pointerPropertiesResampleable(presentSample, pastSample)) {
219*38e8c45fSAndroid Build Coastguard Worker return false;
220*38e8c45fSAndroid Build Coastguard Worker }
221*38e8c45fSAndroid Build Coastguard Worker
222*38e8c45fSAndroid Build Coastguard Worker const nanoseconds delta = presentSample.eventTime - pastSample.eventTime;
223*38e8c45fSAndroid Build Coastguard Worker if (delta < RESAMPLE_MIN_DELTA) {
224*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
225*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. Delta is too small: " << std::setprecision(3)
226*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{delta}.count() << "ms";
227*38e8c45fSAndroid Build Coastguard Worker return false;
228*38e8c45fSAndroid Build Coastguard Worker } else if (delta > RESAMPLE_MAX_DELTA) {
229*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
230*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. Delta is too large: " << std::setprecision(3)
231*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{delta}.count() << "ms";
232*38e8c45fSAndroid Build Coastguard Worker return false;
233*38e8c45fSAndroid Build Coastguard Worker }
234*38e8c45fSAndroid Build Coastguard Worker return true;
235*38e8c45fSAndroid Build Coastguard Worker }
236*38e8c45fSAndroid Build Coastguard Worker
attemptExtrapolation(nanoseconds resampleTime) const237*38e8c45fSAndroid Build Coastguard Worker std::optional<LegacyResampler::Sample> LegacyResampler::attemptExtrapolation(
238*38e8c45fSAndroid Build Coastguard Worker nanoseconds resampleTime) const {
239*38e8c45fSAndroid Build Coastguard Worker if (!canExtrapolate()) {
240*38e8c45fSAndroid Build Coastguard Worker return std::nullopt;
241*38e8c45fSAndroid Build Coastguard Worker }
242*38e8c45fSAndroid Build Coastguard Worker LOG_IF(FATAL, mLatestSamples.size() < 2)
243*38e8c45fSAndroid Build Coastguard Worker << "Not resampled. mLatestSamples must have at least two samples to extrapolate.";
244*38e8c45fSAndroid Build Coastguard Worker
245*38e8c45fSAndroid Build Coastguard Worker const Sample& pastSample = *(mLatestSamples.end() - 2);
246*38e8c45fSAndroid Build Coastguard Worker const Sample& presentSample = *(mLatestSamples.end() - 1);
247*38e8c45fSAndroid Build Coastguard Worker
248*38e8c45fSAndroid Build Coastguard Worker const nanoseconds delta = presentSample.eventTime - pastSample.eventTime;
249*38e8c45fSAndroid Build Coastguard Worker // The farthest future time to which we can extrapolate. If the given resampleTime exceeds this,
250*38e8c45fSAndroid Build Coastguard Worker // we use this value as the resample time target.
251*38e8c45fSAndroid Build Coastguard Worker const nanoseconds farthestPrediction =
252*38e8c45fSAndroid Build Coastguard Worker presentSample.eventTime + std::min<nanoseconds>(delta / 2, RESAMPLE_MAX_PREDICTION);
253*38e8c45fSAndroid Build Coastguard Worker const nanoseconds newResampleTime =
254*38e8c45fSAndroid Build Coastguard Worker (resampleTime > farthestPrediction) ? (farthestPrediction) : (resampleTime);
255*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling() && newResampleTime == farthestPrediction)
256*38e8c45fSAndroid Build Coastguard Worker << "Resample time is too far in the future. Adjusting prediction from "
257*38e8c45fSAndroid Build Coastguard Worker << std::setprecision(3)
258*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{resampleTime - presentSample.eventTime}
259*38e8c45fSAndroid Build Coastguard Worker .count()
260*38e8c45fSAndroid Build Coastguard Worker << "ms to "
261*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{farthestPrediction -
262*38e8c45fSAndroid Build Coastguard Worker presentSample.eventTime}
263*38e8c45fSAndroid Build Coastguard Worker .count()
264*38e8c45fSAndroid Build Coastguard Worker << "ms";
265*38e8c45fSAndroid Build Coastguard Worker const float alpha =
266*38e8c45fSAndroid Build Coastguard Worker std::chrono::duration<float, std::nano>(newResampleTime - pastSample.eventTime) / delta;
267*38e8c45fSAndroid Build Coastguard Worker
268*38e8c45fSAndroid Build Coastguard Worker PointerMap resampledPointerMap;
269*38e8c45fSAndroid Build Coastguard Worker for (const Pointer& pointer : presentSample.pointerMap) {
270*38e8c45fSAndroid Build Coastguard Worker if (std::optional<Pointer> pastSamplePointer =
271*38e8c45fSAndroid Build Coastguard Worker pastSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id});
272*38e8c45fSAndroid Build Coastguard Worker pastSamplePointer.has_value()) {
273*38e8c45fSAndroid Build Coastguard Worker const PointerCoords& resampledCoords =
274*38e8c45fSAndroid Build Coastguard Worker calculateResampledCoords(pastSamplePointer->coords, pointer.coords, alpha);
275*38e8c45fSAndroid Build Coastguard Worker resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords});
276*38e8c45fSAndroid Build Coastguard Worker }
277*38e8c45fSAndroid Build Coastguard Worker }
278*38e8c45fSAndroid Build Coastguard Worker return Sample{newResampleTime, resampledPointerMap};
279*38e8c45fSAndroid Build Coastguard Worker }
280*38e8c45fSAndroid Build Coastguard Worker
addSampleToMotionEvent(const Sample & sample,MotionEvent & motionEvent)281*38e8c45fSAndroid Build Coastguard Worker inline void LegacyResampler::addSampleToMotionEvent(const Sample& sample,
282*38e8c45fSAndroid Build Coastguard Worker MotionEvent& motionEvent) {
283*38e8c45fSAndroid Build Coastguard Worker motionEvent.addSample(sample.eventTime.count(), sample.asPointerCoords().data(),
284*38e8c45fSAndroid Build Coastguard Worker motionEvent.getId());
285*38e8c45fSAndroid Build Coastguard Worker }
286*38e8c45fSAndroid Build Coastguard Worker
getResampleLatency() const287*38e8c45fSAndroid Build Coastguard Worker nanoseconds LegacyResampler::getResampleLatency() const {
288*38e8c45fSAndroid Build Coastguard Worker return RESAMPLE_LATENCY;
289*38e8c45fSAndroid Build Coastguard Worker }
290*38e8c45fSAndroid Build Coastguard Worker
291*38e8c45fSAndroid Build Coastguard Worker /**
292*38e8c45fSAndroid Build Coastguard Worker * The resampler is unaware of ACTION_DOWN. Thus, it needs to constantly check for pointer IDs
293*38e8c45fSAndroid Build Coastguard Worker * occurrences. This problem could be fixed if the resampler has access to the entire stream of
294*38e8c45fSAndroid Build Coastguard Worker * MotionEvent actions. That way, both ACTION_DOWN and ACTION_UP will be visible; therefore,
295*38e8c45fSAndroid Build Coastguard Worker * facilitating pointer tracking between samples.
296*38e8c45fSAndroid Build Coastguard Worker */
overwriteMotionEventSamples(MotionEvent & motionEvent) const297*38e8c45fSAndroid Build Coastguard Worker void LegacyResampler::overwriteMotionEventSamples(MotionEvent& motionEvent) const {
298*38e8c45fSAndroid Build Coastguard Worker const size_t numSamples = motionEvent.getHistorySize() + 1;
299*38e8c45fSAndroid Build Coastguard Worker for (size_t sampleIndex = 0; sampleIndex < numSamples; ++sampleIndex) {
300*38e8c45fSAndroid Build Coastguard Worker overwriteStillPointers(motionEvent, sampleIndex);
301*38e8c45fSAndroid Build Coastguard Worker overwriteOldPointers(motionEvent, sampleIndex);
302*38e8c45fSAndroid Build Coastguard Worker }
303*38e8c45fSAndroid Build Coastguard Worker }
304*38e8c45fSAndroid Build Coastguard Worker
overwriteStillPointers(MotionEvent & motionEvent,size_t sampleIndex) const305*38e8c45fSAndroid Build Coastguard Worker void LegacyResampler::overwriteStillPointers(MotionEvent& motionEvent, size_t sampleIndex) const {
306*38e8c45fSAndroid Build Coastguard Worker if (!mLastRealSample.has_value() || !mPreviousPrediction.has_value()) {
307*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling()) << "Still pointers not overwritten. Not enough data.";
308*38e8c45fSAndroid Build Coastguard Worker return;
309*38e8c45fSAndroid Build Coastguard Worker }
310*38e8c45fSAndroid Build Coastguard Worker for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); ++pointerIndex) {
311*38e8c45fSAndroid Build Coastguard Worker const std::optional<Pointer> lastRealPointer = mLastRealSample->pointerMap.find(
312*38e8c45fSAndroid Build Coastguard Worker PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)});
313*38e8c45fSAndroid Build Coastguard Worker const std::optional<Pointer> previousPointer = mPreviousPrediction->pointerMap.find(
314*38e8c45fSAndroid Build Coastguard Worker PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)});
315*38e8c45fSAndroid Build Coastguard Worker // This could happen because resampler only receives ACTION_MOVE events.
316*38e8c45fSAndroid Build Coastguard Worker if (!lastRealPointer.has_value() || !previousPointer.has_value()) {
317*38e8c45fSAndroid Build Coastguard Worker continue;
318*38e8c45fSAndroid Build Coastguard Worker }
319*38e8c45fSAndroid Build Coastguard Worker const PointerCoords& pointerCoords =
320*38e8c45fSAndroid Build Coastguard Worker *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex));
321*38e8c45fSAndroid Build Coastguard Worker if (equalXY(pointerCoords, lastRealPointer->coords)) {
322*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
323*38e8c45fSAndroid Build Coastguard Worker << "Pointer ID: " << motionEvent.getPointerId(pointerIndex)
324*38e8c45fSAndroid Build Coastguard Worker << " did not move. Overwriting its coordinates from " << pointerCoords << " to "
325*38e8c45fSAndroid Build Coastguard Worker << previousPointer->coords;
326*38e8c45fSAndroid Build Coastguard Worker setMotionEventPointerCoords(motionEvent, sampleIndex, pointerIndex,
327*38e8c45fSAndroid Build Coastguard Worker previousPointer->coords);
328*38e8c45fSAndroid Build Coastguard Worker }
329*38e8c45fSAndroid Build Coastguard Worker }
330*38e8c45fSAndroid Build Coastguard Worker }
331*38e8c45fSAndroid Build Coastguard Worker
overwriteOldPointers(MotionEvent & motionEvent,size_t sampleIndex) const332*38e8c45fSAndroid Build Coastguard Worker void LegacyResampler::overwriteOldPointers(MotionEvent& motionEvent, size_t sampleIndex) const {
333*38e8c45fSAndroid Build Coastguard Worker if (!mPreviousPrediction.has_value()) {
334*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling()) << "Old sample not overwritten. Not enough data.";
335*38e8c45fSAndroid Build Coastguard Worker return;
336*38e8c45fSAndroid Build Coastguard Worker }
337*38e8c45fSAndroid Build Coastguard Worker if (nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)} <
338*38e8c45fSAndroid Build Coastguard Worker mPreviousPrediction->eventTime) {
339*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling())
340*38e8c45fSAndroid Build Coastguard Worker << "Motion event sample older than predicted sample. Overwriting event time from "
341*38e8c45fSAndroid Build Coastguard Worker << std::setprecision(3)
342*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double,
343*38e8c45fSAndroid Build Coastguard Worker std::milli>{nanoseconds{motionEvent.getHistoricalEventTime(
344*38e8c45fSAndroid Build Coastguard Worker sampleIndex)}}
345*38e8c45fSAndroid Build Coastguard Worker .count()
346*38e8c45fSAndroid Build Coastguard Worker << "ms to "
347*38e8c45fSAndroid Build Coastguard Worker << std::chrono::duration<double, std::milli>{mPreviousPrediction->eventTime}.count()
348*38e8c45fSAndroid Build Coastguard Worker << "ms";
349*38e8c45fSAndroid Build Coastguard Worker for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount();
350*38e8c45fSAndroid Build Coastguard Worker ++pointerIndex) {
351*38e8c45fSAndroid Build Coastguard Worker const std::optional<Pointer> previousPointer = mPreviousPrediction->pointerMap.find(
352*38e8c45fSAndroid Build Coastguard Worker PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)});
353*38e8c45fSAndroid Build Coastguard Worker // This could happen because resampler only receives ACTION_MOVE events.
354*38e8c45fSAndroid Build Coastguard Worker if (!previousPointer.has_value()) {
355*38e8c45fSAndroid Build Coastguard Worker continue;
356*38e8c45fSAndroid Build Coastguard Worker }
357*38e8c45fSAndroid Build Coastguard Worker setMotionEventPointerCoords(motionEvent, sampleIndex, pointerIndex,
358*38e8c45fSAndroid Build Coastguard Worker previousPointer->coords);
359*38e8c45fSAndroid Build Coastguard Worker }
360*38e8c45fSAndroid Build Coastguard Worker }
361*38e8c45fSAndroid Build Coastguard Worker }
362*38e8c45fSAndroid Build Coastguard Worker
resampleMotionEvent(nanoseconds frameTime,MotionEvent & motionEvent,const InputMessage * futureSample)363*38e8c45fSAndroid Build Coastguard Worker void LegacyResampler::resampleMotionEvent(nanoseconds frameTime, MotionEvent& motionEvent,
364*38e8c45fSAndroid Build Coastguard Worker const InputMessage* futureSample) {
365*38e8c45fSAndroid Build Coastguard Worker const nanoseconds resampleTime = frameTime - RESAMPLE_LATENCY;
366*38e8c45fSAndroid Build Coastguard Worker
367*38e8c45fSAndroid Build Coastguard Worker if (resampleTime.count() == motionEvent.getEventTime()) {
368*38e8c45fSAndroid Build Coastguard Worker LOG_IF(INFO, debugResampling()) << "Not resampled. Resample time equals motion event time.";
369*38e8c45fSAndroid Build Coastguard Worker return;
370*38e8c45fSAndroid Build Coastguard Worker }
371*38e8c45fSAndroid Build Coastguard Worker
372*38e8c45fSAndroid Build Coastguard Worker updateLatestSamples(motionEvent);
373*38e8c45fSAndroid Build Coastguard Worker
374*38e8c45fSAndroid Build Coastguard Worker const std::optional<Sample> sample = (futureSample != nullptr)
375*38e8c45fSAndroid Build Coastguard Worker ? (attemptInterpolation(resampleTime, *futureSample))
376*38e8c45fSAndroid Build Coastguard Worker : (attemptExtrapolation(resampleTime));
377*38e8c45fSAndroid Build Coastguard Worker if (sample.has_value()) {
378*38e8c45fSAndroid Build Coastguard Worker addSampleToMotionEvent(*sample, motionEvent);
379*38e8c45fSAndroid Build Coastguard Worker if (mPreviousPrediction.has_value()) {
380*38e8c45fSAndroid Build Coastguard Worker overwriteMotionEventSamples(motionEvent);
381*38e8c45fSAndroid Build Coastguard Worker }
382*38e8c45fSAndroid Build Coastguard Worker // mPreviousPrediction is only updated whenever extrapolation occurs because extrapolation
383*38e8c45fSAndroid Build Coastguard Worker // is about predicting upcoming scenarios.
384*38e8c45fSAndroid Build Coastguard Worker if (futureSample == nullptr) {
385*38e8c45fSAndroid Build Coastguard Worker mPreviousPrediction = sample;
386*38e8c45fSAndroid Build Coastguard Worker }
387*38e8c45fSAndroid Build Coastguard Worker }
388*38e8c45fSAndroid Build Coastguard Worker LOG_IF(FATAL, mLatestSamples.empty()) << "mLatestSamples must contain at least one sample.";
389*38e8c45fSAndroid Build Coastguard Worker mLastRealSample = *(mLatestSamples.end() - 1);
390*38e8c45fSAndroid Build Coastguard Worker }
391*38e8c45fSAndroid Build Coastguard Worker
392*38e8c45fSAndroid Build Coastguard Worker // --- FilteredLegacyResampler ---
393*38e8c45fSAndroid Build Coastguard Worker
FilteredLegacyResampler(float minCutoffFreq,float beta)394*38e8c45fSAndroid Build Coastguard Worker FilteredLegacyResampler::FilteredLegacyResampler(float minCutoffFreq, float beta)
395*38e8c45fSAndroid Build Coastguard Worker : mResampler{}, mMinCutoffFreq{minCutoffFreq}, mBeta{beta} {}
396*38e8c45fSAndroid Build Coastguard Worker
resampleMotionEvent(std::chrono::nanoseconds requestedFrameTime,MotionEvent & motionEvent,const InputMessage * futureSample)397*38e8c45fSAndroid Build Coastguard Worker void FilteredLegacyResampler::resampleMotionEvent(std::chrono::nanoseconds requestedFrameTime,
398*38e8c45fSAndroid Build Coastguard Worker MotionEvent& motionEvent,
399*38e8c45fSAndroid Build Coastguard Worker const InputMessage* futureSample) {
400*38e8c45fSAndroid Build Coastguard Worker mResampler.resampleMotionEvent(requestedFrameTime, motionEvent, futureSample);
401*38e8c45fSAndroid Build Coastguard Worker const size_t numSamples = motionEvent.getHistorySize() + 1;
402*38e8c45fSAndroid Build Coastguard Worker for (size_t sampleIndex = 0; sampleIndex < numSamples; ++sampleIndex) {
403*38e8c45fSAndroid Build Coastguard Worker for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount();
404*38e8c45fSAndroid Build Coastguard Worker ++pointerIndex) {
405*38e8c45fSAndroid Build Coastguard Worker const int32_t pointerId = motionEvent.getPointerProperties(pointerIndex)->id;
406*38e8c45fSAndroid Build Coastguard Worker const nanoseconds eventTime =
407*38e8c45fSAndroid Build Coastguard Worker nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)};
408*38e8c45fSAndroid Build Coastguard Worker // Refer to the static function `setMotionEventPointerCoords` for a justification of
409*38e8c45fSAndroid Build Coastguard Worker // casting away const.
410*38e8c45fSAndroid Build Coastguard Worker PointerCoords& pointerCoords = const_cast<PointerCoords&>(
411*38e8c45fSAndroid Build Coastguard Worker *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex)));
412*38e8c45fSAndroid Build Coastguard Worker const auto& [iter, _] = mFilteredPointers.try_emplace(pointerId, mMinCutoffFreq, mBeta);
413*38e8c45fSAndroid Build Coastguard Worker iter->second.filter(eventTime, pointerCoords);
414*38e8c45fSAndroid Build Coastguard Worker }
415*38e8c45fSAndroid Build Coastguard Worker }
416*38e8c45fSAndroid Build Coastguard Worker }
417*38e8c45fSAndroid Build Coastguard Worker
getResampleLatency() const418*38e8c45fSAndroid Build Coastguard Worker std::chrono::nanoseconds FilteredLegacyResampler::getResampleLatency() const {
419*38e8c45fSAndroid Build Coastguard Worker return mResampler.getResampleLatency();
420*38e8c45fSAndroid Build Coastguard Worker }
421*38e8c45fSAndroid Build Coastguard Worker
422*38e8c45fSAndroid Build Coastguard Worker } // namespace android
423