scene/surfaceitem: prevent division by zero

If the steady_clock's resolution is very limited, now - m_lastDamage might be zero,
so enforce a minimum time of 10us (or 100'000 fps)
This commit is contained in:
Xaver Hugl 2024-02-05 15:59:36 +01:00 committed by Vlad Zahorodnii
parent e97b6032c2
commit 41aeecbb2a

View file

@ -8,6 +8,8 @@
#include "core/pixelgrid.h"
#include "scene/scene.h"
using namespace std::chrono_literals;
namespace KWin
{
@ -102,13 +104,13 @@ static QRegion expandRegion(const QRegion &region, const QMargins &padding)
void SurfaceItem::addDamage(const QRegion &region)
{
if (m_lastDamage) {
const auto diff = std::chrono::steady_clock::now() - *m_lastDamage;
const auto diff = std::max(std::chrono::steady_clock::now() - *m_lastDamage, 10'000ns);
m_lastDamageTimeDiffs.push_back(diff);
if (m_lastDamageTimeDiffs.size() > 100) {
m_lastDamageTimeDiffs.pop_front();
}
const auto average = std::accumulate(m_lastDamageTimeDiffs.begin(), m_lastDamageTimeDiffs.end(), std::chrono::nanoseconds::zero()) / m_lastDamageTimeDiffs.size();
m_refreshRate = std::chrono::nanoseconds(1'000'000'000) / average;
const auto average = std::accumulate(m_lastDamageTimeDiffs.begin(), m_lastDamageTimeDiffs.end(), 0ns) / m_lastDamageTimeDiffs.size();
m_refreshRate = 1'000'000'000ns / average;
}
m_lastDamage = std::chrono::steady_clock::now();
m_damage += region;