1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef LINUXDMABUFCLIENTBUFFERINTEGRATION_H
5#define LINUXDMABUFCLIENTBUFFERINTEGRATION_H
6
7#include "linuxdmabuf.h"
8
9#include <QtWaylandCompositor/private/qwlclientbufferintegration_p.h>
10#include <QtWaylandCompositor/private/qwlclientbuffer_p.h>
11#include <QtWaylandCompositor/private/qwayland-server-wayland.h>
12#include <QtCore/QMutex>
13
14#include <drm_fourcc.h>
15
16QT_BEGIN_NAMESPACE
17
18typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYWAYLANDBUFFERWL_compat) (EGLDisplay dpy, struct wl_resource *buffer, EGLint attribute, EGLint *value);
19typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYDMABUFFORMATSEXTPROC) (EGLDisplay dpy, EGLint max_formats, EGLint *formats, EGLint *num_formats);
20typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYDMABUFMODIFIERSEXTPROC) (EGLDisplay dpy, EGLint format, EGLint max_modifiers, EGLuint64KHR *modifiers, EGLBoolean *external_only, EGLint *num_modifiers);
21
22class LinuxDmabufClientBufferIntegrationPrivate;
23class LinuxDmabufParams;
24class LinuxDmabufClientBuffer;
25
26// buffer conversion definitions to import YUV buffers
27struct YuvPlaneConversion {
28 EGLint format = DRM_FORMAT_YUYV;
29 EGLint widthDivisor = 1;
30 EGLint heightDivisor = 1;
31 EGLint planeIndex = 0;
32};
33struct YuvFormatConversion {
34 uint32_t inputPlanes = 1;
35 uint32_t outputPlanes = 1;
36 struct YuvPlaneConversion plane[LinuxDmabufWlBuffer::MaxDmabufPlanes];
37};
38
39class LinuxDmabufClientBufferIntegration : public QtWayland::ClientBufferIntegration
40{
41public:
42 LinuxDmabufClientBufferIntegration();
43 ~LinuxDmabufClientBufferIntegration() override;
44
45 void initializeHardware(struct ::wl_display *display) override;
46 QtWayland::ClientBuffer *createBufferFor(wl_resource *resource) override;
47 bool importBuffer(wl_resource *resource, LinuxDmabufWlBuffer *linuxDmabufBuffer);
48 void removeBuffer(wl_resource *resource);
49 void deleteOrphanedTextures();
50 void deleteSpecificOrphanedTexture(QOpenGLTexture *texture);
51 void deleteImage(EGLImageKHR image);
52 void deleteGLTextureWhenPossible(QOpenGLTexture *texture, QOpenGLContext* ctx);
53 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl_egl_image_target_texture_2d = nullptr;
54
55private:
56 Q_DISABLE_COPY(LinuxDmabufClientBufferIntegration)
57
58 PFNEGLBINDWAYLANDDISPLAYWL egl_bind_wayland_display = nullptr;
59 PFNEGLUNBINDWAYLANDDISPLAYWL egl_unbind_wayland_display = nullptr;
60 PFNEGLCREATEIMAGEKHRPROC egl_create_image = nullptr;
61 PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image = nullptr;
62 PFNEGLQUERYDMABUFMODIFIERSEXTPROC egl_query_dmabuf_modifiers_ext = nullptr;
63 PFNEGLQUERYDMABUFFORMATSEXTPROC egl_query_dmabuf_formats_ext = nullptr;
64
65 bool initSimpleTexture(LinuxDmabufWlBuffer *dmabufBuffer);
66 bool initYuvTexture(LinuxDmabufWlBuffer *dmabufBuffer);
67 QList<uint32_t> supportedDrmFormats();
68 QList<uint64_t> supportedDrmModifiers(uint32_t format);
69
70 EGLDisplay m_eglDisplay = EGL_NO_DISPLAY;
71 ::wl_display *m_wlDisplay = nullptr;
72 bool m_displayBound = false;
73
74 QMutex m_orphanedTexturesLock;
75 QList<QOpenGLTexture *> m_orphanedTextures;
76 QList<QMetaObject::Connection> m_orphanedTexturesAboutToBeDestroyedConnection;
77
78 QHash<EGLint, YuvFormatConversion> m_yuvFormats;
79 bool m_supportsDmabufModifiers = false;
80 QHash<struct ::wl_resource *, LinuxDmabufWlBuffer *> m_importedBuffers;
81 QScopedPointer<LinuxDmabuf> m_linuxDmabuf;
82};
83
84class LinuxDmabufClientBuffer : public QtWayland::ClientBuffer
85{
86public:
87 ~LinuxDmabufClientBuffer() override;
88
89 QWaylandBufferRef::BufferFormatEgl bufferFormatEgl() const override;
90 QSize size() const override;
91 QWaylandSurface::Origin origin() const override;
92 QOpenGLTexture *toOpenGlTexture(int plane) override;
93
94protected:
95 void setDestroyed() override;
96
97private:
98 friend class LinuxDmabufClientBufferIntegration;
99 friend class LinuxDmabufClientBufferIntegrationPrivate;
100
101 LinuxDmabufClientBuffer(LinuxDmabufClientBufferIntegration* integration, wl_resource *bufferResource, LinuxDmabufWlBuffer *dmabufBuffer);
102
103 LinuxDmabufWlBuffer *d = nullptr;
104 LinuxDmabufClientBufferIntegration *m_integration = nullptr;
105};
106
107QT_END_NAMESPACE
108
109#endif // LINUXDMABUFCLIENTBUFFERINTEGRATION_H
110

source code of qtwayland/src/hardwareintegration/compositor/linux-dmabuf-unstable-v1/linuxdmabufclientbufferintegration.h