-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
736a9a7
commit 716fa55
Showing
9 changed files
with
195 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,124 @@ | ||
using Craftimizer.Plugin; | ||
using Dalamud.Interface.Textures; | ||
using Dalamud.Interface.Textures.TextureWraps; | ||
using Dalamud.Utility; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Craftimizer.Utils; | ||
|
||
public static class IconManager | ||
public interface ITextureIcon | ||
{ | ||
public static ISharedImmediateTexture GetIcon(uint id, bool isHq = false) | ||
ISharedImmediateTexture Source { get; } | ||
|
||
Vector2? Dimensions { get; } | ||
|
||
float? AspectRatio => Dimensions is { } d ? d.X / d.Y : null; | ||
|
||
nint ImGuiHandle { get; } | ||
} | ||
|
||
public interface ILoadedTextureIcon : ITextureIcon, IDisposable { } | ||
|
||
public sealed class IconManager : IDisposable | ||
{ | ||
private sealed class LoadedIcon : ILoadedTextureIcon | ||
{ | ||
// 10: DXGI_FORMAT_R16G16B16A16_FLOAT | ||
public static IDalamudTextureWrap EmptyTexture { get; } = Service.TextureProvider.CreateEmpty(new(4, 4, 10), false, false); | ||
|
||
public ISharedImmediateTexture Source { get; } | ||
|
||
public Vector2? Dimensions => GetWrap()?.Size; | ||
|
||
public nint ImGuiHandle => GetWrapOrEmpty().ImGuiHandle; | ||
|
||
private Task<IDalamudTextureWrap> TextureWrapTask { get; } | ||
private CancellationTokenSource DisposeToken { get; } | ||
|
||
public LoadedIcon(ISharedImmediateTexture source) | ||
{ | ||
Source = source; | ||
DisposeToken = new(); | ||
TextureWrapTask = source.RentAsync(DisposeToken.Token); | ||
} | ||
|
||
public IDalamudTextureWrap? GetWrap() | ||
{ | ||
if (TextureWrapTask.IsCompletedSuccessfully) | ||
return TextureWrapTask.Result; | ||
return null; | ||
} | ||
|
||
public IDalamudTextureWrap GetWrapOrEmpty() => GetWrap() ?? EmptyTexture; | ||
|
||
public void Dispose() | ||
{ | ||
DisposeToken.Cancel(); | ||
TextureWrapTask.ToContentDisposedTask(true).Wait(); | ||
} | ||
} | ||
|
||
// TODO: Unload when unused, but with a custom timer? | ||
Check warning on line 66 in Craftimizer/Utils/IconManager.cs GitHub Actions / build
Check warning on line 66 in Craftimizer/Utils/IconManager.cs GitHub Actions / build
|
||
private sealed class CachedIcon : ITextureIcon | ||
{ | ||
return Service.TextureProvider.GetFromGameIcon(new GameIconLookup(id, itemHq: isHq)); | ||
private LoadedIcon Base { get; } | ||
|
||
public ISharedImmediateTexture Source => Base.Source; | ||
|
||
public Vector2? Dimensions => Base.Dimensions; | ||
|
||
public nint ImGuiHandle => Base.ImGuiHandle; | ||
|
||
public CachedIcon(ISharedImmediateTexture source) | ||
{ | ||
Base = new(source); | ||
} | ||
|
||
public void Release() | ||
{ | ||
Base.Dispose(); | ||
} | ||
} | ||
|
||
public static ISharedImmediateTexture GetTexture(string path) | ||
private Dictionary<(uint Id, bool IsHq), CachedIcon> IconCache { get; } = []; | ||
private Dictionary<string, CachedIcon> AssemblyTextureCache { get; } = []; | ||
|
||
private static ISharedImmediateTexture GetIconInternal(uint id, bool isHq = false) => | ||
Service.TextureProvider.GetFromGameIcon(new GameIconLookup(id, itemHq: isHq)); | ||
|
||
private static ISharedImmediateTexture GetAssemblyTextureInternal(string filename) => | ||
Service.TextureProvider.GetFromManifestResource(Assembly.GetExecutingAssembly(), $"Craftimizer.{filename}"); | ||
|
||
public static ILoadedTextureIcon GetIcon(uint id, bool isHq = false) => | ||
new LoadedIcon(GetIconInternal(id, isHq)); | ||
|
||
public static ILoadedTextureIcon GetAssemblyTexture(string filename) => | ||
new LoadedIcon(GetAssemblyTextureInternal(filename)); | ||
|
||
public ITextureIcon GetIconCached(uint id, bool isHq = false) | ||
{ | ||
return Service.TextureProvider.GetFromGame(path); | ||
if (IconCache.TryGetValue((id, isHq), out var icon)) | ||
return icon; | ||
return IconCache[(id, isHq)] = new(GetIconInternal(id, isHq)); | ||
} | ||
|
||
public static ISharedImmediateTexture GetAssemblyTexture(string filename) | ||
public ITextureIcon GetAssemblyTextureCached(string filename) | ||
{ | ||
return Service.TextureProvider.GetFromManifestResource(Assembly.GetExecutingAssembly(), $"Craftimizer.{filename}"); | ||
if (AssemblyTextureCache.TryGetValue(filename, out var texture)) | ||
return texture; | ||
return AssemblyTextureCache[filename] = new(GetAssemblyTextureInternal(filename)); | ||
} | ||
|
||
public static nint GetHandle(this ISharedImmediateTexture me) => | ||
me.GetWrapOrEmpty().ImGuiHandle; | ||
public void Dispose() | ||
{ | ||
foreach (var value in IconCache.Values) | ||
value.Release(); | ||
foreach (var value in AssemblyTextureCache.Values) | ||
value.Release(); | ||
} | ||
} |
Oops, something went wrong.
716fa55
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
Craftimizer.Benchmark.Bench.Solve(State: 7BAA81C9, Config: B4739BA6)
103533140
ns (± 792318.6109135642
)Craftimizer.Benchmark.Bench.Solve(State: 9840DD62, Config: B4739BA6)
92944338.88888887
ns (± 960207.989780114
)This comment was automatically generated by workflow using github-action-benchmark.