Skip to content

Commit

Permalink
chore: fix CompositionSpriteShape bug and move temp methods to SkiaHe…
Browse files Browse the repository at this point in the history
…lper
  • Loading branch information
ramezgerges committed Oct 16, 2024
1 parent 9264d5e commit a207fd5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal override void UpdatePaint(SKPaint paint, SKRect bounds)

void IOnlineBrush.Paint(in Visual.PaintingSession session, SKRect bounds)
{
var resultPaint = SkiaExtensions.GetTempSKPaint();
var resultPaint = SkiaHelper.GetTempSKPaint();
resultPaint.IsAntialias = true;

UpdatePaint(resultPaint, bounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal override void Paint(in Visual.PaintingSession session)
// On Windows, the stroke is simply 1px, it doesn't scale with the height.
// So, to get a correct stroke geometry, we must apply the transformations first.

var strokeFillPath = SkiaExtensions.GetTempSKPath();
var strokeFillPath = SkiaHelper.GetTempSKPath();
// Get the stroke geometry, after scaling has been applied.
strokePaint.GetFillPath(geometryWithTransformations, strokeFillPath);

Expand All @@ -95,7 +95,7 @@ private SKPaint GetTempFillPaint(SKColorFilter? colorFilter)

private static SKPaint GetTempPaint(bool isStroke, SKColorFilter? colorFilter, bool isHighQuality = false)
{
var paint = SkiaExtensions.GetTempSKPaint();
var paint = isStroke ? SkiaHelper.GetTempSKPaint() : SkiaHelper.GetAnotherTempSKPaint();
paint.IsAntialias = true;
paint.ColorFilter = colorFilter;

Expand Down Expand Up @@ -162,7 +162,7 @@ internal override bool HitTest(Point point)
var strokePaint = GetTempStrokePaint(null);
strokePaint.StrokeWidth = StrokeThickness;

var hitTestStrokeFillPath = SkiaExtensions.GetTempSKPath();
var hitTestStrokeFillPath = SkiaHelper.GetTempSKPath();
strokePaint.GetFillPath(geometryWithTransformations, hitTestStrokeFillPath);
if (hitTestStrokeFillPath.Contains((float)point.X, (float)point.Y))
{
Expand Down
19 changes: 0 additions & 19 deletions src/Uno.UI.Composition/Composition/SkiaExtensions.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace Microsoft.UI.Composition
{
public static class SkiaExtensions
{
[ThreadStatic] private static readonly SKPath _tempPath = new SKPath();
[ThreadStatic] private static readonly SKPaint _tempPaint = new SKPaint();

public static SKRect ToSKRect(this Rect rect)
=> new SKRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom);

Expand Down Expand Up @@ -144,21 +141,5 @@ public static SKBitmap ToSKBitmap(this SKImage image)

return bmp!;
}

public static SKPath GetTempSKPath()
{
// Note the difference between Rewind and Reset
// https://api.skia.org/classSkPath.html#a8dc858ee4c95a59b3dd4bdd3f7b85fdc : "Use rewind() instead of reset() if SkPath storage will be reused and performance is critical."
_tempPath.Rewind();
return _tempPath;
}

public static SKPaint GetTempSKPaint()
{
// https://api.skia.org/classSkPaint.html#a6c7118c97a0e8819d75aa757afbc4c49
// "This is equivalent to replacing SkPaint with the result of SkPaint()."
_tempPaint.Reset();
return _tempPaint;
}
}
}
43 changes: 43 additions & 0 deletions src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#nullable enable

using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Windows.Foundation;
using Windows.UI;

namespace Microsoft.UI.Composition
{
public static class SkiaHelper
{
[ThreadStatic] private static readonly SKPath _tempPath = new SKPath();

Check warning on line 15 in src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs#L15

Remove this initialization of '_tempPath' or make it lazy.
[ThreadStatic] private static readonly SKPaint _tempPaint = new SKPaint();

Check warning on line 16 in src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs#L16

Remove this initialization of '_tempPaint' or make it lazy.
[ThreadStatic] private static readonly SKPaint _tempPaint2 = new SKPaint();

Check warning on line 17 in src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.Composition/Composition/SkiaHelper.skia.cs#L17

Remove this initialization of '_tempPaint2' or make it lazy.

public static SKPath GetTempSKPath()
{
// Note the difference between Rewind and Reset
// https://api.skia.org/classSkPath.html#a8dc858ee4c95a59b3dd4bdd3f7b85fdc : "Use rewind() instead of reset() if SkPath storage will be reused and performance is critical."
_tempPath.Rewind();
return _tempPath;
}

public static SKPaint GetTempSKPaint()
{
// https://api.skia.org/classSkPaint.html#a6c7118c97a0e8819d75aa757afbc4c49
// "This is equivalent to replacing SkPaint with the result of SkPaint()."
_tempPaint.Reset();
return _tempPaint;
}

public static SKPaint GetAnotherTempSKPaint()
{
// https://api.skia.org/classSkPaint.html#a6c7118c97a0e8819d75aa757afbc4c49
// "This is equivalent to replacing SkPaint with the result of SkPaint()."
_tempPaint2.Reset();
return _tempPaint2;
}
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ partial void SetupInlines()
var canvas = t.canvas;
var rect = t.rect;

var paint = SkiaExtensions.GetTempSKPaint();
var paint = SkiaHelper.GetTempSKPaint();
paint.Color = SelectionHighlightColor.Color.ToSKColor();
paint.Style = SKPaintStyle.Fill;
canvas.DrawRect(new SKRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom), paint);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void UpdateTextBoxView()
var caretRect = args.rect;
var compositor = _visual.Compositor;
var brush = DefaultBrushes.TextForegroundBrush.GetOrCreateCompositionBrush(compositor);
var caretPaint = Microsoft.UI.Composition.SkiaExtensions.GetTempSKPaint();
var caretPaint = SkiaHelper.GetTempSKPaint();
brush.UpdatePaint(caretPaint, caretRect.ToSKRect());
args.canvas.DrawRect(
new SKRect((float)caretRect.Left, (float)caretRect.Top, (float)caretRect.Right,
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Documents/InlineCollection.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ internal void Draw(in Visual.PaintingSession session)
var inline = segment.Inline;
var fontInfo = segment.FallbackFont ?? inline.FontInfo;

var paint = SkiaExtensions.GetTempSKPaint();
var paint = SkiaHelper.GetTempSKPaint();
paint.TextEncoding = SKTextEncoding.Utf16;
paint.IsStroke = false;
paint.IsAntialias = true;
Expand Down

0 comments on commit a207fd5

Please sign in to comment.