MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / ui / events / latency_info.h
blob86ab04fb62489a5ea1e468a20f671f0b676333ea
1 // Copyright 2013 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.
5 #ifndef UI_EVENTS_LATENCY_INFO_H_
6 #define UI_EVENTS_LATENCY_INFO_H_
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/containers/small_map.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "base/trace_event/trace_event.h"
18 #include "ipc/ipc_param_traits.h"
19 #include "ui/events/events_base_export.h"
21 namespace ui {
23 enum LatencyComponentType {
24 // ---------------------------BEGIN COMPONENT-------------------------------
25 // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
26 // Timestamp when the input event is sent from RenderWidgetHost to renderer.
27 INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
28 // In threaded scrolling, main thread scroll listener update is async to
29 // scroll processing in impl thread. This is the timestamp when we consider
30 // the main thread scroll listener update is begun.
31 LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT,
32 // ---------------------------NORMAL COMPONENT-------------------------------
33 // The original timestamp of the touch event which converts to scroll update.
34 INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT,
35 // The original timestamp of the touch event which converts to the *first*
36 // scroll update in a scroll gesture sequence.
37 INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT,
38 // Original timestamp for input event (e.g. timestamp from kernel).
39 INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
40 // Timestamp when the UI event is created.
41 INPUT_EVENT_LATENCY_UI_COMPONENT,
42 // This is special component indicating there is rendering scheduled for
43 // the event associated with this LatencyInfo on main thread.
44 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT,
45 // This is special component indicating there is rendering scheduled for
46 // the event associated with this LatencyInfo on impl thread.
47 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT,
48 // Timestamp when a scroll update is forwarded to the main thread.
49 INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT,
50 // Timestamp when the event's ack is received by the RWH.
51 INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT,
52 // Frame number when a window snapshot was requested. The snapshot
53 // is taken when the rendering results actually reach the screen.
54 WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT,
55 // Timestamp when a tab is requested to be shown.
56 TAB_SHOW_COMPONENT,
57 // Timestamp when the frame is swapped in renderer.
58 INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT,
59 // Timestamp of when the browser process receives a buffer swap notification
60 // from the renderer.
61 INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT,
62 // Timestamp of when the gpu service began swap buffers, unlike
63 // INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT which measures after.
64 INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT,
65 // ---------------------------TERMINAL COMPONENT-----------------------------
66 // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
67 // Timestamp when the mouse event is acked from renderer and it does not
68 // cause any rendering scheduled.
69 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT,
70 // Timestamp when the mouse wheel event is acked from renderer and it does not
71 // cause any rendering scheduled.
72 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_WHEEL_COMPONENT,
73 // Timestamp when the keyboard event is acked from renderer and it does not
74 // cause any rendering scheduled.
75 INPUT_EVENT_LATENCY_TERMINATED_KEYBOARD_COMPONENT,
76 // Timestamp when the touch event is acked from renderer and it does not
77 // cause any rendering schedueld and does not generate any gesture event.
78 INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT,
79 // Timestamp when the gesture event is acked from renderer, and it does not
80 // cause any rendering schedueld.
81 INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT,
82 // Timestamp when the frame is swapped (i.e. when the rendering caused by
83 // input event actually takes effect).
84 INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT,
85 // This component indicates that the input causes a commit to be scheduled
86 // but the commit failed.
87 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT,
88 // This component indicates that the input causes a commit to be scheduled
89 // but the commit was aborted since it carried no new information.
90 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT,
91 // This component indicates that the input causes a swap to be scheduled
92 // but the swap failed.
93 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT,
94 LATENCY_COMPONENT_TYPE_LAST =
95 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT,
98 class EVENTS_BASE_EXPORT LatencyInfo {
99 public:
100 struct LatencyComponent {
101 // Nondecreasing number that can be used to determine what events happened
102 // in the component at the time this struct was sent on to the next
103 // component.
104 int64 sequence_number;
105 // Average time of events that happened in this component.
106 base::TimeTicks event_time;
107 // Count of events that happened in this component
108 uint32 event_count;
111 struct EVENTS_BASE_EXPORT InputCoordinate {
112 InputCoordinate();
113 InputCoordinate(float x, float y);
115 float x;
116 float y;
119 // Empirically determined constant based on a typical scroll sequence.
120 enum { kTypicalMaxComponentsPerLatencyInfo = 10 };
122 enum { kMaxCoalescedEventTimestamps = 2 };
123 enum { kMaxInputCoordinates = 2 };
125 // Map a Latency Component (with a component-specific int64 id) to a
126 // component info.
127 typedef base::SmallMap<
128 std::map<std::pair<LatencyComponentType, int64>, LatencyComponent>,
129 kTypicalMaxComponentsPerLatencyInfo> LatencyMap;
131 LatencyInfo();
132 ~LatencyInfo();
134 // For test only.
135 LatencyInfo(int64 trace_id, bool terminated);
137 // Returns true if the vector |latency_info| is valid. Returns false
138 // if it is not valid and log the |referring_msg|.
139 // This function is mainly used to check the latency_info vector that
140 // is passed between processes using IPC message has reasonable size
141 // so that we are confident the IPC message is not corrupted/compromised.
142 // This check will go away once the IPC system has better built-in scheme
143 // for corruption/compromise detection.
144 static bool Verify(const std::vector<LatencyInfo>& latency_info,
145 const char* referring_msg);
147 // Copy LatencyComponents with type |type| from |other| into |this|.
148 void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type);
150 // Add LatencyComponents that are in |other| but not in |this|.
151 void AddNewLatencyFrom(const LatencyInfo& other);
153 // Modifies the current sequence number for a component, and adds a new
154 // sequence number with the current timestamp.
155 void AddLatencyNumber(LatencyComponentType component,
156 int64 id,
157 int64 component_sequence_number);
159 // Similar to |AddLatencyNumber|, and also appends |trace_name_str| to
160 // the trace event's name.
161 // This function should only be called when adding a BEGIN component.
162 void AddLatencyNumberWithTraceName(LatencyComponentType component,
163 int64 id,
164 int64 component_sequence_number,
165 const char* trace_name_str);
167 // Modifies the current sequence number and adds a certain number of events
168 // for a specific component.
169 void AddLatencyNumberWithTimestamp(LatencyComponentType component,
170 int64 id,
171 int64 component_sequence_number,
172 base::TimeTicks time,
173 uint32 event_count);
175 // Returns true if the a component with |type| and |id| is found in
176 // the latency_components and the component is stored to |output| if
177 // |output| is not NULL. Returns false if no such component is found.
178 bool FindLatency(LatencyComponentType type,
179 int64 id,
180 LatencyComponent* output) const;
182 void RemoveLatency(LatencyComponentType type);
184 // Returns true if there is still room for keeping the |input_coordinate|,
185 // false otherwise.
186 bool AddInputCoordinate(const InputCoordinate& input_coordinate);
188 uint32 input_coordinates_size() const { return input_coordinates_size_; }
189 const InputCoordinate* input_coordinates() const {
190 return input_coordinates_;
193 // Returns true if there is still room for keeping the |timestamp|,
194 // false otherwise.
195 bool AddCoalescedEventTimestamp(double timestamp);
197 uint32 coalesced_events_size() const { return coalesced_events_size_; }
198 const double* timestamps_of_coalesced_events() const {
199 return timestamps_of_coalesced_events_;
202 const LatencyMap& latency_components() const { return latency_components_; }
204 bool terminated() const { return terminated_; }
205 int64 trace_id() const { return trace_id_; }
207 private:
208 void AddLatencyNumberWithTimestampImpl(LatencyComponentType component,
209 int64 id,
210 int64 component_sequence_number,
211 base::TimeTicks time,
212 uint32 event_count,
213 const char* trace_name_str);
215 // Converts latencyinfo into format that can be dumped into trace buffer.
216 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsTraceableData();
218 // Shown as part of the name of the trace event for this LatencyInfo.
219 // String is empty if no tracing is enabled.
220 std::string trace_name_;
222 LatencyMap latency_components_;
224 // These coordinates represent window coordinates of the original input event.
225 uint32 input_coordinates_size_;
226 InputCoordinate input_coordinates_[kMaxInputCoordinates];
228 uint32 coalesced_events_size_;
229 double timestamps_of_coalesced_events_[kMaxCoalescedEventTimestamps];
231 // The unique id for matching the ASYNC_BEGIN/END trace event.
232 int64 trace_id_;
233 // Whether a terminal component has been added.
234 bool terminated_;
236 friend struct IPC::ParamTraits<ui::LatencyInfo>;
239 } // namespace ui
241 #endif // UI_EVENTS_LATENCY_INFO_H_