QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstD3D11ContextBridge.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
4
5#include <d3d11.h>
6#include <gst/d3d11/gstd3d11.h>
7#include <rhi/qrhi.h> // QRhi::Implementation enum only; no runtime use across threads
8
10#include "QGCLoggingCategory.h"
11#include "QGCRhiCapture.h"
12
13QGC_LOGGING_CATEGORY(GstD3D11BridgeLog, "Video.GStreamer.HwBuffers.GstD3D11Bridge")
14
15namespace {
16
17constexpr const char* kContextTypes[] = {GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE};
18
19class D3D11ContextBridge final : public GstD3DContextBridge
20{
21public:
22 constexpr D3D11ContextBridge() noexcept
23 : GstD3DContextBridge("D3D11", kContextTypes, &GstD3D11BridgeLog, int(QRhi::D3D11))
24 {}
25
26private:
27 GstObject* createDevice() override;
28 GstContext* makeDeviceContext(GstObject* device) override;
29};
30
31constinit D3D11ContextBridge s_bridge;
32
33GstObject* D3D11ContextBridge::createDevice()
34{
35 // Snapshot populated on the render thread (sceneGraphInitialized); only atomic loads here. The pointer holds no
36 // reference until gst_d3d11_device_new_wrapped() takes one, and needs none: whatever withdraws the snapshot
37 // (QGCRhiCapture's unpublishRhi()) then resets this bridge, under the mutex held here, before Qt destroys the
38 // device.
39 auto* dev = static_cast<ID3D11Device*>(QGCRhiCapture::deviceSnapshot().d3d11Device.load(std::memory_order_acquire));
40 if (!dev) {
41 qCWarning(GstD3D11BridgeLog) << "QRhi D3D11 snapshot missing ID3D11Device*";
42 return nullptr;
43 }
44
45 // gst_d3d11_device_new_wrapped: shared device keeps textures QRhi-sampleable without keyed-mutex transfer. Its
46 // elements share QRhi's immediate context, which QGCRhiCapture made multithread-protected before publishing it.
47 GstD3D11Device* device = gst_d3d11_device_new_wrapped(dev);
48 if (!device) {
49 qCWarning(GstD3D11BridgeLog) << "gst_d3d11_device_new_wrapped failed";
50 return nullptr;
51 }
52
53 const gint64 expectedLuid = QGCRhiCapture::deviceSnapshot().adapterLuid.load(std::memory_order_acquire);
54 logAdapterMatch(expectedLuid, device);
55 return GST_OBJECT(device);
56}
57
58GstContext* D3D11ContextBridge::makeDeviceContext(GstObject* device)
59{
60 // gst_d3d11_context_new gst_object_ref's the device internally; caller retains ownership.
61 return gst_d3d11_context_new(GST_D3D11_DEVICE_CAST(device));
62}
63
64} // namespace
65
66GstContextBridgeBase& GstD3D11ContextBridge::instance()
67{
68 return s_bridge;
69}
70
71GstD3D11Device* GstD3D11ContextBridge::currentDevice()
72{
73 return GST_D3D11_DEVICE_CAST(s_bridge.refDevice());
74}
75
76#endif // QGC_HAS_GST_D3D11_GPU_PATH
#define QGC_LOGGING_CATEGORY(name, categoryStr)
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< void * > d3d11Device
ID3D11Device* (Windows only).
std::atomic< qint64 > adapterLuid