File: WPEBufferDMABufFormats.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (353 lines) | stat: -rw-r--r-- 11,772 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (C) 2024 Igalia S.L.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WPEBufferDMABufFormats.h"

#include <wtf/FastMalloc.h>
#include <wtf/Vector.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>

struct DMABufFormat {
    explicit DMABufFormat(guint32 fourcc)
        : fourcc(fourcc)
        , modifiers(adoptGRef(g_array_new(FALSE, TRUE, sizeof(guint64))))
    {
    }

    ~DMABufFormat() = default;
    DMABufFormat(const DMABufFormat&) = delete;
    DMABufFormat& operator=(const DMABufFormat&) = delete;
    DMABufFormat(DMABufFormat&& other)
        : fourcc(other.fourcc)
        , modifiers(WTFMove(other.modifiers))
    {
    }

    guint32 fourcc { 0 };
    GRefPtr<GArray> modifiers;
};

struct DMABufFormatsGroup {
    DMABufFormatsGroup(const char* device, WPEBufferDMABufFormatUsage usage)
        : device(device)
        , usage(usage)
    {
    }

    ~DMABufFormatsGroup() = default;
    DMABufFormatsGroup(const DMABufFormatsGroup&) = delete;
    DMABufFormatsGroup& operator=(const DMABufFormatsGroup&) = delete;
    DMABufFormatsGroup(DMABufFormatsGroup&& other)
        : device(WTFMove(other.device))
        , usage(other.usage)
        , formats(WTFMove(other.formats))
    {
        other.usage = WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING;
    }

    CString device;
    WPEBufferDMABufFormatUsage usage { WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING };
    Vector<DMABufFormat> formats;
};

/**
 * WPEBufferDMABufFormats:
 *
 * List of supported DMA-BUF buffer formats
 */
struct _WPEBufferDMABufFormatsPrivate {
    CString device;
    Vector<DMABufFormatsGroup> groups;
};
WEBKIT_DEFINE_FINAL_TYPE(WPEBufferDMABufFormats, wpe_buffer_dma_buf_formats, G_TYPE_OBJECT, GObject)

static void wpe_buffer_dma_buf_formats_class_init(WPEBufferDMABufFormatsClass*/* bufferDMABufFormatsClass*/)
{
}

/**
 * wpe_buffer_dma_buf_formats_get_device:
 * @formats: a #WPEBufferDMABufFormats
 *
 * Get the main DRM device to be used to allocate buffer for @formats
 *
 * Returns: (transfer none) (nullable): the main DRM device for @formats
 */
const char* wpe_buffer_dma_buf_formats_get_device(WPEBufferDMABufFormats* formats)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), nullptr);

    return formats->priv->device.data();
}

/**
 * wpe_buffer_dma_buf_formats_get_n_groups:
 * @formats: a #WPEBufferDMABufFormats
 *
 * Get the number of groups in @formats
 *
 * Returns: the number of groups
 */
guint wpe_buffer_dma_buf_formats_get_n_groups(WPEBufferDMABufFormats* formats)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), 0);

    return formats->priv->groups.size();
}

/**
 * wpe_buffer_dma_buf_formats_get_group_usage:
 * @formats: a #WPEBufferDMABufFormats
 * @group: a group index
 *
 * Get the #WPEBufferDMABufFormatUsage of @group in @formats
 *
 * Returns: a #WPEBufferDMABufFormatUsage.
 */
WPEBufferDMABufFormatUsage wpe_buffer_dma_buf_formats_get_group_usage(WPEBufferDMABufFormats* formats, guint group)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING);
    g_return_val_if_fail(group < formats->priv->groups.size(), WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING);

    if (group >= formats->priv->groups.size())
        return WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING;

    return formats->priv->groups[group].usage;
}

/**
 * wpe_buffer_dma_buf_formats_get_group_device:
 * @formats: a #WPEBufferDMABufFormats
 * @group: a group index
 *
 * Get the target DRM device of @group in @formats
 *
 * Returns: (transfer none) (nullable): the target DRM device
 */
const char* wpe_buffer_dma_buf_formats_get_group_device(WPEBufferDMABufFormats* formats, guint group)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), nullptr);
    g_return_val_if_fail(group < formats->priv->groups.size(), nullptr);

    if (group >= formats->priv->groups.size())
        return nullptr;

    return formats->priv->groups[group].device.data();
}

/**
 * wpe_buffer_dma_buf_formats_get_group_n_formats:Ñ
 * @formats: a #WPEBufferDMABufFormats
 * @group: a group index
 *
 * Get the number of formats in @group in @formats
 *
 * Returns: the number of formats
 */
guint wpe_buffer_dma_buf_formats_get_group_n_formats(WPEBufferDMABufFormats* formats, guint group)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), 0);
    g_return_val_if_fail(group < formats->priv->groups.size(), 0);

    if (group >= formats->priv->groups.size())
        return 0;

    return formats->priv->groups[group].formats.size();
}

/**
 * wpe_buffer_dma_buf_formats_get_format_fourcc:
 * @formats: a #WPEBufferDMABufFormats
 * @group: a group index
 * @format: a format index
 *
 * Get the DRM fourcc of @format in @group in @formats
 *
 * Returns: the DRM fourcc
 */
guint32 wpe_buffer_dma_buf_formats_get_format_fourcc(WPEBufferDMABufFormats* formats, guint group, guint format)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), 0);
    g_return_val_if_fail(group < formats->priv->groups.size(), 0);
    g_return_val_if_fail(format < formats->priv->groups[group].formats.size(), 0);

    if (group >= formats->priv->groups.size() || format >= formats->priv->groups[group].formats.size())
        return 0;

    return formats->priv->groups[group].formats[format].fourcc;
}

/**
 * wpe_buffer_dma_buf_formats_get_format_modifiers:
 * @formats: a #WPEBufferDMABufFormats
 * @group: a group index
 * @format: a format index
 *
 * Get the list of modifiers of @format in @group in @formats
 *
 * Returns: (transfer none) (element-type guint64): a #GArray of #guint64
 */
GArray* wpe_buffer_dma_buf_formats_get_format_modifiers(WPEBufferDMABufFormats* formats, guint group, guint format)
{
    g_return_val_if_fail(WPE_IS_BUFFER_DMA_BUF_FORMATS(formats), nullptr);
    g_return_val_if_fail(group < formats->priv->groups.size(), nullptr);
    g_return_val_if_fail(format < formats->priv->groups[group].formats.size(), nullptr);

    if (group >= formats->priv->groups.size() || format >= formats->priv->groups[group].formats.size())
        return nullptr;

    return formats->priv->groups[group].formats[format].modifiers.get();
}

/**
 * WPEBufferDMABufFormatsBuilder: (copy-func wpe_buffer_dma_buf_formats_builder_ref) (free-func wpe_buffer_dma_buf_formats_builder_unref)
 *
 * Helper type to build a #WPEBufferDMABufFormats
 */
struct _WPEBufferDMABufFormatsBuilder {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;

    explicit _WPEBufferDMABufFormatsBuilder(const char* mainDevice)
        : device(mainDevice)
    {
    }

    CString device;
    Vector<DMABufFormatsGroup> groups;
    int referenceCount { 1 };
};
G_DEFINE_BOXED_TYPE(WPEBufferDMABufFormatsBuilder, wpe_buffer_dma_buf_formats_builder, wpe_buffer_dma_buf_formats_builder_ref, wpe_buffer_dma_buf_formats_builder_unref)

/**
 * wpe_buffer_dma_buf_formats_builder_new:
 * @device: (nullable): the main DRM device
 *
 * Create a new #WPEBufferDMABufFormatsBuilder
 *
 * Returns: (transfer full): a new #WPEBufferDMABufFormatsBuilder
 */
WPEBufferDMABufFormatsBuilder* wpe_buffer_dma_buf_formats_builder_new(const char* device)
{
    auto* builder = static_cast<WPEBufferDMABufFormatsBuilder*>(fastMalloc(sizeof(WPEBufferDMABufFormatsBuilder)));
    new (builder) WPEBufferDMABufFormatsBuilder(device);
    return builder;
}

/**
 * wpe_buffer_dma_buf_formats_builder_ref:
 * @builder: a #WPEBufferDMABufFormatsBuilder
 *
 * Atomically acquires a reference on the given @builder.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: The same @builder with an additional reference.
 */
WPEBufferDMABufFormatsBuilder* wpe_buffer_dma_buf_formats_builder_ref(WPEBufferDMABufFormatsBuilder* builder)
{
    g_return_val_if_fail(builder, nullptr);

    g_atomic_int_inc(&builder->referenceCount);
    return builder;
}

/**
 * wpe_buffer_dma_buf_formats_builder_unref:
 * @builder: a #WPEBufferDMABufFormatsBuilder
 *
 * Atomically releases a reference on the given @builder.
 *
 * If the reference was the last, the resources associated to the
 * @builder are freed. This function is MT-safe and may be called from
 * any thread.
 */
void wpe_buffer_dma_buf_formats_builder_unref(WPEBufferDMABufFormatsBuilder* builder)
{
    g_return_if_fail(builder);

    if (g_atomic_int_dec_and_test(&builder->referenceCount)) {
        builder->~WPEBufferDMABufFormatsBuilder();
        fastFree(builder);
    }
}

/**
 * wpe_buffer_dma_buf_formats_builder_append_group:
 * @builder: a #WPEBufferDMABufFormatsBuilder
 * @device: (nullable): a device
 * @usage: a #WPEBufferDMABufFormatUsage
 *
 * Append a new group for @device and @usage to @builder.
 * If @device is %NULL, the main device passed to wpe_buffer_dma_buf_formats_builder_new()
 * should be used.
 */
void wpe_buffer_dma_buf_formats_builder_append_group(WPEBufferDMABufFormatsBuilder* builder, const char* device, WPEBufferDMABufFormatUsage usage)
{
    g_return_if_fail(builder);

    builder->groups.append(DMABufFormatsGroup(device, usage));
}

/**
 * wpe_buffer_dma_buf_formats_builder_append_format:
 * @builder: a #WPEBufferDMABufFormatsBuilder
 * @fourcc: a DRM fourcc
 * @modifier: a DRM modifier
 *
 * Append a new pair of @format and @modifier to the last group added to @builder
 */
void wpe_buffer_dma_buf_formats_builder_append_format(WPEBufferDMABufFormatsBuilder* builder, guint32 fourcc, guint64 modifier)
{
    g_return_if_fail(builder);

    auto& group = builder->groups.last();
    if (group.formats.isEmpty() || group.formats.last().fourcc != fourcc)
        group.formats.append(DMABufFormat(fourcc));
    g_array_append_val(group.formats.last().modifiers.get(), modifier);
}

/**
 * wpe_buffer_dma_buf_formats_builder_end:
 * @builder: a #WPEBufferDMABufFormatsBuilder
 *
 * End the builder process and return the constructed #WPEBufferDMABufFormats.
 * This function calls wpe_buffer_dma_buf_formats_builder_unref() on @builder.
 *
 * Returns: (transfer full): a new #WPEBufferDMABufFormats.
 */
WPEBufferDMABufFormats* wpe_buffer_dma_buf_formats_builder_end(WPEBufferDMABufFormatsBuilder* builder)
{
    g_return_val_if_fail(builder, nullptr);

    auto* formats = WPE_BUFFER_DMA_BUF_FORMATS(g_object_new(WPE_TYPE_BUFFER_DMA_BUF_FORMATS, nullptr));
    formats->priv->device = WTFMove(builder->device);
    formats->priv->groups = WTFMove(builder->groups);
    wpe_buffer_dma_buf_formats_builder_unref(builder);

    return formats;
}