Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3787,10 +3787,8 @@ namespace winrt::TerminalApp::implementation
TermControl TerminalPage::_SetupControl(const TermControl& term)
{
// GH#12515: ConPTY assumes it's hidden at the start. If we're not, let it know now.
if (_visible)
{
term.WindowVisibilityChanged(_visible);
}
// If we are hidden, the renderer needs to know, so that it doesn't render while minimized.
term.WindowVisibilityChanged(_visible);

// Even in the case of re-attaching content from another window, this
// will correctly update the control's owning HWND
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// - <none>
void ControlCore::WindowVisibilityChanged(const bool showOrHide)
{
// There's no point in rendering while minimized. The renderer exists from the
// constructor onward, so this doesn't need to wait for initialization.
_renderer->AllowPainting(Render::InhibitionSource::Host, showOrHide);

if (_initializedTerminal.load(std::memory_order_relaxed))
{
// show is true, hide is false
Expand Down
11 changes: 3 additions & 8 deletions src/host/_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ using Microsoft::Console::Interactivity::ServiceLocator;
// - <none>
void WriteToScreen(SCREEN_INFORMATION& screenInfo, const Viewport& region)
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// update to screen, if we're not iconic.
if (!screenInfo.IsActiveScreenBuffer() || WI_IsFlagSet(gci.Flags, CONSOLE_IS_ICONIC))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should keep this one btw. it does a whole fast path exit if minimized, which means we spin less of the render infrastructure (it doesn't have to do any checks or wake anything. i know it's minimal, but the WPG gates are ... harsh sometimes... and we have an existing early-out here)

if (!screenInfo.IsActiveScreenBuffer())
{
return;
}
Expand All @@ -41,12 +39,9 @@ void WriteToScreen(SCREEN_INFORMATION& screenInfo, const Viewport& region)
return;
}

if (screenInfo.IsActiveScreenBuffer())
if (const auto pRender = ServiceLocator::LocateGlobals().pRender)
{
if (ServiceLocator::LocateGlobals().pRender != nullptr)
{
ServiceLocator::LocateGlobals().pRender->TriggerRedraw(region);
}
pRender->TriggerRedraw(region);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/interactivity/win32/windowproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ static constexpr TsfDataProvider s_tsfDataProvider;

case WM_WINDOWPOSCHANGED:
{
// We don't forward this message to DefWindowProc, so we don't get WM_SIZE.
// This is the closest equivalent for detecting being minimized.
if (const auto renderer = g.pRender)
{
renderer->AllowPainting(Render::InhibitionSource::Host, !IsIconic(hWnd));
}

// Only handle this if the DPI is the same as last time.
// If the DPI is different, assume we're about to get a DPICHANGED notification
// which will have a better suggested rectangle than this one.
Expand Down
35 changes: 28 additions & 7 deletions src/renderer/base/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void Renderer::EnablePainting()
// This ensures that any missed invalidations are redrawn.
TriggerRedrawAll();

_enable.SetEvent();
// This also undoes a previous failure, since this function doubles as the "retry" mechanism.
AllowPainting(InhibitionSource::System, true);

if (const auto guard = _threadMutex.lock_exclusive(); !_thread)
{
Expand All @@ -95,9 +96,25 @@ void Renderer::EnablePainting()
}
}

void Renderer::_disablePainting() noexcept
void Renderer::AllowPainting(InhibitionSource source, bool enable) noexcept
{
_enable.ResetEvent();
const auto guard = _paintLock.lock_exclusive();

const auto before = _paintInhibitors.any();
_paintInhibitors.set(source, !enable);
const auto after = _paintInhibitors.any();

if (before != after)
{
if (after)
{
_paintEnable.ResetEvent();
}
else
{
_paintEnable.SetEvent();
}
}
}

// Method Description:
Expand All @@ -116,13 +133,17 @@ void Renderer::TriggerTeardown() noexcept
_threadKeepRunning.store(false, std::memory_order_relaxed);
_shutdownEvent.SetEvent();
NotifyPaintFrame();
_enable.SetEvent();
_paintEnable.SetEvent();

WaitForSingleObject(_thread.get(), INFINITE);
_thread.reset();
}

_disablePainting();
// Undo the _paintEnable.SetEvent() above, so that it's in sync with _paintInhibitors again.
// EnablePainting() will then correctly undo this.
const auto guard = _paintLock.lock_exclusive();
_paintInhibitors.set(InhibitionSource::System);
_paintEnable.ResetEvent();
}

void Renderer::NotifyPaintFrame() noexcept
Expand All @@ -140,7 +161,7 @@ DWORD Renderer::_renderThread() noexcept
{
while (_threadKeepRunning.load(std::memory_order_relaxed))
{
_enable.wait();
_paintEnable.wait();
if (!_waitUntilCanRender())
{
break;
Expand Down Expand Up @@ -385,7 +406,7 @@ DWORD Renderer::_timerToMillis(TimerRepr t) noexcept
if (FAILED(hr))
{
// Stop trying.
_disablePainting();
AllowPainting(InhibitionSource::System, false);
if (_pfnRendererEnteredErrorState)
{
_pfnRendererEnteredErrorState();
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/base/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft::Console::Render
{
enum class InhibitionSource
{
System, // The renderer itself (e.g. not enabled yet, torn down, render failure)
Client, // E.g. VT sequences
Host, // E.g. because the window is out of focus
User, // The user turned it off
Expand All @@ -33,6 +34,7 @@ namespace Microsoft::Console::Render

void NotifyPaintFrame() noexcept;
void SynchronizedOutputChanged() noexcept;
void AllowPainting(InhibitionSource source, bool enable) noexcept;
void AllowCursorVisibility(InhibitionSource source, bool enable) noexcept;
void AllowCursorBlinking(InhibitionSource source, bool enable) noexcept;
void TriggerSystemRedraw(const til::rect* const prcDirtyClient);
Expand Down Expand Up @@ -115,7 +117,6 @@ namespace Microsoft::Console::Render
[[nodiscard]] HRESULT PaintFrame();
[[nodiscard]] HRESULT _PaintFrame(unsigned int attempt) noexcept;
[[nodiscard]] HRESULT _PaintFrameForEngine(_In_ IRenderEngine* const pEngine) noexcept;
void _disablePainting() noexcept;
void _synchronizeWithOutput() noexcept;
bool _CheckViewportAndScroll();
void _scheduleRenditionBlink();
Expand Down Expand Up @@ -146,7 +147,9 @@ namespace Microsoft::Console::Render
wil::srwlock _threadMutex;
wil::unique_handle _thread;
wil::unique_event _shutdownEvent;
wil::slim_event_manual_reset _enable;
wil::srwlock _paintLock;
wil::slim_event_manual_reset _paintEnable;
til::enumset<InhibitionSource, uint8_t> _paintInhibitors{ InhibitionSource::System };
std::atomic<bool> _redraw;
std::atomic<bool> _threadKeepRunning{ false };
til::small_vector<IRenderEngine*, 2> _engines;
Expand Down
Loading