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.
This commit is contained in:
Torstennator 2023-08-16 14:30:00 +02:00 committed by Sam Atkins
parent 1c0aa51684
commit 60b72b8033
7 changed files with 40 additions and 14 deletions

View file

@ -18,7 +18,7 @@ namespace PixelPaint {
ErrorOr<void> 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<void> 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<float>(width()) / 256;

View file

@ -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<void> rebuild_histogram_data();

View file

@ -587,18 +587,16 @@ ErrorOr<void> 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);

View file

@ -6,6 +6,7 @@
#include "ScopeWidget.h"
#include "Layer.h"
#include <LibConfig/Client.h>
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();
}
}

View file

@ -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<Image> m_image;

View file

@ -31,7 +31,7 @@ void VectorscopeWidget::image_changed()
ErrorOr<void> VectorscopeWidget::rebuild_vectorscope_data()
{
if (!m_image)
if (!should_process_data())
return {};
m_vectorscope_data.fill({});

View file

@ -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<void> rebuild_vectorscope_data();