QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstQtDmaBufImport.cc
Go to the documentation of this file.
1#include "GstQtDmaBufImport.h"
2
3#if defined(QGC_HAS_GST_DMABUF_GPU_PATH)
4
5// Qt 6.12 private API (exported as Qt_6_PRIVATE_API): DmaBufEglContext, importDmaBufTextures,
6// dmaBufFourccFromPixelFormat and createTexturesFromHandles. Re-check every call here when raising the Qt minimum.
7#if QT_VERSION < QT_VERSION_CHECK(6, 12, 0)
8#error "GstQtDmaBufImport needs the DMABuf importer Qt Multimedia exports from 6.12"
9#endif
10
11#include <algorithm>
12
13#include <EGL/eglext.h>
14#include <GLES2/gl2.h>
15#include <GLES2/gl2ext.h>
16// Not <drm_fourcc.h>: its fourcc_code() macro breaks Qt's constexpr function of that name.
17#include <private/qdmabuftextureimporter_p.h>
18#include <private/qmultimedia_drm_support_p.h>
19#include <private/qvideotexturehelper_p.h>
20#include <rhi/qrhi.h>
21
22static_assert(GstQtDmaBufImport::kImplicitModifier == QtMultimediaPrivate::DmaBufFormatModifierInvalid);
23
24namespace GstQtDmaBufImport {
25
26PlaneFourccs planeFourccs(QVideoFrameFormat::PixelFormat pixelFormat)
27{
28 PlaneFourccs out;
29 switch (pixelFormat) {
30 // Qt defect, seen in Qt 6.12.0: dmaBufFourccFromPixelFormat() gives these ABGR8888
31 // (qtmultimedia/src/multimedia/video/qmultimedia_drm_support_p.h), which samples B as red: the bytes are
32 // B,G,R,A, i.e. DRM ARGB8888 (what Qt's GStreamer backend uses for BGRA, qgstvideobuffer.cpp). Once the minimum
33 // Qt has the fix, drop these cases: Qt's table then serves every format.
34 case QVideoFrameFormat::Format_BGRA8888:
35 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
36 case QVideoFrameFormat::Format_BGRX8888:
37#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
38 out.fourcc[0] = static_cast<quint32>(QtMultimediaPrivate::DRMFormat::ARGB8888);
39#else
40 out.fourcc[0] = static_cast<quint32>(QtMultimediaPrivate::DRMFormat::BGRA8888);
41#endif
42 out.count = 1;
43 return out;
44 default:
45 break;
46 }
47 const auto formats = QtMultimediaPrivate::dmaBufFourccFromPixelFormat(pixelFormat);
48 out.count = static_cast<int>((std::min) (formats.size(), qsizetype(out.fourcc.size())));
49 for (int i = 0; i < out.count; ++i) {
50 out.fourcc[std::size_t(i)] = static_cast<quint32>(formats[i]);
51 }
52 return out;
53}
54
55EGLDisplay importDisplay(QRhi& rhi)
56{
57 if (rhi.backend() != QRhi::OpenGLES2) {
58 return EGL_NO_DISPLAY;
59 }
60 rhi.makeThreadLocalNativeContextCurrent();
61 const QtMultimediaPrivate::DmaBufEglContext context(&rhi);
62 return context.isValid() ? context.eglDisplay() : EGL_NO_DISPLAY;
63}
64
65ImportResult importTextures(QRhi& rhi, std::span<const Plane> planes, QVideoFrameFormat::PixelFormat pixelFormat,
66 QSize frameSize, std::shared_ptr<void> keepAlive)
67{
68 ImportResult result;
69 // importDmaBufTextures() asserts one plane per texture of the format.
70 const auto* desc = QVideoTextureHelper::textureDescription(pixelFormat);
71 if (!desc || planes.empty() || (std::ssize(planes) != desc->nplanes) || (rhi.backend() != QRhi::OpenGLES2)) {
72 return result;
73 }
74 const QtMultimediaPrivate::DmaBufEglContext context(&rhi);
75 if (!context.isValid()) {
76 return result;
77 }
78
79 std::array<QtMultimediaPrivate::DmaBufPlane, 4> qtPlanes{};
80 for (std::size_t i = 0; i < planes.size(); ++i) {
81 qtPlanes[i] = {.fd = planes[i].fd,
82 .offset = planes[i].offset,
83 .pitch = planes[i].pitch,
84 .drmFormat = static_cast<QtMultimediaPrivate::DRMFormat>(planes[i].drmFourcc),
85 .modifier = planes[i].modifier};
86 }
87 auto handles = QtMultimediaPrivate::importDmaBufTextures(rhi, context, QSpan(qtPlanes.data(), planes.size()),
88 pixelFormat, frameSize, std::move(keepAlive));
89 if (!handles) {
90 result.unrecoverable = (handles.error() == QtMultimediaPrivate::FailureSeverity::unrecoverable);
91 return result;
92 }
93 result.textures = QVideoTextureHelper::createTexturesFromHandles(std::move(*handles), rhi, pixelFormat, frameSize);
94 return result;
95}
96
97} // namespace GstQtDmaBufImport
98
99#endif // QGC_HAS_GST_DMABUF_GPU_PATH