1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Jolla Ltd, author: <giulio.camuffo@jollamobile.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtWaylandCompositor module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QDebug> |
31 | #include <QAtomicInt> |
32 | |
33 | #include "qwaylandbufferref.h" |
34 | #include "wayland_wrapper/qwlclientbuffer_p.h" |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | class QWaylandBufferRefPrivate |
39 | { |
40 | public: |
41 | QtWayland::ClientBuffer *buffer = nullptr; |
42 | |
43 | bool nullOrDestroyed() { |
44 | return !buffer || buffer->isDestroyed(); |
45 | } |
46 | }; |
47 | |
48 | /*! |
49 | * \class QWaylandBufferRef |
50 | * \inmodule QtWaylandCompositor |
51 | * \since 5.8 |
52 | * \brief The QWaylandBufferRef class holds the reference to a surface buffer. |
53 | * |
54 | * This class can be used to reference a surface buffer. As long as a reference |
55 | * to the buffer exists, it is owned by the compositor and the client cannot modify it. |
56 | */ |
57 | |
58 | /*! |
59 | * Constructs a null buffer ref. |
60 | */ |
61 | QWaylandBufferRef::QWaylandBufferRef() |
62 | : d(new QWaylandBufferRefPrivate) |
63 | { |
64 | d->buffer = nullptr; |
65 | } |
66 | |
67 | /*! |
68 | * Constructs a reference to \a buffer. |
69 | */ |
70 | QWaylandBufferRef::QWaylandBufferRef(QtWayland::ClientBuffer *buffer) |
71 | : d(new QWaylandBufferRefPrivate) |
72 | { |
73 | d->buffer = buffer; |
74 | if (buffer) |
75 | buffer->ref(); |
76 | } |
77 | |
78 | /*! |
79 | * Creates a new reference to the buffer referenced by \a ref. |
80 | */ |
81 | QWaylandBufferRef::QWaylandBufferRef(const QWaylandBufferRef &ref) |
82 | : d(new QWaylandBufferRefPrivate) |
83 | { |
84 | d->buffer = ref.d->buffer; |
85 | if (d->buffer) |
86 | d->buffer->ref(); |
87 | } |
88 | |
89 | /*! |
90 | * Dereferences the buffer. |
91 | */ |
92 | QWaylandBufferRef::~QWaylandBufferRef() |
93 | { |
94 | if (d->buffer) |
95 | d->buffer->deref(); |
96 | delete d; |
97 | } |
98 | |
99 | /*! |
100 | * Assigns \a ref to this buffer and adds a reference to it. The previously referenced buffer is |
101 | * dereferenced. |
102 | */ |
103 | QWaylandBufferRef &QWaylandBufferRef::operator=(const QWaylandBufferRef &ref) |
104 | { |
105 | if (ref.d->buffer) |
106 | ref.d->buffer->ref(); |
107 | |
108 | if (d->buffer) |
109 | d->buffer->deref(); |
110 | |
111 | d->buffer = ref.d->buffer; |
112 | |
113 | return *this; |
114 | } |
115 | |
116 | /*! |
117 | * Returns true if this QWaylandBufferRef references the same buffer as \a ref. |
118 | * Otherwise returns false. |
119 | */ |
120 | bool QWaylandBufferRef::operator==(const QWaylandBufferRef &ref) |
121 | { |
122 | return d->buffer == ref.d->buffer; |
123 | } |
124 | |
125 | /*! |
126 | * Returns false if this QWaylandBufferRef references the same buffer as \a ref. |
127 | * Otherwise returns true. |
128 | */ |
129 | bool QWaylandBufferRef::operator!=(const QWaylandBufferRef &ref) |
130 | { |
131 | return d->buffer != ref.d->buffer; |
132 | } |
133 | |
134 | /*! |
135 | * Returns true if this QWaylandBufferRef does not reference a buffer. |
136 | * Otherwise returns false. |
137 | * |
138 | * \sa hasBuffer(), hasContent() |
139 | */ |
140 | bool QWaylandBufferRef::isNull() const |
141 | { |
142 | return !d->buffer; |
143 | } |
144 | |
145 | /*! |
146 | * Returns true if this QWaylandBufferRef references a buffer. Otherwise returns false. |
147 | * |
148 | * \sa isNull(), hasContent() |
149 | */ |
150 | bool QWaylandBufferRef::hasBuffer() const |
151 | { |
152 | return d->buffer; |
153 | } |
154 | /*! |
155 | * Returns true if this QWaylandBufferRef references a buffer that has content. Otherwise returns false. |
156 | * |
157 | * \sa isNull(), hasBuffer() |
158 | */ |
159 | bool QWaylandBufferRef::hasContent() const |
160 | { |
161 | return QtWayland::ClientBuffer::hasContent(buffer: d->buffer); |
162 | } |
163 | |
164 | /*! |
165 | * Returns true if this QWaylandBufferRef references a buffer that |
166 | * has been destroyed. Otherwise returns false. |
167 | */ |
168 | bool QWaylandBufferRef::isDestroyed() const |
169 | { |
170 | return d->buffer && d->buffer->isDestroyed(); |
171 | } |
172 | |
173 | /*! |
174 | * Returns the Wayland resource for the buffer. |
175 | */ |
176 | struct ::wl_resource *QWaylandBufferRef::wl_buffer() const |
177 | { |
178 | return d->buffer ? d->buffer->waylandBufferHandle() : nullptr; |
179 | } |
180 | |
181 | /*! |
182 | * \internal |
183 | */ |
184 | QtWayland::ClientBuffer *QWaylandBufferRef::buffer() const |
185 | { |
186 | return d->buffer; |
187 | } |
188 | |
189 | /*! |
190 | * Returns the size of the buffer. |
191 | * If the buffer referenced is null, an invalid QSize() is returned. |
192 | */ |
193 | QSize QWaylandBufferRef::size() const |
194 | { |
195 | if (d->nullOrDestroyed()) |
196 | return QSize(); |
197 | |
198 | return d->buffer->size(); |
199 | } |
200 | |
201 | /*! |
202 | * Returns the origin of the buffer. |
203 | * If the buffer referenced is null, QWaylandSurface::OriginBottomLeft |
204 | * is returned. |
205 | */ |
206 | QWaylandSurface::Origin QWaylandBufferRef::origin() const |
207 | { |
208 | if (d->buffer) |
209 | return d->buffer->origin(); |
210 | |
211 | return QWaylandSurface::OriginBottomLeft; |
212 | } |
213 | |
214 | QWaylandBufferRef::BufferType QWaylandBufferRef::bufferType() const |
215 | { |
216 | if (d->nullOrDestroyed()) |
217 | return BufferType_Null; |
218 | |
219 | if (isSharedMemory()) |
220 | return BufferType_SharedMemory; |
221 | |
222 | return BufferType_Egl; |
223 | } |
224 | |
225 | QWaylandBufferRef::BufferFormatEgl QWaylandBufferRef::bufferFormatEgl() const |
226 | { |
227 | if (d->nullOrDestroyed()) |
228 | return BufferFormatEgl_Null; |
229 | |
230 | return d->buffer->bufferFormatEgl(); |
231 | } |
232 | |
233 | /*! |
234 | * Returns true if the buffer is a shared memory buffer. Otherwise returns false. |
235 | */ |
236 | bool QWaylandBufferRef::isSharedMemory() const |
237 | { |
238 | if (d->nullOrDestroyed()) |
239 | return false; |
240 | |
241 | return d->buffer->isSharedMemory(); |
242 | } |
243 | |
244 | /*! |
245 | * Returns an image with the contents of the buffer. |
246 | */ |
247 | QImage QWaylandBufferRef::image() const |
248 | { |
249 | if (d->nullOrDestroyed()) |
250 | return QImage(); |
251 | |
252 | return d->buffer->image(); |
253 | } |
254 | |
255 | #if QT_CONFIG(opengl) |
256 | /*! |
257 | * Returns an OpenGL texture for the buffer. \a plane is the index for multi-plane formats, such as YUV. |
258 | * |
259 | * The returned texture is owned by the buffer. The texture is only valid for as |
260 | * long as the buffer reference exists. The caller of this function must not delete the texture, and must |
261 | * keep a reference to the buffer for as long as the texture is being used. |
262 | * |
263 | * Returns \c nullptr if there is no valid buffer, or if no texture can be created. |
264 | */ |
265 | QOpenGLTexture *QWaylandBufferRef::toOpenGLTexture(int plane) const |
266 | { |
267 | if (d->nullOrDestroyed()) |
268 | return nullptr; |
269 | |
270 | return d->buffer->toOpenGlTexture(plane); |
271 | } |
272 | |
273 | /*! |
274 | * Returns the native handle for this buffer, and marks it as locked so it will not be |
275 | * released until unlockNativeBuffer() is called. |
276 | * |
277 | * Returns 0 if there is no native handle for this buffer, or if the lock was unsuccessful. |
278 | */ |
279 | quintptr QWaylandBufferRef::lockNativeBuffer() |
280 | { |
281 | return d->buffer->lockNativeBuffer(); |
282 | } |
283 | |
284 | /*! |
285 | * Marks the native buffer as no longer in use. \a handle must correspond to the value returned by |
286 | * a previous call to lockNativeBuffer(). |
287 | */ |
288 | void QWaylandBufferRef::unlockNativeBuffer(quintptr handle) |
289 | { |
290 | d->buffer->unlockNativeBuffer(native_buffer: handle); |
291 | } |
292 | |
293 | #endif |
294 | |
295 | QT_END_NAMESPACE |
296 | |