QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstD3D11PictureCopy.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
4
5#include <algorithm>
6
8
9namespace GstD3D11PictureCopy {
10
11namespace {
12
13// Tags the textures copy() creates: only those may be overwritten by a later frame. A previous bundle can also wrap a
14// texture GStreamer still owns.
15constexpr GUID kCopyTargetTag = {0x6d3c1f52, 0x8a4e, 0x4b7f, {0x9c, 0x21, 0x5e, 0x0b, 0x7a, 0x93, 0xd4, 0x18}};
16constexpr quint8 kCopyTargetMark = 1;
17
18bool isCopyTarget(ID3D11Texture2D* texture)
19{
20 quint8 mark = 0;
21 UINT size = static_cast<UINT>(sizeof(mark));
22 return SUCCEEDED(texture->GetPrivateData(kCopyTargetTag, &size, &mark)) && (mark == kCopyTargetMark);
23}
24
26UINT textureExtent(int extent, DXGI_FORMAT format)
27{
28 return static_cast<UINT>(hasEvenSizes(format) ? ((extent + 1) & ~1) : extent);
29}
30
32ID3D11Texture2D* reusableTarget(const QVideoFrameTexturesUPtr& old, int plane, const D3D11_TEXTURE2D_DESC& desc)
33{
34 auto* previous = dynamic_cast<GstD3DVideoBufferCommon::FrameTextures<ID3D11Texture2D>*>(old.get());
35 ID3D11Texture2D* texture = previous ? previous->handle(plane) : nullptr;
36 if (!texture || !isCopyTarget(texture)) {
37 return nullptr;
38 }
39 D3D11_TEXTURE2D_DESC have = {};
40 texture->GetDesc(&have);
41 if ((have.Width != desc.Width) || (have.Height != desc.Height) || (have.Format != desc.Format)) {
42 return nullptr;
43 }
44 texture->AddRef();
45 return texture;
46}
47
48} // namespace
49
50bool onDevice(ID3D11Texture2D* texture, ID3D11Device* device)
51{
52 ID3D11Device* owner = nullptr;
53 texture->GetDevice(&owner);
54 const bool same = (owner != nullptr) && (owner == device);
55 if (owner) {
56 owner->Release();
57 }
58 return same;
59}
60
61bool hasEvenSizes(DXGI_FORMAT format)
62{
63 switch (format) {
64 case DXGI_FORMAT_NV12:
65 case DXGI_FORMAT_P010:
66 case DXGI_FORMAT_P016:
67 return true;
68 default:
69 return false;
70 }
71}
72
73ID3D11Texture2D* copy(ID3D11Device* device, ID3D11DeviceContext* context, ID3D11Texture2D* source, UINT slice,
74 QRect picture, const QVideoFrameTexturesUPtr& old, int plane)
75{
76 D3D11_TEXTURE2D_DESC sourceDesc = {};
77 source->GetDesc(&sourceDesc);
78 const UINT left = (std::min) (textureExtent(picture.x(), sourceDesc.Format), sourceDesc.Width);
79 const UINT top = (std::min) (textureExtent(picture.y(), sourceDesc.Format), sourceDesc.Height);
80 const UINT width = (std::min) (textureExtent(picture.width(), sourceDesc.Format), sourceDesc.Width - left);
81 const UINT height = (std::min) (textureExtent(picture.height(), sourceDesc.Format), sourceDesc.Height - top);
82
83 D3D11_TEXTURE2D_DESC targetDesc = {};
84 targetDesc.Width = width;
85 targetDesc.Height = height;
86 targetDesc.MipLevels = 1;
87 targetDesc.ArraySize = 1;
88 targetDesc.Format = sourceDesc.Format;
89 targetDesc.SampleDesc.Count = 1;
90 targetDesc.Usage = D3D11_USAGE_DEFAULT;
91 targetDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
92
93 ID3D11Texture2D* target = reusableTarget(old, plane, targetDesc);
94 if (!target) {
95 if (FAILED(device->CreateTexture2D(&targetDesc, nullptr, &target)) || !target) {
96 return nullptr;
97 }
98 (void) target->SetPrivateData(kCopyTargetTag, static_cast<UINT>(sizeof(kCopyTargetMark)), &kCopyTargetMark);
99 }
100
101 const D3D11_BOX box = {left, top, 0, left + width, top + height, 1};
102 context->CopySubresourceRegion(target, 0, 0, 0, 0, source, slice, &box);
103 return target;
104}
105
106} // namespace GstD3D11PictureCopy
107
108#endif // QGC_HAS_GST_D3D11_GPU_PATH
QCameraDevice device(const QString &description)
The attached camera of description description; a null device when none is.
Definition UVCDevices.cc:21