Overview
TheSwapChainProcessor class manages a dedicated thread that continuously consumes and processes frame buffers from an IddCx swap chain. This is the core rendering pipeline component of the virtual display driver.
Header: Driver.h:71-89
Implementation: Driver.cpp:3082-3400+
Class Declaration
Constructor
SwapChainProcessor::SwapChainProcessor
Signature:hSwapChain: Handle to the IddCx swap chain objectDevice: Shared pointer to the Direct3D device for renderingNewFrameEvent: Event handle signaled when new frame is available
Driver.cpp:3082
Initialization:
- Stores swap chain handle, device, and event handle
- Creates termination event for clean shutdown
- Spawns processing thread immediately
Destructor
SwapChainProcessor::~SwapChainProcessor
Signature:Driver.cpp:3123
Cleanup Process:
- Signals termination event to stop processing thread
- Waits for thread to complete (
WaitForSingleObject) - Handles wait results (WAIT_OBJECT_0, WAIT_ABANDONED, WAIT_TIMEOUT)
- Logs cleanup status
Thread Management
RunThread (Static)
Signature:Driver.cpp:3182
Purpose: Thread entry point that invokes the instance Run() method.
Implementation:
Run
Signature:Driver.cpp:3193
Purpose: Sets up multimedia thread characteristics and invokes core processing loop.
Multimedia Thread Configuration:
- Intelligent thread prioritization by Windows
- Improved throughput in high CPU-load scenarios
- Better frame timing consistency
RunCore
Signature:Driver.cpp:3261
Purpose: Main frame acquisition and processing loop.
Frame Processing Pipeline
Step 1: Set Device to SwapChain
Step 2: Buffer Acquisition Loop
Step 3: Handle Pending Buffers
Step 4: Process Acquired Buffer
Performance Considerations
Critical Performance Section
The frame processing section (marked withTODO in the source) is the most performance-critical part of the driver:
Optimization Guidelines
- Use GPU Operations: Avoid CPU access to frame buffers (requires staging surfaces and map/unmap)
- Asynchronous Processing: Queue GPU commands asynchronously
- Resource Pooling: Reuse D3D resources to avoid allocation overhead
- Minimize State Changes: Batch similar operations together
- Profile Regularly: Use GPU profilers to identify bottlenecks
Retry Logic
The implementation includes retry logic for transient errors:Error Handling
Common Error Scenarios
E_PENDING: No buffer currently available- Action: Wait for
m_hAvailableBufferEventsignal - Timeout: 100ms
- Recovery: Retry acquisition
- Action: Log error and exit processing loop
- Recovery: OS will recreate swap chain with new device
- Action: Exit processing loop
- Recovery: Wait for new swap chain assignment
Logging
All major operations are logged for diagnostics:Thread Lifecycle
Integration Example
Creating SwapChainProcessor
Destroying SwapChainProcessor
Member Variables
m_hSwapChain
Type:IDDCX_SWAPCHAIN
Purpose: Handle to the IddCx swap chain object.
Usage: Passed to all IddCx swap chain APIs.
m_Device
Type:std::shared_ptr<Direct3DDevice>
Purpose: Shared pointer to D3D device used for rendering.
Lifecycle: Reference counted, automatically cleaned up when last reference released.
m_hAvailableBufferEvent
Type:HANDLE
Purpose: Event signaled by IddCx when new frame buffer is available.
Ownership: Created and managed by IddCx, not closed by driver.
m_hThread
Type:Microsoft::WRL::Wrappers::Thread
Purpose: RAII wrapper for processing thread handle.
Cleanup: Automatically closed when wrapper destroyed.
m_hTerminateEvent
Type:Microsoft::WRL::Wrappers::Event
Purpose: Event signaled to request thread termination.
Lifecycle: Created in constructor, signaled in destructor.
References
- Source Files:
~/workspace/source/Virtual Display Driver (HDR)/MttVDD/Driver.h(lines 71-89)~/workspace/source/Virtual Display Driver (HDR)/MttVDD/Driver.cpp(lines 3082-3400+)
- Related Classes: Direct3DDevice
- IddCx SwapChain Reference: Microsoft Docs