Skip to content

Commit

Permalink
Refactor player max dimensions test scene to actually upscale textures
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Sep 29, 2023
1 parent 0bcb99f commit 314ecec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
73 changes: 65 additions & 8 deletions osu.Game.Tests/Visual/Gameplay/TestScenePlayerMaxDimensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Testing;
using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Screens.Play;
using osu.Game.Skinning;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace osu.Game.Tests.Visual.Gameplay
{
Expand All @@ -23,6 +29,9 @@ namespace osu.Game.Tests.Visual.Gameplay
/// </remarks>
public partial class TestScenePlayerMaxDimensions : TestSceneAllRulesetPlayers
{
// scale textures to 4 times their size.
private const int scale_factor = 4;

protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
Expand Down Expand Up @@ -63,18 +72,66 @@ public event Action? SourceChanged
remove { }
}

public override Texture? GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => this;
public IEnumerable<ISkin> AllSources => new[] { this };

protected override IResourceStore<TextureUpload> CreateTextureLoaderStore(IStorageResourceProvider resources, IResourceStore<byte[]> storage)
=> new UpscaledTextureLoaderStore(base.CreateTextureLoaderStore(resources, storage));

private class UpscaledTextureLoaderStore : IResourceStore<TextureUpload>
{
var texture = base.GetTexture(componentName, wrapModeS, wrapModeT);
private readonly IResourceStore<TextureUpload>? textureStore;

if (texture != null)
texture.ScaleAdjust /= 8f;
public UpscaledTextureLoaderStore(IResourceStore<TextureUpload>? textureStore)
{
this.textureStore = textureStore;
}

return texture;
}
public void Dispose()
{
textureStore?.Dispose();
}

public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => this;
public IEnumerable<ISkin> AllSources => new[] { this };
public TextureUpload Get(string name)
{
var textureUpload = textureStore?.Get(name);

// NRT not enabled on framework side classes (IResourceStore / TextureLoaderStore), welp.
if (textureUpload == null)
return null!;

return upscale(textureUpload);
}

public async Task<TextureUpload> GetAsync(string name, CancellationToken cancellationToken = new CancellationToken())
{
// NRT not enabled on framework side classes (IResourceStore / TextureLoaderStore), welp.
if (textureStore == null)
return null!;

var textureUpload = await textureStore.GetAsync(name, cancellationToken).ConfigureAwait(false);

if (textureUpload == null)
return null!;

return await Task.Run(() => upscale(textureUpload), cancellationToken).ConfigureAwait(false);
}

private TextureUpload upscale(TextureUpload textureUpload)
{
var image = Image.LoadPixelData(textureUpload.Data.ToArray(), textureUpload.Width, textureUpload.Height);

// The original texture upload will no longer be returned or used.
textureUpload.Dispose();

image.Mutate(i => i.Resize(new Size(textureUpload.Width, textureUpload.Height) * scale_factor));
return new TextureUpload(image);
}

public Stream? GetStream(string name) => textureStore?.GetStream(name);

public IEnumerable<string> GetAvailableResources() => textureStore?.GetAvailableResources() ?? Array.Empty<string>();
}
}
}
}
5 changes: 4 additions & 1 deletion osu.Game/Skinning/Skin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Skin(SkinInfo skin, IStorageResourceProvider? resources, IResourceStor
}

Samples = samples;
Textures = new TextureStore(resources.Renderer, new MaxDimensionLimitedTextureLoaderStore(resources.CreateTextureLoaderStore(storage)));
Textures = new TextureStore(resources.Renderer, CreateTextureLoaderStore(resources, storage));
}
else
{
Expand Down Expand Up @@ -171,6 +171,9 @@ protected Skin(SkinInfo skin, IStorageResourceProvider? resources, IResourceStor
}
}

protected virtual IResourceStore<TextureUpload> CreateTextureLoaderStore(IStorageResourceProvider resources, IResourceStore<byte[]> storage)
=> new MaxDimensionLimitedTextureLoaderStore(resources.CreateTextureLoaderStore(storage));

protected virtual void ParseConfigurationStream(Stream stream)
{
using (LineBufferedReader reader = new LineBufferedReader(stream, true))
Expand Down

0 comments on commit 314ecec

Please sign in to comment.