AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.16k stars 2.18k forks source link

DrawingContext has a mess in state management in 11.1.0-rc2. #16337

Open liwuqingxin opened 1 month ago

liwuqingxin commented 1 month ago

Describe the bug

When I call DrawingContext.PushOpacity() and then draw text at a position other than (0,0), the transform state of DrawingContext becomes messed.

// Line #1
RenderLine1(context, new Point(0,0), new Point(100,0));
// PushOpacity And DrawText
RenderAText(context, new Point(50, 20));
// Line#2
RenderLine2(context, new Point(100,0), new Point(100,100));

In code above, the line#2 appears at the point (100+50, 0+20) but (100,0).

To Reproduce

I have made a demo to demonstrate it. Avalonia.v11.1.0.rc2.Render.Demo.zip

Expected behavior

In the demo, a square should be drawn.

Avalonia version

11.1.0-rc2

OS

Windows

Additional context

No response

Gillibald commented 1 month ago

When Flush happens here: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Rendering/Composition/Server/DrawingContextProxy.cs#L70

The current transform matrix isn't in sync so I have to adjust this line: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Rendering/Composition/Server/DrawingContextProxy.PendingCommands.cs#L149

by adding this:

 if (Transform != _impl.Transform)
 {
     _impl.Transform = Transform;
 }

It fixes the issue but the question is why the transforms are out of sync