Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Show animator counts in the object status table in generated code. #343

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions source/UIDataCodeGen/CodeGen/InstantiatorGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,14 @@ protected IEnumerable<string> GetSourceDescriptionLines()
}

/// <summary>
/// Returns text that describes the graph statistics.
/// Returns text that describes the graph statistics. This graph shows the
/// number of objects instantiated and is designed to help with investigations
/// or performance.
/// </summary>
/// <returns>A list of strings describing the graph statistics.</returns>
IEnumerable<string> GetGraphStatsLines()
{
foreach (var line in GraphStatsMonospaceTableFormatter.GetGraphStatsLines(
_animatedVisualGenerators.Select(avg => (avg.StatsName, avg.Objects))
))
{
yield return line;
}
}
IEnumerable<string> GetGraphStatsLines() =>
GraphStatsMonospaceTableFormatter.GetGraphStatsLines(
_animatedVisualGenerators.Select(avg => (avg.StatsName, avg.Objects)));

/// <summary>
/// Call this to get a list of the asset files referenced by the generated code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Lottie.UIData.CodeGen.Tables
{
sealed class GraphStatsMonospaceTableFormatter : MonospaceTableFormatter
{
/// <summary>
/// Returns text that describes the graph statistics.
/// </summary>
/// <returns>A formatted string for each line in the table.</returns>
internal static IEnumerable<string> GetGraphStatsLines(IEnumerable<(string name, IEnumerable<object> objects)> objects)
{
var objs = objects.ToArray();
Expand All @@ -32,19 +36,25 @@ internal static IEnumerable<string> GetGraphStatsLines(IEnumerable<(string name,
headerColumns[i + 1] = ColumnData.Create(name);
}

// Get the CompositionObjects for each animated visual.
var compositionObjects =
(from x in objs
select (from o in x.objects
where o is CompositionObject
select (CompositionObject)o).ToArray()).ToArray();

var animatorCounts = GetAnimatorCountRecords(compositionObjects);

var rows = new[] {
Row.HeaderTop,
new Row.ColumnData(headerColumns),
Row.HeaderBottom,
GetCompositionObjectCountRecord(compositionObjects, "All CompositionObjects", (o) => true),
Row.Separator,
GetAnimatorCountRecord(compositionObjects),
animatorCounts.expressions,
animatorCounts.keyFrames,
animatorCounts.referenceParameters,
Row.Separator,
GetCompositionObjectCountRecord(compositionObjects, "Animated brushes", (o) => o is CompositionBrush b && b.Animators.Count > 0),
GetCompositionObjectCountRecord(compositionObjects, "Animated gradient stops", (o) => o is CompositionColorGradientStop s && s.Animators.Count > 0),
GetCompositionObjectCountRecord(compositionObjects, "ExpressionAnimations", (o) => o.Type == CompositionObjectType.ExpressionAnimation),
Expand All @@ -62,6 +72,7 @@ where o is CompositionObject
Row.BodyBottom,
};

// Convert the rows into strings.
return GetTableLines(rows);
}

Expand All @@ -82,18 +93,50 @@ static Row GetCompositionObjectCountRecord(
return new Row.ColumnData(result);
}

static Row GetAnimatorCountRecord(CompositionObject[][] objects)
// Returns a row describing the number of animators for each animated visual.
// The parameter is a set of objects for each generator.
static (Row expressions, Row keyFrames, Row referenceParameters)
GetAnimatorCountRecords(CompositionObject[][] objects)
{
var result = new ColumnData[objects.Length + 1];
result[0] = ColumnData.Create("Animators", TextAlignment.Left);
var expressions = new ColumnData[objects.Length + 1];
var keyFrames = new ColumnData[objects.Length + 1];
var referenceParameters = new ColumnData[objects.Length + 1];

expressions[0] = ColumnData.Create("Expression animators", TextAlignment.Left);
keyFrames[0] = ColumnData.Create("KeyFrame animators", TextAlignment.Left);
referenceParameters[0] = ColumnData.Create("Reference parameters", TextAlignment.Left);

for (var i = 0; i < objects.Length; i++)
{
var count = objects[i].Select(o => o.Animators.Count).Sum();
result[i + 1] = ColumnData.Create(count);
(expressions[i + 1], keyFrames[i + 1], referenceParameters[i + 1]) = GetAnimatorCountColumns(objects[i]);
}

return new Row.ColumnData(result);
return (new Row.ColumnData(expressions), new Row.ColumnData(keyFrames), new Row.ColumnData(referenceParameters));
}

/// <summary>
/// Returns columns describing the counts of animators for the given <see cref="CompositionObject"/>s.
/// </summary>
/// <returns>The animator counts columns.</returns>
static (ColumnData expressions, ColumnData keyFrames, ColumnData referenceParameters)
GetAnimatorCountColumns(IEnumerable<CompositionObject> objects)
{
var expressions = 0;
var keyFrames = 0;
var referenceParameters = 0;

foreach (var obj in objects)
{
var animators = obj.Animators;
referenceParameters += animators.Sum(a => a.Animation.ReferenceParameters.Count());
var expressionsCount = animators.Where(a => a.Animation.Type == CompositionObjectType.ExpressionAnimation).Count();
expressions += expressionsCount;

// Key frame animations are the animations that are not expression animations.
keyFrames += animators.Count - expressionsCount;
}

return (ColumnData.Create(expressions), ColumnData.Create(keyFrames), ColumnData.Create(referenceParameters));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Lottie.UIData.CodeGen.Tables
// that is specific to a particular data set.
abstract class MonospaceTableFormatter
{
/// <summary>
/// Converts a list of <see cref="Row"/>s into monospaced strings.
/// </summary>
/// <returns>Text for each <see cref="Row"/>.</returns>
protected static IEnumerable<string> GetTableLines(IEnumerable<Row> rows)
{
// Get the width of each column in each row and find the maximum width
Expand Down
2 changes: 1 addition & 1 deletion source/WinCompData/CompositionAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void SetReferenceParameter(string key, CompositionObject compositionObjec
/// Returns the reference parameters that have been set on this <see cref="CompositionAnimation"/>.
/// The list is returned ordered alphabetically by key.
/// </summary>
public IEnumerable<KeyValuePair<string, CompositionObject>> ReferenceParameters => _referencedParameters;
public IReadOnlyCollection<KeyValuePair<string, CompositionObject>> ReferenceParameters => _referencedParameters;

internal abstract CompositionAnimation Clone();
}
Expand Down