3#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
9namespace GstD3D11PictureCopy {
15constexpr GUID kCopyTargetTag = {0x6d3c1f52, 0x8a4e, 0x4b7f, {0x9c, 0x21, 0x5e, 0x0b, 0x7a, 0x93, 0xd4, 0x18}};
16constexpr quint8 kCopyTargetMark = 1;
18bool isCopyTarget(ID3D11Texture2D* texture)
21 UINT size =
static_cast<UINT
>(
sizeof(mark));
22 return SUCCEEDED(texture->GetPrivateData(kCopyTargetTag, &size, &mark)) && (mark == kCopyTargetMark);
26UINT textureExtent(
int extent, DXGI_FORMAT format)
28 return static_cast<UINT
>(hasEvenSizes(format) ? ((extent + 1) & ~1) : extent);
32ID3D11Texture2D* reusableTarget(
const QVideoFrameTexturesUPtr& old,
int plane,
const D3D11_TEXTURE2D_DESC& desc)
34 auto* previous =
dynamic_cast<GstD3DVideoBufferCommon::FrameTextures<ID3D11Texture2D>*
>(old.get());
35 ID3D11Texture2D* texture = previous ? previous->handle(plane) :
nullptr;
36 if (!texture || !isCopyTarget(texture)) {
39 D3D11_TEXTURE2D_DESC have = {};
40 texture->GetDesc(&have);
41 if ((have.Width != desc.Width) || (have.Height != desc.Height) || (have.Format != desc.Format)) {
50bool onDevice(ID3D11Texture2D* texture, ID3D11Device* device)
52 ID3D11Device* owner =
nullptr;
53 texture->GetDevice(&owner);
54 const bool same = (owner !=
nullptr) && (owner == device);
61bool hasEvenSizes(DXGI_FORMAT format)
64 case DXGI_FORMAT_NV12:
65 case DXGI_FORMAT_P010:
66 case DXGI_FORMAT_P016:
73ID3D11Texture2D* copy(ID3D11Device* device, ID3D11DeviceContext* context, ID3D11Texture2D* source, UINT slice,
74 QRect picture,
const QVideoFrameTexturesUPtr& old,
int plane)
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);
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;
93 ID3D11Texture2D* target = reusableTarget(old, plane, targetDesc);
95 if (FAILED(
device->CreateTexture2D(&targetDesc,
nullptr, &target)) || !target) {
98 (void) target->SetPrivateData(kCopyTargetTag,
static_cast<UINT
>(
sizeof(kCopyTargetMark)), &kCopyTargetMark);
101 const D3D11_BOX box = {left, top, 0, left + width, top + height, 1};
102 context->CopySubresourceRegion(target, 0, 0, 0, 0, source, slice, &box);
QCameraDevice device(const QString &description)
The attached camera of description description; a null device when none is.