File indexing completed on 2024-12-22 04:12:57

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisStrokeEfficiencyMeasurer.h"
0008 
0009 #include <QPointF>
0010 #include <QVector>
0011 #include <QElapsedTimer>
0012 
0013 #include <boost/optional.hpp>
0014 
0015 #include "kis_global.h"
0016 
0017 struct KisStrokeEfficiencyMeasurer::Private
0018 {
0019     boost::optional<QPointF> lastSamplePos;
0020     qreal distance = 0;
0021 
0022     QElapsedTimer strokeTimeSource;
0023     bool isEnabled = true;
0024 
0025     int renderingStartTime = 0;
0026     int renderingTime = 0;
0027 
0028     int cursorMoveStartTime = 0;
0029     int cursorMoveTime = 0;
0030 
0031     int framesCount = 0;
0032 
0033 };
0034 
0035 KisStrokeEfficiencyMeasurer::KisStrokeEfficiencyMeasurer()
0036     : m_d(new Private())
0037 {
0038     m_d->strokeTimeSource.start();
0039 }
0040 
0041 KisStrokeEfficiencyMeasurer::~KisStrokeEfficiencyMeasurer()
0042 {
0043 }
0044 
0045 void KisStrokeEfficiencyMeasurer::setEnabled(bool value)
0046 {
0047     m_d->isEnabled = value;
0048 }
0049 
0050 bool KisStrokeEfficiencyMeasurer::isEnabled() const
0051 {
0052     return m_d->isEnabled;
0053 }
0054 
0055 void KisStrokeEfficiencyMeasurer::addSample(const QPointF &pt)
0056 {
0057     if (!m_d->isEnabled) return;
0058 
0059     if (!m_d->lastSamplePos) {
0060         m_d->lastSamplePos = pt;
0061     } else {
0062         m_d->distance += kisDistance(pt, *m_d->lastSamplePos);
0063         *m_d->lastSamplePos = pt;
0064     }
0065 }
0066 
0067 void KisStrokeEfficiencyMeasurer::addSamples(const QVector<QPointF> &points)
0068 {
0069     if (!m_d->isEnabled) return;
0070 
0071     Q_FOREACH (const QPointF &pt, points) {
0072         addSample(pt);
0073     }
0074 }
0075 
0076 void KisStrokeEfficiencyMeasurer::notifyRenderingStarted()
0077 {
0078     m_d->renderingStartTime = m_d->strokeTimeSource.elapsed();
0079 }
0080 
0081 void KisStrokeEfficiencyMeasurer::notifyRenderingFinished()
0082 {
0083     m_d->renderingTime = m_d->strokeTimeSource.elapsed() - m_d->renderingStartTime;
0084 }
0085 
0086 void KisStrokeEfficiencyMeasurer::notifyCursorMoveStarted()
0087 {
0088     m_d->cursorMoveStartTime = m_d->strokeTimeSource.elapsed();
0089 }
0090 
0091 void KisStrokeEfficiencyMeasurer::notifyCursorMoveFinished()
0092 {
0093     m_d->cursorMoveTime = m_d->strokeTimeSource.elapsed() - m_d->cursorMoveStartTime;
0094 }
0095 
0096 void KisStrokeEfficiencyMeasurer::notifyFrameRenderingStarted()
0097 {
0098     m_d->framesCount++;
0099 }
0100 
0101 qreal KisStrokeEfficiencyMeasurer::averageCursorSpeed() const
0102 {
0103     return m_d->cursorMoveTime ? m_d->distance / m_d->cursorMoveTime : 0.0;
0104 }
0105 
0106 qreal KisStrokeEfficiencyMeasurer::averageRenderingSpeed() const
0107 {
0108     return m_d->renderingTime ? m_d->distance / m_d->renderingTime : 0.0;
0109 }
0110 
0111 qreal KisStrokeEfficiencyMeasurer::averageFps() const
0112 {
0113     return m_d->renderingTime ? m_d->framesCount * 1000.0 / m_d->renderingTime : 0.0;
0114 }
0115 
0116