From 1c3a82d59e4070159b17d17b7e87bcf869c7c797 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Tue, 28 Dec 2021 13:54:21 +0100 Subject: [PATCH] Profiler: Extract percentage gradient calculation into its own file --- Userland/DevTools/Profiler/CMakeLists.txt | 1 + .../DevTools/Profiler/DisassemblyModel.cpp | 19 +----------- Userland/DevTools/Profiler/Gradient.cpp | 29 +++++++++++++++++++ Userland/DevTools/Profiler/Gradient.h | 15 ++++++++++ Userland/DevTools/Profiler/SourceModel.cpp | 20 ++----------- 5 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 Userland/DevTools/Profiler/Gradient.cpp create mode 100644 Userland/DevTools/Profiler/Gradient.h diff --git a/Userland/DevTools/Profiler/CMakeLists.txt b/Userland/DevTools/Profiler/CMakeLists.txt index e10a82b7d2..e3eacb425b 100644 --- a/Userland/DevTools/Profiler/CMakeLists.txt +++ b/Userland/DevTools/Profiler/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES main.cpp IndividualSampleModel.cpp FlameGraphView.cpp + Gradient.cpp Process.cpp Profile.cpp ProfileModel.cpp diff --git a/Userland/DevTools/Profiler/DisassemblyModel.cpp b/Userland/DevTools/Profiler/DisassemblyModel.cpp index bb6ae0c9ab..7677957911 100644 --- a/Userland/DevTools/Profiler/DisassemblyModel.cpp +++ b/Userland/DevTools/Profiler/DisassemblyModel.cpp @@ -5,11 +5,11 @@ */ #include "DisassemblyModel.h" +#include "Gradient.h" #include "Profile.h" #include #include #include -#include #include #include #include @@ -17,23 +17,6 @@ namespace Profiler { -static Gfx::Bitmap const& heat_gradient() -{ - static RefPtr bitmap; - if (!bitmap) { - bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 101, 1 }).release_value_but_fixme_should_propagate_errors(); - GUI::Painter painter(*bitmap); - painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000)); - } - return *bitmap; -} - -static Color color_for_percent(int percent) -{ - VERIFY(percent >= 0 && percent <= 100); - return heat_gradient().get_pixel(percent, 0); -} - static Optional s_kernel_binary; static ELF::Image* try_load_kernel_binary() diff --git a/Userland/DevTools/Profiler/Gradient.cpp b/Userland/DevTools/Profiler/Gradient.cpp new file mode 100644 index 0000000000..be6ed341b6 --- /dev/null +++ b/Userland/DevTools/Profiler/Gradient.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2020, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "Gradient.h" +#include + +namespace Profiler { + +static Gfx::Bitmap const& heat_gradient() +{ + static RefPtr bitmap; + if (!bitmap) { + bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 101, 1 }).release_value_but_fixme_should_propagate_errors(); + GUI::Painter painter(*bitmap); + painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000)); + } + return *bitmap; +} + +Color color_for_percent(u8 percent) +{ + VERIFY(percent <= 100); + return heat_gradient().get_pixel(percent, 0); +} + +} diff --git a/Userland/DevTools/Profiler/Gradient.h b/Userland/DevTools/Profiler/Gradient.h new file mode 100644 index 0000000000..60a198a42d --- /dev/null +++ b/Userland/DevTools/Profiler/Gradient.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2020, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Profiler { + +Color color_for_percent(u8 percent); + +} diff --git a/Userland/DevTools/Profiler/SourceModel.cpp b/Userland/DevTools/Profiler/SourceModel.cpp index 844d1e9064..e82ac00c72 100644 --- a/Userland/DevTools/Profiler/SourceModel.cpp +++ b/Userland/DevTools/Profiler/SourceModel.cpp @@ -5,9 +5,10 @@ */ #include "SourceModel.h" +#include "Gradient.h" #include "Profile.h" #include -#include +#include #include #include @@ -53,23 +54,6 @@ private: Vector m_lines; }; -static Gfx::Bitmap const& heat_gradient() -{ - static RefPtr bitmap; - if (!bitmap) { - bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 101, 1 }).release_value_but_fixme_should_propagate_errors(); - GUI::Painter painter(*bitmap); - painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000)); - } - return *bitmap; -} - -static Color color_for_percent(int percent) -{ - VERIFY(percent >= 0 && percent <= 100); - return heat_gradient().get_pixel(percent, 0); -} - SourceModel::SourceModel(Profile& profile, ProfileNode& node) : m_profile(profile) , m_node(node)