Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Make recorder writer not interrupt tool usage
Carsten Hartenfels99a1ec8727c6
diff --git a/plugins/dockers/recorder/recorder_writer.cpp b/plugins/dockers/recorder/recorder_writer.cppindex e5f7ff177b..7e1c02e67c 100644--- a/plugins/dockers/recorder/recorder_writer.cpp+++ b/plugins/dockers/recorder/recorder_writer.cpp@@ -127,10 +127,11 @@ bool ThreadCounter::setUsedImpl(int value)class RecorderWriter::Private{public:- Private(QPointer<KisCanvas2> c, const RecorderWriterSettings& s, const QDir& d)+ Private(QPointer<KisCanvas2> c, const RecorderWriterSettings &s, const QDir &d, RecorderWriterManager *m): canvas(c), settings(&s), outputDir(&d)+ , manager(m){}Private() = delete;Private(const Private&) = default;@@ -147,21 +148,33 @@ public:int partIndex = 0; // Consecutive file numberconst RecorderWriterSettings* settings;const QDir* outputDir;+ RecorderWriterManager *manager;const KoColorSpace *targetCs =KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(),Integer8BitsColorDepthID.id(),KoColorSpaceRegistry::instance()->p709SRGBProfile());- void captureImage()+ int captureImage(){KisImageSP image = canvas->image();+ // Make sure we can actually capture something right now+ {+ QMutexLocker lock(manager->captureMutex());+ if (manager->canStartCapture()) {+ // we don't want image->barrierLock() because it will wait until+ // the full stroke is finished+ image->immediateLockForReadOnly();+ // Grab the next index while the capture mutex is held+ partIndex = manager->incrementAndGetIndex();+ } else {+ return STATUS_BLOCKED;+ }+ }+// Create detached paint device that can be converted to target colorspaceKisPaintDeviceSP device = new KisPaintDevice(image->colorSpace());-- // we don't want image->barrierLock() because it will wait until the full stroke is finished- image->immediateLockForReadOnly();device->makeCloneFromRough(image->projection(), image->bounds());image->unlock();@@ -208,6 +221,7 @@ public:imageBufferWidth = width;imageBufferHeight = height;+ return STATUS_OK;}// Calculate ARGB average value using carry save adder:@@ -274,10 +288,10 @@ public:}}- bool writeFrame()+ int writeFrame(){if (!outputDir->exists() && !outputDir->mkpath(settings->outputDirectory))- return false;+ return STATUS_ERROR;const QString fileName = QString("%1").arg(partIndex, 7, 10, QLatin1Char('0'));const QString &filePath = QString("%1%2.%3").arg(settings->outputDirectory, fileName,@@ -293,10 +307,12 @@ public:break;}- bool result = frame.save(filePath, RecorderFormatInfo::fileFormat(settings->format).data(), factor);- if (!result)+ if (!frame.save(filePath, RecorderFormatInfo::fileFormat(settings->format).data(), factor)) {QFile(filePath).remove(); // remove corrupted frame- return result;+ return STATUS_ERROR;+ }++ return STATUS_OK;}};@@ -305,8 +321,9 @@ RecorderWriter::RecorderWriter(unsigned int i,QPointer<KisCanvas2> c,const RecorderWriterSettings& s,- const QDir& d)- : d(new Private(c, s, d))+ const QDir& d,+ RecorderWriterManager *m)+ : d(new Private(c, s, d, m)), id(i){}@@ -315,12 +332,16 @@ RecorderWriter::~RecorderWriter()delete d;}-void RecorderWriter::onCaptureImage(int writerId, int index)+void RecorderWriter::onCaptureImage(int writerId){if (static_cast<int>(id) != writerId)return;- d->captureImage();+ int captureStatus = d->captureImage();+ if (captureStatus != STATUS_OK) {+ Q_EMIT capturingDone(id, captureStatus);+ return;+ }// downscale image bufferfor (int res = 0; res < d->settings->resolution; ++res)@@ -328,11 +349,9 @@ void RecorderWriter::onCaptureImage(int writerId, int index)d->removeFrameTransparency();- d->partIndex = index;-- bool isFrameWritten = d->writeFrame();+ int writeStatus = d->writeFrame();- Q_EMIT capturingDone(id, isFrameWritten);+ Q_EMIT capturingDone(id, writeStatus);}@@ -342,14 +361,14 @@ struct WriterPoolElusing RecorderWriterPtr = QSharedPointer<RecorderWriter>;WriterPoolEl(- QObject* threadParent,+ RecorderWriterManager* m,unsigned int i,QPointer<KisCanvas2> c,const RecorderWriterSettings& s,const QDir& d)- : thread(QThreadPtr::create(threadParent))- , writer(RecorderWriterPtr::create(i, c, s, d))+ : thread(QThreadPtr::create(m))+ , writer(RecorderWriterPtr::create(i, c, s, d, m)){}bool inUse{false};@@ -382,6 +401,7 @@ public:WriterPool writerPool;RecorderWriterSettings settings{};QDir outputDir;+ QMutex captureMutex;int findLastIndex(const QString &directory){@@ -418,8 +438,8 @@ public:{el.thread->quit();el.thread->wait(RecorderConst::waitThreadTimeoutMs);- disconnect(q, SIGNAL(startCapturing(int, int)), el.writer.get(), SLOT(onCaptureImage(int, int)));- disconnect(el.writer.get(), SIGNAL(capturingDone(int, bool)), q, SLOT(onCapturingDone(int, bool)));+ disconnect(q, SIGNAL(startCapturing(int)), el.writer.get(), SLOT(onCaptureImage(int)));+ disconnect(el.writer.get(), SIGNAL(capturingDone(int, int)), q, SLOT(onCapturingDone(int, int)));if (el.thread->isRunning()){if (!alreadyWarn) {@@ -462,8 +482,8 @@ public:auto writerPtr = writerPool[newWorkerId].writer;auto threadPtr = writerPool[newWorkerId].thread;threadPtr->setObjectName(QString("Krita-Recorder-WriterPool#%1").arg(newWorkerId));- connect(q, SIGNAL(startCapturing(int, int)), writerPtr.get(), SLOT(onCaptureImage(int, int)));- connect(writerPtr.get(), SIGNAL(capturingDone(int, bool)), q, SLOT(onCapturingDone(int, bool)));+ connect(q, SIGNAL(startCapturing(int)), writerPtr.get(), SLOT(onCaptureImage(int)));+ connect(writerPtr.get(), SIGNAL(capturingDone(int, int)), q, SLOT(onCapturingDone(int, int)));writerPtr->moveToThread(threadPtr.get());threadPtr->start(QThread::IdlePriority);}@@ -480,15 +500,6 @@ public:}freeWriterId = -1;}-- bool canStartCapture() // skip capture when use some tools.- {- if (isForceBlackTool)- return false;- if (isActivateBlackTool && toolActivated)- return false;- return true;- }};RecorderWriterManager::RecorderWriterManager(const RecorderExportSettings &es)@@ -600,6 +611,25 @@ void RecorderWriterManager::setEnabled(bool enabled = false)d->enabled = enabled;}+bool RecorderWriterManager::canStartCapture() const+{+ if (d->isForceBlackTool)+ return false;+ if (d->isActivateBlackTool && d->toolActivated)+ return false;+ return true;+}++int RecorderWriterManager::incrementAndGetIndex()+{+ return ++d->partIndex;+}++QMutex *RecorderWriterManager::captureMutex()+{+ return &d->captureMutex;+}+void RecorderWriterManager::onTimer(){if (!d->enabled || !d->canvas)@@ -620,7 +650,7 @@ void RecorderWriterManager::onTimer()d->imageModified = false;- if (!d->canStartCapture())+ if (!canStartCapture())return;d->searchForFreeWriter();@@ -634,17 +664,17 @@ void RecorderWriterManager::onTimer()d->writerPool[d->freeWriterId].inUse = true;d->writerPool[d->freeWriterId].thread->setPriority(QThread::HighPriority);recorderThreads.incUsedAndNotify();- Q_EMIT startCapturing(d->freeWriterId, ++d->partIndex);+ Q_EMIT startCapturing(d->freeWriterId);}-void RecorderWriterManager::onCapturingDone(int workerId, bool success)+void RecorderWriterManager::onCapturingDone(int workerId, int status){if (workerId >= d->writerPool.size())return;d->writerPool[workerId].inUse = false;d->writerPool[workerId].thread->setPriority(QThread::IdlePriority);recorderThreads.decUsedAndNotify();- if (!success) {+ if (status == RecorderWriter::STATUS_ERROR) {stop();Q_EMIT frameWriteFailed();}@@ -652,7 +682,7 @@ void RecorderWriterManager::onCapturingDone(int workerId, bool success)void RecorderWriterManager::onImageModified(){- if (!d->enabled || !d->canStartCapture() )+ if (!d->enabled || !canStartCapture() )return;if ((!d->settings.recordIsolateLayerMode) &&@@ -664,11 +694,13 @@ void RecorderWriterManager::onImageModified()void RecorderWriterManager::onToolChanged(const QString &toolId){+ QMutexLocker lock(&d->captureMutex);d->isForceBlackTool = forceBlacklistedTools.contains(toolId);d->isActivateBlackTool = activateBlacklistedTools.contains(toolId);}void RecorderWriterManager::onToolPrimaryActionActivated(bool activated){+ QMutexLocker lock(&d->captureMutex);d->toolActivated = activated;}diff --git a/plugins/dockers/recorder/recorder_writer.h b/plugins/dockers/recorder/recorder_writer.hindex 7c76fe9677..e8a13835ca 100644--- a/plugins/dockers/recorder/recorder_writer.h+++ b/plugins/dockers/recorder/recorder_writer.h@@ -65,15 +65,22 @@ private:QMutex inUseMutex;};+class RecorderWriterManager;+class RecorderWriter : public QObject{Q_OBJECTpublic:+ static constexpr int STATUS_OK = 0;+ static constexpr int STATUS_ERROR = 1;+ static constexpr int STATUS_BLOCKED = 2;+RecorderWriter(unsigned int i,QPointer<KisCanvas2> c,const RecorderWriterSettings& s,- const QDir& d);+ const QDir& d,+ RecorderWriterManager *m);~RecorderWriter();RecorderWriter() = delete;@@ -83,10 +90,10 @@ public:RecorderWriter& operator=(RecorderWriter&&) = delete;Q_SIGNALS:- void capturingDone(int writerId, bool success);+ void capturingDone(int writerId, int status);public Q_SLOTS:- void onCaptureImage(int writerId, int index);+ void onCaptureImage(int writerId);private:class Private;@@ -117,6 +124,20 @@ public:void setEnabled(bool enabled);+ // This does not do its own synchronization! At the time of writing, the+ // behavior is:+ // * Only the main thread writes to this. It takes the capture mutex.+ // * Reading from the main thread does not take locks.+ // * Reading from writer threads takes the capture mutex.+ bool canStartCapture() const;++ // This does not do its own synchronization, make sure access here is+ // serialized! At the time of writing, this is accomplished by only calling+ // this while the capture mutex is held.+ int incrementAndGetIndex();++ QMutex *captureMutex();+Q_SIGNALS:void started();void stopped();@@ -124,11 +145,11 @@ Q_SIGNALS:void lowPerformanceWarning();void recorderStopWarning();- void startCapturing(int writerId, int index);+ void startCapturing(int writerId);private Q_SLOTS:void onTimer();- void onCapturingDone(int workerId, bool success);+ void onCapturingDone(int workerId, int status);void onImageModified();void onToolChanged(const QString &toolId);void onToolPrimaryActionActivated(bool activated);