Skip to content

Commit

Permalink
AtlasEngine: Fix Present() of out of bounds glyphs (#15403)
Browse files Browse the repository at this point in the history
`til::rect`'s truthiness check (= rect is valid) returns `false` for
any rects that have negative coordinates. This makes sense for buffer
handling, but breaks AtlasEngine, where glyph coordinates can go out
of bounds and it's entirely valid for that to happen.

Closes #15416

## Validation Steps Performed
* Use MesloLGM NF and print NF glyphs in the first row
* Text rendering, selection, etc., still works ✅

---------

Co-authored-by: Dustin L. Howett <duhowett@microsoft.com>
(cherry picked from commit a9f34e3)
Service-Card-Id: 89326794
Service-Version: 1.18
  • Loading branch information
lhecker and DHowett committed May 25, 2023
1 parent 9200979 commit 81bf8d5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
29 changes: 15 additions & 14 deletions src/renderer/atlas/AtlasEngine.r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,31 +415,32 @@ void AtlasEngine::_waitUntilCanRender() noexcept

void AtlasEngine::_present()
{
// Present1() dislikes being called with an empty dirty rect.
if (!_p.dirtyRectInPx)
{
return;
}

const til::rect fullRect{ 0, 0, _p.swapChain.targetSize.x, _p.swapChain.targetSize.y };
const RECT fullRect{ 0, 0, _p.swapChain.targetSize.x, _p.swapChain.targetSize.y };

DXGI_PRESENT_PARAMETERS params{};
RECT scrollRect{};
POINT scrollOffset{};

// Since rows might be taller than their cells, they might have drawn outside of the viewport.
auto dirtyRect = _p.dirtyRectInPx;
dirtyRect.left = std::max(dirtyRect.left, 0);
dirtyRect.top = std::max(dirtyRect.top, 0);
dirtyRect.right = std::min(dirtyRect.right, fullRect.right);
dirtyRect.bottom = std::min(dirtyRect.bottom, fullRect.bottom);
RECT dirtyRect{
.left = std::max(_p.dirtyRectInPx.left, 0),
.top = std::max(_p.dirtyRectInPx.top, 0),
.right = std::min<LONG>(_p.dirtyRectInPx.right, fullRect.right),
.bottom = std::min<LONG>(_p.dirtyRectInPx.bottom, fullRect.bottom),
};

// Present1() dislikes being called with an empty dirty rect.
if (dirtyRect.left >= dirtyRect.right || dirtyRect.top >= dirtyRect.bottom)
{
return;
}

if constexpr (!ATLAS_DEBUG_SHOW_DIRTY)
{
if (dirtyRect != fullRect)
if (memcmp(&dirtyRect, &fullRect, sizeof(RECT)) != 0)
{
params.DirtyRectsCount = 1;
params.pDirtyRects = dirtyRect.as_win32_rect();
params.pDirtyRects = &dirtyRect;

if (_p.scrollOffset)
{
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/atlas/BackendD2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ void BackendD2D::_debugShowDirty(const RenderingPayload& p)

for (size_t i = 0; i < std::size(_presentRects); ++i)
{
if (const auto& rect = _presentRects[i])
const auto& rect = _presentRects[(_presentRectsPos + i) % std::size(_presentRects)];
if (rect.non_empty())
{
const D2D1_RECT_F rectF{
static_cast<f32>(rect.left),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/atlas/BackendD2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Microsoft::Console::Render::Atlas
u16x2 _cellCount{};

#if ATLAS_DEBUG_SHOW_DIRTY
til::rect _presentRects[9]{};
i32r _presentRects[9]{};
size_t _presentRectsPos = 0;
#endif

Expand Down
5 changes: 3 additions & 2 deletions src/renderer/atlas/BackendD3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,10 +2047,11 @@ void BackendD3D::_debugShowDirty(const RenderingPayload& p)

for (size_t i = 0; i < std::size(_presentRects); ++i)
{
if (const auto& rect = _presentRects[i])
const auto& rect = _presentRects[(_presentRectsPos + i) % std::size(_presentRects)];
if (rect.non_empty())
{
_appendQuad() = {
.shadingType = ShadingType::SolidFill,
.shadingType = ShadingType::Selection,
.position = {
static_cast<i16>(rect.left),
static_cast<i16>(rect.top),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/atlas/BackendD3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ namespace Microsoft::Console::Render::Atlas
bool _requiresContinuousRedraw = false;

#if ATLAS_DEBUG_SHOW_DIRTY
til::rect _presentRects[9]{};
i32r _presentRects[9]{};
size_t _presentRectsPos = 0;
#endif

Expand Down
8 changes: 6 additions & 2 deletions src/renderer/atlas/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,12 @@ namespace Microsoft::Console::Render::Atlas
std::array<til::generation_t, 2> colorBitmapGenerations{ 1, 1 };
// In columns/rows.
til::rect cursorRect;
// In pixel.
til::rect dirtyRectInPx;
// The viewport/SwapChain area to be presented. In pixel.
// NOTE:
// This cannot use til::rect, because til::rect generally expects positive coordinates only
// (`operator!()` checks for negative values), whereas this one can go out of bounds,
// whenever glyphs go out of bounds. `AtlasEngine::_present()` will clamp it.
i32r dirtyRectInPx{};
// In rows.
range<u16> invalidatedRows{};
// In pixel.
Expand Down

1 comment on commit 81bf8d5

@github-actions
Copy link

Choose a reason for hiding this comment

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

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (1)

HAX

Previously acknowledged words that are now absent Hirots NULs spand xwwyzz xxyyzz :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:microsoft/terminal.git repository
on the release-1.18 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/microsoft/terminal/actions/runs/5084013465/attempts/1'
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. If it doesn't work for you, you can manually add (one word per line) / remove items to expect.txt and the excludes.txt files.

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.