1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Refactor storyboard timeline to reduce GC / fix crashes

This commit is contained in:
smoogipoo 2019-12-18 23:59:48 +09:00
parent 1c9b27a9c9
commit 8d6987c870
2 changed files with 69 additions and 25 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Caching;
using osu.Framework.Graphics;
using System;
using System.Collections.Generic;
@ -12,27 +11,35 @@ namespace osu.Game.Storyboards
public class CommandTimeline<T> : ICommandTimeline
{
private readonly List<TypedCommand> commands = new List<TypedCommand>();
public IEnumerable<TypedCommand> Commands => commands.OrderBy(c => c.StartTime);
public bool HasCommands => commands.Count > 0;
private readonly Cached<double> startTimeBacking = new Cached<double>();
public double StartTime => startTimeBacking.IsValid ? startTimeBacking : startTimeBacking.Value = HasCommands ? commands.Min(c => c.StartTime) : double.MinValue;
public double StartTime { get; private set; } = double.MaxValue;
public double EndTime { get; private set; } = double.MinValue;
private readonly Cached<double> endTimeBacking = new Cached<double>();
public double EndTime => endTimeBacking.IsValid ? endTimeBacking : endTimeBacking.Value = HasCommands ? commands.Max(c => c.EndTime) : double.MaxValue;
public T StartValue => HasCommands ? commands.OrderBy(c => c.StartTime).First().StartValue : default;
public T EndValue => HasCommands ? commands.OrderByDescending(c => c.EndTime).First().EndValue : default;
public T StartValue { get; private set; }
public T EndValue { get; private set; }
public void Add(Easing easing, double startTime, double endTime, T startValue, T endValue)
{
if (endTime < startTime)
return;
commands.Add(new TypedCommand { Easing = easing, StartTime = startTime, EndTime = endTime, StartValue = startValue, EndValue = endValue, });
commands.Add(new TypedCommand { Easing = easing, StartTime = startTime, EndTime = endTime, StartValue = startValue, EndValue = endValue });
startTimeBacking.Invalidate();
endTimeBacking.Invalidate();
if (startTime < StartTime)
{
StartValue = startValue;
StartTime = startTime;
}
if (endTime > EndTime)
{
EndValue = endValue;
EndTime = endTime;
}
}
public override string ToString()

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics;
@ -25,28 +26,52 @@ namespace osu.Game.Storyboards
public CommandTimeline<bool> FlipH = new CommandTimeline<bool>();
public CommandTimeline<bool> FlipV = new CommandTimeline<bool>();
private readonly ICommandTimeline[] timelines;
public CommandTimelineGroup()
{
timelines = new ICommandTimeline[]
{
X,
Y,
Scale,
VectorScale,
Rotation,
Colour,
Alpha,
BlendingParameters,
FlipH,
FlipV
};
}
[JsonIgnore]
public IEnumerable<ICommandTimeline> Timelines
public double CommandsStartTime
{
get
{
yield return X;
yield return Y;
yield return Scale;
yield return Rotation;
yield return Colour;
yield return Alpha;
yield return BlendingParameters;
yield return FlipH;
yield return FlipV;
double min = double.MaxValue;
for (int i = 0; i < timelines.Length; i++)
min = Math.Min(min, timelines[i].StartTime);
return min;
}
}
[JsonIgnore]
public double CommandsStartTime => Timelines.Where(t => t.HasCommands).Min(t => t.StartTime);
public double CommandsEndTime
{
get
{
double max = double.MinValue;
[JsonIgnore]
public double CommandsEndTime => Timelines.Where(t => t.HasCommands).Max(t => t.EndTime);
for (int i = 0; i < timelines.Length; i++)
max = Math.Max(max, timelines[i].EndTime);
return max;
}
}
[JsonIgnore]
public double CommandsDuration => CommandsEndTime - CommandsStartTime;
@ -61,7 +86,19 @@ namespace osu.Game.Storyboards
public double Duration => EndTime - StartTime;
[JsonIgnore]
public bool HasCommands => Timelines.Any(t => t.HasCommands);
public bool HasCommands
{
get
{
for (int i = 0; i < timelines.Length; i++)
{
if (timelines[i].HasCommands)
return true;
}
return false;
}
}
public virtual IEnumerable<CommandTimeline<T>.TypedCommand> GetCommands<T>(CommandTimelineSelector<T> timelineSelector, double offset = 0)
{