2017-10-31 16:53:06 +00:00
|
|
|
/*
|
2017-12-03 15:31:16 +00:00
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2017-10-31 16:53:06 +00:00
|
|
|
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
2017-12-11 17:30:47 +00:00
|
|
|
https://looking-glass.hostfission.com
|
2017-10-31 16:53:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2017-12-14 22:51:23 +00:00
|
|
|
#if CONFIG_CAPTURE_NVFBC
|
|
|
|
|
2017-10-31 13:51:53 +00:00
|
|
|
#include "NvFBC.h"
|
2017-11-02 06:45:25 +00:00
|
|
|
using namespace Capture;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2017-12-14 19:11:41 +00:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/memcpySSE.h"
|
2017-10-31 13:51:53 +00:00
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
#define NVFBC_LIBRARY_NAME "NvFBC64.dll"
|
|
|
|
#else
|
|
|
|
#define NVFBC_LIBRARY_NAME "NvFBC.dll"
|
|
|
|
#endif
|
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NvFBC::NvFBC() :
|
2017-11-16 09:53:22 +00:00
|
|
|
m_options(NULL),
|
2017-12-08 06:51:47 +00:00
|
|
|
m_optNoCrop(false),
|
2017-12-05 09:39:54 +00:00
|
|
|
m_optNoWait(false),
|
2017-11-02 06:45:25 +00:00
|
|
|
m_initialized(false),
|
|
|
|
m_hDLL(NULL),
|
|
|
|
m_nvFBC(NULL)
|
2017-10-31 13:51:53 +00:00
|
|
|
{
|
2017-11-02 06:45:25 +00:00
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NvFBC::~NvFBC()
|
|
|
|
{
|
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
bool Capture::NvFBC::CanInitialize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-16 09:53:22 +00:00
|
|
|
bool NvFBC::Initialize(CaptureOptions * options)
|
2017-11-02 06:45:25 +00:00
|
|
|
{
|
|
|
|
if (m_initialized)
|
|
|
|
DeInitialize();
|
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
m_options = options;
|
2017-12-08 06:51:47 +00:00
|
|
|
m_optNoCrop = false;
|
2017-12-05 09:39:54 +00:00
|
|
|
for (CaptureOptions::const_iterator it = options->cbegin(); it != options->cend(); ++it)
|
2017-11-16 09:53:22 +00:00
|
|
|
{
|
2017-12-08 06:51:47 +00:00
|
|
|
if (_strcmpi(*it, "nocrop") == 0) { m_optNoCrop = false; continue; }
|
|
|
|
if (_strcmpi(*it, "nowait") == 0) { m_optNoWait = true ; continue; }
|
2017-11-16 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 17:03:16 +00:00
|
|
|
std::string nvfbc = Util::GetSystemRoot() + "\\" + NVFBC_LIBRARY_NAME;
|
|
|
|
m_hDLL = LoadLibraryA(nvfbc.c_str());
|
2017-11-02 06:45:25 +00:00
|
|
|
if (!m_hDLL)
|
2017-10-31 13:51:53 +00:00
|
|
|
{
|
2017-12-16 18:19:34 +00:00
|
|
|
DEBUG_ERROR("Failed to load the NvFBC library: %d - %s", (int)GetLastError(), nvfbc.c_str());
|
2017-11-02 06:45:25 +00:00
|
|
|
return false;
|
2017-10-31 13:51:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
m_fnCreateEx = (NvFBC_CreateFunctionExType )GetProcAddress(m_hDLL, "NvFBC_CreateEx" );
|
|
|
|
m_fnSetGlobalFlags = (NvFBC_SetGlobalFlagsType )GetProcAddress(m_hDLL, "NvFBC_SetGlobalFlags");
|
|
|
|
m_fnGetStatusEx = (NvFBC_GetStatusExFunctionType)GetProcAddress(m_hDLL, "NvFBC_GetStatusEx" );
|
|
|
|
m_fnEnable = (NvFBC_EnableFunctionType )GetProcAddress(m_hDLL, "NvFBC_Enable" );
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (!m_fnCreateEx || !m_fnSetGlobalFlags || !m_fnGetStatusEx || !m_fnEnable)
|
|
|
|
{
|
2017-12-16 17:03:16 +00:00
|
|
|
DEBUG_ERROR("Unable to locate required entry points in %s", nvfbc.c_str());
|
2017-11-02 06:45:25 +00:00
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NvFBCStatusEx status;
|
|
|
|
ZeroMemory(&status, sizeof(NvFBCStatusEx));
|
2019-01-03 06:07:32 +00:00
|
|
|
status.dwVersion = NVFBC_STATUS_VER;
|
|
|
|
status.dwAdapterIdx = 0;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NVFBCRESULT ret = m_fnGetStatusEx(&status);
|
|
|
|
if (ret != NVFBC_SUCCESS)
|
2019-01-03 06:07:32 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get NvFBC status");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!status.bIsCapturePossible)
|
2017-11-02 06:45:25 +00:00
|
|
|
{
|
|
|
|
DEBUG_INFO("Attempting to enable NvFBC");
|
2019-01-03 06:07:32 +00:00
|
|
|
switch(m_fnEnable(NVFBC_STATE_ENABLE))
|
2017-10-31 13:51:53 +00:00
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
case NVFBC_SUCCESS:
|
|
|
|
DEBUG_INFO("Success, attempting to get status again");
|
|
|
|
if (m_fnGetStatusEx(&status) != NVFBC_SUCCESS)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get NvFBC status");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NVFBC_ERROR_INSUFFICIENT_PRIVILEGES:
|
|
|
|
DEBUG_ERROR("Please run once as administrator to enable the NvFBC API");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unknown failure enabling NvFBC");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
2017-10-31 13:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
if (!status.bIsCapturePossible)
|
2017-10-31 13:51:53 +00:00
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
DEBUG_ERROR("Capture is not possible, unsupported device or driver");
|
2017-10-31 13:51:53 +00:00
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-02 06:45:25 +00:00
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (!status.bCanCreateNow)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Can not create an instance of NvFBC at this time");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NvFBCCreateParams params;
|
|
|
|
ZeroMemory(¶ms, sizeof(NvFBCCreateParams));
|
2019-01-03 06:07:32 +00:00
|
|
|
params.dwVersion = NVFBC_CREATE_PARAMS_VER;
|
|
|
|
params.dwInterfaceType = NVFBC_TO_SYS;
|
|
|
|
params.pDevice = NULL;
|
|
|
|
params.dwAdapterIdx = 0;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (m_fnCreateEx(¶ms) != NVFBC_SUCCESS)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create an instance of NvFBC");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-31 15:03:26 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
m_maxCaptureWidth = params.dwMaxDisplayWidth;
|
|
|
|
m_maxCaptureHeight = params.dwMaxDisplayHeight;
|
|
|
|
m_nvFBC = static_cast<NvFBCToSys *>(params.pNvFBC);
|
2017-10-31 15:03:26 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
NVFBC_TOSYS_SETUP_PARAMS setupParams;
|
|
|
|
ZeroMemory(&setupParams, sizeof(NVFBC_TOSYS_SETUP_PARAMS));
|
2019-01-03 06:07:32 +00:00
|
|
|
setupParams.dwVersion = NVFBC_TOSYS_SETUP_PARAMS_VER;
|
|
|
|
setupParams.eMode = NVFBC_TOSYS_ARGB;
|
|
|
|
setupParams.bWithHWCursor = TRUE;
|
|
|
|
setupParams.bDiffMap = TRUE;
|
|
|
|
setupParams.eDiffMapBlockSize = NVFBC_TOSYS_DIFFMAP_BLOCKSIZE_128X128;
|
|
|
|
setupParams.ppBuffer = (void **)&m_frameBuffer;
|
|
|
|
setupParams.ppDiffMap = (void **)&m_diffMap;
|
2017-10-31 15:03:26 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (m_nvFBC->NvFBCToSysSetUp(&setupParams) != NVFBC_SUCCESS)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("NvFBCToSysSetUp Failed");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-31 15:03:26 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
// this is required according to NVidia sample code
|
|
|
|
Sleep(100);
|
2017-10-31 15:03:26 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
HMONITOR monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
MONITORINFO monitorInfo;
|
|
|
|
monitorInfo.cbSize = sizeof(MONITORINFO);
|
|
|
|
unsigned int screenWidth, screenHeight;
|
|
|
|
GetMonitorInfo(monitor, &monitorInfo);
|
|
|
|
screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
|
|
|
|
screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top;
|
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
ZeroMemory(&m_grabFrameParams, sizeof(NVFBC_TOSYS_GRAB_FRAME_PARAMS));
|
|
|
|
ZeroMemory(&m_grabInfo, sizeof(NvFBCFrameGrabInfo));
|
2019-01-03 06:07:32 +00:00
|
|
|
m_grabFrameParams.dwVersion = NVFBC_TOSYS_GRAB_FRAME_PARAMS_VER;
|
|
|
|
m_grabFrameParams.dwFlags = m_optNoWait ? NVFBC_TOSYS_NOWAIT : NVFBC_TOSYS_WAIT_WITH_TIMEOUT;
|
|
|
|
m_grabFrameParams.dwWaitTime = 1000;
|
|
|
|
m_grabFrameParams.eGMode = NVFBC_TOSYS_SOURCEMODE_CROP;
|
|
|
|
m_grabFrameParams.dwStartX = 0;
|
|
|
|
m_grabFrameParams.dwStartY = 0;
|
|
|
|
m_grabFrameParams.dwTargetWidth = screenWidth;
|
|
|
|
m_grabFrameParams.dwTargetHeight = screenHeight;
|
2017-11-02 06:45:25 +00:00
|
|
|
m_grabFrameParams.pNvFBCFrameGrabInfo = &m_grabInfo;
|
2017-10-31 15:15:47 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
m_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-31 15:15:47 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
void NvFBC::DeInitialize()
|
|
|
|
{
|
|
|
|
m_frameBuffer = NULL;
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (m_nvFBC)
|
|
|
|
{
|
|
|
|
m_nvFBC->NvFBCToSysRelease();
|
|
|
|
m_nvFBC = NULL;
|
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
m_maxCaptureWidth = 0;
|
2017-11-02 06:45:25 +00:00
|
|
|
m_maxCaptureHeight = 0;
|
2019-01-03 06:07:32 +00:00
|
|
|
m_fnCreateEx = NULL;
|
2017-11-02 06:45:25 +00:00
|
|
|
m_fnSetGlobalFlags = NULL;
|
2019-01-03 06:07:32 +00:00
|
|
|
m_fnGetStatusEx = NULL;
|
|
|
|
m_fnEnable = NULL;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
if (m_hDLL)
|
|
|
|
{
|
|
|
|
FreeLibrary(m_hDLL);
|
|
|
|
m_hDLL = NULL;
|
2017-10-31 13:51:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
m_initialized = false;
|
|
|
|
}
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
FrameType NvFBC::GetFrameType()
|
|
|
|
{
|
|
|
|
if (!m_initialized)
|
|
|
|
return FRAME_TYPE_INVALID;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
return FRAME_TYPE_BGRA;
|
2017-11-02 06:45:25 +00:00
|
|
|
}
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2017-11-02 06:45:25 +00:00
|
|
|
size_t NvFBC::GetMaxFrameSize()
|
|
|
|
{
|
|
|
|
if (!m_initialized)
|
|
|
|
return false;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-11-17 04:24:01 +00:00
|
|
|
return m_maxCaptureWidth * m_maxCaptureHeight * 4;
|
2017-11-02 06:45:25 +00:00
|
|
|
}
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
unsigned int Capture::NvFBC::Capture()
|
2017-11-02 06:45:25 +00:00
|
|
|
{
|
|
|
|
if (!m_initialized)
|
2017-12-07 19:24:17 +00:00
|
|
|
return GRAB_STATUS_ERROR;
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2017-11-02 06:45:25 +00:00
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
NVFBCRESULT status = m_nvFBC->NvFBCToSysGrabFrame(&m_grabFrameParams);
|
2017-11-02 06:45:25 +00:00
|
|
|
if (status == NVFBC_SUCCESS)
|
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
const int diffW = (m_grabInfo.dwWidth + 0x7F) >> 7;
|
2017-12-05 13:14:45 +00:00
|
|
|
const int diffH = (m_grabInfo.dwHeight + 0x7F) >> 7;
|
2017-12-05 09:39:54 +00:00
|
|
|
bool hasDiff = false;
|
2019-01-03 06:07:32 +00:00
|
|
|
for (int y = 0; y < diffH && !hasDiff; ++y)
|
2017-12-05 13:14:45 +00:00
|
|
|
for (int x = 0; x < diffW; ++x)
|
|
|
|
if (m_diffMap[y * diffW + x])
|
|
|
|
{
|
|
|
|
hasDiff = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-05 09:39:54 +00:00
|
|
|
|
|
|
|
if (!hasDiff)
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-03 06:07:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (status == NVFBC_ERROR_DYNAMIC_DISABLE)
|
2017-12-08 06:51:47 +00:00
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
DEBUG_ERROR("NvFBC was disabled by someone else");
|
|
|
|
return GRAB_STATUS_ERROR;
|
2017-12-08 06:51:47 +00:00
|
|
|
}
|
2019-01-03 06:07:32 +00:00
|
|
|
|
|
|
|
if (status == NVFBC_ERROR_INVALIDATED_SESSION)
|
2017-11-16 09:53:22 +00:00
|
|
|
{
|
2019-01-03 06:07:32 +00:00
|
|
|
DEBUG_WARN("Session was invalidated, attempting to restart");
|
|
|
|
return GRAB_STATUS_REINIT;
|
|
|
|
}
|
2017-12-08 06:51:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
if (i == 1)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("NvFBCToSysGrabFrame failed");
|
|
|
|
return GRAB_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 06:51:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
// if the capture size doesn't match the screen resolution then re-initialize to avoid
|
|
|
|
// copying black/blank areas of the screen
|
|
|
|
HMONITOR monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
MONITORINFO monitorInfo;
|
|
|
|
monitorInfo.cbSize = sizeof(MONITORINFO);
|
|
|
|
unsigned int screenWidth, screenHeight;
|
|
|
|
GetMonitorInfo(monitor, &monitorInfo);
|
|
|
|
screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
|
|
|
|
screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top;
|
|
|
|
if (m_grabInfo.dwWidth != screenWidth || m_grabInfo.dwHeight != screenHeight)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("Resolution change detected");
|
|
|
|
return GRAB_STATUS_REINIT;
|
|
|
|
}
|
2017-12-08 06:51:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
return GRAB_STATUS_OK | GRAB_STATUS_FRAME;
|
|
|
|
}
|
2017-11-16 09:53:22 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
bool Capture::NvFBC::GetCursor(CursorInfo & cursor)
|
|
|
|
{
|
|
|
|
cursor.hasShape = false;
|
|
|
|
cursor.hasPos = false;
|
|
|
|
cursor.visible = false;
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-16 09:53:22 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
void Capture::NvFBC::FreeCursor()
|
|
|
|
{
|
|
|
|
}
|
2017-11-16 03:00:40 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
GrabStatus Capture::NvFBC::DiscardFrame()
|
|
|
|
{
|
|
|
|
return GrabStatus();
|
|
|
|
}
|
2017-11-02 06:45:25 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
enum GrabStatus NvFBC::GetFrame(struct FrameInfo & frame)
|
|
|
|
{
|
|
|
|
if (!m_initialized)
|
|
|
|
return GRAB_STATUS_ERROR;
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
frame.width = m_grabInfo.dwWidth;
|
|
|
|
frame.height = m_grabInfo.dwHeight;
|
|
|
|
frame.stride = m_grabInfo.dwBufferWidth;
|
|
|
|
frame.pitch = m_grabInfo.dwBufferWidth * 4;
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2019-01-03 06:07:32 +00:00
|
|
|
memcpySSE((uint8_t*)frame.buffer, (uint8_t *)m_frameBuffer, frame.pitch * frame.height);
|
|
|
|
return GRAB_STATUS_OK;
|
2017-11-02 06:45:25 +00:00
|
|
|
}
|
2017-12-14 22:51:23 +00:00
|
|
|
|
|
|
|
#endif// CONFIG_CAPTURE_NVFBC
|