21#include <QDirIterator>
22#include <QElapsedTimer>
24#include <QRegularExpression>
25#include <QApplication>
26#include <QMutexLocker>
30#include <QSharedPointer>
33#include <qobjectdefs.h>
34#include <qscopedvaluerollback.h>
44 "KritaShape/KisToolLine",
45 "KritaShape/KisToolRectangle",
46 "KritaShape/KisToolEllipse",
49 static QString filePathForIndex(
int index, QString directory,
RecorderFormat format )
51 QString fileName = QString(
"%1").arg(index, 7, 10, QLatin1Char(
'0'));
53 return QString(
"%1%2.%3").arg(directory,
62 threads =
static_cast<unsigned int>(
70 auto oldValue =
get();
123 auto oldValue =
inUse;
124 inUse =
static_cast<unsigned int>(
127 return oldValue !=
inUse;
161 const bool needSrgbConversion = [&]() {
177 if (
targetCs && needSrgbConversion) {
183 const quint32 width = area.width() & bitmask;
184 const quint32 height = area.height() & bitmask;
185 const int bufferSize = device->
pixelSize() * width * height;
193 const int outWidth = width / divider;
194 const int outHeight = height / divider;
195 uchar *outData =
reinterpret_cast<uchar *
>(
imageBuffer.data());
197 frame = QImage(outData, outWidth, outHeight, QImage::Format_ARGB32);
200 device->
readBytes(
reinterpret_cast<quint8 *
>(
imageBuffer.data()), area.x(), area.y(), width, height);
208 inline quint32
avg(quint32 c1, quint32 c2)
210 return (((c1 ^ c2) & 0xfefefefeUL) >> 1) + (c1 & c2);
215 quint32 *buffer =
reinterpret_cast<quint32 *
>(
imageBuffer.data());
216 quint32 *out = buffer;
224 avg(in1[x], in1[x + 1]),
225 avg(in2[x], in2[x + 1])
241 const int inverseAlpha = 255 - alpha;
243 (alpha * qRed(
source) + inverseAlpha * qRed(destination)) >> 8,
244 (alpha * qGreen(
source) + inverseAlpha * qGreen(destination)) >> 8,
245 (alpha * qBlue(
source) + inverseAlpha * qBlue(destination)) >> 8
251 const quint32 background = 0xFFFFFFFF;
252 quint32 *buffer =
reinterpret_cast<quint32 *
>(
imageBuffer.data());
254 while (buffer != end) {
255 const int alpha = qAlpha(*buffer);
260 *buffer = background;
275 const QString fileName = QString(
"%1").arg(
partIndex, 7, 10, QLatin1Char(
'0'));
292 QFile(filePath).remove();
334 bool framesWrittens =
true;
336 Q_FOREACH (QRect region, regions) {
350 QObject* threadParent,
396 QElapsedTimer dbgTimer;
399 QDirIterator dirIterator(directory);
403 int recordIndex = -1;
404 while (dirIterator.hasNext()) {
407 const QString &fileName = dirIterator.fileName();
408 const QRegularExpressionMatch &match = snapshotFilePattern.match(fileName);
409 if (match.hasMatch()) {
410 int index = match.captured(1).toInt();
411 if (recordIndex < index)
415 dbgTools <<
"findLastPartNumber for" << directory <<
": " << dbgTimer.elapsed() <<
"ms";
424 bool alreadyWarn =
false;
425 bool alreadyErr =
false;
428 canvas->image()->waitForDone();
431 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
436 disconnect(el.writer.get(), SIGNAL(capturingDone(
int,
bool)),
q, SLOT(
onCapturingDone(
int,
bool)));
437 if (el.thread->isRunning())
440 warnResources <<
"One of the Recorder WriterPool threads has been blocked and has to be terminated. "
441 <<
"Thread Name: " << el.thread->objectName();
444 el.thread->terminate();
448 errResources <<
"Something odd has been happen. Krita was unable to stop one of the Recorder WriterPool Threads. "
449 <<
"Thread Name: " << el.thread->objectName();
456 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
478 auto writerPtr =
writerPool[newWorkerId].writer;
479 auto threadPtr =
writerPool[newWorkerId].thread;
480 threadPtr->setObjectName(QString(
"Krita-Recorder-WriterPool#%1").arg(newWorkerId));
481 connect(writerPtr.get(), SIGNAL(capturingDone(
int,
bool)),
q, SLOT(
onCapturingDone(
int,
bool)));
482 writerPtr->moveToThread(threadPtr.get());
483 threadPtr->start(QThread::IdlePriority);
490 for(
auto i = 0; i <
writerPool.size(); i++, j++)
510 : d(new
Private(this, recorderThreads))
511 , exporterSettings(es)
513 d->
timer.setTimerType(Qt::PreciseTimer);
529 bool restart =
d->
timer.isActive();
538 disconnect(proxy, SIGNAL(toolChanged(QString)),
this, SLOT(
onToolChanged(QString)));
549 connect(proxy, SIGNAL(toolChanged(QString)),
this, SLOT(
onToolChanged(QString)),
550 Qt::DirectConnection);
552 Qt::DirectConnection);
554 Qt::DirectConnection);
565 bool restart =
d->
timer.isActive();
600 connect(&
d->
timer, SIGNAL (timeout()),
this, SLOT (
onTimer()));
632 result = f.runAction([&] {
659 if (qobject_cast<KisMainWindow *>(QApplication::activeWindow()) ==
nullptr && !
d->
captureTail) {
664 (
d->
canvas->image()->isIsolatingLayer() ||
d->
canvas->image()->isIsolatingGroup())) {
688 QRect fullSize =
d->
canvas->image()->bounds();
704 float diffY = abs(region.y() -
d->
oldRegion.y()) / std::max(1.0f, (
float)
d->
oldRegion.height());
705 float diffW = abs(region.width() -
d->
oldRegion.width()) / std::max(1.0f, (
float)
d->
oldRegion.width());
706 float diffH = abs(region.height() -
d->
oldRegion.height()) / std::max(1.0f, (
float)
d->
oldRegion.height());
709 float maxDiff = std::min(std::max({diffX, diffY, diffW, diffH}), 1.0f);
712 const int maxFrames = 10;
715 int framesToGenerate = std::ceil(maxFrames * maxDiff);
717 totalFrames += framesToGenerate;
720 auto lerp = [](QRect a, QRect b, qreal t) {
725 out.setX(a.x() + t * (b.x() - a.x()));
726 out.setY(a.y() + t * (b.y() - a.y()));
727 out.setWidth(a.width() + t * (b.width() - a.width()));
728 out.setHeight(a.height() + t * (b.height() - a.height()));
734 auto expandToAspectRatio = [](QRectF
source, QRectF
target) {
735 float targetAspect = (float)
target.width() / (float)
target.height();
737 float sourceAspect = (float)
source.width() /
source.height();
741 if (sourceAspect > targetAspect) {
743 out.setHeight(
source.width() / targetAspect);
744 out.moveTop(out.top() - ((
source.height() - out.height()) / 2));
747 out.setWidth(
source.height() * targetAspect);
748 out.moveLeft(out.left() + ((
source.width() - out.width()) / 2));
752 if (out.bottom() >
target.bottom()) {
753 out.moveBottom(
target.bottom());
755 if (out.right() >
target.right()) {
756 out.moveRight(
target.right());
758 if (out.top() <
target.top()) {
759 out.moveTop(
target.top());
761 if (out.left() <
target.left()) {
762 out.moveLeft(
target.left());
769 auto easeOutCubic = [](qreal t) {
770 return 1 - qPow(1 - t, 3);
775 for (
int frame = 0; frame < totalFrames; frame++) {
777 QRect interpolatedRect =
lerp(
d->
oldRegion, region, easeOutCubic((qreal)(frame + 1) / (qreal)totalFrames));
778 QRect expandedRect = expandToAspectRatio(interpolatedRect, fullSize).toRect();
780 rects.push_back(expandedRect);
783 QRect copyArea = rects.first();
785 if (rects.length() > 1) {
786 QRect united = rects.first();
788 Q_FOREACH (QRect r, rects) {
789 united = united.united(r);
803 d->
canvas->image()->addSpontaneousJob(job);
819 d->
writerPool[workerId].thread->setPriority(QThread::IdlePriority);
833 (
d->
canvas->image()->isIsolatingLayer() ||
d->
canvas->image()->isIsolatingGroup()))
859 KConfigGroup cfg = KConfigGroup(config,
"");
871 KConfigGroup cfg = KConfigGroup(config,
"");
873 d->
oldRegion = cfg.readEntry(
"region", QRect(-1, -1, -1, -1));
885 const QString filename =
"tail";
900 for (
int i = beginIndex; i <= endIndex; i++) {
905 KConfigGroup cfg = KConfigGroup(config,
"");
907 cfg.writeEntry(
"tails", fileNames);
918 KConfigGroup cfg = KConfigGroup(config,
"");
922 if (fileNames.isEmpty()) {
929 Q_FOREACH (QString name, fileNames) {
934 QRegularExpression fileRegex =
937 QRegularExpressionMatch match = fileRegex.match(fileNames.first());
939 if (match.hasMatch()) {
940 int index = match.captured(1).toInt();
float value(const T *src, size_t ch)
KisMagneticGraph::vertex_descriptor target(typename KisMagneticGraph::edge_descriptor e, KisMagneticGraph g)
KisMagneticGraph::vertex_descriptor source(typename KisMagneticGraph::edge_descriptor e, KisMagneticGraph g)
QList< QString > QStringList
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
ColorPrimaries
The colorPrimaries enum Enum of colorants, follows ITU H.273 for values 0 to 255, and has extra known...
@ PRIMARIES_ITU_R_BT_709_5
TransferCharacteristics
The transferCharacteristics enum Enum of transfer characteristics, follows ITU H.273 for values 0 to ...
QPointF lerp(const QPointF &p1, const QPointF &p2, qreal t)
quint32 pixelSize() const
const KoColorSpace * colorSpace() const
void convertTo(const KoColorSpace *dstColorSpace, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags(), KUndo2Command *parentCommand=nullptr, KoUpdater *progressUpdater=nullptr)
void readBytes(quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h) const
virtual KoID colorModelId() const =0
virtual KoID colorDepthId() const =0
virtual const KoColorProfile * profile() const =0
volatile std::atomic_bool toolActivated
QPointer< KisCanvas2 > canvas
ThreadCounter & recorderThreads
int findLastIndex(const QString &directory)
RecorderWriterManager *const q
volatile std::atomic_bool isActivateBlackTool
volatile std::atomic_bool imageModified
void searchForFreeWriter()
Private(RecorderWriterManager *q_ptr, ThreadCounter &rt)
std::atomic_int freeWriterId
volatile std::atomic_bool isForceBlackTool
RecorderWriterSettings settings
volatile std::atomic_bool enabled
void saveState()
Generate a tail and save the recording region.
RecorderWriterManager()=delete
void start(bool toggleEnabled, bool restoreState)
bool stop(bool toggleEnabled, bool saveState)
void restoreState()
Remove the tail and restore region from a file.
void onCapturingDone(int workerId, bool success)
void onToolPrimaryActionActivated(bool activated)
void lowPerformanceWarning()
void recorderStopWarning()
void setCanvas(QPointer< KisCanvas2 > canvas)
void setEnabled(bool enabled)
void setup(const RecorderWriterSettings &settings)
ThreadCounter recorderThreads
const RecorderExportSettings & exporterSettings
void onToolChanged(const QString &toolId)
Private(Private &&)=delete
void removeFrameTransparency()
QPointer< KisCanvas2 > canvas
quint32 avg(quint32 c1, quint32 c2)
const KoColorSpace * targetCs
void halfSizeImageBuffer()
Private & operator=(Private &&)=delete
Private & operator=(const Private &)=default
quint32 blendSourceOver(const int alpha, const quint32 source, const quint32 destination)
const RecorderWriterSettings * settings
Private(QPointer< KisCanvas2 > c, const RecorderWriterSettings &s, const QDir &d)
void captureImage(QRect area, KisPaintDevice *device)
Private(const Private &)=default
void onCaptureImage(int index, QVector< QRect > regions, KisPaintDeviceSP device)
bool onCaptureImageImpl(int index, QRect region, KisPaintDevice *device)
void capturingDone(int writerId, bool success)
void setUsedAndNotify(int value)
bool setUsedImpl(int value)
unsigned int getUsed() const
void notifyValueChange(bool valueWasIncreased)
void setAndNotify(int value)
void notifyInUseChange(bool valueWasIncreased)
#define KIS_SAFE_ASSERT_RECOVER(cond)
const QString recorderMetaDataName
QRegularExpression snapshotFilePatternFor(const QString &extension)
constexpr int waitThreadTimeoutMs
const unsigned int MaxThreadCount
virtual ColorPrimaries getColorPrimaries() const
getColorPrimaries
virtual bool hasColorants() const =0
virtual TransferCharacteristics getTransferCharacteristics() const
getTransferCharacteristics This function should be subclassed at some point so we can get the value f...
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()
const KoColorProfile * p709SRGBProfile() const
bool recordIsolateLayerMode
QSharedPointer< RecorderWriter > writer
QSharedPointer< QThread > thread
WriterPoolEl(QObject *threadParent, unsigned int i, QPointer< KisCanvas2 > c, const RecorderWriterSettings &s, const QDir &d)