[idd] project: organize source layout

Move cadence and pipeline declarations under capture, and move local
named-pipe handling under ipc.

Flatten display contexts and rename files after their contained classes.
Keep transport headers limited to transport contracts and capabilities.

Consolidate CSwapChainProcessor definitions so each source file matches
the class it implements.
This commit is contained in:
Geoffrey McRae
2026-08-07 18:16:18 +10:00
parent 6725133375
commit 1de52f6e8a
34 changed files with 482 additions and 523 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,167 +0,0 @@
/**
* 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 <stdint.h>
#include "transport/TransportTypes.h"
class CFrameScheduler
{
public:
struct Schedule
{
uint32_t clientID;
uint32_t generation;
uint32_t epoch;
uint64_t period;
uint64_t targetSlack;
uint64_t deadline;
uint64_t forceTicket;
uint64_t republishTicket;
uint32_t deadlineSerial;
uint32_t deliveryDeadlineSerial;
bool phaseEligible;
};
private:
struct Client
{
uint32_t clientID;
uint32_t generation;
uint64_t period;
uint64_t targetSlack;
uint64_t expiry;
uint64_t nextDelivery;
uint32_t lastFeedbackDeadlineSerial;
uint32_t lastDeliveredFrameSerial;
bool subscribed;
bool ownerCapable;
bool subscriptionSeen;
bool active;
bool immediate;
bool deliveredFrameValid;
};
struct Publication
{
uint32_t generation;
uint32_t epoch;
uint32_t deadlineSerial;
uint32_t frameSerial;
uint64_t deadline;
bool committed;
bool completed;
bool phaseValid;
bool accepted;
};
static const unsigned PUBLICATION_HISTORY_SIZE = 128;
static const unsigned WORK_TIMING_HISTORY_SIZE = 32;
mutable SRWLOCK m_lock = SRWLOCK_INIT;
HANDLE m_wakeEvent = nullptr;
Client m_clients[TRANSPORT_MAX_CLIENTS] = {};
Schedule m_schedule = {};
bool m_scheduling = false;
uint32_t m_epoch = 0;
// A result acknowledges only the request tickets captured by its attempt.
uint64_t m_forceRequestTicket = 0;
uint64_t m_forceAckTicket = 0;
uint64_t m_republishRequestTicket = 0;
uint64_t m_republishAckTicket = 0;
uint64_t m_lastArrival = 0;
uint64_t m_guestPeriod = 0;
uint64_t m_workEstimate = 0;
uint64_t m_workTiming[WORK_TIMING_HISTORY_SIZE] = {};
unsigned m_workTimingCount = 0;
unsigned m_workTimingIndex = 0;
uint64_t m_nextDeadline = 0;
uint32_t m_deadlineSerial = 0;
int64_t m_pendingCorrection = 0;
Publication m_publications[PUBLICATION_HISTORY_SIZE] = {};
unsigned m_publicationIndex = 0;
int64_t m_lastPhaseError = 0;
uint64_t m_acquiredFrames = 0;
uint64_t m_skippedFrames = 0;
uint64_t m_publishedFrames = 0;
uint64_t m_lastLog = 0;
uint64_t m_lastLogAcquired = 0;
uint64_t m_lastLogSkipped = 0;
uint64_t m_lastLogPublished = 0;
Client * FindClient(uint32_t clientID);
Client * FindOrAllocateClient(uint32_t clientID);
Publication * FindPublication(const Schedule& schedule,
uint32_t frameSerial);
bool ElectOwner(uint64_t now, uint32_t resetClientID = 0);
bool ApplyFeedback(Client& client, const FrameScheduleUpdate& schedule);
void AdvanceCurrentDeadline();
void AdvanceDeadlineSerial(uint64_t count);
void AdvanceDeadline(uint64_t now);
static void AdvanceDelivery(Client& client, uint64_t now);
void WakePublisher() const;
public:
CFrameScheduler();
~CFrameScheduler();
static uint64_t Nanotime();
void Reset();
void UpdateSubscribers(const uint32_t * clientIDs, unsigned count,
const uint32_t * ownerClientIDs, unsigned ownerCount, uint64_t now);
bool UpdateSchedule(uint32_t sourceClientID,
const FrameScheduleUpdate& schedule, uint64_t now);
bool GetSchedule(Schedule& schedule) const;
HANDLE GetWakeEvent() const { return m_wakeEvent; }
void ObserveFrame(uint64_t now);
void ForceFrame();
bool GetPublishTarget(uint64_t now, uint64_t& target,
Schedule& schedule, bool& periodic, bool& republish);
void FrameMissed(const Schedule& schedule, uint64_t now, bool periodic);
void FrameSuperseded();
bool TryFrameSubmitted(const Schedule& schedule, uint32_t frameSerial);
void FramePublished(const Schedule& schedule, uint32_t frameSerial,
uint64_t now, bool periodic);
void FrameRetained(const Schedule& schedule, uint64_t now,
bool periodic);
void FrameRepublished(const Schedule& schedule, uint32_t frameSerial);
bool TryFrameCompleted(const Schedule& schedule, uint32_t frameSerial,
uint64_t completedAt);
unsigned GetSecondaryRecipients(const uint32_t * clientIDs,
unsigned count, uint32_t frameSerial, uint64_t now,
uint32_t * recipients) const;
bool GetSecondaryTarget(uint32_t frameSerial, uint64_t now,
const uint32_t * blockedClientIDs, unsigned blockedCount,
uint64_t& target) const;
void FrameDelivered(const uint32_t * clientIDs, unsigned count,
uint32_t frameSerial, uint64_t now);
void RequestRepublish();
void NotifyPublisher() const { WakePublisher(); }
void TryRecordFrameTiming(uint64_t duration);
void LogStatistics(uint64_t now);
};

View File

@@ -1,321 +0,0 @@
/**
* 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 "transport/CPipeServer.h"
#include "CDebug.h"
#include "display/device/CDeviceContext.h"
CPipeServer g_pipe;
bool CPipeServer::Init()
{
_DeInit();
m_pipe.Attach(CreateNamedPipeA(
LG_PIPE_NAME,
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1,
1024,
1024,
0,
NULL));
if (!m_pipe.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to create the named pipe");
return false;
}
m_signal.Attach(CreateEvent(NULL, TRUE, FALSE, NULL));
if (!m_signal.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to create pipe signal event");
return false;
}
m_running = true;
m_thread.Attach(CreateThread(
NULL,
0,
_pipeThread,
(LPVOID)this,
0,
NULL));
if (!m_thread.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Failed to create the pipe thread");
return false;
}
DEBUG_TRACE("Pipe Initialized");
return true;
}
void CPipeServer::_DeInit()
{
m_running = false;
m_connected = false;
if (m_signal.IsValid())
SetEvent(m_signal.Get());
if (m_thread.IsValid())
{
WaitForSingleObject(m_thread.Get(), INFINITE);
m_thread.Close();
}
if (m_pipe.IsValid())
{
FlushFileBuffers(m_pipe.Get());
m_pipe.Close();
}
m_signal.Close();
}
void CPipeServer::DeInit()
{
DEBUG_TRACE("Pipe Stopping");
_DeInit();
DEBUG_TRACE("Pipe Stopped");
}
void CPipeServer::Thread()
{
DEBUG_TRACE("Pipe thread started");
HandleT<EventTraits> ioEvent(CreateEvent(NULL, TRUE, FALSE, NULL));
if (!ioEvent.IsValid())
{
DEBUG_ERROR_HR(GetLastError(), "Can't create event for overlapped I/O!");
WaitForSingleObject(m_signal.Get(), 5000);
return;
}
while(m_running)
{
m_connected = false;
OVERLAPPED overlapped = { 0 };
overlapped.hEvent = ioEvent.Get();
if (!ConnectNamedPipe(m_pipe.Get(), &overlapped))
{
DWORD dwError = GetLastError();
switch (dwError) {
case ERROR_PIPE_CONNECTED:
break;
case ERROR_IO_PENDING:
{
HANDLE hWait[] = { ioEvent.Get(), m_signal.Get() };
switch (WaitForMultipleObjects(2, hWait, FALSE, INFINITE))
{
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0 + 1:
DEBUG_INFO("Connect interrupted by signal");
CancelIo(m_pipe.Get());
WaitForSingleObject(ioEvent.Get(), INFINITE);
continue;
}
break;
}
default:
DEBUG_ERROR_HR(dwError, "Error connecting to the named pipe");
goto end;
}
}
DEBUG_TRACE("Client connected");
m_connected = true;
for (const auto& msg : m_queue)
WriteMsg(msg);
m_queue.clear();
while (m_running && m_connected)
{
LGPipeMsg msg;
if (!ReadFile(m_pipe.Get(), &msg, sizeof(msg), NULL, &overlapped))
{
DWORD dwError = GetLastError();
if (dwError != ERROR_IO_PENDING)
{
DEBUG_ERROR_HR(dwError, "ReadFile Failed");
break;
}
HANDLE hWait[] = { ioEvent.Get(), m_signal.Get() };
switch (WaitForMultipleObjects(2, hWait, FALSE, INFINITE))
{
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0 + 1:
DEBUG_INFO("I/O interrupted by signal");
CancelIo(m_pipe.Get());
WaitForSingleObject(ioEvent.Get(), INFINITE);
continue;
}
}
DWORD bytesRead;
GetOverlappedResult(m_pipe.Get(), &overlapped, &bytesRead, TRUE);
if (bytesRead != sizeof(msg))
{
DEBUG_ERROR("Corrupted data, expected %lld bytes, read %lld bytes", sizeof msg, bytesRead);
break;
}
if (msg.size != sizeof(msg))
{
DEBUG_ERROR("Corrupted data, expected %lld bytes, actual message size: %lld bytes", sizeof msg, msg.size);
break;
}
switch (msg.type)
{
case LGPipeMsg::RELOADSETTINGS:
HandleReloadSettings();
break;
default:
DEBUG_ERROR("Unknown message type %d", msg.type);
break;
}
}
DEBUG_TRACE("Client disconnected");
DisconnectNamedPipe(m_pipe.Get());
if (m_running)
ResetEvent(m_signal.Get());
}
end:
m_running = false;
m_connected = false;
DEBUG_TRACE("Pipe thread shutdown");
}
void CPipeServer::WriteMsg(const LGPipeMsg & msg)
{
if (!m_connected)
{
// Not connected yet: keep only the latest message of each type. These are
// all latest-state-wins messages, so a burst (e.g. display mode changes
// while resizing) must collapse to the final state rather than replay every
// intermediate value when the helper reconnects.
for (auto & queued : m_queue)
if (queued.type == msg.type)
{
queued = msg;
return;
}
m_queue.push_back(msg);
return;
}
DWORD written;
if (!WriteFile(m_pipe.Get(), &msg, sizeof(msg), &written, NULL))
{
DWORD err = GetLastError();
if (err == ERROR_BROKEN_PIPE || err == ERROR_NO_DATA)
{
DEBUG_WARN_HR(err, "Client disconnected, failed to write");
m_connected = false;
SetEvent(m_signal.Get());
return;
}
DEBUG_WARN_HR(err, "WriteFile failed on the pipe");
return;
}
FlushFileBuffers(m_pipe.Get());
}
void CPipeServer::HandleReloadSettings()
{
DEBUG_INFO("Reloading settings");
AcquireSRWLockShared(&m_deviceContextLock);
if (m_deviceContext)
m_deviceContext->ReloadSettings();
ReleaseSRWLockShared(&m_deviceContextLock);
}
void CPipeServer::SetDeviceContext(CDeviceContext * context)
{
AcquireSRWLockExclusive(&m_deviceContextLock);
m_deviceContext = context;
ReleaseSRWLockExclusive(&m_deviceContextLock);
}
void CPipeServer::SetCursorPos(uint32_t x, uint32_t y)
{
// do not send cursor messages if we are not connected or they will end up queued
if (!m_connected)
return;
LGPipeMsg msg = {};
msg.size = sizeof(msg);
msg.type = LGPipeMsg::SETCURSORPOS;
msg.curorPos.x = x;
msg.curorPos.y = y;
WriteMsg(msg);
}
void CPipeServer::SetDisplayMode(
uint32_t width, uint32_t height, uint32_t refreshMilliHz)
{
LGPipeMsg msg = {};
msg.size = sizeof(msg);
msg.type = LGPipeMsg::SETDISPLAYMODE;
msg.displayMode.width = width;
msg.displayMode.height = height;
msg.displayMode.refreshMilliHz = refreshMilliHz;
WriteMsg(msg);
}
void CPipeServer::SetGPUStatus(bool software)
{
LGPipeMsg msg = {};
msg.size = sizeof(msg);
msg.type = LGPipeMsg::GPUSTATUS;
msg.gpuStatus.software = software;
WriteMsg(msg);
}
void CPipeServer::ResolutionRejected(uint32_t width, uint32_t height,
uint32_t requiredSizeMiB)
{
LGPipeMsg msg = {};
msg.size = sizeof(msg);
msg.type = LGPipeMsg::RESOLUTIONREJECTED;
msg.resolutionRejected.width = width;
msg.resolutionRejected.height = height;
msg.resolutionRejected.requiredSizeMiB = requiredSizeMiB;
WriteMsg(msg);
}

View File

@@ -1,76 +0,0 @@
/**
* 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 <stdint.h>
#include <wrl.h>
#include <vector>
#include "PipeMsg.h"
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace Microsoft::WRL::Wrappers::HandleTraits;
class CDeviceContext;
class CPipeServer
{
private:
HandleT<HANDLETraits> m_pipe;
HandleT<HANDLENullTraits> m_thread;
HandleT<EventTraits> m_signal;
std::vector<LGPipeMsg> m_queue;
bool m_running = false;
bool m_connected = false;
SRWLOCK m_deviceContextLock = SRWLOCK_INIT;
CDeviceContext * m_deviceContext = nullptr;
void _DeInit();
static DWORD WINAPI _pipeThread(LPVOID lpParam) { ((CPipeServer*)lpParam)->Thread(); return 0; }
void Thread();
void WriteMsg(const LGPipeMsg & msg);
void HandleReloadSettings();
public:
~CPipeServer() { DeInit(); }
bool Init();
void DeInit();
void SetDeviceContext(CDeviceContext * context);
void SetCursorPos(uint32_t x, uint32_t y);
void SetDisplayMode(
uint32_t width, uint32_t height, uint32_t refreshMilliHz);
void SetGPUStatus(bool software);
void ResolutionRejected(uint32_t width, uint32_t height,
uint32_t requiredSizeMiB);
};
extern CPipeServer g_pipe;

View File

@@ -21,36 +21,6 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
enum : unsigned
{
TRANSPORT_FRAME_QUEUE_LENGTH = 2,
TRANSPORT_FRAME_BUFFER_COUNT = 3,
TRANSPORT_MAX_CLIENTS = 8,
};
enum : uint32_t
{
FRAME_SCHEDULE_ACTIVE = 0x1,
FRAME_SCHEDULE_RELEASE = 0x2,
FRAME_SCHEDULE_RESET = 0x4,
FRAME_SCHEDULE_IMMEDIATE = 0x8,
};
struct FrameScheduleUpdate
{
uint32_t clientID;
uint32_t generation;
uint32_t flags;
uint64_t period;
uint64_t targetSlack;
int64_t phaseError;
uint32_t feedbackFrameSerial;
uint32_t feedbackScheduleEpoch;
uint32_t feedbackDeadlineSerial;
uint32_t lease;
};
struct DirectFrameBufferMemory
{

View File

@@ -21,9 +21,8 @@
#pragma once
#include "postprocess/D12FrameFormat.h"
#include "transport/CFrameScheduler.h"
#include "transport/FrameBufferTypes.h"
#include "transport/TransportTypes.h"
#include "capture/CFrameScheduler.h"
#include "transport/PreparedFrameBuffer.h"
#include <Windows.h>
#include <stddef.h>

View File

@@ -20,8 +20,8 @@
#pragma once
#include "transport/DirectFrameBufferMemory.h"
#include "transport/FrameMemoryLimits.h"
#include "transport/TransportTypes.h"
#include <stddef.h>
#include <stdint.h>

View File

@@ -35,11 +35,11 @@ struct LGMPBuffer
};
#pragma warning(pop)
static_assert(TRANSPORT_FRAME_QUEUE_LENGTH == LGMP_Q_FRAME_LEN,
static_assert(CAPTURE_PIPELINE_SLOTS == LGMP_Q_FRAME_LEN,
"The capture pipeline must match the LGMP frame queue");
static_assert(TRANSPORT_FRAME_BUFFER_COUNT == LGMP_Q_FRAME_BUFFER_LEN,
static_assert(CAPTURE_FRAME_BUFFERS == LGMP_Q_FRAME_BUFFER_LEN,
"The capture buffer pool must match the LGMP frame buffers");
static_assert(TRANSPORT_MAX_CLIENTS == LGMP_MAX_CLIENTS,
static_assert(CFrameScheduler::MAX_CLIENTS == LGMP_MAX_CLIENTS,
"The scheduler must support every LGMP client");
static const struct LGMPQueueConfig FRAME_QUEUE_CONFIG =

View File

@@ -29,7 +29,8 @@ extern "C" {
}
#include "common/KVMFR.h"
#include "transport/CFrameScheduler.h"
#include "capture/CFrameScheduler.h"
#include "capture/FramePipeline.h"
#include "transport/FrameMemoryLimits.h"
#include "transport/IFrameTransport.h"