Skip to content

Commit 6e07c45

Browse files
authored
Merge pull request #19429 from ramezgerges/imagebrush_svg_skia
feat(skia): support ImageBrushes with svg URIs
2 parents 65f80cf + 7bcd9ba commit 6e07c45

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

build/PackageDiffIgnore.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,27 @@
19891989
</Methods>
19901990
</IgnoreSet>
19911991

1992+
<IgnoreSet baseVersion="5.6">
1993+
<Assemblies>
1994+
</Assemblies>
1995+
1996+
<Types>
1997+
</Types>
1998+
1999+
<Events>
2000+
</Events>
2001+
2002+
<Fields>
2003+
</Fields>
2004+
2005+
<Properties>
2006+
</Properties>
2007+
2008+
<Methods>
2009+
<Member fullName="System.Object Uno.UI.Xaml.Media.Imaging.Svg.ISvgProvider::TryGetLoadedDataAsPictureAsync()" reason="Needed internally for ImageBrushes with SVG assets on Skia." />
2010+
</Methods>
2011+
</IgnoreSet>
2012+
19922013
<![CDATA[
19932014
The 'baseVersion' should be the current latest stable version published on nuget,
19942015
NOT what will be published by the PR in progress.

src/AddIns/Uno.UI.Svg/SvgProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public UIElement GetCanvas()
9494
=> new SvgCanvas(_owner, this);
9595
#endif
9696

97+
public object? TryGetLoadedDataAsPictureAsync()
98+
#if __SKIA__
99+
=> _skSvg?.Picture;
100+
#else
101+
=> null;
102+
#endif
103+
97104
public
98105
#if !__NETSTD_REFERENCE__
99106
async

src/Uno.UI/UI/Xaml/Media/ImageBrush.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ private void OnSourceChanged(ImageSource newValue, ImageSource oldValue)
8686
(_, _) => OnSourceChangedPartial(newValue, null)
8787
);
8888
}
89+
else if (newValue is SvgImageSource svgImageSource)
90+
{
91+
_sourceDisposable.Disposable = svgImageSource.RegisterDisposablePropertyChangedCallback(
92+
SvgImageSource.UriSourceProperty,
93+
(_, _) => OnSourceChangedPartial(newValue, null)
94+
);
95+
}
8996
else
9097
{
9198
_sourceDisposable.Disposable = null;

src/Uno.UI/UI/Xaml/Media/Imaging/Svg/ISvgProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public interface ISvgProvider
2525

2626
Task<bool> TryLoadSvgDataAsync(byte[] imageData);
2727

28+
/// <returns>An SKPicture on Skia, otherwise null.</returns>
29+
object? TryGetLoadedDataAsPictureAsync() => default;
30+
2831
void Unload();
2932
}

src/Uno.UI/UI/Xaml/Media/Imaging/SvgImageSource.skia.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,39 @@
66
using Uno.Helpers;
77
using Uno.UI.Xaml.Media;
88
using Windows.Application­Model;
9+
using Microsoft.UI.Composition;
10+
using SkiaSharp;
911

1012
namespace Microsoft.UI.Xaml.Media.Imaging;
1113

1214
partial class SvgImageSource
1315
{
14-
private protected override bool TryOpenSourceAsync(CancellationToken ct, int? targetWidth, int? targetHeight, out Task<ImageData> asyncImage) =>
15-
TryOpenSvgImageData(ct, out asyncImage);
16+
private protected override bool TryOpenSourceAsync(CancellationToken ct, int? targetWidth, int? targetHeight, out Task<ImageData> asyncImage)
17+
{
18+
if (TryOpenSvgImageData(ct, out var imageTask))
19+
{
20+
asyncImage = imageTask.ContinueWith(task =>
21+
{
22+
var imageData = task.Result;
23+
if (imageData is { Kind: ImageDataKind.ByteArray, ByteArray: not null } &&
24+
_svgProvider?.TryGetLoadedDataAsPictureAsync() is SKPicture picture)
25+
{
26+
var sourceSize = _svgProvider.SourceSize;
27+
return ImageData.FromCompositionSurface(new SkiaCompositionSurface(SKImage.FromPicture(picture, new SKSizeI((int)sourceSize.Width, (int)sourceSize.Height))));
28+
}
29+
else
30+
{
31+
return ImageData.Empty;
32+
}
33+
}, ct);
34+
return true;
35+
}
36+
else
37+
{
38+
asyncImage = Task.FromResult(ImageData.Empty);
39+
return false;
40+
}
41+
}
1642

1743
private async Task<ImageData> GetSvgImageDataAsync(CancellationToken ct)
1844
{

0 commit comments

Comments
 (0)