0

in my project (WinUi 3) i draw some polylines to plot some "real time" datas with Skiasharp. I saw the GPU-load is very high. So i tryed, what happens when less datas are drawn. ....the GPU load is still the same. Only if i change the size of the "SKXamlCanvas" it reduces the GPU load. (but the plot shout have a certain size...) So i tryed to render my chart in a bitmap with a lower resolution and scale it. But still it needs the same recources.

Is there any possibility to reduce the basic GPU-load? (as example an possibility to render in a lower resolution and scale it afterwords)

Example Application (.net 6.0 Winui-3) (Installed Modules: SkiaSharp, Skiasharp.Views.Winui) XAML:

x:Class="skEfficientDraw.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:skEfficientDraw"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:skia="using:SkiaSharp.Views.Windows"
mc:Ignorable="d">

<Grid>
    <skia:SKXamlCanvas x:Name="skCanvas" PaintSurface="skCanvas_PaintSurface" />
</Grid>

C#:

public sealed partial class MainWindow : Window
    {
        
        SKPaint linePaint = new SKPaint
        {
            Color = SKColor.Parse("#003366"),
            StrokeWidth = 5,
            IsAntialias = true,
            Style = SKPaintStyle.Stroke
        };

        public MainWindow()
        {
            this.InitializeComponent();

            DispatcherTimer DrawTimer;
            DrawTimer = new DispatcherTimer();
            DrawTimer.Tick += DrawTimer_Tick;
            DrawTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            DrawTimer.Start();
        }

        private void DrawTimer_Tick(object sender, object e)
        {
            skCanvas.Invalidate();
        }

        private void skCanvas_PaintSurface(object sender, SkiaSharp.Views.Windows.SKPaintSurfaceEventArgs e)
        {
            ///
            /// - The GPU load is very high, also if lineCount is 0
            /// - Is there a possibility to reduce the GPU-load? Maybe rendering the "skCanvas" in a lower resolution?
            ///
            SKImageInfo info = e.Info;
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;

            canvas.Clear();

            int lineCount = 5;
            Random rand = new Random();
            for (int i = 0; i < lineCount; i++)
            {
                int x1 = rand.Next(info.Width);
                int y1 = rand.Next(info.Height);
                int x2 = rand.Next(info.Width);
                int y2 = rand.Next(info.Height);
                canvas.DrawLine(x1, y1, x2, y2, linePaint);
            }
        }

    }
Weevil
  • 87
  • 1
  • 3
  • 8
  • To fully understand what my cause this, you should post a minimal reproducible sample https://stackoverflow.com/help/minimal-reproducible-example – Simon Mourier Jul 28 '22 at 05:44
  • @SimonMourier Thank you for your comment! I adjusted my post and included a working sample. – Weevil Aug 02 '22 at 19:55
  • Drawing 5 lines should not cause high GPU load. I'm guessing the rendering is being done on the CPU, then the resulting bitmap is copied across to the GPU, which is not good for performance. Have a look at [this answer](https://stackoverflow.com/a/48940790/2363967) – Andrew Williamson Aug 02 '22 at 20:02
  • Thanks for the comment! Do you have a hint on how to optimize my sample code to render direct on the GPU? I do not find a working example... – Weevil Aug 03 '22 at 05:48
  • My GPU (AMD RX 5600 XT) is only at 3% with your sample. You don't seem to do anything bad, but check this out https://github.com/mono/SkiaSharp/issues/1893 and https://github.com/microsoft/microsoft-ui-xaml/issues/2558 note you may not need SkiaSharp depending on what you really do in your project. – Simon Mourier Aug 03 '22 at 06:58
  • Thats a really useful information! I run my app on a tablet with atom processor (80% GPU usage...). Till now SkiaSharp is the best option I found for drawing in WinUi3. I hope it will be further optimized in future... – Weevil Aug 03 '22 at 17:00

0 Answers0