[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / metrics / average_lag_tracker.h
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CC_METRICS_AVERAGE_LAG_TRACKER_H_
6 #define CC_METRICS_AVERAGE_LAG_TRACKER_H_
7
8 #include <deque>
9 #include <string>
10
11 #include "base/time/time.h"
12 #include "cc/cc_export.h"
13
14 namespace cc {
15
16 // A class for reporting AverageLag metrics. See
17 // https://docs.google.com/document/d/1e8NuzPblIv2B9bz01oSj40rmlse7_PHq5oFS3lqz6N4/
18 class CC_EXPORT AverageLagTracker {
19  public:
20   enum class EventType { ScrollBegin, ScrollUpdate };
21
22   struct EventInfo {
23     EventInfo(float event_scroll_delta,
24               float predicted_scroll_delta,
25               base::TimeTicks event_timestamp,
26               EventType event_type)
27         : event_scroll_delta(event_scroll_delta),
28           predicted_scroll_delta(predicted_scroll_delta),
29           event_timestamp(event_timestamp),
30           event_type(event_type) {}
31     // Delta reported by the scroll event (begin or update).
32     float event_scroll_delta;
33     // Delta predicted (when prediction is on, otherwise should be equals to
34     // |event_scroll_delta|).
35     float predicted_scroll_delta;
36     // Timestamp when the scroll event happened.
37     base::TimeTicks event_timestamp;
38     // Timestamp when the scroll event's frame finished, which is currently
39     // when the frame swap completed.
40     base::TimeTicks finish_timestamp;
41     // Scroll event type (begin or update).
42     EventType event_type;
43   };
44
45   AverageLagTracker();
46   ~AverageLagTracker();
47
48   // Disallow copy and assign.
49   AverageLagTracker(const AverageLagTracker&) = delete;
50   AverageLagTracker& operator=(AverageLagTracker const&) = delete;
51
52   // Adds a scroll event defined by |event_info|.
53   void AddScrollEventInFrame(const EventInfo& event_info);
54
55  protected:
56   std::string GetAverageLagMetricName(EventType) const;
57
58  private:
59   struct LagAreaInFrame {
60     explicit LagAreaInFrame(base::TimeTicks time,
61                             float rendered_pos = 0,
62                             float rendered_pos_no_prediction = 0)
63         : frame_time(time),
64           rendered_accumulated_delta(rendered_pos),
65           lag_area(0),
66           rendered_accumulated_delta_no_prediction(rendered_pos_no_prediction),
67           lag_area_no_prediction(0) {}
68     base::TimeTicks frame_time;
69     // |rendered_accumulated_delta| is the cumulative delta that was swapped for
70     // this frame; this is based on the predicted delta, if prediction is
71     // enabled.
72     float rendered_accumulated_delta;
73     // |lag_area| is computed once a future input is processed that occurs after
74     // the swap timestamp (so that we can compute how far the rendered delta
75     // was from the actual position at the swap time).
76     float lag_area;
77     // |rendered_accumulated_delta_no_prediction| is the what would have been
78     // rendered if prediction was not taken into account, i.e., the actual delta
79     // from the input event.
80     float rendered_accumulated_delta_no_prediction;
81     // |lag_area_no_prediction| is computed the same as |lag_area| but using
82     // rendered_accumulated_delta_no_prediction as the rendered delta.
83     float lag_area_no_prediction;
84   };
85
86   // Processes |event_info| as a ScrollBegin event and add it to the Lag.
87   void AddScrollBeginInFrame(const EventInfo& event_info);
88   // Processes |event_info| as a ScrollUpdate event and add it to the Lag.
89   void AddScrollUpdateInFrame(const EventInfo& event_info);
90
91   // Calculate lag in 1 seconds intervals and report UMA.
92   void CalculateAndReportAverageLagUma(bool send_anyway = false);
93
94   // Helper function to calculate lag area between |front_time| to
95   // |back_time|.
96   float LagBetween(base::TimeTicks front_time,
97                    base::TimeTicks back_time,
98                    float scroll_delta,
99                    base::TimeTicks event_time,
100                    float rendered_accumulated_delta);
101
102   float LagForUnfinishedFrame(float rendered_accumulated_delta);
103
104   std::deque<LagAreaInFrame> frame_lag_infos_;
105
106   // Last scroll event's timestamp in the sequence, reset on ScrollBegin.
107   base::TimeTicks last_event_timestamp_;
108   // Timestamp of the last frame popped from |frame_lag_infos_| queue.
109   base::TimeTicks last_finished_frame_time_;
110
111   // Accumulated scroll delta for actual scroll update events. Cumulated from
112   // event_scroll_delta. Reset on ScrollBegin.
113   float last_event_accumulated_delta_ = 0;
114   // Accumulated scroll delta got rendered on gpu swap. Cumulated from
115   // predicted_scroll_delta. It always has same value as
116   // |last_event_accumulated_delta_| when scroll prediction is disabled.
117   float last_rendered_accumulated_delta_ = 0;
118
119   // This keeps track of the last report_time when we report to UMA, so we can
120   // calculate the report's duration by current - last. Reset on ScrollBegin.
121   base::TimeTicks last_reported_time_;
122
123   // True if the first element of |frame_lag_infos_| is for ScrollBegin.
124   // For ScrollBegin, we don't wait for the 1 second interval but record the
125   // UMA once the frame is finished.
126   bool is_begin_ = false;
127
128   // Accumulated lag area in the 1 second intervals.
129   float accumulated_lag_ = 0;
130   // Accumulated lag not taking into account the predicted deltas.
131   float accumulated_lag_no_prediction_ = 0;
132 };
133
134 }  // namespace cc
135
136 #endif  // CC_METRICS_AVERAGE_LAG_TRACKER_H_