Merge pull request #4455 from peppy/fix-background-loading

Fix screen background performance
This commit is contained in:
Dan Balasescu
2019-03-20 11:35:00 +09:00
committed by GitHub
7 changed files with 68 additions and 41 deletions

View File

@ -11,6 +11,7 @@ namespace osu.Game.Screens
public class BackgroundScreenStack : ScreenStack
{
public BackgroundScreenStack()
: base(false)
{
Scale = new Vector2(1.06f);
RelativeSizeAxes = Axes.Both;

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.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -26,7 +27,24 @@ namespace osu.Game.Screens.Backgrounds
protected virtual UserDimContainer CreateFadeContainer() => new UserDimContainer { RelativeSizeAxes = Axes.Both };
public virtual WorkingBeatmap Beatmap
public BackgroundScreenBeatmap(WorkingBeatmap beatmap = null)
{
Beatmap = beatmap;
InternalChild = fadeContainer = CreateFadeContainer();
fadeContainer.EnableUserDim.BindTo(EnableUserDim);
}
[BackgroundDependencyLoader]
private void load()
{
var background = new BeatmapBackground(beatmap);
LoadComponent(background);
switchBackground(background);
}
private CancellationTokenSource cancellationSource;
public WorkingBeatmap Beatmap
{
get => beatmap;
set
@ -38,54 +56,52 @@ namespace osu.Game.Screens.Backgrounds
Schedule(() =>
{
LoadComponentAsync(new BeatmapBackground(beatmap), b => Schedule(() =>
{
float newDepth = 0;
if (Background != null)
{
newDepth = Background.Depth + 1;
Background.FinishTransforms();
Background.FadeOut(250);
Background.Expire();
}
if ((Background as BeatmapBackground)?.Beatmap == beatmap)
return;
b.Depth = newDepth;
fadeContainer.Add(Background = b);
Background.BlurSigma = BlurTarget;
StoryboardReplacesBackground.BindTo(fadeContainer.StoryboardReplacesBackground);
}));
cancellationSource?.Cancel();
LoadComponentAsync(new BeatmapBackground(beatmap), switchBackground, (cancellationSource = new CancellationTokenSource()).Token);
});
}
}
public BackgroundScreenBeatmap(WorkingBeatmap beatmap = null)
private void switchBackground(BeatmapBackground b)
{
Beatmap = beatmap;
InternalChild = fadeContainer = CreateFadeContainer();
fadeContainer.EnableUserDim.BindTo(EnableUserDim);
float newDepth = 0;
if (Background != null)
{
newDepth = Background.Depth + 1;
Background.FinishTransforms();
Background.FadeOut(250);
Background.Expire();
}
b.Depth = newDepth;
fadeContainer.Add(Background = b);
Background.BlurSigma = BlurTarget;
StoryboardReplacesBackground.BindTo(fadeContainer.StoryboardReplacesBackground);
}
public override bool Equals(BackgroundScreen other)
{
var otherBeatmapBackground = other as BackgroundScreenBeatmap;
if (otherBeatmapBackground == null) return false;
if (!(other is BackgroundScreenBeatmap otherBeatmapBackground)) return false;
return base.Equals(other) && beatmap == otherBeatmapBackground.Beatmap;
}
protected class BeatmapBackground : Background
{
private readonly WorkingBeatmap beatmap;
public readonly WorkingBeatmap Beatmap;
public BeatmapBackground(WorkingBeatmap beatmap)
{
this.beatmap = beatmap;
Beatmap = beatmap;
}
[BackgroundDependencyLoader]
private void load(TextureStore textures)
{
Sprite.Texture = beatmap?.Background ?? textures.Get(@"Backgrounds/bg1");
Sprite.Texture = Beatmap?.Background ?? textures.Get(@"Backgrounds/bg1");
}
}
}

View File

@ -34,7 +34,7 @@ namespace osu.Game.Screens.Backgrounds
currentDisplay = RNG.Next(0, background_count);
Next();
display(createBackground());
}
private void display(Background newBackground)
@ -51,19 +51,21 @@ namespace osu.Game.Screens.Backgrounds
public void Next()
{
nextTask?.Cancel();
nextTask = Scheduler.AddDelayed(() =>
{
Background background;
nextTask = Scheduler.AddDelayed(() => { LoadComponentAsync(createBackground(), display); }, 100);
}
if (user.Value?.IsSupporter ?? false)
background = new SkinnedBackground(skin.Value, backgroundName);
else
background = new Background(backgroundName);
private Background createBackground()
{
Background background;
background.Depth = currentDisplay;
if (user.Value?.IsSupporter ?? false)
background = new SkinnedBackground(skin.Value, backgroundName);
else
background = new Background(backgroundName);
LoadComponentAsync(background, display);
}, 100);
background.Depth = currentDisplay;
return background;
}
private class SkinnedBackground : Background

View File

@ -14,7 +14,7 @@ namespace osu.Game.Screens
protected Vector2 BlurTarget;
public TransformSequence<Background> BlurTo(Vector2 sigma, double duration, Easing easing = Easing.None)
public TransformSequence<Background> BlurTo(Vector2 sigma, double duration = 0, Easing easing = Easing.None)
{
BlurTarget = sigma;
return Background?.BlurTo(BlurTarget, duration, easing);

View File

@ -40,7 +40,9 @@ namespace osu.Game.Screens.Menu
[Resolved]
private GameHost host { get; set; }
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
private BackgroundScreenDefault background;
protected override BackgroundScreen CreateBackground() => background;
[BackgroundDependencyLoader(true)]
private void load(OsuGame game = null)
@ -89,6 +91,7 @@ namespace osu.Game.Screens.Menu
buttons.OnDirect = game.ToggleDirect;
}
LoadComponentAsync(background = new BackgroundScreenDefault());
preloadSongSelect();
}

View File

@ -55,7 +55,7 @@ namespace osu.Game.Screens.Play
/// Called when background elements require updates, usually due to a user changing a setting.
/// </summary>
/// <param name="userChange"></param>
protected virtual void UpdateBackgroundElements()
protected void UpdateBackgroundElements()
{
if (!this.IsCurrentScreen()) return;

View File

@ -1,4 +1,4 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// 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;
@ -61,7 +61,12 @@ namespace osu.Game.Screens.Select
/// </summary>
protected readonly Container FooterPanels;
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap();
protected override BackgroundScreen CreateBackground()
{
var background = new BackgroundScreenBeatmap();
background.BlurTo(background_blur);
return background;
}
protected readonly BeatmapCarousel Carousel;
private readonly BeatmapInfoWedge beatmapInfoWedge;