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:
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;
|
||||
}
|
||||
40
idd/LGIdd/transport/lgmp/CLGMPFrameCaps.h
Normal file
40
idd/LGIdd/transport/lgmp/CLGMPFrameCaps.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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 "transport/FrameCaps.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class CLGMPFrameCaps final : public FrameCaps
|
||||
{
|
||||
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