FlameGraphView.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "Profile.h"
  8. #include <AK/Function.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Widget.h>
  11. #include <LibGfx/Font/FontDatabase.h>
  12. #include <LibGfx/Forward.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. set_scrollbars_enabled(true);
  40. set_frame_thickness(0);
  41. set_should_hide_unnecessary_scrollbars(false);
  42. horizontal_scrollbar().set_visible(false);
  43. m_model.register_client(*this);
  44. m_colors = get_colors();
  45. layout_bars();
  46. scroll_to_bottom();
  47. }
  48. GUI::ModelIndex FlameGraphView::hovered_index() const
  49. {
  50. if (!m_hovered_bar)
  51. return GUI::ModelIndex();
  52. return m_hovered_bar->index;
  53. }
  54. void FlameGraphView::model_did_update(unsigned)
  55. {
  56. m_selected_indexes.clear();
  57. layout_bars();
  58. update();
  59. }
  60. void FlameGraphView::mousemove_event(GUI::MouseEvent& event)
  61. {
  62. StackBar* hovered_bar = nullptr;
  63. for (size_t i = 0; i < m_bars.size(); ++i) {
  64. auto& bar = m_bars[i];
  65. if (to_widget_rect(bar.rect).contains(event.x(), event.y())) {
  66. hovered_bar = &bar;
  67. break;
  68. }
  69. }
  70. if (m_hovered_bar == hovered_bar)
  71. return;
  72. m_hovered_bar = hovered_bar;
  73. if (on_hover_change)
  74. on_hover_change();
  75. DeprecatedString label = "";
  76. if (m_hovered_bar != nullptr && m_hovered_bar->index.is_valid()) {
  77. label = bar_label(*m_hovered_bar);
  78. }
  79. set_tooltip(label);
  80. show_or_hide_tooltip();
  81. update();
  82. }
  83. void FlameGraphView::mousedown_event(GUI::MouseEvent& event)
  84. {
  85. if (event.button() != GUI::MouseButton::Primary)
  86. return;
  87. if (!m_hovered_bar)
  88. return;
  89. m_selected_indexes.clear();
  90. GUI::ModelIndex selected_index = m_hovered_bar->index;
  91. while (selected_index.is_valid()) {
  92. m_selected_indexes.append(selected_index);
  93. selected_index = selected_index.parent();
  94. }
  95. layout_bars();
  96. update();
  97. }
  98. void FlameGraphView::resize_event(GUI::ResizeEvent& event)
  99. {
  100. auto old_scroll = vertical_scrollbar().value();
  101. AbstractScrollableWidget::resize_event(event);
  102. // Adjust scroll to keep the bottom of the graph fixed
  103. auto available_height_delta = m_old_available_size.height() - available_size().height();
  104. vertical_scrollbar().set_value(old_scroll + available_height_delta);
  105. layout_bars();
  106. m_old_available_size = available_size();
  107. }
  108. void FlameGraphView::paint_event(GUI::PaintEvent& event)
  109. {
  110. GUI::Painter painter(*this);
  111. painter.add_clip_rect(event.rect());
  112. auto content_clip_rect = to_content_rect(event.rect());
  113. for (auto const& bar : m_bars) {
  114. if (!content_clip_rect.intersects_vertically(bar.rect))
  115. continue;
  116. auto label = bar_label(bar);
  117. auto color = m_colors[label.hash() % m_colors.size()];
  118. if (&bar == m_hovered_bar)
  119. color = color.lightened(1.2f);
  120. if (bar.selected)
  121. color = color.with_alpha(128);
  122. auto rect = to_widget_rect(bar.rect);
  123. // Do rounded corners if the node will draw with enough width
  124. if (rect.width() > (bar_rounding * 3))
  125. painter.fill_rect_with_rounded_corners(rect.shrunken(0, bar_margin), color, bar_rounding);
  126. else
  127. painter.fill_rect(rect.shrunken(0, bar_margin), color);
  128. if (rect.width() > text_threshold) {
  129. painter.draw_text(
  130. rect.shrunken(bar_padding, 0),
  131. label,
  132. painter.font(),
  133. Gfx::TextAlignment::CenterLeft,
  134. Gfx::Color::Black,
  135. Gfx::TextElision::Right);
  136. }
  137. }
  138. }
  139. DeprecatedString FlameGraphView::bar_label(StackBar const& bar) const
  140. {
  141. auto label_index = bar.index.sibling_at_column(m_text_column);
  142. DeprecatedString label = "All";
  143. if (label_index.is_valid()) {
  144. label = m_model.data(label_index).to_deprecated_string();
  145. }
  146. return label;
  147. }
  148. void FlameGraphView::layout_bars()
  149. {
  150. m_bars.clear();
  151. m_hovered_bar = nullptr;
  152. // Explicit copy here so the layout can mutate
  153. Vector<GUI::ModelIndex> selected = m_selected_indexes;
  154. GUI::ModelIndex null_index;
  155. layout_children(null_index, 0, 0, available_size().width(), selected);
  156. // Translate bars from (-height..0) to (0..height) now that we know the height,
  157. // use available height as minimum to keep the graph at the bottom when it's small
  158. int height = available_size().height();
  159. for (auto& bar : m_bars)
  160. height = max(height, -bar.rect.top());
  161. for (auto& bar : m_bars)
  162. bar.rect.translate_by(0, height);
  163. // Update scrollbars if height changed
  164. if (height != content_size().height()) {
  165. auto old_content_height = content_size().height();
  166. auto old_scroll = vertical_scrollbar().value();
  167. set_content_size(Gfx::IntSize(available_size().width(), height));
  168. // Adjust scroll to keep the bottom of the graph fixed, so it doesn't jump
  169. // around when double-clicking
  170. auto content_height_delta = old_content_height - content_size().height();
  171. vertical_scrollbar().set_value(old_scroll - content_height_delta);
  172. }
  173. }
  174. void FlameGraphView::layout_children(GUI::ModelIndex& index, int depth, int left, int right, Vector<GUI::ModelIndex>& selected_nodes)
  175. {
  176. auto available_width = right - left;
  177. if (available_width < 1)
  178. return;
  179. auto y = -(bar_height * depth) - bar_height;
  180. u32 node_event_count = 0;
  181. if (!index.is_valid()) {
  182. // We're at the root, so calculate the event count across all roots
  183. for (auto i = 0; i < m_model.row_count(index); ++i) {
  184. auto const& root = *static_cast<ProfileNode const*>(m_model.index(i).internal_data());
  185. node_event_count += root.event_count();
  186. }
  187. m_bars.append({ {}, { left, y, available_width, bar_height }, false });
  188. } else {
  189. auto const* node = static_cast<ProfileNode const*>(index.internal_data());
  190. bool selected = !selected_nodes.is_empty();
  191. if (selected) {
  192. VERIFY(selected_nodes.take_last() == index);
  193. }
  194. node_event_count = node->event_count();
  195. Gfx::IntRect node_rect { left, y, available_width, bar_height };
  196. m_bars.append({ index, node_rect, selected });
  197. }
  198. float width_per_sample = static_cast<float>(available_width) / node_event_count;
  199. float new_left = static_cast<float>(left);
  200. for (auto i = 0; i < m_model.row_count(index); ++i) {
  201. auto child_index = m_model.index(i, 0, index);
  202. if (!child_index.is_valid())
  203. continue;
  204. if (!selected_nodes.is_empty()) {
  205. if (selected_nodes.last() != child_index)
  206. continue;
  207. layout_children(child_index, depth + 1, left, right, selected_nodes);
  208. return;
  209. }
  210. auto const* child = static_cast<ProfileNode const*>(child_index.internal_data());
  211. float child_width = width_per_sample * child->event_count();
  212. layout_children(child_index, depth + 1, static_cast<int>(new_left), static_cast<int>(new_left + child_width), selected_nodes);
  213. new_left += child_width;
  214. }
  215. }
  216. }