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