Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SKIA] Don't skip setting SKCanvas.TotalMatrix when we aren't sure what the current transform is due to Save/Restore calls #15027

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/Skia/Avalonia.Skia/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal partial class DrawingContextImpl : IDrawingContextImpl,
private readonly Matrix? _postTransform;
private double _currentOpacity = 1.0f;
private readonly bool _disableSubpixelTextRendering;
private Matrix _currentTransform;
private Matrix? _currentTransform;
private bool _disposed;
private GRContext? _grContext;
public GRContext? GrContext => _grContext;
Expand Down Expand Up @@ -466,7 +466,7 @@ public void DrawRectangle(IBrush? brush, IPen? pen, RoundedRect rect, BoxShadows
Transform = oldTransform;
}

Canvas.Restore();
RestoreCanvas();
}
}
}
Expand Down Expand Up @@ -509,7 +509,7 @@ public void DrawRectangle(IBrush? brush, IPen? pen, RoundedRect rect, BoxShadows
using (var outerRRect = new SKRoundRect(outerRect))
Canvas.DrawRoundRectDifference(outerRRect, shadowRect, shadow.Paint);
Transform = oldTransform;
Canvas.Restore();
RestoreCanvas();
SKRoundRectCache.Shared.Return(shadowRect);
}
}
Expand Down Expand Up @@ -672,11 +672,17 @@ public void PushClip(IPlatformRenderInterfaceRegion region)
Canvas.ClipRegion(r);
}

private void RestoreCanvas()
{
_currentTransform = null;
Canvas.Restore();
}

/// <inheritdoc />
public void PopClip()
{
CheckLease();
Canvas.Restore();
RestoreCanvas();
}

public void PushLayer(Rect bounds)
Expand All @@ -688,7 +694,7 @@ public void PushLayer(Rect bounds)
public void PopLayer()
{
CheckLease();
Canvas.Restore();
RestoreCanvas();
}

/// <inheritdoc />
Expand Down Expand Up @@ -731,7 +737,7 @@ public void PopOpacity()

if (useOpacitySaveLayer)
{
Canvas.Restore();
RestoreCanvas();
}

_currentOpacity = _opacityStack.Pop();
Expand Down Expand Up @@ -796,7 +802,7 @@ public void PushGeometryClip(IGeometryImpl clip)
public void PopGeometryClip()
{
CheckLease();
Canvas.Restore();
RestoreCanvas();
}

/// <inheritdoc />
Expand Down Expand Up @@ -829,15 +835,15 @@ public void PopOpacityMask()
// Return the paint wrapper's paint less the reset since the paint is already reset in the Dispose method above.
SKPaintCache.Shared.Return(paintWrapper.Paint);

Canvas.Restore();
RestoreCanvas();

Canvas.Restore();
RestoreCanvas();
}

/// <inheritdoc />
public Matrix Transform
{
get { return _currentTransform; }
get { return _currentTransform ??= Canvas.TotalMatrix.ToAvaloniaMatrix(); }
set
{
CheckLease();
Expand Down
5 changes: 5 additions & 0 deletions src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public static SKMatrix ToSKMatrix(this Matrix m)
return sm;
}

internal static Matrix ToAvaloniaMatrix(this SKMatrix m) => new(
m.ScaleX, m.SkewY, m.Persp0,
m.SkewX, m.ScaleY, m.Persp1,
m.TransX, m.TransY, m.Persp2);

public static SKColor ToSKColor(this Color c)
{
return new SKColor(c.R, c.G, c.B, c.A);
Expand Down