From 81e4d59da2f861f64ae63a6d7d9c4025c64e935d Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 10 Sep 2025 16:47:26 +0100 Subject: [PATCH] Direct2D: Avoid unnecessarily transforming solid colour brush The changes were not cleared between frames, so they could end up accumulating and causing floating-point exceptions. --- .../juce_Direct2DGraphicsContext_windows.cpp | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp b/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp index 628ee1bc0c..363c99f01c 100644 --- a/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp +++ b/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp @@ -1130,6 +1130,13 @@ void Direct2DGraphicsContext::drawGlyphs (Span glyphNumbers, if (brush == nullptr) return; + const auto getBrushTransform = [] (auto brushIn) -> AffineTransform + { + D2D1::Matrix3x2F matrix{}; + brushIn->GetTransform (&matrix); + return D2DUtilities::matrixToTransform (matrix); + }; + applyPendingClipList(); D2D1_POINT_2F baselineOrigin { 0.0f, 0.0f }; @@ -1140,13 +1147,18 @@ void Direct2DGraphicsContext::drawGlyphs (Span glyphNumbers, } else { - D2D1::Matrix3x2F matrix{}; - brush->GetTransform (&matrix); - const auto brushTransform = D2DUtilities::matrixToTransform (matrix); - brush->SetTransform (D2DUtilities::transformToMatrix (brushTransform.followedBy (textTransform.inverted()))); + if (brush != currentState->colourBrush) + { + const auto brushTransform = getBrushTransform (brush); + brush->SetTransform (D2DUtilities::transformToMatrix (brushTransform.followedBy (textTransform.inverted()))); + } + getPimpl()->setDeviceContextTransform (textAndWorldTransform); } + // There's no need to transform a plain colour brush + jassert (brush != currentState->colourBrush || getBrushTransform (brush).isIdentity()); + auto& run = getPimpl()->glyphRun; run.replace (positions, fontScale);