QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstDmaDrmCaps.cc
Go to the documentation of this file.
1#include "GstDmaDrmCaps.h"
2
3#if defined(QGC_HAS_GST_DMABUF_GPU_PATH) && GST_CHECK_VERSION(1, 24, 0)
4#include <algorithm>
5#include <drm_fourcc.h>
6#include <gst/video/video-info-dma.h>
7#include <gst/video/video.h>
8#include <map>
9#include <vector>
10
12#include "GstEglHelpers.h"
13#include "GstScoped.h"
14#include "QGCLoggingCategory.h"
15#include <EGL/egl.h>
16#include <EGL/eglext.h>
17
18QGC_LOGGING_CATEGORY(GstDmaDrmCapsLog, "Video.GStreamer.HwBuffers.GstDmaDrmCaps")
19
20namespace GstHw {
21
22namespace {
23
24constexpr const char* kModifiersExt = "EGL_EXT_image_dma_buf_import_modifiers";
25
26// Qt's importer binds one EGLImage per format plane, so compressed layouts and those with auxiliary planes (CCS, DCC,
27// AFBC, UBWC) can't be imported. Plain vendor tilings can: the modifier goes with each plane.
28bool modifierImportable(guint64 modifier) noexcept
29{
30 const guint64 code = modifier & 0x00ffffffffffffffULL;
31 switch (modifier >> 56) {
32 case DRM_FORMAT_MOD_VENDOR_INTEL:
33 // X, Y, Yf and 4 tiling; every other Intel modifier is a CCS variant.
34 return (code == 1) || (code == 2) || (code == 3) || (code == 9);
35 case DRM_FORMAT_MOD_VENDOR_AMD:
36 return ((code >> 13) & 0x1) == 0; // AMD_FMT_MOD DCC bit
37 case DRM_FORMAT_MOD_VENDOR_ARM:
38 return ((modifier >> 52) & 0xf) == 0x01; // DRM_FORMAT_MOD_ARM_TYPE_MISC: not AFBC/AFRC compression
39 case DRM_FORMAT_MOD_VENDOR_QCOM:
40 return code != 1; // DRM_FORMAT_MOD_QCOM_COMPRESSED (UBWC)
41 default:
42 return modifier != DRM_FORMAT_MOD_INVALID;
43 }
44}
45
46// What GstQtDmaBufImport::planeFourccs() picks for the Qt counterpart of @p format: one R8/GR88 (8-bit) or
47// R16/GR1616 (16-bit) image per plane of a multi-plane format, the format's own fourcc for a single-plane one.
48guint32 planeImportFourcc(GstVideoFormat format, guint plane) noexcept
49{
50 const GstVideoFormatInfo* finfo = gst_video_format_get_info(format);
51 if (!finfo || (plane >= GST_VIDEO_FORMAT_INFO_N_PLANES(finfo))) {
52 return 0;
53 }
54 if (GST_VIDEO_FORMAT_INFO_N_PLANES(finfo) == 1) {
55 return gst_video_dma_drm_fourcc_from_format(format);
56 }
57 gint components[GST_VIDEO_MAX_COMPONENTS];
58 gst_video_format_info_component(finfo, plane, components);
59 const int count = static_cast<int>(std::ranges::count_if(components, [](gint c) { return c >= 0; }));
60 const int pixelStride = (components[0] >= 0) ? GST_VIDEO_FORMAT_INFO_PSTRIDE(finfo, components[0]) : 0;
61 if (count == 1) {
62 return (pixelStride == 1) ? DRM_FORMAT_R8 : (pixelStride == 2) ? DRM_FORMAT_R16 : 0;
63 }
64 if (count == 2) {
65 return (pixelStride == 2) ? DRM_FORMAT_GR88 : (pixelStride == 4) ? DRM_FORMAT_GR1616 : 0;
66 }
67 return 0;
68}
69
72bool appendDrmFormat(std::string& entries, guint32 fourcc, guint64 modifier)
73{
74 // drm-format wants the DRM fourcc 4CC string, not the GStreamer format name; to_string emits a bare fourcc for
75 // LINEAR and "FOURCC:0xMOD" otherwise ("FOURCC:0x0" fails to parse in gst 1.24).
76 const GStreamer::GCharPtr drmFormat = GStreamer::adoptGChar(gst_video_dma_drm_fourcc_to_string(fourcc, modifier));
77 if (!drmFormat) {
78 return false;
79 }
80 if (!entries.empty()) {
81 entries += ", ";
82 }
83 entries += drmFormat.get();
84 return true;
85}
86
88class ModifierQuery
89{
90public:
91 explicit ModifierQuery(EGLDisplay display)
92 : _display(display)
93 , _query(reinterpret_cast<PFNEGLQUERYDMABUFMODIFIERSEXTPROC>(eglGetProcAddress("eglQueryDmaBufModifiersEXT")))
94 {}
95
96 bool valid() const noexcept { return _query != nullptr; }
97
99 std::vector<EGLuint64KHR> all(EGLint fourcc) const { return query(fourcc, false); }
100
102 const std::vector<EGLuint64KHR>& sampleable(guint32 fourcc)
103 {
104 const auto [it, inserted] = _sampleable.try_emplace(fourcc);
105 if (inserted) {
106 it->second = query(static_cast<EGLint>(fourcc), true);
107 }
108 return it->second;
109 }
110
111private:
112 std::vector<EGLuint64KHR> query(EGLint fourcc, bool texture2DOnly) const
113 {
114 EGLint count = 0;
115 if ((_query(_display, fourcc, 0, nullptr, nullptr, &count) != EGL_TRUE) || (count <= 0)) {
116 return {};
117 }
118 std::vector<EGLuint64KHR> mods(static_cast<std::size_t>(count));
119 std::vector<EGLBoolean> externalOnly(static_cast<std::size_t>(count));
120 if ((_query(_display, fourcc, count, mods.data(), externalOnly.data(), &count) != EGL_TRUE) || (count <= 0)) {
121 return {};
122 }
123 mods.resize(static_cast<std::size_t>(count));
124 if (!texture2DOnly) {
125 return mods;
126 }
127 std::vector<EGLuint64KHR> texture2D;
128 for (std::size_t i = 0; i < mods.size(); ++i) {
129 if (!externalOnly[i]) {
130 texture2D.push_back(mods[i]);
131 }
132 }
133 return texture2D;
134 }
135
136 EGLDisplay _display;
137 PFNEGLQUERYDMABUFMODIFIERSEXTPROC _query;
138 std::map<guint32, std::vector<EGLuint64KHR>> _sampleable;
139};
140
141} // namespace
142
143std::string buildSupportedDmaDrmCaps(std::span<const GstVideoFormat> formats, void* display)
144{
145 if (formats.empty()) {
146 return std::string();
147 }
148
149 const auto dpy = static_cast<EGLDisplay>(display);
150 if ((dpy == EGL_NO_DISPLAY) || !GstEglHelpers::displaySupportsExtension(dpy, kModifiersExt)) {
151 qCDebug(GstDmaDrmCapsLog) << "DMA_DRM modifier query unavailable (no DMABuf import display, or no "
152 "EGL_EXT_image_dma_buf_import_modifiers); offering no DMA_DRM caps";
153 return std::string();
154 }
155
156 const auto queryFormats =
157 reinterpret_cast<PFNEGLQUERYDMABUFFORMATSEXTPROC>(eglGetProcAddress("eglQueryDmaBufFormatsEXT"));
158 ModifierQuery modifiers(dpy);
159 if (!queryFormats || !modifiers.valid()) {
160 return std::string();
161 }
162
163 EGLint numFormats = 0;
164 if ((queryFormats(dpy, 0, nullptr, &numFormats) != EGL_TRUE) || (numFormats <= 0)) {
165 return std::string();
166 }
167 std::vector<EGLint> fourccs(static_cast<std::size_t>(numFormats));
168 if ((queryFormats(dpy, numFormats, fourccs.data(), &numFormats) != EGL_TRUE) || (numFormats <= 0)) {
169 return std::string();
170 }
171 fourccs.resize(static_cast<std::size_t>(numFormats));
172
173 std::string entries;
174 int offeredCount = 0;
175 int excludedCount = 0;
176 for (const EGLint fourcc : fourccs) {
177 // GStreamer's own fourcc mapping, the one it reads drm-format caps with.
178 const GstVideoFormat format = gst_video_dma_drm_fourcc_to_format(static_cast<guint32>(fourcc));
179 if (std::ranges::find(formats, format) == formats.end()) {
180 continue;
181 }
182 const guint planes = GST_VIDEO_FORMAT_INFO_N_PLANES(gst_video_format_get_info(format));
183 for (const EGLuint64KHR mod : modifiers.all(fourcc)) {
184 bool importable = modifierImportable(mod);
185 for (guint plane = 0; importable && (plane < planes); ++plane) {
186 const guint32 planeFourcc = planeImportFourcc(format, plane);
187 importable = (planeFourcc != 0) && std::ranges::any_of(modifiers.sampleable(planeFourcc),
188 [mod](EGLuint64KHR m) { return m == mod; });
189 }
190 if (importable && appendDrmFormat(entries, static_cast<guint32>(fourcc), mod)) {
191 ++offeredCount;
192 } else {
193 ++excludedCount;
194 }
195 }
196 }
197
198 if (entries.empty()) {
199 return std::string();
200 }
201 qCDebug(GstDmaDrmCapsLog) << "EGL-derived DMA_DRM caps:" << offeredCount << "format/modifier pairs offered,"
202 << excludedCount << "not importable per plane";
203 return "video/x-raw(memory:DMABuf), format=DMA_DRM, drm-format={ " + entries + " }; ";
204}
205
206std::string buildVulkanDmaDrmCaps([[maybe_unused]] std::span<const GstVideoFormat> formats)
207{
208 std::string entries;
209#if defined(QGC_HAS_GST_DMABUF_VULKAN_IMPORT)
210 for (const GstVideoFormat format : formats) {
211 const guint32 fourcc = gst_video_dma_drm_fourcc_from_format(format);
212 const guint planes = GST_VIDEO_FORMAT_INFO_N_PLANES(gst_video_format_get_info(format));
213 if ((fourcc == DRM_FORMAT_INVALID) || (planes == 0)) {
214 continue;
215 }
216 // The modifiers the first plane imports with, kept when every other plane imports with them too.
217 for (const quint64 modifier : GstDmaBufVulkan::importableModifiers(planeImportFourcc(format, 0))) {
218 bool importable = modifierImportable(modifier);
219 for (guint plane = 1; importable && (plane < planes); ++plane) {
220 const std::vector<quint64> planeModifiers =
221 GstDmaBufVulkan::importableModifiers(planeImportFourcc(format, plane));
222 importable = std::ranges::find(planeModifiers, modifier) != planeModifiers.end();
223 }
224 if (importable) {
225 appendDrmFormat(entries, fourcc, modifier);
226 }
227 }
228 }
229#endif
230 if (entries.empty()) {
231 return std::string();
232 }
233 return "video/x-raw(memory:DMABuf), format=DMA_DRM, drm-format={ " + entries + " }; ";
234}
235
236#ifdef QGC_UNITTEST_BUILD
237bool dmaDrmModifierImportableForTest(guint64 modifier) noexcept
238{
239 return modifierImportable(modifier);
240}
241
242guint32 dmaDrmPlaneImportFourccForTest(GstVideoFormat format, guint plane) noexcept
243{
244 return planeImportFourcc(format, plane);
245}
246#endif
247
248} // namespace GstHw
249
250#endif // QGC_HAS_GST_DMABUF_GPU_PATH && GST_CHECK_VERSION(1, 24, 0)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
GCharPtr adoptGChar(gchar *str) noexcept
Definition GstScoped.h:134
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
Definition GstScoped.h:71
Defined only in builds with the DMABuf path (QGC_HAS_GST_DMABUF_GPU_PATH) on GStreamer 1....
std::string buildSupportedDmaDrmCaps(std::span< const GstVideoFormat > formats, void *display)
std::string buildVulkanDmaDrmCaps(std::span< const GstVideoFormat > formats)
QByteArray format(const QList< LogEntry > &entries, int fmt)