File indexing completed on 2024-05-26 04:32:47

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-only
0005  */
0006 
0007 #include "Throttle.h"
0008 
0009 #include <QAction>
0010 #include <QThread>
0011 #include <QQmlContext>
0012 #include <QQmlEngine>
0013 #include <qmath.h>
0014 
0015 #include <klocalizedstring.h>
0016 #include <kactioncollection.h>
0017 
0018 #include <kis_image_config.h>
0019 #include <kis_icon.h>
0020 #include <KoCanvasBase.h>
0021 #include <KisViewManager.h>
0022 #include <kis_canvas2.h>
0023 #include <KisMainWindow.h>
0024 #include "kis_signal_compressor.h"
0025 
0026 #include "KisUpdateSchedulerConfigNotifier.h"
0027 
0028 #include <QVersionNumber>
0029 
0030 namespace
0031 {
0032 
0033 bool shouldSetAcceptTouchEvents()
0034 {
0035     // See https://bugreports.qt.io/browse/QTBUG-66718
0036     static QVersionNumber qtVersion = QVersionNumber::fromString(qVersion());
0037     static bool retval = qtVersion > QVersionNumber(5, 9, 3) && qtVersion.normalized() != QVersionNumber(5, 10);
0038     return retval;
0039 }
0040 
0041 } // namespace
0042 
0043 
0044 ThreadManager::ThreadManager(QObject *parent)
0045     : QObject(parent),
0046       m_configUpdateCompressor(new KisSignalCompressor(500, KisSignalCompressor::POSTPONE, this))
0047 {
0048     connect(m_configUpdateCompressor, SIGNAL(timeout()), SLOT(slotDoUpdateConfig()));
0049 }
0050 
0051 ThreadManager::~ThreadManager()
0052 {
0053 }
0054 
0055 void ThreadManager::setThreadCount(int threadCount)
0056 {
0057     threadCount = 1 + qreal(threadCount) * (maxThreadCount() - 1) / 100.0;
0058 
0059     if (m_threadCount != threadCount) {
0060         m_threadCount = threadCount;
0061         m_configUpdateCompressor->start();
0062         emit threadCountChanged();
0063     }
0064 }
0065 
0066 int ThreadManager::threadCount() const
0067 {
0068     return m_threadCount;
0069 }
0070 
0071 int ThreadManager::maxThreadCount() const
0072 {
0073     return QThread::idealThreadCount();
0074 }
0075 
0076 void ThreadManager::slotDoUpdateConfig()
0077 {
0078     KisImageConfig cfg;
0079     cfg.setMaxNumberOfThreads(m_threadCount);
0080     cfg.setFrameRenderingClones(qCeil(m_threadCount * 0.5));
0081     KisUpdateSchedulerConfigNotifier::instance()->notifyConfigChanged();
0082 }
0083 
0084 
0085 Throttle::Throttle(QWidget *parent)
0086     : QQuickWidget(parent)
0087 {
0088     if (shouldSetAcceptTouchEvents()) {
0089         setAttribute(Qt::WA_AcceptTouchEvents);
0090     }
0091     m_threadManager = new ThreadManager();
0092     // In % of available cores...
0093     engine()->rootContext()->setContextProperty("ThreadManager", m_threadManager);
0094     m_threadManager->setThreadCount(100);
0095     setSource(QUrl("qrc:/slider.qml"));
0096     setResizeMode(SizeRootObjectToView);
0097 }
0098 
0099 Throttle::~Throttle()
0100 {
0101     setSource(QUrl());
0102 }