diff --git a/DevTools/ProfileViewer/DisassemblyModel.cpp b/DevTools/ProfileViewer/DisassemblyModel.cpp index 5f4a2c68ee..ffa6c8ecfd 100644 --- a/DevTools/ProfileViewer/DisassemblyModel.cpp +++ b/DevTools/ProfileViewer/DisassemblyModel.cpp @@ -28,10 +28,28 @@ #include "Profile.h" #include #include +#include #include #include #include +static const Gfx::Bitmap& heat_gradient() +{ + static RefPtr bitmap; + if (!bitmap) { + bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 100, 1 }); + 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) +{ + ASSERT(percent >= 0 && percent <= 100); + return heat_gradient().get_pixel(percent, 0); +} + DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node) : m_profile(profile) , m_node(node) @@ -57,8 +75,9 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node) StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length()); size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0); + float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f; - m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction }); + m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent }); offset_into_symbol += insn.value().length(); } @@ -97,20 +116,47 @@ GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const return {}; } +struct ColorPair { + Color background; + Color foreground; +}; + +static Optional color_pair_for(const InstructionData& insn) +{ + if (insn.percent == 0) + return {}; + + Color background = color_for_percent(insn.percent); + Color foreground; + if (insn.percent > 50) + foreground = Color::White; + else + foreground = Color::Black; + return ColorPair { background, foreground }; +} + GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const { auto& insn = m_instructions[index.row()]; if (role == Role::BackgroundColor) { - if (insn.event_count > 0) - return Color(Color::Yellow); - return {}; + auto colors = color_pair_for(insn); + if (!colors.has_value()) + return {}; + return colors.value().background; + } + + if (role == Role::ForegroundColor) { + auto colors = color_pair_for(insn); + if (!colors.has_value()) + return {}; + return colors.value().foreground; } if (role == Role::Display) { if (index.column() == Column::SampleCount) { if (m_profile.show_percentages()) - return ((float)insn.event_count / (float)m_profile.filtered_event_count()) * 100.0f; + return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f; return insn.event_count; } if (index.column() == Column::Address) diff --git a/DevTools/ProfileViewer/DisassemblyModel.h b/DevTools/ProfileViewer/DisassemblyModel.h index 2414e27fed..de45415f8d 100644 --- a/DevTools/ProfileViewer/DisassemblyModel.h +++ b/DevTools/ProfileViewer/DisassemblyModel.h @@ -38,6 +38,7 @@ struct InstructionData { StringView bytes; FlatPtr address { 0 }; u32 event_count { 0 }; + float percent { 0 }; }; class DisassemblyModel final : public GUI::Model {