1 /********************************************************************
2 Copyright © 2018 Fredrik Höglund <fredrik@kde.org>
3 Copyright © 2019-2020 Roman Gilg <subdiff@gmail.com>
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) version 3, or any
9 later version accepted by the membership of KDE e.V. (or its
10 successor approved by the membership of KDE e.V.), which shall
11 act as a proxy defined in Section 6 of version 3 of the license.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20 *********************************************************************/
21 #pragma once
22 
23 #include <Wrapland/Server/wraplandserver_export.h>
24 
25 #include <QHash>
26 #include <QObject>
27 #include <QSet>
28 #include <QSize>
29 
30 #include <memory>
31 
32 namespace Wrapland
33 {
34 namespace Server
35 {
36 class Buffer;
37 class Display;
38 class LinuxDmabufBufferV1;
39 
40 /**
41  * Represents the global zpw_linux_dmabuf_v1 interface.
42  *
43  * This interface provides a way for clients to create generic dmabuf based wl_buffers.
44  */
45 class WRAPLANDSERVER_EXPORT LinuxDmabufV1 : public QObject
46 {
47     Q_OBJECT
48 public:
49     enum Flag {
50         YInverted = (1 << 0),        /// Contents are y-inverted
51         Interlaced = (1 << 1),       /// Content is interlaced
52         BottomFieldFirst = (1 << 2), /// Bottom field first
53     };
54 
55     Q_DECLARE_FLAGS(Flags, Flag)
56 
57     /**
58      * Represents a plane in a buffer
59      */
60     struct Plane {
61         int fd;            /// The dmabuf file descriptor
62         uint32_t offset;   /// The offset from the start of buffer
63         uint32_t stride;   /// The distance from the start of a row to the next row in bytes
64         uint64_t modifier; /// The layout modifier
65     };
66 
67     /**
68      * The Impl class provides an interface from the LinuxDmabufInterface into the compositor.
69      */
70     class Impl
71     {
72     public:
73         Impl() = default;
74         virtual ~Impl() = default;
75 
76         /**
77          * Imports a linux-dmabuf buffer into the compositor.
78          *
79          * The parent LinuxDmabufV1 class takes ownership of returned
80          * buffer objects.
81          *
82          * In return the returned buffer takes ownership of the file descriptor for each
83          * plane.
84          *
85          * Note that it is the responsibility of the caller to close the file descriptors
86          * when the import fails.
87          *
88          * @return The imported buffer on success, and nullptr otherwise.
89          */
90         virtual LinuxDmabufBufferV1*
91         importBuffer(const QVector<Plane>& planes, uint32_t format, const QSize& size, Flags flags)
92             = 0;
93     };
94 
95     ~LinuxDmabufV1() override;
96 
97     /**
98      * Sets the compositor implementation for the dmabuf interface.
99      *
100      * The ownership is not transferred by this call.
101      */
102     void setImpl(Impl* impl);
103 
104     void setSupportedFormatsWithModifiers(QHash<uint32_t, QSet<uint64_t>> const& set);
105 
106 private:
107     explicit LinuxDmabufV1(Display* display, QObject* parent = nullptr);
108 
109     friend class Display;
110     friend class Buffer;
111     friend class ParamsWrapperV1;
112     friend class ParamsV1;
113 
114     class Private;
115     std::unique_ptr<Private> d_ptr;
116 };
117 
118 class WRAPLANDSERVER_EXPORT LinuxDmabufBufferV1 : public QObject
119 {
120     Q_OBJECT
121 public:
122     LinuxDmabufBufferV1(uint32_t format, const QSize& size, QObject* parent = nullptr);
123     ~LinuxDmabufBufferV1() override;
124 
125     uint32_t format() const;
126     QSize size() const;
127 
128 Q_SIGNALS:
129     void resourceDestroyed();
130 
131 private:
132     friend class ParamsV1;
133     class Private;
134     Private* d_ptr;
135 };
136 
137 }
138 }
139 
140 Q_DECLARE_METATYPE(Wrapland::Server::LinuxDmabufV1*)
141 Q_DECLARE_OPERATORS_FOR_FLAGS(Wrapland::Server::LinuxDmabufV1::Flags)
142