Skip to content

Instantly share code, notes, and snippets.

@erisonliang
Forked from Aurumaker72/SkiaCanvas.cs
Created March 1, 2024 06:20
Show Gist options
  • Select an option

  • Save erisonliang/deed3d82687d7738357afbe82660031a to your computer and use it in GitHub Desktop.

Select an option

Save erisonliang/deed3d82687d7738357afbe82660031a to your computer and use it in GitHub Desktop.
SkiaCanvas for Avalonia 11
public class SkiaRenderEventArgs : EventArgs
{
public SkiaCanvas Sender { get; init; }
public SKCanvas Canvas { get; init; }
}
public class SkiaCanvas : UserControl
{
private class SkiaCallbackRenderOperation : ICustomDrawOperation
{
public Action<SKCanvas>? RenderCall;
public Rect Bounds { get; set; }
public void Dispose()
{
}
public bool Equals(ICustomDrawOperation? other)
{
return false;
}
public bool HitTest(Point p)
{
return false;
}
public void Render(ImmediateDrawingContext context)
{
// This method is reached only when resizing the window or opening a menuitem over the control
// No calls from Render, invoked via custom.Render reach this
if (!context.TryGetFeature<ISkiaSharpApiLeaseFeature>(out var leaseFeature))
throw new Exception("SkiaSharp is not supported.");
using var lease = leaseFeature.Lease();
RenderCall?.Invoke(lease.SkCanvas);
}
}
public event Action<SkiaRenderEventArgs>? RenderSkia;
private readonly SkiaCallbackRenderOperation _skiaCallbackRenderOperation;
public SkiaCanvas()
{
_skiaCallbackRenderOperation = new SkiaCallbackRenderOperation
{
RenderCall = canvas =>
{
RenderSkia?.Invoke(new SkiaRenderEventArgs
{
Sender = this,
Canvas = canvas
});
}
};
ClipToBounds = true;
}
public override void Render(DrawingContext context)
{
// This method (Render) is called at refresh rate, everything is fine
_skiaCallbackRenderOperation.Bounds = new Rect(0, 0, DesiredSize.Width, DesiredSize.Height);
// Jump into SkiaCallbackRenderOperation.Render
context.Custom(_skiaCallbackRenderOperation);
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment