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
24.57k stars 2.12k forks source link

Opacity on element with CustomDrawOperation affect Clip or ClipToBounds #11343

Closed dbriard closed 1 year ago

dbriard commented 1 year ago

Describe the bug I have a custom control that use CustomDrawOperation to blur an image. ClipToBounds is false on element and its ancestors... The custom draw operation draw outside of the element bounds (what I want to do). But when I set opacity to something different from 1, my element get clipped to its bounds (what I never asked for)

Expected behavior Opacity on element with CustomDrawOperation should NOT clip to bounds, just change the opacity...

Screenshots Blur with Opacity=1 image

Same Blur with Opacity < 1 (the element is clipped, I don't want that). image

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

kekekeks commented 1 year ago

@Gillibald could it be the result of "proper opacity" with clip rects?

Gillibald commented 1 year ago

Only possible if UseOpacitySaveLayer is enabled

@dbriard Please provide a minimal repro and I will have a look

dbriard commented 1 year ago

Only possible if UseOpacitySaveLayer is enabled

@dbriard Please provide a minimal repro and I will have a look

@Gillibald @kekekeks

Thanks for looking at this issue guys. The Opacity bug only happen when UseOpacitySaveLayer is enabled. Here is minimal repro:

AvaloniaOpacityBug.zip

When writting this repro, I run into a new refresh bug: (happen with SaveLayer on or off)

  1. Run the app -> the space around the image is not what I asked for, part of the image is missing at left and top. image

  2. Change anything that refresh the image (opacity or blur slider, or resize the window), the rendering is now correct (not clipped) image

The image is in a border with uniform padding `

    </Border>`

It is loaded in Window's Loaded event:

private void OnLoaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            var loader = AvaloniaLocator.Current.GetService<IAssetLoader>();
            var s = loader.Open(new Uri("avares://AvaloniaOpacityBug/hirsch-899118_640.jpg"));
            var bitmap = SKBitmap.Decode(s);
            image.Source = bitmap;
        }

When the render function is called again without changing the sliders or resizing, for example when I ctrl+tab between VS and the app, the bug is still present, I have to change sliders or resize to remove it...

and the values in the custom draw op are the same when the issue is visible or not: image

The issue is present in the designer too: image

It is not present with the Avalonia Image, only with the custom draw image in the sample, which basically is the Avalonia image with IImage replaced with SKBitmap and render replaced with custom draw.

Hope that help...