3

I have a background thread, which renders some image to WriteableBitmap. I'm making a custom control, which will use this writeablebitmap and update every frame to make animation. Unfortunately, now forcing this control to InvalidateVisual(). That worked, but the performance is pretty bad. On the main window I subscribed to Renderer.SceneInvalidated to log some framerates.

this.Renderer.SceneInvalidated += (s, e) =>
{
    _logs.Add((DateTime.Now - _prev).Ticks);

    _prev = DateTime.Now;
};

And found that about 7%-10% frames are rendered in 30ms, and others 90% in 16ms. So I'm looking for a better performance solution with this problem.

Kamiky
  • 601
  • 2
  • 8
  • 25

1 Answers1

2

You can utilize custom drawing operation API with direct access to SKCanvas on the render thread.

public class CustomSkiaPage : Control
{   
  public CustomSkiaPage()
  { 
    ClipToBounds = true;
  } 
        
  class CustomDrawOp : ICustomDrawOperation
  { 
    public CustomDrawOp(Rect bounds)
    {   
      Bounds = bounds;
    }   
            
    public void Dispose()
    {   
      // No-op in this example
    }   

    public Rect Bounds { get; }
    public bool HitTest(Point p) => false;
    public bool Equals(ICustomDrawOperation other) => false;
    public void Render(IDrawingContextImpl context)
    {   
      var canvas = (context as ISkiaDrawingContextImpl)?.SkCanvas;
      // draw stuff 
    }   
  } 

  public override void Render(DrawingContext context)
  { 
    context.Custom(new CustomDrawOp(new Rect(0, 0, Bounds.Width, Bounds.Height)));
    Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
  } 
}
kekekeks
  • 3,193
  • 19
  • 16
  • Well, that's fine, but I cant find a way to set pixels from my Bitmap/WriteableBitmap on this control. For example I need a method public void SetBitmap(WriteableBitmap bmp) {...} – Kamiky Mar 18 '21 at 15:20
  • Since you want maximum possible performance, you currently have to use SKBitmap directly instead of WritableBitmap. – kekekeks Mar 18 '21 at 20:38