Skip to content

Commit 93eddfe

Browse files
MartinZikmundmergify[bot]
authored andcommitted
feat: Shared SKPicture recording
(cherry picked from commit ac367bc)
1 parent 2bcb265 commit 93eddfe

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/Uno.UI/Helpers/SkiaRenderHelper.skia.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,52 @@
77
using Microsoft.UI.Xaml;
88
using Microsoft.UI.Xaml.Controls;
99
using SkiaSharp;
10+
using Uno.UI.Xaml.Core;
1011

1112
namespace Uno.UI.Helpers;
1213

1314
internal static class SkiaRenderHelper
1415
{
16+
internal static (SKPicture, SKPath) RecordPictureAndReturnPath(int width, int height, ContainerVisual rootVisual, bool invertPath)
17+
{
18+
using var recorder = new SKPictureRecorder();
19+
using var canvas = recorder.BeginRecording(new SKRect(-999999, -999999, 999999, 999999));
20+
using var _ = new SKAutoCanvasRestore(canvas, true);
21+
canvas.Clear(SKColors.Transparent);
22+
var path = RenderRootVisualAndReturnPath(width, height, rootVisual, canvas, invertPath);
23+
var picture = recorder.EndRecording();
24+
25+
return (picture, path);
26+
}
27+
1528
/// <summary>
1629
/// Does a rendering cycle and returns a path that represents the visible area of the native views.
1730
/// Takes the current TotalMatrix of the surface's canvas into account
1831
/// </summary>
19-
public static SKPath RenderRootVisualAndReturnNegativePath(int width, int height, ContainerVisual rootVisual, SKCanvas canvas)
32+
public static SKPath RenderRootVisualAndReturnPath(int width, int height, ContainerVisual rootVisual, SKCanvas canvas, bool invertPath)
2033
{
2134
rootVisual.Compositor.RenderRootVisual(canvas, rootVisual);
22-
if (!ContentPresenter.HasNativeElements())
35+
SKPath outPath = new SKPath();
36+
if (ContentPresenter.HasNativeElements())
2337
{
24-
return new SKPath();
38+
var parentClipPath = new SKPath();
39+
parentClipPath.AddRect(new SKRect(0, 0, width, height));
40+
rootVisual.GetNativeViewPath(parentClipPath, outPath);
41+
outPath.Transform(canvas.TotalMatrix, outPath); // canvas.TotalMatrix should be the same before and after RenderRootVisual because of the Save and Restore calls inside
2542
}
26-
var parentClipPath = new SKPath();
27-
parentClipPath.AddRect(new SKRect(0, 0, width, height));
28-
var outPath = new SKPath();
29-
rootVisual.GetNativeViewPath(parentClipPath, outPath);
30-
outPath.Transform(canvas.TotalMatrix, outPath); // canvas.TotalMatrix should be the same before and after RenderRootVisual because of the Save and Restore calls inside
31-
return outPath;
32-
}
3343

34-
/// <summary>
35-
/// Does a rendering cycle and returns a path that represents the total area that was drawn
36-
/// minus the native view areas.
37-
/// </summary>
38-
public static SKPath RenderRootVisualAndReturnPath(int width, int height, ContainerVisual rootVisual, SKCanvas canvas)
39-
{
40-
var outPath = new SKPath();
41-
outPath.AddRect(new SKRect(0, 0, width, height));
42-
outPath.Transform(canvas.TotalMatrix, outPath);
43-
outPath.Op(RenderRootVisualAndReturnNegativePath(width, height, rootVisual, canvas), SKPathOp.Difference, outPath);
44-
return outPath;
44+
if (invertPath)
45+
{
46+
var invertedPath = new SKPath();
47+
invertedPath.AddRect(new SKRect(0, 0, width, height));
48+
invertedPath.Transform(canvas.TotalMatrix, invertedPath);
49+
invertedPath.Op(outPath, SKPathOp.Difference, invertedPath);
50+
return invertedPath;
51+
}
52+
else
53+
{
54+
return outPath;
55+
}
4556
}
4657

4758
public class FpsHelper

0 commit comments

Comments
 (0)