Skip to content

Commit

Permalink
Use a shared buffer to initialize AnimatedView and LottieView
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Jun 20, 2020
1 parent dda6d81 commit 5e5c7b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
19 changes: 16 additions & 3 deletions Unigram/Unigram/Controls/AnimationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Unigram.Common;
using Unigram.Native;
using Windows.Foundation;
using Windows.Graphics.DirectX;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -91,10 +92,13 @@ private void OnUnloaded(object sender, RoutedEventArgs e)

Dispose();

//_animation?.Dispose();
_device = null;

_animation?.Dispose();
_animation = null;

//_bitmap?.Dispose();
_bitmap?.Dispose();
_bitmap = null;
}

Expand All @@ -115,6 +119,9 @@ private void OnInvalidate(object sender, EventArgs e)
_canvas?.Invalidate();
}

private static object _reusableLock = new object();
private static byte[] _reusableBuffer;

private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(Task.Run(() =>
Expand All @@ -127,9 +134,15 @@ private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventA

_animation = animation;

var colors = new byte[_animation.PixelWidth * _animation.PixelHeight * 4];
lock (_reusableLock)
{
if (_reusableBuffer == null || _reusableBuffer.Length < _animation.PixelWidth * _animation.PixelHeight * 4)
{
_reusableBuffer = new byte[_animation.PixelWidth * _animation.PixelHeight * 4];
}
}

_bitmap = CanvasBitmap.CreateFromBytes(sender, colors, _animation.PixelWidth, _animation.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.R8G8B8A8UIntNormalized);
_bitmap = CanvasBitmap.CreateFromBytes(sender, _reusableBuffer, _animation.PixelWidth, _animation.PixelHeight, DirectXPixelFormat.R8G8B8A8UIntNormalized);
_device = sender;

}).AsAsyncAction());
Expand Down
19 changes: 12 additions & 7 deletions Unigram/Unigram/Controls/LottieView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,21 @@ private void OnInvalidate(object sender, EventArgs e)
_canvas?.Invalidate();
}

private static object _reusableLock = new object();
private static byte[] _reusableBuffer;

private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
lock (_reusableLock)
{
if (_reusableBuffer == null)
{
_reusableBuffer = new byte[256 * 256 * 4];
}
}

_device = sender;
_bitmap = CanvasBitmap.CreateFromBytes(sender, new byte[256 * 256 * 4], 256, 256, DirectXPixelFormat.B8G8R8A8UIntNormalized);
_bitmap = CanvasBitmap.CreateFromBytes(sender, _reusableBuffer, 256, 256, DirectXPixelFormat.B8G8R8A8UIntNormalized);

if (args.Reason == CanvasCreateResourcesReason.FirstTime)
{
Expand Down Expand Up @@ -324,12 +335,6 @@ private void OnSourceChanged(string newValue, string oldValue)

_index = _isCachingEnabled ? 0 : _animationTotalFrame - 1;

var update = TimeSpan.FromSeconds(_animation.Duration / _animation.TotalFrame);
if (_limitFps && _animation.FrameRate >= 60)
{
update = TimeSpan.FromSeconds(update.TotalSeconds * 2);
}

//canvas.Paused = true;
//canvas.ResetElapsedTime();
//canvas.TargetElapsedTime = update > TimeSpan.Zero ? update : TimeSpan.MaxValue;
Expand Down

0 comments on commit 5e5c7b1

Please sign in to comment.