mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-08-22 07:01:30 +00:00
[idd] transport: intersect frame capabilities
This commit is contained in:
@@ -328,6 +328,8 @@ ITransport::OpenResult CTransportManager::OpenEntry(Entry& entry)
|
||||
bool CTransportManager::InitializeEntry(Entry& entry)
|
||||
{
|
||||
std::shared_ptr<ITransport> transport;
|
||||
uint32_t services = 0;
|
||||
bool required = false;
|
||||
{
|
||||
CSRWSharedLock entryLock(entry.lock);
|
||||
if (entry.state == State::INITIALIZED || entry.state == State::READY)
|
||||
@@ -335,6 +337,8 @@ bool CTransportManager::InitializeEntry(Entry& entry)
|
||||
if (entry.state != State::OPEN)
|
||||
return false;
|
||||
transport = entry.transport;
|
||||
services = entry.config.services;
|
||||
required = entry.required;
|
||||
}
|
||||
|
||||
if (!transport || !transport->Initialize())
|
||||
@@ -344,12 +348,30 @@ bool CTransportManager::InitializeEntry(Entry& entry)
|
||||
return false;
|
||||
}
|
||||
|
||||
const FrameMemoryLimits limits = transport->GetMemoryLimits();
|
||||
std::shared_ptr<const FrameCaps> frameCaps;
|
||||
if (services & TRANSPORT_SERVICE_FRAME)
|
||||
{
|
||||
frameCaps = transport->GetFrameCaps();
|
||||
if (!frameCaps && required)
|
||||
{
|
||||
CSRWExclusiveLock entryLock(entry.lock);
|
||||
entry.state = State::FAILED;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const bool hasFrameCaps = frameCaps != nullptr;
|
||||
{
|
||||
CSRWExclusiveLock entryLock(entry.lock);
|
||||
entry.limits = limits;
|
||||
entry.limitsValid = true;
|
||||
entry.state = State::INITIALIZED;
|
||||
// The first successfully initialized instance fixes the advertised
|
||||
// capability contract. Recreating an instance must not change the mode
|
||||
// list during a runtime restart.
|
||||
if (!entry.frameCaps)
|
||||
entry.frameCaps = std::move(frameCaps);
|
||||
// A best-effort instance without a capability contract can continue to
|
||||
// provide its other configured services, but must not receive frames.
|
||||
entry.frameAbsent = (services & TRANSPORT_SERVICE_FRAME) &&
|
||||
!hasFrameCaps;
|
||||
entry.state = State::INITIALIZED;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -627,8 +649,6 @@ void CTransportManager::RetryEntry(Entry& entry, uint64_t now,
|
||||
{
|
||||
CSRWExclusiveLock entryLock(entry.lock);
|
||||
entry.transport.reset();
|
||||
entry.limits = FrameMemoryLimits {};
|
||||
entry.limitsValid = false;
|
||||
entry.directMemory = DirectFrameBufferMemory {};
|
||||
entry.directMemoryValid = false;
|
||||
entry.setupDone = false;
|
||||
@@ -1180,20 +1200,42 @@ void CTransportManager::RecoveryStatus(const SourceKey& source,
|
||||
}
|
||||
}
|
||||
|
||||
FrameMemoryLimits CTransportManager::GetMemoryLimits() const
|
||||
bool CTransportManager::CanUseMode(const FrameMode& mode,
|
||||
uint32_t * requiredSizeMiB) const
|
||||
{
|
||||
if (requiredSizeMiB)
|
||||
*requiredSizeMiB = 0;
|
||||
|
||||
std::shared_ptr<const FrameCaps> caps[FRAME_MAX_SINKS];
|
||||
unsigned capsCount = 0;
|
||||
Entry * entries[FRAME_MAX_SINKS] = {};
|
||||
const unsigned count = Entries(entries);
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
CSRWSharedLock managerLock(m_lock);
|
||||
if (m_stopping || m_stopped)
|
||||
return FrameMemoryLimits {};
|
||||
Entry& entry = *entries[i];
|
||||
CSRWSharedLock entryLock(entry.lock);
|
||||
if (!entry.required ||
|
||||
!(entry.config.services & TRANSPORT_SERVICE_FRAME))
|
||||
continue;
|
||||
if (!entry.frameCaps)
|
||||
return false;
|
||||
caps[capsCount++] = entry.frameCaps;
|
||||
}
|
||||
|
||||
Entry * primary = Primary();
|
||||
if (!primary)
|
||||
return FrameMemoryLimits {};
|
||||
if (!capsCount)
|
||||
return false;
|
||||
|
||||
CSRWSharedLock entryLock(primary->lock);
|
||||
return primary->limitsValid ? primary->limits : FrameMemoryLimits {};
|
||||
bool supported = true;
|
||||
for (unsigned i = 0; i < capsCount; ++i)
|
||||
{
|
||||
uint32_t hint = 0;
|
||||
if (caps[i]->CanUseMode(mode, capsCount == 1 ? &hint : nullptr))
|
||||
continue;
|
||||
supported = false;
|
||||
if (requiredSizeMiB && capsCount == 1)
|
||||
*requiredSizeMiB = hint;
|
||||
}
|
||||
return supported;
|
||||
}
|
||||
|
||||
DirectFrameBufferMemory CTransportManager::GetDirectMemory() const
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
static_assert(TRANSPORT_MAX_INSTANCES == FRAME_MAX_SINKS,
|
||||
"The transport and frame limits must match");
|
||||
|
||||
class CTransportManager final
|
||||
class CTransportManager final : public FrameCaps
|
||||
{
|
||||
public:
|
||||
using CreateFn = std::unique_ptr<ITransport> (*)(
|
||||
@@ -117,8 +117,7 @@ private:
|
||||
bool syncPending = false;
|
||||
bool recoveryPending = false;
|
||||
RecoveryUpdate recovery;
|
||||
FrameMemoryLimits limits;
|
||||
bool limitsValid = false;
|
||||
std::shared_ptr<const FrameCaps> frameCaps;
|
||||
DirectFrameBufferMemory directMemory;
|
||||
bool directMemoryValid = false;
|
||||
std::shared_ptr<ITransport> exposedTransport;
|
||||
@@ -183,7 +182,8 @@ public:
|
||||
uint64_t session, uint32_t serial, bool active,
|
||||
Recovery state, uint32_t error);
|
||||
|
||||
FrameMemoryLimits GetMemoryLimits() const;
|
||||
bool CanUseMode(const FrameMode& mode,
|
||||
uint32_t * requiredSizeMiB = nullptr) const override;
|
||||
DirectFrameBufferMemory GetDirectMemory() const;
|
||||
|
||||
IFrameTransport& Frames();
|
||||
|
||||
41
idd/LGIdd/transport/FrameCaps.h
Normal file
41
idd/LGIdd/transport/FrameCaps.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 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 <stdint.h>
|
||||
|
||||
struct FrameMode
|
||||
{
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
uint32_t refreshMilliHz = 0;
|
||||
};
|
||||
|
||||
class FrameCaps
|
||||
{
|
||||
public:
|
||||
virtual ~FrameCaps() = default;
|
||||
|
||||
// A rejecting implementation may provide a minimum configured-capacity
|
||||
// hint. Zero means that no useful size recommendation is available.
|
||||
virtual bool CanUseMode(const FrameMode& mode,
|
||||
uint32_t * requiredSizeMiB = nullptr) const = 0;
|
||||
};
|
||||
@@ -21,9 +21,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "transport/DirectFrameBufferMemory.h"
|
||||
#include "transport/FrameMemoryLimits.h"
|
||||
#include "transport/FrameCaps.h"
|
||||
#include "transport/TransportConfig.h"
|
||||
|
||||
#include <memory>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -89,7 +90,14 @@ public:
|
||||
virtual void RecoveryStatus(
|
||||
uint64_t, uint32_t, bool, Recovery, uint32_t) {}
|
||||
|
||||
virtual FrameMemoryLimits GetMemoryLimits() const = 0;
|
||||
// Frame capabilities describe the configured instance, not transient
|
||||
// runtime state. CanUseMode answers are immutable for the returned
|
||||
// object's lifetime, and recreating an instance from the same descriptor
|
||||
// must provide the same capability contract.
|
||||
virtual std::shared_ptr<const FrameCaps> GetFrameCaps() const
|
||||
{
|
||||
return std::shared_ptr<const FrameCaps>();
|
||||
}
|
||||
virtual DirectFrameBufferMemory GetDirectMemory() const = 0;
|
||||
|
||||
// Component pointers are fixed after Setup and remain valid until Stop.
|
||||
|
||||
112
idd/LGIdd/transport/lgmp/CLGMPFrameCaps.cpp
Normal file
112
idd/LGIdd/transport/lgmp/CLGMPFrameCaps.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* 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/lgmp/CLGMPFrameCaps.h"
|
||||
|
||||
#include <d3d12.h>
|
||||
#include <limits>
|
||||
|
||||
namespace
|
||||
{
|
||||
static const uint64_t FRAME_BYTES_PER_PIXEL = 4;
|
||||
|
||||
bool AlignUp(uint64_t value, uint64_t alignment, uint64_t& result)
|
||||
{
|
||||
if (!alignment || (alignment & (alignment - 1)))
|
||||
return false;
|
||||
|
||||
const uint64_t mask = alignment - 1;
|
||||
if (value > (std::numeric_limits<uint64_t>::max)() - mask)
|
||||
return false;
|
||||
result = (value + mask) & ~mask;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CalculateFrameSize(
|
||||
const FrameMode& mode, uint64_t& frameSize)
|
||||
{
|
||||
frameSize = 0;
|
||||
if (!mode.width || !mode.height || !mode.refreshMilliHz)
|
||||
return false;
|
||||
|
||||
uint64_t pitch;
|
||||
if (!AlignUp((uint64_t)mode.width * FRAME_BYTES_PER_PIXEL,
|
||||
D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, pitch) ||
|
||||
pitch > (std::numeric_limits<uint64_t>::max)() / mode.height)
|
||||
return false;
|
||||
|
||||
frameSize = pitch * mode.height;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t RecommendedSizeMiB(uint64_t requiredSize)
|
||||
{
|
||||
uint64_t sizeMiB = requiredSize / 1048576;
|
||||
if (requiredSize % 1048576)
|
||||
++sizeMiB;
|
||||
|
||||
uint64_t result = 1;
|
||||
while (result < sizeMiB && result <= UINT32_MAX / 2)
|
||||
result <<= 1;
|
||||
return result < sizeMiB ? UINT32_MAX : (uint32_t)result;
|
||||
}
|
||||
}
|
||||
|
||||
CLGMPFrameCaps::CLGMPFrameCaps(uint64_t capacity,
|
||||
uint64_t frameMemoryOffset, uint32_t bufferCount) :
|
||||
m_capacity(capacity),
|
||||
m_frameMemoryOffset(frameMemoryOffset),
|
||||
m_bufferCount(bufferCount)
|
||||
{
|
||||
}
|
||||
|
||||
bool CLGMPFrameCaps::CanUseMode(const FrameMode& mode,
|
||||
uint32_t * requiredSizeMiB) const
|
||||
{
|
||||
if (requiredSizeMiB)
|
||||
*requiredSizeMiB = 0;
|
||||
|
||||
const uint64_t alignment =
|
||||
D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
|
||||
uint64_t frameSize;
|
||||
if (!m_frameMemoryOffset || !m_bufferCount ||
|
||||
!CalculateFrameSize(mode, frameSize) ||
|
||||
frameSize > (std::numeric_limits<uint64_t>::max)() - alignment)
|
||||
return false;
|
||||
|
||||
uint64_t frameAllocationSize;
|
||||
uint64_t frameMemoryStart;
|
||||
if (!AlignUp(frameSize + alignment, alignment,
|
||||
frameAllocationSize) ||
|
||||
!AlignUp(m_frameMemoryOffset, alignment, frameMemoryStart) ||
|
||||
frameAllocationSize >
|
||||
((std::numeric_limits<uint64_t>::max)() - frameMemoryStart) /
|
||||
m_bufferCount)
|
||||
return false;
|
||||
|
||||
const uint64_t requiredSize = frameMemoryStart +
|
||||
frameAllocationSize * m_bufferCount;
|
||||
if (requiredSize <= m_capacity)
|
||||
return true;
|
||||
|
||||
if (requiredSizeMiB)
|
||||
*requiredSizeMiB = RecommendedSizeMiB(requiredSize);
|
||||
return false;
|
||||
}
|
||||
@@ -20,13 +20,21 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "transport/FrameCaps.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct FrameMemoryLimits
|
||||
class CLGMPFrameCaps final : public FrameCaps
|
||||
{
|
||||
uint64_t capacity = 0;
|
||||
uint64_t frameMemoryOffset = 0;
|
||||
uint64_t alignment = 0;
|
||||
uint64_t maxFrameSize = 0;
|
||||
unsigned bufferCount = 0;
|
||||
private:
|
||||
uint64_t m_capacity;
|
||||
uint64_t m_frameMemoryOffset;
|
||||
uint32_t m_bufferCount;
|
||||
|
||||
public:
|
||||
CLGMPFrameCaps(uint64_t capacity, uint64_t frameMemoryOffset,
|
||||
uint32_t bufferCount);
|
||||
|
||||
bool CanUseMode(const FrameMode& mode,
|
||||
uint32_t * requiredSizeMiB = nullptr) const override;
|
||||
};
|
||||
@@ -21,10 +21,13 @@
|
||||
#include "transport/lgmp/CLGMPFrameTransport.h"
|
||||
|
||||
#include "transport/lgmp/CIVSHMEM.h"
|
||||
#include "transport/lgmp/CLGMPFrameCaps.h"
|
||||
#include "transport/lgmp/CLGMPHost.h"
|
||||
#include "CDebug.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4200)
|
||||
@@ -245,15 +248,11 @@ void CLGMPFrameTransport::DeInit()
|
||||
memset(m_frameOwnerQueue, 0, sizeof(m_frameOwnerQueue));
|
||||
}
|
||||
|
||||
FrameMemoryLimits CLGMPFrameTransport::GetMemoryLimits() const
|
||||
std::shared_ptr<const FrameCaps> CLGMPFrameTransport::GetFrameCaps() const
|
||||
{
|
||||
FrameMemoryLimits limits;
|
||||
limits.capacity = m_ivshmem.GetUsableSize();
|
||||
limits.frameMemoryOffset = m_frameMemoryOffset;
|
||||
limits.alignment = m_alignSize;
|
||||
limits.maxFrameSize = m_maxFrameSize;
|
||||
limits.bufferCount = LGMP_Q_FRAME_BUFFER_LEN;
|
||||
return limits;
|
||||
return std::shared_ptr<const FrameCaps>(new (std::nothrow)
|
||||
CLGMPFrameCaps(m_ivshmem.GetUsableSize(), m_frameMemoryOffset,
|
||||
LGMP_Q_FRAME_BUFFER_LEN));
|
||||
}
|
||||
|
||||
CLGMPFrameTransport::SubscriberSnapshot
|
||||
|
||||
@@ -33,9 +33,11 @@ extern "C" {
|
||||
#include "common/KVMFR.h"
|
||||
#include "capture/CFrameScheduler.h"
|
||||
#include "capture/FramePipeline.h"
|
||||
#include "transport/FrameMemoryLimits.h"
|
||||
#include "transport/FrameCaps.h"
|
||||
#include "transport/IFrameSink.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class CIVSHMEM;
|
||||
class CLGMPHost;
|
||||
class CLGMPTransport;
|
||||
@@ -151,7 +153,7 @@ private:
|
||||
void SealMemoryLayout();
|
||||
bool Setup(size_t alignSize);
|
||||
void DeInit();
|
||||
FrameMemoryLimits GetMemoryLimits() const;
|
||||
std::shared_ptr<const FrameCaps> GetFrameCaps() const;
|
||||
SubscriberSnapshot SnapshotSubscribers() const;
|
||||
void FinalizeSubscribers(
|
||||
const SubscriberSnapshot& snapshot, uint64_t now);
|
||||
|
||||
@@ -239,9 +239,9 @@ void CLGMPTransport::RecoveryStatus(
|
||||
session, serial, active, wireState, wireError);
|
||||
}
|
||||
|
||||
FrameMemoryLimits CLGMPTransport::GetMemoryLimits() const
|
||||
std::shared_ptr<const FrameCaps> CLGMPTransport::GetFrameCaps() const
|
||||
{
|
||||
return m_frames.GetMemoryLimits();
|
||||
return m_frames.GetFrameCaps();
|
||||
}
|
||||
|
||||
DirectFrameBufferMemory CLGMPTransport::GetDirectMemory() const
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
uint64_t session, uint32_t serial, bool active,
|
||||
Recovery state, uint32_t error) override;
|
||||
|
||||
FrameMemoryLimits GetMemoryLimits() const override;
|
||||
std::shared_ptr<const FrameCaps> GetFrameCaps() const override;
|
||||
DirectFrameBufferMemory GetDirectMemory() const override;
|
||||
|
||||
IFrameSink * FrameSink() override { return &m_frames; }
|
||||
|
||||
Reference in New Issue
Block a user