mirror of
https://github.com/VirtualDrivers/Virtual-Display-Driver.git
synced 2026-12-06 07:01:23 +09:00
Keep swap-chain setup and cleanup off IddCx callbacks
This commit is contained in:
1 parent
e83c104700
commit
e0fc5effef
2 files changed
+226
-272
No files matched your search
@@ -3213,202 +3213,164 @@ HRESULT Direct3DDevice::Init()
|
||||
|
||||
#pragma region SwapChainProcessor
|
||||
|
||||
SwapChainProcessor::SwapChainProcessor(IDDCX_SWAPCHAIN hSwapChain, shared_ptr<Direct3DDevice> Device, HANDLE NewFrameEvent)
|
||||
: m_hSwapChain(hSwapChain), m_Device(Device), m_hAvailableBufferEvent(NewFrameEvent)
|
||||
SwapChainProcessor::SwapChainProcessor(IndirectDeviceContext& adapter, IDDCX_SWAPCHAIN swapChain, LUID renderAdapter)
|
||||
: m_Adapter(adapter), m_hSwapChain(swapChain), m_RenderAdapter(renderAdapter)
|
||||
{
|
||||
stringstream logStream;
|
||||
}
|
||||
|
||||
logStream << "Constructing SwapChainProcessor:"
|
||||
<< "\n SwapChain Handle: " << static_cast<void*>(hSwapChain)
|
||||
<< "\n Device Pointer: " << static_cast<void*>(Device.get())
|
||||
<< "\n NewFrameEvent Handle: " << NewFrameEvent;
|
||||
vddlog("d", logStream.str().c_str());
|
||||
|
||||
m_hTerminateEvent.Attach(CreateEvent(nullptr, FALSE, FALSE, nullptr));
|
||||
if (!m_hTerminateEvent.Get())
|
||||
bool SwapChainProcessor::Start(HANDLE newFrameEvent)
|
||||
{
|
||||
m_hTerminateEvent.Attach(CreateEvent(nullptr, TRUE, FALSE, nullptr));
|
||||
m_hFrameLoopStopped.Attach(CreateEvent(nullptr, TRUE, FALSE, nullptr));
|
||||
if (!m_hTerminateEvent.Get() || !m_hFrameLoopStopped.Get())
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Failed to create terminate event. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Terminate event created successfully.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (HasIddCx111Feature(IDDCX_DEVICE_FEATURES_1_11_UPDATE_REENCODE_COUNT) &&
|
||||
IDD_IS_FUNCTION_AVAILABLE(IddCxSwapChainUpdateStaticDesktopReencodeFrameCount)) {
|
||||
m_hReencodeChangeEvent.Attach(CreateEvent(nullptr, FALSE, FALSE, nullptr));
|
||||
if (m_hReencodeChangeEvent.Get()) {
|
||||
std::lock_guard<std::mutex> lock(g_ReencodeEventsMutex);
|
||||
g_ReencodeEvents.insert(m_hReencodeChangeEvent.Get());
|
||||
if (newFrameEvent && newFrameEvent != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
HANDLE frameEvent = nullptr;
|
||||
const HANDLE process = GetCurrentProcess();
|
||||
if (!DuplicateHandle(process, newFrameEvent, process, &frameEvent, SYNCHRONIZE, FALSE, 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_hAvailableBufferEvent.Attach(frameEvent);
|
||||
}
|
||||
|
||||
// Immediately create and run the swap-chain processing thread, passing 'this' as the thread parameter
|
||||
WdfObjectReferenceWithTag(m_hSwapChain, this);
|
||||
m_hThread.Attach(CreateThread(nullptr, 0, RunThread, this, 0, nullptr));
|
||||
if (!m_hThread.Get())
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Failed to create swap-chain processing thread. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Swap-chain processing thread created and started successfully.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
WdfObjectDereferenceWithTag(m_hSwapChain, this);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
SwapChainProcessor::~SwapChainProcessor()
|
||||
{
|
||||
if (m_hReencodeChangeEvent.Get()) {
|
||||
std::lock_guard<std::mutex> lock(g_ReencodeEventsMutex);
|
||||
g_ReencodeEvents.erase(m_hReencodeChangeEvent.Get());
|
||||
}
|
||||
|
||||
stringstream logStream;
|
||||
|
||||
logStream << "Destructing SwapChainProcessor:";
|
||||
|
||||
vddlog("d", logStream.str().c_str());
|
||||
// Alert the swap-chain processing thread to terminate
|
||||
//SetEvent(m_hTerminateEvent.Get()); changed for error handling + log purposes
|
||||
|
||||
if (SetEvent(m_hTerminateEvent.Get()))
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Terminate event signaled successfully.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Failed to signal terminate event. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
}
|
||||
|
||||
if (m_hThread.Get())
|
||||
{
|
||||
// Wait for the thread to terminate
|
||||
DWORD waitResult = WaitForSingleObject(m_hThread.Get(), INFINITE);
|
||||
switch (waitResult)
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
logStream.str("");
|
||||
logStream << "Thread terminated successfully.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
break;
|
||||
case WAIT_ABANDONED:
|
||||
logStream.str("");
|
||||
logStream << "Thread wait was abandoned. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
break;
|
||||
case WAIT_TIMEOUT:
|
||||
logStream.str("");
|
||||
logStream << "Thread wait timed out. This should not happen. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
break;
|
||||
default:
|
||||
logStream.str("");
|
||||
logStream << "Unexpected result from WaitForSingleObject. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "No valid thread handle to wait for.";
|
||||
vddlog("e", logStream.str().c_str());
|
||||
RequestStop();
|
||||
WaitForSingleObject(m_hThread.Get(), INFINITE);
|
||||
}
|
||||
}
|
||||
|
||||
DWORD CALLBACK SwapChainProcessor::RunThread(LPVOID Argument)
|
||||
void SwapChainProcessor::RequestStop()
|
||||
{
|
||||
stringstream logStream;
|
||||
SetEvent(m_hTerminateEvent.Get());
|
||||
}
|
||||
|
||||
logStream << "RunThread started. Argument: " << Argument;
|
||||
vddlog("d", logStream.str().c_str());
|
||||
bool SwapChainProcessor::StopRequested() const
|
||||
{
|
||||
const auto state = m_State.load();
|
||||
return state == State::UnassignedBeforeProcessing || state == State::Unassigned ||
|
||||
WaitForSingleObject(m_hTerminateEvent.Get(), 0) == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
reinterpret_cast<SwapChainProcessor*>(Argument)->Run();
|
||||
void SwapChainProcessor::StopForUnassign()
|
||||
{
|
||||
auto state = m_State.load();
|
||||
while (state == State::Initializing || state == State::Processing)
|
||||
{
|
||||
const auto unassigned = state == State::Initializing ? State::UnassignedBeforeProcessing : State::Unassigned;
|
||||
if (m_State.compare_exchange_weak(state, unassigned))
|
||||
{
|
||||
RequestStop();
|
||||
if (unassigned == State::UnassignedBeforeProcessing)
|
||||
{
|
||||
WdfObjectDereferenceWithTag(m_hSwapChain, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Allow an in-flight IddCx call to finish without joining a worker that may be blocked in cleanup.
|
||||
WaitForSingleObject(m_hFrameLoopStopped.Get(), 1000);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SwapChainProcessor::HasExited() const
|
||||
{
|
||||
return WaitForSingleObject(m_hThread.Get(), 0) == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
DWORD CALLBACK SwapChainProcessor::RunThread(LPVOID argument)
|
||||
{
|
||||
static_cast<SwapChainProcessor*>(argument)->Run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SwapChainProcessor::Run()
|
||||
{
|
||||
stringstream logStream;
|
||||
DWORD taskIndex = 0;
|
||||
const HANDLE task = AvSetMmThreadCharacteristicsW(L"Distribution", &taskIndex);
|
||||
|
||||
logStream << "Run method started.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
|
||||
// For improved performance, make use of the Multimedia Class Scheduler Service, which will intelligently
|
||||
// prioritize this thread for improved throughput in high CPU-load scenarios.
|
||||
DWORD AvTask = 0;
|
||||
HANDLE AvTaskHandle = AvSetMmThreadCharacteristicsW(L"Distribution", &AvTask);
|
||||
|
||||
if (AvTaskHandle)
|
||||
try
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Multimedia thread characteristics set successfully. AvTask: " << AvTask;
|
||||
vddlog("d", logStream.str().c_str());
|
||||
if (!StopRequested())
|
||||
{
|
||||
const auto device = m_Adapter.GetOrCreateDevice(m_RenderAdapter);
|
||||
auto state = State::Initializing;
|
||||
if (device && m_State.compare_exchange_strong(state, State::Processing) && !StopRequested())
|
||||
{
|
||||
if (HasIddCx111Feature(IDDCX_DEVICE_FEATURES_1_11_UPDATE_REENCODE_COUNT) &&
|
||||
IDD_IS_FUNCTION_AVAILABLE(IddCxSwapChainUpdateStaticDesktopReencodeFrameCount))
|
||||
{
|
||||
m_hReencodeChangeEvent.Attach(CreateEvent(nullptr, FALSE, FALSE, nullptr));
|
||||
if (m_hReencodeChangeEvent.Get())
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_ReencodeEventsMutex);
|
||||
g_ReencodeEvents.insert(m_hReencodeChangeEvent.Get());
|
||||
}
|
||||
}
|
||||
RunCore(*device);
|
||||
}
|
||||
else if (!device)
|
||||
{
|
||||
// Avoid repeatedly requesting a new swap chain while the render adapter is unavailable.
|
||||
WaitForSingleObject(m_hTerminateEvent.Get(), 250);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (const std::bad_alloc&)
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Failed to set multimedia thread characteristics. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
WaitForSingleObject(m_hTerminateEvent.Get(), 250);
|
||||
}
|
||||
|
||||
RunCore();
|
||||
const auto state = m_State.exchange(State::Releasing);
|
||||
SetEvent(m_hFrameLoopStopped.Get());
|
||||
|
||||
logStream.str("");
|
||||
logStream << "Core processing function RunCore() completed.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
// On unassign IddCx detaches the chain. When the worker exits independently, request a replacement.
|
||||
if (state != State::UnassignedBeforeProcessing)
|
||||
{
|
||||
if (state != State::Unassigned)
|
||||
{
|
||||
WdfObjectDelete(m_hSwapChain);
|
||||
}
|
||||
WdfObjectDereferenceWithTag(m_hSwapChain, this);
|
||||
}
|
||||
|
||||
// Always delete the swap-chain object when swap-chain processing loop terminates in order to kick the system to
|
||||
// provide a new swap-chain if necessary.
|
||||
/*
|
||||
WdfObjectDelete((WDFOBJECT)m_hSwapChain);
|
||||
m_hSwapChain = nullptr; added error handling in so its not called to delete swap chain if its not needed.
|
||||
*/
|
||||
if (m_hSwapChain)
|
||||
if (m_hReencodeChangeEvent.Get())
|
||||
{
|
||||
WdfObjectDelete((WDFOBJECT)m_hSwapChain);
|
||||
logStream.str("");
|
||||
logStream << "Swap-chain object deleted.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
m_hSwapChain = nullptr;
|
||||
std::lock_guard<std::mutex> lock(g_ReencodeEventsMutex);
|
||||
g_ReencodeEvents.erase(m_hReencodeChangeEvent.Get());
|
||||
}
|
||||
else
|
||||
|
||||
if (task)
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "No valid swap-chain object to delete.";
|
||||
vddlog("w", logStream.str().c_str());
|
||||
}
|
||||
/*
|
||||
AvRevertMmThreadCharacteristics(AvTaskHandle);
|
||||
*/ //error handling when reversing multimedia thread characteristics
|
||||
if (AvRevertMmThreadCharacteristics(AvTaskHandle))
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Multimedia thread characteristics reverted successfully.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
logStream.str("");
|
||||
logStream << "Failed to revert multimedia thread characteristics. GetLastError: " << GetLastError();
|
||||
vddlog("e", logStream.str().c_str());
|
||||
AvRevertMmThreadCharacteristics(task);
|
||||
}
|
||||
}
|
||||
|
||||
void SwapChainProcessor::RunCore()
|
||||
void SwapChainProcessor::RunCore(const Direct3DDevice& device)
|
||||
{
|
||||
if (StopRequested())
|
||||
{
|
||||
return;
|
||||
}
|
||||
stringstream logStream;
|
||||
DWORD retryDelay = 1;
|
||||
const DWORD maxRetryDelay = 100;
|
||||
@@ -3421,12 +3383,15 @@ void SwapChainProcessor::RunCore()
|
||||
if (d3d12SwapChain && HasIddCx111Feature(IDDCX_DEVICE_FEATURES_1_11_D3D12) &&
|
||||
IDD_IS_FUNCTION_AVAILABLE(IddCxSwapChainSetDevice2) &&
|
||||
IDD_IS_FUNCTION_AVAILABLE(IddCxSwapChainReleaseAndAcquireBuffer2)) {
|
||||
hr = D3D12CreateDevice(m_Device->Adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&d3d12Device));
|
||||
hr = D3D12CreateDevice(device.Adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&d3d12Device));
|
||||
if (SUCCEEDED(hr)) {
|
||||
D3D12_COMMAND_QUEUE_DESC desc{};
|
||||
desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
hr = d3d12Device->CreateCommandQueue(&desc, IID_PPV_ARGS(&d3d12Queue));
|
||||
}
|
||||
if (StopRequested()) {
|
||||
return;
|
||||
}
|
||||
if (SUCCEEDED(hr)) {
|
||||
IDARG_IN_SWAPCHAINSETDEVICE2 setDevice{};
|
||||
setDevice.Type = IDDCX_SWAPCHAIN_DEVICE_TYPE_D3D12;
|
||||
@@ -3442,9 +3407,15 @@ void SwapChainProcessor::RunCore()
|
||||
vddlog("w", "Direct3D 12 swap chain setup failed. Trying Direct3D 11.");
|
||||
}
|
||||
}
|
||||
if (StopRequested()) {
|
||||
return;
|
||||
}
|
||||
if (!d3d12Queue) {
|
||||
ComPtr<IDXGIDevice> dxgiDevice;
|
||||
hr = m_Device->Device.As(&dxgiDevice);
|
||||
hr = device.Device.As(&dxgiDevice);
|
||||
if (StopRequested()) {
|
||||
return;
|
||||
}
|
||||
if (SUCCEEDED(hr)) {
|
||||
IDARG_IN_SWAPCHAINSETDEVICE setDevice{dxgiDevice.Get()};
|
||||
hr = IddCxSwapChainSetDevice(m_hSwapChain, &setDevice);
|
||||
@@ -3467,8 +3438,8 @@ void SwapChainProcessor::RunCore()
|
||||
UINT lastReencodeRequest = initialDesktopReencodeFrameCount;
|
||||
HANDLE waitHandles[3]{m_hTerminateEvent.Get()};
|
||||
DWORD waitHandleCount = 1;
|
||||
if (m_hAvailableBufferEvent && m_hAvailableBufferEvent != INVALID_HANDLE_VALUE) {
|
||||
waitHandles[waitHandleCount++] = m_hAvailableBufferEvent;
|
||||
if (m_hAvailableBufferEvent.Get()) {
|
||||
waitHandles[waitHandleCount++] = m_hAvailableBufferEvent.Get();
|
||||
}
|
||||
if (m_hReencodeChangeEvent.Get()) {
|
||||
waitHandles[waitHandleCount++] = m_hReencodeChangeEvent.Get();
|
||||
@@ -3476,7 +3447,7 @@ void SwapChainProcessor::RunCore()
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (WaitForSingleObject(m_hTerminateEvent.Get(), 0) == WAIT_OBJECT_0) {
|
||||
if (StopRequested()) {
|
||||
break;
|
||||
}
|
||||
if (m_hReencodeChangeEvent.Get()) {
|
||||
@@ -3489,6 +3460,9 @@ void SwapChainProcessor::RunCore()
|
||||
}
|
||||
}
|
||||
|
||||
if (StopRequested()) {
|
||||
break;
|
||||
}
|
||||
IDXGIResource* dxgiSurface = nullptr;
|
||||
ID3D12Resource* d3d12Surface = nullptr;
|
||||
|
||||
@@ -3551,6 +3525,9 @@ void SwapChainProcessor::RunCore()
|
||||
if (dxgiSurface) {
|
||||
dxgiSurface->Release();
|
||||
}
|
||||
if (StopRequested()) {
|
||||
break;
|
||||
}
|
||||
hr = IddCxSwapChainFinishedProcessingFrame(m_hSwapChain);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
@@ -3787,20 +3764,16 @@ IndirectDeviceContext::IndirectDeviceContext(_In_ WDFDEVICE WdfDevice) :
|
||||
|
||||
IndirectDeviceContext::~IndirectDeviceContext()
|
||||
{
|
||||
stringstream logStream;
|
||||
std::map<IDDCX_MONITOR, std::unique_ptr<SwapChainProcessor>> processingThreads;
|
||||
|
||||
logStream << "Destroying IndirectDeviceContext. Releasing per-monitor processing threads.";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
|
||||
ProcessorList processors;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_ProcessingThreadsMutex);
|
||||
processingThreads.swap(m_ProcessingThreads);
|
||||
processors.splice(processors.end(), m_ProcessingThreads);
|
||||
processors.splice(processors.end(), m_RetiredProcessors);
|
||||
}
|
||||
for (auto& entry : processors)
|
||||
{
|
||||
entry.second.RequestStop();
|
||||
}
|
||||
|
||||
logStream.str("");
|
||||
logStream << "Released " << processingThreads.size() << " monitor processing thread(s).";
|
||||
vddlog("d", logStream.str().c_str());
|
||||
}
|
||||
|
||||
#define NUM_VIRTUAL_DISPLAYS 1 //What is this even used for ?? Its never referenced
|
||||
@@ -3974,109 +3947,82 @@ void IndirectDeviceContext::CreateMonitor(unsigned int index)
|
||||
}
|
||||
}
|
||||
|
||||
void IndirectDeviceContext::AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent)
|
||||
NTSTATUS IndirectDeviceContext::AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent)
|
||||
{
|
||||
auto Device = GetOrCreateDevice(RenderAdapter);
|
||||
if (!Device)
|
||||
{
|
||||
vddlog("e", "Failed to get or create Direct3DDevice, deleting existing swap-chain.");
|
||||
WdfObjectDelete(SwapChain);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::unique_ptr<SwapChainProcessor> previousProcessor;
|
||||
auto newProcessor = std::make_unique<SwapChainProcessor>(SwapChain, Device, NewFrameEvent);
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_ProcessingThreadsMutex);
|
||||
const auto previous = std::find_if(m_ProcessingThreads.begin(), m_ProcessingThreads.end(),
|
||||
[Monitor](const auto& entry) { return entry.first == Monitor; });
|
||||
try
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_ProcessingThreadsMutex);
|
||||
auto& processorSlot = m_ProcessingThreads[Monitor];
|
||||
previousProcessor = std::move(processorSlot);
|
||||
processorSlot = std::move(newProcessor);
|
||||
m_ProcessingThreads.emplace_front(std::piecewise_construct, std::forward_as_tuple(Monitor),
|
||||
std::forward_as_tuple(*this, SwapChain, RenderAdapter));
|
||||
}
|
||||
|
||||
if (previousProcessor) {
|
||||
vddlog("d", "Replaced existing processing thread for this monitor only.");
|
||||
catch (const std::bad_alloc&)
|
||||
{
|
||||
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
|
||||
}
|
||||
else {
|
||||
vddlog("d", "Created a new processing thread for this monitor.");
|
||||
if (!m_ProcessingThreads.front().second.Start(NewFrameEvent))
|
||||
{
|
||||
m_ProcessingThreads.pop_front();
|
||||
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
|
||||
}
|
||||
|
||||
if (hardwareCursor){
|
||||
HANDLE mouseEvent = CreateEventA(
|
||||
nullptr,
|
||||
false,
|
||||
false,
|
||||
"VirtualDisplayDriverMouse"
|
||||
);
|
||||
|
||||
if (!mouseEvent)
|
||||
{
|
||||
vddlog("e", "Failed to create mouse event. No hardware cursor supported!");
|
||||
return;
|
||||
}
|
||||
|
||||
IDDCX_CURSOR_CAPS cursorInfo = {};
|
||||
cursorInfo.Size = sizeof(cursorInfo);
|
||||
cursorInfo.ColorXorCursorSupport = IDDCX_XOR_CURSOR_SUPPORT_FULL;
|
||||
cursorInfo.AlphaCursorSupport = alphaCursorSupport;
|
||||
|
||||
cursorInfo.MaxX = CursorMaxX; //Apparently in most cases 128 is fine but for safe guarding we will go 512, older intel cpus may be limited to 64x64
|
||||
cursorInfo.MaxY = CursorMaxY;
|
||||
|
||||
//DirectXDevice->QueryMaxCursorSize(&cursorInfo.MaxX, &cursorInfo.MaxY); Experimental to get max cursor size - THIS IS NTO WORKING CODE
|
||||
|
||||
|
||||
IDARG_IN_SETUP_HWCURSOR hwCursor = {};
|
||||
hwCursor.CursorInfo = cursorInfo;
|
||||
hwCursor.hNewCursorDataAvailable = mouseEvent;
|
||||
|
||||
NTSTATUS Status = IddCxMonitorSetupHardwareCursor(
|
||||
Monitor,
|
||||
&hwCursor
|
||||
);
|
||||
|
||||
if (FAILED(Status))
|
||||
{
|
||||
CloseHandle(mouseEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
vddlog("d", "Hardware cursor setup completed successfully.");
|
||||
if (previous != m_ProcessingThreads.end())
|
||||
{
|
||||
previous->second.RequestStop();
|
||||
m_RetiredProcessors.splice(m_RetiredProcessors.end(), m_ProcessingThreads, previous);
|
||||
}
|
||||
else {
|
||||
vddlog("d", "Hardware cursor is disabled, Skipped creation.");
|
||||
}
|
||||
// At this point, the swap-chain is set up and the hardware cursor is enabled
|
||||
// Further swap-chain and cursor processing will occur in the new processing thread.
|
||||
ReapRetiredProcessors();
|
||||
}
|
||||
}
|
||||
|
||||
if (hardwareCursor)
|
||||
{
|
||||
HANDLE mouseEvent = CreateEventA(nullptr, FALSE, FALSE, "VirtualDisplayDriverMouse");
|
||||
if (mouseEvent)
|
||||
{
|
||||
IDARG_IN_SETUP_HWCURSOR cursor = {};
|
||||
cursor.CursorInfo.Size = sizeof(cursor.CursorInfo);
|
||||
cursor.CursorInfo.ColorXorCursorSupport = IDDCX_XOR_CURSOR_SUPPORT_FULL;
|
||||
cursor.CursorInfo.AlphaCursorSupport = alphaCursorSupport;
|
||||
cursor.CursorInfo.MaxX = CursorMaxX;
|
||||
cursor.CursorInfo.MaxY = CursorMaxY;
|
||||
cursor.hNewCursorDataAvailable = mouseEvent;
|
||||
if (!NT_SUCCESS(IddCxMonitorSetupHardwareCursor(Monitor, &cursor)))
|
||||
{
|
||||
CloseHandle(mouseEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
void IndirectDeviceContext::UnassignSwapChain(IDDCX_MONITOR Monitor)
|
||||
{
|
||||
std::unique_ptr<SwapChainProcessor> processorToStop;
|
||||
|
||||
ProcessorList stopped;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_ProcessingThreadsMutex);
|
||||
auto it = m_ProcessingThreads.find(Monitor);
|
||||
if (it != m_ProcessingThreads.end())
|
||||
const auto active = std::find_if(m_ProcessingThreads.begin(), m_ProcessingThreads.end(),
|
||||
[Monitor](const auto& entry) { return entry.first == Monitor; });
|
||||
if (active == m_ProcessingThreads.end())
|
||||
{
|
||||
processorToStop = std::move(it->second);
|
||||
m_ProcessingThreads.erase(it);
|
||||
return;
|
||||
}
|
||||
stopped.splice(stopped.end(), m_ProcessingThreads, active);
|
||||
}
|
||||
|
||||
if (processorToStop)
|
||||
stopped.front().second.StopForUnassign();
|
||||
{
|
||||
vddlog("i", "Unassigning swapchain for one monitor. Its processing thread will be stopped.");
|
||||
}
|
||||
else
|
||||
{
|
||||
vddlog("w", "UnassignSwapChain called for a monitor without an active processing thread.");
|
||||
std::lock_guard<std::mutex> lock(m_ProcessingThreadsMutex);
|
||||
m_RetiredProcessors.splice(m_RetiredProcessors.end(), stopped);
|
||||
ReapRetiredProcessors();
|
||||
}
|
||||
}
|
||||
|
||||
void IndirectDeviceContext::ReapRetiredProcessors()
|
||||
{
|
||||
m_RetiredProcessors.remove_if([](const auto& entry) { return entry.second.HasExited(); });
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region DDI Callbacks
|
||||
@@ -4313,27 +4259,16 @@ NTSTATUS VirtualDisplayDriverMonitorQueryModes(IDDCX_MONITOR MonitorObject, cons
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS VirtualDisplayDriverMonitorAssignSwapChain(IDDCX_MONITOR MonitorObject, const IDARG_IN_SETSWAPCHAIN* pInArgs)
|
||||
{
|
||||
stringstream logStream;
|
||||
logStream << "Assigning swap chain:"
|
||||
<< "\n hSwapChain: " << pInArgs->hSwapChain
|
||||
<< "\n RenderAdapterLuid: " << pInArgs->RenderAdapterLuid.LowPart << "-" << pInArgs->RenderAdapterLuid.HighPart
|
||||
<< "\n hNextSurfaceAvailable: " << pInArgs->hNextSurfaceAvailable;
|
||||
vddlog("d", logStream.str().c_str());
|
||||
auto* pContext = WdfObjectGet_IndirectDeviceContextWrapper(MonitorObject);
|
||||
pContext->pContext->AssignSwapChain(MonitorObject, pInArgs->hSwapChain, pInArgs->RenderAdapterLuid, pInArgs->hNextSurfaceAvailable);
|
||||
vddlog("d", "Swap chain assigned successfully.");
|
||||
return STATUS_SUCCESS;
|
||||
auto* context = WdfObjectGet_IndirectDeviceContextWrapper(MonitorObject);
|
||||
return context->pContext->AssignSwapChain(MonitorObject, pInArgs->hSwapChain,
|
||||
pInArgs->RenderAdapterLuid, pInArgs->hNextSurfaceAvailable);
|
||||
}
|
||||
|
||||
_Use_decl_annotations_
|
||||
NTSTATUS VirtualDisplayDriverMonitorUnassignSwapChain(IDDCX_MONITOR MonitorObject)
|
||||
{
|
||||
stringstream logStream;
|
||||
logStream << "Unassigning swap chain for monitor object: " << MonitorObject;
|
||||
vddlog("d", logStream.str().c_str());
|
||||
auto* pContext = WdfObjectGet_IndirectDeviceContextWrapper(MonitorObject);
|
||||
pContext->pContext->UnassignSwapChain(MonitorObject);
|
||||
vddlog("d", "Swap chain unassigned successfully.");
|
||||
auto* context = WdfObjectGet_IndirectDeviceContextWrapper(MonitorObject);
|
||||
context->pContext->UnassignSwapChain(MonitorObject);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <avrt.h>
|
||||
#include <wrl.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@@ -65,27 +67,38 @@ namespace Microsoft
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> DeviceContext;
|
||||
};
|
||||
|
||||
class IndirectDeviceContext;
|
||||
|
||||
/// <summary>
|
||||
/// Manages a thread that consumes buffers from an indirect display swap-chain object.
|
||||
/// </summary>
|
||||
class SwapChainProcessor
|
||||
{
|
||||
public:
|
||||
SwapChainProcessor(IDDCX_SWAPCHAIN hSwapChain, std::shared_ptr<Direct3DDevice> Device, HANDLE NewFrameEvent);
|
||||
SwapChainProcessor(IndirectDeviceContext& adapter, IDDCX_SWAPCHAIN swapChain, LUID renderAdapter);
|
||||
~SwapChainProcessor();
|
||||
bool Start(HANDLE newFrameEvent);
|
||||
void RequestStop();
|
||||
void StopForUnassign();
|
||||
bool HasExited() const;
|
||||
|
||||
private:
|
||||
static DWORD CALLBACK RunThread(LPVOID Argument);
|
||||
|
||||
void Run();
|
||||
void RunCore();
|
||||
void RunCore(const Direct3DDevice& device);
|
||||
bool StopRequested() const;
|
||||
|
||||
public:
|
||||
enum class State { Initializing, Processing, UnassignedBeforeProcessing, Unassigned, Releasing };
|
||||
|
||||
IndirectDeviceContext& m_Adapter;
|
||||
IDDCX_SWAPCHAIN m_hSwapChain;
|
||||
std::shared_ptr<Direct3DDevice> m_Device;
|
||||
HANDLE m_hAvailableBufferEvent;
|
||||
LUID m_RenderAdapter;
|
||||
std::atomic<State> m_State{State::Initializing};
|
||||
Microsoft::WRL::Wrappers::Event m_hAvailableBufferEvent;
|
||||
Microsoft::WRL::Wrappers::Thread m_hThread;
|
||||
Microsoft::WRL::Wrappers::Event m_hTerminateEvent;
|
||||
Microsoft::WRL::Wrappers::Event m_hFrameLoopStopped;
|
||||
Microsoft::WRL::Wrappers::Event m_hReencodeChangeEvent;
|
||||
};
|
||||
|
||||
@@ -116,10 +129,15 @@ namespace Microsoft
|
||||
|
||||
void CreateMonitor(unsigned int index);
|
||||
|
||||
void AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent);
|
||||
NTSTATUS AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent);
|
||||
void UnassignSwapChain(IDDCX_MONITOR Monitor);
|
||||
|
||||
private:
|
||||
friend class SwapChainProcessor;
|
||||
|
||||
using ProcessorList = std::list<std::pair<IDDCX_MONITOR, SwapChainProcessor>>;
|
||||
void ReapRetiredProcessors();
|
||||
|
||||
// Keep Direct3D releases in WDF device cleanup, after the monitor workers stop.
|
||||
std::mutex m_DeviceCacheMutex;
|
||||
std::map<LUID, std::shared_ptr<Direct3DDevice>, LuidComparator> m_DeviceCache;
|
||||
@@ -133,7 +151,8 @@ namespace Microsoft
|
||||
IDDCX_MONITOR m_Monitor;
|
||||
IDDCX_MONITOR m_Monitor2;
|
||||
|
||||
std::map<IDDCX_MONITOR, std::unique_ptr<SwapChainProcessor>> m_ProcessingThreads;
|
||||
ProcessorList m_ProcessingThreads;
|
||||
ProcessorList m_RetiredProcessors;
|
||||
std::mutex m_ProcessingThreadsMutex;
|
||||
|
||||
public:
|
||||
|
||||
Reference in new issue
Block a user