Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdmabufsource.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qdmabufallocator_p.h>
7#include <QtMultimedia/private/qmultimedia_dmabuf_support_p.h>
8#include <QtMultimedia/private/qmultimedia_drm_support_p.h>
9#include <QtMultimedia/private/qvideotexturehelper_p.h>
10
11#include <sys/mman.h>
12
13#if QT_CONFIG(linux_dmabuf)
14
15QT_BEGIN_NAMESPACE
16
17namespace QtMultimediaPrivate {
18
19namespace {
20
21bool dmaBufIsSemiPlanar(QVideoFrameFormat::PixelFormat format)
22{
23 switch (format) {
24 case QVideoFrameFormat::Format_NV12:
25 case QVideoFrameFormat::Format_NV21:
26 case QVideoFrameFormat::Format_P010:
27 case QVideoFrameFormat::Format_P016:
28 return true;
29 default:
30 return false;
31 }
32}
33
34} // namespace
35
36std::optional<DmaBufLayout> dmaBufLayoutForFormat(QVideoFrameFormat::PixelFormat format,
37 QSize frameSize, int bytesPerLine,
38 quint32 sizeImage)
39{
40 // Compressed frames have no pitch or plane layout, even though they map to a DRM fourcc.
41 if (format == QVideoFrameFormat::Format_Jpeg)
42 return std::nullopt;
43
44 const auto *desc = QVideoTextureHelper::textureDescription(format);
45 const auto drmFormats = dmaBufFourccFromPixelFormat(format);
46 if (!desc || drmFormats.empty() || bytesPerLine <= 0)
47 return std::nullopt;
48
49 const bool supported = drmFormats.size() == 1 || dmaBufIsSemiPlanar(format);
50 if (!supported)
51 return std::nullopt;
52
53 DmaBufLayout layout;
54 qsizetype offset = 0;
55 for (qsizetype plane = 0; plane < drmFormats.size(); ++plane) {
56 const qsizetype size =
57 qsizetype(bytesPerLine) * desc->heightForPlane(frameSize.height(), int(plane));
58 layout.push_back(DmaBufPlaneLayout{
59 uint32_t(offset),
60 uint32_t(bytesPerLine),
61 size,
62 });
63 offset += size;
64 }
65
66 if (offset > qsizetype(sizeImage))
67 return std::nullopt;
68
69 return layout;
70}
71
72DmaBufSource::DmaBufSource() = default;
73
74DmaBufSource::DmaBufSource(std::shared_ptr<const QDmaBuf> buffer, DmaBufLayout layout)
75 : m_buffer(std::move(buffer)), m_layout(std::move(layout))
76{
77 Q_ASSERT(m_buffer);
78 Q_ASSERT(!m_layout.isEmpty() && m_layout[0].offset == 0);
79 Q_ASSERT(qsizetype(m_layout.back().offset) + m_layout.back().size <= m_buffer->size());
80}
81
82std::optional<QAbstractVideoBuffer::MapData> DmaBufSource::mapReadOnly() const
83{
84 Q_ASSERT(m_buffer);
85
86 void *mapped =
87 ::mmap(nullptr, size_t(m_buffer->size()), PROT_READ, MAP_SHARED, m_buffer->fd(), 0);
88 if (mapped == MAP_FAILED)
89 return std::nullopt;
90
91 dmaBufSyncRead(m_buffer->fd(), DmaBufCpuAccess::Begin);
92
93 QAbstractVideoBuffer::MapData mapData;
94 mapData.planeCount = int(m_layout.size());
95 for (qsizetype plane = 0; plane < m_layout.size(); ++plane) {
96 const DmaBufPlaneLayout &p = m_layout[plane];
97 mapData.data[plane] = static_cast<uchar *>(mapped) + p.offset;
98 mapData.bytesPerLine[plane] = int(p.pitch);
99 mapData.dataSize[plane] = int(p.size);
100 }
101 return mapData;
102}
103
104void DmaBufSource::unmap(QAbstractVideoBuffer::MapData &mapData) const
105{
106 Q_ASSERT(m_buffer);
107
108 dmaBufSyncRead(m_buffer->fd(), DmaBufCpuAccess::End);
109 ::munmap(mapData.data[0], size_t(m_buffer->size()));
110}
111
112} // namespace QtMultimediaPrivate
113
114QT_END_NAMESPACE
115
116#endif // QT_CONFIG(linux_dmabuf)