Some environments never deliver a frame from
IDXGIOutputDuplication::AcquireNextFrame: headless or virtual displays, Session 0
desktops, and drivers that do not push duplication notifications. It returns
DXGI_ERROR_WAIT_TIMEOUT forever, even while the desktop is changing.
Consequences:
- The first screen could not be captured, so the server kept showing
"Please wait..." because m_bIsFirst was never cleared.
- Adding a GDI fallback for the first screen alone was not enough: the picture
appeared but then froze, because ScanNextScreen still polled DXGI and always
got nothing back, leaving the encoder without input.
Fixes:
- Add CaptureByGDI(), which grabs a frame with GetDC(NULL) + CreateDIBSection +
BitBlt. GDI does not depend on desktop changes, which is why the plain GDI
capture path always worked in these environments.
- When the first screen cannot be obtained from DXGI, capture it with GDI and
mark the whole session as GDI (m_bGDIFallback = true), releasing the
duplication object. All later frames then go through GDI so the picture keeps
updating.
- Normal machines are unaffected: if DXGI delivers the probe frame or the first
screen, the flag stays false and the GDI path is never entered. This also
avoids mis-detecting a momentarily static desktop as a broken duplication,
which a plain timeout counter would do.
- Create the GDI offscreen DIB lazily and reuse it (m_hGdiMemDC, m_hGdiBmp,
m_pGdiBits). The fallback is a per-frame path, so allocating and freeing about
15MB every frame would cause visible jitter.
- Reuse m_BitmapInfor_Full for CreateDIBSection instead of building a second
BITMAPINFO: it is already 32bpp BI_RGB with a positive height (bottom-up) and
matches the surface size.
- Guard the GDI buffer readiness check. If CreateCompatibleDC or SelectObject
fails, drop the bitmap so the next call does not mistake a half-initialized
state for a usable one; otherwise BitBlt would silently write nowhere and
memcpy would read uninitialized memory.
- Restore the previous bitmap before DeleteDC and check for HGDI_ERROR,
otherwise DeleteObject on the still-selected bitmap fails and leaks it.
The fallback matches the plain GDI capture path (full-size BitBlt plus
ScaleBitmap), so a downgraded session performs the same as GDI mode. The only
added cost versus DXGI is one BitBlt per frame.