Skip to content

Commit

Permalink
AtlasEngine: Improve robustness against weird font sizes (#17258)
Browse files Browse the repository at this point in the history
This clamps the font sizes between 1 and 100. Additionally, it fixes
a warning that I randomly noticed when reproducing the issue: D2D
complained that `EndDraw` must be called before releasing resources.
Finally, this fixes a crash when the terminal size is exactly (1,1)
cells, which happened because the initial (invalid) size was (1,1) too.

This doesn't fully fix all font-size related issues, but that's
currently difficult to achieve, as for instance the swap chain size
isn't actually based on the window size, nay, it's based on the cell
size multiplied by the cell count. So if the cell size is egregiously
large then we get a swap chain size that's larger than the display and
potentially larger than what the GPU supports which results in errors.

Closes #17227

(cherry picked from commit f62d2d5)
Service-Card-Id: 92546859
Service-Version: 1.20
  • Loading branch information
lhecker authored and DHowett committed May 16, 2024
1 parent fc6f83e commit b02c11b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/renderer/atlas/AtlasEngine.api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ void AtlasEngine::_resolveFontMetrics(const wchar_t* requestedFaceName, const Fo
{
const auto requestedFamily = fontInfoDesired.GetFamily();
auto requestedWeight = fontInfoDesired.GetWeight();
auto fontSize = fontInfoDesired.GetFontSize();
auto fontSize = std::clamp(fontInfoDesired.GetFontSize(), 1.0f, 100.0f);
auto requestedSize = fontInfoDesired.GetEngineSize();

if (!requestedFaceName)
Expand Down
26 changes: 18 additions & 8 deletions src/renderer/atlas/BackendD2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,28 @@ void BackendD2D::Render(RenderingPayload& p)
}

_renderTarget->BeginDraw();
try
{
#if ATLAS_DEBUG_SHOW_DIRTY || ATLAS_DEBUG_DUMP_RENDER_TARGET
// Invalidating the render target helps with spotting Present1() bugs.
_renderTarget->Clear();
// Invalidating the render target helps with spotting Present1() bugs.
_renderTarget->Clear();
#endif
_drawBackground(p);
_drawCursorPart1(p);
_drawText(p);
_drawCursorPart2(p);
_drawSelection(p);
_drawBackground(p);
_drawCursorPart1(p);
_drawText(p);
_drawCursorPart2(p);
_drawSelection(p);
#if ATLAS_DEBUG_SHOW_DIRTY
_debugShowDirty(p);
_debugShowDirty(p);
#endif
}
catch (...)
{
// In case an exception is thrown for some reason between BeginDraw() and EndDraw()
// we still technically need to call EndDraw() before releasing any resources.
LOG_IF_FAILED(_renderTarget->EndDraw());
throw;
}
THROW_IF_FAILED(_renderTarget->EndDraw());

#if ATLAS_DEBUG_DUMP_RENDER_TARGET
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/atlas/BackendD3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ BackendD3D::BackendD3D(const RenderingPayload& p)
#endif
}

#pragma warning(suppress : 26432) // If you define or delete any default operation in the type '...', define or delete them all (c.21).
BackendD3D::~BackendD3D()
{
// In case an exception is thrown for some reason between BeginDraw() and EndDraw()
// we still technically need to call EndDraw() before releasing any resources.
if (_d2dBeganDrawing)
{
#pragma warning(suppress : 26447) // The function is declared 'noexcept' but calls function '...' which may throw exceptions (f.6).
LOG_IF_FAILED(_d2dRenderTarget->EndDraw());
}
}

void BackendD3D::ReleaseResources() noexcept
{
_renderTargetView.reset();
Expand Down
1 change: 1 addition & 0 deletions src/renderer/atlas/BackendD3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft::Console::Render::Atlas
struct BackendD3D : IBackend
{
BackendD3D(const RenderingPayload& p);
~BackendD3D() override;

void ReleaseResources() noexcept override;
void Render(RenderingPayload& payload) override;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/atlas/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ namespace Microsoft::Console::Render::Atlas
til::generational<CursorSettings> cursor;
til::generational<MiscellaneousSettings> misc;
// Size of the viewport / swap chain in pixel.
u16x2 targetSize{ 1, 1 };
u16x2 targetSize{ 0, 0 };
// Size of the portion of the text buffer that we're drawing on the screen.
u16x2 viewportCellCount{ 1, 1 };
u16x2 viewportCellCount{ 0, 0 };
// The position of the viewport inside the text buffer (in cells).
u16x2 viewportOffset{ 0, 0 };
};
Expand Down

0 comments on commit b02c11b

Please sign in to comment.