QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwImportPreflight.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_ANY_GPU_PATH)
4
5#include <array>
6
7#include <QtCore/QMutex>
8
9#include <private/qvideotexturehelper_p.h>
10
11#include "GstHwPathTelemetry.h"
12
13namespace {
14
17struct PreflightMemo {
18 QRhi* rhi = nullptr;
19 QVideoFrameFormat::PixelFormat pixelFormat = QVideoFrameFormat::Format_Invalid;
20 QSize size;
21 QRhiTexture::Flags flags;
22 bool result = false;
23 bool valid = false;
24};
25
26constexpr std::size_t kPathCount = static_cast<std::size_t>(HwVideoBufferPath::Vulkan) + 1;
27QMutex s_memoMutex;
28std::array<PreflightMemo, kPathCount> s_memo{};
29
30} // namespace
31
32namespace GstHwImportPreflight {
33
34bool canImportTexture(QRhi* rhi, QRhiTexture::Format fmt, const QSize& size, QRhiTexture::Flags flags) noexcept
35{
36 if (!rhi) {
37 return true;
38 }
39 if (fmt == QRhiTexture::UnknownFormat) {
40 return false;
41 }
42 if (!rhi->isTextureFormatSupported(fmt, flags)) {
43 return false;
44 }
45 const int maxDim = rhi->resourceLimit(QRhi::TextureSizeMax);
46 if (maxDim > 0 && (size.width() > maxDim || size.height() > maxDim)) {
47 return false;
48 }
49 return true;
50}
51
52bool canImportPlanes(QRhi* rhi, QVideoFrameFormat::PixelFormat pixelFormat, const QSize& size,
53 QRhiTexture::Flags flags) noexcept
54{
55 if (!rhi) {
56 return true;
57 }
58 const auto* desc = QVideoTextureHelper::textureDescription(pixelFormat);
59 if (!desc) {
60 return false;
61 }
62 // Disable the helper's own format fallback chain so the pre-flight tests the exact format createFrom() will use.
63 using FallbackPolicy = QVideoTextureHelper::TextureDescription::FallbackPolicy;
64 for (int plane = 0; plane < desc->nplanes; ++plane) {
65 const QRhiTexture::Format fmt = desc->rhiTextureFormat(plane, rhi, FallbackPolicy::Disable);
66 const QSize planeSize = desc->rhiPlaneSize(size, plane, rhi);
67 if (!canImportTexture(rhi, fmt, planeSize, flags)) {
68 return false;
69 }
70 }
71 return true;
72}
73
74bool preflightOrRecord(QRhi* rhi, HwVideoBufferPath path, QVideoFrameFormat::PixelFormat pixelFormat,
75 const QSize& size, QRhiTexture::Flags flags) noexcept
76{
77 bool ok = false;
78 const std::size_t idx = static_cast<std::size_t>(path);
79 if (idx < s_memo.size()) {
80 QMutexLocker lock(&s_memoMutex);
81 PreflightMemo& memo = s_memo[idx];
82 if (memo.valid && (memo.rhi == rhi) && (memo.pixelFormat == pixelFormat) && (memo.size == size) &&
83 (memo.flags == flags)) {
84 ok = memo.result;
85 } else {
86 ok = canImportPlanes(rhi, pixelFormat, size, flags);
87 memo = {rhi, pixelFormat, size, flags, ok, true};
88 }
89 } else {
90 ok = canImportPlanes(rhi, pixelFormat, size, flags);
91 }
92 if (ok) {
93 return true;
94 }
95 // Recorded per call (not per epoch) to keep fallback counts frame-accurate.
97 return false;
98}
99
100} // namespace GstHwImportPreflight
101
102#endif // QGC_HAS_ANY_GPU_PATH
HwVideoBufferPath
Identifies which GPU path was chosen; used by the adapter to increment the right counter.
void recordFallbackReason(HwVideoBufferPath attemptedPath, HwFallbackReason reason) noexcept
Per-(path,reason) fallback accounting; lets a bug report show why a path demoted to CPU.