Repository: microsoft/terminal
File: src/renderer/base/renderer.cpp (C++)

Code before:
}

[[nodiscard]] HRESULT Renderer::_PaintFrameForEngine(_In_ IRenderEngine* const pEngine) noexcept
try
{
    FAIL_FAST_IF_NULL(pEngine); // This is a programming error. Fail fast.

    _pData->LockConsole();
    auto unlock = wil::scope_exit([&]() {
        _pData->UnlockConsole();
    });

    // Last chance check if anything scrolled without an explicit invalidate notification since the last frame.
    _CheckViewportAndScroll();

    // Try to start painting a frame
    const auto hr = pEngine->StartPaint();
    RETURN_IF_FAILED(hr);

    // Return early if there's nothing to paint.
    // The renderer itself tracks if there's something to do with the title, the
    //      engine won't know that.
    if (S_FALSE == hr)
    {
        return S_OK;
    }

    auto endPaint = wil::scope_exit([&]() {
        LOG_IF_FAILED(pEngine->EndPaint());

        // If the engine tells us it really wants to redraw immediately,
        // tell the thread so it doesn't go to sleep and ticks again
        // at the next opportunity.
        if (pEngine->RequiresContinuousRedraw())
        {
            NotifyPaintFrame();
        }
    });

    // A. Prep Colors
    RETURN_IF_FAILED(_UpdateDrawingBrushes(pEngine, {}, false, true));

    // B. Perform Scroll Operations
    RETURN_IF_FAILED(_PerformScrolling(pEngine));

    // C. Prepare the engine with additional information before we start drawing.
    RETURN_IF_FAILED(_PrepareRenderInfo(pEngine));

    // 1. Paint Background
    RETURN_IF_FAILED(_PaintBackground(pEngine));


Review comment (bug):
FYI I found a bug here: It's missing a `view.ConvertToOrigin(&cursorRect);`
I'm going to fix that in my TSF PR. The impact on Canary without this fix is not _that_ severe. There may be "cursor droppings" again in the meantime, but it should be rare, and there's workarounds.

Code after:
}

[[nodiscard]] HRESULT Renderer::_PaintFrame() noexcept
{
    {
        _pData->LockConsole();
        auto unlock = wil::scope_exit([&]() {
            _pData->UnlockConsole();
        });

        // Last chance check if anything scrolled without an explicit invalidate notification since the last frame.
        _CheckViewportAndScroll();

        if (_currentCursorOptions)
        {
            const auto coord = _currentCursorOptions->coordCursor;
            const auto& buffer = _pData->GetTextBuffer();
            const auto lineRendition = buffer.GetLineRendition(coord.y);
            const auto cursorWidth = _pData->IsCursorDoubleWidth() ? 2 : 1;

            til::rect cursorRect{ coord.x, coord.y, coord.x + cursorWidth, coord.y + 1 };
            cursorRect = BufferToScreenLine(cursorRect, lineRendition);

            if (buffer.GetSize().TrimToViewport(&cursorRect))
            {
                FOREACH_ENGINE(pEngine)
                {
                    LOG_IF_FAILED(pEngine->InvalidateCursor(&cursorRect));
                }
            }
        }

        _currentCursorOptions = _GetCursorInfo();

        FOREACH_ENGINE(pEngine)
        {
            RETURN_IF_FAILED(_PaintFrameForEngine(pEngine));
        }
    }

    FOREACH_ENGINE(pEngine)
    {
        RETURN_IF_FAILED(pEngine->Present());
    }

    return S_OK;
}

[[nodiscard]] HRESULT Renderer::_PaintFrameForEngine(_In_ IRenderEngine* const pEngine) noexcept
try
{