Skip to content

Commit

Permalink
Added more merge optimizer options and moved optimizer call for Lotti…
Browse files Browse the repository at this point in the history
…eViewer (#460)
  • Loading branch information
aborziak-ms authored Aug 18, 2021
1 parent 76d04c3 commit b242a33
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 19 deletions.
6 changes: 6 additions & 0 deletions source/Lottie/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.UI.Lottie.CompMetadata;
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieData;
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieData.Optimization;
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieData.Serialization;
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieToWinComp;
using Windows.Foundation.Metadata;
Expand Down Expand Up @@ -84,6 +85,11 @@ await Task.Run(() =>
LottieCompositionReader.Options.IgnoreMatchNames,
out var readerIssues);

if (lottieComposition is not null)
{
lottieComposition = LottieMergeOptimizer.Optimize(lottieComposition);
}

if (diagnostics is not null)
{
diagnostics.JsonParsingIssues = ToIssues(readerIssues);
Expand Down
106 changes: 89 additions & 17 deletions source/LottieData/Optimization/Experimental/MergeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,82 @@ MergeResult<Path> MergePaths(Path a, TimeRange aRange, Path b, TimeRange bRange)
return new MergeResult<Path>(new Path(args, a.DrawingDirection, geometryData.Value!));
}

MergeResult<Rectangle> MergeRectangles(Rectangle a, TimeRange aRange, Rectangle b, TimeRange bRange)
{
if (a.BlendMode != b.BlendMode || a.DrawingDirection != b.DrawingDirection)
{
return MergeResult<Rectangle>.Failed;
}

var args = new ShapeLayerContentArgs
{
Name = $"{a.Name} {b.Name}",
MatchName = $"{a.MatchName}{b.MatchName}",
BlendMode = a.BlendMode,
};

var position = MergeIAnimatableVector3(a.Position, aRange, b.Position, bRange);
var size = MergeIAnimatableVector3(a.Size, aRange, b.Size, bRange);
var roundness = MergeAnimatable(a.Roundness, aRange, b.Roundness, bRange);

if (!position.Success || !size.Success || !roundness.Success)
{
return MergeResult<Rectangle>.Failed;
}

return new MergeResult<Rectangle>(new Rectangle(args, a.DrawingDirection, position.Value!, size.Value!, roundness.Value!));
}

MergeResult<TrimPath> MergeTrimPaths(TrimPath a, TimeRange aRange, TrimPath b, TimeRange bRange)
{
if (a.BlendMode != b.BlendMode || a.TrimPathType != b.TrimPathType)
{
return MergeResult<TrimPath>.Failed;
}

var args = new ShapeLayerContentArgs
{
Name = $"{a.Name} {b.Name}",
MatchName = $"{a.MatchName}{b.MatchName}",
BlendMode = a.BlendMode,
};

var start = MergeAnimatable(a.Start, aRange, b.Start, bRange);
var end = MergeAnimatable(a.End, aRange, b.End, bRange);
var offset = MergeAnimatable(a.Offset, aRange, b.Offset, bRange);

if (!start.Success || !end.Success || !offset.Success)
{
return MergeResult<TrimPath>.Failed;
}

return new MergeResult<TrimPath>(new TrimPath(args, a.TrimPathType, start.Value!, end.Value!, offset.Value!));
}

MergeResult<RoundCorners> MergeRoundCorners(RoundCorners a, TimeRange aRange, RoundCorners b, TimeRange bRange)
{
if (a.BlendMode != b.BlendMode)
{
return MergeResult<RoundCorners>.Failed;
}

var args = new ShapeLayerContentArgs
{
Name = $"{a.Name} {b.Name}",
MatchName = $"{a.MatchName}{b.MatchName}",
BlendMode = a.BlendMode,
};

var radius = MergeAnimatable(a.Radius, aRange, b.Radius, bRange);

if (!radius.Success)
{
return MergeResult<RoundCorners>.Failed;
}

return new MergeResult<RoundCorners>(new RoundCorners(args, radius.Value!));
}

MergeResult<Ellipse> MergeEllipses(Ellipse a, TimeRange aRange, Ellipse b, TimeRange bRange)
{
if (a.BlendMode != b.BlendMode || a.DrawingDirection != b.DrawingDirection)
Expand Down Expand Up @@ -508,20 +584,26 @@ MergeResult<ShapeLayerContent> MergeShapeLayerContents(ShapeLayerContent a, Time

switch (a.ContentType)
{
case ShapeContentType.Group:
return MergeResult<ShapeLayerContent>.From(MergeShapeGroup((ShapeGroup)a, aRange, (ShapeGroup)b, bRange));
case ShapeContentType.Path:
return MergeResult<ShapeLayerContent>.From(MergePaths((Path)a, aRange, (Path)b, bRange));
case ShapeContentType.Ellipse:
return MergeResult<ShapeLayerContent>.From(MergeEllipses((Ellipse)a, aRange, (Ellipse)b, bRange));
case ShapeContentType.Group:
return MergeResult<ShapeLayerContent>.From(MergeShapeGroup((ShapeGroup)a, aRange, (ShapeGroup)b, bRange));
case ShapeContentType.LinearGradientFill:
return MergeResult<ShapeLayerContent>.From(MergeLinearGradientFills((LinearGradientFill)a, aRange, (LinearGradientFill)b, bRange));
case ShapeContentType.Transform:
return MergeResult<ShapeLayerContent>.From(MergeTransform((Transform)a, aRange, (Transform)b, bRange));
case ShapeContentType.Path:
return MergeResult<ShapeLayerContent>.From(MergePaths((Path)a, aRange, (Path)b, bRange));
case ShapeContentType.Rectangle:
return MergeResult<ShapeLayerContent>.From(MergeRectangles((Rectangle)a, aRange, (Rectangle)b, bRange));
case ShapeContentType.RoundCorners:
return MergeResult<ShapeLayerContent>.From(MergeRoundCorners((RoundCorners)a, aRange, (RoundCorners)b, bRange));
case ShapeContentType.SolidColorFill:
return MergeResult<ShapeLayerContent>.From(MergeSolidColorFills((SolidColorFill)a, aRange, (SolidColorFill)b, bRange));
case ShapeContentType.SolidColorStroke:
return MergeResult<ShapeLayerContent>.From(MergeSolidColorStrokes((SolidColorStroke)a, aRange, (SolidColorStroke)b, bRange));
case ShapeContentType.Transform:
return MergeResult<ShapeLayerContent>.From(MergeTransform((Transform)a, aRange, (Transform)b, bRange));
case ShapeContentType.TrimPath:
return MergeResult<ShapeLayerContent>.From(MergeTrimPaths((TrimPath)a, aRange, (TrimPath)b, bRange));
}

return MergeResult<ShapeLayerContent>.Failed;
Expand Down Expand Up @@ -678,16 +760,6 @@ MergeResult<Asset> MergeAssets(Asset a, TimeRange aParentRange, Asset b, TimeRan

public MergeResult<LayerGroup> MergeLayerGroups(LayerGroup a, LayerGroup b)
{
if (a.MainLayer is PreCompLayer || b.MainLayer is PreCompLayer)
{
return MergeResult<LayerGroup>.Failed;
}

if (a.MatteLayer is PreCompLayer || b.MatteLayer is PreCompLayer)
{
return MergeResult<LayerGroup>.Failed;
}

if (!a.CanBeMerged || !b.CanBeMerged)
{
return MergeResult<LayerGroup>.Failed;
Expand Down Expand Up @@ -836,7 +908,7 @@ MergeResult<PreCompLayer> MergePreCompLayers(PreCompLayer a, PreCompLayer b)

var bAsset = GetAssetById(b.RefId);

if (aAsset is not LayerCollectionAsset || bAsset is not LayerCollectionAsset || aAsset == bAsset)
if (aAsset is not LayerCollectionAsset || bAsset is not LayerCollectionAsset)
{
return MergeResult<PreCompLayer>.Failed;
}
Expand Down
2 changes: 0 additions & 2 deletions source/LottieReader/Serialization/LottieCompositionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ LottieComposition ParseLottieComposition(ref Reader reader)
layers: new LayerCollection(layers),
markers: markers);

result = LottieMergeOptimizer.Optimize(result);

return result;
}

Expand Down

0 comments on commit b242a33

Please sign in to comment.