8#include <private/qhwvideobuffer_p.h>
9#include <private/qvideoframe_p.h>
38 m_mapMode(
QtVideo::MapMode::NotMapped)
45 m_memSurface->UnlockRect();
55 if (FAILED(m_memSurface->GetDesc(&desc)))
59 ComPtr<IMFMediaBuffer>
buffer;
64 ComPtr<IDirect3DSurface9> surface;
65 hr = MFGetService(
buffer.Get(), MR_BUFFER_SERVICE, IID_IDirect3DSurface9, (
void **)(surface.GetAddressOf()));
69 if (FAILED(surface->GetDesc(&desc)))
72 if (FAILED(
m_device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, m_memSurface.GetAddressOf(),
nullptr)))
75 if (FAILED(
m_device->GetRenderTargetData(surface.Get(), m_memSurface.Get()))) {
91 mapData.dataSize[0] = (int)(
rect.Pitch * desc.Height);
102 m_memSurface->UnlockRect();
110 ComPtr<IDirect3DSurface9> m_memSurface;
118 : m_tex(
std::move(tex))
119 , m_d3d11tex(
std::move(d3d11tex))
124 return plane == 0 ? m_tex.get() :
nullptr;
128 std::unique_ptr<QRhiTexture> m_tex;
129 ComPtr<ID3D11Texture2D> m_d3d11tex;
136 HANDLE sharedHandle,
QRhi *
rhi)
138 , m_sharedHandle(sharedHandle)
150 auto dev =
reinterpret_cast<ID3D11Device *
>(nh->dev);
154 ComPtr<ID3D11Texture2D> d3d11tex;
155 HRESULT hr = dev->OpenSharedResource(m_sharedHandle, __uuidof(ID3D11Texture2D), (
void**)(d3d11tex.GetAddressOf()));
157 D3D11_TEXTURE2D_DESC desc = {};
158 d3d11tex->GetDesc(&desc);
160 if (desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM)
162 else if (desc.Format == DXGI_FORMAT_R8G8B8A8_UNORM)
168 tex->createFrom({
quint64(d3d11tex.Get()), 0});
169 return std::make_unique<QVideoFrameD3D11Textures>(std::move(tex), std::move(d3d11tex));
172 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to obtain D3D11Texture2D from D3D9Texture2D handle";
178 HANDLE m_sharedHandle =
nullptr;
184 struct InterOpHandles {
191 Q_DISABLE_COPY(QVideoFrameOpenGlTextures);
193 QVideoFrameOpenGlTextures(std::unique_ptr<QRhiTexture> &&tex,
const WglNvDxInterop &wgl, InterOpHandles &handles)
194 : m_tex(
std::move(tex))
199 ~QVideoFrameOpenGlTextures()
override {
201 if (!m_wgl.wglDXUnlockObjectsNV(m_handles.device, 1, &m_handles.texture))
202 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to unlock OpenGL texture";
204 if (!m_wgl.wglDXUnregisterObjectNV(m_handles.device, m_handles.texture))
205 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to unregister OpenGL texture";
209 funcs->glDeleteTextures(1, &m_handles.textureName);
211 qCDebug(qLcEvrD3DPresentEngine) <<
"Could not delete texture, OpenGL context functions missing";
213 if (!m_wgl.wglDXCloseDeviceNV(m_handles.device))
214 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to close D3D-GL device";
217 qCDebug(qLcEvrD3DPresentEngine) <<
"Could not release texture, OpenGL context missing";
221 static std::unique_ptr<QVideoFrameOpenGlTextures>
create(
const WglNvDxInterop &wgl,
QRhi *rhi,
231 InterOpHandles handles = {};
232 handles.device = wgl.wglDXOpenDeviceNV(
device);
233 if (!handles.device) {
234 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to open D3D device";
238 wgl.wglDXSetResourceShareHandleNV(
texture, sharedHandle);
242 funcs->glGenTextures(1, &handles.textureName);
243 handles.texture = wgl.wglDXRegisterObjectNV(handles.device,
texture, handles.textureName,
244 GL_TEXTURE_2D, WglNvDxInterop::WGL_ACCESS_READ_ONLY_NV);
245 if (handles.texture) {
246 if (wgl.wglDXLockObjectsNV(handles.device, 1, &handles.texture)) {
247 D3DSURFACE_DESC
desc;
248 texture->GetLevelDesc(0, &desc);
250 if (
desc.Format == D3DFMT_A8R8G8B8)
252 else if (
desc.Format == D3DFMT_A8B8G8R8)
257 std::unique_ptr<QRhiTexture> tex(rhi->
newTexture(
format,
QSize{int(desc.Width), int(desc.Height)}, 1, {}));
258 tex->createFrom({
quint64(handles.textureName), 0});
259 return std::make_unique<QVideoFrameOpenGlTextures>(std::move(tex), wgl, handles);
262 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed to lock OpenGL texture";
263 wgl.wglDXUnregisterObjectNV(handles.device, handles.texture);
265 qCDebug(qLcEvrD3DPresentEngine) <<
"Could not register D3D9 texture in OpenGL";
268 funcs->glDeleteTextures(1, &handles.textureName);
270 qCDebug(qLcEvrD3DPresentEngine) <<
"Failed generate texture names, OpenGL context functions missing";
277 return plane == 0 ? m_tex.get() :
nullptr;
280 std::unique_ptr<QRhiTexture> m_tex;
281 WglNvDxInterop m_wgl;
282 InterOpHandles m_handles;
288 OpenGlVideoBuffer(ComPtr<IDirect3DDevice9Ex>
device,
const ComPtr<IMFSample> &sample,
289 const WglNvDxInterop &wglNvDxInterop, HANDLE sharedHandle,
QRhi *rhi)
291 , m_sharedHandle(sharedHandle)
292 , m_wgl(wglNvDxInterop)
295 std::unique_ptr<QVideoFrameTextures> mapTextures(
QRhi *rhi)
override
298 ComPtr<IMFMediaBuffer>
buffer;
299 HRESULT hr = m_sample->GetBufferByIndex(0,
buffer.GetAddressOf());
303 ComPtr<IDirect3DSurface9> surface;
304 hr = MFGetService(
buffer.Get(), MR_BUFFER_SERVICE, IID_IDirect3DSurface9,
305 (
void **)(surface.GetAddressOf()));
309 hr = surface->GetContainer(IID_IDirect3DTexture9, (
void **)m_texture.GetAddressOf());
314 return QVideoFrameOpenGlTextures::create(m_wgl, rhi, m_device.Get(), m_texture.Get(), m_sharedHandle);
318 HANDLE m_sharedHandle =
nullptr;
319 WglNvDxInterop m_wgl;
320 ComPtr<IDirect3DTexture9> m_texture;
325 : m_deviceResetToken(0)
327 ZeroMemory(&m_displayMode,
sizeof(m_displayMode));
354 hr = createD3DDevice();
356 qWarning(
"Failed to create D3D device");
358 qWarning(
"Failed to initialize D3D");
362HRESULT D3DPresentEngine::initializeD3D()
364 HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, m_D3D9.GetAddressOf());
367 hr = DXVA2CreateDirect3DDeviceManager9(&m_deviceResetToken, m_devices.GetAddressOf());
376 for (
auto i = 0u;
i < D3D9->GetAdapterCount(); ++
i) {
378 D3D9->GetAdapterLUID(
i, &luid);
379 if (luid.LowPart == nh->adapterLuidLow && luid.HighPart == nh->adapterLuidHigh) {
393 fn =
reinterpret_cast<T
>(
ctx->getProcAddress(fName));
394 return fn !=
nullptr;
397static bool readWglNvDxInteropProc(WglNvDxInterop &
f)
403 ctx->makeCurrent(surface.get());
405 auto wglGetExtensionsStringARB =
reinterpret_cast<const char* (WINAPI* )(HDC)
>
406 (
ctx->getProcAddress(
"wglGetExtensionsStringARB"));
407 if (!wglGetExtensionsStringARB) {
408 qCDebug(qLcEvrD3DPresentEngine) <<
"WGL extensions missing (no wglGetExtensionsStringARB function)";
412 HWND hwnd = ::GetShellWindow();
413 auto dc = ::GetDC(hwnd);
415 const char *wglExtString = wglGetExtensionsStringARB(dc);
417 qCDebug(qLcEvrD3DPresentEngine) <<
"WGL extensions missing (wglGetExtensionsStringARB returned null)";
419 bool hasExtension = wglExtString && strstr(wglExtString,
"WGL_NV_DX_interop");
422 qCDebug(qLcEvrD3DPresentEngine) <<
"WGL_NV_DX_interop missing";
426 return getProc(
ctx.get(),
f.wglDXOpenDeviceNV,
"wglDXOpenDeviceNV")
427 && getProc(
ctx.get(),
f.wglDXCloseDeviceNV,
"wglDXCloseDeviceNV")
428 && getProc(
ctx.get(),
f.wglDXSetResourceShareHandleNV,
"wglDXSetResourceShareHandleNV")
429 && getProc(
ctx.get(),
f.wglDXRegisterObjectNV,
"wglDXRegisterObjectNV")
430 && getProc(
ctx.get(),
f.wglDXUnregisterObjectNV,
"wglDXUnregisterObjectNV")
431 && getProc(
ctx.get(),
f.wglDXLockObjectsNV,
"wglDXLockObjectsNV")
432 && getProc(
ctx.get(),
f.wglDXUnlockObjectsNV,
"wglDXUnlockObjectsNV");
438bool hwTextureRenderingEnabled() {
441 static bool isDisableConversionSet =
false;
443 "QT_DISABLE_HW_TEXTURES_CONVERSION", &isDisableConversionSet);
445 return !isDisableConversionSet || !disableHwConversion;
450HRESULT D3DPresentEngine::createD3DDevice()
452 if (!m_D3D9 || !m_devices)
453 return MF_E_NOT_INITIALIZED;
455 m_useTextureRendering =
false;
458 if (hwTextureRenderingEnabled()) {
459 QRhi *rhi = m_sink ? m_sink->
rhi() :
nullptr;
465 m_useTextureRendering = readWglNvDxInteropProc(m_wglNvDxInterop);
468 qCDebug(qLcEvrD3DPresentEngine) <<
"Not supported RHI backend type";
471 qCDebug(qLcEvrD3DPresentEngine) <<
"No RHI associated with this sink";
474 if (!m_useTextureRendering)
475 qCDebug(qLcEvrD3DPresentEngine) <<
"Could not find compatible RHI adapter, zero copy disabled";
479 ZeroMemory(&ddCaps,
sizeof(ddCaps));
481 HRESULT hr = m_D3D9->GetDeviceCaps(adapterID, D3DDEVTYPE_HAL, &ddCaps);
486 if (ddCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
487 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
489 vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
491 D3DPRESENT_PARAMETERS pp;
492 ZeroMemory(&pp,
sizeof(pp));
494 pp.BackBufferWidth = 1;
495 pp.BackBufferHeight = 1;
496 pp.BackBufferCount = 1;
498 pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
499 pp.BackBufferFormat = D3DFMT_UNKNOWN;
500 pp.hDeviceWindow =
nullptr;
501 pp.Flags = D3DPRESENTFLAG_VIDEO;
502 pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
504 ComPtr<IDirect3DDevice9Ex>
device;
506 hr = m_D3D9->CreateDeviceEx(
510 vp | D3DCREATE_NOWINDOWCHANGES | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
518 hr = m_D3D9->GetAdapterDisplayMode(adapterID, &m_displayMode);
522 hr = m_devices->ResetDevice(
device.Get(), m_deviceResetToken);
532 return m_device.Get() !=
nullptr;
544 if (
riid == __uuidof(IDirect3DDeviceManager9)) {
546 hr = MF_E_UNSUPPORTED_SERVICE;
548 *
ppv = m_devices.Get();
552 hr = MF_E_UNSUPPORTED_SERVICE;
560 if (!m_D3D9 || !m_device)
566 D3DDEVICE_CREATION_PARAMETERS
params;
568 hr = m_device->GetCreationParameters(&
params);
572 UINT uAdapter =
params.AdapterOrdinal;
575 hr = m_D3D9->GetAdapterDisplayMode(uAdapter, &
mode);
579 hr = m_D3D9->CheckDeviceFormat(uAdapter,
type,
mode.Format,
580 D3DUSAGE_RENDERTARGET,
587 ||
format == D3DFMT_A8R8G8B8
588 ||
format == D3DFMT_X8B8G8R8
589 ||
format == D3DFMT_A8B8G8R8;
591 return ok ? S_OK : D3DERR_NOTAVAILABLE;
595 QList<ComPtr<IMFSample>> &videoSampleQueue,
599 return MF_E_UNEXPECTED;
620 if (d3dFormat == D3DFMT_X8R8G8B8)
621 d3dFormat = D3DFMT_A8R8G8B8;
622 else if (d3dFormat == D3DFMT_X8B8G8R8)
623 d3dFormat = D3DFMT_A8B8G8R8;
625 for (
int i = 0;
i < PRESENTER_BUFFER_COUNT;
i++) {
628 ComPtr<IDirect3DTexture9>
texture;
629 HANDLE sharedHandle =
nullptr;
630 hr = m_device->CreateTexture(
width,
height, 1, D3DUSAGE_RENDERTARGET, (D3DFORMAT)d3dFormat, D3DPOOL_DEFAULT,
texture.GetAddressOf(), &sharedHandle);
634 ComPtr<IDirect3DSurface9> surface;
635 hr =
texture->GetSurfaceLevel(0, surface.GetAddressOf());
639 ComPtr<IMFSample> videoSample;
640 hr = MFCreateVideoSampleFromSurface(surface.Get(), videoSample.GetAddressOf());
644 m_sampleTextureHandle[
i] = {videoSample.Get(), sharedHandle};
645 videoSampleQueue.append(videoSample);
662 HANDLE sharedHandle =
nullptr;
663 for (
const auto &
p : m_sampleTextureHandle)
664 if (
p.first == sample.Get())
665 sharedHandle =
p.second;
667 std::unique_ptr<IMFSampleVideoBuffer> vb;
668 QRhi *rhi = m_sink ? m_sink->
rhi() :
nullptr;
669 if (m_useTextureRendering && sharedHandle && rhi) {
671 vb = std::make_unique<D3D11TextureVideoBuffer>(m_device, sample, sharedHandle, rhi);
674 vb = std::make_unique<OpenGlVideoBuffer>(m_device, sample, m_wglNvDxInterop,
681 vb = std::make_unique<IMFSampleVideoBuffer>(m_device, sample, rhi);
687 auto hr = sample->GetSampleTime(&
startTime);
691 LONGLONG duration = -1;
692 if (SUCCEEDED(sample->GetSampleDuration(&duration)))
IOBluetoothDevice * device
std::unique_ptr< QVideoFrameTextures > mapTextures(QRhi *rhi) override
D3D11TextureVideoBuffer(ComPtr< IDirect3DDevice9Ex > device, const ComPtr< IMFSample > &sample, HANDLE sharedHandle, QRhi *rhi)
void setSink(QVideoSink *sink)
HRESULT createVideoSamples(IMFMediaType *format, QList< ComPtr< IMFSample > > &videoSampleQueue, QSize frameSize)
HRESULT getService(REFGUID guidService, REFIID riid, void **ppv)
virtual ~D3DPresentEngine()
D3DPresentEngine(QVideoSink *sink)
HRESULT checkFormat(D3DFORMAT format)
QVideoFrame makeVideoFrame(const ComPtr< IMFSample > &sample)
ComPtr< IMFSample > m_sample
ComPtr< IDirect3DDevice9Ex > m_device
MapData map(QtVideo::MapMode mode) override
Maps the planes of a video buffer to memory.
void unmap() override
Releases the memory mapped by the map() function.
IMFSampleVideoBuffer(ComPtr< IDirect3DDevice9Ex > device, const ComPtr< IMFSample > &sample, QRhi *rhi, QVideoFrame::HandleType type=QVideoFrame::NoHandle)
~IMFSampleVideoBuffer() override
QVideoFrameFormat format() const override
Gets \l QVideoFrameFormat of the underlying video buffer.
static QOpenGLContext * currentContext()
Returns the last context which called makeCurrent in the current thread, or \nullptr,...
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
\variable QRhiD3D11InitParams::enableDebugLayer
Format
Specifies the texture format.
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Implementation backend() const
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
const QRhiNativeHandles * nativeHandles()
QVideoFrameD3D11Textures(std::unique_ptr< QRhiTexture > &&tex, ComPtr< ID3D11Texture2D > &&d3d11tex)
QRhiTexture * texture(uint plane) const override
static QVideoFrame createFrame(std::unique_ptr< Buffer > buffer, QVideoFrameFormat format)
The QVideoFrame class represents a frame of video data.
HandleType
Identifies the type of a video buffers handle.
The QVideoSink class represents a generic sink for video data.
QRhi * rhi() const
Returns the QRhi instance being used to create texture data in the video frames.
static VulkanServerBufferGlFunctions * funcs
static bool findD3D11AdapterID(QRhi &rhi, IDirect3D9Ex *D3D9, UINT &adapterID)
QVideoFrameFormat::PixelFormat qt_evr_pixelFormatFromD3DFormat(DWORD format)
QT_BEGIN_NAMESPACE HRESULT qt_evr_getFourCC(IMFMediaType *type, DWORD *fourCC)
Combined button and popup list for selecting options.
Enumerations for camera and video functionality.
MapMode
Enumerates how a video buffer's data is mapped to system memory.
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
GLint GLsizei GLsizei height
GLint GLsizei GLsizei GLenum format
GLsizei GLenum GLboolean sink
static QAbstractVideoBuffer::MapData mapData(const camera_frame_nv12_t &frame, unsigned char *baseAddress)
static constexpr QSize frameSize(const T &frame)
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
unsigned long long quint64
IUIViewSettingsInterop __RPC__in REFIID riid
IUIViewSettingsInterop __RPC__in REFIID __RPC__deref_out_opt void ** ppv
The QAbstractVideoBuffer::MapData structure describes the mapped plane layout.