Skip to content

Commit

Permalink
Merge pull request #215 from wieslawsoltes/RefactorSvgSource
Browse files Browse the repository at this point in the history
Optimize memory usage of the SvgSource
  • Loading branch information
wieslawsoltes committed Mar 27, 2024
2 parents dd0be69 + dbe4661 commit 345fc31
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 145 deletions.
15 changes: 6 additions & 9 deletions samples/AvaloniaSvgSkiaSample/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ private void Drop(object sender, DragEventArgs e)
}
else if (sender == svgExtensionDockPanel)
{
var svg = new SvgSource(default(Uri));
var picture = svg.Load(fileName);
if (picture is { })
var svg = SvgSource.Load(fileName);
if (svg is { })
{
svgExtensionImage.Source = new SvgImage
{
Expand All @@ -119,9 +118,8 @@ private void Drop(object sender, DragEventArgs e)
}
else if (sender == svgSourceDockPanel)
{
var svg = new SvgSource(default(Uri));
var picture = svg.Load(fileName);
if (picture is { })
var svg = SvgSource.Load(fileName);
if (svg is { })
{
svgSourceImage.Source = new SvgImage
{
Expand All @@ -131,9 +129,8 @@ private void Drop(object sender, DragEventArgs e)
}
else if (sender == svgResourceDockPanel)
{
var svg = new SvgSource(default(Uri));
var picture = svg.Load(fileName);
if (picture is { })
var svg = SvgSource.Load(fileName);
if (svg is { })
{
svgResourceImage.Source = new SvgImage
{
Expand Down
40 changes: 2 additions & 38 deletions samples/TestApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public ReadOnlyObservableCollection<FileItemViewModel>? FilteredItems

public ICommand AddItemCommand { get; }

public ICommand CopyAsCSharpCommand { get; }

public ICommand ExportCommand { get; }

private List<FilePickerFileType> GetConfigurationFileTypes()
Expand Down Expand Up @@ -115,14 +113,12 @@ public MainWindowViewModel()

AddItemCommand = ReactiveCommand.CreateFromTask(async () => await AddItemExecute());

CopyAsCSharpCommand = ReactiveCommand.CreateFromTask<Avalonia.Svg.Skia.Svg>(async svg => await CopyAsCSharpExecute(svg));

ExportCommand = ReactiveCommand.CreateFromTask<Avalonia.Svg.Skia.Svg>(async svg => await ExportExecute(svg));
}

private async Task ExportExecute(Avalonia.Svg.Skia.Svg svg)
{
if (_selectedItem is null || svg.Model is null)
if (_selectedItem is null || svg.Picture is null)
{
return;
}
Expand Down Expand Up @@ -157,31 +153,6 @@ private async Task ExportExecute(Avalonia.Svg.Skia.Svg svg)
}
}

private async Task CopyAsCSharpExecute(Avalonia.Svg.Skia.Svg svg)
{
if (_selectedItem is null || svg?.Model is null)
{
return;
}

var code = SkiaCSharpCodeGen.Generate(svg.Model, "Svg", CreateClassName(_selectedItem.Path));

await Dispatcher.UIThread.InvokeAsync(() =>
{
try
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
{
lifetime.MainWindow?.Clipboard?.SetTextAsync(code);
}
}
catch
{
// ignored
}
});
}

private async Task AddItemExecute()
{
var window = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
Expand Down Expand Up @@ -370,7 +341,7 @@ public void SaveConfiguration(Stream stream)

public async Task Export(Stream stream, string name, Avalonia.Svg.Skia.Svg? svg, string backgroundColor, float scaleX, float scaleY)
{
if (svg?.Model is null || svg.Picture is null)
if (svg.Picture is null)
{
return;
}
Expand Down Expand Up @@ -422,13 +393,6 @@ public async Task Export(Stream stream, string name, Avalonia.Svg.Skia.Svg? svg,
svg.Picture?.ToXps(stream, skBackgroundColor, scaleX, scaleY);
break;
}
case ".cs":
{
var code = SkiaCSharpCodeGen.Generate(svg.Model, "Svg", CreateClassName(name));
await using var writer = new StreamWriter(stream);
await writer.WriteAsync(code);
break;
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions samples/TestApp/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@
OnContent="Cache Enabled"
OffContent="Cache Disabled"
Margin="6,0,6,0" />
<Button Content="Copy as C#"
Command="{Binding CopyAsCSharpCommand}"
CommandParameter="{Binding #Svg}"
Margin="0,0,6,0" />
<Button Content="Export"
Command="{Binding ExportCommand}"
CommandParameter="{Binding #Svg}"
Expand All @@ -150,9 +146,6 @@
VerticalScrollBarVisibility="{Binding #VerticalScrollBarVisibility.SelectedItem}">
<ScrollViewer.ContextFlyout>
<MenuFlyout Placement="Bottom">
<MenuItem Header="Copy as C#"
Command="{Binding CopyAsCSharpCommand}"
CommandParameter="{Binding #Svg}" />
<MenuItem Header="Export"
Command="{Binding ExportCommand}"
CommandParameter="{Binding #Svg}" />
Expand Down
20 changes: 4 additions & 16 deletions src/Avalonia.Svg.Skia/Svg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using Avalonia.Logging;
using Avalonia.Media;
using Avalonia.Metadata;
using ShimSkiaSharp;
using Svg.Model;
using Svg.Skia;

namespace Avalonia.Svg.Skia;

Expand All @@ -16,9 +14,9 @@ namespace Avalonia.Svg.Skia;
public class Svg : Control
{
private readonly Uri _baseUri;
private SKSvg? _svg;
private SvgSource? _svg;
private bool _enableCache;
private Dictionary<string, SKSvg>? _cache;
private Dictionary<string, SvgSource>? _cache;

/// <summary>
/// Defines the <see cref="Path"/> property.
Expand Down Expand Up @@ -112,16 +110,6 @@ public bool EnableCache
set { SetAndRaise(EnableCacheProperty, ref _enableCache, value); }
}

/// <summary>
/// Gets svg drawable.
/// </summary>
public SKDrawable? Drawable => _svg?.Drawable;

/// <summary>
/// Gets svg model.
/// </summary>
public SKPicture? Model => _svg?.Model;

/// <summary>
/// Gets svg picture.
/// </summary>
Expand Down Expand Up @@ -245,7 +233,7 @@ public override void Render(DrawingContext context)
using (context.PushTransform(translateMatrix * scaleMatrix))
{
context.Custom(
new SvgCustomDrawOperation(
new SvgSourceCustomDrawOperation(
new Rect(0, 0, bounds.Width, bounds.Height),
source));
}
Expand Down Expand Up @@ -302,7 +290,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
else
{
_cache = new Dictionary<string, SKSvg>();
_cache = new Dictionary<string, SvgSource>();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg.Skia/SvgImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect)
using (context.PushTransform(translateMatrix * scaleMatrix))
{
context.Custom(
new SvgCustomDrawOperation(
new SvgSourceCustomDrawOperation(
new Rect(0, 0, bounds.Width, bounds.Height),
source));
}
Expand Down
Loading

0 comments on commit 345fc31

Please sign in to comment.