Skip to content

Commit

Permalink
own the handle
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Apr 13, 2022
1 parent 7b3ca83 commit 33b1ab9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

if (auto control{ weakThis.get() })
{
// TODO! very good chance we leak this handle
const HANDLE chainHandle = reinterpret_cast<HANDLE>(control->_contentIsOutOfProc() ?
control->_contentProc.RequestSwapChainHandle(GetCurrentProcessId()) :
control->_core.SwapChainHandle());
_AttachDxgiSwapChainToXaml(chainHandle);
control->_aquireAndAttachSwapChainHandle();
}
}

Expand Down Expand Up @@ -727,6 +723,28 @@ namespace winrt::Microsoft::Terminal::Control::implementation
nativePanel->SetSwapChainHandle(swapChainHandle);
}

void TermControl::_aquireAndAttachSwapChainHandle()
{
const HANDLE chainHandle = reinterpret_cast<HANDLE>(_contentIsOutOfProc() ?
_contentProc.RequestSwapChainHandle(GetCurrentProcessId()) :
_core.SwapChainHandle());
// If we're in-proc, then the render engine has it's own ownership
// handle to the swapchain. We don't need to also own it in the XAML
// layer. It doesn't really make sense for us to duplicate it to
// ourself again, so we're just gonna not.
//
// If we're out of proc though, the content proc has one handle in
// it's process, and now there's a HANDLE in our process with this
// value. Wrap that boy up in a unique_handle so that we can release
// our handle when needed.
if (_contentIsOutOfProc())
{
_contentSwapChain.reset(chainHandle);
}

_AttachDxgiSwapChainToXaml(chainHandle);
}

bool TermControl::_InitializeTerminal()
{
if (_initializedTerminal)
Expand Down Expand Up @@ -764,11 +782,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_interactivity.Initialize();
}

// TODO! very good chance we leak this handle
const HANDLE chainHandle = reinterpret_cast<HANDLE>(_contentIsOutOfProc() ?
_contentProc.RequestSwapChainHandle(GetCurrentProcessId()) :
_core.SwapChainHandle());
_AttachDxgiSwapChainToXaml(chainHandle);
_aquireAndAttachSwapChainHandle();

// Tell the DX Engine to notify us when the swap chain changes. We do
// this after we initially set the swapchain so as to avoid unnecessary
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation

wil::unique_event _contentWaitInterrupt;
std::thread _contentWaitThread;
wil::unique_handle _contentSwapChain;
void _createContentWaitThread();
bool _contentIsOutOfProc() const;

void _aquireAndAttachSwapChainHandle();

inline bool _IsClosing() const noexcept
{
#ifndef NDEBUG
Expand Down

1 comment on commit 33b1ab9

@github-actions

This comment was marked as outdated.

Please sign in to comment.