Skip to content

Commit

Permalink
Merge pull request #19429 from ramezgerges/imagebrush_svg_skia
Browse files Browse the repository at this point in the history
feat(skia): support ImageBrushes with svg URIs
  • Loading branch information
jeromelaban authored Feb 20, 2025
2 parents 65f80cf + 7bcd9ba commit 6e07c45
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
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

0 comments on commit 6e07c45

Please sign in to comment.