3#include <QtGui/qtguiglobal.h>
5#if defined(QGC_HAS_GST_VULKAN_GPU_PATH) && QT_CONFIG(vulkan)
11#include <vulkan/vulkan.h>
13#include <QtCore/QLoggingCategory>
16#pragma push_macro("slots")
22#include <gst/vulkan/gstvkdevice.h>
23#include <gst/vulkan/gstvkimagememory.h>
24#include <gst/vulkan/gstvkinstance.h>
25#include <gst/vulkan/gstvkphysicaldevice.h>
26#include <gst/vulkan/gstvkqueue.h>
28#pragma pop_macro("slots")
43 std::array<quint64, 2> pipelineCacheUuid{};
45 friend bool operator==(
const DeviceIdentity&,
const DeviceIdentity&) =
default;
48DeviceIdentity identityOf(
const VkPhysicalDeviceProperties&
properties)
51 static_assert(
sizeof(identity.pipelineCacheUuid) ==
sizeof(
properties.pipelineCacheUUID));
52 std::memcpy(identity.pipelineCacheUuid.data(),
properties.pipelineCacheUUID,
sizeof(identity.pipelineCacheUuid));
58bool readQtDevice(DeviceIdentity& out)
62 if (snap.backend.load(std::memory_order_acquire) !=
static_cast<int>(QRhi::Vulkan)) {
65 out.vendorId = snap.vkVendorId.load(std::memory_order_acquire);
66 out.deviceId = snap.vkDeviceId.load(std::memory_order_acquire);
67 for (std::size_t i = 0; i < out.pipelineCacheUuid.size(); ++i) {
68 out.pipelineCacheUuid[i] = snap.vkPipelineCacheUuid[i].load(std::memory_order_acquire);
73using PhysicalDevicePtr = std::unique_ptr<GstVulkanPhysicalDevice, GStreamer::GstObjectDeleter>;
76PhysicalDevicePtr findPhysicalDevice(GstVulkanInstance* instance,
const DeviceIdentity& want)
78 const guint n = instance->n_physical_devices;
79 for (guint i = 0; i < n; ++i) {
80 PhysicalDevicePtr phys{gst_vulkan_physical_device_new(instance, i)};
81 if (phys && (identityOf(phys->properties) == want)) {
90gboolean takeGraphicsQueue(GstVulkanDevice* device, GstVulkanQueue* queue, gpointer userData)
92 const GstVulkanPhysicalDevice*
const phys =
device->physical_device;
93 if ((queue->family >= phys->n_queue_families) ||
94 !(phys->queue_family_props[queue->family].queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
97 *
static_cast<GstVulkanQueue**
>(userData) = GST_VULKAN_QUEUE(gst_object_ref(queue));
103GstVulkanDevice* provideBridgeDevice(GstVulkanInstance* , guint , gpointer bridgeDevice)
105 return GST_VULKAN_DEVICE(gst_object_ref(bridgeDevice));
108constexpr const char* kContextTypes[] = {
109 GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR,
110 GST_VULKAN_DEVICE_CONTEXT_TYPE_STR,
111 GST_VULKAN_QUEUE_CONTEXT_TYPE_STR,
114class VulkanContextBridge final :
public GstContextBridgeBase
117 constexpr VulkanContextBridge() noexcept
118 : GstContextBridgeBase(
"Vulkan", kContextTypes, &GstVulkanBridgeLog)
122 bool primeLocked()
override;
123 GstObject* refObject(
const char* contextType)
override;
124 GstContext* buildContext(
const char* contextType, GstObject*
object)
override;
125 void clearLocked()
override;
127 GstVulkanInstance* _instance =
nullptr;
128 GstVulkanDevice* _device =
nullptr;
129 GstVulkanQueue* _queue =
nullptr;
131 gulong _createDeviceHandler = 0;
134constinit VulkanContextBridge s_bridge;
136bool VulkanContextBridge::primeLocked()
138 DeviceIdentity qtDevice;
139 if (!readQtDevice(qtDevice)) {
141 if (!retryWithinBudget() && retriesJustExhausted()) {
142 qCInfo(GstVulkanBridgeLog) <<
"active RHI is not Vulkan after" << kMaxPrimeRetries
143 <<
"retries; Vulkan bridge inactive";
151 auto disable = [
this](
const char* what) ->
bool {
152 qCWarning(GstVulkanBridgeLog) <<
"Vulkan bridge disabled:" << what;
157 _instance = gst_vulkan_instance_new();
158 if (!_instance || !gst_vulkan_instance_open(_instance,
nullptr)) {
159 return disable(
"gst_vulkan_instance_open failed");
162 const PhysicalDevicePtr phys = findPhysicalDevice(_instance, qtDevice);
164 return disable(
"GStreamer's Vulkan instance lists no physical device with the properties of Qt's");
166 _device = gst_vulkan_device_new(phys.get());
167 if (!_device || !gst_vulkan_device_open(_device,
nullptr)) {
168 return disable(
"gst_vulkan_device_open failed");
170 gst_vulkan_device_foreach_queue(_device, &takeGraphicsQueue, &_queue);
172 return disable(
"GStreamer's Vulkan device has no graphics queue");
179 if (g_signal_lookup(
"create-device", GST_TYPE_VULKAN_INSTANCE) != 0) {
180 _createDeviceHandler = g_signal_connect_data(
181 _instance,
"create-device", G_CALLBACK(&provideBridgeDevice), gst_object_ref(_device),
182 +[](gpointer device, GClosure*) { gst_object_unref(device); }, GConnectFlags(0));
185 qCInfo(GstVulkanBridgeLog) <<
"Vulkan bridge primed on Qt's physical device" << phys->properties.deviceName
186 <<
"with a VkDevice of GStreamer's own: gst-vulkan can't wrap Qt's, so QRhi can't "
187 "sample VulkanImage frames";
191GstObject* VulkanContextBridge::refObject(
const char* contextType)
193 GstObject*
object = GST_OBJECT(_queue);
194 if (g_strcmp0(contextType, GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR) == 0) {
195 object = GST_OBJECT(_instance);
196 }
else if (g_strcmp0(contextType, GST_VULKAN_DEVICE_CONTEXT_TYPE_STR) == 0) {
197 object = GST_OBJECT(_device);
199 return object ? GST_OBJECT(gst_object_ref(
object)) : nullptr;
202GstContext* VulkanContextBridge::buildContext(
const char* contextType, GstObject*
object)
204 if (g_strcmp0(contextType, GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR) == 0) {
205 GstContext* ctx = gst_context_new(GST_VULKAN_INSTANCE_CONTEXT_TYPE_STR, TRUE);
206 gst_context_set_vulkan_instance(ctx, GST_VULKAN_INSTANCE(
object));
209 if (g_strcmp0(contextType, GST_VULKAN_DEVICE_CONTEXT_TYPE_STR) == 0) {
210 GstContext* ctx = gst_context_new(GST_VULKAN_DEVICE_CONTEXT_TYPE_STR, TRUE);
211 gst_context_set_vulkan_device(ctx, GST_VULKAN_DEVICE(
object));
214 GstContext* ctx = gst_context_new(GST_VULKAN_QUEUE_CONTEXT_TYPE_STR, TRUE);
215 gst_context_set_vulkan_queue(ctx, GST_VULKAN_QUEUE(
object));
219void VulkanContextBridge::clearLocked()
221 if (_createDeviceHandler != 0) {
222 g_signal_handler_disconnect(_instance, _createDeviceHandler);
223 _createDeviceHandler = 0;
225 gst_clear_object(&_queue);
226 gst_clear_object(&_device);
227 gst_clear_object(&_instance);
232GstContextBridgeBase& GstVulkanContextBridge::instance()
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static GParamSpec * properties[PROP_LAST]
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.