File indexing completed on 2024-12-22 05:09:19

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #ifndef WAYLAND_BUFFER_H
0007 #define WAYLAND_BUFFER_H
0008 
0009 #include <QScopedPointer>
0010 #include <QSize>
0011 #include <QWeakPointer>
0012 
0013 #include "KWayland/Client/kwaylandclient_export.h"
0014 
0015 struct wl_buffer;
0016 
0017 namespace KWayland
0018 {
0019 namespace Client
0020 {
0021 class ShmPool;
0022 
0023 /**
0024  * @short Wrapper class for wl_buffer interface.
0025  *
0026  * The Buffer is provided by ShmPool and is owned by ShmPool.
0027  *
0028  * @see ShmPool
0029  **/
0030 class KWAYLANDCLIENT_EXPORT Buffer
0031 {
0032 public:
0033     /**
0034      * All image formats supported by the implementation.
0035      **/
0036     enum class Format {
0037         ARGB32, ///< 32-bit ARGB format, can be used for QImage::Format_ARGB32 and QImage::Format_ARGB32_Premultiplied
0038         RGB32, ///< 32-bit RGB format, can be used for QImage::Format_RGB32
0039     };
0040 
0041     ~Buffer();
0042     /**
0043      * Copies the data from @p src into the Buffer.
0044      **/
0045     void copy(const void *src);
0046     /**
0047      * Sets the Buffer as @p released.
0048      * This is automatically invoked when the Wayland server sends the release event.
0049      * @param released Whether the Buffer got released by the Wayland server.
0050      * @see isReleased
0051      **/
0052     void setReleased(bool released);
0053     /**
0054      * Sets whether the Buffer is used.
0055      * If the Buffer may not be reused when it gets released, the user of a Buffer should
0056      * mark the Buffer as used. This is needed for example when the memory is shared with
0057      * a QImage. As soon as the Buffer can be reused again one should call this method with
0058      * @c false again.
0059      *
0060      * By default a Buffer is not used.
0061      *
0062      * @param used Whether the Buffer should be marked as used.
0063      * @see isUsed
0064      **/
0065     void setUsed(bool used);
0066 
0067     wl_buffer *buffer() const;
0068     /**
0069      * @returns The size of this Buffer.
0070      **/
0071     QSize size() const;
0072     /**
0073      * @returns The stride (bytes per line) of this Buffer.
0074      **/
0075     int32_t stride() const;
0076     /**
0077      * @returns @c true if the Wayland server doesn't need the Buffer anymore.
0078      **/
0079     bool isReleased() const;
0080     /**
0081      * @returns @c true if the Buffer's user is still needing the Buffer.
0082      **/
0083     bool isUsed() const;
0084     /**
0085      * @returns the memory address of this Buffer.
0086      **/
0087     uchar *address();
0088     /**
0089      * @returns The image format used by this Buffer.
0090      **/
0091     Format format() const;
0092 
0093     operator wl_buffer *();
0094     operator wl_buffer *() const;
0095 
0096     typedef QWeakPointer<Buffer> Ptr;
0097 
0098     /**
0099      * Helper method to get the id for a provided native buffer.
0100      * @since 5.3
0101      **/
0102     static quint32 getId(wl_buffer *b);
0103 
0104 private:
0105     friend class ShmPool;
0106     explicit Buffer(ShmPool *parent, wl_buffer *buffer, const QSize &size, int32_t stride, size_t offset, Format format);
0107     class Private;
0108     QScopedPointer<Private> d;
0109 };
0110 
0111 }
0112 }
0113 
0114 #endif