2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-12-14 18:44:29 +01:00
|
|
|
#include "ProfileTimelineWidget.h"
|
|
|
|
#include "Profile.h"
|
|
|
|
#include <LibGUI/GPainter.h>
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
ProfileTimelineWidget::ProfileTimelineWidget(Profile& profile, GUI::Widget* parent)
|
|
|
|
: GUI::Frame(parent)
|
2019-12-14 18:44:29 +01:00
|
|
|
, m_profile(profile)
|
|
|
|
{
|
|
|
|
set_frame_thickness(2);
|
2020-02-06 11:56:38 +01:00
|
|
|
set_frame_shadow(Gfx::FrameShadow::Sunken);
|
|
|
|
set_frame_shape(Gfx::FrameShape::Container);
|
2019-12-14 18:44:29 +01:00
|
|
|
set_background_color(Color::White);
|
|
|
|
set_fill_with_background_color(true);
|
2020-02-02 15:07:41 +01:00
|
|
|
set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
2019-12-14 18:44:29 +01:00
|
|
|
set_preferred_size(0, 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProfileTimelineWidget::~ProfileTimelineWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void ProfileTimelineWidget::paint_event(GUI::PaintEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Frame::paint_event(event);
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Painter painter(*this);
|
2019-12-14 18:44:29 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
|
|
|
float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
|
2019-12-15 18:08:20 +01:00
|
|
|
float frame_height = (float)frame_inner_rect().height() / (float)m_profile.deepest_stack_depth();
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2019-12-15 17:26:54 +01:00
|
|
|
for (auto& sample : m_profile.samples()) {
|
|
|
|
u64 t = sample.timestamp - m_profile.first_timestamp();
|
2019-12-14 18:44:29 +01:00
|
|
|
int x = (int)((float)t * column_width);
|
|
|
|
int cw = max(1, (int)column_width);
|
|
|
|
|
2019-12-15 18:08:20 +01:00
|
|
|
int column_height = frame_inner_rect().height() - (int)((float)sample.frames.size() * frame_height);
|
|
|
|
|
2019-12-14 19:25:33 +01:00
|
|
|
bool in_kernel = sample.in_kernel;
|
2019-12-14 18:44:29 +01:00
|
|
|
Color color = in_kernel ? Color::from_rgb(0xc25e5a) : Color::from_rgb(0x5a65c2);
|
|
|
|
for (int i = 0; i < cw; ++i)
|
2019-12-15 18:08:20 +01:00
|
|
|
painter.draw_line({ x + i, frame_thickness() + column_height }, { x + i, height() - frame_thickness() * 2 }, color);
|
2019-12-14 19:25:33 +01:00
|
|
|
}
|
2019-12-14 19:10:12 +01:00
|
|
|
|
|
|
|
u64 normalized_start_time = min(m_select_start_time, m_select_end_time);
|
|
|
|
u64 normalized_end_time = max(m_select_start_time, m_select_end_time);
|
|
|
|
|
|
|
|
int select_start_x = (int)((float)(normalized_start_time - m_profile.first_timestamp()) * column_width);
|
|
|
|
int select_end_x = (int)((float)(normalized_end_time - m_profile.first_timestamp()) * column_width);
|
|
|
|
painter.fill_rect({ select_start_x, frame_thickness(), select_end_x - select_start_x, height() - frame_thickness() * 2 }, Color(0, 0, 0, 60));
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2019-12-14 19:10:12 +01:00
|
|
|
u64 ProfileTimelineWidget::timestamp_at_x(int x) const
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2019-12-14 19:10:12 +01:00
|
|
|
float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
|
|
|
|
float ms_into_profile = (float)x / column_width;
|
|
|
|
return m_profile.first_timestamp() + (u64)ms_into_profile;
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void ProfileTimelineWidget::mousedown_event(GUI::MouseEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() != GUI::MouseButton::Left)
|
2019-12-14 19:10:12 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_selecting = true;
|
|
|
|
m_select_start_time = timestamp_at_x(event.x());
|
|
|
|
m_select_end_time = m_select_start_time;
|
|
|
|
m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
|
|
|
|
update();
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void ProfileTimelineWidget::mousemove_event(GUI::MouseEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2019-12-14 19:10:12 +01:00
|
|
|
if (!m_selecting)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_select_end_time = timestamp_at_x(event.x());
|
|
|
|
m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void ProfileTimelineWidget::mouseup_event(GUI::MouseEvent& event)
|
2019-12-14 19:10:12 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() != GUI::MouseButton::Left)
|
2019-12-14 19:10:12 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_selecting = false;
|
|
|
|
if (m_select_start_time == m_select_end_time)
|
|
|
|
m_profile.clear_timestamp_filter_range();
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|