2019-03-16 04:47:11 +00:00
|
|
|
// 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;
|
2022-08-15 07:55:55 +00:00
|
|
|
using System.Diagnostics;
|
2019-03-16 04:47:11 +00:00
|
|
|
using osu.Framework.Allocation;
|
2022-09-07 08:38:00 +00:00
|
|
|
using osu.Framework.Audio;
|
2020-10-27 05:10:12 +00:00
|
|
|
using osu.Framework.Bindables;
|
2019-03-16 04:47:11 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Input.Handlers;
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A container which consumes a parent gameplay clock and standardises frame counts for children.
|
2020-03-26 03:50:00 +00:00
|
|
|
/// Will ensure a minimum of 50 frames per clock second is maintained, regardless of any system lag or seeks.
|
2019-03-16 04:47:11 +00:00
|
|
|
/// </summary>
|
2022-08-15 09:53:10 +00:00
|
|
|
[Cached(typeof(IGameplayClock))]
|
|
|
|
[Cached(typeof(IFrameStableClock))]
|
2022-09-08 09:23:54 +00:00
|
|
|
public sealed partial class FrameStabilityContainer : Container, IHasReplayHandler, IFrameStableClock
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2022-08-15 10:28:12 +00:00
|
|
|
public ReplayInputHandler? ReplayInputHandler { get; set; }
|
2019-05-09 07:36:47 +00:00
|
|
|
|
2019-05-09 07:37:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The number of frames (per parent frame) which can be run in an attempt to catch-up to real-time.
|
|
|
|
/// </summary>
|
|
|
|
public int MaxCatchUpFrames { get; set; } = 5;
|
2019-05-10 09:48:39 +00:00
|
|
|
|
2019-08-15 09:25:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to enable frame-stable playback.
|
|
|
|
/// </summary>
|
2022-08-15 10:28:12 +00:00
|
|
|
internal bool FrameStablePlayback { get; set; } = true;
|
2019-08-15 09:25:31 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
protected override bool RequiresChildrenUpdate => base.RequiresChildrenUpdate && state != PlaybackState.NotValid;
|
2022-08-15 09:53:10 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
private readonly Bindable<bool> isCatchingUp = new Bindable<bool>();
|
2020-10-27 05:10:12 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
private readonly Bindable<bool> waitingOnFrames = new Bindable<bool>();
|
2019-05-09 07:37:34 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
private readonly double gameplayStartTime;
|
2019-05-09 09:06:11 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
private IGameplayClock? parentGameplayClock;
|
2019-05-09 07:36:47 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A clock which is used as reference for time, rate and running state.
|
|
|
|
/// </summary>
|
|
|
|
private IClock referenceClock = null!;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A local manual clock which tracks the reference clock.
|
|
|
|
/// Values are transferred from <see cref="referenceClock"/> each update call.
|
|
|
|
/// </summary>
|
2019-03-16 04:47:11 +00:00
|
|
|
private readonly ManualClock manualClock;
|
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The main framed clock which has stability applied to it.
|
|
|
|
/// This gets exposed to children as an <see cref="IGameplayClock"/>.
|
|
|
|
/// </summary>
|
2019-03-16 04:47:11 +00:00
|
|
|
private readonly FramedClock framedClock;
|
|
|
|
|
2019-10-03 07:00:47 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The current direction of playback to be exposed to frame stable children.
|
|
|
|
/// </summary>
|
2021-10-11 19:39:48 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// Initially it is presumed that playback will proceed in the forward direction.
|
|
|
|
/// </remarks>
|
|
|
|
private int direction = 1;
|
2019-10-03 07:00:47 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
private PlaybackState state;
|
|
|
|
|
|
|
|
private bool hasReplayAttached => ReplayInputHandler != null;
|
|
|
|
|
|
|
|
private bool firstConsumption = true;
|
|
|
|
|
|
|
|
public FrameStabilityContainer(double gameplayStartTime = double.MinValue)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
framedClock = new FramedClock(manualClock = new ManualClock());
|
|
|
|
|
|
|
|
this.gameplayStartTime = gameplayStartTime;
|
|
|
|
}
|
|
|
|
|
2022-08-22 06:57:24 +00:00
|
|
|
[BackgroundDependencyLoader(true)]
|
2022-08-15 10:17:41 +00:00
|
|
|
private void load(IGameplayClock? gameplayClock)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2022-08-15 10:17:41 +00:00
|
|
|
if (gameplayClock != null)
|
2019-03-25 11:25:47 +00:00
|
|
|
{
|
2022-08-15 10:17:41 +00:00
|
|
|
parentGameplayClock = gameplayClock;
|
2022-08-15 09:53:10 +00:00
|
|
|
IsPaused.BindTo(parentGameplayClock.IsPaused);
|
2019-03-25 11:25:47 +00:00
|
|
|
}
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2022-08-15 10:17:41 +00:00
|
|
|
referenceClock = gameplayClock ?? Clock;
|
2022-08-15 10:01:28 +00:00
|
|
|
Clock = this;
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override bool UpdateSubTree()
|
|
|
|
{
|
2020-10-28 06:11:53 +00:00
|
|
|
int loops = MaxCatchUpFrames;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2020-10-30 06:25:53 +00:00
|
|
|
do
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2020-10-29 09:11:50 +00:00
|
|
|
// update clock is always trying to approach the aim time.
|
|
|
|
// it should be provided as the original value each loop.
|
2020-10-30 11:20:34 +00:00
|
|
|
updateClock();
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2020-10-28 06:11:53 +00:00
|
|
|
if (state == PlaybackState.NotValid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
base.UpdateSubTree();
|
|
|
|
UpdateSubTreeMasking(this, ScreenSpaceDrawQuad.AABBFloat);
|
2020-10-30 06:25:53 +00:00
|
|
|
} while (state == PlaybackState.RequiresCatchUp && loops-- > 0);
|
2019-03-16 04:47:11 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:20:34 +00:00
|
|
|
private void updateClock()
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2022-08-15 10:28:12 +00:00
|
|
|
if (waitingOnFrames.Value)
|
2020-10-30 11:37:07 +00:00
|
|
|
{
|
|
|
|
// if waiting on frames, run one update loop to determine if frames have arrived.
|
|
|
|
state = PlaybackState.Valid;
|
|
|
|
}
|
2022-08-15 09:53:10 +00:00
|
|
|
else if (IsPaused.Value)
|
2020-10-30 11:37:07 +00:00
|
|
|
{
|
|
|
|
// time should not advance while paused, nor should anything run.
|
|
|
|
state = PlaybackState.NotValid;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = PlaybackState.Valid;
|
|
|
|
}
|
|
|
|
|
2022-08-15 10:17:41 +00:00
|
|
|
double proposedTime = referenceClock.CurrentTime;
|
2020-10-30 11:20:34 +00:00
|
|
|
|
2020-10-28 06:12:39 +00:00
|
|
|
if (FrameStablePlayback)
|
|
|
|
// if we require frame stability, the proposed time will be adjusted to move at most one known
|
|
|
|
// frame interval in the current direction.
|
|
|
|
applyFrameStability(ref proposedTime);
|
2019-04-24 06:23:00 +00:00
|
|
|
|
2020-10-28 06:12:39 +00:00
|
|
|
if (hasReplayAttached)
|
2020-10-28 06:32:20 +00:00
|
|
|
{
|
|
|
|
bool valid = updateReplay(ref proposedTime);
|
|
|
|
|
|
|
|
if (!valid)
|
|
|
|
state = PlaybackState.NotValid;
|
|
|
|
}
|
2019-04-24 06:23:00 +00:00
|
|
|
|
2021-10-11 19:39:48 +00:00
|
|
|
// if the proposed time is the same as the current time, assume that the clock will continue progressing in the same direction as previously.
|
|
|
|
// this avoids spurious flips in direction from -1 to 1 during rewinds.
|
|
|
|
if (state == PlaybackState.Valid && proposedTime != manualClock.CurrentTime)
|
2020-10-28 06:12:39 +00:00
|
|
|
direction = proposedTime >= manualClock.CurrentTime ? 1 : -1;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2022-08-15 11:16:14 +00:00
|
|
|
double timeBehind = Math.Abs(proposedTime - referenceClock.CurrentTime);
|
2020-10-30 06:26:21 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
isCatchingUp.Value = timeBehind > 200;
|
|
|
|
waitingOnFrames.Value = state == PlaybackState.NotValid;
|
2020-10-30 06:26:21 +00:00
|
|
|
|
2020-10-28 06:12:39 +00:00
|
|
|
manualClock.CurrentTime = proposedTime;
|
2022-08-15 10:17:41 +00:00
|
|
|
manualClock.Rate = Math.Abs(referenceClock.Rate) * direction;
|
|
|
|
manualClock.IsRunning = referenceClock.IsRunning;
|
2019-03-16 04:47:11 +00:00
|
|
|
|
2020-10-28 06:12:39 +00:00
|
|
|
// determine whether catch-up is required.
|
2020-10-28 06:32:20 +00:00
|
|
|
if (state == PlaybackState.Valid && timeBehind > 0)
|
|
|
|
state = PlaybackState.RequiresCatchUp;
|
2020-10-27 05:10:12 +00:00
|
|
|
|
2020-10-28 06:12:39 +00:00
|
|
|
// The manual clock time has changed in the above code. The framed clock now needs to be updated
|
|
|
|
// to ensure that the its time is valid for our children before input is processed
|
|
|
|
framedClock.ProcessFrame();
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 05:53:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Attempt to advance replay playback for a given time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposedTime">The time which is to be displayed.</param>
|
2020-10-28 06:32:20 +00:00
|
|
|
/// <returns>Whether playback is still valid.</returns>
|
|
|
|
private bool updateReplay(ref double proposedTime)
|
2020-10-28 05:53:31 +00:00
|
|
|
{
|
2022-08-15 09:53:10 +00:00
|
|
|
Debug.Assert(ReplayInputHandler != null);
|
|
|
|
|
2020-10-28 05:53:31 +00:00
|
|
|
double? newTime;
|
|
|
|
|
|
|
|
if (FrameStablePlayback)
|
|
|
|
{
|
|
|
|
// when stability is turned on, we shouldn't execute for time values the replay is unable to satisfy.
|
2020-10-28 06:11:53 +00:00
|
|
|
newTime = ReplayInputHandler.SetFrameFromTime(proposedTime);
|
2020-10-28 05:53:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// when stability is disabled, we don't really care about accuracy.
|
|
|
|
// looping over the replay will allow it to catch up and feed out the required values
|
|
|
|
// for the current time.
|
|
|
|
while ((newTime = ReplayInputHandler.SetFrameFromTime(proposedTime)) != proposedTime)
|
|
|
|
{
|
|
|
|
if (newTime == null)
|
2019-03-16 04:47:11 +00:00
|
|
|
{
|
2020-10-28 05:53:31 +00:00
|
|
|
// special case for when the replay actually can't arrive at the required time.
|
|
|
|
// protects from potential endless loop.
|
2020-10-28 06:11:53 +00:00
|
|
|
break;
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 07:00:47 +00:00
|
|
|
|
2020-10-28 06:11:53 +00:00
|
|
|
if (newTime == null)
|
2020-10-28 06:32:20 +00:00
|
|
|
return false;
|
2019-10-04 05:42:06 +00:00
|
|
|
|
2020-10-28 05:53:31 +00:00
|
|
|
proposedTime = newTime.Value;
|
2020-10-28 06:32:20 +00:00
|
|
|
return true;
|
2020-10-28 05:53:31 +00:00
|
|
|
}
|
2020-10-27 08:14:41 +00:00
|
|
|
|
2020-10-28 05:42:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Apply frame stability modifier to a time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposedTime">The time which is to be displayed.</param>
|
|
|
|
private void applyFrameStability(ref double proposedTime)
|
|
|
|
{
|
2022-08-15 10:28:12 +00:00
|
|
|
const double sixty_frame_time = 1000.0 / 60;
|
|
|
|
|
2020-10-28 05:42:23 +00:00
|
|
|
if (firstConsumption)
|
|
|
|
{
|
|
|
|
// On the first update, frame-stability seeking would result in unexpected/unwanted behaviour.
|
|
|
|
// Instead we perform an initial seek to the proposed time.
|
2020-10-27 05:10:12 +00:00
|
|
|
|
2020-10-28 05:42:23 +00:00
|
|
|
// process frame (in addition to finally clause) to clear out ElapsedTime
|
|
|
|
manualClock.CurrentTime = proposedTime;
|
2019-03-16 04:47:11 +00:00
|
|
|
framedClock.ProcessFrame();
|
2020-10-28 05:42:23 +00:00
|
|
|
|
|
|
|
firstConsumption = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manualClock.CurrentTime < gameplayStartTime)
|
|
|
|
manualClock.CurrentTime = proposedTime = Math.Min(gameplayStartTime, proposedTime);
|
|
|
|
else if (Math.Abs(manualClock.CurrentTime - proposedTime) > sixty_frame_time * 1.2f)
|
|
|
|
{
|
|
|
|
proposedTime = proposedTime > manualClock.CurrentTime
|
|
|
|
? Math.Min(proposedTime, manualClock.CurrentTime + sixty_frame_time)
|
|
|
|
: Math.Max(proposedTime, manualClock.CurrentTime - sixty_frame_time);
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
#region Delegation of IGameplayClock
|
2022-08-15 09:53:10 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
public IBindable<bool> IsPaused { get; } = new BindableBool();
|
2022-08-15 09:53:10 +00:00
|
|
|
|
|
|
|
public double CurrentTime => framedClock.CurrentTime;
|
|
|
|
|
|
|
|
public double Rate => framedClock.Rate;
|
|
|
|
|
|
|
|
public bool IsRunning => framedClock.IsRunning;
|
|
|
|
|
2022-08-15 10:17:41 +00:00
|
|
|
public void ProcessFrame() { }
|
2022-08-15 09:53:10 +00:00
|
|
|
|
|
|
|
public double ElapsedFrameTime => framedClock.ElapsedFrameTime;
|
|
|
|
|
|
|
|
public double FramesPerSecond => framedClock.FramesPerSecond;
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => framedClock.TimeInfo;
|
|
|
|
|
2022-08-18 09:10:20 +00:00
|
|
|
public double StartTime => parentGameplayClock?.StartTime ?? 0;
|
2022-08-15 09:53:10 +00:00
|
|
|
|
2022-09-07 08:38:00 +00:00
|
|
|
private readonly AudioAdjustments gameplayAdjustments = new AudioAdjustments();
|
|
|
|
|
2022-09-08 08:14:06 +00:00
|
|
|
public IAdjustableAudioComponent AdjustmentsFromMods => parentGameplayClock?.AdjustmentsFromMods ?? gameplayAdjustments;
|
2022-08-15 09:53:10 +00:00
|
|
|
|
|
|
|
#endregion
|
2020-10-27 05:10:12 +00:00
|
|
|
|
2022-08-15 10:28:12 +00:00
|
|
|
#region Delegation of IFrameStableClock
|
|
|
|
|
|
|
|
IBindable<bool> IFrameStableClock.IsCatchingUp => isCatchingUp;
|
|
|
|
IBindable<bool> IFrameStableClock.WaitingOnFrames => waitingOnFrames;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2020-10-28 06:11:53 +00:00
|
|
|
private enum PlaybackState
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Playback is not possible. Child hierarchy should not be processed.
|
|
|
|
/// </summary>
|
|
|
|
NotValid,
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-29 04:16:27 +00:00
|
|
|
/// Playback is running behind real-time. Catch-up will be attempted by processing more than once per
|
|
|
|
/// game loop (limited to a sane maximum to avoid frame drops).
|
2020-10-28 06:11:53 +00:00
|
|
|
/// </summary>
|
|
|
|
RequiresCatchUp,
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-29 04:16:27 +00:00
|
|
|
/// In a valid state, progressing one child hierarchy loop per game loop.
|
2020-10-28 06:11:53 +00:00
|
|
|
/// </summary>
|
|
|
|
Valid
|
|
|
|
}
|
2019-03-16 04:47:11 +00:00
|
|
|
}
|
|
|
|
}
|