blob: 9699735039355830c90b2ebc1949b0f5e6d5357d [file] [log] [blame]
Axel Antoineebf99702019-06-26 17:00:231// Copyright 2019 The Chromium Authors. All rights reserved.
2// 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);
29 ~LinearPredictor() override;
30
31 const char* GetName() const override;
32
33 // Reset the predictor to initial state.
34 void Reset() override;
35
36 // Store current input in queue.
37 void Update(const InputData& new_input) override;
38
39 // Return if there is enough data in the queue to generate prediction.
40 bool HasPrediction() const override;
41
42 // Generate the prediction based on stored points and given time_stamp.
43 // Return false if no prediction available.
Ella Gef7afddc2019-10-21 23:37:4844 std::unique_ptr<InputData> GeneratePrediction(
Joao Victor Almeida371702c2021-01-08 18:41:5645 base::TimeTicks predict_time,
46 base::TimeDelta frame_interval) override;
Axel Antoineebf99702019-06-26 17:00:2347
Luis Sanchez Padillaca0e4b512019-07-31 23:16:3248 // Return the average time delta in the event queue.
49 base::TimeDelta TimeInterval() const override;
50
Axel Antoineebf99702019-06-26 17:00:2351 // Return the number of events needed to compute a prediction
52 size_t NumberOfEventsNeeded();
53
54 private:
Ella Gef7afddc2019-10-21 23:37:4855 gfx::PointF GeneratePredictionFirstOrder(float pred_dt) const;
Axel Antoineebf99702019-06-26 17:00:2356
Ella Gef7afddc2019-10-21 23:37:4857 gfx::PointF GeneratePredictionSecondOrder(float pred_dt) const;
Axel Antoineebf99702019-06-26 17:00:2358
59 // Store the last events received
60 std::deque<InputData> events_queue_;
61
62 // Store the equation order of the predictor
63 // The enum value also represents the number of events needed to compute the
64 // prediction
65 EquationOrder equation_order_;
66
67 // Store the current velocity of the 2 last events
68 gfx::Vector2dF cur_velocity_;
69
70 // Store the last velocity of the 2 last past events
71 gfx::Vector2dF last_velocity_;
72
73 // Store the current delta time between the last 2 events
74 float events_dt_;
75
76 DISALLOW_COPY_AND_ASSIGN(LinearPredictor);
77};
78
Mario Bianucci926a2d92020-07-28 02:04:3279} // namespace ui
Axel Antoineebf99702019-06-26 17:00:2380
Mario Bianucci926a2d92020-07-28 02:04:3281#endif // UI_BASE_PREDICTION_LINEAR_PREDICTOR_H_