GCC Code Coverage Report


Directory: ./
File: src/client/linux_dmabuf_v1.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 83 106 78.3%
Branches: 13 28 46.4%

Line Branch Exec Source
1 /********************************************************************
2 Copyright 2020 Faveraux Adrien <ad1rie3@hotmail.fr>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "event_queue.h"
21 #include "linux_dmabuf_v1_p.h"
22
23 #include <drm_fourcc.h>
24
25 namespace Wrapland::Client
26 {
27
28 constexpr size_t modifier_shift = 32;
29 constexpr uint32_t modifier_cut = 0xFFFFFFFF;
30
31 const struct zwp_linux_dmabuf_v1_listener LinuxDmabufV1::Private::s_listener = {
32 LinuxDmabufV1::Private::callbackFormat,
33 LinuxDmabufV1::Private::callbackModifier,
34 };
35
36 3 LinuxDmabufV1::Private::Private(LinuxDmabufV1* q)
37 3 : q_ptr(q)
38 {
39 3 }
40
41 3 LinuxDmabufV1::LinuxDmabufV1(QObject* parent)
42 3 : QObject(parent)
43
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 , d_ptr(new Private(this))
44 3 {
45 3 }
46
47 6 LinuxDmabufV1::~LinuxDmabufV1() = default;
48
49 void LinuxDmabufV1::Private::callbackFormat(
50 [[maybe_unused]] void* data,
51 [[maybe_unused]] zwp_linux_dmabuf_v1* zwp_linux_dmabuf_v1,
52 uint32_t format)
53 {
54 /* DEPRECATED */
55 3 auto dmabuf = reinterpret_cast<LinuxDmabufV1*>(data);
56 dmabuf->d_ptr->formats.push_back({format, DRM_FORMAT_MOD_INVALID});
57 Q_EMIT dmabuf->supportedFormatsChanged();
58 }
59
60 4 void LinuxDmabufV1::Private::callbackModifier(
61 void* data,
62 [[maybe_unused]] zwp_linux_dmabuf_v1* zwp_linux_dmabuf_v1,
63 uint32_t format,
64 uint32_t modifier_hi,
65 uint32_t modifier_lo)
66 {
67 4 LinuxDmabufV1* dmabuf = reinterpret_cast<LinuxDmabufV1*>(data);
68
69 4 auto modifier = (static_cast<uint64_t>(modifier_hi) << modifier_shift) | modifier_lo;
70 4 dmabuf->d_ptr->formats.push_back({format, modifier});
71 4 Q_EMIT dmabuf->supportedFormatsChanged();
72 7 }
73
74 1 std::vector<drm_format> const& LinuxDmabufV1::supportedFormats()
75 {
76 1 return d_ptr->formats;
77 }
78
79 3 ParamsV1* LinuxDmabufV1::createParamsV1(QObject* parent)
80 {
81
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(isValid());
82
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto params = new ParamsV1(parent);
83 3 auto w = zwp_linux_dmabuf_v1_create_params(d_ptr->dmabuf);
84
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (d_ptr->queue) {
86 3 d_ptr->queue->addProxy(w);
87 3 }
88 3 params->setup(w);
89 3 return params;
90 }
91
92 void LinuxDmabufV1::release()
93 {
94 d_ptr->dmabuf.release();
95 }
96
97 6 bool LinuxDmabufV1::isValid() const
98 {
99 6 return d_ptr->dmabuf.isValid();
100 }
101
102 3 void LinuxDmabufV1::setup(zwp_linux_dmabuf_v1* dmabuf)
103 {
104
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(dmabuf);
105
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(!d_ptr->dmabuf);
106 3 d_ptr->dmabuf.setup(dmabuf);
107 3 zwp_linux_dmabuf_v1_add_listener(dmabuf, &(d_ptr->s_listener), this);
108 3 }
109
110 3 void LinuxDmabufV1::setEventQueue(EventQueue* queue)
111 {
112 3 d_ptr->queue = queue;
113 3 }
114
115 EventQueue* LinuxDmabufV1::eventQueue()
116 {
117 return d_ptr->queue;
118 }
119
120 LinuxDmabufV1::operator zwp_linux_dmabuf_v1*()
121 {
122 return d_ptr->dmabuf;
123 }
124
125 LinuxDmabufV1::operator zwp_linux_dmabuf_v1*() const
126 {
127 return d_ptr->dmabuf;
128 }
129
130 const zwp_linux_buffer_params_v1_listener ParamsV1::Private::s_listener = {
131 ParamsV1::Private::callbackCreateSucceeded,
132 ParamsV1::Private::callbackBufferCreationFail,
133 };
134
135 3 ParamsV1::Private::Private(ParamsV1* q)
136 3 : q_ptr(q)
137 {
138 3 }
139
140 3 ParamsV1::ParamsV1(QObject* parent)
141 3 : QObject(parent)
142
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 , d_ptr(new Private(this))
143 3 {
144 3 }
145
146 6 ParamsV1::~ParamsV1()
147 6 {
148
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 release();
149 6 }
150
151 3 void ParamsV1::release()
152 {
153 3 d_ptr->params.release();
154 3 }
155
156 1 void ParamsV1::Private::callbackCreateSucceeded(
157 [[maybe_unused]] void* data,
158 [[maybe_unused]] zwp_linux_buffer_params_v1* wlParams,
159 wl_buffer* wlBuffer)
160 {
161 1 auto params = reinterpret_cast<ParamsV1*>(data);
162 1 params->d_ptr->createdBuffer = wlBuffer;
163 1 Q_EMIT params->createSuccess(wlBuffer);
164 1 }
165
166 1 void ParamsV1::Private::callbackBufferCreationFail(
167 void* data,
168 [[maybe_unused]] zwp_linux_buffer_params_v1* wlParams)
169 {
170 1 auto params = reinterpret_cast<ParamsV1*>(data);
171 1 params->d_ptr->createdBuffer = nullptr;
172 1 Q_EMIT params->createFail();
173 1 }
174
175 3 void ParamsV1::setup(zwp_linux_buffer_params_v1* params)
176 {
177
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(params);
178
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(!d_ptr->params);
179 3 d_ptr->params.setup(params);
180 3 zwp_linux_buffer_params_v1_add_listener(d_ptr->params, &(d_ptr->s_listener), this);
181 3 }
182
183 3 bool ParamsV1::isValid() const
184 {
185 3 return d_ptr->params.isValid();
186 }
187
188 2 void ParamsV1::addDmabuf(int32_t fd,
189 uint32_t plane_idx,
190 uint32_t offset,
191 uint32_t stride,
192 uint64_t modifier)
193 {
194 2 auto modifier_hi = static_cast<uint32_t>(modifier >> modifier_shift);
195 2 auto modifier_lo = static_cast<uint32_t>(modifier & modifier_cut);
196 2 zwp_linux_buffer_params_v1_add(
197 2 d_ptr->params, fd, plane_idx, offset, stride, modifier_hi, modifier_lo);
198 2 }
199
200 2 void ParamsV1::createDmabuf(int32_t width, int32_t height, uint32_t format, uint32_t flags)
201 {
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (d_ptr->createdBuffer) {
203 Q_EMIT createSuccess(d_ptr->createdBuffer);
204 return;
205 }
206 2 zwp_linux_buffer_params_v1_create(d_ptr->params, width, height, format, flags);
207 2 }
208
209 wl_buffer*
210 ParamsV1::createDmabufImmediate(int32_t width, int32_t height, uint32_t format, uint32_t flags)
211 {
212 if (d_ptr->createdBuffer) {
213 return d_ptr->createdBuffer;
214 }
215 d_ptr->createdBuffer
216 = zwp_linux_buffer_params_v1_create_immed(d_ptr->params, width, height, format, flags);
217 return d_ptr->createdBuffer;
218 }
219
220 1 wl_buffer* ParamsV1::getBuffer()
221 {
222 1 return d_ptr->createdBuffer;
223 }
224
225 ParamsV1::operator zwp_linux_buffer_params_v1*()
226 {
227 return d_ptr->params;
228 }
229
230 ParamsV1::operator zwp_linux_buffer_params_v1*() const
231 {
232 return d_ptr->params;
233 }
234
235 }
236