mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-20 23:07:43 +01:00
Add texture leak tracker
This commit is contained in:
parent
2572f24e08
commit
6a0f774625
14 changed files with 597 additions and 63 deletions
|
|
@ -28,6 +28,8 @@ internal sealed class FileSystemSharedImmediateTexture : SharedImmediateTexture
|
|||
protected override async Task<IDalamudTextureWrap> CreateTextureAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var tm = await Service<TextureManager>.GetAsync();
|
||||
return await tm.NoThrottleCreateFromFileAsync(this.path, cancellationToken);
|
||||
var wrap = await tm.NoThrottleCreateFromFileAsync(this.path, cancellationToken);
|
||||
tm.BlameSetName(wrap, this.ToString());
|
||||
return wrap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ internal sealed class GamePathSharedImmediateTexture : SharedImmediateTexture
|
|||
if (dm.GetFile<TexFile>(substPath) is not { } file)
|
||||
throw new FileNotFoundException();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return tm.NoThrottleCreateFromTexFile(file);
|
||||
var wrap = tm.NoThrottleCreateFromTexFile(file);
|
||||
tm.BlameSetName(wrap, this.ToString());
|
||||
return wrap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ internal sealed class ManifestResourceSharedImmediateTexture : SharedImmediateTe
|
|||
var tm = await Service<TextureManager>.GetAsync();
|
||||
var ms = new MemoryStream(stream.CanSeek ? checked((int)stream.Length) : 0);
|
||||
await stream.CopyToAsync(ms, cancellationToken);
|
||||
return tm.NoThrottleCreateFromImage(ms.GetBuffer().AsMemory(0, checked((int)ms.Length)), cancellationToken);
|
||||
var wrap = tm.NoThrottleCreateFromImage(ms.GetBuffer().AsMemory(0, checked((int)ms.Length)), cancellationToken);
|
||||
tm.BlameSetName(wrap, this.ToString());
|
||||
return wrap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,373 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Dalamud.Interface.Internal;
|
||||
using Dalamud.Plugin.Internal.Types;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Dalamud.Storage.Assets;
|
||||
using Dalamud.Utility;
|
||||
|
||||
using TerraFX.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace Dalamud.Interface.Textures.Internal;
|
||||
|
||||
/// <summary>Service responsible for loading and disposing ImGui texture wraps.</summary>
|
||||
internal sealed partial class TextureManager
|
||||
{
|
||||
private readonly List<BlameTag> blameTracker = new();
|
||||
|
||||
/// <summary>A wrapper for underlying texture2D resources.</summary>
|
||||
public interface IBlameableDalamudTextureWrap : IDalamudTextureWrap
|
||||
{
|
||||
/// <summary>Gets the name of the underlying resource of this texture wrap.</summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>Gets the format of the texture.</summary>
|
||||
public DXGI_FORMAT Format { get; }
|
||||
|
||||
/// <summary>Gets the list of owner plugins.</summary>
|
||||
public List<LocalPlugin> OwnerPlugins { get; }
|
||||
}
|
||||
|
||||
/// <summary>Gets all the loaded textures from plugins.</summary>
|
||||
/// <returns>The enumerable that goes through all textures and relevant plugins.</returns>
|
||||
/// <remarks>Returned value must be used inside a lock.</remarks>
|
||||
[SuppressMessage("ReSharper", "InconsistentlySynchronizedField", Justification = "Caller locks the return value.")]
|
||||
public IReadOnlyList<IBlameableDalamudTextureWrap> AllBlamesForDebug => this.blameTracker;
|
||||
|
||||
/// <summary>Puts a plugin on blame for a texture.</summary>
|
||||
/// <param name="textureWrap">The texture.</param>
|
||||
/// <param name="ownerPlugin">The plugin.</param>
|
||||
/// <returns>Same <paramref name="textureWrap"/>.</returns>
|
||||
public unsafe IDalamudTextureWrap Blame(IDalamudTextureWrap textureWrap, LocalPlugin? ownerPlugin)
|
||||
{
|
||||
if (!this.dalamudConfiguration.UseTexturePluginTracking)
|
||||
return textureWrap;
|
||||
|
||||
using var wrapAux = new WrapAux(textureWrap, true);
|
||||
var blame = BlameTag.From(wrapAux.ResPtr, out var isNew);
|
||||
|
||||
if (ownerPlugin is not null)
|
||||
{
|
||||
lock (blame.OwnerPlugins)
|
||||
blame.OwnerPlugins.Add(ownerPlugin);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
lock (this.blameTracker)
|
||||
this.blameTracker.Add(blame);
|
||||
}
|
||||
|
||||
return textureWrap;
|
||||
}
|
||||
|
||||
/// <summary>Sets the blame name for a texture.</summary>
|
||||
/// <param name="textureWrap">The texture.</param>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <returns>Same <paramref name="textureWrap"/>.</returns>
|
||||
public unsafe IDalamudTextureWrap BlameSetName(IDalamudTextureWrap textureWrap, string name)
|
||||
{
|
||||
if (!this.dalamudConfiguration.UseTexturePluginTracking)
|
||||
return textureWrap;
|
||||
|
||||
using var wrapAux = new WrapAux(textureWrap, true);
|
||||
var blame = BlameTag.From(wrapAux.ResPtr, out var isNew);
|
||||
blame.Name = name;
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
lock (this.blameTracker)
|
||||
this.blameTracker.Add(blame);
|
||||
}
|
||||
|
||||
return textureWrap;
|
||||
}
|
||||
|
||||
private void BlameTrackerUpdate(IFramework unused)
|
||||
{
|
||||
lock (this.blameTracker)
|
||||
{
|
||||
for (var i = 0; i < this.blameTracker.Count;)
|
||||
{
|
||||
var entry = this.blameTracker[i];
|
||||
if (entry.TestIsReleasedOrShouldRelease())
|
||||
{
|
||||
this.blameTracker[i] = this.blameTracker[^1];
|
||||
this.blameTracker.RemoveAt(this.blameTracker.Count - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A COM object that works by tagging itself to a DirectX resource. When the resource destructs, it will
|
||||
/// also release our instance of the tag, letting us know that it is no longer being used, and can be evicted from
|
||||
/// our tracker.</summary>
|
||||
[Guid("2c3809e4-4f22-4c50-abde-4f22e5120875")]
|
||||
private sealed unsafe class BlameTag : IUnknown.Interface, IRefCountable, IBlameableDalamudTextureWrap
|
||||
{
|
||||
private static readonly Guid MyGuid = typeof(BlameTag).GUID;
|
||||
|
||||
private readonly nint[] comObject;
|
||||
private readonly IUnknown.Vtbl<IUnknown> vtbl;
|
||||
private readonly D3D11_TEXTURE2D_DESC desc;
|
||||
|
||||
private ID3D11Texture2D* tex2D;
|
||||
private GCHandle gchThis;
|
||||
private GCHandle gchComObject;
|
||||
private GCHandle gchVtbl;
|
||||
private int refCount;
|
||||
|
||||
private ComPtr<ID3D11ShaderResourceView> srvDebugPreview;
|
||||
private long srvDebugPreviewExpiryTick;
|
||||
|
||||
private BlameTag(IUnknown* trackWhat)
|
||||
{
|
||||
try
|
||||
{
|
||||
fixed (Guid* piid = &IID.IID_ID3D11Texture2D)
|
||||
fixed (ID3D11Texture2D** ppTex2D = &this.tex2D)
|
||||
trackWhat->QueryInterface(piid, (void**)ppTex2D).ThrowOnError();
|
||||
|
||||
fixed (D3D11_TEXTURE2D_DESC* pDesc = &this.desc)
|
||||
this.tex2D->GetDesc(pDesc);
|
||||
|
||||
this.comObject = new nint[2];
|
||||
|
||||
this.vtbl.QueryInterface = &QueryInterfaceStatic;
|
||||
this.vtbl.AddRef = &AddRefStatic;
|
||||
this.vtbl.Release = &ReleaseStatic;
|
||||
|
||||
this.gchThis = GCHandle.Alloc(this);
|
||||
this.gchVtbl = GCHandle.Alloc(this.vtbl, GCHandleType.Pinned);
|
||||
this.gchComObject = GCHandle.Alloc(this.comObject, GCHandleType.Pinned);
|
||||
this.comObject[0] = this.gchVtbl.AddrOfPinnedObject();
|
||||
this.comObject[1] = GCHandle.ToIntPtr(this.gchThis);
|
||||
this.refCount = 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
this.refCount = 0;
|
||||
if (this.gchComObject.IsAllocated)
|
||||
this.gchComObject.Free();
|
||||
if (this.gchVtbl.IsAllocated)
|
||||
this.gchVtbl.Free();
|
||||
if (this.gchThis.IsAllocated)
|
||||
this.gchThis.Free();
|
||||
this.tex2D->Release();
|
||||
throw;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
fixed (Guid* pMyGuid = &MyGuid)
|
||||
this.tex2D->SetPrivateDataInterface(pMyGuid, this).ThrowOnError();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// We don't own this.
|
||||
this.tex2D->Release();
|
||||
|
||||
// If the try block above failed, then we will dispose ourselves right away.
|
||||
// Otherwise, we are transferring our ownership to the device child tagging system.
|
||||
this.Release();
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
static int QueryInterfaceStatic(IUnknown* pThis, Guid* riid, void** ppvObject) =>
|
||||
ToManagedObject(pThis)?.QueryInterface(riid, ppvObject) ?? E.E_UNEXPECTED;
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
static uint AddRefStatic(IUnknown* pThis) => (uint)(ToManagedObject(pThis)?.AddRef() ?? 0);
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
static uint ReleaseStatic(IUnknown* pThis) => (uint)(ToManagedObject(pThis)?.Release() ?? 0);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="INativeGuid.NativeGuid"/>
|
||||
public static Guid* NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in MyGuid));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public List<LocalPlugin> OwnerPlugins { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Name { get; set; } = "<unnamed>";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public DXGI_FORMAT Format => this.desc.Format;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IntPtr ImGuiHandle
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.refCount == 0)
|
||||
return Service<DalamudAssetManager>.Get().Empty4X4.ImGuiHandle;
|
||||
|
||||
this.srvDebugPreviewExpiryTick = Environment.TickCount64 + 1000;
|
||||
if (!this.srvDebugPreview.IsEmpty())
|
||||
return (nint)this.srvDebugPreview.Get();
|
||||
var srvDesc = new D3D11_SHADER_RESOURCE_VIEW_DESC(
|
||||
this.tex2D,
|
||||
D3D_SRV_DIMENSION.D3D11_SRV_DIMENSION_TEXTURE2D);
|
||||
|
||||
using var device = default(ComPtr<ID3D11Device>);
|
||||
this.tex2D->GetDevice(device.GetAddressOf());
|
||||
|
||||
using var srv = default(ComPtr<ID3D11ShaderResourceView>);
|
||||
if (device.Get()->CreateShaderResourceView((ID3D11Resource*)this.tex2D, &srvDesc, srv.GetAddressOf())
|
||||
.FAILED)
|
||||
return Service<DalamudAssetManager>.Get().Empty4X4.ImGuiHandle;
|
||||
|
||||
srv.Swap(ref this.srvDebugPreview);
|
||||
return (nint)this.srvDebugPreview.Get();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Width => (int)this.desc.Width;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Height => (int)this.desc.Height;
|
||||
|
||||
public static implicit operator IUnknown*(BlameTag bt) => (IUnknown*)bt.gchComObject.AddrOfPinnedObject();
|
||||
|
||||
/// <summary>Gets or creates an instance of <see cref="BlameTag"/> for the given resource.</summary>
|
||||
/// <param name="trackWhat">The COM object to track.</param>
|
||||
/// <param name="isNew"><c>true</c> if the tracker is new.</param>
|
||||
/// <typeparam name="T">A COM object type.</typeparam>
|
||||
/// <returns>A new instance of <see cref="BlameTag"/>.</returns>
|
||||
public static BlameTag From<T>(T* trackWhat, out bool isNew) where T : unmanaged, IUnknown.Interface
|
||||
{
|
||||
using var deviceChild = default(ComPtr<ID3D11DeviceChild>);
|
||||
fixed (Guid* piid = &IID.IID_ID3D11DeviceChild)
|
||||
trackWhat->QueryInterface(piid, (void**)deviceChild.GetAddressOf()).ThrowOnError();
|
||||
|
||||
fixed (Guid* pMyGuid = &MyGuid)
|
||||
{
|
||||
var dataSize = (uint)sizeof(nint);
|
||||
IUnknown* existingTag;
|
||||
if (deviceChild.Get()->GetPrivateData(pMyGuid, &dataSize, &existingTag).SUCCEEDED)
|
||||
{
|
||||
if (ToManagedObject(existingTag) is { } existingTagInstance)
|
||||
{
|
||||
existingTagInstance.Release();
|
||||
isNew = false;
|
||||
return existingTagInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isNew = true;
|
||||
return new((IUnknown*)trackWhat);
|
||||
}
|
||||
|
||||
/// <summary>Tests whether the tag and the underlying resource are released or should be released.</summary>
|
||||
/// <returns><c>true</c> if there are no more remaining references to this instance.</returns>
|
||||
public bool TestIsReleasedOrShouldRelease()
|
||||
{
|
||||
if (this.srvDebugPreviewExpiryTick <= Environment.TickCount64)
|
||||
this.srvDebugPreview.Reset();
|
||||
|
||||
return this.refCount == 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public HRESULT QueryInterface(Guid* riid, void** ppvObject)
|
||||
{
|
||||
if (ppvObject == null)
|
||||
return E.E_POINTER;
|
||||
|
||||
if (*riid == IID.IID_IUnknown ||
|
||||
*riid == MyGuid)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.AddRef();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return E.E_FAIL;
|
||||
}
|
||||
|
||||
*ppvObject = (IUnknown*)this;
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
*ppvObject = null;
|
||||
return E.E_NOINTERFACE;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int AddRef() => IRefCountable.AlterRefCount(1, ref this.refCount, out var newRefCount) switch
|
||||
{
|
||||
IRefCountable.RefCountResult.StillAlive => newRefCount,
|
||||
IRefCountable.RefCountResult.AlreadyDisposed => throw new ObjectDisposedException(nameof(BlameTag)),
|
||||
IRefCountable.RefCountResult.FinalRelease => throw new InvalidOperationException(),
|
||||
_ => throw new InvalidOperationException(),
|
||||
};
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Release()
|
||||
{
|
||||
switch (IRefCountable.AlterRefCount(-1, ref this.refCount, out var newRefCount))
|
||||
{
|
||||
case IRefCountable.RefCountResult.StillAlive:
|
||||
return newRefCount;
|
||||
|
||||
case IRefCountable.RefCountResult.FinalRelease:
|
||||
this.gchThis.Free();
|
||||
this.gchComObject.Free();
|
||||
this.gchVtbl.Free();
|
||||
return newRefCount;
|
||||
|
||||
case IRefCountable.RefCountResult.AlreadyDisposed:
|
||||
throw new ObjectDisposedException(nameof(BlameTag));
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
uint IUnknown.Interface.AddRef()
|
||||
{
|
||||
try
|
||||
{
|
||||
return (uint)this.AddRef();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
uint IUnknown.Interface.Release()
|
||||
{
|
||||
this.srvDebugPreviewExpiryTick = 0;
|
||||
try
|
||||
{
|
||||
return (uint)this.Release();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static BlameTag? ToManagedObject(void* pThis) =>
|
||||
GCHandle.FromIntPtr(((nint*)pThis)[1]).Target as BlameTag;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
|
||||
using Dalamud.Interface.Internal;
|
||||
using Dalamud.Plugin.Internal.Types;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Dalamud.Utility;
|
||||
|
||||
|
|
@ -71,23 +72,34 @@ internal sealed partial class TextureManager
|
|||
|
||||
var desc = default(D3D11_TEXTURE2D_DESC);
|
||||
tex.Get()->GetDesc(&desc);
|
||||
return new UnknownTextureWrap(
|
||||
|
||||
var outWrap = new UnknownTextureWrap(
|
||||
(IUnknown*)srv.Get(),
|
||||
(int)desc.Width,
|
||||
(int)desc.Height,
|
||||
true);
|
||||
this.BlameSetName(
|
||||
outWrap,
|
||||
$"{nameof(this.CreateFromExistingTextureAsync)}({nameof(wrap)}, {nameof(args)}, {nameof(leaveWrapOpen)}, {nameof(cancellationToken)})");
|
||||
return outWrap;
|
||||
}
|
||||
},
|
||||
cancellationToken,
|
||||
leaveWrapOpen ? null : wrap);
|
||||
|
||||
/// <inheritdoc/>
|
||||
Task<IDalamudTextureWrap> ITextureProvider.CreateFromImGuiViewportAsync(
|
||||
ImGuiViewportTextureArgs args,
|
||||
CancellationToken cancellationToken) => this.CreateFromImGuiViewportAsync(args, null, cancellationToken);
|
||||
|
||||
/// <inheritdoc cref="ITextureProvider.CreateFromImGuiViewportAsync"/>
|
||||
public Task<IDalamudTextureWrap> CreateFromImGuiViewportAsync(
|
||||
ImGuiViewportTextureArgs args,
|
||||
LocalPlugin? ownerPlugin,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
args.ThrowOnInvalidValues();
|
||||
var t = new ViewportTextureWrap(args, cancellationToken);
|
||||
var t = new ViewportTextureWrap(args, ownerPlugin, cancellationToken);
|
||||
t.QueueUpdate();
|
||||
return t.FirstUpdateTask;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
|
@ -11,7 +9,6 @@ using Dalamud.Game;
|
|||
using Dalamud.Interface.Internal;
|
||||
using Dalamud.Interface.Textures.Internal.SharedImmediateTextures;
|
||||
using Dalamud.Logging.Internal;
|
||||
using Dalamud.Plugin.Internal.Types;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Dalamud.Utility;
|
||||
|
||||
|
|
@ -64,6 +61,8 @@ internal sealed partial class TextureManager
|
|||
failsafe.Add(this.sharedTextureManager = new(this));
|
||||
failsafe.Add(this.wicManager = new(this));
|
||||
failsafe.Add(this.simpleDrawer = new());
|
||||
this.framework.Update += this.BlameTrackerUpdate;
|
||||
failsafe.Add(() => this.framework.Update -= this.BlameTrackerUpdate);
|
||||
this.simpleDrawer.Setup(this.device.Get());
|
||||
|
||||
failsafe.Cancel();
|
||||
|
|
@ -116,21 +115,18 @@ internal sealed partial class TextureManager
|
|||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>Puts a plugin on blame for a texture.</summary>
|
||||
/// <param name="textureWrap">The texture.</param>
|
||||
/// <param name="ownerPlugin">The plugin.</param>
|
||||
public void Blame(IDalamudTextureWrap textureWrap, LocalPlugin ownerPlugin)
|
||||
{
|
||||
// nop for now
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IDalamudTextureWrap> CreateFromImageAsync(
|
||||
ReadOnlyMemory<byte> bytes,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
this.DynamicPriorityTextureLoader.LoadAsync(
|
||||
null,
|
||||
ct => Task.Run(() => this.NoThrottleCreateFromImage(bytes.ToArray(), ct), ct),
|
||||
ct => Task.Run(
|
||||
() =>
|
||||
this.BlameSetName(
|
||||
this.NoThrottleCreateFromImage(bytes.ToArray(), ct),
|
||||
$"{nameof(this.CreateFromImageAsync)}({nameof(bytes)}, {nameof(cancellationToken)})"),
|
||||
ct),
|
||||
cancellationToken);
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
|
@ -144,7 +140,9 @@ internal sealed partial class TextureManager
|
|||
{
|
||||
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
||||
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
||||
return this.NoThrottleCreateFromImage(ms.GetBuffer(), ct);
|
||||
return this.BlameSetName(
|
||||
this.NoThrottleCreateFromImage(ms.GetBuffer(), ct),
|
||||
$"{nameof(this.CreateFromImageAsync)}({nameof(stream)}, {nameof(leaveOpen)}, {nameof(cancellationToken)})");
|
||||
},
|
||||
cancellationToken,
|
||||
leaveOpen ? null : stream);
|
||||
|
|
@ -153,7 +151,10 @@ internal sealed partial class TextureManager
|
|||
// It probably doesn't make sense to throttle this, as it copies the passed bytes to GPU without any transformation.
|
||||
public IDalamudTextureWrap CreateFromRaw(
|
||||
RawImageSpecification specs,
|
||||
ReadOnlySpan<byte> bytes) => this.NoThrottleCreateFromRaw(specs, bytes);
|
||||
ReadOnlySpan<byte> bytes) =>
|
||||
this.BlameSetName(
|
||||
this.NoThrottleCreateFromRaw(specs, bytes),
|
||||
$"{nameof(this.CreateFromRaw)}({nameof(specs)}, {nameof(bytes)})");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IDalamudTextureWrap> CreateFromRawAsync(
|
||||
|
|
@ -162,7 +163,10 @@ internal sealed partial class TextureManager
|
|||
CancellationToken cancellationToken = default) =>
|
||||
this.DynamicPriorityTextureLoader.LoadAsync(
|
||||
null,
|
||||
_ => Task.FromResult(this.NoThrottleCreateFromRaw(specs, bytes.Span)),
|
||||
_ => Task.FromResult(
|
||||
this.BlameSetName(
|
||||
this.NoThrottleCreateFromRaw(specs, bytes.Span),
|
||||
$"{nameof(this.CreateFromRawAsync)}({nameof(specs)}, {nameof(bytes)}, {nameof(cancellationToken)})")),
|
||||
cancellationToken);
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
|
@ -177,13 +181,18 @@ internal sealed partial class TextureManager
|
|||
{
|
||||
await using var ms = stream.CanSeek ? new MemoryStream((int)stream.Length) : new();
|
||||
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
|
||||
return this.NoThrottleCreateFromRaw(specs, ms.GetBuffer().AsSpan(0, (int)ms.Length));
|
||||
return this.BlameSetName(
|
||||
this.NoThrottleCreateFromRaw(specs, ms.GetBuffer().AsSpan(0, (int)ms.Length)),
|
||||
$"{nameof(this.CreateFromRawAsync)}({nameof(specs)}, {nameof(stream)}, {nameof(leaveOpen)}, {nameof(cancellationToken)})");
|
||||
},
|
||||
cancellationToken,
|
||||
leaveOpen ? null : stream);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IDalamudTextureWrap CreateFromTexFile(TexFile file) => this.CreateFromTexFileAsync(file).Result;
|
||||
public IDalamudTextureWrap CreateFromTexFile(TexFile file) =>
|
||||
this.BlameSetName(
|
||||
this.CreateFromTexFileAsync(file).Result,
|
||||
$"{nameof(this.CreateFromTexFile)}({nameof(file)})");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IDalamudTextureWrap> CreateFromTexFileAsync(
|
||||
|
|
@ -191,7 +200,10 @@ internal sealed partial class TextureManager
|
|||
CancellationToken cancellationToken = default) =>
|
||||
this.DynamicPriorityTextureLoader.LoadAsync(
|
||||
null,
|
||||
_ => Task.FromResult(this.NoThrottleCreateFromTexFile(file)),
|
||||
_ => Task.FromResult(
|
||||
this.BlameSetName(
|
||||
this.NoThrottleCreateFromTexFile(file),
|
||||
$"{nameof(this.CreateFromTexFile)}({nameof(file)})")),
|
||||
cancellationToken);
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
|
@ -244,7 +256,9 @@ internal sealed partial class TextureManager
|
|||
this.device.Get()->CreateShaderResourceView((ID3D11Resource*)texture.Get(), &viewDesc, view.GetAddressOf())
|
||||
.ThrowOnError();
|
||||
|
||||
return new UnknownTextureWrap((IUnknown*)view.Get(), specs.Width, specs.Height, true);
|
||||
var wrap = new UnknownTextureWrap((IUnknown*)view.Get(), specs.Width, specs.Height, true);
|
||||
this.BlameSetName(wrap, $"{nameof(this.NoThrottleCreateFromRaw)}({nameof(specs)}, {nameof(bytes)})");
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/// <summary>Creates a texture from the given <see cref="TexFile"/>. Skips the load throttler; intended to be used
|
||||
|
|
@ -264,9 +278,9 @@ internal sealed partial class TextureManager
|
|||
buffer = buffer.Filter(0, 0, TexFile.TextureFormat.B8G8R8A8);
|
||||
}
|
||||
|
||||
return this.NoThrottleCreateFromRaw(
|
||||
new(buffer.Width, buffer.Height, dxgiFormat),
|
||||
buffer.RawData);
|
||||
var wrap = this.NoThrottleCreateFromRaw(new(buffer.Width, buffer.Height, dxgiFormat), buffer.RawData);
|
||||
this.BlameSetName(wrap, $"{nameof(this.NoThrottleCreateFromTexFile)}({nameof(file)})");
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/// <summary>Creates a texture from the given <paramref name="fileBytes"/>, trying to interpret it as a
|
||||
|
|
@ -289,7 +303,10 @@ internal sealed partial class TextureManager
|
|||
tf,
|
||||
new object?[] { new LuminaBinaryReader(bytesArray) });
|
||||
// Note: FileInfo and FilePath are not used from TexFile; skip it.
|
||||
return this.NoThrottleCreateFromTexFile(tf);
|
||||
|
||||
var wrap = this.NoThrottleCreateFromTexFile(tf);
|
||||
this.BlameSetName(wrap, $"{nameof(this.NoThrottleCreateFromTexFile)}({nameof(fileBytes)})");
|
||||
return wrap;
|
||||
}
|
||||
|
||||
private void ReleaseUnmanagedResources() => this.device.Reset();
|
||||
|
|
|
|||
|
|
@ -40,8 +40,10 @@ internal sealed partial class TextureManagerPluginScoped
|
|||
|
||||
private Task<TextureManager>? managerTaskNullable;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="TextureManagerPluginScoped"/> class.</summary>
|
||||
/// <param name="plugin">The plugin.</param>
|
||||
[ServiceManager.ServiceConstructor]
|
||||
private TextureManagerPluginScoped(LocalPlugin plugin)
|
||||
public TextureManagerPluginScoped(LocalPlugin plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
if (plugin.Manifest is LocalPluginManifest lpm)
|
||||
|
|
@ -155,7 +157,7 @@ internal sealed partial class TextureManagerPluginScoped
|
|||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var manager = await this.ManagerTask;
|
||||
var textureWrap = await manager.CreateFromImGuiViewportAsync(args, cancellationToken);
|
||||
var textureWrap = await manager.CreateFromImGuiViewportAsync(args, this.plugin, cancellationToken);
|
||||
manager.Blame(textureWrap, this.plugin);
|
||||
return textureWrap;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ using System.Threading.Tasks;
|
|||
|
||||
using Dalamud.Game;
|
||||
using Dalamud.Interface.Internal;
|
||||
using Dalamud.Plugin.Internal.Types;
|
||||
using Dalamud.Storage.Assets;
|
||||
using Dalamud.Utility;
|
||||
|
||||
using ImGuiNET;
|
||||
|
|
@ -18,6 +20,7 @@ namespace Dalamud.Interface.Textures.Internal;
|
|||
/// <summary>A texture wrap that takes its buffer from the frame buffer (of swap chain).</summary>
|
||||
internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDisposable
|
||||
{
|
||||
private readonly LocalPlugin? ownerPlugin;
|
||||
private readonly CancellationToken cancellationToken;
|
||||
private readonly TaskCompletionSource<IDalamudTextureWrap> firstUpdateTaskCompletionSource = new();
|
||||
|
||||
|
|
@ -31,10 +34,13 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
|
|||
|
||||
/// <summary>Initializes a new instance of the <see cref="ViewportTextureWrap"/> class.</summary>
|
||||
/// <param name="args">The arguments for creating a texture.</param>
|
||||
/// <param name="ownerPlugin">The owner plugin.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
public ViewportTextureWrap(ImGuiViewportTextureArgs args, CancellationToken cancellationToken)
|
||||
public ViewportTextureWrap(
|
||||
ImGuiViewportTextureArgs args, LocalPlugin? ownerPlugin, CancellationToken cancellationToken)
|
||||
{
|
||||
this.args = args;
|
||||
this.ownerPlugin = ownerPlugin;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +48,14 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
|
|||
~ViewportTextureWrap() => this.Dispose(false);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public unsafe nint ImGuiHandle => (nint)this.srv.Get();
|
||||
public unsafe nint ImGuiHandle
|
||||
{
|
||||
get
|
||||
{
|
||||
var t = (nint)this.srv.Get();
|
||||
return t == nint.Zero ? Service<DalamudAssetManager>.Get().Empty4X4.ImGuiHandle : t;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Width => (int)this.desc.Width;
|
||||
|
|
@ -134,6 +147,11 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
|
|||
srvTemp.Swap(ref this.srv);
|
||||
rtvTemp.Swap(ref this.rtv);
|
||||
texTemp.Swap(ref this.tex);
|
||||
|
||||
Service<TextureManager>.Get().Blame(this, this.ownerPlugin);
|
||||
Service<TextureManager>.Get().BlameSetName(
|
||||
this,
|
||||
$"{nameof(ViewportTextureWrap)}({this.args})");
|
||||
}
|
||||
|
||||
// context.Get()->CopyResource((ID3D11Resource*)this.tex.Get(), (ID3D11Resource*)backBuffer.Get());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue