1 | /* |
2 | SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | #include "buffer.h" |
7 | #include "buffer_p.h" |
8 | #include "shm_pool.h" |
9 | // system |
10 | #include <string.h> |
11 | // wayland |
12 | #include <wayland-client-protocol.h> |
13 | |
14 | namespace KWayland |
15 | { |
16 | namespace Client |
17 | { |
18 | #ifndef K_DOXYGEN |
19 | const struct wl_buffer_listener Buffer::Private::s_listener = {.release: Buffer::Private::releasedCallback}; |
20 | #endif |
21 | |
22 | Buffer::Private::Private(Buffer *q, ShmPool *parent, wl_buffer *nativeBuffer, const QSize &size, int32_t stride, size_t offset, Format format) |
23 | : shm(parent) |
24 | , nativeBuffer(nativeBuffer) |
25 | , released(false) |
26 | , size(size) |
27 | , stride(stride) |
28 | , offset(offset) |
29 | , used(false) |
30 | , format(format) |
31 | , q(q) |
32 | { |
33 | wl_buffer_add_listener(wl_buffer: nativeBuffer, listener: &s_listener, data: this); |
34 | } |
35 | |
36 | Buffer::Private::~Private() |
37 | { |
38 | nativeBuffer.release(); |
39 | } |
40 | |
41 | void Buffer::Private::destroy() |
42 | { |
43 | nativeBuffer.destroy(); |
44 | } |
45 | |
46 | void Buffer::Private::releasedCallback(void *data, wl_buffer *buffer) |
47 | { |
48 | auto b = reinterpret_cast<Buffer::Private *>(data); |
49 | Q_ASSERT(b->nativeBuffer == buffer); |
50 | b->q->setReleased(true); |
51 | } |
52 | |
53 | Buffer::Buffer(ShmPool *parent, wl_buffer *buffer, const QSize &size, int32_t stride, size_t offset, Format format) |
54 | : d(new Private(this, parent, buffer, size, stride, offset, format)) |
55 | { |
56 | } |
57 | |
58 | Buffer::~Buffer() = default; |
59 | |
60 | void Buffer::copy(const void *src) |
61 | { |
62 | memcpy(dest: address(), src: src, n: d->size.height() * d->stride); |
63 | } |
64 | |
65 | uchar *Buffer::address() |
66 | { |
67 | return reinterpret_cast<uchar *>(d->shm->poolAddress()) + d->offset; |
68 | } |
69 | |
70 | wl_buffer *Buffer::buffer() const |
71 | { |
72 | return d->nativeBuffer; |
73 | } |
74 | |
75 | Buffer::operator wl_buffer *() |
76 | { |
77 | return d->nativeBuffer; |
78 | } |
79 | |
80 | Buffer::operator wl_buffer *() const |
81 | { |
82 | return d->nativeBuffer; |
83 | } |
84 | |
85 | bool Buffer::isReleased() const |
86 | { |
87 | return d->released; |
88 | } |
89 | |
90 | void Buffer::setReleased(bool released) |
91 | { |
92 | d->released = released; |
93 | } |
94 | |
95 | QSize Buffer::size() const |
96 | { |
97 | return d->size; |
98 | } |
99 | |
100 | int32_t Buffer::stride() const |
101 | { |
102 | return d->stride; |
103 | } |
104 | |
105 | bool Buffer::isUsed() const |
106 | { |
107 | return d->used; |
108 | } |
109 | |
110 | void Buffer::setUsed(bool used) |
111 | { |
112 | d->used = used; |
113 | } |
114 | |
115 | Buffer::Format Buffer::format() const |
116 | { |
117 | return d->format; |
118 | } |
119 | |
120 | quint32 Buffer::getId(wl_buffer *b) |
121 | { |
122 | return wl_proxy_get_id(proxy: reinterpret_cast<wl_proxy *>(b)); |
123 | } |
124 | |
125 | } |
126 | } |
127 | |