2022-07-19 17:53:29 +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.
|
|
|
|
|
2022-07-21 12:39:24 +00:00
|
|
|
using System;
|
2022-07-19 17:53:29 +00:00
|
|
|
using osu.Framework.Allocation;
|
2022-07-20 11:49:57 +00:00
|
|
|
using osu.Framework.Bindables;
|
2022-07-19 17:53:29 +00:00
|
|
|
using osu.Framework.Graphics;
|
2022-07-20 11:28:58 +00:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2022-07-19 17:53:29 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2022-07-20 11:29:18 +00:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input.Events;
|
2022-07-19 17:53:29 +00:00
|
|
|
using osu.Framework.Platform;
|
2022-07-21 14:02:31 +00:00
|
|
|
using osu.Framework.Timing;
|
2022-07-20 11:28:58 +00:00
|
|
|
using osu.Framework.Utils;
|
2022-07-20 11:49:57 +00:00
|
|
|
using osu.Game.Configuration;
|
2022-07-19 17:53:29 +00:00
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
2022-07-20 12:05:20 +00:00
|
|
|
public partial class FPSCounter : VisibilityContainer, IHasCustomTooltip
|
2022-07-19 17:53:29 +00:00
|
|
|
{
|
2022-07-21 13:27:22 +00:00
|
|
|
private OsuSpriteText counterUpdateFrameTime = null!;
|
|
|
|
private OsuSpriteText counterDrawFPS = null!;
|
2022-07-19 17:53:29 +00:00
|
|
|
|
|
|
|
private Container mainContent = null!;
|
|
|
|
|
2022-07-20 11:29:18 +00:00
|
|
|
private Container background = null!;
|
|
|
|
|
2022-07-21 09:56:39 +00:00
|
|
|
private Container counters = null!;
|
|
|
|
|
2022-07-21 13:55:45 +00:00
|
|
|
private const double min_time_between_updates = 10;
|
|
|
|
|
|
|
|
private const double spike_time_ms = 20;
|
|
|
|
|
2022-07-20 11:29:18 +00:00
|
|
|
private const float idle_background_alpha = 0.4f;
|
|
|
|
|
2022-07-20 12:05:20 +00:00
|
|
|
private readonly BindableBool showFpsDisplay = new BindableBool(true);
|
2022-07-20 11:49:57 +00:00
|
|
|
|
2022-07-21 13:27:22 +00:00
|
|
|
private double displayedFpsCount;
|
|
|
|
private double displayedFrameTime;
|
|
|
|
|
2022-07-21 14:02:31 +00:00
|
|
|
private bool isDisplayed;
|
|
|
|
|
|
|
|
private double aimDrawFPS;
|
|
|
|
private double aimUpdateFPS;
|
|
|
|
|
|
|
|
private double lastUpdate;
|
|
|
|
private ThrottledFrameClock drawClock = null!;
|
|
|
|
private ThrottledFrameClock updateClock = null!;
|
|
|
|
private ThrottledFrameClock inputClock = null!;
|
|
|
|
|
2022-07-25 07:26:47 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The last time value where the display was required (due to a significant change or hovering).
|
|
|
|
/// </summary>
|
|
|
|
private double lastDisplayRequiredTime;
|
|
|
|
|
2022-07-20 11:28:58 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; } = null!;
|
|
|
|
|
2022-07-19 17:53:29 +00:00
|
|
|
public FPSCounter()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-07-21 14:02:31 +00:00
|
|
|
private void load(OsuConfigManager config, GameHost gameHost)
|
2022-07-19 17:53:29 +00:00
|
|
|
{
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
mainContent = new Container
|
|
|
|
{
|
|
|
|
Alpha = 0,
|
2022-07-21 09:56:39 +00:00
|
|
|
Height = 26,
|
2022-07-19 17:53:29 +00:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2022-07-20 11:29:18 +00:00
|
|
|
background = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
CornerRadius = 5,
|
2022-07-20 14:59:09 +00:00
|
|
|
CornerExponent = 5f,
|
2022-07-20 11:29:18 +00:00
|
|
|
Masking = true,
|
|
|
|
Alpha = idle_background_alpha,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
Colour = colours.Gray0,
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 09:56:39 +00:00
|
|
|
counters = new Container
|
2022-07-19 17:53:29 +00:00
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2022-07-21 09:56:39 +00:00
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2022-07-21 13:27:22 +00:00
|
|
|
counterUpdateFrameTime = new OsuSpriteText
|
2022-07-21 09:56:39 +00:00
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Margin = new MarginPadding(1),
|
2022-07-21 13:27:22 +00:00
|
|
|
Font = OsuFont.Default.With(fixedWidth: true, size: 16, weight: FontWeight.SemiBold),
|
|
|
|
Spacing = new Vector2(-1),
|
2022-07-21 09:56:39 +00:00
|
|
|
Y = -2,
|
|
|
|
},
|
2022-07-21 13:27:22 +00:00
|
|
|
counterDrawFPS = new OsuSpriteText
|
2022-07-21 09:56:39 +00:00
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Margin = new MarginPadding(2),
|
2022-07-21 13:27:22 +00:00
|
|
|
Font = OsuFont.Default.With(fixedWidth: true, size: 13, weight: FontWeight.SemiBold),
|
|
|
|
Spacing = new Vector2(-2),
|
2022-07-21 09:56:39 +00:00
|
|
|
Y = 10,
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 17:53:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2022-07-20 11:49:57 +00:00
|
|
|
|
2022-07-20 12:05:20 +00:00
|
|
|
config.BindWith(OsuSetting.ShowFpsDisplay, showFpsDisplay);
|
2022-07-21 14:02:31 +00:00
|
|
|
|
|
|
|
drawClock = gameHost.DrawThread.Clock;
|
|
|
|
updateClock = gameHost.UpdateThread.Clock;
|
|
|
|
inputClock = gameHost.InputThread.Clock;
|
2022-07-19 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2022-07-20 11:49:57 +00:00
|
|
|
|
2022-07-25 07:26:47 +00:00
|
|
|
requestDisplay();
|
2022-07-20 11:49:57 +00:00
|
|
|
|
|
|
|
showFpsDisplay.BindValueChanged(showFps =>
|
|
|
|
{
|
2022-07-20 12:05:20 +00:00
|
|
|
State.Value = showFps.NewValue ? Visibility.Visible : Visibility.Hidden;
|
|
|
|
if (showFps.NewValue)
|
2022-07-25 07:26:47 +00:00
|
|
|
requestDisplay();
|
2022-07-20 11:49:57 +00:00
|
|
|
}, true);
|
2022-07-20 12:05:20 +00:00
|
|
|
|
|
|
|
State.BindValueChanged(state => showFpsDisplay.Value = state.NewValue == Visibility.Visible);
|
2022-07-19 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 12:05:20 +00:00
|
|
|
protected override void PopIn() => this.FadeIn(100);
|
|
|
|
|
|
|
|
protected override void PopOut() => this.FadeOut(100);
|
|
|
|
|
2022-07-20 11:29:18 +00:00
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
|
|
|
background.FadeTo(1, 200);
|
2022-07-25 07:26:47 +00:00
|
|
|
requestDisplay();
|
2022-07-20 11:29:18 +00:00
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
|
|
|
background.FadeTo(idle_background_alpha, 200);
|
2022-07-25 07:26:47 +00:00
|
|
|
requestDisplay();
|
2022-07-20 11:29:18 +00:00
|
|
|
base.OnHoverLost(e);
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:53:29 +00:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2023-06-16 16:32:41 +00:00
|
|
|
double elapsedDrawFrameTime = drawClock.ElapsedFrameTime;
|
|
|
|
double elapsedUpdateFrameTime = updateClock.ElapsedFrameTime;
|
|
|
|
|
2022-08-08 06:17:33 +00:00
|
|
|
// If the game goes into a suspended state (ie. debugger attached or backgrounded on a mobile device)
|
|
|
|
// we want to ignore really long periods of no processing.
|
2023-06-16 16:32:41 +00:00
|
|
|
if (elapsedUpdateFrameTime > 10000)
|
2022-08-08 06:17:33 +00:00
|
|
|
return;
|
|
|
|
|
2022-07-21 09:56:39 +00:00
|
|
|
mainContent.Width = Math.Max(mainContent.Width, counters.DrawWidth);
|
|
|
|
|
2022-07-21 03:57:38 +00: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 03:50:39 +00:00
|
|
|
|
2023-06-16 16:32:41 +00:00
|
|
|
bool hasUpdateSpike = displayedFrameTime < spike_time_ms && elapsedUpdateFrameTime > spike_time_ms;
|
2022-07-21 10:11:16 +00:00
|
|
|
// use elapsed frame time rather then FramesPerSecond to better catch stutter frames.
|
2023-06-16 16:32:41 +00:00
|
|
|
bool hasDrawSpike = displayedFpsCount > (1000 / spike_time_ms) && elapsedDrawFrameTime > spike_time_ms;
|
2022-07-19 17:53:29 +00:00
|
|
|
|
2022-08-08 06:17:42 +00:00
|
|
|
const float damp_time = 100;
|
|
|
|
|
2023-06-16 16:32:41 +00:00
|
|
|
displayedFrameTime = Interpolation.DampContinuously(displayedFrameTime, elapsedUpdateFrameTime, hasUpdateSpike ? 0 : damp_time, elapsedUpdateFrameTime);
|
2022-07-21 03:50:39 +00:00
|
|
|
|
|
|
|
if (hasDrawSpike)
|
2022-07-21 10:11:16 +00:00
|
|
|
// show spike time using raw elapsed value, to account for `FramesPerSecond` being so averaged spike frames don't show.
|
2023-06-16 16:32:41 +00:00
|
|
|
displayedFpsCount = 1000 / elapsedDrawFrameTime;
|
2022-07-20 11:28:58 +00:00
|
|
|
else
|
2022-08-08 06:17:42 +00:00
|
|
|
displayedFpsCount = Interpolation.DampContinuously(displayedFpsCount, drawClock.FramesPerSecond, damp_time, Time.Elapsed);
|
2022-07-20 11:28:58 +00:00
|
|
|
|
2022-07-21 13:55:45 +00:00
|
|
|
if (Time.Current - lastUpdate > min_time_between_updates)
|
|
|
|
{
|
|
|
|
updateFpsDisplay();
|
|
|
|
updateFrameTimeDisplay();
|
|
|
|
|
|
|
|
lastUpdate = Time.Current;
|
|
|
|
}
|
2022-07-20 11:28:58 +00:00
|
|
|
|
2022-07-21 03:57:38 +00:00
|
|
|
bool hasSignificantChanges = aimRatesChanged
|
|
|
|
|| hasDrawSpike
|
2022-07-21 03:50:39 +00:00
|
|
|
|| hasUpdateSpike
|
2022-07-21 13:27:22 +00:00
|
|
|
|| displayedFpsCount < aimDrawFPS * 0.8
|
|
|
|
|| 1000 / displayedFrameTime < aimUpdateFPS * 0.8;
|
2022-07-20 11:28:58 +00:00
|
|
|
|
2022-07-21 03:50:39 +00:00
|
|
|
if (hasSignificantChanges)
|
2022-07-25 07:26:47 +00:00
|
|
|
requestDisplay();
|
2022-08-08 05:52:47 +00:00
|
|
|
else if (isDisplayed && Time.Current - lastDisplayRequiredTime > 2000 && !IsHovered)
|
2022-07-25 07:26:47 +00:00
|
|
|
{
|
2023-08-14 14:55:56 +00:00
|
|
|
mainContent.FadeTo(0.7f, 300, Easing.OutQuint);
|
2022-07-25 07:26:47 +00:00
|
|
|
isDisplayed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void requestDisplay()
|
|
|
|
{
|
|
|
|
lastDisplayRequiredTime = Time.Current;
|
|
|
|
|
|
|
|
if (!isDisplayed)
|
|
|
|
{
|
|
|
|
mainContent.FadeTo(1, 300, Easing.OutQuint);
|
|
|
|
isDisplayed = true;
|
|
|
|
}
|
2022-07-20 11:28:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 13:27:22 +00:00
|
|
|
private void updateFpsDisplay()
|
|
|
|
{
|
|
|
|
counterDrawFPS.Colour = getColour(displayedFpsCount / aimDrawFPS);
|
|
|
|
counterDrawFPS.Text = $"{displayedFpsCount:#,0}fps";
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateFrameTimeDisplay()
|
|
|
|
{
|
|
|
|
counterUpdateFrameTime.Text = displayedFrameTime < 5
|
|
|
|
? $"{displayedFrameTime:N1}ms"
|
|
|
|
: $"{displayedFrameTime:N0}ms";
|
|
|
|
|
|
|
|
counterUpdateFrameTime.Colour = getColour((1000 / displayedFrameTime) / aimUpdateFPS);
|
|
|
|
}
|
|
|
|
|
2022-07-21 03:57:38 +00:00
|
|
|
private bool updateAimFPS()
|
|
|
|
{
|
2022-07-21 14:02:31 +00:00
|
|
|
if (updateClock.Throttling)
|
2022-07-21 03:57:38 +00:00
|
|
|
{
|
2022-07-21 14:02:31 +00:00
|
|
|
double newAimDrawFPS = drawClock.MaximumUpdateHz;
|
|
|
|
double newAimUpdateFPS = updateClock.MaximumUpdateHz;
|
2022-07-21 03:57:38 +00:00
|
|
|
|
|
|
|
if (aimDrawFPS != newAimDrawFPS || aimUpdateFPS != newAimUpdateFPS)
|
|
|
|
{
|
|
|
|
aimDrawFPS = newAimDrawFPS;
|
|
|
|
aimUpdateFPS = newAimUpdateFPS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-21 14:02:31 +00:00
|
|
|
double newAimFPS = inputClock.MaximumUpdateHz;
|
2022-07-21 03:57:38 +00:00
|
|
|
|
|
|
|
if (aimDrawFPS != newAimFPS || aimUpdateFPS != newAimFPS)
|
|
|
|
{
|
|
|
|
aimUpdateFPS = aimDrawFPS = newAimFPS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-20 11:28:58 +00:00
|
|
|
private ColourInfo getColour(double performanceRatio)
|
|
|
|
{
|
|
|
|
if (performanceRatio < 0.5f)
|
2022-07-20 14:59:09 +00:00
|
|
|
return Interpolation.ValueAt(performanceRatio, colours.Red, colours.Orange2, 0, 0.5);
|
2022-07-20 11:28:58 +00:00
|
|
|
|
2022-07-20 14:59:09 +00:00
|
|
|
return Interpolation.ValueAt(performanceRatio, colours.Orange2, colours.Lime0, 0.5, 0.9);
|
2022-07-19 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ITooltip GetCustomTooltip() => new FPSCounterTooltip();
|
|
|
|
|
|
|
|
public object TooltipContent => this;
|
|
|
|
}
|
|
|
|
}
|