1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 01:02:55 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/FPSCounter.cs

288 lines
9.8 KiB
C#
Raw Normal View History

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.
2022-07-21 12:06:43 +08:00
using System;
2022-07-20 01:53:29 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2022-07-20 01:53:29 +08:00
using osu.Framework.Graphics;
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;
using osu.Framework.Utils;
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
{
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!;
private const float idle_background_alpha = 0.4f;
2022-07-20 20:05:20 +08:00
private readonly BindableBool showFpsDisplay = new BindableBool(true);
[Resolved]
private OsuColour colours { get; set; } = null!;
2022-07-20 01:53:29 +08:00
public FPSCounter()
{
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
2022-07-20 01:53:29 +08:00
{
InternalChildren = new Drawable[]
{
mainContent = new Container
{
Alpha = 0,
2022-07-20 22:59:09 +08:00
Size = new Vector2(42, 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,
},
}
},
counterUpdateFrameTime = new FrameTimeCounter
2022-07-20 01:53:29 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2022-07-20 22:59:09 +08:00
Margin = new MarginPadding(1),
Y = -2,
2022-07-20 01:53:29 +08:00
},
counterDrawFPS = new FramesPerSecondCounter
2022-07-20 01:53:29 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(2),
2022-07-20 22:59:09 +08:00
Y = 10,
2022-07-20 01:53:29 +08:00
Scale = new Vector2(0.8f),
}
}
},
};
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 01:53:29 +08:00
displayTemporarily();
showFpsDisplay.BindValueChanged(showFps =>
{
2022-07-20 20:05:20 +08:00
State.Value = showFps.NewValue ? Visibility.Visible : Visibility.Hidden;
if (showFps.NewValue)
displayTemporarily();
}, 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;
private double aimDrawFPS;
private double aimUpdateFPS;
2022-07-20 01:53:29 +08:00
private void displayTemporarily()
{
if (!isDisplayed)
{
2022-07-20 01:53:29 +08:00
mainContent.FadeTo(1, 300, Easing.OutQuint);
isDisplayed = true;
}
2022-07-20 01:53:29 +08:00
fadeOutDelegate?.Cancel();
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();
// 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-20 01:53:29 +08:00
// TODO: this is wrong (elapsed clock time, not actual run time).
double newUpdateFrameTime = gameHost.UpdateThread.Clock.ElapsedFrameTime;
// use elapsed frame time rather then FramesPerSecond to better catch stutter frames.
2022-07-21 12:06:43 +08:00
double newDrawFps = 1000 / Math.Max(0.001, gameHost.DrawThread.Clock.ElapsedFrameTime);
2022-07-20 01:53:29 +08:00
const double spike_time_ms = 20;
2022-07-20 01:53:29 +08:00
bool hasUpdateSpike = counterUpdateFrameTime.Current.Value < spike_time_ms && newUpdateFrameTime > spike_time_ms;
bool hasDrawSpike = counterDrawFPS.Current.Value > (1000 / spike_time_ms) && newDrawFps <= (1000 / spike_time_ms);
2022-07-20 01:53:29 +08:00
// If the frame time spikes up, make sure it shows immediately on the counter.
if (hasUpdateSpike)
counterUpdateFrameTime.SetCountWithoutRolling(newUpdateFrameTime);
else
counterUpdateFrameTime.Current.Value = newUpdateFrameTime;
if (hasDrawSpike)
counterDrawFPS.SetCountWithoutRolling(newDrawFps);
else
counterDrawFPS.Current.Value = newDrawFps;
counterDrawFPS.Colour = getColour(counterDrawFPS.DisplayedCount / aimDrawFPS);
double displayedUpdateFPS = 1000 / counterUpdateFrameTime.DisplayedCount;
counterUpdateFrameTime.Colour = getColour(displayedUpdateFPS / aimUpdateFPS);
bool hasSignificantChanges = aimRatesChanged
|| hasDrawSpike
|| hasUpdateSpike
|| counterDrawFPS.DisplayedCount < aimDrawFPS * 0.8
|| displayedUpdateFPS < aimUpdateFPS * 0.8;
if (hasSignificantChanges)
displayTemporarily();
}
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;
}
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 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>
{
protected override double RollingDuration => 1000;
2022-07-20 01:53:29 +08:00
protected override OsuSpriteText CreateSpriteText()
{
return new OsuSpriteText
{
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
{
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";
}
}
}
}