blob: b0720df8f2e30d9ccddf6d5865df957d709657a9 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2019 The Chromium Authors
Axel Antoineebf99702019-06-26 17:00:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Mario Bianucci926a2d92020-07-28 02:04:325#ifndef UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_
6#define UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_
Axel Antoineebf99702019-06-26 17:00:237
8#include <deque>
9
Mario Bianucci926a2d92020-07-28 02:04:3210#include "base/component_export.h"
11#include "ui/base/prediction/input_predictor.h"
Axel Antoineebf99702019-06-26 17:00:2312
Mario Bianucci926a2d92020-07-28 02:04:3213namespace ui {
Axel Antoineebf99702019-06-26 17:00:2314
15// This class use a linear model for prediction
16// You can choose between a first order equation:
17// pred_p = last_p + velocity*pred_time
18// and a second order equation:
19// pred_p = last_p + velocity*pred_dt + 0.5*acceleration*pred_dt^2
20
Mario Bianucci926a2d92020-07-28 02:04:3221class COMPONENT_EXPORT(UI_BASE_PREDICTION) LinearPredictor
22 : public InputPredictor {
Axel Antoineebf99702019-06-26 17:00:2323 public:
24 // Used to dissociate the order of the equation used but also used to
25 // define the number of events needed by each model
26 enum class EquationOrder : size_t { kFirstOrder = 2, kSecondOrder = 3 };
27
28 explicit LinearPredictor(EquationOrder order);
Peter Boströmc8c12352021-09-21 23:37:1529
30 LinearPredictor(const LinearPredictor&) = delete;
31 LinearPredictor& operator=(const LinearPredictor&) = delete;
32
Axel Antoineebf99702019-06-26 17:00:2333 ~LinearPredictor() override;
34
35 const char* GetName() const override;
36
37 // Reset the predictor to initial state.
38 void Reset() override;
39
40 // Store current input in queue.
41 void Update(const InputData& new_input) override;
42
43 // Return if there is enough data in the queue to generate prediction.
44 bool HasPrediction() const override;
45
46 // Generate the prediction based on stored points and given time_stamp.
47 // Return false if no prediction available.
Ella Gef7afddc2019-10-21 23:37:4848 std::unique_ptr<InputData> GeneratePrediction(
Joao Victor Almeida371702c2021-01-08 18:41:5649 base::TimeTicks predict_time,
50 base::TimeDelta frame_interval) override;
Axel Antoineebf99702019-06-26 17:00:2351
Luis Sanchez Padillaca0e4b512019-07-31 23:16:3252 // Return the average time delta in the event queue.
53 base::TimeDelta TimeInterval() const override;
54
Axel Antoineebf99702019-06-26 17:00:2355 // Return the number of events needed to compute a prediction
56 size_t NumberOfEventsNeeded();
57
58 private:
Ella Gef7afddc2019-10-21 23:37:4859 gfx::PointF GeneratePredictionFirstOrder(float pred_dt) const;
Axel Antoineebf99702019-06-26 17:00:2360
Ella Gef7afddc2019-10-21 23:37:4861 gfx::PointF GeneratePredictionSecondOrder(float pred_dt) const;
Axel Antoineebf99702019-06-26 17:00:2362
63 // Store the last events received
64 std::deque<InputData> events_queue_;
65
66 // Store the equation order of the predictor
67 // The enum value also represents the number of events needed to compute the
68 // prediction
69 EquationOrder equation_order_;
70
71 // Store the current velocity of the 2 last events
72 gfx::Vector2dF cur_velocity_;
73
74 // Store the last velocity of the 2 last past events
75 gfx::Vector2dF last_velocity_;
76
77 // Store the current delta time between the last 2 events
78 float events_dt_;
Axel Antoineebf99702019-06-26 17:00:2379};
80
Mario Bianucci926a2d92020-07-28 02:04:3281} // namespace ui
Axel Antoineebf99702019-06-26 17:00:2382
Mario Bianucci926a2d92020-07-28 02:04:3283#endif // UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_