Skip to content

Skia/Transforms: - Debug assertion on SKMatrix uses exact equality β€” flakes when rootTransform is definedΒ #21356

@carldebilly

Description

@carldebilly

Current behavior πŸ›

Visual.skia.cs file: When running in Debug builds we have a Debug.Assert that compares canvas.TotalMatrix to an expected SKMatrix. The code uses exact equality (==) on SKMatrix instances and causes intermittent assertion failures when rootTransform is present. The values differ by tiny floating-point rounding errors (e.g. due to matrix multiplications), so the assertion fails even though the matrices are effectively equal for rendering purposes.

Code location: https://github.com/unoplatform/uno/blob/master/src/Uno.UI.Composition/Composition/Visual.skia.cs#L612

Expected behavior 🎯

The Debug.Assert should compare matrices with a small epsilon tolerance (an element-wise "almost equal") rather than strict equality, to avoid flaky assertion failures caused by floating-point rounding.

How to reproduce it (as minimally and precisely as possible) πŸ”¬

The assertion triggers when rootTransform is defined and TotalMatrix has been multiplied with it (TotalMatrix * rootTransform); small rounding differences across platforms or calculation order make the exact-equality check fail.

Workaround πŸ› οΈ

The workaround right now is to comment the Debug.Assert() line to prevent application from crashing.

Proposed fix

Use an element-wise approximate comparison (with a configurable epsilon) instead of ==. Do this in the Debug.Assert (or compute the matrices beforehand and assert using a helper method). Also avoid expensive matrix conversions inside asserts by computing expected/actual once and reusing them.

The epsilon value can be tuned; around 1e-4 is typical for SKMatrix single-precision comparisons. You should use the CompositionMathHelpers.IsCloseReal() to check the values.

Renderer 🎨

  • Skia
  • Native

Affected platforms πŸ“±πŸ’»πŸ–₯️

Desktop (Windows)

Uno.Sdk version (and other relevant versions) πŸ“¦

No response

IDE version πŸ§‘β€πŸ’»

No response

Anything else we need to know? πŸ’¬

No response

Metadata

Metadata

Assignees

Labels

difficulty/tbdCategorizes an issue for which the difficulty level needs to be defined.triage/untriagedIndicates an issue requires triaging or verification

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions