diff --git a/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86/uDesktopDuplication.dll index 67efd53..f65a3a4 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 96be68f..2c03a22 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 86babce..032cbc6 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp @@ -15,39 +15,8 @@ using namespace Microsoft::WRL; -const Microsoft::WRL::ComPtr& SharedTextureWrapper::Get() const +IsolatedD3D11Device::IsolatedD3D11Device() { - return pointer_; -} - - -Microsoft::WRL::ComPtr SharedTextureWrapper::Lock() -{ - if (locked_) return nullptr; - locked_ = true; - return pointer_; -} - - -void SharedTextureWrapper::Unlock() -{ - locked_ = false; -} - - -bool SharedTextureWrapper::IsLocked() const -{ - return locked_; -} - - - -IsolatedD3D11Device::IsolatedD3D11Device(UINT cachedTextureNum) -{ - for (UINT i = 0; i < cachedTextureNum; ++i) - { - cachedSharedTextures_.push_back(std::make_shared()); - } } @@ -96,42 +65,33 @@ ComPtr IsolatedD3D11Device::GetDevice() } -std::shared_ptr IsolatedD3D11Device::GetCompatibleSharedTexture( - const ComPtr& src, - UINT index) +Microsoft::WRL::ComPtr IsolatedD3D11Device::GetCompatibleSharedTexture( + const ComPtr& src) { - if (index < 0 || index >= cachedSharedTextures_.size()) - { - Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => ", index, " is out of cachedTextures range."); - return nullptr; - } - - auto& sharedTextureWrapper = cachedSharedTextures_.at(index); - auto& texture = sharedTextureWrapper->pointer_; // raw pointer - - // check if the format and size of the current texture are same as the source one D3D11_TEXTURE2D_DESC srcDesc; src->GetDesc(&srcDesc); - if (texture) + + // check if the format and size of the current texture are same as the source one + if (cachedSharedTexture_) { D3D11_TEXTURE2D_DESC targetDesc; - texture->GetDesc(&targetDesc); + cachedSharedTexture_->GetDesc(&targetDesc); if (targetDesc.Format == srcDesc.Format && targetDesc.Width == srcDesc.Width && targetDesc.Height == srcDesc.Height) { - return sharedTextureWrapper; + return cachedSharedTexture_; } } // for sharing this texture with unity device srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; - if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &texture))) + if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedSharedTexture_))) { Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed."); return nullptr; } - return sharedTextureWrapper; + return cachedSharedTexture_; } \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h index 6b2cff4..8058844 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h @@ -7,36 +7,19 @@ #include -// Shared texture wrapper to manager locking state -class SharedTextureWrapper -{ -friend class IsolatedD3D11Device; -public: - const Microsoft::WRL::ComPtr& Get() const; - Microsoft::WRL::ComPtr Lock(); - void Unlock(); - bool IsLocked() const; - -private: - std::atomic locked_ = false; - Microsoft::WRL::ComPtr pointer_; -}; - - // Thraed safe self created ID3D11Device from specified adapter class IsolatedD3D11Device { public: - explicit IsolatedD3D11Device(UINT cachedTextureNum); + IsolatedD3D11Device(); ~IsolatedD3D11Device(); HRESULT Create(const Microsoft::WRL::ComPtr& adapter); Microsoft::WRL::ComPtr GetDevice(); - std::shared_ptr GetCompatibleSharedTexture( - const Microsoft::WRL::ComPtr& src, - UINT index); + Microsoft::WRL::ComPtr GetCompatibleSharedTexture( + const Microsoft::WRL::ComPtr& src); private: Microsoft::WRL::ComPtr device_; - std::vector> cachedSharedTextures_; + Microsoft::WRL::ComPtr cachedSharedTexture_; }; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp index df9a8af..c1eb538 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.cpp @@ -33,8 +33,7 @@ Duplicator::~Duplicator() void Duplicator::InitializeDevice() { - const UINT cachedTextureNum = 2; - device_ = std::make_shared(cachedTextureNum); + device_ = std::make_shared(); if (FAILED(device_->Create(monitor_->GetAdapter()))) { @@ -315,18 +314,13 @@ void Duplicator::Duplicate(UINT timeout) return; } - //auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, lastFrameId_ % 2); - auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, 0); - if (!sharedTextureWrapper) + auto sharedTexture = device_->GetCompatibleSharedTexture(texture); + if (!sharedTexture) { Debug::Error("Duplicator::Duplicate() => Shared texture is null."); return; } - auto sharedTexture = sharedTextureWrapper->Lock(); - if (!sharedTexture) return; - ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); }); - { UDD_SCOPE_TIMER(CopyResource); ComPtr context; @@ -342,7 +336,7 @@ void Duplicator::Duplicate(UINT timeout) lastFrame_ = Frame { lastFrameId_++, - sharedTextureWrapper, + sharedTexture, frameInfo, metaData_ }; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h index b52d361..3f4523b 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Duplicator.h @@ -45,7 +45,7 @@ public: struct Frame { UINT id; - std::shared_ptr texture; + Microsoft::WRL::ComPtr texture; DXGI_OUTDUPL_FRAME_INFO info; Metadata metaData; }; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index 9b148b6..f5b927f 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -75,15 +75,10 @@ void Monitor::Render() if (frame.id == lastFrameId_) return; lastFrameId_ = frame.id; - const auto& sharedTextureWrapper = frame.texture; - auto sharedTexture = sharedTextureWrapper->Lock(); - if (!sharedTexture) return; - ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); }); - if (unityTexture_) { D3D11_TEXTURE2D_DESC srcDesc, dstDesc; - sharedTexture->GetDesc(&srcDesc); + frame.texture->GetDesc(&srcDesc); unityTexture_->GetDesc(&dstDesc); if (srcDesc.Width != dstDesc.Width || srcDesc.Height != dstDesc.Height) @@ -97,7 +92,7 @@ void Monitor::Render() { ComPtr context; GetDevice()->GetImmediateContext(&context); - context->CopyResource(unityTexture_, sharedTexture.Get()); + context->CopyResource(unityTexture_, frame.texture.Get()); auto cursor = GetMonitorManager()->GetCursor(); cursor->Draw(unityTexture_);