touch_hud_debug.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #include "ash/touch/touch_hud_debug.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include "ash/root_window_controller.h"
  9. #include "ash/shell.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "third_party/skia/include/core/SkPath.h"
  13. #include "ui/aura/window_event_dispatcher.h"
  14. #include "ui/compositor/layer.h"
  15. #include "ui/display/display.h"
  16. #include "ui/display/manager/display_manager.h"
  17. #include "ui/events/event.h"
  18. #include "ui/gfx/canvas.h"
  19. #include "ui/gfx/color_utils.h"
  20. #include "ui/gfx/geometry/size.h"
  21. #include "ui/gfx/geometry/transform.h"
  22. #include "ui/gfx/geometry/vector2d.h"
  23. #include "ui/views/controls/label.h"
  24. #include "ui/views/layout/box_layout.h"
  25. #include "ui/views/widget/widget.h"
  26. namespace ash {
  27. const int kPointRadius = 20;
  28. const SkColor kColors[] = {
  29. SK_ColorYELLOW,
  30. SK_ColorGREEN,
  31. SK_ColorRED,
  32. SK_ColorBLUE,
  33. SK_ColorGRAY,
  34. SK_ColorMAGENTA,
  35. SK_ColorCYAN,
  36. SK_ColorWHITE,
  37. SK_ColorBLACK,
  38. SkColorSetRGB(0xFF, 0x8C, 0x00),
  39. SkColorSetRGB(0x8B, 0x45, 0x13),
  40. SkColorSetRGB(0xFF, 0xDE, 0xAD),
  41. };
  42. const int kAlpha = 0x60;
  43. const int kMaxPaths = std::size(kColors);
  44. const int kReducedScale = 10;
  45. const char* GetTouchEventLabel(ui::EventType type) {
  46. switch (type) {
  47. case ui::ET_UNKNOWN:
  48. return " ";
  49. case ui::ET_TOUCH_PRESSED:
  50. return "P";
  51. case ui::ET_TOUCH_MOVED:
  52. return "M";
  53. case ui::ET_TOUCH_RELEASED:
  54. return "R";
  55. case ui::ET_TOUCH_CANCELLED:
  56. return "C";
  57. default:
  58. break;
  59. }
  60. return "?";
  61. }
  62. // A TouchPointLog represents a single touch-event of a touch point.
  63. struct TouchPointLog {
  64. public:
  65. explicit TouchPointLog(const ui::TouchEvent& touch)
  66. : type(touch.type()),
  67. location(touch.root_location()),
  68. radius_x(touch.pointer_details().radius_x),
  69. radius_y(touch.pointer_details().radius_y) {}
  70. ui::EventType type;
  71. gfx::Point location;
  72. float radius_x;
  73. float radius_y;
  74. };
  75. // A TouchTrace keeps track of all the touch events of a single touch point
  76. // (starting from a touch-press and ending at a touch-release or touch-cancel).
  77. class TouchTrace {
  78. public:
  79. typedef std::vector<TouchPointLog>::iterator iterator;
  80. typedef std::vector<TouchPointLog>::const_iterator const_iterator;
  81. typedef std::vector<TouchPointLog>::reverse_iterator reverse_iterator;
  82. typedef std::vector<TouchPointLog>::const_reverse_iterator
  83. const_reverse_iterator;
  84. TouchTrace() = default;
  85. TouchTrace(const TouchTrace&) = delete;
  86. TouchTrace& operator=(const TouchTrace&) = delete;
  87. void AddTouchPoint(const ui::TouchEvent& touch) {
  88. log_.push_back(TouchPointLog(touch));
  89. }
  90. const std::vector<TouchPointLog>& log() const { return log_; }
  91. bool active() const {
  92. return !log_.empty() && log_.back().type != ui::ET_TOUCH_RELEASED &&
  93. log_.back().type != ui::ET_TOUCH_CANCELLED;
  94. }
  95. void Reset() { log_.clear(); }
  96. private:
  97. std::vector<TouchPointLog> log_;
  98. };
  99. // A TouchLog keeps track of all touch events of all touch points.
  100. class TouchLog {
  101. public:
  102. TouchLog() : next_trace_index_(0) {}
  103. TouchLog(const TouchLog&) = delete;
  104. TouchLog& operator=(const TouchLog&) = delete;
  105. void AddTouchPoint(const ui::TouchEvent& touch) {
  106. if (touch.type() == ui::ET_TOUCH_PRESSED)
  107. StartTrace(touch);
  108. AddToTrace(touch);
  109. }
  110. void Reset() {
  111. next_trace_index_ = 0;
  112. for (int i = 0; i < kMaxPaths; ++i)
  113. traces_[i].Reset();
  114. }
  115. int GetTraceIndex(int touch_id) const {
  116. return touch_id_to_trace_index_.at(touch_id);
  117. }
  118. const TouchTrace* traces() const { return traces_; }
  119. private:
  120. void StartTrace(const ui::TouchEvent& touch) {
  121. // Find the first inactive spot; otherwise, overwrite the one
  122. // |next_trace_index_| is pointing to.
  123. int old_trace_index = next_trace_index_;
  124. do {
  125. if (!traces_[next_trace_index_].active())
  126. break;
  127. next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
  128. } while (next_trace_index_ != old_trace_index);
  129. int touch_id = touch.pointer_details().id;
  130. traces_[next_trace_index_].Reset();
  131. touch_id_to_trace_index_[touch_id] = next_trace_index_;
  132. next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
  133. }
  134. void AddToTrace(const ui::TouchEvent& touch) {
  135. int touch_id = touch.pointer_details().id;
  136. int trace_index = touch_id_to_trace_index_[touch_id];
  137. traces_[trace_index].AddTouchPoint(touch);
  138. }
  139. TouchTrace traces_[kMaxPaths];
  140. int next_trace_index_;
  141. std::map<int, int> touch_id_to_trace_index_;
  142. };
  143. // TouchHudCanvas draws touch traces in |FULLSCREEN| and |REDUCED_SCALE| modes.
  144. class TouchHudCanvas : public views::View {
  145. public:
  146. explicit TouchHudCanvas(const TouchLog& touch_log)
  147. : touch_log_(touch_log), scale_(1) {
  148. SetPaintToLayer();
  149. layer()->SetFillsBoundsOpaquely(false);
  150. flags_.setStyle(cc::PaintFlags::kFill_Style);
  151. }
  152. TouchHudCanvas(const TouchHudCanvas&) = delete;
  153. TouchHudCanvas& operator=(const TouchHudCanvas&) = delete;
  154. ~TouchHudCanvas() override = default;
  155. void SetScale(int scale) {
  156. if (scale_ == scale)
  157. return;
  158. scale_ = scale;
  159. gfx::Transform transform;
  160. transform.Scale(1. / scale_, 1. / scale_);
  161. layer()->SetTransform(transform);
  162. }
  163. int scale() const { return scale_; }
  164. void TouchPointAdded(int touch_id) {
  165. int trace_index = touch_log_.GetTraceIndex(touch_id);
  166. const TouchTrace& trace = touch_log_.traces()[trace_index];
  167. const TouchPointLog& point = trace.log().back();
  168. if (point.type == ui::ET_TOUCH_PRESSED)
  169. StartedTrace(trace_index);
  170. if (point.type != ui::ET_TOUCH_CANCELLED)
  171. AddedPointToTrace(trace_index);
  172. }
  173. void Clear() {
  174. for (int i = 0; i < kMaxPaths; ++i)
  175. paths_[i].reset();
  176. SchedulePaint();
  177. }
  178. private:
  179. void StartedTrace(int trace_index) {
  180. paths_[trace_index].reset();
  181. colors_[trace_index] = SkColorSetA(kColors[trace_index], kAlpha);
  182. }
  183. void AddedPointToTrace(int trace_index) {
  184. const TouchTrace& trace = touch_log_.traces()[trace_index];
  185. const TouchPointLog& point = trace.log().back();
  186. const gfx::Point& location = point.location;
  187. SkScalar x = SkIntToScalar(location.x());
  188. SkScalar y = SkIntToScalar(location.y());
  189. SkPoint last;
  190. if (!paths_[trace_index].getLastPt(&last) || x != last.x() ||
  191. y != last.y()) {
  192. paths_[trace_index].addCircle(x, y, SkIntToScalar(kPointRadius));
  193. SchedulePaint();
  194. }
  195. }
  196. // Overridden from views::View.
  197. void OnPaint(gfx::Canvas* canvas) override {
  198. for (int i = 0; i < kMaxPaths; ++i) {
  199. if (paths_[i].countPoints() == 0)
  200. continue;
  201. flags_.setColor(colors_[i]);
  202. canvas->DrawPath(paths_[i], flags_);
  203. }
  204. }
  205. cc::PaintFlags flags_;
  206. const TouchLog& touch_log_;
  207. SkPath paths_[kMaxPaths];
  208. SkColor colors_[kMaxPaths];
  209. int scale_;
  210. };
  211. TouchHudDebug::TouchHudDebug(aura::Window* initial_root)
  212. : TouchObserverHud(initial_root, "TouchHudDebug"),
  213. mode_(FULLSCREEN),
  214. touch_log_(new TouchLog()),
  215. canvas_(new TouchHudCanvas(*touch_log_)),
  216. label_container_(new views::View()) {
  217. const display::Display& display =
  218. Shell::Get()->display_manager()->GetDisplayForId(display_id());
  219. views::View* content = widget()->GetContentsView();
  220. content->AddChildView(canvas_);
  221. const gfx::Size& display_size = display.size();
  222. canvas_->SetSize(display_size);
  223. label_container_->SetLayoutManager(std::make_unique<views::BoxLayout>(
  224. views::BoxLayout::Orientation::kVertical));
  225. constexpr SkColor kShadowColor = SK_ColorWHITE;
  226. const SkColor label_color =
  227. color_utils::GetColorWithMaxContrast(kShadowColor);
  228. for (int i = 0; i < kMaxTouchPoints; ++i) {
  229. touch_labels_[i] = new views::Label;
  230. touch_labels_[i]->SetEnabledColor(label_color);
  231. touch_labels_[i]->SetBackgroundColor(SK_ColorTRANSPARENT);
  232. touch_labels_[i]->SetShadows(gfx::ShadowValues(
  233. 1, gfx::ShadowValue(gfx::Vector2d(1, 1), 0, kShadowColor)));
  234. label_container_->AddChildView(touch_labels_[i]);
  235. }
  236. label_container_->SetX(0);
  237. label_container_->SetY(display_size.height() / kReducedScale);
  238. label_container_->SetSize(label_container_->GetPreferredSize());
  239. label_container_->SetVisible(false);
  240. content->AddChildView(label_container_);
  241. }
  242. TouchHudDebug::~TouchHudDebug() = default;
  243. void TouchHudDebug::ChangeToNextMode() {
  244. switch (mode_) {
  245. case FULLSCREEN:
  246. SetMode(REDUCED_SCALE);
  247. break;
  248. case REDUCED_SCALE:
  249. SetMode(INVISIBLE);
  250. break;
  251. case INVISIBLE:
  252. SetMode(FULLSCREEN);
  253. break;
  254. }
  255. }
  256. void TouchHudDebug::Clear() {
  257. if (widget()->IsVisible()) {
  258. canvas_->Clear();
  259. for (int i = 0; i < kMaxTouchPoints; ++i)
  260. touch_labels_[i]->SetText(std::u16string());
  261. label_container_->SetSize(label_container_->GetPreferredSize());
  262. }
  263. }
  264. void TouchHudDebug::SetMode(Mode mode) {
  265. if (mode_ == mode)
  266. return;
  267. mode_ = mode;
  268. switch (mode) {
  269. case FULLSCREEN:
  270. label_container_->SetVisible(false);
  271. canvas_->SetVisible(true);
  272. canvas_->SetScale(1);
  273. canvas_->SchedulePaint();
  274. widget()->Show();
  275. break;
  276. case REDUCED_SCALE:
  277. label_container_->SetVisible(true);
  278. canvas_->SetVisible(true);
  279. canvas_->SetScale(kReducedScale);
  280. canvas_->SchedulePaint();
  281. widget()->Show();
  282. break;
  283. case INVISIBLE:
  284. widget()->Hide();
  285. break;
  286. }
  287. }
  288. void TouchHudDebug::UpdateTouchPointLabel(int index) {
  289. int trace_index = touch_log_->GetTraceIndex(index);
  290. const TouchTrace& trace = touch_log_->traces()[trace_index];
  291. TouchTrace::const_reverse_iterator point = trace.log().rbegin();
  292. ui::EventType touch_status = point->type;
  293. float touch_radius = std::max(point->radius_x, point->radius_y);
  294. while (point != trace.log().rend() && point->type == ui::ET_TOUCH_CANCELLED)
  295. point++;
  296. DCHECK(point != trace.log().rend());
  297. gfx::Point touch_position = point->location;
  298. std::string string = base::StringPrintf(
  299. "%2d: %s %s (%.4f)", index, GetTouchEventLabel(touch_status),
  300. touch_position.ToString().c_str(), touch_radius);
  301. touch_labels_[index]->SetText(base::UTF8ToUTF16(string));
  302. }
  303. void TouchHudDebug::OnTouchEvent(ui::TouchEvent* event) {
  304. if (event->pointer_details().id >= kMaxTouchPoints)
  305. return;
  306. touch_log_->AddTouchPoint(*event);
  307. canvas_->TouchPointAdded(event->pointer_details().id);
  308. UpdateTouchPointLabel(event->pointer_details().id);
  309. label_container_->SetSize(label_container_->GetPreferredSize());
  310. }
  311. void TouchHudDebug::OnDisplayMetricsChanged(const display::Display& display,
  312. uint32_t metrics) {
  313. TouchObserverHud::OnDisplayMetricsChanged(display, metrics);
  314. if (display.id() != display_id() || !(metrics & DISPLAY_METRIC_BOUNDS))
  315. return;
  316. const gfx::Size& size = display.size();
  317. canvas_->SetSize(size);
  318. label_container_->SetY(size.height() / kReducedScale);
  319. }
  320. void TouchHudDebug::SetHudForRootWindowController(
  321. RootWindowController* controller) {
  322. controller->set_touch_hud_debug(this);
  323. }
  324. void TouchHudDebug::UnsetHudForRootWindowController(
  325. RootWindowController* controller) {
  326. controller->set_touch_hud_debug(NULL);
  327. }
  328. } // namespace ash