File indexing completed on 2024-05-19 05:32:28

0001 /*
0002     SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kwin_export.h"
0010 
0011 #include <QList>
0012 #include <QRegion>
0013 
0014 namespace KWin
0015 {
0016 
0017 /**
0018  * The DamageJournal class is a helper that tracks last N damage regions.
0019  */
0020 class KWIN_EXPORT DamageJournal
0021 {
0022 public:
0023     /**
0024      * Returns the maximum number of damage regions that can be stored in the journal.
0025      */
0026     int capacity() const
0027     {
0028         return m_capacity;
0029     }
0030 
0031     /**
0032      * Sets the maximum number of damage regions that can be stored in the journal
0033      * to @a capacity.
0034      */
0035     void setCapacity(int capacity)
0036     {
0037         m_capacity = capacity;
0038     }
0039 
0040     /**
0041      * Adds the specified @a region to the journal.
0042      */
0043     void add(const QRegion &region)
0044     {
0045         while (m_log.size() >= m_capacity) {
0046             m_log.takeLast();
0047         }
0048         m_log.prepend(region);
0049     }
0050 
0051     /**
0052      * Clears the damage journal. Typically, one would want to clear the damage journal
0053      * if a buffer swap fails for some reason.
0054      */
0055     void clear()
0056     {
0057         m_log.clear();
0058     }
0059 
0060     /**
0061      * Accumulates the damage regions in the log up to the specified @a bufferAge.
0062      *
0063      * If the specified buffer age value refers to a damage region older than the last
0064      * one in the journal, @a fallback will be returned.
0065      */
0066     QRegion accumulate(int bufferAge, const QRegion &fallback = QRegion()) const
0067     {
0068         QRegion region;
0069         if (bufferAge > 0 && bufferAge <= m_log.size()) {
0070             for (int i = 0; i < bufferAge - 1; ++i) {
0071                 region += m_log[i];
0072             }
0073         } else {
0074             region = fallback;
0075         }
0076         return region;
0077     }
0078 
0079     QRegion lastDamage() const
0080     {
0081         return m_log.first();
0082     }
0083 
0084 private:
0085     QList<QRegion> m_log;
0086     int m_capacity = 10;
0087 };
0088 
0089 } // namespace KWin