ProfileTimelineWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ProfileTimelineWidget.h"
  27. #include "Profile.h"
  28. #include <LibGUI/GPainter.h>
  29. ProfileTimelineWidget::ProfileTimelineWidget(Profile& profile, GUI::Widget* parent)
  30. : GUI::Frame(parent)
  31. , m_profile(profile)
  32. {
  33. set_frame_thickness(2);
  34. set_frame_shadow(Gfx::FrameShadow::Sunken);
  35. set_frame_shape(Gfx::FrameShape::Container);
  36. set_background_color(Color::White);
  37. set_fill_with_background_color(true);
  38. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  39. set_preferred_size(0, 80);
  40. }
  41. ProfileTimelineWidget::~ProfileTimelineWidget()
  42. {
  43. }
  44. void ProfileTimelineWidget::paint_event(GUI::PaintEvent& event)
  45. {
  46. GUI::Frame::paint_event(event);
  47. GUI::Painter painter(*this);
  48. painter.add_clip_rect(event.rect());
  49. float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
  50. float frame_height = (float)frame_inner_rect().height() / (float)m_profile.deepest_stack_depth();
  51. for (auto& sample : m_profile.samples()) {
  52. u64 t = sample.timestamp - m_profile.first_timestamp();
  53. int x = (int)((float)t * column_width);
  54. int cw = max(1, (int)column_width);
  55. int column_height = frame_inner_rect().height() - (int)((float)sample.frames.size() * frame_height);
  56. bool in_kernel = sample.in_kernel;
  57. Color color = in_kernel ? Color::from_rgb(0xc25e5a) : Color::from_rgb(0x5a65c2);
  58. for (int i = 0; i < cw; ++i)
  59. painter.draw_line({ x + i, frame_thickness() + column_height }, { x + i, height() - frame_thickness() * 2 }, color);
  60. }
  61. u64 normalized_start_time = min(m_select_start_time, m_select_end_time);
  62. u64 normalized_end_time = max(m_select_start_time, m_select_end_time);
  63. int select_start_x = (int)((float)(normalized_start_time - m_profile.first_timestamp()) * column_width);
  64. int select_end_x = (int)((float)(normalized_end_time - m_profile.first_timestamp()) * column_width);
  65. painter.fill_rect({ select_start_x, frame_thickness(), select_end_x - select_start_x, height() - frame_thickness() * 2 }, Color(0, 0, 0, 60));
  66. }
  67. u64 ProfileTimelineWidget::timestamp_at_x(int x) const
  68. {
  69. float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
  70. float ms_into_profile = (float)x / column_width;
  71. return m_profile.first_timestamp() + (u64)ms_into_profile;
  72. }
  73. void ProfileTimelineWidget::mousedown_event(GUI::MouseEvent& event)
  74. {
  75. if (event.button() != GUI::MouseButton::Left)
  76. return;
  77. m_selecting = true;
  78. m_select_start_time = timestamp_at_x(event.x());
  79. m_select_end_time = m_select_start_time;
  80. m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
  81. update();
  82. }
  83. void ProfileTimelineWidget::mousemove_event(GUI::MouseEvent& event)
  84. {
  85. if (!m_selecting)
  86. return;
  87. m_select_end_time = timestamp_at_x(event.x());
  88. m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
  89. update();
  90. }
  91. void ProfileTimelineWidget::mouseup_event(GUI::MouseEvent& event)
  92. {
  93. if (event.button() != GUI::MouseButton::Left)
  94. return;
  95. m_selecting = false;
  96. if (m_select_start_time == m_select_end_time)
  97. m_profile.clear_timestamp_filter_range();
  98. }