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

feat(skia): support ImageBrushes with svg URIs #19429

Merged
merged 6 commits into from
Feb 20, 2025
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
21 changes: 21 additions & 0 deletions build/PackageDiffIgnore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,27 @@
</Methods>
</IgnoreSet>

<IgnoreSet baseVersion="5.6">
<Assemblies>
</Assemblies>

<Types>
</Types>

<Events>
</Events>

<Fields>
</Fields>

<Properties>
</Properties>

<Methods>
<Member fullName="System.Object Uno.UI.Xaml.Media.Imaging.Svg.ISvgProvider::TryGetLoadedDataAsPictureAsync()" reason="Needed internally for ImageBrushes with SVG assets on Skia." />
</Methods>
</IgnoreSet>

<![CDATA[
The 'baseVersion' should be the current latest stable version published on nuget,
NOT what will be published by the PR in progress.
Expand Down
7 changes: 7 additions & 0 deletions src/AddIns/Uno.UI.Svg/SvgProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public UIElement GetCanvas()
=> new SvgCanvas(_owner, this);
#endif

public object? TryGetLoadedDataAsPictureAsync()
#if __SKIA__
=> _skSvg?.Picture;
#else
=> null;
#endif

public
#if !__NETSTD_REFERENCE__
async
Expand Down
7 changes: 7 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/ImageBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ private void OnSourceChanged(ImageSource newValue, ImageSource oldValue)
(_, _) => OnSourceChangedPartial(newValue, null)
);
}
else if (newValue is SvgImageSource svgImageSource)
{
_sourceDisposable.Disposable = svgImageSource.RegisterDisposablePropertyChangedCallback(
SvgImageSource.UriSourceProperty,
(_, _) => OnSourceChangedPartial(newValue, null)
);
}
else
{
_sourceDisposable.Disposable = null;
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Imaging/Svg/ISvgProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ public interface ISvgProvider

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

/// <returns>An SKPicture on Skia, otherwise null.</returns>
object? TryGetLoadedDataAsPictureAsync() => default;

void Unload();
}
30 changes: 28 additions & 2 deletions src/Uno.UI/UI/Xaml/Media/Imaging/SvgImageSource.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,39 @@
using Uno.Helpers;
using Uno.UI.Xaml.Media;
using Windows.Application­Model;
using Microsoft.UI.Composition;
using SkiaSharp;

namespace Microsoft.UI.Xaml.Media.Imaging;

partial class SvgImageSource
{
private protected override bool TryOpenSourceAsync(CancellationToken ct, int? targetWidth, int? targetHeight, out Task<ImageData> asyncImage) =>
TryOpenSvgImageData(ct, out asyncImage);
private protected override bool TryOpenSourceAsync(CancellationToken ct, int? targetWidth, int? targetHeight, out Task<ImageData> asyncImage)
{
if (TryOpenSvgImageData(ct, out var imageTask))
{
asyncImage = imageTask.ContinueWith(task =>
{
var imageData = task.Result;
if (imageData is { Kind: ImageDataKind.ByteArray, ByteArray: not null } &&
_svgProvider?.TryGetLoadedDataAsPictureAsync() is SKPicture picture)
{
var sourceSize = _svgProvider.SourceSize;
return ImageData.FromCompositionSurface(new SkiaCompositionSurface(SKImage.FromPicture(picture, new SKSizeI((int)sourceSize.Width, (int)sourceSize.Height))));
}
else
{
return ImageData.Empty;
}
}, ct);
return true;
}
else
{
asyncImage = Task.FromResult(ImageData.Empty);
return false;
}
}

private async Task<ImageData> GetSvgImageDataAsync(CancellationToken ct)
{
Expand Down
Loading