Does someone have a code sample of how to do multithreaded SkiaSharp drawing? I'm trying to achieve that in a PCL, but I've not gotten any further.
What I'm trying to do is:
1. await method X in method A
2. in method X start a new Task which returns a SKImage
3. in method X create (and dispose when done) SKSurface
4. draw to the Canvas of created SKSurface
5. return SKSurface.Canvas.Snapshot
6. in method A draw the returned SKImage to the original SKCanvasView's Surface.Canvas
7. dispose the SKImage
8. dispose original SKCanvasView's Surface.Canvas
But at step 6 I get the following exception:
[Mono] Found as 'sk_canvas_draw_image'.
[mono-rt] Stacktrace:
[mono-rt]
[mono-rt] at <unknown> <0xffffffff>
[mono-rt] at (wrapper managed-to-native) SkiaSharp.SkiaApi.sk_canvas_draw_image (intptr,intptr,single,single,intptr) <0x0004f>
[mono-rt] at SkiaSharp.SKCanvas.DrawImage (SkiaSharp.SKImage,single,single,SkiaSharp.SKPaint) [0x00037] in <8e4c06e066264a27bdda5f7ce87d3a90>:0
[mono-rt] at MyView/<OnDrawGraph>d__6.MoveNext () [0x00177] in /.../Views/MyView.xaml.cs:72
[mono-rt] at System.Runtime.CompilerServices.AsyncMethodBuilderCore/MoveNextRunner.InvokeMoveNext (object) [0x00006] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00073] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00004] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Runtime.CompilerServices.AsyncMethodBuilderCore/MoveNextRunner.Run () [0x00032] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.AwaitTaskContinuation.InvokeAction (object) [0x00006] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.Threading.ContextCallback,object,System.Threading.Tasks.Task&) [0x00013] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run (System.Threading.Tasks.Task,bool) [0x00021] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.Task.FinishContinuations () [0x000b5] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.Task.FinishStageThree () [0x0003d] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Threading.Tasks.Task`1<TResult_REF>.TrySetResult (TResult_REF) [0x00050] in <3fd174ff54b146228c505f23cf75ce71>:0
[mono-rt] at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<TResult_REF>.SetResult (TResult_REF) [0x00040] in <3fd174ff54b146228c505f23cf75ce71>:0
The original code:
async void OnDrawGraph(object sender, SKPaintSurfaceEventArgs e)
{
var canvasWidth = e.Info.Width;
var canvasHeight = e.Info.Height;
using (var theCanvas = e.Surface.Canvas)
{
theCanvas.Clear(MyArgs.GraphBackgroundColor.ToSKColor());
using (var axesImage = await DrawAxes(args, theCanvas))
{
theCanvas.DrawImage(axesImage, 0, 0);
}
theCanvas.Flush();
}
}
// theCanvas is from old code and is not used anymore (gonna remove it if multithreading works)
async Task<SKImage> DrawAxes(MyArgs args, SKCanvas theCanvas)
{
return await Task.Run(() =>
{
using (var myCanvas = SKSurface.Create((int)args.GraphWidth, (int)args.GraphHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul))
using (SKPaint paint = new SKPaint())
{
int numberOfAxes = ...;
float axisYPosition = ...;
var axis = new Axis
{
Args = args,
YPosition = axisYPosition,
HeaderText = "1",
IsBaseXAxis = (i == 0)
};
axis.Draw(paint, myCanvas.Canvas);
}
return myCanvas.Snapshot();
});
}
Any help would be appreciated, as I'm highly stuck :-)