summaryrefslogtreecommitdiff
path: root/src/protocols/core/Shm.hpp
blob: 37210398a8dc418a7e953db1aaeafe37a1beabe6 (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
#pragma once

/*
    Implementations for:
     - wl_shm
     - wl_shm_pool
     - wl_buffer with shm
*/

#include <hyprutils/os/FileDescriptor.hpp>
#include <memory>
#include <vector>
#include <cstdint>
#include "../WaylandProtocol.hpp"
#include "wayland.hpp"
#include "../types/Buffer.hpp"
#include "../../helpers/math/Math.hpp"

class CWLSHMPoolResource;

class CSHMPool {
  public:
    CSHMPool() = delete;
    CSHMPool(Hyprutils::OS::CFileDescriptor fd, size_t size);
    ~CSHMPool();

    Hyprutils::OS::CFileDescriptor m_fd;
    size_t                         m_size = 0;
    void*                          m_data = nullptr;

    void                           resize(size_t size);
};

class CWLSHMBuffer : public IHLBuffer {
  public:
    CWLSHMBuffer(WP<CWLSHMPoolResource> pool, uint32_t id, int32_t offset, const Vector2D& size, int32_t stride, uint32_t fmt);
    virtual ~CWLSHMBuffer();

    virtual Aquamarine::eBufferCapability          caps();
    virtual Aquamarine::eBufferType                type();
    virtual void                                   update(const CRegion& damage);
    virtual bool                                   isSynchronous();
    virtual Aquamarine::SSHMAttrs                  shm();
    virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
    virtual void                                   endDataPtr();

    bool                                           good();

    int32_t                                        m_offset = 0;
    int32_t                                        m_stride = 0;
    uint32_t                                       m_fmt    = 0;
    SP<CSHMPool>                                   m_pool;

  private:
    struct {
        CHyprSignalListener bufferResourceDestroy;
    } m_listeners;
};

class CWLSHMPoolResource {
  public:
    CWLSHMPoolResource(UP<CWlShmPool>&& resource_, Hyprutils::OS::CFileDescriptor fd, size_t size);

    bool                   good();

    SP<CSHMPool>           m_pool;

    WP<CWLSHMPoolResource> m_self;

  private:
    UP<CWlShmPool> m_resource;

    friend class CWLSHMBuffer;
};

class CWLSHMResource {
  public:
    CWLSHMResource(UP<CWlShm>&& resource_);

    bool good();

  private:
    UP<CWlShm> m_resource;
};

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

    virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);

  private:
    void destroyResource(CWLSHMResource* resource);
    void destroyResource(CWLSHMPoolResource* resource);
    void destroyResource(CWLSHMBuffer* resource);

    //
    std::vector<UP<CWLSHMResource>>     m_managers;
    std::vector<UP<CWLSHMPoolResource>> m_pools;
    std::vector<SP<CWLSHMBuffer>>       m_buffers;

    //
    std::vector<uint32_t> m_shmFormats;

    friend class CWLSHMResource;
    friend class CWLSHMPoolResource;
    friend class CWLSHMBuffer;
};

namespace PROTO {
    inline UP<CWLSHMProtocol> shm;
};