Skip to content

Commit

Permalink
feat(skia): Add support for WriteableBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 18, 2020
1 parent 50aef82 commit 8da3a80
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Imaging/WriteableBitmap.skia.cs
@@ -0,0 +1,30 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Windows.Foundation;
using Windows.Storage.Streams;
using Uno.Foundation;
using Windows.UI.Composition;

namespace Windows.UI.Xaml.Media.Imaging
{
partial class WriteableBitmap
{
private SkiaCompositionSurface _surface;

private protected override bool TryOpenSourceSync(int? targetWidth, int? targetHeight, out ImageData image)
{
var handle = GCHandle.Alloc(_buffer.Data, GCHandleType.Pinned);

_surface ??= new SkiaCompositionSurface();

_surface.SetPixels(PixelWidth, PixelHeight, _buffer.Data);

image = new ImageData {
Value = _surface
};

return true;
}
}
}
1 change: 1 addition & 0 deletions src/Uno.UI/Uno.UI.Skia.csproj
Expand Up @@ -7,6 +7,7 @@
<UnoSourceGeneratorUseGenerationHost>false</UnoSourceGeneratorUseGenerationHost>
<NoWarn>$(NoWarn);NU1701</NoWarn>
<DefineConstants>$(DefineConstants);IS_UNO</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<Deterministic>true</Deterministic>
Expand Down
11 changes: 11 additions & 0 deletions src/Uno.UWP/UI/Composition/SkiaCompositionSurface.skia.cs
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Uno.Extensions;
using Uno.Logging;
Expand Down Expand Up @@ -56,5 +57,15 @@ internal void LoadFromBytes(byte[] image)
}
}
}

internal unsafe void SetPixels(int pixelWidth, int pixelHeight, byte[] data)
{
var info = new SKImageInfo(pixelWidth, pixelHeight, SKColorType.Rgba8888, SKAlphaType.Premul);

fixed (byte* pData = data)
{
_image = SKImage.FromPixels(info, (IntPtr)pData, pixelWidth * 4);
}
}
}
}
1 change: 1 addition & 0 deletions src/Uno.UWP/Uno.Skia.csproj
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(UnoTargetFrameworkOverride)'!=''">
Expand Down

0 comments on commit 8da3a80

Please sign in to comment.