Desktop Code ScreenCapture - shibotsu/obs-clone GitHub Wiki
Desktop Code: ScreenCapture Module
This page documents the ScreenCapture class used in the Desktop application, which handles screen capturing using DirectX 11 and DXGI Desktop Duplication API.
🧱 Purpose
The ScreenCapture
class captures the current frame of the desktop, including mouse position, and converts it into a QImage
for further processing or UI rendering.
🔧 Core Responsibilities
- Initialize DirectX 11 and DXGI components
- Create and manage a staging texture to transfer GPU image data to CPU
- Continuously capture and map desktop frames
- Handle mouse pointer data
- Manage cleanup and resource safety
🛠️ Key Methods
bool initialize()
Initializes DirectX and the DXGI duplication interface. Returns false
if either fails.
bool initDirectX()
Creates the D3D11 device and context, checking supported feature levels.
Returns true
if the DirectX device is created successfully, otherwise returns false
.
bool initDuplication()
Initializes the DXGI duplication for capturing frames:
- Queries DXGI device and adapter
- Obtains output display description
- Duplicates the output for capture
- Creates a CPU-readable staging texture
Retruns true
if we're ready to capture frames, otherwise returns false
meaning the setup failed (e.g., no outputs, unsupported device).
bool captureFrame()
Captures the next available desktop frame:
- Releases previously acquired frame
- Acquires new frame
- Queries it as a
ID3D11Texture2D
- Copies to CPU-readable staging texture
- Maps the texture to access pixel data
- Updates a
QImage
(m_latestFrame
) with raw frame pixels
Handles errors such as:
DXGI_ERROR_ACCESS_LOST
: Reinitializes duplicationDXGI_ERROR_WAIT_TIMEOUT
: Skips frame
📂 Related files
- ScreenCapture.h
- ScreenCapture.cpp
🧱 Dependencies
- Qt (for
qDebug()
) - Direct3D (part of Windows SDK)