From c7162dbc4764d413d87e785ad55c445245626729 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Mon, 27 Apr 2020 21:50:40 +0200 Subject: [PATCH] First go at a zero-allocation --- .../SkiaSharp.Views.UWP/SKSwapChainPanel.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.UWP/SKSwapChainPanel.cs b/source/SkiaSharp.Views/SkiaSharp.Views.UWP/SKSwapChainPanel.cs index a04e2073d3..c76e6a3c54 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.UWP/SKSwapChainPanel.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.UWP/SKSwapChainPanel.cs @@ -18,6 +18,9 @@ public class SKSwapChainPanel : AngleSwapChainPanel private SKSizeI lastSize; + private SKPaintGLSurfaceEventArgs lastPaintEventArgs; + private bool reusePaintEventArgsInstance = true; + public SKSwapChainPanel() { } @@ -80,14 +83,18 @@ protected override void OnRenderFrame(Rect rect) { surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType); canvas = surface.Canvas; - } - using (new SKAutoCanvasRestore(canvas, true)) - { - // start drawing - OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType, glInfo)); + // reset the args + lastPaintEventArgs = null; } + // start drawing + if (!reusePaintEventArgsInstance || lastPaintEventArgs == null) + lastPaintEventArgs = new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType, glInfo); + + OnPaintSurface(lastPaintEventArgs); + canvas.RestoreToCount(0); + // update the control canvas.Flush(); context.Flush(); @@ -97,6 +104,7 @@ protected override void OnDestroyingContext() { base.OnDestroyingContext(); + lastPaintEventArgs = null; lastSize = default; canvas?.Dispose();