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

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_opengl_canvas_debugger.h"
0008 
0009 #include <QGlobalStatic>
0010 
0011 #include <QElapsedTimer>
0012 #include <QDebug>
0013 
0014 #include "kis_config.h"
0015 #include <kis_config_notifier.h>
0016 
0017 struct KisOpenglCanvasDebugger::Private
0018 {
0019 
0020     Private()
0021         : fpsCounter(0),
0022           fpsSum(0),
0023           syncFlaggedCounter(0),
0024           syncFlaggedSum(0),
0025           isEnabled(true) {}
0026 
0027     QElapsedTimer time;
0028 
0029     int fpsCounter;
0030     int fpsSum;
0031 
0032     int syncFlaggedCounter;
0033     int syncFlaggedSum;
0034 
0035     bool isEnabled;
0036 };
0037 
0038 Q_GLOBAL_STATIC(KisOpenglCanvasDebugger, s_instance)
0039 
0040 KisOpenglCanvasDebugger::KisOpenglCanvasDebugger()
0041     : m_d(new Private)
0042 {
0043     connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(slotConfigChanged()));
0044     slotConfigChanged();
0045 }
0046 
0047 KisOpenglCanvasDebugger::~KisOpenglCanvasDebugger()
0048 {
0049 }
0050 
0051 KisOpenglCanvasDebugger*
0052 KisOpenglCanvasDebugger::instance()
0053 {
0054     return s_instance;
0055 }
0056 
0057 bool KisOpenglCanvasDebugger::showFpsOnCanvas() const
0058 {
0059     return m_d->isEnabled;
0060 }
0061 
0062 qreal KisOpenglCanvasDebugger::accumulatedFps()
0063 {
0064     qreal value = 0;
0065 
0066     if (m_d->fpsSum > 0) {
0067         value = qreal(m_d->fpsCounter) / m_d->fpsSum * 1000.0;
0068     }
0069 
0070     return value;
0071 }
0072 
0073 void KisOpenglCanvasDebugger::slotConfigChanged()
0074 {
0075     KisConfig cfg(true);
0076     m_d->isEnabled = cfg.enableOpenGLFramerateLogging();
0077 
0078     if (m_d->isEnabled) {
0079         m_d->time.start();
0080     }
0081 }
0082 
0083 void KisOpenglCanvasDebugger::notifyPaintRequested()
0084 {
0085     if (!m_d->isEnabled) return;
0086 
0087     m_d->fpsSum += m_d->time.restart();
0088     m_d->fpsCounter++;
0089 
0090     if (m_d->fpsCounter > 100 && m_d->fpsSum > 0) {
0091         qDebug() << "Requested FPS:" << qreal(m_d->fpsCounter) / m_d->fpsSum * 1000.0;
0092         m_d->fpsSum = 0;
0093         m_d->fpsCounter = 0;
0094     }
0095 }
0096 
0097 void KisOpenglCanvasDebugger::notifySyncStatus(bool isBusy)
0098 {
0099     if (!m_d->isEnabled) return;
0100 
0101     m_d->syncFlaggedSum += isBusy;
0102     m_d->syncFlaggedCounter++;
0103 
0104     if (m_d->syncFlaggedCounter > 500 && m_d->syncFlaggedSum > 0) {
0105         qDebug() << "glSync effectiveness:" << qreal(m_d->syncFlaggedSum) / m_d->syncFlaggedCounter;
0106         m_d->syncFlaggedSum = 0;
0107         m_d->syncFlaggedCounter = 0;
0108     }
0109 }