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

0001 /*
0002     SPDX-FileCopyrightText: 2013 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 #include "buffer.h"
0007 #include "buffer_p.h"
0008 #include "shm_pool.h"
0009 // system
0010 #include <string.h>
0011 // wayland
0012 #include <wayland-client-protocol.h>
0013 
0014 namespace KWayland
0015 {
0016 namespace Client
0017 {
0018 #ifndef K_DOXYGEN
0019 const struct wl_buffer_listener Buffer::Private::s_listener = {Buffer::Private::releasedCallback};
0020 #endif
0021 
0022 Buffer::Private::Private(Buffer *q, ShmPool *parent, wl_buffer *nativeBuffer, const QSize &size, int32_t stride, size_t offset, Format format)
0023     : shm(parent)
0024     , nativeBuffer(nativeBuffer)
0025     , released(false)
0026     , size(size)
0027     , stride(stride)
0028     , offset(offset)
0029     , used(false)
0030     , format(format)
0031     , q(q)
0032 {
0033     wl_buffer_add_listener(nativeBuffer, &s_listener, this);
0034 }
0035 
0036 Buffer::Private::~Private()
0037 {
0038     nativeBuffer.release();
0039 }
0040 
0041 void Buffer::Private::destroy()
0042 {
0043     nativeBuffer.destroy();
0044 }
0045 
0046 void Buffer::Private::releasedCallback(void *data, wl_buffer *buffer)
0047 {
0048     auto b = reinterpret_cast<Buffer::Private *>(data);
0049     Q_ASSERT(b->nativeBuffer == buffer);
0050     b->q->setReleased(true);
0051 }
0052 
0053 Buffer::Buffer(ShmPool *parent, wl_buffer *buffer, const QSize &size, int32_t stride, size_t offset, Format format)
0054     : d(new Private(this, parent, buffer, size, stride, offset, format))
0055 {
0056 }
0057 
0058 Buffer::~Buffer() = default;
0059 
0060 void Buffer::copy(const void *src)
0061 {
0062     memcpy(address(), src, d->size.height() * d->stride);
0063 }
0064 
0065 uchar *Buffer::address()
0066 {
0067     return reinterpret_cast<uchar *>(d->shm->poolAddress()) + d->offset;
0068 }
0069 
0070 wl_buffer *Buffer::buffer() const
0071 {
0072     return d->nativeBuffer;
0073 }
0074 
0075 Buffer::operator wl_buffer *()
0076 {
0077     return d->nativeBuffer;
0078 }
0079 
0080 Buffer::operator wl_buffer *() const
0081 {
0082     return d->nativeBuffer;
0083 }
0084 
0085 bool Buffer::isReleased() const
0086 {
0087     return d->released;
0088 }
0089 
0090 void Buffer::setReleased(bool released)
0091 {
0092     d->released = released;
0093 }
0094 
0095 QSize Buffer::size() const
0096 {
0097     return d->size;
0098 }
0099 
0100 int32_t Buffer::stride() const
0101 {
0102     return d->stride;
0103 }
0104 
0105 bool Buffer::isUsed() const
0106 {
0107     return d->used;
0108 }
0109 
0110 void Buffer::setUsed(bool used)
0111 {
0112     d->used = used;
0113 }
0114 
0115 Buffer::Format Buffer::format() const
0116 {
0117     return d->format;
0118 }
0119 
0120 quint32 Buffer::getId(wl_buffer *b)
0121 {
0122     return wl_proxy_get_id(reinterpret_cast<wl_proxy *>(b));
0123 }
0124 
0125 }
0126 }