diff --git a/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll index 773d708..2aaf09a 100644 Binary files a/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll and b/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll differ diff --git a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll index 28b6a90..3b1ab98 100644 Binary files a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll and b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll differ diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp index 87c2847..01240e7 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp @@ -14,7 +14,8 @@ using namespace Microsoft::WRL; -IsolatedD3D11Device::IsolatedD3D11Device() +IsolatedD3D11Device::IsolatedD3D11Device(UINT cachedTextureNum) + : cachedTextures_(cachedTextureNum) { } @@ -64,33 +65,42 @@ ComPtr IsolatedD3D11Device::GetDevice() } -ComPtr IsolatedD3D11Device::GetCompatibleSharedTexture(const ComPtr& src) +ComPtr IsolatedD3D11Device::GetCompatibleSharedTexture( + const ComPtr& src, + UINT index) { + if (index < 0 || index >= cachedTextures_.size()) + { + Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => ", index, " is out of cachedTextures range."); + return nullptr; + } + + auto& cachedTexture = cachedTextures_.at(index); + D3D11_TEXTURE2D_DESC srcDesc; src->GetDesc(&srcDesc); // check if the format and size of the current texture are same as the source one - if (cachedTexture_) + if (cachedTexture) { D3D11_TEXTURE2D_DESC targetDesc; - cachedTexture_->GetDesc(&targetDesc); + cachedTexture->GetDesc(&targetDesc); if (targetDesc.Format == srcDesc.Format && targetDesc.Width == srcDesc.Width && targetDesc.Height == srcDesc.Height) { - return cachedTexture_; + return cachedTexture; } - cachedTexture_ = nullptr; } // for sharing this texture with unity device srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; - if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedTexture_))) + if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedTexture))) { Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed."); return nullptr; } - return cachedTexture_; + return cachedTexture; } \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h index 4b6af1e..70db375 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -8,15 +9,16 @@ class IsolatedD3D11Device { public: - IsolatedD3D11Device(); + explicit IsolatedD3D11Device(UINT cachedTextureNum); ~IsolatedD3D11Device(); HRESULT Create(const Microsoft::WRL::ComPtr& adapter); Microsoft::WRL::ComPtr GetDevice(); Microsoft::WRL::ComPtr GetCompatibleSharedTexture( - const Microsoft::WRL::ComPtr& src); + const Microsoft::WRL::ComPtr& src, + UINT index); private: Microsoft::WRL::ComPtr device_; - Microsoft::WRL::ComPtr cachedTexture_; + std::vector> cachedTextures_; }; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp index 065d7dc..b13dc35 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp @@ -31,7 +31,8 @@ Duplicator::~Duplicator() void Duplicator::InitializeDevice() { - device_ = std::make_shared(); + const UINT cachedTextureNum = 2; + device_ = std::make_shared(cachedTextureNum); if (FAILED(device_->Create(monitor_->GetAdapter()))) { @@ -210,6 +211,17 @@ const Duplicator::Frame& Duplicator::GetLastFrame() const } +void Duplicator::CopyLastTexture( + const Microsoft::WRL::ComPtr& device, + const Microsoft::WRL::ComPtr& texture) +{ + std::lock_guard lock(mutex_); + ComPtr context; + device->GetImmediateContext(&context); + context->CopyResource(texture.Get(), lastFrame_.texture.Get()); +} + + bool Duplicator::Duplicate() { if (!dupl_ || !device_) return false; @@ -291,22 +303,23 @@ bool Duplicator::Duplicate() return false; } - auto copyTarget = device_->GetCompatibleSharedTexture(texture); - if (!copyTarget) + auto sharedTexture = device_->GetCompatibleSharedTexture(texture, frame % 2); + if (!sharedTexture) { return false; } ComPtr context; device_->GetDevice()->GetImmediateContext(&context); - context->CopyResource(copyTarget.Get(), texture.Get()); + context->CopyResource(sharedTexture.Get(), texture.Get()); - UpdateCursor(copyTarget, frameInfo); + UpdateCursor(sharedTexture, frameInfo); UpdateMetadata(frameInfo.TotalMetadataBufferSize); { std::lock_guard lock(mutex_); - lastFrame_.texture = copyTarget; + lastFrame_.frame = frame++; + lastFrame_.texture = sharedTexture; lastFrame_.info = frameInfo; lastFrame_.metaData = metaData_; } diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h index 1d24c1a..0b98820 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h @@ -44,6 +44,7 @@ public: struct Frame { + UINT frame = 0; Microsoft::WRL::ComPtr texture; DXGI_OUTDUPL_FRAME_INFO info; Metadata metaData; @@ -60,6 +61,9 @@ public: Microsoft::WRL::ComPtr GetDevice(); Microsoft::WRL::ComPtr GetDuplication(); const Frame& GetLastFrame() const; + void CopyLastTexture( + const Microsoft::WRL::ComPtr& device, + const Microsoft::WRL::ComPtr& texture); private: void InitializeDevice(); @@ -81,6 +85,7 @@ private: std::shared_ptr device_; Microsoft::WRL::ComPtr dupl_; Frame lastFrame_; + UINT frame = 0; volatile bool shouldRun_ = false; std::thread thread_; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index b0cf818..5aa4e4a 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -71,8 +71,7 @@ void Monitor::Finalize() void Monitor::Render() { const auto& frame = duplicator_->GetLastFrame(); - const auto texture = frame.texture; - const auto frameInfo = frame.info; // clone + const auto& texture = frame.texture; if (!texture) return; @@ -97,9 +96,7 @@ void Monitor::Render() } else { - ComPtr context; - GetDevice()->GetImmediateContext(&context); - context->CopyResource(unityTexture_, texture.Get()); + duplicator_->CopyLastTexture(GetDevice(), unityTexture_); } }