2022-07-20 01:53:29 +08: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 osu.Framework.Allocation;
|
2022-07-20 19:49:57 +08:00
|
|
|
using osu.Framework.Bindables;
|
2022-07-20 01:53:29 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-07-20 19:28:58 +08:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2022-07-20 01:53:29 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2022-07-20 19:29:18 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input.Events;
|
2022-07-20 01:53:29 +08:00
|
|
|
using osu.Framework.Localisation;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Threading;
|
2022-07-20 19:28:58 +08:00
|
|
|
using osu.Framework.Utils;
|
2022-07-20 19:49:57 +08:00
|
|
|
using osu.Game.Configuration;
|
2022-07-20 01:53:29 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
2022-07-20 20:05:20 +08:00
|
|
|
public class FPSCounter : VisibilityContainer, IHasCustomTooltip
|
2022-07-20 01:53:29 +08:00
|
|
|
{
|
2022-07-21 11:50:39 +08:00
|
|
|
private RollingCounter<double> counterUpdateFrameTime = null!;
|
|
|
|
private RollingCounter<double> counterDrawFPS = null!;
|
2022-07-20 01:53:29 +08:00
|
|
|
|
|
|
|
private Container mainContent = null!;
|
|
|
|
|
2022-07-20 19:29:18 +08:00
|
|
|
private Container background = null!;
|
|
|
|
|
2022-07-21 17:56:39 +08:00
|
|
|
private Container counters = null!;
|
|
|
|
|
2022-07-20 19:29:18 +08:00
|
|
|
private const float idle_background_alpha = 0.4f;
|
|
|
|
|
2022-07-20 20:05:20 +08:00
|
|
|
private readonly BindableBool showFpsDisplay = new BindableBool(true);
|
2022-07-20 19:49:57 +08:00
|
|
|
|
2022-07-20 19:28:58 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; } = null!;
|
|
|
|
|
2022-07-20 01:53:29 +08:00
|
|
|
public FPSCounter()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-07-20 19:49:57 +08:00
|
|
|
private void load(OsuConfigManager config)
|
2022-07-20 01:53:29 +08:00
|
|
|
{
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
mainContent = new Container
|
|
|
|
{
|
|
|
|
Alpha = 0,
|
2022-07-21 17:56:39 +08:00
|
|
|
Height = 26,
|
2022-07-20 01:53:29 +08:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2022-07-20 19:29:18 +08:00
|
|
|
background = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
CornerRadius = 5,
|
2022-07-20 22:59:09 +08:00
|
|
|
CornerExponent = 5f,
|
2022-07-20 19:29:18 +08:00
|
|
|
Masking = true,
|
|
|
|
Alpha = idle_background_alpha,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
Colour = colours.Gray0,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 17:56:39 +08:00
|
|
|
counters = new Container
|
2022-07-20 01:53:29 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2022-07-21 17:56:39 +08:00
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
counterUpdateFrameTime = new FrameTimeCounter
|
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Margin = new MarginPadding(1),
|
|
|
|
Y = -2,
|
|
|
|
},
|
|
|
|
counterDrawFPS = new FramesPerSecondCounter
|
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Margin = new MarginPadding(2),
|
|
|
|
Y = 10,
|
|
|
|
Scale = new Vector2(0.8f),
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 01:53:29 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2022-07-20 19:49:57 +08:00
|
|
|
|
2022-07-20 20:05:20 +08:00
|
|
|
config.BindWith(OsuSetting.ShowFpsDisplay, showFpsDisplay);
|
2022-07-20 01:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2022-07-20 19:49:57 +08:00
|
|
|
|
2022-07-20 01:53:29 +08:00
|
|
|
displayTemporarily();
|
2022-07-20 19:49:57 +08:00
|
|
|
|
|
|
|
showFpsDisplay.BindValueChanged(showFps =>
|
|
|
|
{
|
2022-07-20 20:05:20 +08:00
|
|
|
State.Value = showFps.NewValue ? Visibility.Visible : Visibility.Hidden;
|
|
|
|
if (showFps.NewValue)
|
|
|
|
displayTemporarily();
|
2022-07-20 19:49:57 +08:00
|
|
|
}, true);
|
2022-07-20 20:05:20 +08:00
|
|
|
|
|
|
|
State.BindValueChanged(state => showFpsDisplay.Value = state.NewValue == Visibility.Visible);
|
2022-07-20 01:53:29 +08:00
|
|
|
}
|
|
|
|
|
2022-07-20 20:05:20 +08:00
|
|
|
protected override void PopIn() => this.FadeIn(100);
|
|
|
|
|
|
|
|
protected override void PopOut() => this.FadeOut(100);
|
|
|
|
|
2022-07-20 19:29:18 +08:00
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
|
|
|
background.FadeTo(1, 200);
|
|
|
|
displayTemporarily();
|
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
|
|
|
background.FadeTo(idle_background_alpha, 200);
|
|
|
|
displayTemporarily();
|
|
|
|
base.OnHoverLost(e);
|
|
|
|
}
|
|
|
|
|
2022-07-20 01:53:29 +08:00
|
|
|
private bool isDisplayed;
|
|
|
|
|
|
|
|
private ScheduledDelegate? fadeOutDelegate;
|
|
|
|
|
2022-07-21 11:57:38 +08:00
|
|
|
private double aimDrawFPS;
|
|
|
|
private double aimUpdateFPS;
|
|
|
|
|
2022-07-20 01:53:29 +08:00
|
|
|
private void displayTemporarily()
|
|
|
|
{
|
|
|
|
if (!isDisplayed)
|
2022-07-21 11:40:27 +08:00
|
|
|
{
|
2022-07-20 01:53:29 +08:00
|
|
|
mainContent.FadeTo(1, 300, Easing.OutQuint);
|
2022-07-21 11:40:27 +08:00
|
|
|
isDisplayed = true;
|
|
|
|
}
|
2022-07-20 01:53:29 +08:00
|
|
|
|
|
|
|
fadeOutDelegate?.Cancel();
|
2022-07-21 11:40:27 +08:00
|
|
|
fadeOutDelegate = null;
|
2022-07-20 19:29:18 +08:00
|
|
|
|
|
|
|
if (!IsHovered)
|
2022-07-20 01:53:29 +08:00
|
|
|
{
|
2022-07-20 19:29:18 +08:00
|
|
|
fadeOutDelegate = Scheduler.AddDelayed(() =>
|
|
|
|
{
|
2022-07-20 22:59:09 +08:00
|
|
|
mainContent.FadeTo(0, 300, Easing.OutQuint);
|
2022-07-20 19:29:18 +08:00
|
|
|
isDisplayed = false;
|
|
|
|
}, 2000);
|
|
|
|
}
|
2022-07-20 01:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private GameHost gameHost { get; set; } = null!;
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2022-07-21 17:56:39 +08:00
|
|
|
mainContent.Width = Math.Max(mainContent.Width, counters.DrawWidth);
|
|
|
|
|
2022-07-21 11:57:38 +08:00
|
|
|
// Handle the case where the window has become inactive or the user changed the
|
|
|
|
// frame limiter (we want to show the FPS as it's changing, even if it isn't an outlier).
|
|
|
|
bool aimRatesChanged = updateAimFPS();
|
2022-07-21 11:50:39 +08:00
|
|
|
|
2022-07-20 01:53:29 +08:00
|
|
|
// TODO: this is wrong (elapsed clock time, not actual run time).
|
2022-07-21 11:50:39 +08:00
|
|
|
double newUpdateFrameTime = gameHost.UpdateThread.Clock.ElapsedFrameTime;
|
2022-07-21 18:11:16 +08:00
|
|
|
double newDrawFrameTime = gameHost.DrawThread.Clock.ElapsedFrameTime;
|
|
|
|
double newDrawFps = gameHost.DrawThread.Clock.FramesPerSecond;
|
2022-07-20 01:53:29 +08:00
|
|
|
|
2022-07-21 11:50:39 +08:00
|
|
|
const double spike_time_ms = 20;
|
2022-07-20 01:53:29 +08:00
|
|
|
|
2022-07-21 11:50:39 +08:00
|
|
|
bool hasUpdateSpike = counterUpdateFrameTime.Current.Value < spike_time_ms && newUpdateFrameTime > spike_time_ms;
|
2022-07-21 18:11:16 +08:00
|
|
|
// use elapsed frame time rather then FramesPerSecond to better catch stutter frames.
|
|
|
|
bool hasDrawSpike = counterDrawFPS.Current.Value > (1000 / spike_time_ms) && newDrawFrameTime > spike_time_ms;
|
2022-07-20 01:53:29 +08:00
|
|
|
|
2022-07-20 19:28:58 +08:00
|
|
|
// If the frame time spikes up, make sure it shows immediately on the counter.
|
2022-07-21 11:50:39 +08:00
|
|
|
if (hasUpdateSpike)
|
|
|
|
counterUpdateFrameTime.SetCountWithoutRolling(newUpdateFrameTime);
|
|
|
|
else
|
|
|
|
counterUpdateFrameTime.Current.Value = newUpdateFrameTime;
|
|
|
|
|
|
|
|
if (hasDrawSpike)
|
2022-07-21 18:11:16 +08:00
|
|
|
// show spike time using raw elapsed value, to account for `FramesPerSecond` being so averaged spike frames don't show.
|
|
|
|
counterDrawFPS.SetCountWithoutRolling(1000 / newDrawFrameTime);
|
2022-07-20 19:28:58 +08:00
|
|
|
else
|
2022-07-21 11:50:39 +08:00
|
|
|
counterDrawFPS.Current.Value = newDrawFps;
|
2022-07-20 19:28:58 +08:00
|
|
|
|
2022-07-21 11:50:39 +08:00
|
|
|
counterDrawFPS.Colour = getColour(counterDrawFPS.DisplayedCount / aimDrawFPS);
|
2022-07-20 19:28:58 +08:00
|
|
|
|
2022-07-21 11:50:39 +08:00
|
|
|
double displayedUpdateFPS = 1000 / counterUpdateFrameTime.DisplayedCount;
|
|
|
|
counterUpdateFrameTime.Colour = getColour(displayedUpdateFPS / aimUpdateFPS);
|
2022-07-20 19:28:58 +08:00
|
|
|
|
2022-07-21 11:57:38 +08:00
|
|
|
bool hasSignificantChanges = aimRatesChanged
|
|
|
|
|| hasDrawSpike
|
2022-07-21 11:50:39 +08:00
|
|
|
|| hasUpdateSpike
|
|
|
|
|| counterDrawFPS.DisplayedCount < aimDrawFPS * 0.8
|
|
|
|
|| displayedUpdateFPS < aimUpdateFPS * 0.8;
|
2022-07-20 19:28:58 +08:00
|
|
|
|
2022-07-21 11:50:39 +08:00
|
|
|
if (hasSignificantChanges)
|
|
|
|
displayTemporarily();
|
2022-07-20 19:28:58 +08:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:57:38 +08:00
|
|
|
private bool updateAimFPS()
|
|
|
|
{
|
|
|
|
if (gameHost.UpdateThread.Clock.Throttling)
|
|
|
|
{
|
|
|
|
double newAimDrawFPS = gameHost.DrawThread.Clock.MaximumUpdateHz;
|
|
|
|
double newAimUpdateFPS = gameHost.UpdateThread.Clock.MaximumUpdateHz;
|
|
|
|
|
|
|
|
if (aimDrawFPS != newAimDrawFPS || aimUpdateFPS != newAimUpdateFPS)
|
|
|
|
{
|
|
|
|
aimDrawFPS = newAimDrawFPS;
|
|
|
|
aimUpdateFPS = newAimUpdateFPS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double newAimFPS = gameHost.InputThread.Clock.MaximumUpdateHz;
|
|
|
|
|
|
|
|
if (aimDrawFPS != newAimFPS || aimUpdateFPS != newAimFPS)
|
|
|
|
{
|
|
|
|
aimUpdateFPS = aimDrawFPS = newAimFPS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:28:58 +08:00
|
|
|
private ColourInfo getColour(double performanceRatio)
|
|
|
|
{
|
|
|
|
if (performanceRatio < 0.5f)
|
2022-07-20 22:59:09 +08:00
|
|
|
return Interpolation.ValueAt(performanceRatio, colours.Red, colours.Orange2, 0, 0.5);
|
2022-07-20 19:28:58 +08:00
|
|
|
|
2022-07-20 22:59:09 +08:00
|
|
|
return Interpolation.ValueAt(performanceRatio, colours.Orange2, colours.Lime0, 0.5, 0.9);
|
2022-07-20 01:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public ITooltip GetCustomTooltip() => new FPSCounterTooltip();
|
|
|
|
|
|
|
|
public object TooltipContent => this;
|
|
|
|
|
|
|
|
public class FramesPerSecondCounter : RollingCounter<double>
|
|
|
|
{
|
2022-07-21 11:50:39 +08:00
|
|
|
protected override double RollingDuration => 1000;
|
2022-07-20 01:53:29 +08:00
|
|
|
|
|
|
|
protected override OsuSpriteText CreateSpriteText()
|
|
|
|
{
|
|
|
|
return new OsuSpriteText
|
|
|
|
{
|
2022-07-21 10:58:28 +08:00
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2022-07-20 01:53:29 +08:00
|
|
|
Font = OsuFont.Default.With(fixedWidth: true, size: 16, weight: FontWeight.SemiBold),
|
|
|
|
Spacing = new Vector2(-2),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override LocalisableString FormatCount(double count)
|
|
|
|
{
|
|
|
|
return $"{count:#,0}fps";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class FrameTimeCounter : RollingCounter<double>
|
|
|
|
{
|
|
|
|
protected override double RollingDuration => 1000;
|
|
|
|
|
|
|
|
protected override OsuSpriteText CreateSpriteText()
|
|
|
|
{
|
|
|
|
return new OsuSpriteText
|
|
|
|
{
|
2022-07-21 10:58:28 +08:00
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2022-07-20 01:53:29 +08:00
|
|
|
Font = OsuFont.Default.With(fixedWidth: true, size: 16, weight: FontWeight.SemiBold),
|
|
|
|
Spacing = new Vector2(-1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override LocalisableString FormatCount(double count)
|
|
|
|
{
|
|
|
|
if (count < 1)
|
|
|
|
return $"{count:N1}ms";
|
|
|
|
|
|
|
|
return $"{count:N0}ms";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|