[idd] common: use CSRWLock throughout the drivers

Make CSRWLock own the native lock and provide scoped guards for shared,
exclusive, early-unlock, and non-blocking use.

Replace direct SRW lock management throughout the IDD, input driver,
and helper while preserving the existing lock scopes.
This commit is contained in:
Geoffrey McRae
2026-08-11 22:06:31 +10:00
parent 2543366f5f
commit 61a49ebaba
37 changed files with 576 additions and 525 deletions

View File

@@ -30,7 +30,7 @@
CFrameProcessor::CFrameProcessor(IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent) :
CSRWLock * pipelineLock, HANDLE terminateEvent) :
m_transport(transport),
m_dx12(std::move(dx12)),
m_postProcessors(postProcessors),
@@ -53,12 +53,11 @@ void CFrameProcessor::Reset()
void CFrameProcessor::Invalidate()
{
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
m_previousDamageCount = 0;
m_hasPendingDamage = true;
m_pendingDamageCount = 0;
SetFullDamageLocked();
ReleaseSRWLockExclusive(&m_damageLock);
}
void CFrameProcessor::ResetPipeline()
@@ -69,21 +68,19 @@ void CFrameProcessor::ResetPipeline()
void CFrameProcessor::AccumulateDamage(
const RECT dirtyRects[], unsigned count)
{
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
CFrameProcessorUtil::AccumulateDamage(
m_pendingDamage, &m_pendingDamageCount, &m_hasPendingDamage,
dirtyRects, count);
AccumulateDamageLocked(dirtyRects, count);
ReleaseSRWLockExclusive(&m_damageLock);
}
void CFrameProcessor::SetFullDamage()
{
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
m_hasPendingDamage = true;
m_pendingDamageCount = 0;
SetFullDamageLocked();
ReleaseSRWLockExclusive(&m_damageLock);
}
void CFrameProcessor::AccumulateDamageLocked(
@@ -97,16 +94,15 @@ void CFrameProcessor::SetFullDamageLocked()
bool CFrameProcessor::HasPendingDamage() const
{
AcquireSRWLockShared(&m_damageLock);
CSRWSharedLock lock(m_damageLock);
const bool result = m_hasPendingDamage;
ReleaseSRWLockShared(&m_damageLock);
return result;
}
bool CFrameProcessor::TakePendingDamage(
RECT dirtyRects[], unsigned * count)
{
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
const bool hasDamage = m_hasPendingDamage;
*count = hasDamage ? m_pendingDamageCount : 0;
if (*count)
@@ -114,7 +110,6 @@ bool CFrameProcessor::TakePendingDamage(
*count * sizeof(*dirtyRects));
m_hasPendingDamage = false;
m_pendingDamageCount = 0;
ReleaseSRWLockExclusive(&m_damageLock);
return hasDamage;
}
@@ -124,40 +119,37 @@ void CFrameProcessor::RestorePendingDamage(
if (!hasDamage)
return;
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
CFrameProcessorUtil::AccumulateDamage(
m_pendingDamage, &m_pendingDamageCount, &m_hasPendingDamage,
dirtyRects, count);
ReleaseSRWLockExclusive(&m_damageLock);
}
void CFrameProcessor::CommitDamage(
const RECT dirtyRects[], unsigned count)
{
AcquireSRWLockExclusive(&m_damageLock);
CSRWExclusiveLock lock(m_damageLock);
m_previousDamageCount = count;
if (count)
memcpy(m_previousDamage, dirtyRects,
count * sizeof(*m_previousDamage));
ReleaseSRWLockExclusive(&m_damageLock);
}
void CFrameProcessor::GetPreviousDamage(
RECT dirtyRects[], unsigned * count) const
{
AcquireSRWLockShared(&m_damageLock);
CSRWSharedLock lock(m_damageLock);
*count = m_previousDamageCount;
if (*count)
memcpy(dirtyRects, m_previousDamage,
*count * sizeof(*dirtyRects));
ReleaseSRWLockShared(&m_damageLock);
}
std::unique_ptr<CFrameProcessor> CreateFrameProcessor(
bool software, IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent)
CSRWLock * pipelineLock, HANDLE terminateEvent)
{
std::unique_ptr<CFrameProcessor> processor;
if (software)

View File

@@ -20,6 +20,7 @@
#pragma once
#include "CSRWLock.h"
#include "d3d/CD3D12Device.h"
#include "capture/CFrameBufferPool.h"
#include "d3d/CInteropResource.h"
@@ -51,17 +52,17 @@ protected:
IFrameTransport * m_transport;
std::shared_ptr<CD3D12Device> m_dx12;
CPostProcessor * m_postProcessors;
SRWLOCK * m_pipelineLock;
CSRWLock * m_pipelineLock;
HANDLE m_terminateEvent;
CFrameBufferPool m_frameBuffers;
Wrappers::Event m_readyEvent;
mutable SRWLOCK m_damageLock = SRWLOCK_INIT;
RECT m_previousDamage[LG_MAX_DIRTY_RECTS] = {};
unsigned m_previousDamageCount = 0;
RECT m_pendingDamage[LG_MAX_DIRTY_RECTS] = {};
unsigned m_pendingDamageCount = 0;
bool m_hasPendingDamage = true;
mutable CSRWLock m_damageLock;
RECT m_previousDamage[LG_MAX_DIRTY_RECTS] = {};
unsigned m_previousDamageCount = 0;
RECT m_pendingDamage[LG_MAX_DIRTY_RECTS] = {};
unsigned m_pendingDamageCount = 0;
bool m_hasPendingDamage = true;
bool HasPendingDamage() const;
bool TakePendingDamage(RECT dirtyRects[], unsigned * count);
@@ -77,7 +78,7 @@ public:
CFrameProcessor(IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent);
CSRWLock * pipelineLock, HANDLE terminateEvent);
virtual ~CFrameProcessor() = default;
virtual bool IsValid() const;
@@ -99,4 +100,4 @@ std::unique_ptr<CFrameProcessor> CreateFrameProcessor(
bool software, IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent);
CSRWLock * pipelineLock, HANDLE terminateEvent);

View File

@@ -247,7 +247,7 @@ bool CFrameScheduler::ElectOwner(uint64_t now, uint32_t resetClientID)
void CFrameScheduler::Reset()
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
for (Client& client : m_clients)
client = {};
m_schedule = {};
@@ -278,7 +278,7 @@ void CFrameScheduler::Reset()
m_lastLogAcquired = 0;
m_lastLogSkipped = 0;
m_lastLogPublished = 0;
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
WakePublisher();
}
@@ -286,7 +286,7 @@ void CFrameScheduler::UpdateSubscribers(const uint32_t * clientIDs,
unsigned count, const uint32_t * ownerClientIDs, unsigned ownerCount,
uint64_t now)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
uint32_t oldClientIDs [MAX_CLIENTS] = {};
bool wasSubscribed [MAX_CLIENTS] = {};
@@ -343,7 +343,7 @@ void CFrameScheduler::UpdateSubscribers(const uint32_t * clientIDs,
}
const bool changed = ElectOwner(now) || subscribersChanged;
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (changed)
WakePublisher();
}
@@ -363,7 +363,7 @@ bool CFrameScheduler::UpdateSchedule(uint32_t sourceClientID,
if (schedule.flags & FRAME_SCHEDULE_RELEASE)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
Client * client = FindClient(schedule.clientID);
bool wake = false;
if (client)
@@ -375,7 +375,7 @@ bool CFrameScheduler::UpdateSchedule(uint32_t sourceClientID,
wake = true;
wake |= ElectOwner(now);
}
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (wake)
WakePublisher();
return true;
@@ -390,14 +390,11 @@ bool CFrameScheduler::UpdateSchedule(uint32_t sourceClientID,
schedule.lease < MIN_LEASE_MS || schedule.lease > MAX_LEASE_MS)
return false;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
Client * client = FindOrAllocateClient(schedule.clientID);
if (!client)
{
ReleaseSRWLockExclusive(&m_lock);
return false;
}
const bool explicitReset =
(schedule.flags & FRAME_SCHEDULE_RESET) != 0;
@@ -432,7 +429,7 @@ bool CFrameScheduler::UpdateSchedule(uint32_t sourceClientID,
wake = true;
}
wake |= ApplyFeedback(*client, schedule);
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (wake)
WakePublisher();
return true;
@@ -555,18 +552,17 @@ void CFrameScheduler::AdvanceDelivery(Client& client, uint64_t now)
bool CFrameScheduler::GetSchedule(Schedule& schedule) const
{
AcquireSRWLockShared(&m_lock);
CSRWSharedLock lock(m_lock);
const bool result = m_scheduling;
if (result)
schedule = m_schedule;
ReleaseSRWLockShared(&m_lock);
return result;
}
void CFrameScheduler::ObserveFrame(uint64_t now)
{
bool wake = false;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
++m_acquiredFrames;
if (m_lastArrival && now > m_lastArrival)
{
@@ -587,16 +583,17 @@ void CFrameScheduler::ObserveFrame(uint64_t now)
}
}
m_lastArrival = now;
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (wake)
WakePublisher();
}
void CFrameScheduler::ForceFrame()
{
AcquireSRWLockExclusive(&m_lock);
++m_forceRequestTicket;
ReleaseSRWLockExclusive(&m_lock);
{
CSRWExclusiveLock lock(m_lock);
++m_forceRequestTicket;
}
WakePublisher();
}
@@ -607,12 +604,9 @@ bool CFrameScheduler::GetPublishTarget(uint64_t now, uint64_t& target,
schedule = {};
periodic = false;
republish = false;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (!m_scheduling)
{
ReleaseSRWLockExclusive(&m_lock);
return true;
}
schedule = m_schedule;
schedule.forceTicket = m_forceRequestTicket;
@@ -634,7 +628,6 @@ bool CFrameScheduler::GetPublishTarget(uint64_t now, uint64_t& target,
target = periodicTarget;
schedule.deliveryDeadlineSerial = periodic ? m_deadlineSerial : 0;
schedule.phaseEligible = periodic;
ReleaseSRWLockExclusive(&m_lock);
return true;
}
@@ -642,7 +635,6 @@ bool CFrameScheduler::GetPublishTarget(uint64_t now, uint64_t& target,
schedule.deliveryDeadlineSerial = m_deadlineSerial;
schedule.phaseEligible = true;
target = periodicTarget;
ReleaseSRWLockExclusive(&m_lock);
return true;
}
@@ -652,7 +644,7 @@ void CFrameScheduler::FrameMissed(const Schedule& schedule, uint64_t now,
if (!periodic)
return;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (m_scheduling && schedule.clientID == m_schedule.clientID &&
schedule.generation == m_schedule.generation &&
schedule.epoch == m_schedule.epoch &&
@@ -666,14 +658,12 @@ void CFrameScheduler::FrameMissed(const Schedule& schedule, uint64_t now,
if (client)
client->nextDelivery = m_nextDeadline;
}
ReleaseSRWLockExclusive(&m_lock);
}
void CFrameScheduler::FrameSuperseded()
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
++m_skippedFrames;
ReleaseSRWLockExclusive(&m_lock);
}
bool CFrameScheduler::TryFrameSubmitted(const Schedule& schedule,
@@ -683,7 +673,8 @@ bool CFrameScheduler::TryFrameSubmitted(const Schedule& schedule,
schedule.deliveryDeadlineSerial != schedule.deadlineSerial)
return false;
if (!TryAcquireSRWLockExclusive(&m_lock))
auto lock = CSRWExclusiveLock::Try(m_lock);
if (!lock)
return false;
bool registered = false;
@@ -704,14 +695,13 @@ bool CFrameScheduler::TryFrameSubmitted(const Schedule& schedule,
publication.committed = true;
registered = true;
}
ReleaseSRWLockExclusive(&m_lock);
return registered;
}
void CFrameScheduler::FramePublished(const Schedule& schedule,
uint32_t frameSerial, uint64_t now, bool periodic)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (m_scheduling && schedule.clientID == m_schedule.clientID &&
schedule.generation == m_schedule.generation &&
schedule.epoch == m_schedule.epoch &&
@@ -744,14 +734,13 @@ void CFrameScheduler::FramePublished(const Schedule& schedule,
if (client)
client->nextDelivery = m_nextDeadline;
}
ReleaseSRWLockExclusive(&m_lock);
}
void CFrameScheduler::FrameRetained(const Schedule& schedule,
uint64_t now, bool periodic)
{
bool wake = false;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (m_scheduling && schedule.clientID == m_schedule.clientID &&
schedule.generation == m_schedule.generation &&
schedule.epoch == m_schedule.epoch &&
@@ -779,7 +768,7 @@ void CFrameScheduler::FrameRetained(const Schedule& schedule,
if (client)
client->nextDelivery = m_nextDeadline;
}
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (wake)
WakePublisher();
@@ -793,7 +782,8 @@ bool CFrameScheduler::TryFrameCompleted(const Schedule& schedule,
!schedule.deadline)
return false;
if (!TryAcquireSRWLockExclusive(&m_lock))
auto lock = CSRWExclusiveLock::Try(m_lock);
if (!lock)
return false;
bool phaseValid = false;
@@ -809,14 +799,13 @@ bool CFrameScheduler::TryFrameCompleted(const Schedule& schedule,
phaseValid = publication->phaseValid;
}
}
ReleaseSRWLockExclusive(&m_lock);
return phaseValid;
}
void CFrameScheduler::FrameRepublished(const Schedule& schedule,
uint32_t frameSerial)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (m_scheduling && schedule.clientID == m_schedule.clientID &&
schedule.generation == m_schedule.generation &&
schedule.epoch == m_schedule.epoch)
@@ -835,13 +824,12 @@ void CFrameScheduler::FrameRepublished(const Schedule& schedule,
}
++m_publishedFrames;
}
ReleaseSRWLockExclusive(&m_lock);
}
void CFrameScheduler::RequestRepublish()
{
bool wake = false;
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
Client * client = FindClient(m_schedule.clientID);
if (m_scheduling && client && !client->deliveredFrameValid &&
m_republishRequestTicket == m_republishAckTicket)
@@ -849,7 +837,7 @@ void CFrameScheduler::RequestRepublish()
++m_republishRequestTicket;
wake = true;
}
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
if (wake)
WakePublisher();
@@ -859,7 +847,7 @@ unsigned CFrameScheduler::GetSecondaryRecipients(
const uint32_t * clientIDs, unsigned count, uint32_t frameSerial,
uint64_t now, uint32_t * recipients) const
{
AcquireSRWLockShared(&m_lock);
CSRWSharedLock lock(m_lock);
unsigned recipientCount = 0;
for (unsigned i = 0; i < count; ++i)
{
@@ -891,7 +879,6 @@ unsigned CFrameScheduler::GetSecondaryRecipients(
if (due)
recipients[recipientCount++] = clientIDs[i];
}
ReleaseSRWLockShared(&m_lock);
return recipientCount;
}
@@ -902,7 +889,7 @@ bool CFrameScheduler::GetSecondaryTarget(uint32_t frameSerial,
bool found = false;
target = now;
AcquireSRWLockShared(&m_lock);
CSRWSharedLock lock(m_lock);
for (const Client& client : m_clients)
{
if (!client.clientID || !client.subscribed ||
@@ -935,14 +922,13 @@ bool CFrameScheduler::GetSecondaryTarget(uint32_t frameSerial,
found = true;
}
}
ReleaseSRWLockShared(&m_lock);
return found;
}
void CFrameScheduler::FrameDelivered(const uint32_t * clientIDs,
unsigned count, uint32_t frameSerial, uint64_t now)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
for (unsigned i = 0; i < count; ++i)
{
Client * client = FindClient(clientIDs[i]);
@@ -972,17 +958,13 @@ void CFrameScheduler::FrameDelivered(const uint32_t * clientIDs,
if (!immediate || periodic)
AdvanceDelivery(*client, now);
}
ReleaseSRWLockExclusive(&m_lock);
}
void CFrameScheduler::LogStatistics(uint64_t now)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (!m_scheduling || now - m_lastLog < LOG_INTERVAL_NS)
{
ReleaseSRWLockExclusive(&m_lock);
return;
}
const uint32_t clientID = m_schedule.clientID;
const uint64_t period = m_schedule.period;
@@ -997,7 +979,7 @@ void CFrameScheduler::LogStatistics(uint64_t now)
m_lastLogAcquired = m_acquiredFrames;
m_lastLogSkipped = m_skippedFrames;
m_lastLogPublished = m_publishedFrames;
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
const double acquiredRate =
static_cast<double>(acquired) * 1000000000.0 / elapsed;
@@ -1019,7 +1001,8 @@ void CFrameScheduler::TryRecordFrameTiming(uint64_t duration)
if (!duration)
return;
if (!TryAcquireSRWLockExclusive(&m_lock))
auto lock = CSRWExclusiveLock::Try(m_lock);
if (!lock)
return;
m_workTiming[m_workTimingIndex] = duration;
@@ -1052,5 +1035,4 @@ void CFrameScheduler::TryRecordFrameTiming(uint64_t duration)
m_workEstimate = sorted[m_workTimingCount - discarded - 1];
}
ReleaseSRWLockExclusive(&m_lock);
}

View File

@@ -20,6 +20,8 @@
#pragma once
#include "CSRWLock.h"
#include <Windows.h>
#include <stdint.h>
@@ -100,12 +102,12 @@ private:
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[MAX_CLIENTS] = {};
Schedule m_schedule = {};
bool m_scheduling = false;
uint32_t m_epoch = 0;
mutable CSRWLock m_lock;
HANDLE m_wakeEvent = nullptr;
Client m_clients[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;

View File

@@ -35,21 +35,20 @@ static_assert(CAPTURE_PIPELINE_SLOTS == 2,
class CPublishPending
{
private:
SRWLOCK * m_lock;
CSRWLock& m_lock;
bool * m_pending;
HANDLE m_event;
bool m_active = true;
public:
CPublishPending(SRWLOCK * lock, bool * pending, HANDLE event) :
m_lock(lock),
CPublishPending(CSRWLock& stateLock, bool * pending, HANDLE event) :
m_lock(stateLock),
m_pending(pending),
m_event(event)
{
AcquireSRWLockExclusive(m_lock);
CSRWExclusiveLock guard(m_lock);
*m_pending = true;
ResetEvent(m_event);
ReleaseSRWLockExclusive(m_lock);
}
~CPublishPending()
@@ -62,10 +61,11 @@ public:
if (!m_active)
return;
AcquireSRWLockExclusive(m_lock);
*m_pending = false;
SetEvent(m_event);
ReleaseSRWLockExclusive(m_lock);
{
CSRWExclusiveLock lock(m_lock);
*m_pending = false;
SetEvent(m_event);
}
m_active = false;
}
};
@@ -73,7 +73,7 @@ public:
CHardwareFrameProcessor::CHardwareFrameProcessor(
IFrameTransport * transport, std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent) :
CSRWLock * pipelineLock, HANDLE terminateEvent) :
CFrameProcessor(transport, std::move(dx12), postProcessors,
pipelineLock, terminateEvent)
{
@@ -116,15 +116,17 @@ void CHardwareFrameProcessor::AccumulateDamageLocked(
void CHardwareFrameProcessor::ResetCandidates()
{
AcquireSRWLockExclusive(&m_candidateLock);
for (FrameCandidate& candidate : m_candidates)
candidate = {};
ReleaseSRWLockExclusive(&m_candidateLock);
{
CSRWExclusiveLock lock(m_candidateLock);
for (FrameCandidate& candidate : m_candidates)
candidate = {};
}
AcquireSRWLockExclusive(&m_damageLock);
for (CandidateDamageTail& tail : m_candidateDamageTail)
tail = {};
ReleaseSRWLockExclusive(&m_damageLock);
{
CSRWExclusiveLock lock(m_damageLock);
for (CandidateDamageTail& tail : m_candidateDamageTail)
tail = {};
}
SignalCandidateState();
}
@@ -143,14 +145,13 @@ void CHardwareFrameProcessor::ResetPipeline()
bool CHardwareFrameProcessor::HasReadyFrame() const
{
bool ready = false;
AcquireSRWLockShared(&m_candidateLock);
CSRWSharedLock lock(m_candidateLock);
for (const FrameCandidate& candidate : m_candidates)
if (candidate.state == CANDIDATE_READY)
{
ready = true;
break;
}
ReleaseSRWLockShared(&m_candidateLock);
return ready;
}
@@ -163,46 +164,47 @@ int CHardwareFrameProcessor::AcquireCandidate(
bool idle = true;
bool publishing = false;
AcquireSRWLockExclusive(&m_candidateLock);
for (unsigned i = 0; i < ARRAYSIZE(m_candidates); ++i)
{
if (m_candidates[i].state != CANDIDATE_FREE)
{
idle = false;
if (m_candidates[i].state == CANDIDATE_PUBLISHING)
publishing = true;
}
else if (selected < 0)
selected = static_cast<int>(i);
}
if (exclusiveSample && !idle)
selected = -1;
unsigned readyCount = 0;
for (const FrameCandidate& candidate : m_candidates)
if (candidate.state == CANDIDATE_READY)
++readyCount;
if (allowSupersede && !exclusiveSample && selected < 0 &&
readyCount > (publishing ? 0U : 1U))
CSRWExclusiveLock lock(m_candidateLock);
for (unsigned i = 0; i < ARRAYSIZE(m_candidates); ++i)
if (m_candidates[i].state == CANDIDATE_READY &&
m_candidates[i].sequence < oldest)
{
if (m_candidates[i].state != CANDIDATE_FREE)
{
selected = static_cast<int>(i);
oldest = m_candidates[i].sequence;
idle = false;
if (m_candidates[i].state == CANDIDATE_PUBLISHING)
publishing = true;
}
else if (selected < 0)
selected = static_cast<int>(i);
}
if (selected >= 0)
{
FrameCandidate& candidate =
m_candidates[static_cast<unsigned>(selected)];
superseded = candidate.state == CANDIDATE_READY;
candidate.state = CANDIDATE_PREPARING;
candidate.sequence = ++m_candidateSequence;
if (exclusiveSample && !idle)
selected = -1;
unsigned readyCount = 0;
for (const FrameCandidate& candidate : m_candidates)
if (candidate.state == CANDIDATE_READY)
++readyCount;
if (allowSupersede && !exclusiveSample && selected < 0 &&
readyCount > (publishing ? 0U : 1U))
for (unsigned i = 0; i < ARRAYSIZE(m_candidates); ++i)
if (m_candidates[i].state == CANDIDATE_READY &&
m_candidates[i].sequence < oldest)
{
selected = static_cast<int>(i);
oldest = m_candidates[i].sequence;
}
if (selected >= 0)
{
FrameCandidate& candidate =
m_candidates[static_cast<unsigned>(selected)];
superseded = candidate.state == CANDIDATE_READY;
candidate.state = CANDIDATE_PREPARING;
candidate.sequence = ++m_candidateSequence;
}
}
ReleaseSRWLockExclusive(&m_candidateLock);
if (superseded)
m_transport->FrameSuperseded();
@@ -214,9 +216,10 @@ void CHardwareFrameProcessor::ReleaseCandidate(unsigned candidateIndex)
if (candidateIndex >= ARRAYSIZE(m_candidates))
return;
AcquireSRWLockExclusive(&m_candidateLock);
m_candidates[candidateIndex].state = CANDIDATE_FREE;
ReleaseSRWLockExclusive(&m_candidateLock);
{
CSRWExclusiveLock lock(m_candidateLock);
m_candidates[candidateIndex].state = CANDIDATE_FREE;
}
SignalCandidateState();
}
@@ -280,14 +283,11 @@ bool CHardwareFrameProcessor::ExecuteCandidateCopy(
for (;;)
{
AcquireSRWLockExclusive(&m_copySubmitLock);
if (!m_publishPending)
{
const bool result = copySlot->Execute();
ReleaseSRWLockExclusive(&m_copySubmitLock);
return result;
CSRWExclusiveLock lock(m_copySubmitLock);
if (!m_publishPending)
return copySlot->Execute();
}
ReleaseSRWLockExclusive(&m_copySubmitLock);
const DWORD result = WaitForMultipleObjects(
ARRAYSIZE(waitHandles), waitHandles, FALSE, INFINITE);
@@ -313,18 +313,19 @@ void CHardwareFrameProcessor::CandidateCompletionFunction(
const bool timingValid = result && slot->GetGPUTimes(gpuStart, gpuEnd);
bool forceFrame = false;
AcquireSRWLockExclusive(&processor->m_candidateLock);
if (candidate->state == CANDIDATE_PREPARING)
{
candidate->prepareReady = CFrameScheduler::Nanotime();
candidate->prepareGPUStart = gpuStart;
candidate->prepareGPUEnd = gpuEnd;
candidate->prepareTimingValid = timingValid;
candidate->state =
result ? CANDIDATE_READY : CANDIDATE_FREE;
forceFrame = result && candidate->timingToken != 0;
CSRWExclusiveLock lock(processor->m_candidateLock);
if (candidate->state == CANDIDATE_PREPARING)
{
candidate->prepareReady = CFrameScheduler::Nanotime();
candidate->prepareGPUStart = gpuStart;
candidate->prepareGPUEnd = gpuEnd;
candidate->prepareTimingValid = timingValid;
candidate->state =
result ? CANDIDATE_READY : CANDIDATE_FREE;
forceFrame = result && candidate->timingToken != 0;
}
}
ReleaseSRWLockExclusive(&processor->m_candidateLock);
if (!result)
{
@@ -358,16 +359,17 @@ void CHardwareFrameProcessor::CompletionFunction(
uint64_t prepareGPUEnd;
uint64_t timingStart;
bool prepareTimingValid;
AcquireSRWLockShared(&processor->m_candidateLock);
const FrameCandidate& candidate =
processor->m_candidates[candidateIndex];
prepareCopyStart = candidate.prepareCopyStart;
prepareReady = candidate.prepareReady;
prepareGPUStart = candidate.prepareGPUStart;
prepareGPUEnd = candidate.prepareGPUEnd;
timingStart = candidate.timingStart;
prepareTimingValid = candidate.prepareTimingValid;
ReleaseSRWLockShared(&processor->m_candidateLock);
{
CSRWSharedLock lock(processor->m_candidateLock);
const FrameCandidate& candidate =
processor->m_candidates[candidateIndex];
prepareCopyStart = candidate.prepareCopyStart;
prepareReady = candidate.prepareReady;
prepareGPUStart = candidate.prepareGPUStart;
prepareGPUEnd = candidate.prepareGPUEnd;
timingStart = candidate.timingStart;
prepareTimingValid = candidate.prepareTimingValid;
}
const uint64_t publishStart = fbRes->GetCopyStart();
uint64_t gpuCopyStart = 0;
@@ -443,26 +445,27 @@ bool CHardwareFrameProcessor::Publish(
uint64_t publishStart)
{
CPublishPending publishPending(
&m_copySubmitLock, &m_publishPending, m_copySubmitEvent.Get());
CSRWSharedLock pipelineLock(m_pipelineLock);
m_copySubmitLock, &m_publishPending, m_copySubmitEvent.Get());
CSRWSharedLock pipelineLock(*m_pipelineLock);
int selectedCandidate = -1;
uint64_t newestSequence = 0;
AcquireSRWLockExclusive(&m_candidateLock);
for (unsigned i = 0; i < ARRAYSIZE(m_candidates); ++i)
if (m_candidates[i].state == CANDIDATE_READY &&
(selectedCandidate < 0 ||
m_candidates[i].sequence > newestSequence))
{
selectedCandidate = static_cast<int>(i);
newestSequence = m_candidates[i].sequence;
}
{
CSRWExclusiveLock lock(m_candidateLock);
for (unsigned i = 0; i < ARRAYSIZE(m_candidates); ++i)
if (m_candidates[i].state == CANDIDATE_READY &&
(selectedCandidate < 0 ||
m_candidates[i].sequence > newestSequence))
{
selectedCandidate = static_cast<int>(i);
newestSequence = m_candidates[i].sequence;
}
if (selectedCandidate >= 0)
m_candidates[static_cast<unsigned>(selectedCandidate)].state =
CANDIDATE_PUBLISHING;
ReleaseSRWLockExclusive(&m_candidateLock);
if (selectedCandidate >= 0)
m_candidates[static_cast<unsigned>(selectedCandidate)].state =
CANDIDATE_PUBLISHING;
}
if (selectedCandidate < 0)
return false;
@@ -471,18 +474,21 @@ bool CHardwareFrameProcessor::Publish(
const auto restoreCandidate = [this, candidateIndex]()
{
AcquireSRWLockExclusive(&m_candidateLock);
if (m_candidates[candidateIndex].state == CANDIDATE_PUBLISHING)
m_candidates[candidateIndex].state = CANDIDATE_READY;
ReleaseSRWLockExclusive(&m_candidateLock);
{
CSRWExclusiveLock lock(m_candidateLock);
if (m_candidates[candidateIndex].state == CANDIDATE_PUBLISHING)
m_candidates[candidateIndex].state = CANDIDATE_READY;
}
SignalCandidateState();
};
AcquireSRWLockShared(&m_candidateLock);
const bool candidateValid =
m_candidates[candidateIndex].state == CANDIDATE_PUBLISHING &&
m_candidates[candidateIndex].resource.Get();
ReleaseSRWLockShared(&m_candidateLock);
bool candidateValid;
{
CSRWSharedLock lock(m_candidateLock);
candidateValid =
m_candidates[candidateIndex].state == CANDIDATE_PUBLISHING &&
m_candidates[candidateIndex].resource.Get();
}
if (!candidateValid)
{
restoreCandidate();
@@ -564,33 +570,35 @@ bool CHardwareFrameProcessor::Publish(
frameSchedule.phaseEligible = false;
fbRes->SetSchedule(frameSchedule);
AcquireSRWLockExclusive(&m_damageLock);
if (candidate.nbDirtyRects)
memcpy(m_previousDamage, candidate.dirtyRects,
candidate.nbDirtyRects * sizeof(*m_previousDamage));
m_previousDamageCount = candidate.nbDirtyRects;
CandidateDamageTail& tail = m_candidateDamageTail[candidateIndex];
if (tail.active && tail.ownerSequence == candidateSequence)
{
m_hasPendingDamage = tail.hasDamage;
m_pendingDamageCount = tail.nbDirtyRects;
if (tail.hasDamage && tail.nbDirtyRects)
memcpy(m_pendingDamage, tail.dirtyRects,
tail.nbDirtyRects * sizeof(*m_pendingDamage));
tail.ownerSequence = 0;
tail.active = false;
CSRWExclusiveLock lock(m_damageLock);
if (candidate.nbDirtyRects)
memcpy(m_previousDamage, candidate.dirtyRects,
candidate.nbDirtyRects * sizeof(*m_previousDamage));
m_previousDamageCount = candidate.nbDirtyRects;
CandidateDamageTail& tail = m_candidateDamageTail[candidateIndex];
if (tail.active && tail.ownerSequence == candidateSequence)
{
m_hasPendingDamage = tail.hasDamage;
m_pendingDamageCount = tail.nbDirtyRects;
if (tail.hasDamage && tail.nbDirtyRects)
memcpy(m_pendingDamage, tail.dirtyRects,
tail.nbDirtyRects * sizeof(*m_pendingDamage));
tail.ownerSequence = 0;
tail.active = false;
}
}
ReleaseSRWLockExclusive(&m_damageLock);
const bool submitted = copySlot->Execute();
publishPending.Clear();
if (!submitted)
{
SetFullDamage();
AcquireSRWLockShared(&m_candidateLock);
const bool callbackPending =
candidate.state == CANDIDATE_PUBLISHING;
ReleaseSRWLockShared(&m_candidateLock);
bool callbackPending;
{
CSRWSharedLock lock(m_candidateLock);
callbackPending = candidate.state == CANDIDATE_PUBLISHING;
}
if (callbackPending && !copySlot->HasSubmittedWork())
{
m_transport->FailFrameBuffer(buffer.frameIndex);
@@ -605,15 +613,16 @@ bool CHardwareFrameProcessor::Publish(
buffer.frameIndex, schedule, periodic, deliveredToOwner);
unsigned superseded = 0;
AcquireSRWLockExclusive(&m_candidateLock);
for (FrameCandidate& ready : m_candidates)
if (ready.state == CANDIDATE_READY &&
ready.sequence < candidateSequence)
{
ready.state = CANDIDATE_FREE;
++superseded;
}
ReleaseSRWLockExclusive(&m_candidateLock);
{
CSRWExclusiveLock lock(m_candidateLock);
for (FrameCandidate& ready : m_candidates)
if (ready.state == CANDIDATE_READY &&
ready.sequence < candidateSequence)
{
ready.state = CANDIDATE_FREE;
++superseded;
}
}
for (unsigned i = 0; i < superseded; ++i)
m_transport->FrameSuperseded();
SignalCandidateState();
@@ -654,26 +663,27 @@ bool CHardwareFrameProcessor::Submit(const FrameSubmission& submission)
static_cast<unsigned>(selectedCandidate);
FrameCandidate& candidate = m_candidates[candidateIndex];
CSRWSharedLock pipelineLock(m_pipelineLock);
CSRWSharedLock pipelineLock(*m_pipelineLock);
CPostProcessor& postProcessor = m_postProcessors[candidateIndex];
const D12FrameFormat& dstFormat = postProcessor.GetOutputFormat();
RECT currentDirtyRects[LG_MAX_DIRTY_RECTS] = {};
unsigned nbDirtyRects = 0;
AcquireSRWLockExclusive(&m_damageLock);
if (m_hasPendingDamage)
{
nbDirtyRects = m_pendingDamageCount;
if (nbDirtyRects)
memcpy(currentDirtyRects, m_pendingDamage,
nbDirtyRects * sizeof(*currentDirtyRects));
CSRWExclusiveLock lock(m_damageLock);
if (m_hasPendingDamage)
{
nbDirtyRects = m_pendingDamageCount;
if (nbDirtyRects)
memcpy(currentDirtyRects, m_pendingDamage,
nbDirtyRects * sizeof(*currentDirtyRects));
}
CandidateDamageTail& tail = m_candidateDamageTail[candidateIndex];
tail.ownerSequence = candidate.sequence;
tail.nbDirtyRects = 0;
tail.hasDamage = false;
tail.active = true;
}
CandidateDamageTail& tail = m_candidateDamageTail[candidateIndex];
tail.ownerSequence = candidate.sequence;
tail.nbDirtyRects = 0;
tail.hasDamage = false;
tail.active = true;
ReleaseSRWLockExclusive(&m_damageLock);
CD3D12CommandSlot * copySlot = m_dx12->GetCopySlot(candidateIndex);
if (!copySlot)

View File

@@ -67,8 +67,8 @@ private:
FrameCandidate m_candidates[CAPTURE_PIPELINE_SLOTS];
CandidateDamageTail m_candidateDamageTail[CAPTURE_PIPELINE_SLOTS];
mutable SRWLOCK m_candidateLock = SRWLOCK_INIT;
SRWLOCK m_copySubmitLock = SRWLOCK_INIT;
mutable CSRWLock m_candidateLock;
CSRWLock m_copySubmitLock;
uint64_t m_candidateSequence = 0;
bool m_publishPending = false;
Wrappers::Event m_candidateAvailableEvent;
@@ -92,7 +92,7 @@ public:
CHardwareFrameProcessor(IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent);
CSRWLock * pipelineLock, HANDLE terminateEvent);
bool IsValid() const override;
bool Submit(const FrameSubmission& submission) override;

View File

@@ -30,7 +30,7 @@
CSoftwareFrameProcessor::CSoftwareFrameProcessor(
IFrameTransport * transport, std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent) :
CSRWLock * pipelineLock, HANDLE terminateEvent) :
CFrameProcessor(transport, std::move(dx12), postProcessors,
pipelineLock, terminateEvent)
{
@@ -112,7 +112,7 @@ void CSoftwareFrameProcessor::CompletionFunction(
bool CSoftwareFrameProcessor::Submit(const FrameSubmission& submission)
{
CSRWSharedLock pipelineLock(m_pipelineLock);
CSRWSharedLock pipelineLock(*m_pipelineLock);
CPostProcessor& postProcessor = m_postProcessors[0];
const D12FrameFormat& dstFormat = postProcessor.GetOutputFormat();

View File

@@ -32,7 +32,7 @@ public:
CSoftwareFrameProcessor(IFrameTransport * transport,
std::shared_ptr<CD3D12Device> dx12,
CPostProcessor postProcessors[CAPTURE_PIPELINE_SLOTS],
SRWLOCK * pipelineLock, HANDLE terminateEvent);
CSRWLock * pipelineLock, HANDLE terminateEvent);
bool Submit(const FrameSubmission& submission) override;
bool HasReadyFrame() const override { return false; }

View File

@@ -655,7 +655,7 @@ bool CSwapChainProcessor::SwapChainNewFrame(ComPtr<IDXGIResource> acquiredBuffer
unsigned timingEffectIndex = 0;
uint64_t timingToken = 0;
{
CSRWExclusiveLock pipelineLock(&m_pipelineLock);
CSRWExclusiveLock pipelineLock(m_pipelineLock);
m_postProcessors[0].Update(srcFormat);
frameMetadataChanged = noImageUpdate &&

View File

@@ -59,7 +59,7 @@ private:
CPostProcessor m_postProcessors[CAPTURE_PIPELINE_SLOTS];
std::unique_ptr<CFrameProcessor> m_frameProcessor;
// Reconfiguration is exclusive while per-candidate recording is shared.
SRWLOCK m_pipelineLock = SRWLOCK_INIT;
CSRWLock m_pipelineLock;
Wrappers::HandleT<Wrappers::HandleTraits::HANDLENullTraits> m_thread[3];
Wrappers::Event m_terminateEvent;