blob: 5980d5d4c4836ef4d058cf3ed3237dec328c18d3 [file] [log] [blame]
Paul Ramirezbe9c5442024-07-10 00:12:41 +00001/**
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <chrono>
20#include <optional>
21
22#include <input/Input.h>
23#include <input/InputTransport.h>
24#include <input/RingBuffer.h>
25#include <utils/Timers.h>
26
27namespace android {
28
29/**
30 * Resampler is an interface for resampling MotionEvents. Every resampling implementation
31 * must use this interface to enable resampling inside InputConsumer's logic.
32 */
33struct Resampler {
34 virtual ~Resampler() = default;
35
36 /**
37 * Tries to resample motionEvent at resampleTime. The provided resampleTime must be greater than
38 * the latest sample time of motionEvent. It is not guaranteed that resampling occurs at
39 * resampleTime. Interpolation may occur is futureSample is available. Otherwise, motionEvent
40 * may be resampled by another method, or not resampled at all. Furthermore, it is the
41 * implementer's responsibility to guarantee the following:
42 * - If resampling occurs, a single additional sample should be added to motionEvent. That is,
43 * if motionEvent had N samples before being passed to Resampler, then it will have N + 1
44 * samples by the end of the resampling. No other field of motionEvent should be modified.
45 * - If resampling does not occur, then motionEvent must not be modified in any way.
46 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +000047 virtual void resampleMotionEvent(std::chrono::nanoseconds resampleTime,
Paul Ramirezbe9c5442024-07-10 00:12:41 +000048 MotionEvent& motionEvent,
49 const InputMessage* futureSample) = 0;
50};
51
52class LegacyResampler final : public Resampler {
53public:
54 /**
55 * Tries to resample `motionEvent` at `resampleTime` by adding a resampled sample at the end of
56 * `motionEvent` with eventTime equal to `resampleTime` and pointer coordinates determined by
57 * linear interpolation or linear extrapolation. An earlier `resampleTime` will be used if
58 * extrapolation takes place and `resampleTime` is too far in the future. If `futureSample` is
59 * not null, interpolation will occur. If `futureSample` is null and there is enough historical
60 * data, LegacyResampler will extrapolate. Otherwise, no resampling takes place and
61 * `motionEvent` is unmodified.
62 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +000063 void resampleMotionEvent(std::chrono::nanoseconds resampleTime, MotionEvent& motionEvent,
Paul Ramirezbe9c5442024-07-10 00:12:41 +000064 const InputMessage* futureSample) override;
65
66private:
67 struct Pointer {
68 PointerProperties properties;
69 PointerCoords coords;
70 };
71
72 struct Sample {
73 std::chrono::nanoseconds eventTime;
74 Pointer pointer;
Paul Ramirezbe9c5442024-07-10 00:12:41 +000075 };
76
77 /**
78 * Keeps track of the previous MotionEvent deviceId to enable comparison between the previous
79 * and the current deviceId.
80 */
81 std::optional<DeviceId> mPreviousDeviceId;
82
83 /**
84 * Up to two latest samples from MotionEvent. Updated every time resampleMotionEvent is called.
85 * Note: We store up to two samples in order to simplify the implementation. Although,
86 * calculations are possible with only one previous sample.
87 */
88 RingBuffer<Sample> mLatestSamples{/*capacity=*/2};
89
90 /**
91 * Adds up to mLatestSamples.capacity() of motionEvent's latest samples to mLatestSamples. (If
92 * motionEvent has fewer samples than mLatestSamples.capacity(), then the available samples are
93 * added to mLatestSamples.)
94 */
95 void updateLatestSamples(const MotionEvent& motionEvent);
96
97 /**
Paul Ramirez486ca6d2024-08-12 14:37:02 +000098 * Checks if there are necessary conditions to interpolate. For example, interpolation cannot
99 * take place if samples are too far apart in time. mLatestSamples must have at least one sample
100 * when canInterpolate is invoked.
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000101 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000102 bool canInterpolate(const InputMessage& futureSample) const;
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000103
104 /**
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000105 * Returns a sample interpolated between the latest sample of mLatestSamples and futureSample,
106 * if the conditions from canInterpolate are satisfied. Otherwise, returns nullopt.
107 * mLatestSamples must have at least one sample when attemptInterpolation is called.
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000108 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000109 std::optional<Sample> attemptInterpolation(std::chrono::nanoseconds resampleTime,
110 const InputMessage& futureSample) const;
111
112 /**
113 * Checks if there are necessary conditions to extrapolate. That is, there are at least two
114 * samples in mLatestSamples, and delta is bounded within a time interval.
115 */
116 bool canExtrapolate() const;
117
118 /**
119 * Returns a sample extrapolated from the two samples of mLatestSamples, if the conditions from
120 * canExtrapolate are satisfied. The returned sample either has eventTime equal to resampleTime,
121 * or an earlier time if resampleTime is too far in the future. If canExtrapolate returns false,
122 * this function returns nullopt.
123 */
124 std::optional<Sample> attemptExtrapolation(std::chrono::nanoseconds resampleTime) const;
125
126 inline static void addSampleToMotionEvent(const Sample& sample, MotionEvent& motionEvent);
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000127};
128} // namespace android