Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 19 | #include <array> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 23 | #include <optional> |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame] | 24 | #include <string> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
| 26 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 27 | #include <android-base/result.h> |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 28 | #include <android-base/thread_annotations.h> |
| 29 | #include <android/sysprop/InputProperties.sysprop.h> |
| 30 | #include <input/Input.h> |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 31 | #include <input/MotionPredictorMetricsManager.h> |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 32 | #include <input/RingBuffer.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 33 | #include <input/TfLiteMotionPredictor.h> |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 34 | #include <utils/Timers.h> // for nsecs_t |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | static inline bool isMotionPredictionEnabled() { |
| 39 | return sysprop::InputProperties::enable_motion_prediction().value_or(true); |
| 40 | } |
| 41 | |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 42 | // Tracker to calculate jerk from motion position samples. |
| 43 | class JerkTracker { |
| 44 | public: |
| 45 | // Initialize the tracker. If normalizedDt is true, assume that each sample pushed has dt=1. |
| 46 | JerkTracker(bool normalizedDt); |
| 47 | |
| 48 | // Add a position to the tracker and update derivative estimates. |
| 49 | void pushSample(int64_t timestamp, float xPos, float yPos); |
| 50 | |
| 51 | // Reset JerkTracker for a new motion input. |
| 52 | void reset(); |
| 53 | |
| 54 | // Return last jerk calculation, if enough samples have been collected. |
| 55 | // Jerk is defined as the 3rd derivative of position (change in |
| 56 | // acceleration) and has the units of d^3p/dt^3. |
| 57 | std::optional<float> jerkMagnitude() const; |
| 58 | |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame^] | 59 | // forgetFactor is the coefficient of the first-order IIR filter for jerk. A factor of 1 results |
| 60 | // in no smoothing. |
| 61 | void setForgetFactor(float forgetFactor); |
| 62 | float getForgetFactor() const; |
| 63 | |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 64 | private: |
| 65 | const bool mNormalizedDt; |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame^] | 66 | // Coefficient of first-order IIR filter to smooth jerk calculation. |
| 67 | float mForgetFactor = 1; |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 68 | |
| 69 | RingBuffer<int64_t> mTimestamps{4}; |
| 70 | std::array<float, 4> mXDerivatives{}; // [x, x', x'', x'''] |
| 71 | std::array<float, 4> mYDerivatives{}; // [y, y', y'', y'''] |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame^] | 72 | float mJerkMagnitude; |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 77 | * contains a set of samples in the future. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 78 | * |
| 79 | * The typical usage is like this: |
| 80 | * |
| 81 | * MotionPredictor predictor(offset = MY_OFFSET); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 82 | * predictor.record(DOWN_MOTION_EVENT); |
| 83 | * predictor.record(MOVE_MOTION_EVENT); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 84 | * prediction = predictor.predict(futureTime); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 85 | * |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 86 | * The resulting motion event will have eventTime <= (futureTime + MY_OFFSET). It might contain |
| 87 | * historical data, which are additional samples from the latest recorded MotionEvent's eventTime |
| 88 | * to the futureTime + MY_OFFSET. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 89 | * |
| 90 | * The offset is used to provide additional flexibility to the caller, in case the default present |
| 91 | * time (typically provided by the choreographer) does not account for some delays, or to simply |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 92 | * reduce the aggressiveness of the prediction. Offset can be positive or negative. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 93 | */ |
| 94 | class MotionPredictor { |
| 95 | public: |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 96 | using ReportAtomFunction = MotionPredictorMetricsManager::ReportAtomFunction; |
| 97 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Parameters: |
| 100 | * predictionTimestampOffsetNanos: additional, constant shift to apply to the target |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 101 | * prediction time. The prediction will target the time t=(prediction time + |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 102 | * predictionTimestampOffsetNanos). |
| 103 | * |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 104 | * checkEnableMotionPrediction: the function to check whether the prediction should run. Used to |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 105 | * provide an additional way of turning prediction on and off. Can be toggled at runtime. |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 106 | * |
| 107 | * reportAtomFunction: the function that will be called to report prediction metrics. If |
| 108 | * omitted, the implementation will choose a default metrics reporting mechanism. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 109 | */ |
Siarhei Vishniakou | fd0a68e | 2023-02-28 13:25:36 -0800 | [diff] [blame] | 110 | MotionPredictor(nsecs_t predictionTimestampOffsetNanos, |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 111 | std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled, |
| 112 | ReportAtomFunction reportAtomFunction = {}); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 113 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Record the actual motion received by the view. This event will be used for calculating the |
| 116 | * predictions. |
| 117 | * |
| 118 | * @return empty result if the event was processed correctly, error if the event is not |
| 119 | * consistent with the previously recorded events. |
| 120 | */ |
| 121 | android::base::Result<void> record(const MotionEvent& event); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 122 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 123 | std::unique_ptr<MotionEvent> predict(nsecs_t timestamp); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 124 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 125 | bool isPredictionAvailable(int32_t deviceId, int32_t source); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 126 | |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame^] | 127 | /** |
| 128 | * Currently used to expose config constants in testing. |
| 129 | */ |
| 130 | const TfLiteMotionPredictorModel::Config& getModelConfig(); |
| 131 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 132 | private: |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 133 | const nsecs_t mPredictionTimestampOffsetNanos; |
| 134 | const std::function<bool()> mCheckMotionPredictionEnabled; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 135 | |
| 136 | std::unique_ptr<TfLiteMotionPredictorModel> mModel; |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 137 | |
| 138 | std::unique_ptr<TfLiteMotionPredictorBuffers> mBuffers; |
| 139 | std::optional<MotionEvent> mLastEvent; |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 140 | // mJerkTracker assumes normalized dt = 1 between recorded samples because |
| 141 | // the underlying mModel input also assumes fixed-interval samples. |
| 142 | // Normalized dt as 1 is also used to correspond with the similar Jank |
| 143 | // implementation from the JetPack MotionPredictor implementation. |
| 144 | JerkTracker mJerkTracker{true}; |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 145 | |
| 146 | std::optional<MotionPredictorMetricsManager> mMetricsManager; |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 147 | |
| 148 | const ReportAtomFunction mReportAtomFunction; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace android |