add timeout to avoid unnecessarily high cpu usage.
This commit is contained in:
@@ -62,3 +62,18 @@ void SendMessageToUnity(Message message)
|
||||
{
|
||||
g_messages.push(message);
|
||||
}
|
||||
|
||||
|
||||
ScopedTimer::ScopedTimer(TimerFuncType&& func)
|
||||
: func_(func)
|
||||
, start_(std::chrono::high_resolution_clock::now())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ScopedTimer::~ScopedTimer()
|
||||
{
|
||||
const auto end = std::chrono::high_resolution_clock::now();
|
||||
const auto time = std::chrono::duration_cast<microseconds>(end - start_);
|
||||
func_(time);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include <wrl/client.h>
|
||||
|
||||
|
||||
@@ -32,7 +33,23 @@ public:
|
||||
~ScopedReleaser() { func_(); }
|
||||
|
||||
private:
|
||||
ReleaseFuncType func_;
|
||||
const ReleaseFuncType func_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Timer
|
||||
class ScopedTimer
|
||||
{
|
||||
public:
|
||||
using microseconds = std::chrono::microseconds;
|
||||
using TimerFuncType = std::function<void(microseconds)>;
|
||||
ScopedTimer(TimerFuncType&& func);
|
||||
~ScopedTimer();
|
||||
|
||||
private:
|
||||
const TimerFuncType func_;
|
||||
const std::chrono::time_point<std::chrono::steady_clock> start_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "Duplicator.h"
|
||||
#include "Monitor.h"
|
||||
#include "MonitorManager.h"
|
||||
@@ -139,12 +141,25 @@ void Duplicator::Start()
|
||||
|
||||
thread_ = std::thread([this]
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
state_ = State::Running;
|
||||
|
||||
shouldRun_ = true;
|
||||
while (shouldRun_)
|
||||
{
|
||||
if (!Duplicate()) break;
|
||||
const UINT timeout = 16 /* milliseconds */;
|
||||
|
||||
ScopedTimer timer([timeout] (microseconds us)
|
||||
{
|
||||
const auto waitTime = milliseconds(timeout) - us;
|
||||
if (waitTime > microseconds::zero())
|
||||
{
|
||||
std::this_thread::sleep_for(waitTime);
|
||||
}
|
||||
});
|
||||
|
||||
if (!Duplicate(timeout)) break;
|
||||
}
|
||||
|
||||
if (state_ == State::Running)
|
||||
@@ -211,12 +226,10 @@ const Duplicator::Frame& Duplicator::GetLastFrame() const
|
||||
}
|
||||
|
||||
|
||||
bool Duplicator::Duplicate()
|
||||
bool Duplicator::Duplicate(UINT timeout)
|
||||
{
|
||||
if (!dupl_ || !device_) return false;
|
||||
|
||||
const UINT timeout = 16;
|
||||
|
||||
ComPtr<IDXGIResource> resource;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
const auto hr = dupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
|
||||
|
||||
@@ -67,7 +67,7 @@ private:
|
||||
void InitializeDuplication();
|
||||
void CheckUnityAdapter();
|
||||
|
||||
bool Duplicate();
|
||||
bool Duplicate(UINT timeout);
|
||||
|
||||
void UpdateCursor(
|
||||
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture,
|
||||
|
||||
Reference in New Issue
Block a user