File indexing completed on 2024-11-10 04:56:39

0001 /*
0002     SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "renderjournal.h"
0008 
0009 using namespace std::chrono_literals;
0010 
0011 namespace KWin
0012 {
0013 
0014 RenderJournal::RenderJournal()
0015 {
0016 }
0017 
0018 static std::chrono::nanoseconds mix(std::chrono::nanoseconds duration1, std::chrono::nanoseconds duration2, double ratio)
0019 {
0020     return std::chrono::nanoseconds(int64_t(std::round(duration1.count() * ratio + duration2.count() * (1 - ratio))));
0021 }
0022 
0023 void RenderJournal::add(std::chrono::nanoseconds renderTime, std::chrono::nanoseconds presentationTimestamp)
0024 {
0025     const auto timeDifference = m_lastAdd ? presentationTimestamp - *m_lastAdd : 10s;
0026     m_lastAdd = presentationTimestamp;
0027 
0028     static constexpr std::chrono::nanoseconds varianceTimeConstant = 3s;
0029     const double varianceRatio = std::clamp(timeDifference.count() / double(varianceTimeConstant.count()), 0.1, 1.0);
0030     const auto renderTimeDiff = std::max(renderTime - m_result, 0ns);
0031     m_variance = std::max(mix(renderTimeDiff, m_variance, varianceRatio), renderTimeDiff);
0032 
0033     static constexpr std::chrono::nanoseconds timeConstant = 500ms;
0034     const double ratio = std::clamp(timeDifference.count() / double(timeConstant.count()), 0.1, 1.0);
0035     m_result = mix(renderTime, m_result, ratio);
0036 }
0037 
0038 std::chrono::nanoseconds RenderJournal::result() const
0039 {
0040     return m_result + m_variance * 2;
0041 }
0042 
0043 } // namespace KWin