Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / events / latency_info.h
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.
4
5 #ifndef UI_EVENTS_LATENCY_INFO_H_
6 #define UI_EVENTS_LATENCY_INFO_H_
7
8 #include <utility>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/containers/small_map.h"
13 #include "base/time/time.h"
14 #include "ui/events/events_base_export.h"
15
16 namespace ui {
17
18 enum LatencyComponentType {
19   // ---------------------------BEGIN COMPONENT-------------------------------
20   // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
21   // Timestamp when the input event is sent from RenderWidgetHost to renderer.
22   INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
23   // ---------------------------NORMAL COMPONENT-------------------------------
24   // Timestamp when the scroll update gesture event is sent from RWH to
25   // renderer. In Aura, touch event's LatencyInfo is carried over to the gesture
26   // event. So gesture event's INPUT_EVENT_LATENCY_RWH_COMPONENT is the
27   // timestamp when its original touch events is sent from RWH to renderer.
28   // In non-aura platform, INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT
29   // is the same as INPUT_EVENT_LATENCY_RWH_COMPONENT.
30   INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT,
31   // The original timestamp of the touch event which converts to scroll update.
32   INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT,
33   // Original timestamp for input event (e.g. timestamp from kernel).
34   INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
35   // Timestamp when the UI event is created.
36   INPUT_EVENT_LATENCY_UI_COMPONENT,
37   // This is special component indicating there is rendering scheduled for
38   // the event associated with this LatencyInfo.
39   INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT,
40   // Timestamp when the touch event is acked.
41   INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT,
42   // Frame number when a window snapshot was requested. The snapshot
43   // is taken when the rendering results actually reach the screen.
44   WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT,
45   // ---------------------------TERMINAL COMPONENT-----------------------------
46   // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
47   // Timestamp when the mouse event is acked from renderer and it does not
48   // cause any rendering scheduled.
49   INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT,
50   // Timestamp when the touch event is acked from renderer and it does not
51   // cause any rendering schedueld and does not generate any gesture event.
52   INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT,
53   // Timestamp when the gesture event is acked from renderer, and it does not
54   // cause any rendering schedueld.
55   INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT,
56   // Timestamp when the frame is swapped (i.e. when the rendering caused by
57   // input event actually takes effect).
58   INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT,
59   // This component indicates that the input causes a commit to be scheduled
60   // but the commit failed.
61   INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT,
62   // This component indicates that the input causes a swap to be scheduled
63   // but the swap failed.
64   INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT,
65   // This component indicates that the cached LatencyInfo number exceeds the
66   // maximal allowed size.
67   LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT,
68   LATENCY_COMPONENT_TYPE_LAST = LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT
69 };
70
71 struct EVENTS_BASE_EXPORT LatencyInfo {
72   struct LatencyComponent {
73     // Nondecreasing number that can be used to determine what events happened
74     // in the component at the time this struct was sent on to the next
75     // component.
76     int64 sequence_number;
77     // Average time of events that happened in this component.
78     base::TimeTicks event_time;
79     // Count of events that happened in this component
80     uint32 event_count;
81   };
82
83   // Empirically determined constant based on a typical scroll sequence.
84   enum { kTypicalMaxComponentsPerLatencyInfo = 6 };
85
86   // Map a Latency Component (with a component-specific int64 id) to a
87   // component info.
88   typedef base::SmallMap<
89       std::map<std::pair<LatencyComponentType, int64>, LatencyComponent>,
90       kTypicalMaxComponentsPerLatencyInfo> LatencyMap;
91
92   LatencyInfo();
93
94   ~LatencyInfo();
95
96   // Returns true if the vector |latency_info| is valid. Returns false
97   // if it is not valid and log the |referring_msg|.
98   // This function is mainly used to check the latency_info vector that
99   // is passed between processes using IPC message has reasonable size
100   // so that we are confident the IPC message is not corrupted/compromised.
101   // This check will go away once the IPC system has better built-in scheme
102   // for corruption/compromise detection.
103   static bool Verify(const std::vector<LatencyInfo>& latency_info,
104                      const char* referring_msg);
105
106   // Copy LatencyComponents with type |type| from |other| into |this|.
107   void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type);
108
109   // Add LatencyComponents that are in |other| but not in |this|.
110   void AddNewLatencyFrom(const LatencyInfo& other);
111
112   // Modifies the current sequence number for a component, and adds a new
113   // sequence number with the current timestamp.
114   void AddLatencyNumber(LatencyComponentType component,
115                         int64 id,
116                         int64 component_sequence_number);
117
118   // Modifies the current sequence number and adds a certain number of events
119   // for a specific component.
120   void AddLatencyNumberWithTimestamp(LatencyComponentType component,
121                                      int64 id,
122                                      int64 component_sequence_number,
123                                      base::TimeTicks time,
124                                      uint32 event_count);
125
126   // Returns true if the a component with |type| and |id| is found in
127   // the latency_components and the component is stored to |output| if
128   // |output| is not NULL. Returns false if no such component is found.
129   bool FindLatency(LatencyComponentType type,
130                    int64 id,
131                    LatencyComponent* output) const;
132
133   void RemoveLatency(LatencyComponentType type);
134
135   void Clear();
136
137   // Records the |event_type| in trace buffer as TRACE_EVENT_ASYNC_STEP.
138   void TraceEventType(const char* event_type);
139
140   LatencyMap latency_components;
141   // The unique id for matching the ASYNC_BEGIN/END trace event.
142   int64 trace_id;
143   // Whether a terminal component has been added.
144   bool terminated;
145 };
146
147 }  // namespace ui
148
149 #endif  // UI_EVENTS_LATENCY_INFO_H_