-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21e4bba
commit ce6f35c
Showing
9 changed files
with
302 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.IO; | ||
using SkiaSharp.Resources; | ||
|
||
namespace SkiaSharp.Skottie | ||
{ | ||
public sealed unsafe class AnimationBuilder : SKObject, ISKSkipObjectRegistration | ||
{ | ||
internal AnimationBuilder (AnimationBuilderFlags flags) | ||
: this (SkottieApi.skottie_animation_builder_new (flags), true) | ||
{ | ||
} | ||
|
||
internal AnimationBuilder (IntPtr handle, bool owns) | ||
: base (handle, owns) | ||
{ | ||
} | ||
|
||
public AnimationBuilder SetFontManager (SKFontManager fontManager) | ||
{ | ||
_ = fontManager ?? throw new ArgumentNullException (nameof (fontManager)); | ||
SkottieApi.skottie_animation_builder_set_font_manager (Handle, fontManager.Handle); | ||
return this; | ||
} | ||
|
||
public AnimationBuilder SetResourceProvider (ResourceProvider resourceProvider) | ||
{ | ||
_ = resourceProvider ?? throw new ArgumentNullException (nameof (resourceProvider)); | ||
SkottieApi.skottie_animation_builder_set_resource_provider (Handle, resourceProvider.Handle); | ||
return this; | ||
} | ||
|
||
public AnimationBuilderStats Stats | ||
{ | ||
get | ||
{ | ||
AnimationBuilderStats stats; | ||
SkottieApi.skottie_animation_builder_get_stats (Handle, &stats); | ||
return stats; | ||
} | ||
} | ||
|
||
public Animation? Build (Stream stream) | ||
{ | ||
_ = stream ?? throw new ArgumentNullException (nameof (stream)); | ||
|
||
using var data = SKData.Create (stream); | ||
return Build (data); | ||
} | ||
|
||
public Animation? Build (SKStream stream) | ||
{ | ||
_ = stream ?? throw new ArgumentNullException (nameof (stream)); | ||
|
||
using var data = SKData.Create (stream); | ||
return Build (data); | ||
} | ||
|
||
public Animation? Build (SKData data) | ||
{ | ||
_ = data ?? throw new ArgumentNullException (nameof (data)); | ||
|
||
var preamble = Utils.GetPreambleSize (data); | ||
var span = data.AsSpan ().Slice (preamble); | ||
|
||
fixed (byte* ptr = span) { | ||
return Animation.GetObject (SkottieApi.skottie_animation_builder_make_from_data (Handle, ptr, (IntPtr)span.Length)); | ||
} | ||
} | ||
|
||
public Animation? Build (string path) | ||
{ | ||
_ = path ?? throw new ArgumentNullException (nameof (path)); | ||
|
||
using var data = SKData.Create (path); | ||
return Build (data); | ||
} | ||
|
||
protected override void DisposeNative () | ||
=> SkottieApi.skottie_animation_builder_delete (Handle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
using System; | ||
|
||
namespace SkiaSharp.Skottie | ||
{ | ||
public partial struct AnimationBuilderStats | ||
{ | ||
public readonly TimeSpan TotalLoadTime => | ||
TimeSpan.FromMilliseconds (fTotalLoadTimeMS); | ||
|
||
public readonly TimeSpan JsonParseTime => | ||
TimeSpan.FromMilliseconds (fJsonParseTimeMS); | ||
|
||
public readonly TimeSpan SceneParseTime => | ||
TimeSpan.FromMilliseconds (fSceneParseTimeMS); | ||
|
||
public readonly int JsonSize => (int)fJsonSize; | ||
|
||
public readonly int AnimatorCount => (int)fAnimatorCount; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.IO; | ||
using SkiaSharp.Resources; | ||
using SkiaSharp.SceneGraph; | ||
using SkiaSharp.Skottie; | ||
using Xunit; | ||
|
||
namespace SkiaSharp.Tests | ||
{ | ||
public class AnimationBuilderTest : SKTest | ||
{ | ||
public static TheoryData<string> DefaultLottieFiles => | ||
AnimationTest.DefaultLottieFiles; | ||
|
||
public static TheoryData<string> Base64Files => | ||
new TheoryData<string> | ||
{ | ||
"lottie-base64_dotnet-bot.json", | ||
"lottie-base64_women-thinking.json", | ||
}; | ||
|
||
[SkippableTheory] | ||
[MemberData(nameof(DefaultLottieFiles))] | ||
public void DefaultBuilderIsTheSameAsDefaultCreate(string filename) | ||
{ | ||
var path = Path.Combine(PathToImages, filename); | ||
using var data = SKData.Create(path); | ||
var directAnimation = Animation.Create(data); | ||
|
||
var builderAnimation = Animation.CreateBuilder().Build(data); | ||
Assert.NotNull(builderAnimation); | ||
Assert.NotEqual(IntPtr.Zero, builderAnimation.Handle); | ||
|
||
Assert.Equal(directAnimation.Duration, builderAnimation.Duration); | ||
Assert.Equal(directAnimation.Fps, builderAnimation.Fps); | ||
Assert.Equal(directAnimation.InPoint, builderAnimation.InPoint); | ||
Assert.Equal(directAnimation.OutPoint, builderAnimation.OutPoint); | ||
Assert.Equal(directAnimation.Version, builderAnimation.Version); | ||
Assert.Equal(directAnimation.Size, builderAnimation.Size); | ||
} | ||
|
||
[SkippableTheory] | ||
[MemberData(nameof(DefaultLottieFiles))] | ||
public void DefaultBuilderHasStats(string filename) | ||
{ | ||
var path = Path.Combine(PathToImages, filename); | ||
using var data = SKData.Create(path); | ||
|
||
var builder = Animation.CreateBuilder(); | ||
var animation = builder.Build(data); | ||
Assert.NotNull(animation); | ||
|
||
var stats = builder.Stats; | ||
Assert.True(stats.SceneParseTime > TimeSpan.Zero); | ||
Assert.True(stats.JsonParseTime > TimeSpan.Zero); | ||
Assert.True(stats.TotalLoadTime > TimeSpan.Zero); | ||
Assert.True(stats.JsonSize > 0); | ||
Assert.True(stats.AnimatorCount > 0); | ||
} | ||
|
||
[SkippableTheory] | ||
[MemberData(nameof(Base64Files))] | ||
public void CanLoadBase64Images(string filename) | ||
{ | ||
var path = Path.Combine(PathToImages, filename); | ||
using var data = SKData.Create(path); | ||
|
||
var animation = Animation | ||
.CreateBuilder() | ||
.SetResourceProvider(new DataUriResourceProvider()) | ||
.Build(data); | ||
|
||
Assert.NotNull(animation); | ||
Assert.True(animation.Duration > TimeSpan.Zero); | ||
} | ||
|
||
[SkippableTheory] | ||
[MemberData(nameof(Base64Files))] | ||
public void CanRenderWithBase64(string filename) | ||
{ | ||
var animation = Animation | ||
.CreateBuilder() | ||
.SetResourceProvider(new DataUriResourceProvider()) | ||
.Build(Path.Combine(PathToImages, filename)); | ||
|
||
using var bmp = new SKBitmap((int)animation.Size.Width, (int)animation.Size.Height); | ||
bmp.Erase(SKColors.Red); | ||
var beforePixels = bmp.Pixels; | ||
|
||
using var canvas = new SKCanvas(bmp); | ||
animation.Seek(0.1); | ||
animation.Render(canvas, bmp.Info.Rect); | ||
var afterPixels = bmp.Pixels; | ||
|
||
Assert.NotEqual(beforePixels, afterPixels); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters