FlameGraphView.cpp 6.0 KB

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