Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / ash / touch / touch_uma.cc
blob97415355df6e7c9c33cc03232d545cdf4ab76f70
1 // Copyright (c) 2012 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 #include "ash/touch/touch_uma.h"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/shell.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/gfx/point_conversions.h"
19 #if defined(USE_X11)
20 #include <X11/extensions/XInput2.h>
21 #include <X11/Xlib.h>
22 #endif
24 namespace {
26 struct WindowTouchDetails {
27 // Move and start times of the touch points. The key is the touch-id.
28 std::map<int, base::TimeDelta> last_move_time_;
29 std::map<int, base::TimeDelta> last_start_time_;
31 // The first and last positions of the touch points.
32 std::map<int, gfx::Point> start_touch_position_;
33 std::map<int, gfx::Point> last_touch_position_;
35 // Last time-stamp of the last touch-end event.
36 base::TimeDelta last_release_time_;
38 // Stores the time of the last touch released on this window (if there was a
39 // multi-touch gesture on the window, then this is the release-time of the
40 // last touch on the window).
41 base::TimeDelta last_mt_time_;
44 DEFINE_OWNED_WINDOW_PROPERTY_KEY(WindowTouchDetails,
45 kWindowTouchDetails,
46 NULL);
49 namespace ash {
51 // static
52 TouchUMA* TouchUMA::GetInstance() {
53 return Singleton<TouchUMA>::get();
56 void TouchUMA::RecordGestureEvent(aura::Window* target,
57 const ui::GestureEvent& event) {
58 GestureActionType action = FindGestureActionType(target, event);
59 RecordGestureAction(action);
61 if (event.type() == ui::ET_GESTURE_END &&
62 event.details().touch_points() == 2) {
63 WindowTouchDetails* details = target->GetProperty(kWindowTouchDetails);
64 if (!details) {
65 LOG(ERROR) << "Window received gesture events without receiving any touch"
66 " events";
67 return;
69 details->last_mt_time_ = event.time_stamp();
73 void TouchUMA::RecordGestureAction(GestureActionType action) {
74 if (action == GESTURE_UNKNOWN || action >= GESTURE_ACTION_COUNT)
75 return;
76 UMA_HISTOGRAM_ENUMERATION("Ash.GestureTarget", action,
77 GESTURE_ACTION_COUNT);
80 void TouchUMA::RecordTouchEvent(aura::Window* target,
81 const ui::TouchEvent& event) {
82 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchRadius",
83 static_cast<int>(std::max(event.radius_x(), event.radius_y())),
84 1, 500, 100);
86 UpdateTouchState(event);
88 WindowTouchDetails* details = target->GetProperty(kWindowTouchDetails);
89 if (!details) {
90 details = new WindowTouchDetails;
91 target->SetProperty(kWindowTouchDetails, details);
94 // Record the location of the touch points.
95 const int kBucketCountForLocation = 100;
96 const gfx::Rect bounds = target->GetRootWindow()->bounds();
97 const int bucket_size_x = std::max(1,
98 bounds.width() / kBucketCountForLocation);
99 const int bucket_size_y = std::max(1,
100 bounds.height() / kBucketCountForLocation);
102 gfx::Point position = event.root_location();
104 // Prefer raw event location (when available) over calibrated location.
105 if (event.HasNativeEvent()) {
106 position = ui::EventLocationFromNative(event.native_event());
107 position = gfx::ToFlooredPoint(
108 gfx::ScalePoint(position, 1. / target->layer()->device_scale_factor()));
111 position.set_x(std::min(bounds.width() - 1, std::max(0, position.x())));
112 position.set_y(std::min(bounds.height() - 1, std::max(0, position.y())));
114 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionX",
115 position.x() / bucket_size_x,
116 0, kBucketCountForLocation, kBucketCountForLocation + 1);
117 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionY",
118 position.y() / bucket_size_y,
119 0, kBucketCountForLocation, kBucketCountForLocation + 1);
121 if (event.type() == ui::ET_TOUCH_PRESSED) {
122 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
123 UMA_TOUCHSCREEN_TAP_DOWN);
125 details->last_start_time_[event.touch_id()] = event.time_stamp();
126 details->start_touch_position_[event.touch_id()] = event.root_location();
127 details->last_touch_position_[event.touch_id()] = event.location();
129 if (details->last_release_time_.ToInternalValue()) {
130 // Measuring the interval between a touch-release and the next
131 // touch-start is probably less useful when doing multi-touch (e.g.
132 // gestures, or multi-touch friendly apps). So count this only if the user
133 // hasn't done any multi-touch during the last 30 seconds.
134 base::TimeDelta diff = event.time_stamp() - details->last_mt_time_;
135 if (diff.InSeconds() > 30) {
136 base::TimeDelta gap = event.time_stamp() - details->last_release_time_;
137 UMA_HISTOGRAM_COUNTS_10000("Ash.TouchStartAfterEnd",
138 gap.InMilliseconds());
142 // Record the number of touch-points currently active for the window.
143 const int kMaxTouchPoints = 10;
144 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.ActiveTouchPoints",
145 details->last_start_time_.size(),
146 1, kMaxTouchPoints, kMaxTouchPoints + 1);
147 } else if (event.type() == ui::ET_TOUCH_RELEASED) {
148 if (details->last_start_time_.count(event.touch_id())) {
149 base::TimeDelta duration = event.time_stamp() -
150 details->last_start_time_[event.touch_id()];
151 // Look for touches that were [almost] stationary for a long time.
152 const double kLongStationaryTouchDuration = 10;
153 const int kLongStationaryTouchDistanceSquared = 100;
154 if (duration.InSecondsF() > kLongStationaryTouchDuration) {
155 gfx::Vector2d distance = event.root_location() -
156 details->start_touch_position_[event.touch_id()];
157 if (distance.LengthSquared() < kLongStationaryTouchDistanceSquared) {
158 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.StationaryTouchDuration",
159 duration.InSeconds(),
160 kLongStationaryTouchDuration,
161 1000,
162 20);
166 details->last_start_time_.erase(event.touch_id());
167 details->last_move_time_.erase(event.touch_id());
168 details->start_touch_position_.erase(event.touch_id());
169 details->last_touch_position_.erase(event.touch_id());
170 details->last_release_time_ = event.time_stamp();
171 } else if (event.type() == ui::ET_TOUCH_MOVED) {
172 int distance = 0;
173 if (details->last_touch_position_.count(event.touch_id())) {
174 gfx::Point lastpos = details->last_touch_position_[event.touch_id()];
175 distance =
176 std::abs(lastpos.x() - event.x()) + std::abs(lastpos.y() - event.y());
179 if (details->last_move_time_.count(event.touch_id())) {
180 base::TimeDelta move_delay = event.time_stamp() -
181 details->last_move_time_[event.touch_id()];
182 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveInterval",
183 move_delay.InMilliseconds(),
184 1, 50, 25);
187 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveSteps", distance, 1, 1000, 50);
189 details->last_move_time_[event.touch_id()] = event.time_stamp();
190 details->last_touch_position_[event.touch_id()] = event.location();
194 TouchUMA::TouchUMA()
195 : is_single_finger_gesture_(false),
196 touch_in_progress_(false),
197 burst_length_(0) {
200 TouchUMA::~TouchUMA() {
203 void TouchUMA::UpdateTouchState(const ui::TouchEvent& event) {
204 if (event.type() == ui::ET_TOUCH_PRESSED) {
205 if (!touch_in_progress_) {
206 is_single_finger_gesture_ = true;
207 base::TimeDelta difference = event.time_stamp() - last_touch_down_time_;
208 if (difference > base::TimeDelta::FromMilliseconds(250)) {
209 if (burst_length_) {
210 UMA_HISTOGRAM_COUNTS_100("Ash.TouchStartBurst",
211 std::min(burst_length_, 100));
213 burst_length_ = 1;
214 } else {
215 ++burst_length_;
217 } else {
218 is_single_finger_gesture_ = false;
220 touch_in_progress_ = true;
221 last_touch_down_time_ = event.time_stamp();
222 } else if (event.type() == ui::ET_TOUCH_RELEASED) {
223 if (!aura::Env::GetInstance()->is_touch_down())
224 touch_in_progress_ = false;
228 TouchUMA::GestureActionType TouchUMA::FindGestureActionType(
229 aura::Window* window,
230 const ui::GestureEvent& event) {
231 if (!window || window->GetRootWindow() == window) {
232 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
233 return GESTURE_BEZEL_SCROLL;
234 if (event.type() == ui::ET_GESTURE_BEGIN)
235 return GESTURE_BEZEL_DOWN;
236 return GESTURE_UNKNOWN;
239 std::string name = window ? window->name() : std::string();
241 const char kDesktopBackgroundView[] = "DesktopBackgroundView";
242 if (name == kDesktopBackgroundView) {
243 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
244 return GESTURE_DESKTOP_SCROLL;
245 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
246 return GESTURE_DESKTOP_PINCH;
247 return GESTURE_UNKNOWN;
250 const char kWebPage[] = "RenderWidgetHostViewAura";
251 if (name == kWebPage) {
252 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
253 return GESTURE_WEBPAGE_PINCH;
254 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
255 return GESTURE_WEBPAGE_SCROLL;
256 if (event.type() == ui::ET_GESTURE_TAP)
257 return GESTURE_WEBPAGE_TAP;
258 return GESTURE_UNKNOWN;
261 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
262 if (!widget)
263 return GESTURE_UNKNOWN;
265 views::View* view = widget->GetRootView()->
266 GetEventHandlerForPoint(event.location());
267 if (!view)
268 return GESTURE_UNKNOWN;
270 name = view->GetClassName();
272 const char kTabStrip[] = "TabStrip";
273 const char kTab[] = "BrowserTab";
274 if (name == kTabStrip || name == kTab) {
275 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
276 return GESTURE_TABSTRIP_SCROLL;
277 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
278 return GESTURE_TABSTRIP_PINCH;
279 if (event.type() == ui::ET_GESTURE_TAP)
280 return GESTURE_TABSTRIP_TAP;
281 return GESTURE_UNKNOWN;
284 const char kOmnibox[] = "BrowserOmniboxViewViews";
285 if (name == kOmnibox) {
286 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
287 return GESTURE_OMNIBOX_SCROLL;
288 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
289 return GESTURE_OMNIBOX_PINCH;
290 return GESTURE_UNKNOWN;
293 return GESTURE_UNKNOWN;
296 } // namespace ash