Skip to content

Commit 43e0460

Browse files
committed
fix many crash bugs.
1 parent 7118bcc commit 43e0460

File tree

11 files changed

+203
-171
lines changed

11 files changed

+203
-171
lines changed
3 KB
Binary file not shown.
2.5 KB
Binary file not shown.
0 Bytes
Binary file not shown.

Plugins/uWindowCapture/uWindowCapture/CaptureManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class CaptureManager
2525
void RequestCaptureIcon(int id);
2626

2727
private:
28-
ThreadLoop windowCaptureThreadLoop_ = { L"Window Capture Thread" };
29-
ThreadLoop iconCaptureThreadLoop_ = { L"Icon Capture Thread" };
28+
ThreadLoop windowCaptureThreadLoop_ = { L"uWindowCapture - Window Capture Thread" };
29+
ThreadLoop iconCaptureThreadLoop_ = { L"uWindowCapture - Icon Capture Thread" };
3030
WindowQueue highPriorityQueue_;
3131
WindowQueue middlePriorityQueue_;
3232
WindowQueue lowPriorityQueue_;

Plugins/uWindowCapture/uWindowCapture/UploadManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UploadManager
3434
bool isReady_ = false;
3535
DevicePtr device_;
3636
std::thread initThread_;
37-
ThreadLoop threadLoop_ = { L"Upload Thread" };
37+
ThreadLoop threadLoop_ = { L"uWindowCapture - Upload Thread" };
3838
WindowQueue windowUploadQueue_;
3939
WindowQueue iconUploadQueue_;
4040
};

Plugins/uWindowCapture/uWindowCapture/Window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ void Window::UpdateTitle()
363363
{
364364
if (windowTexture_->IsWindowsGraphicsCaptureAvailable())
365365
{
366-
if (const auto& wgc = windowTexture_->GetWindowsGraphicsCapture())
366+
if (const auto wgc = windowTexture_->GetWindowsGraphicsCapture())
367367
{
368368
data2_.title = wgc->GetDisplayName();
369369
}

Plugins/uWindowCapture/uWindowCapture/WindowManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class WindowManager
5555
std::weak_ptr<Window> cursorWindow_;
5656
mutable std::mutex windowsListMutex_;
5757

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

6060
std::vector<Window::Data1> windowDataList_[2];
6161
mutable std::mutex windowsDataListMutex_;

Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ using namespace Microsoft::WRL;
1616
WindowTexture::WindowTexture(Window* window)
1717
: window_(window)
1818
{
19-
if (window_->IsDesktop())
19+
if (const auto& wgcManager = WindowManager::GetWindowsGraphicsCaptureManager())
2020
{
21-
windowsGraphicsCapture_ = std::make_shared<WindowsGraphicsCapture>(window_->GetMonitorHandle());
22-
}
23-
else// if (window_->IsAltTab())
24-
{
25-
windowsGraphicsCapture_ = std::make_shared<WindowsGraphicsCapture>(window_->GetWindowHandle());
21+
if (window_->IsDesktop())
22+
{
23+
windowsGraphicsCapture_ = wgcManager->Create(window_->GetMonitorHandle());
24+
}
25+
else
26+
{
27+
windowsGraphicsCapture_ = wgcManager->Create(window_->GetWindowHandle());
28+
}
2629
}
2730
}
2831

@@ -31,6 +34,14 @@ WindowTexture::~WindowTexture()
3134
{
3235
std::lock_guard<std::mutex> lock(bufferMutex_);
3336
DeleteBitmap();
37+
38+
if (auto wgc = windowsGraphicsCapture_.lock())
39+
{
40+
if (const auto& wgcManager = WindowManager::GetWindowsGraphicsCaptureManager())
41+
{
42+
wgcManager->Destroy(wgc);
43+
}
44+
}
3445
}
3546

3647

@@ -162,9 +173,9 @@ bool WindowTexture::IsWindowsGraphicsCapture() const
162173
}
163174

164175

165-
const std::shared_ptr<WindowsGraphicsCapture> & WindowTexture::GetWindowsGraphicsCapture() const
176+
std::shared_ptr<WindowsGraphicsCapture> WindowTexture::GetWindowsGraphicsCapture() const
166177
{
167-
return windowsGraphicsCapture_;
178+
return windowsGraphicsCapture_.lock();
168179
}
169180

170181

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

418429
bool WindowTexture::CaptureByWindowsGraphicsCapture()
419430
{
420-
if (!windowsGraphicsCapture_) return false;
431+
auto wgc = windowsGraphicsCapture_.lock();
421432

422-
if (!windowsGraphicsCapture_->IsStarted())
423-
{
424-
windowsGraphicsCapture_->Start();
425-
}
433+
if (!wgc) return false;
426434

427-
if (windowsGraphicsCapture_->IsSessionRestartRequested())
435+
if (!wgc->IsStarted())
428436
{
429-
windowsGraphicsCapture_->Restart();
437+
wgc->RequestStart();
430438
}
431439

432-
windowsGraphicsCapture_->EnableCursorCapture(GetCursorDraw());
440+
wgc->EnableCursorCapture(GetCursorDraw());
433441

434-
textureWidth_ = windowsGraphicsCapture_->GetWidth();
435-
textureHeight_ = windowsGraphicsCapture_->GetHeight();
442+
textureWidth_ = wgc->GetWidth();
443+
textureHeight_ = wgc->GetHeight();
436444
offsetX_ = 0;
437445
offsetY_ = 0;
438446

@@ -521,6 +529,7 @@ bool WindowTexture::RecreateSharedTextureIfNeeded()
521529
if (!dxgiResource || FAILED(dxgiResource->GetSharedHandle(&sharedHandle_)))
522530
{
523531
Debug::Error(__FUNCTION__, " => GetSharedHandle() failed.");
532+
sharedTexture_.Reset();
524533
return false;
525534
}
526535
}
@@ -558,28 +567,35 @@ bool WindowTexture::UploadByWindowsGraphicsCapture()
558567
{
559568
UWC_SCOPE_TIMER(UploadByWindowsGraphicsCapture)
560569

561-
if (!windowsGraphicsCapture_) return false;
570+
auto wgc = windowsGraphicsCapture_.lock();
571+
if (!wgc) return false;
562572

563-
const auto result = windowsGraphicsCapture_->TryGetLatestResult();
573+
const auto result = wgc->TryGetLatestResult();
564574
if (!result.pTexture) return false;
565-
ScopedReleaser resultReleaser([&] { windowsGraphicsCapture_->ReleaseLatestResult(); });
575+
ScopedReleaser resultReleaser([&] { wgc->ReleaseLatestResult(); });
566576

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

580+
try
570581
{
571582
std::lock_guard<std::mutex> lock(sharedTextureMutex_);
572583
ComPtr<ID3D11DeviceContext> context;
573584
uploader->GetDevice()->GetImmediateContext(&context);
574585
context->CopyResource(sharedTexture_.Get(), result.pTexture);
575586
context->Flush();
576587
}
588+
catch (...)
589+
{
590+
Debug::Error(__FUNCTION__, " => CopyResource() threw an exception.");
591+
return false;
592+
}
577593

578594
if (result.hasSizeChanged)
579595
{
580596
const auto w = result.width;
581597
const auto h = result.height;
582-
windowsGraphicsCapture_->ChangePoolSize(w, h);
598+
wgc->ChangePoolSize(w, h);
583599
}
584600

585601
return true;
@@ -604,7 +620,14 @@ bool WindowTexture::Render()
604620
return false;
605621
}
606622

607-
context->CopyResource(unityTexture_.load(), texture.Get());
623+
try
624+
{
625+
context->CopyResource(unityTexture_.load(), texture.Get());
626+
}
627+
catch (...)
628+
{
629+
Debug::Error(__FUNCTION__, " => CopyResource() threw an exception.");
630+
}
608631

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

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

679702
bool WindowTexture::IsWindowsGraphicsCaptureAvailable() const
680703
{
681-
return windowsGraphicsCapture_ && windowsGraphicsCapture_->IsAvailable();
704+
auto wgc = windowsGraphicsCapture_.lock();
705+
return wgc && wgc->IsAvailable();
682706
}

Plugins/uWindowCapture/uWindowCapture/WindowTexture.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class WindowTexture
5353
bool GetPixels(BYTE* output, int x, int y, int width, int height) const;
5454

5555
bool IsWindowsGraphicsCaptureAvailable() const;
56-
const std::shared_ptr<WindowsGraphicsCapture> & GetWindowsGraphicsCapture() const;
56+
std::shared_ptr<WindowsGraphicsCapture> GetWindowsGraphicsCapture() const;
5757

5858
private:
5959
CaptureMode GetCaptureModeInternal() const;
@@ -69,7 +69,7 @@ class WindowTexture
6969

7070
const Window* const window_;
7171
CaptureMode captureMode_ = CaptureMode::Auto;
72-
std::shared_ptr<WindowsGraphicsCapture> windowsGraphicsCapture_;
72+
std::weak_ptr<WindowsGraphicsCapture> windowsGraphicsCapture_;
7373
bool isPrintWindowFailed_ = false;
7474

7575
std::atomic<ID3D11Texture2D*> unityTexture_ = nullptr;

0 commit comments

Comments
 (0)