From a6addc303bfd1d3550ba7abb7244beb501cca2b9 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 10 Jul 2017 18:13:33 -0700 Subject: [PATCH] Using information available instead of getting it again in the ProcessFrame Functions --- include/Internal/ThreadManager.h | 183 ++++++++++++++-------------- include/windows/DXFrameProcessor.h | 2 +- include/windows/GDIFrameProcessor.h | 2 +- src/windows/DXFrameProcessor.cpp | 4 +- src/windows/GDIFrameProcessor.cpp | 3 +- 5 files changed, 99 insertions(+), 95 deletions(-) diff --git a/include/Internal/ThreadManager.h b/include/Internal/ThreadManager.h index b268f6bd..9adeb27c 100644 --- a/include/Internal/ThreadManager.h +++ b/include/Internal/ThreadManager.h @@ -12,109 +12,112 @@ using namespace std::chrono_literals; // this is internal stuff.. namespace SL { -namespace Screen_Capture -{ - class ThreadManager + namespace Screen_Capture { + class ThreadManager + { - std::vector m_ThreadHandles; - std::shared_ptr TerminateThreadsEvent; + std::vector m_ThreadHandles; + std::shared_ptr TerminateThreadsEvent; - public: - ThreadManager(); - ~ThreadManager(); - void Init(const std::shared_ptr& settings); - void Join(); - }; - template bool TryCaptureMouse(const F& data) - { - T frameprocessor; - auto ret = frameprocessor.Init(data); - if(ret != DUPL_RETURN_SUCCESS) { - return false; - } - frameprocessor.ImageBufferSize = frameprocessor.MaxCursurorSize * frameprocessor.MaxCursurorSize * PixelStride; + public: + ThreadManager(); + ~ThreadManager(); + void Init(const std::shared_ptr& settings); + void Join(); + }; + template bool TryCaptureMouse(const F& data) + { + T frameprocessor; + auto ret = frameprocessor.Init(data); + if (ret != DUPL_RETURN_SUCCESS) { + return false; + } + frameprocessor.ImageBufferSize = frameprocessor.MaxCursurorSize * frameprocessor.MaxCursurorSize * PixelStride; - frameprocessor.OldImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); - frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); + frameprocessor.OldImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); + frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); - while(!data->TerminateThreadsEvent) { - // get a copy of the shared_ptr in a safe way - auto timer = std::atomic_load(&data->Mouse_Capture_Timer); - timer->start(); - // Process Frame - ret = frameprocessor.ProcessFrame(); - if(ret != DUPL_RETURN_SUCCESS) { - if(ret == DUPL_RETURN_ERROR_EXPECTED) { - // The system is in a transition state so request the duplication be restarted - data->ExpectedErrorEvent = true; - std::cout << "Exiting Thread due to expected error " << std::endl; - } else { - // Unexpected error so exit the application - data->UnexpectedErrorEvent = true; - std::cout << "Exiting Thread due to Unexpected error " << std::endl; + while (!data->TerminateThreadsEvent) { + // get a copy of the shared_ptr in a safe way + auto timer = std::atomic_load(&data->Mouse_Capture_Timer); + timer->start(); + // Process Frame + ret = frameprocessor.ProcessFrame(); + if (ret != DUPL_RETURN_SUCCESS) { + if (ret == DUPL_RETURN_ERROR_EXPECTED) { + // The system is in a transition state so request the duplication be restarted + data->ExpectedErrorEvent = true; + std::cout << "Exiting Thread due to expected error " << std::endl; + } + else { + // Unexpected error so exit the application + data->UnexpectedErrorEvent = true; + std::cout << "Exiting Thread due to Unexpected error " << std::endl; + } + return true; + } + timer->wait(); + while (data->Paused) { + std::this_thread::sleep_for(50ms); } - return true; - } - timer->wait(); - while(data->Paused) { - std::this_thread::sleep_for(50ms); } + return true; } - return true; - } - - template bool TryCaptureMonitor(const F& data, Monitor& monitor) - { - T frameprocessor; - auto ret = frameprocessor.Init(data, monitor); - if(ret != DUPL_RETURN_SUCCESS) { - return false; - } - frameprocessor.ImageBufferSize = Width(monitor) * Height(monitor) * PixelStride; - if(data->CaptureDifMonitor) { // only need the old buffer if difs are needed. If no dif is needed, then the - // image is always new - frameprocessor.OldImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); - frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); - } - if((data->CaptureEntireMonitor) && !frameprocessor.NewImageBuffer) { - - frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); - } - while(!data->TerminateThreadsEvent) { - // get a copy of the shared_ptr in a safe way - auto timer = std::atomic_load(&data->Monitor_Capture_Timer); - timer->start(); - auto monitors = GetMonitors(); - if(isMonitorInsideBounds(monitors, monitor)) { - ret = frameprocessor.ProcessFrame(); - } else { - // something happened, rebuild - ret = DUPL_RETURN_ERROR_EXPECTED; + + template bool TryCaptureMonitor(const F& data, Monitor& monitor) + { + T frameprocessor; + auto ret = frameprocessor.Init(data, monitor); + if (ret != DUPL_RETURN_SUCCESS) { + return false; } + frameprocessor.ImageBufferSize = Width(monitor) * Height(monitor) * PixelStride; + if (data->CaptureDifMonitor) { // only need the old buffer if difs are needed. If no dif is needed, then the + // image is always new + frameprocessor.OldImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); + frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); + } + if ((data->CaptureEntireMonitor) && !frameprocessor.NewImageBuffer) { - if(ret != DUPL_RETURN_SUCCESS) { - if(ret == DUPL_RETURN_ERROR_EXPECTED) { - // The system is in a transition state so request the duplication be restarted - data->ExpectedErrorEvent = true; - std::cout << "Exiting Thread due to expected error " << std::endl; - } else { - // Unexpected error so exit the application - data->UnexpectedErrorEvent = true; - std::cout << "Exiting Thread due to Unexpected error " << std::endl; - } - return true; + frameprocessor.NewImageBuffer = std::make_unique(frameprocessor.ImageBufferSize); } - timer->wait(); - while(data->Paused) { - std::this_thread::sleep_for(50ms); + while (!data->TerminateThreadsEvent) { + // get a copy of the shared_ptr in a safe way + auto timer = std::atomic_load(&data->Monitor_Capture_Timer); + timer->start(); + auto monitors = GetMonitors(); + if (isMonitorInsideBounds(monitors, monitor)) { + ret = frameprocessor.ProcessFrame(monitors[Index(monitor)]); + } + else { + // something happened, rebuild + ret = DUPL_RETURN_ERROR_EXPECTED; + } + + if (ret != DUPL_RETURN_SUCCESS) { + if (ret == DUPL_RETURN_ERROR_EXPECTED) { + // The system is in a transition state so request the duplication be restarted + data->ExpectedErrorEvent = true; + std::cout << "Exiting Thread due to expected error " << std::endl; + } + else { + // Unexpected error so exit the application + data->UnexpectedErrorEvent = true; + std::cout << "Exiting Thread due to Unexpected error " << std::endl; + } + return true; + } + timer->wait(); + while (data->Paused) { + std::this_thread::sleep_for(50ms); + } } + return true; } - return true; - } - void RunCaptureMonitor(std::shared_ptr data, Monitor monitor); - void RunCaptureMouse(std::shared_ptr data); -} + void RunCaptureMonitor(std::shared_ptr data, Monitor monitor); + void RunCaptureMouse(std::shared_ptr data); + } } diff --git a/include/windows/DXFrameProcessor.h b/include/windows/DXFrameProcessor.h index b60c7d5c..9420581b 100644 --- a/include/windows/DXFrameProcessor.h +++ b/include/windows/DXFrameProcessor.h @@ -27,7 +27,7 @@ namespace SL { public: DUPL_RETURN Init(std::shared_ptr data, Monitor& monitor); - DUPL_RETURN ProcessFrame(); + DUPL_RETURN ProcessFrame(const Monitor& currentmonitorinfo); }; diff --git a/include/windows/GDIFrameProcessor.h b/include/windows/GDIFrameProcessor.h index 7a42827f..e7d5986c 100644 --- a/include/windows/GDIFrameProcessor.h +++ b/include/windows/GDIFrameProcessor.h @@ -15,7 +15,7 @@ namespace SL { std::shared_ptr Data; public: DUPL_RETURN Init(std::shared_ptr data, Monitor& monitor); - DUPL_RETURN ProcessFrame(); + DUPL_RETURN ProcessFrame(const Monitor& currentmonitorinfo); }; } diff --git a/src/windows/DXFrameProcessor.cpp b/src/windows/DXFrameProcessor.cpp index 4007e782..f3530a64 100644 --- a/src/windows/DXFrameProcessor.cpp +++ b/src/windows/DXFrameProcessor.cpp @@ -348,7 +348,7 @@ namespace SL { // Process a given frame and its metadata // - DUPL_RETURN DXFrameProcessor::ProcessFrame() + DUPL_RETURN DXFrameProcessor::ProcessFrame(const Monitor& currentmonitorinfo) { auto Ret = DUPL_RETURN_SUCCESS; @@ -394,7 +394,7 @@ namespace SL { return ProcessFailure(Device.Get(), L"Failed to create staging texture for move rects", L"Error", hr, SystemTransitionsExpectedErrors); } } - if (ThisDesc.Width == Width(SelectedMonitor) && ThisDesc.Height == Height(SelectedMonitor)) { + if (Width(currentmonitorinfo) == Width(SelectedMonitor) && Height(currentmonitorinfo) == Height(SelectedMonitor)) { DeviceContext->CopyResource(StagingSurf.Get(), aquireddesktopimage.Get()); } else { diff --git a/src/windows/GDIFrameProcessor.cpp b/src/windows/GDIFrameProcessor.cpp index cf336968..854fee78 100644 --- a/src/windows/GDIFrameProcessor.cpp +++ b/src/windows/GDIFrameProcessor.cpp @@ -21,8 +21,9 @@ namespace SL { // // Process a given frame and its metadata // - DUPL_RETURN GDIFrameProcessor::ProcessFrame() + DUPL_RETURN GDIFrameProcessor::ProcessFrame(const Monitor& currentmonitorinfo) { + auto Ret = DUPL_RETURN_SUCCESS; ImageRect ret;