iVS3D v2.0.9
Loading...
Searching...
No Matches
maskcommand.h
1#ifndef MASKCOMMAND_H
2#define MASKCOMMAND_H
3
4#include <QDir>
5#include <QMutex>
6#include <QWaitCondition>
7#include <QString>
8#include <atomic>
9#include <condition_variable>
10#include <map>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <vector>
15
16#include <opencv2/core.hpp>
17#include <opencv2/imgcodecs.hpp>
18#include <opencv2/imgproc.hpp>
19
20#include <tl/expected.hpp>
21
22#include "imageprocessor.h"
23#include "maskstack.h"
24#include "pluginthread.h"
25
39class MaskCommand : public ImageCommand {
40
41public:
50 MaskCommand(const MaskRecord* record, Resolution exportResolution, ROI roi,
51 QString folder, PluginThread* pluginThread,
52 volatile bool* stopped = nullptr);
53
59 std::optional<QString> execute(ImageContext& ctx) override;
60
65 void onMaskFinished(const MaskGenerationResult& result);
66
67private:
68 const MaskRecord* m_record;
69 Resolution m_exportResolution;
70 ROI m_roi;
71 QString m_folder;
72 PluginThread* m_pluginThread;
73 volatile bool* m_stopped = nullptr;
74 QString m_pluginName;
75 cv::Size m_exportSize;
76 bool m_initialized = false;
77
78 // Async mask handling
79 // s_pendingMasks: Tracks the number of mask generation requests currently in flight.
80 // This prevents the export pipeline from overwhelming the plugin thread
81 // with requests faster than they can be processed.
82 // MAX_PENDING_MASKS: Maximum allowed concurrent mask requests (3).
83 // When this limit is reached, execute() blocks until masks complete.
84 // This value balances latency (low = more blocking) vs. throughput.
85 // Typical GPU operations take 100-500ms, so 3 concurrent requests
86 // keeps a reasonable buffer without excessive memory usage.
87 static std::atomic<int> s_pendingMasks;
88 static constexpr int MAX_PENDING_MASKS = 3; // Limit concurrent mask requests
89
90 // Thread-safe mask result storage (using std::mutex for condition_variable compatibility)
91 std::mutex m_resultMutex;
92 std::map<uint, cv::Mat> m_maskResults; // imageIndex -> mask
93 std::map<uint, QString> m_maskErrors; // imageIndex -> error message (if any)
94 std::condition_variable m_maskReady; // Signal when a mask arrives
95
109 tl::expected< cv::Mat, QString > waitForMask(uint imageIndex,
110 int timeoutMs = 30000);
111};
112
113#endif // MASKCOMMAND_H
The ImageCommand class is an abstract interface for commands regarding image operations such as resiz...
Definition imageprocessor.h:33
The ImageContext class provides contetx information for executing ImageCommands. This data can be mod...
Definition imageprocessor.h:22
Generates masks asynchronously via PluginThread and writes them to disk.
Definition maskcommand.h:39
std::optional< QString > execute(ImageContext &ctx) override
Requests mask generation and waits for result before writing to disk.
Definition maskcommand.cpp:39
void onMaskFinished(const MaskGenerationResult &result)
Receives completed mask from PluginThread (to be called by external connection).
Definition maskcommand.cpp:107
tl::expected< cv::Mat, QString > waitForMask(uint imageIndex, int timeoutMs=30000)
Waits for a specific mask to arrive from PluginThread.
Definition maskcommand.cpp:121
Definition pluginthread.h:123
The ROI class manages a region of interest represented as a rectangle in the [0,1]x[0,...
Definition roi.h:21
The Resolution class encapsulates an image resolution (width and height). It provides functionality f...
Definition resolution.h:17
Definition pluginthread.h:56
Contains all information needed to generate a mask at export time.
Definition maskstack.h:25