diff --git a/Virtual Display Driver (HDR)/MttVDD/Driver.cpp b/Virtual Display Driver (HDR)/MttVDD/Driver.cpp index 3145b04..f02a43c 100644 --- a/Virtual Display Driver (HDR)/MttVDD/Driver.cpp +++ b/Virtual Display Driver (HDR)/MttVDD/Driver.cpp @@ -301,8 +301,6 @@ const char* XorCursorSupportLevelToString(IDDCX_XOR_CURSOR_SUPPORT level) { vector Microsoft::IndirectDisp::IndirectDeviceContext::s_KnownMonitorEdid; //Changed to support static vector -std::map, Microsoft::IndirectDisp::LuidComparator> Microsoft::IndirectDisp::IndirectDeviceContext::s_DeviceCache; -std::mutex Microsoft::IndirectDisp::IndirectDeviceContext::s_DeviceCacheMutex; struct IndirectDeviceContextWrapper { @@ -3577,77 +3575,22 @@ int maincalc() { std::shared_ptr IndirectDeviceContext::GetOrCreateDevice(LUID RenderAdapter) { - std::shared_ptr Device; - stringstream logStream; - - logStream << "GetOrCreateDevice called for LUID: " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart; - vddlog("d", logStream.str().c_str()); - { - std::lock_guard lock(s_DeviceCacheMutex); - - logStream.str(""); - logStream << "Device cache size: " << s_DeviceCache.size(); - vddlog("d", logStream.str().c_str()); - - auto it = s_DeviceCache.find(RenderAdapter); - if (it != s_DeviceCache.end()) { - Device = it->second; - if (Device) { - logStream.str(""); - logStream << "Reusing cached Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart; - vddlog("d", logStream.str().c_str()); - return Device; - } else { - logStream.str(""); - logStream << "Cached Direct3DDevice is null for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart << ", removing from cache"; - vddlog("d", logStream.str().c_str()); - s_DeviceCache.erase(it); - } + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(RenderAdapter); + if (it != m_DeviceCache.end()) { + return it->second; } } - logStream.str(""); - logStream << "Creating new Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart; - vddlog("d", logStream.str().c_str()); - - Device = make_shared(RenderAdapter); + auto Device = make_shared(RenderAdapter); if (FAILED(Device->Init())) { vddlog("e", "Failed to initialize new Direct3DDevice"); return nullptr; } - { - std::lock_guard lock(s_DeviceCacheMutex); - s_DeviceCache[RenderAdapter] = Device; - logStream.str(""); - logStream << "Created and cached new Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart << " (cache size now: " << s_DeviceCache.size() << ")"; - vddlog("d", logStream.str().c_str()); - } - - return Device; -} - -void IndirectDeviceContext::CleanupExpiredDevices() -{ - std::lock_guard lock(s_DeviceCacheMutex); - - int removed = 0; - for (auto it = s_DeviceCache.begin(); it != s_DeviceCache.end();) { - // With shared_ptr cache, we only remove null devices (shouldn't happen) - if (!it->second) { - it = s_DeviceCache.erase(it); - removed++; - } else { - ++it; - } - } - - if (removed > 0) { - stringstream logStream; - logStream << "Cleaned up " << removed << " null Direct3DDevice references from cache"; - vddlog("d", logStream.str().c_str()); - } + std::lock_guard lock(m_DeviceCacheMutex); + return m_DeviceCache.try_emplace(RenderAdapter, Device).first->second; } IndirectDeviceContext::IndirectDeviceContext(_In_ WDFDEVICE WdfDevice) : @@ -3875,12 +3818,6 @@ void IndirectDeviceContext::CreateMonitor(unsigned int index) { void IndirectDeviceContext::AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent) { - // Only cleanup expired devices periodically, not on every assignment - static int assignmentCount = 0; - if (++assignmentCount % 10 == 0) { - CleanupExpiredDevices(); - } - auto Device = GetOrCreateDevice(RenderAdapter); if (!Device) { diff --git a/Virtual Display Driver (HDR)/MttVDD/Driver.h b/Virtual Display Driver (HDR)/MttVDD/Driver.h index b48b97f..a23f74a 100644 --- a/Virtual Display Driver (HDR)/MttVDD/Driver.h +++ b/Virtual Display Driver (HDR)/MttVDD/Driver.h @@ -118,6 +118,12 @@ namespace Microsoft void AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent); void UnassignSwapChain(IDDCX_MONITOR Monitor); + private: + // Keep Direct3D releases in WDF device cleanup, after the monitor workers stop. + std::mutex m_DeviceCacheMutex; + std::map, LuidComparator> m_DeviceCache; + std::shared_ptr GetOrCreateDevice(LUID RenderAdapter); + protected: WDFDEVICE m_WdfDevice; @@ -131,12 +137,6 @@ namespace Microsoft public: static const DISPLAYCONFIG_VIDEO_SIGNAL_INFO s_KnownMonitorModes[]; static std::vector s_KnownMonitorEdid; - - private: - static std::map, LuidComparator> s_DeviceCache; - static std::mutex s_DeviceCacheMutex; - static std::shared_ptr GetOrCreateDevice(LUID RenderAdapter); - static void CleanupExpiredDevices(); }; } }