Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwaylandbufferref.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Jolla Ltd, author: <giulio.camuffo@jollamobile.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QDebug>
6#include <QAtomicInt>
7
9#include "wayland_wrapper/qwlclientbuffer_p.h"
10
11#include <type_traits>
12
14
15#define CHECK1(l, r, op)
16 static_assert(std::is_same_v<
17 bool,
18 decltype(std::declval<QWaylandBufferRef l >() op
19 std::declval<QWaylandBufferRef r >())
20 >)
21#define CHECK2(l, r)
22 CHECK1(l, r, ==);
23 CHECK1(l, r, !=)
24#define CHECK(l, r)
25 CHECK2(l, r);
26 CHECK2(l &, r);
27 CHECK2(l &, r &);
28 CHECK2(l, r &)
29
31CHECK(const, );
32CHECK(const, const);
33CHECK(, const);
34
35#undef CHECK
36#undef CHECK2
37#undef CHECK1
38
40{
41public:
42 QtWayland::ClientBuffer *buffer = nullptr;
43
45 return !buffer || buffer->isDestroyed();
46 }
47};
48
49/*!
50 * \class QWaylandBufferRef
51 * \inmodule QtWaylandCompositor
52 * \since 5.8
53 * \brief The QWaylandBufferRef class holds the reference to a surface buffer.
54 *
55 * This class can be used to reference a surface buffer. As long as a reference
56 * to the buffer exists, it is owned by the compositor and the client cannot modify it.
57 */
58
59/*!
60 * Constructs a null buffer ref.
61 */
62QWaylandBufferRef::QWaylandBufferRef()
63 : d(new QWaylandBufferRefPrivate)
64{
65 d->buffer = nullptr;
66}
67
68/*!
69 * Constructs a reference to \a buffer.
70 */
71QWaylandBufferRef::QWaylandBufferRef(QtWayland::ClientBuffer *buffer)
72 : d(new QWaylandBufferRefPrivate)
73{
74 d->buffer = buffer;
75 if (buffer)
76 buffer->ref();
77}
78
79/*!
80 * Creates a new reference to the buffer referenced by \a ref.
81 */
82QWaylandBufferRef::QWaylandBufferRef(const QWaylandBufferRef &ref)
83 : d(new QWaylandBufferRefPrivate)
84{
85 d->buffer = ref.d->buffer;
86 if (d->buffer)
87 d->buffer->ref();
88}
89
90/*!
91 * Dereferences the buffer.
92 */
93QWaylandBufferRef::~QWaylandBufferRef()
94{
95 if (d->buffer)
96 d->buffer->deref();
97 delete d;
98}
99
100/*!
101 * Assigns \a ref to this buffer and adds a reference to it. The previously referenced buffer is
102 * dereferenced.
103 */
104QWaylandBufferRef &QWaylandBufferRef::operator=(const QWaylandBufferRef &ref)
105{
106 if (ref.d->buffer)
107 ref.d->buffer->ref();
108
109 if (d->buffer)
110 d->buffer->deref();
111
112 d->buffer = ref.d->buffer;
113
114 return *this;
115}
116
117/*!
118 \fn bool QWaylandBufferRef::operator==(const QWaylandBufferRef &lhs, const QWaylandBufferRef &rhs)
119
120 Returns \c true if \a lhs references the same buffer as \a rhs.
121 Otherwise returns \c{false}.
122 */
123bool operator==(const QWaylandBufferRef &lhs, const QWaylandBufferRef &rhs) noexcept
124{
125 return lhs.d->buffer == rhs.d->buffer;
126}
127
128/*!
129 \fn bool QWaylandBufferRef::operator!=(const QWaylandBufferRef &lhs, const QWaylandBufferRef &rhs)
130
131 Returns \c false if \a lhs references the same buffer as \a rhs.
132 Otherwise returns \c {true}.
133 */
134
135/*!
136 * Returns true if this QWaylandBufferRef does not reference a buffer.
137 * Otherwise returns false.
138 *
139 * \sa hasBuffer(), hasContent()
140 */
141bool QWaylandBufferRef::isNull() const
142{
143 return !d->buffer;
144}
145
146/*!
147 * Returns true if this QWaylandBufferRef references a buffer. Otherwise returns false.
148 *
149 * \sa isNull(), hasContent()
150 */
151bool QWaylandBufferRef::hasBuffer() const
152{
153 return d->buffer;
154}
155/*!
156 * Returns true if this QWaylandBufferRef references a buffer that has content. Otherwise returns false.
157 *
158 * \sa isNull(), hasBuffer()
159 */
160bool QWaylandBufferRef::hasContent() const
161{
162 return QtWayland::ClientBuffer::hasContent(d->buffer);
163}
164/*!
165 * Returns true if this QWaylandBufferRef references a buffer that has protected content. Otherwise returns false.
166 *
167 * \note This is an enabler which presumes support in the client buffer integration. None of the
168 * client buffer integrations included with Qt currently support protected content buffers.
169 *
170 * \since 6.2
171 * \sa hasContent()
172 */
173bool QWaylandBufferRef::hasProtectedContent() const
174{
175 return QtWayland::ClientBuffer::hasProtectedContent(d->buffer);
176}
177
178/*!
179 * Returns true if this QWaylandBufferRef references a buffer that
180 * has been destroyed. Otherwise returns false.
181 */
182bool QWaylandBufferRef::isDestroyed() const
183{
184 return d->buffer && d->buffer->isDestroyed();
185}
186
187/*!
188 * Returns the Wayland resource for the buffer.
189 */
190struct ::wl_resource *QWaylandBufferRef::wl_buffer() const
191{
192 return d->buffer ? d->buffer->waylandBufferHandle() : nullptr;
193}
194
195/*!
196 * \internal
197 */
198QtWayland::ClientBuffer *QWaylandBufferRef::buffer() const
199{
200 return d->buffer;
201}
202
203/*!
204 * Returns the size of the buffer.
205 * If the buffer referenced is null, an invalid QSize() is returned.
206 */
207QSize QWaylandBufferRef::size() const
208{
209 if (d->nullOrDestroyed())
210 return QSize();
211
212 return d->buffer->size();
213}
214
215/*!
216 * Returns the origin of the buffer.
217 * If the buffer referenced is null, QWaylandSurface::OriginBottomLeft
218 * is returned.
219 */
220QWaylandSurface::Origin QWaylandBufferRef::origin() const
221{
222 if (d->buffer)
223 return d->buffer->origin();
224
225 return QWaylandSurface::OriginBottomLeft;
226}
227
228QWaylandBufferRef::BufferType QWaylandBufferRef::bufferType() const
229{
230 if (d->nullOrDestroyed())
231 return BufferType_Null;
232
233 if (isSharedMemory())
234 return BufferType_SharedMemory;
235
236 return BufferType_Egl;
237}
238
239QWaylandBufferRef::BufferFormatEgl QWaylandBufferRef::bufferFormatEgl() const
240{
241 if (d->nullOrDestroyed())
242 return BufferFormatEgl_Null;
243
244 return d->buffer->bufferFormatEgl();
245}
246
247/*!
248 * Returns true if the buffer is a shared memory buffer. Otherwise returns false.
249 */
250bool QWaylandBufferRef::isSharedMemory() const
251{
252 if (d->nullOrDestroyed())
253 return false;
254
255 return d->buffer->isSharedMemory();
256}
257
258/*!
259 * Returns an image with the contents of the buffer.
260 */
261QImage QWaylandBufferRef::image() const
262{
263 if (d->nullOrDestroyed())
264 return QImage();
265
266 return d->buffer->image();
267}
268
269#if QT_CONFIG(opengl)
270/*!
271 * Returns an OpenGL texture for the buffer. \a plane is the index for multi-plane formats, such as YUV.
272 *
273 * The returned texture is owned by the buffer. The texture is only valid for as
274 * long as the buffer reference exists. The caller of this function must not delete the texture, and must
275 * keep a reference to the buffer for as long as the texture is being used.
276 *
277 * Returns \c nullptr if there is no valid buffer, or if no texture can be created.
278 */
279QOpenGLTexture *QWaylandBufferRef::toOpenGLTexture(int plane) const
280{
281 if (d->nullOrDestroyed())
282 return nullptr;
283
284 return d->buffer->toOpenGlTexture(plane);
285}
286
287/*!
288 * Returns the native handle for this buffer, and marks it as locked so it will not be
289 * released until unlockNativeBuffer() is called.
290 *
291 * Returns 0 if there is no native handle for this buffer, or if the lock was unsuccessful.
292 */
293quintptr QWaylandBufferRef::lockNativeBuffer()
294{
295 return d->buffer->lockNativeBuffer();
296}
297
298/*!
299 * Marks the native buffer as no longer in use. \a handle must correspond to the value returned by
300 * a previous call to lockNativeBuffer().
301 */
302void QWaylandBufferRef::unlockNativeBuffer(quintptr handle)
303{
304 d->buffer->unlockNativeBuffer(handle);
305}
306
307#endif
308
309QT_END_NAMESPACE
QtWayland::ClientBuffer * buffer
Combined button and popup list for selecting options.
#define CHECK(cvref)
bool operator==(const QWaylandBufferRef &lhs, const QWaylandBufferRef &rhs) noexcept