FlameGraphView.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2021, Nicholas Hollett <niax@niax.co.uk>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "FlameGraphView.h"
  7. #include "DevTools/Profiler/Profile.h"
  8. #include "LibGfx/Forward.h"
  9. #include <AK/Function.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGUI/Widget.h>
  12. #include <LibGfx/Font/FontDatabase.h>
  13. #include <LibGfx/Palette.h>
  14. namespace Profiler {
  15. constexpr int bar_rounding = 2;
  16. constexpr int bar_margin = 2;
  17. constexpr int bar_padding = 8;
  18. constexpr int bar_height = 20;
  19. constexpr int text_threshold = 30;
  20. Vector<Gfx::Color> s_colors;
  21. static Vector<Gfx::Color> const& get_colors()
  22. {
  23. if (s_colors.is_empty()) {
  24. // Start with a nice orange, then make shades of it
  25. Gfx::Color midpoint(255, 94, 19);
  26. s_colors.extend(midpoint.shades(3, 0.5f));
  27. s_colors.append(midpoint);
  28. s_colors.extend(midpoint.tints(3, 0.5f));
  29. }
  30. return s_colors;
  31. }
  32. FlameGraphView::FlameGraphView(GUI::Model& model, int text_column, int width_column)
  33. : m_model(model)
  34. , m_text_column(text_column)
  35. , m_width_column(width_column)
  36. {
  37. set_fill_with_background_color(true);
  38. set_background_role(Gfx::ColorRole::Base);
  39. m_model.register_client(*this);
  40. m_colors = get_colors();
  41. layout_bars();
  42. }
  43. GUI::ModelIndex FlameGraphView::hovered_index() const
  44. {
  45. if (!m_hovered_bar)
  46. return GUI::ModelIndex();
  47. return m_hovered_bar->index;
  48. }
  49. void FlameGraphView::model_did_update(unsigned)
  50. {
  51. m_selected_indexes.clear();
  52. layout_bars();
  53. update();
  54. }
  55. void FlameGraphView::mousemove_event(GUI::MouseEvent& event)
  56. {
  57. StackBar* hovered_bar = nullptr;
  58. for (size_t i = 0; i < m_bars.size(); ++i) {
  59. auto& bar = m_bars[i];
  60. if (bar.rect.contains(event.x(), event.y())) {
  61. hovered_bar = &bar;
  62. break;
  63. }
  64. }
  65. if (m_hovered_bar == hovered_bar)
  66. return;
  67. m_hovered_bar = hovered_bar;
  68. if (on_hover_change)
  69. on_hover_change();
  70. String label = "";
  71. if (m_hovered_bar != nullptr && m_hovered_bar->index.is_valid()) {
  72. label = bar_label(*m_hovered_bar);
  73. }
  74. set_tooltip(label);
  75. show_or_hide_tooltip();
  76. update();
  77. }
  78. void FlameGraphView::mousedown_event(GUI::MouseEvent& event)
  79. {
  80. if (event.button() != GUI::MouseButton::Primary)
  81. return;
  82. if (!m_hovered_bar)
  83. return;
  84. m_selected_indexes.clear();
  85. GUI::ModelIndex selected_index = m_hovered_bar->index;
  86. while (selected_index.is_valid()) {
  87. m_selected_indexes.append(selected_index);
  88. selected_index = selected_index.parent();
  89. }
  90. layout_bars();
  91. update();
  92. }
  93. void FlameGraphView::resize_event(GUI::ResizeEvent&)
  94. {
  95. layout_bars();
  96. }
  97. void FlameGraphView::paint_event(GUI::PaintEvent& event)
  98. {
  99. GUI::Painter painter(*this);
  100. painter.add_clip_rect(event.rect());
  101. for (auto const& bar : m_bars) {
  102. auto label = bar_label(bar);
  103. auto color = m_colors[label.hash() % m_colors.size()];
  104. if (&bar == m_hovered_bar)
  105. color = color.lightened(1.2f);
  106. if (bar.selected)
  107. color = color.with_alpha(128);
  108. // Do rounded corners if the node will draw with enough width
  109. if (bar.rect.width() > (bar_rounding * 3))
  110. painter.fill_rect_with_rounded_corners(bar.rect.shrunken(0, bar_margin), color, bar_rounding);
  111. else
  112. painter.fill_rect(bar.rect.shrunken(0, bar_margin), color);
  113. if (bar.rect.width() > text_threshold) {
  114. painter.draw_text(
  115. bar.rect.shrunken(bar_padding, 0),
  116. label,
  117. painter.font(),
  118. Gfx::TextAlignment::CenterLeft,
  119. Gfx::Color::Black,
  120. Gfx::TextElision::Right);
  121. }
  122. }
  123. }
  124. String FlameGraphView::bar_label(StackBar const& bar) const
  125. {
  126. auto label_index = bar.index.sibling_at_column(m_text_column);
  127. String label = "All";
  128. if (label_index.is_valid()) {
  129. label = m_model.data(label_index).to_string();
  130. }
  131. return label;
  132. }
  133. void FlameGraphView::layout_bars()
  134. {
  135. m_bars.clear();
  136. // Explicit copy here so the layout can mutate
  137. Vector<GUI::ModelIndex> selected = m_selected_indexes;
  138. GUI::ModelIndex null_index;
  139. layout_children(null_index, 0, 0, this->width(), selected);
  140. }
  141. void FlameGraphView::layout_children(GUI::ModelIndex& index, int depth, int left, int right, Vector<GUI::ModelIndex>& selected_nodes)
  142. {
  143. auto available_width = right - left;
  144. if (available_width < 1)
  145. return;
  146. auto y = this->height() - (bar_height * depth) - bar_height;
  147. if (y < 0)
  148. return;
  149. u32 node_event_count = 0;
  150. if (!index.is_valid()) {
  151. // We're at the root, so calculate the event count across all roots
  152. for (auto i = 0; i < m_model.row_count(index); ++i) {
  153. auto const& root = *static_cast<ProfileNode const*>(m_model.index(i).internal_data());
  154. node_event_count += root.event_count();
  155. }
  156. m_bars.append({ {}, { left, y, available_width, bar_height }, false });
  157. } else {
  158. auto const* node = static_cast<ProfileNode const*>(index.internal_data());
  159. bool selected = !selected_nodes.is_empty();
  160. if (selected) {
  161. VERIFY(selected_nodes.take_last() == index);
  162. }
  163. node_event_count = node->event_count();
  164. Gfx::IntRect node_rect { left, y, available_width, bar_height };
  165. m_bars.append({ index, node_rect, selected });
  166. }
  167. float width_per_sample = static_cast<float>(available_width) / node_event_count;
  168. float new_left = static_cast<float>(left);
  169. for (auto i = 0; i < m_model.row_count(index); ++i) {
  170. auto child_index = m_model.index(i, 0, index);
  171. if (!child_index.is_valid())
  172. continue;
  173. if (!selected_nodes.is_empty()) {
  174. if (selected_nodes.last() != child_index)
  175. continue;
  176. layout_children(child_index, depth + 1, left, right, selected_nodes);
  177. return;
  178. }
  179. auto const* child = static_cast<ProfileNode const*>(child_index.internal_data());
  180. float child_width = width_per_sample * child->event_count();
  181. layout_children(child_index, depth + 1, static_cast<int>(new_left), static_cast<int>(new_left + child_width), selected_nodes);
  182. new_left += child_width;
  183. }
  184. }
  185. }