diff --git a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll index 9a666f6..3e587b8 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/Common.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Common.h index 170d01b..a8958d8 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Common.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Common.h @@ -17,6 +17,17 @@ class MonitorManager; const std::unique_ptr& GetMonitorManager(); +template +auto MakeUniqueWithReleaser(T* ptr) +{ + const auto deleter = [](T* ptr) + { + if (ptr != nullptr) ptr->Release(); + }; + return std::unique_ptr(ptr, deleter); +} + + // Message is pooled and fetch from Unity. enum class Message { diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp index 11fc2ab..5c79d00 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Cursor.cpp @@ -58,12 +58,12 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo) // Get mouse pointer information UINT bufferSize; DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo; - if (FAILED( - monitor_->GetDeskDupl()->GetFramePointerShape( - apiBufferSize_, - reinterpret_cast(apiBuffer_.get()), - &bufferSize, - &shapeInfo))) + const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape( + apiBufferSize_, + reinterpret_cast(apiBuffer_.get()), + &bufferSize, + &shapeInfo); + if (FAILED(hr)) { Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed."); apiBuffer_.reset(); @@ -149,12 +149,13 @@ void Cursor::UpdateTexture() desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.MiscFlags = 0; - ComPtr texture; + ID3D11Texture2D* texture; if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture))) { Debug::Error("Cursor::UpdateTexture() => GetDevice()->CreateTexture2D() failed."); return; } + const auto textureReleaser = MakeUniqueWithReleaser(texture); D3D11_BOX box; box.front = 0; @@ -170,16 +171,18 @@ void Cursor::UpdateTexture() return; } - ComPtr context; + ID3D11DeviceContext* context; GetDevice()->GetImmediateContext(&context); - context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box); + context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box); + context->Release(); - ComPtr surface; - if (FAILED(texture.As(&surface))) + IDXGISurface* surface; + if (FAILED(texture->QueryInterface(&surface))) { Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed."); return; } + const auto surfaceReleaser = MakeUniqueWithReleaser(surface); DXGI_MAPPED_RECT mappedSurface; if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ))) @@ -279,9 +282,10 @@ void Cursor::GetTexture(ID3D11Texture2D* texture) return; } - ComPtr context; + ID3D11DeviceContext* context; GetDevice()->GetImmediateContext(&context); context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), GetWidth() * 4, 0); + context->Release(); } diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index bf82481..93c51aa 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -18,7 +18,10 @@ Monitor::Monitor(int id) Monitor::~Monitor() { - deskDupl_->Release(); + if (deskDupl_) + { + deskDupl_->Release(); + } } @@ -110,7 +113,7 @@ void Monitor::Render(UINT timeout) { if (!deskDupl_) return; - ComPtr resource; + IDXGIResource* resource; DXGI_OUTDUPL_FRAME_INFO frameInfo; const auto hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource); @@ -154,8 +157,8 @@ void Monitor::Render(UINT timeout) if (unityTexture_) { - ComPtr texture; - if (FAILED(resource.As(&texture))) + ID3D11Texture2D* texture; + if (FAILED(resource->QueryInterface(&texture))) { Debug::Error("Monitor::Render() => resource.As() failed."); return; @@ -178,9 +181,10 @@ void Monitor::Render(UINT timeout) } else { - ComPtr context; + ID3D11DeviceContext* context; GetDevice()->GetImmediateContext(&context); - context->CopyResource(unityTexture_, texture.Get()); + context->CopyResource(unityTexture_, texture); + context->Release(); } } @@ -218,7 +222,7 @@ ID3D11Texture2D* Monitor::GetUnityTexture() const } -const ComPtr& Monitor::GetDeskDupl() +IDXGIOutputDuplication* Monitor::GetDeskDupl() { return deskDupl_; } diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h index 820f51a..b7c3af4 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h @@ -48,7 +48,7 @@ public: int GetRotation() const; int GetDpiX() const; int GetDpiY() const; - const Microsoft::WRL::ComPtr& GetDeskDupl(); + IDXGIOutputDuplication* GetDeskDupl(); const std::unique_ptr& GetCursor(); private: @@ -57,7 +57,7 @@ private: int width_ = -1, height_ = -1; State state_ = State::NotSet; std::unique_ptr cursor_; - Microsoft::WRL::ComPtr deskDupl_; + IDXGIOutputDuplication* deskDupl_ = nullptr; ID3D11Texture2D* unityTexture_ = nullptr; DXGI_OUTPUT_DESC outputDesc_; MONITORINFOEX monitorInfo_;