Index: libs/ui/kis_config.h =================================================================== --- libs/ui/kis_config.h +++ libs/ui/kis_config.h @@ -498,8 +498,8 @@ int stabilizerSampleSize(bool defaultValue = false) const; void setStabilizerSampleSize(int value); - int stabilizerDelayedPaintInterval(bool defaultValue = false) const; - void setStabilizerDelayedPaintInterval(int value); + int stabilizerDelayedPaint(bool defaultValue = false) const; + void setStabilizerDelayedPaint(bool value); QString customFFMpegPath(bool defaultValue = false) const; void setCustomFFMpegPath(const QString &value) const; Index: libs/ui/kis_config.cc =================================================================== --- libs/ui/kis_config.cc +++ libs/ui/kis_config.cc @@ -1747,17 +1747,17 @@ m_cfg.writeEntry("stabilizerSampleSize", value); } -int KisConfig::stabilizerDelayedPaintInterval(bool defaultValue) const +int KisConfig::stabilizerDelayedPaint(bool defaultValue) const { - const int defaultInterval = 20; + const bool defaultEnabled = true; return defaultValue ? - defaultInterval : m_cfg.readEntry("stabilizerDelayedPaintInterval", defaultInterval); + defaultEnabled : m_cfg.readEntry("stabilizerDelayedPaint", defaultEnabled); } -void KisConfig::setStabilizerDelayedPaintInterval(int value) +void KisConfig::setStabilizerDelayedPaint(bool value) { - m_cfg.writeEntry("stabilizerDelayedPaintInterval", value); + m_cfg.writeEntry("stabilizerDelayedPaint", value); } QString KisConfig::customFFMpegPath(bool defaultValue) const Index: libs/ui/tool/KisStabilizerDelayedPaintHelper.h =================================================================== --- libs/ui/tool/KisStabilizerDelayedPaintHelper.h +++ libs/ui/tool/KisStabilizerDelayedPaintHelper.h @@ -19,9 +19,9 @@ #ifndef KIS_STABILIZER_DELAYED_PAINT_HELPER_H #define KIS_STABILIZER_DELAYED_PAINT_HELPER_H +#include #include #include -#include #include #include @@ -40,9 +40,10 @@ TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo); }; - QTimer m_paintTimer; + QBasicTimer m_paintTimer; QQueue m_paintQueue; int m_lastPendingTime; + int m_paintInterval; QElapsedTimer m_elapsedTimer; // Callbacks @@ -54,7 +55,7 @@ ~KisStabilizerDelayedPaintHelper() override {} bool running() const { - return m_paintTimer.isActive(); + return m_elapsedTimer.isValid(); } bool hasLastPaintInformation() const { @@ -74,13 +75,14 @@ m_requestUpdateOutline = requestUpdateOutline; } - void start(int paintInterval, const KisPaintInformation &firstPaintInfo); + void start(const KisPaintInformation &firstPaintInfo); void update(const QVector &newPaintInfos); + void paintSome(); void end(); void cancel(); -private Q_SLOTS: - void stabilizerDelayedPaint(bool isEndStroke = false); +protected: + virtual void timerEvent(QTimerEvent *event) override; }; #endif // KIS_STABILIZER_DELAYED_PAINT_HELPER_H Index: libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp =================================================================== --- libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp +++ libs/ui/tool/KisStabilizerDelayedPaintHelper.cpp @@ -18,6 +18,10 @@ #include "KisStabilizerDelayedPaintHelper.h" +#include + +constexpr int fixedPaintTimerInterval = 20; + KisStabilizerDelayedPaintHelper::TimedPaintInfo::TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo) : elapsedTime(elapsedTime) , paintInfo(paintInfo) @@ -26,15 +30,14 @@ KisStabilizerDelayedPaintHelper::KisStabilizerDelayedPaintHelper() { - connect(&m_paintTimer, SIGNAL(timeout()), SLOT(stabilizerDelayedPaint())); } -void KisStabilizerDelayedPaintHelper::start(int paintInterval, const KisPaintInformation &firstPaintInfo) { +void KisStabilizerDelayedPaintHelper::start(const KisPaintInformation &firstPaintInfo) { if (running()) { cancel(); } - m_paintTimer.setInterval(paintInterval); - m_paintTimer.start(); + // No need to start m_paintTimer here because nothing will need to be + // painted before any further pointer events. m_elapsedTimer.start(); m_lastPendingTime = m_elapsedTimer.elapsed(); m_paintQueue.enqueue(TimedPaintInfo(m_lastPendingTime, firstPaintInfo)); @@ -51,35 +54,47 @@ m_lastPendingTime = now; } +void KisStabilizerDelayedPaintHelper::paintSome() { + // [Re]start timer. + // No need to use Qt::PreciseTimer. This function is also called + // from KisToolFreehandHelper::paint + m_paintTimer.start(fixedPaintTimerInterval, Qt::CoarseTimer, this); + if (m_paintQueue.isEmpty()) { + return; + } + int now = m_elapsedTimer.elapsed(); + // Always keep one in the queue since painting requires two points + while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { + const TimedPaintInfo dequeued = m_paintQueue.dequeue(); + m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); + } +} + void KisStabilizerDelayedPaintHelper::end() { - stabilizerDelayedPaint(true); m_paintTimer.stop(); + m_elapsedTimer.invalidate(); + if (m_paintQueue.isEmpty()) { + return; + } + TimedPaintInfo dequeued = m_paintQueue.dequeue(); + while (!m_paintQueue.isEmpty()) { + const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); + m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); + dequeued = dequeued2; + } } void KisStabilizerDelayedPaintHelper::cancel() { m_paintTimer.stop(); + m_elapsedTimer.invalidate(); m_paintQueue.clear(); } -void KisStabilizerDelayedPaintHelper::stabilizerDelayedPaint(bool isEndStroke) { - if (m_paintQueue.isEmpty()) { - return; - } - if (isEndStroke) { - TimedPaintInfo dequeued = m_paintQueue.dequeue(); - while (!m_paintQueue.isEmpty()) { - const TimedPaintInfo dequeued2 = m_paintQueue.dequeue(); - m_paintLine(dequeued.paintInfo, dequeued2.paintInfo); - dequeued = dequeued2; - } +void KisStabilizerDelayedPaintHelper::timerEvent(QTimerEvent *event) { + if (event->timerId() == m_paintTimer.timerId()) { + paintSome(); + // Explicitly update the outline because this is outside the pointer + // event m_requestUpdateOutline(); - return; - } - int now = m_elapsedTimer.elapsed(); - // Always keep one in the queue since painting requires two points - while (m_paintQueue.size() > 1 && m_paintQueue.head().elapsedTime <= now) { - const TimedPaintInfo dequeued = m_paintQueue.dequeue(); - m_paintLine(dequeued.paintInfo, m_paintQueue.head().paintInfo); } - m_requestUpdateOutline(); } Index: libs/ui/tool/kis_tool_freehand_helper.h =================================================================== --- libs/ui/tool/kis_tool_freehand_helper.h +++ libs/ui/tool/kis_tool_freehand_helper.h @@ -137,6 +137,8 @@ const QPointF &control2, const KisPaintInformation &pi2); + virtual void timerEvent(QTimerEvent *event) override; + private: void paint(KisPaintInformation &info); void paintBezierSegment(KisPaintInformation pi1, KisPaintInformation pi2, Index: libs/ui/tool/kis_tool_freehand_helper.cpp =================================================================== --- libs/ui/tool/kis_tool_freehand_helper.cpp +++ libs/ui/tool/kis_tool_freehand_helper.cpp @@ -18,6 +18,7 @@ #include "kis_tool_freehand_helper.h" +#include #include #include @@ -97,7 +98,7 @@ // Stabilizer data QQueue stabilizerDeque; - QTimer stabilizerPollTimer; + QBasicTimer stabilizerPollTimer; KisStabilizedEventsSampler stabilizedSampler; KisStabilizerDelayedPaintHelper stabilizerDelayedPaintHelper; @@ -124,7 +125,6 @@ m_d->strokeTimeoutTimer.setSingleShot(true); connect(&m_d->strokeTimeoutTimer, SIGNAL(timeout()), SLOT(finishStroke())); connect(&m_d->airbrushingTimer, SIGNAL(timeout()), SLOT(doAirbrushing())); - connect(&m_d->stabilizerPollTimer, SIGNAL(timeout()), SLOT(stabilizerPollAndPaint())); m_d->stabilizerDelayedPaintHelper.setPaintLineCallback( [this](const KisPaintInformation &pi1, const KisPaintInformation &pi2) { @@ -586,6 +586,10 @@ if (m_d->smoothingOptions->smoothingType() == KisSmoothingOptions::STABILIZER) { m_d->stabilizedSampler.addEvent(info); + if (m_d->stabilizerDelayedPaintHelper.running()) { + // Paint here so we don't have to rely on the timer + m_d->stabilizerDelayedPaintHelper.paintSome(); + } } else { m_d->previousPaintInformation = info; } @@ -678,12 +682,11 @@ // Poll and draw regularly KisConfig cfg; int stabilizerSampleSize = cfg.stabilizerSampleSize(); - m_d->stabilizerPollTimer.setInterval(stabilizerSampleSize); - m_d->stabilizerPollTimer.start(); + m_d->stabilizerPollTimer.start(stabilizerSampleSize, Qt::CoarseTimer, this); - int delayedPaintInterval = cfg.stabilizerDelayedPaintInterval(); - if (delayedPaintInterval < stabilizerSampleSize) { - m_d->stabilizerDelayedPaintHelper.start(delayedPaintInterval, firstPaintInfo); + bool delayedPaintEnabled = cfg.stabilizerDelayedPaint(); + if (delayedPaintEnabled) { + m_d->stabilizerDelayedPaintHelper.start(firstPaintInfo); } m_d->stabilizedSampler.clear(); @@ -976,3 +979,9 @@ m_d->canvasMirroredH = mirrored; } +void KisToolFreehandHelper::timerEvent(QTimerEvent *event) +{ + if (event->timerId() == m_d->stabilizerPollTimer.timerId()) { + stabilizerPollAndPaint(); + } +}