summaryrefslogtreecommitdiff
path: root/src/protocols/Screencopy.hpp
blob: d854c3bded8bd2cfdf364b6647c0dd79007359b9 (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
#pragma once

#include "../defines.hpp"
#include "wlr-screencopy-unstable-v1-protocol.h"

#include <list>
#include <vector>

class CMonitor;

struct SScreencopyClient {
    int          ref      = 0;
    wl_resource* resource = nullptr;

    bool         operator==(const SScreencopyClient& other) const {
        return resource == other.resource;
    }
};

struct SScreencopyFrame {
    wl_resource*       resource = nullptr;
    SScreencopyClient* client   = nullptr;

    uint32_t           shmFormat    = 0;
    uint32_t           dmabufFormat = 0;
    wlr_box            box          = {0};
    int                shmStride    = 0;

    bool               overlayCursor = false;
    bool               withDamage    = false;

    wlr_buffer_cap     bufferCap = WLR_BUFFER_CAP_SHM;

    wlr_buffer*        buffer = nullptr;

    CMonitor*          pMonitor = nullptr;

    bool               operator==(const SScreencopyFrame& other) const {
        return resource == other.resource && client == other.client;
    }
};

class CScreencopyProtocolManager {
  public:
    CScreencopyProtocolManager();

    void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id);
    void removeClient(SScreencopyClient* client, bool force = false);
    void removeFrame(SScreencopyFrame* frame, bool force = false);
    void displayDestroy();

    void captureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, wlr_box box = {0, 0, 0, 0});

    void copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer);

    void onRenderEnd(CMonitor* pMonitor);

  private:
    wl_global*                     m_pGlobal = nullptr;
    std::list<SScreencopyFrame>    m_lFrames;
    std::list<SScreencopyClient>   m_lClients;

    wl_listener                    m_liDisplayDestroy;

    std::vector<SScreencopyFrame*> m_vFramesAwaitingWrite;

    void                           shareFrame(SScreencopyFrame* frame);
    void                           sendFrameDamage(SScreencopyFrame* frame);
    bool                           copyFrameDmabuf(SScreencopyFrame* frame);
    bool                           copyFrameShm(SScreencopyFrame* frame, timespec* now);
};