James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | // Timestamp when the input event is received in plugin. |
| 24 | INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT, |
| 25 | // Timestamp when a scroll update for the main thread is begun. |
| 26 | INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT, |
| 27 | // ---------------------------NORMAL COMPONENT------------------------------- |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 28 | // Timestamp when the scroll update gesture event is sent from RWH to |
| 29 | // renderer. In Aura, touch event's LatencyInfo is carried over to the gesture |
| 30 | // event. So gesture event's INPUT_EVENT_LATENCY_RWH_COMPONENT is the |
| 31 | // timestamp when its original touch events is sent from RWH to renderer. |
| 32 | // In non-aura platform, INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT |
| 33 | // is the same as INPUT_EVENT_LATENCY_RWH_COMPONENT. |
| 34 | INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 35 | // The original timestamp of the touch event which converts to scroll update. |
| 36 | INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 37 | // Original timestamp for input event (e.g. timestamp from kernel). |
| 38 | INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, |
| 39 | // Timestamp when the UI event is created. |
| 40 | INPUT_EVENT_LATENCY_UI_COMPONENT, |
| 41 | // This is special component indicating there is rendering scheduled for |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 42 | // the event associated with this LatencyInfo. |
| 43 | INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 44 | // Timestamp when a scroll update is forwarded to the main thread. |
| 45 | INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT, |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 46 | // Timestamp when the touch event is acked. |
| 47 | INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 48 | // Frame number when a window snapshot was requested. The snapshot |
| 49 | // is taken when the rendering results actually reach the screen. |
| 50 | WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT, |
| 51 | // Frame number for a snapshot requested via |
| 52 | // gpuBenchmarking.beginWindowSnapshotPNG |
| 53 | // TODO(vkuzkokov): remove when patch adding this hits Stable |
| 54 | WINDOW_OLD_SNAPSHOT_FRAME_NUMBER_COMPONENT, |
| 55 | // Timestamp when a tab is requested to be shown. |
| 56 | TAB_SHOW_COMPONENT, |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 57 | // Timestamp of when the Browser process began compositing |
| 58 | INPUT_EVENT_BROWSER_COMPOSITE_COMPONENT, |
| 59 | // Timestamp of when the Browser process began swap buffers |
| 60 | INPUT_EVENT_BROWSER_SWAP_BUFFER_COMPONENT, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 61 | // Timestamp of when the gpu service began swap buffers, unlike |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 62 | // INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT which measure after |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 63 | INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, |
| 64 | // ---------------------------TERMINAL COMPONENT----------------------------- |
| 65 | // TERMINAL COMPONENT is when we show the latency end in chrome://tracing. |
| 66 | // Timestamp when the mouse event is acked from renderer and it does not |
| 67 | // cause any rendering scheduled. |
| 68 | INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT, |
| 69 | // Timestamp when the touch event is acked from renderer and it does not |
| 70 | // cause any rendering schedueld and does not generate any gesture event. |
| 71 | INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT, |
| 72 | // Timestamp when the gesture event is acked from renderer, and it does not |
| 73 | // cause any rendering schedueld. |
| 74 | INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT, |
| 75 | // Timestamp when the frame is swapped (i.e. when the rendering caused by |
| 76 | // input event actually takes effect). |
| 77 | INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, |
| 78 | // This component indicates that the input causes a commit to be scheduled |
| 79 | // but the commit failed. |
| 80 | INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT, |
| 81 | // This component indicates that the input causes a commit to be scheduled |
| 82 | // but the commit was aborted since it carried no new information. |
| 83 | INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT, |
| 84 | // This component indicates that the input causes a swap to be scheduled |
| 85 | // but the swap failed. |
| 86 | INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT, |
| 87 | // Timestamp when the input event is considered not cause any rendering |
| 88 | // damage in plugin and thus terminated. |
| 89 | INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT, |
| 90 | LATENCY_COMPONENT_TYPE_LAST = INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT |
| 91 | }; |
| 92 | |
| 93 | struct EVENTS_BASE_EXPORT LatencyInfo { |
| 94 | struct LatencyComponent { |
| 95 | // Nondecreasing number that can be used to determine what events happened |
| 96 | // in the component at the time this struct was sent on to the next |
| 97 | // component. |
| 98 | int64 sequence_number; |
| 99 | // Average time of events that happened in this component. |
| 100 | base::TimeTicks event_time; |
| 101 | // Count of events that happened in this component |
| 102 | uint32 event_count; |
| 103 | }; |
| 104 | |
| 105 | struct EVENTS_BASE_EXPORT InputCoordinate { |
| 106 | InputCoordinate(); |
| 107 | InputCoordinate(float x, float y); |
| 108 | |
| 109 | float x; |
| 110 | float y; |
| 111 | }; |
| 112 | |
| 113 | // Empirically determined constant based on a typical scroll sequence. |
Eric Seidel | 101a533 | 2015-02-23 14:42:04 -0800 | [diff] [blame] | 114 | enum { kTypicalMaxComponentsPerLatencyInfo = 9 }; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 115 | |
| 116 | enum { kMaxInputCoordinates = 2 }; |
| 117 | |
| 118 | // Map a Latency Component (with a component-specific int64 id) to a |
| 119 | // component info. |
| 120 | typedef base::SmallMap< |
| 121 | std::map<std::pair<LatencyComponentType, int64>, LatencyComponent>, |
| 122 | kTypicalMaxComponentsPerLatencyInfo> LatencyMap; |
| 123 | |
| 124 | LatencyInfo(); |
| 125 | |
| 126 | ~LatencyInfo(); |
| 127 | |
| 128 | // Returns true if the vector |latency_info| is valid. Returns false |
| 129 | // if it is not valid and log the |referring_msg|. |
| 130 | // This function is mainly used to check the latency_info vector that |
| 131 | // is passed between processes using IPC message has reasonable size |
| 132 | // so that we are confident the IPC message is not corrupted/compromised. |
| 133 | // This check will go away once the IPC system has better built-in scheme |
| 134 | // for corruption/compromise detection. |
| 135 | static bool Verify(const std::vector<LatencyInfo>& latency_info, |
| 136 | const char* referring_msg); |
| 137 | |
| 138 | // Copy LatencyComponents with type |type| from |other| into |this|. |
| 139 | void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type); |
| 140 | |
| 141 | // Add LatencyComponents that are in |other| but not in |this|. |
| 142 | void AddNewLatencyFrom(const LatencyInfo& other); |
| 143 | |
| 144 | // Modifies the current sequence number for a component, and adds a new |
| 145 | // sequence number with the current timestamp. |
| 146 | void AddLatencyNumber(LatencyComponentType component, |
| 147 | int64 id, |
| 148 | int64 component_sequence_number); |
| 149 | |
| 150 | // Modifies the current sequence number and adds a certain number of events |
| 151 | // for a specific component. |
| 152 | void AddLatencyNumberWithTimestamp(LatencyComponentType component, |
| 153 | int64 id, |
| 154 | int64 component_sequence_number, |
| 155 | base::TimeTicks time, |
| 156 | uint32 event_count); |
| 157 | |
| 158 | // Returns true if the a component with |type| and |id| is found in |
| 159 | // the latency_components and the component is stored to |output| if |
| 160 | // |output| is not NULL. Returns false if no such component is found. |
| 161 | bool FindLatency(LatencyComponentType type, |
| 162 | int64 id, |
| 163 | LatencyComponent* output) const; |
| 164 | |
| 165 | void RemoveLatency(LatencyComponentType type); |
| 166 | |
| 167 | void Clear(); |
| 168 | |
| 169 | // Records the |event_type| in trace buffer as TRACE_EVENT_ASYNC_STEP. |
| 170 | void TraceEventType(const char* event_type); |
| 171 | |
| 172 | LatencyMap latency_components; |
| 173 | |
| 174 | // These coordinates represent window coordinates of the original input event. |
| 175 | uint32 input_coordinates_size; |
| 176 | InputCoordinate input_coordinates[kMaxInputCoordinates]; |
| 177 | |
| 178 | // The unique id for matching the ASYNC_BEGIN/END trace event. |
| 179 | int64 trace_id; |
| 180 | // Whether a terminal component has been added. |
| 181 | bool terminated; |
| 182 | }; |
| 183 | |
| 184 | } // namespace ui |
| 185 | |
| 186 | #endif // UI_EVENTS_LATENCY_INFO_H_ |