kwin/platformsupport/vsyncconvenience/softwarevsyncmonitor.cpp

61 lines
1.5 KiB
C++
Raw Normal View History

2020-11-19 08:52:29 +00:00
/*
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "softwarevsyncmonitor.h"
namespace KWin
{
SoftwareVsyncMonitor *SoftwareVsyncMonitor::create(QObject *parent)
{
return new SoftwareVsyncMonitor(parent);
}
SoftwareVsyncMonitor::SoftwareVsyncMonitor(QObject *parent)
: VsyncMonitor(parent)
, m_softwareClock(new QTimer(this))
{
connect(m_softwareClock, &QTimer::timeout, this, &SoftwareVsyncMonitor::handleSyntheticVsync);
m_softwareClock->setSingleShot(true);
}
int SoftwareVsyncMonitor::refreshRate() const
{
return m_refreshRate;
}
void SoftwareVsyncMonitor::setRefreshRate(int refreshRate)
{
m_refreshRate = refreshRate;
}
void SoftwareVsyncMonitor::handleSyntheticVsync()
{
emit vblankOccurred(m_vblankTimestamp);
}
template <typename T>
T alignTimestamp(const T &timestamp, const T &alignment)
{
return timestamp + ((alignment - (timestamp % alignment)) % alignment);
}
void SoftwareVsyncMonitor::arm()
{
if (m_softwareClock->isActive()) {
return;
}
const std::chrono::nanoseconds currentTime(std::chrono::steady_clock::now().time_since_epoch());
const std::chrono::nanoseconds vblankInterval(1'000'000'000'000ull / m_refreshRate);
m_vblankTimestamp = alignTimestamp(currentTime, vblankInterval);
m_softwareClock->start(std::chrono::duration_cast<std::chrono::milliseconds>(m_vblankTimestamp - currentTime));
}
} // namespace KWin