@@ -16,13 +16,16 @@ using namespace Microsoft::WRL;
1616WindowTexture::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
418429bool 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
679702bool WindowTexture::IsWindowsGraphicsCaptureAvailable () const
680703{
681- return windowsGraphicsCapture_ && windowsGraphicsCapture_->IsAvailable ();
704+ auto wgc = windowsGraphicsCapture_.lock ();
705+ return wgc && wgc->IsAvailable ();
682706}
0 commit comments