1 // Copyright 2020 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 
5 #ifndef CC_METRICS_EVENT_METRICS_H_
6 #define CC_METRICS_EVENT_METRICS_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/optional.h"
12 #include "base/time/time.h"
13 #include "cc/cc_export.h"
14 #include "ui/events/types/event_type.h"
15 #include "ui/events/types/scroll_input_type.h"
16 
17 namespace cc {
18 
19 // Data about an event used by CompositorFrameReporter in generating event
20 // latency metrics.
21 class CC_EXPORT EventMetrics {
22  public:
23   using List = std::vector<std::unique_ptr<EventMetrics>>;
24 
25   // Event types we are interested in. This list should be in the same order as
26   // values of EventLatencyEventType enum from enums.xml file.
27   enum class EventType {
28     kMousePressed,
29     kMouseReleased,
30     kMouseWheel,
31     // TODO(crbug/1071645): Currently, all ET_KEY_PRESSED events are reported
32     // under EventLatency.KeyPressed histogram. This includes both key-down and
33     // key-char events. Consider reporting them separately.
34     kKeyPressed,
35     kKeyReleased,
36     kTouchPressed,
37     kTouchReleased,
38     kTouchMoved,
39     kGestureScrollBegin,
40     kGestureScrollUpdate,
41     kGestureScrollEnd,
42     kGestureDoubleTap,
43     kGestureLongPress,
44     kGestureLongTap,
45     kGestureShowPress,
46     kGestureTap,
47     kGestureTapCancel,
48     kGestureTapDown,
49     kGestureTapUnconfirmed,
50     kGestureTwoFingerTap,
51     kFirstGestureScrollUpdate,
52     kMaxValue = kFirstGestureScrollUpdate,
53   };
54 
55   // Type of scroll events. This list should be in the same order as values of
56   // EventLatencyScrollInputType enum from enums.xml file.
57   enum class ScrollType {
58     kAutoscroll,
59     kScrollbar,
60     kTouchscreen,
61     kWheel,
62     kMaxValue = kWheel,
63   };
64 
65   // Determines whether a scroll-update event is the first one in a gesture
66   // scroll sequence or not.
67   enum class ScrollUpdateType {
68     kStarted,
69     kContinued,
70     kMaxValue = kContinued,
71   };
72 
73   // Returns a new instance if |type| is an event type we are interested in.
74   // Otherwise, returns nullptr.
75   static std::unique_ptr<EventMetrics> Create(
76       ui::EventType type,
77       base::Optional<ScrollUpdateType> scroll_update_type,
78       base::TimeTicks time_stamp,
79       base::Optional<ui::ScrollInputType> scroll_input_type);
80 
81   EventMetrics(const EventMetrics&) = delete;
82   EventMetrics& operator=(const EventMetrics&) = delete;
83 
type()84   EventType type() const { return type_; }
85 
86   // Returns a string representing event type.
87   const char* GetTypeName() const;
88 
time_stamp()89   base::TimeTicks time_stamp() const { return time_stamp_; }
90 
scroll_type()91   const base::Optional<ScrollType>& scroll_type() const { return scroll_type_; }
92 
93   // Returns a string representing input type for a scroll event. Should only be
94   // called for scroll events.
95   const char* GetScrollTypeName() const;
96 
97   // Used in tests to check expectations on EventMetrics objects.
98   bool operator==(const EventMetrics& other) const;
99 
100  private:
101   EventMetrics(EventType type,
102                base::TimeTicks time_stamp,
103                base::Optional<ScrollType> scroll_type);
104 
105   EventType type_;
106   base::TimeTicks time_stamp_;
107 
108   // Only available for scroll events and represents the type of input device
109   // for the event.
110   base::Optional<ScrollType> scroll_type_;
111 };
112 
113 // Struct storing event metrics from both main and impl threads.
114 struct CC_EXPORT EventMetricsSet {
115   EventMetricsSet();
116   ~EventMetricsSet();
117   EventMetricsSet(EventMetrics::List main_thread_event_metrics,
118                   EventMetrics::List impl_thread_event_metrics);
119   EventMetricsSet(EventMetricsSet&&);
120   EventMetricsSet& operator=(EventMetricsSet&&);
121 
122   EventMetricsSet(const EventMetricsSet&) = delete;
123   EventMetricsSet& operator=(const EventMetricsSet&) = delete;
124 
125   EventMetrics::List main_event_metrics;
126   EventMetrics::List impl_event_metrics;
127 };
128 
129 }  // namespace cc
130 
131 #endif  // CC_METRICS_EVENT_METRICS_H_
132