QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstD3DContextBridge.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_GST_D3D11_GPU_PATH) || defined(QGC_HAS_GST_D3D12_GPU_PATH)
4
5#include <glib-object.h>
6
7#include <QtCore/QLoggingCategory>
8#include <QtCore/QMutexLocker>
9#include <QtCore/QString>
10
11#include "QGCRhiCapture.h"
12
13// GStreamer's D3D11 device notifies from its monitor thread, which it joins when disposed, and its D3D12 device from
14// whichever thread saw the loss, with GStreamer's device list locked: neither may drop the device or wait for a
15// bridge's mutex, which noteDeviceRemoved() leaves to the GUI thread.
16void GstD3DContextBridge::onDeviceRemoved(GObject* /*device*/, GParamSpec* /*property*/, gpointer bridge)
17{
18 static_cast<GstD3DContextBridge*>(bridge)->noteDeviceRemoved();
19}
20
21gint64 GstD3DContextBridge::readAdapterLuid(gpointer device)
22{
23 if (!device || !G_IS_OBJECT(device)) {
24 return 0;
25 }
26 gint64 luid = 0;
27 g_object_get(G_OBJECT(device), "adapter-luid", &luid, nullptr);
28 return luid;
29}
30
31// Reads QGCRhiCapture::deviceSnapshot() via atomic loads — safe from the bus-sync thread without touching QRhi
32// cross-thread.
33bool GstD3DContextBridge::snapshotBackendMatches()
34{
35 const int backend = QGCRhiCapture::deviceSnapshot().backend.load(std::memory_order_acquire);
36 if (backend < 0) {
37 qCDebug(category()) << "QRhi snapshot not yet populated; will retry on next NEED_CONTEXT";
38 return false;
39 }
40 if ((backend != _qrhiBackend) && (backend != _secondQrhiBackend)) {
41 if (!_warnedWrongBackend) {
42 qCInfo(category()) << "QRhi backend tag is" << backend << "(not" << apiName() << "); bridge inactive";
43 _warnedWrongBackend = true;
44 }
45 return false;
46 }
47 return true;
48}
49
50bool GstD3DContextBridge::primeLocked()
51{
52 if (!snapshotBackendMatches()) {
53 return retryLater();
54 }
55 GstObject* device = createDevice();
56 if (!device) {
57 return retryLater();
58 }
59 _device = device;
60 // Looked up by name, so this builds against releases older than 1.26 too.
61 if (g_object_class_find_property(G_OBJECT_GET_CLASS(device), "device-removed-reason")) {
62 _removalHandler = g_signal_connect(device, "notify::device-removed-reason", G_CALLBACK(&onDeviceRemoved), this);
63 }
64 qCInfo(category()) << apiName() << "bridge primed: shared device =" << device;
65 return true;
66}
67
68GstObject* GstD3DContextBridge::refObject(const char* /*contextType*/)
69{
70 return _device ? GST_OBJECT(gst_object_ref(_device)) : nullptr;
71}
72
73GstContext* GstD3DContextBridge::buildContext(const char* /*contextType*/, GstObject* object)
74{
75 GstContext* ctx = makeDeviceContext(object);
76 if (!ctx) {
77 qCWarning(category()) << apiName() << "context_new failed";
78 }
79 return ctx;
80}
81
82void GstD3DContextBridge::clearLocked()
83{
84 if (_removalHandler != 0) {
85 g_signal_handler_disconnect(_device, _removalHandler);
86 _removalHandler = 0;
87 }
88 gst_clear_object(&_device);
89 _warnedWrongBackend = false;
90}
91
92GstObject* GstD3DContextBridge::refDevice()
93{
94 QMutexLocker lock(&_mutex);
95 return _device ? GST_OBJECT(gst_object_ref(_device)) : nullptr;
96}
97
98void GstD3DContextBridge::logAdapterMatch(gint64 expectedLuid, gpointer gstDevice) const
99{
100 const gint64 actualLuid = readAdapterLuid(gstDevice);
101 if (actualLuid != expectedLuid) {
102 qCWarning(category()).noquote() << apiName() << "bridge: gst device LUID mismatch — QRhi LUID=" << expectedLuid
103 << "but wrapped device LUID=" << actualLuid
104 << "(zero-copy will appear corrupt; check NEED_CONTEXT race)";
105 return;
106 }
107 qCInfo(category()).noquote() << apiName() << "bridge adapter LUID="
108 << QString::asprintf("0x%llx", static_cast<long long>(expectedLuid));
109}
110
111#endif // QGC_HAS_GST_D3D11_GPU_PATH || QGC_HAS_GST_D3D12_GPU_PATH
DeviceSnapshot & deviceSnapshot() noexcept
Returns the global snapshot. Atomic fields make individual reads thread-safe.
QCameraDevice device(const QString &description)
The attached camera of description description; a null device when none is.
Definition UVCDevices.cc:21
std::atomic< int > backend
QRhi::Implementation cast to int (-1 = unset).