mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-28 05:52:53 +00:00
PixelPaint: Add a general-purpose parallel image processing pipeline
The ImageProcessor singleton is intended to be used by all sorts of image processing which might take some time to complete; or other background actions. We're not using BackgroundTask here because this system is specifically designed to work with task queues and PixelPaint interaction; e.g. it provides common image processing tasks such as filter application.
This commit is contained in:
parent
9483adee46
commit
ade868aa56
Notes:
sideshowbarker
2024-07-17 06:38:11 +09:00
Author: https://github.com/kleinesfilmroellchen
Commit: ade868aa56
Pull-request: https://github.com/SerenityOS/serenity/pull/14844
Reviewed-by: https://github.com/AtkinsSJ ✅
Reviewed-by: https://github.com/MacDue
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/demostanis
3 changed files with 146 additions and 0 deletions
69
Userland/Applications/PixelPaint/ImageProcessor.cpp
Normal file
69
Userland/Applications/PixelPaint/ImageProcessor.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "ImageProcessor.h"
|
||||
|
||||
namespace PixelPaint {
|
||||
|
||||
FilterApplicationCommand::FilterApplicationCommand(NonnullRefPtr<Filter> filter, NonnullRefPtr<Layer> target_layer)
|
||||
: m_filter(move(filter))
|
||||
, m_target_layer(move(target_layer))
|
||||
{
|
||||
}
|
||||
|
||||
void FilterApplicationCommand::execute()
|
||||
{
|
||||
m_filter->apply(m_target_layer->content_bitmap(), m_target_layer->content_bitmap());
|
||||
m_filter->m_editor->gui_event_loop().deferred_invoke([strong_this = NonnullRefPtr(*this)]() {
|
||||
// HACK: we can't tell strong_this to not be const
|
||||
(*const_cast<NonnullRefPtr<Layer>*>(&strong_this->m_target_layer))->did_modify_bitmap(strong_this->m_target_layer->rect());
|
||||
strong_this->m_filter->m_editor->did_complete_action();
|
||||
});
|
||||
}
|
||||
|
||||
static Singleton<ImageProcessor> s_image_processor;
|
||||
|
||||
ImageProcessor::ImageProcessor()
|
||||
: m_command_queue(MUST(Queue::try_create()))
|
||||
, m_processor_thread(Threading::Thread::construct([this]() {
|
||||
while (true) {
|
||||
if (auto next_command = m_command_queue.try_dequeue(); !next_command.is_error()) {
|
||||
next_command.value()->execute();
|
||||
} else {
|
||||
Threading::MutexLocker locker { m_wakeup_mutex };
|
||||
m_wakeup_variable.wait_while([this]() { return m_command_queue.weak_used() == 0; });
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
"Image Processor"sv))
|
||||
, m_wakeup_variable(m_wakeup_mutex)
|
||||
{
|
||||
}
|
||||
|
||||
ImageProcessor* ImageProcessor::the()
|
||||
{
|
||||
return s_image_processor;
|
||||
}
|
||||
|
||||
ErrorOr<void> ImageProcessor::enqueue_command(NonnullRefPtr<ImageProcessingCommand> command)
|
||||
{
|
||||
if (auto queue_status = m_command_queue.try_enqueue(move(command)); queue_status.is_error())
|
||||
return ENOSPC;
|
||||
|
||||
if (!m_processor_thread->is_started()) {
|
||||
m_processor_thread->start();
|
||||
m_processor_thread->detach();
|
||||
}
|
||||
|
||||
m_wakeup_mutex.lock();
|
||||
m_wakeup_variable.signal();
|
||||
m_wakeup_mutex.unlock();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue