microsoft / Windows.UI.Composition-Win32-Samples

Windows.UI.Composition Win32 Samples
MIT License
459 stars 186 forks source link

Any idea why WinRT capture cost 10s+ to finished initialized? #135

Closed TitusQian closed 1 month ago

TitusQian commented 2 months ago

Issue: Our product have a feature similar to screen sharing which use WinRT to capture desktop/window, but from a customer feedback, we found WinRT capture cost 10s+ to finished initialized, we have no idea while it only cost 1s in local test PC,

I'm sorry to say I can't reduce the scope which API call is cost the maximum time due to lack of logs in our code, and this issue happened only one day but can't reproduce any more with about 15+ times retry, then the cost will reduce to 5s to initialized,

So my question is any possibility why it will take so long time to initialized, if can't reduce the time, how to break the queue in a graceful way as you may know below code is running in a DispatcherQueue.

code snippet

// CreateForWindow cost 2s
auto hr = interopFactory->CreateForWindow(hwnd, winrt::guid_of< ABI::Windows::Graphics::Capture::IGraphicsCaptureItem >(),
    reinterpret_cast<void**>(winrt::put_abi(item)));
// CreateForWindow cost 2s

// cost 10s to finished call
m_framePool = winrt::Direct3D11CaptureFramePool::Create(m_d3dDevice, winrt::DirectXPixelFormat::B8G8R8A8UIntNormalized,
    m_framePoolSize, m_item.Size());

m_revoker = m_framePool.FrameArrived(winrt::auto_revoke, { this, &WinRTCapture::FrameReady });

m_session = m_framePool.CreateCaptureSession(m_item);

m_cursorCapture = m_extParams->isWindow() ? false : m_cursorCapture;
m_session.IsCursorCaptureEnabled(m_cursorCapture);
m_session.StartCapture();
// cost 10s to finished call
robmikh commented 2 months ago

Unfortunately, there isn't a lot to go off of here.

What version/build of Windows do you see this on? Is this on a VM or on real hardware?

What else does your application do in relation to capture? Are there other captures ongoing or do you only ever create one?

TitusQian commented 2 months ago

@robmikh thanks for your quick response, I'm still debugging the code and try to reproduce, but unfortunately there is no substantial breakthrough, the issue happened in customer's PC with Windows version is 10.0.19045.4291, and it's not VM, the capture is tried to capture Firefox window that time, but it cost 10s+ to initialized as described.

TitusQian commented 1 month ago

@robmikh I have found out the root cause, I just simulate system high CPU by CpuStres, after CpuStres cost about 90% CPU, WinRT initialized will be very slow even for 15s+, also check the customer's performance data that time, the CPU is 80% - 90% which match the conclusion.

I think this issue can be closed, thanks for your support!

robmikh commented 1 month ago

Good to know! Thanks for circling back on this one, I appreciate the information!