12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#ifndef UI_EVENTS_LATENCY_INFO_H_
668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#define UI_EVENTS_LATENCY_INFO_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <utility>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/basictypes.h"
1223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "base/containers/small_map.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/events/events_base_export.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace ui {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)enum LatencyComponentType {
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // ---------------------------BEGIN COMPONENT-------------------------------
204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Timestamp when the input event is sent from RenderWidgetHost to renderer.
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Timestamp when the input event is received in plugin.
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT,
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // ---------------------------NORMAL COMPONENT-------------------------------
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Timestamp when the scroll update gesture event is sent from RWH to
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // renderer. In Aura, touch event's LatencyInfo is carried over to the gesture
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // event. So gesture event's INPUT_EVENT_LATENCY_RWH_COMPONENT is the
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // timestamp when its original touch events is sent from RWH to renderer.
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // In non-aura platform, INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // is the same as INPUT_EVENT_LATENCY_RWH_COMPONENT.
327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT,
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // The original timestamp of the touch event which converts to scroll update.
347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT,
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Original timestamp for input event (e.g. timestamp from kernel).
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Timestamp when the UI event is created.
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  INPUT_EVENT_LATENCY_UI_COMPONENT,
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // This is special component indicating there is rendering scheduled for
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // the event associated with this LatencyInfo.
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT,
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Timestamp when the touch event is acked.
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT,
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Frame number when a window snapshot was requested. The snapshot
45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // is taken when the rendering results actually reach the screen.
46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT,
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // ---------------------------TERMINAL COMPONENT-----------------------------
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Timestamp when the mouse event is acked from renderer and it does not
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // cause any rendering scheduled.
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT,
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Timestamp when the touch event is acked from renderer and it does not
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // cause any rendering schedueld and does not generate any gesture event.
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT,
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Timestamp when the gesture event is acked from renderer, and it does not
564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // cause any rendering schedueld.
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT,
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Timestamp when the frame is swapped (i.e. when the rendering caused by
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // input event actually takes effect).
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT,
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This component indicates that the input causes a commit to be scheduled
62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // but the commit failed.
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT,
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This component indicates that the input causes a swap to be scheduled
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // but the swap failed.
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT,
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This component indicates that the cached LatencyInfo number exceeds the
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // maximal allowed size.
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT,
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Timestamp when the input event is considered not cause any rendering
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // damage in plugin and thus terminated.
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT,
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  LATENCY_COMPONENT_TYPE_LAST = INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)struct EVENTS_BASE_EXPORT LatencyInfo {
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct LatencyComponent {
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Nondecreasing number that can be used to determine what events happened
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // in the component at the time this struct was sent on to the next
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // component.
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int64 sequence_number;
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Average time of events that happened in this component.
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::TimeTicks event_time;
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Count of events that happened in this component
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    uint32 event_count;
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Empirically determined constant based on a typical scroll sequence.
8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  enum { kTypicalMaxComponentsPerLatencyInfo = 6 };
9023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Map a Latency Component (with a component-specific int64 id) to a
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // component info.
9323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  typedef base::SmallMap<
9423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      std::map<std::pair<LatencyComponentType, int64>, LatencyComponent>,
9523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      kTypicalMaxComponentsPerLatencyInfo> LatencyMap;
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LatencyInfo();
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ~LatencyInfo();
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true if the vector |latency_info| is valid. Returns false
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // if it is not valid and log the |referring_msg|.
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This function is mainly used to check the latency_info vector that
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is passed between processes using IPC message has reasonable size
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // so that we are confident the IPC message is not corrupted/compromised.
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This check will go away once the IPC system has better built-in scheme
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for corruption/compromise detection.
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static bool Verify(const std::vector<LatencyInfo>& latency_info,
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const char* referring_msg);
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Copy LatencyComponents with type |type| from |other| into |this|.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type);
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1143240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // Add LatencyComponents that are in |other| but not in |this|.
1153240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void AddNewLatencyFrom(const LatencyInfo& other);
1163240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Modifies the current sequence number for a component, and adds a new
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // sequence number with the current timestamp.
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void AddLatencyNumber(LatencyComponentType component,
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                        int64 id,
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        int64 component_sequence_number);
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Modifies the current sequence number and adds a certain number of events
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // for a specific component.
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void AddLatencyNumberWithTimestamp(LatencyComponentType component,
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                     int64 id,
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                     int64 component_sequence_number,
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                     base::TimeTicks time,
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     uint32 event_count);
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Returns true if the a component with |type| and |id| is found in
132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the latency_components and the component is stored to |output| if
133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |output| is not NULL. Returns false if no such component is found.
134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool FindLatency(LatencyComponentType type,
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                   int64 id,
136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                   LatencyComponent* output) const;
137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void RemoveLatency(LatencyComponentType type);
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Clear();
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Records the |event_type| in trace buffer as TRACE_EVENT_ASYNC_STEP.
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void TraceEventType(const char* event_type);
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  LatencyMap latency_components;
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The unique id for matching the ASYNC_BEGIN/END trace event.
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 trace_id;
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Whether a terminal component has been added.
1494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool terminated;
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace ui
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif  // UI_EVENTS_LATENCY_INFO_H_
155