Is it possible to create a Skia Canvas element in an Avalonia application?

Viewed 2513

I am hoping to port an Electron app over to Avalonia. The app currently uses Paper.js to draw and manage interactions with complex polygons. Looking into Avalonia, I noticed it uses Skia, which seems to offer much of the same functionality as Paper.js. I was hoping there would be an easy way of creating a Skia "canvas" and just using the SkiaSharp API directly.

Unfortunately, I'm not having much luck finding documentation/answers. Someone on the avalonia gitter mentioned I might use RenderTargetBitmap, but after looking into the source (can't find any documentation on it) I think it would be easier/more elegant to use a Skia canvas directly.

Is this possible?

1 Answers

You have several options:

  • Use WriteableBitmap: lock the bits, create SKBitmap from it, create SKCanvas, draw. Then use that WritableBitmap as a Source for Image control. This is the safest most portable, but unfortunately least performant way.
  • Access the underlying Skia context directly on the render thread, you can find an example here. Note that Render callback might be called from any thread, so manage your locks properly. Also note that Avalonia has pluggable renderer architecture, so renderer is technically not guaranteed to be Skia, even if it's currently used by default on all platforms and is highly likely to remain the primary renderer
  • Once 0.10 is out, you'll be able to create a hardware-accelerated SKCanvas and render to OpenGL texture. The infrastructure required for using OpenGL textures as Avalonia images is currently being worked on in OpenGL control branch
Related