Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ functions, which is done by editing vdd_settings.xml. You should be able to loca
For more information about manual installation, uninstallation and "personalization", please check out the [Wiki](https://github.com/VirtualDrivers/Virtual-Display-Driver/wiki) here on
the project GitHub repository. If you are into tinkering, check out the Powershell scripts in [Community scripts](https://github.com/VirtualDrivers/Virtual-Display-Driver/tree/master/Community%20Scripts).

### Starting without a monitor (on-demand monitors)

By default the driver creates `<count>` monitors when it starts. Set `<count>0</count>` in `vdd_settings.xml` to start
with no monitor at all, so nothing shows up in Display settings or Win+P while the virtual display is not in use.
An application (for example a game-streaming host) can then plug a monitor in and out at runtime through the
control pipe `\\.\pipe\MTTVirtualDisplayPipe`, without reloading the driver:

| Command | Effect | Reply |
| :--- | :--- | :--- |
| `ATTACH` | If no monitor is attached, plugs in `<count>` monitors (one when `<count>` is 0). Does nothing if monitors are already attached. | `ATTACHED` or `ERROR` |
| `DETACH` | Unplugs every attached monitor. Does nothing if none is attached. Windows moves open windows to the remaining displays. | `DETACHED` or `ERROR` |

Commands are sent as UTF-16 text, like the existing pipe commands (`PING`, `RELOAD_DRIVER`, ...). If pipe logging is
enabled, log lines may arrive before the reply. `ATTACH` uses the settings that were loaded when the driver started;
monitors created by `ATTACH` behave the same as monitors created at startup.

The driver remembers the last `ATTACH`/`DETACH`: when Windows re-initializes the adapter (for example after sleep,
or after `RELOAD_DRIVER`), an attached monitor is plugged back in and a detached one stays unplugged. Until the first
`ATTACH` or `DETACH`, `<count>` decides as before.

## 🤔 Comparison with other IDDs

The table below shows a comparison with other popular Indirect Display Driver
Expand Down
104 changes: 100 additions & 4 deletions Virtual Display Driver (HDR)/MttVDD/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ bool g_Running = true;
mutex g_Mutex;
HANDLE g_pipeHandle = INVALID_HANDLE_VALUE;

// Device context used by the ATTACH / DETACH pipe commands. Set in FinishInit, cleared in the destructor.
// g_DeviceContextMutex also serializes monitor creation/departure between the pipe thread and IddCx callbacks.
Microsoft::IndirectDisp::IndirectDeviceContext* g_DeviceContext = nullptr;
mutex g_DeviceContextMutex;
// Last ATTACH/DETACH request. The adapter is re-initialized on every D0 entry (e.g. after sleep), which drops all
// monitors; FinishInit uses this to restore the state the client asked for instead of the startup <count>.
enum class AttachRequest { None, Attached, Detached };
AttachRequest g_AttachRequest = AttachRequest::None;

using namespace std;
using namespace Microsoft::IndirectDisp;
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -2250,6 +2259,32 @@ void HandleClient(HANDLE hPipe) {
WriteFile(hPipe, settingsResponse.c_str(), bytesToWrite, &bytesWritten, NULL);

}
else if (wcsncmp(buffer, L"ATTACH", 6) == 0) {
// Plug the configured virtual monitor(s) in without reinitializing the adapter.
vddlog("c", "Attaching virtual monitor(s)");
bool ok = false;
{
lock_guard<mutex> lock(g_DeviceContextMutex);
g_AttachRequest = AttachRequest::Attached;
if (g_DeviceContext) {
ok = g_DeviceContext->AttachMonitors();
}
}
SendToPipe(ok ? "ATTACHED" : "ERROR");
}
else if (wcsncmp(buffer, L"DETACH", 6) == 0) {
// Unplug all virtual monitors. The adapter stays loaded, so a later ATTACH is fast.
vddlog("c", "Detaching virtual monitor(s)");
bool ok = false;
{
lock_guard<mutex> lock(g_DeviceContextMutex);
g_AttachRequest = AttachRequest::Detached;
if (g_DeviceContext) {
ok = g_DeviceContext->DetachMonitors();
}
}
SendToPipe(ok ? "DETACHED" : "ERROR");
}
else if (wcsncmp(buffer, L"PING", 4) == 0) {
SendToPipe("PONG");
vddlog("p", "Heartbeat Ping");
Expand Down Expand Up @@ -2599,8 +2634,8 @@ void loadSettings() {
if (currentElement == L"count") {
monitorcount = stoi(wstring(pwszValue, cwchValue));
if (monitorcount == 0) {
monitorcount = 1;
vddlog("i", "Loading singular monitor (Monitor Count is not valid)");
// No monitor at startup. Monitors are plugged in later with the ATTACH pipe command.
vddlog("i", "Monitor count is 0: starting without monitors (use ATTACH to add one)");
}
}
else if (currentElement == L"friendlyname") {
Expand Down Expand Up @@ -3665,6 +3700,13 @@ IndirectDeviceContext::IndirectDeviceContext(_In_ WDFDEVICE WdfDevice) :

IndirectDeviceContext::~IndirectDeviceContext()
{
{
lock_guard<mutex> lock(g_DeviceContextMutex);
if (g_DeviceContext == this) {
g_DeviceContext = nullptr;
}
}

stringstream logStream;
std::map<IDDCX_MONITOR, std::unique_ptr<SwapChainProcessor>> processingThreads;

Expand Down Expand Up @@ -3708,7 +3750,8 @@ void IndirectDeviceContext::InitAdapter()
}

// Declare basic feature support for the adapter (required)
AdapterCaps.MaxMonitorsSupported = numVirtualDisplays;
// With <count>0</count> the adapter still has to support one monitor so that ATTACH can add it later.
AdapterCaps.MaxMonitorsSupported = numVirtualDisplays > 0 ? numVirtualDisplays : 1;
AdapterCaps.EndPointDiagnostics.Size = sizeof(AdapterCaps.EndPointDiagnostics);
AdapterCaps.EndPointDiagnostics.GammaSupport = IDDCX_FEATURE_IMPLEMENTATION_NONE;
AdapterCaps.EndPointDiagnostics.TransmissionType = IDDCX_TRANSMISSION_TYPE_WIRED_OTHER;
Expand Down Expand Up @@ -3774,11 +3817,63 @@ void IndirectDeviceContext::InitAdapter()

void IndirectDeviceContext::FinishInit()
{
lock_guard<mutex> lock(g_DeviceContextMutex);
g_DeviceContext = this;
m_Monitors.clear(); // Monitors of a previous adapter instance are gone after re-initialization
Options.Adapter.apply(m_Adapter);
vddlog("i", "Applied Adapter configs.");
for (unsigned int i = 0; i < numVirtualDisplays; i++) {
// After ATTACH/DETACH, keep that state across re-initialization (e.g. after sleep); otherwise use <count>.
unsigned int startupMonitors = numVirtualDisplays;
if (g_AttachRequest == AttachRequest::Attached && startupMonitors == 0) {
startupMonitors = 1;
}
else if (g_AttachRequest == AttachRequest::Detached) {
startupMonitors = 0;
}
for (unsigned int i = 0; i < startupMonitors; i++) {
CreateMonitor(i);
}
}

// Plugs in the configured number of monitors (at least one) if none is attached.
// Does nothing if monitors are already attached. Call with g_DeviceContextMutex held.
bool IndirectDeviceContext::AttachMonitors()
{
if (!m_Monitors.empty()) {
vddlog("i", "Monitor(s) already attached");
return true;
}
if (m_Adapter == nullptr) {
vddlog("e", "Cannot attach monitor: adapter is not initialized");
return false;
}
unsigned int count = numVirtualDisplays > 0 ? numVirtualDisplays : 1;
for (unsigned int i = 0; i < count; i++) {
CreateMonitor(i);
}
return !m_Monitors.empty();
}

// Reports departure of every attached monitor. Succeeds if none is attached.
// Call with g_DeviceContextMutex held.
bool IndirectDeviceContext::DetachMonitors()
{
bool ok = true;
for (IDDCX_MONITOR monitor : m_Monitors) {
NTSTATUS Status = IddCxMonitorDeparture(monitor);
if (NT_SUCCESS(Status)) {
vddlog("d", "Monitor departure successfully reported.");
}
else {
stringstream ss;
ss << "Failed to report monitor departure. Status: " << Status;
vddlog("e", ss.str().c_str());
ok = false;
}
}
// The monitor objects must not be used after departure, even if it failed.
m_Monitors.clear();
return ok;
}

void IndirectDeviceContext::CreateMonitor(unsigned int index) {
Expand Down Expand Up @@ -3857,6 +3952,7 @@ void IndirectDeviceContext::CreateMonitor(unsigned int index) {
if (NT_SUCCESS(Status))
{
vddlog("d", "Monitor arrival successfully reported.");
m_Monitors.push_back(MonitorCreateOut.MonitorObject);
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions Virtual Display Driver (HDR)/MttVDD/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ namespace Microsoft

void CreateMonitor(unsigned int index);

// Plug/unplug the virtual monitors at runtime (pipe commands ATTACH / DETACH).
bool AttachMonitors();
bool DetachMonitors();

void AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent);
void UnassignSwapChain(IDDCX_MONITOR Monitor);

Expand All @@ -124,6 +128,7 @@ namespace Microsoft
IDDCX_ADAPTER m_Adapter;
IDDCX_MONITOR m_Monitor;
IDDCX_MONITOR m_Monitor2;
std::vector<IDDCX_MONITOR> m_Monitors; // Monitors that have arrived and not departed yet

std::map<IDDCX_MONITOR, std::unique_ptr<SwapChainProcessor>> m_ProcessingThreads;
std::mutex m_ProcessingThreadsMutex;
Expand Down
1 change: 1 addition & 0 deletions Virtual Display Driver (HDR)/vdd_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<!-- === BASIC DRIVER CONFIGURATION === -->
<monitors>
<!-- 0 = start without a monitor; plug one in later with the ATTACH pipe command -->
<count>1</count>
</monitors>

Expand Down