diff --git a/README.md b/README.md index 83106c24..2c038b3f 100644 --- a/README.md +++ b/README.md @@ -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 `` monitors when it starts. Set `0` 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 `` monitors (one when `` 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`, `` decides as before. + ## 🤔 Comparison with other IDDs The table below shows a comparison with other popular Indirect Display Driver diff --git a/Virtual Display Driver (HDR)/MttVDD/Driver.cpp b/Virtual Display Driver (HDR)/MttVDD/Driver.cpp index 3145b04a..d77cc439 100644 --- a/Virtual Display Driver (HDR)/MttVDD/Driver.cpp +++ b/Virtual Display Driver (HDR)/MttVDD/Driver.cpp @@ -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 . +enum class AttachRequest { None, Attached, Detached }; +AttachRequest g_AttachRequest = AttachRequest::None; + using namespace std; using namespace Microsoft::IndirectDisp; using namespace Microsoft::WRL; @@ -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 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 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"); @@ -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") { @@ -3665,6 +3700,13 @@ IndirectDeviceContext::IndirectDeviceContext(_In_ WDFDEVICE WdfDevice) : IndirectDeviceContext::~IndirectDeviceContext() { + { + lock_guard lock(g_DeviceContextMutex); + if (g_DeviceContext == this) { + g_DeviceContext = nullptr; + } + } + stringstream logStream; std::map> processingThreads; @@ -3708,7 +3750,8 @@ void IndirectDeviceContext::InitAdapter() } // Declare basic feature support for the adapter (required) - AdapterCaps.MaxMonitorsSupported = numVirtualDisplays; + // With 0 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; @@ -3774,11 +3817,63 @@ void IndirectDeviceContext::InitAdapter() void IndirectDeviceContext::FinishInit() { + lock_guard 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 . + 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) { @@ -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 { diff --git a/Virtual Display Driver (HDR)/MttVDD/Driver.h b/Virtual Display Driver (HDR)/MttVDD/Driver.h index b48b97ff..0ea14718 100644 --- a/Virtual Display Driver (HDR)/MttVDD/Driver.h +++ b/Virtual Display Driver (HDR)/MttVDD/Driver.h @@ -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); @@ -124,6 +128,7 @@ namespace Microsoft IDDCX_ADAPTER m_Adapter; IDDCX_MONITOR m_Monitor; IDDCX_MONITOR m_Monitor2; + std::vector m_Monitors; // Monitors that have arrived and not departed yet std::map> m_ProcessingThreads; std::mutex m_ProcessingThreadsMutex; diff --git a/Virtual Display Driver (HDR)/vdd_settings.xml b/Virtual Display Driver (HDR)/vdd_settings.xml index 4bf082c3..e4591c90 100644 --- a/Virtual Display Driver (HDR)/vdd_settings.xml +++ b/Virtual Display Driver (HDR)/vdd_settings.xml @@ -10,6 +10,7 @@ + 1