Guidance on Best SkiaSharp Performance

@mattleibow We are creating a graphic-intensive Xamarin.Forms app that uses Skiasharp to create high resolution images for printing. We use low-level drawing, SVG, local and remote images, text with custom fonts, etc. to compose our final output. All of the work is done in the .Netstandard shared project.

Can you direct us to examples, articles, documentation, anything that can give us guidance for how to do this in the most performant way? Here are a few questions.

  1. Presently we have a class that does all the drawing and returns an SKBitmap as the result. While we can use the class and do the work on a background thread, the SKBitmap cannot be used by the UI thread. Can you point to an example of how to properly (i.e. the correct SkiaSharp syntax and functions) marshal the image from the background thread to an SKBitmap on the UI thread?
  2. Related to that, we stopped using the SKCanvasView for two reasons. First, it does not resize nicely within Xamarin.Forms containers, so creating an image with SkiaSharp and then displaying it in a Xamarin.Forms Image object is best because it automatically resizes, playing nicely with Xamarin.Forms containers. Second, our graphics are apparently complex enough, or we are drawing them wrongly, so drawing them in response to the PaintSurface event on the UI thread caused the app to freeze. We can't even display a simple wait animation. That's why we're trying to do all drawing on a background thread.
  3. Would we benefit from using a GRContext and doing all this drawing on the GPU? Does that free the UI thread so it can display a wait animation? I am new to this realm and can find no example code showing how to create and use a GRContext in the .Netstandard shared project. The SkiaSharpFormsDemo does not do anything with the GPU as far as I can tell.
  4. I have studied the SkiaSharpFormsDemo, and have read through dozens of forum posts and most of the issues on the SkiaSharp GitHub project, trying to glean some tips. You mention often that it is important to reuse SKPaint objects, SKTypeFace objects, etc. Would you recommend making them static members of our drawing class and just changing the values every time they are used? The SkiaSharpFormsDemo seems to create SKPaint objects as needed, or within a using block, but I can't find an example of how to properly reuse them.

Thank you so much for your help and all your hard work!

Tagged:
Sign In or Register to comment.