[host] reworked capture to capture at the hosts's frame rate

This will help frame delivery to the host arrive on time, as well as
allow lower latency with a higher host refresh rate.

Data offset addresses are now also pre-calculated rather then
updated on every frame.
This commit is contained in:
Geoffrey McRae 2017-12-03 22:03:22 +11:00
parent aa191f6c90
commit a54f3000f5
3 changed files with 67 additions and 63 deletions

View File

@ -154,7 +154,7 @@ bool NvFBC::Initialize(CaptureOptions * options)
ZeroMemory(&m_grabFrameParams, sizeof(NVFBC_TOSYS_GRAB_FRAME_PARAMS)); ZeroMemory(&m_grabFrameParams, sizeof(NVFBC_TOSYS_GRAB_FRAME_PARAMS));
ZeroMemory(&m_grabInfo, sizeof(NvFBCFrameGrabInfo)); ZeroMemory(&m_grabInfo, sizeof(NvFBCFrameGrabInfo));
m_grabFrameParams.dwVersion = NVFBC_TOSYS_GRAB_FRAME_PARAMS_VER; m_grabFrameParams.dwVersion = NVFBC_TOSYS_GRAB_FRAME_PARAMS_VER;
m_grabFrameParams.dwFlags = NVFBC_TOSYS_WAIT_WITH_TIMEOUT; m_grabFrameParams.dwFlags = NVFBC_TOSYS_NOWAIT;
m_grabFrameParams.dwWaitTime = 100; m_grabFrameParams.dwWaitTime = 100;
m_grabFrameParams.eGMode = NVFBC_TOSYS_SOURCEMODE_FULL; m_grabFrameParams.eGMode = NVFBC_TOSYS_SOURCEMODE_FULL;
m_grabFrameParams.dwStartX = 0; m_grabFrameParams.dwStartX = 0;

View File

@ -30,7 +30,7 @@ Service::Service() :
m_initialized(false), m_initialized(false),
m_readyEvent(INVALID_HANDLE_VALUE), m_readyEvent(INVALID_HANDLE_VALUE),
m_capture(NULL), m_capture(NULL),
m_memory(NULL), m_header(NULL),
m_frameIndex(0) m_frameIndex(0)
{ {
m_ivshmem = IVSHMEM::Get(); m_ivshmem = IVSHMEM::Get();
@ -60,8 +60,8 @@ bool Service::Initialize(ICapture * captureDevice)
return false; return false;
} }
m_memory = static_cast<uint8_t*>(m_ivshmem->GetMemory()); uint8_t * memory = static_cast<uint8_t*>(m_ivshmem->GetMemory());
if (!m_memory) if (!memory)
{ {
DEBUG_ERROR("Failed to get IVSHMEM memory"); DEBUG_ERROR("Failed to get IVSHMEM memory");
DeInitialize(); DeInitialize();
@ -76,17 +76,30 @@ bool Service::Initialize(ICapture * captureDevice)
return false; return false;
} }
KVMGFXHeader * header = reinterpret_cast<KVMGFXHeader*>(m_memory); m_header = reinterpret_cast<KVMGFXHeader *>(memory);
m_frame[0] = (uint8_t *)(((uintptr_t)memory + sizeof(KVMGFXHeader *) + 0x7F) & ~0x7F);
m_frameSize = ((m_ivshmem->GetSize() - (m_frame[0] - memory)) & ~0x7F) >> 1;
m_frame[1] = m_frame[0] + m_frameSize;
m_dataOffset[0] = m_frame[0] - memory;
m_dataOffset[1] = m_frame[1] - memory;
if (m_capture->GetMaxFrameSize() > m_frameSize)
{
DEBUG_ERROR("Frame can exceed buffer size!");
DeInitialize();
return false;
}
// we save this as it might actually be valid // we save this as it might actually be valid
UINT16 hostID = header->hostID; UINT16 hostID = m_header->hostID;
ZeroMemory(header, sizeof(KVMGFXHeader)); ZeroMemory(m_header, sizeof(KVMGFXHeader));
memcpy(header->magic, KVMGFX_HEADER_MAGIC, sizeof(KVMGFX_HEADER_MAGIC)); memcpy(m_header->magic, KVMGFX_HEADER_MAGIC, sizeof(KVMGFX_HEADER_MAGIC));
header->version = KVMGFX_HEADER_VERSION; m_header->version = KVMGFX_HEADER_VERSION;
header->guestID = m_ivshmem->GetPeerID(); m_header->guestID = m_ivshmem->GetPeerID();
header->hostID = hostID; m_header->hostID = hostID;
m_header->frameType = m_capture->GetFrameType();
m_initialized = true; m_initialized = true;
return true; return true;
@ -97,7 +110,12 @@ void Service::DeInitialize()
if (m_readyEvent != INVALID_HANDLE_VALUE) if (m_readyEvent != INVALID_HANDLE_VALUE)
CloseHandle(m_readyEvent); CloseHandle(m_readyEvent);
m_memory = NULL; m_header = NULL;
m_frame[0] = NULL;
m_frame[1] = NULL;
m_dataOffset[0] = 0;
m_dataOffset[1] = 0;
m_ivshmem->DeInitialize(); m_ivshmem->DeInitialize();
if (m_capture) if (m_capture)
@ -114,56 +132,9 @@ bool Service::Process()
if (!m_initialized) if (!m_initialized)
return false; return false;
KVMGFXHeader * header = reinterpret_cast<KVMGFXHeader *>(m_memory);
// calculate the current offset and ensure it is 16-byte aligned for SMID performance
uint64_t dataOffset = sizeof(KVMGFXHeader) + m_frameIndex * m_capture->GetMaxFrameSize();
dataOffset = (dataOffset + 0xF) & ~0xF;
uint8_t * data = m_memory + dataOffset;
const size_t available = m_ivshmem->GetSize() - sizeof(KVMGFXHeader);
if (dataOffset + m_capture->GetMaxFrameSize() > available)
{
DEBUG_ERROR("Frame can exceed buffer size!");
return false;
}
// setup the header
header->frameType = m_capture->GetFrameType();
header->dataLen = 0;
FrameInfo frame; FrameInfo frame;
frame.buffer = data; frame.buffer = m_frame[m_frameIndex];
frame.bufferSize = m_ivshmem->GetSize() - sizeof(KVMGFXHeader); frame.bufferSize = m_frameSize;
// capture a frame of data
if (!m_capture->GrabFrame(frame))
{
header->dataLen = 0;
DEBUG_ERROR("Capture failed");
return false;
}
// copy the frame details into the header
header->width = frame.width;
header->height = frame.height;
header->stride = frame.stride;
header->dataPos = dataOffset;
header->dataLen = frame.outSize;
// tell the host where the cursor is
POINT cursorPos;
GetCursorPos(&cursorPos);
header->mouseX = cursorPos.x;
header->mouseY = cursorPos.y;
ResetEvent(m_readyEvent);
if (!m_ivshmem->RingDoorbell(header->hostID, 0))
{
DEBUG_ERROR("Failed to ring doorbell");
return false;
}
// wait for the host to notify that is it is ready to proceed // wait for the host to notify that is it is ready to proceed
bool eventDone = false; bool eventDone = false;
@ -191,6 +162,35 @@ bool Service::Process()
return false; return false;
} }
} }
ResetEvent(m_readyEvent);
// capture a frame of data
if (!m_capture->GrabFrame(frame))
{
m_header->dataLen = 0;
DEBUG_ERROR("Capture failed");
return false;
}
// copy the frame details into the header
// setup the header
m_header->width = frame.width;
m_header->height = frame.height;
m_header->stride = frame.stride;
m_header->dataPos = m_dataOffset[m_frameIndex];
m_header->dataLen = frame.outSize;
// tell the host where the cursor is
POINT cursorPos;
GetCursorPos(&cursorPos);
m_header->mouseX = cursorPos.x;
m_header->mouseY = cursorPos.y;
if (!m_ivshmem->RingDoorbell(m_header->hostID, 0))
{
DEBUG_ERROR("Failed to ring doorbell");
return false;
}
if (++m_frameIndex == 2) if (++m_frameIndex == 2)
m_frameIndex = 0; m_frameIndex = 0;

View File

@ -49,6 +49,10 @@ private:
IVSHMEM * m_ivshmem; IVSHMEM * m_ivshmem;
HANDLE m_readyEvent; HANDLE m_readyEvent;
ICapture * m_capture; ICapture * m_capture;
uint8_t * m_memory;
int m_frameIndex; KVMGFXHeader * m_header;
uint8_t * m_frame[2];
size_t m_frameSize;
uint64_t m_dataOffset[2];
int m_frameIndex;
}; };