From 9ac3cadc7d23299fd60cacc0976b588d5801fb09 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Wed, 1 Nov 2017 02:15:47 +1100 Subject: [PATCH] [host] added multithreaded memcopy implementation --- host/Capture/NvFBC.cpp | 15 ++++- host/Capture/NvFBC.h | 4 +- host/MTMemcpy.cpp | 79 +++++++++++++++++++++++++++ host/MTMemcpy.h | 37 +++++++++++++ host/kvm-ivshmem-host.vcxproj | 2 + host/kvm-ivshmem-host.vcxproj.filters | 6 ++ 6 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 host/MTMemcpy.cpp create mode 100644 host/MTMemcpy.h diff --git a/host/Capture/NvFBC.cpp b/host/Capture/NvFBC.cpp index d115e668..97e71207 100644 --- a/host/Capture/NvFBC.cpp +++ b/host/Capture/NvFBC.cpp @@ -123,12 +123,21 @@ namespace Capture m_grabFrameParams.eGMode = NVFBC_TOSYS_SOURCEMODE_SCALE; m_grabFrameParams.pNvFBCFrameGrabInfo = &m_grabInfo; + if (!m_memcpy.Initialize()) + { + DEBUG_ERROR("Failed to initialize MTMemcpy"); + DeInitialize(); + return false; + } + m_initialized = true; return true; } void NvFBC::DeInitialize() { + m_memcpy.DeInitialize(); + m_frameBuffer = NULL; if (m_nvFBC) @@ -197,7 +206,11 @@ namespace Capture frame.height = m_grabInfo.dwHeight; frame.stride = m_grabInfo.dwBufferWidth; frame.outSize = m_grabInfo.dwBufferWidth * m_grabInfo.dwHeight * 3; - memcpy(frame.buffer, m_frameBuffer, frame.outSize); + if (!m_memcpy.Copy(frame.buffer, m_frameBuffer, frame.outSize)) + { + DEBUG_ERROR("Memory copy failed"); + return false; + } return true; } diff --git a/host/Capture/NvFBC.h b/host/Capture/NvFBC.h index 8418de1f..0ab13bdb 100644 --- a/host/Capture/NvFBC.h +++ b/host/Capture/NvFBC.h @@ -1,5 +1,6 @@ #pragma once #include "ICapture.h" +#include "MTMemcpy.h" #define W32_LEAN_AND_MEAN #include @@ -24,7 +25,8 @@ namespace Capture private: bool m_initialized; - HMODULE m_hDLL; + HMODULE m_hDLL; + MTMemcpy m_memcpy; NvFBC_CreateFunctionExType m_fnCreateEx; NvFBC_SetGlobalFlagsType m_fnSetGlobalFlags; diff --git a/host/MTMemcpy.cpp b/host/MTMemcpy.cpp new file mode 100644 index 00000000..e73970cb --- /dev/null +++ b/host/MTMemcpy.cpp @@ -0,0 +1,79 @@ +#include "MTMemcpy.h" + +MTMemcpy::MTMemcpy() : + m_initialized(false) +{ + +} + +MTMemcpy::~MTMemcpy() +{ + DeInitialize(); +} + +DWORD WINAPI MTMemcpy::thread_copy_proc(LPVOID param) +{ + mt_cpy_t * p = (mt_cpy_t *)param; + + while (1) + { + WaitForSingleObject(p->s->hCopyStartSemaphores[p->ct], INFINITE); + memcpy(p->dest, p->src, p->size); + ReleaseSemaphore(p->s->hCopyStopSemaphores[p->ct], 1, NULL); + } + + return 0; +} + +bool MTMemcpy::Initialize() +{ + if (m_initialized) + DeInitialize(); + + for (int ctr = 0; ctr < NUM_CPY_THREADS; ctr++) + { + hCopyStartSemaphores[ctr] = CreateSemaphore(NULL, 0, 1, NULL); + hCopyStopSemaphores[ctr] = CreateSemaphore(NULL, 0, 1, NULL); + mtParamters[ctr].s = this; + mtParamters[ctr].ct = ctr; + hCopyThreads[ctr] = CreateThread(0, 0, thread_copy_proc, &mtParamters[ctr], 0, NULL); + } + + m_initialized = true; + return true; +} + +bool MTMemcpy::Copy(void * dest, void * src, size_t bytes) +{ + if (!m_initialized) + return false; + + //set up parameters + for (int ctr = 0; ctr < NUM_CPY_THREADS; ctr++) + { + mtParamters[ctr].dest = (char *)dest + ctr * bytes / NUM_CPY_THREADS; + mtParamters[ctr].src = (char *)src + ctr * bytes / NUM_CPY_THREADS; + mtParamters[ctr].size = (ctr + 1) * bytes / NUM_CPY_THREADS - ctr * bytes / NUM_CPY_THREADS; + ReleaseSemaphore(hCopyStartSemaphores[ctr], 1, NULL); + } + + //wait for all threads to finish + WaitForMultipleObjects(NUM_CPY_THREADS, hCopyStopSemaphores, TRUE, INFINITE); + + return true; +} + +void MTMemcpy::DeInitialize() +{ + if (!m_initialized) + return; + + for (int ctr = 0; ctr < NUM_CPY_THREADS; ctr++) + { + TerminateThread(hCopyThreads[ctr], 0); + CloseHandle(hCopyStartSemaphores[ctr]); + CloseHandle(hCopyStopSemaphores[ctr]); + } + + m_initialized = false; +} \ No newline at end of file diff --git a/host/MTMemcpy.h b/host/MTMemcpy.h new file mode 100644 index 00000000..9f832cc1 --- /dev/null +++ b/host/MTMemcpy.h @@ -0,0 +1,37 @@ +#pragma once + +#define W32_LEAN_AND_MEAN +#include + +#define NUM_CPY_THREADS 4 + +class MTMemcpy +{ +public: + bool MTMemcpy::Initialize(); + void MTMemcpy::DeInitialize(); + bool MTMemcpy::Copy(void * dest, void * src, size_t bytes); + + MTMemcpy(); + ~MTMemcpy(); + +private: + bool m_initialized; + static DWORD WINAPI MTMemcpy::thread_copy_proc(LPVOID param); + + typedef struct + { + MTMemcpy * s; + int ct; + void * src; + void * dest; + size_t size; + } + mt_cpy_t; + + HANDLE hCopyThreads[NUM_CPY_THREADS] = { 0 }; + HANDLE hCopyStartSemaphores[NUM_CPY_THREADS] = { 0 }; + HANDLE hCopyStopSemaphores[NUM_CPY_THREADS] = { 0 }; + + mt_cpy_t mtParamters[NUM_CPY_THREADS] = { 0 }; +}; \ No newline at end of file diff --git a/host/kvm-ivshmem-host.vcxproj b/host/kvm-ivshmem-host.vcxproj index dc6f1ce8..9cef2947 100644 --- a/host/kvm-ivshmem-host.vcxproj +++ b/host/kvm-ivshmem-host.vcxproj @@ -163,6 +163,7 @@ + @@ -170,6 +171,7 @@ + diff --git a/host/kvm-ivshmem-host.vcxproj.filters b/host/kvm-ivshmem-host.vcxproj.filters index 379721b9..af101ef0 100644 --- a/host/kvm-ivshmem-host.vcxproj.filters +++ b/host/kvm-ivshmem-host.vcxproj.filters @@ -33,6 +33,9 @@ Source Files\Capture + + Source Files + @@ -53,5 +56,8 @@ Header Files + + Header Files + \ No newline at end of file