Skip to content

Commit 64bdd18

Browse files
authored
Added lottie merge optimization (#453)
* Added lottie merge optimization
1 parent 1b4e3d2 commit 64bdd18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1804
-5
lines changed

source/Animatables/Animatable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Animatable(T initialValue, IReadOnlyList<KeyFrame<T>> keyFrames)
102102
public bool IsEverNot(T value) => !IsAlways(value);
103103

104104
public Animatable<T> WithTimeOffset(double timeOffset)
105-
=> timeOffset != 0 || IsAnimated
105+
=> timeOffset != 0 && IsAnimated
106106
? new Animatable<T>(KeyFrames.Select(kf => kf.WithTimeOffset(timeOffset)))
107107
: this;
108108

source/Animatables/AnimatableVector2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public AnimatableVector2 WithOffset(Vector2 offset)
3030
=> Select(vector2 => vector2 + offset);
3131

3232
public new AnimatableVector2 WithTimeOffset(double timeOffset)
33-
=> timeOffset != 0 || IsAnimated
33+
=> timeOffset != 0 && IsAnimated
3434
? new AnimatableVector2(KeyFrames.Select(kf => kf.WithTimeOffset(timeOffset)))
3535
: this;
3636

source/Animatables/AnimatableVector3.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public AnimatableVector3 WithOffset(Vector2 offset)
3030
=> Select(vector3 => vector3 + new Vector3(offset.X, offset.Y, 0));
3131

3232
public new AnimatableVector3 WithTimeOffset(double timeOffset)
33-
=> timeOffset != 0 || IsAnimated
33+
=> timeOffset != 0 && IsAnimated
3434
? new AnimatableVector3(KeyFrames.Select(kf => kf.WithTimeOffset(timeOffset)))
3535
: this;
3636

source/LottieData/Ellipse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public Ellipse(
3030
public override ShapeContentType ContentType => ShapeContentType.Ellipse;
3131

3232
public override ShapeType ShapeType => ShapeType.Ellipse;
33+
34+
public override ShapeLayerContent WithTimeOffset(double offset)
35+
{
36+
return new Ellipse(CopyArgs(), DrawingDirection, Position.WithTimeOffset(offset), Diameter.WithTimeOffset(offset));
37+
}
3338
}
3439
}

source/LottieData/ImageLayer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@ public ImageLayer(
2424

2525
/// <inheritdoc/>
2626
public override LayerType Type => LayerType.Image;
27+
28+
public override Layer WithIndicesChanged(int index, int? parentIndex)
29+
{
30+
return new ImageLayer(GetArgsWithIndicesChanged(index, parentIndex), RefId);
31+
}
32+
33+
public override Layer WithTimeOffset(double offset)
34+
{
35+
return new ImageLayer(GetArgsWithTimeOffset(offset), RefId);
36+
}
2737
}
2838
}

source/LottieData/Layer.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private protected Layer(in LayerArgs args)
6262
/// <summary>
6363
/// Gets the list of masks appplied to the layer.
6464
/// </summary>
65-
public IReadOnlyList<Mask> Masks { get; set; }
65+
public IReadOnlyList<Mask> Masks { get; }
6666

6767
/// <summary>
6868
/// Gets the frame at which this <see cref="Layer"/> becomes invisible. <see cref="OutPoint"/>.
@@ -123,6 +123,46 @@ public ref struct LayerArgs
123123
public MatteType LayerMatteType { get; set; }
124124
}
125125

126+
public LayerArgs CopyArgs()
127+
{
128+
return new LayerArgs
129+
{
130+
Name = Name,
131+
Index = Index,
132+
Parent = Parent,
133+
IsHidden = IsHidden,
134+
Transform = Transform,
135+
TimeStretch = TimeStretch,
136+
StartFrame = InPoint,
137+
InFrame = InPoint,
138+
OutFrame = OutPoint,
139+
BlendMode = BlendMode,
140+
Is3d = Is3d,
141+
AutoOrient = AutoOrient,
142+
LayerMatteType = LayerMatteType,
143+
Effects = Effects,
144+
Masks = Masks,
145+
};
146+
}
147+
148+
protected LayerArgs GetArgsWithIndicesChanged(int index, int? parentIndex)
149+
{
150+
var args = CopyArgs();
151+
args.Index = index;
152+
args.Parent = parentIndex;
153+
return args;
154+
}
155+
156+
protected LayerArgs GetArgsWithTimeOffset(double shiftValue)
157+
{
158+
var args = CopyArgs();
159+
args.Transform = (Transform)args.Transform.WithTimeOffset(shiftValue);
160+
args.StartFrame += shiftValue;
161+
args.InFrame += shiftValue;
162+
args.OutFrame += shiftValue;
163+
return args;
164+
}
165+
126166
public enum LayerType
127167
{
128168
PreComp,
@@ -139,5 +179,20 @@ public enum MatteType
139179
Add,
140180
Invert,
141181
}
182+
183+
/// <summary>
184+
/// Make a copy of the layer and change its index and parent index.
185+
/// </summary>
186+
/// <param name="index">Index to be set.</param>
187+
/// <param name="parentIndex">Parent index to be set.</param>
188+
/// <returns>Layer copy with changed indices.</returns>
189+
public abstract Layer WithIndicesChanged(int index, int? parentIndex);
190+
191+
/// <summary>
192+
/// Make a copy of the layer and offset all frames by some value.
193+
/// </summary>
194+
/// <param name="offset">Offset value.</param>
195+
/// <returns>Layer copy with offsetted frames.</returns>
196+
public abstract Layer WithTimeOffset(double offset);
142197
}
143198
}

source/LottieData/LayerCollection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@ public LayerCollection(IEnumerable<Layer> layers)
9797
// Not found.
9898
return null;
9999
}
100+
101+
public LayerCollection WithTimeOffset(double offset)
102+
{
103+
return new LayerCollection(_layers.Select(layer => layer.WithTimeOffset(offset)));
104+
}
100105
}
101106
}

source/LottieData/LinearGradientFill.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,16 @@ public LinearGradientFill(
3636

3737
/// <inheritdoc/>
3838
public override ShapeContentType ContentType => ShapeContentType.LinearGradientFill;
39+
40+
public override ShapeLayerContent WithTimeOffset(double offset)
41+
{
42+
return new LinearGradientFill(
43+
CopyArgs(),
44+
FillType,
45+
Opacity.WithTimeOffset(offset),
46+
StartPoint.WithTimeOffset(offset),
47+
EndPoint.WithTimeOffset(offset),
48+
GradientStops.WithTimeOffset(offset));
49+
}
3950
}
4051
}

source/LottieData/LinearGradientStroke.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,20 @@ public LinearGradientStroke(
3838
public override ShapeContentType ContentType => ShapeContentType.LinearGradientStroke;
3939

4040
public override ShapeStrokeKind StrokeKind => ShapeStrokeKind.LinearGradient;
41+
42+
public override ShapeLayerContent WithTimeOffset(double offset)
43+
{
44+
return new LinearGradientStroke(
45+
CopyArgs(),
46+
Opacity.WithTimeOffset(offset),
47+
StrokeWidth.WithTimeOffset(offset),
48+
CapType,
49+
JoinType,
50+
MiterLimit,
51+
StartPoint.WithTimeOffset(offset),
52+
EndPoint.WithTimeOffset(offset),
53+
GradientStops.WithTimeOffset(offset)
54+
);
55+
}
4156
}
4257
}

source/LottieData/LottieData.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@
3535
<Compile Include="$(MSBuildThisFileDirectory)Mask.cs" />
3636
<Compile Include="$(MSBuildThisFileDirectory)MergePaths.cs" />
3737
<Compile Include="$(MSBuildThisFileDirectory)NullLayer.cs" />
38+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\LayerGroup.cs" />
39+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\LayersGraph.cs" />
40+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\MergeHelper.cs" />
3841
<Compile Include="$(MSBuildThisFileDirectory)Optimization\GradientStopOptimizer.cs" />
42+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\LayersIndexMapper.cs" />
43+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\LottieMergeOptimizer.cs" />
3944
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Optimizer.cs" />
45+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\TimeRange.cs" />
46+
<Compile Include="$(MSBuildThisFileDirectory)Optimization\Experimental\MergeResult.cs" />
4047
<Compile Include="$(MSBuildThisFileDirectory)Path.cs" />
4148
<Compile Include="$(MSBuildThisFileDirectory)Polystar.cs" />
4249
<Compile Include="$(MSBuildThisFileDirectory)PreCompLayer.cs" />

0 commit comments

Comments
 (0)