Remove existing Skia suppressions
[chromium-blink-merge.git] / ash / touch / touch_hud_debug.cc
blobeb6c04da83a5b22b643834fd00e338f66818aa6c
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 #include "ash/touch/touch_hud_debug.h"
7 #include "ash/display/display_manager.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shell.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/skia/include/core/SkPath.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/events/event.h"
17 #include "ui/gfx/animation/animation_delegate.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/geometry/vector2d.h"
22 #include "ui/gfx/transform.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/layout/box_layout.h"
25 #include "ui/views/widget/widget.h"
27 #if defined(USE_X11)
28 #include <X11/extensions/XInput2.h>
29 #include <X11/Xlib.h>
31 #include "ui/events/devices/x11/device_data_manager_x11.h"
32 #endif
34 namespace ash {
36 const int kPointRadius = 20;
37 const SkColor kColors[] = {
38 SK_ColorYELLOW,
39 SK_ColorGREEN,
40 SK_ColorRED,
41 SK_ColorBLUE,
42 SK_ColorGRAY,
43 SK_ColorMAGENTA,
44 SK_ColorCYAN,
45 SK_ColorWHITE,
46 SK_ColorBLACK,
47 SkColorSetRGB(0xFF, 0x8C, 0x00),
48 SkColorSetRGB(0x8B, 0x45, 0x13),
49 SkColorSetRGB(0xFF, 0xDE, 0xAD),
51 const int kAlpha = 0x60;
52 const int kMaxPaths = arraysize(kColors);
53 const int kReducedScale = 10;
55 const char* GetTouchEventLabel(ui::EventType type) {
56 switch (type) {
57 case ui::ET_UNKNOWN:
58 return " ";
59 case ui::ET_TOUCH_PRESSED:
60 return "P";
61 case ui::ET_TOUCH_MOVED:
62 return "M";
63 case ui::ET_TOUCH_RELEASED:
64 return "R";
65 case ui::ET_TOUCH_CANCELLED:
66 return "C";
67 default:
68 break;
70 return "?";
73 int GetTrackingId(const ui::TouchEvent& event) {
74 if (!event.HasNativeEvent())
75 return 0;
76 #if defined(USE_X11)
77 ui::DeviceDataManagerX11* manager = ui::DeviceDataManagerX11::GetInstance();
78 double tracking_id;
79 if (manager->GetEventData(*event.native_event(),
80 ui::DeviceDataManagerX11::DT_TOUCH_TRACKING_ID,
81 &tracking_id)) {
82 return static_cast<int>(tracking_id);
84 #endif
85 return 0;
88 // A TouchPointLog represents a single touch-event of a touch point.
89 struct TouchPointLog {
90 public:
91 explicit TouchPointLog(const ui::TouchEvent& touch)
92 : id(touch.touch_id()),
93 type(touch.type()),
94 location(touch.root_location()),
95 timestamp(touch.time_stamp().InMillisecondsF()),
96 radius_x(touch.radius_x()),
97 radius_y(touch.radius_y()),
98 pressure(touch.force()),
99 tracking_id(GetTrackingId(touch)),
100 source_device(touch.source_device_id()) {
103 // Populates a dictionary value with all the information about the touch
104 // point.
105 scoped_ptr<base::DictionaryValue> GetAsDictionary() const {
106 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
108 value->SetInteger("id", id);
109 value->SetString("type", std::string(GetTouchEventLabel(type)));
110 value->SetString("location", location.ToString());
111 value->SetDouble("timestamp", timestamp);
112 value->SetDouble("radius_x", radius_x);
113 value->SetDouble("radius_y", radius_y);
114 value->SetDouble("pressure", pressure);
115 value->SetInteger("tracking_id", tracking_id);
116 value->SetInteger("source_device", source_device);
118 return value.Pass();
121 int id;
122 ui::EventType type;
123 gfx::Point location;
124 double timestamp;
125 float radius_x;
126 float radius_y;
127 float pressure;
128 int tracking_id;
129 int source_device;
132 // A TouchTrace keeps track of all the touch events of a single touch point
133 // (starting from a touch-press and ending at a touch-release or touch-cancel).
134 class TouchTrace {
135 public:
136 typedef std::vector<TouchPointLog>::iterator iterator;
137 typedef std::vector<TouchPointLog>::const_iterator const_iterator;
138 typedef std::vector<TouchPointLog>::reverse_iterator reverse_iterator;
139 typedef std::vector<TouchPointLog>::const_reverse_iterator
140 const_reverse_iterator;
142 TouchTrace() {
145 void AddTouchPoint(const ui::TouchEvent& touch) {
146 log_.push_back(TouchPointLog(touch));
149 const std::vector<TouchPointLog>& log() const { return log_; }
151 bool active() const {
152 return !log_.empty() && log_.back().type != ui::ET_TOUCH_RELEASED &&
153 log_.back().type != ui::ET_TOUCH_CANCELLED;
156 // Returns a list containing data from all events for the touch point.
157 scoped_ptr<base::ListValue> GetAsList() const {
158 scoped_ptr<base::ListValue> list(new base::ListValue());
159 for (const_iterator i = log_.begin(); i != log_.end(); ++i)
160 list->Append((*i).GetAsDictionary().release());
161 return list.Pass();
164 void Reset() {
165 log_.clear();
168 private:
169 std::vector<TouchPointLog> log_;
171 DISALLOW_COPY_AND_ASSIGN(TouchTrace);
174 // A TouchLog keeps track of all touch events of all touch points.
175 class TouchLog {
176 public:
177 TouchLog() : next_trace_index_(0) {
180 void AddTouchPoint(const ui::TouchEvent& touch) {
181 if (touch.type() == ui::ET_TOUCH_PRESSED)
182 StartTrace(touch);
183 AddToTrace(touch);
186 void Reset() {
187 next_trace_index_ = 0;
188 for (int i = 0; i < kMaxPaths; ++i)
189 traces_[i].Reset();
192 scoped_ptr<base::ListValue> GetAsList() const {
193 scoped_ptr<base::ListValue> list(new base::ListValue());
194 for (int i = 0; i < kMaxPaths; ++i) {
195 if (!traces_[i].log().empty())
196 list->Append(traces_[i].GetAsList().release());
198 return list.Pass();
201 int GetTraceIndex(int touch_id) const {
202 return touch_id_to_trace_index_.at(touch_id);
205 const TouchTrace* traces() const {
206 return traces_;
209 private:
210 void StartTrace(const ui::TouchEvent& touch) {
211 // Find the first inactive spot; otherwise, overwrite the one
212 // |next_trace_index_| is pointing to.
213 int old_trace_index = next_trace_index_;
214 do {
215 if (!traces_[next_trace_index_].active())
216 break;
217 next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
218 } while (next_trace_index_ != old_trace_index);
219 int touch_id = touch.touch_id();
220 traces_[next_trace_index_].Reset();
221 touch_id_to_trace_index_[touch_id] = next_trace_index_;
222 next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
225 void AddToTrace(const ui::TouchEvent& touch) {
226 int touch_id = touch.touch_id();
227 int trace_index = touch_id_to_trace_index_[touch_id];
228 traces_[trace_index].AddTouchPoint(touch);
231 TouchTrace traces_[kMaxPaths];
232 int next_trace_index_;
234 std::map<int, int> touch_id_to_trace_index_;
236 DISALLOW_COPY_AND_ASSIGN(TouchLog);
239 // TouchHudCanvas draws touch traces in |FULLSCREEN| and |REDUCED_SCALE| modes.
240 class TouchHudCanvas : public views::View {
241 public:
242 explicit TouchHudCanvas(const TouchLog& touch_log)
243 : touch_log_(touch_log),
244 scale_(1) {
245 SetPaintToLayer(true);
246 SetFillsBoundsOpaquely(false);
248 paint_.setStyle(SkPaint::kFill_Style);
251 ~TouchHudCanvas() override {}
253 void SetScale(int scale) {
254 if (scale_ == scale)
255 return;
256 scale_ = scale;
257 gfx::Transform transform;
258 transform.Scale(1. / scale_, 1. / scale_);
259 layer()->SetTransform(transform);
262 int scale() const { return scale_; }
264 void TouchPointAdded(int touch_id) {
265 int trace_index = touch_log_.GetTraceIndex(touch_id);
266 const TouchTrace& trace = touch_log_.traces()[trace_index];
267 const TouchPointLog& point = trace.log().back();
268 if (point.type == ui::ET_TOUCH_PRESSED)
269 StartedTrace(trace_index);
270 if (point.type != ui::ET_TOUCH_CANCELLED)
271 AddedPointToTrace(trace_index);
274 void Clear() {
275 for (int i = 0; i < kMaxPaths; ++i)
276 paths_[i].reset();
278 SchedulePaint();
281 private:
282 void StartedTrace(int trace_index) {
283 paths_[trace_index].reset();
284 colors_[trace_index] = SkColorSetA(kColors[trace_index], kAlpha);
287 void AddedPointToTrace(int trace_index) {
288 const TouchTrace& trace = touch_log_.traces()[trace_index];
289 const TouchPointLog& point = trace.log().back();
290 const gfx::Point& location = point.location;
291 SkScalar x = SkIntToScalar(location.x());
292 SkScalar y = SkIntToScalar(location.y());
293 SkPoint last;
294 if (!paths_[trace_index].getLastPt(&last) || x != last.x() ||
295 y != last.y()) {
296 paths_[trace_index].addCircle(x, y, SkIntToScalar(kPointRadius));
297 SchedulePaint();
301 // Overridden from views::View.
302 void OnPaint(gfx::Canvas* canvas) override {
303 for (int i = 0; i < kMaxPaths; ++i) {
304 if (paths_[i].countPoints() == 0)
305 continue;
306 paint_.setColor(colors_[i]);
307 canvas->DrawPath(paths_[i], paint_);
311 SkPaint paint_;
313 const TouchLog& touch_log_;
314 SkPath paths_[kMaxPaths];
315 SkColor colors_[kMaxPaths];
317 int scale_;
319 DISALLOW_COPY_AND_ASSIGN(TouchHudCanvas);
322 TouchHudDebug::TouchHudDebug(aura::Window* initial_root)
323 : TouchObserverHUD(initial_root),
324 mode_(FULLSCREEN),
325 touch_log_(new TouchLog()),
326 canvas_(NULL),
327 label_container_(NULL) {
328 const gfx::Display& display =
329 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id());
331 views::View* content = widget()->GetContentsView();
333 canvas_ = new TouchHudCanvas(*touch_log_);
334 content->AddChildView(canvas_);
336 const gfx::Size& display_size = display.size();
337 canvas_->SetSize(display_size);
339 label_container_ = new views::View;
340 label_container_->SetLayoutManager(new views::BoxLayout(
341 views::BoxLayout::kVertical, 0, 0, 0));
343 for (int i = 0; i < kMaxTouchPoints; ++i) {
344 touch_labels_[i] = new views::Label;
345 touch_labels_[i]->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
346 touch_labels_[i]->SetShadows(gfx::ShadowValues(
347 1, gfx::ShadowValue(gfx::Vector2d(1, 1), 0, SK_ColorWHITE)));
348 label_container_->AddChildView(touch_labels_[i]);
350 label_container_->SetX(0);
351 label_container_->SetY(display_size.height() / kReducedScale);
352 label_container_->SetSize(label_container_->GetPreferredSize());
353 label_container_->SetVisible(false);
354 content->AddChildView(label_container_);
357 TouchHudDebug::~TouchHudDebug() {
360 // static
361 scoped_ptr<base::DictionaryValue> TouchHudDebug::GetAllAsDictionary() {
362 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
363 aura::Window::Windows roots = Shell::GetInstance()->GetAllRootWindows();
364 for (aura::Window::Windows::iterator iter = roots.begin();
365 iter != roots.end(); ++iter) {
366 RootWindowController* controller = GetRootWindowController(*iter);
367 TouchHudDebug* hud = controller->touch_hud_debug();
368 if (hud) {
369 scoped_ptr<base::ListValue> list = hud->GetLogAsList();
370 if (!list->empty())
371 value->Set(base::Int64ToString(hud->display_id()), list.release());
374 return value.Pass();
377 void TouchHudDebug::ChangeToNextMode() {
378 switch (mode_) {
379 case FULLSCREEN:
380 SetMode(REDUCED_SCALE);
381 break;
382 case REDUCED_SCALE:
383 SetMode(INVISIBLE);
384 break;
385 case INVISIBLE:
386 SetMode(FULLSCREEN);
387 break;
391 scoped_ptr<base::ListValue> TouchHudDebug::GetLogAsList() const {
392 return touch_log_->GetAsList();
395 void TouchHudDebug::Clear() {
396 if (widget()->IsVisible()) {
397 canvas_->Clear();
398 for (int i = 0; i < kMaxTouchPoints; ++i)
399 touch_labels_[i]->SetText(base::string16());
400 label_container_->SetSize(label_container_->GetPreferredSize());
404 void TouchHudDebug::SetMode(Mode mode) {
405 if (mode_ == mode)
406 return;
407 mode_ = mode;
408 switch (mode) {
409 case FULLSCREEN:
410 label_container_->SetVisible(false);
411 canvas_->SetVisible(true);
412 canvas_->SetScale(1);
413 canvas_->SchedulePaint();
414 widget()->Show();
415 break;
416 case REDUCED_SCALE:
417 label_container_->SetVisible(true);
418 canvas_->SetVisible(true);
419 canvas_->SetScale(kReducedScale);
420 canvas_->SchedulePaint();
421 widget()->Show();
422 break;
423 case INVISIBLE:
424 widget()->Hide();
425 break;
429 void TouchHudDebug::UpdateTouchPointLabel(int index) {
430 int trace_index = touch_log_->GetTraceIndex(index);
431 const TouchTrace& trace = touch_log_->traces()[trace_index];
432 TouchTrace::const_reverse_iterator point = trace.log().rbegin();
433 ui::EventType touch_status = point->type;
434 float touch_radius = std::max(point->radius_x, point->radius_y);
435 while (point != trace.log().rend() && point->type == ui::ET_TOUCH_CANCELLED)
436 point++;
437 DCHECK(point != trace.log().rend());
438 gfx::Point touch_position = point->location;
440 std::string string = base::StringPrintf("%2d: %s %s (%.4f)",
441 index,
442 GetTouchEventLabel(touch_status),
443 touch_position.ToString().c_str(),
444 touch_radius);
445 touch_labels_[index]->SetText(base::UTF8ToUTF16(string));
448 void TouchHudDebug::OnTouchEvent(ui::TouchEvent* event) {
449 if (event->touch_id() >= kMaxTouchPoints)
450 return;
452 touch_log_->AddTouchPoint(*event);
453 canvas_->TouchPointAdded(event->touch_id());
454 UpdateTouchPointLabel(event->touch_id());
455 label_container_->SetSize(label_container_->GetPreferredSize());
458 void TouchHudDebug::OnDisplayMetricsChanged(const gfx::Display& display,
459 uint32_t metrics) {
460 TouchObserverHUD::OnDisplayMetricsChanged(display, metrics);
462 if (display.id() != display_id() || !(metrics & DISPLAY_METRIC_BOUNDS))
463 return;
464 const gfx::Size& size = display.size();
465 canvas_->SetSize(size);
466 label_container_->SetY(size.height() / kReducedScale);
469 void TouchHudDebug::SetHudForRootWindowController(
470 RootWindowController* controller) {
471 controller->set_touch_hud_debug(this);
474 void TouchHudDebug::UnsetHudForRootWindowController(
475 RootWindowController* controller) {
476 controller->set_touch_hud_debug(NULL);
479 } // namespace ash