Skip to content

Commit

Permalink
fix many crash bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed May 8, 2021
1 parent 7118bcc commit 43e0460
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 171 deletions.
Binary file modified Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll
Binary file not shown.
Binary file modified Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll
Binary file not shown.
Binary file modified Assets/uWindowCapture/Prefabs/uWC Window Object.prefab
Binary file not shown.
4 changes: 2 additions & 2 deletions Plugins/uWindowCapture/uWindowCapture/CaptureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CaptureManager
void RequestCaptureIcon(int id);

private:
ThreadLoop windowCaptureThreadLoop_ = { L"Window Capture Thread" };
ThreadLoop iconCaptureThreadLoop_ = { L"Icon Capture Thread" };
ThreadLoop windowCaptureThreadLoop_ = { L"uWindowCapture - Window Capture Thread" };
ThreadLoop iconCaptureThreadLoop_ = { L"uWindowCapture - Icon Capture Thread" };
WindowQueue highPriorityQueue_;
WindowQueue middlePriorityQueue_;
WindowQueue lowPriorityQueue_;
Expand Down
2 changes: 1 addition & 1 deletion Plugins/uWindowCapture/uWindowCapture/UploadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UploadManager
bool isReady_ = false;
DevicePtr device_;
std::thread initThread_;
ThreadLoop threadLoop_ = { L"Upload Thread" };
ThreadLoop threadLoop_ = { L"uWindowCapture - Upload Thread" };
WindowQueue windowUploadQueue_;
WindowQueue iconUploadQueue_;
};
2 changes: 1 addition & 1 deletion Plugins/uWindowCapture/uWindowCapture/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void Window::UpdateTitle()
{
if (windowTexture_->IsWindowsGraphicsCaptureAvailable())
{
if (const auto& wgc = windowTexture_->GetWindowsGraphicsCapture())
if (const auto wgc = windowTexture_->GetWindowsGraphicsCapture())
{
data2_.title = wgc->GetDisplayName();
}
Expand Down
2 changes: 1 addition & 1 deletion Plugins/uWindowCapture/uWindowCapture/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WindowManager
std::weak_ptr<Window> cursorWindow_;
mutable std::mutex windowsListMutex_;

ThreadLoop windowHandleListThreadLoop_ = { L"Window Handle List Thread" };
ThreadLoop windowHandleListThreadLoop_ = { L"uWindowCapture - Window Handle List Thread" };

std::vector<Window::Data1> windowDataList_[2];
mutable std::mutex windowsDataListMutex_;
Expand Down
72 changes: 48 additions & 24 deletions Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ using namespace Microsoft::WRL;
WindowTexture::WindowTexture(Window* window)
: window_(window)
{
if (window_->IsDesktop())
if (const auto& wgcManager = WindowManager::GetWindowsGraphicsCaptureManager())
{
windowsGraphicsCapture_ = std::make_shared<WindowsGraphicsCapture>(window_->GetMonitorHandle());
}
else// if (window_->IsAltTab())
{
windowsGraphicsCapture_ = std::make_shared<WindowsGraphicsCapture>(window_->GetWindowHandle());
if (window_->IsDesktop())
{
windowsGraphicsCapture_ = wgcManager->Create(window_->GetMonitorHandle());
}
else
{
windowsGraphicsCapture_ = wgcManager->Create(window_->GetWindowHandle());
}
}
}

Expand All @@ -31,6 +34,14 @@ WindowTexture::~WindowTexture()
{
std::lock_guard<std::mutex> lock(bufferMutex_);
DeleteBitmap();

if (auto wgc = windowsGraphicsCapture_.lock())
{
if (const auto& wgcManager = WindowManager::GetWindowsGraphicsCaptureManager())
{
wgcManager->Destroy(wgc);
}
}
}


Expand Down Expand Up @@ -162,9 +173,9 @@ bool WindowTexture::IsWindowsGraphicsCapture() const
}


const std::shared_ptr<WindowsGraphicsCapture> & WindowTexture::GetWindowsGraphicsCapture() const
std::shared_ptr<WindowsGraphicsCapture> WindowTexture::GetWindowsGraphicsCapture() const
{
return windowsGraphicsCapture_;
return windowsGraphicsCapture_.lock();
}


Expand Down Expand Up @@ -417,22 +428,19 @@ void WindowTexture::DrawCursorByWin32API(HWND hWnd, HDC hDcMem)

bool WindowTexture::CaptureByWindowsGraphicsCapture()
{
if (!windowsGraphicsCapture_) return false;
auto wgc = windowsGraphicsCapture_.lock();

if (!windowsGraphicsCapture_->IsStarted())
{
windowsGraphicsCapture_->Start();
}
if (!wgc) return false;

if (windowsGraphicsCapture_->IsSessionRestartRequested())
if (!wgc->IsStarted())
{
windowsGraphicsCapture_->Restart();
wgc->RequestStart();
}

windowsGraphicsCapture_->EnableCursorCapture(GetCursorDraw());
wgc->EnableCursorCapture(GetCursorDraw());

textureWidth_ = windowsGraphicsCapture_->GetWidth();
textureHeight_ = windowsGraphicsCapture_->GetHeight();
textureWidth_ = wgc->GetWidth();
textureHeight_ = wgc->GetHeight();
offsetX_ = 0;
offsetY_ = 0;

Expand Down Expand Up @@ -521,6 +529,7 @@ bool WindowTexture::RecreateSharedTextureIfNeeded()
if (!dxgiResource || FAILED(dxgiResource->GetSharedHandle(&sharedHandle_)))
{
Debug::Error(__FUNCTION__, " => GetSharedHandle() failed.");
sharedTexture_.Reset();
return false;
}
}
Expand Down Expand Up @@ -558,28 +567,35 @@ bool WindowTexture::UploadByWindowsGraphicsCapture()
{
UWC_SCOPE_TIMER(UploadByWindowsGraphicsCapture)

if (!windowsGraphicsCapture_) return false;
auto wgc = windowsGraphicsCapture_.lock();
if (!wgc) return false;

const auto result = windowsGraphicsCapture_->TryGetLatestResult();
const auto result = wgc->TryGetLatestResult();
if (!result.pTexture) return false;
ScopedReleaser resultReleaser([&] { windowsGraphicsCapture_->ReleaseLatestResult(); });
ScopedReleaser resultReleaser([&] { wgc->ReleaseLatestResult(); });

const auto& uploader = WindowManager::GetUploadManager();
if (!uploader) return false;

try
{
std::lock_guard<std::mutex> lock(sharedTextureMutex_);
ComPtr<ID3D11DeviceContext> context;
uploader->GetDevice()->GetImmediateContext(&context);
context->CopyResource(sharedTexture_.Get(), result.pTexture);
context->Flush();
}
catch (...)
{
Debug::Error(__FUNCTION__, " => CopyResource() threw an exception.");
return false;
}

if (result.hasSizeChanged)
{
const auto w = result.width;
const auto h = result.height;
windowsGraphicsCapture_->ChangePoolSize(w, h);
wgc->ChangePoolSize(w, h);
}

return true;
Expand All @@ -604,7 +620,14 @@ bool WindowTexture::Render()
return false;
}

context->CopyResource(unityTexture_.load(), texture.Get());
try
{
context->CopyResource(unityTexture_.load(), texture.Get());
}
catch (...)
{
Debug::Error(__FUNCTION__, " => CopyResource() threw an exception.");
}

MessageManager::Get().Add({ MessageType::WindowCaptured, window_->GetId(), window_->GetWindowHandle() });

Expand Down Expand Up @@ -678,5 +701,6 @@ bool WindowTexture::GetPixels(BYTE* output, int x, int y, int width, int height)

bool WindowTexture::IsWindowsGraphicsCaptureAvailable() const
{
return windowsGraphicsCapture_ && windowsGraphicsCapture_->IsAvailable();
auto wgc = windowsGraphicsCapture_.lock();
return wgc && wgc->IsAvailable();
}
4 changes: 2 additions & 2 deletions Plugins/uWindowCapture/uWindowCapture/WindowTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WindowTexture
bool GetPixels(BYTE* output, int x, int y, int width, int height) const;

bool IsWindowsGraphicsCaptureAvailable() const;
const std::shared_ptr<WindowsGraphicsCapture> & GetWindowsGraphicsCapture() const;
std::shared_ptr<WindowsGraphicsCapture> GetWindowsGraphicsCapture() const;

private:
CaptureMode GetCaptureModeInternal() const;
Expand All @@ -69,7 +69,7 @@ class WindowTexture

const Window* const window_;
CaptureMode captureMode_ = CaptureMode::Auto;
std::shared_ptr<WindowsGraphicsCapture> windowsGraphicsCapture_;
std::weak_ptr<WindowsGraphicsCapture> windowsGraphicsCapture_;
bool isPrintWindowFailed_ = false;

std::atomic<ID3D11Texture2D*> unityTexture_ = nullptr;
Expand Down
Loading

0 comments on commit 43e0460

Please sign in to comment.