From 60b72b803317939041d07751292d13ad9eca2abc Mon Sep 17 00:00:00 2001 From: Torstennator Date: Wed, 16 Aug 2023 14:30:00 +0200 Subject: [PATCH] PixelPaint: Calculate histogram and vectorscope data only when needed With this patch the histogram and vectorscope data for the image is only computed when the widgets are visible to the user and therefore saves some processing time when this information is not required to be computed. --- .../PixelPaint/HistogramWidget.cpp | 9 ++++---- .../Applications/PixelPaint/HistogramWidget.h | 1 + .../Applications/PixelPaint/MainWidget.cpp | 14 +++++------ .../Applications/PixelPaint/ScopeWidget.cpp | 23 +++++++++++++++++++ .../Applications/PixelPaint/ScopeWidget.h | 4 ++++ .../PixelPaint/VectorscopeWidget.cpp | 2 +- .../PixelPaint/VectorscopeWidget.h | 1 + 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Userland/Applications/PixelPaint/HistogramWidget.cpp b/Userland/Applications/PixelPaint/HistogramWidget.cpp index 60159b1465..72f78a28ad 100644 --- a/Userland/Applications/PixelPaint/HistogramWidget.cpp +++ b/Userland/Applications/PixelPaint/HistogramWidget.cpp @@ -18,7 +18,7 @@ namespace PixelPaint { ErrorOr HistogramWidget::rebuild_histogram_data() { - if (!m_image) + if (!should_process_data()) return {}; auto full_bitmap = TRY(m_image->compose_bitmap(Gfx::BitmapFormat::BGRA8888)); @@ -76,12 +76,11 @@ ErrorOr HistogramWidget::rebuild_histogram_data() void HistogramWidget::paint_event(GUI::PaintEvent& event) { - GUI::Painter painter(*this); - painter.add_clip_rect(event.rect()); - - if (!m_image) + if (!should_process_data()) return; + GUI::Painter painter(*this); + painter.add_clip_rect(event.rect()); int bottom_line = height() - 1; float step_width = static_cast(width()) / 256; diff --git a/Userland/Applications/PixelPaint/HistogramWidget.h b/Userland/Applications/PixelPaint/HistogramWidget.h index 6e581d1502..b9aac29981 100644 --- a/Userland/Applications/PixelPaint/HistogramWidget.h +++ b/Userland/Applications/PixelPaint/HistogramWidget.h @@ -22,6 +22,7 @@ public: private: HistogramWidget() = default; + virtual AK::StringView widget_config_name() const override { return "ShowHistogram"sv; } virtual void paint_event(GUI::PaintEvent&) override; ErrorOr rebuild_histogram_data(); diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 5e395ecfe0..6b838aafa9 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -587,18 +587,16 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) m_view_menu->add_separator(); auto histogram_action = GUI::Action::create_checkable("&Histogram", [&](auto& action) { - Config::write_bool("PixelPaint"sv, "Scopes"sv, "ShowHistogram"sv, action.is_checked()); - m_histogram_widget->parent_widget()->set_visible(action.is_checked()); + m_histogram_widget->set_scope_visibility(action.is_checked()); }); - histogram_action->set_checked(Config::read_bool("PixelPaint"sv, "Scopes"sv, "ShowHistogram"sv, false)); - m_histogram_widget->parent_widget()->set_visible(histogram_action->is_checked()); + histogram_action->set_checked(m_histogram_widget->read_visibility_from_configuration()); + m_histogram_widget->set_scope_visibility(histogram_action->is_checked()); auto vectorscope_action = GUI::Action::create_checkable("&Vectorscope", [&](auto& action) { - Config::write_bool("PixelPaint"sv, "Scopes"sv, "ShowVectorscope"sv, action.is_checked()); - m_vectorscope_widget->parent_widget()->set_visible(action.is_checked()); + m_vectorscope_widget->set_scope_visibility(action.is_checked()); }); - vectorscope_action->set_checked(Config::read_bool("PixelPaint"sv, "Scopes"sv, "ShowVectorscope"sv, false)); - m_vectorscope_widget->parent_widget()->set_visible(vectorscope_action->is_checked()); + vectorscope_action->set_checked(m_vectorscope_widget->read_visibility_from_configuration()); + m_vectorscope_widget->set_scope_visibility(vectorscope_action->is_checked()); auto scopes_menu = m_view_menu->add_submenu("&Scopes"_string); scopes_menu->add_action(histogram_action); diff --git a/Userland/Applications/PixelPaint/ScopeWidget.cpp b/Userland/Applications/PixelPaint/ScopeWidget.cpp index afa69a7965..b0e8ea4f4c 100644 --- a/Userland/Applications/PixelPaint/ScopeWidget.cpp +++ b/Userland/Applications/PixelPaint/ScopeWidget.cpp @@ -6,6 +6,7 @@ #include "ScopeWidget.h" #include "Layer.h" +#include namespace PixelPaint { @@ -38,4 +39,26 @@ void ScopeWidget::set_color_at_mouseposition(Color color) update(); } +void ScopeWidget::set_scope_visibility(bool visible) +{ + if (visible != read_visibility_from_configuration()) + Config::write_bool("PixelPaint"sv, "Scopes"sv, widget_config_name(), visible); + + // since we are housed within a other widget we need to set the visibility on our parent widget + if (parent_widget()) + parent_widget()->set_visible(visible); + + if (visible) + image_changed(); +} + +bool ScopeWidget::read_visibility_from_configuration() +{ + return Config::read_bool("PixelPaint"sv, "Scopes"sv, widget_config_name(), false); +} + +bool ScopeWidget::should_process_data() +{ + return m_image && read_visibility_from_configuration(); +} } diff --git a/Userland/Applications/PixelPaint/ScopeWidget.h b/Userland/Applications/PixelPaint/ScopeWidget.h index be258d7253..70604f93b6 100644 --- a/Userland/Applications/PixelPaint/ScopeWidget.h +++ b/Userland/Applications/PixelPaint/ScopeWidget.h @@ -23,9 +23,13 @@ public: void set_image(Image*); virtual void image_changed() = 0; void set_color_at_mouseposition(Color); + void set_scope_visibility(bool); + bool read_visibility_from_configuration(); protected: virtual void paint_event(GUI::PaintEvent&) override = 0; + virtual AK::StringView widget_config_name() const = 0; + bool should_process_data(); Color m_color_at_mouseposition = Color::Transparent; RefPtr m_image; diff --git a/Userland/Applications/PixelPaint/VectorscopeWidget.cpp b/Userland/Applications/PixelPaint/VectorscopeWidget.cpp index aa34ef4ae8..5d9920c159 100644 --- a/Userland/Applications/PixelPaint/VectorscopeWidget.cpp +++ b/Userland/Applications/PixelPaint/VectorscopeWidget.cpp @@ -31,7 +31,7 @@ void VectorscopeWidget::image_changed() ErrorOr VectorscopeWidget::rebuild_vectorscope_data() { - if (!m_image) + if (!should_process_data()) return {}; m_vectorscope_data.fill({}); diff --git a/Userland/Applications/PixelPaint/VectorscopeWidget.h b/Userland/Applications/PixelPaint/VectorscopeWidget.h index 03c9a7e37c..2721487a8a 100644 --- a/Userland/Applications/PixelPaint/VectorscopeWidget.h +++ b/Userland/Applications/PixelPaint/VectorscopeWidget.h @@ -118,6 +118,7 @@ public: virtual void image_changed() override; private: + virtual AK::StringView widget_config_name() const override { return "ShowVectorscope"sv; } virtual void paint_event(GUI::PaintEvent&) override; ErrorOr rebuild_vectorscope_data();