QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstVulkanFrameTextures.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtGui/qtguiglobal.h>
4
5#if defined(QGC_HAS_GST_VULKAN_GPU_PATH) && QT_CONFIG(vulkan)
6
7#include <QtCore/QSize>
8#include <QtMultimedia/QVideoFrameFormat>
9#include <private/qvideotexturehelper_p.h>
10#include <rhi/qrhi.h>
11#include <vulkan/vulkan.h>
12
14
17class GstVulkanFrameTexturesBase : public GstHwFrameTexturesBase
18{
19public:
20 bool valid() const noexcept { return _textures[0] != nullptr; }
21
22protected:
23 void wrapImage(QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, VkImage image, int layout)
24 {
25 _count = 1;
26 const auto* desc = QVideoTextureHelper::textureDescription(pixelFormat);
27 if (!desc) {
28 return;
29 }
30 _textures[0].reset(rhi->newTexture(
31 desc->rhiTextureFormat(0, rhi, QVideoTextureHelper::TextureDescription::FallbackPolicy::Disable), size, 1,
32 {}));
33 if (_textures[0] && !_textures[0]->createFrom({reinterpret_cast<quint64>(image), layout})) {
34 _textures[0].reset();
35 }
36 }
37};
38
40class GstVulkanOwnedFrameTextures final : public GstVulkanFrameTexturesBase
41{
42public:
43 GstVulkanOwnedFrameTextures(QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, VkImage image)
44 {
45 wrapImage(rhi, size, pixelFormat, image, VK_IMAGE_LAYOUT_UNDEFINED);
46 }
47
48 ~GstVulkanOwnedFrameTextures() override { releaseVulkan(); }
49
50 void onFrameEndInvoked() override
51 {
52 releaseVulkan();
54 }
55
56 HwVideoBufferPath sourcePath() const override { return HwVideoBufferPath::DmaBuf; }
57
59 void adoptVulkanResources(VkDevice dev, VkImage image, VkDeviceMemory memory, PFN_vkDestroyImage destroyImage,
60 PFN_vkFreeMemory freeMemory) noexcept
61 {
62 _dev = dev;
63 _image = image;
64 _memory = memory;
65 _destroyImage = destroyImage;
66 _freeMemory = freeMemory;
67 }
68
69private:
70 void releaseVulkan()
71 {
72 // QRhiTexture must be gone before the VkImage it wraps; reset it first.
73 _textures[0].reset();
74 if (_image != VK_NULL_HANDLE && _destroyImage) {
75 _destroyImage(_dev, _image, nullptr);
76 }
77 if (_memory != VK_NULL_HANDLE && _freeMemory) {
78 _freeMemory(_dev, _memory, nullptr);
79 }
80 _image = VK_NULL_HANDLE;
81 _memory = VK_NULL_HANDLE;
82 }
83
84 VkDevice _dev = VK_NULL_HANDLE;
85 VkImage _image = VK_NULL_HANDLE;
86 VkDeviceMemory _memory = VK_NULL_HANDLE;
87 PFN_vkDestroyImage _destroyImage = nullptr;
88 PFN_vkFreeMemory _freeMemory = nullptr;
89};
90
93class GstVulkanBorrowedFrameTextures final : public GstVulkanFrameTexturesBase
94{
95public:
96 GstVulkanBorrowedFrameTextures(QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, VkImage image,
97 int layout)
98 {
99 wrapImage(rhi, size, pixelFormat, image, layout);
100 }
101
102 HwVideoBufferPath sourcePath() const override { return HwVideoBufferPath::Vulkan; }
103};
104
105#endif // QGC_HAS_GST_VULKAN_GPU_PATH
HwVideoBufferPath
Identifies which GPU path was chosen; used by the adapter to increment the right counter.
Common base for per-platform FrameTextures : QVideoFrameTextures from *VideoBuffer::mapTextures().