From f9c1570ac63b45436ff31c7b9a7c36334c28b47c Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Sun, 22 Feb 2026 14:32:01 +0500 Subject: [PATCH] Fixed incorrect opacity stack handling (#20730) --- .../ServerCompositionVisual.Render.cs | 3 +- tests/Avalonia.RenderTests/BugRepros.cs | 55 ++++++++++++++++++ ..._Should_Not_Affect_Each_Other.expected.png | Bin 0 -> 1896 bytes 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/Avalonia.RenderTests/BugRepros.cs create mode 100644 tests/TestFiles/Skia/BugRepros/Sibling_Visuals_With_Opacity_Should_Not_Affect_Each_Other.expected.png diff --git a/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual/ServerCompositionVisual.Render.cs b/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual/ServerCompositionVisual.Render.cs index d2dd0346d6..8d8b862bb3 100644 --- a/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual/ServerCompositionVisual.Render.cs +++ b/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual/ServerCompositionVisual.Render.cs @@ -94,7 +94,8 @@ partial class ServerCompositionVisual if (visual.Opacity != 1) { - _opacityStack.Push(effectiveOpacity); + _opacityStack.Push(_opacity); + _opacity = effectiveOpacity; _canvas.PushOpacity(visual.Opacity, visual._transformedSubTreeBounds.Value.ToRect()); } diff --git a/tests/Avalonia.RenderTests/BugRepros.cs b/tests/Avalonia.RenderTests/BugRepros.cs new file mode 100644 index 0000000000..02d41569a6 --- /dev/null +++ b/tests/Avalonia.RenderTests/BugRepros.cs @@ -0,0 +1,55 @@ +using Avalonia.Controls; +using Avalonia.Controls.Shapes; +using Avalonia.Layout; +using Avalonia.Media; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace Avalonia.Skia.RenderTests; + +public class BugRepros() : TestBase(nameof(BugRepros)) +{ + [Fact] + public async Task Sibling_Visuals_With_Opacity_Should_Not_Affect_Each_Other() + { + var brushes = new IBrush[] + { + Brushes.Red, + Brushes.Green, + Brushes.Blue, + Brushes.Yellow, + Brushes.Magenta, + Brushes.Cyan, + Brushes.Orange, + Brushes.Purple, + Brushes.Pink, + Brushes.Brown + }; + + var stackPanel = new StackPanel + { + Orientation = Orientation.Vertical, + Width = 300, + Height = 500, + Background = Brushes.White, + }; + + for (int i = 0; i < brushes.Length; i++) + { + var border = new Border + { + Width = 280, + Height = 40, + BorderThickness = new Thickness(2), + Margin = new Thickness(5), + Background = brushes[i], + Opacity = 0.3 + }; + stackPanel.Children.Add(border); + } + + await RenderToFile(stackPanel); + CompareImages(); + } +} diff --git a/tests/TestFiles/Skia/BugRepros/Sibling_Visuals_With_Opacity_Should_Not_Affect_Each_Other.expected.png b/tests/TestFiles/Skia/BugRepros/Sibling_Visuals_With_Opacity_Should_Not_Affect_Each_Other.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..328c1a3e7d58edf0db4855531ce17ec266289702 GIT binary patch literal 1896 zcmeAS@N?(olHy`uVBq!ia0y~yVAKKPFC1(@k#+$Ab0Ec1?Bp530R%N1DIE+9?DC#2 zjv*Cu-d)sgjRm;69UMN!@}wLd+OuQ(tF!kDwaNe9I@|fBE8)T4WQd+&RkYz2>SsKNGxuJ z1@U^B`Oj;e8_KQvzD5s;Ainjp@;cvxt$k0uhlF!4eXYFB_h4<`*P)VtEAR6?SlY+E z-Ht$FhbAE0NsCOGSTt5^{yfBDs_4CPa~h#eg1Q@%2)W zLA!>C(&