blob: 67d92bd3ad33d4b6a39ba30647d663225afcc699 [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>
Paul Ramirezcf1b06e2024-08-01 17:11:58 +000021#include <vector>
Paul Ramirezbe9c5442024-07-10 00:12:41 +000022
23#include <input/Input.h>
24#include <input/InputTransport.h>
25#include <input/RingBuffer.h>
26#include <utils/Timers.h>
27
28namespace android {
29
30/**
31 * Resampler is an interface for resampling MotionEvents. Every resampling implementation
32 * must use this interface to enable resampling inside InputConsumer's logic.
33 */
34struct Resampler {
35 virtual ~Resampler() = default;
36
37 /**
Paul Ramirez6affbdb2024-09-11 22:20:26 +000038 * Tries to resample motionEvent at frameTime. The provided frameTime must be greater than
Paul Ramirezbe9c5442024-07-10 00:12:41 +000039 * the latest sample time of motionEvent. It is not guaranteed that resampling occurs at
Paul Ramirez6affbdb2024-09-11 22:20:26 +000040 * frameTime. Interpolation may occur is futureSample is available. Otherwise, motionEvent
Paul Ramirezbe9c5442024-07-10 00:12:41 +000041 * may be resampled by another method, or not resampled at all. Furthermore, it is the
42 * implementer's responsibility to guarantee the following:
43 * - If resampling occurs, a single additional sample should be added to motionEvent. That is,
44 * if motionEvent had N samples before being passed to Resampler, then it will have N + 1
45 * samples by the end of the resampling. No other field of motionEvent should be modified.
46 * - If resampling does not occur, then motionEvent must not be modified in any way.
47 */
Paul Ramirez6affbdb2024-09-11 22:20:26 +000048 virtual void resampleMotionEvent(std::chrono::nanoseconds frameTime, MotionEvent& motionEvent,
Paul Ramirezbe9c5442024-07-10 00:12:41 +000049 const InputMessage* futureSample) = 0;
50};
51
52class LegacyResampler final : public Resampler {
53public:
54 /**
Paul Ramirez6affbdb2024-09-11 22:20:26 +000055 * Tries to resample `motionEvent` at `frameTime` by adding a resampled sample at the end of
Paul Ramirezbe9c5442024-07-10 00:12:41 +000056 * `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 Ramirez6affbdb2024-09-11 22:20:26 +000063 void resampleMotionEvent(std::chrono::nanoseconds frameTime, 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;
Paul Ramirezcf1b06e2024-08-01 17:11:58 +000074 std::vector<Pointer> pointers;
75
76 std::vector<PointerCoords> asPointerCoords() const {
77 std::vector<PointerCoords> pointersCoords;
78 for (const Pointer& pointer : pointers) {
79 pointersCoords.push_back(pointer.coords);
80 }
81 return pointersCoords;
82 }
Paul Ramirezbe9c5442024-07-10 00:12:41 +000083 };
84
85 /**
86 * Keeps track of the previous MotionEvent deviceId to enable comparison between the previous
87 * and the current deviceId.
88 */
89 std::optional<DeviceId> mPreviousDeviceId;
90
91 /**
92 * Up to two latest samples from MotionEvent. Updated every time resampleMotionEvent is called.
93 * Note: We store up to two samples in order to simplify the implementation. Although,
94 * calculations are possible with only one previous sample.
95 */
96 RingBuffer<Sample> mLatestSamples{/*capacity=*/2};
97
98 /**
Paul Ramirezcf1b06e2024-08-01 17:11:58 +000099 * Adds up to mLatestSamples.capacity() of motionEvent's latest samples to mLatestSamples. If
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000100 * motionEvent has fewer samples than mLatestSamples.capacity(), then the available samples are
Paul Ramirezcf1b06e2024-08-01 17:11:58 +0000101 * added to mLatestSamples.
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000102 */
103 void updateLatestSamples(const MotionEvent& motionEvent);
104
Paul Ramirezcf1b06e2024-08-01 17:11:58 +0000105 static Sample messageToSample(const InputMessage& message);
106
107 /**
108 * Checks if auxiliary sample has the same pointer properties of target sample. That is,
109 * auxiliary pointer IDs must appear in the same order as target pointer IDs, their toolType
110 * must match and be resampleable.
111 */
112 static bool pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary);
113
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000114 /**
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000115 * Checks if there are necessary conditions to interpolate. For example, interpolation cannot
116 * take place if samples are too far apart in time. mLatestSamples must have at least one sample
117 * when canInterpolate is invoked.
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000118 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000119 bool canInterpolate(const InputMessage& futureSample) const;
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000120
121 /**
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000122 * Returns a sample interpolated between the latest sample of mLatestSamples and futureSample,
123 * if the conditions from canInterpolate are satisfied. Otherwise, returns nullopt.
124 * mLatestSamples must have at least one sample when attemptInterpolation is called.
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000125 */
Paul Ramirez486ca6d2024-08-12 14:37:02 +0000126 std::optional<Sample> attemptInterpolation(std::chrono::nanoseconds resampleTime,
127 const InputMessage& futureSample) const;
128
129 /**
130 * Checks if there are necessary conditions to extrapolate. That is, there are at least two
131 * samples in mLatestSamples, and delta is bounded within a time interval.
132 */
133 bool canExtrapolate() const;
134
135 /**
136 * Returns a sample extrapolated from the two samples of mLatestSamples, if the conditions from
137 * canExtrapolate are satisfied. The returned sample either has eventTime equal to resampleTime,
138 * or an earlier time if resampleTime is too far in the future. If canExtrapolate returns false,
139 * this function returns nullopt.
140 */
141 std::optional<Sample> attemptExtrapolation(std::chrono::nanoseconds resampleTime) const;
142
143 inline static void addSampleToMotionEvent(const Sample& sample, MotionEvent& motionEvent);
Paul Ramirezbe9c5442024-07-10 00:12:41 +0000144};
Paul Ramirezcf1b06e2024-08-01 17:11:58 +0000145} // namespace android