mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-08-09 16:51:31 +00:00
[idd] project: organize driver sources by responsibility
Group the IDD sources and Visual Studio filters by subsystem. Split the device and swap-chain implementations into focused units, rename the context classes, and reduce header coupling.
This commit is contained in:
124
idd/LGIdd/display/monitor/Context.cpp
Normal file
124
idd/LGIdd/display/monitor/Context.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "display/monitor/Context.h"
|
||||
#include "display/device/CDeviceContext.h"
|
||||
#include "capture/CSwapChainProcessor.h"
|
||||
#include "d3d/CD3D11Device.h"
|
||||
#include "CDebug.h"
|
||||
|
||||
CMonitorContext::CMonitorContext(
|
||||
_In_ IDDCX_MONITOR monitor, CDeviceContext * device) :
|
||||
m_monitor(monitor),
|
||||
m_devContext(device)
|
||||
{
|
||||
}
|
||||
|
||||
CMonitorContext::~CMonitorContext()
|
||||
{
|
||||
UnassignSwapChain();
|
||||
m_devContext->OnMonitorDestroyed(m_monitor);
|
||||
}
|
||||
|
||||
NTSTATUS CMonitorContext::AssignSwapChain(
|
||||
IDDCX_SWAPCHAIN swapChain, LUID renderAdapter, HANDLE newFrameEvent)
|
||||
{
|
||||
std::lock_guard<std::mutex> assignGuard(m_assignMutex);
|
||||
|
||||
// Finish tearing down the previous assignment before reserving a generation
|
||||
// for the new one. Deleting the old processor can itself cause IddCx to
|
||||
// re-enter UnassignSwapChain, and that old callback must happen before the
|
||||
// new generation is established.
|
||||
DetachSwapChain();
|
||||
|
||||
const UINT64 assignmentGeneration =
|
||||
m_assignmentGeneration.fetch_add(1, std::memory_order_acq_rel) + 1;
|
||||
|
||||
// Build the D3D11 device into a local so the member is never observed
|
||||
// half-constructed. The worker binds it before performing the expensive
|
||||
// D3D12, LGMP and post-processing initialization.
|
||||
auto dx11Device = std::make_shared<CD3D11Device>(renderAdapter);
|
||||
const HRESULT initStatus = dx11Device->Init();
|
||||
if (FAILED(initStatus))
|
||||
{
|
||||
DEBUG_ERROR_HR(initStatus, "Failed to initialize D3D11 device");
|
||||
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
|
||||
}
|
||||
|
||||
AcquireSRWLockExclusive(&m_lock);
|
||||
if (!IsAssignmentCurrent(assignmentGeneration))
|
||||
{
|
||||
ReleaseSRWLockExclusive(&m_lock);
|
||||
DEBUG_INFO("Swap chain assignment canceled before processor startup");
|
||||
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
|
||||
}
|
||||
|
||||
// Publish the assignment atomically with starting its worker. An unassign
|
||||
// now blocks on m_lock until m_swapChain exists, at which point it can
|
||||
// signal and join the processor normally.
|
||||
m_devContext->OnSwapChainAssigned();
|
||||
m_dx11Device = std::move(dx11Device);
|
||||
m_swapChain.reset(new CSwapChainProcessor(
|
||||
this, assignmentGeneration, m_monitor, m_devContext, swapChain,
|
||||
renderAdapter, m_dx11Device, newFrameEvent));
|
||||
if (!m_swapChain->Start())
|
||||
{
|
||||
auto processor = std::move(m_swapChain);
|
||||
dx11Device = std::move(m_dx11Device);
|
||||
ReleaseSRWLockExclusive(&m_lock);
|
||||
processor.reset();
|
||||
dx11Device.reset();
|
||||
m_devContext->OnSwapChainReleased();
|
||||
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_lock);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
void CMonitorContext::DetachSwapChain()
|
||||
{
|
||||
// Invalidate setup in progress before waiting for m_lock. This also lets a
|
||||
// worker about to call SetDevice observe an unassign whose callback is
|
||||
// blocked waiting for the processor to be published.
|
||||
m_assignmentGeneration.fetch_add(1, std::memory_order_acq_rel);
|
||||
|
||||
// Detach under the lock, then destroy outside it. Destroying the processor
|
||||
// joins its worker thread, whose teardown (WdfObjectDelete) re-enters this
|
||||
// method on another thread - holding the lock across that would deadlock.
|
||||
std::unique_ptr<CSwapChainProcessor> processor;
|
||||
std::shared_ptr<CD3D11Device> dx11Device;
|
||||
|
||||
AcquireSRWLockExclusive(&m_lock);
|
||||
processor = std::move(m_swapChain);
|
||||
dx11Device = std::move(m_dx11Device);
|
||||
ReleaseSRWLockExclusive(&m_lock);
|
||||
|
||||
const bool hadSwapChain = !!processor;
|
||||
processor.reset();
|
||||
dx11Device.reset();
|
||||
|
||||
if (hadSwapChain)
|
||||
m_devContext->OnSwapChainReleased();
|
||||
}
|
||||
|
||||
void CMonitorContext::UnassignSwapChain()
|
||||
{
|
||||
DetachSwapChain();
|
||||
}
|
||||
89
idd/LGIdd/display/monitor/Context.h
Normal file
89
idd/LGIdd/display/monitor/Context.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
#include <wdf.h>
|
||||
#include <IddCx.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
struct CD3D11Device;
|
||||
class CDeviceContext;
|
||||
class CSwapChainProcessor;
|
||||
|
||||
class CMonitorContext
|
||||
{
|
||||
private:
|
||||
IDDCX_MONITOR m_monitor;
|
||||
|
||||
// Guards the swap chain and device pointers. Assign and unassign can run
|
||||
// concurrently (an unassign triggered by the worker's WdfObjectDelete can
|
||||
// race the next assign), and shared_ptr copy/reset is not thread safe.
|
||||
SRWLOCK m_lock = SRWLOCK_INIT;
|
||||
|
||||
// IddCx can issue a replacement assignment before an earlier assignment
|
||||
// has finished creating its devices. Serialize those expensive setup paths
|
||||
// while still allowing UnassignSwapChain to cancel the active one.
|
||||
std::mutex m_assignMutex;
|
||||
std::shared_ptr<CD3D11Device> m_dx11Device;
|
||||
|
||||
CDeviceContext * m_devContext;
|
||||
std::unique_ptr<CSwapChainProcessor> m_swapChain;
|
||||
|
||||
// Incremented whenever the current assignment is replaced or unassigned.
|
||||
// Device creation is performed outside m_lock, so this lets an unassign
|
||||
// cancel that work before a processor is started on a stale swap chain.
|
||||
std::atomic<UINT64> m_assignmentGeneration = 0;
|
||||
|
||||
void DetachSwapChain();
|
||||
|
||||
public:
|
||||
CMonitorContext(
|
||||
_In_ IDDCX_MONITOR monitor, CDeviceContext * device);
|
||||
|
||||
virtual ~CMonitorContext();
|
||||
|
||||
NTSTATUS AssignSwapChain(
|
||||
IDDCX_SWAPCHAIN swapChain, LUID renderAdapter, HANDLE newFrameEvent);
|
||||
void UnassignSwapChain();
|
||||
bool IsAssignmentCurrent(UINT64 generation) const
|
||||
{
|
||||
return m_assignmentGeneration.load(std::memory_order_acquire) == generation;
|
||||
}
|
||||
|
||||
CDeviceContext * GetDeviceContext() { return m_devContext; }
|
||||
};
|
||||
|
||||
struct CMonitorContextWrapper
|
||||
{
|
||||
CMonitorContext * context;
|
||||
|
||||
void Cleanup()
|
||||
{
|
||||
delete context;
|
||||
context = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
WDF_DECLARE_CONTEXT_TYPE(CMonitorContextWrapper);
|
||||
Reference in New Issue
Block a user