1 | // Copyright (C) 2018 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef LINUXDMABUF_H |
5 | #define LINUXDMABUF_H |
6 | |
7 | #include "qwayland-server-linux-dmabuf-unstable-v1.h" |
8 | |
9 | #include <QtWaylandCompositor/private/qwayland-server-wayland.h> |
10 | #include <QtWaylandCompositor/private/qwlclientbufferintegration_p.h> |
11 | |
12 | #include <QtOpenGL/QOpenGLTexture> |
13 | #include <QtCore/QObject> |
14 | #include <QtCore/QHash> |
15 | #include <QtCore/QSize> |
16 | #include <QtCore/QTextStream> |
17 | |
18 | #include <array> |
19 | #include <QtGui/QOpenGLContext> |
20 | #include <QtCore/QMutex> |
21 | |
22 | #include <EGL/egl.h> |
23 | #include <EGL/eglext.h> |
24 | |
25 | // compatibility with libdrm <= 2.4.74 |
26 | #ifndef DRM_FORMAT_RESERVED |
27 | #define DRM_FORMAT_RESERVED ((1ULL << 56) - 1) |
28 | #endif |
29 | #ifndef DRM_FORMAT_MOD_VENDOR_NONE |
30 | #define DRM_FORMAT_MOD_VENDOR_NONE 0 |
31 | #endif |
32 | #ifndef DRM_FORMAT_MOD_LINEAR |
33 | #define DRM_FORMAT_MOD_LINEAR fourcc_mod_code(NONE, 0) |
34 | #endif |
35 | #ifndef DRM_FORMAT_MOD_INVALID |
36 | #define DRM_FORMAT_MOD_INVALID fourcc_mod_code(NONE, DRM_FORMAT_RESERVED) |
37 | #endif |
38 | |
39 | // Copied from eglmesaext.h |
40 | #ifndef EGL_WL_bind_wayland_display |
41 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDWAYLANDDISPLAYWL) (EGLDisplay dpy, struct wl_display *display); |
42 | typedef EGLBoolean (EGLAPIENTRYP PFNEGLUNBINDWAYLANDDISPLAYWL) (EGLDisplay dpy, struct wl_display *display); |
43 | #endif |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | class QWaylandCompositor; |
48 | class QWaylandResource; |
49 | class LinuxDmabufParams; |
50 | class LinuxDmabufClientBufferIntegration; |
51 | |
52 | struct Plane { |
53 | int fd = -1; |
54 | uint32_t offset = 0; |
55 | uint32_t stride = 0; |
56 | uint64_t modifiers = 0; |
57 | }; |
58 | |
59 | class LinuxDmabuf : public QtWaylandServer::zwp_linux_dmabuf_v1 |
60 | { |
61 | public: |
62 | explicit LinuxDmabuf(wl_display *display, LinuxDmabufClientBufferIntegration *clientBufferIntegration); |
63 | |
64 | void setSupportedModifiers(const QHash<uint32_t, QList<uint64_t>> &modifiers); |
65 | |
66 | protected: |
67 | void zwp_linux_dmabuf_v1_bind_resource(Resource *resource) override; |
68 | void zwp_linux_dmabuf_v1_create_params(Resource *resource, uint32_t params_id) override; |
69 | |
70 | private: |
71 | QHash<uint32_t, QList<uint64_t>> m_modifiers; // key=DRM format, value=supported DRM modifiers for format |
72 | LinuxDmabufClientBufferIntegration *m_clientBufferIntegration; |
73 | }; |
74 | |
75 | class LinuxDmabufParams : public QtWaylandServer::zwp_linux_buffer_params_v1 |
76 | { |
77 | public: |
78 | explicit LinuxDmabufParams(LinuxDmabufClientBufferIntegration *clientBufferIntegration, wl_resource *resource); |
79 | ~LinuxDmabufParams() override; |
80 | |
81 | private: |
82 | bool handleCreateParams(Resource *resource, int width, int height, uint format, uint flags); |
83 | uint m_drmFormat = 0; |
84 | uint m_flags = 0; |
85 | QSize m_size; |
86 | bool m_used = false; |
87 | QMap<uint, Plane> m_planes; |
88 | LinuxDmabufClientBufferIntegration *m_clientBufferIntegration; |
89 | |
90 | protected: |
91 | void zwp_linux_buffer_params_v1_destroy(Resource *resource) override; |
92 | void zwp_linux_buffer_params_v1_add(Resource *resource, int32_t fd, uint32_t plane_idx, uint32_t offset, uint32_t stride, uint32_t modifier_hi, uint32_t modifier_lo) override; |
93 | void zwp_linux_buffer_params_v1_create(Resource *resource, int32_t width, int32_t height, uint32_t format, uint32_t flags) override; |
94 | void zwp_linux_buffer_params_v1_create_immed(Resource *resource, uint32_t buffer_id, int32_t width, int32_t height, uint32_t format, uint32_t flags) override; |
95 | void zwp_linux_buffer_params_v1_destroy_resource(Resource *resource) override; |
96 | |
97 | friend class LinuxDmabufClientBufferIntegrationPrivate; |
98 | }; |
99 | |
100 | class LinuxDmabufWlBuffer : public QtWaylandServer::wl_buffer |
101 | { |
102 | public: |
103 | explicit LinuxDmabufWlBuffer(::wl_client *client, LinuxDmabufClientBufferIntegration *clientBufferIntegration, uint id = 0); |
104 | ~LinuxDmabufWlBuffer() override; |
105 | |
106 | void initImage(uint32_t plane, EGLImageKHR image); |
107 | void initTexture(uint32_t plane, QOpenGLTexture *texture); |
108 | inline QSize size() const { return m_size; } |
109 | inline uint32_t flags() const { return m_flags; } |
110 | inline uint32_t drmFormat() const { return m_drmFormat; } |
111 | inline Plane& plane(uint index) { return m_planes.at(n: index); } |
112 | inline uint32_t planesNumber() const { return m_planesNumber; } |
113 | inline EGLImageKHR image(uint32_t plane) { return m_eglImages.at(n: plane); } |
114 | inline QOpenGLTexture *texture(uint32_t plane) const { return m_textures.at(n: plane); } |
115 | void buffer_destroy_resource(Resource *resource) override; |
116 | |
117 | static const uint32_t MaxDmabufPlanes = 4; |
118 | |
119 | private: |
120 | QSize m_size; |
121 | uint32_t m_flags = 0; |
122 | uint32_t m_drmFormat = EGL_TEXTURE_RGBA; |
123 | std::array<Plane, MaxDmabufPlanes> m_planes; |
124 | uint32_t m_planesNumber = 1; |
125 | LinuxDmabufClientBufferIntegration *m_clientBufferIntegration = nullptr; |
126 | std::array<EGLImageKHR, MaxDmabufPlanes> m_eglImages = { ._M_elems: {EGL_NO_IMAGE_KHR, EGL_NO_IMAGE_KHR, EGL_NO_IMAGE_KHR, EGL_NO_IMAGE_KHR} }; |
127 | std::array<QOpenGLTexture *, MaxDmabufPlanes> m_textures = { ._M_elems: {nullptr, nullptr, nullptr, nullptr} }; |
128 | std::array<QOpenGLContext *, MaxDmabufPlanes> m_texturesContext = { ._M_elems: {nullptr, nullptr, nullptr, nullptr} }; |
129 | std::array<QMetaObject::Connection, MaxDmabufPlanes> m_texturesAboutToBeDestroyedConnection = { ._M_elems: {QMetaObject::Connection(), QMetaObject::Connection(), QMetaObject::Connection(), QMetaObject::Connection()} }; |
130 | QMutex m_texturesLock; |
131 | |
132 | void freeResources(); |
133 | void buffer_destroy(Resource *resource) override; |
134 | |
135 | friend class LinuxDmabufParams; |
136 | }; |
137 | |
138 | QT_END_NAMESPACE |
139 | |
140 | #endif // LINUXDMABUF_H |
141 | |