Reduce unnecessary background changes via `IEquatable` implementation

This commit is contained in:
Bartłomiej Dach 2021-06-08 22:26:15 +02:00
parent f628ec25ef
commit 97204b6f27
4 changed files with 34 additions and 7 deletions

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;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -14,7 +15,7 @@ namespace osu.Game.Graphics.Backgrounds
/// <summary>
/// A background which offers blurring via a <see cref="BufferedContainer"/> on demand.
/// </summary>
public class Background : CompositeDrawable
public class Background : CompositeDrawable, IEquatable<Background>
{
private const float blur_scale = 0.5f;
@ -71,5 +72,14 @@ public void BlurTo(Vector2 newBlurSigma, double duration = 0, Easing easing = Ea
bufferedContainer?.BlurTo(newBlurSigma * blur_scale, duration, easing);
}
public virtual bool Equals(Background other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.GetType() == GetType()
&& other.textureName == textureName;
}
}
}

View File

@ -24,5 +24,14 @@ private void load(LargeTextureStore textures)
{
Sprite.Texture = Beatmap?.Background ?? textures.Get(fallbackTextureName);
}
public override bool Equals(Background other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.GetType() == GetType()
&& ((BeatmapBackground)other).Beatmap == Beatmap;
}
}
}

View File

@ -21,5 +21,14 @@ private void load()
{
Sprite.Texture = skin.GetTexture("menu-background") ?? Sprite.Texture;
}
public override bool Equals(Background other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.GetType() == GetType()
&& ((SkinBackground)other).skin.SkinInfo.Equals(skin.SkinInfo);
}
}
}

View File

@ -112,12 +112,6 @@ private Background createBackground()
newBackground = new BeatmapBackgroundWithStoryboard(beatmap.Value, getBackgroundTextureName());
newBackground ??= new BeatmapBackground(beatmap.Value, getBackgroundTextureName());
// this method is called in many cases where the beatmap hasn't changed (ie. on screen transitions).
// if a background is already displayed for the requested beatmap, we don't want to load it again.
if (background?.GetType() == newBackground.GetType() &&
(background as BeatmapBackground)?.Beatmap == beatmap.Value)
return background;
break;
}
@ -127,6 +121,11 @@ private Background createBackground()
}
}
// this method is called in many cases where the background might not necessarily need to change.
// if an equivalent background is currently being shown, we don't want to load it again.
if (newBackground?.Equals(background) == true)
return background;
newBackground ??= new Background(getBackgroundTextureName());
newBackground.Depth = currentDisplay;