1 /*
2  *  Copyright (c) 2017 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "KisStrokeEfficiencyMeasurer.h"
20 
21 #include <QPointF>
22 #include <QVector>
23 #include <QElapsedTimer>
24 
25 #include <boost/optional.hpp>
26 
27 #include "kis_global.h"
28 
29 struct KisStrokeEfficiencyMeasurer::Private
30 {
31     boost::optional<QPointF> lastSamplePos;
32     qreal distance = 0;
33 
34     QElapsedTimer strokeTimeSource;
35     bool isEnabled = true;
36 
37     int renderingStartTime = 0;
38     int renderingTime = 0;
39 
40     int cursorMoveStartTime = 0;
41     int cursorMoveTime = 0;
42 
43     int framesCount = 0;
44 
45 };
46 
KisStrokeEfficiencyMeasurer()47 KisStrokeEfficiencyMeasurer::KisStrokeEfficiencyMeasurer()
48     : m_d(new Private())
49 {
50     m_d->strokeTimeSource.start();
51 }
52 
~KisStrokeEfficiencyMeasurer()53 KisStrokeEfficiencyMeasurer::~KisStrokeEfficiencyMeasurer()
54 {
55 }
56 
setEnabled(bool value)57 void KisStrokeEfficiencyMeasurer::setEnabled(bool value)
58 {
59     m_d->isEnabled = value;
60 }
61 
isEnabled() const62 bool KisStrokeEfficiencyMeasurer::isEnabled() const
63 {
64     return m_d->isEnabled;
65 }
66 
addSample(const QPointF & pt)67 void KisStrokeEfficiencyMeasurer::addSample(const QPointF &pt)
68 {
69     if (!m_d->isEnabled) return;
70 
71     if (!m_d->lastSamplePos) {
72         m_d->lastSamplePos = pt;
73     } else {
74         m_d->distance += kisDistance(pt, *m_d->lastSamplePos);
75         *m_d->lastSamplePos = pt;
76     }
77 }
78 
addSamples(const QVector<QPointF> & points)79 void KisStrokeEfficiencyMeasurer::addSamples(const QVector<QPointF> &points)
80 {
81     if (!m_d->isEnabled) return;
82 
83     Q_FOREACH (const QPointF &pt, points) {
84         addSample(pt);
85     }
86 }
87 
notifyRenderingStarted()88 void KisStrokeEfficiencyMeasurer::notifyRenderingStarted()
89 {
90     m_d->renderingStartTime = m_d->strokeTimeSource.elapsed();
91 }
92 
notifyRenderingFinished()93 void KisStrokeEfficiencyMeasurer::notifyRenderingFinished()
94 {
95     m_d->renderingTime = m_d->strokeTimeSource.elapsed() - m_d->renderingStartTime;
96 }
97 
notifyCursorMoveStarted()98 void KisStrokeEfficiencyMeasurer::notifyCursorMoveStarted()
99 {
100     m_d->cursorMoveStartTime = m_d->strokeTimeSource.elapsed();
101 }
102 
notifyCursorMoveFinished()103 void KisStrokeEfficiencyMeasurer::notifyCursorMoveFinished()
104 {
105     m_d->cursorMoveTime = m_d->strokeTimeSource.elapsed() - m_d->cursorMoveStartTime;
106 }
107 
notifyFrameRenderingStarted()108 void KisStrokeEfficiencyMeasurer::notifyFrameRenderingStarted()
109 {
110     m_d->framesCount++;
111 }
112 
averageCursorSpeed() const113 qreal KisStrokeEfficiencyMeasurer::averageCursorSpeed() const
114 {
115     return m_d->cursorMoveTime ? m_d->distance / m_d->cursorMoveTime : 0.0;
116 }
117 
averageRenderingSpeed() const118 qreal KisStrokeEfficiencyMeasurer::averageRenderingSpeed() const
119 {
120     return m_d->renderingTime ? m_d->distance / m_d->renderingTime : 0.0;
121 }
122 
averageFps() const123 qreal KisStrokeEfficiencyMeasurer::averageFps() const
124 {
125     return m_d->renderingTime ? m_d->framesCount * 1000.0 / m_d->renderingTime : 0.0;
126 }
127 
128 
129