summaryrefslogtreecommitdiff
path: root/src/debug/Overlay.hpp
blob: 02f7f60f56cb307e852e92ff69113b3f51353676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once

#include "../defines.hpp"
#include "../render/Texture.hpp"
#include "../helpers/time/Timer.hpp"
#include <map>
#include <deque>
#include <string>
#include <vector>

namespace Render {
    class IHyprRenderer;
    class ITexture;
}

namespace Debug {

    class CMonitorOverlay {
      public:
        int         draw(int offset, bool& cacheUpdated);
        const CBox& lastDrawnBox() const;

        void        renderData(PHLMONITOR pMonitor, float durationUs);
        void        renderDataNoOverlay(PHLMONITOR pMonitor, float durationUs);
        void        frameData(PHLMONITOR pMonitor);

        Vector2D    size() const;

      private:
        struct STextLine {
            std::string          text;
            CHyprColor           color;
            int                  fontSize = 10;
            SP<Render::ITexture> texture;
        };

        struct SMetricData {
            float avg = 0.F;
            float min = 0.F;
            float max = 0.F;
            float var = 0.F;
        };

        void                                           updateLine(size_t idx, const std::string& text, const CHyprColor& color, int fontSize, const std::string& fontFamily);
        void                                           rebuildCache();

        std::deque<float>                              m_lastFrametimes;
        std::deque<float>                              m_lastFPSPerSecond;
        std::deque<float>                              m_lastRenderTimes;
        std::deque<float>                              m_lastRenderTimesNoOverlay;
        std::deque<float>                              m_lastAnimationTicks;
        std::chrono::high_resolution_clock::time_point m_lastFrame;
        std::chrono::high_resolution_clock::time_point m_fpsSecondStart;
        size_t                                         m_framesInCurrentSecond = 0;
        PHLMONITORREF                                  m_monitor;

        std::vector<STextLine>                         m_cachedLines;
        bool                                           m_cacheValid = false;
        CTimer                                         m_cacheTimer;

        CBox                                           m_lastDrawnBox;

        friend class Render::IHyprRenderer;
    };

    class COverlay {
      public:
        COverlay();
        void draw();
        void renderData(PHLMONITOR, float durationUs);
        void renderDataNoOverlay(PHLMONITOR, float durationUs);
        void frameData(PHLMONITOR);

      private:
        std::map<PHLMONITORREF, CMonitorOverlay> m_monitorOverlays;
        CBox                                     m_lastDrawnBox;
        CTimer                                   m_frameTimer;

        void                                     createWarningTexture(float maxW);
        SP<Render::ITexture>                     m_warningTexture;
        float                                    m_warningTextureMaxW = 0;

        friend class CHyprMonitorDebugOverlay;
        friend class Render::IHyprRenderer;
    };

    UP<COverlay>& overlay();
}