Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 1 | /** |
| 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 Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 21 | #include <vector> |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 22 | |
| 23 | #include <input/Input.h> |
| 24 | #include <input/InputTransport.h> |
| 25 | #include <input/RingBuffer.h> |
| 26 | #include <utils/Timers.h> |
| 27 | |
| 28 | namespace 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 | */ |
| 34 | struct Resampler { |
| 35 | virtual ~Resampler() = default; |
| 36 | |
| 37 | /** |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 38 | * Tries to resample motionEvent at frameTime. The provided frameTime must be greater than |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 39 | * the latest sample time of motionEvent. It is not guaranteed that resampling occurs at |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 40 | * frameTime. Interpolation may occur is futureSample is available. Otherwise, motionEvent |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 41 | * 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 Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 48 | virtual void resampleMotionEvent(std::chrono::nanoseconds frameTime, MotionEvent& motionEvent, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 49 | const InputMessage* futureSample) = 0; |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 50 | |
| 51 | /** |
| 52 | * Returns resample latency. Resample latency is the time difference between frame time and |
| 53 | * resample time. More precisely, let frameTime and resampleTime be two timestamps, and |
| 54 | * frameTime > resampleTime. Resample latency is defined as frameTime - resampleTime. |
| 55 | */ |
| 56 | virtual std::chrono::nanoseconds getResampleLatency() const = 0; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | class LegacyResampler final : public Resampler { |
| 60 | public: |
| 61 | /** |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 62 | * Tries to resample `motionEvent` at `frameTime` by adding a resampled sample at the end of |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 63 | * `motionEvent` with eventTime equal to `resampleTime` and pointer coordinates determined by |
| 64 | * linear interpolation or linear extrapolation. An earlier `resampleTime` will be used if |
| 65 | * extrapolation takes place and `resampleTime` is too far in the future. If `futureSample` is |
| 66 | * not null, interpolation will occur. If `futureSample` is null and there is enough historical |
| 67 | * data, LegacyResampler will extrapolate. Otherwise, no resampling takes place and |
| 68 | * `motionEvent` is unmodified. |
| 69 | */ |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 70 | void resampleMotionEvent(std::chrono::nanoseconds frameTime, MotionEvent& motionEvent, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 71 | const InputMessage* futureSample) override; |
| 72 | |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 73 | std::chrono::nanoseconds getResampleLatency() const override; |
| 74 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 75 | private: |
| 76 | struct Pointer { |
| 77 | PointerProperties properties; |
| 78 | PointerCoords coords; |
| 79 | }; |
| 80 | |
| 81 | struct Sample { |
| 82 | std::chrono::nanoseconds eventTime; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 83 | std::vector<Pointer> pointers; |
| 84 | |
| 85 | std::vector<PointerCoords> asPointerCoords() const { |
| 86 | std::vector<PointerCoords> pointersCoords; |
| 87 | for (const Pointer& pointer : pointers) { |
| 88 | pointersCoords.push_back(pointer.coords); |
| 89 | } |
| 90 | return pointersCoords; |
| 91 | } |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Keeps track of the previous MotionEvent deviceId to enable comparison between the previous |
| 96 | * and the current deviceId. |
| 97 | */ |
| 98 | std::optional<DeviceId> mPreviousDeviceId; |
| 99 | |
| 100 | /** |
| 101 | * Up to two latest samples from MotionEvent. Updated every time resampleMotionEvent is called. |
| 102 | * Note: We store up to two samples in order to simplify the implementation. Although, |
| 103 | * calculations are possible with only one previous sample. |
| 104 | */ |
| 105 | RingBuffer<Sample> mLatestSamples{/*capacity=*/2}; |
| 106 | |
| 107 | /** |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 108 | * Adds up to mLatestSamples.capacity() of motionEvent's latest samples to mLatestSamples. If |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 109 | * motionEvent has fewer samples than mLatestSamples.capacity(), then the available samples are |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 110 | * added to mLatestSamples. |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 111 | */ |
| 112 | void updateLatestSamples(const MotionEvent& motionEvent); |
| 113 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 114 | static Sample messageToSample(const InputMessage& message); |
| 115 | |
| 116 | /** |
| 117 | * Checks if auxiliary sample has the same pointer properties of target sample. That is, |
| 118 | * auxiliary pointer IDs must appear in the same order as target pointer IDs, their toolType |
| 119 | * must match and be resampleable. |
| 120 | */ |
| 121 | static bool pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary); |
| 122 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 123 | /** |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 124 | * Checks if there are necessary conditions to interpolate. For example, interpolation cannot |
| 125 | * take place if samples are too far apart in time. mLatestSamples must have at least one sample |
| 126 | * when canInterpolate is invoked. |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 127 | */ |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 128 | bool canInterpolate(const InputMessage& futureSample) const; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 129 | |
| 130 | /** |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 131 | * Returns a sample interpolated between the latest sample of mLatestSamples and futureSample, |
| 132 | * if the conditions from canInterpolate are satisfied. Otherwise, returns nullopt. |
| 133 | * mLatestSamples must have at least one sample when attemptInterpolation is called. |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 134 | */ |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 135 | std::optional<Sample> attemptInterpolation(std::chrono::nanoseconds resampleTime, |
| 136 | const InputMessage& futureSample) const; |
| 137 | |
| 138 | /** |
| 139 | * Checks if there are necessary conditions to extrapolate. That is, there are at least two |
| 140 | * samples in mLatestSamples, and delta is bounded within a time interval. |
| 141 | */ |
| 142 | bool canExtrapolate() const; |
| 143 | |
| 144 | /** |
| 145 | * Returns a sample extrapolated from the two samples of mLatestSamples, if the conditions from |
| 146 | * canExtrapolate are satisfied. The returned sample either has eventTime equal to resampleTime, |
| 147 | * or an earlier time if resampleTime is too far in the future. If canExtrapolate returns false, |
| 148 | * this function returns nullopt. |
| 149 | */ |
| 150 | std::optional<Sample> attemptExtrapolation(std::chrono::nanoseconds resampleTime) const; |
| 151 | |
| 152 | inline static void addSampleToMotionEvent(const Sample& sample, MotionEvent& motionEvent); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 153 | }; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 154 | } // namespace android |