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