summaryrefslogtreecommitdiff
path: root/src/protocols/Screencopy.hpp
blob: 54b0d28c6c3932e2f3f95801dc29453b5fdd973d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include "../defines.hpp"
#include "./types/Buffer.hpp"
#include "wlr-screencopy-unstable-v1.hpp"
#include "WaylandProtocol.hpp"

#include <list>
#include <vector>
#include "../managers/HookSystemManager.hpp"
#include "../helpers/time/Timer.hpp"
#include "../helpers/time/Time.hpp"
#include "../render/Framebuffer.hpp"
#include "../managers/eventLoop/EventLoopTimer.hpp"
#include <aquamarine/buffer/Buffer.hpp>

class CMonitor;
class IHLBuffer;

enum eClientOwners {
    CLIENT_SCREENCOPY = 0,
    CLIENT_TOPLEVEL_EXPORT
};

class CScreencopyClient {
  public:
    CScreencopyClient(SP<CZwlrScreencopyManagerV1> resource_);
    ~CScreencopyClient();

    bool                  good();
    wl_client*            client();

    WP<CScreencopyClient> m_self;
    eClientOwners         m_clientOwner = CLIENT_SCREENCOPY;

    CTimer                m_lastFrame;
    int                   m_frameCounter = 0;

  private:
    SP<CZwlrScreencopyManagerV1> m_resource;

    int                          m_framesInLastHalfSecond = 0;
    CTimer                       m_lastMeasure;
    bool                         m_sentScreencast = false;

    SP<HOOK_CALLBACK_FN>         m_tickCallback;
    void                         onTick();

    void                         captureOutput(uint32_t frame, int32_t overlayCursor, wl_resource* output, CBox box);

    friend class CScreencopyProtocol;
};

class CScreencopyFrame {
  public:
    CScreencopyFrame(SP<CZwlrScreencopyFrameV1> resource, int32_t overlay_cursor, wl_resource* output, CBox box);

    bool                  good();

    WP<CScreencopyFrame>  m_self;
    WP<CScreencopyClient> m_client;

  private:
    SP<CZwlrScreencopyFrameV1> m_resource;

    PHLMONITORREF              m_monitor;
    bool                       m_overlayCursor = false;
    bool                       m_withDamage    = false;

    CHLBufferReference         m_buffer;
    bool                       m_bufferDMA    = false;
    uint32_t                   m_shmFormat    = 0;
    uint32_t                   m_dmabufFormat = 0;
    int                        m_shmStride    = 0;
    CBox                       m_box          = {};

    // if we have a pending perm, hold the buffer.
    CFramebuffer m_tempFb;

    void         copy(CZwlrScreencopyFrameV1* pFrame, wl_resource* buffer);
    void         copyDmabuf(std::function<void(bool)> callback);
    bool         copyShm();
    void         renderMon();
    void         storeTempFB();
    void         share();

    friend class CScreencopyProtocol;
};

class CScreencopyProtocol : public IWaylandProtocol {
  public:
    CScreencopyProtocol(const wl_interface* iface, const int& ver, const std::string& name);

    virtual void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id);
    void         destroyResource(CScreencopyClient* resource);
    void         destroyResource(CScreencopyFrame* resource);

    void         onOutputCommit(PHLMONITOR pMonitor);

  private:
    std::vector<SP<CScreencopyFrame>>  m_frames;
    std::vector<WP<CScreencopyFrame>>  m_framesAwaitingWrite;
    std::vector<SP<CScreencopyClient>> m_clients;

    void                               shareAllFrames(PHLMONITOR pMonitor);
    void                               shareFrame(CScreencopyFrame* frame);
    void                               sendFrameDamage(CScreencopyFrame* frame);
    bool                               copyFrameDmabuf(CScreencopyFrame* frame);
    bool                               copyFrameShm(CScreencopyFrame* frame, const Time::steady_tp& now);

    uint32_t                           drmFormatForMonitor(PHLMONITOR pMonitor);

    friend class CScreencopyFrame;
    friend class CScreencopyClient;
};

namespace PROTO {
    inline UP<CScreencopyProtocol> screencopy;
};